2012-07-06 Tom de Vries <tom@codesourcery.com>
[official-gcc.git] / gcc / tree-ssa-structalias.c
blobcb3e54b1110e73a82f3d3a82a637b667835eaebc
1 /* Tree based points-to analysis
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Daniel Berlin <dberlin@dberlin.org>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "ggc.h"
27 #include "obstack.h"
28 #include "bitmap.h"
29 #include "flags.h"
30 #include "basic-block.h"
31 #include "tree.h"
32 #include "tree-flow.h"
33 #include "tree-inline.h"
34 #include "diagnostic-core.h"
35 #include "gimple.h"
36 #include "hashtab.h"
37 #include "function.h"
38 #include "cgraph.h"
39 #include "tree-pass.h"
40 #include "timevar.h"
41 #include "alloc-pool.h"
42 #include "splay-tree.h"
43 #include "params.h"
44 #include "cgraph.h"
45 #include "alias.h"
46 #include "pointer-set.h"
48 /* The idea behind this analyzer is to generate set constraints from the
49 program, then solve the resulting constraints in order to generate the
50 points-to sets.
52 Set constraints are a way of modeling program analysis problems that
53 involve sets. They consist of an inclusion constraint language,
54 describing the variables (each variable is a set) and operations that
55 are involved on the variables, and a set of rules that derive facts
56 from these operations. To solve a system of set constraints, you derive
57 all possible facts under the rules, which gives you the correct sets
58 as a consequence.
60 See "Efficient Field-sensitive pointer analysis for C" by "David
61 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
62 http://citeseer.ist.psu.edu/pearce04efficient.html
64 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
65 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
66 http://citeseer.ist.psu.edu/heintze01ultrafast.html
68 There are three types of real constraint expressions, DEREF,
69 ADDRESSOF, and SCALAR. Each constraint expression consists
70 of a constraint type, a variable, and an offset.
72 SCALAR is a constraint expression type used to represent x, whether
73 it appears on the LHS or the RHS of a statement.
74 DEREF is a constraint expression type used to represent *x, whether
75 it appears on the LHS or the RHS of a statement.
76 ADDRESSOF is a constraint expression used to represent &x, whether
77 it appears on the LHS or the RHS of a statement.
79 Each pointer variable in the program is assigned an integer id, and
80 each field of a structure variable is assigned an integer id as well.
82 Structure variables are linked to their list of fields through a "next
83 field" in each variable that points to the next field in offset
84 order.
85 Each variable for a structure field has
87 1. "size", that tells the size in bits of that field.
88 2. "fullsize, that tells the size in bits of the entire structure.
89 3. "offset", that tells the offset in bits from the beginning of the
90 structure to this field.
92 Thus,
93 struct f
95 int a;
96 int b;
97 } foo;
98 int *bar;
100 looks like
102 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
103 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
104 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
107 In order to solve the system of set constraints, the following is
108 done:
110 1. Each constraint variable x has a solution set associated with it,
111 Sol(x).
113 2. Constraints are separated into direct, copy, and complex.
114 Direct constraints are ADDRESSOF constraints that require no extra
115 processing, such as P = &Q
116 Copy constraints are those of the form P = Q.
117 Complex constraints are all the constraints involving dereferences
118 and offsets (including offsetted copies).
120 3. All direct constraints of the form P = &Q are processed, such
121 that Q is added to Sol(P)
123 4. All complex constraints for a given constraint variable are stored in a
124 linked list attached to that variable's node.
126 5. A directed graph is built out of the copy constraints. Each
127 constraint variable is a node in the graph, and an edge from
128 Q to P is added for each copy constraint of the form P = Q
130 6. The graph is then walked, and solution sets are
131 propagated along the copy edges, such that an edge from Q to P
132 causes Sol(P) <- Sol(P) union Sol(Q).
134 7. As we visit each node, all complex constraints associated with
135 that node are processed by adding appropriate copy edges to the graph, or the
136 appropriate variables to the solution set.
138 8. The process of walking the graph is iterated until no solution
139 sets change.
141 Prior to walking the graph in steps 6 and 7, We perform static
142 cycle elimination on the constraint graph, as well
143 as off-line variable substitution.
145 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
146 on and turned into anything), but isn't. You can just see what offset
147 inside the pointed-to struct it's going to access.
149 TODO: Constant bounded arrays can be handled as if they were structs of the
150 same number of elements.
152 TODO: Modeling heap and incoming pointers becomes much better if we
153 add fields to them as we discover them, which we could do.
155 TODO: We could handle unions, but to be honest, it's probably not
156 worth the pain or slowdown. */
158 /* IPA-PTA optimizations possible.
160 When the indirect function called is ANYTHING we can add disambiguation
161 based on the function signatures (or simply the parameter count which
162 is the varinfo size). We also do not need to consider functions that
163 do not have their address taken.
165 The is_global_var bit which marks escape points is overly conservative
166 in IPA mode. Split it to is_escape_point and is_global_var - only
167 externally visible globals are escape points in IPA mode. This is
168 also needed to fix the pt_solution_includes_global predicate
169 (and thus ptr_deref_may_alias_global_p).
171 The way we introduce DECL_PT_UID to avoid fixing up all points-to
172 sets in the translation unit when we copy a DECL during inlining
173 pessimizes precision. The advantage is that the DECL_PT_UID keeps
174 compile-time and memory usage overhead low - the points-to sets
175 do not grow or get unshared as they would during a fixup phase.
176 An alternative solution is to delay IPA PTA until after all
177 inlining transformations have been applied.
179 The way we propagate clobber/use information isn't optimized.
180 It should use a new complex constraint that properly filters
181 out local variables of the callee (though that would make
182 the sets invalid after inlining). OTOH we might as well
183 admit defeat to WHOPR and simply do all the clobber/use analysis
184 and propagation after PTA finished but before we threw away
185 points-to information for memory variables. WHOPR and PTA
186 do not play along well anyway - the whole constraint solving
187 would need to be done in WPA phase and it will be very interesting
188 to apply the results to local SSA names during LTRANS phase.
190 We probably should compute a per-function unit-ESCAPE solution
191 propagating it simply like the clobber / uses solutions. The
192 solution can go alongside the non-IPA espaced solution and be
193 used to query which vars escape the unit through a function.
195 We never put function decls in points-to sets so we do not
196 keep the set of called functions for indirect calls.
198 And probably more. */
200 static bool use_field_sensitive = true;
201 static int in_ipa_mode = 0;
203 /* Used for predecessor bitmaps. */
204 static bitmap_obstack predbitmap_obstack;
206 /* Used for points-to sets. */
207 static bitmap_obstack pta_obstack;
209 /* Used for oldsolution members of variables. */
210 static bitmap_obstack oldpta_obstack;
212 /* Used for per-solver-iteration bitmaps. */
213 static bitmap_obstack iteration_obstack;
215 static unsigned int create_variable_info_for (tree, const char *);
216 typedef struct constraint_graph *constraint_graph_t;
217 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
219 struct constraint;
220 typedef struct constraint *constraint_t;
222 DEF_VEC_P(constraint_t);
223 DEF_VEC_ALLOC_P(constraint_t,heap);
225 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
226 if (a) \
227 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
229 static struct constraint_stats
231 unsigned int total_vars;
232 unsigned int nonpointer_vars;
233 unsigned int unified_vars_static;
234 unsigned int unified_vars_dynamic;
235 unsigned int iterations;
236 unsigned int num_edges;
237 unsigned int num_implicit_edges;
238 unsigned int points_to_sets_created;
239 } stats;
241 struct variable_info
243 /* ID of this variable */
244 unsigned int id;
246 /* True if this is a variable created by the constraint analysis, such as
247 heap variables and constraints we had to break up. */
248 unsigned int is_artificial_var : 1;
250 /* True if this is a special variable whose solution set should not be
251 changed. */
252 unsigned int is_special_var : 1;
254 /* True for variables whose size is not known or variable. */
255 unsigned int is_unknown_size_var : 1;
257 /* True for (sub-)fields that represent a whole variable. */
258 unsigned int is_full_var : 1;
260 /* True if this is a heap variable. */
261 unsigned int is_heap_var : 1;
263 /* True if this field may contain pointers. */
264 unsigned int may_have_pointers : 1;
266 /* True if this field has only restrict qualified pointers. */
267 unsigned int only_restrict_pointers : 1;
269 /* True if this represents a global variable. */
270 unsigned int is_global_var : 1;
272 /* True if this represents a IPA function info. */
273 unsigned int is_fn_info : 1;
275 /* A link to the variable for the next field in this structure. */
276 struct variable_info *next;
278 /* Offset of this variable, in bits, from the base variable */
279 unsigned HOST_WIDE_INT offset;
281 /* Size of the variable, in bits. */
282 unsigned HOST_WIDE_INT size;
284 /* Full size of the base variable, in bits. */
285 unsigned HOST_WIDE_INT fullsize;
287 /* Name of this variable */
288 const char *name;
290 /* Tree that this variable is associated with. */
291 tree decl;
293 /* Points-to set for this variable. */
294 bitmap solution;
296 /* Old points-to set for this variable. */
297 bitmap oldsolution;
299 typedef struct variable_info *varinfo_t;
301 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
302 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
303 unsigned HOST_WIDE_INT);
304 static varinfo_t lookup_vi_for_tree (tree);
305 static inline bool type_can_have_subvars (const_tree);
307 /* Pool of variable info structures. */
308 static alloc_pool variable_info_pool;
310 DEF_VEC_P(varinfo_t);
312 DEF_VEC_ALLOC_P(varinfo_t, heap);
314 /* Table of variable info structures for constraint variables.
315 Indexed directly by variable info id. */
316 static VEC(varinfo_t,heap) *varmap;
318 /* Return the varmap element N */
320 static inline varinfo_t
321 get_varinfo (unsigned int n)
323 return VEC_index (varinfo_t, varmap, n);
326 /* Static IDs for the special variables. */
327 enum { nothing_id = 0, anything_id = 1, readonly_id = 2,
328 escaped_id = 3, nonlocal_id = 4,
329 storedanything_id = 5, integer_id = 6 };
331 /* Return a new variable info structure consisting for a variable
332 named NAME, and using constraint graph node NODE. Append it
333 to the vector of variable info structures. */
335 static varinfo_t
336 new_var_info (tree t, const char *name)
338 unsigned index = VEC_length (varinfo_t, varmap);
339 varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
341 ret->id = index;
342 ret->name = name;
343 ret->decl = t;
344 /* Vars without decl are artificial and do not have sub-variables. */
345 ret->is_artificial_var = (t == NULL_TREE);
346 ret->is_special_var = false;
347 ret->is_unknown_size_var = false;
348 ret->is_full_var = (t == NULL_TREE);
349 ret->is_heap_var = false;
350 ret->may_have_pointers = true;
351 ret->only_restrict_pointers = false;
352 ret->is_global_var = (t == NULL_TREE);
353 ret->is_fn_info = false;
354 if (t && DECL_P (t))
355 ret->is_global_var = (is_global_var (t)
356 /* We have to treat even local register variables
357 as escape points. */
358 || (TREE_CODE (t) == VAR_DECL
359 && DECL_HARD_REGISTER (t)));
360 ret->solution = BITMAP_ALLOC (&pta_obstack);
361 ret->oldsolution = NULL;
362 ret->next = NULL;
364 stats.total_vars++;
366 VEC_safe_push (varinfo_t, heap, varmap, ret);
368 return ret;
372 /* A map mapping call statements to per-stmt variables for uses
373 and clobbers specific to the call. */
374 struct pointer_map_t *call_stmt_vars;
376 /* Lookup or create the variable for the call statement CALL. */
378 static varinfo_t
379 get_call_vi (gimple call)
381 void **slot_p;
382 varinfo_t vi, vi2;
384 slot_p = pointer_map_insert (call_stmt_vars, call);
385 if (*slot_p)
386 return (varinfo_t) *slot_p;
388 vi = new_var_info (NULL_TREE, "CALLUSED");
389 vi->offset = 0;
390 vi->size = 1;
391 vi->fullsize = 2;
392 vi->is_full_var = true;
394 vi->next = vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED");
395 vi2->offset = 1;
396 vi2->size = 1;
397 vi2->fullsize = 2;
398 vi2->is_full_var = true;
400 *slot_p = (void *) vi;
401 return vi;
404 /* Lookup the variable for the call statement CALL representing
405 the uses. Returns NULL if there is nothing special about this call. */
407 static varinfo_t
408 lookup_call_use_vi (gimple call)
410 void **slot_p;
412 slot_p = pointer_map_contains (call_stmt_vars, call);
413 if (slot_p)
414 return (varinfo_t) *slot_p;
416 return NULL;
419 /* Lookup the variable for the call statement CALL representing
420 the clobbers. Returns NULL if there is nothing special about this call. */
422 static varinfo_t
423 lookup_call_clobber_vi (gimple call)
425 varinfo_t uses = lookup_call_use_vi (call);
426 if (!uses)
427 return NULL;
429 return uses->next;
432 /* Lookup or create the variable for the call statement CALL representing
433 the uses. */
435 static varinfo_t
436 get_call_use_vi (gimple call)
438 return get_call_vi (call);
441 /* Lookup or create the variable for the call statement CALL representing
442 the clobbers. */
444 static varinfo_t ATTRIBUTE_UNUSED
445 get_call_clobber_vi (gimple call)
447 return get_call_vi (call)->next;
451 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
453 /* An expression that appears in a constraint. */
455 struct constraint_expr
457 /* Constraint type. */
458 constraint_expr_type type;
460 /* Variable we are referring to in the constraint. */
461 unsigned int var;
463 /* Offset, in bits, of this constraint from the beginning of
464 variables it ends up referring to.
466 IOW, in a deref constraint, we would deref, get the result set,
467 then add OFFSET to each member. */
468 HOST_WIDE_INT offset;
471 /* Use 0x8000... as special unknown offset. */
472 #define UNKNOWN_OFFSET ((HOST_WIDE_INT)-1 << (HOST_BITS_PER_WIDE_INT-1))
474 typedef struct constraint_expr ce_s;
475 DEF_VEC_O(ce_s);
476 DEF_VEC_ALLOC_O(ce_s, heap);
477 static void get_constraint_for_1 (tree, VEC(ce_s, heap) **, bool, bool);
478 static void get_constraint_for (tree, VEC(ce_s, heap) **);
479 static void get_constraint_for_rhs (tree, VEC(ce_s, heap) **);
480 static void do_deref (VEC (ce_s, heap) **);
482 /* Our set constraints are made up of two constraint expressions, one
483 LHS, and one RHS.
485 As described in the introduction, our set constraints each represent an
486 operation between set valued variables.
488 struct constraint
490 struct constraint_expr lhs;
491 struct constraint_expr rhs;
494 /* List of constraints that we use to build the constraint graph from. */
496 static VEC(constraint_t,heap) *constraints;
497 static alloc_pool constraint_pool;
499 /* The constraint graph is represented as an array of bitmaps
500 containing successor nodes. */
502 struct constraint_graph
504 /* Size of this graph, which may be different than the number of
505 nodes in the variable map. */
506 unsigned int size;
508 /* Explicit successors of each node. */
509 bitmap *succs;
511 /* Implicit predecessors of each node (Used for variable
512 substitution). */
513 bitmap *implicit_preds;
515 /* Explicit predecessors of each node (Used for variable substitution). */
516 bitmap *preds;
518 /* Indirect cycle representatives, or -1 if the node has no indirect
519 cycles. */
520 int *indirect_cycles;
522 /* Representative node for a node. rep[a] == a unless the node has
523 been unified. */
524 unsigned int *rep;
526 /* Equivalence class representative for a label. This is used for
527 variable substitution. */
528 int *eq_rep;
530 /* Pointer equivalence label for a node. All nodes with the same
531 pointer equivalence label can be unified together at some point
532 (either during constraint optimization or after the constraint
533 graph is built). */
534 unsigned int *pe;
536 /* Pointer equivalence representative for a label. This is used to
537 handle nodes that are pointer equivalent but not location
538 equivalent. We can unite these once the addressof constraints
539 are transformed into initial points-to sets. */
540 int *pe_rep;
542 /* Pointer equivalence label for each node, used during variable
543 substitution. */
544 unsigned int *pointer_label;
546 /* Location equivalence label for each node, used during location
547 equivalence finding. */
548 unsigned int *loc_label;
550 /* Pointed-by set for each node, used during location equivalence
551 finding. This is pointed-by rather than pointed-to, because it
552 is constructed using the predecessor graph. */
553 bitmap *pointed_by;
555 /* Points to sets for pointer equivalence. This is *not* the actual
556 points-to sets for nodes. */
557 bitmap *points_to;
559 /* Bitmap of nodes where the bit is set if the node is a direct
560 node. Used for variable substitution. */
561 sbitmap direct_nodes;
563 /* Bitmap of nodes where the bit is set if the node is address
564 taken. Used for variable substitution. */
565 bitmap address_taken;
567 /* Vector of complex constraints for each graph node. Complex
568 constraints are those involving dereferences or offsets that are
569 not 0. */
570 VEC(constraint_t,heap) **complex;
573 static constraint_graph_t graph;
575 /* During variable substitution and the offline version of indirect
576 cycle finding, we create nodes to represent dereferences and
577 address taken constraints. These represent where these start and
578 end. */
579 #define FIRST_REF_NODE (VEC_length (varinfo_t, varmap))
580 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
582 /* Return the representative node for NODE, if NODE has been unioned
583 with another NODE.
584 This function performs path compression along the way to finding
585 the representative. */
587 static unsigned int
588 find (unsigned int node)
590 gcc_assert (node < graph->size);
591 if (graph->rep[node] != node)
592 return graph->rep[node] = find (graph->rep[node]);
593 return node;
596 /* Union the TO and FROM nodes to the TO nodes.
597 Note that at some point in the future, we may want to do
598 union-by-rank, in which case we are going to have to return the
599 node we unified to. */
601 static bool
602 unite (unsigned int to, unsigned int from)
604 gcc_assert (to < graph->size && from < graph->size);
605 if (to != from && graph->rep[from] != to)
607 graph->rep[from] = to;
608 return true;
610 return false;
613 /* Create a new constraint consisting of LHS and RHS expressions. */
615 static constraint_t
616 new_constraint (const struct constraint_expr lhs,
617 const struct constraint_expr rhs)
619 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
620 ret->lhs = lhs;
621 ret->rhs = rhs;
622 return ret;
625 /* Print out constraint C to FILE. */
627 static void
628 dump_constraint (FILE *file, constraint_t c)
630 if (c->lhs.type == ADDRESSOF)
631 fprintf (file, "&");
632 else if (c->lhs.type == DEREF)
633 fprintf (file, "*");
634 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
635 if (c->lhs.offset == UNKNOWN_OFFSET)
636 fprintf (file, " + UNKNOWN");
637 else if (c->lhs.offset != 0)
638 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
639 fprintf (file, " = ");
640 if (c->rhs.type == ADDRESSOF)
641 fprintf (file, "&");
642 else if (c->rhs.type == DEREF)
643 fprintf (file, "*");
644 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
645 if (c->rhs.offset == UNKNOWN_OFFSET)
646 fprintf (file, " + UNKNOWN");
647 else if (c->rhs.offset != 0)
648 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
652 void debug_constraint (constraint_t);
653 void debug_constraints (void);
654 void debug_constraint_graph (void);
655 void debug_solution_for_var (unsigned int);
656 void debug_sa_points_to_info (void);
658 /* Print out constraint C to stderr. */
660 DEBUG_FUNCTION void
661 debug_constraint (constraint_t c)
663 dump_constraint (stderr, c);
664 fprintf (stderr, "\n");
667 /* Print out all constraints to FILE */
669 static void
670 dump_constraints (FILE *file, int from)
672 int i;
673 constraint_t c;
674 for (i = from; VEC_iterate (constraint_t, constraints, i, c); i++)
675 if (c)
677 dump_constraint (file, c);
678 fprintf (file, "\n");
682 /* Print out all constraints to stderr. */
684 DEBUG_FUNCTION void
685 debug_constraints (void)
687 dump_constraints (stderr, 0);
690 /* Print the constraint graph in dot format. */
692 static void
693 dump_constraint_graph (FILE *file)
695 unsigned int i;
697 /* Only print the graph if it has already been initialized: */
698 if (!graph)
699 return;
701 /* Prints the header of the dot file: */
702 fprintf (file, "strict digraph {\n");
703 fprintf (file, " node [\n shape = box\n ]\n");
704 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
705 fprintf (file, "\n // List of nodes and complex constraints in "
706 "the constraint graph:\n");
708 /* The next lines print the nodes in the graph together with the
709 complex constraints attached to them. */
710 for (i = 0; i < graph->size; i++)
712 if (find (i) != i)
713 continue;
714 if (i < FIRST_REF_NODE)
715 fprintf (file, "\"%s\"", get_varinfo (i)->name);
716 else
717 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
718 if (graph->complex[i])
720 unsigned j;
721 constraint_t c;
722 fprintf (file, " [label=\"\\N\\n");
723 for (j = 0; VEC_iterate (constraint_t, graph->complex[i], j, c); ++j)
725 dump_constraint (file, c);
726 fprintf (file, "\\l");
728 fprintf (file, "\"]");
730 fprintf (file, ";\n");
733 /* Go over the edges. */
734 fprintf (file, "\n // Edges in the constraint graph:\n");
735 for (i = 0; i < graph->size; i++)
737 unsigned j;
738 bitmap_iterator bi;
739 if (find (i) != i)
740 continue;
741 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
743 unsigned to = find (j);
744 if (i == to)
745 continue;
746 if (i < FIRST_REF_NODE)
747 fprintf (file, "\"%s\"", get_varinfo (i)->name);
748 else
749 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
750 fprintf (file, " -> ");
751 if (to < FIRST_REF_NODE)
752 fprintf (file, "\"%s\"", get_varinfo (to)->name);
753 else
754 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name);
755 fprintf (file, ";\n");
759 /* Prints the tail of the dot file. */
760 fprintf (file, "}\n");
763 /* Print out the constraint graph to stderr. */
765 DEBUG_FUNCTION void
766 debug_constraint_graph (void)
768 dump_constraint_graph (stderr);
771 /* SOLVER FUNCTIONS
773 The solver is a simple worklist solver, that works on the following
774 algorithm:
776 sbitmap changed_nodes = all zeroes;
777 changed_count = 0;
778 For each node that is not already collapsed:
779 changed_count++;
780 set bit in changed nodes
782 while (changed_count > 0)
784 compute topological ordering for constraint graph
786 find and collapse cycles in the constraint graph (updating
787 changed if necessary)
789 for each node (n) in the graph in topological order:
790 changed_count--;
792 Process each complex constraint associated with the node,
793 updating changed if necessary.
795 For each outgoing edge from n, propagate the solution from n to
796 the destination of the edge, updating changed as necessary.
798 } */
800 /* Return true if two constraint expressions A and B are equal. */
802 static bool
803 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
805 return a.type == b.type && a.var == b.var && a.offset == b.offset;
808 /* Return true if constraint expression A is less than constraint expression
809 B. This is just arbitrary, but consistent, in order to give them an
810 ordering. */
812 static bool
813 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
815 if (a.type == b.type)
817 if (a.var == b.var)
818 return a.offset < b.offset;
819 else
820 return a.var < b.var;
822 else
823 return a.type < b.type;
826 /* Return true if constraint A is less than constraint B. This is just
827 arbitrary, but consistent, in order to give them an ordering. */
829 static bool
830 constraint_less (const constraint_t a, const constraint_t b)
832 if (constraint_expr_less (a->lhs, b->lhs))
833 return true;
834 else if (constraint_expr_less (b->lhs, a->lhs))
835 return false;
836 else
837 return constraint_expr_less (a->rhs, b->rhs);
840 /* Return true if two constraints A and B are equal. */
842 static bool
843 constraint_equal (struct constraint a, struct constraint b)
845 return constraint_expr_equal (a.lhs, b.lhs)
846 && constraint_expr_equal (a.rhs, b.rhs);
850 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
852 static constraint_t
853 constraint_vec_find (VEC(constraint_t,heap) *vec,
854 struct constraint lookfor)
856 unsigned int place;
857 constraint_t found;
859 if (vec == NULL)
860 return NULL;
862 place = VEC_lower_bound (constraint_t, vec, &lookfor, constraint_less);
863 if (place >= VEC_length (constraint_t, vec))
864 return NULL;
865 found = VEC_index (constraint_t, vec, place);
866 if (!constraint_equal (*found, lookfor))
867 return NULL;
868 return found;
871 /* Union two constraint vectors, TO and FROM. Put the result in TO. */
873 static void
874 constraint_set_union (VEC(constraint_t,heap) **to,
875 VEC(constraint_t,heap) **from)
877 int i;
878 constraint_t c;
880 FOR_EACH_VEC_ELT (constraint_t, *from, i, c)
882 if (constraint_vec_find (*to, *c) == NULL)
884 unsigned int place = VEC_lower_bound (constraint_t, *to, c,
885 constraint_less);
886 VEC_safe_insert (constraint_t, heap, *to, place, c);
891 /* Expands the solution in SET to all sub-fields of variables included.
892 Union the expanded result into RESULT. */
894 static void
895 solution_set_expand (bitmap result, bitmap set)
897 bitmap_iterator bi;
898 bitmap vars = NULL;
899 unsigned j;
901 /* In a first pass record all variables we need to add all
902 sub-fields off. This avoids quadratic behavior. */
903 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
905 varinfo_t v = get_varinfo (j);
906 if (v->is_artificial_var
907 || v->is_full_var)
908 continue;
909 v = lookup_vi_for_tree (v->decl);
910 if (vars == NULL)
911 vars = BITMAP_ALLOC (NULL);
912 bitmap_set_bit (vars, v->id);
915 /* In the second pass now do the addition to the solution and
916 to speed up solving add it to the delta as well. */
917 if (vars != NULL)
919 EXECUTE_IF_SET_IN_BITMAP (vars, 0, j, bi)
921 varinfo_t v = get_varinfo (j);
922 for (; v != NULL; v = v->next)
923 bitmap_set_bit (result, v->id);
925 BITMAP_FREE (vars);
929 /* Take a solution set SET, add OFFSET to each member of the set, and
930 overwrite SET with the result when done. */
932 static void
933 solution_set_add (bitmap set, HOST_WIDE_INT offset)
935 bitmap result = BITMAP_ALLOC (&iteration_obstack);
936 unsigned int i;
937 bitmap_iterator bi;
939 /* If the offset is unknown we have to expand the solution to
940 all subfields. */
941 if (offset == UNKNOWN_OFFSET)
943 solution_set_expand (set, set);
944 return;
947 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
949 varinfo_t vi = get_varinfo (i);
951 /* If this is a variable with just one field just set its bit
952 in the result. */
953 if (vi->is_artificial_var
954 || vi->is_unknown_size_var
955 || vi->is_full_var)
956 bitmap_set_bit (result, i);
957 else
959 unsigned HOST_WIDE_INT fieldoffset = vi->offset + offset;
961 /* If the offset makes the pointer point to before the
962 variable use offset zero for the field lookup. */
963 if (offset < 0
964 && fieldoffset > vi->offset)
965 fieldoffset = 0;
967 if (offset != 0)
968 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
970 bitmap_set_bit (result, vi->id);
971 /* If the result is not exactly at fieldoffset include the next
972 field as well. See get_constraint_for_ptr_offset for more
973 rationale. */
974 if (vi->offset != fieldoffset
975 && vi->next != NULL)
976 bitmap_set_bit (result, vi->next->id);
980 bitmap_copy (set, result);
981 BITMAP_FREE (result);
984 /* Union solution sets TO and FROM, and add INC to each member of FROM in the
985 process. */
987 static bool
988 set_union_with_increment (bitmap to, bitmap from, HOST_WIDE_INT inc)
990 if (inc == 0)
991 return bitmap_ior_into (to, from);
992 else
994 bitmap tmp;
995 bool res;
997 tmp = BITMAP_ALLOC (&iteration_obstack);
998 bitmap_copy (tmp, from);
999 solution_set_add (tmp, inc);
1000 res = bitmap_ior_into (to, tmp);
1001 BITMAP_FREE (tmp);
1002 return res;
1006 /* Insert constraint C into the list of complex constraints for graph
1007 node VAR. */
1009 static void
1010 insert_into_complex (constraint_graph_t graph,
1011 unsigned int var, constraint_t c)
1013 VEC (constraint_t, heap) *complex = graph->complex[var];
1014 unsigned int place = VEC_lower_bound (constraint_t, complex, c,
1015 constraint_less);
1017 /* Only insert constraints that do not already exist. */
1018 if (place >= VEC_length (constraint_t, complex)
1019 || !constraint_equal (*c, *VEC_index (constraint_t, complex, place)))
1020 VEC_safe_insert (constraint_t, heap, graph->complex[var], place, c);
1024 /* Condense two variable nodes into a single variable node, by moving
1025 all associated info from SRC to TO. */
1027 static void
1028 merge_node_constraints (constraint_graph_t graph, unsigned int to,
1029 unsigned int from)
1031 unsigned int i;
1032 constraint_t c;
1034 gcc_assert (find (from) == to);
1036 /* Move all complex constraints from src node into to node */
1037 FOR_EACH_VEC_ELT (constraint_t, graph->complex[from], i, c)
1039 /* In complex constraints for node src, we may have either
1040 a = *src, and *src = a, or an offseted constraint which are
1041 always added to the rhs node's constraints. */
1043 if (c->rhs.type == DEREF)
1044 c->rhs.var = to;
1045 else if (c->lhs.type == DEREF)
1046 c->lhs.var = to;
1047 else
1048 c->rhs.var = to;
1050 constraint_set_union (&graph->complex[to], &graph->complex[from]);
1051 VEC_free (constraint_t, heap, graph->complex[from]);
1052 graph->complex[from] = NULL;
1056 /* Remove edges involving NODE from GRAPH. */
1058 static void
1059 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
1061 if (graph->succs[node])
1062 BITMAP_FREE (graph->succs[node]);
1065 /* Merge GRAPH nodes FROM and TO into node TO. */
1067 static void
1068 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1069 unsigned int from)
1071 if (graph->indirect_cycles[from] != -1)
1073 /* If we have indirect cycles with the from node, and we have
1074 none on the to node, the to node has indirect cycles from the
1075 from node now that they are unified.
1076 If indirect cycles exist on both, unify the nodes that they
1077 are in a cycle with, since we know they are in a cycle with
1078 each other. */
1079 if (graph->indirect_cycles[to] == -1)
1080 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1083 /* Merge all the successor edges. */
1084 if (graph->succs[from])
1086 if (!graph->succs[to])
1087 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1088 bitmap_ior_into (graph->succs[to],
1089 graph->succs[from]);
1092 clear_edges_for_node (graph, from);
1096 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1097 it doesn't exist in the graph already. */
1099 static void
1100 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1101 unsigned int from)
1103 if (to == from)
1104 return;
1106 if (!graph->implicit_preds[to])
1107 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1109 if (bitmap_set_bit (graph->implicit_preds[to], from))
1110 stats.num_implicit_edges++;
1113 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1114 it doesn't exist in the graph already.
1115 Return false if the edge already existed, true otherwise. */
1117 static void
1118 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1119 unsigned int from)
1121 if (!graph->preds[to])
1122 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1123 bitmap_set_bit (graph->preds[to], from);
1126 /* Add a graph edge to GRAPH, going from FROM to TO if
1127 it doesn't exist in the graph already.
1128 Return false if the edge already existed, true otherwise. */
1130 static bool
1131 add_graph_edge (constraint_graph_t graph, unsigned int to,
1132 unsigned int from)
1134 if (to == from)
1136 return false;
1138 else
1140 bool r = false;
1142 if (!graph->succs[from])
1143 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1144 if (bitmap_set_bit (graph->succs[from], to))
1146 r = true;
1147 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1148 stats.num_edges++;
1150 return r;
1155 /* Return true if {DEST.SRC} is an existing graph edge in GRAPH. */
1157 static bool
1158 valid_graph_edge (constraint_graph_t graph, unsigned int src,
1159 unsigned int dest)
1161 return (graph->succs[dest]
1162 && bitmap_bit_p (graph->succs[dest], src));
1165 /* Initialize the constraint graph structure to contain SIZE nodes. */
1167 static void
1168 init_graph (unsigned int size)
1170 unsigned int j;
1172 graph = XCNEW (struct constraint_graph);
1173 graph->size = size;
1174 graph->succs = XCNEWVEC (bitmap, graph->size);
1175 graph->indirect_cycles = XNEWVEC (int, graph->size);
1176 graph->rep = XNEWVEC (unsigned int, graph->size);
1177 graph->complex = XCNEWVEC (VEC(constraint_t, heap) *, size);
1178 graph->pe = XCNEWVEC (unsigned int, graph->size);
1179 graph->pe_rep = XNEWVEC (int, graph->size);
1181 for (j = 0; j < graph->size; j++)
1183 graph->rep[j] = j;
1184 graph->pe_rep[j] = -1;
1185 graph->indirect_cycles[j] = -1;
1189 /* Build the constraint graph, adding only predecessor edges right now. */
1191 static void
1192 build_pred_graph (void)
1194 int i;
1195 constraint_t c;
1196 unsigned int j;
1198 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1199 graph->preds = XCNEWVEC (bitmap, graph->size);
1200 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1201 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1202 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1203 graph->points_to = XCNEWVEC (bitmap, graph->size);
1204 graph->eq_rep = XNEWVEC (int, graph->size);
1205 graph->direct_nodes = sbitmap_alloc (graph->size);
1206 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1207 sbitmap_zero (graph->direct_nodes);
1209 for (j = 0; j < FIRST_REF_NODE; j++)
1211 if (!get_varinfo (j)->is_special_var)
1212 SET_BIT (graph->direct_nodes, j);
1215 for (j = 0; j < graph->size; j++)
1216 graph->eq_rep[j] = -1;
1218 for (j = 0; j < VEC_length (varinfo_t, varmap); j++)
1219 graph->indirect_cycles[j] = -1;
1221 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
1223 struct constraint_expr lhs = c->lhs;
1224 struct constraint_expr rhs = c->rhs;
1225 unsigned int lhsvar = lhs.var;
1226 unsigned int rhsvar = rhs.var;
1228 if (lhs.type == DEREF)
1230 /* *x = y. */
1231 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1232 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1234 else if (rhs.type == DEREF)
1236 /* x = *y */
1237 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1238 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1239 else
1240 RESET_BIT (graph->direct_nodes, lhsvar);
1242 else if (rhs.type == ADDRESSOF)
1244 varinfo_t v;
1246 /* x = &y */
1247 if (graph->points_to[lhsvar] == NULL)
1248 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1249 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1251 if (graph->pointed_by[rhsvar] == NULL)
1252 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1253 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1255 /* Implicitly, *x = y */
1256 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1258 /* All related variables are no longer direct nodes. */
1259 RESET_BIT (graph->direct_nodes, rhsvar);
1260 v = get_varinfo (rhsvar);
1261 if (!v->is_full_var)
1263 v = lookup_vi_for_tree (v->decl);
1266 RESET_BIT (graph->direct_nodes, v->id);
1267 v = v->next;
1269 while (v != NULL);
1271 bitmap_set_bit (graph->address_taken, rhsvar);
1273 else if (lhsvar > anything_id
1274 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1276 /* x = y */
1277 add_pred_graph_edge (graph, lhsvar, rhsvar);
1278 /* Implicitly, *x = *y */
1279 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1280 FIRST_REF_NODE + rhsvar);
1282 else if (lhs.offset != 0 || rhs.offset != 0)
1284 if (rhs.offset != 0)
1285 RESET_BIT (graph->direct_nodes, lhs.var);
1286 else if (lhs.offset != 0)
1287 RESET_BIT (graph->direct_nodes, rhs.var);
1292 /* Build the constraint graph, adding successor edges. */
1294 static void
1295 build_succ_graph (void)
1297 unsigned i, t;
1298 constraint_t c;
1300 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
1302 struct constraint_expr lhs;
1303 struct constraint_expr rhs;
1304 unsigned int lhsvar;
1305 unsigned int rhsvar;
1307 if (!c)
1308 continue;
1310 lhs = c->lhs;
1311 rhs = c->rhs;
1312 lhsvar = find (lhs.var);
1313 rhsvar = find (rhs.var);
1315 if (lhs.type == DEREF)
1317 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1318 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1320 else if (rhs.type == DEREF)
1322 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1323 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1325 else if (rhs.type == ADDRESSOF)
1327 /* x = &y */
1328 gcc_assert (find (rhs.var) == rhs.var);
1329 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1331 else if (lhsvar > anything_id
1332 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1334 add_graph_edge (graph, lhsvar, rhsvar);
1338 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1339 receive pointers. */
1340 t = find (storedanything_id);
1341 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1343 if (!TEST_BIT (graph->direct_nodes, i)
1344 && get_varinfo (i)->may_have_pointers)
1345 add_graph_edge (graph, find (i), t);
1348 /* Everything stored to ANYTHING also potentially escapes. */
1349 add_graph_edge (graph, find (escaped_id), t);
1353 /* Changed variables on the last iteration. */
1354 static bitmap changed;
1356 /* Strongly Connected Component visitation info. */
1358 struct scc_info
1360 sbitmap visited;
1361 sbitmap deleted;
1362 unsigned int *dfs;
1363 unsigned int *node_mapping;
1364 int current_index;
1365 VEC(unsigned,heap) *scc_stack;
1369 /* Recursive routine to find strongly connected components in GRAPH.
1370 SI is the SCC info to store the information in, and N is the id of current
1371 graph node we are processing.
1373 This is Tarjan's strongly connected component finding algorithm, as
1374 modified by Nuutila to keep only non-root nodes on the stack.
1375 The algorithm can be found in "On finding the strongly connected
1376 connected components in a directed graph" by Esko Nuutila and Eljas
1377 Soisalon-Soininen, in Information Processing Letters volume 49,
1378 number 1, pages 9-14. */
1380 static void
1381 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1383 unsigned int i;
1384 bitmap_iterator bi;
1385 unsigned int my_dfs;
1387 SET_BIT (si->visited, n);
1388 si->dfs[n] = si->current_index ++;
1389 my_dfs = si->dfs[n];
1391 /* Visit all the successors. */
1392 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1394 unsigned int w;
1396 if (i > LAST_REF_NODE)
1397 break;
1399 w = find (i);
1400 if (TEST_BIT (si->deleted, w))
1401 continue;
1403 if (!TEST_BIT (si->visited, w))
1404 scc_visit (graph, si, w);
1406 unsigned int t = find (w);
1407 unsigned int nnode = find (n);
1408 gcc_assert (nnode == n);
1410 if (si->dfs[t] < si->dfs[nnode])
1411 si->dfs[n] = si->dfs[t];
1415 /* See if any components have been identified. */
1416 if (si->dfs[n] == my_dfs)
1418 if (VEC_length (unsigned, si->scc_stack) > 0
1419 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1421 bitmap scc = BITMAP_ALLOC (NULL);
1422 unsigned int lowest_node;
1423 bitmap_iterator bi;
1425 bitmap_set_bit (scc, n);
1427 while (VEC_length (unsigned, si->scc_stack) != 0
1428 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1430 unsigned int w = VEC_pop (unsigned, si->scc_stack);
1432 bitmap_set_bit (scc, w);
1435 lowest_node = bitmap_first_set_bit (scc);
1436 gcc_assert (lowest_node < FIRST_REF_NODE);
1438 /* Collapse the SCC nodes into a single node, and mark the
1439 indirect cycles. */
1440 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1442 if (i < FIRST_REF_NODE)
1444 if (unite (lowest_node, i))
1445 unify_nodes (graph, lowest_node, i, false);
1447 else
1449 unite (lowest_node, i);
1450 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1454 SET_BIT (si->deleted, n);
1456 else
1457 VEC_safe_push (unsigned, heap, si->scc_stack, n);
1460 /* Unify node FROM into node TO, updating the changed count if
1461 necessary when UPDATE_CHANGED is true. */
1463 static void
1464 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1465 bool update_changed)
1468 gcc_assert (to != from && find (to) == to);
1469 if (dump_file && (dump_flags & TDF_DETAILS))
1470 fprintf (dump_file, "Unifying %s to %s\n",
1471 get_varinfo (from)->name,
1472 get_varinfo (to)->name);
1474 if (update_changed)
1475 stats.unified_vars_dynamic++;
1476 else
1477 stats.unified_vars_static++;
1479 merge_graph_nodes (graph, to, from);
1480 merge_node_constraints (graph, to, from);
1482 /* Mark TO as changed if FROM was changed. If TO was already marked
1483 as changed, decrease the changed count. */
1485 if (update_changed
1486 && bitmap_bit_p (changed, from))
1488 bitmap_clear_bit (changed, from);
1489 bitmap_set_bit (changed, to);
1491 if (get_varinfo (from)->solution)
1493 /* If the solution changes because of the merging, we need to mark
1494 the variable as changed. */
1495 if (bitmap_ior_into (get_varinfo (to)->solution,
1496 get_varinfo (from)->solution))
1498 if (update_changed)
1499 bitmap_set_bit (changed, to);
1502 BITMAP_FREE (get_varinfo (from)->solution);
1503 if (get_varinfo (from)->oldsolution)
1504 BITMAP_FREE (get_varinfo (from)->oldsolution);
1506 if (stats.iterations > 0
1507 && get_varinfo (to)->oldsolution)
1508 BITMAP_FREE (get_varinfo (to)->oldsolution);
1510 if (valid_graph_edge (graph, to, to))
1512 if (graph->succs[to])
1513 bitmap_clear_bit (graph->succs[to], to);
1517 /* Information needed to compute the topological ordering of a graph. */
1519 struct topo_info
1521 /* sbitmap of visited nodes. */
1522 sbitmap visited;
1523 /* Array that stores the topological order of the graph, *in
1524 reverse*. */
1525 VEC(unsigned,heap) *topo_order;
1529 /* Initialize and return a topological info structure. */
1531 static struct topo_info *
1532 init_topo_info (void)
1534 size_t size = graph->size;
1535 struct topo_info *ti = XNEW (struct topo_info);
1536 ti->visited = sbitmap_alloc (size);
1537 sbitmap_zero (ti->visited);
1538 ti->topo_order = VEC_alloc (unsigned, heap, 1);
1539 return ti;
1543 /* Free the topological sort info pointed to by TI. */
1545 static void
1546 free_topo_info (struct topo_info *ti)
1548 sbitmap_free (ti->visited);
1549 VEC_free (unsigned, heap, ti->topo_order);
1550 free (ti);
1553 /* Visit the graph in topological order, and store the order in the
1554 topo_info structure. */
1556 static void
1557 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1558 unsigned int n)
1560 bitmap_iterator bi;
1561 unsigned int j;
1563 SET_BIT (ti->visited, n);
1565 if (graph->succs[n])
1566 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1568 if (!TEST_BIT (ti->visited, j))
1569 topo_visit (graph, ti, j);
1572 VEC_safe_push (unsigned, heap, ti->topo_order, n);
1575 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1576 starting solution for y. */
1578 static void
1579 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1580 bitmap delta)
1582 unsigned int lhs = c->lhs.var;
1583 bool flag = false;
1584 bitmap sol = get_varinfo (lhs)->solution;
1585 unsigned int j;
1586 bitmap_iterator bi;
1587 HOST_WIDE_INT roffset = c->rhs.offset;
1589 /* Our IL does not allow this. */
1590 gcc_assert (c->lhs.offset == 0);
1592 /* If the solution of Y contains anything it is good enough to transfer
1593 this to the LHS. */
1594 if (bitmap_bit_p (delta, anything_id))
1596 flag |= bitmap_set_bit (sol, anything_id);
1597 goto done;
1600 /* If we do not know at with offset the rhs is dereferenced compute
1601 the reachability set of DELTA, conservatively assuming it is
1602 dereferenced at all valid offsets. */
1603 if (roffset == UNKNOWN_OFFSET)
1605 solution_set_expand (delta, delta);
1606 /* No further offset processing is necessary. */
1607 roffset = 0;
1610 /* For each variable j in delta (Sol(y)), add
1611 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1612 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1614 varinfo_t v = get_varinfo (j);
1615 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1616 unsigned int t;
1618 if (v->is_full_var)
1619 fieldoffset = v->offset;
1620 else if (roffset != 0)
1621 v = first_vi_for_offset (v, fieldoffset);
1622 /* If the access is outside of the variable we can ignore it. */
1623 if (!v)
1624 continue;
1628 t = find (v->id);
1630 /* Adding edges from the special vars is pointless.
1631 They don't have sets that can change. */
1632 if (get_varinfo (t)->is_special_var)
1633 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1634 /* Merging the solution from ESCAPED needlessly increases
1635 the set. Use ESCAPED as representative instead. */
1636 else if (v->id == escaped_id)
1637 flag |= bitmap_set_bit (sol, escaped_id);
1638 else if (v->may_have_pointers
1639 && add_graph_edge (graph, lhs, t))
1640 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1642 /* If the variable is not exactly at the requested offset
1643 we have to include the next one. */
1644 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1645 || v->next == NULL)
1646 break;
1648 v = v->next;
1649 fieldoffset = v->offset;
1651 while (1);
1654 done:
1655 /* If the LHS solution changed, mark the var as changed. */
1656 if (flag)
1658 get_varinfo (lhs)->solution = sol;
1659 bitmap_set_bit (changed, lhs);
1663 /* Process a constraint C that represents *(x + off) = y using DELTA
1664 as the starting solution for x. */
1666 static void
1667 do_ds_constraint (constraint_t c, bitmap delta)
1669 unsigned int rhs = c->rhs.var;
1670 bitmap sol = get_varinfo (rhs)->solution;
1671 unsigned int j;
1672 bitmap_iterator bi;
1673 HOST_WIDE_INT loff = c->lhs.offset;
1674 bool escaped_p = false;
1676 /* Our IL does not allow this. */
1677 gcc_assert (c->rhs.offset == 0);
1679 /* If the solution of y contains ANYTHING simply use the ANYTHING
1680 solution. This avoids needlessly increasing the points-to sets. */
1681 if (bitmap_bit_p (sol, anything_id))
1682 sol = get_varinfo (find (anything_id))->solution;
1684 /* If the solution for x contains ANYTHING we have to merge the
1685 solution of y into all pointer variables which we do via
1686 STOREDANYTHING. */
1687 if (bitmap_bit_p (delta, anything_id))
1689 unsigned t = find (storedanything_id);
1690 if (add_graph_edge (graph, t, rhs))
1692 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1693 bitmap_set_bit (changed, t);
1695 return;
1698 /* If we do not know at with offset the rhs is dereferenced compute
1699 the reachability set of DELTA, conservatively assuming it is
1700 dereferenced at all valid offsets. */
1701 if (loff == UNKNOWN_OFFSET)
1703 solution_set_expand (delta, delta);
1704 loff = 0;
1707 /* For each member j of delta (Sol(x)), add an edge from y to j and
1708 union Sol(y) into Sol(j) */
1709 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1711 varinfo_t v = get_varinfo (j);
1712 unsigned int t;
1713 HOST_WIDE_INT fieldoffset = v->offset + loff;
1715 if (v->is_full_var)
1716 fieldoffset = v->offset;
1717 else if (loff != 0)
1718 v = first_vi_for_offset (v, fieldoffset);
1719 /* If the access is outside of the variable we can ignore it. */
1720 if (!v)
1721 continue;
1725 if (v->may_have_pointers)
1727 /* If v is a global variable then this is an escape point. */
1728 if (v->is_global_var
1729 && !escaped_p)
1731 t = find (escaped_id);
1732 if (add_graph_edge (graph, t, rhs)
1733 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1734 bitmap_set_bit (changed, t);
1735 /* Enough to let rhs escape once. */
1736 escaped_p = true;
1739 if (v->is_special_var)
1740 break;
1742 t = find (v->id);
1743 if (add_graph_edge (graph, t, rhs)
1744 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1745 bitmap_set_bit (changed, t);
1748 /* If the variable is not exactly at the requested offset
1749 we have to include the next one. */
1750 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1751 || v->next == NULL)
1752 break;
1754 v = v->next;
1755 fieldoffset = v->offset;
1757 while (1);
1761 /* Handle a non-simple (simple meaning requires no iteration),
1762 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1764 static void
1765 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta)
1767 if (c->lhs.type == DEREF)
1769 if (c->rhs.type == ADDRESSOF)
1771 gcc_unreachable();
1773 else
1775 /* *x = y */
1776 do_ds_constraint (c, delta);
1779 else if (c->rhs.type == DEREF)
1781 /* x = *y */
1782 if (!(get_varinfo (c->lhs.var)->is_special_var))
1783 do_sd_constraint (graph, c, delta);
1785 else
1787 bitmap tmp;
1788 bitmap solution;
1789 bool flag = false;
1791 gcc_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR);
1792 solution = get_varinfo (c->rhs.var)->solution;
1793 tmp = get_varinfo (c->lhs.var)->solution;
1795 flag = set_union_with_increment (tmp, solution, c->rhs.offset);
1797 if (flag)
1799 get_varinfo (c->lhs.var)->solution = tmp;
1800 bitmap_set_bit (changed, c->lhs.var);
1805 /* Initialize and return a new SCC info structure. */
1807 static struct scc_info *
1808 init_scc_info (size_t size)
1810 struct scc_info *si = XNEW (struct scc_info);
1811 size_t i;
1813 si->current_index = 0;
1814 si->visited = sbitmap_alloc (size);
1815 sbitmap_zero (si->visited);
1816 si->deleted = sbitmap_alloc (size);
1817 sbitmap_zero (si->deleted);
1818 si->node_mapping = XNEWVEC (unsigned int, size);
1819 si->dfs = XCNEWVEC (unsigned int, size);
1821 for (i = 0; i < size; i++)
1822 si->node_mapping[i] = i;
1824 si->scc_stack = VEC_alloc (unsigned, heap, 1);
1825 return si;
1828 /* Free an SCC info structure pointed to by SI */
1830 static void
1831 free_scc_info (struct scc_info *si)
1833 sbitmap_free (si->visited);
1834 sbitmap_free (si->deleted);
1835 free (si->node_mapping);
1836 free (si->dfs);
1837 VEC_free (unsigned, heap, si->scc_stack);
1838 free (si);
1842 /* Find indirect cycles in GRAPH that occur, using strongly connected
1843 components, and note them in the indirect cycles map.
1845 This technique comes from Ben Hardekopf and Calvin Lin,
1846 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1847 Lines of Code", submitted to PLDI 2007. */
1849 static void
1850 find_indirect_cycles (constraint_graph_t graph)
1852 unsigned int i;
1853 unsigned int size = graph->size;
1854 struct scc_info *si = init_scc_info (size);
1856 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1857 if (!TEST_BIT (si->visited, i) && find (i) == i)
1858 scc_visit (graph, si, i);
1860 free_scc_info (si);
1863 /* Compute a topological ordering for GRAPH, and store the result in the
1864 topo_info structure TI. */
1866 static void
1867 compute_topo_order (constraint_graph_t graph,
1868 struct topo_info *ti)
1870 unsigned int i;
1871 unsigned int size = graph->size;
1873 for (i = 0; i != size; ++i)
1874 if (!TEST_BIT (ti->visited, i) && find (i) == i)
1875 topo_visit (graph, ti, i);
1878 /* Structure used to for hash value numbering of pointer equivalence
1879 classes. */
1881 typedef struct equiv_class_label
1883 hashval_t hashcode;
1884 unsigned int equivalence_class;
1885 bitmap labels;
1886 } *equiv_class_label_t;
1887 typedef const struct equiv_class_label *const_equiv_class_label_t;
1889 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1890 classes. */
1891 static htab_t pointer_equiv_class_table;
1893 /* A hashtable for mapping a bitmap of labels->location equivalence
1894 classes. */
1895 static htab_t location_equiv_class_table;
1897 /* Hash function for a equiv_class_label_t */
1899 static hashval_t
1900 equiv_class_label_hash (const void *p)
1902 const_equiv_class_label_t const ecl = (const_equiv_class_label_t) p;
1903 return ecl->hashcode;
1906 /* Equality function for two equiv_class_label_t's. */
1908 static int
1909 equiv_class_label_eq (const void *p1, const void *p2)
1911 const_equiv_class_label_t const eql1 = (const_equiv_class_label_t) p1;
1912 const_equiv_class_label_t const eql2 = (const_equiv_class_label_t) p2;
1913 return (eql1->hashcode == eql2->hashcode
1914 && bitmap_equal_p (eql1->labels, eql2->labels));
1917 /* Lookup a equivalence class in TABLE by the bitmap of LABELS it
1918 contains. */
1920 static unsigned int
1921 equiv_class_lookup (htab_t table, bitmap labels)
1923 void **slot;
1924 struct equiv_class_label ecl;
1926 ecl.labels = labels;
1927 ecl.hashcode = bitmap_hash (labels);
1929 slot = htab_find_slot_with_hash (table, &ecl,
1930 ecl.hashcode, NO_INSERT);
1931 if (!slot)
1932 return 0;
1933 else
1934 return ((equiv_class_label_t) *slot)->equivalence_class;
1938 /* Add an equivalence class named EQUIVALENCE_CLASS with labels LABELS
1939 to TABLE. */
1941 static void
1942 equiv_class_add (htab_t table, unsigned int equivalence_class,
1943 bitmap labels)
1945 void **slot;
1946 equiv_class_label_t ecl = XNEW (struct equiv_class_label);
1948 ecl->labels = labels;
1949 ecl->equivalence_class = equivalence_class;
1950 ecl->hashcode = bitmap_hash (labels);
1952 slot = htab_find_slot_with_hash (table, ecl,
1953 ecl->hashcode, INSERT);
1954 gcc_assert (!*slot);
1955 *slot = (void *) ecl;
1958 /* Perform offline variable substitution.
1960 This is a worst case quadratic time way of identifying variables
1961 that must have equivalent points-to sets, including those caused by
1962 static cycles, and single entry subgraphs, in the constraint graph.
1964 The technique is described in "Exploiting Pointer and Location
1965 Equivalence to Optimize Pointer Analysis. In the 14th International
1966 Static Analysis Symposium (SAS), August 2007." It is known as the
1967 "HU" algorithm, and is equivalent to value numbering the collapsed
1968 constraint graph including evaluating unions.
1970 The general method of finding equivalence classes is as follows:
1971 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1972 Initialize all non-REF nodes to be direct nodes.
1973 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1974 variable}
1975 For each constraint containing the dereference, we also do the same
1976 thing.
1978 We then compute SCC's in the graph and unify nodes in the same SCC,
1979 including pts sets.
1981 For each non-collapsed node x:
1982 Visit all unvisited explicit incoming edges.
1983 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1984 where y->x.
1985 Lookup the equivalence class for pts(x).
1986 If we found one, equivalence_class(x) = found class.
1987 Otherwise, equivalence_class(x) = new class, and new_class is
1988 added to the lookup table.
1990 All direct nodes with the same equivalence class can be replaced
1991 with a single representative node.
1992 All unlabeled nodes (label == 0) are not pointers and all edges
1993 involving them can be eliminated.
1994 We perform these optimizations during rewrite_constraints
1996 In addition to pointer equivalence class finding, we also perform
1997 location equivalence class finding. This is the set of variables
1998 that always appear together in points-to sets. We use this to
1999 compress the size of the points-to sets. */
2001 /* Current maximum pointer equivalence class id. */
2002 static int pointer_equiv_class;
2004 /* Current maximum location equivalence class id. */
2005 static int location_equiv_class;
2007 /* Recursive routine to find strongly connected components in GRAPH,
2008 and label it's nodes with DFS numbers. */
2010 static void
2011 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2013 unsigned int i;
2014 bitmap_iterator bi;
2015 unsigned int my_dfs;
2017 gcc_assert (si->node_mapping[n] == n);
2018 SET_BIT (si->visited, n);
2019 si->dfs[n] = si->current_index ++;
2020 my_dfs = si->dfs[n];
2022 /* Visit all the successors. */
2023 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2025 unsigned int w = si->node_mapping[i];
2027 if (TEST_BIT (si->deleted, w))
2028 continue;
2030 if (!TEST_BIT (si->visited, w))
2031 condense_visit (graph, si, w);
2033 unsigned int t = si->node_mapping[w];
2034 unsigned int nnode = si->node_mapping[n];
2035 gcc_assert (nnode == n);
2037 if (si->dfs[t] < si->dfs[nnode])
2038 si->dfs[n] = si->dfs[t];
2042 /* Visit all the implicit predecessors. */
2043 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2045 unsigned int w = si->node_mapping[i];
2047 if (TEST_BIT (si->deleted, w))
2048 continue;
2050 if (!TEST_BIT (si->visited, w))
2051 condense_visit (graph, si, w);
2053 unsigned int t = si->node_mapping[w];
2054 unsigned int nnode = si->node_mapping[n];
2055 gcc_assert (nnode == n);
2057 if (si->dfs[t] < si->dfs[nnode])
2058 si->dfs[n] = si->dfs[t];
2062 /* See if any components have been identified. */
2063 if (si->dfs[n] == my_dfs)
2065 while (VEC_length (unsigned, si->scc_stack) != 0
2066 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
2068 unsigned int w = VEC_pop (unsigned, si->scc_stack);
2069 si->node_mapping[w] = n;
2071 if (!TEST_BIT (graph->direct_nodes, w))
2072 RESET_BIT (graph->direct_nodes, n);
2074 /* Unify our nodes. */
2075 if (graph->preds[w])
2077 if (!graph->preds[n])
2078 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2079 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2081 if (graph->implicit_preds[w])
2083 if (!graph->implicit_preds[n])
2084 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2085 bitmap_ior_into (graph->implicit_preds[n],
2086 graph->implicit_preds[w]);
2088 if (graph->points_to[w])
2090 if (!graph->points_to[n])
2091 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2092 bitmap_ior_into (graph->points_to[n],
2093 graph->points_to[w]);
2096 SET_BIT (si->deleted, n);
2098 else
2099 VEC_safe_push (unsigned, heap, si->scc_stack, n);
2102 /* Label pointer equivalences. */
2104 static void
2105 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2107 unsigned int i;
2108 bitmap_iterator bi;
2109 SET_BIT (si->visited, n);
2111 if (!graph->points_to[n])
2112 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2114 /* Label and union our incoming edges's points to sets. */
2115 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2117 unsigned int w = si->node_mapping[i];
2118 if (!TEST_BIT (si->visited, w))
2119 label_visit (graph, si, w);
2121 /* Skip unused edges */
2122 if (w == n || graph->pointer_label[w] == 0)
2123 continue;
2125 if (graph->points_to[w])
2126 bitmap_ior_into(graph->points_to[n], graph->points_to[w]);
2128 /* Indirect nodes get fresh variables. */
2129 if (!TEST_BIT (graph->direct_nodes, n))
2130 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2132 if (!bitmap_empty_p (graph->points_to[n]))
2134 unsigned int label = equiv_class_lookup (pointer_equiv_class_table,
2135 graph->points_to[n]);
2136 if (!label)
2138 label = pointer_equiv_class++;
2139 equiv_class_add (pointer_equiv_class_table,
2140 label, graph->points_to[n]);
2142 graph->pointer_label[n] = label;
2146 /* Perform offline variable substitution, discovering equivalence
2147 classes, and eliminating non-pointer variables. */
2149 static struct scc_info *
2150 perform_var_substitution (constraint_graph_t graph)
2152 unsigned int i;
2153 unsigned int size = graph->size;
2154 struct scc_info *si = init_scc_info (size);
2156 bitmap_obstack_initialize (&iteration_obstack);
2157 pointer_equiv_class_table = htab_create (511, equiv_class_label_hash,
2158 equiv_class_label_eq, free);
2159 location_equiv_class_table = htab_create (511, equiv_class_label_hash,
2160 equiv_class_label_eq, free);
2161 pointer_equiv_class = 1;
2162 location_equiv_class = 1;
2164 /* Condense the nodes, which means to find SCC's, count incoming
2165 predecessors, and unite nodes in SCC's. */
2166 for (i = 0; i < FIRST_REF_NODE; i++)
2167 if (!TEST_BIT (si->visited, si->node_mapping[i]))
2168 condense_visit (graph, si, si->node_mapping[i]);
2170 sbitmap_zero (si->visited);
2171 /* Actually the label the nodes for pointer equivalences */
2172 for (i = 0; i < FIRST_REF_NODE; i++)
2173 if (!TEST_BIT (si->visited, si->node_mapping[i]))
2174 label_visit (graph, si, si->node_mapping[i]);
2176 /* Calculate location equivalence labels. */
2177 for (i = 0; i < FIRST_REF_NODE; i++)
2179 bitmap pointed_by;
2180 bitmap_iterator bi;
2181 unsigned int j;
2182 unsigned int label;
2184 if (!graph->pointed_by[i])
2185 continue;
2186 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2188 /* Translate the pointed-by mapping for pointer equivalence
2189 labels. */
2190 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2192 bitmap_set_bit (pointed_by,
2193 graph->pointer_label[si->node_mapping[j]]);
2195 /* The original pointed_by is now dead. */
2196 BITMAP_FREE (graph->pointed_by[i]);
2198 /* Look up the location equivalence label if one exists, or make
2199 one otherwise. */
2200 label = equiv_class_lookup (location_equiv_class_table,
2201 pointed_by);
2202 if (label == 0)
2204 label = location_equiv_class++;
2205 equiv_class_add (location_equiv_class_table,
2206 label, pointed_by);
2208 else
2210 if (dump_file && (dump_flags & TDF_DETAILS))
2211 fprintf (dump_file, "Found location equivalence for node %s\n",
2212 get_varinfo (i)->name);
2213 BITMAP_FREE (pointed_by);
2215 graph->loc_label[i] = label;
2219 if (dump_file && (dump_flags & TDF_DETAILS))
2220 for (i = 0; i < FIRST_REF_NODE; i++)
2222 bool direct_node = TEST_BIT (graph->direct_nodes, i);
2223 fprintf (dump_file,
2224 "Equivalence classes for %s node id %d:%s are pointer: %d"
2225 ", location:%d\n",
2226 direct_node ? "Direct node" : "Indirect node", i,
2227 get_varinfo (i)->name,
2228 graph->pointer_label[si->node_mapping[i]],
2229 graph->loc_label[si->node_mapping[i]]);
2232 /* Quickly eliminate our non-pointer variables. */
2234 for (i = 0; i < FIRST_REF_NODE; i++)
2236 unsigned int node = si->node_mapping[i];
2238 if (graph->pointer_label[node] == 0)
2240 if (dump_file && (dump_flags & TDF_DETAILS))
2241 fprintf (dump_file,
2242 "%s is a non-pointer variable, eliminating edges.\n",
2243 get_varinfo (node)->name);
2244 stats.nonpointer_vars++;
2245 clear_edges_for_node (graph, node);
2249 return si;
2252 /* Free information that was only necessary for variable
2253 substitution. */
2255 static void
2256 free_var_substitution_info (struct scc_info *si)
2258 free_scc_info (si);
2259 free (graph->pointer_label);
2260 free (graph->loc_label);
2261 free (graph->pointed_by);
2262 free (graph->points_to);
2263 free (graph->eq_rep);
2264 sbitmap_free (graph->direct_nodes);
2265 htab_delete (pointer_equiv_class_table);
2266 htab_delete (location_equiv_class_table);
2267 bitmap_obstack_release (&iteration_obstack);
2270 /* Return an existing node that is equivalent to NODE, which has
2271 equivalence class LABEL, if one exists. Return NODE otherwise. */
2273 static unsigned int
2274 find_equivalent_node (constraint_graph_t graph,
2275 unsigned int node, unsigned int label)
2277 /* If the address version of this variable is unused, we can
2278 substitute it for anything else with the same label.
2279 Otherwise, we know the pointers are equivalent, but not the
2280 locations, and we can unite them later. */
2282 if (!bitmap_bit_p (graph->address_taken, node))
2284 gcc_assert (label < graph->size);
2286 if (graph->eq_rep[label] != -1)
2288 /* Unify the two variables since we know they are equivalent. */
2289 if (unite (graph->eq_rep[label], node))
2290 unify_nodes (graph, graph->eq_rep[label], node, false);
2291 return graph->eq_rep[label];
2293 else
2295 graph->eq_rep[label] = node;
2296 graph->pe_rep[label] = node;
2299 else
2301 gcc_assert (label < graph->size);
2302 graph->pe[node] = label;
2303 if (graph->pe_rep[label] == -1)
2304 graph->pe_rep[label] = node;
2307 return node;
2310 /* Unite pointer equivalent but not location equivalent nodes in
2311 GRAPH. This may only be performed once variable substitution is
2312 finished. */
2314 static void
2315 unite_pointer_equivalences (constraint_graph_t graph)
2317 unsigned int i;
2319 /* Go through the pointer equivalences and unite them to their
2320 representative, if they aren't already. */
2321 for (i = 0; i < FIRST_REF_NODE; i++)
2323 unsigned int label = graph->pe[i];
2324 if (label)
2326 int label_rep = graph->pe_rep[label];
2328 if (label_rep == -1)
2329 continue;
2331 label_rep = find (label_rep);
2332 if (label_rep >= 0 && unite (label_rep, find (i)))
2333 unify_nodes (graph, label_rep, i, false);
2338 /* Move complex constraints to the GRAPH nodes they belong to. */
2340 static void
2341 move_complex_constraints (constraint_graph_t graph)
2343 int i;
2344 constraint_t c;
2346 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
2348 if (c)
2350 struct constraint_expr lhs = c->lhs;
2351 struct constraint_expr rhs = c->rhs;
2353 if (lhs.type == DEREF)
2355 insert_into_complex (graph, lhs.var, c);
2357 else if (rhs.type == DEREF)
2359 if (!(get_varinfo (lhs.var)->is_special_var))
2360 insert_into_complex (graph, rhs.var, c);
2362 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2363 && (lhs.offset != 0 || rhs.offset != 0))
2365 insert_into_complex (graph, rhs.var, c);
2372 /* Optimize and rewrite complex constraints while performing
2373 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2374 result of perform_variable_substitution. */
2376 static void
2377 rewrite_constraints (constraint_graph_t graph,
2378 struct scc_info *si)
2380 int i;
2381 unsigned int j;
2382 constraint_t c;
2384 for (j = 0; j < graph->size; j++)
2385 gcc_assert (find (j) == j);
2387 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
2389 struct constraint_expr lhs = c->lhs;
2390 struct constraint_expr rhs = c->rhs;
2391 unsigned int lhsvar = find (lhs.var);
2392 unsigned int rhsvar = find (rhs.var);
2393 unsigned int lhsnode, rhsnode;
2394 unsigned int lhslabel, rhslabel;
2396 lhsnode = si->node_mapping[lhsvar];
2397 rhsnode = si->node_mapping[rhsvar];
2398 lhslabel = graph->pointer_label[lhsnode];
2399 rhslabel = graph->pointer_label[rhsnode];
2401 /* See if it is really a non-pointer variable, and if so, ignore
2402 the constraint. */
2403 if (lhslabel == 0)
2405 if (dump_file && (dump_flags & TDF_DETAILS))
2408 fprintf (dump_file, "%s is a non-pointer variable,"
2409 "ignoring constraint:",
2410 get_varinfo (lhs.var)->name);
2411 dump_constraint (dump_file, c);
2412 fprintf (dump_file, "\n");
2414 VEC_replace (constraint_t, constraints, i, NULL);
2415 continue;
2418 if (rhslabel == 0)
2420 if (dump_file && (dump_flags & TDF_DETAILS))
2423 fprintf (dump_file, "%s is a non-pointer variable,"
2424 "ignoring constraint:",
2425 get_varinfo (rhs.var)->name);
2426 dump_constraint (dump_file, c);
2427 fprintf (dump_file, "\n");
2429 VEC_replace (constraint_t, constraints, i, NULL);
2430 continue;
2433 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2434 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2435 c->lhs.var = lhsvar;
2436 c->rhs.var = rhsvar;
2441 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2442 part of an SCC, false otherwise. */
2444 static bool
2445 eliminate_indirect_cycles (unsigned int node)
2447 if (graph->indirect_cycles[node] != -1
2448 && !bitmap_empty_p (get_varinfo (node)->solution))
2450 unsigned int i;
2451 VEC(unsigned,heap) *queue = NULL;
2452 int queuepos;
2453 unsigned int to = find (graph->indirect_cycles[node]);
2454 bitmap_iterator bi;
2456 /* We can't touch the solution set and call unify_nodes
2457 at the same time, because unify_nodes is going to do
2458 bitmap unions into it. */
2460 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2462 if (find (i) == i && i != to)
2464 if (unite (to, i))
2465 VEC_safe_push (unsigned, heap, queue, i);
2469 for (queuepos = 0;
2470 VEC_iterate (unsigned, queue, queuepos, i);
2471 queuepos++)
2473 unify_nodes (graph, to, i, true);
2475 VEC_free (unsigned, heap, queue);
2476 return true;
2478 return false;
2481 /* Solve the constraint graph GRAPH using our worklist solver.
2482 This is based on the PW* family of solvers from the "Efficient Field
2483 Sensitive Pointer Analysis for C" paper.
2484 It works by iterating over all the graph nodes, processing the complex
2485 constraints and propagating the copy constraints, until everything stops
2486 changed. This corresponds to steps 6-8 in the solving list given above. */
2488 static void
2489 solve_graph (constraint_graph_t graph)
2491 unsigned int size = graph->size;
2492 unsigned int i;
2493 bitmap pts;
2495 changed = BITMAP_ALLOC (NULL);
2497 /* Mark all initial non-collapsed nodes as changed. */
2498 for (i = 0; i < size; i++)
2500 varinfo_t ivi = get_varinfo (i);
2501 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2502 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2503 || VEC_length (constraint_t, graph->complex[i]) > 0))
2504 bitmap_set_bit (changed, i);
2507 /* Allocate a bitmap to be used to store the changed bits. */
2508 pts = BITMAP_ALLOC (&pta_obstack);
2510 while (!bitmap_empty_p (changed))
2512 unsigned int i;
2513 struct topo_info *ti = init_topo_info ();
2514 stats.iterations++;
2516 bitmap_obstack_initialize (&iteration_obstack);
2518 compute_topo_order (graph, ti);
2520 while (VEC_length (unsigned, ti->topo_order) != 0)
2523 i = VEC_pop (unsigned, ti->topo_order);
2525 /* If this variable is not a representative, skip it. */
2526 if (find (i) != i)
2527 continue;
2529 /* In certain indirect cycle cases, we may merge this
2530 variable to another. */
2531 if (eliminate_indirect_cycles (i) && find (i) != i)
2532 continue;
2534 /* If the node has changed, we need to process the
2535 complex constraints and outgoing edges again. */
2536 if (bitmap_clear_bit (changed, i))
2538 unsigned int j;
2539 constraint_t c;
2540 bitmap solution;
2541 VEC(constraint_t,heap) *complex = graph->complex[i];
2542 varinfo_t vi = get_varinfo (i);
2543 bool solution_empty;
2545 /* Compute the changed set of solution bits. */
2546 if (vi->oldsolution)
2547 bitmap_and_compl (pts, vi->solution, vi->oldsolution);
2548 else
2549 bitmap_copy (pts, vi->solution);
2551 if (bitmap_empty_p (pts))
2552 continue;
2554 if (vi->oldsolution)
2555 bitmap_ior_into (vi->oldsolution, pts);
2556 else
2558 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
2559 bitmap_copy (vi->oldsolution, pts);
2562 solution = vi->solution;
2563 solution_empty = bitmap_empty_p (solution);
2565 /* Process the complex constraints */
2566 FOR_EACH_VEC_ELT (constraint_t, complex, j, c)
2568 /* XXX: This is going to unsort the constraints in
2569 some cases, which will occasionally add duplicate
2570 constraints during unification. This does not
2571 affect correctness. */
2572 c->lhs.var = find (c->lhs.var);
2573 c->rhs.var = find (c->rhs.var);
2575 /* The only complex constraint that can change our
2576 solution to non-empty, given an empty solution,
2577 is a constraint where the lhs side is receiving
2578 some set from elsewhere. */
2579 if (!solution_empty || c->lhs.type != DEREF)
2580 do_complex_constraint (graph, c, pts);
2583 solution_empty = bitmap_empty_p (solution);
2585 if (!solution_empty)
2587 bitmap_iterator bi;
2588 unsigned eff_escaped_id = find (escaped_id);
2590 /* Propagate solution to all successors. */
2591 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2592 0, j, bi)
2594 bitmap tmp;
2595 bool flag;
2597 unsigned int to = find (j);
2598 tmp = get_varinfo (to)->solution;
2599 flag = false;
2601 /* Don't try to propagate to ourselves. */
2602 if (to == i)
2603 continue;
2605 /* If we propagate from ESCAPED use ESCAPED as
2606 placeholder. */
2607 if (i == eff_escaped_id)
2608 flag = bitmap_set_bit (tmp, escaped_id);
2609 else
2610 flag = set_union_with_increment (tmp, pts, 0);
2612 if (flag)
2614 get_varinfo (to)->solution = tmp;
2615 bitmap_set_bit (changed, to);
2621 free_topo_info (ti);
2622 bitmap_obstack_release (&iteration_obstack);
2625 BITMAP_FREE (pts);
2626 BITMAP_FREE (changed);
2627 bitmap_obstack_release (&oldpta_obstack);
2630 /* Map from trees to variable infos. */
2631 static struct pointer_map_t *vi_for_tree;
2634 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2636 static void
2637 insert_vi_for_tree (tree t, varinfo_t vi)
2639 void **slot = pointer_map_insert (vi_for_tree, t);
2640 gcc_assert (vi);
2641 gcc_assert (*slot == NULL);
2642 *slot = vi;
2645 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2646 exist in the map, return NULL, otherwise, return the varinfo we found. */
2648 static varinfo_t
2649 lookup_vi_for_tree (tree t)
2651 void **slot = pointer_map_contains (vi_for_tree, t);
2652 if (slot == NULL)
2653 return NULL;
2655 return (varinfo_t) *slot;
2658 /* Return a printable name for DECL */
2660 static const char *
2661 alias_get_name (tree decl)
2663 const char *res;
2664 char *temp;
2665 int num_printed = 0;
2667 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2668 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2669 else
2670 res= get_name (decl);
2671 if (res != NULL)
2672 return res;
2674 res = "NULL";
2675 if (!dump_file)
2676 return res;
2678 if (TREE_CODE (decl) == SSA_NAME)
2680 num_printed = asprintf (&temp, "%s_%u",
2681 alias_get_name (SSA_NAME_VAR (decl)),
2682 SSA_NAME_VERSION (decl));
2684 else if (DECL_P (decl))
2686 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2688 if (num_printed > 0)
2690 res = ggc_strdup (temp);
2691 free (temp);
2693 return res;
2696 /* Find the variable id for tree T in the map.
2697 If T doesn't exist in the map, create an entry for it and return it. */
2699 static varinfo_t
2700 get_vi_for_tree (tree t)
2702 void **slot = pointer_map_contains (vi_for_tree, t);
2703 if (slot == NULL)
2704 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2706 return (varinfo_t) *slot;
2709 /* Get a scalar constraint expression for a new temporary variable. */
2711 static struct constraint_expr
2712 new_scalar_tmp_constraint_exp (const char *name)
2714 struct constraint_expr tmp;
2715 varinfo_t vi;
2717 vi = new_var_info (NULL_TREE, name);
2718 vi->offset = 0;
2719 vi->size = -1;
2720 vi->fullsize = -1;
2721 vi->is_full_var = 1;
2723 tmp.var = vi->id;
2724 tmp.type = SCALAR;
2725 tmp.offset = 0;
2727 return tmp;
2730 /* Get a constraint expression vector from an SSA_VAR_P node.
2731 If address_p is true, the result will be taken its address of. */
2733 static void
2734 get_constraint_for_ssa_var (tree t, VEC(ce_s, heap) **results, bool address_p)
2736 struct constraint_expr cexpr;
2737 varinfo_t vi;
2739 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2740 gcc_assert (SSA_VAR_P (t) || DECL_P (t));
2742 /* For parameters, get at the points-to set for the actual parm
2743 decl. */
2744 if (TREE_CODE (t) == SSA_NAME
2745 && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2746 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL)
2747 && SSA_NAME_IS_DEFAULT_DEF (t))
2749 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2750 return;
2753 /* For global variables resort to the alias target. */
2754 if (TREE_CODE (t) == VAR_DECL
2755 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
2757 struct varpool_node *node = varpool_get_node (t);
2758 if (node && node->alias)
2760 node = varpool_variable_node (node, NULL);
2761 t = node->symbol.decl;
2765 vi = get_vi_for_tree (t);
2766 cexpr.var = vi->id;
2767 cexpr.type = SCALAR;
2768 cexpr.offset = 0;
2769 /* If we determine the result is "anything", and we know this is readonly,
2770 say it points to readonly memory instead. */
2771 if (cexpr.var == anything_id && TREE_READONLY (t))
2773 gcc_unreachable ();
2774 cexpr.type = ADDRESSOF;
2775 cexpr.var = readonly_id;
2778 /* If we are not taking the address of the constraint expr, add all
2779 sub-fiels of the variable as well. */
2780 if (!address_p
2781 && !vi->is_full_var)
2783 for (; vi; vi = vi->next)
2785 cexpr.var = vi->id;
2786 VEC_safe_push (ce_s, heap, *results, &cexpr);
2788 return;
2791 VEC_safe_push (ce_s, heap, *results, &cexpr);
2794 /* Process constraint T, performing various simplifications and then
2795 adding it to our list of overall constraints. */
2797 static void
2798 process_constraint (constraint_t t)
2800 struct constraint_expr rhs = t->rhs;
2801 struct constraint_expr lhs = t->lhs;
2803 gcc_assert (rhs.var < VEC_length (varinfo_t, varmap));
2804 gcc_assert (lhs.var < VEC_length (varinfo_t, varmap));
2806 /* If we didn't get any useful constraint from the lhs we get
2807 &ANYTHING as fallback from get_constraint_for. Deal with
2808 it here by turning it into *ANYTHING. */
2809 if (lhs.type == ADDRESSOF
2810 && lhs.var == anything_id)
2811 lhs.type = DEREF;
2813 /* ADDRESSOF on the lhs is invalid. */
2814 gcc_assert (lhs.type != ADDRESSOF);
2816 /* We shouldn't add constraints from things that cannot have pointers.
2817 It's not completely trivial to avoid in the callers, so do it here. */
2818 if (rhs.type != ADDRESSOF
2819 && !get_varinfo (rhs.var)->may_have_pointers)
2820 return;
2822 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2823 if (!get_varinfo (lhs.var)->may_have_pointers)
2824 return;
2826 /* This can happen in our IR with things like n->a = *p */
2827 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
2829 /* Split into tmp = *rhs, *lhs = tmp */
2830 struct constraint_expr tmplhs;
2831 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp");
2832 process_constraint (new_constraint (tmplhs, rhs));
2833 process_constraint (new_constraint (lhs, tmplhs));
2835 else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
2837 /* Split into tmp = &rhs, *lhs = tmp */
2838 struct constraint_expr tmplhs;
2839 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp");
2840 process_constraint (new_constraint (tmplhs, rhs));
2841 process_constraint (new_constraint (lhs, tmplhs));
2843 else
2845 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
2846 VEC_safe_push (constraint_t, heap, constraints, t);
2851 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2852 structure. */
2854 static HOST_WIDE_INT
2855 bitpos_of_field (const tree fdecl)
2857 if (!host_integerp (DECL_FIELD_OFFSET (fdecl), 0)
2858 || !host_integerp (DECL_FIELD_BIT_OFFSET (fdecl), 0))
2859 return -1;
2861 return (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
2862 + TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (fdecl)));
2866 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
2867 resulting constraint expressions in *RESULTS. */
2869 static void
2870 get_constraint_for_ptr_offset (tree ptr, tree offset,
2871 VEC (ce_s, heap) **results)
2873 struct constraint_expr c;
2874 unsigned int j, n;
2875 HOST_WIDE_INT rhsoffset;
2877 /* If we do not do field-sensitive PTA adding offsets to pointers
2878 does not change the points-to solution. */
2879 if (!use_field_sensitive)
2881 get_constraint_for_rhs (ptr, results);
2882 return;
2885 /* If the offset is not a non-negative integer constant that fits
2886 in a HOST_WIDE_INT, we have to fall back to a conservative
2887 solution which includes all sub-fields of all pointed-to
2888 variables of ptr. */
2889 if (offset == NULL_TREE
2890 || TREE_CODE (offset) != INTEGER_CST)
2891 rhsoffset = UNKNOWN_OFFSET;
2892 else
2894 /* Sign-extend the offset. */
2895 double_int soffset
2896 = double_int_sext (tree_to_double_int (offset),
2897 TYPE_PRECISION (TREE_TYPE (offset)));
2898 if (!double_int_fits_in_shwi_p (soffset))
2899 rhsoffset = UNKNOWN_OFFSET;
2900 else
2902 /* Make sure the bit-offset also fits. */
2903 HOST_WIDE_INT rhsunitoffset = soffset.low;
2904 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
2905 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
2906 rhsoffset = UNKNOWN_OFFSET;
2910 get_constraint_for_rhs (ptr, results);
2911 if (rhsoffset == 0)
2912 return;
2914 /* As we are eventually appending to the solution do not use
2915 VEC_iterate here. */
2916 n = VEC_length (ce_s, *results);
2917 for (j = 0; j < n; j++)
2919 varinfo_t curr;
2920 c = *VEC_index (ce_s, *results, j);
2921 curr = get_varinfo (c.var);
2923 if (c.type == ADDRESSOF
2924 /* If this varinfo represents a full variable just use it. */
2925 && curr->is_full_var)
2926 c.offset = 0;
2927 else if (c.type == ADDRESSOF
2928 /* If we do not know the offset add all subfields. */
2929 && rhsoffset == UNKNOWN_OFFSET)
2931 varinfo_t temp = lookup_vi_for_tree (curr->decl);
2934 struct constraint_expr c2;
2935 c2.var = temp->id;
2936 c2.type = ADDRESSOF;
2937 c2.offset = 0;
2938 if (c2.var != c.var)
2939 VEC_safe_push (ce_s, heap, *results, &c2);
2940 temp = temp->next;
2942 while (temp);
2944 else if (c.type == ADDRESSOF)
2946 varinfo_t temp;
2947 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
2949 /* Search the sub-field which overlaps with the
2950 pointed-to offset. If the result is outside of the variable
2951 we have to provide a conservative result, as the variable is
2952 still reachable from the resulting pointer (even though it
2953 technically cannot point to anything). The last and first
2954 sub-fields are such conservative results.
2955 ??? If we always had a sub-field for &object + 1 then
2956 we could represent this in a more precise way. */
2957 if (rhsoffset < 0
2958 && curr->offset < offset)
2959 offset = 0;
2960 temp = first_or_preceding_vi_for_offset (curr, offset);
2962 /* If the found variable is not exactly at the pointed to
2963 result, we have to include the next variable in the
2964 solution as well. Otherwise two increments by offset / 2
2965 do not result in the same or a conservative superset
2966 solution. */
2967 if (temp->offset != offset
2968 && temp->next != NULL)
2970 struct constraint_expr c2;
2971 c2.var = temp->next->id;
2972 c2.type = ADDRESSOF;
2973 c2.offset = 0;
2974 VEC_safe_push (ce_s, heap, *results, &c2);
2976 c.var = temp->id;
2977 c.offset = 0;
2979 else
2980 c.offset = rhsoffset;
2982 VEC_replace (ce_s, *results, j, &c);
2987 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
2988 If address_p is true the result will be taken its address of.
2989 If lhs_p is true then the constraint expression is assumed to be used
2990 as the lhs. */
2992 static void
2993 get_constraint_for_component_ref (tree t, VEC(ce_s, heap) **results,
2994 bool address_p, bool lhs_p)
2996 tree orig_t = t;
2997 HOST_WIDE_INT bitsize = -1;
2998 HOST_WIDE_INT bitmaxsize = -1;
2999 HOST_WIDE_INT bitpos;
3000 tree forzero;
3001 struct constraint_expr *result;
3003 /* Some people like to do cute things like take the address of
3004 &0->a.b */
3005 forzero = t;
3006 while (handled_component_p (forzero)
3007 || INDIRECT_REF_P (forzero)
3008 || TREE_CODE (forzero) == MEM_REF)
3009 forzero = TREE_OPERAND (forzero, 0);
3011 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
3013 struct constraint_expr temp;
3015 temp.offset = 0;
3016 temp.var = integer_id;
3017 temp.type = SCALAR;
3018 VEC_safe_push (ce_s, heap, *results, &temp);
3019 return;
3022 /* Handle type-punning through unions. If we are extracting a pointer
3023 from a union via a possibly type-punning access that pointer
3024 points to anything, similar to a conversion of an integer to
3025 a pointer. */
3026 if (!lhs_p)
3028 tree u;
3029 for (u = t;
3030 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3031 u = TREE_OPERAND (u, 0))
3032 if (TREE_CODE (u) == COMPONENT_REF
3033 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3035 struct constraint_expr temp;
3037 temp.offset = 0;
3038 temp.var = anything_id;
3039 temp.type = ADDRESSOF;
3040 VEC_safe_push (ce_s, heap, *results, &temp);
3041 return;
3045 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
3047 /* Pretend to take the address of the base, we'll take care of
3048 adding the required subset of sub-fields below. */
3049 get_constraint_for_1 (t, results, true, lhs_p);
3050 gcc_assert (VEC_length (ce_s, *results) == 1);
3051 result = VEC_last (ce_s, *results);
3053 if (result->type == SCALAR
3054 && get_varinfo (result->var)->is_full_var)
3055 /* For single-field vars do not bother about the offset. */
3056 result->offset = 0;
3057 else if (result->type == SCALAR)
3059 /* In languages like C, you can access one past the end of an
3060 array. You aren't allowed to dereference it, so we can
3061 ignore this constraint. When we handle pointer subtraction,
3062 we may have to do something cute here. */
3064 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result->var)->fullsize
3065 && bitmaxsize != 0)
3067 /* It's also not true that the constraint will actually start at the
3068 right offset, it may start in some padding. We only care about
3069 setting the constraint to the first actual field it touches, so
3070 walk to find it. */
3071 struct constraint_expr cexpr = *result;
3072 varinfo_t curr;
3073 VEC_pop (ce_s, *results);
3074 cexpr.offset = 0;
3075 for (curr = get_varinfo (cexpr.var); curr; curr = curr->next)
3077 if (ranges_overlap_p (curr->offset, curr->size,
3078 bitpos, bitmaxsize))
3080 cexpr.var = curr->id;
3081 VEC_safe_push (ce_s, heap, *results, &cexpr);
3082 if (address_p)
3083 break;
3086 /* If we are going to take the address of this field then
3087 to be able to compute reachability correctly add at least
3088 the last field of the variable. */
3089 if (address_p
3090 && VEC_length (ce_s, *results) == 0)
3092 curr = get_varinfo (cexpr.var);
3093 while (curr->next != NULL)
3094 curr = curr->next;
3095 cexpr.var = curr->id;
3096 VEC_safe_push (ce_s, heap, *results, &cexpr);
3098 else if (VEC_length (ce_s, *results) == 0)
3099 /* Assert that we found *some* field there. The user couldn't be
3100 accessing *only* padding. */
3101 /* Still the user could access one past the end of an array
3102 embedded in a struct resulting in accessing *only* padding. */
3103 /* Or accessing only padding via type-punning to a type
3104 that has a filed just in padding space. */
3106 cexpr.type = SCALAR;
3107 cexpr.var = anything_id;
3108 cexpr.offset = 0;
3109 VEC_safe_push (ce_s, heap, *results, &cexpr);
3112 else if (bitmaxsize == 0)
3114 if (dump_file && (dump_flags & TDF_DETAILS))
3115 fprintf (dump_file, "Access to zero-sized part of variable,"
3116 "ignoring\n");
3118 else
3119 if (dump_file && (dump_flags & TDF_DETAILS))
3120 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3122 else if (result->type == DEREF)
3124 /* If we do not know exactly where the access goes say so. Note
3125 that only for non-structure accesses we know that we access
3126 at most one subfiled of any variable. */
3127 if (bitpos == -1
3128 || bitsize != bitmaxsize
3129 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
3130 || result->offset == UNKNOWN_OFFSET)
3131 result->offset = UNKNOWN_OFFSET;
3132 else
3133 result->offset += bitpos;
3135 else if (result->type == ADDRESSOF)
3137 /* We can end up here for component references on a
3138 VIEW_CONVERT_EXPR <>(&foobar). */
3139 result->type = SCALAR;
3140 result->var = anything_id;
3141 result->offset = 0;
3143 else
3144 gcc_unreachable ();
3148 /* Dereference the constraint expression CONS, and return the result.
3149 DEREF (ADDRESSOF) = SCALAR
3150 DEREF (SCALAR) = DEREF
3151 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3152 This is needed so that we can handle dereferencing DEREF constraints. */
3154 static void
3155 do_deref (VEC (ce_s, heap) **constraints)
3157 struct constraint_expr *c;
3158 unsigned int i = 0;
3160 FOR_EACH_VEC_ELT (ce_s, *constraints, i, c)
3162 if (c->type == SCALAR)
3163 c->type = DEREF;
3164 else if (c->type == ADDRESSOF)
3165 c->type = SCALAR;
3166 else if (c->type == DEREF)
3168 struct constraint_expr tmplhs;
3169 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp");
3170 process_constraint (new_constraint (tmplhs, *c));
3171 c->var = tmplhs.var;
3173 else
3174 gcc_unreachable ();
3178 /* Given a tree T, return the constraint expression for taking the
3179 address of it. */
3181 static void
3182 get_constraint_for_address_of (tree t, VEC (ce_s, heap) **results)
3184 struct constraint_expr *c;
3185 unsigned int i;
3187 get_constraint_for_1 (t, results, true, true);
3189 FOR_EACH_VEC_ELT (ce_s, *results, i, c)
3191 if (c->type == DEREF)
3192 c->type = SCALAR;
3193 else
3194 c->type = ADDRESSOF;
3198 /* Given a tree T, return the constraint expression for it. */
3200 static void
3201 get_constraint_for_1 (tree t, VEC (ce_s, heap) **results, bool address_p,
3202 bool lhs_p)
3204 struct constraint_expr temp;
3206 /* x = integer is all glommed to a single variable, which doesn't
3207 point to anything by itself. That is, of course, unless it is an
3208 integer constant being treated as a pointer, in which case, we
3209 will return that this is really the addressof anything. This
3210 happens below, since it will fall into the default case. The only
3211 case we know something about an integer treated like a pointer is
3212 when it is the NULL pointer, and then we just say it points to
3213 NULL.
3215 Do not do that if -fno-delete-null-pointer-checks though, because
3216 in that case *NULL does not fail, so it _should_ alias *anything.
3217 It is not worth adding a new option or renaming the existing one,
3218 since this case is relatively obscure. */
3219 if ((TREE_CODE (t) == INTEGER_CST
3220 && integer_zerop (t))
3221 /* The only valid CONSTRUCTORs in gimple with pointer typed
3222 elements are zero-initializer. But in IPA mode we also
3223 process global initializers, so verify at least. */
3224 || (TREE_CODE (t) == CONSTRUCTOR
3225 && CONSTRUCTOR_NELTS (t) == 0))
3227 if (flag_delete_null_pointer_checks)
3228 temp.var = nothing_id;
3229 else
3230 temp.var = nonlocal_id;
3231 temp.type = ADDRESSOF;
3232 temp.offset = 0;
3233 VEC_safe_push (ce_s, heap, *results, &temp);
3234 return;
3237 /* String constants are read-only. */
3238 if (TREE_CODE (t) == STRING_CST)
3240 temp.var = readonly_id;
3241 temp.type = SCALAR;
3242 temp.offset = 0;
3243 VEC_safe_push (ce_s, heap, *results, &temp);
3244 return;
3247 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3249 case tcc_expression:
3251 switch (TREE_CODE (t))
3253 case ADDR_EXPR:
3254 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3255 return;
3256 default:;
3258 break;
3260 case tcc_reference:
3262 switch (TREE_CODE (t))
3264 case MEM_REF:
3266 struct constraint_expr cs;
3267 varinfo_t vi, curr;
3268 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0),
3269 TREE_OPERAND (t, 1), results);
3270 do_deref (results);
3272 /* If we are not taking the address then make sure to process
3273 all subvariables we might access. */
3274 if (address_p)
3275 return;
3277 cs = *VEC_last (ce_s, *results);
3278 if (cs.type == DEREF
3279 && type_can_have_subvars (TREE_TYPE (t)))
3281 /* For dereferences this means we have to defer it
3282 to solving time. */
3283 VEC_last (ce_s, *results)->offset = UNKNOWN_OFFSET;
3284 return;
3286 if (cs.type != SCALAR)
3287 return;
3289 vi = get_varinfo (cs.var);
3290 curr = vi->next;
3291 if (!vi->is_full_var
3292 && curr)
3294 unsigned HOST_WIDE_INT size;
3295 if (host_integerp (TYPE_SIZE (TREE_TYPE (t)), 1))
3296 size = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (t)));
3297 else
3298 size = -1;
3299 for (; curr; curr = curr->next)
3301 if (curr->offset - vi->offset < size)
3303 cs.var = curr->id;
3304 VEC_safe_push (ce_s, heap, *results, &cs);
3306 else
3307 break;
3310 return;
3312 case ARRAY_REF:
3313 case ARRAY_RANGE_REF:
3314 case COMPONENT_REF:
3315 get_constraint_for_component_ref (t, results, address_p, lhs_p);
3316 return;
3317 case VIEW_CONVERT_EXPR:
3318 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p,
3319 lhs_p);
3320 return;
3321 /* We are missing handling for TARGET_MEM_REF here. */
3322 default:;
3324 break;
3326 case tcc_exceptional:
3328 switch (TREE_CODE (t))
3330 case SSA_NAME:
3332 get_constraint_for_ssa_var (t, results, address_p);
3333 return;
3335 case CONSTRUCTOR:
3337 unsigned int i;
3338 tree val;
3339 VEC (ce_s, heap) *tmp = NULL;
3340 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
3342 struct constraint_expr *rhsp;
3343 unsigned j;
3344 get_constraint_for_1 (val, &tmp, address_p, lhs_p);
3345 FOR_EACH_VEC_ELT (ce_s, tmp, j, rhsp)
3346 VEC_safe_push (ce_s, heap, *results, rhsp);
3347 VEC_truncate (ce_s, tmp, 0);
3349 VEC_free (ce_s, heap, tmp);
3350 /* We do not know whether the constructor was complete,
3351 so technically we have to add &NOTHING or &ANYTHING
3352 like we do for an empty constructor as well. */
3353 return;
3355 default:;
3357 break;
3359 case tcc_declaration:
3361 get_constraint_for_ssa_var (t, results, address_p);
3362 return;
3364 case tcc_constant:
3366 /* We cannot refer to automatic variables through constants. */
3367 temp.type = ADDRESSOF;
3368 temp.var = nonlocal_id;
3369 temp.offset = 0;
3370 VEC_safe_push (ce_s, heap, *results, &temp);
3371 return;
3373 default:;
3376 /* The default fallback is a constraint from anything. */
3377 temp.type = ADDRESSOF;
3378 temp.var = anything_id;
3379 temp.offset = 0;
3380 VEC_safe_push (ce_s, heap, *results, &temp);
3383 /* Given a gimple tree T, return the constraint expression vector for it. */
3385 static void
3386 get_constraint_for (tree t, VEC (ce_s, heap) **results)
3388 gcc_assert (VEC_length (ce_s, *results) == 0);
3390 get_constraint_for_1 (t, results, false, true);
3393 /* Given a gimple tree T, return the constraint expression vector for it
3394 to be used as the rhs of a constraint. */
3396 static void
3397 get_constraint_for_rhs (tree t, VEC (ce_s, heap) **results)
3399 gcc_assert (VEC_length (ce_s, *results) == 0);
3401 get_constraint_for_1 (t, results, false, false);
3405 /* Efficiently generates constraints from all entries in *RHSC to all
3406 entries in *LHSC. */
3408 static void
3409 process_all_all_constraints (VEC (ce_s, heap) *lhsc, VEC (ce_s, heap) *rhsc)
3411 struct constraint_expr *lhsp, *rhsp;
3412 unsigned i, j;
3414 if (VEC_length (ce_s, lhsc) <= 1
3415 || VEC_length (ce_s, rhsc) <= 1)
3417 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
3418 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
3419 process_constraint (new_constraint (*lhsp, *rhsp));
3421 else
3423 struct constraint_expr tmp;
3424 tmp = new_scalar_tmp_constraint_exp ("allalltmp");
3425 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
3426 process_constraint (new_constraint (tmp, *rhsp));
3427 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
3428 process_constraint (new_constraint (*lhsp, tmp));
3432 /* Handle aggregate copies by expanding into copies of the respective
3433 fields of the structures. */
3435 static void
3436 do_structure_copy (tree lhsop, tree rhsop)
3438 struct constraint_expr *lhsp, *rhsp;
3439 VEC (ce_s, heap) *lhsc = NULL, *rhsc = NULL;
3440 unsigned j;
3442 get_constraint_for (lhsop, &lhsc);
3443 get_constraint_for_rhs (rhsop, &rhsc);
3444 lhsp = VEC_index (ce_s, lhsc, 0);
3445 rhsp = VEC_index (ce_s, rhsc, 0);
3446 if (lhsp->type == DEREF
3447 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3448 || rhsp->type == DEREF)
3450 if (lhsp->type == DEREF)
3452 gcc_assert (VEC_length (ce_s, lhsc) == 1);
3453 lhsp->offset = UNKNOWN_OFFSET;
3455 if (rhsp->type == DEREF)
3457 gcc_assert (VEC_length (ce_s, rhsc) == 1);
3458 rhsp->offset = UNKNOWN_OFFSET;
3460 process_all_all_constraints (lhsc, rhsc);
3462 else if (lhsp->type == SCALAR
3463 && (rhsp->type == SCALAR
3464 || rhsp->type == ADDRESSOF))
3466 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3467 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
3468 unsigned k = 0;
3469 get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize);
3470 get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize);
3471 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp);)
3473 varinfo_t lhsv, rhsv;
3474 rhsp = VEC_index (ce_s, rhsc, k);
3475 lhsv = get_varinfo (lhsp->var);
3476 rhsv = get_varinfo (rhsp->var);
3477 if (lhsv->may_have_pointers
3478 && (lhsv->is_full_var
3479 || rhsv->is_full_var
3480 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3481 rhsv->offset + lhsoffset, rhsv->size)))
3482 process_constraint (new_constraint (*lhsp, *rhsp));
3483 if (!rhsv->is_full_var
3484 && (lhsv->is_full_var
3485 || (lhsv->offset + rhsoffset + lhsv->size
3486 > rhsv->offset + lhsoffset + rhsv->size)))
3488 ++k;
3489 if (k >= VEC_length (ce_s, rhsc))
3490 break;
3492 else
3493 ++j;
3496 else
3497 gcc_unreachable ();
3499 VEC_free (ce_s, heap, lhsc);
3500 VEC_free (ce_s, heap, rhsc);
3503 /* Create constraints ID = { rhsc }. */
3505 static void
3506 make_constraints_to (unsigned id, VEC(ce_s, heap) *rhsc)
3508 struct constraint_expr *c;
3509 struct constraint_expr includes;
3510 unsigned int j;
3512 includes.var = id;
3513 includes.offset = 0;
3514 includes.type = SCALAR;
3516 FOR_EACH_VEC_ELT (ce_s, rhsc, j, c)
3517 process_constraint (new_constraint (includes, *c));
3520 /* Create a constraint ID = OP. */
3522 static void
3523 make_constraint_to (unsigned id, tree op)
3525 VEC(ce_s, heap) *rhsc = NULL;
3526 get_constraint_for_rhs (op, &rhsc);
3527 make_constraints_to (id, rhsc);
3528 VEC_free (ce_s, heap, rhsc);
3531 /* Create a constraint ID = &FROM. */
3533 static void
3534 make_constraint_from (varinfo_t vi, int from)
3536 struct constraint_expr lhs, rhs;
3538 lhs.var = vi->id;
3539 lhs.offset = 0;
3540 lhs.type = SCALAR;
3542 rhs.var = from;
3543 rhs.offset = 0;
3544 rhs.type = ADDRESSOF;
3545 process_constraint (new_constraint (lhs, rhs));
3548 /* Create a constraint ID = FROM. */
3550 static void
3551 make_copy_constraint (varinfo_t vi, int from)
3553 struct constraint_expr lhs, rhs;
3555 lhs.var = vi->id;
3556 lhs.offset = 0;
3557 lhs.type = SCALAR;
3559 rhs.var = from;
3560 rhs.offset = 0;
3561 rhs.type = SCALAR;
3562 process_constraint (new_constraint (lhs, rhs));
3565 /* Make constraints necessary to make OP escape. */
3567 static void
3568 make_escape_constraint (tree op)
3570 make_constraint_to (escaped_id, op);
3573 /* Add constraints to that the solution of VI is transitively closed. */
3575 static void
3576 make_transitive_closure_constraints (varinfo_t vi)
3578 struct constraint_expr lhs, rhs;
3580 /* VAR = *VAR; */
3581 lhs.type = SCALAR;
3582 lhs.var = vi->id;
3583 lhs.offset = 0;
3584 rhs.type = DEREF;
3585 rhs.var = vi->id;
3586 rhs.offset = 0;
3587 process_constraint (new_constraint (lhs, rhs));
3589 /* VAR = VAR + UNKNOWN; */
3590 lhs.type = SCALAR;
3591 lhs.var = vi->id;
3592 lhs.offset = 0;
3593 rhs.type = SCALAR;
3594 rhs.var = vi->id;
3595 rhs.offset = UNKNOWN_OFFSET;
3596 process_constraint (new_constraint (lhs, rhs));
3599 /* Temporary storage for fake var decls. */
3600 struct obstack fake_var_decl_obstack;
3602 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3604 static tree
3605 build_fake_var_decl (tree type)
3607 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl);
3608 memset (decl, 0, sizeof (struct tree_var_decl));
3609 TREE_SET_CODE (decl, VAR_DECL);
3610 TREE_TYPE (decl) = type;
3611 DECL_UID (decl) = allocate_decl_uid ();
3612 SET_DECL_PT_UID (decl, -1);
3613 layout_decl (decl, 0);
3614 return decl;
3617 /* Create a new artificial heap variable with NAME.
3618 Return the created variable. */
3620 static varinfo_t
3621 make_heapvar (const char *name)
3623 varinfo_t vi;
3624 tree heapvar;
3626 heapvar = build_fake_var_decl (ptr_type_node);
3627 DECL_EXTERNAL (heapvar) = 1;
3629 vi = new_var_info (heapvar, name);
3630 vi->is_artificial_var = true;
3631 vi->is_heap_var = true;
3632 vi->is_unknown_size_var = true;
3633 vi->offset = 0;
3634 vi->fullsize = ~0;
3635 vi->size = ~0;
3636 vi->is_full_var = true;
3637 insert_vi_for_tree (heapvar, vi);
3639 return vi;
3642 /* Create a new artificial heap variable with NAME and make a
3643 constraint from it to LHS. Set flags according to a tag used
3644 for tracking restrict pointers. */
3646 static varinfo_t
3647 make_constraint_from_restrict (varinfo_t lhs, const char *name)
3649 varinfo_t vi = make_heapvar (name);
3650 vi->is_global_var = 1;
3651 vi->may_have_pointers = 1;
3652 make_constraint_from (lhs, vi->id);
3653 return vi;
3656 /* Create a new artificial heap variable with NAME and make a
3657 constraint from it to LHS. Set flags according to a tag used
3658 for tracking restrict pointers and make the artificial heap
3659 point to global memory. */
3661 static varinfo_t
3662 make_constraint_from_global_restrict (varinfo_t lhs, const char *name)
3664 varinfo_t vi = make_constraint_from_restrict (lhs, name);
3665 make_copy_constraint (vi, nonlocal_id);
3666 return vi;
3669 /* In IPA mode there are varinfos for different aspects of reach
3670 function designator. One for the points-to set of the return
3671 value, one for the variables that are clobbered by the function,
3672 one for its uses and one for each parameter (including a single
3673 glob for remaining variadic arguments). */
3675 enum { fi_clobbers = 1, fi_uses = 2,
3676 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 };
3678 /* Get a constraint for the requested part of a function designator FI
3679 when operating in IPA mode. */
3681 static struct constraint_expr
3682 get_function_part_constraint (varinfo_t fi, unsigned part)
3684 struct constraint_expr c;
3686 gcc_assert (in_ipa_mode);
3688 if (fi->id == anything_id)
3690 /* ??? We probably should have a ANYFN special variable. */
3691 c.var = anything_id;
3692 c.offset = 0;
3693 c.type = SCALAR;
3695 else if (TREE_CODE (fi->decl) == FUNCTION_DECL)
3697 varinfo_t ai = first_vi_for_offset (fi, part);
3698 if (ai)
3699 c.var = ai->id;
3700 else
3701 c.var = anything_id;
3702 c.offset = 0;
3703 c.type = SCALAR;
3705 else
3707 c.var = fi->id;
3708 c.offset = part;
3709 c.type = DEREF;
3712 return c;
3715 /* For non-IPA mode, generate constraints necessary for a call on the
3716 RHS. */
3718 static void
3719 handle_rhs_call (gimple stmt, VEC(ce_s, heap) **results)
3721 struct constraint_expr rhsc;
3722 unsigned i;
3723 bool returns_uses = false;
3725 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3727 tree arg = gimple_call_arg (stmt, i);
3728 int flags = gimple_call_arg_flags (stmt, i);
3730 /* If the argument is not used we can ignore it. */
3731 if (flags & EAF_UNUSED)
3732 continue;
3734 /* As we compute ESCAPED context-insensitive we do not gain
3735 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3736 set. The argument would still get clobbered through the
3737 escape solution.
3738 ??? We might get away with less (and more precise) constraints
3739 if using a temporary for transitively closing things. */
3740 if ((flags & EAF_NOCLOBBER)
3741 && (flags & EAF_NOESCAPE))
3743 varinfo_t uses = get_call_use_vi (stmt);
3744 if (!(flags & EAF_DIRECT))
3745 make_transitive_closure_constraints (uses);
3746 make_constraint_to (uses->id, arg);
3747 returns_uses = true;
3749 else if (flags & EAF_NOESCAPE)
3751 varinfo_t uses = get_call_use_vi (stmt);
3752 varinfo_t clobbers = get_call_clobber_vi (stmt);
3753 if (!(flags & EAF_DIRECT))
3755 make_transitive_closure_constraints (uses);
3756 make_transitive_closure_constraints (clobbers);
3758 make_constraint_to (uses->id, arg);
3759 make_constraint_to (clobbers->id, arg);
3760 returns_uses = true;
3762 else
3763 make_escape_constraint (arg);
3766 /* If we added to the calls uses solution make sure we account for
3767 pointers to it to be returned. */
3768 if (returns_uses)
3770 rhsc.var = get_call_use_vi (stmt)->id;
3771 rhsc.offset = 0;
3772 rhsc.type = SCALAR;
3773 VEC_safe_push (ce_s, heap, *results, &rhsc);
3776 /* The static chain escapes as well. */
3777 if (gimple_call_chain (stmt))
3778 make_escape_constraint (gimple_call_chain (stmt));
3780 /* And if we applied NRV the address of the return slot escapes as well. */
3781 if (gimple_call_return_slot_opt_p (stmt)
3782 && gimple_call_lhs (stmt) != NULL_TREE
3783 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3785 VEC(ce_s, heap) *tmpc = NULL;
3786 struct constraint_expr lhsc, *c;
3787 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3788 lhsc.var = escaped_id;
3789 lhsc.offset = 0;
3790 lhsc.type = SCALAR;
3791 FOR_EACH_VEC_ELT (ce_s, tmpc, i, c)
3792 process_constraint (new_constraint (lhsc, *c));
3793 VEC_free(ce_s, heap, tmpc);
3796 /* Regular functions return nonlocal memory. */
3797 rhsc.var = nonlocal_id;
3798 rhsc.offset = 0;
3799 rhsc.type = SCALAR;
3800 VEC_safe_push (ce_s, heap, *results, &rhsc);
3803 /* For non-IPA mode, generate constraints necessary for a call
3804 that returns a pointer and assigns it to LHS. This simply makes
3805 the LHS point to global and escaped variables. */
3807 static void
3808 handle_lhs_call (gimple stmt, tree lhs, int flags, VEC(ce_s, heap) *rhsc,
3809 tree fndecl)
3811 VEC(ce_s, heap) *lhsc = NULL;
3813 get_constraint_for (lhs, &lhsc);
3814 /* If the store is to a global decl make sure to
3815 add proper escape constraints. */
3816 lhs = get_base_address (lhs);
3817 if (lhs
3818 && DECL_P (lhs)
3819 && is_global_var (lhs))
3821 struct constraint_expr tmpc;
3822 tmpc.var = escaped_id;
3823 tmpc.offset = 0;
3824 tmpc.type = SCALAR;
3825 VEC_safe_push (ce_s, heap, lhsc, &tmpc);
3828 /* If the call returns an argument unmodified override the rhs
3829 constraints. */
3830 flags = gimple_call_return_flags (stmt);
3831 if (flags & ERF_RETURNS_ARG
3832 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
3834 tree arg;
3835 rhsc = NULL;
3836 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
3837 get_constraint_for (arg, &rhsc);
3838 process_all_all_constraints (lhsc, rhsc);
3839 VEC_free (ce_s, heap, rhsc);
3841 else if (flags & ERF_NOALIAS)
3843 varinfo_t vi;
3844 struct constraint_expr tmpc;
3845 rhsc = NULL;
3846 vi = make_heapvar ("HEAP");
3847 /* We delay marking allocated storage global until we know if
3848 it escapes. */
3849 DECL_EXTERNAL (vi->decl) = 0;
3850 vi->is_global_var = 0;
3851 /* If this is not a real malloc call assume the memory was
3852 initialized and thus may point to global memory. All
3853 builtin functions with the malloc attribute behave in a sane way. */
3854 if (!fndecl
3855 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3856 make_constraint_from (vi, nonlocal_id);
3857 tmpc.var = vi->id;
3858 tmpc.offset = 0;
3859 tmpc.type = ADDRESSOF;
3860 VEC_safe_push (ce_s, heap, rhsc, &tmpc);
3863 process_all_all_constraints (lhsc, rhsc);
3865 VEC_free (ce_s, heap, lhsc);
3868 /* For non-IPA mode, generate constraints necessary for a call of a
3869 const function that returns a pointer in the statement STMT. */
3871 static void
3872 handle_const_call (gimple stmt, VEC(ce_s, heap) **results)
3874 struct constraint_expr rhsc;
3875 unsigned int k;
3877 /* Treat nested const functions the same as pure functions as far
3878 as the static chain is concerned. */
3879 if (gimple_call_chain (stmt))
3881 varinfo_t uses = get_call_use_vi (stmt);
3882 make_transitive_closure_constraints (uses);
3883 make_constraint_to (uses->id, gimple_call_chain (stmt));
3884 rhsc.var = uses->id;
3885 rhsc.offset = 0;
3886 rhsc.type = SCALAR;
3887 VEC_safe_push (ce_s, heap, *results, &rhsc);
3890 /* May return arguments. */
3891 for (k = 0; k < gimple_call_num_args (stmt); ++k)
3893 tree arg = gimple_call_arg (stmt, k);
3894 VEC(ce_s, heap) *argc = NULL;
3895 unsigned i;
3896 struct constraint_expr *argp;
3897 get_constraint_for_rhs (arg, &argc);
3898 FOR_EACH_VEC_ELT (ce_s, argc, i, argp)
3899 VEC_safe_push (ce_s, heap, *results, argp);
3900 VEC_free(ce_s, heap, argc);
3903 /* May return addresses of globals. */
3904 rhsc.var = nonlocal_id;
3905 rhsc.offset = 0;
3906 rhsc.type = ADDRESSOF;
3907 VEC_safe_push (ce_s, heap, *results, &rhsc);
3910 /* For non-IPA mode, generate constraints necessary for a call to a
3911 pure function in statement STMT. */
3913 static void
3914 handle_pure_call (gimple stmt, VEC(ce_s, heap) **results)
3916 struct constraint_expr rhsc;
3917 unsigned i;
3918 varinfo_t uses = NULL;
3920 /* Memory reached from pointer arguments is call-used. */
3921 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3923 tree arg = gimple_call_arg (stmt, i);
3924 if (!uses)
3926 uses = get_call_use_vi (stmt);
3927 make_transitive_closure_constraints (uses);
3929 make_constraint_to (uses->id, arg);
3932 /* The static chain is used as well. */
3933 if (gimple_call_chain (stmt))
3935 if (!uses)
3937 uses = get_call_use_vi (stmt);
3938 make_transitive_closure_constraints (uses);
3940 make_constraint_to (uses->id, gimple_call_chain (stmt));
3943 /* Pure functions may return call-used and nonlocal memory. */
3944 if (uses)
3946 rhsc.var = uses->id;
3947 rhsc.offset = 0;
3948 rhsc.type = SCALAR;
3949 VEC_safe_push (ce_s, heap, *results, &rhsc);
3951 rhsc.var = nonlocal_id;
3952 rhsc.offset = 0;
3953 rhsc.type = SCALAR;
3954 VEC_safe_push (ce_s, heap, *results, &rhsc);
3958 /* Return the varinfo for the callee of CALL. */
3960 static varinfo_t
3961 get_fi_for_callee (gimple call)
3963 tree decl, fn = gimple_call_fn (call);
3965 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
3966 fn = OBJ_TYPE_REF_EXPR (fn);
3968 /* If we can directly resolve the function being called, do so.
3969 Otherwise, it must be some sort of indirect expression that
3970 we should still be able to handle. */
3971 decl = gimple_call_addr_fndecl (fn);
3972 if (decl)
3973 return get_vi_for_tree (decl);
3975 /* If the function is anything other than a SSA name pointer we have no
3976 clue and should be getting ANYFN (well, ANYTHING for now). */
3977 if (!fn || TREE_CODE (fn) != SSA_NAME)
3978 return get_varinfo (anything_id);
3980 if ((TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
3981 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL)
3982 && SSA_NAME_IS_DEFAULT_DEF (fn))
3983 fn = SSA_NAME_VAR (fn);
3985 return get_vi_for_tree (fn);
3988 /* Create constraints for the builtin call T. Return true if the call
3989 was handled, otherwise false. */
3991 static bool
3992 find_func_aliases_for_builtin_call (gimple t)
3994 tree fndecl = gimple_call_fndecl (t);
3995 VEC(ce_s, heap) *lhsc = NULL;
3996 VEC(ce_s, heap) *rhsc = NULL;
3997 varinfo_t fi;
3999 if (fndecl != NULL_TREE
4000 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4001 /* ??? All builtins that are handled here need to be handled
4002 in the alias-oracle query functions explicitly! */
4003 switch (DECL_FUNCTION_CODE (fndecl))
4005 /* All the following functions return a pointer to the same object
4006 as their first argument points to. The functions do not add
4007 to the ESCAPED solution. The functions make the first argument
4008 pointed to memory point to what the second argument pointed to
4009 memory points to. */
4010 case BUILT_IN_STRCPY:
4011 case BUILT_IN_STRNCPY:
4012 case BUILT_IN_BCOPY:
4013 case BUILT_IN_MEMCPY:
4014 case BUILT_IN_MEMMOVE:
4015 case BUILT_IN_MEMPCPY:
4016 case BUILT_IN_STPCPY:
4017 case BUILT_IN_STPNCPY:
4018 case BUILT_IN_STRCAT:
4019 case BUILT_IN_STRNCAT:
4020 case BUILT_IN_STRCPY_CHK:
4021 case BUILT_IN_STRNCPY_CHK:
4022 case BUILT_IN_MEMCPY_CHK:
4023 case BUILT_IN_MEMMOVE_CHK:
4024 case BUILT_IN_MEMPCPY_CHK:
4025 case BUILT_IN_STPCPY_CHK:
4026 case BUILT_IN_STPNCPY_CHK:
4027 case BUILT_IN_STRCAT_CHK:
4028 case BUILT_IN_STRNCAT_CHK:
4029 case BUILT_IN_TM_MEMCPY:
4030 case BUILT_IN_TM_MEMMOVE:
4032 tree res = gimple_call_lhs (t);
4033 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4034 == BUILT_IN_BCOPY ? 1 : 0));
4035 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4036 == BUILT_IN_BCOPY ? 0 : 1));
4037 if (res != NULL_TREE)
4039 get_constraint_for (res, &lhsc);
4040 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4041 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
4042 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4043 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
4044 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK
4045 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK)
4046 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4047 else
4048 get_constraint_for (dest, &rhsc);
4049 process_all_all_constraints (lhsc, rhsc);
4050 VEC_free (ce_s, heap, lhsc);
4051 VEC_free (ce_s, heap, rhsc);
4053 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4054 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4055 do_deref (&lhsc);
4056 do_deref (&rhsc);
4057 process_all_all_constraints (lhsc, rhsc);
4058 VEC_free (ce_s, heap, lhsc);
4059 VEC_free (ce_s, heap, rhsc);
4060 return true;
4062 case BUILT_IN_MEMSET:
4063 case BUILT_IN_MEMSET_CHK:
4064 case BUILT_IN_TM_MEMSET:
4066 tree res = gimple_call_lhs (t);
4067 tree dest = gimple_call_arg (t, 0);
4068 unsigned i;
4069 ce_s *lhsp;
4070 struct constraint_expr ac;
4071 if (res != NULL_TREE)
4073 get_constraint_for (res, &lhsc);
4074 get_constraint_for (dest, &rhsc);
4075 process_all_all_constraints (lhsc, rhsc);
4076 VEC_free (ce_s, heap, lhsc);
4077 VEC_free (ce_s, heap, rhsc);
4079 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4080 do_deref (&lhsc);
4081 if (flag_delete_null_pointer_checks
4082 && integer_zerop (gimple_call_arg (t, 1)))
4084 ac.type = ADDRESSOF;
4085 ac.var = nothing_id;
4087 else
4089 ac.type = SCALAR;
4090 ac.var = integer_id;
4092 ac.offset = 0;
4093 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4094 process_constraint (new_constraint (*lhsp, ac));
4095 VEC_free (ce_s, heap, lhsc);
4096 return true;
4098 case BUILT_IN_ASSUME_ALIGNED:
4100 tree res = gimple_call_lhs (t);
4101 tree dest = gimple_call_arg (t, 0);
4102 if (res != NULL_TREE)
4104 get_constraint_for (res, &lhsc);
4105 get_constraint_for (dest, &rhsc);
4106 process_all_all_constraints (lhsc, rhsc);
4107 VEC_free (ce_s, heap, lhsc);
4108 VEC_free (ce_s, heap, rhsc);
4110 return true;
4112 /* All the following functions do not return pointers, do not
4113 modify the points-to sets of memory reachable from their
4114 arguments and do not add to the ESCAPED solution. */
4115 case BUILT_IN_SINCOS:
4116 case BUILT_IN_SINCOSF:
4117 case BUILT_IN_SINCOSL:
4118 case BUILT_IN_FREXP:
4119 case BUILT_IN_FREXPF:
4120 case BUILT_IN_FREXPL:
4121 case BUILT_IN_GAMMA_R:
4122 case BUILT_IN_GAMMAF_R:
4123 case BUILT_IN_GAMMAL_R:
4124 case BUILT_IN_LGAMMA_R:
4125 case BUILT_IN_LGAMMAF_R:
4126 case BUILT_IN_LGAMMAL_R:
4127 case BUILT_IN_MODF:
4128 case BUILT_IN_MODFF:
4129 case BUILT_IN_MODFL:
4130 case BUILT_IN_REMQUO:
4131 case BUILT_IN_REMQUOF:
4132 case BUILT_IN_REMQUOL:
4133 case BUILT_IN_FREE:
4134 return true;
4135 case BUILT_IN_STRDUP:
4136 case BUILT_IN_STRNDUP:
4137 if (gimple_call_lhs (t))
4139 handle_lhs_call (t, gimple_call_lhs (t), gimple_call_flags (t),
4140 NULL, fndecl);
4141 get_constraint_for_ptr_offset (gimple_call_lhs (t),
4142 NULL_TREE, &lhsc);
4143 get_constraint_for_ptr_offset (gimple_call_arg (t, 0),
4144 NULL_TREE, &rhsc);
4145 do_deref (&lhsc);
4146 do_deref (&rhsc);
4147 process_all_all_constraints (lhsc, rhsc);
4148 VEC_free (ce_s, heap, lhsc);
4149 VEC_free (ce_s, heap, rhsc);
4150 return true;
4152 break;
4153 /* Trampolines are special - they set up passing the static
4154 frame. */
4155 case BUILT_IN_INIT_TRAMPOLINE:
4157 tree tramp = gimple_call_arg (t, 0);
4158 tree nfunc = gimple_call_arg (t, 1);
4159 tree frame = gimple_call_arg (t, 2);
4160 unsigned i;
4161 struct constraint_expr lhs, *rhsp;
4162 if (in_ipa_mode)
4164 varinfo_t nfi = NULL;
4165 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR);
4166 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0));
4167 if (nfi)
4169 lhs = get_function_part_constraint (nfi, fi_static_chain);
4170 get_constraint_for (frame, &rhsc);
4171 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4172 process_constraint (new_constraint (lhs, *rhsp));
4173 VEC_free (ce_s, heap, rhsc);
4175 /* Make the frame point to the function for
4176 the trampoline adjustment call. */
4177 get_constraint_for (tramp, &lhsc);
4178 do_deref (&lhsc);
4179 get_constraint_for (nfunc, &rhsc);
4180 process_all_all_constraints (lhsc, rhsc);
4181 VEC_free (ce_s, heap, rhsc);
4182 VEC_free (ce_s, heap, lhsc);
4184 return true;
4187 /* Else fallthru to generic handling which will let
4188 the frame escape. */
4189 break;
4191 case BUILT_IN_ADJUST_TRAMPOLINE:
4193 tree tramp = gimple_call_arg (t, 0);
4194 tree res = gimple_call_lhs (t);
4195 if (in_ipa_mode && res)
4197 get_constraint_for (res, &lhsc);
4198 get_constraint_for (tramp, &rhsc);
4199 do_deref (&rhsc);
4200 process_all_all_constraints (lhsc, rhsc);
4201 VEC_free (ce_s, heap, rhsc);
4202 VEC_free (ce_s, heap, lhsc);
4204 return true;
4206 CASE_BUILT_IN_TM_STORE (1):
4207 CASE_BUILT_IN_TM_STORE (2):
4208 CASE_BUILT_IN_TM_STORE (4):
4209 CASE_BUILT_IN_TM_STORE (8):
4210 CASE_BUILT_IN_TM_STORE (FLOAT):
4211 CASE_BUILT_IN_TM_STORE (DOUBLE):
4212 CASE_BUILT_IN_TM_STORE (LDOUBLE):
4213 CASE_BUILT_IN_TM_STORE (M64):
4214 CASE_BUILT_IN_TM_STORE (M128):
4215 CASE_BUILT_IN_TM_STORE (M256):
4217 tree addr = gimple_call_arg (t, 0);
4218 tree src = gimple_call_arg (t, 1);
4220 get_constraint_for (addr, &lhsc);
4221 do_deref (&lhsc);
4222 get_constraint_for (src, &rhsc);
4223 process_all_all_constraints (lhsc, rhsc);
4224 VEC_free (ce_s, heap, lhsc);
4225 VEC_free (ce_s, heap, rhsc);
4226 return true;
4228 CASE_BUILT_IN_TM_LOAD (1):
4229 CASE_BUILT_IN_TM_LOAD (2):
4230 CASE_BUILT_IN_TM_LOAD (4):
4231 CASE_BUILT_IN_TM_LOAD (8):
4232 CASE_BUILT_IN_TM_LOAD (FLOAT):
4233 CASE_BUILT_IN_TM_LOAD (DOUBLE):
4234 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
4235 CASE_BUILT_IN_TM_LOAD (M64):
4236 CASE_BUILT_IN_TM_LOAD (M128):
4237 CASE_BUILT_IN_TM_LOAD (M256):
4239 tree dest = gimple_call_lhs (t);
4240 tree addr = gimple_call_arg (t, 0);
4242 get_constraint_for (dest, &lhsc);
4243 get_constraint_for (addr, &rhsc);
4244 do_deref (&rhsc);
4245 process_all_all_constraints (lhsc, rhsc);
4246 VEC_free (ce_s, heap, lhsc);
4247 VEC_free (ce_s, heap, rhsc);
4248 return true;
4250 /* Variadic argument handling needs to be handled in IPA
4251 mode as well. */
4252 case BUILT_IN_VA_START:
4254 tree valist = gimple_call_arg (t, 0);
4255 struct constraint_expr rhs, *lhsp;
4256 unsigned i;
4257 get_constraint_for (valist, &lhsc);
4258 do_deref (&lhsc);
4259 /* The va_list gets access to pointers in variadic
4260 arguments. Which we know in the case of IPA analysis
4261 and otherwise are just all nonlocal variables. */
4262 if (in_ipa_mode)
4264 fi = lookup_vi_for_tree (cfun->decl);
4265 rhs = get_function_part_constraint (fi, ~0);
4266 rhs.type = ADDRESSOF;
4268 else
4270 rhs.var = nonlocal_id;
4271 rhs.type = ADDRESSOF;
4272 rhs.offset = 0;
4274 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4275 process_constraint (new_constraint (*lhsp, rhs));
4276 VEC_free (ce_s, heap, lhsc);
4277 /* va_list is clobbered. */
4278 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4279 return true;
4281 /* va_end doesn't have any effect that matters. */
4282 case BUILT_IN_VA_END:
4283 return true;
4284 /* Alternate return. Simply give up for now. */
4285 case BUILT_IN_RETURN:
4287 fi = NULL;
4288 if (!in_ipa_mode
4289 || !(fi = get_vi_for_tree (cfun->decl)))
4290 make_constraint_from (get_varinfo (escaped_id), anything_id);
4291 else if (in_ipa_mode
4292 && fi != NULL)
4294 struct constraint_expr lhs, rhs;
4295 lhs = get_function_part_constraint (fi, fi_result);
4296 rhs.var = anything_id;
4297 rhs.offset = 0;
4298 rhs.type = SCALAR;
4299 process_constraint (new_constraint (lhs, rhs));
4301 return true;
4303 /* printf-style functions may have hooks to set pointers to
4304 point to somewhere into the generated string. Leave them
4305 for a later excercise... */
4306 default:
4307 /* Fallthru to general call handling. */;
4310 return false;
4313 /* Create constraints for the call T. */
4315 static void
4316 find_func_aliases_for_call (gimple t)
4318 tree fndecl = gimple_call_fndecl (t);
4319 VEC(ce_s, heap) *lhsc = NULL;
4320 VEC(ce_s, heap) *rhsc = NULL;
4321 varinfo_t fi;
4323 if (fndecl != NULL_TREE
4324 && DECL_BUILT_IN (fndecl)
4325 && find_func_aliases_for_builtin_call (t))
4326 return;
4328 fi = get_fi_for_callee (t);
4329 if (!in_ipa_mode
4330 || (fndecl && !fi->is_fn_info))
4332 VEC(ce_s, heap) *rhsc = NULL;
4333 int flags = gimple_call_flags (t);
4335 /* Const functions can return their arguments and addresses
4336 of global memory but not of escaped memory. */
4337 if (flags & (ECF_CONST|ECF_NOVOPS))
4339 if (gimple_call_lhs (t))
4340 handle_const_call (t, &rhsc);
4342 /* Pure functions can return addresses in and of memory
4343 reachable from their arguments, but they are not an escape
4344 point for reachable memory of their arguments. */
4345 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4346 handle_pure_call (t, &rhsc);
4347 else
4348 handle_rhs_call (t, &rhsc);
4349 if (gimple_call_lhs (t))
4350 handle_lhs_call (t, gimple_call_lhs (t), flags, rhsc, fndecl);
4351 VEC_free (ce_s, heap, rhsc);
4353 else
4355 tree lhsop;
4356 unsigned j;
4358 /* Assign all the passed arguments to the appropriate incoming
4359 parameters of the function. */
4360 for (j = 0; j < gimple_call_num_args (t); j++)
4362 struct constraint_expr lhs ;
4363 struct constraint_expr *rhsp;
4364 tree arg = gimple_call_arg (t, j);
4366 get_constraint_for_rhs (arg, &rhsc);
4367 lhs = get_function_part_constraint (fi, fi_parm_base + j);
4368 while (VEC_length (ce_s, rhsc) != 0)
4370 rhsp = VEC_last (ce_s, rhsc);
4371 process_constraint (new_constraint (lhs, *rhsp));
4372 VEC_pop (ce_s, rhsc);
4376 /* If we are returning a value, assign it to the result. */
4377 lhsop = gimple_call_lhs (t);
4378 if (lhsop)
4380 struct constraint_expr rhs;
4381 struct constraint_expr *lhsp;
4383 get_constraint_for (lhsop, &lhsc);
4384 rhs = get_function_part_constraint (fi, fi_result);
4385 if (fndecl
4386 && DECL_RESULT (fndecl)
4387 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4389 VEC(ce_s, heap) *tem = NULL;
4390 VEC_safe_push (ce_s, heap, tem, &rhs);
4391 do_deref (&tem);
4392 rhs = *VEC_index (ce_s, tem, 0);
4393 VEC_free(ce_s, heap, tem);
4395 FOR_EACH_VEC_ELT (ce_s, lhsc, j, lhsp)
4396 process_constraint (new_constraint (*lhsp, rhs));
4399 /* If we pass the result decl by reference, honor that. */
4400 if (lhsop
4401 && fndecl
4402 && DECL_RESULT (fndecl)
4403 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4405 struct constraint_expr lhs;
4406 struct constraint_expr *rhsp;
4408 get_constraint_for_address_of (lhsop, &rhsc);
4409 lhs = get_function_part_constraint (fi, fi_result);
4410 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
4411 process_constraint (new_constraint (lhs, *rhsp));
4412 VEC_free (ce_s, heap, rhsc);
4415 /* If we use a static chain, pass it along. */
4416 if (gimple_call_chain (t))
4418 struct constraint_expr lhs;
4419 struct constraint_expr *rhsp;
4421 get_constraint_for (gimple_call_chain (t), &rhsc);
4422 lhs = get_function_part_constraint (fi, fi_static_chain);
4423 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
4424 process_constraint (new_constraint (lhs, *rhsp));
4429 /* Walk statement T setting up aliasing constraints according to the
4430 references found in T. This function is the main part of the
4431 constraint builder. AI points to auxiliary alias information used
4432 when building alias sets and computing alias grouping heuristics. */
4434 static void
4435 find_func_aliases (gimple origt)
4437 gimple t = origt;
4438 VEC(ce_s, heap) *lhsc = NULL;
4439 VEC(ce_s, heap) *rhsc = NULL;
4440 struct constraint_expr *c;
4441 varinfo_t fi;
4443 /* Now build constraints expressions. */
4444 if (gimple_code (t) == GIMPLE_PHI)
4446 size_t i;
4447 unsigned int j;
4449 /* For a phi node, assign all the arguments to
4450 the result. */
4451 get_constraint_for (gimple_phi_result (t), &lhsc);
4452 for (i = 0; i < gimple_phi_num_args (t); i++)
4454 tree strippedrhs = PHI_ARG_DEF (t, i);
4456 STRIP_NOPS (strippedrhs);
4457 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
4459 FOR_EACH_VEC_ELT (ce_s, lhsc, j, c)
4461 struct constraint_expr *c2;
4462 while (VEC_length (ce_s, rhsc) > 0)
4464 c2 = VEC_last (ce_s, rhsc);
4465 process_constraint (new_constraint (*c, *c2));
4466 VEC_pop (ce_s, rhsc);
4471 /* In IPA mode, we need to generate constraints to pass call
4472 arguments through their calls. There are two cases,
4473 either a GIMPLE_CALL returning a value, or just a plain
4474 GIMPLE_CALL when we are not.
4476 In non-ipa mode, we need to generate constraints for each
4477 pointer passed by address. */
4478 else if (is_gimple_call (t))
4479 find_func_aliases_for_call (t);
4481 /* Otherwise, just a regular assignment statement. Only care about
4482 operations with pointer result, others are dealt with as escape
4483 points if they have pointer operands. */
4484 else if (is_gimple_assign (t))
4486 /* Otherwise, just a regular assignment statement. */
4487 tree lhsop = gimple_assign_lhs (t);
4488 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
4490 if (rhsop && TREE_CLOBBER_P (rhsop))
4491 /* Ignore clobbers, they don't actually store anything into
4492 the LHS. */
4494 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
4495 do_structure_copy (lhsop, rhsop);
4496 else
4498 enum tree_code code = gimple_assign_rhs_code (t);
4500 get_constraint_for (lhsop, &lhsc);
4502 if (code == POINTER_PLUS_EXPR)
4503 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4504 gimple_assign_rhs2 (t), &rhsc);
4505 else if (code == BIT_AND_EXPR
4506 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
4508 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4509 the pointer. Handle it by offsetting it by UNKNOWN. */
4510 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4511 NULL_TREE, &rhsc);
4513 else if ((CONVERT_EXPR_CODE_P (code)
4514 && !(POINTER_TYPE_P (gimple_expr_type (t))
4515 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
4516 || gimple_assign_single_p (t))
4517 get_constraint_for_rhs (rhsop, &rhsc);
4518 else if (truth_value_p (code))
4519 /* Truth value results are not pointer (parts). Or at least
4520 very very unreasonable obfuscation of a part. */
4522 else
4524 /* All other operations are merges. */
4525 VEC (ce_s, heap) *tmp = NULL;
4526 struct constraint_expr *rhsp;
4527 unsigned i, j;
4528 get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc);
4529 for (i = 2; i < gimple_num_ops (t); ++i)
4531 get_constraint_for_rhs (gimple_op (t, i), &tmp);
4532 FOR_EACH_VEC_ELT (ce_s, tmp, j, rhsp)
4533 VEC_safe_push (ce_s, heap, rhsc, rhsp);
4534 VEC_truncate (ce_s, tmp, 0);
4536 VEC_free (ce_s, heap, tmp);
4538 process_all_all_constraints (lhsc, rhsc);
4540 /* If there is a store to a global variable the rhs escapes. */
4541 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
4542 && DECL_P (lhsop)
4543 && is_global_var (lhsop)
4544 && (!in_ipa_mode
4545 || DECL_EXTERNAL (lhsop) || TREE_PUBLIC (lhsop)))
4546 make_escape_constraint (rhsop);
4548 /* Handle escapes through return. */
4549 else if (gimple_code (t) == GIMPLE_RETURN
4550 && gimple_return_retval (t) != NULL_TREE)
4552 fi = NULL;
4553 if (!in_ipa_mode
4554 || !(fi = get_vi_for_tree (cfun->decl)))
4555 make_escape_constraint (gimple_return_retval (t));
4556 else if (in_ipa_mode
4557 && fi != NULL)
4559 struct constraint_expr lhs ;
4560 struct constraint_expr *rhsp;
4561 unsigned i;
4563 lhs = get_function_part_constraint (fi, fi_result);
4564 get_constraint_for_rhs (gimple_return_retval (t), &rhsc);
4565 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4566 process_constraint (new_constraint (lhs, *rhsp));
4569 /* Handle asms conservatively by adding escape constraints to everything. */
4570 else if (gimple_code (t) == GIMPLE_ASM)
4572 unsigned i, noutputs;
4573 const char **oconstraints;
4574 const char *constraint;
4575 bool allows_mem, allows_reg, is_inout;
4577 noutputs = gimple_asm_noutputs (t);
4578 oconstraints = XALLOCAVEC (const char *, noutputs);
4580 for (i = 0; i < noutputs; ++i)
4582 tree link = gimple_asm_output_op (t, i);
4583 tree op = TREE_VALUE (link);
4585 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4586 oconstraints[i] = constraint;
4587 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4588 &allows_reg, &is_inout);
4590 /* A memory constraint makes the address of the operand escape. */
4591 if (!allows_reg && allows_mem)
4592 make_escape_constraint (build_fold_addr_expr (op));
4594 /* The asm may read global memory, so outputs may point to
4595 any global memory. */
4596 if (op)
4598 VEC(ce_s, heap) *lhsc = NULL;
4599 struct constraint_expr rhsc, *lhsp;
4600 unsigned j;
4601 get_constraint_for (op, &lhsc);
4602 rhsc.var = nonlocal_id;
4603 rhsc.offset = 0;
4604 rhsc.type = SCALAR;
4605 FOR_EACH_VEC_ELT (ce_s, lhsc, j, lhsp)
4606 process_constraint (new_constraint (*lhsp, rhsc));
4607 VEC_free (ce_s, heap, lhsc);
4610 for (i = 0; i < gimple_asm_ninputs (t); ++i)
4612 tree link = gimple_asm_input_op (t, i);
4613 tree op = TREE_VALUE (link);
4615 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4617 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4618 &allows_mem, &allows_reg);
4620 /* A memory constraint makes the address of the operand escape. */
4621 if (!allows_reg && allows_mem)
4622 make_escape_constraint (build_fold_addr_expr (op));
4623 /* Strictly we'd only need the constraint to ESCAPED if
4624 the asm clobbers memory, otherwise using something
4625 along the lines of per-call clobbers/uses would be enough. */
4626 else if (op)
4627 make_escape_constraint (op);
4631 VEC_free (ce_s, heap, rhsc);
4632 VEC_free (ce_s, heap, lhsc);
4636 /* Create a constraint adding to the clobber set of FI the memory
4637 pointed to by PTR. */
4639 static void
4640 process_ipa_clobber (varinfo_t fi, tree ptr)
4642 VEC(ce_s, heap) *ptrc = NULL;
4643 struct constraint_expr *c, lhs;
4644 unsigned i;
4645 get_constraint_for_rhs (ptr, &ptrc);
4646 lhs = get_function_part_constraint (fi, fi_clobbers);
4647 FOR_EACH_VEC_ELT (ce_s, ptrc, i, c)
4648 process_constraint (new_constraint (lhs, *c));
4649 VEC_free (ce_s, heap, ptrc);
4652 /* Walk statement T setting up clobber and use constraints according to the
4653 references found in T. This function is a main part of the
4654 IPA constraint builder. */
4656 static void
4657 find_func_clobbers (gimple origt)
4659 gimple t = origt;
4660 VEC(ce_s, heap) *lhsc = NULL;
4661 VEC(ce_s, heap) *rhsc = NULL;
4662 varinfo_t fi;
4664 /* Add constraints for clobbered/used in IPA mode.
4665 We are not interested in what automatic variables are clobbered
4666 or used as we only use the information in the caller to which
4667 they do not escape. */
4668 gcc_assert (in_ipa_mode);
4670 /* If the stmt refers to memory in any way it better had a VUSE. */
4671 if (gimple_vuse (t) == NULL_TREE)
4672 return;
4674 /* We'd better have function information for the current function. */
4675 fi = lookup_vi_for_tree (cfun->decl);
4676 gcc_assert (fi != NULL);
4678 /* Account for stores in assignments and calls. */
4679 if (gimple_vdef (t) != NULL_TREE
4680 && gimple_has_lhs (t))
4682 tree lhs = gimple_get_lhs (t);
4683 tree tem = lhs;
4684 while (handled_component_p (tem))
4685 tem = TREE_OPERAND (tem, 0);
4686 if ((DECL_P (tem)
4687 && !auto_var_in_fn_p (tem, cfun->decl))
4688 || INDIRECT_REF_P (tem)
4689 || (TREE_CODE (tem) == MEM_REF
4690 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4691 && auto_var_in_fn_p
4692 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4694 struct constraint_expr lhsc, *rhsp;
4695 unsigned i;
4696 lhsc = get_function_part_constraint (fi, fi_clobbers);
4697 get_constraint_for_address_of (lhs, &rhsc);
4698 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4699 process_constraint (new_constraint (lhsc, *rhsp));
4700 VEC_free (ce_s, heap, rhsc);
4704 /* Account for uses in assigments and returns. */
4705 if (gimple_assign_single_p (t)
4706 || (gimple_code (t) == GIMPLE_RETURN
4707 && gimple_return_retval (t) != NULL_TREE))
4709 tree rhs = (gimple_assign_single_p (t)
4710 ? gimple_assign_rhs1 (t) : gimple_return_retval (t));
4711 tree tem = rhs;
4712 while (handled_component_p (tem))
4713 tem = TREE_OPERAND (tem, 0);
4714 if ((DECL_P (tem)
4715 && !auto_var_in_fn_p (tem, cfun->decl))
4716 || INDIRECT_REF_P (tem)
4717 || (TREE_CODE (tem) == MEM_REF
4718 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4719 && auto_var_in_fn_p
4720 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4722 struct constraint_expr lhs, *rhsp;
4723 unsigned i;
4724 lhs = get_function_part_constraint (fi, fi_uses);
4725 get_constraint_for_address_of (rhs, &rhsc);
4726 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4727 process_constraint (new_constraint (lhs, *rhsp));
4728 VEC_free (ce_s, heap, rhsc);
4732 if (is_gimple_call (t))
4734 varinfo_t cfi = NULL;
4735 tree decl = gimple_call_fndecl (t);
4736 struct constraint_expr lhs, rhs;
4737 unsigned i, j;
4739 /* For builtins we do not have separate function info. For those
4740 we do not generate escapes for we have to generate clobbers/uses. */
4741 if (decl
4742 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4743 switch (DECL_FUNCTION_CODE (decl))
4745 /* The following functions use and clobber memory pointed to
4746 by their arguments. */
4747 case BUILT_IN_STRCPY:
4748 case BUILT_IN_STRNCPY:
4749 case BUILT_IN_BCOPY:
4750 case BUILT_IN_MEMCPY:
4751 case BUILT_IN_MEMMOVE:
4752 case BUILT_IN_MEMPCPY:
4753 case BUILT_IN_STPCPY:
4754 case BUILT_IN_STPNCPY:
4755 case BUILT_IN_STRCAT:
4756 case BUILT_IN_STRNCAT:
4757 case BUILT_IN_STRCPY_CHK:
4758 case BUILT_IN_STRNCPY_CHK:
4759 case BUILT_IN_MEMCPY_CHK:
4760 case BUILT_IN_MEMMOVE_CHK:
4761 case BUILT_IN_MEMPCPY_CHK:
4762 case BUILT_IN_STPCPY_CHK:
4763 case BUILT_IN_STPNCPY_CHK:
4764 case BUILT_IN_STRCAT_CHK:
4765 case BUILT_IN_STRNCAT_CHK:
4767 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4768 == BUILT_IN_BCOPY ? 1 : 0));
4769 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4770 == BUILT_IN_BCOPY ? 0 : 1));
4771 unsigned i;
4772 struct constraint_expr *rhsp, *lhsp;
4773 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4774 lhs = get_function_part_constraint (fi, fi_clobbers);
4775 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4776 process_constraint (new_constraint (lhs, *lhsp));
4777 VEC_free (ce_s, heap, lhsc);
4778 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4779 lhs = get_function_part_constraint (fi, fi_uses);
4780 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4781 process_constraint (new_constraint (lhs, *rhsp));
4782 VEC_free (ce_s, heap, rhsc);
4783 return;
4785 /* The following function clobbers memory pointed to by
4786 its argument. */
4787 case BUILT_IN_MEMSET:
4788 case BUILT_IN_MEMSET_CHK:
4790 tree dest = gimple_call_arg (t, 0);
4791 unsigned i;
4792 ce_s *lhsp;
4793 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4794 lhs = get_function_part_constraint (fi, fi_clobbers);
4795 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4796 process_constraint (new_constraint (lhs, *lhsp));
4797 VEC_free (ce_s, heap, lhsc);
4798 return;
4800 /* The following functions clobber their second and third
4801 arguments. */
4802 case BUILT_IN_SINCOS:
4803 case BUILT_IN_SINCOSF:
4804 case BUILT_IN_SINCOSL:
4806 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4807 process_ipa_clobber (fi, gimple_call_arg (t, 2));
4808 return;
4810 /* The following functions clobber their second argument. */
4811 case BUILT_IN_FREXP:
4812 case BUILT_IN_FREXPF:
4813 case BUILT_IN_FREXPL:
4814 case BUILT_IN_LGAMMA_R:
4815 case BUILT_IN_LGAMMAF_R:
4816 case BUILT_IN_LGAMMAL_R:
4817 case BUILT_IN_GAMMA_R:
4818 case BUILT_IN_GAMMAF_R:
4819 case BUILT_IN_GAMMAL_R:
4820 case BUILT_IN_MODF:
4821 case BUILT_IN_MODFF:
4822 case BUILT_IN_MODFL:
4824 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4825 return;
4827 /* The following functions clobber their third argument. */
4828 case BUILT_IN_REMQUO:
4829 case BUILT_IN_REMQUOF:
4830 case BUILT_IN_REMQUOL:
4832 process_ipa_clobber (fi, gimple_call_arg (t, 2));
4833 return;
4835 /* The following functions neither read nor clobber memory. */
4836 case BUILT_IN_ASSUME_ALIGNED:
4837 case BUILT_IN_FREE:
4838 return;
4839 /* Trampolines are of no interest to us. */
4840 case BUILT_IN_INIT_TRAMPOLINE:
4841 case BUILT_IN_ADJUST_TRAMPOLINE:
4842 return;
4843 case BUILT_IN_VA_START:
4844 case BUILT_IN_VA_END:
4845 return;
4846 /* printf-style functions may have hooks to set pointers to
4847 point to somewhere into the generated string. Leave them
4848 for a later excercise... */
4849 default:
4850 /* Fallthru to general call handling. */;
4853 /* Parameters passed by value are used. */
4854 lhs = get_function_part_constraint (fi, fi_uses);
4855 for (i = 0; i < gimple_call_num_args (t); i++)
4857 struct constraint_expr *rhsp;
4858 tree arg = gimple_call_arg (t, i);
4860 if (TREE_CODE (arg) == SSA_NAME
4861 || is_gimple_min_invariant (arg))
4862 continue;
4864 get_constraint_for_address_of (arg, &rhsc);
4865 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
4866 process_constraint (new_constraint (lhs, *rhsp));
4867 VEC_free (ce_s, heap, rhsc);
4870 /* Build constraints for propagating clobbers/uses along the
4871 callgraph edges. */
4872 cfi = get_fi_for_callee (t);
4873 if (cfi->id == anything_id)
4875 if (gimple_vdef (t))
4876 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
4877 anything_id);
4878 make_constraint_from (first_vi_for_offset (fi, fi_uses),
4879 anything_id);
4880 return;
4883 /* For callees without function info (that's external functions),
4884 ESCAPED is clobbered and used. */
4885 if (gimple_call_fndecl (t)
4886 && !cfi->is_fn_info)
4888 varinfo_t vi;
4890 if (gimple_vdef (t))
4891 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
4892 escaped_id);
4893 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
4895 /* Also honor the call statement use/clobber info. */
4896 if ((vi = lookup_call_clobber_vi (t)) != NULL)
4897 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
4898 vi->id);
4899 if ((vi = lookup_call_use_vi (t)) != NULL)
4900 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
4901 vi->id);
4902 return;
4905 /* Otherwise the caller clobbers and uses what the callee does.
4906 ??? This should use a new complex constraint that filters
4907 local variables of the callee. */
4908 if (gimple_vdef (t))
4910 lhs = get_function_part_constraint (fi, fi_clobbers);
4911 rhs = get_function_part_constraint (cfi, fi_clobbers);
4912 process_constraint (new_constraint (lhs, rhs));
4914 lhs = get_function_part_constraint (fi, fi_uses);
4915 rhs = get_function_part_constraint (cfi, fi_uses);
4916 process_constraint (new_constraint (lhs, rhs));
4918 else if (gimple_code (t) == GIMPLE_ASM)
4920 /* ??? Ick. We can do better. */
4921 if (gimple_vdef (t))
4922 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
4923 anything_id);
4924 make_constraint_from (first_vi_for_offset (fi, fi_uses),
4925 anything_id);
4928 VEC_free (ce_s, heap, rhsc);
4932 /* Find the first varinfo in the same variable as START that overlaps with
4933 OFFSET. Return NULL if we can't find one. */
4935 static varinfo_t
4936 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
4938 /* If the offset is outside of the variable, bail out. */
4939 if (offset >= start->fullsize)
4940 return NULL;
4942 /* If we cannot reach offset from start, lookup the first field
4943 and start from there. */
4944 if (start->offset > offset)
4945 start = lookup_vi_for_tree (start->decl);
4947 while (start)
4949 /* We may not find a variable in the field list with the actual
4950 offset when when we have glommed a structure to a variable.
4951 In that case, however, offset should still be within the size
4952 of the variable. */
4953 if (offset >= start->offset
4954 && (offset - start->offset) < start->size)
4955 return start;
4957 start= start->next;
4960 return NULL;
4963 /* Find the first varinfo in the same variable as START that overlaps with
4964 OFFSET. If there is no such varinfo the varinfo directly preceding
4965 OFFSET is returned. */
4967 static varinfo_t
4968 first_or_preceding_vi_for_offset (varinfo_t start,
4969 unsigned HOST_WIDE_INT offset)
4971 /* If we cannot reach offset from start, lookup the first field
4972 and start from there. */
4973 if (start->offset > offset)
4974 start = lookup_vi_for_tree (start->decl);
4976 /* We may not find a variable in the field list with the actual
4977 offset when when we have glommed a structure to a variable.
4978 In that case, however, offset should still be within the size
4979 of the variable.
4980 If we got beyond the offset we look for return the field
4981 directly preceding offset which may be the last field. */
4982 while (start->next
4983 && offset >= start->offset
4984 && !((offset - start->offset) < start->size))
4985 start = start->next;
4987 return start;
4991 /* This structure is used during pushing fields onto the fieldstack
4992 to track the offset of the field, since bitpos_of_field gives it
4993 relative to its immediate containing type, and we want it relative
4994 to the ultimate containing object. */
4996 struct fieldoff
4998 /* Offset from the base of the base containing object to this field. */
4999 HOST_WIDE_INT offset;
5001 /* Size, in bits, of the field. */
5002 unsigned HOST_WIDE_INT size;
5004 unsigned has_unknown_size : 1;
5006 unsigned must_have_pointers : 1;
5008 unsigned may_have_pointers : 1;
5010 unsigned only_restrict_pointers : 1;
5012 typedef struct fieldoff fieldoff_s;
5014 DEF_VEC_O(fieldoff_s);
5015 DEF_VEC_ALLOC_O(fieldoff_s,heap);
5017 /* qsort comparison function for two fieldoff's PA and PB */
5019 static int
5020 fieldoff_compare (const void *pa, const void *pb)
5022 const fieldoff_s *foa = (const fieldoff_s *)pa;
5023 const fieldoff_s *fob = (const fieldoff_s *)pb;
5024 unsigned HOST_WIDE_INT foasize, fobsize;
5026 if (foa->offset < fob->offset)
5027 return -1;
5028 else if (foa->offset > fob->offset)
5029 return 1;
5031 foasize = foa->size;
5032 fobsize = fob->size;
5033 if (foasize < fobsize)
5034 return -1;
5035 else if (foasize > fobsize)
5036 return 1;
5037 return 0;
5040 /* Sort a fieldstack according to the field offset and sizes. */
5041 static void
5042 sort_fieldstack (VEC(fieldoff_s,heap) *fieldstack)
5044 VEC_qsort (fieldoff_s, fieldstack, fieldoff_compare);
5047 /* Return true if T is a type that can have subvars. */
5049 static inline bool
5050 type_can_have_subvars (const_tree t)
5052 /* Aggregates without overlapping fields can have subvars. */
5053 return TREE_CODE (t) == RECORD_TYPE;
5056 /* Return true if V is a tree that we can have subvars for.
5057 Normally, this is any aggregate type. Also complex
5058 types which are not gimple registers can have subvars. */
5060 static inline bool
5061 var_can_have_subvars (const_tree v)
5063 /* Volatile variables should never have subvars. */
5064 if (TREE_THIS_VOLATILE (v))
5065 return false;
5067 /* Non decls or memory tags can never have subvars. */
5068 if (!DECL_P (v))
5069 return false;
5071 return type_can_have_subvars (TREE_TYPE (v));
5074 /* Return true if T is a type that does contain pointers. */
5076 static bool
5077 type_must_have_pointers (tree type)
5079 if (POINTER_TYPE_P (type))
5080 return true;
5082 if (TREE_CODE (type) == ARRAY_TYPE)
5083 return type_must_have_pointers (TREE_TYPE (type));
5085 /* A function or method can have pointers as arguments, so track
5086 those separately. */
5087 if (TREE_CODE (type) == FUNCTION_TYPE
5088 || TREE_CODE (type) == METHOD_TYPE)
5089 return true;
5091 return false;
5094 static bool
5095 field_must_have_pointers (tree t)
5097 return type_must_have_pointers (TREE_TYPE (t));
5100 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5101 the fields of TYPE onto fieldstack, recording their offsets along
5102 the way.
5104 OFFSET is used to keep track of the offset in this entire
5105 structure, rather than just the immediately containing structure.
5106 Returns false if the caller is supposed to handle the field we
5107 recursed for. */
5109 static bool
5110 push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
5111 HOST_WIDE_INT offset)
5113 tree field;
5114 bool empty_p = true;
5116 if (TREE_CODE (type) != RECORD_TYPE)
5117 return false;
5119 /* If the vector of fields is growing too big, bail out early.
5120 Callers check for VEC_length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5121 sure this fails. */
5122 if (VEC_length (fieldoff_s, *fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5123 return false;
5125 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5126 if (TREE_CODE (field) == FIELD_DECL)
5128 bool push = false;
5129 HOST_WIDE_INT foff = bitpos_of_field (field);
5131 if (!var_can_have_subvars (field)
5132 || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
5133 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5134 push = true;
5135 else if (!push_fields_onto_fieldstack
5136 (TREE_TYPE (field), fieldstack, offset + foff)
5137 && (DECL_SIZE (field)
5138 && !integer_zerop (DECL_SIZE (field))))
5139 /* Empty structures may have actual size, like in C++. So
5140 see if we didn't push any subfields and the size is
5141 nonzero, push the field onto the stack. */
5142 push = true;
5144 if (push)
5146 fieldoff_s *pair = NULL;
5147 bool has_unknown_size = false;
5148 bool must_have_pointers_p;
5150 if (!VEC_empty (fieldoff_s, *fieldstack))
5151 pair = VEC_last (fieldoff_s, *fieldstack);
5153 /* If there isn't anything at offset zero, create sth. */
5154 if (!pair
5155 && offset + foff != 0)
5157 pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
5158 pair->offset = 0;
5159 pair->size = offset + foff;
5160 pair->has_unknown_size = false;
5161 pair->must_have_pointers = false;
5162 pair->may_have_pointers = false;
5163 pair->only_restrict_pointers = false;
5166 if (!DECL_SIZE (field)
5167 || !host_integerp (DECL_SIZE (field), 1))
5168 has_unknown_size = true;
5170 /* If adjacent fields do not contain pointers merge them. */
5171 must_have_pointers_p = field_must_have_pointers (field);
5172 if (pair
5173 && !has_unknown_size
5174 && !must_have_pointers_p
5175 && !pair->must_have_pointers
5176 && !pair->has_unknown_size
5177 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5179 pair->size += TREE_INT_CST_LOW (DECL_SIZE (field));
5181 else
5183 pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
5184 pair->offset = offset + foff;
5185 pair->has_unknown_size = has_unknown_size;
5186 if (!has_unknown_size)
5187 pair->size = TREE_INT_CST_LOW (DECL_SIZE (field));
5188 else
5189 pair->size = -1;
5190 pair->must_have_pointers = must_have_pointers_p;
5191 pair->may_have_pointers = true;
5192 pair->only_restrict_pointers
5193 = (!has_unknown_size
5194 && POINTER_TYPE_P (TREE_TYPE (field))
5195 && TYPE_RESTRICT (TREE_TYPE (field)));
5199 empty_p = false;
5202 return !empty_p;
5205 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5206 if it is a varargs function. */
5208 static unsigned int
5209 count_num_arguments (tree decl, bool *is_varargs)
5211 unsigned int num = 0;
5212 tree t;
5214 /* Capture named arguments for K&R functions. They do not
5215 have a prototype and thus no TYPE_ARG_TYPES. */
5216 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5217 ++num;
5219 /* Check if the function has variadic arguments. */
5220 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5221 if (TREE_VALUE (t) == void_type_node)
5222 break;
5223 if (!t)
5224 *is_varargs = true;
5226 return num;
5229 /* Creation function node for DECL, using NAME, and return the index
5230 of the variable we've created for the function. */
5232 static varinfo_t
5233 create_function_info_for (tree decl, const char *name)
5235 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5236 varinfo_t vi, prev_vi;
5237 tree arg;
5238 unsigned int i;
5239 bool is_varargs = false;
5240 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5242 /* Create the variable info. */
5244 vi = new_var_info (decl, name);
5245 vi->offset = 0;
5246 vi->size = 1;
5247 vi->fullsize = fi_parm_base + num_args;
5248 vi->is_fn_info = 1;
5249 vi->may_have_pointers = false;
5250 if (is_varargs)
5251 vi->fullsize = ~0;
5252 insert_vi_for_tree (vi->decl, vi);
5254 prev_vi = vi;
5256 /* Create a variable for things the function clobbers and one for
5257 things the function uses. */
5259 varinfo_t clobbervi, usevi;
5260 const char *newname;
5261 char *tempname;
5263 asprintf (&tempname, "%s.clobber", name);
5264 newname = ggc_strdup (tempname);
5265 free (tempname);
5267 clobbervi = new_var_info (NULL, newname);
5268 clobbervi->offset = fi_clobbers;
5269 clobbervi->size = 1;
5270 clobbervi->fullsize = vi->fullsize;
5271 clobbervi->is_full_var = true;
5272 clobbervi->is_global_var = false;
5273 gcc_assert (prev_vi->offset < clobbervi->offset);
5274 prev_vi->next = clobbervi;
5275 prev_vi = clobbervi;
5277 asprintf (&tempname, "%s.use", name);
5278 newname = ggc_strdup (tempname);
5279 free (tempname);
5281 usevi = new_var_info (NULL, newname);
5282 usevi->offset = fi_uses;
5283 usevi->size = 1;
5284 usevi->fullsize = vi->fullsize;
5285 usevi->is_full_var = true;
5286 usevi->is_global_var = false;
5287 gcc_assert (prev_vi->offset < usevi->offset);
5288 prev_vi->next = usevi;
5289 prev_vi = usevi;
5292 /* And one for the static chain. */
5293 if (fn->static_chain_decl != NULL_TREE)
5295 varinfo_t chainvi;
5296 const char *newname;
5297 char *tempname;
5299 asprintf (&tempname, "%s.chain", name);
5300 newname = ggc_strdup (tempname);
5301 free (tempname);
5303 chainvi = new_var_info (fn->static_chain_decl, newname);
5304 chainvi->offset = fi_static_chain;
5305 chainvi->size = 1;
5306 chainvi->fullsize = vi->fullsize;
5307 chainvi->is_full_var = true;
5308 chainvi->is_global_var = false;
5309 gcc_assert (prev_vi->offset < chainvi->offset);
5310 prev_vi->next = chainvi;
5311 prev_vi = chainvi;
5312 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5315 /* Create a variable for the return var. */
5316 if (DECL_RESULT (decl) != NULL
5317 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5319 varinfo_t resultvi;
5320 const char *newname;
5321 char *tempname;
5322 tree resultdecl = decl;
5324 if (DECL_RESULT (decl))
5325 resultdecl = DECL_RESULT (decl);
5327 asprintf (&tempname, "%s.result", name);
5328 newname = ggc_strdup (tempname);
5329 free (tempname);
5331 resultvi = new_var_info (resultdecl, newname);
5332 resultvi->offset = fi_result;
5333 resultvi->size = 1;
5334 resultvi->fullsize = vi->fullsize;
5335 resultvi->is_full_var = true;
5336 if (DECL_RESULT (decl))
5337 resultvi->may_have_pointers = true;
5338 gcc_assert (prev_vi->offset < resultvi->offset);
5339 prev_vi->next = resultvi;
5340 prev_vi = resultvi;
5341 if (DECL_RESULT (decl))
5342 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
5345 /* Set up variables for each argument. */
5346 arg = DECL_ARGUMENTS (decl);
5347 for (i = 0; i < num_args; i++)
5349 varinfo_t argvi;
5350 const char *newname;
5351 char *tempname;
5352 tree argdecl = decl;
5354 if (arg)
5355 argdecl = arg;
5357 asprintf (&tempname, "%s.arg%d", name, i);
5358 newname = ggc_strdup (tempname);
5359 free (tempname);
5361 argvi = new_var_info (argdecl, newname);
5362 argvi->offset = fi_parm_base + i;
5363 argvi->size = 1;
5364 argvi->is_full_var = true;
5365 argvi->fullsize = vi->fullsize;
5366 if (arg)
5367 argvi->may_have_pointers = true;
5368 gcc_assert (prev_vi->offset < argvi->offset);
5369 prev_vi->next = argvi;
5370 prev_vi = argvi;
5371 if (arg)
5373 insert_vi_for_tree (arg, argvi);
5374 arg = DECL_CHAIN (arg);
5378 /* Add one representative for all further args. */
5379 if (is_varargs)
5381 varinfo_t argvi;
5382 const char *newname;
5383 char *tempname;
5384 tree decl;
5386 asprintf (&tempname, "%s.varargs", name);
5387 newname = ggc_strdup (tempname);
5388 free (tempname);
5390 /* We need sth that can be pointed to for va_start. */
5391 decl = build_fake_var_decl (ptr_type_node);
5393 argvi = new_var_info (decl, newname);
5394 argvi->offset = fi_parm_base + num_args;
5395 argvi->size = ~0;
5396 argvi->is_full_var = true;
5397 argvi->is_heap_var = true;
5398 argvi->fullsize = vi->fullsize;
5399 gcc_assert (prev_vi->offset < argvi->offset);
5400 prev_vi->next = argvi;
5401 prev_vi = argvi;
5404 return vi;
5408 /* Return true if FIELDSTACK contains fields that overlap.
5409 FIELDSTACK is assumed to be sorted by offset. */
5411 static bool
5412 check_for_overlaps (VEC (fieldoff_s,heap) *fieldstack)
5414 fieldoff_s *fo = NULL;
5415 unsigned int i;
5416 HOST_WIDE_INT lastoffset = -1;
5418 FOR_EACH_VEC_ELT (fieldoff_s, fieldstack, i, fo)
5420 if (fo->offset == lastoffset)
5421 return true;
5422 lastoffset = fo->offset;
5424 return false;
5427 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5428 This will also create any varinfo structures necessary for fields
5429 of DECL. */
5431 static varinfo_t
5432 create_variable_info_for_1 (tree decl, const char *name)
5434 varinfo_t vi, newvi;
5435 tree decl_type = TREE_TYPE (decl);
5436 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
5437 VEC (fieldoff_s,heap) *fieldstack = NULL;
5438 fieldoff_s *fo;
5439 unsigned int i;
5441 if (!declsize
5442 || !host_integerp (declsize, 1))
5444 vi = new_var_info (decl, name);
5445 vi->offset = 0;
5446 vi->size = ~0;
5447 vi->fullsize = ~0;
5448 vi->is_unknown_size_var = true;
5449 vi->is_full_var = true;
5450 vi->may_have_pointers = true;
5451 return vi;
5454 /* Collect field information. */
5455 if (use_field_sensitive
5456 && var_can_have_subvars (decl)
5457 /* ??? Force us to not use subfields for global initializers
5458 in IPA mode. Else we'd have to parse arbitrary initializers. */
5459 && !(in_ipa_mode
5460 && is_global_var (decl)
5461 && DECL_INITIAL (decl)))
5463 fieldoff_s *fo = NULL;
5464 bool notokay = false;
5465 unsigned int i;
5467 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
5469 for (i = 0; !notokay && VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
5470 if (fo->has_unknown_size
5471 || fo->offset < 0)
5473 notokay = true;
5474 break;
5477 /* We can't sort them if we have a field with a variable sized type,
5478 which will make notokay = true. In that case, we are going to return
5479 without creating varinfos for the fields anyway, so sorting them is a
5480 waste to boot. */
5481 if (!notokay)
5483 sort_fieldstack (fieldstack);
5484 /* Due to some C++ FE issues, like PR 22488, we might end up
5485 what appear to be overlapping fields even though they,
5486 in reality, do not overlap. Until the C++ FE is fixed,
5487 we will simply disable field-sensitivity for these cases. */
5488 notokay = check_for_overlaps (fieldstack);
5491 if (notokay)
5492 VEC_free (fieldoff_s, heap, fieldstack);
5495 /* If we didn't end up collecting sub-variables create a full
5496 variable for the decl. */
5497 if (VEC_length (fieldoff_s, fieldstack) <= 1
5498 || VEC_length (fieldoff_s, fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5500 vi = new_var_info (decl, name);
5501 vi->offset = 0;
5502 vi->may_have_pointers = true;
5503 vi->fullsize = TREE_INT_CST_LOW (declsize);
5504 vi->size = vi->fullsize;
5505 vi->is_full_var = true;
5506 VEC_free (fieldoff_s, heap, fieldstack);
5507 return vi;
5510 vi = new_var_info (decl, name);
5511 vi->fullsize = TREE_INT_CST_LOW (declsize);
5512 for (i = 0, newvi = vi;
5513 VEC_iterate (fieldoff_s, fieldstack, i, fo);
5514 ++i, newvi = newvi->next)
5516 const char *newname = "NULL";
5517 char *tempname;
5519 if (dump_file)
5521 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC
5522 "+" HOST_WIDE_INT_PRINT_DEC, name, fo->offset, fo->size);
5523 newname = ggc_strdup (tempname);
5524 free (tempname);
5526 newvi->name = newname;
5527 newvi->offset = fo->offset;
5528 newvi->size = fo->size;
5529 newvi->fullsize = vi->fullsize;
5530 newvi->may_have_pointers = fo->may_have_pointers;
5531 newvi->only_restrict_pointers = fo->only_restrict_pointers;
5532 if (i + 1 < VEC_length (fieldoff_s, fieldstack))
5533 newvi->next = new_var_info (decl, name);
5536 VEC_free (fieldoff_s, heap, fieldstack);
5538 return vi;
5541 static unsigned int
5542 create_variable_info_for (tree decl, const char *name)
5544 varinfo_t vi = create_variable_info_for_1 (decl, name);
5545 unsigned int id = vi->id;
5547 insert_vi_for_tree (decl, vi);
5549 if (TREE_CODE (decl) != VAR_DECL)
5550 return id;
5552 /* Create initial constraints for globals. */
5553 for (; vi; vi = vi->next)
5555 if (!vi->may_have_pointers
5556 || !vi->is_global_var)
5557 continue;
5559 /* Mark global restrict qualified pointers. */
5560 if ((POINTER_TYPE_P (TREE_TYPE (decl))
5561 && TYPE_RESTRICT (TREE_TYPE (decl)))
5562 || vi->only_restrict_pointers)
5564 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5565 continue;
5568 /* In non-IPA mode the initializer from nonlocal is all we need. */
5569 if (!in_ipa_mode
5570 || DECL_HARD_REGISTER (decl))
5571 make_copy_constraint (vi, nonlocal_id);
5573 /* In IPA mode parse the initializer and generate proper constraints
5574 for it. */
5575 else
5577 struct varpool_node *vnode = varpool_get_node (decl);
5579 /* For escaped variables initialize them from nonlocal. */
5580 if (!varpool_all_refs_explicit_p (vnode))
5581 make_copy_constraint (vi, nonlocal_id);
5583 /* If this is a global variable with an initializer and we are in
5584 IPA mode generate constraints for it. */
5585 if (DECL_INITIAL (decl)
5586 && vnode->analyzed)
5588 VEC (ce_s, heap) *rhsc = NULL;
5589 struct constraint_expr lhs, *rhsp;
5590 unsigned i;
5591 get_constraint_for_rhs (DECL_INITIAL (decl), &rhsc);
5592 lhs.var = vi->id;
5593 lhs.offset = 0;
5594 lhs.type = SCALAR;
5595 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
5596 process_constraint (new_constraint (lhs, *rhsp));
5597 /* If this is a variable that escapes from the unit
5598 the initializer escapes as well. */
5599 if (!varpool_all_refs_explicit_p (vnode))
5601 lhs.var = escaped_id;
5602 lhs.offset = 0;
5603 lhs.type = SCALAR;
5604 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
5605 process_constraint (new_constraint (lhs, *rhsp));
5607 VEC_free (ce_s, heap, rhsc);
5612 return id;
5615 /* Print out the points-to solution for VAR to FILE. */
5617 static void
5618 dump_solution_for_var (FILE *file, unsigned int var)
5620 varinfo_t vi = get_varinfo (var);
5621 unsigned int i;
5622 bitmap_iterator bi;
5624 /* Dump the solution for unified vars anyway, this avoids difficulties
5625 in scanning dumps in the testsuite. */
5626 fprintf (file, "%s = { ", vi->name);
5627 vi = get_varinfo (find (var));
5628 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5629 fprintf (file, "%s ", get_varinfo (i)->name);
5630 fprintf (file, "}");
5632 /* But note when the variable was unified. */
5633 if (vi->id != var)
5634 fprintf (file, " same as %s", vi->name);
5636 fprintf (file, "\n");
5639 /* Print the points-to solution for VAR to stdout. */
5641 DEBUG_FUNCTION void
5642 debug_solution_for_var (unsigned int var)
5644 dump_solution_for_var (stdout, var);
5647 /* Create varinfo structures for all of the variables in the
5648 function for intraprocedural mode. */
5650 static void
5651 intra_create_variable_infos (void)
5653 tree t;
5655 /* For each incoming pointer argument arg, create the constraint ARG
5656 = NONLOCAL or a dummy variable if it is a restrict qualified
5657 passed-by-reference argument. */
5658 for (t = DECL_ARGUMENTS (current_function_decl); t; t = DECL_CHAIN (t))
5660 varinfo_t p = get_vi_for_tree (t);
5662 /* For restrict qualified pointers to objects passed by
5663 reference build a real representative for the pointed-to object.
5664 Treat restrict qualified references the same. */
5665 if (TYPE_RESTRICT (TREE_TYPE (t))
5666 && ((DECL_BY_REFERENCE (t) && POINTER_TYPE_P (TREE_TYPE (t)))
5667 || TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5668 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t))))
5670 struct constraint_expr lhsc, rhsc;
5671 varinfo_t vi;
5672 tree heapvar = build_fake_var_decl (TREE_TYPE (TREE_TYPE (t)));
5673 DECL_EXTERNAL (heapvar) = 1;
5674 vi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS");
5675 insert_vi_for_tree (heapvar, vi);
5676 lhsc.var = p->id;
5677 lhsc.type = SCALAR;
5678 lhsc.offset = 0;
5679 rhsc.var = vi->id;
5680 rhsc.type = ADDRESSOF;
5681 rhsc.offset = 0;
5682 process_constraint (new_constraint (lhsc, rhsc));
5683 for (; vi; vi = vi->next)
5684 if (vi->may_have_pointers)
5686 if (vi->only_restrict_pointers)
5687 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5688 else
5689 make_copy_constraint (vi, nonlocal_id);
5691 continue;
5694 if (POINTER_TYPE_P (TREE_TYPE (t))
5695 && TYPE_RESTRICT (TREE_TYPE (t)))
5696 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5697 else
5699 for (; p; p = p->next)
5701 if (p->only_restrict_pointers)
5702 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5703 else if (p->may_have_pointers)
5704 make_constraint_from (p, nonlocal_id);
5709 /* Add a constraint for a result decl that is passed by reference. */
5710 if (DECL_RESULT (cfun->decl)
5711 && DECL_BY_REFERENCE (DECL_RESULT (cfun->decl)))
5713 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (cfun->decl));
5715 for (p = result_vi; p; p = p->next)
5716 make_constraint_from (p, nonlocal_id);
5719 /* Add a constraint for the incoming static chain parameter. */
5720 if (cfun->static_chain_decl != NULL_TREE)
5722 varinfo_t p, chain_vi = get_vi_for_tree (cfun->static_chain_decl);
5724 for (p = chain_vi; p; p = p->next)
5725 make_constraint_from (p, nonlocal_id);
5729 /* Structure used to put solution bitmaps in a hashtable so they can
5730 be shared among variables with the same points-to set. */
5732 typedef struct shared_bitmap_info
5734 bitmap pt_vars;
5735 hashval_t hashcode;
5736 } *shared_bitmap_info_t;
5737 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
5739 static htab_t shared_bitmap_table;
5741 /* Hash function for a shared_bitmap_info_t */
5743 static hashval_t
5744 shared_bitmap_hash (const void *p)
5746 const_shared_bitmap_info_t const bi = (const_shared_bitmap_info_t) p;
5747 return bi->hashcode;
5750 /* Equality function for two shared_bitmap_info_t's. */
5752 static int
5753 shared_bitmap_eq (const void *p1, const void *p2)
5755 const_shared_bitmap_info_t const sbi1 = (const_shared_bitmap_info_t) p1;
5756 const_shared_bitmap_info_t const sbi2 = (const_shared_bitmap_info_t) p2;
5757 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
5760 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5761 existing instance if there is one, NULL otherwise. */
5763 static bitmap
5764 shared_bitmap_lookup (bitmap pt_vars)
5766 void **slot;
5767 struct shared_bitmap_info sbi;
5769 sbi.pt_vars = pt_vars;
5770 sbi.hashcode = bitmap_hash (pt_vars);
5772 slot = htab_find_slot_with_hash (shared_bitmap_table, &sbi,
5773 sbi.hashcode, NO_INSERT);
5774 if (!slot)
5775 return NULL;
5776 else
5777 return ((shared_bitmap_info_t) *slot)->pt_vars;
5781 /* Add a bitmap to the shared bitmap hashtable. */
5783 static void
5784 shared_bitmap_add (bitmap pt_vars)
5786 void **slot;
5787 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
5789 sbi->pt_vars = pt_vars;
5790 sbi->hashcode = bitmap_hash (pt_vars);
5792 slot = htab_find_slot_with_hash (shared_bitmap_table, sbi,
5793 sbi->hashcode, INSERT);
5794 gcc_assert (!*slot);
5795 *slot = (void *) sbi;
5799 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
5801 static void
5802 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt)
5804 unsigned int i;
5805 bitmap_iterator bi;
5807 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
5809 varinfo_t vi = get_varinfo (i);
5811 /* The only artificial variables that are allowed in a may-alias
5812 set are heap variables. */
5813 if (vi->is_artificial_var && !vi->is_heap_var)
5814 continue;
5816 if (TREE_CODE (vi->decl) == VAR_DECL
5817 || TREE_CODE (vi->decl) == PARM_DECL
5818 || TREE_CODE (vi->decl) == RESULT_DECL)
5820 /* If we are in IPA mode we will not recompute points-to
5821 sets after inlining so make sure they stay valid. */
5822 if (in_ipa_mode
5823 && !DECL_PT_UID_SET_P (vi->decl))
5824 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
5826 /* Add the decl to the points-to set. Note that the points-to
5827 set contains global variables. */
5828 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
5829 if (vi->is_global_var)
5830 pt->vars_contains_global = true;
5836 /* Compute the points-to solution *PT for the variable VI. */
5838 static void
5839 find_what_var_points_to (varinfo_t orig_vi, struct pt_solution *pt)
5841 unsigned int i;
5842 bitmap_iterator bi;
5843 bitmap finished_solution;
5844 bitmap result;
5845 varinfo_t vi;
5847 memset (pt, 0, sizeof (struct pt_solution));
5849 /* This variable may have been collapsed, let's get the real
5850 variable. */
5851 vi = get_varinfo (find (orig_vi->id));
5853 /* Translate artificial variables into SSA_NAME_PTR_INFO
5854 attributes. */
5855 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5857 varinfo_t vi = get_varinfo (i);
5859 if (vi->is_artificial_var)
5861 if (vi->id == nothing_id)
5862 pt->null = 1;
5863 else if (vi->id == escaped_id)
5865 if (in_ipa_mode)
5866 pt->ipa_escaped = 1;
5867 else
5868 pt->escaped = 1;
5870 else if (vi->id == nonlocal_id)
5871 pt->nonlocal = 1;
5872 else if (vi->is_heap_var)
5873 /* We represent heapvars in the points-to set properly. */
5875 else if (vi->id == readonly_id)
5876 /* Nobody cares. */
5878 else if (vi->id == anything_id
5879 || vi->id == integer_id)
5880 pt->anything = 1;
5884 /* Instead of doing extra work, simply do not create
5885 elaborate points-to information for pt_anything pointers. */
5886 if (pt->anything)
5887 return;
5889 /* Share the final set of variables when possible. */
5890 finished_solution = BITMAP_GGC_ALLOC ();
5891 stats.points_to_sets_created++;
5893 set_uids_in_ptset (finished_solution, vi->solution, pt);
5894 result = shared_bitmap_lookup (finished_solution);
5895 if (!result)
5897 shared_bitmap_add (finished_solution);
5898 pt->vars = finished_solution;
5900 else
5902 pt->vars = result;
5903 bitmap_clear (finished_solution);
5907 /* Given a pointer variable P, fill in its points-to set. */
5909 static void
5910 find_what_p_points_to (tree p)
5912 struct ptr_info_def *pi;
5913 tree lookup_p = p;
5914 varinfo_t vi;
5916 /* For parameters, get at the points-to set for the actual parm
5917 decl. */
5918 if (TREE_CODE (p) == SSA_NAME
5919 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
5920 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL)
5921 && SSA_NAME_IS_DEFAULT_DEF (p))
5922 lookup_p = SSA_NAME_VAR (p);
5924 vi = lookup_vi_for_tree (lookup_p);
5925 if (!vi)
5926 return;
5928 pi = get_ptr_info (p);
5929 find_what_var_points_to (vi, &pi->pt);
5933 /* Query statistics for points-to solutions. */
5935 static struct {
5936 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
5937 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
5938 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
5939 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
5940 } pta_stats;
5942 void
5943 dump_pta_stats (FILE *s)
5945 fprintf (s, "\nPTA query stats:\n");
5946 fprintf (s, " pt_solution_includes: "
5947 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
5948 HOST_WIDE_INT_PRINT_DEC" queries\n",
5949 pta_stats.pt_solution_includes_no_alias,
5950 pta_stats.pt_solution_includes_no_alias
5951 + pta_stats.pt_solution_includes_may_alias);
5952 fprintf (s, " pt_solutions_intersect: "
5953 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
5954 HOST_WIDE_INT_PRINT_DEC" queries\n",
5955 pta_stats.pt_solutions_intersect_no_alias,
5956 pta_stats.pt_solutions_intersect_no_alias
5957 + pta_stats.pt_solutions_intersect_may_alias);
5961 /* Reset the points-to solution *PT to a conservative default
5962 (point to anything). */
5964 void
5965 pt_solution_reset (struct pt_solution *pt)
5967 memset (pt, 0, sizeof (struct pt_solution));
5968 pt->anything = true;
5971 /* Set the points-to solution *PT to point only to the variables
5972 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
5973 global variables and VARS_CONTAINS_RESTRICT specifies whether
5974 it contains restrict tag variables. */
5976 void
5977 pt_solution_set (struct pt_solution *pt, bitmap vars, bool vars_contains_global)
5979 memset (pt, 0, sizeof (struct pt_solution));
5980 pt->vars = vars;
5981 pt->vars_contains_global = vars_contains_global;
5984 /* Set the points-to solution *PT to point only to the variable VAR. */
5986 void
5987 pt_solution_set_var (struct pt_solution *pt, tree var)
5989 memset (pt, 0, sizeof (struct pt_solution));
5990 pt->vars = BITMAP_GGC_ALLOC ();
5991 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
5992 pt->vars_contains_global = is_global_var (var);
5995 /* Computes the union of the points-to solutions *DEST and *SRC and
5996 stores the result in *DEST. This changes the points-to bitmap
5997 of *DEST and thus may not be used if that might be shared.
5998 The points-to bitmap of *SRC and *DEST will not be shared after
5999 this function if they were not before. */
6001 static void
6002 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6004 dest->anything |= src->anything;
6005 if (dest->anything)
6007 pt_solution_reset (dest);
6008 return;
6011 dest->nonlocal |= src->nonlocal;
6012 dest->escaped |= src->escaped;
6013 dest->ipa_escaped |= src->ipa_escaped;
6014 dest->null |= src->null;
6015 dest->vars_contains_global |= src->vars_contains_global;
6016 if (!src->vars)
6017 return;
6019 if (!dest->vars)
6020 dest->vars = BITMAP_GGC_ALLOC ();
6021 bitmap_ior_into (dest->vars, src->vars);
6024 /* Return true if the points-to solution *PT is empty. */
6026 bool
6027 pt_solution_empty_p (struct pt_solution *pt)
6029 if (pt->anything
6030 || pt->nonlocal)
6031 return false;
6033 if (pt->vars
6034 && !bitmap_empty_p (pt->vars))
6035 return false;
6037 /* If the solution includes ESCAPED, check if that is empty. */
6038 if (pt->escaped
6039 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6040 return false;
6042 /* If the solution includes ESCAPED, check if that is empty. */
6043 if (pt->ipa_escaped
6044 && !pt_solution_empty_p (&ipa_escaped_pt))
6045 return false;
6047 return true;
6050 /* Return true if the points-to solution *PT only point to a single var, and
6051 return the var uid in *UID. */
6053 bool
6054 pt_solution_singleton_p (struct pt_solution *pt, unsigned *uid)
6056 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
6057 || pt->null || pt->vars == NULL
6058 || !bitmap_single_bit_set_p (pt->vars))
6059 return false;
6061 *uid = bitmap_first_set_bit (pt->vars);
6062 return true;
6065 /* Return true if the points-to solution *PT includes global memory. */
6067 bool
6068 pt_solution_includes_global (struct pt_solution *pt)
6070 if (pt->anything
6071 || pt->nonlocal
6072 || pt->vars_contains_global)
6073 return true;
6075 if (pt->escaped)
6076 return pt_solution_includes_global (&cfun->gimple_df->escaped);
6078 if (pt->ipa_escaped)
6079 return pt_solution_includes_global (&ipa_escaped_pt);
6081 /* ??? This predicate is not correct for the IPA-PTA solution
6082 as we do not properly distinguish between unit escape points
6083 and global variables. */
6084 if (cfun->gimple_df->ipa_pta)
6085 return true;
6087 return false;
6090 /* Return true if the points-to solution *PT includes the variable
6091 declaration DECL. */
6093 static bool
6094 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6096 if (pt->anything)
6097 return true;
6099 if (pt->nonlocal
6100 && is_global_var (decl))
6101 return true;
6103 if (pt->vars
6104 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6105 return true;
6107 /* If the solution includes ESCAPED, check it. */
6108 if (pt->escaped
6109 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6110 return true;
6112 /* If the solution includes ESCAPED, check it. */
6113 if (pt->ipa_escaped
6114 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6115 return true;
6117 return false;
6120 bool
6121 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6123 bool res = pt_solution_includes_1 (pt, decl);
6124 if (res)
6125 ++pta_stats.pt_solution_includes_may_alias;
6126 else
6127 ++pta_stats.pt_solution_includes_no_alias;
6128 return res;
6131 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6132 intersection. */
6134 static bool
6135 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
6137 if (pt1->anything || pt2->anything)
6138 return true;
6140 /* If either points to unknown global memory and the other points to
6141 any global memory they alias. */
6142 if ((pt1->nonlocal
6143 && (pt2->nonlocal
6144 || pt2->vars_contains_global))
6145 || (pt2->nonlocal
6146 && pt1->vars_contains_global))
6147 return true;
6149 /* Check the escaped solution if required. */
6150 if ((pt1->escaped || pt2->escaped)
6151 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6153 /* If both point to escaped memory and that solution
6154 is not empty they alias. */
6155 if (pt1->escaped && pt2->escaped)
6156 return true;
6158 /* If either points to escaped memory see if the escaped solution
6159 intersects with the other. */
6160 if ((pt1->escaped
6161 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt2))
6162 || (pt2->escaped
6163 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt1)))
6164 return true;
6167 /* Check the escaped solution if required.
6168 ??? Do we need to check the local against the IPA escaped sets? */
6169 if ((pt1->ipa_escaped || pt2->ipa_escaped)
6170 && !pt_solution_empty_p (&ipa_escaped_pt))
6172 /* If both point to escaped memory and that solution
6173 is not empty they alias. */
6174 if (pt1->ipa_escaped && pt2->ipa_escaped)
6175 return true;
6177 /* If either points to escaped memory see if the escaped solution
6178 intersects with the other. */
6179 if ((pt1->ipa_escaped
6180 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
6181 || (pt2->ipa_escaped
6182 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
6183 return true;
6186 /* Now both pointers alias if their points-to solution intersects. */
6187 return (pt1->vars
6188 && pt2->vars
6189 && bitmap_intersect_p (pt1->vars, pt2->vars));
6192 bool
6193 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
6195 bool res = pt_solutions_intersect_1 (pt1, pt2);
6196 if (res)
6197 ++pta_stats.pt_solutions_intersect_may_alias;
6198 else
6199 ++pta_stats.pt_solutions_intersect_no_alias;
6200 return res;
6204 /* Dump points-to information to OUTFILE. */
6206 static void
6207 dump_sa_points_to_info (FILE *outfile)
6209 unsigned int i;
6211 fprintf (outfile, "\nPoints-to sets\n\n");
6213 if (dump_flags & TDF_STATS)
6215 fprintf (outfile, "Stats:\n");
6216 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
6217 fprintf (outfile, "Non-pointer vars: %d\n",
6218 stats.nonpointer_vars);
6219 fprintf (outfile, "Statically unified vars: %d\n",
6220 stats.unified_vars_static);
6221 fprintf (outfile, "Dynamically unified vars: %d\n",
6222 stats.unified_vars_dynamic);
6223 fprintf (outfile, "Iterations: %d\n", stats.iterations);
6224 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
6225 fprintf (outfile, "Number of implicit edges: %d\n",
6226 stats.num_implicit_edges);
6229 for (i = 0; i < VEC_length (varinfo_t, varmap); i++)
6231 varinfo_t vi = get_varinfo (i);
6232 if (!vi->may_have_pointers)
6233 continue;
6234 dump_solution_for_var (outfile, i);
6239 /* Debug points-to information to stderr. */
6241 DEBUG_FUNCTION void
6242 debug_sa_points_to_info (void)
6244 dump_sa_points_to_info (stderr);
6248 /* Initialize the always-existing constraint variables for NULL
6249 ANYTHING, READONLY, and INTEGER */
6251 static void
6252 init_base_vars (void)
6254 struct constraint_expr lhs, rhs;
6255 varinfo_t var_anything;
6256 varinfo_t var_nothing;
6257 varinfo_t var_readonly;
6258 varinfo_t var_escaped;
6259 varinfo_t var_nonlocal;
6260 varinfo_t var_storedanything;
6261 varinfo_t var_integer;
6263 /* Create the NULL variable, used to represent that a variable points
6264 to NULL. */
6265 var_nothing = new_var_info (NULL_TREE, "NULL");
6266 gcc_assert (var_nothing->id == nothing_id);
6267 var_nothing->is_artificial_var = 1;
6268 var_nothing->offset = 0;
6269 var_nothing->size = ~0;
6270 var_nothing->fullsize = ~0;
6271 var_nothing->is_special_var = 1;
6272 var_nothing->may_have_pointers = 0;
6273 var_nothing->is_global_var = 0;
6275 /* Create the ANYTHING variable, used to represent that a variable
6276 points to some unknown piece of memory. */
6277 var_anything = new_var_info (NULL_TREE, "ANYTHING");
6278 gcc_assert (var_anything->id == anything_id);
6279 var_anything->is_artificial_var = 1;
6280 var_anything->size = ~0;
6281 var_anything->offset = 0;
6282 var_anything->next = NULL;
6283 var_anything->fullsize = ~0;
6284 var_anything->is_special_var = 1;
6286 /* Anything points to anything. This makes deref constraints just
6287 work in the presence of linked list and other p = *p type loops,
6288 by saying that *ANYTHING = ANYTHING. */
6289 lhs.type = SCALAR;
6290 lhs.var = anything_id;
6291 lhs.offset = 0;
6292 rhs.type = ADDRESSOF;
6293 rhs.var = anything_id;
6294 rhs.offset = 0;
6296 /* This specifically does not use process_constraint because
6297 process_constraint ignores all anything = anything constraints, since all
6298 but this one are redundant. */
6299 VEC_safe_push (constraint_t, heap, constraints, new_constraint (lhs, rhs));
6301 /* Create the READONLY variable, used to represent that a variable
6302 points to readonly memory. */
6303 var_readonly = new_var_info (NULL_TREE, "READONLY");
6304 gcc_assert (var_readonly->id == readonly_id);
6305 var_readonly->is_artificial_var = 1;
6306 var_readonly->offset = 0;
6307 var_readonly->size = ~0;
6308 var_readonly->fullsize = ~0;
6309 var_readonly->next = NULL;
6310 var_readonly->is_special_var = 1;
6312 /* readonly memory points to anything, in order to make deref
6313 easier. In reality, it points to anything the particular
6314 readonly variable can point to, but we don't track this
6315 separately. */
6316 lhs.type = SCALAR;
6317 lhs.var = readonly_id;
6318 lhs.offset = 0;
6319 rhs.type = ADDRESSOF;
6320 rhs.var = readonly_id; /* FIXME */
6321 rhs.offset = 0;
6322 process_constraint (new_constraint (lhs, rhs));
6324 /* Create the ESCAPED variable, used to represent the set of escaped
6325 memory. */
6326 var_escaped = new_var_info (NULL_TREE, "ESCAPED");
6327 gcc_assert (var_escaped->id == escaped_id);
6328 var_escaped->is_artificial_var = 1;
6329 var_escaped->offset = 0;
6330 var_escaped->size = ~0;
6331 var_escaped->fullsize = ~0;
6332 var_escaped->is_special_var = 0;
6334 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6335 memory. */
6336 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL");
6337 gcc_assert (var_nonlocal->id == nonlocal_id);
6338 var_nonlocal->is_artificial_var = 1;
6339 var_nonlocal->offset = 0;
6340 var_nonlocal->size = ~0;
6341 var_nonlocal->fullsize = ~0;
6342 var_nonlocal->is_special_var = 1;
6344 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6345 lhs.type = SCALAR;
6346 lhs.var = escaped_id;
6347 lhs.offset = 0;
6348 rhs.type = DEREF;
6349 rhs.var = escaped_id;
6350 rhs.offset = 0;
6351 process_constraint (new_constraint (lhs, rhs));
6353 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6354 whole variable escapes. */
6355 lhs.type = SCALAR;
6356 lhs.var = escaped_id;
6357 lhs.offset = 0;
6358 rhs.type = SCALAR;
6359 rhs.var = escaped_id;
6360 rhs.offset = UNKNOWN_OFFSET;
6361 process_constraint (new_constraint (lhs, rhs));
6363 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6364 everything pointed to by escaped points to what global memory can
6365 point to. */
6366 lhs.type = DEREF;
6367 lhs.var = escaped_id;
6368 lhs.offset = 0;
6369 rhs.type = SCALAR;
6370 rhs.var = nonlocal_id;
6371 rhs.offset = 0;
6372 process_constraint (new_constraint (lhs, rhs));
6374 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6375 global memory may point to global memory and escaped memory. */
6376 lhs.type = SCALAR;
6377 lhs.var = nonlocal_id;
6378 lhs.offset = 0;
6379 rhs.type = ADDRESSOF;
6380 rhs.var = nonlocal_id;
6381 rhs.offset = 0;
6382 process_constraint (new_constraint (lhs, rhs));
6383 rhs.type = ADDRESSOF;
6384 rhs.var = escaped_id;
6385 rhs.offset = 0;
6386 process_constraint (new_constraint (lhs, rhs));
6388 /* Create the STOREDANYTHING variable, used to represent the set of
6389 variables stored to *ANYTHING. */
6390 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING");
6391 gcc_assert (var_storedanything->id == storedanything_id);
6392 var_storedanything->is_artificial_var = 1;
6393 var_storedanything->offset = 0;
6394 var_storedanything->size = ~0;
6395 var_storedanything->fullsize = ~0;
6396 var_storedanything->is_special_var = 0;
6398 /* Create the INTEGER variable, used to represent that a variable points
6399 to what an INTEGER "points to". */
6400 var_integer = new_var_info (NULL_TREE, "INTEGER");
6401 gcc_assert (var_integer->id == integer_id);
6402 var_integer->is_artificial_var = 1;
6403 var_integer->size = ~0;
6404 var_integer->fullsize = ~0;
6405 var_integer->offset = 0;
6406 var_integer->next = NULL;
6407 var_integer->is_special_var = 1;
6409 /* INTEGER = ANYTHING, because we don't know where a dereference of
6410 a random integer will point to. */
6411 lhs.type = SCALAR;
6412 lhs.var = integer_id;
6413 lhs.offset = 0;
6414 rhs.type = ADDRESSOF;
6415 rhs.var = anything_id;
6416 rhs.offset = 0;
6417 process_constraint (new_constraint (lhs, rhs));
6420 /* Initialize things necessary to perform PTA */
6422 static void
6423 init_alias_vars (void)
6425 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
6427 bitmap_obstack_initialize (&pta_obstack);
6428 bitmap_obstack_initialize (&oldpta_obstack);
6429 bitmap_obstack_initialize (&predbitmap_obstack);
6431 constraint_pool = create_alloc_pool ("Constraint pool",
6432 sizeof (struct constraint), 30);
6433 variable_info_pool = create_alloc_pool ("Variable info pool",
6434 sizeof (struct variable_info), 30);
6435 constraints = VEC_alloc (constraint_t, heap, 8);
6436 varmap = VEC_alloc (varinfo_t, heap, 8);
6437 vi_for_tree = pointer_map_create ();
6438 call_stmt_vars = pointer_map_create ();
6440 memset (&stats, 0, sizeof (stats));
6441 shared_bitmap_table = htab_create (511, shared_bitmap_hash,
6442 shared_bitmap_eq, free);
6443 init_base_vars ();
6445 gcc_obstack_init (&fake_var_decl_obstack);
6448 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6449 predecessor edges. */
6451 static void
6452 remove_preds_and_fake_succs (constraint_graph_t graph)
6454 unsigned int i;
6456 /* Clear the implicit ref and address nodes from the successor
6457 lists. */
6458 for (i = 0; i < FIRST_REF_NODE; i++)
6460 if (graph->succs[i])
6461 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
6462 FIRST_REF_NODE * 2);
6465 /* Free the successor list for the non-ref nodes. */
6466 for (i = FIRST_REF_NODE; i < graph->size; i++)
6468 if (graph->succs[i])
6469 BITMAP_FREE (graph->succs[i]);
6472 /* Now reallocate the size of the successor list as, and blow away
6473 the predecessor bitmaps. */
6474 graph->size = VEC_length (varinfo_t, varmap);
6475 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
6477 free (graph->implicit_preds);
6478 graph->implicit_preds = NULL;
6479 free (graph->preds);
6480 graph->preds = NULL;
6481 bitmap_obstack_release (&predbitmap_obstack);
6484 /* Solve the constraint set. */
6486 static void
6487 solve_constraints (void)
6489 struct scc_info *si;
6491 if (dump_file)
6492 fprintf (dump_file,
6493 "\nCollapsing static cycles and doing variable "
6494 "substitution\n");
6496 init_graph (VEC_length (varinfo_t, varmap) * 2);
6498 if (dump_file)
6499 fprintf (dump_file, "Building predecessor graph\n");
6500 build_pred_graph ();
6502 if (dump_file)
6503 fprintf (dump_file, "Detecting pointer and location "
6504 "equivalences\n");
6505 si = perform_var_substitution (graph);
6507 if (dump_file)
6508 fprintf (dump_file, "Rewriting constraints and unifying "
6509 "variables\n");
6510 rewrite_constraints (graph, si);
6512 build_succ_graph ();
6514 free_var_substitution_info (si);
6516 /* Attach complex constraints to graph nodes. */
6517 move_complex_constraints (graph);
6519 if (dump_file)
6520 fprintf (dump_file, "Uniting pointer but not location equivalent "
6521 "variables\n");
6522 unite_pointer_equivalences (graph);
6524 if (dump_file)
6525 fprintf (dump_file, "Finding indirect cycles\n");
6526 find_indirect_cycles (graph);
6528 /* Implicit nodes and predecessors are no longer necessary at this
6529 point. */
6530 remove_preds_and_fake_succs (graph);
6532 if (dump_file && (dump_flags & TDF_GRAPH))
6534 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
6535 "in dot format:\n");
6536 dump_constraint_graph (dump_file);
6537 fprintf (dump_file, "\n\n");
6540 if (dump_file)
6541 fprintf (dump_file, "Solving graph\n");
6543 solve_graph (graph);
6545 if (dump_file && (dump_flags & TDF_GRAPH))
6547 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
6548 "in dot format:\n");
6549 dump_constraint_graph (dump_file);
6550 fprintf (dump_file, "\n\n");
6553 if (dump_file)
6554 dump_sa_points_to_info (dump_file);
6557 /* Create points-to sets for the current function. See the comments
6558 at the start of the file for an algorithmic overview. */
6560 static void
6561 compute_points_to_sets (void)
6563 basic_block bb;
6564 unsigned i;
6565 varinfo_t vi;
6567 timevar_push (TV_TREE_PTA);
6569 init_alias_vars ();
6571 intra_create_variable_infos ();
6573 /* Now walk all statements and build the constraint set. */
6574 FOR_EACH_BB (bb)
6576 gimple_stmt_iterator gsi;
6578 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6580 gimple phi = gsi_stmt (gsi);
6582 if (is_gimple_reg (gimple_phi_result (phi)))
6583 find_func_aliases (phi);
6586 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6588 gimple stmt = gsi_stmt (gsi);
6590 find_func_aliases (stmt);
6594 if (dump_file)
6596 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
6597 dump_constraints (dump_file, 0);
6600 /* From the constraints compute the points-to sets. */
6601 solve_constraints ();
6603 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6604 find_what_var_points_to (get_varinfo (escaped_id),
6605 &cfun->gimple_df->escaped);
6607 /* Make sure the ESCAPED solution (which is used as placeholder in
6608 other solutions) does not reference itself. This simplifies
6609 points-to solution queries. */
6610 cfun->gimple_df->escaped.escaped = 0;
6612 /* Mark escaped HEAP variables as global. */
6613 FOR_EACH_VEC_ELT (varinfo_t, varmap, i, vi)
6614 if (vi->is_heap_var
6615 && !vi->is_global_var)
6616 DECL_EXTERNAL (vi->decl) = vi->is_global_var
6617 = pt_solution_includes (&cfun->gimple_df->escaped, vi->decl);
6619 /* Compute the points-to sets for pointer SSA_NAMEs. */
6620 for (i = 0; i < num_ssa_names; ++i)
6622 tree ptr = ssa_name (i);
6623 if (ptr
6624 && POINTER_TYPE_P (TREE_TYPE (ptr)))
6625 find_what_p_points_to (ptr);
6628 /* Compute the call-used/clobbered sets. */
6629 FOR_EACH_BB (bb)
6631 gimple_stmt_iterator gsi;
6633 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6635 gimple stmt = gsi_stmt (gsi);
6636 struct pt_solution *pt;
6637 if (!is_gimple_call (stmt))
6638 continue;
6640 pt = gimple_call_use_set (stmt);
6641 if (gimple_call_flags (stmt) & ECF_CONST)
6642 memset (pt, 0, sizeof (struct pt_solution));
6643 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
6645 find_what_var_points_to (vi, pt);
6646 /* Escaped (and thus nonlocal) variables are always
6647 implicitly used by calls. */
6648 /* ??? ESCAPED can be empty even though NONLOCAL
6649 always escaped. */
6650 pt->nonlocal = 1;
6651 pt->escaped = 1;
6653 else
6655 /* If there is nothing special about this call then
6656 we have made everything that is used also escape. */
6657 *pt = cfun->gimple_df->escaped;
6658 pt->nonlocal = 1;
6661 pt = gimple_call_clobber_set (stmt);
6662 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
6663 memset (pt, 0, sizeof (struct pt_solution));
6664 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
6666 find_what_var_points_to (vi, pt);
6667 /* Escaped (and thus nonlocal) variables are always
6668 implicitly clobbered by calls. */
6669 /* ??? ESCAPED can be empty even though NONLOCAL
6670 always escaped. */
6671 pt->nonlocal = 1;
6672 pt->escaped = 1;
6674 else
6676 /* If there is nothing special about this call then
6677 we have made everything that is used also escape. */
6678 *pt = cfun->gimple_df->escaped;
6679 pt->nonlocal = 1;
6684 timevar_pop (TV_TREE_PTA);
6688 /* Delete created points-to sets. */
6690 static void
6691 delete_points_to_sets (void)
6693 unsigned int i;
6695 htab_delete (shared_bitmap_table);
6696 if (dump_file && (dump_flags & TDF_STATS))
6697 fprintf (dump_file, "Points to sets created:%d\n",
6698 stats.points_to_sets_created);
6700 pointer_map_destroy (vi_for_tree);
6701 pointer_map_destroy (call_stmt_vars);
6702 bitmap_obstack_release (&pta_obstack);
6703 VEC_free (constraint_t, heap, constraints);
6705 for (i = 0; i < graph->size; i++)
6706 VEC_free (constraint_t, heap, graph->complex[i]);
6707 free (graph->complex);
6709 free (graph->rep);
6710 free (graph->succs);
6711 free (graph->pe);
6712 free (graph->pe_rep);
6713 free (graph->indirect_cycles);
6714 free (graph);
6716 VEC_free (varinfo_t, heap, varmap);
6717 free_alloc_pool (variable_info_pool);
6718 free_alloc_pool (constraint_pool);
6720 obstack_free (&fake_var_decl_obstack, NULL);
6724 /* Compute points-to information for every SSA_NAME pointer in the
6725 current function and compute the transitive closure of escaped
6726 variables to re-initialize the call-clobber states of local variables. */
6728 unsigned int
6729 compute_may_aliases (void)
6731 if (cfun->gimple_df->ipa_pta)
6733 if (dump_file)
6735 fprintf (dump_file, "\nNot re-computing points-to information "
6736 "because IPA points-to information is available.\n\n");
6738 /* But still dump what we have remaining it. */
6739 dump_alias_info (dump_file);
6741 if (dump_flags & TDF_DETAILS)
6742 dump_referenced_vars (dump_file);
6745 return 0;
6748 /* For each pointer P_i, determine the sets of variables that P_i may
6749 point-to. Compute the reachability set of escaped and call-used
6750 variables. */
6751 compute_points_to_sets ();
6753 /* Debugging dumps. */
6754 if (dump_file)
6756 dump_alias_info (dump_file);
6758 if (dump_flags & TDF_DETAILS)
6759 dump_referenced_vars (dump_file);
6762 /* Deallocate memory used by aliasing data structures and the internal
6763 points-to solution. */
6764 delete_points_to_sets ();
6766 gcc_assert (!need_ssa_update_p (cfun));
6768 return 0;
6771 static bool
6772 gate_tree_pta (void)
6774 return flag_tree_pta;
6777 /* A dummy pass to cause points-to information to be computed via
6778 TODO_rebuild_alias. */
6780 struct gimple_opt_pass pass_build_alias =
6783 GIMPLE_PASS,
6784 "alias", /* name */
6785 gate_tree_pta, /* gate */
6786 NULL, /* execute */
6787 NULL, /* sub */
6788 NULL, /* next */
6789 0, /* static_pass_number */
6790 TV_NONE, /* tv_id */
6791 PROP_cfg | PROP_ssa, /* properties_required */
6792 0, /* properties_provided */
6793 0, /* properties_destroyed */
6794 0, /* todo_flags_start */
6795 TODO_rebuild_alias /* todo_flags_finish */
6799 /* A dummy pass to cause points-to information to be computed via
6800 TODO_rebuild_alias. */
6802 struct gimple_opt_pass pass_build_ealias =
6805 GIMPLE_PASS,
6806 "ealias", /* name */
6807 gate_tree_pta, /* gate */
6808 NULL, /* execute */
6809 NULL, /* sub */
6810 NULL, /* next */
6811 0, /* static_pass_number */
6812 TV_NONE, /* tv_id */
6813 PROP_cfg | PROP_ssa, /* properties_required */
6814 0, /* properties_provided */
6815 0, /* properties_destroyed */
6816 0, /* todo_flags_start */
6817 TODO_rebuild_alias /* todo_flags_finish */
6822 /* Return true if we should execute IPA PTA. */
6823 static bool
6824 gate_ipa_pta (void)
6826 return (optimize
6827 && flag_ipa_pta
6828 /* Don't bother doing anything if the program has errors. */
6829 && !seen_error ());
6832 /* IPA PTA solutions for ESCAPED. */
6833 struct pt_solution ipa_escaped_pt
6834 = { true, false, false, false, false, false, NULL };
6836 /* Associate node with varinfo DATA. Worker for
6837 cgraph_for_node_and_aliases. */
6838 static bool
6839 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
6841 if (node->alias || node->thunk.thunk_p)
6842 insert_vi_for_tree (node->symbol.decl, (varinfo_t)data);
6843 return false;
6846 /* Execute the driver for IPA PTA. */
6847 static unsigned int
6848 ipa_pta_execute (void)
6850 struct cgraph_node *node;
6851 struct varpool_node *var;
6852 int from;
6854 in_ipa_mode = 1;
6856 init_alias_vars ();
6858 if (dump_file && (dump_flags & TDF_DETAILS))
6860 dump_symtab (dump_file);
6861 fprintf (dump_file, "\n");
6864 /* Build the constraints. */
6865 FOR_EACH_DEFINED_FUNCTION (node)
6867 varinfo_t vi;
6868 /* Nodes without a body are not interesting. Especially do not
6869 visit clones at this point for now - we get duplicate decls
6870 there for inline clones at least. */
6871 if (!cgraph_function_with_gimple_body_p (node))
6872 continue;
6874 gcc_assert (!node->clone_of);
6876 vi = create_function_info_for (node->symbol.decl,
6877 alias_get_name (node->symbol.decl));
6878 cgraph_for_node_and_aliases (node, associate_varinfo_to_alias, vi, true);
6881 /* Create constraints for global variables and their initializers. */
6882 FOR_EACH_VARIABLE (var)
6884 if (var->alias)
6885 continue;
6887 get_vi_for_tree (var->symbol.decl);
6890 if (dump_file)
6892 fprintf (dump_file,
6893 "Generating constraints for global initializers\n\n");
6894 dump_constraints (dump_file, 0);
6895 fprintf (dump_file, "\n");
6897 from = VEC_length (constraint_t, constraints);
6899 FOR_EACH_DEFINED_FUNCTION (node)
6901 struct function *func;
6902 basic_block bb;
6903 tree old_func_decl;
6905 /* Nodes without a body are not interesting. */
6906 if (!cgraph_function_with_gimple_body_p (node))
6907 continue;
6909 if (dump_file)
6911 fprintf (dump_file,
6912 "Generating constraints for %s", cgraph_node_name (node));
6913 if (DECL_ASSEMBLER_NAME_SET_P (node->symbol.decl))
6914 fprintf (dump_file, " (%s)",
6915 IDENTIFIER_POINTER
6916 (DECL_ASSEMBLER_NAME (node->symbol.decl)));
6917 fprintf (dump_file, "\n");
6920 func = DECL_STRUCT_FUNCTION (node->symbol.decl);
6921 old_func_decl = current_function_decl;
6922 push_cfun (func);
6923 current_function_decl = node->symbol.decl;
6925 /* For externally visible or attribute used annotated functions use
6926 local constraints for their arguments.
6927 For local functions we see all callers and thus do not need initial
6928 constraints for parameters. */
6929 if (node->symbol.used_from_other_partition
6930 || node->symbol.externally_visible
6931 || node->symbol.force_output)
6933 intra_create_variable_infos ();
6935 /* We also need to make function return values escape. Nothing
6936 escapes by returning from main though. */
6937 if (!MAIN_NAME_P (DECL_NAME (node->symbol.decl)))
6939 varinfo_t fi, rvi;
6940 fi = lookup_vi_for_tree (node->symbol.decl);
6941 rvi = first_vi_for_offset (fi, fi_result);
6942 if (rvi && rvi->offset == fi_result)
6944 struct constraint_expr includes;
6945 struct constraint_expr var;
6946 includes.var = escaped_id;
6947 includes.offset = 0;
6948 includes.type = SCALAR;
6949 var.var = rvi->id;
6950 var.offset = 0;
6951 var.type = SCALAR;
6952 process_constraint (new_constraint (includes, var));
6957 /* Build constriants for the function body. */
6958 FOR_EACH_BB_FN (bb, func)
6960 gimple_stmt_iterator gsi;
6962 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
6963 gsi_next (&gsi))
6965 gimple phi = gsi_stmt (gsi);
6967 if (is_gimple_reg (gimple_phi_result (phi)))
6968 find_func_aliases (phi);
6971 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6973 gimple stmt = gsi_stmt (gsi);
6975 find_func_aliases (stmt);
6976 find_func_clobbers (stmt);
6980 current_function_decl = old_func_decl;
6981 pop_cfun ();
6983 if (dump_file)
6985 fprintf (dump_file, "\n");
6986 dump_constraints (dump_file, from);
6987 fprintf (dump_file, "\n");
6989 from = VEC_length (constraint_t, constraints);
6992 /* From the constraints compute the points-to sets. */
6993 solve_constraints ();
6995 /* Compute the global points-to sets for ESCAPED.
6996 ??? Note that the computed escape set is not correct
6997 for the whole unit as we fail to consider graph edges to
6998 externally visible functions. */
6999 find_what_var_points_to (get_varinfo (escaped_id), &ipa_escaped_pt);
7001 /* Make sure the ESCAPED solution (which is used as placeholder in
7002 other solutions) does not reference itself. This simplifies
7003 points-to solution queries. */
7004 ipa_escaped_pt.ipa_escaped = 0;
7006 /* Assign the points-to sets to the SSA names in the unit. */
7007 FOR_EACH_DEFINED_FUNCTION (node)
7009 tree ptr;
7010 struct function *fn;
7011 unsigned i;
7012 varinfo_t fi;
7013 basic_block bb;
7014 struct pt_solution uses, clobbers;
7015 struct cgraph_edge *e;
7017 /* Nodes without a body are not interesting. */
7018 if (!cgraph_function_with_gimple_body_p (node))
7019 continue;
7021 fn = DECL_STRUCT_FUNCTION (node->symbol.decl);
7023 /* Compute the points-to sets for pointer SSA_NAMEs. */
7024 FOR_EACH_VEC_ELT (tree, fn->gimple_df->ssa_names, i, ptr)
7026 if (ptr
7027 && POINTER_TYPE_P (TREE_TYPE (ptr)))
7028 find_what_p_points_to (ptr);
7031 /* Compute the call-use and call-clobber sets for all direct calls. */
7032 fi = lookup_vi_for_tree (node->symbol.decl);
7033 gcc_assert (fi->is_fn_info);
7034 find_what_var_points_to (first_vi_for_offset (fi, fi_clobbers),
7035 &clobbers);
7036 find_what_var_points_to (first_vi_for_offset (fi, fi_uses), &uses);
7037 for (e = node->callers; e; e = e->next_caller)
7039 if (!e->call_stmt)
7040 continue;
7042 *gimple_call_clobber_set (e->call_stmt) = clobbers;
7043 *gimple_call_use_set (e->call_stmt) = uses;
7046 /* Compute the call-use and call-clobber sets for indirect calls
7047 and calls to external functions. */
7048 FOR_EACH_BB_FN (bb, fn)
7050 gimple_stmt_iterator gsi;
7052 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7054 gimple stmt = gsi_stmt (gsi);
7055 struct pt_solution *pt;
7056 varinfo_t vi;
7057 tree decl;
7059 if (!is_gimple_call (stmt))
7060 continue;
7062 /* Handle direct calls to external functions. */
7063 decl = gimple_call_fndecl (stmt);
7064 if (decl
7065 && (!(fi = lookup_vi_for_tree (decl))
7066 || !fi->is_fn_info))
7068 pt = gimple_call_use_set (stmt);
7069 if (gimple_call_flags (stmt) & ECF_CONST)
7070 memset (pt, 0, sizeof (struct pt_solution));
7071 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7073 find_what_var_points_to (vi, pt);
7074 /* Escaped (and thus nonlocal) variables are always
7075 implicitly used by calls. */
7076 /* ??? ESCAPED can be empty even though NONLOCAL
7077 always escaped. */
7078 pt->nonlocal = 1;
7079 pt->ipa_escaped = 1;
7081 else
7083 /* If there is nothing special about this call then
7084 we have made everything that is used also escape. */
7085 *pt = ipa_escaped_pt;
7086 pt->nonlocal = 1;
7089 pt = gimple_call_clobber_set (stmt);
7090 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7091 memset (pt, 0, sizeof (struct pt_solution));
7092 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7094 find_what_var_points_to (vi, pt);
7095 /* Escaped (and thus nonlocal) variables are always
7096 implicitly clobbered by calls. */
7097 /* ??? ESCAPED can be empty even though NONLOCAL
7098 always escaped. */
7099 pt->nonlocal = 1;
7100 pt->ipa_escaped = 1;
7102 else
7104 /* If there is nothing special about this call then
7105 we have made everything that is used also escape. */
7106 *pt = ipa_escaped_pt;
7107 pt->nonlocal = 1;
7111 /* Handle indirect calls. */
7112 if (!decl
7113 && (fi = get_fi_for_callee (stmt)))
7115 /* We need to accumulate all clobbers/uses of all possible
7116 callees. */
7117 fi = get_varinfo (find (fi->id));
7118 /* If we cannot constrain the set of functions we'll end up
7119 calling we end up using/clobbering everything. */
7120 if (bitmap_bit_p (fi->solution, anything_id)
7121 || bitmap_bit_p (fi->solution, nonlocal_id)
7122 || bitmap_bit_p (fi->solution, escaped_id))
7124 pt_solution_reset (gimple_call_clobber_set (stmt));
7125 pt_solution_reset (gimple_call_use_set (stmt));
7127 else
7129 bitmap_iterator bi;
7130 unsigned i;
7131 struct pt_solution *uses, *clobbers;
7133 uses = gimple_call_use_set (stmt);
7134 clobbers = gimple_call_clobber_set (stmt);
7135 memset (uses, 0, sizeof (struct pt_solution));
7136 memset (clobbers, 0, sizeof (struct pt_solution));
7137 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
7139 struct pt_solution sol;
7141 vi = get_varinfo (i);
7142 if (!vi->is_fn_info)
7144 /* ??? We could be more precise here? */
7145 uses->nonlocal = 1;
7146 uses->ipa_escaped = 1;
7147 clobbers->nonlocal = 1;
7148 clobbers->ipa_escaped = 1;
7149 continue;
7152 if (!uses->anything)
7154 find_what_var_points_to
7155 (first_vi_for_offset (vi, fi_uses), &sol);
7156 pt_solution_ior_into (uses, &sol);
7158 if (!clobbers->anything)
7160 find_what_var_points_to
7161 (first_vi_for_offset (vi, fi_clobbers), &sol);
7162 pt_solution_ior_into (clobbers, &sol);
7170 fn->gimple_df->ipa_pta = true;
7173 delete_points_to_sets ();
7175 in_ipa_mode = 0;
7177 return 0;
7180 struct simple_ipa_opt_pass pass_ipa_pta =
7183 SIMPLE_IPA_PASS,
7184 "pta", /* name */
7185 gate_ipa_pta, /* gate */
7186 ipa_pta_execute, /* execute */
7187 NULL, /* sub */
7188 NULL, /* next */
7189 0, /* static_pass_number */
7190 TV_IPA_PTA, /* tv_id */
7191 0, /* properties_required */
7192 0, /* properties_provided */
7193 0, /* properties_destroyed */
7194 0, /* todo_flags_start */
7195 TODO_update_ssa /* todo_flags_finish */