* many: Replace uses of __GXX_EXPERIMENTAL_CXX0X__ with __cplusplus.
[official-gcc.git] / gcc / tree-ssa-structalias.c
blobe3fdeb6972cba9a27ee4be063075920ad0e0c2b8
1 /* Tree based points-to analysis
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Daniel Berlin <dberlin@dberlin.org>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "ggc.h"
27 #include "obstack.h"
28 #include "bitmap.h"
29 #include "flags.h"
30 #include "basic-block.h"
31 #include "tree.h"
32 #include "tree-flow.h"
33 #include "tree-inline.h"
34 #include "diagnostic-core.h"
35 #include "gimple.h"
36 #include "hashtab.h"
37 #include "function.h"
38 #include "cgraph.h"
39 #include "tree-pass.h"
40 #include "alloc-pool.h"
41 #include "splay-tree.h"
42 #include "params.h"
43 #include "cgraph.h"
44 #include "alias.h"
45 #include "pointer-set.h"
47 /* The idea behind this analyzer is to generate set constraints from the
48 program, then solve the resulting constraints in order to generate the
49 points-to sets.
51 Set constraints are a way of modeling program analysis problems that
52 involve sets. They consist of an inclusion constraint language,
53 describing the variables (each variable is a set) and operations that
54 are involved on the variables, and a set of rules that derive facts
55 from these operations. To solve a system of set constraints, you derive
56 all possible facts under the rules, which gives you the correct sets
57 as a consequence.
59 See "Efficient Field-sensitive pointer analysis for C" by "David
60 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
61 http://citeseer.ist.psu.edu/pearce04efficient.html
63 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
64 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
65 http://citeseer.ist.psu.edu/heintze01ultrafast.html
67 There are three types of real constraint expressions, DEREF,
68 ADDRESSOF, and SCALAR. Each constraint expression consists
69 of a constraint type, a variable, and an offset.
71 SCALAR is a constraint expression type used to represent x, whether
72 it appears on the LHS or the RHS of a statement.
73 DEREF is a constraint expression type used to represent *x, whether
74 it appears on the LHS or the RHS of a statement.
75 ADDRESSOF is a constraint expression used to represent &x, whether
76 it appears on the LHS or the RHS of a statement.
78 Each pointer variable in the program is assigned an integer id, and
79 each field of a structure variable is assigned an integer id as well.
81 Structure variables are linked to their list of fields through a "next
82 field" in each variable that points to the next field in offset
83 order.
84 Each variable for a structure field has
86 1. "size", that tells the size in bits of that field.
87 2. "fullsize, that tells the size in bits of the entire structure.
88 3. "offset", that tells the offset in bits from the beginning of the
89 structure to this field.
91 Thus,
92 struct f
94 int a;
95 int b;
96 } foo;
97 int *bar;
99 looks like
101 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
102 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
103 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
106 In order to solve the system of set constraints, the following is
107 done:
109 1. Each constraint variable x has a solution set associated with it,
110 Sol(x).
112 2. Constraints are separated into direct, copy, and complex.
113 Direct constraints are ADDRESSOF constraints that require no extra
114 processing, such as P = &Q
115 Copy constraints are those of the form P = Q.
116 Complex constraints are all the constraints involving dereferences
117 and offsets (including offsetted copies).
119 3. All direct constraints of the form P = &Q are processed, such
120 that Q is added to Sol(P)
122 4. All complex constraints for a given constraint variable are stored in a
123 linked list attached to that variable's node.
125 5. A directed graph is built out of the copy constraints. Each
126 constraint variable is a node in the graph, and an edge from
127 Q to P is added for each copy constraint of the form P = Q
129 6. The graph is then walked, and solution sets are
130 propagated along the copy edges, such that an edge from Q to P
131 causes Sol(P) <- Sol(P) union Sol(Q).
133 7. As we visit each node, all complex constraints associated with
134 that node are processed by adding appropriate copy edges to the graph, or the
135 appropriate variables to the solution set.
137 8. The process of walking the graph is iterated until no solution
138 sets change.
140 Prior to walking the graph in steps 6 and 7, We perform static
141 cycle elimination on the constraint graph, as well
142 as off-line variable substitution.
144 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
145 on and turned into anything), but isn't. You can just see what offset
146 inside the pointed-to struct it's going to access.
148 TODO: Constant bounded arrays can be handled as if they were structs of the
149 same number of elements.
151 TODO: Modeling heap and incoming pointers becomes much better if we
152 add fields to them as we discover them, which we could do.
154 TODO: We could handle unions, but to be honest, it's probably not
155 worth the pain or slowdown. */
157 /* IPA-PTA optimizations possible.
159 When the indirect function called is ANYTHING we can add disambiguation
160 based on the function signatures (or simply the parameter count which
161 is the varinfo size). We also do not need to consider functions that
162 do not have their address taken.
164 The is_global_var bit which marks escape points is overly conservative
165 in IPA mode. Split it to is_escape_point and is_global_var - only
166 externally visible globals are escape points in IPA mode. This is
167 also needed to fix the pt_solution_includes_global predicate
168 (and thus ptr_deref_may_alias_global_p).
170 The way we introduce DECL_PT_UID to avoid fixing up all points-to
171 sets in the translation unit when we copy a DECL during inlining
172 pessimizes precision. The advantage is that the DECL_PT_UID keeps
173 compile-time and memory usage overhead low - the points-to sets
174 do not grow or get unshared as they would during a fixup phase.
175 An alternative solution is to delay IPA PTA until after all
176 inlining transformations have been applied.
178 The way we propagate clobber/use information isn't optimized.
179 It should use a new complex constraint that properly filters
180 out local variables of the callee (though that would make
181 the sets invalid after inlining). OTOH we might as well
182 admit defeat to WHOPR and simply do all the clobber/use analysis
183 and propagation after PTA finished but before we threw away
184 points-to information for memory variables. WHOPR and PTA
185 do not play along well anyway - the whole constraint solving
186 would need to be done in WPA phase and it will be very interesting
187 to apply the results to local SSA names during LTRANS phase.
189 We probably should compute a per-function unit-ESCAPE solution
190 propagating it simply like the clobber / uses solutions. The
191 solution can go alongside the non-IPA espaced solution and be
192 used to query which vars escape the unit through a function.
194 We never put function decls in points-to sets so we do not
195 keep the set of called functions for indirect calls.
197 And probably more. */
199 static bool use_field_sensitive = true;
200 static int in_ipa_mode = 0;
202 /* Used for predecessor bitmaps. */
203 static bitmap_obstack predbitmap_obstack;
205 /* Used for points-to sets. */
206 static bitmap_obstack pta_obstack;
208 /* Used for oldsolution members of variables. */
209 static bitmap_obstack oldpta_obstack;
211 /* Used for per-solver-iteration bitmaps. */
212 static bitmap_obstack iteration_obstack;
214 static unsigned int create_variable_info_for (tree, const char *);
215 typedef struct constraint_graph *constraint_graph_t;
216 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
218 struct constraint;
219 typedef struct constraint *constraint_t;
221 DEF_VEC_P(constraint_t);
222 DEF_VEC_ALLOC_P(constraint_t,heap);
224 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
225 if (a) \
226 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
228 static struct constraint_stats
230 unsigned int total_vars;
231 unsigned int nonpointer_vars;
232 unsigned int unified_vars_static;
233 unsigned int unified_vars_dynamic;
234 unsigned int iterations;
235 unsigned int num_edges;
236 unsigned int num_implicit_edges;
237 unsigned int points_to_sets_created;
238 } stats;
240 struct variable_info
242 /* ID of this variable */
243 unsigned int id;
245 /* True if this is a variable created by the constraint analysis, such as
246 heap variables and constraints we had to break up. */
247 unsigned int is_artificial_var : 1;
249 /* True if this is a special variable whose solution set should not be
250 changed. */
251 unsigned int is_special_var : 1;
253 /* True for variables whose size is not known or variable. */
254 unsigned int is_unknown_size_var : 1;
256 /* True for (sub-)fields that represent a whole variable. */
257 unsigned int is_full_var : 1;
259 /* True if this is a heap variable. */
260 unsigned int is_heap_var : 1;
262 /* True if this field may contain pointers. */
263 unsigned int may_have_pointers : 1;
265 /* True if this field has only restrict qualified pointers. */
266 unsigned int only_restrict_pointers : 1;
268 /* True if this represents a global variable. */
269 unsigned int is_global_var : 1;
271 /* True if this represents a IPA function info. */
272 unsigned int is_fn_info : 1;
274 /* A link to the variable for the next field in this structure. */
275 struct variable_info *next;
277 /* Offset of this variable, in bits, from the base variable */
278 unsigned HOST_WIDE_INT offset;
280 /* Size of the variable, in bits. */
281 unsigned HOST_WIDE_INT size;
283 /* Full size of the base variable, in bits. */
284 unsigned HOST_WIDE_INT fullsize;
286 /* Name of this variable */
287 const char *name;
289 /* Tree that this variable is associated with. */
290 tree decl;
292 /* Points-to set for this variable. */
293 bitmap solution;
295 /* Old points-to set for this variable. */
296 bitmap oldsolution;
298 typedef struct variable_info *varinfo_t;
300 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
301 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
302 unsigned HOST_WIDE_INT);
303 static varinfo_t lookup_vi_for_tree (tree);
304 static inline bool type_can_have_subvars (const_tree);
306 /* Pool of variable info structures. */
307 static alloc_pool variable_info_pool;
309 DEF_VEC_P(varinfo_t);
311 DEF_VEC_ALLOC_P(varinfo_t, heap);
313 /* Table of variable info structures for constraint variables.
314 Indexed directly by variable info id. */
315 static VEC(varinfo_t,heap) *varmap;
317 /* Return the varmap element N */
319 static inline varinfo_t
320 get_varinfo (unsigned int n)
322 return VEC_index (varinfo_t, varmap, n);
325 /* Static IDs for the special variables. */
326 enum { nothing_id = 0, anything_id = 1, readonly_id = 2,
327 escaped_id = 3, nonlocal_id = 4,
328 storedanything_id = 5, integer_id = 6 };
330 /* Return a new variable info structure consisting for a variable
331 named NAME, and using constraint graph node NODE. Append it
332 to the vector of variable info structures. */
334 static varinfo_t
335 new_var_info (tree t, const char *name)
337 unsigned index = VEC_length (varinfo_t, varmap);
338 varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
340 ret->id = index;
341 ret->name = name;
342 ret->decl = t;
343 /* Vars without decl are artificial and do not have sub-variables. */
344 ret->is_artificial_var = (t == NULL_TREE);
345 ret->is_special_var = false;
346 ret->is_unknown_size_var = false;
347 ret->is_full_var = (t == NULL_TREE);
348 ret->is_heap_var = false;
349 ret->may_have_pointers = true;
350 ret->only_restrict_pointers = false;
351 ret->is_global_var = (t == NULL_TREE);
352 ret->is_fn_info = false;
353 if (t && DECL_P (t))
354 ret->is_global_var = (is_global_var (t)
355 /* We have to treat even local register variables
356 as escape points. */
357 || (TREE_CODE (t) == VAR_DECL
358 && DECL_HARD_REGISTER (t)));
359 ret->solution = BITMAP_ALLOC (&pta_obstack);
360 ret->oldsolution = NULL;
361 ret->next = NULL;
363 stats.total_vars++;
365 VEC_safe_push (varinfo_t, heap, varmap, ret);
367 return ret;
371 /* A map mapping call statements to per-stmt variables for uses
372 and clobbers specific to the call. */
373 struct pointer_map_t *call_stmt_vars;
375 /* Lookup or create the variable for the call statement CALL. */
377 static varinfo_t
378 get_call_vi (gimple call)
380 void **slot_p;
381 varinfo_t vi, vi2;
383 slot_p = pointer_map_insert (call_stmt_vars, call);
384 if (*slot_p)
385 return (varinfo_t) *slot_p;
387 vi = new_var_info (NULL_TREE, "CALLUSED");
388 vi->offset = 0;
389 vi->size = 1;
390 vi->fullsize = 2;
391 vi->is_full_var = true;
393 vi->next = vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED");
394 vi2->offset = 1;
395 vi2->size = 1;
396 vi2->fullsize = 2;
397 vi2->is_full_var = true;
399 *slot_p = (void *) vi;
400 return vi;
403 /* Lookup the variable for the call statement CALL representing
404 the uses. Returns NULL if there is nothing special about this call. */
406 static varinfo_t
407 lookup_call_use_vi (gimple call)
409 void **slot_p;
411 slot_p = pointer_map_contains (call_stmt_vars, call);
412 if (slot_p)
413 return (varinfo_t) *slot_p;
415 return NULL;
418 /* Lookup the variable for the call statement CALL representing
419 the clobbers. Returns NULL if there is nothing special about this call. */
421 static varinfo_t
422 lookup_call_clobber_vi (gimple call)
424 varinfo_t uses = lookup_call_use_vi (call);
425 if (!uses)
426 return NULL;
428 return uses->next;
431 /* Lookup or create the variable for the call statement CALL representing
432 the uses. */
434 static varinfo_t
435 get_call_use_vi (gimple call)
437 return get_call_vi (call);
440 /* Lookup or create the variable for the call statement CALL representing
441 the clobbers. */
443 static varinfo_t ATTRIBUTE_UNUSED
444 get_call_clobber_vi (gimple call)
446 return get_call_vi (call)->next;
450 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
452 /* An expression that appears in a constraint. */
454 struct constraint_expr
456 /* Constraint type. */
457 constraint_expr_type type;
459 /* Variable we are referring to in the constraint. */
460 unsigned int var;
462 /* Offset, in bits, of this constraint from the beginning of
463 variables it ends up referring to.
465 IOW, in a deref constraint, we would deref, get the result set,
466 then add OFFSET to each member. */
467 HOST_WIDE_INT offset;
470 /* Use 0x8000... as special unknown offset. */
471 #define UNKNOWN_OFFSET ((HOST_WIDE_INT)-1 << (HOST_BITS_PER_WIDE_INT-1))
473 typedef struct constraint_expr ce_s;
474 DEF_VEC_O(ce_s);
475 DEF_VEC_ALLOC_O(ce_s, heap);
476 static void get_constraint_for_1 (tree, VEC(ce_s, heap) **, bool, bool);
477 static void get_constraint_for (tree, VEC(ce_s, heap) **);
478 static void get_constraint_for_rhs (tree, VEC(ce_s, heap) **);
479 static void do_deref (VEC (ce_s, heap) **);
481 /* Our set constraints are made up of two constraint expressions, one
482 LHS, and one RHS.
484 As described in the introduction, our set constraints each represent an
485 operation between set valued variables.
487 struct constraint
489 struct constraint_expr lhs;
490 struct constraint_expr rhs;
493 /* List of constraints that we use to build the constraint graph from. */
495 static VEC(constraint_t,heap) *constraints;
496 static alloc_pool constraint_pool;
498 /* The constraint graph is represented as an array of bitmaps
499 containing successor nodes. */
501 struct constraint_graph
503 /* Size of this graph, which may be different than the number of
504 nodes in the variable map. */
505 unsigned int size;
507 /* Explicit successors of each node. */
508 bitmap *succs;
510 /* Implicit predecessors of each node (Used for variable
511 substitution). */
512 bitmap *implicit_preds;
514 /* Explicit predecessors of each node (Used for variable substitution). */
515 bitmap *preds;
517 /* Indirect cycle representatives, or -1 if the node has no indirect
518 cycles. */
519 int *indirect_cycles;
521 /* Representative node for a node. rep[a] == a unless the node has
522 been unified. */
523 unsigned int *rep;
525 /* Equivalence class representative for a label. This is used for
526 variable substitution. */
527 int *eq_rep;
529 /* Pointer equivalence label for a node. All nodes with the same
530 pointer equivalence label can be unified together at some point
531 (either during constraint optimization or after the constraint
532 graph is built). */
533 unsigned int *pe;
535 /* Pointer equivalence representative for a label. This is used to
536 handle nodes that are pointer equivalent but not location
537 equivalent. We can unite these once the addressof constraints
538 are transformed into initial points-to sets. */
539 int *pe_rep;
541 /* Pointer equivalence label for each node, used during variable
542 substitution. */
543 unsigned int *pointer_label;
545 /* Location equivalence label for each node, used during location
546 equivalence finding. */
547 unsigned int *loc_label;
549 /* Pointed-by set for each node, used during location equivalence
550 finding. This is pointed-by rather than pointed-to, because it
551 is constructed using the predecessor graph. */
552 bitmap *pointed_by;
554 /* Points to sets for pointer equivalence. This is *not* the actual
555 points-to sets for nodes. */
556 bitmap *points_to;
558 /* Bitmap of nodes where the bit is set if the node is a direct
559 node. Used for variable substitution. */
560 sbitmap direct_nodes;
562 /* Bitmap of nodes where the bit is set if the node is address
563 taken. Used for variable substitution. */
564 bitmap address_taken;
566 /* Vector of complex constraints for each graph node. Complex
567 constraints are those involving dereferences or offsets that are
568 not 0. */
569 VEC(constraint_t,heap) **complex;
572 static constraint_graph_t graph;
574 /* During variable substitution and the offline version of indirect
575 cycle finding, we create nodes to represent dereferences and
576 address taken constraints. These represent where these start and
577 end. */
578 #define FIRST_REF_NODE (VEC_length (varinfo_t, varmap))
579 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
581 /* Return the representative node for NODE, if NODE has been unioned
582 with another NODE.
583 This function performs path compression along the way to finding
584 the representative. */
586 static unsigned int
587 find (unsigned int node)
589 gcc_assert (node < graph->size);
590 if (graph->rep[node] != node)
591 return graph->rep[node] = find (graph->rep[node]);
592 return node;
595 /* Union the TO and FROM nodes to the TO nodes.
596 Note that at some point in the future, we may want to do
597 union-by-rank, in which case we are going to have to return the
598 node we unified to. */
600 static bool
601 unite (unsigned int to, unsigned int from)
603 gcc_assert (to < graph->size && from < graph->size);
604 if (to != from && graph->rep[from] != to)
606 graph->rep[from] = to;
607 return true;
609 return false;
612 /* Create a new constraint consisting of LHS and RHS expressions. */
614 static constraint_t
615 new_constraint (const struct constraint_expr lhs,
616 const struct constraint_expr rhs)
618 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
619 ret->lhs = lhs;
620 ret->rhs = rhs;
621 return ret;
624 /* Print out constraint C to FILE. */
626 static void
627 dump_constraint (FILE *file, constraint_t c)
629 if (c->lhs.type == ADDRESSOF)
630 fprintf (file, "&");
631 else if (c->lhs.type == DEREF)
632 fprintf (file, "*");
633 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
634 if (c->lhs.offset == UNKNOWN_OFFSET)
635 fprintf (file, " + UNKNOWN");
636 else if (c->lhs.offset != 0)
637 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
638 fprintf (file, " = ");
639 if (c->rhs.type == ADDRESSOF)
640 fprintf (file, "&");
641 else if (c->rhs.type == DEREF)
642 fprintf (file, "*");
643 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
644 if (c->rhs.offset == UNKNOWN_OFFSET)
645 fprintf (file, " + UNKNOWN");
646 else if (c->rhs.offset != 0)
647 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
651 void debug_constraint (constraint_t);
652 void debug_constraints (void);
653 void debug_constraint_graph (void);
654 void debug_solution_for_var (unsigned int);
655 void debug_sa_points_to_info (void);
657 /* Print out constraint C to stderr. */
659 DEBUG_FUNCTION void
660 debug_constraint (constraint_t c)
662 dump_constraint (stderr, c);
663 fprintf (stderr, "\n");
666 /* Print out all constraints to FILE */
668 static void
669 dump_constraints (FILE *file, int from)
671 int i;
672 constraint_t c;
673 for (i = from; VEC_iterate (constraint_t, constraints, i, c); i++)
674 if (c)
676 dump_constraint (file, c);
677 fprintf (file, "\n");
681 /* Print out all constraints to stderr. */
683 DEBUG_FUNCTION void
684 debug_constraints (void)
686 dump_constraints (stderr, 0);
689 /* Print the constraint graph in dot format. */
691 static void
692 dump_constraint_graph (FILE *file)
694 unsigned int i;
696 /* Only print the graph if it has already been initialized: */
697 if (!graph)
698 return;
700 /* Prints the header of the dot file: */
701 fprintf (file, "strict digraph {\n");
702 fprintf (file, " node [\n shape = box\n ]\n");
703 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
704 fprintf (file, "\n // List of nodes and complex constraints in "
705 "the constraint graph:\n");
707 /* The next lines print the nodes in the graph together with the
708 complex constraints attached to them. */
709 for (i = 0; i < graph->size; i++)
711 if (find (i) != i)
712 continue;
713 if (i < FIRST_REF_NODE)
714 fprintf (file, "\"%s\"", get_varinfo (i)->name);
715 else
716 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
717 if (graph->complex[i])
719 unsigned j;
720 constraint_t c;
721 fprintf (file, " [label=\"\\N\\n");
722 for (j = 0; VEC_iterate (constraint_t, graph->complex[i], j, c); ++j)
724 dump_constraint (file, c);
725 fprintf (file, "\\l");
727 fprintf (file, "\"]");
729 fprintf (file, ";\n");
732 /* Go over the edges. */
733 fprintf (file, "\n // Edges in the constraint graph:\n");
734 for (i = 0; i < graph->size; i++)
736 unsigned j;
737 bitmap_iterator bi;
738 if (find (i) != i)
739 continue;
740 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
742 unsigned to = find (j);
743 if (i == to)
744 continue;
745 if (i < FIRST_REF_NODE)
746 fprintf (file, "\"%s\"", get_varinfo (i)->name);
747 else
748 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
749 fprintf (file, " -> ");
750 if (to < FIRST_REF_NODE)
751 fprintf (file, "\"%s\"", get_varinfo (to)->name);
752 else
753 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name);
754 fprintf (file, ";\n");
758 /* Prints the tail of the dot file. */
759 fprintf (file, "}\n");
762 /* Print out the constraint graph to stderr. */
764 DEBUG_FUNCTION void
765 debug_constraint_graph (void)
767 dump_constraint_graph (stderr);
770 /* SOLVER FUNCTIONS
772 The solver is a simple worklist solver, that works on the following
773 algorithm:
775 sbitmap changed_nodes = all zeroes;
776 changed_count = 0;
777 For each node that is not already collapsed:
778 changed_count++;
779 set bit in changed nodes
781 while (changed_count > 0)
783 compute topological ordering for constraint graph
785 find and collapse cycles in the constraint graph (updating
786 changed if necessary)
788 for each node (n) in the graph in topological order:
789 changed_count--;
791 Process each complex constraint associated with the node,
792 updating changed if necessary.
794 For each outgoing edge from n, propagate the solution from n to
795 the destination of the edge, updating changed as necessary.
797 } */
799 /* Return true if two constraint expressions A and B are equal. */
801 static bool
802 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
804 return a.type == b.type && a.var == b.var && a.offset == b.offset;
807 /* Return true if constraint expression A is less than constraint expression
808 B. This is just arbitrary, but consistent, in order to give them an
809 ordering. */
811 static bool
812 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
814 if (a.type == b.type)
816 if (a.var == b.var)
817 return a.offset < b.offset;
818 else
819 return a.var < b.var;
821 else
822 return a.type < b.type;
825 /* Return true if constraint A is less than constraint B. This is just
826 arbitrary, but consistent, in order to give them an ordering. */
828 static bool
829 constraint_less (const constraint_t &a, const constraint_t &b)
831 if (constraint_expr_less (a->lhs, b->lhs))
832 return true;
833 else if (constraint_expr_less (b->lhs, a->lhs))
834 return false;
835 else
836 return constraint_expr_less (a->rhs, b->rhs);
839 /* Return true if two constraints A and B are equal. */
841 static bool
842 constraint_equal (struct constraint a, struct constraint b)
844 return constraint_expr_equal (a.lhs, b.lhs)
845 && constraint_expr_equal (a.rhs, b.rhs);
849 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
851 static constraint_t
852 constraint_vec_find (VEC(constraint_t,heap) *vec,
853 struct constraint lookfor)
855 unsigned int place;
856 constraint_t found;
858 if (vec == NULL)
859 return NULL;
861 place = VEC_lower_bound (constraint_t, vec, &lookfor, constraint_less);
862 if (place >= VEC_length (constraint_t, vec))
863 return NULL;
864 found = VEC_index (constraint_t, vec, place);
865 if (!constraint_equal (*found, lookfor))
866 return NULL;
867 return found;
870 /* Union two constraint vectors, TO and FROM. Put the result in TO. */
872 static void
873 constraint_set_union (VEC(constraint_t,heap) **to,
874 VEC(constraint_t,heap) **from)
876 int i;
877 constraint_t c;
879 FOR_EACH_VEC_ELT (constraint_t, *from, i, c)
881 if (constraint_vec_find (*to, *c) == NULL)
883 unsigned int place = VEC_lower_bound (constraint_t, *to, c,
884 constraint_less);
885 VEC_safe_insert (constraint_t, heap, *to, place, c);
890 /* Expands the solution in SET to all sub-fields of variables included.
891 Union the expanded result into RESULT. */
893 static void
894 solution_set_expand (bitmap result, bitmap set)
896 bitmap_iterator bi;
897 bitmap vars = NULL;
898 unsigned j;
900 /* In a first pass record all variables we need to add all
901 sub-fields off. This avoids quadratic behavior. */
902 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
904 varinfo_t v = get_varinfo (j);
905 if (v->is_artificial_var
906 || v->is_full_var)
907 continue;
908 v = lookup_vi_for_tree (v->decl);
909 if (vars == NULL)
910 vars = BITMAP_ALLOC (NULL);
911 bitmap_set_bit (vars, v->id);
914 /* In the second pass now do the addition to the solution and
915 to speed up solving add it to the delta as well. */
916 if (vars != NULL)
918 EXECUTE_IF_SET_IN_BITMAP (vars, 0, j, bi)
920 varinfo_t v = get_varinfo (j);
921 for (; v != NULL; v = v->next)
922 bitmap_set_bit (result, v->id);
924 BITMAP_FREE (vars);
928 /* Take a solution set SET, add OFFSET to each member of the set, and
929 overwrite SET with the result when done. */
931 static void
932 solution_set_add (bitmap set, HOST_WIDE_INT offset)
934 bitmap result = BITMAP_ALLOC (&iteration_obstack);
935 unsigned int i;
936 bitmap_iterator bi;
938 /* If the offset is unknown we have to expand the solution to
939 all subfields. */
940 if (offset == UNKNOWN_OFFSET)
942 solution_set_expand (set, set);
943 return;
946 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
948 varinfo_t vi = get_varinfo (i);
950 /* If this is a variable with just one field just set its bit
951 in the result. */
952 if (vi->is_artificial_var
953 || vi->is_unknown_size_var
954 || vi->is_full_var)
955 bitmap_set_bit (result, i);
956 else
958 unsigned HOST_WIDE_INT fieldoffset = vi->offset + offset;
960 /* If the offset makes the pointer point to before the
961 variable use offset zero for the field lookup. */
962 if (offset < 0
963 && fieldoffset > vi->offset)
964 fieldoffset = 0;
966 if (offset != 0)
967 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
969 bitmap_set_bit (result, vi->id);
970 /* If the result is not exactly at fieldoffset include the next
971 field as well. See get_constraint_for_ptr_offset for more
972 rationale. */
973 if (vi->offset != fieldoffset
974 && vi->next != NULL)
975 bitmap_set_bit (result, vi->next->id);
979 bitmap_copy (set, result);
980 BITMAP_FREE (result);
983 /* Union solution sets TO and FROM, and add INC to each member of FROM in the
984 process. */
986 static bool
987 set_union_with_increment (bitmap to, bitmap from, HOST_WIDE_INT inc)
989 if (inc == 0)
990 return bitmap_ior_into (to, from);
991 else
993 bitmap tmp;
994 bool res;
996 tmp = BITMAP_ALLOC (&iteration_obstack);
997 bitmap_copy (tmp, from);
998 solution_set_add (tmp, inc);
999 res = bitmap_ior_into (to, tmp);
1000 BITMAP_FREE (tmp);
1001 return res;
1005 /* Insert constraint C into the list of complex constraints for graph
1006 node VAR. */
1008 static void
1009 insert_into_complex (constraint_graph_t graph,
1010 unsigned int var, constraint_t c)
1012 VEC (constraint_t, heap) *complex = graph->complex[var];
1013 unsigned int place = VEC_lower_bound (constraint_t, complex, c,
1014 constraint_less);
1016 /* Only insert constraints that do not already exist. */
1017 if (place >= VEC_length (constraint_t, complex)
1018 || !constraint_equal (*c, *VEC_index (constraint_t, complex, place)))
1019 VEC_safe_insert (constraint_t, heap, graph->complex[var], place, c);
1023 /* Condense two variable nodes into a single variable node, by moving
1024 all associated info from SRC to TO. */
1026 static void
1027 merge_node_constraints (constraint_graph_t graph, unsigned int to,
1028 unsigned int from)
1030 unsigned int i;
1031 constraint_t c;
1033 gcc_assert (find (from) == to);
1035 /* Move all complex constraints from src node into to node */
1036 FOR_EACH_VEC_ELT (constraint_t, graph->complex[from], i, c)
1038 /* In complex constraints for node src, we may have either
1039 a = *src, and *src = a, or an offseted constraint which are
1040 always added to the rhs node's constraints. */
1042 if (c->rhs.type == DEREF)
1043 c->rhs.var = to;
1044 else if (c->lhs.type == DEREF)
1045 c->lhs.var = to;
1046 else
1047 c->rhs.var = to;
1049 constraint_set_union (&graph->complex[to], &graph->complex[from]);
1050 VEC_free (constraint_t, heap, graph->complex[from]);
1051 graph->complex[from] = NULL;
1055 /* Remove edges involving NODE from GRAPH. */
1057 static void
1058 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
1060 if (graph->succs[node])
1061 BITMAP_FREE (graph->succs[node]);
1064 /* Merge GRAPH nodes FROM and TO into node TO. */
1066 static void
1067 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1068 unsigned int from)
1070 if (graph->indirect_cycles[from] != -1)
1072 /* If we have indirect cycles with the from node, and we have
1073 none on the to node, the to node has indirect cycles from the
1074 from node now that they are unified.
1075 If indirect cycles exist on both, unify the nodes that they
1076 are in a cycle with, since we know they are in a cycle with
1077 each other. */
1078 if (graph->indirect_cycles[to] == -1)
1079 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1082 /* Merge all the successor edges. */
1083 if (graph->succs[from])
1085 if (!graph->succs[to])
1086 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1087 bitmap_ior_into (graph->succs[to],
1088 graph->succs[from]);
1091 clear_edges_for_node (graph, from);
1095 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1096 it doesn't exist in the graph already. */
1098 static void
1099 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1100 unsigned int from)
1102 if (to == from)
1103 return;
1105 if (!graph->implicit_preds[to])
1106 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1108 if (bitmap_set_bit (graph->implicit_preds[to], from))
1109 stats.num_implicit_edges++;
1112 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1113 it doesn't exist in the graph already.
1114 Return false if the edge already existed, true otherwise. */
1116 static void
1117 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1118 unsigned int from)
1120 if (!graph->preds[to])
1121 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1122 bitmap_set_bit (graph->preds[to], from);
1125 /* Add a graph edge to GRAPH, going from FROM to TO if
1126 it doesn't exist in the graph already.
1127 Return false if the edge already existed, true otherwise. */
1129 static bool
1130 add_graph_edge (constraint_graph_t graph, unsigned int to,
1131 unsigned int from)
1133 if (to == from)
1135 return false;
1137 else
1139 bool r = false;
1141 if (!graph->succs[from])
1142 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1143 if (bitmap_set_bit (graph->succs[from], to))
1145 r = true;
1146 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1147 stats.num_edges++;
1149 return r;
1154 /* Return true if {DEST.SRC} is an existing graph edge in GRAPH. */
1156 static bool
1157 valid_graph_edge (constraint_graph_t graph, unsigned int src,
1158 unsigned int dest)
1160 return (graph->succs[dest]
1161 && bitmap_bit_p (graph->succs[dest], src));
1164 /* Initialize the constraint graph structure to contain SIZE nodes. */
1166 static void
1167 init_graph (unsigned int size)
1169 unsigned int j;
1171 graph = XCNEW (struct constraint_graph);
1172 graph->size = size;
1173 graph->succs = XCNEWVEC (bitmap, graph->size);
1174 graph->indirect_cycles = XNEWVEC (int, graph->size);
1175 graph->rep = XNEWVEC (unsigned int, graph->size);
1176 graph->complex = XCNEWVEC (VEC(constraint_t, heap) *, size);
1177 graph->pe = XCNEWVEC (unsigned int, graph->size);
1178 graph->pe_rep = XNEWVEC (int, graph->size);
1180 for (j = 0; j < graph->size; j++)
1182 graph->rep[j] = j;
1183 graph->pe_rep[j] = -1;
1184 graph->indirect_cycles[j] = -1;
1188 /* Build the constraint graph, adding only predecessor edges right now. */
1190 static void
1191 build_pred_graph (void)
1193 int i;
1194 constraint_t c;
1195 unsigned int j;
1197 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1198 graph->preds = XCNEWVEC (bitmap, graph->size);
1199 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1200 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1201 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1202 graph->points_to = XCNEWVEC (bitmap, graph->size);
1203 graph->eq_rep = XNEWVEC (int, graph->size);
1204 graph->direct_nodes = sbitmap_alloc (graph->size);
1205 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1206 bitmap_clear (graph->direct_nodes);
1208 for (j = 0; j < FIRST_REF_NODE; j++)
1210 if (!get_varinfo (j)->is_special_var)
1211 bitmap_set_bit (graph->direct_nodes, j);
1214 for (j = 0; j < graph->size; j++)
1215 graph->eq_rep[j] = -1;
1217 for (j = 0; j < VEC_length (varinfo_t, varmap); j++)
1218 graph->indirect_cycles[j] = -1;
1220 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
1222 struct constraint_expr lhs = c->lhs;
1223 struct constraint_expr rhs = c->rhs;
1224 unsigned int lhsvar = lhs.var;
1225 unsigned int rhsvar = rhs.var;
1227 if (lhs.type == DEREF)
1229 /* *x = y. */
1230 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1231 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1233 else if (rhs.type == DEREF)
1235 /* x = *y */
1236 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1237 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1238 else
1239 bitmap_clear_bit (graph->direct_nodes, lhsvar);
1241 else if (rhs.type == ADDRESSOF)
1243 varinfo_t v;
1245 /* x = &y */
1246 if (graph->points_to[lhsvar] == NULL)
1247 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1248 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1250 if (graph->pointed_by[rhsvar] == NULL)
1251 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1252 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1254 /* Implicitly, *x = y */
1255 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1257 /* All related variables are no longer direct nodes. */
1258 bitmap_clear_bit (graph->direct_nodes, rhsvar);
1259 v = get_varinfo (rhsvar);
1260 if (!v->is_full_var)
1262 v = lookup_vi_for_tree (v->decl);
1265 bitmap_clear_bit (graph->direct_nodes, v->id);
1266 v = v->next;
1268 while (v != NULL);
1270 bitmap_set_bit (graph->address_taken, rhsvar);
1272 else if (lhsvar > anything_id
1273 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1275 /* x = y */
1276 add_pred_graph_edge (graph, lhsvar, rhsvar);
1277 /* Implicitly, *x = *y */
1278 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1279 FIRST_REF_NODE + rhsvar);
1281 else if (lhs.offset != 0 || rhs.offset != 0)
1283 if (rhs.offset != 0)
1284 bitmap_clear_bit (graph->direct_nodes, lhs.var);
1285 else if (lhs.offset != 0)
1286 bitmap_clear_bit (graph->direct_nodes, rhs.var);
1291 /* Build the constraint graph, adding successor edges. */
1293 static void
1294 build_succ_graph (void)
1296 unsigned i, t;
1297 constraint_t c;
1299 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
1301 struct constraint_expr lhs;
1302 struct constraint_expr rhs;
1303 unsigned int lhsvar;
1304 unsigned int rhsvar;
1306 if (!c)
1307 continue;
1309 lhs = c->lhs;
1310 rhs = c->rhs;
1311 lhsvar = find (lhs.var);
1312 rhsvar = find (rhs.var);
1314 if (lhs.type == DEREF)
1316 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1317 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1319 else if (rhs.type == DEREF)
1321 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1322 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1324 else if (rhs.type == ADDRESSOF)
1326 /* x = &y */
1327 gcc_assert (find (rhs.var) == rhs.var);
1328 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1330 else if (lhsvar > anything_id
1331 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1333 add_graph_edge (graph, lhsvar, rhsvar);
1337 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1338 receive pointers. */
1339 t = find (storedanything_id);
1340 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1342 if (!bitmap_bit_p (graph->direct_nodes, i)
1343 && get_varinfo (i)->may_have_pointers)
1344 add_graph_edge (graph, find (i), t);
1347 /* Everything stored to ANYTHING also potentially escapes. */
1348 add_graph_edge (graph, find (escaped_id), t);
1352 /* Changed variables on the last iteration. */
1353 static bitmap changed;
1355 /* Strongly Connected Component visitation info. */
1357 struct scc_info
1359 sbitmap visited;
1360 sbitmap deleted;
1361 unsigned int *dfs;
1362 unsigned int *node_mapping;
1363 int current_index;
1364 VEC(unsigned,heap) *scc_stack;
1368 /* Recursive routine to find strongly connected components in GRAPH.
1369 SI is the SCC info to store the information in, and N is the id of current
1370 graph node we are processing.
1372 This is Tarjan's strongly connected component finding algorithm, as
1373 modified by Nuutila to keep only non-root nodes on the stack.
1374 The algorithm can be found in "On finding the strongly connected
1375 connected components in a directed graph" by Esko Nuutila and Eljas
1376 Soisalon-Soininen, in Information Processing Letters volume 49,
1377 number 1, pages 9-14. */
1379 static void
1380 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1382 unsigned int i;
1383 bitmap_iterator bi;
1384 unsigned int my_dfs;
1386 bitmap_set_bit (si->visited, n);
1387 si->dfs[n] = si->current_index ++;
1388 my_dfs = si->dfs[n];
1390 /* Visit all the successors. */
1391 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1393 unsigned int w;
1395 if (i > LAST_REF_NODE)
1396 break;
1398 w = find (i);
1399 if (bitmap_bit_p (si->deleted, w))
1400 continue;
1402 if (!bitmap_bit_p (si->visited, w))
1403 scc_visit (graph, si, w);
1405 unsigned int t = find (w);
1406 unsigned int nnode = find (n);
1407 gcc_assert (nnode == n);
1409 if (si->dfs[t] < si->dfs[nnode])
1410 si->dfs[n] = si->dfs[t];
1414 /* See if any components have been identified. */
1415 if (si->dfs[n] == my_dfs)
1417 if (VEC_length (unsigned, si->scc_stack) > 0
1418 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1420 bitmap scc = BITMAP_ALLOC (NULL);
1421 unsigned int lowest_node;
1422 bitmap_iterator bi;
1424 bitmap_set_bit (scc, n);
1426 while (VEC_length (unsigned, si->scc_stack) != 0
1427 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1429 unsigned int w = VEC_pop (unsigned, si->scc_stack);
1431 bitmap_set_bit (scc, w);
1434 lowest_node = bitmap_first_set_bit (scc);
1435 gcc_assert (lowest_node < FIRST_REF_NODE);
1437 /* Collapse the SCC nodes into a single node, and mark the
1438 indirect cycles. */
1439 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1441 if (i < FIRST_REF_NODE)
1443 if (unite (lowest_node, i))
1444 unify_nodes (graph, lowest_node, i, false);
1446 else
1448 unite (lowest_node, i);
1449 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1453 bitmap_set_bit (si->deleted, n);
1455 else
1456 VEC_safe_push (unsigned, heap, si->scc_stack, n);
1459 /* Unify node FROM into node TO, updating the changed count if
1460 necessary when UPDATE_CHANGED is true. */
1462 static void
1463 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1464 bool update_changed)
1467 gcc_assert (to != from && find (to) == to);
1468 if (dump_file && (dump_flags & TDF_DETAILS))
1469 fprintf (dump_file, "Unifying %s to %s\n",
1470 get_varinfo (from)->name,
1471 get_varinfo (to)->name);
1473 if (update_changed)
1474 stats.unified_vars_dynamic++;
1475 else
1476 stats.unified_vars_static++;
1478 merge_graph_nodes (graph, to, from);
1479 merge_node_constraints (graph, to, from);
1481 /* Mark TO as changed if FROM was changed. If TO was already marked
1482 as changed, decrease the changed count. */
1484 if (update_changed
1485 && bitmap_bit_p (changed, from))
1487 bitmap_clear_bit (changed, from);
1488 bitmap_set_bit (changed, to);
1490 if (get_varinfo (from)->solution)
1492 /* If the solution changes because of the merging, we need to mark
1493 the variable as changed. */
1494 if (bitmap_ior_into (get_varinfo (to)->solution,
1495 get_varinfo (from)->solution))
1497 if (update_changed)
1498 bitmap_set_bit (changed, to);
1501 BITMAP_FREE (get_varinfo (from)->solution);
1502 if (get_varinfo (from)->oldsolution)
1503 BITMAP_FREE (get_varinfo (from)->oldsolution);
1505 if (stats.iterations > 0
1506 && get_varinfo (to)->oldsolution)
1507 BITMAP_FREE (get_varinfo (to)->oldsolution);
1509 if (valid_graph_edge (graph, to, to))
1511 if (graph->succs[to])
1512 bitmap_clear_bit (graph->succs[to], to);
1516 /* Information needed to compute the topological ordering of a graph. */
1518 struct topo_info
1520 /* sbitmap of visited nodes. */
1521 sbitmap visited;
1522 /* Array that stores the topological order of the graph, *in
1523 reverse*. */
1524 VEC(unsigned,heap) *topo_order;
1528 /* Initialize and return a topological info structure. */
1530 static struct topo_info *
1531 init_topo_info (void)
1533 size_t size = graph->size;
1534 struct topo_info *ti = XNEW (struct topo_info);
1535 ti->visited = sbitmap_alloc (size);
1536 bitmap_clear (ti->visited);
1537 ti->topo_order = VEC_alloc (unsigned, heap, 1);
1538 return ti;
1542 /* Free the topological sort info pointed to by TI. */
1544 static void
1545 free_topo_info (struct topo_info *ti)
1547 sbitmap_free (ti->visited);
1548 VEC_free (unsigned, heap, ti->topo_order);
1549 free (ti);
1552 /* Visit the graph in topological order, and store the order in the
1553 topo_info structure. */
1555 static void
1556 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1557 unsigned int n)
1559 bitmap_iterator bi;
1560 unsigned int j;
1562 bitmap_set_bit (ti->visited, n);
1564 if (graph->succs[n])
1565 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1567 if (!bitmap_bit_p (ti->visited, j))
1568 topo_visit (graph, ti, j);
1571 VEC_safe_push (unsigned, heap, ti->topo_order, n);
1574 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1575 starting solution for y. */
1577 static void
1578 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1579 bitmap delta)
1581 unsigned int lhs = c->lhs.var;
1582 bool flag = false;
1583 bitmap sol = get_varinfo (lhs)->solution;
1584 unsigned int j;
1585 bitmap_iterator bi;
1586 HOST_WIDE_INT roffset = c->rhs.offset;
1588 /* Our IL does not allow this. */
1589 gcc_assert (c->lhs.offset == 0);
1591 /* If the solution of Y contains anything it is good enough to transfer
1592 this to the LHS. */
1593 if (bitmap_bit_p (delta, anything_id))
1595 flag |= bitmap_set_bit (sol, anything_id);
1596 goto done;
1599 /* If we do not know at with offset the rhs is dereferenced compute
1600 the reachability set of DELTA, conservatively assuming it is
1601 dereferenced at all valid offsets. */
1602 if (roffset == UNKNOWN_OFFSET)
1604 solution_set_expand (delta, delta);
1605 /* No further offset processing is necessary. */
1606 roffset = 0;
1609 /* For each variable j in delta (Sol(y)), add
1610 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1611 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1613 varinfo_t v = get_varinfo (j);
1614 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1615 unsigned int t;
1617 if (v->is_full_var)
1618 fieldoffset = v->offset;
1619 else if (roffset != 0)
1620 v = first_vi_for_offset (v, fieldoffset);
1621 /* If the access is outside of the variable we can ignore it. */
1622 if (!v)
1623 continue;
1627 t = find (v->id);
1629 /* Adding edges from the special vars is pointless.
1630 They don't have sets that can change. */
1631 if (get_varinfo (t)->is_special_var)
1632 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1633 /* Merging the solution from ESCAPED needlessly increases
1634 the set. Use ESCAPED as representative instead. */
1635 else if (v->id == escaped_id)
1636 flag |= bitmap_set_bit (sol, escaped_id);
1637 else if (v->may_have_pointers
1638 && add_graph_edge (graph, lhs, t))
1639 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1641 /* If the variable is not exactly at the requested offset
1642 we have to include the next one. */
1643 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1644 || v->next == NULL)
1645 break;
1647 v = v->next;
1648 fieldoffset = v->offset;
1650 while (1);
1653 done:
1654 /* If the LHS solution changed, mark the var as changed. */
1655 if (flag)
1657 get_varinfo (lhs)->solution = sol;
1658 bitmap_set_bit (changed, lhs);
1662 /* Process a constraint C that represents *(x + off) = y using DELTA
1663 as the starting solution for x. */
1665 static void
1666 do_ds_constraint (constraint_t c, bitmap delta)
1668 unsigned int rhs = c->rhs.var;
1669 bitmap sol = get_varinfo (rhs)->solution;
1670 unsigned int j;
1671 bitmap_iterator bi;
1672 HOST_WIDE_INT loff = c->lhs.offset;
1673 bool escaped_p = false;
1675 /* Our IL does not allow this. */
1676 gcc_assert (c->rhs.offset == 0);
1678 /* If the solution of y contains ANYTHING simply use the ANYTHING
1679 solution. This avoids needlessly increasing the points-to sets. */
1680 if (bitmap_bit_p (sol, anything_id))
1681 sol = get_varinfo (find (anything_id))->solution;
1683 /* If the solution for x contains ANYTHING we have to merge the
1684 solution of y into all pointer variables which we do via
1685 STOREDANYTHING. */
1686 if (bitmap_bit_p (delta, anything_id))
1688 unsigned t = find (storedanything_id);
1689 if (add_graph_edge (graph, t, rhs))
1691 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1692 bitmap_set_bit (changed, t);
1694 return;
1697 /* If we do not know at with offset the rhs is dereferenced compute
1698 the reachability set of DELTA, conservatively assuming it is
1699 dereferenced at all valid offsets. */
1700 if (loff == UNKNOWN_OFFSET)
1702 solution_set_expand (delta, delta);
1703 loff = 0;
1706 /* For each member j of delta (Sol(x)), add an edge from y to j and
1707 union Sol(y) into Sol(j) */
1708 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1710 varinfo_t v = get_varinfo (j);
1711 unsigned int t;
1712 HOST_WIDE_INT fieldoffset = v->offset + loff;
1714 if (v->is_full_var)
1715 fieldoffset = v->offset;
1716 else if (loff != 0)
1717 v = first_vi_for_offset (v, fieldoffset);
1718 /* If the access is outside of the variable we can ignore it. */
1719 if (!v)
1720 continue;
1724 if (v->may_have_pointers)
1726 /* If v is a global variable then this is an escape point. */
1727 if (v->is_global_var
1728 && !escaped_p)
1730 t = find (escaped_id);
1731 if (add_graph_edge (graph, t, rhs)
1732 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1733 bitmap_set_bit (changed, t);
1734 /* Enough to let rhs escape once. */
1735 escaped_p = true;
1738 if (v->is_special_var)
1739 break;
1741 t = find (v->id);
1742 if (add_graph_edge (graph, t, rhs)
1743 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1744 bitmap_set_bit (changed, t);
1747 /* If the variable is not exactly at the requested offset
1748 we have to include the next one. */
1749 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1750 || v->next == NULL)
1751 break;
1753 v = v->next;
1754 fieldoffset = v->offset;
1756 while (1);
1760 /* Handle a non-simple (simple meaning requires no iteration),
1761 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1763 static void
1764 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta)
1766 if (c->lhs.type == DEREF)
1768 if (c->rhs.type == ADDRESSOF)
1770 gcc_unreachable();
1772 else
1774 /* *x = y */
1775 do_ds_constraint (c, delta);
1778 else if (c->rhs.type == DEREF)
1780 /* x = *y */
1781 if (!(get_varinfo (c->lhs.var)->is_special_var))
1782 do_sd_constraint (graph, c, delta);
1784 else
1786 bitmap tmp;
1787 bitmap solution;
1788 bool flag = false;
1790 gcc_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR);
1791 solution = get_varinfo (c->rhs.var)->solution;
1792 tmp = get_varinfo (c->lhs.var)->solution;
1794 flag = set_union_with_increment (tmp, solution, c->rhs.offset);
1796 if (flag)
1798 get_varinfo (c->lhs.var)->solution = tmp;
1799 bitmap_set_bit (changed, c->lhs.var);
1804 /* Initialize and return a new SCC info structure. */
1806 static struct scc_info *
1807 init_scc_info (size_t size)
1809 struct scc_info *si = XNEW (struct scc_info);
1810 size_t i;
1812 si->current_index = 0;
1813 si->visited = sbitmap_alloc (size);
1814 bitmap_clear (si->visited);
1815 si->deleted = sbitmap_alloc (size);
1816 bitmap_clear (si->deleted);
1817 si->node_mapping = XNEWVEC (unsigned int, size);
1818 si->dfs = XCNEWVEC (unsigned int, size);
1820 for (i = 0; i < size; i++)
1821 si->node_mapping[i] = i;
1823 si->scc_stack = VEC_alloc (unsigned, heap, 1);
1824 return si;
1827 /* Free an SCC info structure pointed to by SI */
1829 static void
1830 free_scc_info (struct scc_info *si)
1832 sbitmap_free (si->visited);
1833 sbitmap_free (si->deleted);
1834 free (si->node_mapping);
1835 free (si->dfs);
1836 VEC_free (unsigned, heap, si->scc_stack);
1837 free (si);
1841 /* Find indirect cycles in GRAPH that occur, using strongly connected
1842 components, and note them in the indirect cycles map.
1844 This technique comes from Ben Hardekopf and Calvin Lin,
1845 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1846 Lines of Code", submitted to PLDI 2007. */
1848 static void
1849 find_indirect_cycles (constraint_graph_t graph)
1851 unsigned int i;
1852 unsigned int size = graph->size;
1853 struct scc_info *si = init_scc_info (size);
1855 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1856 if (!bitmap_bit_p (si->visited, i) && find (i) == i)
1857 scc_visit (graph, si, i);
1859 free_scc_info (si);
1862 /* Compute a topological ordering for GRAPH, and store the result in the
1863 topo_info structure TI. */
1865 static void
1866 compute_topo_order (constraint_graph_t graph,
1867 struct topo_info *ti)
1869 unsigned int i;
1870 unsigned int size = graph->size;
1872 for (i = 0; i != size; ++i)
1873 if (!bitmap_bit_p (ti->visited, i) && find (i) == i)
1874 topo_visit (graph, ti, i);
1877 /* Structure used to for hash value numbering of pointer equivalence
1878 classes. */
1880 typedef struct equiv_class_label
1882 hashval_t hashcode;
1883 unsigned int equivalence_class;
1884 bitmap labels;
1885 } *equiv_class_label_t;
1886 typedef const struct equiv_class_label *const_equiv_class_label_t;
1888 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1889 classes. */
1890 static htab_t pointer_equiv_class_table;
1892 /* A hashtable for mapping a bitmap of labels->location equivalence
1893 classes. */
1894 static htab_t location_equiv_class_table;
1896 /* Hash function for a equiv_class_label_t */
1898 static hashval_t
1899 equiv_class_label_hash (const void *p)
1901 const_equiv_class_label_t const ecl = (const_equiv_class_label_t) p;
1902 return ecl->hashcode;
1905 /* Equality function for two equiv_class_label_t's. */
1907 static int
1908 equiv_class_label_eq (const void *p1, const void *p2)
1910 const_equiv_class_label_t const eql1 = (const_equiv_class_label_t) p1;
1911 const_equiv_class_label_t const eql2 = (const_equiv_class_label_t) p2;
1912 return (eql1->hashcode == eql2->hashcode
1913 && bitmap_equal_p (eql1->labels, eql2->labels));
1916 /* Lookup a equivalence class in TABLE by the bitmap of LABELS it
1917 contains. */
1919 static unsigned int
1920 equiv_class_lookup (htab_t table, bitmap labels)
1922 void **slot;
1923 struct equiv_class_label ecl;
1925 ecl.labels = labels;
1926 ecl.hashcode = bitmap_hash (labels);
1928 slot = htab_find_slot_with_hash (table, &ecl,
1929 ecl.hashcode, NO_INSERT);
1930 if (!slot)
1931 return 0;
1932 else
1933 return ((equiv_class_label_t) *slot)->equivalence_class;
1937 /* Add an equivalence class named EQUIVALENCE_CLASS with labels LABELS
1938 to TABLE. */
1940 static void
1941 equiv_class_add (htab_t table, unsigned int equivalence_class,
1942 bitmap labels)
1944 void **slot;
1945 equiv_class_label_t ecl = XNEW (struct equiv_class_label);
1947 ecl->labels = labels;
1948 ecl->equivalence_class = equivalence_class;
1949 ecl->hashcode = bitmap_hash (labels);
1951 slot = htab_find_slot_with_hash (table, ecl,
1952 ecl->hashcode, INSERT);
1953 gcc_assert (!*slot);
1954 *slot = (void *) ecl;
1957 /* Perform offline variable substitution.
1959 This is a worst case quadratic time way of identifying variables
1960 that must have equivalent points-to sets, including those caused by
1961 static cycles, and single entry subgraphs, in the constraint graph.
1963 The technique is described in "Exploiting Pointer and Location
1964 Equivalence to Optimize Pointer Analysis. In the 14th International
1965 Static Analysis Symposium (SAS), August 2007." It is known as the
1966 "HU" algorithm, and is equivalent to value numbering the collapsed
1967 constraint graph including evaluating unions.
1969 The general method of finding equivalence classes is as follows:
1970 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1971 Initialize all non-REF nodes to be direct nodes.
1972 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1973 variable}
1974 For each constraint containing the dereference, we also do the same
1975 thing.
1977 We then compute SCC's in the graph and unify nodes in the same SCC,
1978 including pts sets.
1980 For each non-collapsed node x:
1981 Visit all unvisited explicit incoming edges.
1982 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1983 where y->x.
1984 Lookup the equivalence class for pts(x).
1985 If we found one, equivalence_class(x) = found class.
1986 Otherwise, equivalence_class(x) = new class, and new_class is
1987 added to the lookup table.
1989 All direct nodes with the same equivalence class can be replaced
1990 with a single representative node.
1991 All unlabeled nodes (label == 0) are not pointers and all edges
1992 involving them can be eliminated.
1993 We perform these optimizations during rewrite_constraints
1995 In addition to pointer equivalence class finding, we also perform
1996 location equivalence class finding. This is the set of variables
1997 that always appear together in points-to sets. We use this to
1998 compress the size of the points-to sets. */
2000 /* Current maximum pointer equivalence class id. */
2001 static int pointer_equiv_class;
2003 /* Current maximum location equivalence class id. */
2004 static int location_equiv_class;
2006 /* Recursive routine to find strongly connected components in GRAPH,
2007 and label it's nodes with DFS numbers. */
2009 static void
2010 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2012 unsigned int i;
2013 bitmap_iterator bi;
2014 unsigned int my_dfs;
2016 gcc_assert (si->node_mapping[n] == n);
2017 bitmap_set_bit (si->visited, n);
2018 si->dfs[n] = si->current_index ++;
2019 my_dfs = si->dfs[n];
2021 /* Visit all the successors. */
2022 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2024 unsigned int w = si->node_mapping[i];
2026 if (bitmap_bit_p (si->deleted, w))
2027 continue;
2029 if (!bitmap_bit_p (si->visited, w))
2030 condense_visit (graph, si, w);
2032 unsigned int t = si->node_mapping[w];
2033 unsigned int nnode = si->node_mapping[n];
2034 gcc_assert (nnode == n);
2036 if (si->dfs[t] < si->dfs[nnode])
2037 si->dfs[n] = si->dfs[t];
2041 /* Visit all the implicit predecessors. */
2042 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2044 unsigned int w = si->node_mapping[i];
2046 if (bitmap_bit_p (si->deleted, w))
2047 continue;
2049 if (!bitmap_bit_p (si->visited, w))
2050 condense_visit (graph, si, w);
2052 unsigned int t = si->node_mapping[w];
2053 unsigned int nnode = si->node_mapping[n];
2054 gcc_assert (nnode == n);
2056 if (si->dfs[t] < si->dfs[nnode])
2057 si->dfs[n] = si->dfs[t];
2061 /* See if any components have been identified. */
2062 if (si->dfs[n] == my_dfs)
2064 while (VEC_length (unsigned, si->scc_stack) != 0
2065 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
2067 unsigned int w = VEC_pop (unsigned, si->scc_stack);
2068 si->node_mapping[w] = n;
2070 if (!bitmap_bit_p (graph->direct_nodes, w))
2071 bitmap_clear_bit (graph->direct_nodes, n);
2073 /* Unify our nodes. */
2074 if (graph->preds[w])
2076 if (!graph->preds[n])
2077 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2078 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2080 if (graph->implicit_preds[w])
2082 if (!graph->implicit_preds[n])
2083 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2084 bitmap_ior_into (graph->implicit_preds[n],
2085 graph->implicit_preds[w]);
2087 if (graph->points_to[w])
2089 if (!graph->points_to[n])
2090 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2091 bitmap_ior_into (graph->points_to[n],
2092 graph->points_to[w]);
2095 bitmap_set_bit (si->deleted, n);
2097 else
2098 VEC_safe_push (unsigned, heap, si->scc_stack, n);
2101 /* Label pointer equivalences. */
2103 static void
2104 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2106 unsigned int i;
2107 bitmap_iterator bi;
2108 bitmap_set_bit (si->visited, n);
2110 if (!graph->points_to[n])
2111 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2113 /* Label and union our incoming edges's points to sets. */
2114 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2116 unsigned int w = si->node_mapping[i];
2117 if (!bitmap_bit_p (si->visited, w))
2118 label_visit (graph, si, w);
2120 /* Skip unused edges */
2121 if (w == n || graph->pointer_label[w] == 0)
2122 continue;
2124 if (graph->points_to[w])
2125 bitmap_ior_into(graph->points_to[n], graph->points_to[w]);
2127 /* Indirect nodes get fresh variables. */
2128 if (!bitmap_bit_p (graph->direct_nodes, n))
2129 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2131 if (!bitmap_empty_p (graph->points_to[n]))
2133 unsigned int label = equiv_class_lookup (pointer_equiv_class_table,
2134 graph->points_to[n]);
2135 if (!label)
2137 label = pointer_equiv_class++;
2138 equiv_class_add (pointer_equiv_class_table,
2139 label, graph->points_to[n]);
2141 graph->pointer_label[n] = label;
2145 /* Perform offline variable substitution, discovering equivalence
2146 classes, and eliminating non-pointer variables. */
2148 static struct scc_info *
2149 perform_var_substitution (constraint_graph_t graph)
2151 unsigned int i;
2152 unsigned int size = graph->size;
2153 struct scc_info *si = init_scc_info (size);
2155 bitmap_obstack_initialize (&iteration_obstack);
2156 pointer_equiv_class_table = htab_create (511, equiv_class_label_hash,
2157 equiv_class_label_eq, free);
2158 location_equiv_class_table = htab_create (511, equiv_class_label_hash,
2159 equiv_class_label_eq, free);
2160 pointer_equiv_class = 1;
2161 location_equiv_class = 1;
2163 /* Condense the nodes, which means to find SCC's, count incoming
2164 predecessors, and unite nodes in SCC's. */
2165 for (i = 0; i < FIRST_REF_NODE; i++)
2166 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2167 condense_visit (graph, si, si->node_mapping[i]);
2169 bitmap_clear (si->visited);
2170 /* Actually the label the nodes for pointer equivalences */
2171 for (i = 0; i < FIRST_REF_NODE; i++)
2172 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2173 label_visit (graph, si, si->node_mapping[i]);
2175 /* Calculate location equivalence labels. */
2176 for (i = 0; i < FIRST_REF_NODE; i++)
2178 bitmap pointed_by;
2179 bitmap_iterator bi;
2180 unsigned int j;
2181 unsigned int label;
2183 if (!graph->pointed_by[i])
2184 continue;
2185 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2187 /* Translate the pointed-by mapping for pointer equivalence
2188 labels. */
2189 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2191 bitmap_set_bit (pointed_by,
2192 graph->pointer_label[si->node_mapping[j]]);
2194 /* The original pointed_by is now dead. */
2195 BITMAP_FREE (graph->pointed_by[i]);
2197 /* Look up the location equivalence label if one exists, or make
2198 one otherwise. */
2199 label = equiv_class_lookup (location_equiv_class_table,
2200 pointed_by);
2201 if (label == 0)
2203 label = location_equiv_class++;
2204 equiv_class_add (location_equiv_class_table,
2205 label, pointed_by);
2207 else
2209 if (dump_file && (dump_flags & TDF_DETAILS))
2210 fprintf (dump_file, "Found location equivalence for node %s\n",
2211 get_varinfo (i)->name);
2212 BITMAP_FREE (pointed_by);
2214 graph->loc_label[i] = label;
2218 if (dump_file && (dump_flags & TDF_DETAILS))
2219 for (i = 0; i < FIRST_REF_NODE; i++)
2221 bool direct_node = bitmap_bit_p (graph->direct_nodes, i);
2222 fprintf (dump_file,
2223 "Equivalence classes for %s node id %d:%s are pointer: %d"
2224 ", location:%d\n",
2225 direct_node ? "Direct node" : "Indirect node", i,
2226 get_varinfo (i)->name,
2227 graph->pointer_label[si->node_mapping[i]],
2228 graph->loc_label[si->node_mapping[i]]);
2231 /* Quickly eliminate our non-pointer variables. */
2233 for (i = 0; i < FIRST_REF_NODE; i++)
2235 unsigned int node = si->node_mapping[i];
2237 if (graph->pointer_label[node] == 0)
2239 if (dump_file && (dump_flags & TDF_DETAILS))
2240 fprintf (dump_file,
2241 "%s is a non-pointer variable, eliminating edges.\n",
2242 get_varinfo (node)->name);
2243 stats.nonpointer_vars++;
2244 clear_edges_for_node (graph, node);
2248 return si;
2251 /* Free information that was only necessary for variable
2252 substitution. */
2254 static void
2255 free_var_substitution_info (struct scc_info *si)
2257 free_scc_info (si);
2258 free (graph->pointer_label);
2259 free (graph->loc_label);
2260 free (graph->pointed_by);
2261 free (graph->points_to);
2262 free (graph->eq_rep);
2263 sbitmap_free (graph->direct_nodes);
2264 htab_delete (pointer_equiv_class_table);
2265 htab_delete (location_equiv_class_table);
2266 bitmap_obstack_release (&iteration_obstack);
2269 /* Return an existing node that is equivalent to NODE, which has
2270 equivalence class LABEL, if one exists. Return NODE otherwise. */
2272 static unsigned int
2273 find_equivalent_node (constraint_graph_t graph,
2274 unsigned int node, unsigned int label)
2276 /* If the address version of this variable is unused, we can
2277 substitute it for anything else with the same label.
2278 Otherwise, we know the pointers are equivalent, but not the
2279 locations, and we can unite them later. */
2281 if (!bitmap_bit_p (graph->address_taken, node))
2283 gcc_assert (label < graph->size);
2285 if (graph->eq_rep[label] != -1)
2287 /* Unify the two variables since we know they are equivalent. */
2288 if (unite (graph->eq_rep[label], node))
2289 unify_nodes (graph, graph->eq_rep[label], node, false);
2290 return graph->eq_rep[label];
2292 else
2294 graph->eq_rep[label] = node;
2295 graph->pe_rep[label] = node;
2298 else
2300 gcc_assert (label < graph->size);
2301 graph->pe[node] = label;
2302 if (graph->pe_rep[label] == -1)
2303 graph->pe_rep[label] = node;
2306 return node;
2309 /* Unite pointer equivalent but not location equivalent nodes in
2310 GRAPH. This may only be performed once variable substitution is
2311 finished. */
2313 static void
2314 unite_pointer_equivalences (constraint_graph_t graph)
2316 unsigned int i;
2318 /* Go through the pointer equivalences and unite them to their
2319 representative, if they aren't already. */
2320 for (i = 0; i < FIRST_REF_NODE; i++)
2322 unsigned int label = graph->pe[i];
2323 if (label)
2325 int label_rep = graph->pe_rep[label];
2327 if (label_rep == -1)
2328 continue;
2330 label_rep = find (label_rep);
2331 if (label_rep >= 0 && unite (label_rep, find (i)))
2332 unify_nodes (graph, label_rep, i, false);
2337 /* Move complex constraints to the GRAPH nodes they belong to. */
2339 static void
2340 move_complex_constraints (constraint_graph_t graph)
2342 int i;
2343 constraint_t c;
2345 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
2347 if (c)
2349 struct constraint_expr lhs = c->lhs;
2350 struct constraint_expr rhs = c->rhs;
2352 if (lhs.type == DEREF)
2354 insert_into_complex (graph, lhs.var, c);
2356 else if (rhs.type == DEREF)
2358 if (!(get_varinfo (lhs.var)->is_special_var))
2359 insert_into_complex (graph, rhs.var, c);
2361 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2362 && (lhs.offset != 0 || rhs.offset != 0))
2364 insert_into_complex (graph, rhs.var, c);
2371 /* Optimize and rewrite complex constraints while performing
2372 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2373 result of perform_variable_substitution. */
2375 static void
2376 rewrite_constraints (constraint_graph_t graph,
2377 struct scc_info *si)
2379 int i;
2380 unsigned int j;
2381 constraint_t c;
2383 for (j = 0; j < graph->size; j++)
2384 gcc_assert (find (j) == j);
2386 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
2388 struct constraint_expr lhs = c->lhs;
2389 struct constraint_expr rhs = c->rhs;
2390 unsigned int lhsvar = find (lhs.var);
2391 unsigned int rhsvar = find (rhs.var);
2392 unsigned int lhsnode, rhsnode;
2393 unsigned int lhslabel, rhslabel;
2395 lhsnode = si->node_mapping[lhsvar];
2396 rhsnode = si->node_mapping[rhsvar];
2397 lhslabel = graph->pointer_label[lhsnode];
2398 rhslabel = graph->pointer_label[rhsnode];
2400 /* See if it is really a non-pointer variable, and if so, ignore
2401 the constraint. */
2402 if (lhslabel == 0)
2404 if (dump_file && (dump_flags & TDF_DETAILS))
2407 fprintf (dump_file, "%s is a non-pointer variable,"
2408 "ignoring constraint:",
2409 get_varinfo (lhs.var)->name);
2410 dump_constraint (dump_file, c);
2411 fprintf (dump_file, "\n");
2413 VEC_replace (constraint_t, constraints, i, NULL);
2414 continue;
2417 if (rhslabel == 0)
2419 if (dump_file && (dump_flags & TDF_DETAILS))
2422 fprintf (dump_file, "%s is a non-pointer variable,"
2423 "ignoring constraint:",
2424 get_varinfo (rhs.var)->name);
2425 dump_constraint (dump_file, c);
2426 fprintf (dump_file, "\n");
2428 VEC_replace (constraint_t, constraints, i, NULL);
2429 continue;
2432 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2433 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2434 c->lhs.var = lhsvar;
2435 c->rhs.var = rhsvar;
2440 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2441 part of an SCC, false otherwise. */
2443 static bool
2444 eliminate_indirect_cycles (unsigned int node)
2446 if (graph->indirect_cycles[node] != -1
2447 && !bitmap_empty_p (get_varinfo (node)->solution))
2449 unsigned int i;
2450 VEC(unsigned,heap) *queue = NULL;
2451 int queuepos;
2452 unsigned int to = find (graph->indirect_cycles[node]);
2453 bitmap_iterator bi;
2455 /* We can't touch the solution set and call unify_nodes
2456 at the same time, because unify_nodes is going to do
2457 bitmap unions into it. */
2459 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2461 if (find (i) == i && i != to)
2463 if (unite (to, i))
2464 VEC_safe_push (unsigned, heap, queue, i);
2468 for (queuepos = 0;
2469 VEC_iterate (unsigned, queue, queuepos, i);
2470 queuepos++)
2472 unify_nodes (graph, to, i, true);
2474 VEC_free (unsigned, heap, queue);
2475 return true;
2477 return false;
2480 /* Solve the constraint graph GRAPH using our worklist solver.
2481 This is based on the PW* family of solvers from the "Efficient Field
2482 Sensitive Pointer Analysis for C" paper.
2483 It works by iterating over all the graph nodes, processing the complex
2484 constraints and propagating the copy constraints, until everything stops
2485 changed. This corresponds to steps 6-8 in the solving list given above. */
2487 static void
2488 solve_graph (constraint_graph_t graph)
2490 unsigned int size = graph->size;
2491 unsigned int i;
2492 bitmap pts;
2494 changed = BITMAP_ALLOC (NULL);
2496 /* Mark all initial non-collapsed nodes as changed. */
2497 for (i = 0; i < size; i++)
2499 varinfo_t ivi = get_varinfo (i);
2500 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2501 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2502 || VEC_length (constraint_t, graph->complex[i]) > 0))
2503 bitmap_set_bit (changed, i);
2506 /* Allocate a bitmap to be used to store the changed bits. */
2507 pts = BITMAP_ALLOC (&pta_obstack);
2509 while (!bitmap_empty_p (changed))
2511 unsigned int i;
2512 struct topo_info *ti = init_topo_info ();
2513 stats.iterations++;
2515 bitmap_obstack_initialize (&iteration_obstack);
2517 compute_topo_order (graph, ti);
2519 while (VEC_length (unsigned, ti->topo_order) != 0)
2522 i = VEC_pop (unsigned, ti->topo_order);
2524 /* If this variable is not a representative, skip it. */
2525 if (find (i) != i)
2526 continue;
2528 /* In certain indirect cycle cases, we may merge this
2529 variable to another. */
2530 if (eliminate_indirect_cycles (i) && find (i) != i)
2531 continue;
2533 /* If the node has changed, we need to process the
2534 complex constraints and outgoing edges again. */
2535 if (bitmap_clear_bit (changed, i))
2537 unsigned int j;
2538 constraint_t c;
2539 bitmap solution;
2540 VEC(constraint_t,heap) *complex = graph->complex[i];
2541 varinfo_t vi = get_varinfo (i);
2542 bool solution_empty;
2544 /* Compute the changed set of solution bits. */
2545 if (vi->oldsolution)
2546 bitmap_and_compl (pts, vi->solution, vi->oldsolution);
2547 else
2548 bitmap_copy (pts, vi->solution);
2550 if (bitmap_empty_p (pts))
2551 continue;
2553 if (vi->oldsolution)
2554 bitmap_ior_into (vi->oldsolution, pts);
2555 else
2557 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
2558 bitmap_copy (vi->oldsolution, pts);
2561 solution = vi->solution;
2562 solution_empty = bitmap_empty_p (solution);
2564 /* Process the complex constraints */
2565 FOR_EACH_VEC_ELT (constraint_t, complex, j, c)
2567 /* XXX: This is going to unsort the constraints in
2568 some cases, which will occasionally add duplicate
2569 constraints during unification. This does not
2570 affect correctness. */
2571 c->lhs.var = find (c->lhs.var);
2572 c->rhs.var = find (c->rhs.var);
2574 /* The only complex constraint that can change our
2575 solution to non-empty, given an empty solution,
2576 is a constraint where the lhs side is receiving
2577 some set from elsewhere. */
2578 if (!solution_empty || c->lhs.type != DEREF)
2579 do_complex_constraint (graph, c, pts);
2582 solution_empty = bitmap_empty_p (solution);
2584 if (!solution_empty)
2586 bitmap_iterator bi;
2587 unsigned eff_escaped_id = find (escaped_id);
2589 /* Propagate solution to all successors. */
2590 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2591 0, j, bi)
2593 bitmap tmp;
2594 bool flag;
2596 unsigned int to = find (j);
2597 tmp = get_varinfo (to)->solution;
2598 flag = false;
2600 /* Don't try to propagate to ourselves. */
2601 if (to == i)
2602 continue;
2604 /* If we propagate from ESCAPED use ESCAPED as
2605 placeholder. */
2606 if (i == eff_escaped_id)
2607 flag = bitmap_set_bit (tmp, escaped_id);
2608 else
2609 flag = set_union_with_increment (tmp, pts, 0);
2611 if (flag)
2613 get_varinfo (to)->solution = tmp;
2614 bitmap_set_bit (changed, to);
2620 free_topo_info (ti);
2621 bitmap_obstack_release (&iteration_obstack);
2624 BITMAP_FREE (pts);
2625 BITMAP_FREE (changed);
2626 bitmap_obstack_release (&oldpta_obstack);
2629 /* Map from trees to variable infos. */
2630 static struct pointer_map_t *vi_for_tree;
2633 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2635 static void
2636 insert_vi_for_tree (tree t, varinfo_t vi)
2638 void **slot = pointer_map_insert (vi_for_tree, t);
2639 gcc_assert (vi);
2640 gcc_assert (*slot == NULL);
2641 *slot = vi;
2644 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2645 exist in the map, return NULL, otherwise, return the varinfo we found. */
2647 static varinfo_t
2648 lookup_vi_for_tree (tree t)
2650 void **slot = pointer_map_contains (vi_for_tree, t);
2651 if (slot == NULL)
2652 return NULL;
2654 return (varinfo_t) *slot;
2657 /* Return a printable name for DECL */
2659 static const char *
2660 alias_get_name (tree decl)
2662 const char *res = NULL;
2663 char *temp;
2664 int num_printed = 0;
2666 if (!dump_file)
2667 return "NULL";
2669 if (TREE_CODE (decl) == SSA_NAME)
2671 res = get_name (decl);
2672 if (res)
2673 num_printed = asprintf (&temp, "%s_%u", res, SSA_NAME_VERSION (decl));
2674 else
2675 num_printed = asprintf (&temp, "_%u", SSA_NAME_VERSION (decl));
2676 if (num_printed > 0)
2678 res = ggc_strdup (temp);
2679 free (temp);
2682 else if (DECL_P (decl))
2684 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2685 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2686 else
2688 res = get_name (decl);
2689 if (!res)
2691 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2692 if (num_printed > 0)
2694 res = ggc_strdup (temp);
2695 free (temp);
2700 if (res != NULL)
2701 return res;
2703 return "NULL";
2706 /* Find the variable id for tree T in the map.
2707 If T doesn't exist in the map, create an entry for it and return it. */
2709 static varinfo_t
2710 get_vi_for_tree (tree t)
2712 void **slot = pointer_map_contains (vi_for_tree, t);
2713 if (slot == NULL)
2714 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2716 return (varinfo_t) *slot;
2719 /* Get a scalar constraint expression for a new temporary variable. */
2721 static struct constraint_expr
2722 new_scalar_tmp_constraint_exp (const char *name)
2724 struct constraint_expr tmp;
2725 varinfo_t vi;
2727 vi = new_var_info (NULL_TREE, name);
2728 vi->offset = 0;
2729 vi->size = -1;
2730 vi->fullsize = -1;
2731 vi->is_full_var = 1;
2733 tmp.var = vi->id;
2734 tmp.type = SCALAR;
2735 tmp.offset = 0;
2737 return tmp;
2740 /* Get a constraint expression vector from an SSA_VAR_P node.
2741 If address_p is true, the result will be taken its address of. */
2743 static void
2744 get_constraint_for_ssa_var (tree t, VEC(ce_s, heap) **results, bool address_p)
2746 struct constraint_expr cexpr;
2747 varinfo_t vi;
2749 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2750 gcc_assert (TREE_CODE (t) == SSA_NAME || DECL_P (t));
2752 /* For parameters, get at the points-to set for the actual parm
2753 decl. */
2754 if (TREE_CODE (t) == SSA_NAME
2755 && SSA_NAME_IS_DEFAULT_DEF (t)
2756 && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2757 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL))
2759 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2760 return;
2763 /* For global variables resort to the alias target. */
2764 if (TREE_CODE (t) == VAR_DECL
2765 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
2767 struct varpool_node *node = varpool_get_node (t);
2768 if (node && node->alias)
2770 node = varpool_variable_node (node, NULL);
2771 t = node->symbol.decl;
2775 vi = get_vi_for_tree (t);
2776 cexpr.var = vi->id;
2777 cexpr.type = SCALAR;
2778 cexpr.offset = 0;
2779 /* If we determine the result is "anything", and we know this is readonly,
2780 say it points to readonly memory instead. */
2781 if (cexpr.var == anything_id && TREE_READONLY (t))
2783 gcc_unreachable ();
2784 cexpr.type = ADDRESSOF;
2785 cexpr.var = readonly_id;
2788 /* If we are not taking the address of the constraint expr, add all
2789 sub-fiels of the variable as well. */
2790 if (!address_p
2791 && !vi->is_full_var)
2793 for (; vi; vi = vi->next)
2795 cexpr.var = vi->id;
2796 VEC_safe_push (ce_s, heap, *results, cexpr);
2798 return;
2801 VEC_safe_push (ce_s, heap, *results, cexpr);
2804 /* Process constraint T, performing various simplifications and then
2805 adding it to our list of overall constraints. */
2807 static void
2808 process_constraint (constraint_t t)
2810 struct constraint_expr rhs = t->rhs;
2811 struct constraint_expr lhs = t->lhs;
2813 gcc_assert (rhs.var < VEC_length (varinfo_t, varmap));
2814 gcc_assert (lhs.var < VEC_length (varinfo_t, varmap));
2816 /* If we didn't get any useful constraint from the lhs we get
2817 &ANYTHING as fallback from get_constraint_for. Deal with
2818 it here by turning it into *ANYTHING. */
2819 if (lhs.type == ADDRESSOF
2820 && lhs.var == anything_id)
2821 lhs.type = DEREF;
2823 /* ADDRESSOF on the lhs is invalid. */
2824 gcc_assert (lhs.type != ADDRESSOF);
2826 /* We shouldn't add constraints from things that cannot have pointers.
2827 It's not completely trivial to avoid in the callers, so do it here. */
2828 if (rhs.type != ADDRESSOF
2829 && !get_varinfo (rhs.var)->may_have_pointers)
2830 return;
2832 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2833 if (!get_varinfo (lhs.var)->may_have_pointers)
2834 return;
2836 /* This can happen in our IR with things like n->a = *p */
2837 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
2839 /* Split into tmp = *rhs, *lhs = tmp */
2840 struct constraint_expr tmplhs;
2841 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp");
2842 process_constraint (new_constraint (tmplhs, rhs));
2843 process_constraint (new_constraint (lhs, tmplhs));
2845 else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
2847 /* Split into tmp = &rhs, *lhs = tmp */
2848 struct constraint_expr tmplhs;
2849 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp");
2850 process_constraint (new_constraint (tmplhs, rhs));
2851 process_constraint (new_constraint (lhs, tmplhs));
2853 else
2855 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
2856 VEC_safe_push (constraint_t, heap, constraints, t);
2861 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2862 structure. */
2864 static HOST_WIDE_INT
2865 bitpos_of_field (const tree fdecl)
2867 if (!host_integerp (DECL_FIELD_OFFSET (fdecl), 0)
2868 || !host_integerp (DECL_FIELD_BIT_OFFSET (fdecl), 0))
2869 return -1;
2871 return (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
2872 + TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (fdecl)));
2876 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
2877 resulting constraint expressions in *RESULTS. */
2879 static void
2880 get_constraint_for_ptr_offset (tree ptr, tree offset,
2881 VEC (ce_s, heap) **results)
2883 struct constraint_expr c;
2884 unsigned int j, n;
2885 HOST_WIDE_INT rhsoffset;
2887 /* If we do not do field-sensitive PTA adding offsets to pointers
2888 does not change the points-to solution. */
2889 if (!use_field_sensitive)
2891 get_constraint_for_rhs (ptr, results);
2892 return;
2895 /* If the offset is not a non-negative integer constant that fits
2896 in a HOST_WIDE_INT, we have to fall back to a conservative
2897 solution which includes all sub-fields of all pointed-to
2898 variables of ptr. */
2899 if (offset == NULL_TREE
2900 || TREE_CODE (offset) != INTEGER_CST)
2901 rhsoffset = UNKNOWN_OFFSET;
2902 else
2904 /* Sign-extend the offset. */
2905 double_int soffset = tree_to_double_int (offset)
2906 .sext (TYPE_PRECISION (TREE_TYPE (offset)));
2907 if (!soffset.fits_shwi ())
2908 rhsoffset = UNKNOWN_OFFSET;
2909 else
2911 /* Make sure the bit-offset also fits. */
2912 HOST_WIDE_INT rhsunitoffset = soffset.low;
2913 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
2914 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
2915 rhsoffset = UNKNOWN_OFFSET;
2919 get_constraint_for_rhs (ptr, results);
2920 if (rhsoffset == 0)
2921 return;
2923 /* As we are eventually appending to the solution do not use
2924 VEC_iterate here. */
2925 n = VEC_length (ce_s, *results);
2926 for (j = 0; j < n; j++)
2928 varinfo_t curr;
2929 c = VEC_index (ce_s, *results, j);
2930 curr = get_varinfo (c.var);
2932 if (c.type == ADDRESSOF
2933 /* If this varinfo represents a full variable just use it. */
2934 && curr->is_full_var)
2935 c.offset = 0;
2936 else if (c.type == ADDRESSOF
2937 /* If we do not know the offset add all subfields. */
2938 && rhsoffset == UNKNOWN_OFFSET)
2940 varinfo_t temp = lookup_vi_for_tree (curr->decl);
2943 struct constraint_expr c2;
2944 c2.var = temp->id;
2945 c2.type = ADDRESSOF;
2946 c2.offset = 0;
2947 if (c2.var != c.var)
2948 VEC_safe_push (ce_s, heap, *results, c2);
2949 temp = temp->next;
2951 while (temp);
2953 else if (c.type == ADDRESSOF)
2955 varinfo_t temp;
2956 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
2958 /* Search the sub-field which overlaps with the
2959 pointed-to offset. If the result is outside of the variable
2960 we have to provide a conservative result, as the variable is
2961 still reachable from the resulting pointer (even though it
2962 technically cannot point to anything). The last and first
2963 sub-fields are such conservative results.
2964 ??? If we always had a sub-field for &object + 1 then
2965 we could represent this in a more precise way. */
2966 if (rhsoffset < 0
2967 && curr->offset < offset)
2968 offset = 0;
2969 temp = first_or_preceding_vi_for_offset (curr, offset);
2971 /* If the found variable is not exactly at the pointed to
2972 result, we have to include the next variable in the
2973 solution as well. Otherwise two increments by offset / 2
2974 do not result in the same or a conservative superset
2975 solution. */
2976 if (temp->offset != offset
2977 && temp->next != NULL)
2979 struct constraint_expr c2;
2980 c2.var = temp->next->id;
2981 c2.type = ADDRESSOF;
2982 c2.offset = 0;
2983 VEC_safe_push (ce_s, heap, *results, c2);
2985 c.var = temp->id;
2986 c.offset = 0;
2988 else
2989 c.offset = rhsoffset;
2991 VEC_replace (ce_s, *results, j, c);
2996 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
2997 If address_p is true the result will be taken its address of.
2998 If lhs_p is true then the constraint expression is assumed to be used
2999 as the lhs. */
3001 static void
3002 get_constraint_for_component_ref (tree t, VEC(ce_s, heap) **results,
3003 bool address_p, bool lhs_p)
3005 tree orig_t = t;
3006 HOST_WIDE_INT bitsize = -1;
3007 HOST_WIDE_INT bitmaxsize = -1;
3008 HOST_WIDE_INT bitpos;
3009 tree forzero;
3010 struct constraint_expr *result;
3012 /* Some people like to do cute things like take the address of
3013 &0->a.b */
3014 forzero = t;
3015 while (handled_component_p (forzero)
3016 || INDIRECT_REF_P (forzero)
3017 || TREE_CODE (forzero) == MEM_REF)
3018 forzero = TREE_OPERAND (forzero, 0);
3020 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
3022 struct constraint_expr temp;
3024 temp.offset = 0;
3025 temp.var = integer_id;
3026 temp.type = SCALAR;
3027 VEC_safe_push (ce_s, heap, *results, temp);
3028 return;
3031 /* Handle type-punning through unions. If we are extracting a pointer
3032 from a union via a possibly type-punning access that pointer
3033 points to anything, similar to a conversion of an integer to
3034 a pointer. */
3035 if (!lhs_p)
3037 tree u;
3038 for (u = t;
3039 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3040 u = TREE_OPERAND (u, 0))
3041 if (TREE_CODE (u) == COMPONENT_REF
3042 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3044 struct constraint_expr temp;
3046 temp.offset = 0;
3047 temp.var = anything_id;
3048 temp.type = ADDRESSOF;
3049 VEC_safe_push (ce_s, heap, *results, temp);
3050 return;
3054 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
3056 /* Pretend to take the address of the base, we'll take care of
3057 adding the required subset of sub-fields below. */
3058 get_constraint_for_1 (t, results, true, lhs_p);
3059 gcc_assert (VEC_length (ce_s, *results) == 1);
3060 result = &VEC_last (ce_s, *results);
3062 if (result->type == SCALAR
3063 && get_varinfo (result->var)->is_full_var)
3064 /* For single-field vars do not bother about the offset. */
3065 result->offset = 0;
3066 else if (result->type == SCALAR)
3068 /* In languages like C, you can access one past the end of an
3069 array. You aren't allowed to dereference it, so we can
3070 ignore this constraint. When we handle pointer subtraction,
3071 we may have to do something cute here. */
3073 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result->var)->fullsize
3074 && bitmaxsize != 0)
3076 /* It's also not true that the constraint will actually start at the
3077 right offset, it may start in some padding. We only care about
3078 setting the constraint to the first actual field it touches, so
3079 walk to find it. */
3080 struct constraint_expr cexpr = *result;
3081 varinfo_t curr;
3082 VEC_pop (ce_s, *results);
3083 cexpr.offset = 0;
3084 for (curr = get_varinfo (cexpr.var); curr; curr = curr->next)
3086 if (ranges_overlap_p (curr->offset, curr->size,
3087 bitpos, bitmaxsize))
3089 cexpr.var = curr->id;
3090 VEC_safe_push (ce_s, heap, *results, cexpr);
3091 if (address_p)
3092 break;
3095 /* If we are going to take the address of this field then
3096 to be able to compute reachability correctly add at least
3097 the last field of the variable. */
3098 if (address_p
3099 && VEC_length (ce_s, *results) == 0)
3101 curr = get_varinfo (cexpr.var);
3102 while (curr->next != NULL)
3103 curr = curr->next;
3104 cexpr.var = curr->id;
3105 VEC_safe_push (ce_s, heap, *results, cexpr);
3107 else if (VEC_length (ce_s, *results) == 0)
3108 /* Assert that we found *some* field there. The user couldn't be
3109 accessing *only* padding. */
3110 /* Still the user could access one past the end of an array
3111 embedded in a struct resulting in accessing *only* padding. */
3112 /* Or accessing only padding via type-punning to a type
3113 that has a filed just in padding space. */
3115 cexpr.type = SCALAR;
3116 cexpr.var = anything_id;
3117 cexpr.offset = 0;
3118 VEC_safe_push (ce_s, heap, *results, cexpr);
3121 else if (bitmaxsize == 0)
3123 if (dump_file && (dump_flags & TDF_DETAILS))
3124 fprintf (dump_file, "Access to zero-sized part of variable,"
3125 "ignoring\n");
3127 else
3128 if (dump_file && (dump_flags & TDF_DETAILS))
3129 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3131 else if (result->type == DEREF)
3133 /* If we do not know exactly where the access goes say so. Note
3134 that only for non-structure accesses we know that we access
3135 at most one subfiled of any variable. */
3136 if (bitpos == -1
3137 || bitsize != bitmaxsize
3138 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
3139 || result->offset == UNKNOWN_OFFSET)
3140 result->offset = UNKNOWN_OFFSET;
3141 else
3142 result->offset += bitpos;
3144 else if (result->type == ADDRESSOF)
3146 /* We can end up here for component references on a
3147 VIEW_CONVERT_EXPR <>(&foobar). */
3148 result->type = SCALAR;
3149 result->var = anything_id;
3150 result->offset = 0;
3152 else
3153 gcc_unreachable ();
3157 /* Dereference the constraint expression CONS, and return the result.
3158 DEREF (ADDRESSOF) = SCALAR
3159 DEREF (SCALAR) = DEREF
3160 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3161 This is needed so that we can handle dereferencing DEREF constraints. */
3163 static void
3164 do_deref (VEC (ce_s, heap) **constraints)
3166 struct constraint_expr *c;
3167 unsigned int i = 0;
3169 FOR_EACH_VEC_ELT (ce_s, *constraints, i, c)
3171 if (c->type == SCALAR)
3172 c->type = DEREF;
3173 else if (c->type == ADDRESSOF)
3174 c->type = SCALAR;
3175 else if (c->type == DEREF)
3177 struct constraint_expr tmplhs;
3178 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp");
3179 process_constraint (new_constraint (tmplhs, *c));
3180 c->var = tmplhs.var;
3182 else
3183 gcc_unreachable ();
3187 /* Given a tree T, return the constraint expression for taking the
3188 address of it. */
3190 static void
3191 get_constraint_for_address_of (tree t, VEC (ce_s, heap) **results)
3193 struct constraint_expr *c;
3194 unsigned int i;
3196 get_constraint_for_1 (t, results, true, true);
3198 FOR_EACH_VEC_ELT (ce_s, *results, i, c)
3200 if (c->type == DEREF)
3201 c->type = SCALAR;
3202 else
3203 c->type = ADDRESSOF;
3207 /* Given a tree T, return the constraint expression for it. */
3209 static void
3210 get_constraint_for_1 (tree t, VEC (ce_s, heap) **results, bool address_p,
3211 bool lhs_p)
3213 struct constraint_expr temp;
3215 /* x = integer is all glommed to a single variable, which doesn't
3216 point to anything by itself. That is, of course, unless it is an
3217 integer constant being treated as a pointer, in which case, we
3218 will return that this is really the addressof anything. This
3219 happens below, since it will fall into the default case. The only
3220 case we know something about an integer treated like a pointer is
3221 when it is the NULL pointer, and then we just say it points to
3222 NULL.
3224 Do not do that if -fno-delete-null-pointer-checks though, because
3225 in that case *NULL does not fail, so it _should_ alias *anything.
3226 It is not worth adding a new option or renaming the existing one,
3227 since this case is relatively obscure. */
3228 if ((TREE_CODE (t) == INTEGER_CST
3229 && integer_zerop (t))
3230 /* The only valid CONSTRUCTORs in gimple with pointer typed
3231 elements are zero-initializer. But in IPA mode we also
3232 process global initializers, so verify at least. */
3233 || (TREE_CODE (t) == CONSTRUCTOR
3234 && CONSTRUCTOR_NELTS (t) == 0))
3236 if (flag_delete_null_pointer_checks)
3237 temp.var = nothing_id;
3238 else
3239 temp.var = nonlocal_id;
3240 temp.type = ADDRESSOF;
3241 temp.offset = 0;
3242 VEC_safe_push (ce_s, heap, *results, temp);
3243 return;
3246 /* String constants are read-only. */
3247 if (TREE_CODE (t) == STRING_CST)
3249 temp.var = readonly_id;
3250 temp.type = SCALAR;
3251 temp.offset = 0;
3252 VEC_safe_push (ce_s, heap, *results, temp);
3253 return;
3256 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3258 case tcc_expression:
3260 switch (TREE_CODE (t))
3262 case ADDR_EXPR:
3263 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3264 return;
3265 default:;
3267 break;
3269 case tcc_reference:
3271 switch (TREE_CODE (t))
3273 case MEM_REF:
3275 struct constraint_expr cs;
3276 varinfo_t vi, curr;
3277 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0),
3278 TREE_OPERAND (t, 1), results);
3279 do_deref (results);
3281 /* If we are not taking the address then make sure to process
3282 all subvariables we might access. */
3283 if (address_p)
3284 return;
3286 cs = VEC_last (ce_s, *results);
3287 if (cs.type == DEREF
3288 && type_can_have_subvars (TREE_TYPE (t)))
3290 /* For dereferences this means we have to defer it
3291 to solving time. */
3292 VEC_last (ce_s, *results).offset = UNKNOWN_OFFSET;
3293 return;
3295 if (cs.type != SCALAR)
3296 return;
3298 vi = get_varinfo (cs.var);
3299 curr = vi->next;
3300 if (!vi->is_full_var
3301 && curr)
3303 unsigned HOST_WIDE_INT size;
3304 if (host_integerp (TYPE_SIZE (TREE_TYPE (t)), 1))
3305 size = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (t)));
3306 else
3307 size = -1;
3308 for (; curr; curr = curr->next)
3310 if (curr->offset - vi->offset < size)
3312 cs.var = curr->id;
3313 VEC_safe_push (ce_s, heap, *results, cs);
3315 else
3316 break;
3319 return;
3321 case ARRAY_REF:
3322 case ARRAY_RANGE_REF:
3323 case COMPONENT_REF:
3324 get_constraint_for_component_ref (t, results, address_p, lhs_p);
3325 return;
3326 case VIEW_CONVERT_EXPR:
3327 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p,
3328 lhs_p);
3329 return;
3330 /* We are missing handling for TARGET_MEM_REF here. */
3331 default:;
3333 break;
3335 case tcc_exceptional:
3337 switch (TREE_CODE (t))
3339 case SSA_NAME:
3341 get_constraint_for_ssa_var (t, results, address_p);
3342 return;
3344 case CONSTRUCTOR:
3346 unsigned int i;
3347 tree val;
3348 VEC (ce_s, heap) *tmp = NULL;
3349 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
3351 struct constraint_expr *rhsp;
3352 unsigned j;
3353 get_constraint_for_1 (val, &tmp, address_p, lhs_p);
3354 FOR_EACH_VEC_ELT (ce_s, tmp, j, rhsp)
3355 VEC_safe_push (ce_s, heap, *results, *rhsp);
3356 VEC_truncate (ce_s, tmp, 0);
3358 VEC_free (ce_s, heap, tmp);
3359 /* We do not know whether the constructor was complete,
3360 so technically we have to add &NOTHING or &ANYTHING
3361 like we do for an empty constructor as well. */
3362 return;
3364 default:;
3366 break;
3368 case tcc_declaration:
3370 get_constraint_for_ssa_var (t, results, address_p);
3371 return;
3373 case tcc_constant:
3375 /* We cannot refer to automatic variables through constants. */
3376 temp.type = ADDRESSOF;
3377 temp.var = nonlocal_id;
3378 temp.offset = 0;
3379 VEC_safe_push (ce_s, heap, *results, temp);
3380 return;
3382 default:;
3385 /* The default fallback is a constraint from anything. */
3386 temp.type = ADDRESSOF;
3387 temp.var = anything_id;
3388 temp.offset = 0;
3389 VEC_safe_push (ce_s, heap, *results, temp);
3392 /* Given a gimple tree T, return the constraint expression vector for it. */
3394 static void
3395 get_constraint_for (tree t, VEC (ce_s, heap) **results)
3397 gcc_assert (VEC_length (ce_s, *results) == 0);
3399 get_constraint_for_1 (t, results, false, true);
3402 /* Given a gimple tree T, return the constraint expression vector for it
3403 to be used as the rhs of a constraint. */
3405 static void
3406 get_constraint_for_rhs (tree t, VEC (ce_s, heap) **results)
3408 gcc_assert (VEC_length (ce_s, *results) == 0);
3410 get_constraint_for_1 (t, results, false, false);
3414 /* Efficiently generates constraints from all entries in *RHSC to all
3415 entries in *LHSC. */
3417 static void
3418 process_all_all_constraints (VEC (ce_s, heap) *lhsc, VEC (ce_s, heap) *rhsc)
3420 struct constraint_expr *lhsp, *rhsp;
3421 unsigned i, j;
3423 if (VEC_length (ce_s, lhsc) <= 1
3424 || VEC_length (ce_s, rhsc) <= 1)
3426 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
3427 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
3428 process_constraint (new_constraint (*lhsp, *rhsp));
3430 else
3432 struct constraint_expr tmp;
3433 tmp = new_scalar_tmp_constraint_exp ("allalltmp");
3434 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
3435 process_constraint (new_constraint (tmp, *rhsp));
3436 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
3437 process_constraint (new_constraint (*lhsp, tmp));
3441 /* Handle aggregate copies by expanding into copies of the respective
3442 fields of the structures. */
3444 static void
3445 do_structure_copy (tree lhsop, tree rhsop)
3447 struct constraint_expr *lhsp, *rhsp;
3448 VEC (ce_s, heap) *lhsc = NULL, *rhsc = NULL;
3449 unsigned j;
3451 get_constraint_for (lhsop, &lhsc);
3452 get_constraint_for_rhs (rhsop, &rhsc);
3453 lhsp = &VEC_index (ce_s, lhsc, 0);
3454 rhsp = &VEC_index (ce_s, rhsc, 0);
3455 if (lhsp->type == DEREF
3456 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3457 || rhsp->type == DEREF)
3459 if (lhsp->type == DEREF)
3461 gcc_assert (VEC_length (ce_s, lhsc) == 1);
3462 lhsp->offset = UNKNOWN_OFFSET;
3464 if (rhsp->type == DEREF)
3466 gcc_assert (VEC_length (ce_s, rhsc) == 1);
3467 rhsp->offset = UNKNOWN_OFFSET;
3469 process_all_all_constraints (lhsc, rhsc);
3471 else if (lhsp->type == SCALAR
3472 && (rhsp->type == SCALAR
3473 || rhsp->type == ADDRESSOF))
3475 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3476 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
3477 unsigned k = 0;
3478 get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize);
3479 get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize);
3480 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp);)
3482 varinfo_t lhsv, rhsv;
3483 rhsp = &VEC_index (ce_s, rhsc, k);
3484 lhsv = get_varinfo (lhsp->var);
3485 rhsv = get_varinfo (rhsp->var);
3486 if (lhsv->may_have_pointers
3487 && (lhsv->is_full_var
3488 || rhsv->is_full_var
3489 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3490 rhsv->offset + lhsoffset, rhsv->size)))
3491 process_constraint (new_constraint (*lhsp, *rhsp));
3492 if (!rhsv->is_full_var
3493 && (lhsv->is_full_var
3494 || (lhsv->offset + rhsoffset + lhsv->size
3495 > rhsv->offset + lhsoffset + rhsv->size)))
3497 ++k;
3498 if (k >= VEC_length (ce_s, rhsc))
3499 break;
3501 else
3502 ++j;
3505 else
3506 gcc_unreachable ();
3508 VEC_free (ce_s, heap, lhsc);
3509 VEC_free (ce_s, heap, rhsc);
3512 /* Create constraints ID = { rhsc }. */
3514 static void
3515 make_constraints_to (unsigned id, VEC(ce_s, heap) *rhsc)
3517 struct constraint_expr *c;
3518 struct constraint_expr includes;
3519 unsigned int j;
3521 includes.var = id;
3522 includes.offset = 0;
3523 includes.type = SCALAR;
3525 FOR_EACH_VEC_ELT (ce_s, rhsc, j, c)
3526 process_constraint (new_constraint (includes, *c));
3529 /* Create a constraint ID = OP. */
3531 static void
3532 make_constraint_to (unsigned id, tree op)
3534 VEC(ce_s, heap) *rhsc = NULL;
3535 get_constraint_for_rhs (op, &rhsc);
3536 make_constraints_to (id, rhsc);
3537 VEC_free (ce_s, heap, rhsc);
3540 /* Create a constraint ID = &FROM. */
3542 static void
3543 make_constraint_from (varinfo_t vi, int from)
3545 struct constraint_expr lhs, rhs;
3547 lhs.var = vi->id;
3548 lhs.offset = 0;
3549 lhs.type = SCALAR;
3551 rhs.var = from;
3552 rhs.offset = 0;
3553 rhs.type = ADDRESSOF;
3554 process_constraint (new_constraint (lhs, rhs));
3557 /* Create a constraint ID = FROM. */
3559 static void
3560 make_copy_constraint (varinfo_t vi, int from)
3562 struct constraint_expr lhs, rhs;
3564 lhs.var = vi->id;
3565 lhs.offset = 0;
3566 lhs.type = SCALAR;
3568 rhs.var = from;
3569 rhs.offset = 0;
3570 rhs.type = SCALAR;
3571 process_constraint (new_constraint (lhs, rhs));
3574 /* Make constraints necessary to make OP escape. */
3576 static void
3577 make_escape_constraint (tree op)
3579 make_constraint_to (escaped_id, op);
3582 /* Add constraints to that the solution of VI is transitively closed. */
3584 static void
3585 make_transitive_closure_constraints (varinfo_t vi)
3587 struct constraint_expr lhs, rhs;
3589 /* VAR = *VAR; */
3590 lhs.type = SCALAR;
3591 lhs.var = vi->id;
3592 lhs.offset = 0;
3593 rhs.type = DEREF;
3594 rhs.var = vi->id;
3595 rhs.offset = 0;
3596 process_constraint (new_constraint (lhs, rhs));
3598 /* VAR = VAR + UNKNOWN; */
3599 lhs.type = SCALAR;
3600 lhs.var = vi->id;
3601 lhs.offset = 0;
3602 rhs.type = SCALAR;
3603 rhs.var = vi->id;
3604 rhs.offset = UNKNOWN_OFFSET;
3605 process_constraint (new_constraint (lhs, rhs));
3608 /* Temporary storage for fake var decls. */
3609 struct obstack fake_var_decl_obstack;
3611 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3613 static tree
3614 build_fake_var_decl (tree type)
3616 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl);
3617 memset (decl, 0, sizeof (struct tree_var_decl));
3618 TREE_SET_CODE (decl, VAR_DECL);
3619 TREE_TYPE (decl) = type;
3620 DECL_UID (decl) = allocate_decl_uid ();
3621 SET_DECL_PT_UID (decl, -1);
3622 layout_decl (decl, 0);
3623 return decl;
3626 /* Create a new artificial heap variable with NAME.
3627 Return the created variable. */
3629 static varinfo_t
3630 make_heapvar (const char *name)
3632 varinfo_t vi;
3633 tree heapvar;
3635 heapvar = build_fake_var_decl (ptr_type_node);
3636 DECL_EXTERNAL (heapvar) = 1;
3638 vi = new_var_info (heapvar, name);
3639 vi->is_artificial_var = true;
3640 vi->is_heap_var = true;
3641 vi->is_unknown_size_var = true;
3642 vi->offset = 0;
3643 vi->fullsize = ~0;
3644 vi->size = ~0;
3645 vi->is_full_var = true;
3646 insert_vi_for_tree (heapvar, vi);
3648 return vi;
3651 /* Create a new artificial heap variable with NAME and make a
3652 constraint from it to LHS. Set flags according to a tag used
3653 for tracking restrict pointers. */
3655 static varinfo_t
3656 make_constraint_from_restrict (varinfo_t lhs, const char *name)
3658 varinfo_t vi = make_heapvar (name);
3659 vi->is_global_var = 1;
3660 vi->may_have_pointers = 1;
3661 make_constraint_from (lhs, vi->id);
3662 return vi;
3665 /* Create a new artificial heap variable with NAME and make a
3666 constraint from it to LHS. Set flags according to a tag used
3667 for tracking restrict pointers and make the artificial heap
3668 point to global memory. */
3670 static varinfo_t
3671 make_constraint_from_global_restrict (varinfo_t lhs, const char *name)
3673 varinfo_t vi = make_constraint_from_restrict (lhs, name);
3674 make_copy_constraint (vi, nonlocal_id);
3675 return vi;
3678 /* In IPA mode there are varinfos for different aspects of reach
3679 function designator. One for the points-to set of the return
3680 value, one for the variables that are clobbered by the function,
3681 one for its uses and one for each parameter (including a single
3682 glob for remaining variadic arguments). */
3684 enum { fi_clobbers = 1, fi_uses = 2,
3685 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 };
3687 /* Get a constraint for the requested part of a function designator FI
3688 when operating in IPA mode. */
3690 static struct constraint_expr
3691 get_function_part_constraint (varinfo_t fi, unsigned part)
3693 struct constraint_expr c;
3695 gcc_assert (in_ipa_mode);
3697 if (fi->id == anything_id)
3699 /* ??? We probably should have a ANYFN special variable. */
3700 c.var = anything_id;
3701 c.offset = 0;
3702 c.type = SCALAR;
3704 else if (TREE_CODE (fi->decl) == FUNCTION_DECL)
3706 varinfo_t ai = first_vi_for_offset (fi, part);
3707 if (ai)
3708 c.var = ai->id;
3709 else
3710 c.var = anything_id;
3711 c.offset = 0;
3712 c.type = SCALAR;
3714 else
3716 c.var = fi->id;
3717 c.offset = part;
3718 c.type = DEREF;
3721 return c;
3724 /* For non-IPA mode, generate constraints necessary for a call on the
3725 RHS. */
3727 static void
3728 handle_rhs_call (gimple stmt, VEC(ce_s, heap) **results)
3730 struct constraint_expr rhsc;
3731 unsigned i;
3732 bool returns_uses = false;
3734 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3736 tree arg = gimple_call_arg (stmt, i);
3737 int flags = gimple_call_arg_flags (stmt, i);
3739 /* If the argument is not used we can ignore it. */
3740 if (flags & EAF_UNUSED)
3741 continue;
3743 /* As we compute ESCAPED context-insensitive we do not gain
3744 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3745 set. The argument would still get clobbered through the
3746 escape solution. */
3747 if ((flags & EAF_NOCLOBBER)
3748 && (flags & EAF_NOESCAPE))
3750 varinfo_t uses = get_call_use_vi (stmt);
3751 if (!(flags & EAF_DIRECT))
3753 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3754 make_constraint_to (tem->id, arg);
3755 make_transitive_closure_constraints (tem);
3756 make_copy_constraint (uses, tem->id);
3758 else
3759 make_constraint_to (uses->id, arg);
3760 returns_uses = true;
3762 else if (flags & EAF_NOESCAPE)
3764 struct constraint_expr lhs, rhs;
3765 varinfo_t uses = get_call_use_vi (stmt);
3766 varinfo_t clobbers = get_call_clobber_vi (stmt);
3767 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3768 make_constraint_to (tem->id, arg);
3769 if (!(flags & EAF_DIRECT))
3770 make_transitive_closure_constraints (tem);
3771 make_copy_constraint (uses, tem->id);
3772 make_copy_constraint (clobbers, tem->id);
3773 /* Add *tem = nonlocal, do not add *tem = callused as
3774 EAF_NOESCAPE parameters do not escape to other parameters
3775 and all other uses appear in NONLOCAL as well. */
3776 lhs.type = DEREF;
3777 lhs.var = tem->id;
3778 lhs.offset = 0;
3779 rhs.type = SCALAR;
3780 rhs.var = nonlocal_id;
3781 rhs.offset = 0;
3782 process_constraint (new_constraint (lhs, rhs));
3783 returns_uses = true;
3785 else
3786 make_escape_constraint (arg);
3789 /* If we added to the calls uses solution make sure we account for
3790 pointers to it to be returned. */
3791 if (returns_uses)
3793 rhsc.var = get_call_use_vi (stmt)->id;
3794 rhsc.offset = 0;
3795 rhsc.type = SCALAR;
3796 VEC_safe_push (ce_s, heap, *results, rhsc);
3799 /* The static chain escapes as well. */
3800 if (gimple_call_chain (stmt))
3801 make_escape_constraint (gimple_call_chain (stmt));
3803 /* And if we applied NRV the address of the return slot escapes as well. */
3804 if (gimple_call_return_slot_opt_p (stmt)
3805 && gimple_call_lhs (stmt) != NULL_TREE
3806 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3808 VEC(ce_s, heap) *tmpc = NULL;
3809 struct constraint_expr lhsc, *c;
3810 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3811 lhsc.var = escaped_id;
3812 lhsc.offset = 0;
3813 lhsc.type = SCALAR;
3814 FOR_EACH_VEC_ELT (ce_s, tmpc, i, c)
3815 process_constraint (new_constraint (lhsc, *c));
3816 VEC_free(ce_s, heap, tmpc);
3819 /* Regular functions return nonlocal memory. */
3820 rhsc.var = nonlocal_id;
3821 rhsc.offset = 0;
3822 rhsc.type = SCALAR;
3823 VEC_safe_push (ce_s, heap, *results, rhsc);
3826 /* For non-IPA mode, generate constraints necessary for a call
3827 that returns a pointer and assigns it to LHS. This simply makes
3828 the LHS point to global and escaped variables. */
3830 static void
3831 handle_lhs_call (gimple stmt, tree lhs, int flags, VEC(ce_s, heap) *rhsc,
3832 tree fndecl)
3834 VEC(ce_s, heap) *lhsc = NULL;
3836 get_constraint_for (lhs, &lhsc);
3837 /* If the store is to a global decl make sure to
3838 add proper escape constraints. */
3839 lhs = get_base_address (lhs);
3840 if (lhs
3841 && DECL_P (lhs)
3842 && is_global_var (lhs))
3844 struct constraint_expr tmpc;
3845 tmpc.var = escaped_id;
3846 tmpc.offset = 0;
3847 tmpc.type = SCALAR;
3848 VEC_safe_push (ce_s, heap, lhsc, tmpc);
3851 /* If the call returns an argument unmodified override the rhs
3852 constraints. */
3853 flags = gimple_call_return_flags (stmt);
3854 if (flags & ERF_RETURNS_ARG
3855 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
3857 tree arg;
3858 rhsc = NULL;
3859 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
3860 get_constraint_for (arg, &rhsc);
3861 process_all_all_constraints (lhsc, rhsc);
3862 VEC_free (ce_s, heap, rhsc);
3864 else if (flags & ERF_NOALIAS)
3866 varinfo_t vi;
3867 struct constraint_expr tmpc;
3868 rhsc = NULL;
3869 vi = make_heapvar ("HEAP");
3870 /* We delay marking allocated storage global until we know if
3871 it escapes. */
3872 DECL_EXTERNAL (vi->decl) = 0;
3873 vi->is_global_var = 0;
3874 /* If this is not a real malloc call assume the memory was
3875 initialized and thus may point to global memory. All
3876 builtin functions with the malloc attribute behave in a sane way. */
3877 if (!fndecl
3878 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3879 make_constraint_from (vi, nonlocal_id);
3880 tmpc.var = vi->id;
3881 tmpc.offset = 0;
3882 tmpc.type = ADDRESSOF;
3883 VEC_safe_push (ce_s, heap, rhsc, tmpc);
3884 process_all_all_constraints (lhsc, rhsc);
3885 VEC_free (ce_s, heap, rhsc);
3887 else
3888 process_all_all_constraints (lhsc, rhsc);
3890 VEC_free (ce_s, heap, lhsc);
3893 /* For non-IPA mode, generate constraints necessary for a call of a
3894 const function that returns a pointer in the statement STMT. */
3896 static void
3897 handle_const_call (gimple stmt, VEC(ce_s, heap) **results)
3899 struct constraint_expr rhsc;
3900 unsigned int k;
3902 /* Treat nested const functions the same as pure functions as far
3903 as the static chain is concerned. */
3904 if (gimple_call_chain (stmt))
3906 varinfo_t uses = get_call_use_vi (stmt);
3907 make_transitive_closure_constraints (uses);
3908 make_constraint_to (uses->id, gimple_call_chain (stmt));
3909 rhsc.var = uses->id;
3910 rhsc.offset = 0;
3911 rhsc.type = SCALAR;
3912 VEC_safe_push (ce_s, heap, *results, rhsc);
3915 /* May return arguments. */
3916 for (k = 0; k < gimple_call_num_args (stmt); ++k)
3918 tree arg = gimple_call_arg (stmt, k);
3919 VEC(ce_s, heap) *argc = NULL;
3920 unsigned i;
3921 struct constraint_expr *argp;
3922 get_constraint_for_rhs (arg, &argc);
3923 FOR_EACH_VEC_ELT (ce_s, argc, i, argp)
3924 VEC_safe_push (ce_s, heap, *results, *argp);
3925 VEC_free(ce_s, heap, argc);
3928 /* May return addresses of globals. */
3929 rhsc.var = nonlocal_id;
3930 rhsc.offset = 0;
3931 rhsc.type = ADDRESSOF;
3932 VEC_safe_push (ce_s, heap, *results, rhsc);
3935 /* For non-IPA mode, generate constraints necessary for a call to a
3936 pure function in statement STMT. */
3938 static void
3939 handle_pure_call (gimple stmt, VEC(ce_s, heap) **results)
3941 struct constraint_expr rhsc;
3942 unsigned i;
3943 varinfo_t uses = NULL;
3945 /* Memory reached from pointer arguments is call-used. */
3946 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3948 tree arg = gimple_call_arg (stmt, i);
3949 if (!uses)
3951 uses = get_call_use_vi (stmt);
3952 make_transitive_closure_constraints (uses);
3954 make_constraint_to (uses->id, arg);
3957 /* The static chain is used as well. */
3958 if (gimple_call_chain (stmt))
3960 if (!uses)
3962 uses = get_call_use_vi (stmt);
3963 make_transitive_closure_constraints (uses);
3965 make_constraint_to (uses->id, gimple_call_chain (stmt));
3968 /* Pure functions may return call-used and nonlocal memory. */
3969 if (uses)
3971 rhsc.var = uses->id;
3972 rhsc.offset = 0;
3973 rhsc.type = SCALAR;
3974 VEC_safe_push (ce_s, heap, *results, rhsc);
3976 rhsc.var = nonlocal_id;
3977 rhsc.offset = 0;
3978 rhsc.type = SCALAR;
3979 VEC_safe_push (ce_s, heap, *results, rhsc);
3983 /* Return the varinfo for the callee of CALL. */
3985 static varinfo_t
3986 get_fi_for_callee (gimple call)
3988 tree decl, fn = gimple_call_fn (call);
3990 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
3991 fn = OBJ_TYPE_REF_EXPR (fn);
3993 /* If we can directly resolve the function being called, do so.
3994 Otherwise, it must be some sort of indirect expression that
3995 we should still be able to handle. */
3996 decl = gimple_call_addr_fndecl (fn);
3997 if (decl)
3998 return get_vi_for_tree (decl);
4000 /* If the function is anything other than a SSA name pointer we have no
4001 clue and should be getting ANYFN (well, ANYTHING for now). */
4002 if (!fn || TREE_CODE (fn) != SSA_NAME)
4003 return get_varinfo (anything_id);
4005 if (SSA_NAME_IS_DEFAULT_DEF (fn)
4006 && (TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
4007 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL))
4008 fn = SSA_NAME_VAR (fn);
4010 return get_vi_for_tree (fn);
4013 /* Create constraints for the builtin call T. Return true if the call
4014 was handled, otherwise false. */
4016 static bool
4017 find_func_aliases_for_builtin_call (gimple t)
4019 tree fndecl = gimple_call_fndecl (t);
4020 VEC(ce_s, heap) *lhsc = NULL;
4021 VEC(ce_s, heap) *rhsc = NULL;
4022 varinfo_t fi;
4024 if (fndecl != NULL_TREE
4025 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4026 /* ??? All builtins that are handled here need to be handled
4027 in the alias-oracle query functions explicitly! */
4028 switch (DECL_FUNCTION_CODE (fndecl))
4030 /* All the following functions return a pointer to the same object
4031 as their first argument points to. The functions do not add
4032 to the ESCAPED solution. The functions make the first argument
4033 pointed to memory point to what the second argument pointed to
4034 memory points to. */
4035 case BUILT_IN_STRCPY:
4036 case BUILT_IN_STRNCPY:
4037 case BUILT_IN_BCOPY:
4038 case BUILT_IN_MEMCPY:
4039 case BUILT_IN_MEMMOVE:
4040 case BUILT_IN_MEMPCPY:
4041 case BUILT_IN_STPCPY:
4042 case BUILT_IN_STPNCPY:
4043 case BUILT_IN_STRCAT:
4044 case BUILT_IN_STRNCAT:
4045 case BUILT_IN_STRCPY_CHK:
4046 case BUILT_IN_STRNCPY_CHK:
4047 case BUILT_IN_MEMCPY_CHK:
4048 case BUILT_IN_MEMMOVE_CHK:
4049 case BUILT_IN_MEMPCPY_CHK:
4050 case BUILT_IN_STPCPY_CHK:
4051 case BUILT_IN_STPNCPY_CHK:
4052 case BUILT_IN_STRCAT_CHK:
4053 case BUILT_IN_STRNCAT_CHK:
4054 case BUILT_IN_TM_MEMCPY:
4055 case BUILT_IN_TM_MEMMOVE:
4057 tree res = gimple_call_lhs (t);
4058 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4059 == BUILT_IN_BCOPY ? 1 : 0));
4060 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4061 == BUILT_IN_BCOPY ? 0 : 1));
4062 if (res != NULL_TREE)
4064 get_constraint_for (res, &lhsc);
4065 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4066 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
4067 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4068 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
4069 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK
4070 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK)
4071 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4072 else
4073 get_constraint_for (dest, &rhsc);
4074 process_all_all_constraints (lhsc, rhsc);
4075 VEC_free (ce_s, heap, lhsc);
4076 VEC_free (ce_s, heap, rhsc);
4078 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4079 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4080 do_deref (&lhsc);
4081 do_deref (&rhsc);
4082 process_all_all_constraints (lhsc, rhsc);
4083 VEC_free (ce_s, heap, lhsc);
4084 VEC_free (ce_s, heap, rhsc);
4085 return true;
4087 case BUILT_IN_MEMSET:
4088 case BUILT_IN_MEMSET_CHK:
4089 case BUILT_IN_TM_MEMSET:
4091 tree res = gimple_call_lhs (t);
4092 tree dest = gimple_call_arg (t, 0);
4093 unsigned i;
4094 ce_s *lhsp;
4095 struct constraint_expr ac;
4096 if (res != NULL_TREE)
4098 get_constraint_for (res, &lhsc);
4099 get_constraint_for (dest, &rhsc);
4100 process_all_all_constraints (lhsc, rhsc);
4101 VEC_free (ce_s, heap, lhsc);
4102 VEC_free (ce_s, heap, rhsc);
4104 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4105 do_deref (&lhsc);
4106 if (flag_delete_null_pointer_checks
4107 && integer_zerop (gimple_call_arg (t, 1)))
4109 ac.type = ADDRESSOF;
4110 ac.var = nothing_id;
4112 else
4114 ac.type = SCALAR;
4115 ac.var = integer_id;
4117 ac.offset = 0;
4118 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4119 process_constraint (new_constraint (*lhsp, ac));
4120 VEC_free (ce_s, heap, lhsc);
4121 return true;
4123 case BUILT_IN_ASSUME_ALIGNED:
4125 tree res = gimple_call_lhs (t);
4126 tree dest = gimple_call_arg (t, 0);
4127 if (res != NULL_TREE)
4129 get_constraint_for (res, &lhsc);
4130 get_constraint_for (dest, &rhsc);
4131 process_all_all_constraints (lhsc, rhsc);
4132 VEC_free (ce_s, heap, lhsc);
4133 VEC_free (ce_s, heap, rhsc);
4135 return true;
4137 /* All the following functions do not return pointers, do not
4138 modify the points-to sets of memory reachable from their
4139 arguments and do not add to the ESCAPED solution. */
4140 case BUILT_IN_SINCOS:
4141 case BUILT_IN_SINCOSF:
4142 case BUILT_IN_SINCOSL:
4143 case BUILT_IN_FREXP:
4144 case BUILT_IN_FREXPF:
4145 case BUILT_IN_FREXPL:
4146 case BUILT_IN_GAMMA_R:
4147 case BUILT_IN_GAMMAF_R:
4148 case BUILT_IN_GAMMAL_R:
4149 case BUILT_IN_LGAMMA_R:
4150 case BUILT_IN_LGAMMAF_R:
4151 case BUILT_IN_LGAMMAL_R:
4152 case BUILT_IN_MODF:
4153 case BUILT_IN_MODFF:
4154 case BUILT_IN_MODFL:
4155 case BUILT_IN_REMQUO:
4156 case BUILT_IN_REMQUOF:
4157 case BUILT_IN_REMQUOL:
4158 case BUILT_IN_FREE:
4159 return true;
4160 case BUILT_IN_STRDUP:
4161 case BUILT_IN_STRNDUP:
4162 if (gimple_call_lhs (t))
4164 handle_lhs_call (t, gimple_call_lhs (t), gimple_call_flags (t),
4165 NULL, fndecl);
4166 get_constraint_for_ptr_offset (gimple_call_lhs (t),
4167 NULL_TREE, &lhsc);
4168 get_constraint_for_ptr_offset (gimple_call_arg (t, 0),
4169 NULL_TREE, &rhsc);
4170 do_deref (&lhsc);
4171 do_deref (&rhsc);
4172 process_all_all_constraints (lhsc, rhsc);
4173 VEC_free (ce_s, heap, lhsc);
4174 VEC_free (ce_s, heap, rhsc);
4175 return true;
4177 break;
4178 /* Trampolines are special - they set up passing the static
4179 frame. */
4180 case BUILT_IN_INIT_TRAMPOLINE:
4182 tree tramp = gimple_call_arg (t, 0);
4183 tree nfunc = gimple_call_arg (t, 1);
4184 tree frame = gimple_call_arg (t, 2);
4185 unsigned i;
4186 struct constraint_expr lhs, *rhsp;
4187 if (in_ipa_mode)
4189 varinfo_t nfi = NULL;
4190 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR);
4191 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0));
4192 if (nfi)
4194 lhs = get_function_part_constraint (nfi, fi_static_chain);
4195 get_constraint_for (frame, &rhsc);
4196 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4197 process_constraint (new_constraint (lhs, *rhsp));
4198 VEC_free (ce_s, heap, rhsc);
4200 /* Make the frame point to the function for
4201 the trampoline adjustment call. */
4202 get_constraint_for (tramp, &lhsc);
4203 do_deref (&lhsc);
4204 get_constraint_for (nfunc, &rhsc);
4205 process_all_all_constraints (lhsc, rhsc);
4206 VEC_free (ce_s, heap, rhsc);
4207 VEC_free (ce_s, heap, lhsc);
4209 return true;
4212 /* Else fallthru to generic handling which will let
4213 the frame escape. */
4214 break;
4216 case BUILT_IN_ADJUST_TRAMPOLINE:
4218 tree tramp = gimple_call_arg (t, 0);
4219 tree res = gimple_call_lhs (t);
4220 if (in_ipa_mode && res)
4222 get_constraint_for (res, &lhsc);
4223 get_constraint_for (tramp, &rhsc);
4224 do_deref (&rhsc);
4225 process_all_all_constraints (lhsc, rhsc);
4226 VEC_free (ce_s, heap, rhsc);
4227 VEC_free (ce_s, heap, lhsc);
4229 return true;
4231 CASE_BUILT_IN_TM_STORE (1):
4232 CASE_BUILT_IN_TM_STORE (2):
4233 CASE_BUILT_IN_TM_STORE (4):
4234 CASE_BUILT_IN_TM_STORE (8):
4235 CASE_BUILT_IN_TM_STORE (FLOAT):
4236 CASE_BUILT_IN_TM_STORE (DOUBLE):
4237 CASE_BUILT_IN_TM_STORE (LDOUBLE):
4238 CASE_BUILT_IN_TM_STORE (M64):
4239 CASE_BUILT_IN_TM_STORE (M128):
4240 CASE_BUILT_IN_TM_STORE (M256):
4242 tree addr = gimple_call_arg (t, 0);
4243 tree src = gimple_call_arg (t, 1);
4245 get_constraint_for (addr, &lhsc);
4246 do_deref (&lhsc);
4247 get_constraint_for (src, &rhsc);
4248 process_all_all_constraints (lhsc, rhsc);
4249 VEC_free (ce_s, heap, lhsc);
4250 VEC_free (ce_s, heap, rhsc);
4251 return true;
4253 CASE_BUILT_IN_TM_LOAD (1):
4254 CASE_BUILT_IN_TM_LOAD (2):
4255 CASE_BUILT_IN_TM_LOAD (4):
4256 CASE_BUILT_IN_TM_LOAD (8):
4257 CASE_BUILT_IN_TM_LOAD (FLOAT):
4258 CASE_BUILT_IN_TM_LOAD (DOUBLE):
4259 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
4260 CASE_BUILT_IN_TM_LOAD (M64):
4261 CASE_BUILT_IN_TM_LOAD (M128):
4262 CASE_BUILT_IN_TM_LOAD (M256):
4264 tree dest = gimple_call_lhs (t);
4265 tree addr = gimple_call_arg (t, 0);
4267 get_constraint_for (dest, &lhsc);
4268 get_constraint_for (addr, &rhsc);
4269 do_deref (&rhsc);
4270 process_all_all_constraints (lhsc, rhsc);
4271 VEC_free (ce_s, heap, lhsc);
4272 VEC_free (ce_s, heap, rhsc);
4273 return true;
4275 /* Variadic argument handling needs to be handled in IPA
4276 mode as well. */
4277 case BUILT_IN_VA_START:
4279 tree valist = gimple_call_arg (t, 0);
4280 struct constraint_expr rhs, *lhsp;
4281 unsigned i;
4282 get_constraint_for (valist, &lhsc);
4283 do_deref (&lhsc);
4284 /* The va_list gets access to pointers in variadic
4285 arguments. Which we know in the case of IPA analysis
4286 and otherwise are just all nonlocal variables. */
4287 if (in_ipa_mode)
4289 fi = lookup_vi_for_tree (cfun->decl);
4290 rhs = get_function_part_constraint (fi, ~0);
4291 rhs.type = ADDRESSOF;
4293 else
4295 rhs.var = nonlocal_id;
4296 rhs.type = ADDRESSOF;
4297 rhs.offset = 0;
4299 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4300 process_constraint (new_constraint (*lhsp, rhs));
4301 VEC_free (ce_s, heap, lhsc);
4302 /* va_list is clobbered. */
4303 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4304 return true;
4306 /* va_end doesn't have any effect that matters. */
4307 case BUILT_IN_VA_END:
4308 return true;
4309 /* Alternate return. Simply give up for now. */
4310 case BUILT_IN_RETURN:
4312 fi = NULL;
4313 if (!in_ipa_mode
4314 || !(fi = get_vi_for_tree (cfun->decl)))
4315 make_constraint_from (get_varinfo (escaped_id), anything_id);
4316 else if (in_ipa_mode
4317 && fi != NULL)
4319 struct constraint_expr lhs, rhs;
4320 lhs = get_function_part_constraint (fi, fi_result);
4321 rhs.var = anything_id;
4322 rhs.offset = 0;
4323 rhs.type = SCALAR;
4324 process_constraint (new_constraint (lhs, rhs));
4326 return true;
4328 /* printf-style functions may have hooks to set pointers to
4329 point to somewhere into the generated string. Leave them
4330 for a later excercise... */
4331 default:
4332 /* Fallthru to general call handling. */;
4335 return false;
4338 /* Create constraints for the call T. */
4340 static void
4341 find_func_aliases_for_call (gimple t)
4343 tree fndecl = gimple_call_fndecl (t);
4344 VEC(ce_s, heap) *lhsc = NULL;
4345 VEC(ce_s, heap) *rhsc = NULL;
4346 varinfo_t fi;
4348 if (fndecl != NULL_TREE
4349 && DECL_BUILT_IN (fndecl)
4350 && find_func_aliases_for_builtin_call (t))
4351 return;
4353 fi = get_fi_for_callee (t);
4354 if (!in_ipa_mode
4355 || (fndecl && !fi->is_fn_info))
4357 VEC(ce_s, heap) *rhsc = NULL;
4358 int flags = gimple_call_flags (t);
4360 /* Const functions can return their arguments and addresses
4361 of global memory but not of escaped memory. */
4362 if (flags & (ECF_CONST|ECF_NOVOPS))
4364 if (gimple_call_lhs (t))
4365 handle_const_call (t, &rhsc);
4367 /* Pure functions can return addresses in and of memory
4368 reachable from their arguments, but they are not an escape
4369 point for reachable memory of their arguments. */
4370 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4371 handle_pure_call (t, &rhsc);
4372 else
4373 handle_rhs_call (t, &rhsc);
4374 if (gimple_call_lhs (t))
4375 handle_lhs_call (t, gimple_call_lhs (t), flags, rhsc, fndecl);
4376 VEC_free (ce_s, heap, rhsc);
4378 else
4380 tree lhsop;
4381 unsigned j;
4383 /* Assign all the passed arguments to the appropriate incoming
4384 parameters of the function. */
4385 for (j = 0; j < gimple_call_num_args (t); j++)
4387 struct constraint_expr lhs ;
4388 struct constraint_expr *rhsp;
4389 tree arg = gimple_call_arg (t, j);
4391 get_constraint_for_rhs (arg, &rhsc);
4392 lhs = get_function_part_constraint (fi, fi_parm_base + j);
4393 while (VEC_length (ce_s, rhsc) != 0)
4395 rhsp = &VEC_last (ce_s, rhsc);
4396 process_constraint (new_constraint (lhs, *rhsp));
4397 VEC_pop (ce_s, rhsc);
4401 /* If we are returning a value, assign it to the result. */
4402 lhsop = gimple_call_lhs (t);
4403 if (lhsop)
4405 struct constraint_expr rhs;
4406 struct constraint_expr *lhsp;
4408 get_constraint_for (lhsop, &lhsc);
4409 rhs = get_function_part_constraint (fi, fi_result);
4410 if (fndecl
4411 && DECL_RESULT (fndecl)
4412 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4414 VEC(ce_s, heap) *tem = NULL;
4415 VEC_safe_push (ce_s, heap, tem, rhs);
4416 do_deref (&tem);
4417 rhs = VEC_index (ce_s, tem, 0);
4418 VEC_free(ce_s, heap, tem);
4420 FOR_EACH_VEC_ELT (ce_s, lhsc, j, lhsp)
4421 process_constraint (new_constraint (*lhsp, rhs));
4424 /* If we pass the result decl by reference, honor that. */
4425 if (lhsop
4426 && fndecl
4427 && DECL_RESULT (fndecl)
4428 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4430 struct constraint_expr lhs;
4431 struct constraint_expr *rhsp;
4433 get_constraint_for_address_of (lhsop, &rhsc);
4434 lhs = get_function_part_constraint (fi, fi_result);
4435 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
4436 process_constraint (new_constraint (lhs, *rhsp));
4437 VEC_free (ce_s, heap, rhsc);
4440 /* If we use a static chain, pass it along. */
4441 if (gimple_call_chain (t))
4443 struct constraint_expr lhs;
4444 struct constraint_expr *rhsp;
4446 get_constraint_for (gimple_call_chain (t), &rhsc);
4447 lhs = get_function_part_constraint (fi, fi_static_chain);
4448 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
4449 process_constraint (new_constraint (lhs, *rhsp));
4454 /* Walk statement T setting up aliasing constraints according to the
4455 references found in T. This function is the main part of the
4456 constraint builder. AI points to auxiliary alias information used
4457 when building alias sets and computing alias grouping heuristics. */
4459 static void
4460 find_func_aliases (gimple origt)
4462 gimple t = origt;
4463 VEC(ce_s, heap) *lhsc = NULL;
4464 VEC(ce_s, heap) *rhsc = NULL;
4465 struct constraint_expr *c;
4466 varinfo_t fi;
4468 /* Now build constraints expressions. */
4469 if (gimple_code (t) == GIMPLE_PHI)
4471 size_t i;
4472 unsigned int j;
4474 /* For a phi node, assign all the arguments to
4475 the result. */
4476 get_constraint_for (gimple_phi_result (t), &lhsc);
4477 for (i = 0; i < gimple_phi_num_args (t); i++)
4479 tree strippedrhs = PHI_ARG_DEF (t, i);
4481 STRIP_NOPS (strippedrhs);
4482 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
4484 FOR_EACH_VEC_ELT (ce_s, lhsc, j, c)
4486 struct constraint_expr *c2;
4487 while (VEC_length (ce_s, rhsc) > 0)
4489 c2 = &VEC_last (ce_s, rhsc);
4490 process_constraint (new_constraint (*c, *c2));
4491 VEC_pop (ce_s, rhsc);
4496 /* In IPA mode, we need to generate constraints to pass call
4497 arguments through their calls. There are two cases,
4498 either a GIMPLE_CALL returning a value, or just a plain
4499 GIMPLE_CALL when we are not.
4501 In non-ipa mode, we need to generate constraints for each
4502 pointer passed by address. */
4503 else if (is_gimple_call (t))
4504 find_func_aliases_for_call (t);
4506 /* Otherwise, just a regular assignment statement. Only care about
4507 operations with pointer result, others are dealt with as escape
4508 points if they have pointer operands. */
4509 else if (is_gimple_assign (t))
4511 /* Otherwise, just a regular assignment statement. */
4512 tree lhsop = gimple_assign_lhs (t);
4513 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
4515 if (rhsop && TREE_CLOBBER_P (rhsop))
4516 /* Ignore clobbers, they don't actually store anything into
4517 the LHS. */
4519 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
4520 do_structure_copy (lhsop, rhsop);
4521 else
4523 enum tree_code code = gimple_assign_rhs_code (t);
4525 get_constraint_for (lhsop, &lhsc);
4527 if (code == POINTER_PLUS_EXPR)
4528 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4529 gimple_assign_rhs2 (t), &rhsc);
4530 else if (code == BIT_AND_EXPR
4531 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
4533 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4534 the pointer. Handle it by offsetting it by UNKNOWN. */
4535 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4536 NULL_TREE, &rhsc);
4538 else if ((CONVERT_EXPR_CODE_P (code)
4539 && !(POINTER_TYPE_P (gimple_expr_type (t))
4540 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
4541 || gimple_assign_single_p (t))
4542 get_constraint_for_rhs (rhsop, &rhsc);
4543 else if (code == COND_EXPR)
4545 /* The result is a merge of both COND_EXPR arms. */
4546 VEC (ce_s, heap) *tmp = NULL;
4547 struct constraint_expr *rhsp;
4548 unsigned i;
4549 get_constraint_for_rhs (gimple_assign_rhs2 (t), &rhsc);
4550 get_constraint_for_rhs (gimple_assign_rhs3 (t), &tmp);
4551 FOR_EACH_VEC_ELT (ce_s, tmp, i, rhsp)
4552 VEC_safe_push (ce_s, heap, rhsc, *rhsp);
4553 VEC_free (ce_s, heap, tmp);
4555 else if (truth_value_p (code))
4556 /* Truth value results are not pointer (parts). Or at least
4557 very very unreasonable obfuscation of a part. */
4559 else
4561 /* All other operations are merges. */
4562 VEC (ce_s, heap) *tmp = NULL;
4563 struct constraint_expr *rhsp;
4564 unsigned i, j;
4565 get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc);
4566 for (i = 2; i < gimple_num_ops (t); ++i)
4568 get_constraint_for_rhs (gimple_op (t, i), &tmp);
4569 FOR_EACH_VEC_ELT (ce_s, tmp, j, rhsp)
4570 VEC_safe_push (ce_s, heap, rhsc, *rhsp);
4571 VEC_truncate (ce_s, tmp, 0);
4573 VEC_free (ce_s, heap, tmp);
4575 process_all_all_constraints (lhsc, rhsc);
4577 /* If there is a store to a global variable the rhs escapes. */
4578 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
4579 && DECL_P (lhsop)
4580 && is_global_var (lhsop)
4581 && (!in_ipa_mode
4582 || DECL_EXTERNAL (lhsop) || TREE_PUBLIC (lhsop)))
4583 make_escape_constraint (rhsop);
4585 /* Handle escapes through return. */
4586 else if (gimple_code (t) == GIMPLE_RETURN
4587 && gimple_return_retval (t) != NULL_TREE)
4589 fi = NULL;
4590 if (!in_ipa_mode
4591 || !(fi = get_vi_for_tree (cfun->decl)))
4592 make_escape_constraint (gimple_return_retval (t));
4593 else if (in_ipa_mode
4594 && fi != NULL)
4596 struct constraint_expr lhs ;
4597 struct constraint_expr *rhsp;
4598 unsigned i;
4600 lhs = get_function_part_constraint (fi, fi_result);
4601 get_constraint_for_rhs (gimple_return_retval (t), &rhsc);
4602 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4603 process_constraint (new_constraint (lhs, *rhsp));
4606 /* Handle asms conservatively by adding escape constraints to everything. */
4607 else if (gimple_code (t) == GIMPLE_ASM)
4609 unsigned i, noutputs;
4610 const char **oconstraints;
4611 const char *constraint;
4612 bool allows_mem, allows_reg, is_inout;
4614 noutputs = gimple_asm_noutputs (t);
4615 oconstraints = XALLOCAVEC (const char *, noutputs);
4617 for (i = 0; i < noutputs; ++i)
4619 tree link = gimple_asm_output_op (t, i);
4620 tree op = TREE_VALUE (link);
4622 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4623 oconstraints[i] = constraint;
4624 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4625 &allows_reg, &is_inout);
4627 /* A memory constraint makes the address of the operand escape. */
4628 if (!allows_reg && allows_mem)
4629 make_escape_constraint (build_fold_addr_expr (op));
4631 /* The asm may read global memory, so outputs may point to
4632 any global memory. */
4633 if (op)
4635 VEC(ce_s, heap) *lhsc = NULL;
4636 struct constraint_expr rhsc, *lhsp;
4637 unsigned j;
4638 get_constraint_for (op, &lhsc);
4639 rhsc.var = nonlocal_id;
4640 rhsc.offset = 0;
4641 rhsc.type = SCALAR;
4642 FOR_EACH_VEC_ELT (ce_s, lhsc, j, lhsp)
4643 process_constraint (new_constraint (*lhsp, rhsc));
4644 VEC_free (ce_s, heap, lhsc);
4647 for (i = 0; i < gimple_asm_ninputs (t); ++i)
4649 tree link = gimple_asm_input_op (t, i);
4650 tree op = TREE_VALUE (link);
4652 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4654 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4655 &allows_mem, &allows_reg);
4657 /* A memory constraint makes the address of the operand escape. */
4658 if (!allows_reg && allows_mem)
4659 make_escape_constraint (build_fold_addr_expr (op));
4660 /* Strictly we'd only need the constraint to ESCAPED if
4661 the asm clobbers memory, otherwise using something
4662 along the lines of per-call clobbers/uses would be enough. */
4663 else if (op)
4664 make_escape_constraint (op);
4668 VEC_free (ce_s, heap, rhsc);
4669 VEC_free (ce_s, heap, lhsc);
4673 /* Create a constraint adding to the clobber set of FI the memory
4674 pointed to by PTR. */
4676 static void
4677 process_ipa_clobber (varinfo_t fi, tree ptr)
4679 VEC(ce_s, heap) *ptrc = NULL;
4680 struct constraint_expr *c, lhs;
4681 unsigned i;
4682 get_constraint_for_rhs (ptr, &ptrc);
4683 lhs = get_function_part_constraint (fi, fi_clobbers);
4684 FOR_EACH_VEC_ELT (ce_s, ptrc, i, c)
4685 process_constraint (new_constraint (lhs, *c));
4686 VEC_free (ce_s, heap, ptrc);
4689 /* Walk statement T setting up clobber and use constraints according to the
4690 references found in T. This function is a main part of the
4691 IPA constraint builder. */
4693 static void
4694 find_func_clobbers (gimple origt)
4696 gimple t = origt;
4697 VEC(ce_s, heap) *lhsc = NULL;
4698 VEC(ce_s, heap) *rhsc = NULL;
4699 varinfo_t fi;
4701 /* Add constraints for clobbered/used in IPA mode.
4702 We are not interested in what automatic variables are clobbered
4703 or used as we only use the information in the caller to which
4704 they do not escape. */
4705 gcc_assert (in_ipa_mode);
4707 /* If the stmt refers to memory in any way it better had a VUSE. */
4708 if (gimple_vuse (t) == NULL_TREE)
4709 return;
4711 /* We'd better have function information for the current function. */
4712 fi = lookup_vi_for_tree (cfun->decl);
4713 gcc_assert (fi != NULL);
4715 /* Account for stores in assignments and calls. */
4716 if (gimple_vdef (t) != NULL_TREE
4717 && gimple_has_lhs (t))
4719 tree lhs = gimple_get_lhs (t);
4720 tree tem = lhs;
4721 while (handled_component_p (tem))
4722 tem = TREE_OPERAND (tem, 0);
4723 if ((DECL_P (tem)
4724 && !auto_var_in_fn_p (tem, cfun->decl))
4725 || INDIRECT_REF_P (tem)
4726 || (TREE_CODE (tem) == MEM_REF
4727 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4728 && auto_var_in_fn_p
4729 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4731 struct constraint_expr lhsc, *rhsp;
4732 unsigned i;
4733 lhsc = get_function_part_constraint (fi, fi_clobbers);
4734 get_constraint_for_address_of (lhs, &rhsc);
4735 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4736 process_constraint (new_constraint (lhsc, *rhsp));
4737 VEC_free (ce_s, heap, rhsc);
4741 /* Account for uses in assigments and returns. */
4742 if (gimple_assign_single_p (t)
4743 || (gimple_code (t) == GIMPLE_RETURN
4744 && gimple_return_retval (t) != NULL_TREE))
4746 tree rhs = (gimple_assign_single_p (t)
4747 ? gimple_assign_rhs1 (t) : gimple_return_retval (t));
4748 tree tem = rhs;
4749 while (handled_component_p (tem))
4750 tem = TREE_OPERAND (tem, 0);
4751 if ((DECL_P (tem)
4752 && !auto_var_in_fn_p (tem, cfun->decl))
4753 || INDIRECT_REF_P (tem)
4754 || (TREE_CODE (tem) == MEM_REF
4755 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4756 && auto_var_in_fn_p
4757 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4759 struct constraint_expr lhs, *rhsp;
4760 unsigned i;
4761 lhs = get_function_part_constraint (fi, fi_uses);
4762 get_constraint_for_address_of (rhs, &rhsc);
4763 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4764 process_constraint (new_constraint (lhs, *rhsp));
4765 VEC_free (ce_s, heap, rhsc);
4769 if (is_gimple_call (t))
4771 varinfo_t cfi = NULL;
4772 tree decl = gimple_call_fndecl (t);
4773 struct constraint_expr lhs, rhs;
4774 unsigned i, j;
4776 /* For builtins we do not have separate function info. For those
4777 we do not generate escapes for we have to generate clobbers/uses. */
4778 if (decl
4779 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4780 switch (DECL_FUNCTION_CODE (decl))
4782 /* The following functions use and clobber memory pointed to
4783 by their arguments. */
4784 case BUILT_IN_STRCPY:
4785 case BUILT_IN_STRNCPY:
4786 case BUILT_IN_BCOPY:
4787 case BUILT_IN_MEMCPY:
4788 case BUILT_IN_MEMMOVE:
4789 case BUILT_IN_MEMPCPY:
4790 case BUILT_IN_STPCPY:
4791 case BUILT_IN_STPNCPY:
4792 case BUILT_IN_STRCAT:
4793 case BUILT_IN_STRNCAT:
4794 case BUILT_IN_STRCPY_CHK:
4795 case BUILT_IN_STRNCPY_CHK:
4796 case BUILT_IN_MEMCPY_CHK:
4797 case BUILT_IN_MEMMOVE_CHK:
4798 case BUILT_IN_MEMPCPY_CHK:
4799 case BUILT_IN_STPCPY_CHK:
4800 case BUILT_IN_STPNCPY_CHK:
4801 case BUILT_IN_STRCAT_CHK:
4802 case BUILT_IN_STRNCAT_CHK:
4804 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4805 == BUILT_IN_BCOPY ? 1 : 0));
4806 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4807 == BUILT_IN_BCOPY ? 0 : 1));
4808 unsigned i;
4809 struct constraint_expr *rhsp, *lhsp;
4810 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4811 lhs = get_function_part_constraint (fi, fi_clobbers);
4812 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4813 process_constraint (new_constraint (lhs, *lhsp));
4814 VEC_free (ce_s, heap, lhsc);
4815 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4816 lhs = get_function_part_constraint (fi, fi_uses);
4817 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4818 process_constraint (new_constraint (lhs, *rhsp));
4819 VEC_free (ce_s, heap, rhsc);
4820 return;
4822 /* The following function clobbers memory pointed to by
4823 its argument. */
4824 case BUILT_IN_MEMSET:
4825 case BUILT_IN_MEMSET_CHK:
4827 tree dest = gimple_call_arg (t, 0);
4828 unsigned i;
4829 ce_s *lhsp;
4830 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4831 lhs = get_function_part_constraint (fi, fi_clobbers);
4832 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4833 process_constraint (new_constraint (lhs, *lhsp));
4834 VEC_free (ce_s, heap, lhsc);
4835 return;
4837 /* The following functions clobber their second and third
4838 arguments. */
4839 case BUILT_IN_SINCOS:
4840 case BUILT_IN_SINCOSF:
4841 case BUILT_IN_SINCOSL:
4843 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4844 process_ipa_clobber (fi, gimple_call_arg (t, 2));
4845 return;
4847 /* The following functions clobber their second argument. */
4848 case BUILT_IN_FREXP:
4849 case BUILT_IN_FREXPF:
4850 case BUILT_IN_FREXPL:
4851 case BUILT_IN_LGAMMA_R:
4852 case BUILT_IN_LGAMMAF_R:
4853 case BUILT_IN_LGAMMAL_R:
4854 case BUILT_IN_GAMMA_R:
4855 case BUILT_IN_GAMMAF_R:
4856 case BUILT_IN_GAMMAL_R:
4857 case BUILT_IN_MODF:
4858 case BUILT_IN_MODFF:
4859 case BUILT_IN_MODFL:
4861 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4862 return;
4864 /* The following functions clobber their third argument. */
4865 case BUILT_IN_REMQUO:
4866 case BUILT_IN_REMQUOF:
4867 case BUILT_IN_REMQUOL:
4869 process_ipa_clobber (fi, gimple_call_arg (t, 2));
4870 return;
4872 /* The following functions neither read nor clobber memory. */
4873 case BUILT_IN_ASSUME_ALIGNED:
4874 case BUILT_IN_FREE:
4875 return;
4876 /* Trampolines are of no interest to us. */
4877 case BUILT_IN_INIT_TRAMPOLINE:
4878 case BUILT_IN_ADJUST_TRAMPOLINE:
4879 return;
4880 case BUILT_IN_VA_START:
4881 case BUILT_IN_VA_END:
4882 return;
4883 /* printf-style functions may have hooks to set pointers to
4884 point to somewhere into the generated string. Leave them
4885 for a later excercise... */
4886 default:
4887 /* Fallthru to general call handling. */;
4890 /* Parameters passed by value are used. */
4891 lhs = get_function_part_constraint (fi, fi_uses);
4892 for (i = 0; i < gimple_call_num_args (t); i++)
4894 struct constraint_expr *rhsp;
4895 tree arg = gimple_call_arg (t, i);
4897 if (TREE_CODE (arg) == SSA_NAME
4898 || is_gimple_min_invariant (arg))
4899 continue;
4901 get_constraint_for_address_of (arg, &rhsc);
4902 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
4903 process_constraint (new_constraint (lhs, *rhsp));
4904 VEC_free (ce_s, heap, rhsc);
4907 /* Build constraints for propagating clobbers/uses along the
4908 callgraph edges. */
4909 cfi = get_fi_for_callee (t);
4910 if (cfi->id == anything_id)
4912 if (gimple_vdef (t))
4913 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
4914 anything_id);
4915 make_constraint_from (first_vi_for_offset (fi, fi_uses),
4916 anything_id);
4917 return;
4920 /* For callees without function info (that's external functions),
4921 ESCAPED is clobbered and used. */
4922 if (gimple_call_fndecl (t)
4923 && !cfi->is_fn_info)
4925 varinfo_t vi;
4927 if (gimple_vdef (t))
4928 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
4929 escaped_id);
4930 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
4932 /* Also honor the call statement use/clobber info. */
4933 if ((vi = lookup_call_clobber_vi (t)) != NULL)
4934 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
4935 vi->id);
4936 if ((vi = lookup_call_use_vi (t)) != NULL)
4937 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
4938 vi->id);
4939 return;
4942 /* Otherwise the caller clobbers and uses what the callee does.
4943 ??? This should use a new complex constraint that filters
4944 local variables of the callee. */
4945 if (gimple_vdef (t))
4947 lhs = get_function_part_constraint (fi, fi_clobbers);
4948 rhs = get_function_part_constraint (cfi, fi_clobbers);
4949 process_constraint (new_constraint (lhs, rhs));
4951 lhs = get_function_part_constraint (fi, fi_uses);
4952 rhs = get_function_part_constraint (cfi, fi_uses);
4953 process_constraint (new_constraint (lhs, rhs));
4955 else if (gimple_code (t) == GIMPLE_ASM)
4957 /* ??? Ick. We can do better. */
4958 if (gimple_vdef (t))
4959 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
4960 anything_id);
4961 make_constraint_from (first_vi_for_offset (fi, fi_uses),
4962 anything_id);
4965 VEC_free (ce_s, heap, rhsc);
4969 /* Find the first varinfo in the same variable as START that overlaps with
4970 OFFSET. Return NULL if we can't find one. */
4972 static varinfo_t
4973 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
4975 /* If the offset is outside of the variable, bail out. */
4976 if (offset >= start->fullsize)
4977 return NULL;
4979 /* If we cannot reach offset from start, lookup the first field
4980 and start from there. */
4981 if (start->offset > offset)
4982 start = lookup_vi_for_tree (start->decl);
4984 while (start)
4986 /* We may not find a variable in the field list with the actual
4987 offset when when we have glommed a structure to a variable.
4988 In that case, however, offset should still be within the size
4989 of the variable. */
4990 if (offset >= start->offset
4991 && (offset - start->offset) < start->size)
4992 return start;
4994 start= start->next;
4997 return NULL;
5000 /* Find the first varinfo in the same variable as START that overlaps with
5001 OFFSET. If there is no such varinfo the varinfo directly preceding
5002 OFFSET is returned. */
5004 static varinfo_t
5005 first_or_preceding_vi_for_offset (varinfo_t start,
5006 unsigned HOST_WIDE_INT offset)
5008 /* If we cannot reach offset from start, lookup the first field
5009 and start from there. */
5010 if (start->offset > offset)
5011 start = lookup_vi_for_tree (start->decl);
5013 /* We may not find a variable in the field list with the actual
5014 offset when when we have glommed a structure to a variable.
5015 In that case, however, offset should still be within the size
5016 of the variable.
5017 If we got beyond the offset we look for return the field
5018 directly preceding offset which may be the last field. */
5019 while (start->next
5020 && offset >= start->offset
5021 && !((offset - start->offset) < start->size))
5022 start = start->next;
5024 return start;
5028 /* This structure is used during pushing fields onto the fieldstack
5029 to track the offset of the field, since bitpos_of_field gives it
5030 relative to its immediate containing type, and we want it relative
5031 to the ultimate containing object. */
5033 struct fieldoff
5035 /* Offset from the base of the base containing object to this field. */
5036 HOST_WIDE_INT offset;
5038 /* Size, in bits, of the field. */
5039 unsigned HOST_WIDE_INT size;
5041 unsigned has_unknown_size : 1;
5043 unsigned must_have_pointers : 1;
5045 unsigned may_have_pointers : 1;
5047 unsigned only_restrict_pointers : 1;
5049 typedef struct fieldoff fieldoff_s;
5051 DEF_VEC_O(fieldoff_s);
5052 DEF_VEC_ALLOC_O(fieldoff_s,heap);
5054 /* qsort comparison function for two fieldoff's PA and PB */
5056 static int
5057 fieldoff_compare (const void *pa, const void *pb)
5059 const fieldoff_s *foa = (const fieldoff_s *)pa;
5060 const fieldoff_s *fob = (const fieldoff_s *)pb;
5061 unsigned HOST_WIDE_INT foasize, fobsize;
5063 if (foa->offset < fob->offset)
5064 return -1;
5065 else if (foa->offset > fob->offset)
5066 return 1;
5068 foasize = foa->size;
5069 fobsize = fob->size;
5070 if (foasize < fobsize)
5071 return -1;
5072 else if (foasize > fobsize)
5073 return 1;
5074 return 0;
5077 /* Sort a fieldstack according to the field offset and sizes. */
5078 static void
5079 sort_fieldstack (VEC(fieldoff_s,heap) *fieldstack)
5081 VEC_qsort (fieldoff_s, fieldstack, fieldoff_compare);
5084 /* Return true if T is a type that can have subvars. */
5086 static inline bool
5087 type_can_have_subvars (const_tree t)
5089 /* Aggregates without overlapping fields can have subvars. */
5090 return TREE_CODE (t) == RECORD_TYPE;
5093 /* Return true if V is a tree that we can have subvars for.
5094 Normally, this is any aggregate type. Also complex
5095 types which are not gimple registers can have subvars. */
5097 static inline bool
5098 var_can_have_subvars (const_tree v)
5100 /* Volatile variables should never have subvars. */
5101 if (TREE_THIS_VOLATILE (v))
5102 return false;
5104 /* Non decls or memory tags can never have subvars. */
5105 if (!DECL_P (v))
5106 return false;
5108 return type_can_have_subvars (TREE_TYPE (v));
5111 /* Return true if T is a type that does contain pointers. */
5113 static bool
5114 type_must_have_pointers (tree type)
5116 if (POINTER_TYPE_P (type))
5117 return true;
5119 if (TREE_CODE (type) == ARRAY_TYPE)
5120 return type_must_have_pointers (TREE_TYPE (type));
5122 /* A function or method can have pointers as arguments, so track
5123 those separately. */
5124 if (TREE_CODE (type) == FUNCTION_TYPE
5125 || TREE_CODE (type) == METHOD_TYPE)
5126 return true;
5128 return false;
5131 static bool
5132 field_must_have_pointers (tree t)
5134 return type_must_have_pointers (TREE_TYPE (t));
5137 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5138 the fields of TYPE onto fieldstack, recording their offsets along
5139 the way.
5141 OFFSET is used to keep track of the offset in this entire
5142 structure, rather than just the immediately containing structure.
5143 Returns false if the caller is supposed to handle the field we
5144 recursed for. */
5146 static bool
5147 push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
5148 HOST_WIDE_INT offset)
5150 tree field;
5151 bool empty_p = true;
5153 if (TREE_CODE (type) != RECORD_TYPE)
5154 return false;
5156 /* If the vector of fields is growing too big, bail out early.
5157 Callers check for VEC_length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5158 sure this fails. */
5159 if (VEC_length (fieldoff_s, *fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5160 return false;
5162 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5163 if (TREE_CODE (field) == FIELD_DECL)
5165 bool push = false;
5166 HOST_WIDE_INT foff = bitpos_of_field (field);
5168 if (!var_can_have_subvars (field)
5169 || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
5170 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5171 push = true;
5172 else if (!push_fields_onto_fieldstack
5173 (TREE_TYPE (field), fieldstack, offset + foff)
5174 && (DECL_SIZE (field)
5175 && !integer_zerop (DECL_SIZE (field))))
5176 /* Empty structures may have actual size, like in C++. So
5177 see if we didn't push any subfields and the size is
5178 nonzero, push the field onto the stack. */
5179 push = true;
5181 if (push)
5183 fieldoff_s *pair = NULL;
5184 bool has_unknown_size = false;
5185 bool must_have_pointers_p;
5187 if (!VEC_empty (fieldoff_s, *fieldstack))
5188 pair = &VEC_last (fieldoff_s, *fieldstack);
5190 /* If there isn't anything at offset zero, create sth. */
5191 if (!pair
5192 && offset + foff != 0)
5194 fieldoff_s e = {0, offset + foff, false, false, false, false};
5195 pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, e);
5198 if (!DECL_SIZE (field)
5199 || !host_integerp (DECL_SIZE (field), 1))
5200 has_unknown_size = true;
5202 /* If adjacent fields do not contain pointers merge them. */
5203 must_have_pointers_p = field_must_have_pointers (field);
5204 if (pair
5205 && !has_unknown_size
5206 && !must_have_pointers_p
5207 && !pair->must_have_pointers
5208 && !pair->has_unknown_size
5209 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5211 pair->size += TREE_INT_CST_LOW (DECL_SIZE (field));
5213 else
5215 fieldoff_s e;
5216 e.offset = offset + foff;
5217 e.has_unknown_size = has_unknown_size;
5218 if (!has_unknown_size)
5219 e.size = TREE_INT_CST_LOW (DECL_SIZE (field));
5220 else
5221 e.size = -1;
5222 e.must_have_pointers = must_have_pointers_p;
5223 e.may_have_pointers = true;
5224 e.only_restrict_pointers
5225 = (!has_unknown_size
5226 && POINTER_TYPE_P (TREE_TYPE (field))
5227 && TYPE_RESTRICT (TREE_TYPE (field)));
5228 VEC_safe_push (fieldoff_s, heap, *fieldstack, e);
5232 empty_p = false;
5235 return !empty_p;
5238 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5239 if it is a varargs function. */
5241 static unsigned int
5242 count_num_arguments (tree decl, bool *is_varargs)
5244 unsigned int num = 0;
5245 tree t;
5247 /* Capture named arguments for K&R functions. They do not
5248 have a prototype and thus no TYPE_ARG_TYPES. */
5249 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5250 ++num;
5252 /* Check if the function has variadic arguments. */
5253 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5254 if (TREE_VALUE (t) == void_type_node)
5255 break;
5256 if (!t)
5257 *is_varargs = true;
5259 return num;
5262 /* Creation function node for DECL, using NAME, and return the index
5263 of the variable we've created for the function. */
5265 static varinfo_t
5266 create_function_info_for (tree decl, const char *name)
5268 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5269 varinfo_t vi, prev_vi;
5270 tree arg;
5271 unsigned int i;
5272 bool is_varargs = false;
5273 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5275 /* Create the variable info. */
5277 vi = new_var_info (decl, name);
5278 vi->offset = 0;
5279 vi->size = 1;
5280 vi->fullsize = fi_parm_base + num_args;
5281 vi->is_fn_info = 1;
5282 vi->may_have_pointers = false;
5283 if (is_varargs)
5284 vi->fullsize = ~0;
5285 insert_vi_for_tree (vi->decl, vi);
5287 prev_vi = vi;
5289 /* Create a variable for things the function clobbers and one for
5290 things the function uses. */
5292 varinfo_t clobbervi, usevi;
5293 const char *newname;
5294 char *tempname;
5296 asprintf (&tempname, "%s.clobber", name);
5297 newname = ggc_strdup (tempname);
5298 free (tempname);
5300 clobbervi = new_var_info (NULL, newname);
5301 clobbervi->offset = fi_clobbers;
5302 clobbervi->size = 1;
5303 clobbervi->fullsize = vi->fullsize;
5304 clobbervi->is_full_var = true;
5305 clobbervi->is_global_var = false;
5306 gcc_assert (prev_vi->offset < clobbervi->offset);
5307 prev_vi->next = clobbervi;
5308 prev_vi = clobbervi;
5310 asprintf (&tempname, "%s.use", name);
5311 newname = ggc_strdup (tempname);
5312 free (tempname);
5314 usevi = new_var_info (NULL, newname);
5315 usevi->offset = fi_uses;
5316 usevi->size = 1;
5317 usevi->fullsize = vi->fullsize;
5318 usevi->is_full_var = true;
5319 usevi->is_global_var = false;
5320 gcc_assert (prev_vi->offset < usevi->offset);
5321 prev_vi->next = usevi;
5322 prev_vi = usevi;
5325 /* And one for the static chain. */
5326 if (fn->static_chain_decl != NULL_TREE)
5328 varinfo_t chainvi;
5329 const char *newname;
5330 char *tempname;
5332 asprintf (&tempname, "%s.chain", name);
5333 newname = ggc_strdup (tempname);
5334 free (tempname);
5336 chainvi = new_var_info (fn->static_chain_decl, newname);
5337 chainvi->offset = fi_static_chain;
5338 chainvi->size = 1;
5339 chainvi->fullsize = vi->fullsize;
5340 chainvi->is_full_var = true;
5341 chainvi->is_global_var = false;
5342 gcc_assert (prev_vi->offset < chainvi->offset);
5343 prev_vi->next = chainvi;
5344 prev_vi = chainvi;
5345 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5348 /* Create a variable for the return var. */
5349 if (DECL_RESULT (decl) != NULL
5350 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5352 varinfo_t resultvi;
5353 const char *newname;
5354 char *tempname;
5355 tree resultdecl = decl;
5357 if (DECL_RESULT (decl))
5358 resultdecl = DECL_RESULT (decl);
5360 asprintf (&tempname, "%s.result", name);
5361 newname = ggc_strdup (tempname);
5362 free (tempname);
5364 resultvi = new_var_info (resultdecl, newname);
5365 resultvi->offset = fi_result;
5366 resultvi->size = 1;
5367 resultvi->fullsize = vi->fullsize;
5368 resultvi->is_full_var = true;
5369 if (DECL_RESULT (decl))
5370 resultvi->may_have_pointers = true;
5371 gcc_assert (prev_vi->offset < resultvi->offset);
5372 prev_vi->next = resultvi;
5373 prev_vi = resultvi;
5374 if (DECL_RESULT (decl))
5375 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
5378 /* Set up variables for each argument. */
5379 arg = DECL_ARGUMENTS (decl);
5380 for (i = 0; i < num_args; i++)
5382 varinfo_t argvi;
5383 const char *newname;
5384 char *tempname;
5385 tree argdecl = decl;
5387 if (arg)
5388 argdecl = arg;
5390 asprintf (&tempname, "%s.arg%d", name, i);
5391 newname = ggc_strdup (tempname);
5392 free (tempname);
5394 argvi = new_var_info (argdecl, newname);
5395 argvi->offset = fi_parm_base + i;
5396 argvi->size = 1;
5397 argvi->is_full_var = true;
5398 argvi->fullsize = vi->fullsize;
5399 if (arg)
5400 argvi->may_have_pointers = true;
5401 gcc_assert (prev_vi->offset < argvi->offset);
5402 prev_vi->next = argvi;
5403 prev_vi = argvi;
5404 if (arg)
5406 insert_vi_for_tree (arg, argvi);
5407 arg = DECL_CHAIN (arg);
5411 /* Add one representative for all further args. */
5412 if (is_varargs)
5414 varinfo_t argvi;
5415 const char *newname;
5416 char *tempname;
5417 tree decl;
5419 asprintf (&tempname, "%s.varargs", name);
5420 newname = ggc_strdup (tempname);
5421 free (tempname);
5423 /* We need sth that can be pointed to for va_start. */
5424 decl = build_fake_var_decl (ptr_type_node);
5426 argvi = new_var_info (decl, newname);
5427 argvi->offset = fi_parm_base + num_args;
5428 argvi->size = ~0;
5429 argvi->is_full_var = true;
5430 argvi->is_heap_var = true;
5431 argvi->fullsize = vi->fullsize;
5432 gcc_assert (prev_vi->offset < argvi->offset);
5433 prev_vi->next = argvi;
5434 prev_vi = argvi;
5437 return vi;
5441 /* Return true if FIELDSTACK contains fields that overlap.
5442 FIELDSTACK is assumed to be sorted by offset. */
5444 static bool
5445 check_for_overlaps (VEC (fieldoff_s,heap) *fieldstack)
5447 fieldoff_s *fo = NULL;
5448 unsigned int i;
5449 HOST_WIDE_INT lastoffset = -1;
5451 FOR_EACH_VEC_ELT (fieldoff_s, fieldstack, i, fo)
5453 if (fo->offset == lastoffset)
5454 return true;
5455 lastoffset = fo->offset;
5457 return false;
5460 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5461 This will also create any varinfo structures necessary for fields
5462 of DECL. */
5464 static varinfo_t
5465 create_variable_info_for_1 (tree decl, const char *name)
5467 varinfo_t vi, newvi;
5468 tree decl_type = TREE_TYPE (decl);
5469 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
5470 VEC (fieldoff_s,heap) *fieldstack = NULL;
5471 fieldoff_s *fo;
5472 unsigned int i;
5474 if (!declsize
5475 || !host_integerp (declsize, 1))
5477 vi = new_var_info (decl, name);
5478 vi->offset = 0;
5479 vi->size = ~0;
5480 vi->fullsize = ~0;
5481 vi->is_unknown_size_var = true;
5482 vi->is_full_var = true;
5483 vi->may_have_pointers = true;
5484 return vi;
5487 /* Collect field information. */
5488 if (use_field_sensitive
5489 && var_can_have_subvars (decl)
5490 /* ??? Force us to not use subfields for global initializers
5491 in IPA mode. Else we'd have to parse arbitrary initializers. */
5492 && !(in_ipa_mode
5493 && is_global_var (decl)
5494 && DECL_INITIAL (decl)))
5496 fieldoff_s *fo = NULL;
5497 bool notokay = false;
5498 unsigned int i;
5500 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
5502 for (i = 0; !notokay && VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
5503 if (fo->has_unknown_size
5504 || fo->offset < 0)
5506 notokay = true;
5507 break;
5510 /* We can't sort them if we have a field with a variable sized type,
5511 which will make notokay = true. In that case, we are going to return
5512 without creating varinfos for the fields anyway, so sorting them is a
5513 waste to boot. */
5514 if (!notokay)
5516 sort_fieldstack (fieldstack);
5517 /* Due to some C++ FE issues, like PR 22488, we might end up
5518 what appear to be overlapping fields even though they,
5519 in reality, do not overlap. Until the C++ FE is fixed,
5520 we will simply disable field-sensitivity for these cases. */
5521 notokay = check_for_overlaps (fieldstack);
5524 if (notokay)
5525 VEC_free (fieldoff_s, heap, fieldstack);
5528 /* If we didn't end up collecting sub-variables create a full
5529 variable for the decl. */
5530 if (VEC_length (fieldoff_s, fieldstack) <= 1
5531 || VEC_length (fieldoff_s, fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5533 vi = new_var_info (decl, name);
5534 vi->offset = 0;
5535 vi->may_have_pointers = true;
5536 vi->fullsize = TREE_INT_CST_LOW (declsize);
5537 vi->size = vi->fullsize;
5538 vi->is_full_var = true;
5539 VEC_free (fieldoff_s, heap, fieldstack);
5540 return vi;
5543 vi = new_var_info (decl, name);
5544 vi->fullsize = TREE_INT_CST_LOW (declsize);
5545 for (i = 0, newvi = vi;
5546 VEC_iterate (fieldoff_s, fieldstack, i, fo);
5547 ++i, newvi = newvi->next)
5549 const char *newname = "NULL";
5550 char *tempname;
5552 if (dump_file)
5554 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC
5555 "+" HOST_WIDE_INT_PRINT_DEC, name, fo->offset, fo->size);
5556 newname = ggc_strdup (tempname);
5557 free (tempname);
5559 newvi->name = newname;
5560 newvi->offset = fo->offset;
5561 newvi->size = fo->size;
5562 newvi->fullsize = vi->fullsize;
5563 newvi->may_have_pointers = fo->may_have_pointers;
5564 newvi->only_restrict_pointers = fo->only_restrict_pointers;
5565 if (i + 1 < VEC_length (fieldoff_s, fieldstack))
5566 newvi->next = new_var_info (decl, name);
5569 VEC_free (fieldoff_s, heap, fieldstack);
5571 return vi;
5574 static unsigned int
5575 create_variable_info_for (tree decl, const char *name)
5577 varinfo_t vi = create_variable_info_for_1 (decl, name);
5578 unsigned int id = vi->id;
5580 insert_vi_for_tree (decl, vi);
5582 if (TREE_CODE (decl) != VAR_DECL)
5583 return id;
5585 /* Create initial constraints for globals. */
5586 for (; vi; vi = vi->next)
5588 if (!vi->may_have_pointers
5589 || !vi->is_global_var)
5590 continue;
5592 /* Mark global restrict qualified pointers. */
5593 if ((POINTER_TYPE_P (TREE_TYPE (decl))
5594 && TYPE_RESTRICT (TREE_TYPE (decl)))
5595 || vi->only_restrict_pointers)
5597 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5598 continue;
5601 /* In non-IPA mode the initializer from nonlocal is all we need. */
5602 if (!in_ipa_mode
5603 || DECL_HARD_REGISTER (decl))
5604 make_copy_constraint (vi, nonlocal_id);
5606 /* In IPA mode parse the initializer and generate proper constraints
5607 for it. */
5608 else
5610 struct varpool_node *vnode = varpool_get_node (decl);
5612 /* For escaped variables initialize them from nonlocal. */
5613 if (!varpool_all_refs_explicit_p (vnode))
5614 make_copy_constraint (vi, nonlocal_id);
5616 /* If this is a global variable with an initializer and we are in
5617 IPA mode generate constraints for it. */
5618 if (DECL_INITIAL (decl)
5619 && vnode->analyzed)
5621 VEC (ce_s, heap) *rhsc = NULL;
5622 struct constraint_expr lhs, *rhsp;
5623 unsigned i;
5624 get_constraint_for_rhs (DECL_INITIAL (decl), &rhsc);
5625 lhs.var = vi->id;
5626 lhs.offset = 0;
5627 lhs.type = SCALAR;
5628 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
5629 process_constraint (new_constraint (lhs, *rhsp));
5630 /* If this is a variable that escapes from the unit
5631 the initializer escapes as well. */
5632 if (!varpool_all_refs_explicit_p (vnode))
5634 lhs.var = escaped_id;
5635 lhs.offset = 0;
5636 lhs.type = SCALAR;
5637 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
5638 process_constraint (new_constraint (lhs, *rhsp));
5640 VEC_free (ce_s, heap, rhsc);
5645 return id;
5648 /* Print out the points-to solution for VAR to FILE. */
5650 static void
5651 dump_solution_for_var (FILE *file, unsigned int var)
5653 varinfo_t vi = get_varinfo (var);
5654 unsigned int i;
5655 bitmap_iterator bi;
5657 /* Dump the solution for unified vars anyway, this avoids difficulties
5658 in scanning dumps in the testsuite. */
5659 fprintf (file, "%s = { ", vi->name);
5660 vi = get_varinfo (find (var));
5661 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5662 fprintf (file, "%s ", get_varinfo (i)->name);
5663 fprintf (file, "}");
5665 /* But note when the variable was unified. */
5666 if (vi->id != var)
5667 fprintf (file, " same as %s", vi->name);
5669 fprintf (file, "\n");
5672 /* Print the points-to solution for VAR to stdout. */
5674 DEBUG_FUNCTION void
5675 debug_solution_for_var (unsigned int var)
5677 dump_solution_for_var (stdout, var);
5680 /* Create varinfo structures for all of the variables in the
5681 function for intraprocedural mode. */
5683 static void
5684 intra_create_variable_infos (void)
5686 tree t;
5688 /* For each incoming pointer argument arg, create the constraint ARG
5689 = NONLOCAL or a dummy variable if it is a restrict qualified
5690 passed-by-reference argument. */
5691 for (t = DECL_ARGUMENTS (current_function_decl); t; t = DECL_CHAIN (t))
5693 varinfo_t p = get_vi_for_tree (t);
5695 /* For restrict qualified pointers to objects passed by
5696 reference build a real representative for the pointed-to object.
5697 Treat restrict qualified references the same. */
5698 if (TYPE_RESTRICT (TREE_TYPE (t))
5699 && ((DECL_BY_REFERENCE (t) && POINTER_TYPE_P (TREE_TYPE (t)))
5700 || TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5701 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t))))
5703 struct constraint_expr lhsc, rhsc;
5704 varinfo_t vi;
5705 tree heapvar = build_fake_var_decl (TREE_TYPE (TREE_TYPE (t)));
5706 DECL_EXTERNAL (heapvar) = 1;
5707 vi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS");
5708 insert_vi_for_tree (heapvar, vi);
5709 lhsc.var = p->id;
5710 lhsc.type = SCALAR;
5711 lhsc.offset = 0;
5712 rhsc.var = vi->id;
5713 rhsc.type = ADDRESSOF;
5714 rhsc.offset = 0;
5715 process_constraint (new_constraint (lhsc, rhsc));
5716 for (; vi; vi = vi->next)
5717 if (vi->may_have_pointers)
5719 if (vi->only_restrict_pointers)
5720 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5721 else
5722 make_copy_constraint (vi, nonlocal_id);
5724 continue;
5727 if (POINTER_TYPE_P (TREE_TYPE (t))
5728 && TYPE_RESTRICT (TREE_TYPE (t)))
5729 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5730 else
5732 for (; p; p = p->next)
5734 if (p->only_restrict_pointers)
5735 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5736 else if (p->may_have_pointers)
5737 make_constraint_from (p, nonlocal_id);
5742 /* Add a constraint for a result decl that is passed by reference. */
5743 if (DECL_RESULT (cfun->decl)
5744 && DECL_BY_REFERENCE (DECL_RESULT (cfun->decl)))
5746 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (cfun->decl));
5748 for (p = result_vi; p; p = p->next)
5749 make_constraint_from (p, nonlocal_id);
5752 /* Add a constraint for the incoming static chain parameter. */
5753 if (cfun->static_chain_decl != NULL_TREE)
5755 varinfo_t p, chain_vi = get_vi_for_tree (cfun->static_chain_decl);
5757 for (p = chain_vi; p; p = p->next)
5758 make_constraint_from (p, nonlocal_id);
5762 /* Structure used to put solution bitmaps in a hashtable so they can
5763 be shared among variables with the same points-to set. */
5765 typedef struct shared_bitmap_info
5767 bitmap pt_vars;
5768 hashval_t hashcode;
5769 } *shared_bitmap_info_t;
5770 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
5772 static htab_t shared_bitmap_table;
5774 /* Hash function for a shared_bitmap_info_t */
5776 static hashval_t
5777 shared_bitmap_hash (const void *p)
5779 const_shared_bitmap_info_t const bi = (const_shared_bitmap_info_t) p;
5780 return bi->hashcode;
5783 /* Equality function for two shared_bitmap_info_t's. */
5785 static int
5786 shared_bitmap_eq (const void *p1, const void *p2)
5788 const_shared_bitmap_info_t const sbi1 = (const_shared_bitmap_info_t) p1;
5789 const_shared_bitmap_info_t const sbi2 = (const_shared_bitmap_info_t) p2;
5790 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
5793 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5794 existing instance if there is one, NULL otherwise. */
5796 static bitmap
5797 shared_bitmap_lookup (bitmap pt_vars)
5799 void **slot;
5800 struct shared_bitmap_info sbi;
5802 sbi.pt_vars = pt_vars;
5803 sbi.hashcode = bitmap_hash (pt_vars);
5805 slot = htab_find_slot_with_hash (shared_bitmap_table, &sbi,
5806 sbi.hashcode, NO_INSERT);
5807 if (!slot)
5808 return NULL;
5809 else
5810 return ((shared_bitmap_info_t) *slot)->pt_vars;
5814 /* Add a bitmap to the shared bitmap hashtable. */
5816 static void
5817 shared_bitmap_add (bitmap pt_vars)
5819 void **slot;
5820 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
5822 sbi->pt_vars = pt_vars;
5823 sbi->hashcode = bitmap_hash (pt_vars);
5825 slot = htab_find_slot_with_hash (shared_bitmap_table, sbi,
5826 sbi->hashcode, INSERT);
5827 gcc_assert (!*slot);
5828 *slot = (void *) sbi;
5832 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
5834 static void
5835 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt)
5837 unsigned int i;
5838 bitmap_iterator bi;
5840 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
5842 varinfo_t vi = get_varinfo (i);
5844 /* The only artificial variables that are allowed in a may-alias
5845 set are heap variables. */
5846 if (vi->is_artificial_var && !vi->is_heap_var)
5847 continue;
5849 if (TREE_CODE (vi->decl) == VAR_DECL
5850 || TREE_CODE (vi->decl) == PARM_DECL
5851 || TREE_CODE (vi->decl) == RESULT_DECL)
5853 /* If we are in IPA mode we will not recompute points-to
5854 sets after inlining so make sure they stay valid. */
5855 if (in_ipa_mode
5856 && !DECL_PT_UID_SET_P (vi->decl))
5857 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
5859 /* Add the decl to the points-to set. Note that the points-to
5860 set contains global variables. */
5861 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
5862 if (vi->is_global_var)
5863 pt->vars_contains_global = true;
5869 /* Compute the points-to solution *PT for the variable VI. */
5871 static void
5872 find_what_var_points_to (varinfo_t orig_vi, struct pt_solution *pt)
5874 unsigned int i;
5875 bitmap_iterator bi;
5876 bitmap finished_solution;
5877 bitmap result;
5878 varinfo_t vi;
5880 memset (pt, 0, sizeof (struct pt_solution));
5882 /* This variable may have been collapsed, let's get the real
5883 variable. */
5884 vi = get_varinfo (find (orig_vi->id));
5886 /* Translate artificial variables into SSA_NAME_PTR_INFO
5887 attributes. */
5888 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5890 varinfo_t vi = get_varinfo (i);
5892 if (vi->is_artificial_var)
5894 if (vi->id == nothing_id)
5895 pt->null = 1;
5896 else if (vi->id == escaped_id)
5898 if (in_ipa_mode)
5899 pt->ipa_escaped = 1;
5900 else
5901 pt->escaped = 1;
5903 else if (vi->id == nonlocal_id)
5904 pt->nonlocal = 1;
5905 else if (vi->is_heap_var)
5906 /* We represent heapvars in the points-to set properly. */
5908 else if (vi->id == readonly_id)
5909 /* Nobody cares. */
5911 else if (vi->id == anything_id
5912 || vi->id == integer_id)
5913 pt->anything = 1;
5917 /* Instead of doing extra work, simply do not create
5918 elaborate points-to information for pt_anything pointers. */
5919 if (pt->anything)
5920 return;
5922 /* Share the final set of variables when possible. */
5923 finished_solution = BITMAP_GGC_ALLOC ();
5924 stats.points_to_sets_created++;
5926 set_uids_in_ptset (finished_solution, vi->solution, pt);
5927 result = shared_bitmap_lookup (finished_solution);
5928 if (!result)
5930 shared_bitmap_add (finished_solution);
5931 pt->vars = finished_solution;
5933 else
5935 pt->vars = result;
5936 bitmap_clear (finished_solution);
5940 /* Given a pointer variable P, fill in its points-to set. */
5942 static void
5943 find_what_p_points_to (tree p)
5945 struct ptr_info_def *pi;
5946 tree lookup_p = p;
5947 varinfo_t vi;
5949 /* For parameters, get at the points-to set for the actual parm
5950 decl. */
5951 if (TREE_CODE (p) == SSA_NAME
5952 && SSA_NAME_IS_DEFAULT_DEF (p)
5953 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
5954 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL))
5955 lookup_p = SSA_NAME_VAR (p);
5957 vi = lookup_vi_for_tree (lookup_p);
5958 if (!vi)
5959 return;
5961 pi = get_ptr_info (p);
5962 find_what_var_points_to (vi, &pi->pt);
5966 /* Query statistics for points-to solutions. */
5968 static struct {
5969 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
5970 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
5971 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
5972 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
5973 } pta_stats;
5975 void
5976 dump_pta_stats (FILE *s)
5978 fprintf (s, "\nPTA query stats:\n");
5979 fprintf (s, " pt_solution_includes: "
5980 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
5981 HOST_WIDE_INT_PRINT_DEC" queries\n",
5982 pta_stats.pt_solution_includes_no_alias,
5983 pta_stats.pt_solution_includes_no_alias
5984 + pta_stats.pt_solution_includes_may_alias);
5985 fprintf (s, " pt_solutions_intersect: "
5986 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
5987 HOST_WIDE_INT_PRINT_DEC" queries\n",
5988 pta_stats.pt_solutions_intersect_no_alias,
5989 pta_stats.pt_solutions_intersect_no_alias
5990 + pta_stats.pt_solutions_intersect_may_alias);
5994 /* Reset the points-to solution *PT to a conservative default
5995 (point to anything). */
5997 void
5998 pt_solution_reset (struct pt_solution *pt)
6000 memset (pt, 0, sizeof (struct pt_solution));
6001 pt->anything = true;
6004 /* Set the points-to solution *PT to point only to the variables
6005 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6006 global variables and VARS_CONTAINS_RESTRICT specifies whether
6007 it contains restrict tag variables. */
6009 void
6010 pt_solution_set (struct pt_solution *pt, bitmap vars, bool vars_contains_global)
6012 memset (pt, 0, sizeof (struct pt_solution));
6013 pt->vars = vars;
6014 pt->vars_contains_global = vars_contains_global;
6017 /* Set the points-to solution *PT to point only to the variable VAR. */
6019 void
6020 pt_solution_set_var (struct pt_solution *pt, tree var)
6022 memset (pt, 0, sizeof (struct pt_solution));
6023 pt->vars = BITMAP_GGC_ALLOC ();
6024 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
6025 pt->vars_contains_global = is_global_var (var);
6028 /* Computes the union of the points-to solutions *DEST and *SRC and
6029 stores the result in *DEST. This changes the points-to bitmap
6030 of *DEST and thus may not be used if that might be shared.
6031 The points-to bitmap of *SRC and *DEST will not be shared after
6032 this function if they were not before. */
6034 static void
6035 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6037 dest->anything |= src->anything;
6038 if (dest->anything)
6040 pt_solution_reset (dest);
6041 return;
6044 dest->nonlocal |= src->nonlocal;
6045 dest->escaped |= src->escaped;
6046 dest->ipa_escaped |= src->ipa_escaped;
6047 dest->null |= src->null;
6048 dest->vars_contains_global |= src->vars_contains_global;
6049 if (!src->vars)
6050 return;
6052 if (!dest->vars)
6053 dest->vars = BITMAP_GGC_ALLOC ();
6054 bitmap_ior_into (dest->vars, src->vars);
6057 /* Return true if the points-to solution *PT is empty. */
6059 bool
6060 pt_solution_empty_p (struct pt_solution *pt)
6062 if (pt->anything
6063 || pt->nonlocal)
6064 return false;
6066 if (pt->vars
6067 && !bitmap_empty_p (pt->vars))
6068 return false;
6070 /* If the solution includes ESCAPED, check if that is empty. */
6071 if (pt->escaped
6072 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6073 return false;
6075 /* If the solution includes ESCAPED, check if that is empty. */
6076 if (pt->ipa_escaped
6077 && !pt_solution_empty_p (&ipa_escaped_pt))
6078 return false;
6080 return true;
6083 /* Return true if the points-to solution *PT only point to a single var, and
6084 return the var uid in *UID. */
6086 bool
6087 pt_solution_singleton_p (struct pt_solution *pt, unsigned *uid)
6089 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
6090 || pt->null || pt->vars == NULL
6091 || !bitmap_single_bit_set_p (pt->vars))
6092 return false;
6094 *uid = bitmap_first_set_bit (pt->vars);
6095 return true;
6098 /* Return true if the points-to solution *PT includes global memory. */
6100 bool
6101 pt_solution_includes_global (struct pt_solution *pt)
6103 if (pt->anything
6104 || pt->nonlocal
6105 || pt->vars_contains_global)
6106 return true;
6108 if (pt->escaped)
6109 return pt_solution_includes_global (&cfun->gimple_df->escaped);
6111 if (pt->ipa_escaped)
6112 return pt_solution_includes_global (&ipa_escaped_pt);
6114 /* ??? This predicate is not correct for the IPA-PTA solution
6115 as we do not properly distinguish between unit escape points
6116 and global variables. */
6117 if (cfun->gimple_df->ipa_pta)
6118 return true;
6120 return false;
6123 /* Return true if the points-to solution *PT includes the variable
6124 declaration DECL. */
6126 static bool
6127 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6129 if (pt->anything)
6130 return true;
6132 if (pt->nonlocal
6133 && is_global_var (decl))
6134 return true;
6136 if (pt->vars
6137 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6138 return true;
6140 /* If the solution includes ESCAPED, check it. */
6141 if (pt->escaped
6142 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6143 return true;
6145 /* If the solution includes ESCAPED, check it. */
6146 if (pt->ipa_escaped
6147 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6148 return true;
6150 return false;
6153 bool
6154 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6156 bool res = pt_solution_includes_1 (pt, decl);
6157 if (res)
6158 ++pta_stats.pt_solution_includes_may_alias;
6159 else
6160 ++pta_stats.pt_solution_includes_no_alias;
6161 return res;
6164 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6165 intersection. */
6167 static bool
6168 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
6170 if (pt1->anything || pt2->anything)
6171 return true;
6173 /* If either points to unknown global memory and the other points to
6174 any global memory they alias. */
6175 if ((pt1->nonlocal
6176 && (pt2->nonlocal
6177 || pt2->vars_contains_global))
6178 || (pt2->nonlocal
6179 && pt1->vars_contains_global))
6180 return true;
6182 /* Check the escaped solution if required. */
6183 if ((pt1->escaped || pt2->escaped)
6184 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6186 /* If both point to escaped memory and that solution
6187 is not empty they alias. */
6188 if (pt1->escaped && pt2->escaped)
6189 return true;
6191 /* If either points to escaped memory see if the escaped solution
6192 intersects with the other. */
6193 if ((pt1->escaped
6194 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt2))
6195 || (pt2->escaped
6196 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt1)))
6197 return true;
6200 /* Check the escaped solution if required.
6201 ??? Do we need to check the local against the IPA escaped sets? */
6202 if ((pt1->ipa_escaped || pt2->ipa_escaped)
6203 && !pt_solution_empty_p (&ipa_escaped_pt))
6205 /* If both point to escaped memory and that solution
6206 is not empty they alias. */
6207 if (pt1->ipa_escaped && pt2->ipa_escaped)
6208 return true;
6210 /* If either points to escaped memory see if the escaped solution
6211 intersects with the other. */
6212 if ((pt1->ipa_escaped
6213 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
6214 || (pt2->ipa_escaped
6215 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
6216 return true;
6219 /* Now both pointers alias if their points-to solution intersects. */
6220 return (pt1->vars
6221 && pt2->vars
6222 && bitmap_intersect_p (pt1->vars, pt2->vars));
6225 bool
6226 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
6228 bool res = pt_solutions_intersect_1 (pt1, pt2);
6229 if (res)
6230 ++pta_stats.pt_solutions_intersect_may_alias;
6231 else
6232 ++pta_stats.pt_solutions_intersect_no_alias;
6233 return res;
6237 /* Dump points-to information to OUTFILE. */
6239 static void
6240 dump_sa_points_to_info (FILE *outfile)
6242 unsigned int i;
6244 fprintf (outfile, "\nPoints-to sets\n\n");
6246 if (dump_flags & TDF_STATS)
6248 fprintf (outfile, "Stats:\n");
6249 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
6250 fprintf (outfile, "Non-pointer vars: %d\n",
6251 stats.nonpointer_vars);
6252 fprintf (outfile, "Statically unified vars: %d\n",
6253 stats.unified_vars_static);
6254 fprintf (outfile, "Dynamically unified vars: %d\n",
6255 stats.unified_vars_dynamic);
6256 fprintf (outfile, "Iterations: %d\n", stats.iterations);
6257 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
6258 fprintf (outfile, "Number of implicit edges: %d\n",
6259 stats.num_implicit_edges);
6262 for (i = 0; i < VEC_length (varinfo_t, varmap); i++)
6264 varinfo_t vi = get_varinfo (i);
6265 if (!vi->may_have_pointers)
6266 continue;
6267 dump_solution_for_var (outfile, i);
6272 /* Debug points-to information to stderr. */
6274 DEBUG_FUNCTION void
6275 debug_sa_points_to_info (void)
6277 dump_sa_points_to_info (stderr);
6281 /* Initialize the always-existing constraint variables for NULL
6282 ANYTHING, READONLY, and INTEGER */
6284 static void
6285 init_base_vars (void)
6287 struct constraint_expr lhs, rhs;
6288 varinfo_t var_anything;
6289 varinfo_t var_nothing;
6290 varinfo_t var_readonly;
6291 varinfo_t var_escaped;
6292 varinfo_t var_nonlocal;
6293 varinfo_t var_storedanything;
6294 varinfo_t var_integer;
6296 /* Create the NULL variable, used to represent that a variable points
6297 to NULL. */
6298 var_nothing = new_var_info (NULL_TREE, "NULL");
6299 gcc_assert (var_nothing->id == nothing_id);
6300 var_nothing->is_artificial_var = 1;
6301 var_nothing->offset = 0;
6302 var_nothing->size = ~0;
6303 var_nothing->fullsize = ~0;
6304 var_nothing->is_special_var = 1;
6305 var_nothing->may_have_pointers = 0;
6306 var_nothing->is_global_var = 0;
6308 /* Create the ANYTHING variable, used to represent that a variable
6309 points to some unknown piece of memory. */
6310 var_anything = new_var_info (NULL_TREE, "ANYTHING");
6311 gcc_assert (var_anything->id == anything_id);
6312 var_anything->is_artificial_var = 1;
6313 var_anything->size = ~0;
6314 var_anything->offset = 0;
6315 var_anything->next = NULL;
6316 var_anything->fullsize = ~0;
6317 var_anything->is_special_var = 1;
6319 /* Anything points to anything. This makes deref constraints just
6320 work in the presence of linked list and other p = *p type loops,
6321 by saying that *ANYTHING = ANYTHING. */
6322 lhs.type = SCALAR;
6323 lhs.var = anything_id;
6324 lhs.offset = 0;
6325 rhs.type = ADDRESSOF;
6326 rhs.var = anything_id;
6327 rhs.offset = 0;
6329 /* This specifically does not use process_constraint because
6330 process_constraint ignores all anything = anything constraints, since all
6331 but this one are redundant. */
6332 VEC_safe_push (constraint_t, heap, constraints, new_constraint (lhs, rhs));
6334 /* Create the READONLY variable, used to represent that a variable
6335 points to readonly memory. */
6336 var_readonly = new_var_info (NULL_TREE, "READONLY");
6337 gcc_assert (var_readonly->id == readonly_id);
6338 var_readonly->is_artificial_var = 1;
6339 var_readonly->offset = 0;
6340 var_readonly->size = ~0;
6341 var_readonly->fullsize = ~0;
6342 var_readonly->next = NULL;
6343 var_readonly->is_special_var = 1;
6345 /* readonly memory points to anything, in order to make deref
6346 easier. In reality, it points to anything the particular
6347 readonly variable can point to, but we don't track this
6348 separately. */
6349 lhs.type = SCALAR;
6350 lhs.var = readonly_id;
6351 lhs.offset = 0;
6352 rhs.type = ADDRESSOF;
6353 rhs.var = readonly_id; /* FIXME */
6354 rhs.offset = 0;
6355 process_constraint (new_constraint (lhs, rhs));
6357 /* Create the ESCAPED variable, used to represent the set of escaped
6358 memory. */
6359 var_escaped = new_var_info (NULL_TREE, "ESCAPED");
6360 gcc_assert (var_escaped->id == escaped_id);
6361 var_escaped->is_artificial_var = 1;
6362 var_escaped->offset = 0;
6363 var_escaped->size = ~0;
6364 var_escaped->fullsize = ~0;
6365 var_escaped->is_special_var = 0;
6367 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6368 memory. */
6369 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL");
6370 gcc_assert (var_nonlocal->id == nonlocal_id);
6371 var_nonlocal->is_artificial_var = 1;
6372 var_nonlocal->offset = 0;
6373 var_nonlocal->size = ~0;
6374 var_nonlocal->fullsize = ~0;
6375 var_nonlocal->is_special_var = 1;
6377 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6378 lhs.type = SCALAR;
6379 lhs.var = escaped_id;
6380 lhs.offset = 0;
6381 rhs.type = DEREF;
6382 rhs.var = escaped_id;
6383 rhs.offset = 0;
6384 process_constraint (new_constraint (lhs, rhs));
6386 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6387 whole variable escapes. */
6388 lhs.type = SCALAR;
6389 lhs.var = escaped_id;
6390 lhs.offset = 0;
6391 rhs.type = SCALAR;
6392 rhs.var = escaped_id;
6393 rhs.offset = UNKNOWN_OFFSET;
6394 process_constraint (new_constraint (lhs, rhs));
6396 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6397 everything pointed to by escaped points to what global memory can
6398 point to. */
6399 lhs.type = DEREF;
6400 lhs.var = escaped_id;
6401 lhs.offset = 0;
6402 rhs.type = SCALAR;
6403 rhs.var = nonlocal_id;
6404 rhs.offset = 0;
6405 process_constraint (new_constraint (lhs, rhs));
6407 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6408 global memory may point to global memory and escaped memory. */
6409 lhs.type = SCALAR;
6410 lhs.var = nonlocal_id;
6411 lhs.offset = 0;
6412 rhs.type = ADDRESSOF;
6413 rhs.var = nonlocal_id;
6414 rhs.offset = 0;
6415 process_constraint (new_constraint (lhs, rhs));
6416 rhs.type = ADDRESSOF;
6417 rhs.var = escaped_id;
6418 rhs.offset = 0;
6419 process_constraint (new_constraint (lhs, rhs));
6421 /* Create the STOREDANYTHING variable, used to represent the set of
6422 variables stored to *ANYTHING. */
6423 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING");
6424 gcc_assert (var_storedanything->id == storedanything_id);
6425 var_storedanything->is_artificial_var = 1;
6426 var_storedanything->offset = 0;
6427 var_storedanything->size = ~0;
6428 var_storedanything->fullsize = ~0;
6429 var_storedanything->is_special_var = 0;
6431 /* Create the INTEGER variable, used to represent that a variable points
6432 to what an INTEGER "points to". */
6433 var_integer = new_var_info (NULL_TREE, "INTEGER");
6434 gcc_assert (var_integer->id == integer_id);
6435 var_integer->is_artificial_var = 1;
6436 var_integer->size = ~0;
6437 var_integer->fullsize = ~0;
6438 var_integer->offset = 0;
6439 var_integer->next = NULL;
6440 var_integer->is_special_var = 1;
6442 /* INTEGER = ANYTHING, because we don't know where a dereference of
6443 a random integer will point to. */
6444 lhs.type = SCALAR;
6445 lhs.var = integer_id;
6446 lhs.offset = 0;
6447 rhs.type = ADDRESSOF;
6448 rhs.var = anything_id;
6449 rhs.offset = 0;
6450 process_constraint (new_constraint (lhs, rhs));
6453 /* Initialize things necessary to perform PTA */
6455 static void
6456 init_alias_vars (void)
6458 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
6460 bitmap_obstack_initialize (&pta_obstack);
6461 bitmap_obstack_initialize (&oldpta_obstack);
6462 bitmap_obstack_initialize (&predbitmap_obstack);
6464 constraint_pool = create_alloc_pool ("Constraint pool",
6465 sizeof (struct constraint), 30);
6466 variable_info_pool = create_alloc_pool ("Variable info pool",
6467 sizeof (struct variable_info), 30);
6468 constraints = VEC_alloc (constraint_t, heap, 8);
6469 varmap = VEC_alloc (varinfo_t, heap, 8);
6470 vi_for_tree = pointer_map_create ();
6471 call_stmt_vars = pointer_map_create ();
6473 memset (&stats, 0, sizeof (stats));
6474 shared_bitmap_table = htab_create (511, shared_bitmap_hash,
6475 shared_bitmap_eq, free);
6476 init_base_vars ();
6478 gcc_obstack_init (&fake_var_decl_obstack);
6481 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6482 predecessor edges. */
6484 static void
6485 remove_preds_and_fake_succs (constraint_graph_t graph)
6487 unsigned int i;
6489 /* Clear the implicit ref and address nodes from the successor
6490 lists. */
6491 for (i = 0; i < FIRST_REF_NODE; i++)
6493 if (graph->succs[i])
6494 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
6495 FIRST_REF_NODE * 2);
6498 /* Free the successor list for the non-ref nodes. */
6499 for (i = FIRST_REF_NODE; i < graph->size; i++)
6501 if (graph->succs[i])
6502 BITMAP_FREE (graph->succs[i]);
6505 /* Now reallocate the size of the successor list as, and blow away
6506 the predecessor bitmaps. */
6507 graph->size = VEC_length (varinfo_t, varmap);
6508 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
6510 free (graph->implicit_preds);
6511 graph->implicit_preds = NULL;
6512 free (graph->preds);
6513 graph->preds = NULL;
6514 bitmap_obstack_release (&predbitmap_obstack);
6517 /* Solve the constraint set. */
6519 static void
6520 solve_constraints (void)
6522 struct scc_info *si;
6524 if (dump_file)
6525 fprintf (dump_file,
6526 "\nCollapsing static cycles and doing variable "
6527 "substitution\n");
6529 init_graph (VEC_length (varinfo_t, varmap) * 2);
6531 if (dump_file)
6532 fprintf (dump_file, "Building predecessor graph\n");
6533 build_pred_graph ();
6535 if (dump_file)
6536 fprintf (dump_file, "Detecting pointer and location "
6537 "equivalences\n");
6538 si = perform_var_substitution (graph);
6540 if (dump_file)
6541 fprintf (dump_file, "Rewriting constraints and unifying "
6542 "variables\n");
6543 rewrite_constraints (graph, si);
6545 build_succ_graph ();
6547 free_var_substitution_info (si);
6549 /* Attach complex constraints to graph nodes. */
6550 move_complex_constraints (graph);
6552 if (dump_file)
6553 fprintf (dump_file, "Uniting pointer but not location equivalent "
6554 "variables\n");
6555 unite_pointer_equivalences (graph);
6557 if (dump_file)
6558 fprintf (dump_file, "Finding indirect cycles\n");
6559 find_indirect_cycles (graph);
6561 /* Implicit nodes and predecessors are no longer necessary at this
6562 point. */
6563 remove_preds_and_fake_succs (graph);
6565 if (dump_file && (dump_flags & TDF_GRAPH))
6567 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
6568 "in dot format:\n");
6569 dump_constraint_graph (dump_file);
6570 fprintf (dump_file, "\n\n");
6573 if (dump_file)
6574 fprintf (dump_file, "Solving graph\n");
6576 solve_graph (graph);
6578 if (dump_file && (dump_flags & TDF_GRAPH))
6580 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
6581 "in dot format:\n");
6582 dump_constraint_graph (dump_file);
6583 fprintf (dump_file, "\n\n");
6586 if (dump_file)
6587 dump_sa_points_to_info (dump_file);
6590 /* Create points-to sets for the current function. See the comments
6591 at the start of the file for an algorithmic overview. */
6593 static void
6594 compute_points_to_sets (void)
6596 basic_block bb;
6597 unsigned i;
6598 varinfo_t vi;
6600 timevar_push (TV_TREE_PTA);
6602 init_alias_vars ();
6604 intra_create_variable_infos ();
6606 /* Now walk all statements and build the constraint set. */
6607 FOR_EACH_BB (bb)
6609 gimple_stmt_iterator gsi;
6611 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6613 gimple phi = gsi_stmt (gsi);
6615 if (! virtual_operand_p (gimple_phi_result (phi)))
6616 find_func_aliases (phi);
6619 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6621 gimple stmt = gsi_stmt (gsi);
6623 find_func_aliases (stmt);
6627 if (dump_file)
6629 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
6630 dump_constraints (dump_file, 0);
6633 /* From the constraints compute the points-to sets. */
6634 solve_constraints ();
6636 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6637 find_what_var_points_to (get_varinfo (escaped_id),
6638 &cfun->gimple_df->escaped);
6640 /* Make sure the ESCAPED solution (which is used as placeholder in
6641 other solutions) does not reference itself. This simplifies
6642 points-to solution queries. */
6643 cfun->gimple_df->escaped.escaped = 0;
6645 /* Mark escaped HEAP variables as global. */
6646 FOR_EACH_VEC_ELT (varinfo_t, varmap, i, vi)
6647 if (vi->is_heap_var
6648 && !vi->is_global_var)
6649 DECL_EXTERNAL (vi->decl) = vi->is_global_var
6650 = pt_solution_includes (&cfun->gimple_df->escaped, vi->decl);
6652 /* Compute the points-to sets for pointer SSA_NAMEs. */
6653 for (i = 0; i < num_ssa_names; ++i)
6655 tree ptr = ssa_name (i);
6656 if (ptr
6657 && POINTER_TYPE_P (TREE_TYPE (ptr)))
6658 find_what_p_points_to (ptr);
6661 /* Compute the call-used/clobbered sets. */
6662 FOR_EACH_BB (bb)
6664 gimple_stmt_iterator gsi;
6666 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6668 gimple stmt = gsi_stmt (gsi);
6669 struct pt_solution *pt;
6670 if (!is_gimple_call (stmt))
6671 continue;
6673 pt = gimple_call_use_set (stmt);
6674 if (gimple_call_flags (stmt) & ECF_CONST)
6675 memset (pt, 0, sizeof (struct pt_solution));
6676 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
6678 find_what_var_points_to (vi, pt);
6679 /* Escaped (and thus nonlocal) variables are always
6680 implicitly used by calls. */
6681 /* ??? ESCAPED can be empty even though NONLOCAL
6682 always escaped. */
6683 pt->nonlocal = 1;
6684 pt->escaped = 1;
6686 else
6688 /* If there is nothing special about this call then
6689 we have made everything that is used also escape. */
6690 *pt = cfun->gimple_df->escaped;
6691 pt->nonlocal = 1;
6694 pt = gimple_call_clobber_set (stmt);
6695 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
6696 memset (pt, 0, sizeof (struct pt_solution));
6697 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
6699 find_what_var_points_to (vi, pt);
6700 /* Escaped (and thus nonlocal) variables are always
6701 implicitly clobbered by calls. */
6702 /* ??? ESCAPED can be empty even though NONLOCAL
6703 always escaped. */
6704 pt->nonlocal = 1;
6705 pt->escaped = 1;
6707 else
6709 /* If there is nothing special about this call then
6710 we have made everything that is used also escape. */
6711 *pt = cfun->gimple_df->escaped;
6712 pt->nonlocal = 1;
6717 timevar_pop (TV_TREE_PTA);
6721 /* Delete created points-to sets. */
6723 static void
6724 delete_points_to_sets (void)
6726 unsigned int i;
6728 htab_delete (shared_bitmap_table);
6729 if (dump_file && (dump_flags & TDF_STATS))
6730 fprintf (dump_file, "Points to sets created:%d\n",
6731 stats.points_to_sets_created);
6733 pointer_map_destroy (vi_for_tree);
6734 pointer_map_destroy (call_stmt_vars);
6735 bitmap_obstack_release (&pta_obstack);
6736 VEC_free (constraint_t, heap, constraints);
6738 for (i = 0; i < graph->size; i++)
6739 VEC_free (constraint_t, heap, graph->complex[i]);
6740 free (graph->complex);
6742 free (graph->rep);
6743 free (graph->succs);
6744 free (graph->pe);
6745 free (graph->pe_rep);
6746 free (graph->indirect_cycles);
6747 free (graph);
6749 VEC_free (varinfo_t, heap, varmap);
6750 free_alloc_pool (variable_info_pool);
6751 free_alloc_pool (constraint_pool);
6753 obstack_free (&fake_var_decl_obstack, NULL);
6757 /* Compute points-to information for every SSA_NAME pointer in the
6758 current function and compute the transitive closure of escaped
6759 variables to re-initialize the call-clobber states of local variables. */
6761 unsigned int
6762 compute_may_aliases (void)
6764 if (cfun->gimple_df->ipa_pta)
6766 if (dump_file)
6768 fprintf (dump_file, "\nNot re-computing points-to information "
6769 "because IPA points-to information is available.\n\n");
6771 /* But still dump what we have remaining it. */
6772 dump_alias_info (dump_file);
6775 return 0;
6778 /* For each pointer P_i, determine the sets of variables that P_i may
6779 point-to. Compute the reachability set of escaped and call-used
6780 variables. */
6781 compute_points_to_sets ();
6783 /* Debugging dumps. */
6784 if (dump_file)
6785 dump_alias_info (dump_file);
6787 /* Deallocate memory used by aliasing data structures and the internal
6788 points-to solution. */
6789 delete_points_to_sets ();
6791 gcc_assert (!need_ssa_update_p (cfun));
6793 return 0;
6796 static bool
6797 gate_tree_pta (void)
6799 return flag_tree_pta;
6802 /* A dummy pass to cause points-to information to be computed via
6803 TODO_rebuild_alias. */
6805 struct gimple_opt_pass pass_build_alias =
6808 GIMPLE_PASS,
6809 "alias", /* name */
6810 OPTGROUP_NONE, /* optinfo_flags */
6811 gate_tree_pta, /* gate */
6812 NULL, /* execute */
6813 NULL, /* sub */
6814 NULL, /* next */
6815 0, /* static_pass_number */
6816 TV_NONE, /* tv_id */
6817 PROP_cfg | PROP_ssa, /* properties_required */
6818 0, /* properties_provided */
6819 0, /* properties_destroyed */
6820 0, /* todo_flags_start */
6821 TODO_rebuild_alias /* todo_flags_finish */
6825 /* A dummy pass to cause points-to information to be computed via
6826 TODO_rebuild_alias. */
6828 struct gimple_opt_pass pass_build_ealias =
6831 GIMPLE_PASS,
6832 "ealias", /* name */
6833 OPTGROUP_NONE, /* optinfo_flags */
6834 gate_tree_pta, /* gate */
6835 NULL, /* execute */
6836 NULL, /* sub */
6837 NULL, /* next */
6838 0, /* static_pass_number */
6839 TV_NONE, /* tv_id */
6840 PROP_cfg | PROP_ssa, /* properties_required */
6841 0, /* properties_provided */
6842 0, /* properties_destroyed */
6843 0, /* todo_flags_start */
6844 TODO_rebuild_alias /* todo_flags_finish */
6849 /* Return true if we should execute IPA PTA. */
6850 static bool
6851 gate_ipa_pta (void)
6853 return (optimize
6854 && flag_ipa_pta
6855 /* Don't bother doing anything if the program has errors. */
6856 && !seen_error ());
6859 /* IPA PTA solutions for ESCAPED. */
6860 struct pt_solution ipa_escaped_pt
6861 = { true, false, false, false, false, false, NULL };
6863 /* Associate node with varinfo DATA. Worker for
6864 cgraph_for_node_and_aliases. */
6865 static bool
6866 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
6868 if (node->alias || node->thunk.thunk_p)
6869 insert_vi_for_tree (node->symbol.decl, (varinfo_t)data);
6870 return false;
6873 /* Execute the driver for IPA PTA. */
6874 static unsigned int
6875 ipa_pta_execute (void)
6877 struct cgraph_node *node;
6878 struct varpool_node *var;
6879 int from;
6881 in_ipa_mode = 1;
6883 init_alias_vars ();
6885 if (dump_file && (dump_flags & TDF_DETAILS))
6887 dump_symtab (dump_file);
6888 fprintf (dump_file, "\n");
6891 /* Build the constraints. */
6892 FOR_EACH_DEFINED_FUNCTION (node)
6894 varinfo_t vi;
6895 /* Nodes without a body are not interesting. Especially do not
6896 visit clones at this point for now - we get duplicate decls
6897 there for inline clones at least. */
6898 if (!cgraph_function_with_gimple_body_p (node))
6899 continue;
6901 gcc_assert (!node->clone_of);
6903 vi = create_function_info_for (node->symbol.decl,
6904 alias_get_name (node->symbol.decl));
6905 cgraph_for_node_and_aliases (node, associate_varinfo_to_alias, vi, true);
6908 /* Create constraints for global variables and their initializers. */
6909 FOR_EACH_VARIABLE (var)
6911 if (var->alias)
6912 continue;
6914 get_vi_for_tree (var->symbol.decl);
6917 if (dump_file)
6919 fprintf (dump_file,
6920 "Generating constraints for global initializers\n\n");
6921 dump_constraints (dump_file, 0);
6922 fprintf (dump_file, "\n");
6924 from = VEC_length (constraint_t, constraints);
6926 FOR_EACH_DEFINED_FUNCTION (node)
6928 struct function *func;
6929 basic_block bb;
6931 /* Nodes without a body are not interesting. */
6932 if (!cgraph_function_with_gimple_body_p (node))
6933 continue;
6935 if (dump_file)
6937 fprintf (dump_file,
6938 "Generating constraints for %s", cgraph_node_name (node));
6939 if (DECL_ASSEMBLER_NAME_SET_P (node->symbol.decl))
6940 fprintf (dump_file, " (%s)",
6941 IDENTIFIER_POINTER
6942 (DECL_ASSEMBLER_NAME (node->symbol.decl)));
6943 fprintf (dump_file, "\n");
6946 func = DECL_STRUCT_FUNCTION (node->symbol.decl);
6947 push_cfun (func);
6949 /* For externally visible or attribute used annotated functions use
6950 local constraints for their arguments.
6951 For local functions we see all callers and thus do not need initial
6952 constraints for parameters. */
6953 if (node->symbol.used_from_other_partition
6954 || node->symbol.externally_visible
6955 || node->symbol.force_output)
6957 intra_create_variable_infos ();
6959 /* We also need to make function return values escape. Nothing
6960 escapes by returning from main though. */
6961 if (!MAIN_NAME_P (DECL_NAME (node->symbol.decl)))
6963 varinfo_t fi, rvi;
6964 fi = lookup_vi_for_tree (node->symbol.decl);
6965 rvi = first_vi_for_offset (fi, fi_result);
6966 if (rvi && rvi->offset == fi_result)
6968 struct constraint_expr includes;
6969 struct constraint_expr var;
6970 includes.var = escaped_id;
6971 includes.offset = 0;
6972 includes.type = SCALAR;
6973 var.var = rvi->id;
6974 var.offset = 0;
6975 var.type = SCALAR;
6976 process_constraint (new_constraint (includes, var));
6981 /* Build constriants for the function body. */
6982 FOR_EACH_BB_FN (bb, func)
6984 gimple_stmt_iterator gsi;
6986 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
6987 gsi_next (&gsi))
6989 gimple phi = gsi_stmt (gsi);
6991 if (! virtual_operand_p (gimple_phi_result (phi)))
6992 find_func_aliases (phi);
6995 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6997 gimple stmt = gsi_stmt (gsi);
6999 find_func_aliases (stmt);
7000 find_func_clobbers (stmt);
7004 pop_cfun ();
7006 if (dump_file)
7008 fprintf (dump_file, "\n");
7009 dump_constraints (dump_file, from);
7010 fprintf (dump_file, "\n");
7012 from = VEC_length (constraint_t, constraints);
7015 /* From the constraints compute the points-to sets. */
7016 solve_constraints ();
7018 /* Compute the global points-to sets for ESCAPED.
7019 ??? Note that the computed escape set is not correct
7020 for the whole unit as we fail to consider graph edges to
7021 externally visible functions. */
7022 find_what_var_points_to (get_varinfo (escaped_id), &ipa_escaped_pt);
7024 /* Make sure the ESCAPED solution (which is used as placeholder in
7025 other solutions) does not reference itself. This simplifies
7026 points-to solution queries. */
7027 ipa_escaped_pt.ipa_escaped = 0;
7029 /* Assign the points-to sets to the SSA names in the unit. */
7030 FOR_EACH_DEFINED_FUNCTION (node)
7032 tree ptr;
7033 struct function *fn;
7034 unsigned i;
7035 varinfo_t fi;
7036 basic_block bb;
7037 struct pt_solution uses, clobbers;
7038 struct cgraph_edge *e;
7040 /* Nodes without a body are not interesting. */
7041 if (!cgraph_function_with_gimple_body_p (node))
7042 continue;
7044 fn = DECL_STRUCT_FUNCTION (node->symbol.decl);
7046 /* Compute the points-to sets for pointer SSA_NAMEs. */
7047 FOR_EACH_VEC_ELT (tree, fn->gimple_df->ssa_names, i, ptr)
7049 if (ptr
7050 && POINTER_TYPE_P (TREE_TYPE (ptr)))
7051 find_what_p_points_to (ptr);
7054 /* Compute the call-use and call-clobber sets for all direct calls. */
7055 fi = lookup_vi_for_tree (node->symbol.decl);
7056 gcc_assert (fi->is_fn_info);
7057 find_what_var_points_to (first_vi_for_offset (fi, fi_clobbers),
7058 &clobbers);
7059 find_what_var_points_to (first_vi_for_offset (fi, fi_uses), &uses);
7060 for (e = node->callers; e; e = e->next_caller)
7062 if (!e->call_stmt)
7063 continue;
7065 *gimple_call_clobber_set (e->call_stmt) = clobbers;
7066 *gimple_call_use_set (e->call_stmt) = uses;
7069 /* Compute the call-use and call-clobber sets for indirect calls
7070 and calls to external functions. */
7071 FOR_EACH_BB_FN (bb, fn)
7073 gimple_stmt_iterator gsi;
7075 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7077 gimple stmt = gsi_stmt (gsi);
7078 struct pt_solution *pt;
7079 varinfo_t vi;
7080 tree decl;
7082 if (!is_gimple_call (stmt))
7083 continue;
7085 /* Handle direct calls to external functions. */
7086 decl = gimple_call_fndecl (stmt);
7087 if (decl
7088 && (!(fi = lookup_vi_for_tree (decl))
7089 || !fi->is_fn_info))
7091 pt = gimple_call_use_set (stmt);
7092 if (gimple_call_flags (stmt) & ECF_CONST)
7093 memset (pt, 0, sizeof (struct pt_solution));
7094 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7096 find_what_var_points_to (vi, pt);
7097 /* Escaped (and thus nonlocal) variables are always
7098 implicitly used by calls. */
7099 /* ??? ESCAPED can be empty even though NONLOCAL
7100 always escaped. */
7101 pt->nonlocal = 1;
7102 pt->ipa_escaped = 1;
7104 else
7106 /* If there is nothing special about this call then
7107 we have made everything that is used also escape. */
7108 *pt = ipa_escaped_pt;
7109 pt->nonlocal = 1;
7112 pt = gimple_call_clobber_set (stmt);
7113 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7114 memset (pt, 0, sizeof (struct pt_solution));
7115 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7117 find_what_var_points_to (vi, pt);
7118 /* Escaped (and thus nonlocal) variables are always
7119 implicitly clobbered by calls. */
7120 /* ??? ESCAPED can be empty even though NONLOCAL
7121 always escaped. */
7122 pt->nonlocal = 1;
7123 pt->ipa_escaped = 1;
7125 else
7127 /* If there is nothing special about this call then
7128 we have made everything that is used also escape. */
7129 *pt = ipa_escaped_pt;
7130 pt->nonlocal = 1;
7134 /* Handle indirect calls. */
7135 if (!decl
7136 && (fi = get_fi_for_callee (stmt)))
7138 /* We need to accumulate all clobbers/uses of all possible
7139 callees. */
7140 fi = get_varinfo (find (fi->id));
7141 /* If we cannot constrain the set of functions we'll end up
7142 calling we end up using/clobbering everything. */
7143 if (bitmap_bit_p (fi->solution, anything_id)
7144 || bitmap_bit_p (fi->solution, nonlocal_id)
7145 || bitmap_bit_p (fi->solution, escaped_id))
7147 pt_solution_reset (gimple_call_clobber_set (stmt));
7148 pt_solution_reset (gimple_call_use_set (stmt));
7150 else
7152 bitmap_iterator bi;
7153 unsigned i;
7154 struct pt_solution *uses, *clobbers;
7156 uses = gimple_call_use_set (stmt);
7157 clobbers = gimple_call_clobber_set (stmt);
7158 memset (uses, 0, sizeof (struct pt_solution));
7159 memset (clobbers, 0, sizeof (struct pt_solution));
7160 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
7162 struct pt_solution sol;
7164 vi = get_varinfo (i);
7165 if (!vi->is_fn_info)
7167 /* ??? We could be more precise here? */
7168 uses->nonlocal = 1;
7169 uses->ipa_escaped = 1;
7170 clobbers->nonlocal = 1;
7171 clobbers->ipa_escaped = 1;
7172 continue;
7175 if (!uses->anything)
7177 find_what_var_points_to
7178 (first_vi_for_offset (vi, fi_uses), &sol);
7179 pt_solution_ior_into (uses, &sol);
7181 if (!clobbers->anything)
7183 find_what_var_points_to
7184 (first_vi_for_offset (vi, fi_clobbers), &sol);
7185 pt_solution_ior_into (clobbers, &sol);
7193 fn->gimple_df->ipa_pta = true;
7196 delete_points_to_sets ();
7198 in_ipa_mode = 0;
7200 return 0;
7203 struct simple_ipa_opt_pass pass_ipa_pta =
7206 SIMPLE_IPA_PASS,
7207 "pta", /* name */
7208 OPTGROUP_NONE, /* optinfo_flags */
7209 gate_ipa_pta, /* gate */
7210 ipa_pta_execute, /* execute */
7211 NULL, /* sub */
7212 NULL, /* next */
7213 0, /* static_pass_number */
7214 TV_IPA_PTA, /* tv_id */
7215 0, /* properties_required */
7216 0, /* properties_provided */
7217 0, /* properties_destroyed */
7218 0, /* todo_flags_start */
7219 TODO_update_ssa /* todo_flags_finish */