2011-07-21 François Dumont <francois.cppdevs@free.fr>
[official-gcc.git] / gcc / tree-ssa-structalias.c
blob9bfbad63f70dee07994c65211d1e08ca0940cf18
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 "output.h"
32 #include "tree.h"
33 #include "tree-flow.h"
34 #include "tree-inline.h"
35 #include "diagnostic-core.h"
36 #include "gimple.h"
37 #include "hashtab.h"
38 #include "function.h"
39 #include "cgraph.h"
40 #include "tree-pass.h"
41 #include "timevar.h"
42 #include "alloc-pool.h"
43 #include "splay-tree.h"
44 #include "params.h"
45 #include "cgraph.h"
46 #include "alias.h"
47 #include "pointer-set.h"
49 /* The idea behind this analyzer is to generate set constraints from the
50 program, then solve the resulting constraints in order to generate the
51 points-to sets.
53 Set constraints are a way of modeling program analysis problems that
54 involve sets. They consist of an inclusion constraint language,
55 describing the variables (each variable is a set) and operations that
56 are involved on the variables, and a set of rules that derive facts
57 from these operations. To solve a system of set constraints, you derive
58 all possible facts under the rules, which gives you the correct sets
59 as a consequence.
61 See "Efficient Field-sensitive pointer analysis for C" by "David
62 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
63 http://citeseer.ist.psu.edu/pearce04efficient.html
65 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
66 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
67 http://citeseer.ist.psu.edu/heintze01ultrafast.html
69 There are three types of real constraint expressions, DEREF,
70 ADDRESSOF, and SCALAR. Each constraint expression consists
71 of a constraint type, a variable, and an offset.
73 SCALAR is a constraint expression type used to represent x, whether
74 it appears on the LHS or the RHS of a statement.
75 DEREF is a constraint expression type used to represent *x, whether
76 it appears on the LHS or the RHS of a statement.
77 ADDRESSOF is a constraint expression used to represent &x, whether
78 it appears on the LHS or the RHS of a statement.
80 Each pointer variable in the program is assigned an integer id, and
81 each field of a structure variable is assigned an integer id as well.
83 Structure variables are linked to their list of fields through a "next
84 field" in each variable that points to the next field in offset
85 order.
86 Each variable for a structure field has
88 1. "size", that tells the size in bits of that field.
89 2. "fullsize, that tells the size in bits of the entire structure.
90 3. "offset", that tells the offset in bits from the beginning of the
91 structure to this field.
93 Thus,
94 struct f
96 int a;
97 int b;
98 } foo;
99 int *bar;
101 looks like
103 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
104 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
105 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
108 In order to solve the system of set constraints, the following is
109 done:
111 1. Each constraint variable x has a solution set associated with it,
112 Sol(x).
114 2. Constraints are separated into direct, copy, and complex.
115 Direct constraints are ADDRESSOF constraints that require no extra
116 processing, such as P = &Q
117 Copy constraints are those of the form P = Q.
118 Complex constraints are all the constraints involving dereferences
119 and offsets (including offsetted copies).
121 3. All direct constraints of the form P = &Q are processed, such
122 that Q is added to Sol(P)
124 4. All complex constraints for a given constraint variable are stored in a
125 linked list attached to that variable's node.
127 5. A directed graph is built out of the copy constraints. Each
128 constraint variable is a node in the graph, and an edge from
129 Q to P is added for each copy constraint of the form P = Q
131 6. The graph is then walked, and solution sets are
132 propagated along the copy edges, such that an edge from Q to P
133 causes Sol(P) <- Sol(P) union Sol(Q).
135 7. As we visit each node, all complex constraints associated with
136 that node are processed by adding appropriate copy edges to the graph, or the
137 appropriate variables to the solution set.
139 8. The process of walking the graph is iterated until no solution
140 sets change.
142 Prior to walking the graph in steps 6 and 7, We perform static
143 cycle elimination on the constraint graph, as well
144 as off-line variable substitution.
146 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
147 on and turned into anything), but isn't. You can just see what offset
148 inside the pointed-to struct it's going to access.
150 TODO: Constant bounded arrays can be handled as if they were structs of the
151 same number of elements.
153 TODO: Modeling heap and incoming pointers becomes much better if we
154 add fields to them as we discover them, which we could do.
156 TODO: We could handle unions, but to be honest, it's probably not
157 worth the pain or slowdown. */
159 /* IPA-PTA optimizations possible.
161 When the indirect function called is ANYTHING we can add disambiguation
162 based on the function signatures (or simply the parameter count which
163 is the varinfo size). We also do not need to consider functions that
164 do not have their address taken.
166 The is_global_var bit which marks escape points is overly conservative
167 in IPA mode. Split it to is_escape_point and is_global_var - only
168 externally visible globals are escape points in IPA mode. This is
169 also needed to fix the pt_solution_includes_global predicate
170 (and thus ptr_deref_may_alias_global_p).
172 The way we introduce DECL_PT_UID to avoid fixing up all points-to
173 sets in the translation unit when we copy a DECL during inlining
174 pessimizes precision. The advantage is that the DECL_PT_UID keeps
175 compile-time and memory usage overhead low - the points-to sets
176 do not grow or get unshared as they would during a fixup phase.
177 An alternative solution is to delay IPA PTA until after all
178 inlining transformations have been applied.
180 The way we propagate clobber/use information isn't optimized.
181 It should use a new complex constraint that properly filters
182 out local variables of the callee (though that would make
183 the sets invalid after inlining). OTOH we might as well
184 admit defeat to WHOPR and simply do all the clobber/use analysis
185 and propagation after PTA finished but before we threw away
186 points-to information for memory variables. WHOPR and PTA
187 do not play along well anyway - the whole constraint solving
188 would need to be done in WPA phase and it will be very interesting
189 to apply the results to local SSA names during LTRANS phase.
191 We probably should compute a per-function unit-ESCAPE solution
192 propagating it simply like the clobber / uses solutions. The
193 solution can go alongside the non-IPA espaced solution and be
194 used to query which vars escape the unit through a function.
196 We never put function decls in points-to sets so we do not
197 keep the set of called functions for indirect calls.
199 And probably more. */
201 static bool use_field_sensitive = true;
202 static int in_ipa_mode = 0;
204 /* Used for predecessor bitmaps. */
205 static bitmap_obstack predbitmap_obstack;
207 /* Used for points-to sets. */
208 static bitmap_obstack pta_obstack;
210 /* Used for oldsolution members of variables. */
211 static bitmap_obstack oldpta_obstack;
213 /* Used for per-solver-iteration bitmaps. */
214 static bitmap_obstack iteration_obstack;
216 static unsigned int create_variable_info_for (tree, const char *);
217 typedef struct constraint_graph *constraint_graph_t;
218 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
220 struct constraint;
221 typedef struct constraint *constraint_t;
223 DEF_VEC_P(constraint_t);
224 DEF_VEC_ALLOC_P(constraint_t,heap);
226 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
227 if (a) \
228 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
230 static struct constraint_stats
232 unsigned int total_vars;
233 unsigned int nonpointer_vars;
234 unsigned int unified_vars_static;
235 unsigned int unified_vars_dynamic;
236 unsigned int iterations;
237 unsigned int num_edges;
238 unsigned int num_implicit_edges;
239 unsigned int points_to_sets_created;
240 } stats;
242 struct variable_info
244 /* ID of this variable */
245 unsigned int id;
247 /* True if this is a variable created by the constraint analysis, such as
248 heap variables and constraints we had to break up. */
249 unsigned int is_artificial_var : 1;
251 /* True if this is a special variable whose solution set should not be
252 changed. */
253 unsigned int is_special_var : 1;
255 /* True for variables whose size is not known or variable. */
256 unsigned int is_unknown_size_var : 1;
258 /* True for (sub-)fields that represent a whole variable. */
259 unsigned int is_full_var : 1;
261 /* True if this is a heap variable. */
262 unsigned int is_heap_var : 1;
264 /* True if this is a variable tracking a restrict pointer source. */
265 unsigned int is_restrict_var : 1;
267 /* True if this field may contain pointers. */
268 unsigned int may_have_pointers : 1;
270 /* True if this field has only restrict qualified pointers. */
271 unsigned int only_restrict_pointers : 1;
273 /* True if this represents a global variable. */
274 unsigned int is_global_var : 1;
276 /* True if this represents a IPA function info. */
277 unsigned int is_fn_info : 1;
279 /* A link to the variable for the next field in this structure. */
280 struct variable_info *next;
282 /* Offset of this variable, in bits, from the base variable */
283 unsigned HOST_WIDE_INT offset;
285 /* Size of the variable, in bits. */
286 unsigned HOST_WIDE_INT size;
288 /* Full size of the base variable, in bits. */
289 unsigned HOST_WIDE_INT fullsize;
291 /* Name of this variable */
292 const char *name;
294 /* Tree that this variable is associated with. */
295 tree decl;
297 /* Points-to set for this variable. */
298 bitmap solution;
300 /* Old points-to set for this variable. */
301 bitmap oldsolution;
303 typedef struct variable_info *varinfo_t;
305 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
306 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
307 unsigned HOST_WIDE_INT);
308 static varinfo_t lookup_vi_for_tree (tree);
310 /* Pool of variable info structures. */
311 static alloc_pool variable_info_pool;
313 DEF_VEC_P(varinfo_t);
315 DEF_VEC_ALLOC_P(varinfo_t, heap);
317 /* Table of variable info structures for constraint variables.
318 Indexed directly by variable info id. */
319 static VEC(varinfo_t,heap) *varmap;
321 /* Return the varmap element N */
323 static inline varinfo_t
324 get_varinfo (unsigned int n)
326 return VEC_index (varinfo_t, varmap, n);
329 /* Static IDs for the special variables. */
330 enum { nothing_id = 0, anything_id = 1, readonly_id = 2,
331 escaped_id = 3, nonlocal_id = 4,
332 storedanything_id = 5, integer_id = 6 };
334 /* Return a new variable info structure consisting for a variable
335 named NAME, and using constraint graph node NODE. Append it
336 to the vector of variable info structures. */
338 static varinfo_t
339 new_var_info (tree t, const char *name)
341 unsigned index = VEC_length (varinfo_t, varmap);
342 varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
344 ret->id = index;
345 ret->name = name;
346 ret->decl = t;
347 /* Vars without decl are artificial and do not have sub-variables. */
348 ret->is_artificial_var = (t == NULL_TREE);
349 ret->is_special_var = false;
350 ret->is_unknown_size_var = false;
351 ret->is_full_var = (t == NULL_TREE);
352 ret->is_heap_var = false;
353 ret->is_restrict_var = false;
354 ret->may_have_pointers = true;
355 ret->only_restrict_pointers = false;
356 ret->is_global_var = (t == NULL_TREE);
357 ret->is_fn_info = false;
358 if (t && DECL_P (t))
359 ret->is_global_var = (is_global_var (t)
360 /* We have to treat even local register variables
361 as escape points. */
362 || (TREE_CODE (t) == VAR_DECL
363 && DECL_HARD_REGISTER (t)));
364 ret->solution = BITMAP_ALLOC (&pta_obstack);
365 ret->oldsolution = NULL;
366 ret->next = NULL;
368 stats.total_vars++;
370 VEC_safe_push (varinfo_t, heap, varmap, ret);
372 return ret;
376 /* A map mapping call statements to per-stmt variables for uses
377 and clobbers specific to the call. */
378 struct pointer_map_t *call_stmt_vars;
380 /* Lookup or create the variable for the call statement CALL. */
382 static varinfo_t
383 get_call_vi (gimple call)
385 void **slot_p;
386 varinfo_t vi, vi2;
388 slot_p = pointer_map_insert (call_stmt_vars, call);
389 if (*slot_p)
390 return (varinfo_t) *slot_p;
392 vi = new_var_info (NULL_TREE, "CALLUSED");
393 vi->offset = 0;
394 vi->size = 1;
395 vi->fullsize = 2;
396 vi->is_full_var = true;
398 vi->next = vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED");
399 vi2->offset = 1;
400 vi2->size = 1;
401 vi2->fullsize = 2;
402 vi2->is_full_var = true;
404 *slot_p = (void *) vi;
405 return vi;
408 /* Lookup the variable for the call statement CALL representing
409 the uses. Returns NULL if there is nothing special about this call. */
411 static varinfo_t
412 lookup_call_use_vi (gimple call)
414 void **slot_p;
416 slot_p = pointer_map_contains (call_stmt_vars, call);
417 if (slot_p)
418 return (varinfo_t) *slot_p;
420 return NULL;
423 /* Lookup the variable for the call statement CALL representing
424 the clobbers. Returns NULL if there is nothing special about this call. */
426 static varinfo_t
427 lookup_call_clobber_vi (gimple call)
429 varinfo_t uses = lookup_call_use_vi (call);
430 if (!uses)
431 return NULL;
433 return uses->next;
436 /* Lookup or create the variable for the call statement CALL representing
437 the uses. */
439 static varinfo_t
440 get_call_use_vi (gimple call)
442 return get_call_vi (call);
445 /* Lookup or create the variable for the call statement CALL representing
446 the clobbers. */
448 static varinfo_t ATTRIBUTE_UNUSED
449 get_call_clobber_vi (gimple call)
451 return get_call_vi (call)->next;
455 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
457 /* An expression that appears in a constraint. */
459 struct constraint_expr
461 /* Constraint type. */
462 constraint_expr_type type;
464 /* Variable we are referring to in the constraint. */
465 unsigned int var;
467 /* Offset, in bits, of this constraint from the beginning of
468 variables it ends up referring to.
470 IOW, in a deref constraint, we would deref, get the result set,
471 then add OFFSET to each member. */
472 HOST_WIDE_INT offset;
475 /* Use 0x8000... as special unknown offset. */
476 #define UNKNOWN_OFFSET ((HOST_WIDE_INT)-1 << (HOST_BITS_PER_WIDE_INT-1))
478 typedef struct constraint_expr ce_s;
479 DEF_VEC_O(ce_s);
480 DEF_VEC_ALLOC_O(ce_s, heap);
481 static void get_constraint_for_1 (tree, VEC(ce_s, heap) **, bool, bool);
482 static void get_constraint_for (tree, VEC(ce_s, heap) **);
483 static void get_constraint_for_rhs (tree, VEC(ce_s, heap) **);
484 static void do_deref (VEC (ce_s, heap) **);
486 /* Our set constraints are made up of two constraint expressions, one
487 LHS, and one RHS.
489 As described in the introduction, our set constraints each represent an
490 operation between set valued variables.
492 struct constraint
494 struct constraint_expr lhs;
495 struct constraint_expr rhs;
498 /* List of constraints that we use to build the constraint graph from. */
500 static VEC(constraint_t,heap) *constraints;
501 static alloc_pool constraint_pool;
503 /* The constraint graph is represented as an array of bitmaps
504 containing successor nodes. */
506 struct constraint_graph
508 /* Size of this graph, which may be different than the number of
509 nodes in the variable map. */
510 unsigned int size;
512 /* Explicit successors of each node. */
513 bitmap *succs;
515 /* Implicit predecessors of each node (Used for variable
516 substitution). */
517 bitmap *implicit_preds;
519 /* Explicit predecessors of each node (Used for variable substitution). */
520 bitmap *preds;
522 /* Indirect cycle representatives, or -1 if the node has no indirect
523 cycles. */
524 int *indirect_cycles;
526 /* Representative node for a node. rep[a] == a unless the node has
527 been unified. */
528 unsigned int *rep;
530 /* Equivalence class representative for a label. This is used for
531 variable substitution. */
532 int *eq_rep;
534 /* Pointer equivalence label for a node. All nodes with the same
535 pointer equivalence label can be unified together at some point
536 (either during constraint optimization or after the constraint
537 graph is built). */
538 unsigned int *pe;
540 /* Pointer equivalence representative for a label. This is used to
541 handle nodes that are pointer equivalent but not location
542 equivalent. We can unite these once the addressof constraints
543 are transformed into initial points-to sets. */
544 int *pe_rep;
546 /* Pointer equivalence label for each node, used during variable
547 substitution. */
548 unsigned int *pointer_label;
550 /* Location equivalence label for each node, used during location
551 equivalence finding. */
552 unsigned int *loc_label;
554 /* Pointed-by set for each node, used during location equivalence
555 finding. This is pointed-by rather than pointed-to, because it
556 is constructed using the predecessor graph. */
557 bitmap *pointed_by;
559 /* Points to sets for pointer equivalence. This is *not* the actual
560 points-to sets for nodes. */
561 bitmap *points_to;
563 /* Bitmap of nodes where the bit is set if the node is a direct
564 node. Used for variable substitution. */
565 sbitmap direct_nodes;
567 /* Bitmap of nodes where the bit is set if the node is address
568 taken. Used for variable substitution. */
569 bitmap address_taken;
571 /* Vector of complex constraints for each graph node. Complex
572 constraints are those involving dereferences or offsets that are
573 not 0. */
574 VEC(constraint_t,heap) **complex;
577 static constraint_graph_t graph;
579 /* During variable substitution and the offline version of indirect
580 cycle finding, we create nodes to represent dereferences and
581 address taken constraints. These represent where these start and
582 end. */
583 #define FIRST_REF_NODE (VEC_length (varinfo_t, varmap))
584 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
586 /* Return the representative node for NODE, if NODE has been unioned
587 with another NODE.
588 This function performs path compression along the way to finding
589 the representative. */
591 static unsigned int
592 find (unsigned int node)
594 gcc_assert (node < graph->size);
595 if (graph->rep[node] != node)
596 return graph->rep[node] = find (graph->rep[node]);
597 return node;
600 /* Union the TO and FROM nodes to the TO nodes.
601 Note that at some point in the future, we may want to do
602 union-by-rank, in which case we are going to have to return the
603 node we unified to. */
605 static bool
606 unite (unsigned int to, unsigned int from)
608 gcc_assert (to < graph->size && from < graph->size);
609 if (to != from && graph->rep[from] != to)
611 graph->rep[from] = to;
612 return true;
614 return false;
617 /* Create a new constraint consisting of LHS and RHS expressions. */
619 static constraint_t
620 new_constraint (const struct constraint_expr lhs,
621 const struct constraint_expr rhs)
623 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
624 ret->lhs = lhs;
625 ret->rhs = rhs;
626 return ret;
629 /* Print out constraint C to FILE. */
631 static void
632 dump_constraint (FILE *file, constraint_t c)
634 if (c->lhs.type == ADDRESSOF)
635 fprintf (file, "&");
636 else if (c->lhs.type == DEREF)
637 fprintf (file, "*");
638 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
639 if (c->lhs.offset == UNKNOWN_OFFSET)
640 fprintf (file, " + UNKNOWN");
641 else if (c->lhs.offset != 0)
642 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
643 fprintf (file, " = ");
644 if (c->rhs.type == ADDRESSOF)
645 fprintf (file, "&");
646 else if (c->rhs.type == DEREF)
647 fprintf (file, "*");
648 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
649 if (c->rhs.offset == UNKNOWN_OFFSET)
650 fprintf (file, " + UNKNOWN");
651 else if (c->rhs.offset != 0)
652 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
656 void debug_constraint (constraint_t);
657 void debug_constraints (void);
658 void debug_constraint_graph (void);
659 void debug_solution_for_var (unsigned int);
660 void debug_sa_points_to_info (void);
662 /* Print out constraint C to stderr. */
664 DEBUG_FUNCTION void
665 debug_constraint (constraint_t c)
667 dump_constraint (stderr, c);
668 fprintf (stderr, "\n");
671 /* Print out all constraints to FILE */
673 static void
674 dump_constraints (FILE *file, int from)
676 int i;
677 constraint_t c;
678 for (i = from; VEC_iterate (constraint_t, constraints, i, c); i++)
679 if (c)
681 dump_constraint (file, c);
682 fprintf (file, "\n");
686 /* Print out all constraints to stderr. */
688 DEBUG_FUNCTION void
689 debug_constraints (void)
691 dump_constraints (stderr, 0);
694 /* Print the constraint graph in dot format. */
696 static void
697 dump_constraint_graph (FILE *file)
699 unsigned int i;
701 /* Only print the graph if it has already been initialized: */
702 if (!graph)
703 return;
705 /* Prints the header of the dot file: */
706 fprintf (file, "strict digraph {\n");
707 fprintf (file, " node [\n shape = box\n ]\n");
708 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
709 fprintf (file, "\n // List of nodes and complex constraints in "
710 "the constraint graph:\n");
712 /* The next lines print the nodes in the graph together with the
713 complex constraints attached to them. */
714 for (i = 0; i < graph->size; i++)
716 if (find (i) != i)
717 continue;
718 if (i < FIRST_REF_NODE)
719 fprintf (file, "\"%s\"", get_varinfo (i)->name);
720 else
721 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
722 if (graph->complex[i])
724 unsigned j;
725 constraint_t c;
726 fprintf (file, " [label=\"\\N\\n");
727 for (j = 0; VEC_iterate (constraint_t, graph->complex[i], j, c); ++j)
729 dump_constraint (file, c);
730 fprintf (file, "\\l");
732 fprintf (file, "\"]");
734 fprintf (file, ";\n");
737 /* Go over the edges. */
738 fprintf (file, "\n // Edges in the constraint graph:\n");
739 for (i = 0; i < graph->size; i++)
741 unsigned j;
742 bitmap_iterator bi;
743 if (find (i) != i)
744 continue;
745 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
747 unsigned to = find (j);
748 if (i == to)
749 continue;
750 if (i < FIRST_REF_NODE)
751 fprintf (file, "\"%s\"", get_varinfo (i)->name);
752 else
753 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
754 fprintf (file, " -> ");
755 if (to < FIRST_REF_NODE)
756 fprintf (file, "\"%s\"", get_varinfo (to)->name);
757 else
758 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name);
759 fprintf (file, ";\n");
763 /* Prints the tail of the dot file. */
764 fprintf (file, "}\n");
767 /* Print out the constraint graph to stderr. */
769 DEBUG_FUNCTION void
770 debug_constraint_graph (void)
772 dump_constraint_graph (stderr);
775 /* SOLVER FUNCTIONS
777 The solver is a simple worklist solver, that works on the following
778 algorithm:
780 sbitmap changed_nodes = all zeroes;
781 changed_count = 0;
782 For each node that is not already collapsed:
783 changed_count++;
784 set bit in changed nodes
786 while (changed_count > 0)
788 compute topological ordering for constraint graph
790 find and collapse cycles in the constraint graph (updating
791 changed if necessary)
793 for each node (n) in the graph in topological order:
794 changed_count--;
796 Process each complex constraint associated with the node,
797 updating changed if necessary.
799 For each outgoing edge from n, propagate the solution from n to
800 the destination of the edge, updating changed as necessary.
802 } */
804 /* Return true if two constraint expressions A and B are equal. */
806 static bool
807 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
809 return a.type == b.type && a.var == b.var && a.offset == b.offset;
812 /* Return true if constraint expression A is less than constraint expression
813 B. This is just arbitrary, but consistent, in order to give them an
814 ordering. */
816 static bool
817 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
819 if (a.type == b.type)
821 if (a.var == b.var)
822 return a.offset < b.offset;
823 else
824 return a.var < b.var;
826 else
827 return a.type < b.type;
830 /* Return true if constraint A is less than constraint B. This is just
831 arbitrary, but consistent, in order to give them an ordering. */
833 static bool
834 constraint_less (const constraint_t a, const constraint_t b)
836 if (constraint_expr_less (a->lhs, b->lhs))
837 return true;
838 else if (constraint_expr_less (b->lhs, a->lhs))
839 return false;
840 else
841 return constraint_expr_less (a->rhs, b->rhs);
844 /* Return true if two constraints A and B are equal. */
846 static bool
847 constraint_equal (struct constraint a, struct constraint b)
849 return constraint_expr_equal (a.lhs, b.lhs)
850 && constraint_expr_equal (a.rhs, b.rhs);
854 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
856 static constraint_t
857 constraint_vec_find (VEC(constraint_t,heap) *vec,
858 struct constraint lookfor)
860 unsigned int place;
861 constraint_t found;
863 if (vec == NULL)
864 return NULL;
866 place = VEC_lower_bound (constraint_t, vec, &lookfor, constraint_less);
867 if (place >= VEC_length (constraint_t, vec))
868 return NULL;
869 found = VEC_index (constraint_t, vec, place);
870 if (!constraint_equal (*found, lookfor))
871 return NULL;
872 return found;
875 /* Union two constraint vectors, TO and FROM. Put the result in TO. */
877 static void
878 constraint_set_union (VEC(constraint_t,heap) **to,
879 VEC(constraint_t,heap) **from)
881 int i;
882 constraint_t c;
884 FOR_EACH_VEC_ELT (constraint_t, *from, i, c)
886 if (constraint_vec_find (*to, *c) == NULL)
888 unsigned int place = VEC_lower_bound (constraint_t, *to, c,
889 constraint_less);
890 VEC_safe_insert (constraint_t, heap, *to, place, c);
895 /* Expands the solution in SET to all sub-fields of variables included.
896 Union the expanded result into RESULT. */
898 static void
899 solution_set_expand (bitmap result, bitmap set)
901 bitmap_iterator bi;
902 bitmap vars = NULL;
903 unsigned j;
905 /* In a first pass record all variables we need to add all
906 sub-fields off. This avoids quadratic behavior. */
907 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
909 varinfo_t v = get_varinfo (j);
910 if (v->is_artificial_var
911 || v->is_full_var)
912 continue;
913 v = lookup_vi_for_tree (v->decl);
914 if (vars == NULL)
915 vars = BITMAP_ALLOC (NULL);
916 bitmap_set_bit (vars, v->id);
919 /* In the second pass now do the addition to the solution and
920 to speed up solving add it to the delta as well. */
921 if (vars != NULL)
923 EXECUTE_IF_SET_IN_BITMAP (vars, 0, j, bi)
925 varinfo_t v = get_varinfo (j);
926 for (; v != NULL; v = v->next)
927 bitmap_set_bit (result, v->id);
929 BITMAP_FREE (vars);
933 /* Take a solution set SET, add OFFSET to each member of the set, and
934 overwrite SET with the result when done. */
936 static void
937 solution_set_add (bitmap set, HOST_WIDE_INT offset)
939 bitmap result = BITMAP_ALLOC (&iteration_obstack);
940 unsigned int i;
941 bitmap_iterator bi;
943 /* If the offset is unknown we have to expand the solution to
944 all subfields. */
945 if (offset == UNKNOWN_OFFSET)
947 solution_set_expand (set, set);
948 return;
951 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
953 varinfo_t vi = get_varinfo (i);
955 /* If this is a variable with just one field just set its bit
956 in the result. */
957 if (vi->is_artificial_var
958 || vi->is_unknown_size_var
959 || vi->is_full_var)
960 bitmap_set_bit (result, i);
961 else
963 unsigned HOST_WIDE_INT fieldoffset = vi->offset + offset;
965 /* If the offset makes the pointer point to before the
966 variable use offset zero for the field lookup. */
967 if (offset < 0
968 && fieldoffset > vi->offset)
969 fieldoffset = 0;
971 if (offset != 0)
972 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
974 bitmap_set_bit (result, vi->id);
975 /* If the result is not exactly at fieldoffset include the next
976 field as well. See get_constraint_for_ptr_offset for more
977 rationale. */
978 if (vi->offset != fieldoffset
979 && vi->next != NULL)
980 bitmap_set_bit (result, vi->next->id);
984 bitmap_copy (set, result);
985 BITMAP_FREE (result);
988 /* Union solution sets TO and FROM, and add INC to each member of FROM in the
989 process. */
991 static bool
992 set_union_with_increment (bitmap to, bitmap from, HOST_WIDE_INT inc)
994 if (inc == 0)
995 return bitmap_ior_into (to, from);
996 else
998 bitmap tmp;
999 bool res;
1001 tmp = BITMAP_ALLOC (&iteration_obstack);
1002 bitmap_copy (tmp, from);
1003 solution_set_add (tmp, inc);
1004 res = bitmap_ior_into (to, tmp);
1005 BITMAP_FREE (tmp);
1006 return res;
1010 /* Insert constraint C into the list of complex constraints for graph
1011 node VAR. */
1013 static void
1014 insert_into_complex (constraint_graph_t graph,
1015 unsigned int var, constraint_t c)
1017 VEC (constraint_t, heap) *complex = graph->complex[var];
1018 unsigned int place = VEC_lower_bound (constraint_t, complex, c,
1019 constraint_less);
1021 /* Only insert constraints that do not already exist. */
1022 if (place >= VEC_length (constraint_t, complex)
1023 || !constraint_equal (*c, *VEC_index (constraint_t, complex, place)))
1024 VEC_safe_insert (constraint_t, heap, graph->complex[var], place, c);
1028 /* Condense two variable nodes into a single variable node, by moving
1029 all associated info from SRC to TO. */
1031 static void
1032 merge_node_constraints (constraint_graph_t graph, unsigned int to,
1033 unsigned int from)
1035 unsigned int i;
1036 constraint_t c;
1038 gcc_assert (find (from) == to);
1040 /* Move all complex constraints from src node into to node */
1041 FOR_EACH_VEC_ELT (constraint_t, graph->complex[from], i, c)
1043 /* In complex constraints for node src, we may have either
1044 a = *src, and *src = a, or an offseted constraint which are
1045 always added to the rhs node's constraints. */
1047 if (c->rhs.type == DEREF)
1048 c->rhs.var = to;
1049 else if (c->lhs.type == DEREF)
1050 c->lhs.var = to;
1051 else
1052 c->rhs.var = to;
1054 constraint_set_union (&graph->complex[to], &graph->complex[from]);
1055 VEC_free (constraint_t, heap, graph->complex[from]);
1056 graph->complex[from] = NULL;
1060 /* Remove edges involving NODE from GRAPH. */
1062 static void
1063 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
1065 if (graph->succs[node])
1066 BITMAP_FREE (graph->succs[node]);
1069 /* Merge GRAPH nodes FROM and TO into node TO. */
1071 static void
1072 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1073 unsigned int from)
1075 if (graph->indirect_cycles[from] != -1)
1077 /* If we have indirect cycles with the from node, and we have
1078 none on the to node, the to node has indirect cycles from the
1079 from node now that they are unified.
1080 If indirect cycles exist on both, unify the nodes that they
1081 are in a cycle with, since we know they are in a cycle with
1082 each other. */
1083 if (graph->indirect_cycles[to] == -1)
1084 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1087 /* Merge all the successor edges. */
1088 if (graph->succs[from])
1090 if (!graph->succs[to])
1091 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1092 bitmap_ior_into (graph->succs[to],
1093 graph->succs[from]);
1096 clear_edges_for_node (graph, from);
1100 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1101 it doesn't exist in the graph already. */
1103 static void
1104 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1105 unsigned int from)
1107 if (to == from)
1108 return;
1110 if (!graph->implicit_preds[to])
1111 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1113 if (bitmap_set_bit (graph->implicit_preds[to], from))
1114 stats.num_implicit_edges++;
1117 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1118 it doesn't exist in the graph already.
1119 Return false if the edge already existed, true otherwise. */
1121 static void
1122 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1123 unsigned int from)
1125 if (!graph->preds[to])
1126 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1127 bitmap_set_bit (graph->preds[to], from);
1130 /* Add a graph edge to GRAPH, going from FROM to TO if
1131 it doesn't exist in the graph already.
1132 Return false if the edge already existed, true otherwise. */
1134 static bool
1135 add_graph_edge (constraint_graph_t graph, unsigned int to,
1136 unsigned int from)
1138 if (to == from)
1140 return false;
1142 else
1144 bool r = false;
1146 if (!graph->succs[from])
1147 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1148 if (bitmap_set_bit (graph->succs[from], to))
1150 r = true;
1151 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1152 stats.num_edges++;
1154 return r;
1159 /* Return true if {DEST.SRC} is an existing graph edge in GRAPH. */
1161 static bool
1162 valid_graph_edge (constraint_graph_t graph, unsigned int src,
1163 unsigned int dest)
1165 return (graph->succs[dest]
1166 && bitmap_bit_p (graph->succs[dest], src));
1169 /* Initialize the constraint graph structure to contain SIZE nodes. */
1171 static void
1172 init_graph (unsigned int size)
1174 unsigned int j;
1176 graph = XCNEW (struct constraint_graph);
1177 graph->size = size;
1178 graph->succs = XCNEWVEC (bitmap, graph->size);
1179 graph->indirect_cycles = XNEWVEC (int, graph->size);
1180 graph->rep = XNEWVEC (unsigned int, graph->size);
1181 graph->complex = XCNEWVEC (VEC(constraint_t, heap) *, size);
1182 graph->pe = XCNEWVEC (unsigned int, graph->size);
1183 graph->pe_rep = XNEWVEC (int, graph->size);
1185 for (j = 0; j < graph->size; j++)
1187 graph->rep[j] = j;
1188 graph->pe_rep[j] = -1;
1189 graph->indirect_cycles[j] = -1;
1193 /* Build the constraint graph, adding only predecessor edges right now. */
1195 static void
1196 build_pred_graph (void)
1198 int i;
1199 constraint_t c;
1200 unsigned int j;
1202 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1203 graph->preds = XCNEWVEC (bitmap, graph->size);
1204 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1205 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1206 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1207 graph->points_to = XCNEWVEC (bitmap, graph->size);
1208 graph->eq_rep = XNEWVEC (int, graph->size);
1209 graph->direct_nodes = sbitmap_alloc (graph->size);
1210 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1211 sbitmap_zero (graph->direct_nodes);
1213 for (j = 0; j < FIRST_REF_NODE; j++)
1215 if (!get_varinfo (j)->is_special_var)
1216 SET_BIT (graph->direct_nodes, j);
1219 for (j = 0; j < graph->size; j++)
1220 graph->eq_rep[j] = -1;
1222 for (j = 0; j < VEC_length (varinfo_t, varmap); j++)
1223 graph->indirect_cycles[j] = -1;
1225 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
1227 struct constraint_expr lhs = c->lhs;
1228 struct constraint_expr rhs = c->rhs;
1229 unsigned int lhsvar = lhs.var;
1230 unsigned int rhsvar = rhs.var;
1232 if (lhs.type == DEREF)
1234 /* *x = y. */
1235 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1236 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1238 else if (rhs.type == DEREF)
1240 /* x = *y */
1241 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1242 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1243 else
1244 RESET_BIT (graph->direct_nodes, lhsvar);
1246 else if (rhs.type == ADDRESSOF)
1248 varinfo_t v;
1250 /* x = &y */
1251 if (graph->points_to[lhsvar] == NULL)
1252 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1253 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1255 if (graph->pointed_by[rhsvar] == NULL)
1256 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1257 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1259 /* Implicitly, *x = y */
1260 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1262 /* All related variables are no longer direct nodes. */
1263 RESET_BIT (graph->direct_nodes, rhsvar);
1264 v = get_varinfo (rhsvar);
1265 if (!v->is_full_var)
1267 v = lookup_vi_for_tree (v->decl);
1270 RESET_BIT (graph->direct_nodes, v->id);
1271 v = v->next;
1273 while (v != NULL);
1275 bitmap_set_bit (graph->address_taken, rhsvar);
1277 else if (lhsvar > anything_id
1278 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1280 /* x = y */
1281 add_pred_graph_edge (graph, lhsvar, rhsvar);
1282 /* Implicitly, *x = *y */
1283 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1284 FIRST_REF_NODE + rhsvar);
1286 else if (lhs.offset != 0 || rhs.offset != 0)
1288 if (rhs.offset != 0)
1289 RESET_BIT (graph->direct_nodes, lhs.var);
1290 else if (lhs.offset != 0)
1291 RESET_BIT (graph->direct_nodes, rhs.var);
1296 /* Build the constraint graph, adding successor edges. */
1298 static void
1299 build_succ_graph (void)
1301 unsigned i, t;
1302 constraint_t c;
1304 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
1306 struct constraint_expr lhs;
1307 struct constraint_expr rhs;
1308 unsigned int lhsvar;
1309 unsigned int rhsvar;
1311 if (!c)
1312 continue;
1314 lhs = c->lhs;
1315 rhs = c->rhs;
1316 lhsvar = find (lhs.var);
1317 rhsvar = find (rhs.var);
1319 if (lhs.type == DEREF)
1321 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1322 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1324 else if (rhs.type == DEREF)
1326 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1327 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1329 else if (rhs.type == ADDRESSOF)
1331 /* x = &y */
1332 gcc_assert (find (rhs.var) == rhs.var);
1333 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1335 else if (lhsvar > anything_id
1336 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1338 add_graph_edge (graph, lhsvar, rhsvar);
1342 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1343 receive pointers. */
1344 t = find (storedanything_id);
1345 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1347 if (!TEST_BIT (graph->direct_nodes, i)
1348 && get_varinfo (i)->may_have_pointers)
1349 add_graph_edge (graph, find (i), t);
1352 /* Everything stored to ANYTHING also potentially escapes. */
1353 add_graph_edge (graph, find (escaped_id), t);
1357 /* Changed variables on the last iteration. */
1358 static bitmap changed;
1360 /* Strongly Connected Component visitation info. */
1362 struct scc_info
1364 sbitmap visited;
1365 sbitmap deleted;
1366 unsigned int *dfs;
1367 unsigned int *node_mapping;
1368 int current_index;
1369 VEC(unsigned,heap) *scc_stack;
1373 /* Recursive routine to find strongly connected components in GRAPH.
1374 SI is the SCC info to store the information in, and N is the id of current
1375 graph node we are processing.
1377 This is Tarjan's strongly connected component finding algorithm, as
1378 modified by Nuutila to keep only non-root nodes on the stack.
1379 The algorithm can be found in "On finding the strongly connected
1380 connected components in a directed graph" by Esko Nuutila and Eljas
1381 Soisalon-Soininen, in Information Processing Letters volume 49,
1382 number 1, pages 9-14. */
1384 static void
1385 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1387 unsigned int i;
1388 bitmap_iterator bi;
1389 unsigned int my_dfs;
1391 SET_BIT (si->visited, n);
1392 si->dfs[n] = si->current_index ++;
1393 my_dfs = si->dfs[n];
1395 /* Visit all the successors. */
1396 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1398 unsigned int w;
1400 if (i > LAST_REF_NODE)
1401 break;
1403 w = find (i);
1404 if (TEST_BIT (si->deleted, w))
1405 continue;
1407 if (!TEST_BIT (si->visited, w))
1408 scc_visit (graph, si, w);
1410 unsigned int t = find (w);
1411 unsigned int nnode = find (n);
1412 gcc_assert (nnode == n);
1414 if (si->dfs[t] < si->dfs[nnode])
1415 si->dfs[n] = si->dfs[t];
1419 /* See if any components have been identified. */
1420 if (si->dfs[n] == my_dfs)
1422 if (VEC_length (unsigned, si->scc_stack) > 0
1423 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1425 bitmap scc = BITMAP_ALLOC (NULL);
1426 unsigned int lowest_node;
1427 bitmap_iterator bi;
1429 bitmap_set_bit (scc, n);
1431 while (VEC_length (unsigned, si->scc_stack) != 0
1432 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1434 unsigned int w = VEC_pop (unsigned, si->scc_stack);
1436 bitmap_set_bit (scc, w);
1439 lowest_node = bitmap_first_set_bit (scc);
1440 gcc_assert (lowest_node < FIRST_REF_NODE);
1442 /* Collapse the SCC nodes into a single node, and mark the
1443 indirect cycles. */
1444 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1446 if (i < FIRST_REF_NODE)
1448 if (unite (lowest_node, i))
1449 unify_nodes (graph, lowest_node, i, false);
1451 else
1453 unite (lowest_node, i);
1454 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1458 SET_BIT (si->deleted, n);
1460 else
1461 VEC_safe_push (unsigned, heap, si->scc_stack, n);
1464 /* Unify node FROM into node TO, updating the changed count if
1465 necessary when UPDATE_CHANGED is true. */
1467 static void
1468 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1469 bool update_changed)
1472 gcc_assert (to != from && find (to) == to);
1473 if (dump_file && (dump_flags & TDF_DETAILS))
1474 fprintf (dump_file, "Unifying %s to %s\n",
1475 get_varinfo (from)->name,
1476 get_varinfo (to)->name);
1478 if (update_changed)
1479 stats.unified_vars_dynamic++;
1480 else
1481 stats.unified_vars_static++;
1483 merge_graph_nodes (graph, to, from);
1484 merge_node_constraints (graph, to, from);
1486 /* Mark TO as changed if FROM was changed. If TO was already marked
1487 as changed, decrease the changed count. */
1489 if (update_changed
1490 && bitmap_bit_p (changed, from))
1492 bitmap_clear_bit (changed, from);
1493 bitmap_set_bit (changed, to);
1495 if (get_varinfo (from)->solution)
1497 /* If the solution changes because of the merging, we need to mark
1498 the variable as changed. */
1499 if (bitmap_ior_into (get_varinfo (to)->solution,
1500 get_varinfo (from)->solution))
1502 if (update_changed)
1503 bitmap_set_bit (changed, to);
1506 BITMAP_FREE (get_varinfo (from)->solution);
1507 if (get_varinfo (from)->oldsolution)
1508 BITMAP_FREE (get_varinfo (from)->oldsolution);
1510 if (stats.iterations > 0
1511 && get_varinfo (to)->oldsolution)
1512 BITMAP_FREE (get_varinfo (to)->oldsolution);
1514 if (valid_graph_edge (graph, to, to))
1516 if (graph->succs[to])
1517 bitmap_clear_bit (graph->succs[to], to);
1521 /* Information needed to compute the topological ordering of a graph. */
1523 struct topo_info
1525 /* sbitmap of visited nodes. */
1526 sbitmap visited;
1527 /* Array that stores the topological order of the graph, *in
1528 reverse*. */
1529 VEC(unsigned,heap) *topo_order;
1533 /* Initialize and return a topological info structure. */
1535 static struct topo_info *
1536 init_topo_info (void)
1538 size_t size = graph->size;
1539 struct topo_info *ti = XNEW (struct topo_info);
1540 ti->visited = sbitmap_alloc (size);
1541 sbitmap_zero (ti->visited);
1542 ti->topo_order = VEC_alloc (unsigned, heap, 1);
1543 return ti;
1547 /* Free the topological sort info pointed to by TI. */
1549 static void
1550 free_topo_info (struct topo_info *ti)
1552 sbitmap_free (ti->visited);
1553 VEC_free (unsigned, heap, ti->topo_order);
1554 free (ti);
1557 /* Visit the graph in topological order, and store the order in the
1558 topo_info structure. */
1560 static void
1561 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1562 unsigned int n)
1564 bitmap_iterator bi;
1565 unsigned int j;
1567 SET_BIT (ti->visited, n);
1569 if (graph->succs[n])
1570 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1572 if (!TEST_BIT (ti->visited, j))
1573 topo_visit (graph, ti, j);
1576 VEC_safe_push (unsigned, heap, ti->topo_order, n);
1579 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1580 starting solution for y. */
1582 static void
1583 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1584 bitmap delta)
1586 unsigned int lhs = c->lhs.var;
1587 bool flag = false;
1588 bitmap sol = get_varinfo (lhs)->solution;
1589 unsigned int j;
1590 bitmap_iterator bi;
1591 HOST_WIDE_INT roffset = c->rhs.offset;
1593 /* Our IL does not allow this. */
1594 gcc_assert (c->lhs.offset == 0);
1596 /* If the solution of Y contains anything it is good enough to transfer
1597 this to the LHS. */
1598 if (bitmap_bit_p (delta, anything_id))
1600 flag |= bitmap_set_bit (sol, anything_id);
1601 goto done;
1604 /* If we do not know at with offset the rhs is dereferenced compute
1605 the reachability set of DELTA, conservatively assuming it is
1606 dereferenced at all valid offsets. */
1607 if (roffset == UNKNOWN_OFFSET)
1609 solution_set_expand (delta, delta);
1610 /* No further offset processing is necessary. */
1611 roffset = 0;
1614 /* For each variable j in delta (Sol(y)), add
1615 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1616 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1618 varinfo_t v = get_varinfo (j);
1619 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1620 unsigned int t;
1622 if (v->is_full_var)
1623 fieldoffset = v->offset;
1624 else if (roffset != 0)
1625 v = first_vi_for_offset (v, fieldoffset);
1626 /* If the access is outside of the variable we can ignore it. */
1627 if (!v)
1628 continue;
1632 t = find (v->id);
1634 /* Adding edges from the special vars is pointless.
1635 They don't have sets that can change. */
1636 if (get_varinfo (t)->is_special_var)
1637 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1638 /* Merging the solution from ESCAPED needlessly increases
1639 the set. Use ESCAPED as representative instead. */
1640 else if (v->id == escaped_id)
1641 flag |= bitmap_set_bit (sol, escaped_id);
1642 else if (v->may_have_pointers
1643 && add_graph_edge (graph, lhs, t))
1644 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1646 /* If the variable is not exactly at the requested offset
1647 we have to include the next one. */
1648 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1649 || v->next == NULL)
1650 break;
1652 v = v->next;
1653 fieldoffset = v->offset;
1655 while (1);
1658 done:
1659 /* If the LHS solution changed, mark the var as changed. */
1660 if (flag)
1662 get_varinfo (lhs)->solution = sol;
1663 bitmap_set_bit (changed, lhs);
1667 /* Process a constraint C that represents *(x + off) = y using DELTA
1668 as the starting solution for x. */
1670 static void
1671 do_ds_constraint (constraint_t c, bitmap delta)
1673 unsigned int rhs = c->rhs.var;
1674 bitmap sol = get_varinfo (rhs)->solution;
1675 unsigned int j;
1676 bitmap_iterator bi;
1677 HOST_WIDE_INT loff = c->lhs.offset;
1678 bool escaped_p = false;
1680 /* Our IL does not allow this. */
1681 gcc_assert (c->rhs.offset == 0);
1683 /* If the solution of y contains ANYTHING simply use the ANYTHING
1684 solution. This avoids needlessly increasing the points-to sets. */
1685 if (bitmap_bit_p (sol, anything_id))
1686 sol = get_varinfo (find (anything_id))->solution;
1688 /* If the solution for x contains ANYTHING we have to merge the
1689 solution of y into all pointer variables which we do via
1690 STOREDANYTHING. */
1691 if (bitmap_bit_p (delta, anything_id))
1693 unsigned t = find (storedanything_id);
1694 if (add_graph_edge (graph, t, rhs))
1696 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1697 bitmap_set_bit (changed, t);
1699 return;
1702 /* If we do not know at with offset the rhs is dereferenced compute
1703 the reachability set of DELTA, conservatively assuming it is
1704 dereferenced at all valid offsets. */
1705 if (loff == UNKNOWN_OFFSET)
1707 solution_set_expand (delta, delta);
1708 loff = 0;
1711 /* For each member j of delta (Sol(x)), add an edge from y to j and
1712 union Sol(y) into Sol(j) */
1713 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1715 varinfo_t v = get_varinfo (j);
1716 unsigned int t;
1717 HOST_WIDE_INT fieldoffset = v->offset + loff;
1719 if (v->is_full_var)
1720 fieldoffset = v->offset;
1721 else if (loff != 0)
1722 v = first_vi_for_offset (v, fieldoffset);
1723 /* If the access is outside of the variable we can ignore it. */
1724 if (!v)
1725 continue;
1729 if (v->may_have_pointers)
1731 /* If v is a global variable then this is an escape point. */
1732 if (v->is_global_var
1733 && !escaped_p)
1735 t = find (escaped_id);
1736 if (add_graph_edge (graph, t, rhs)
1737 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1738 bitmap_set_bit (changed, t);
1739 /* Enough to let rhs escape once. */
1740 escaped_p = true;
1743 if (v->is_special_var)
1744 break;
1746 t = find (v->id);
1747 if (add_graph_edge (graph, t, rhs)
1748 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1749 bitmap_set_bit (changed, t);
1752 /* If the variable is not exactly at the requested offset
1753 we have to include the next one. */
1754 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1755 || v->next == NULL)
1756 break;
1758 v = v->next;
1759 fieldoffset = v->offset;
1761 while (1);
1765 /* Handle a non-simple (simple meaning requires no iteration),
1766 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1768 static void
1769 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta)
1771 if (c->lhs.type == DEREF)
1773 if (c->rhs.type == ADDRESSOF)
1775 gcc_unreachable();
1777 else
1779 /* *x = y */
1780 do_ds_constraint (c, delta);
1783 else if (c->rhs.type == DEREF)
1785 /* x = *y */
1786 if (!(get_varinfo (c->lhs.var)->is_special_var))
1787 do_sd_constraint (graph, c, delta);
1789 else
1791 bitmap tmp;
1792 bitmap solution;
1793 bool flag = false;
1795 gcc_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR);
1796 solution = get_varinfo (c->rhs.var)->solution;
1797 tmp = get_varinfo (c->lhs.var)->solution;
1799 flag = set_union_with_increment (tmp, solution, c->rhs.offset);
1801 if (flag)
1803 get_varinfo (c->lhs.var)->solution = tmp;
1804 bitmap_set_bit (changed, c->lhs.var);
1809 /* Initialize and return a new SCC info structure. */
1811 static struct scc_info *
1812 init_scc_info (size_t size)
1814 struct scc_info *si = XNEW (struct scc_info);
1815 size_t i;
1817 si->current_index = 0;
1818 si->visited = sbitmap_alloc (size);
1819 sbitmap_zero (si->visited);
1820 si->deleted = sbitmap_alloc (size);
1821 sbitmap_zero (si->deleted);
1822 si->node_mapping = XNEWVEC (unsigned int, size);
1823 si->dfs = XCNEWVEC (unsigned int, size);
1825 for (i = 0; i < size; i++)
1826 si->node_mapping[i] = i;
1828 si->scc_stack = VEC_alloc (unsigned, heap, 1);
1829 return si;
1832 /* Free an SCC info structure pointed to by SI */
1834 static void
1835 free_scc_info (struct scc_info *si)
1837 sbitmap_free (si->visited);
1838 sbitmap_free (si->deleted);
1839 free (si->node_mapping);
1840 free (si->dfs);
1841 VEC_free (unsigned, heap, si->scc_stack);
1842 free (si);
1846 /* Find indirect cycles in GRAPH that occur, using strongly connected
1847 components, and note them in the indirect cycles map.
1849 This technique comes from Ben Hardekopf and Calvin Lin,
1850 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1851 Lines of Code", submitted to PLDI 2007. */
1853 static void
1854 find_indirect_cycles (constraint_graph_t graph)
1856 unsigned int i;
1857 unsigned int size = graph->size;
1858 struct scc_info *si = init_scc_info (size);
1860 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1861 if (!TEST_BIT (si->visited, i) && find (i) == i)
1862 scc_visit (graph, si, i);
1864 free_scc_info (si);
1867 /* Compute a topological ordering for GRAPH, and store the result in the
1868 topo_info structure TI. */
1870 static void
1871 compute_topo_order (constraint_graph_t graph,
1872 struct topo_info *ti)
1874 unsigned int i;
1875 unsigned int size = graph->size;
1877 for (i = 0; i != size; ++i)
1878 if (!TEST_BIT (ti->visited, i) && find (i) == i)
1879 topo_visit (graph, ti, i);
1882 /* Structure used to for hash value numbering of pointer equivalence
1883 classes. */
1885 typedef struct equiv_class_label
1887 hashval_t hashcode;
1888 unsigned int equivalence_class;
1889 bitmap labels;
1890 } *equiv_class_label_t;
1891 typedef const struct equiv_class_label *const_equiv_class_label_t;
1893 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1894 classes. */
1895 static htab_t pointer_equiv_class_table;
1897 /* A hashtable for mapping a bitmap of labels->location equivalence
1898 classes. */
1899 static htab_t location_equiv_class_table;
1901 /* Hash function for a equiv_class_label_t */
1903 static hashval_t
1904 equiv_class_label_hash (const void *p)
1906 const_equiv_class_label_t const ecl = (const_equiv_class_label_t) p;
1907 return ecl->hashcode;
1910 /* Equality function for two equiv_class_label_t's. */
1912 static int
1913 equiv_class_label_eq (const void *p1, const void *p2)
1915 const_equiv_class_label_t const eql1 = (const_equiv_class_label_t) p1;
1916 const_equiv_class_label_t const eql2 = (const_equiv_class_label_t) p2;
1917 return (eql1->hashcode == eql2->hashcode
1918 && bitmap_equal_p (eql1->labels, eql2->labels));
1921 /* Lookup a equivalence class in TABLE by the bitmap of LABELS it
1922 contains. */
1924 static unsigned int
1925 equiv_class_lookup (htab_t table, bitmap labels)
1927 void **slot;
1928 struct equiv_class_label ecl;
1930 ecl.labels = labels;
1931 ecl.hashcode = bitmap_hash (labels);
1933 slot = htab_find_slot_with_hash (table, &ecl,
1934 ecl.hashcode, NO_INSERT);
1935 if (!slot)
1936 return 0;
1937 else
1938 return ((equiv_class_label_t) *slot)->equivalence_class;
1942 /* Add an equivalence class named EQUIVALENCE_CLASS with labels LABELS
1943 to TABLE. */
1945 static void
1946 equiv_class_add (htab_t table, unsigned int equivalence_class,
1947 bitmap labels)
1949 void **slot;
1950 equiv_class_label_t ecl = XNEW (struct equiv_class_label);
1952 ecl->labels = labels;
1953 ecl->equivalence_class = equivalence_class;
1954 ecl->hashcode = bitmap_hash (labels);
1956 slot = htab_find_slot_with_hash (table, ecl,
1957 ecl->hashcode, INSERT);
1958 gcc_assert (!*slot);
1959 *slot = (void *) ecl;
1962 /* Perform offline variable substitution.
1964 This is a worst case quadratic time way of identifying variables
1965 that must have equivalent points-to sets, including those caused by
1966 static cycles, and single entry subgraphs, in the constraint graph.
1968 The technique is described in "Exploiting Pointer and Location
1969 Equivalence to Optimize Pointer Analysis. In the 14th International
1970 Static Analysis Symposium (SAS), August 2007." It is known as the
1971 "HU" algorithm, and is equivalent to value numbering the collapsed
1972 constraint graph including evaluating unions.
1974 The general method of finding equivalence classes is as follows:
1975 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1976 Initialize all non-REF nodes to be direct nodes.
1977 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1978 variable}
1979 For each constraint containing the dereference, we also do the same
1980 thing.
1982 We then compute SCC's in the graph and unify nodes in the same SCC,
1983 including pts sets.
1985 For each non-collapsed node x:
1986 Visit all unvisited explicit incoming edges.
1987 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1988 where y->x.
1989 Lookup the equivalence class for pts(x).
1990 If we found one, equivalence_class(x) = found class.
1991 Otherwise, equivalence_class(x) = new class, and new_class is
1992 added to the lookup table.
1994 All direct nodes with the same equivalence class can be replaced
1995 with a single representative node.
1996 All unlabeled nodes (label == 0) are not pointers and all edges
1997 involving them can be eliminated.
1998 We perform these optimizations during rewrite_constraints
2000 In addition to pointer equivalence class finding, we also perform
2001 location equivalence class finding. This is the set of variables
2002 that always appear together in points-to sets. We use this to
2003 compress the size of the points-to sets. */
2005 /* Current maximum pointer equivalence class id. */
2006 static int pointer_equiv_class;
2008 /* Current maximum location equivalence class id. */
2009 static int location_equiv_class;
2011 /* Recursive routine to find strongly connected components in GRAPH,
2012 and label it's nodes with DFS numbers. */
2014 static void
2015 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2017 unsigned int i;
2018 bitmap_iterator bi;
2019 unsigned int my_dfs;
2021 gcc_assert (si->node_mapping[n] == n);
2022 SET_BIT (si->visited, n);
2023 si->dfs[n] = si->current_index ++;
2024 my_dfs = si->dfs[n];
2026 /* Visit all the successors. */
2027 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2029 unsigned int w = si->node_mapping[i];
2031 if (TEST_BIT (si->deleted, w))
2032 continue;
2034 if (!TEST_BIT (si->visited, w))
2035 condense_visit (graph, si, w);
2037 unsigned int t = si->node_mapping[w];
2038 unsigned int nnode = si->node_mapping[n];
2039 gcc_assert (nnode == n);
2041 if (si->dfs[t] < si->dfs[nnode])
2042 si->dfs[n] = si->dfs[t];
2046 /* Visit all the implicit predecessors. */
2047 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2049 unsigned int w = si->node_mapping[i];
2051 if (TEST_BIT (si->deleted, w))
2052 continue;
2054 if (!TEST_BIT (si->visited, w))
2055 condense_visit (graph, si, w);
2057 unsigned int t = si->node_mapping[w];
2058 unsigned int nnode = si->node_mapping[n];
2059 gcc_assert (nnode == n);
2061 if (si->dfs[t] < si->dfs[nnode])
2062 si->dfs[n] = si->dfs[t];
2066 /* See if any components have been identified. */
2067 if (si->dfs[n] == my_dfs)
2069 while (VEC_length (unsigned, si->scc_stack) != 0
2070 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
2072 unsigned int w = VEC_pop (unsigned, si->scc_stack);
2073 si->node_mapping[w] = n;
2075 if (!TEST_BIT (graph->direct_nodes, w))
2076 RESET_BIT (graph->direct_nodes, n);
2078 /* Unify our nodes. */
2079 if (graph->preds[w])
2081 if (!graph->preds[n])
2082 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2083 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2085 if (graph->implicit_preds[w])
2087 if (!graph->implicit_preds[n])
2088 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2089 bitmap_ior_into (graph->implicit_preds[n],
2090 graph->implicit_preds[w]);
2092 if (graph->points_to[w])
2094 if (!graph->points_to[n])
2095 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2096 bitmap_ior_into (graph->points_to[n],
2097 graph->points_to[w]);
2100 SET_BIT (si->deleted, n);
2102 else
2103 VEC_safe_push (unsigned, heap, si->scc_stack, n);
2106 /* Label pointer equivalences. */
2108 static void
2109 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2111 unsigned int i;
2112 bitmap_iterator bi;
2113 SET_BIT (si->visited, n);
2115 if (!graph->points_to[n])
2116 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2118 /* Label and union our incoming edges's points to sets. */
2119 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2121 unsigned int w = si->node_mapping[i];
2122 if (!TEST_BIT (si->visited, w))
2123 label_visit (graph, si, w);
2125 /* Skip unused edges */
2126 if (w == n || graph->pointer_label[w] == 0)
2127 continue;
2129 if (graph->points_to[w])
2130 bitmap_ior_into(graph->points_to[n], graph->points_to[w]);
2132 /* Indirect nodes get fresh variables. */
2133 if (!TEST_BIT (graph->direct_nodes, n))
2134 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2136 if (!bitmap_empty_p (graph->points_to[n]))
2138 unsigned int label = equiv_class_lookup (pointer_equiv_class_table,
2139 graph->points_to[n]);
2140 if (!label)
2142 label = pointer_equiv_class++;
2143 equiv_class_add (pointer_equiv_class_table,
2144 label, graph->points_to[n]);
2146 graph->pointer_label[n] = label;
2150 /* Perform offline variable substitution, discovering equivalence
2151 classes, and eliminating non-pointer variables. */
2153 static struct scc_info *
2154 perform_var_substitution (constraint_graph_t graph)
2156 unsigned int i;
2157 unsigned int size = graph->size;
2158 struct scc_info *si = init_scc_info (size);
2160 bitmap_obstack_initialize (&iteration_obstack);
2161 pointer_equiv_class_table = htab_create (511, equiv_class_label_hash,
2162 equiv_class_label_eq, free);
2163 location_equiv_class_table = htab_create (511, equiv_class_label_hash,
2164 equiv_class_label_eq, free);
2165 pointer_equiv_class = 1;
2166 location_equiv_class = 1;
2168 /* Condense the nodes, which means to find SCC's, count incoming
2169 predecessors, and unite nodes in SCC's. */
2170 for (i = 0; i < FIRST_REF_NODE; i++)
2171 if (!TEST_BIT (si->visited, si->node_mapping[i]))
2172 condense_visit (graph, si, si->node_mapping[i]);
2174 sbitmap_zero (si->visited);
2175 /* Actually the label the nodes for pointer equivalences */
2176 for (i = 0; i < FIRST_REF_NODE; i++)
2177 if (!TEST_BIT (si->visited, si->node_mapping[i]))
2178 label_visit (graph, si, si->node_mapping[i]);
2180 /* Calculate location equivalence labels. */
2181 for (i = 0; i < FIRST_REF_NODE; i++)
2183 bitmap pointed_by;
2184 bitmap_iterator bi;
2185 unsigned int j;
2186 unsigned int label;
2188 if (!graph->pointed_by[i])
2189 continue;
2190 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2192 /* Translate the pointed-by mapping for pointer equivalence
2193 labels. */
2194 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2196 bitmap_set_bit (pointed_by,
2197 graph->pointer_label[si->node_mapping[j]]);
2199 /* The original pointed_by is now dead. */
2200 BITMAP_FREE (graph->pointed_by[i]);
2202 /* Look up the location equivalence label if one exists, or make
2203 one otherwise. */
2204 label = equiv_class_lookup (location_equiv_class_table,
2205 pointed_by);
2206 if (label == 0)
2208 label = location_equiv_class++;
2209 equiv_class_add (location_equiv_class_table,
2210 label, pointed_by);
2212 else
2214 if (dump_file && (dump_flags & TDF_DETAILS))
2215 fprintf (dump_file, "Found location equivalence for node %s\n",
2216 get_varinfo (i)->name);
2217 BITMAP_FREE (pointed_by);
2219 graph->loc_label[i] = label;
2223 if (dump_file && (dump_flags & TDF_DETAILS))
2224 for (i = 0; i < FIRST_REF_NODE; i++)
2226 bool direct_node = TEST_BIT (graph->direct_nodes, i);
2227 fprintf (dump_file,
2228 "Equivalence classes for %s node id %d:%s are pointer: %d"
2229 ", location:%d\n",
2230 direct_node ? "Direct node" : "Indirect node", i,
2231 get_varinfo (i)->name,
2232 graph->pointer_label[si->node_mapping[i]],
2233 graph->loc_label[si->node_mapping[i]]);
2236 /* Quickly eliminate our non-pointer variables. */
2238 for (i = 0; i < FIRST_REF_NODE; i++)
2240 unsigned int node = si->node_mapping[i];
2242 if (graph->pointer_label[node] == 0)
2244 if (dump_file && (dump_flags & TDF_DETAILS))
2245 fprintf (dump_file,
2246 "%s is a non-pointer variable, eliminating edges.\n",
2247 get_varinfo (node)->name);
2248 stats.nonpointer_vars++;
2249 clear_edges_for_node (graph, node);
2253 return si;
2256 /* Free information that was only necessary for variable
2257 substitution. */
2259 static void
2260 free_var_substitution_info (struct scc_info *si)
2262 free_scc_info (si);
2263 free (graph->pointer_label);
2264 free (graph->loc_label);
2265 free (graph->pointed_by);
2266 free (graph->points_to);
2267 free (graph->eq_rep);
2268 sbitmap_free (graph->direct_nodes);
2269 htab_delete (pointer_equiv_class_table);
2270 htab_delete (location_equiv_class_table);
2271 bitmap_obstack_release (&iteration_obstack);
2274 /* Return an existing node that is equivalent to NODE, which has
2275 equivalence class LABEL, if one exists. Return NODE otherwise. */
2277 static unsigned int
2278 find_equivalent_node (constraint_graph_t graph,
2279 unsigned int node, unsigned int label)
2281 /* If the address version of this variable is unused, we can
2282 substitute it for anything else with the same label.
2283 Otherwise, we know the pointers are equivalent, but not the
2284 locations, and we can unite them later. */
2286 if (!bitmap_bit_p (graph->address_taken, node))
2288 gcc_assert (label < graph->size);
2290 if (graph->eq_rep[label] != -1)
2292 /* Unify the two variables since we know they are equivalent. */
2293 if (unite (graph->eq_rep[label], node))
2294 unify_nodes (graph, graph->eq_rep[label], node, false);
2295 return graph->eq_rep[label];
2297 else
2299 graph->eq_rep[label] = node;
2300 graph->pe_rep[label] = node;
2303 else
2305 gcc_assert (label < graph->size);
2306 graph->pe[node] = label;
2307 if (graph->pe_rep[label] == -1)
2308 graph->pe_rep[label] = node;
2311 return node;
2314 /* Unite pointer equivalent but not location equivalent nodes in
2315 GRAPH. This may only be performed once variable substitution is
2316 finished. */
2318 static void
2319 unite_pointer_equivalences (constraint_graph_t graph)
2321 unsigned int i;
2323 /* Go through the pointer equivalences and unite them to their
2324 representative, if they aren't already. */
2325 for (i = 0; i < FIRST_REF_NODE; i++)
2327 unsigned int label = graph->pe[i];
2328 if (label)
2330 int label_rep = graph->pe_rep[label];
2332 if (label_rep == -1)
2333 continue;
2335 label_rep = find (label_rep);
2336 if (label_rep >= 0 && unite (label_rep, find (i)))
2337 unify_nodes (graph, label_rep, i, false);
2342 /* Move complex constraints to the GRAPH nodes they belong to. */
2344 static void
2345 move_complex_constraints (constraint_graph_t graph)
2347 int i;
2348 constraint_t c;
2350 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
2352 if (c)
2354 struct constraint_expr lhs = c->lhs;
2355 struct constraint_expr rhs = c->rhs;
2357 if (lhs.type == DEREF)
2359 insert_into_complex (graph, lhs.var, c);
2361 else if (rhs.type == DEREF)
2363 if (!(get_varinfo (lhs.var)->is_special_var))
2364 insert_into_complex (graph, rhs.var, c);
2366 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2367 && (lhs.offset != 0 || rhs.offset != 0))
2369 insert_into_complex (graph, rhs.var, c);
2376 /* Optimize and rewrite complex constraints while performing
2377 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2378 result of perform_variable_substitution. */
2380 static void
2381 rewrite_constraints (constraint_graph_t graph,
2382 struct scc_info *si)
2384 int i;
2385 unsigned int j;
2386 constraint_t c;
2388 for (j = 0; j < graph->size; j++)
2389 gcc_assert (find (j) == j);
2391 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
2393 struct constraint_expr lhs = c->lhs;
2394 struct constraint_expr rhs = c->rhs;
2395 unsigned int lhsvar = find (lhs.var);
2396 unsigned int rhsvar = find (rhs.var);
2397 unsigned int lhsnode, rhsnode;
2398 unsigned int lhslabel, rhslabel;
2400 lhsnode = si->node_mapping[lhsvar];
2401 rhsnode = si->node_mapping[rhsvar];
2402 lhslabel = graph->pointer_label[lhsnode];
2403 rhslabel = graph->pointer_label[rhsnode];
2405 /* See if it is really a non-pointer variable, and if so, ignore
2406 the constraint. */
2407 if (lhslabel == 0)
2409 if (dump_file && (dump_flags & TDF_DETAILS))
2412 fprintf (dump_file, "%s is a non-pointer variable,"
2413 "ignoring constraint:",
2414 get_varinfo (lhs.var)->name);
2415 dump_constraint (dump_file, c);
2416 fprintf (dump_file, "\n");
2418 VEC_replace (constraint_t, constraints, i, NULL);
2419 continue;
2422 if (rhslabel == 0)
2424 if (dump_file && (dump_flags & TDF_DETAILS))
2427 fprintf (dump_file, "%s is a non-pointer variable,"
2428 "ignoring constraint:",
2429 get_varinfo (rhs.var)->name);
2430 dump_constraint (dump_file, c);
2431 fprintf (dump_file, "\n");
2433 VEC_replace (constraint_t, constraints, i, NULL);
2434 continue;
2437 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2438 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2439 c->lhs.var = lhsvar;
2440 c->rhs.var = rhsvar;
2445 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2446 part of an SCC, false otherwise. */
2448 static bool
2449 eliminate_indirect_cycles (unsigned int node)
2451 if (graph->indirect_cycles[node] != -1
2452 && !bitmap_empty_p (get_varinfo (node)->solution))
2454 unsigned int i;
2455 VEC(unsigned,heap) *queue = NULL;
2456 int queuepos;
2457 unsigned int to = find (graph->indirect_cycles[node]);
2458 bitmap_iterator bi;
2460 /* We can't touch the solution set and call unify_nodes
2461 at the same time, because unify_nodes is going to do
2462 bitmap unions into it. */
2464 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2466 if (find (i) == i && i != to)
2468 if (unite (to, i))
2469 VEC_safe_push (unsigned, heap, queue, i);
2473 for (queuepos = 0;
2474 VEC_iterate (unsigned, queue, queuepos, i);
2475 queuepos++)
2477 unify_nodes (graph, to, i, true);
2479 VEC_free (unsigned, heap, queue);
2480 return true;
2482 return false;
2485 /* Solve the constraint graph GRAPH using our worklist solver.
2486 This is based on the PW* family of solvers from the "Efficient Field
2487 Sensitive Pointer Analysis for C" paper.
2488 It works by iterating over all the graph nodes, processing the complex
2489 constraints and propagating the copy constraints, until everything stops
2490 changed. This corresponds to steps 6-8 in the solving list given above. */
2492 static void
2493 solve_graph (constraint_graph_t graph)
2495 unsigned int size = graph->size;
2496 unsigned int i;
2497 bitmap pts;
2499 changed = BITMAP_ALLOC (NULL);
2501 /* Mark all initial non-collapsed nodes as changed. */
2502 for (i = 0; i < size; i++)
2504 varinfo_t ivi = get_varinfo (i);
2505 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2506 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2507 || VEC_length (constraint_t, graph->complex[i]) > 0))
2508 bitmap_set_bit (changed, i);
2511 /* Allocate a bitmap to be used to store the changed bits. */
2512 pts = BITMAP_ALLOC (&pta_obstack);
2514 while (!bitmap_empty_p (changed))
2516 unsigned int i;
2517 struct topo_info *ti = init_topo_info ();
2518 stats.iterations++;
2520 bitmap_obstack_initialize (&iteration_obstack);
2522 compute_topo_order (graph, ti);
2524 while (VEC_length (unsigned, ti->topo_order) != 0)
2527 i = VEC_pop (unsigned, ti->topo_order);
2529 /* If this variable is not a representative, skip it. */
2530 if (find (i) != i)
2531 continue;
2533 /* In certain indirect cycle cases, we may merge this
2534 variable to another. */
2535 if (eliminate_indirect_cycles (i) && find (i) != i)
2536 continue;
2538 /* If the node has changed, we need to process the
2539 complex constraints and outgoing edges again. */
2540 if (bitmap_clear_bit (changed, i))
2542 unsigned int j;
2543 constraint_t c;
2544 bitmap solution;
2545 VEC(constraint_t,heap) *complex = graph->complex[i];
2546 varinfo_t vi = get_varinfo (i);
2547 bool solution_empty;
2549 /* Compute the changed set of solution bits. */
2550 if (vi->oldsolution)
2551 bitmap_and_compl (pts, vi->solution, vi->oldsolution);
2552 else
2553 bitmap_copy (pts, vi->solution);
2555 if (bitmap_empty_p (pts))
2556 continue;
2558 if (vi->oldsolution)
2559 bitmap_ior_into (vi->oldsolution, pts);
2560 else
2562 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
2563 bitmap_copy (vi->oldsolution, pts);
2566 solution = vi->solution;
2567 solution_empty = bitmap_empty_p (solution);
2569 /* Process the complex constraints */
2570 FOR_EACH_VEC_ELT (constraint_t, complex, j, c)
2572 /* XXX: This is going to unsort the constraints in
2573 some cases, which will occasionally add duplicate
2574 constraints during unification. This does not
2575 affect correctness. */
2576 c->lhs.var = find (c->lhs.var);
2577 c->rhs.var = find (c->rhs.var);
2579 /* The only complex constraint that can change our
2580 solution to non-empty, given an empty solution,
2581 is a constraint where the lhs side is receiving
2582 some set from elsewhere. */
2583 if (!solution_empty || c->lhs.type != DEREF)
2584 do_complex_constraint (graph, c, pts);
2587 solution_empty = bitmap_empty_p (solution);
2589 if (!solution_empty)
2591 bitmap_iterator bi;
2592 unsigned eff_escaped_id = find (escaped_id);
2594 /* Propagate solution to all successors. */
2595 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2596 0, j, bi)
2598 bitmap tmp;
2599 bool flag;
2601 unsigned int to = find (j);
2602 tmp = get_varinfo (to)->solution;
2603 flag = false;
2605 /* Don't try to propagate to ourselves. */
2606 if (to == i)
2607 continue;
2609 /* If we propagate from ESCAPED use ESCAPED as
2610 placeholder. */
2611 if (i == eff_escaped_id)
2612 flag = bitmap_set_bit (tmp, escaped_id);
2613 else
2614 flag = set_union_with_increment (tmp, pts, 0);
2616 if (flag)
2618 get_varinfo (to)->solution = tmp;
2619 bitmap_set_bit (changed, to);
2625 free_topo_info (ti);
2626 bitmap_obstack_release (&iteration_obstack);
2629 BITMAP_FREE (pts);
2630 BITMAP_FREE (changed);
2631 bitmap_obstack_release (&oldpta_obstack);
2634 /* Map from trees to variable infos. */
2635 static struct pointer_map_t *vi_for_tree;
2638 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2640 static void
2641 insert_vi_for_tree (tree t, varinfo_t vi)
2643 void **slot = pointer_map_insert (vi_for_tree, t);
2644 gcc_assert (vi);
2645 gcc_assert (*slot == NULL);
2646 *slot = vi;
2649 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2650 exist in the map, return NULL, otherwise, return the varinfo we found. */
2652 static varinfo_t
2653 lookup_vi_for_tree (tree t)
2655 void **slot = pointer_map_contains (vi_for_tree, t);
2656 if (slot == NULL)
2657 return NULL;
2659 return (varinfo_t) *slot;
2662 /* Return a printable name for DECL */
2664 static const char *
2665 alias_get_name (tree decl)
2667 const char *res;
2668 char *temp;
2669 int num_printed = 0;
2671 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2672 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2673 else
2674 res= get_name (decl);
2675 if (res != NULL)
2676 return res;
2678 res = "NULL";
2679 if (!dump_file)
2680 return res;
2682 if (TREE_CODE (decl) == SSA_NAME)
2684 num_printed = asprintf (&temp, "%s_%u",
2685 alias_get_name (SSA_NAME_VAR (decl)),
2686 SSA_NAME_VERSION (decl));
2688 else if (DECL_P (decl))
2690 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2692 if (num_printed > 0)
2694 res = ggc_strdup (temp);
2695 free (temp);
2697 return res;
2700 /* Find the variable id for tree T in the map.
2701 If T doesn't exist in the map, create an entry for it and return it. */
2703 static varinfo_t
2704 get_vi_for_tree (tree t)
2706 void **slot = pointer_map_contains (vi_for_tree, t);
2707 if (slot == NULL)
2708 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2710 return (varinfo_t) *slot;
2713 /* Get a scalar constraint expression for a new temporary variable. */
2715 static struct constraint_expr
2716 new_scalar_tmp_constraint_exp (const char *name)
2718 struct constraint_expr tmp;
2719 varinfo_t vi;
2721 vi = new_var_info (NULL_TREE, name);
2722 vi->offset = 0;
2723 vi->size = -1;
2724 vi->fullsize = -1;
2725 vi->is_full_var = 1;
2727 tmp.var = vi->id;
2728 tmp.type = SCALAR;
2729 tmp.offset = 0;
2731 return tmp;
2734 /* Get a constraint expression vector from an SSA_VAR_P node.
2735 If address_p is true, the result will be taken its address of. */
2737 static void
2738 get_constraint_for_ssa_var (tree t, VEC(ce_s, heap) **results, bool address_p)
2740 struct constraint_expr cexpr;
2741 varinfo_t vi;
2743 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2744 gcc_assert (SSA_VAR_P (t) || DECL_P (t));
2746 /* For parameters, get at the points-to set for the actual parm
2747 decl. */
2748 if (TREE_CODE (t) == SSA_NAME
2749 && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2750 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL)
2751 && SSA_NAME_IS_DEFAULT_DEF (t))
2753 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2754 return;
2757 /* For global variables resort to the alias target. */
2758 if (TREE_CODE (t) == VAR_DECL
2759 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
2761 struct varpool_node *node = varpool_get_node (t);
2762 if (node && node->alias)
2764 node = varpool_variable_node (node, NULL);
2765 t = node->decl;
2769 vi = get_vi_for_tree (t);
2770 cexpr.var = vi->id;
2771 cexpr.type = SCALAR;
2772 cexpr.offset = 0;
2773 /* If we determine the result is "anything", and we know this is readonly,
2774 say it points to readonly memory instead. */
2775 if (cexpr.var == anything_id && TREE_READONLY (t))
2777 gcc_unreachable ();
2778 cexpr.type = ADDRESSOF;
2779 cexpr.var = readonly_id;
2782 /* If we are not taking the address of the constraint expr, add all
2783 sub-fiels of the variable as well. */
2784 if (!address_p
2785 && !vi->is_full_var)
2787 for (; vi; vi = vi->next)
2789 cexpr.var = vi->id;
2790 VEC_safe_push (ce_s, heap, *results, &cexpr);
2792 return;
2795 VEC_safe_push (ce_s, heap, *results, &cexpr);
2798 /* Process constraint T, performing various simplifications and then
2799 adding it to our list of overall constraints. */
2801 static void
2802 process_constraint (constraint_t t)
2804 struct constraint_expr rhs = t->rhs;
2805 struct constraint_expr lhs = t->lhs;
2807 gcc_assert (rhs.var < VEC_length (varinfo_t, varmap));
2808 gcc_assert (lhs.var < VEC_length (varinfo_t, varmap));
2810 /* If we didn't get any useful constraint from the lhs we get
2811 &ANYTHING as fallback from get_constraint_for. Deal with
2812 it here by turning it into *ANYTHING. */
2813 if (lhs.type == ADDRESSOF
2814 && lhs.var == anything_id)
2815 lhs.type = DEREF;
2817 /* ADDRESSOF on the lhs is invalid. */
2818 gcc_assert (lhs.type != ADDRESSOF);
2820 /* We shouldn't add constraints from things that cannot have pointers.
2821 It's not completely trivial to avoid in the callers, so do it here. */
2822 if (rhs.type != ADDRESSOF
2823 && !get_varinfo (rhs.var)->may_have_pointers)
2824 return;
2826 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2827 if (!get_varinfo (lhs.var)->may_have_pointers)
2828 return;
2830 /* This can happen in our IR with things like n->a = *p */
2831 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
2833 /* Split into tmp = *rhs, *lhs = tmp */
2834 struct constraint_expr tmplhs;
2835 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp");
2836 process_constraint (new_constraint (tmplhs, rhs));
2837 process_constraint (new_constraint (lhs, tmplhs));
2839 else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
2841 /* Split into tmp = &rhs, *lhs = tmp */
2842 struct constraint_expr tmplhs;
2843 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp");
2844 process_constraint (new_constraint (tmplhs, rhs));
2845 process_constraint (new_constraint (lhs, tmplhs));
2847 else
2849 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
2850 VEC_safe_push (constraint_t, heap, constraints, t);
2855 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2856 structure. */
2858 static HOST_WIDE_INT
2859 bitpos_of_field (const tree fdecl)
2861 if (!host_integerp (DECL_FIELD_OFFSET (fdecl), 0)
2862 || !host_integerp (DECL_FIELD_BIT_OFFSET (fdecl), 0))
2863 return -1;
2865 return (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
2866 + TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (fdecl)));
2870 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
2871 resulting constraint expressions in *RESULTS. */
2873 static void
2874 get_constraint_for_ptr_offset (tree ptr, tree offset,
2875 VEC (ce_s, heap) **results)
2877 struct constraint_expr c;
2878 unsigned int j, n;
2879 HOST_WIDE_INT rhsunitoffset, rhsoffset;
2881 /* If we do not do field-sensitive PTA adding offsets to pointers
2882 does not change the points-to solution. */
2883 if (!use_field_sensitive)
2885 get_constraint_for_rhs (ptr, results);
2886 return;
2889 /* If the offset is not a non-negative integer constant that fits
2890 in a HOST_WIDE_INT, we have to fall back to a conservative
2891 solution which includes all sub-fields of all pointed-to
2892 variables of ptr. */
2893 if (offset == NULL_TREE
2894 || !host_integerp (offset, 0))
2895 rhsoffset = UNKNOWN_OFFSET;
2896 else
2898 /* Make sure the bit-offset also fits. */
2899 rhsunitoffset = TREE_INT_CST_LOW (offset);
2900 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
2901 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
2902 rhsoffset = UNKNOWN_OFFSET;
2905 get_constraint_for_rhs (ptr, results);
2906 if (rhsoffset == 0)
2907 return;
2909 /* As we are eventually appending to the solution do not use
2910 VEC_iterate here. */
2911 n = VEC_length (ce_s, *results);
2912 for (j = 0; j < n; j++)
2914 varinfo_t curr;
2915 c = *VEC_index (ce_s, *results, j);
2916 curr = get_varinfo (c.var);
2918 if (c.type == ADDRESSOF
2919 /* If this varinfo represents a full variable just use it. */
2920 && curr->is_full_var)
2921 c.offset = 0;
2922 else if (c.type == ADDRESSOF
2923 /* If we do not know the offset add all subfields. */
2924 && rhsoffset == UNKNOWN_OFFSET)
2926 varinfo_t temp = lookup_vi_for_tree (curr->decl);
2929 struct constraint_expr c2;
2930 c2.var = temp->id;
2931 c2.type = ADDRESSOF;
2932 c2.offset = 0;
2933 if (c2.var != c.var)
2934 VEC_safe_push (ce_s, heap, *results, &c2);
2935 temp = temp->next;
2937 while (temp);
2939 else if (c.type == ADDRESSOF)
2941 varinfo_t temp;
2942 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
2944 /* Search the sub-field which overlaps with the
2945 pointed-to offset. If the result is outside of the variable
2946 we have to provide a conservative result, as the variable is
2947 still reachable from the resulting pointer (even though it
2948 technically cannot point to anything). The last and first
2949 sub-fields are such conservative results.
2950 ??? If we always had a sub-field for &object + 1 then
2951 we could represent this in a more precise way. */
2952 if (rhsoffset < 0
2953 && curr->offset < offset)
2954 offset = 0;
2955 temp = first_or_preceding_vi_for_offset (curr, offset);
2957 /* If the found variable is not exactly at the pointed to
2958 result, we have to include the next variable in the
2959 solution as well. Otherwise two increments by offset / 2
2960 do not result in the same or a conservative superset
2961 solution. */
2962 if (temp->offset != offset
2963 && temp->next != NULL)
2965 struct constraint_expr c2;
2966 c2.var = temp->next->id;
2967 c2.type = ADDRESSOF;
2968 c2.offset = 0;
2969 VEC_safe_push (ce_s, heap, *results, &c2);
2971 c.var = temp->id;
2972 c.offset = 0;
2974 else
2975 c.offset = rhsoffset;
2977 VEC_replace (ce_s, *results, j, &c);
2982 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
2983 If address_p is true the result will be taken its address of.
2984 If lhs_p is true then the constraint expression is assumed to be used
2985 as the lhs. */
2987 static void
2988 get_constraint_for_component_ref (tree t, VEC(ce_s, heap) **results,
2989 bool address_p, bool lhs_p)
2991 tree orig_t = t;
2992 HOST_WIDE_INT bitsize = -1;
2993 HOST_WIDE_INT bitmaxsize = -1;
2994 HOST_WIDE_INT bitpos;
2995 tree forzero;
2996 struct constraint_expr *result;
2998 /* Some people like to do cute things like take the address of
2999 &0->a.b */
3000 forzero = t;
3001 while (handled_component_p (forzero)
3002 || INDIRECT_REF_P (forzero)
3003 || TREE_CODE (forzero) == MEM_REF)
3004 forzero = TREE_OPERAND (forzero, 0);
3006 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
3008 struct constraint_expr temp;
3010 temp.offset = 0;
3011 temp.var = integer_id;
3012 temp.type = SCALAR;
3013 VEC_safe_push (ce_s, heap, *results, &temp);
3014 return;
3017 /* Handle type-punning through unions. If we are extracting a pointer
3018 from a union via a possibly type-punning access that pointer
3019 points to anything, similar to a conversion of an integer to
3020 a pointer. */
3021 if (!lhs_p)
3023 tree u;
3024 for (u = t;
3025 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3026 u = TREE_OPERAND (u, 0))
3027 if (TREE_CODE (u) == COMPONENT_REF
3028 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3030 struct constraint_expr temp;
3032 temp.offset = 0;
3033 temp.var = anything_id;
3034 temp.type = ADDRESSOF;
3035 VEC_safe_push (ce_s, heap, *results, &temp);
3036 return;
3040 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
3042 /* Pretend to take the address of the base, we'll take care of
3043 adding the required subset of sub-fields below. */
3044 get_constraint_for_1 (t, results, true, lhs_p);
3045 gcc_assert (VEC_length (ce_s, *results) == 1);
3046 result = VEC_last (ce_s, *results);
3048 if (result->type == SCALAR
3049 && get_varinfo (result->var)->is_full_var)
3050 /* For single-field vars do not bother about the offset. */
3051 result->offset = 0;
3052 else if (result->type == SCALAR)
3054 /* In languages like C, you can access one past the end of an
3055 array. You aren't allowed to dereference it, so we can
3056 ignore this constraint. When we handle pointer subtraction,
3057 we may have to do something cute here. */
3059 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result->var)->fullsize
3060 && bitmaxsize != 0)
3062 /* It's also not true that the constraint will actually start at the
3063 right offset, it may start in some padding. We only care about
3064 setting the constraint to the first actual field it touches, so
3065 walk to find it. */
3066 struct constraint_expr cexpr = *result;
3067 varinfo_t curr;
3068 VEC_pop (ce_s, *results);
3069 cexpr.offset = 0;
3070 for (curr = get_varinfo (cexpr.var); curr; curr = curr->next)
3072 if (ranges_overlap_p (curr->offset, curr->size,
3073 bitpos, bitmaxsize))
3075 cexpr.var = curr->id;
3076 VEC_safe_push (ce_s, heap, *results, &cexpr);
3077 if (address_p)
3078 break;
3081 /* If we are going to take the address of this field then
3082 to be able to compute reachability correctly add at least
3083 the last field of the variable. */
3084 if (address_p
3085 && VEC_length (ce_s, *results) == 0)
3087 curr = get_varinfo (cexpr.var);
3088 while (curr->next != NULL)
3089 curr = curr->next;
3090 cexpr.var = curr->id;
3091 VEC_safe_push (ce_s, heap, *results, &cexpr);
3093 else if (VEC_length (ce_s, *results) == 0)
3094 /* Assert that we found *some* field there. The user couldn't be
3095 accessing *only* padding. */
3096 /* Still the user could access one past the end of an array
3097 embedded in a struct resulting in accessing *only* padding. */
3098 /* Or accessing only padding via type-punning to a type
3099 that has a filed just in padding space. */
3101 cexpr.type = SCALAR;
3102 cexpr.var = anything_id;
3103 cexpr.offset = 0;
3104 VEC_safe_push (ce_s, heap, *results, &cexpr);
3107 else if (bitmaxsize == 0)
3109 if (dump_file && (dump_flags & TDF_DETAILS))
3110 fprintf (dump_file, "Access to zero-sized part of variable,"
3111 "ignoring\n");
3113 else
3114 if (dump_file && (dump_flags & TDF_DETAILS))
3115 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3117 else if (result->type == DEREF)
3119 /* If we do not know exactly where the access goes say so. Note
3120 that only for non-structure accesses we know that we access
3121 at most one subfiled of any variable. */
3122 if (bitpos == -1
3123 || bitsize != bitmaxsize
3124 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
3125 || result->offset == UNKNOWN_OFFSET)
3126 result->offset = UNKNOWN_OFFSET;
3127 else
3128 result->offset += bitpos;
3130 else if (result->type == ADDRESSOF)
3132 /* We can end up here for component references on a
3133 VIEW_CONVERT_EXPR <>(&foobar). */
3134 result->type = SCALAR;
3135 result->var = anything_id;
3136 result->offset = 0;
3138 else
3139 gcc_unreachable ();
3143 /* Dereference the constraint expression CONS, and return the result.
3144 DEREF (ADDRESSOF) = SCALAR
3145 DEREF (SCALAR) = DEREF
3146 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3147 This is needed so that we can handle dereferencing DEREF constraints. */
3149 static void
3150 do_deref (VEC (ce_s, heap) **constraints)
3152 struct constraint_expr *c;
3153 unsigned int i = 0;
3155 FOR_EACH_VEC_ELT (ce_s, *constraints, i, c)
3157 if (c->type == SCALAR)
3158 c->type = DEREF;
3159 else if (c->type == ADDRESSOF)
3160 c->type = SCALAR;
3161 else if (c->type == DEREF)
3163 struct constraint_expr tmplhs;
3164 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp");
3165 process_constraint (new_constraint (tmplhs, *c));
3166 c->var = tmplhs.var;
3168 else
3169 gcc_unreachable ();
3173 /* Given a tree T, return the constraint expression for taking the
3174 address of it. */
3176 static void
3177 get_constraint_for_address_of (tree t, VEC (ce_s, heap) **results)
3179 struct constraint_expr *c;
3180 unsigned int i;
3182 get_constraint_for_1 (t, results, true, true);
3184 FOR_EACH_VEC_ELT (ce_s, *results, i, c)
3186 if (c->type == DEREF)
3187 c->type = SCALAR;
3188 else
3189 c->type = ADDRESSOF;
3193 /* Given a tree T, return the constraint expression for it. */
3195 static void
3196 get_constraint_for_1 (tree t, VEC (ce_s, heap) **results, bool address_p,
3197 bool lhs_p)
3199 struct constraint_expr temp;
3201 /* x = integer is all glommed to a single variable, which doesn't
3202 point to anything by itself. That is, of course, unless it is an
3203 integer constant being treated as a pointer, in which case, we
3204 will return that this is really the addressof anything. This
3205 happens below, since it will fall into the default case. The only
3206 case we know something about an integer treated like a pointer is
3207 when it is the NULL pointer, and then we just say it points to
3208 NULL.
3210 Do not do that if -fno-delete-null-pointer-checks though, because
3211 in that case *NULL does not fail, so it _should_ alias *anything.
3212 It is not worth adding a new option or renaming the existing one,
3213 since this case is relatively obscure. */
3214 if ((TREE_CODE (t) == INTEGER_CST
3215 && integer_zerop (t))
3216 /* The only valid CONSTRUCTORs in gimple with pointer typed
3217 elements are zero-initializer. But in IPA mode we also
3218 process global initializers, so verify at least. */
3219 || (TREE_CODE (t) == CONSTRUCTOR
3220 && CONSTRUCTOR_NELTS (t) == 0))
3222 if (flag_delete_null_pointer_checks)
3223 temp.var = nothing_id;
3224 else
3225 temp.var = nonlocal_id;
3226 temp.type = ADDRESSOF;
3227 temp.offset = 0;
3228 VEC_safe_push (ce_s, heap, *results, &temp);
3229 return;
3232 /* String constants are read-only. */
3233 if (TREE_CODE (t) == STRING_CST)
3235 temp.var = readonly_id;
3236 temp.type = SCALAR;
3237 temp.offset = 0;
3238 VEC_safe_push (ce_s, heap, *results, &temp);
3239 return;
3242 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3244 case tcc_expression:
3246 switch (TREE_CODE (t))
3248 case ADDR_EXPR:
3249 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3250 return;
3251 default:;
3253 break;
3255 case tcc_reference:
3257 switch (TREE_CODE (t))
3259 case MEM_REF:
3261 struct constraint_expr cs;
3262 varinfo_t vi, curr;
3263 tree off = double_int_to_tree (sizetype, mem_ref_offset (t));
3264 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0), off, results);
3265 do_deref (results);
3267 /* If we are not taking the address then make sure to process
3268 all subvariables we might access. */
3269 if (address_p)
3270 return;
3272 cs = *VEC_last (ce_s, *results);
3273 if (cs.type == DEREF)
3275 /* For dereferences this means we have to defer it
3276 to solving time. */
3277 VEC_last (ce_s, *results)->offset = UNKNOWN_OFFSET;
3278 return;
3280 if (cs.type != SCALAR)
3281 return;
3283 vi = get_varinfo (cs.var);
3284 curr = vi->next;
3285 if (!vi->is_full_var
3286 && curr)
3288 unsigned HOST_WIDE_INT size;
3289 if (host_integerp (TYPE_SIZE (TREE_TYPE (t)), 1))
3290 size = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (t)));
3291 else
3292 size = -1;
3293 for (; curr; curr = curr->next)
3295 if (curr->offset - vi->offset < size)
3297 cs.var = curr->id;
3298 VEC_safe_push (ce_s, heap, *results, &cs);
3300 else
3301 break;
3304 return;
3306 case ARRAY_REF:
3307 case ARRAY_RANGE_REF:
3308 case COMPONENT_REF:
3309 get_constraint_for_component_ref (t, results, address_p, lhs_p);
3310 return;
3311 case VIEW_CONVERT_EXPR:
3312 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p,
3313 lhs_p);
3314 return;
3315 /* We are missing handling for TARGET_MEM_REF here. */
3316 default:;
3318 break;
3320 case tcc_exceptional:
3322 switch (TREE_CODE (t))
3324 case SSA_NAME:
3326 get_constraint_for_ssa_var (t, results, address_p);
3327 return;
3329 case CONSTRUCTOR:
3331 unsigned int i;
3332 tree val;
3333 VEC (ce_s, heap) *tmp = NULL;
3334 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
3336 struct constraint_expr *rhsp;
3337 unsigned j;
3338 get_constraint_for_1 (val, &tmp, address_p, lhs_p);
3339 FOR_EACH_VEC_ELT (ce_s, tmp, j, rhsp)
3340 VEC_safe_push (ce_s, heap, *results, rhsp);
3341 VEC_truncate (ce_s, tmp, 0);
3343 VEC_free (ce_s, heap, tmp);
3344 /* We do not know whether the constructor was complete,
3345 so technically we have to add &NOTHING or &ANYTHING
3346 like we do for an empty constructor as well. */
3347 return;
3349 default:;
3351 break;
3353 case tcc_declaration:
3355 get_constraint_for_ssa_var (t, results, address_p);
3356 return;
3358 case tcc_constant:
3360 /* We cannot refer to automatic variables through constants. */
3361 temp.type = ADDRESSOF;
3362 temp.var = nonlocal_id;
3363 temp.offset = 0;
3364 VEC_safe_push (ce_s, heap, *results, &temp);
3365 return;
3367 default:;
3370 /* The default fallback is a constraint from anything. */
3371 temp.type = ADDRESSOF;
3372 temp.var = anything_id;
3373 temp.offset = 0;
3374 VEC_safe_push (ce_s, heap, *results, &temp);
3377 /* Given a gimple tree T, return the constraint expression vector for it. */
3379 static void
3380 get_constraint_for (tree t, VEC (ce_s, heap) **results)
3382 gcc_assert (VEC_length (ce_s, *results) == 0);
3384 get_constraint_for_1 (t, results, false, true);
3387 /* Given a gimple tree T, return the constraint expression vector for it
3388 to be used as the rhs of a constraint. */
3390 static void
3391 get_constraint_for_rhs (tree t, VEC (ce_s, heap) **results)
3393 gcc_assert (VEC_length (ce_s, *results) == 0);
3395 get_constraint_for_1 (t, results, false, false);
3399 /* Efficiently generates constraints from all entries in *RHSC to all
3400 entries in *LHSC. */
3402 static void
3403 process_all_all_constraints (VEC (ce_s, heap) *lhsc, VEC (ce_s, heap) *rhsc)
3405 struct constraint_expr *lhsp, *rhsp;
3406 unsigned i, j;
3408 if (VEC_length (ce_s, lhsc) <= 1
3409 || VEC_length (ce_s, rhsc) <= 1)
3411 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
3412 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
3413 process_constraint (new_constraint (*lhsp, *rhsp));
3415 else
3417 struct constraint_expr tmp;
3418 tmp = new_scalar_tmp_constraint_exp ("allalltmp");
3419 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
3420 process_constraint (new_constraint (tmp, *rhsp));
3421 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
3422 process_constraint (new_constraint (*lhsp, tmp));
3426 /* Handle aggregate copies by expanding into copies of the respective
3427 fields of the structures. */
3429 static void
3430 do_structure_copy (tree lhsop, tree rhsop)
3432 struct constraint_expr *lhsp, *rhsp;
3433 VEC (ce_s, heap) *lhsc = NULL, *rhsc = NULL;
3434 unsigned j;
3436 get_constraint_for (lhsop, &lhsc);
3437 get_constraint_for_rhs (rhsop, &rhsc);
3438 lhsp = VEC_index (ce_s, lhsc, 0);
3439 rhsp = VEC_index (ce_s, rhsc, 0);
3440 if (lhsp->type == DEREF
3441 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3442 || rhsp->type == DEREF)
3444 if (lhsp->type == DEREF)
3446 gcc_assert (VEC_length (ce_s, lhsc) == 1);
3447 lhsp->offset = UNKNOWN_OFFSET;
3449 if (rhsp->type == DEREF)
3451 gcc_assert (VEC_length (ce_s, rhsc) == 1);
3452 rhsp->offset = UNKNOWN_OFFSET;
3454 process_all_all_constraints (lhsc, rhsc);
3456 else if (lhsp->type == SCALAR
3457 && (rhsp->type == SCALAR
3458 || rhsp->type == ADDRESSOF))
3460 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3461 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
3462 unsigned k = 0;
3463 get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize);
3464 get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize);
3465 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp);)
3467 varinfo_t lhsv, rhsv;
3468 rhsp = VEC_index (ce_s, rhsc, k);
3469 lhsv = get_varinfo (lhsp->var);
3470 rhsv = get_varinfo (rhsp->var);
3471 if (lhsv->may_have_pointers
3472 && (lhsv->is_full_var
3473 || rhsv->is_full_var
3474 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3475 rhsv->offset + lhsoffset, rhsv->size)))
3476 process_constraint (new_constraint (*lhsp, *rhsp));
3477 if (!rhsv->is_full_var
3478 && (lhsv->is_full_var
3479 || (lhsv->offset + rhsoffset + lhsv->size
3480 > rhsv->offset + lhsoffset + rhsv->size)))
3482 ++k;
3483 if (k >= VEC_length (ce_s, rhsc))
3484 break;
3486 else
3487 ++j;
3490 else
3491 gcc_unreachable ();
3493 VEC_free (ce_s, heap, lhsc);
3494 VEC_free (ce_s, heap, rhsc);
3497 /* Create constraints ID = { rhsc }. */
3499 static void
3500 make_constraints_to (unsigned id, VEC(ce_s, heap) *rhsc)
3502 struct constraint_expr *c;
3503 struct constraint_expr includes;
3504 unsigned int j;
3506 includes.var = id;
3507 includes.offset = 0;
3508 includes.type = SCALAR;
3510 FOR_EACH_VEC_ELT (ce_s, rhsc, j, c)
3511 process_constraint (new_constraint (includes, *c));
3514 /* Create a constraint ID = OP. */
3516 static void
3517 make_constraint_to (unsigned id, tree op)
3519 VEC(ce_s, heap) *rhsc = NULL;
3520 get_constraint_for_rhs (op, &rhsc);
3521 make_constraints_to (id, rhsc);
3522 VEC_free (ce_s, heap, rhsc);
3525 /* Create a constraint ID = &FROM. */
3527 static void
3528 make_constraint_from (varinfo_t vi, int from)
3530 struct constraint_expr lhs, rhs;
3532 lhs.var = vi->id;
3533 lhs.offset = 0;
3534 lhs.type = SCALAR;
3536 rhs.var = from;
3537 rhs.offset = 0;
3538 rhs.type = ADDRESSOF;
3539 process_constraint (new_constraint (lhs, rhs));
3542 /* Create a constraint ID = FROM. */
3544 static void
3545 make_copy_constraint (varinfo_t vi, int from)
3547 struct constraint_expr lhs, rhs;
3549 lhs.var = vi->id;
3550 lhs.offset = 0;
3551 lhs.type = SCALAR;
3553 rhs.var = from;
3554 rhs.offset = 0;
3555 rhs.type = SCALAR;
3556 process_constraint (new_constraint (lhs, rhs));
3559 /* Make constraints necessary to make OP escape. */
3561 static void
3562 make_escape_constraint (tree op)
3564 make_constraint_to (escaped_id, op);
3567 /* Add constraints to that the solution of VI is transitively closed. */
3569 static void
3570 make_transitive_closure_constraints (varinfo_t vi)
3572 struct constraint_expr lhs, rhs;
3574 /* VAR = *VAR; */
3575 lhs.type = SCALAR;
3576 lhs.var = vi->id;
3577 lhs.offset = 0;
3578 rhs.type = DEREF;
3579 rhs.var = vi->id;
3580 rhs.offset = 0;
3581 process_constraint (new_constraint (lhs, rhs));
3583 /* VAR = VAR + UNKNOWN; */
3584 lhs.type = SCALAR;
3585 lhs.var = vi->id;
3586 lhs.offset = 0;
3587 rhs.type = SCALAR;
3588 rhs.var = vi->id;
3589 rhs.offset = UNKNOWN_OFFSET;
3590 process_constraint (new_constraint (lhs, rhs));
3593 /* Temporary storage for fake var decls. */
3594 struct obstack fake_var_decl_obstack;
3596 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3598 static tree
3599 build_fake_var_decl (tree type)
3601 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl);
3602 memset (decl, 0, sizeof (struct tree_var_decl));
3603 TREE_SET_CODE (decl, VAR_DECL);
3604 TREE_TYPE (decl) = type;
3605 DECL_UID (decl) = allocate_decl_uid ();
3606 SET_DECL_PT_UID (decl, -1);
3607 layout_decl (decl, 0);
3608 return decl;
3611 /* Create a new artificial heap variable with NAME.
3612 Return the created variable. */
3614 static varinfo_t
3615 make_heapvar (const char *name)
3617 varinfo_t vi;
3618 tree heapvar;
3620 heapvar = build_fake_var_decl (ptr_type_node);
3621 DECL_EXTERNAL (heapvar) = 1;
3623 vi = new_var_info (heapvar, name);
3624 vi->is_artificial_var = true;
3625 vi->is_heap_var = true;
3626 vi->is_unknown_size_var = true;
3627 vi->offset = 0;
3628 vi->fullsize = ~0;
3629 vi->size = ~0;
3630 vi->is_full_var = true;
3631 insert_vi_for_tree (heapvar, vi);
3633 return vi;
3636 /* Create a new artificial heap variable with NAME and make a
3637 constraint from it to LHS. Return the created variable. */
3639 static varinfo_t
3640 make_constraint_from_heapvar (varinfo_t lhs, const char *name)
3642 varinfo_t vi = make_heapvar (name);
3643 make_constraint_from (lhs, vi->id);
3645 return vi;
3648 /* Create a new artificial heap variable with NAME and make a
3649 constraint from it to LHS. Set flags according to a tag used
3650 for tracking restrict pointers. */
3652 static void
3653 make_constraint_from_restrict (varinfo_t lhs, const char *name)
3655 varinfo_t vi;
3656 vi = make_constraint_from_heapvar (lhs, name);
3657 vi->is_restrict_var = 1;
3658 vi->is_global_var = 0;
3659 vi->is_special_var = 1;
3660 vi->may_have_pointers = 0;
3663 /* In IPA mode there are varinfos for different aspects of reach
3664 function designator. One for the points-to set of the return
3665 value, one for the variables that are clobbered by the function,
3666 one for its uses and one for each parameter (including a single
3667 glob for remaining variadic arguments). */
3669 enum { fi_clobbers = 1, fi_uses = 2,
3670 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 };
3672 /* Get a constraint for the requested part of a function designator FI
3673 when operating in IPA mode. */
3675 static struct constraint_expr
3676 get_function_part_constraint (varinfo_t fi, unsigned part)
3678 struct constraint_expr c;
3680 gcc_assert (in_ipa_mode);
3682 if (fi->id == anything_id)
3684 /* ??? We probably should have a ANYFN special variable. */
3685 c.var = anything_id;
3686 c.offset = 0;
3687 c.type = SCALAR;
3689 else if (TREE_CODE (fi->decl) == FUNCTION_DECL)
3691 varinfo_t ai = first_vi_for_offset (fi, part);
3692 if (ai)
3693 c.var = ai->id;
3694 else
3695 c.var = anything_id;
3696 c.offset = 0;
3697 c.type = SCALAR;
3699 else
3701 c.var = fi->id;
3702 c.offset = part;
3703 c.type = DEREF;
3706 return c;
3709 /* For non-IPA mode, generate constraints necessary for a call on the
3710 RHS. */
3712 static void
3713 handle_rhs_call (gimple stmt, VEC(ce_s, heap) **results)
3715 struct constraint_expr rhsc;
3716 unsigned i;
3717 bool returns_uses = false;
3719 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3721 tree arg = gimple_call_arg (stmt, i);
3722 int flags = gimple_call_arg_flags (stmt, i);
3724 /* If the argument is not used we can ignore it. */
3725 if (flags & EAF_UNUSED)
3726 continue;
3728 /* As we compute ESCAPED context-insensitive we do not gain
3729 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3730 set. The argument would still get clobbered through the
3731 escape solution.
3732 ??? We might get away with less (and more precise) constraints
3733 if using a temporary for transitively closing things. */
3734 if ((flags & EAF_NOCLOBBER)
3735 && (flags & EAF_NOESCAPE))
3737 varinfo_t uses = get_call_use_vi (stmt);
3738 if (!(flags & EAF_DIRECT))
3739 make_transitive_closure_constraints (uses);
3740 make_constraint_to (uses->id, arg);
3741 returns_uses = true;
3743 else if (flags & EAF_NOESCAPE)
3745 varinfo_t uses = get_call_use_vi (stmt);
3746 varinfo_t clobbers = get_call_clobber_vi (stmt);
3747 if (!(flags & EAF_DIRECT))
3749 make_transitive_closure_constraints (uses);
3750 make_transitive_closure_constraints (clobbers);
3752 make_constraint_to (uses->id, arg);
3753 make_constraint_to (clobbers->id, arg);
3754 returns_uses = true;
3756 else
3757 make_escape_constraint (arg);
3760 /* If we added to the calls uses solution make sure we account for
3761 pointers to it to be returned. */
3762 if (returns_uses)
3764 rhsc.var = get_call_use_vi (stmt)->id;
3765 rhsc.offset = 0;
3766 rhsc.type = SCALAR;
3767 VEC_safe_push (ce_s, heap, *results, &rhsc);
3770 /* The static chain escapes as well. */
3771 if (gimple_call_chain (stmt))
3772 make_escape_constraint (gimple_call_chain (stmt));
3774 /* And if we applied NRV the address of the return slot escapes as well. */
3775 if (gimple_call_return_slot_opt_p (stmt)
3776 && gimple_call_lhs (stmt) != NULL_TREE
3777 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3779 VEC(ce_s, heap) *tmpc = NULL;
3780 struct constraint_expr lhsc, *c;
3781 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3782 lhsc.var = escaped_id;
3783 lhsc.offset = 0;
3784 lhsc.type = SCALAR;
3785 FOR_EACH_VEC_ELT (ce_s, tmpc, i, c)
3786 process_constraint (new_constraint (lhsc, *c));
3787 VEC_free(ce_s, heap, tmpc);
3790 /* Regular functions return nonlocal memory. */
3791 rhsc.var = nonlocal_id;
3792 rhsc.offset = 0;
3793 rhsc.type = SCALAR;
3794 VEC_safe_push (ce_s, heap, *results, &rhsc);
3797 /* For non-IPA mode, generate constraints necessary for a call
3798 that returns a pointer and assigns it to LHS. This simply makes
3799 the LHS point to global and escaped variables. */
3801 static void
3802 handle_lhs_call (gimple stmt, tree lhs, int flags, VEC(ce_s, heap) *rhsc,
3803 tree fndecl)
3805 VEC(ce_s, heap) *lhsc = NULL;
3807 get_constraint_for (lhs, &lhsc);
3808 /* If the store is to a global decl make sure to
3809 add proper escape constraints. */
3810 lhs = get_base_address (lhs);
3811 if (lhs
3812 && DECL_P (lhs)
3813 && is_global_var (lhs))
3815 struct constraint_expr tmpc;
3816 tmpc.var = escaped_id;
3817 tmpc.offset = 0;
3818 tmpc.type = SCALAR;
3819 VEC_safe_push (ce_s, heap, lhsc, &tmpc);
3822 /* If the call returns an argument unmodified override the rhs
3823 constraints. */
3824 flags = gimple_call_return_flags (stmt);
3825 if (flags & ERF_RETURNS_ARG
3826 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
3828 tree arg;
3829 rhsc = NULL;
3830 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
3831 get_constraint_for (arg, &rhsc);
3832 process_all_all_constraints (lhsc, rhsc);
3833 VEC_free (ce_s, heap, rhsc);
3835 else if (flags & ERF_NOALIAS)
3837 varinfo_t vi;
3838 struct constraint_expr tmpc;
3839 rhsc = NULL;
3840 vi = make_heapvar ("HEAP");
3841 /* We delay marking allocated storage global until we know if
3842 it escapes. */
3843 DECL_EXTERNAL (vi->decl) = 0;
3844 vi->is_global_var = 0;
3845 /* If this is not a real malloc call assume the memory was
3846 initialized and thus may point to global memory. All
3847 builtin functions with the malloc attribute behave in a sane way. */
3848 if (!fndecl
3849 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3850 make_constraint_from (vi, nonlocal_id);
3851 tmpc.var = vi->id;
3852 tmpc.offset = 0;
3853 tmpc.type = ADDRESSOF;
3854 VEC_safe_push (ce_s, heap, rhsc, &tmpc);
3857 process_all_all_constraints (lhsc, rhsc);
3859 VEC_free (ce_s, heap, lhsc);
3862 /* For non-IPA mode, generate constraints necessary for a call of a
3863 const function that returns a pointer in the statement STMT. */
3865 static void
3866 handle_const_call (gimple stmt, VEC(ce_s, heap) **results)
3868 struct constraint_expr rhsc;
3869 unsigned int k;
3871 /* Treat nested const functions the same as pure functions as far
3872 as the static chain is concerned. */
3873 if (gimple_call_chain (stmt))
3875 varinfo_t uses = get_call_use_vi (stmt);
3876 make_transitive_closure_constraints (uses);
3877 make_constraint_to (uses->id, gimple_call_chain (stmt));
3878 rhsc.var = uses->id;
3879 rhsc.offset = 0;
3880 rhsc.type = SCALAR;
3881 VEC_safe_push (ce_s, heap, *results, &rhsc);
3884 /* May return arguments. */
3885 for (k = 0; k < gimple_call_num_args (stmt); ++k)
3887 tree arg = gimple_call_arg (stmt, k);
3888 VEC(ce_s, heap) *argc = NULL;
3889 unsigned i;
3890 struct constraint_expr *argp;
3891 get_constraint_for_rhs (arg, &argc);
3892 FOR_EACH_VEC_ELT (ce_s, argc, i, argp)
3893 VEC_safe_push (ce_s, heap, *results, argp);
3894 VEC_free(ce_s, heap, argc);
3897 /* May return addresses of globals. */
3898 rhsc.var = nonlocal_id;
3899 rhsc.offset = 0;
3900 rhsc.type = ADDRESSOF;
3901 VEC_safe_push (ce_s, heap, *results, &rhsc);
3904 /* For non-IPA mode, generate constraints necessary for a call to a
3905 pure function in statement STMT. */
3907 static void
3908 handle_pure_call (gimple stmt, VEC(ce_s, heap) **results)
3910 struct constraint_expr rhsc;
3911 unsigned i;
3912 varinfo_t uses = NULL;
3914 /* Memory reached from pointer arguments is call-used. */
3915 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3917 tree arg = gimple_call_arg (stmt, i);
3918 if (!uses)
3920 uses = get_call_use_vi (stmt);
3921 make_transitive_closure_constraints (uses);
3923 make_constraint_to (uses->id, arg);
3926 /* The static chain is used as well. */
3927 if (gimple_call_chain (stmt))
3929 if (!uses)
3931 uses = get_call_use_vi (stmt);
3932 make_transitive_closure_constraints (uses);
3934 make_constraint_to (uses->id, gimple_call_chain (stmt));
3937 /* Pure functions may return call-used and nonlocal memory. */
3938 if (uses)
3940 rhsc.var = uses->id;
3941 rhsc.offset = 0;
3942 rhsc.type = SCALAR;
3943 VEC_safe_push (ce_s, heap, *results, &rhsc);
3945 rhsc.var = nonlocal_id;
3946 rhsc.offset = 0;
3947 rhsc.type = SCALAR;
3948 VEC_safe_push (ce_s, heap, *results, &rhsc);
3952 /* Return the varinfo for the callee of CALL. */
3954 static varinfo_t
3955 get_fi_for_callee (gimple call)
3957 tree decl, fn = gimple_call_fn (call);
3959 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
3960 fn = OBJ_TYPE_REF_EXPR (fn);
3962 /* If we can directly resolve the function being called, do so.
3963 Otherwise, it must be some sort of indirect expression that
3964 we should still be able to handle. */
3965 decl = gimple_call_addr_fndecl (fn);
3966 if (decl)
3967 return get_vi_for_tree (decl);
3969 /* If the function is anything other than a SSA name pointer we have no
3970 clue and should be getting ANYFN (well, ANYTHING for now). */
3971 if (!fn || TREE_CODE (fn) != SSA_NAME)
3972 return get_varinfo (anything_id);
3974 if ((TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
3975 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL)
3976 && SSA_NAME_IS_DEFAULT_DEF (fn))
3977 fn = SSA_NAME_VAR (fn);
3979 return get_vi_for_tree (fn);
3982 /* Create constraints for the builtin call T. Return true if the call
3983 was handled, otherwise false. */
3985 static bool
3986 find_func_aliases_for_builtin_call (gimple t)
3988 tree fndecl = gimple_call_fndecl (t);
3989 VEC(ce_s, heap) *lhsc = NULL;
3990 VEC(ce_s, heap) *rhsc = NULL;
3991 varinfo_t fi;
3993 if (fndecl != NULL_TREE
3994 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3995 /* ??? All builtins that are handled here need to be handled
3996 in the alias-oracle query functions explicitly! */
3997 switch (DECL_FUNCTION_CODE (fndecl))
3999 /* All the following functions return a pointer to the same object
4000 as their first argument points to. The functions do not add
4001 to the ESCAPED solution. The functions make the first argument
4002 pointed to memory point to what the second argument pointed to
4003 memory points to. */
4004 case BUILT_IN_STRCPY:
4005 case BUILT_IN_STRNCPY:
4006 case BUILT_IN_BCOPY:
4007 case BUILT_IN_MEMCPY:
4008 case BUILT_IN_MEMMOVE:
4009 case BUILT_IN_MEMPCPY:
4010 case BUILT_IN_STPCPY:
4011 case BUILT_IN_STPNCPY:
4012 case BUILT_IN_STRCAT:
4013 case BUILT_IN_STRNCAT:
4014 case BUILT_IN_STRCPY_CHK:
4015 case BUILT_IN_STRNCPY_CHK:
4016 case BUILT_IN_MEMCPY_CHK:
4017 case BUILT_IN_MEMMOVE_CHK:
4018 case BUILT_IN_MEMPCPY_CHK:
4019 case BUILT_IN_STPCPY_CHK:
4020 case BUILT_IN_STRCAT_CHK:
4021 case BUILT_IN_STRNCAT_CHK:
4023 tree res = gimple_call_lhs (t);
4024 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4025 == BUILT_IN_BCOPY ? 1 : 0));
4026 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4027 == BUILT_IN_BCOPY ? 0 : 1));
4028 if (res != NULL_TREE)
4030 get_constraint_for (res, &lhsc);
4031 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4032 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
4033 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4034 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
4035 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK)
4036 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4037 else
4038 get_constraint_for (dest, &rhsc);
4039 process_all_all_constraints (lhsc, rhsc);
4040 VEC_free (ce_s, heap, lhsc);
4041 VEC_free (ce_s, heap, rhsc);
4043 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4044 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4045 do_deref (&lhsc);
4046 do_deref (&rhsc);
4047 process_all_all_constraints (lhsc, rhsc);
4048 VEC_free (ce_s, heap, lhsc);
4049 VEC_free (ce_s, heap, rhsc);
4050 return true;
4052 case BUILT_IN_MEMSET:
4053 case BUILT_IN_MEMSET_CHK:
4055 tree res = gimple_call_lhs (t);
4056 tree dest = gimple_call_arg (t, 0);
4057 unsigned i;
4058 ce_s *lhsp;
4059 struct constraint_expr ac;
4060 if (res != NULL_TREE)
4062 get_constraint_for (res, &lhsc);
4063 get_constraint_for (dest, &rhsc);
4064 process_all_all_constraints (lhsc, rhsc);
4065 VEC_free (ce_s, heap, lhsc);
4066 VEC_free (ce_s, heap, rhsc);
4068 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4069 do_deref (&lhsc);
4070 if (flag_delete_null_pointer_checks
4071 && integer_zerop (gimple_call_arg (t, 1)))
4073 ac.type = ADDRESSOF;
4074 ac.var = nothing_id;
4076 else
4078 ac.type = SCALAR;
4079 ac.var = integer_id;
4081 ac.offset = 0;
4082 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4083 process_constraint (new_constraint (*lhsp, ac));
4084 VEC_free (ce_s, heap, lhsc);
4085 return true;
4087 case BUILT_IN_ASSUME_ALIGNED:
4089 tree res = gimple_call_lhs (t);
4090 tree dest = gimple_call_arg (t, 0);
4091 if (res != NULL_TREE)
4093 get_constraint_for (res, &lhsc);
4094 get_constraint_for (dest, &rhsc);
4095 process_all_all_constraints (lhsc, rhsc);
4096 VEC_free (ce_s, heap, lhsc);
4097 VEC_free (ce_s, heap, rhsc);
4099 return true;
4101 /* All the following functions do not return pointers, do not
4102 modify the points-to sets of memory reachable from their
4103 arguments and do not add to the ESCAPED solution. */
4104 case BUILT_IN_SINCOS:
4105 case BUILT_IN_SINCOSF:
4106 case BUILT_IN_SINCOSL:
4107 case BUILT_IN_FREXP:
4108 case BUILT_IN_FREXPF:
4109 case BUILT_IN_FREXPL:
4110 case BUILT_IN_GAMMA_R:
4111 case BUILT_IN_GAMMAF_R:
4112 case BUILT_IN_GAMMAL_R:
4113 case BUILT_IN_LGAMMA_R:
4114 case BUILT_IN_LGAMMAF_R:
4115 case BUILT_IN_LGAMMAL_R:
4116 case BUILT_IN_MODF:
4117 case BUILT_IN_MODFF:
4118 case BUILT_IN_MODFL:
4119 case BUILT_IN_REMQUO:
4120 case BUILT_IN_REMQUOF:
4121 case BUILT_IN_REMQUOL:
4122 case BUILT_IN_FREE:
4123 return true;
4124 /* Trampolines are special - they set up passing the static
4125 frame. */
4126 case BUILT_IN_INIT_TRAMPOLINE:
4128 tree tramp = gimple_call_arg (t, 0);
4129 tree nfunc = gimple_call_arg (t, 1);
4130 tree frame = gimple_call_arg (t, 2);
4131 unsigned i;
4132 struct constraint_expr lhs, *rhsp;
4133 if (in_ipa_mode)
4135 varinfo_t nfi = NULL;
4136 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR);
4137 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0));
4138 if (nfi)
4140 lhs = get_function_part_constraint (nfi, fi_static_chain);
4141 get_constraint_for (frame, &rhsc);
4142 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4143 process_constraint (new_constraint (lhs, *rhsp));
4144 VEC_free (ce_s, heap, rhsc);
4146 /* Make the frame point to the function for
4147 the trampoline adjustment call. */
4148 get_constraint_for (tramp, &lhsc);
4149 do_deref (&lhsc);
4150 get_constraint_for (nfunc, &rhsc);
4151 process_all_all_constraints (lhsc, rhsc);
4152 VEC_free (ce_s, heap, rhsc);
4153 VEC_free (ce_s, heap, lhsc);
4155 return true;
4158 /* Else fallthru to generic handling which will let
4159 the frame escape. */
4160 break;
4162 case BUILT_IN_ADJUST_TRAMPOLINE:
4164 tree tramp = gimple_call_arg (t, 0);
4165 tree res = gimple_call_lhs (t);
4166 if (in_ipa_mode && res)
4168 get_constraint_for (res, &lhsc);
4169 get_constraint_for (tramp, &rhsc);
4170 do_deref (&rhsc);
4171 process_all_all_constraints (lhsc, rhsc);
4172 VEC_free (ce_s, heap, rhsc);
4173 VEC_free (ce_s, heap, lhsc);
4175 return true;
4177 /* Variadic argument handling needs to be handled in IPA
4178 mode as well. */
4179 case BUILT_IN_VA_START:
4181 if (in_ipa_mode)
4183 tree valist = gimple_call_arg (t, 0);
4184 struct constraint_expr rhs, *lhsp;
4185 unsigned i;
4186 /* The va_list gets access to pointers in variadic
4187 arguments. */
4188 fi = lookup_vi_for_tree (cfun->decl);
4189 gcc_assert (fi != NULL);
4190 get_constraint_for (valist, &lhsc);
4191 do_deref (&lhsc);
4192 rhs = get_function_part_constraint (fi, ~0);
4193 rhs.type = ADDRESSOF;
4194 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4195 process_constraint (new_constraint (*lhsp, rhs));
4196 VEC_free (ce_s, heap, lhsc);
4197 /* va_list is clobbered. */
4198 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4199 return true;
4201 break;
4203 /* va_end doesn't have any effect that matters. */
4204 case BUILT_IN_VA_END:
4205 return true;
4206 /* Alternate return. Simply give up for now. */
4207 case BUILT_IN_RETURN:
4209 fi = NULL;
4210 if (!in_ipa_mode
4211 || !(fi = get_vi_for_tree (cfun->decl)))
4212 make_constraint_from (get_varinfo (escaped_id), anything_id);
4213 else if (in_ipa_mode
4214 && fi != NULL)
4216 struct constraint_expr lhs, rhs;
4217 lhs = get_function_part_constraint (fi, fi_result);
4218 rhs.var = anything_id;
4219 rhs.offset = 0;
4220 rhs.type = SCALAR;
4221 process_constraint (new_constraint (lhs, rhs));
4223 return true;
4225 /* printf-style functions may have hooks to set pointers to
4226 point to somewhere into the generated string. Leave them
4227 for a later excercise... */
4228 default:
4229 /* Fallthru to general call handling. */;
4232 return false;
4235 /* Create constraints for the call T. */
4237 static void
4238 find_func_aliases_for_call (gimple t)
4240 tree fndecl = gimple_call_fndecl (t);
4241 VEC(ce_s, heap) *lhsc = NULL;
4242 VEC(ce_s, heap) *rhsc = NULL;
4243 varinfo_t fi;
4245 if (fndecl != NULL_TREE
4246 && DECL_BUILT_IN (fndecl)
4247 && find_func_aliases_for_builtin_call (t))
4248 return;
4250 fi = get_fi_for_callee (t);
4251 if (!in_ipa_mode
4252 || (fndecl && !fi->is_fn_info))
4254 VEC(ce_s, heap) *rhsc = NULL;
4255 int flags = gimple_call_flags (t);
4257 /* Const functions can return their arguments and addresses
4258 of global memory but not of escaped memory. */
4259 if (flags & (ECF_CONST|ECF_NOVOPS))
4261 if (gimple_call_lhs (t))
4262 handle_const_call (t, &rhsc);
4264 /* Pure functions can return addresses in and of memory
4265 reachable from their arguments, but they are not an escape
4266 point for reachable memory of their arguments. */
4267 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4268 handle_pure_call (t, &rhsc);
4269 else
4270 handle_rhs_call (t, &rhsc);
4271 if (gimple_call_lhs (t))
4272 handle_lhs_call (t, gimple_call_lhs (t), flags, rhsc, fndecl);
4273 VEC_free (ce_s, heap, rhsc);
4275 else
4277 tree lhsop;
4278 unsigned j;
4280 /* Assign all the passed arguments to the appropriate incoming
4281 parameters of the function. */
4282 for (j = 0; j < gimple_call_num_args (t); j++)
4284 struct constraint_expr lhs ;
4285 struct constraint_expr *rhsp;
4286 tree arg = gimple_call_arg (t, j);
4288 get_constraint_for_rhs (arg, &rhsc);
4289 lhs = get_function_part_constraint (fi, fi_parm_base + j);
4290 while (VEC_length (ce_s, rhsc) != 0)
4292 rhsp = VEC_last (ce_s, rhsc);
4293 process_constraint (new_constraint (lhs, *rhsp));
4294 VEC_pop (ce_s, rhsc);
4298 /* If we are returning a value, assign it to the result. */
4299 lhsop = gimple_call_lhs (t);
4300 if (lhsop)
4302 struct constraint_expr rhs;
4303 struct constraint_expr *lhsp;
4305 get_constraint_for (lhsop, &lhsc);
4306 rhs = get_function_part_constraint (fi, fi_result);
4307 if (fndecl
4308 && DECL_RESULT (fndecl)
4309 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4311 VEC(ce_s, heap) *tem = NULL;
4312 VEC_safe_push (ce_s, heap, tem, &rhs);
4313 do_deref (&tem);
4314 rhs = *VEC_index (ce_s, tem, 0);
4315 VEC_free(ce_s, heap, tem);
4317 FOR_EACH_VEC_ELT (ce_s, lhsc, j, lhsp)
4318 process_constraint (new_constraint (*lhsp, rhs));
4321 /* If we pass the result decl by reference, honor that. */
4322 if (lhsop
4323 && fndecl
4324 && DECL_RESULT (fndecl)
4325 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4327 struct constraint_expr lhs;
4328 struct constraint_expr *rhsp;
4330 get_constraint_for_address_of (lhsop, &rhsc);
4331 lhs = get_function_part_constraint (fi, fi_result);
4332 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
4333 process_constraint (new_constraint (lhs, *rhsp));
4334 VEC_free (ce_s, heap, rhsc);
4337 /* If we use a static chain, pass it along. */
4338 if (gimple_call_chain (t))
4340 struct constraint_expr lhs;
4341 struct constraint_expr *rhsp;
4343 get_constraint_for (gimple_call_chain (t), &rhsc);
4344 lhs = get_function_part_constraint (fi, fi_static_chain);
4345 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
4346 process_constraint (new_constraint (lhs, *rhsp));
4351 /* Walk statement T setting up aliasing constraints according to the
4352 references found in T. This function is the main part of the
4353 constraint builder. AI points to auxiliary alias information used
4354 when building alias sets and computing alias grouping heuristics. */
4356 static void
4357 find_func_aliases (gimple origt)
4359 gimple t = origt;
4360 VEC(ce_s, heap) *lhsc = NULL;
4361 VEC(ce_s, heap) *rhsc = NULL;
4362 struct constraint_expr *c;
4363 varinfo_t fi;
4365 /* Now build constraints expressions. */
4366 if (gimple_code (t) == GIMPLE_PHI)
4368 size_t i;
4369 unsigned int j;
4371 /* For a phi node, assign all the arguments to
4372 the result. */
4373 get_constraint_for (gimple_phi_result (t), &lhsc);
4374 for (i = 0; i < gimple_phi_num_args (t); i++)
4376 tree strippedrhs = PHI_ARG_DEF (t, i);
4378 STRIP_NOPS (strippedrhs);
4379 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
4381 FOR_EACH_VEC_ELT (ce_s, lhsc, j, c)
4383 struct constraint_expr *c2;
4384 while (VEC_length (ce_s, rhsc) > 0)
4386 c2 = VEC_last (ce_s, rhsc);
4387 process_constraint (new_constraint (*c, *c2));
4388 VEC_pop (ce_s, rhsc);
4393 /* In IPA mode, we need to generate constraints to pass call
4394 arguments through their calls. There are two cases,
4395 either a GIMPLE_CALL returning a value, or just a plain
4396 GIMPLE_CALL when we are not.
4398 In non-ipa mode, we need to generate constraints for each
4399 pointer passed by address. */
4400 else if (is_gimple_call (t))
4401 find_func_aliases_for_call (t);
4403 /* Otherwise, just a regular assignment statement. Only care about
4404 operations with pointer result, others are dealt with as escape
4405 points if they have pointer operands. */
4406 else if (is_gimple_assign (t))
4408 /* Otherwise, just a regular assignment statement. */
4409 tree lhsop = gimple_assign_lhs (t);
4410 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
4412 if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
4413 do_structure_copy (lhsop, rhsop);
4414 else
4416 enum tree_code code = gimple_assign_rhs_code (t);
4418 get_constraint_for (lhsop, &lhsc);
4420 if (code == POINTER_PLUS_EXPR)
4421 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4422 gimple_assign_rhs2 (t), &rhsc);
4423 else if (code == BIT_AND_EXPR
4424 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
4426 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4427 the pointer. Handle it by offsetting it by UNKNOWN. */
4428 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4429 NULL_TREE, &rhsc);
4431 else if ((CONVERT_EXPR_CODE_P (code)
4432 && !(POINTER_TYPE_P (gimple_expr_type (t))
4433 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
4434 || gimple_assign_single_p (t))
4435 get_constraint_for_rhs (rhsop, &rhsc);
4436 else if (truth_value_p (code))
4437 /* Truth value results are not pointer (parts). Or at least
4438 very very unreasonable obfuscation of a part. */
4440 else
4442 /* All other operations are merges. */
4443 VEC (ce_s, heap) *tmp = NULL;
4444 struct constraint_expr *rhsp;
4445 unsigned i, j;
4446 get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc);
4447 for (i = 2; i < gimple_num_ops (t); ++i)
4449 get_constraint_for_rhs (gimple_op (t, i), &tmp);
4450 FOR_EACH_VEC_ELT (ce_s, tmp, j, rhsp)
4451 VEC_safe_push (ce_s, heap, rhsc, rhsp);
4452 VEC_truncate (ce_s, tmp, 0);
4454 VEC_free (ce_s, heap, tmp);
4456 process_all_all_constraints (lhsc, rhsc);
4458 /* If there is a store to a global variable the rhs escapes. */
4459 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
4460 && DECL_P (lhsop)
4461 && is_global_var (lhsop)
4462 && (!in_ipa_mode
4463 || DECL_EXTERNAL (lhsop) || TREE_PUBLIC (lhsop)))
4464 make_escape_constraint (rhsop);
4465 /* If this is a conversion of a non-restrict pointer to a
4466 restrict pointer track it with a new heapvar. */
4467 else if (gimple_assign_cast_p (t)
4468 && POINTER_TYPE_P (TREE_TYPE (rhsop))
4469 && POINTER_TYPE_P (TREE_TYPE (lhsop))
4470 && !TYPE_RESTRICT (TREE_TYPE (rhsop))
4471 && TYPE_RESTRICT (TREE_TYPE (lhsop)))
4472 make_constraint_from_restrict (get_vi_for_tree (lhsop),
4473 "CAST_RESTRICT");
4475 /* Handle escapes through return. */
4476 else if (gimple_code (t) == GIMPLE_RETURN
4477 && gimple_return_retval (t) != NULL_TREE)
4479 fi = NULL;
4480 if (!in_ipa_mode
4481 || !(fi = get_vi_for_tree (cfun->decl)))
4482 make_escape_constraint (gimple_return_retval (t));
4483 else if (in_ipa_mode
4484 && fi != NULL)
4486 struct constraint_expr lhs ;
4487 struct constraint_expr *rhsp;
4488 unsigned i;
4490 lhs = get_function_part_constraint (fi, fi_result);
4491 get_constraint_for_rhs (gimple_return_retval (t), &rhsc);
4492 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4493 process_constraint (new_constraint (lhs, *rhsp));
4496 /* Handle asms conservatively by adding escape constraints to everything. */
4497 else if (gimple_code (t) == GIMPLE_ASM)
4499 unsigned i, noutputs;
4500 const char **oconstraints;
4501 const char *constraint;
4502 bool allows_mem, allows_reg, is_inout;
4504 noutputs = gimple_asm_noutputs (t);
4505 oconstraints = XALLOCAVEC (const char *, noutputs);
4507 for (i = 0; i < noutputs; ++i)
4509 tree link = gimple_asm_output_op (t, i);
4510 tree op = TREE_VALUE (link);
4512 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4513 oconstraints[i] = constraint;
4514 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4515 &allows_reg, &is_inout);
4517 /* A memory constraint makes the address of the operand escape. */
4518 if (!allows_reg && allows_mem)
4519 make_escape_constraint (build_fold_addr_expr (op));
4521 /* The asm may read global memory, so outputs may point to
4522 any global memory. */
4523 if (op)
4525 VEC(ce_s, heap) *lhsc = NULL;
4526 struct constraint_expr rhsc, *lhsp;
4527 unsigned j;
4528 get_constraint_for (op, &lhsc);
4529 rhsc.var = nonlocal_id;
4530 rhsc.offset = 0;
4531 rhsc.type = SCALAR;
4532 FOR_EACH_VEC_ELT (ce_s, lhsc, j, lhsp)
4533 process_constraint (new_constraint (*lhsp, rhsc));
4534 VEC_free (ce_s, heap, lhsc);
4537 for (i = 0; i < gimple_asm_ninputs (t); ++i)
4539 tree link = gimple_asm_input_op (t, i);
4540 tree op = TREE_VALUE (link);
4542 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4544 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4545 &allows_mem, &allows_reg);
4547 /* A memory constraint makes the address of the operand escape. */
4548 if (!allows_reg && allows_mem)
4549 make_escape_constraint (build_fold_addr_expr (op));
4550 /* Strictly we'd only need the constraint to ESCAPED if
4551 the asm clobbers memory, otherwise using something
4552 along the lines of per-call clobbers/uses would be enough. */
4553 else if (op)
4554 make_escape_constraint (op);
4558 VEC_free (ce_s, heap, rhsc);
4559 VEC_free (ce_s, heap, lhsc);
4563 /* Create a constraint adding to the clobber set of FI the memory
4564 pointed to by PTR. */
4566 static void
4567 process_ipa_clobber (varinfo_t fi, tree ptr)
4569 VEC(ce_s, heap) *ptrc = NULL;
4570 struct constraint_expr *c, lhs;
4571 unsigned i;
4572 get_constraint_for_rhs (ptr, &ptrc);
4573 lhs = get_function_part_constraint (fi, fi_clobbers);
4574 FOR_EACH_VEC_ELT (ce_s, ptrc, i, c)
4575 process_constraint (new_constraint (lhs, *c));
4576 VEC_free (ce_s, heap, ptrc);
4579 /* Walk statement T setting up clobber and use constraints according to the
4580 references found in T. This function is a main part of the
4581 IPA constraint builder. */
4583 static void
4584 find_func_clobbers (gimple origt)
4586 gimple t = origt;
4587 VEC(ce_s, heap) *lhsc = NULL;
4588 VEC(ce_s, heap) *rhsc = NULL;
4589 varinfo_t fi;
4591 /* Add constraints for clobbered/used in IPA mode.
4592 We are not interested in what automatic variables are clobbered
4593 or used as we only use the information in the caller to which
4594 they do not escape. */
4595 gcc_assert (in_ipa_mode);
4597 /* If the stmt refers to memory in any way it better had a VUSE. */
4598 if (gimple_vuse (t) == NULL_TREE)
4599 return;
4601 /* We'd better have function information for the current function. */
4602 fi = lookup_vi_for_tree (cfun->decl);
4603 gcc_assert (fi != NULL);
4605 /* Account for stores in assignments and calls. */
4606 if (gimple_vdef (t) != NULL_TREE
4607 && gimple_has_lhs (t))
4609 tree lhs = gimple_get_lhs (t);
4610 tree tem = lhs;
4611 while (handled_component_p (tem))
4612 tem = TREE_OPERAND (tem, 0);
4613 if ((DECL_P (tem)
4614 && !auto_var_in_fn_p (tem, cfun->decl))
4615 || INDIRECT_REF_P (tem)
4616 || (TREE_CODE (tem) == MEM_REF
4617 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4618 && auto_var_in_fn_p
4619 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4621 struct constraint_expr lhsc, *rhsp;
4622 unsigned i;
4623 lhsc = get_function_part_constraint (fi, fi_clobbers);
4624 get_constraint_for_address_of (lhs, &rhsc);
4625 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4626 process_constraint (new_constraint (lhsc, *rhsp));
4627 VEC_free (ce_s, heap, rhsc);
4631 /* Account for uses in assigments and returns. */
4632 if (gimple_assign_single_p (t)
4633 || (gimple_code (t) == GIMPLE_RETURN
4634 && gimple_return_retval (t) != NULL_TREE))
4636 tree rhs = (gimple_assign_single_p (t)
4637 ? gimple_assign_rhs1 (t) : gimple_return_retval (t));
4638 tree tem = rhs;
4639 while (handled_component_p (tem))
4640 tem = TREE_OPERAND (tem, 0);
4641 if ((DECL_P (tem)
4642 && !auto_var_in_fn_p (tem, cfun->decl))
4643 || INDIRECT_REF_P (tem)
4644 || (TREE_CODE (tem) == MEM_REF
4645 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4646 && auto_var_in_fn_p
4647 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4649 struct constraint_expr lhs, *rhsp;
4650 unsigned i;
4651 lhs = get_function_part_constraint (fi, fi_uses);
4652 get_constraint_for_address_of (rhs, &rhsc);
4653 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4654 process_constraint (new_constraint (lhs, *rhsp));
4655 VEC_free (ce_s, heap, rhsc);
4659 if (is_gimple_call (t))
4661 varinfo_t cfi = NULL;
4662 tree decl = gimple_call_fndecl (t);
4663 struct constraint_expr lhs, rhs;
4664 unsigned i, j;
4666 /* For builtins we do not have separate function info. For those
4667 we do not generate escapes for we have to generate clobbers/uses. */
4668 if (decl
4669 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4670 switch (DECL_FUNCTION_CODE (decl))
4672 /* The following functions use and clobber memory pointed to
4673 by their arguments. */
4674 case BUILT_IN_STRCPY:
4675 case BUILT_IN_STRNCPY:
4676 case BUILT_IN_BCOPY:
4677 case BUILT_IN_MEMCPY:
4678 case BUILT_IN_MEMMOVE:
4679 case BUILT_IN_MEMPCPY:
4680 case BUILT_IN_STPCPY:
4681 case BUILT_IN_STPNCPY:
4682 case BUILT_IN_STRCAT:
4683 case BUILT_IN_STRNCAT:
4684 case BUILT_IN_STRCPY_CHK:
4685 case BUILT_IN_STRNCPY_CHK:
4686 case BUILT_IN_MEMCPY_CHK:
4687 case BUILT_IN_MEMMOVE_CHK:
4688 case BUILT_IN_MEMPCPY_CHK:
4689 case BUILT_IN_STPCPY_CHK:
4690 case BUILT_IN_STRCAT_CHK:
4691 case BUILT_IN_STRNCAT_CHK:
4693 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4694 == BUILT_IN_BCOPY ? 1 : 0));
4695 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4696 == BUILT_IN_BCOPY ? 0 : 1));
4697 unsigned i;
4698 struct constraint_expr *rhsp, *lhsp;
4699 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4700 lhs = get_function_part_constraint (fi, fi_clobbers);
4701 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4702 process_constraint (new_constraint (lhs, *lhsp));
4703 VEC_free (ce_s, heap, lhsc);
4704 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4705 lhs = get_function_part_constraint (fi, fi_uses);
4706 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4707 process_constraint (new_constraint (lhs, *rhsp));
4708 VEC_free (ce_s, heap, rhsc);
4709 return;
4711 /* The following function clobbers memory pointed to by
4712 its argument. */
4713 case BUILT_IN_MEMSET:
4714 case BUILT_IN_MEMSET_CHK:
4716 tree dest = gimple_call_arg (t, 0);
4717 unsigned i;
4718 ce_s *lhsp;
4719 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4720 lhs = get_function_part_constraint (fi, fi_clobbers);
4721 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4722 process_constraint (new_constraint (lhs, *lhsp));
4723 VEC_free (ce_s, heap, lhsc);
4724 return;
4726 /* The following functions clobber their second and third
4727 arguments. */
4728 case BUILT_IN_SINCOS:
4729 case BUILT_IN_SINCOSF:
4730 case BUILT_IN_SINCOSL:
4732 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4733 process_ipa_clobber (fi, gimple_call_arg (t, 2));
4734 return;
4736 /* The following functions clobber their second argument. */
4737 case BUILT_IN_FREXP:
4738 case BUILT_IN_FREXPF:
4739 case BUILT_IN_FREXPL:
4740 case BUILT_IN_LGAMMA_R:
4741 case BUILT_IN_LGAMMAF_R:
4742 case BUILT_IN_LGAMMAL_R:
4743 case BUILT_IN_GAMMA_R:
4744 case BUILT_IN_GAMMAF_R:
4745 case BUILT_IN_GAMMAL_R:
4746 case BUILT_IN_MODF:
4747 case BUILT_IN_MODFF:
4748 case BUILT_IN_MODFL:
4750 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4751 return;
4753 /* The following functions clobber their third argument. */
4754 case BUILT_IN_REMQUO:
4755 case BUILT_IN_REMQUOF:
4756 case BUILT_IN_REMQUOL:
4758 process_ipa_clobber (fi, gimple_call_arg (t, 2));
4759 return;
4761 /* The following functions neither read nor clobber memory. */
4762 case BUILT_IN_ASSUME_ALIGNED:
4763 case BUILT_IN_FREE:
4764 return;
4765 /* Trampolines are of no interest to us. */
4766 case BUILT_IN_INIT_TRAMPOLINE:
4767 case BUILT_IN_ADJUST_TRAMPOLINE:
4768 return;
4769 case BUILT_IN_VA_START:
4770 case BUILT_IN_VA_END:
4771 return;
4772 /* printf-style functions may have hooks to set pointers to
4773 point to somewhere into the generated string. Leave them
4774 for a later excercise... */
4775 default:
4776 /* Fallthru to general call handling. */;
4779 /* Parameters passed by value are used. */
4780 lhs = get_function_part_constraint (fi, fi_uses);
4781 for (i = 0; i < gimple_call_num_args (t); i++)
4783 struct constraint_expr *rhsp;
4784 tree arg = gimple_call_arg (t, i);
4786 if (TREE_CODE (arg) == SSA_NAME
4787 || is_gimple_min_invariant (arg))
4788 continue;
4790 get_constraint_for_address_of (arg, &rhsc);
4791 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
4792 process_constraint (new_constraint (lhs, *rhsp));
4793 VEC_free (ce_s, heap, rhsc);
4796 /* Build constraints for propagating clobbers/uses along the
4797 callgraph edges. */
4798 cfi = get_fi_for_callee (t);
4799 if (cfi->id == anything_id)
4801 if (gimple_vdef (t))
4802 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
4803 anything_id);
4804 make_constraint_from (first_vi_for_offset (fi, fi_uses),
4805 anything_id);
4806 return;
4809 /* For callees without function info (that's external functions),
4810 ESCAPED is clobbered and used. */
4811 if (gimple_call_fndecl (t)
4812 && !cfi->is_fn_info)
4814 varinfo_t vi;
4816 if (gimple_vdef (t))
4817 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
4818 escaped_id);
4819 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
4821 /* Also honor the call statement use/clobber info. */
4822 if ((vi = lookup_call_clobber_vi (t)) != NULL)
4823 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
4824 vi->id);
4825 if ((vi = lookup_call_use_vi (t)) != NULL)
4826 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
4827 vi->id);
4828 return;
4831 /* Otherwise the caller clobbers and uses what the callee does.
4832 ??? This should use a new complex constraint that filters
4833 local variables of the callee. */
4834 if (gimple_vdef (t))
4836 lhs = get_function_part_constraint (fi, fi_clobbers);
4837 rhs = get_function_part_constraint (cfi, fi_clobbers);
4838 process_constraint (new_constraint (lhs, rhs));
4840 lhs = get_function_part_constraint (fi, fi_uses);
4841 rhs = get_function_part_constraint (cfi, fi_uses);
4842 process_constraint (new_constraint (lhs, rhs));
4844 else if (gimple_code (t) == GIMPLE_ASM)
4846 /* ??? Ick. We can do better. */
4847 if (gimple_vdef (t))
4848 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
4849 anything_id);
4850 make_constraint_from (first_vi_for_offset (fi, fi_uses),
4851 anything_id);
4854 VEC_free (ce_s, heap, rhsc);
4858 /* Find the first varinfo in the same variable as START that overlaps with
4859 OFFSET. Return NULL if we can't find one. */
4861 static varinfo_t
4862 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
4864 /* If the offset is outside of the variable, bail out. */
4865 if (offset >= start->fullsize)
4866 return NULL;
4868 /* If we cannot reach offset from start, lookup the first field
4869 and start from there. */
4870 if (start->offset > offset)
4871 start = lookup_vi_for_tree (start->decl);
4873 while (start)
4875 /* We may not find a variable in the field list with the actual
4876 offset when when we have glommed a structure to a variable.
4877 In that case, however, offset should still be within the size
4878 of the variable. */
4879 if (offset >= start->offset
4880 && (offset - start->offset) < start->size)
4881 return start;
4883 start= start->next;
4886 return NULL;
4889 /* Find the first varinfo in the same variable as START that overlaps with
4890 OFFSET. If there is no such varinfo the varinfo directly preceding
4891 OFFSET is returned. */
4893 static varinfo_t
4894 first_or_preceding_vi_for_offset (varinfo_t start,
4895 unsigned HOST_WIDE_INT offset)
4897 /* If we cannot reach offset from start, lookup the first field
4898 and start from there. */
4899 if (start->offset > offset)
4900 start = lookup_vi_for_tree (start->decl);
4902 /* We may not find a variable in the field list with the actual
4903 offset when when we have glommed a structure to a variable.
4904 In that case, however, offset should still be within the size
4905 of the variable.
4906 If we got beyond the offset we look for return the field
4907 directly preceding offset which may be the last field. */
4908 while (start->next
4909 && offset >= start->offset
4910 && !((offset - start->offset) < start->size))
4911 start = start->next;
4913 return start;
4917 /* This structure is used during pushing fields onto the fieldstack
4918 to track the offset of the field, since bitpos_of_field gives it
4919 relative to its immediate containing type, and we want it relative
4920 to the ultimate containing object. */
4922 struct fieldoff
4924 /* Offset from the base of the base containing object to this field. */
4925 HOST_WIDE_INT offset;
4927 /* Size, in bits, of the field. */
4928 unsigned HOST_WIDE_INT size;
4930 unsigned has_unknown_size : 1;
4932 unsigned must_have_pointers : 1;
4934 unsigned may_have_pointers : 1;
4936 unsigned only_restrict_pointers : 1;
4938 typedef struct fieldoff fieldoff_s;
4940 DEF_VEC_O(fieldoff_s);
4941 DEF_VEC_ALLOC_O(fieldoff_s,heap);
4943 /* qsort comparison function for two fieldoff's PA and PB */
4945 static int
4946 fieldoff_compare (const void *pa, const void *pb)
4948 const fieldoff_s *foa = (const fieldoff_s *)pa;
4949 const fieldoff_s *fob = (const fieldoff_s *)pb;
4950 unsigned HOST_WIDE_INT foasize, fobsize;
4952 if (foa->offset < fob->offset)
4953 return -1;
4954 else if (foa->offset > fob->offset)
4955 return 1;
4957 foasize = foa->size;
4958 fobsize = fob->size;
4959 if (foasize < fobsize)
4960 return -1;
4961 else if (foasize > fobsize)
4962 return 1;
4963 return 0;
4966 /* Sort a fieldstack according to the field offset and sizes. */
4967 static void
4968 sort_fieldstack (VEC(fieldoff_s,heap) *fieldstack)
4970 VEC_qsort (fieldoff_s, fieldstack, fieldoff_compare);
4973 /* Return true if V is a tree that we can have subvars for.
4974 Normally, this is any aggregate type. Also complex
4975 types which are not gimple registers can have subvars. */
4977 static inline bool
4978 var_can_have_subvars (const_tree v)
4980 /* Volatile variables should never have subvars. */
4981 if (TREE_THIS_VOLATILE (v))
4982 return false;
4984 /* Non decls or memory tags can never have subvars. */
4985 if (!DECL_P (v))
4986 return false;
4988 /* Aggregates without overlapping fields can have subvars. */
4989 if (TREE_CODE (TREE_TYPE (v)) == RECORD_TYPE)
4990 return true;
4992 return false;
4995 /* Return true if T is a type that does contain pointers. */
4997 static bool
4998 type_must_have_pointers (tree type)
5000 if (POINTER_TYPE_P (type))
5001 return true;
5003 if (TREE_CODE (type) == ARRAY_TYPE)
5004 return type_must_have_pointers (TREE_TYPE (type));
5006 /* A function or method can have pointers as arguments, so track
5007 those separately. */
5008 if (TREE_CODE (type) == FUNCTION_TYPE
5009 || TREE_CODE (type) == METHOD_TYPE)
5010 return true;
5012 return false;
5015 static bool
5016 field_must_have_pointers (tree t)
5018 return type_must_have_pointers (TREE_TYPE (t));
5021 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5022 the fields of TYPE onto fieldstack, recording their offsets along
5023 the way.
5025 OFFSET is used to keep track of the offset in this entire
5026 structure, rather than just the immediately containing structure.
5027 Returns false if the caller is supposed to handle the field we
5028 recursed for. */
5030 static bool
5031 push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
5032 HOST_WIDE_INT offset)
5034 tree field;
5035 bool empty_p = true;
5037 if (TREE_CODE (type) != RECORD_TYPE)
5038 return false;
5040 /* If the vector of fields is growing too big, bail out early.
5041 Callers check for VEC_length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5042 sure this fails. */
5043 if (VEC_length (fieldoff_s, *fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5044 return false;
5046 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5047 if (TREE_CODE (field) == FIELD_DECL)
5049 bool push = false;
5050 HOST_WIDE_INT foff = bitpos_of_field (field);
5052 if (!var_can_have_subvars (field)
5053 || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
5054 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5055 push = true;
5056 else if (!push_fields_onto_fieldstack
5057 (TREE_TYPE (field), fieldstack, offset + foff)
5058 && (DECL_SIZE (field)
5059 && !integer_zerop (DECL_SIZE (field))))
5060 /* Empty structures may have actual size, like in C++. So
5061 see if we didn't push any subfields and the size is
5062 nonzero, push the field onto the stack. */
5063 push = true;
5065 if (push)
5067 fieldoff_s *pair = NULL;
5068 bool has_unknown_size = false;
5069 bool must_have_pointers_p;
5071 if (!VEC_empty (fieldoff_s, *fieldstack))
5072 pair = VEC_last (fieldoff_s, *fieldstack);
5074 /* If there isn't anything at offset zero, create sth. */
5075 if (!pair
5076 && offset + foff != 0)
5078 pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
5079 pair->offset = 0;
5080 pair->size = offset + foff;
5081 pair->has_unknown_size = false;
5082 pair->must_have_pointers = false;
5083 pair->may_have_pointers = false;
5084 pair->only_restrict_pointers = false;
5087 if (!DECL_SIZE (field)
5088 || !host_integerp (DECL_SIZE (field), 1))
5089 has_unknown_size = true;
5091 /* If adjacent fields do not contain pointers merge them. */
5092 must_have_pointers_p = field_must_have_pointers (field);
5093 if (pair
5094 && !has_unknown_size
5095 && !must_have_pointers_p
5096 && !pair->must_have_pointers
5097 && !pair->has_unknown_size
5098 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5100 pair->size += TREE_INT_CST_LOW (DECL_SIZE (field));
5102 else
5104 pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
5105 pair->offset = offset + foff;
5106 pair->has_unknown_size = has_unknown_size;
5107 if (!has_unknown_size)
5108 pair->size = TREE_INT_CST_LOW (DECL_SIZE (field));
5109 else
5110 pair->size = -1;
5111 pair->must_have_pointers = must_have_pointers_p;
5112 pair->may_have_pointers = true;
5113 pair->only_restrict_pointers
5114 = (!has_unknown_size
5115 && POINTER_TYPE_P (TREE_TYPE (field))
5116 && TYPE_RESTRICT (TREE_TYPE (field)));
5120 empty_p = false;
5123 return !empty_p;
5126 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5127 if it is a varargs function. */
5129 static unsigned int
5130 count_num_arguments (tree decl, bool *is_varargs)
5132 unsigned int num = 0;
5133 tree t;
5135 /* Capture named arguments for K&R functions. They do not
5136 have a prototype and thus no TYPE_ARG_TYPES. */
5137 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5138 ++num;
5140 /* Check if the function has variadic arguments. */
5141 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5142 if (TREE_VALUE (t) == void_type_node)
5143 break;
5144 if (!t)
5145 *is_varargs = true;
5147 return num;
5150 /* Creation function node for DECL, using NAME, and return the index
5151 of the variable we've created for the function. */
5153 static varinfo_t
5154 create_function_info_for (tree decl, const char *name)
5156 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5157 varinfo_t vi, prev_vi;
5158 tree arg;
5159 unsigned int i;
5160 bool is_varargs = false;
5161 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5163 /* Create the variable info. */
5165 vi = new_var_info (decl, name);
5166 vi->offset = 0;
5167 vi->size = 1;
5168 vi->fullsize = fi_parm_base + num_args;
5169 vi->is_fn_info = 1;
5170 vi->may_have_pointers = false;
5171 if (is_varargs)
5172 vi->fullsize = ~0;
5173 insert_vi_for_tree (vi->decl, vi);
5175 prev_vi = vi;
5177 /* Create a variable for things the function clobbers and one for
5178 things the function uses. */
5180 varinfo_t clobbervi, usevi;
5181 const char *newname;
5182 char *tempname;
5184 asprintf (&tempname, "%s.clobber", name);
5185 newname = ggc_strdup (tempname);
5186 free (tempname);
5188 clobbervi = new_var_info (NULL, newname);
5189 clobbervi->offset = fi_clobbers;
5190 clobbervi->size = 1;
5191 clobbervi->fullsize = vi->fullsize;
5192 clobbervi->is_full_var = true;
5193 clobbervi->is_global_var = false;
5194 gcc_assert (prev_vi->offset < clobbervi->offset);
5195 prev_vi->next = clobbervi;
5196 prev_vi = clobbervi;
5198 asprintf (&tempname, "%s.use", name);
5199 newname = ggc_strdup (tempname);
5200 free (tempname);
5202 usevi = new_var_info (NULL, newname);
5203 usevi->offset = fi_uses;
5204 usevi->size = 1;
5205 usevi->fullsize = vi->fullsize;
5206 usevi->is_full_var = true;
5207 usevi->is_global_var = false;
5208 gcc_assert (prev_vi->offset < usevi->offset);
5209 prev_vi->next = usevi;
5210 prev_vi = usevi;
5213 /* And one for the static chain. */
5214 if (fn->static_chain_decl != NULL_TREE)
5216 varinfo_t chainvi;
5217 const char *newname;
5218 char *tempname;
5220 asprintf (&tempname, "%s.chain", name);
5221 newname = ggc_strdup (tempname);
5222 free (tempname);
5224 chainvi = new_var_info (fn->static_chain_decl, newname);
5225 chainvi->offset = fi_static_chain;
5226 chainvi->size = 1;
5227 chainvi->fullsize = vi->fullsize;
5228 chainvi->is_full_var = true;
5229 chainvi->is_global_var = false;
5230 gcc_assert (prev_vi->offset < chainvi->offset);
5231 prev_vi->next = chainvi;
5232 prev_vi = chainvi;
5233 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5236 /* Create a variable for the return var. */
5237 if (DECL_RESULT (decl) != NULL
5238 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5240 varinfo_t resultvi;
5241 const char *newname;
5242 char *tempname;
5243 tree resultdecl = decl;
5245 if (DECL_RESULT (decl))
5246 resultdecl = DECL_RESULT (decl);
5248 asprintf (&tempname, "%s.result", name);
5249 newname = ggc_strdup (tempname);
5250 free (tempname);
5252 resultvi = new_var_info (resultdecl, newname);
5253 resultvi->offset = fi_result;
5254 resultvi->size = 1;
5255 resultvi->fullsize = vi->fullsize;
5256 resultvi->is_full_var = true;
5257 if (DECL_RESULT (decl))
5258 resultvi->may_have_pointers = true;
5259 gcc_assert (prev_vi->offset < resultvi->offset);
5260 prev_vi->next = resultvi;
5261 prev_vi = resultvi;
5262 if (DECL_RESULT (decl))
5263 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
5266 /* Set up variables for each argument. */
5267 arg = DECL_ARGUMENTS (decl);
5268 for (i = 0; i < num_args; i++)
5270 varinfo_t argvi;
5271 const char *newname;
5272 char *tempname;
5273 tree argdecl = decl;
5275 if (arg)
5276 argdecl = arg;
5278 asprintf (&tempname, "%s.arg%d", name, i);
5279 newname = ggc_strdup (tempname);
5280 free (tempname);
5282 argvi = new_var_info (argdecl, newname);
5283 argvi->offset = fi_parm_base + i;
5284 argvi->size = 1;
5285 argvi->is_full_var = true;
5286 argvi->fullsize = vi->fullsize;
5287 if (arg)
5288 argvi->may_have_pointers = true;
5289 gcc_assert (prev_vi->offset < argvi->offset);
5290 prev_vi->next = argvi;
5291 prev_vi = argvi;
5292 if (arg)
5294 insert_vi_for_tree (arg, argvi);
5295 arg = DECL_CHAIN (arg);
5299 /* Add one representative for all further args. */
5300 if (is_varargs)
5302 varinfo_t argvi;
5303 const char *newname;
5304 char *tempname;
5305 tree decl;
5307 asprintf (&tempname, "%s.varargs", name);
5308 newname = ggc_strdup (tempname);
5309 free (tempname);
5311 /* We need sth that can be pointed to for va_start. */
5312 decl = build_fake_var_decl (ptr_type_node);
5314 argvi = new_var_info (decl, newname);
5315 argvi->offset = fi_parm_base + num_args;
5316 argvi->size = ~0;
5317 argvi->is_full_var = true;
5318 argvi->is_heap_var = true;
5319 argvi->fullsize = vi->fullsize;
5320 gcc_assert (prev_vi->offset < argvi->offset);
5321 prev_vi->next = argvi;
5322 prev_vi = argvi;
5325 return vi;
5329 /* Return true if FIELDSTACK contains fields that overlap.
5330 FIELDSTACK is assumed to be sorted by offset. */
5332 static bool
5333 check_for_overlaps (VEC (fieldoff_s,heap) *fieldstack)
5335 fieldoff_s *fo = NULL;
5336 unsigned int i;
5337 HOST_WIDE_INT lastoffset = -1;
5339 FOR_EACH_VEC_ELT (fieldoff_s, fieldstack, i, fo)
5341 if (fo->offset == lastoffset)
5342 return true;
5343 lastoffset = fo->offset;
5345 return false;
5348 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5349 This will also create any varinfo structures necessary for fields
5350 of DECL. */
5352 static varinfo_t
5353 create_variable_info_for_1 (tree decl, const char *name)
5355 varinfo_t vi, newvi;
5356 tree decl_type = TREE_TYPE (decl);
5357 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
5358 VEC (fieldoff_s,heap) *fieldstack = NULL;
5359 fieldoff_s *fo;
5360 unsigned int i;
5362 if (!declsize
5363 || !host_integerp (declsize, 1))
5365 vi = new_var_info (decl, name);
5366 vi->offset = 0;
5367 vi->size = ~0;
5368 vi->fullsize = ~0;
5369 vi->is_unknown_size_var = true;
5370 vi->is_full_var = true;
5371 vi->may_have_pointers = true;
5372 return vi;
5375 /* Collect field information. */
5376 if (use_field_sensitive
5377 && var_can_have_subvars (decl)
5378 /* ??? Force us to not use subfields for global initializers
5379 in IPA mode. Else we'd have to parse arbitrary initializers. */
5380 && !(in_ipa_mode
5381 && is_global_var (decl)
5382 && DECL_INITIAL (decl)))
5384 fieldoff_s *fo = NULL;
5385 bool notokay = false;
5386 unsigned int i;
5388 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
5390 for (i = 0; !notokay && VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
5391 if (fo->has_unknown_size
5392 || fo->offset < 0)
5394 notokay = true;
5395 break;
5398 /* We can't sort them if we have a field with a variable sized type,
5399 which will make notokay = true. In that case, we are going to return
5400 without creating varinfos for the fields anyway, so sorting them is a
5401 waste to boot. */
5402 if (!notokay)
5404 sort_fieldstack (fieldstack);
5405 /* Due to some C++ FE issues, like PR 22488, we might end up
5406 what appear to be overlapping fields even though they,
5407 in reality, do not overlap. Until the C++ FE is fixed,
5408 we will simply disable field-sensitivity for these cases. */
5409 notokay = check_for_overlaps (fieldstack);
5412 if (notokay)
5413 VEC_free (fieldoff_s, heap, fieldstack);
5416 /* If we didn't end up collecting sub-variables create a full
5417 variable for the decl. */
5418 if (VEC_length (fieldoff_s, fieldstack) <= 1
5419 || VEC_length (fieldoff_s, fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5421 vi = new_var_info (decl, name);
5422 vi->offset = 0;
5423 vi->may_have_pointers = true;
5424 vi->fullsize = TREE_INT_CST_LOW (declsize);
5425 vi->size = vi->fullsize;
5426 vi->is_full_var = true;
5427 VEC_free (fieldoff_s, heap, fieldstack);
5428 return vi;
5431 vi = new_var_info (decl, name);
5432 vi->fullsize = TREE_INT_CST_LOW (declsize);
5433 for (i = 0, newvi = vi;
5434 VEC_iterate (fieldoff_s, fieldstack, i, fo);
5435 ++i, newvi = newvi->next)
5437 const char *newname = "NULL";
5438 char *tempname;
5440 if (dump_file)
5442 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC
5443 "+" HOST_WIDE_INT_PRINT_DEC, name, fo->offset, fo->size);
5444 newname = ggc_strdup (tempname);
5445 free (tempname);
5447 newvi->name = newname;
5448 newvi->offset = fo->offset;
5449 newvi->size = fo->size;
5450 newvi->fullsize = vi->fullsize;
5451 newvi->may_have_pointers = fo->may_have_pointers;
5452 newvi->only_restrict_pointers = fo->only_restrict_pointers;
5453 if (i + 1 < VEC_length (fieldoff_s, fieldstack))
5454 newvi->next = new_var_info (decl, name);
5457 VEC_free (fieldoff_s, heap, fieldstack);
5459 return vi;
5462 static unsigned int
5463 create_variable_info_for (tree decl, const char *name)
5465 varinfo_t vi = create_variable_info_for_1 (decl, name);
5466 unsigned int id = vi->id;
5468 insert_vi_for_tree (decl, vi);
5470 if (TREE_CODE (decl) != VAR_DECL)
5471 return id;
5473 /* Create initial constraints for globals. */
5474 for (; vi; vi = vi->next)
5476 if (!vi->may_have_pointers
5477 || !vi->is_global_var)
5478 continue;
5480 /* Mark global restrict qualified pointers. */
5481 if ((POINTER_TYPE_P (TREE_TYPE (decl))
5482 && TYPE_RESTRICT (TREE_TYPE (decl)))
5483 || vi->only_restrict_pointers)
5484 make_constraint_from_restrict (vi, "GLOBAL_RESTRICT");
5486 /* In non-IPA mode the initializer from nonlocal is all we need. */
5487 if (!in_ipa_mode
5488 || DECL_HARD_REGISTER (decl))
5489 make_copy_constraint (vi, nonlocal_id);
5491 else
5493 struct varpool_node *vnode = varpool_get_node (decl);
5495 /* For escaped variables initialize them from nonlocal. */
5496 if (!varpool_all_refs_explicit_p (vnode))
5497 make_copy_constraint (vi, nonlocal_id);
5499 /* If this is a global variable with an initializer and we are in
5500 IPA mode generate constraints for it. */
5501 if (DECL_INITIAL (decl))
5503 VEC (ce_s, heap) *rhsc = NULL;
5504 struct constraint_expr lhs, *rhsp;
5505 unsigned i;
5506 get_constraint_for_rhs (DECL_INITIAL (decl), &rhsc);
5507 lhs.var = vi->id;
5508 lhs.offset = 0;
5509 lhs.type = SCALAR;
5510 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
5511 process_constraint (new_constraint (lhs, *rhsp));
5512 /* If this is a variable that escapes from the unit
5513 the initializer escapes as well. */
5514 if (!varpool_all_refs_explicit_p (vnode))
5516 lhs.var = escaped_id;
5517 lhs.offset = 0;
5518 lhs.type = SCALAR;
5519 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
5520 process_constraint (new_constraint (lhs, *rhsp));
5522 VEC_free (ce_s, heap, rhsc);
5527 return id;
5530 /* Print out the points-to solution for VAR to FILE. */
5532 static void
5533 dump_solution_for_var (FILE *file, unsigned int var)
5535 varinfo_t vi = get_varinfo (var);
5536 unsigned int i;
5537 bitmap_iterator bi;
5539 /* Dump the solution for unified vars anyway, this avoids difficulties
5540 in scanning dumps in the testsuite. */
5541 fprintf (file, "%s = { ", vi->name);
5542 vi = get_varinfo (find (var));
5543 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5544 fprintf (file, "%s ", get_varinfo (i)->name);
5545 fprintf (file, "}");
5547 /* But note when the variable was unified. */
5548 if (vi->id != var)
5549 fprintf (file, " same as %s", vi->name);
5551 fprintf (file, "\n");
5554 /* Print the points-to solution for VAR to stdout. */
5556 DEBUG_FUNCTION void
5557 debug_solution_for_var (unsigned int var)
5559 dump_solution_for_var (stdout, var);
5562 /* Create varinfo structures for all of the variables in the
5563 function for intraprocedural mode. */
5565 static void
5566 intra_create_variable_infos (void)
5568 tree t;
5570 /* For each incoming pointer argument arg, create the constraint ARG
5571 = NONLOCAL or a dummy variable if it is a restrict qualified
5572 passed-by-reference argument. */
5573 for (t = DECL_ARGUMENTS (current_function_decl); t; t = DECL_CHAIN (t))
5575 varinfo_t p;
5577 /* For restrict qualified pointers to objects passed by
5578 reference build a real representative for the pointed-to object. */
5579 if (DECL_BY_REFERENCE (t)
5580 && POINTER_TYPE_P (TREE_TYPE (t))
5581 && TYPE_RESTRICT (TREE_TYPE (t)))
5583 struct constraint_expr lhsc, rhsc;
5584 varinfo_t vi;
5585 tree heapvar = build_fake_var_decl (TREE_TYPE (TREE_TYPE (t)));
5586 DECL_EXTERNAL (heapvar) = 1;
5587 vi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS");
5588 insert_vi_for_tree (heapvar, vi);
5589 lhsc.var = get_vi_for_tree (t)->id;
5590 lhsc.type = SCALAR;
5591 lhsc.offset = 0;
5592 rhsc.var = vi->id;
5593 rhsc.type = ADDRESSOF;
5594 rhsc.offset = 0;
5595 process_constraint (new_constraint (lhsc, rhsc));
5596 vi->is_restrict_var = 1;
5597 for (; vi; vi = vi->next)
5598 if (vi->may_have_pointers)
5600 if (vi->only_restrict_pointers)
5601 make_constraint_from_restrict (vi, "GLOBAL_RESTRICT");
5602 make_copy_constraint (vi, nonlocal_id);
5604 continue;
5607 for (p = get_vi_for_tree (t); p; p = p->next)
5609 if (p->may_have_pointers)
5610 make_constraint_from (p, nonlocal_id);
5611 if (p->only_restrict_pointers)
5612 make_constraint_from_restrict (p, "PARM_RESTRICT");
5614 if (POINTER_TYPE_P (TREE_TYPE (t))
5615 && TYPE_RESTRICT (TREE_TYPE (t)))
5616 make_constraint_from_restrict (get_vi_for_tree (t), "PARM_RESTRICT");
5619 /* Add a constraint for a result decl that is passed by reference. */
5620 if (DECL_RESULT (cfun->decl)
5621 && DECL_BY_REFERENCE (DECL_RESULT (cfun->decl)))
5623 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (cfun->decl));
5625 for (p = result_vi; p; p = p->next)
5626 make_constraint_from (p, nonlocal_id);
5629 /* Add a constraint for the incoming static chain parameter. */
5630 if (cfun->static_chain_decl != NULL_TREE)
5632 varinfo_t p, chain_vi = get_vi_for_tree (cfun->static_chain_decl);
5634 for (p = chain_vi; p; p = p->next)
5635 make_constraint_from (p, nonlocal_id);
5639 /* Structure used to put solution bitmaps in a hashtable so they can
5640 be shared among variables with the same points-to set. */
5642 typedef struct shared_bitmap_info
5644 bitmap pt_vars;
5645 hashval_t hashcode;
5646 } *shared_bitmap_info_t;
5647 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
5649 static htab_t shared_bitmap_table;
5651 /* Hash function for a shared_bitmap_info_t */
5653 static hashval_t
5654 shared_bitmap_hash (const void *p)
5656 const_shared_bitmap_info_t const bi = (const_shared_bitmap_info_t) p;
5657 return bi->hashcode;
5660 /* Equality function for two shared_bitmap_info_t's. */
5662 static int
5663 shared_bitmap_eq (const void *p1, const void *p2)
5665 const_shared_bitmap_info_t const sbi1 = (const_shared_bitmap_info_t) p1;
5666 const_shared_bitmap_info_t const sbi2 = (const_shared_bitmap_info_t) p2;
5667 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
5670 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5671 existing instance if there is one, NULL otherwise. */
5673 static bitmap
5674 shared_bitmap_lookup (bitmap pt_vars)
5676 void **slot;
5677 struct shared_bitmap_info sbi;
5679 sbi.pt_vars = pt_vars;
5680 sbi.hashcode = bitmap_hash (pt_vars);
5682 slot = htab_find_slot_with_hash (shared_bitmap_table, &sbi,
5683 sbi.hashcode, NO_INSERT);
5684 if (!slot)
5685 return NULL;
5686 else
5687 return ((shared_bitmap_info_t) *slot)->pt_vars;
5691 /* Add a bitmap to the shared bitmap hashtable. */
5693 static void
5694 shared_bitmap_add (bitmap pt_vars)
5696 void **slot;
5697 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
5699 sbi->pt_vars = pt_vars;
5700 sbi->hashcode = bitmap_hash (pt_vars);
5702 slot = htab_find_slot_with_hash (shared_bitmap_table, sbi,
5703 sbi->hashcode, INSERT);
5704 gcc_assert (!*slot);
5705 *slot = (void *) sbi;
5709 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
5711 static void
5712 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt)
5714 unsigned int i;
5715 bitmap_iterator bi;
5717 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
5719 varinfo_t vi = get_varinfo (i);
5721 /* The only artificial variables that are allowed in a may-alias
5722 set are heap variables. */
5723 if (vi->is_artificial_var && !vi->is_heap_var)
5724 continue;
5726 if (TREE_CODE (vi->decl) == VAR_DECL
5727 || TREE_CODE (vi->decl) == PARM_DECL
5728 || TREE_CODE (vi->decl) == RESULT_DECL)
5730 /* If we are in IPA mode we will not recompute points-to
5731 sets after inlining so make sure they stay valid. */
5732 if (in_ipa_mode
5733 && !DECL_PT_UID_SET_P (vi->decl))
5734 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
5736 /* Add the decl to the points-to set. Note that the points-to
5737 set contains global variables. */
5738 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
5739 if (vi->is_global_var)
5740 pt->vars_contains_global = true;
5746 /* Compute the points-to solution *PT for the variable VI. */
5748 static void
5749 find_what_var_points_to (varinfo_t orig_vi, struct pt_solution *pt)
5751 unsigned int i;
5752 bitmap_iterator bi;
5753 bitmap finished_solution;
5754 bitmap result;
5755 varinfo_t vi;
5757 memset (pt, 0, sizeof (struct pt_solution));
5759 /* This variable may have been collapsed, let's get the real
5760 variable. */
5761 vi = get_varinfo (find (orig_vi->id));
5763 /* Translate artificial variables into SSA_NAME_PTR_INFO
5764 attributes. */
5765 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5767 varinfo_t vi = get_varinfo (i);
5769 if (vi->is_artificial_var)
5771 if (vi->id == nothing_id)
5772 pt->null = 1;
5773 else if (vi->id == escaped_id)
5775 if (in_ipa_mode)
5776 pt->ipa_escaped = 1;
5777 else
5778 pt->escaped = 1;
5780 else if (vi->id == nonlocal_id)
5781 pt->nonlocal = 1;
5782 else if (vi->is_heap_var)
5783 /* We represent heapvars in the points-to set properly. */
5785 else if (vi->id == readonly_id)
5786 /* Nobody cares. */
5788 else if (vi->id == anything_id
5789 || vi->id == integer_id)
5790 pt->anything = 1;
5792 if (vi->is_restrict_var)
5793 pt->vars_contains_restrict = true;
5796 /* Instead of doing extra work, simply do not create
5797 elaborate points-to information for pt_anything pointers. */
5798 if (pt->anything
5799 && (orig_vi->is_artificial_var
5800 || !pt->vars_contains_restrict))
5801 return;
5803 /* Share the final set of variables when possible. */
5804 finished_solution = BITMAP_GGC_ALLOC ();
5805 stats.points_to_sets_created++;
5807 set_uids_in_ptset (finished_solution, vi->solution, pt);
5808 result = shared_bitmap_lookup (finished_solution);
5809 if (!result)
5811 shared_bitmap_add (finished_solution);
5812 pt->vars = finished_solution;
5814 else
5816 pt->vars = result;
5817 bitmap_clear (finished_solution);
5821 /* Given a pointer variable P, fill in its points-to set. */
5823 static void
5824 find_what_p_points_to (tree p)
5826 struct ptr_info_def *pi;
5827 tree lookup_p = p;
5828 varinfo_t vi;
5830 /* For parameters, get at the points-to set for the actual parm
5831 decl. */
5832 if (TREE_CODE (p) == SSA_NAME
5833 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
5834 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL)
5835 && SSA_NAME_IS_DEFAULT_DEF (p))
5836 lookup_p = SSA_NAME_VAR (p);
5838 vi = lookup_vi_for_tree (lookup_p);
5839 if (!vi)
5840 return;
5842 pi = get_ptr_info (p);
5843 find_what_var_points_to (vi, &pi->pt);
5847 /* Query statistics for points-to solutions. */
5849 static struct {
5850 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
5851 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
5852 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
5853 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
5854 } pta_stats;
5856 void
5857 dump_pta_stats (FILE *s)
5859 fprintf (s, "\nPTA query stats:\n");
5860 fprintf (s, " pt_solution_includes: "
5861 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
5862 HOST_WIDE_INT_PRINT_DEC" queries\n",
5863 pta_stats.pt_solution_includes_no_alias,
5864 pta_stats.pt_solution_includes_no_alias
5865 + pta_stats.pt_solution_includes_may_alias);
5866 fprintf (s, " pt_solutions_intersect: "
5867 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
5868 HOST_WIDE_INT_PRINT_DEC" queries\n",
5869 pta_stats.pt_solutions_intersect_no_alias,
5870 pta_stats.pt_solutions_intersect_no_alias
5871 + pta_stats.pt_solutions_intersect_may_alias);
5875 /* Reset the points-to solution *PT to a conservative default
5876 (point to anything). */
5878 void
5879 pt_solution_reset (struct pt_solution *pt)
5881 memset (pt, 0, sizeof (struct pt_solution));
5882 pt->anything = true;
5885 /* Set the points-to solution *PT to point only to the variables
5886 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
5887 global variables and VARS_CONTAINS_RESTRICT specifies whether
5888 it contains restrict tag variables. */
5890 void
5891 pt_solution_set (struct pt_solution *pt, bitmap vars,
5892 bool vars_contains_global, bool vars_contains_restrict)
5894 memset (pt, 0, sizeof (struct pt_solution));
5895 pt->vars = vars;
5896 pt->vars_contains_global = vars_contains_global;
5897 pt->vars_contains_restrict = vars_contains_restrict;
5900 /* Set the points-to solution *PT to point only to the variable VAR. */
5902 void
5903 pt_solution_set_var (struct pt_solution *pt, tree var)
5905 memset (pt, 0, sizeof (struct pt_solution));
5906 pt->vars = BITMAP_GGC_ALLOC ();
5907 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
5908 pt->vars_contains_global = is_global_var (var);
5911 /* Computes the union of the points-to solutions *DEST and *SRC and
5912 stores the result in *DEST. This changes the points-to bitmap
5913 of *DEST and thus may not be used if that might be shared.
5914 The points-to bitmap of *SRC and *DEST will not be shared after
5915 this function if they were not before. */
5917 static void
5918 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
5920 dest->anything |= src->anything;
5921 if (dest->anything)
5923 pt_solution_reset (dest);
5924 return;
5927 dest->nonlocal |= src->nonlocal;
5928 dest->escaped |= src->escaped;
5929 dest->ipa_escaped |= src->ipa_escaped;
5930 dest->null |= src->null;
5931 dest->vars_contains_global |= src->vars_contains_global;
5932 dest->vars_contains_restrict |= src->vars_contains_restrict;
5933 if (!src->vars)
5934 return;
5936 if (!dest->vars)
5937 dest->vars = BITMAP_GGC_ALLOC ();
5938 bitmap_ior_into (dest->vars, src->vars);
5941 /* Return true if the points-to solution *PT is empty. */
5943 bool
5944 pt_solution_empty_p (struct pt_solution *pt)
5946 if (pt->anything
5947 || pt->nonlocal)
5948 return false;
5950 if (pt->vars
5951 && !bitmap_empty_p (pt->vars))
5952 return false;
5954 /* If the solution includes ESCAPED, check if that is empty. */
5955 if (pt->escaped
5956 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
5957 return false;
5959 /* If the solution includes ESCAPED, check if that is empty. */
5960 if (pt->ipa_escaped
5961 && !pt_solution_empty_p (&ipa_escaped_pt))
5962 return false;
5964 return true;
5967 /* Return true if the points-to solution *PT includes global memory. */
5969 bool
5970 pt_solution_includes_global (struct pt_solution *pt)
5972 if (pt->anything
5973 || pt->nonlocal
5974 || pt->vars_contains_global)
5975 return true;
5977 if (pt->escaped)
5978 return pt_solution_includes_global (&cfun->gimple_df->escaped);
5980 if (pt->ipa_escaped)
5981 return pt_solution_includes_global (&ipa_escaped_pt);
5983 /* ??? This predicate is not correct for the IPA-PTA solution
5984 as we do not properly distinguish between unit escape points
5985 and global variables. */
5986 if (cfun->gimple_df->ipa_pta)
5987 return true;
5989 return false;
5992 /* Return true if the points-to solution *PT includes the variable
5993 declaration DECL. */
5995 static bool
5996 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
5998 if (pt->anything)
5999 return true;
6001 if (pt->nonlocal
6002 && is_global_var (decl))
6003 return true;
6005 if (pt->vars
6006 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6007 return true;
6009 /* If the solution includes ESCAPED, check it. */
6010 if (pt->escaped
6011 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6012 return true;
6014 /* If the solution includes ESCAPED, check it. */
6015 if (pt->ipa_escaped
6016 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6017 return true;
6019 return false;
6022 bool
6023 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6025 bool res = pt_solution_includes_1 (pt, decl);
6026 if (res)
6027 ++pta_stats.pt_solution_includes_may_alias;
6028 else
6029 ++pta_stats.pt_solution_includes_no_alias;
6030 return res;
6033 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6034 intersection. */
6036 static bool
6037 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
6039 if (pt1->anything || pt2->anything)
6040 return true;
6042 /* If either points to unknown global memory and the other points to
6043 any global memory they alias. */
6044 if ((pt1->nonlocal
6045 && (pt2->nonlocal
6046 || pt2->vars_contains_global))
6047 || (pt2->nonlocal
6048 && pt1->vars_contains_global))
6049 return true;
6051 /* Check the escaped solution if required. */
6052 if ((pt1->escaped || pt2->escaped)
6053 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6055 /* If both point to escaped memory and that solution
6056 is not empty they alias. */
6057 if (pt1->escaped && pt2->escaped)
6058 return true;
6060 /* If either points to escaped memory see if the escaped solution
6061 intersects with the other. */
6062 if ((pt1->escaped
6063 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt2))
6064 || (pt2->escaped
6065 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt1)))
6066 return true;
6069 /* Check the escaped solution if required.
6070 ??? Do we need to check the local against the IPA escaped sets? */
6071 if ((pt1->ipa_escaped || pt2->ipa_escaped)
6072 && !pt_solution_empty_p (&ipa_escaped_pt))
6074 /* If both point to escaped memory and that solution
6075 is not empty they alias. */
6076 if (pt1->ipa_escaped && pt2->ipa_escaped)
6077 return true;
6079 /* If either points to escaped memory see if the escaped solution
6080 intersects with the other. */
6081 if ((pt1->ipa_escaped
6082 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
6083 || (pt2->ipa_escaped
6084 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
6085 return true;
6088 /* Now both pointers alias if their points-to solution intersects. */
6089 return (pt1->vars
6090 && pt2->vars
6091 && bitmap_intersect_p (pt1->vars, pt2->vars));
6094 bool
6095 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
6097 bool res = pt_solutions_intersect_1 (pt1, pt2);
6098 if (res)
6099 ++pta_stats.pt_solutions_intersect_may_alias;
6100 else
6101 ++pta_stats.pt_solutions_intersect_no_alias;
6102 return res;
6105 /* Return true if both points-to solutions PT1 and PT2 for two restrict
6106 qualified pointers are possibly based on the same pointer. */
6108 bool
6109 pt_solutions_same_restrict_base (struct pt_solution *pt1,
6110 struct pt_solution *pt2)
6112 /* If we deal with points-to solutions of two restrict qualified
6113 pointers solely rely on the pointed-to variable bitmap intersection.
6114 For two pointers that are based on each other the bitmaps will
6115 intersect. */
6116 if (pt1->vars_contains_restrict
6117 && pt2->vars_contains_restrict)
6119 gcc_assert (pt1->vars && pt2->vars);
6120 return bitmap_intersect_p (pt1->vars, pt2->vars);
6123 return true;
6127 /* Dump points-to information to OUTFILE. */
6129 static void
6130 dump_sa_points_to_info (FILE *outfile)
6132 unsigned int i;
6134 fprintf (outfile, "\nPoints-to sets\n\n");
6136 if (dump_flags & TDF_STATS)
6138 fprintf (outfile, "Stats:\n");
6139 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
6140 fprintf (outfile, "Non-pointer vars: %d\n",
6141 stats.nonpointer_vars);
6142 fprintf (outfile, "Statically unified vars: %d\n",
6143 stats.unified_vars_static);
6144 fprintf (outfile, "Dynamically unified vars: %d\n",
6145 stats.unified_vars_dynamic);
6146 fprintf (outfile, "Iterations: %d\n", stats.iterations);
6147 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
6148 fprintf (outfile, "Number of implicit edges: %d\n",
6149 stats.num_implicit_edges);
6152 for (i = 0; i < VEC_length (varinfo_t, varmap); i++)
6154 varinfo_t vi = get_varinfo (i);
6155 if (!vi->may_have_pointers)
6156 continue;
6157 dump_solution_for_var (outfile, i);
6162 /* Debug points-to information to stderr. */
6164 DEBUG_FUNCTION void
6165 debug_sa_points_to_info (void)
6167 dump_sa_points_to_info (stderr);
6171 /* Initialize the always-existing constraint variables for NULL
6172 ANYTHING, READONLY, and INTEGER */
6174 static void
6175 init_base_vars (void)
6177 struct constraint_expr lhs, rhs;
6178 varinfo_t var_anything;
6179 varinfo_t var_nothing;
6180 varinfo_t var_readonly;
6181 varinfo_t var_escaped;
6182 varinfo_t var_nonlocal;
6183 varinfo_t var_storedanything;
6184 varinfo_t var_integer;
6186 /* Create the NULL variable, used to represent that a variable points
6187 to NULL. */
6188 var_nothing = new_var_info (NULL_TREE, "NULL");
6189 gcc_assert (var_nothing->id == nothing_id);
6190 var_nothing->is_artificial_var = 1;
6191 var_nothing->offset = 0;
6192 var_nothing->size = ~0;
6193 var_nothing->fullsize = ~0;
6194 var_nothing->is_special_var = 1;
6195 var_nothing->may_have_pointers = 0;
6196 var_nothing->is_global_var = 0;
6198 /* Create the ANYTHING variable, used to represent that a variable
6199 points to some unknown piece of memory. */
6200 var_anything = new_var_info (NULL_TREE, "ANYTHING");
6201 gcc_assert (var_anything->id == anything_id);
6202 var_anything->is_artificial_var = 1;
6203 var_anything->size = ~0;
6204 var_anything->offset = 0;
6205 var_anything->next = NULL;
6206 var_anything->fullsize = ~0;
6207 var_anything->is_special_var = 1;
6209 /* Anything points to anything. This makes deref constraints just
6210 work in the presence of linked list and other p = *p type loops,
6211 by saying that *ANYTHING = ANYTHING. */
6212 lhs.type = SCALAR;
6213 lhs.var = anything_id;
6214 lhs.offset = 0;
6215 rhs.type = ADDRESSOF;
6216 rhs.var = anything_id;
6217 rhs.offset = 0;
6219 /* This specifically does not use process_constraint because
6220 process_constraint ignores all anything = anything constraints, since all
6221 but this one are redundant. */
6222 VEC_safe_push (constraint_t, heap, constraints, new_constraint (lhs, rhs));
6224 /* Create the READONLY variable, used to represent that a variable
6225 points to readonly memory. */
6226 var_readonly = new_var_info (NULL_TREE, "READONLY");
6227 gcc_assert (var_readonly->id == readonly_id);
6228 var_readonly->is_artificial_var = 1;
6229 var_readonly->offset = 0;
6230 var_readonly->size = ~0;
6231 var_readonly->fullsize = ~0;
6232 var_readonly->next = NULL;
6233 var_readonly->is_special_var = 1;
6235 /* readonly memory points to anything, in order to make deref
6236 easier. In reality, it points to anything the particular
6237 readonly variable can point to, but we don't track this
6238 separately. */
6239 lhs.type = SCALAR;
6240 lhs.var = readonly_id;
6241 lhs.offset = 0;
6242 rhs.type = ADDRESSOF;
6243 rhs.var = readonly_id; /* FIXME */
6244 rhs.offset = 0;
6245 process_constraint (new_constraint (lhs, rhs));
6247 /* Create the ESCAPED variable, used to represent the set of escaped
6248 memory. */
6249 var_escaped = new_var_info (NULL_TREE, "ESCAPED");
6250 gcc_assert (var_escaped->id == escaped_id);
6251 var_escaped->is_artificial_var = 1;
6252 var_escaped->offset = 0;
6253 var_escaped->size = ~0;
6254 var_escaped->fullsize = ~0;
6255 var_escaped->is_special_var = 0;
6257 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6258 memory. */
6259 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL");
6260 gcc_assert (var_nonlocal->id == nonlocal_id);
6261 var_nonlocal->is_artificial_var = 1;
6262 var_nonlocal->offset = 0;
6263 var_nonlocal->size = ~0;
6264 var_nonlocal->fullsize = ~0;
6265 var_nonlocal->is_special_var = 1;
6267 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6268 lhs.type = SCALAR;
6269 lhs.var = escaped_id;
6270 lhs.offset = 0;
6271 rhs.type = DEREF;
6272 rhs.var = escaped_id;
6273 rhs.offset = 0;
6274 process_constraint (new_constraint (lhs, rhs));
6276 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6277 whole variable escapes. */
6278 lhs.type = SCALAR;
6279 lhs.var = escaped_id;
6280 lhs.offset = 0;
6281 rhs.type = SCALAR;
6282 rhs.var = escaped_id;
6283 rhs.offset = UNKNOWN_OFFSET;
6284 process_constraint (new_constraint (lhs, rhs));
6286 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6287 everything pointed to by escaped points to what global memory can
6288 point to. */
6289 lhs.type = DEREF;
6290 lhs.var = escaped_id;
6291 lhs.offset = 0;
6292 rhs.type = SCALAR;
6293 rhs.var = nonlocal_id;
6294 rhs.offset = 0;
6295 process_constraint (new_constraint (lhs, rhs));
6297 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6298 global memory may point to global memory and escaped memory. */
6299 lhs.type = SCALAR;
6300 lhs.var = nonlocal_id;
6301 lhs.offset = 0;
6302 rhs.type = ADDRESSOF;
6303 rhs.var = nonlocal_id;
6304 rhs.offset = 0;
6305 process_constraint (new_constraint (lhs, rhs));
6306 rhs.type = ADDRESSOF;
6307 rhs.var = escaped_id;
6308 rhs.offset = 0;
6309 process_constraint (new_constraint (lhs, rhs));
6311 /* Create the STOREDANYTHING variable, used to represent the set of
6312 variables stored to *ANYTHING. */
6313 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING");
6314 gcc_assert (var_storedanything->id == storedanything_id);
6315 var_storedanything->is_artificial_var = 1;
6316 var_storedanything->offset = 0;
6317 var_storedanything->size = ~0;
6318 var_storedanything->fullsize = ~0;
6319 var_storedanything->is_special_var = 0;
6321 /* Create the INTEGER variable, used to represent that a variable points
6322 to what an INTEGER "points to". */
6323 var_integer = new_var_info (NULL_TREE, "INTEGER");
6324 gcc_assert (var_integer->id == integer_id);
6325 var_integer->is_artificial_var = 1;
6326 var_integer->size = ~0;
6327 var_integer->fullsize = ~0;
6328 var_integer->offset = 0;
6329 var_integer->next = NULL;
6330 var_integer->is_special_var = 1;
6332 /* INTEGER = ANYTHING, because we don't know where a dereference of
6333 a random integer will point to. */
6334 lhs.type = SCALAR;
6335 lhs.var = integer_id;
6336 lhs.offset = 0;
6337 rhs.type = ADDRESSOF;
6338 rhs.var = anything_id;
6339 rhs.offset = 0;
6340 process_constraint (new_constraint (lhs, rhs));
6343 /* Initialize things necessary to perform PTA */
6345 static void
6346 init_alias_vars (void)
6348 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
6350 bitmap_obstack_initialize (&pta_obstack);
6351 bitmap_obstack_initialize (&oldpta_obstack);
6352 bitmap_obstack_initialize (&predbitmap_obstack);
6354 constraint_pool = create_alloc_pool ("Constraint pool",
6355 sizeof (struct constraint), 30);
6356 variable_info_pool = create_alloc_pool ("Variable info pool",
6357 sizeof (struct variable_info), 30);
6358 constraints = VEC_alloc (constraint_t, heap, 8);
6359 varmap = VEC_alloc (varinfo_t, heap, 8);
6360 vi_for_tree = pointer_map_create ();
6361 call_stmt_vars = pointer_map_create ();
6363 memset (&stats, 0, sizeof (stats));
6364 shared_bitmap_table = htab_create (511, shared_bitmap_hash,
6365 shared_bitmap_eq, free);
6366 init_base_vars ();
6368 gcc_obstack_init (&fake_var_decl_obstack);
6371 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6372 predecessor edges. */
6374 static void
6375 remove_preds_and_fake_succs (constraint_graph_t graph)
6377 unsigned int i;
6379 /* Clear the implicit ref and address nodes from the successor
6380 lists. */
6381 for (i = 0; i < FIRST_REF_NODE; i++)
6383 if (graph->succs[i])
6384 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
6385 FIRST_REF_NODE * 2);
6388 /* Free the successor list for the non-ref nodes. */
6389 for (i = FIRST_REF_NODE; i < graph->size; i++)
6391 if (graph->succs[i])
6392 BITMAP_FREE (graph->succs[i]);
6395 /* Now reallocate the size of the successor list as, and blow away
6396 the predecessor bitmaps. */
6397 graph->size = VEC_length (varinfo_t, varmap);
6398 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
6400 free (graph->implicit_preds);
6401 graph->implicit_preds = NULL;
6402 free (graph->preds);
6403 graph->preds = NULL;
6404 bitmap_obstack_release (&predbitmap_obstack);
6407 /* Solve the constraint set. */
6409 static void
6410 solve_constraints (void)
6412 struct scc_info *si;
6414 if (dump_file)
6415 fprintf (dump_file,
6416 "\nCollapsing static cycles and doing variable "
6417 "substitution\n");
6419 init_graph (VEC_length (varinfo_t, varmap) * 2);
6421 if (dump_file)
6422 fprintf (dump_file, "Building predecessor graph\n");
6423 build_pred_graph ();
6425 if (dump_file)
6426 fprintf (dump_file, "Detecting pointer and location "
6427 "equivalences\n");
6428 si = perform_var_substitution (graph);
6430 if (dump_file)
6431 fprintf (dump_file, "Rewriting constraints and unifying "
6432 "variables\n");
6433 rewrite_constraints (graph, si);
6435 build_succ_graph ();
6437 free_var_substitution_info (si);
6439 /* Attach complex constraints to graph nodes. */
6440 move_complex_constraints (graph);
6442 if (dump_file)
6443 fprintf (dump_file, "Uniting pointer but not location equivalent "
6444 "variables\n");
6445 unite_pointer_equivalences (graph);
6447 if (dump_file)
6448 fprintf (dump_file, "Finding indirect cycles\n");
6449 find_indirect_cycles (graph);
6451 /* Implicit nodes and predecessors are no longer necessary at this
6452 point. */
6453 remove_preds_and_fake_succs (graph);
6455 if (dump_file && (dump_flags & TDF_GRAPH))
6457 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
6458 "in dot format:\n");
6459 dump_constraint_graph (dump_file);
6460 fprintf (dump_file, "\n\n");
6463 if (dump_file)
6464 fprintf (dump_file, "Solving graph\n");
6466 solve_graph (graph);
6468 if (dump_file && (dump_flags & TDF_GRAPH))
6470 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
6471 "in dot format:\n");
6472 dump_constraint_graph (dump_file);
6473 fprintf (dump_file, "\n\n");
6476 if (dump_file)
6477 dump_sa_points_to_info (dump_file);
6480 /* Create points-to sets for the current function. See the comments
6481 at the start of the file for an algorithmic overview. */
6483 static void
6484 compute_points_to_sets (void)
6486 basic_block bb;
6487 unsigned i;
6488 varinfo_t vi;
6490 timevar_push (TV_TREE_PTA);
6492 init_alias_vars ();
6494 intra_create_variable_infos ();
6496 /* Now walk all statements and build the constraint set. */
6497 FOR_EACH_BB (bb)
6499 gimple_stmt_iterator gsi;
6501 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6503 gimple phi = gsi_stmt (gsi);
6505 if (is_gimple_reg (gimple_phi_result (phi)))
6506 find_func_aliases (phi);
6509 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6511 gimple stmt = gsi_stmt (gsi);
6513 find_func_aliases (stmt);
6517 if (dump_file)
6519 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
6520 dump_constraints (dump_file, 0);
6523 /* From the constraints compute the points-to sets. */
6524 solve_constraints ();
6526 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6527 find_what_var_points_to (get_varinfo (escaped_id),
6528 &cfun->gimple_df->escaped);
6530 /* Make sure the ESCAPED solution (which is used as placeholder in
6531 other solutions) does not reference itself. This simplifies
6532 points-to solution queries. */
6533 cfun->gimple_df->escaped.escaped = 0;
6535 /* Mark escaped HEAP variables as global. */
6536 FOR_EACH_VEC_ELT (varinfo_t, varmap, i, vi)
6537 if (vi->is_heap_var
6538 && !vi->is_restrict_var
6539 && !vi->is_global_var)
6540 DECL_EXTERNAL (vi->decl) = vi->is_global_var
6541 = pt_solution_includes (&cfun->gimple_df->escaped, vi->decl);
6543 /* Compute the points-to sets for pointer SSA_NAMEs. */
6544 for (i = 0; i < num_ssa_names; ++i)
6546 tree ptr = ssa_name (i);
6547 if (ptr
6548 && POINTER_TYPE_P (TREE_TYPE (ptr)))
6549 find_what_p_points_to (ptr);
6552 /* Compute the call-used/clobbered sets. */
6553 FOR_EACH_BB (bb)
6555 gimple_stmt_iterator gsi;
6557 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6559 gimple stmt = gsi_stmt (gsi);
6560 struct pt_solution *pt;
6561 if (!is_gimple_call (stmt))
6562 continue;
6564 pt = gimple_call_use_set (stmt);
6565 if (gimple_call_flags (stmt) & ECF_CONST)
6566 memset (pt, 0, sizeof (struct pt_solution));
6567 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
6569 find_what_var_points_to (vi, pt);
6570 /* Escaped (and thus nonlocal) variables are always
6571 implicitly used by calls. */
6572 /* ??? ESCAPED can be empty even though NONLOCAL
6573 always escaped. */
6574 pt->nonlocal = 1;
6575 pt->escaped = 1;
6577 else
6579 /* If there is nothing special about this call then
6580 we have made everything that is used also escape. */
6581 *pt = cfun->gimple_df->escaped;
6582 pt->nonlocal = 1;
6585 pt = gimple_call_clobber_set (stmt);
6586 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
6587 memset (pt, 0, sizeof (struct pt_solution));
6588 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
6590 find_what_var_points_to (vi, pt);
6591 /* Escaped (and thus nonlocal) variables are always
6592 implicitly clobbered by calls. */
6593 /* ??? ESCAPED can be empty even though NONLOCAL
6594 always escaped. */
6595 pt->nonlocal = 1;
6596 pt->escaped = 1;
6598 else
6600 /* If there is nothing special about this call then
6601 we have made everything that is used also escape. */
6602 *pt = cfun->gimple_df->escaped;
6603 pt->nonlocal = 1;
6608 timevar_pop (TV_TREE_PTA);
6612 /* Delete created points-to sets. */
6614 static void
6615 delete_points_to_sets (void)
6617 unsigned int i;
6619 htab_delete (shared_bitmap_table);
6620 if (dump_file && (dump_flags & TDF_STATS))
6621 fprintf (dump_file, "Points to sets created:%d\n",
6622 stats.points_to_sets_created);
6624 pointer_map_destroy (vi_for_tree);
6625 pointer_map_destroy (call_stmt_vars);
6626 bitmap_obstack_release (&pta_obstack);
6627 VEC_free (constraint_t, heap, constraints);
6629 for (i = 0; i < graph->size; i++)
6630 VEC_free (constraint_t, heap, graph->complex[i]);
6631 free (graph->complex);
6633 free (graph->rep);
6634 free (graph->succs);
6635 free (graph->pe);
6636 free (graph->pe_rep);
6637 free (graph->indirect_cycles);
6638 free (graph);
6640 VEC_free (varinfo_t, heap, varmap);
6641 free_alloc_pool (variable_info_pool);
6642 free_alloc_pool (constraint_pool);
6644 obstack_free (&fake_var_decl_obstack, NULL);
6648 /* Compute points-to information for every SSA_NAME pointer in the
6649 current function and compute the transitive closure of escaped
6650 variables to re-initialize the call-clobber states of local variables. */
6652 unsigned int
6653 compute_may_aliases (void)
6655 if (cfun->gimple_df->ipa_pta)
6657 if (dump_file)
6659 fprintf (dump_file, "\nNot re-computing points-to information "
6660 "because IPA points-to information is available.\n\n");
6662 /* But still dump what we have remaining it. */
6663 dump_alias_info (dump_file);
6665 if (dump_flags & TDF_DETAILS)
6666 dump_referenced_vars (dump_file);
6669 return 0;
6672 /* For each pointer P_i, determine the sets of variables that P_i may
6673 point-to. Compute the reachability set of escaped and call-used
6674 variables. */
6675 compute_points_to_sets ();
6677 /* Debugging dumps. */
6678 if (dump_file)
6680 dump_alias_info (dump_file);
6682 if (dump_flags & TDF_DETAILS)
6683 dump_referenced_vars (dump_file);
6686 /* Deallocate memory used by aliasing data structures and the internal
6687 points-to solution. */
6688 delete_points_to_sets ();
6690 gcc_assert (!need_ssa_update_p (cfun));
6692 return 0;
6695 static bool
6696 gate_tree_pta (void)
6698 return flag_tree_pta;
6701 /* A dummy pass to cause points-to information to be computed via
6702 TODO_rebuild_alias. */
6704 struct gimple_opt_pass pass_build_alias =
6707 GIMPLE_PASS,
6708 "alias", /* name */
6709 gate_tree_pta, /* gate */
6710 NULL, /* execute */
6711 NULL, /* sub */
6712 NULL, /* next */
6713 0, /* static_pass_number */
6714 TV_NONE, /* tv_id */
6715 PROP_cfg | PROP_ssa, /* properties_required */
6716 0, /* properties_provided */
6717 0, /* properties_destroyed */
6718 0, /* todo_flags_start */
6719 TODO_rebuild_alias /* todo_flags_finish */
6723 /* A dummy pass to cause points-to information to be computed via
6724 TODO_rebuild_alias. */
6726 struct gimple_opt_pass pass_build_ealias =
6729 GIMPLE_PASS,
6730 "ealias", /* name */
6731 gate_tree_pta, /* gate */
6732 NULL, /* execute */
6733 NULL, /* sub */
6734 NULL, /* next */
6735 0, /* static_pass_number */
6736 TV_NONE, /* tv_id */
6737 PROP_cfg | PROP_ssa, /* properties_required */
6738 0, /* properties_provided */
6739 0, /* properties_destroyed */
6740 0, /* todo_flags_start */
6741 TODO_rebuild_alias /* todo_flags_finish */
6746 /* Return true if we should execute IPA PTA. */
6747 static bool
6748 gate_ipa_pta (void)
6750 return (optimize
6751 && flag_ipa_pta
6752 /* Don't bother doing anything if the program has errors. */
6753 && !seen_error ());
6756 /* IPA PTA solutions for ESCAPED. */
6757 struct pt_solution ipa_escaped_pt
6758 = { true, false, false, false, false, false, false, NULL };
6760 /* Associate node with varinfo DATA. Worker for
6761 cgraph_for_node_and_aliases. */
6762 static bool
6763 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
6765 if (node->alias || node->thunk.thunk_p)
6766 insert_vi_for_tree (node->decl, (varinfo_t)data);
6767 return false;
6770 /* Execute the driver for IPA PTA. */
6771 static unsigned int
6772 ipa_pta_execute (void)
6774 struct cgraph_node *node;
6775 struct varpool_node *var;
6776 int from;
6778 in_ipa_mode = 1;
6780 init_alias_vars ();
6782 if (dump_file && (dump_flags & TDF_DETAILS))
6784 dump_cgraph (dump_file);
6785 fprintf (dump_file, "\n");
6788 /* Build the constraints. */
6789 for (node = cgraph_nodes; node; node = node->next)
6791 varinfo_t vi;
6792 /* Nodes without a body are not interesting. Especially do not
6793 visit clones at this point for now - we get duplicate decls
6794 there for inline clones at least. */
6795 if (!cgraph_function_with_gimple_body_p (node))
6796 continue;
6798 gcc_assert (!node->clone_of);
6800 vi = create_function_info_for (node->decl,
6801 alias_get_name (node->decl));
6802 cgraph_for_node_and_aliases (node, associate_varinfo_to_alias, vi, true);
6805 /* Create constraints for global variables and their initializers. */
6806 for (var = varpool_nodes; var; var = var->next)
6808 if (var->alias)
6809 continue;
6811 get_vi_for_tree (var->decl);
6814 if (dump_file)
6816 fprintf (dump_file,
6817 "Generating constraints for global initializers\n\n");
6818 dump_constraints (dump_file, 0);
6819 fprintf (dump_file, "\n");
6821 from = VEC_length (constraint_t, constraints);
6823 for (node = cgraph_nodes; node; node = node->next)
6825 struct function *func;
6826 basic_block bb;
6827 tree old_func_decl;
6829 /* Nodes without a body are not interesting. */
6830 if (!cgraph_function_with_gimple_body_p (node))
6831 continue;
6833 if (dump_file)
6835 fprintf (dump_file,
6836 "Generating constraints for %s", cgraph_node_name (node));
6837 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
6838 fprintf (dump_file, " (%s)",
6839 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
6840 fprintf (dump_file, "\n");
6843 func = DECL_STRUCT_FUNCTION (node->decl);
6844 old_func_decl = current_function_decl;
6845 push_cfun (func);
6846 current_function_decl = node->decl;
6848 /* For externally visible or attribute used annotated functions use
6849 local constraints for their arguments.
6850 For local functions we see all callers and thus do not need initial
6851 constraints for parameters. */
6852 if (node->reachable_from_other_partition
6853 || node->local.externally_visible
6854 || node->needed)
6856 intra_create_variable_infos ();
6858 /* We also need to make function return values escape. Nothing
6859 escapes by returning from main though. */
6860 if (!MAIN_NAME_P (DECL_NAME (node->decl)))
6862 varinfo_t fi, rvi;
6863 fi = lookup_vi_for_tree (node->decl);
6864 rvi = first_vi_for_offset (fi, fi_result);
6865 if (rvi && rvi->offset == fi_result)
6867 struct constraint_expr includes;
6868 struct constraint_expr var;
6869 includes.var = escaped_id;
6870 includes.offset = 0;
6871 includes.type = SCALAR;
6872 var.var = rvi->id;
6873 var.offset = 0;
6874 var.type = SCALAR;
6875 process_constraint (new_constraint (includes, var));
6880 /* Build constriants for the function body. */
6881 FOR_EACH_BB_FN (bb, func)
6883 gimple_stmt_iterator gsi;
6885 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
6886 gsi_next (&gsi))
6888 gimple phi = gsi_stmt (gsi);
6890 if (is_gimple_reg (gimple_phi_result (phi)))
6891 find_func_aliases (phi);
6894 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6896 gimple stmt = gsi_stmt (gsi);
6898 find_func_aliases (stmt);
6899 find_func_clobbers (stmt);
6903 current_function_decl = old_func_decl;
6904 pop_cfun ();
6906 if (dump_file)
6908 fprintf (dump_file, "\n");
6909 dump_constraints (dump_file, from);
6910 fprintf (dump_file, "\n");
6912 from = VEC_length (constraint_t, constraints);
6915 /* From the constraints compute the points-to sets. */
6916 solve_constraints ();
6918 /* Compute the global points-to sets for ESCAPED.
6919 ??? Note that the computed escape set is not correct
6920 for the whole unit as we fail to consider graph edges to
6921 externally visible functions. */
6922 find_what_var_points_to (get_varinfo (escaped_id), &ipa_escaped_pt);
6924 /* Make sure the ESCAPED solution (which is used as placeholder in
6925 other solutions) does not reference itself. This simplifies
6926 points-to solution queries. */
6927 ipa_escaped_pt.ipa_escaped = 0;
6929 /* Assign the points-to sets to the SSA names in the unit. */
6930 for (node = cgraph_nodes; node; node = node->next)
6932 tree ptr;
6933 struct function *fn;
6934 unsigned i;
6935 varinfo_t fi;
6936 basic_block bb;
6937 struct pt_solution uses, clobbers;
6938 struct cgraph_edge *e;
6940 /* Nodes without a body are not interesting. */
6941 if (!cgraph_function_with_gimple_body_p (node))
6942 continue;
6944 fn = DECL_STRUCT_FUNCTION (node->decl);
6946 /* Compute the points-to sets for pointer SSA_NAMEs. */
6947 FOR_EACH_VEC_ELT (tree, fn->gimple_df->ssa_names, i, ptr)
6949 if (ptr
6950 && POINTER_TYPE_P (TREE_TYPE (ptr)))
6951 find_what_p_points_to (ptr);
6954 /* Compute the call-use and call-clobber sets for all direct calls. */
6955 fi = lookup_vi_for_tree (node->decl);
6956 gcc_assert (fi->is_fn_info);
6957 find_what_var_points_to (first_vi_for_offset (fi, fi_clobbers),
6958 &clobbers);
6959 find_what_var_points_to (first_vi_for_offset (fi, fi_uses), &uses);
6960 for (e = node->callers; e; e = e->next_caller)
6962 if (!e->call_stmt)
6963 continue;
6965 *gimple_call_clobber_set (e->call_stmt) = clobbers;
6966 *gimple_call_use_set (e->call_stmt) = uses;
6969 /* Compute the call-use and call-clobber sets for indirect calls
6970 and calls to external functions. */
6971 FOR_EACH_BB_FN (bb, fn)
6973 gimple_stmt_iterator gsi;
6975 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6977 gimple stmt = gsi_stmt (gsi);
6978 struct pt_solution *pt;
6979 varinfo_t vi;
6980 tree decl;
6982 if (!is_gimple_call (stmt))
6983 continue;
6985 /* Handle direct calls to external functions. */
6986 decl = gimple_call_fndecl (stmt);
6987 if (decl
6988 && (!(fi = lookup_vi_for_tree (decl))
6989 || !fi->is_fn_info))
6991 pt = gimple_call_use_set (stmt);
6992 if (gimple_call_flags (stmt) & ECF_CONST)
6993 memset (pt, 0, sizeof (struct pt_solution));
6994 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
6996 find_what_var_points_to (vi, pt);
6997 /* Escaped (and thus nonlocal) variables are always
6998 implicitly used by calls. */
6999 /* ??? ESCAPED can be empty even though NONLOCAL
7000 always escaped. */
7001 pt->nonlocal = 1;
7002 pt->ipa_escaped = 1;
7004 else
7006 /* If there is nothing special about this call then
7007 we have made everything that is used also escape. */
7008 *pt = ipa_escaped_pt;
7009 pt->nonlocal = 1;
7012 pt = gimple_call_clobber_set (stmt);
7013 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7014 memset (pt, 0, sizeof (struct pt_solution));
7015 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7017 find_what_var_points_to (vi, pt);
7018 /* Escaped (and thus nonlocal) variables are always
7019 implicitly clobbered by calls. */
7020 /* ??? ESCAPED can be empty even though NONLOCAL
7021 always escaped. */
7022 pt->nonlocal = 1;
7023 pt->ipa_escaped = 1;
7025 else
7027 /* If there is nothing special about this call then
7028 we have made everything that is used also escape. */
7029 *pt = ipa_escaped_pt;
7030 pt->nonlocal = 1;
7034 /* Handle indirect calls. */
7035 if (!decl
7036 && (fi = get_fi_for_callee (stmt)))
7038 /* We need to accumulate all clobbers/uses of all possible
7039 callees. */
7040 fi = get_varinfo (find (fi->id));
7041 /* If we cannot constrain the set of functions we'll end up
7042 calling we end up using/clobbering everything. */
7043 if (bitmap_bit_p (fi->solution, anything_id)
7044 || bitmap_bit_p (fi->solution, nonlocal_id)
7045 || bitmap_bit_p (fi->solution, escaped_id))
7047 pt_solution_reset (gimple_call_clobber_set (stmt));
7048 pt_solution_reset (gimple_call_use_set (stmt));
7050 else
7052 bitmap_iterator bi;
7053 unsigned i;
7054 struct pt_solution *uses, *clobbers;
7056 uses = gimple_call_use_set (stmt);
7057 clobbers = gimple_call_clobber_set (stmt);
7058 memset (uses, 0, sizeof (struct pt_solution));
7059 memset (clobbers, 0, sizeof (struct pt_solution));
7060 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
7062 struct pt_solution sol;
7064 vi = get_varinfo (i);
7065 if (!vi->is_fn_info)
7067 /* ??? We could be more precise here? */
7068 uses->nonlocal = 1;
7069 uses->ipa_escaped = 1;
7070 clobbers->nonlocal = 1;
7071 clobbers->ipa_escaped = 1;
7072 continue;
7075 if (!uses->anything)
7077 find_what_var_points_to
7078 (first_vi_for_offset (vi, fi_uses), &sol);
7079 pt_solution_ior_into (uses, &sol);
7081 if (!clobbers->anything)
7083 find_what_var_points_to
7084 (first_vi_for_offset (vi, fi_clobbers), &sol);
7085 pt_solution_ior_into (clobbers, &sol);
7093 fn->gimple_df->ipa_pta = true;
7096 delete_points_to_sets ();
7098 in_ipa_mode = 0;
7100 return 0;
7103 struct simple_ipa_opt_pass pass_ipa_pta =
7106 SIMPLE_IPA_PASS,
7107 "pta", /* name */
7108 gate_ipa_pta, /* gate */
7109 ipa_pta_execute, /* execute */
7110 NULL, /* sub */
7111 NULL, /* next */
7112 0, /* static_pass_number */
7113 TV_IPA_PTA, /* tv_id */
7114 0, /* properties_required */
7115 0, /* properties_provided */
7116 0, /* properties_destroyed */
7117 0, /* todo_flags_start */
7118 TODO_update_ssa /* todo_flags_finish */