Daily bump.
[official-gcc.git] / gcc / tree-ssa-structalias.c
blobabc99ba377a49d402df8c90f19982e3f2ed54241
1 /* Tree based points-to analysis
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dberlin@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "obstack.h"
26 #include "bitmap.h"
27 #include "sbitmap.h"
28 #include "flags.h"
29 #include "basic-block.h"
30 #include "tree.h"
31 #include "stor-layout.h"
32 #include "stmt.h"
33 #include "pointer-set.h"
34 #include "hash-table.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-expr.h"
38 #include "is-a.h"
39 #include "gimple.h"
40 #include "gimple-iterator.h"
41 #include "gimple-ssa.h"
42 #include "cgraph.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
45 #include "tree-into-ssa.h"
46 #include "expr.h"
47 #include "tree-dfa.h"
48 #include "tree-inline.h"
49 #include "diagnostic-core.h"
50 #include "function.h"
51 #include "tree-pass.h"
52 #include "alloc-pool.h"
53 #include "splay-tree.h"
54 #include "params.h"
55 #include "alias.h"
57 /* The idea behind this analyzer is to generate set constraints from the
58 program, then solve the resulting constraints in order to generate the
59 points-to sets.
61 Set constraints are a way of modeling program analysis problems that
62 involve sets. They consist of an inclusion constraint language,
63 describing the variables (each variable is a set) and operations that
64 are involved on the variables, and a set of rules that derive facts
65 from these operations. To solve a system of set constraints, you derive
66 all possible facts under the rules, which gives you the correct sets
67 as a consequence.
69 See "Efficient Field-sensitive pointer analysis for C" by "David
70 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
71 http://citeseer.ist.psu.edu/pearce04efficient.html
73 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
74 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
75 http://citeseer.ist.psu.edu/heintze01ultrafast.html
77 There are three types of real constraint expressions, DEREF,
78 ADDRESSOF, and SCALAR. Each constraint expression consists
79 of a constraint type, a variable, and an offset.
81 SCALAR is a constraint expression type used to represent x, whether
82 it appears on the LHS or the RHS of a statement.
83 DEREF is a constraint expression type used to represent *x, whether
84 it appears on the LHS or the RHS of a statement.
85 ADDRESSOF is a constraint expression used to represent &x, whether
86 it appears on the LHS or the RHS of a statement.
88 Each pointer variable in the program is assigned an integer id, and
89 each field of a structure variable is assigned an integer id as well.
91 Structure variables are linked to their list of fields through a "next
92 field" in each variable that points to the next field in offset
93 order.
94 Each variable for a structure field has
96 1. "size", that tells the size in bits of that field.
97 2. "fullsize, that tells the size in bits of the entire structure.
98 3. "offset", that tells the offset in bits from the beginning of the
99 structure to this field.
101 Thus,
102 struct f
104 int a;
105 int b;
106 } foo;
107 int *bar;
109 looks like
111 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
112 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
113 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
116 In order to solve the system of set constraints, the following is
117 done:
119 1. Each constraint variable x has a solution set associated with it,
120 Sol(x).
122 2. Constraints are separated into direct, copy, and complex.
123 Direct constraints are ADDRESSOF constraints that require no extra
124 processing, such as P = &Q
125 Copy constraints are those of the form P = Q.
126 Complex constraints are all the constraints involving dereferences
127 and offsets (including offsetted copies).
129 3. All direct constraints of the form P = &Q are processed, such
130 that Q is added to Sol(P)
132 4. All complex constraints for a given constraint variable are stored in a
133 linked list attached to that variable's node.
135 5. A directed graph is built out of the copy constraints. Each
136 constraint variable is a node in the graph, and an edge from
137 Q to P is added for each copy constraint of the form P = Q
139 6. The graph is then walked, and solution sets are
140 propagated along the copy edges, such that an edge from Q to P
141 causes Sol(P) <- Sol(P) union Sol(Q).
143 7. As we visit each node, all complex constraints associated with
144 that node are processed by adding appropriate copy edges to the graph, or the
145 appropriate variables to the solution set.
147 8. The process of walking the graph is iterated until no solution
148 sets change.
150 Prior to walking the graph in steps 6 and 7, We perform static
151 cycle elimination on the constraint graph, as well
152 as off-line variable substitution.
154 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
155 on and turned into anything), but isn't. You can just see what offset
156 inside the pointed-to struct it's going to access.
158 TODO: Constant bounded arrays can be handled as if they were structs of the
159 same number of elements.
161 TODO: Modeling heap and incoming pointers becomes much better if we
162 add fields to them as we discover them, which we could do.
164 TODO: We could handle unions, but to be honest, it's probably not
165 worth the pain or slowdown. */
167 /* IPA-PTA optimizations possible.
169 When the indirect function called is ANYTHING we can add disambiguation
170 based on the function signatures (or simply the parameter count which
171 is the varinfo size). We also do not need to consider functions that
172 do not have their address taken.
174 The is_global_var bit which marks escape points is overly conservative
175 in IPA mode. Split it to is_escape_point and is_global_var - only
176 externally visible globals are escape points in IPA mode. This is
177 also needed to fix the pt_solution_includes_global predicate
178 (and thus ptr_deref_may_alias_global_p).
180 The way we introduce DECL_PT_UID to avoid fixing up all points-to
181 sets in the translation unit when we copy a DECL during inlining
182 pessimizes precision. The advantage is that the DECL_PT_UID keeps
183 compile-time and memory usage overhead low - the points-to sets
184 do not grow or get unshared as they would during a fixup phase.
185 An alternative solution is to delay IPA PTA until after all
186 inlining transformations have been applied.
188 The way we propagate clobber/use information isn't optimized.
189 It should use a new complex constraint that properly filters
190 out local variables of the callee (though that would make
191 the sets invalid after inlining). OTOH we might as well
192 admit defeat to WHOPR and simply do all the clobber/use analysis
193 and propagation after PTA finished but before we threw away
194 points-to information for memory variables. WHOPR and PTA
195 do not play along well anyway - the whole constraint solving
196 would need to be done in WPA phase and it will be very interesting
197 to apply the results to local SSA names during LTRANS phase.
199 We probably should compute a per-function unit-ESCAPE solution
200 propagating it simply like the clobber / uses solutions. The
201 solution can go alongside the non-IPA espaced solution and be
202 used to query which vars escape the unit through a function.
204 We never put function decls in points-to sets so we do not
205 keep the set of called functions for indirect calls.
207 And probably more. */
209 static bool use_field_sensitive = true;
210 static int in_ipa_mode = 0;
212 /* Used for predecessor bitmaps. */
213 static bitmap_obstack predbitmap_obstack;
215 /* Used for points-to sets. */
216 static bitmap_obstack pta_obstack;
218 /* Used for oldsolution members of variables. */
219 static bitmap_obstack oldpta_obstack;
221 /* Used for per-solver-iteration bitmaps. */
222 static bitmap_obstack iteration_obstack;
224 static unsigned int create_variable_info_for (tree, const char *);
225 typedef struct constraint_graph *constraint_graph_t;
226 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
228 struct constraint;
229 typedef struct constraint *constraint_t;
232 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
233 if (a) \
234 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
236 static struct constraint_stats
238 unsigned int total_vars;
239 unsigned int nonpointer_vars;
240 unsigned int unified_vars_static;
241 unsigned int unified_vars_dynamic;
242 unsigned int iterations;
243 unsigned int num_edges;
244 unsigned int num_implicit_edges;
245 unsigned int points_to_sets_created;
246 } stats;
248 struct variable_info
250 /* ID of this variable */
251 unsigned int id;
253 /* True if this is a variable created by the constraint analysis, such as
254 heap variables and constraints we had to break up. */
255 unsigned int is_artificial_var : 1;
257 /* True if this is a special variable whose solution set should not be
258 changed. */
259 unsigned int is_special_var : 1;
261 /* True for variables whose size is not known or variable. */
262 unsigned int is_unknown_size_var : 1;
264 /* True for (sub-)fields that represent a whole variable. */
265 unsigned int is_full_var : 1;
267 /* True if this is a heap variable. */
268 unsigned int is_heap_var : 1;
270 /* True if this field may contain pointers. */
271 unsigned int may_have_pointers : 1;
273 /* True if this field has only restrict qualified pointers. */
274 unsigned int only_restrict_pointers : 1;
276 /* True if this represents a global variable. */
277 unsigned int is_global_var : 1;
279 /* True if this represents a IPA function info. */
280 unsigned int is_fn_info : 1;
282 /* The ID of the variable for the next field in this structure
283 or zero for the last field in this structure. */
284 unsigned next;
286 /* The ID of the variable for the first field in this structure. */
287 unsigned head;
289 /* Offset of this variable, in bits, from the base variable */
290 unsigned HOST_WIDE_INT offset;
292 /* Size of the variable, in bits. */
293 unsigned HOST_WIDE_INT size;
295 /* Full size of the base variable, in bits. */
296 unsigned HOST_WIDE_INT fullsize;
298 /* Name of this variable */
299 const char *name;
301 /* Tree that this variable is associated with. */
302 tree decl;
304 /* Points-to set for this variable. */
305 bitmap solution;
307 /* Old points-to set for this variable. */
308 bitmap oldsolution;
310 typedef struct variable_info *varinfo_t;
312 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
313 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
314 unsigned HOST_WIDE_INT);
315 static varinfo_t lookup_vi_for_tree (tree);
316 static inline bool type_can_have_subvars (const_tree);
318 /* Pool of variable info structures. */
319 static alloc_pool variable_info_pool;
321 /* Map varinfo to final pt_solution. */
322 static pointer_map_t *final_solutions;
323 struct obstack final_solutions_obstack;
325 /* Table of variable info structures for constraint variables.
326 Indexed directly by variable info id. */
327 static vec<varinfo_t> varmap;
329 /* Return the varmap element N */
331 static inline varinfo_t
332 get_varinfo (unsigned int n)
334 return varmap[n];
337 /* Return the next variable in the list of sub-variables of VI
338 or NULL if VI is the last sub-variable. */
340 static inline varinfo_t
341 vi_next (varinfo_t vi)
343 return get_varinfo (vi->next);
346 /* Static IDs for the special variables. Variable ID zero is unused
347 and used as terminator for the sub-variable chain. */
348 enum { nothing_id = 1, anything_id = 2, readonly_id = 3,
349 escaped_id = 4, nonlocal_id = 5,
350 storedanything_id = 6, integer_id = 7 };
352 /* Return a new variable info structure consisting for a variable
353 named NAME, and using constraint graph node NODE. Append it
354 to the vector of variable info structures. */
356 static varinfo_t
357 new_var_info (tree t, const char *name)
359 unsigned index = varmap.length ();
360 varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
362 ret->id = index;
363 ret->name = name;
364 ret->decl = t;
365 /* Vars without decl are artificial and do not have sub-variables. */
366 ret->is_artificial_var = (t == NULL_TREE);
367 ret->is_special_var = false;
368 ret->is_unknown_size_var = false;
369 ret->is_full_var = (t == NULL_TREE);
370 ret->is_heap_var = false;
371 ret->may_have_pointers = true;
372 ret->only_restrict_pointers = false;
373 ret->is_global_var = (t == NULL_TREE);
374 ret->is_fn_info = false;
375 if (t && DECL_P (t))
376 ret->is_global_var = (is_global_var (t)
377 /* We have to treat even local register variables
378 as escape points. */
379 || (TREE_CODE (t) == VAR_DECL
380 && DECL_HARD_REGISTER (t)));
381 ret->solution = BITMAP_ALLOC (&pta_obstack);
382 ret->oldsolution = NULL;
383 ret->next = 0;
384 ret->head = ret->id;
386 stats.total_vars++;
388 varmap.safe_push (ret);
390 return ret;
394 /* A map mapping call statements to per-stmt variables for uses
395 and clobbers specific to the call. */
396 static struct pointer_map_t *call_stmt_vars;
398 /* Lookup or create the variable for the call statement CALL. */
400 static varinfo_t
401 get_call_vi (gimple call)
403 void **slot_p;
404 varinfo_t vi, vi2;
406 slot_p = pointer_map_insert (call_stmt_vars, call);
407 if (*slot_p)
408 return (varinfo_t) *slot_p;
410 vi = new_var_info (NULL_TREE, "CALLUSED");
411 vi->offset = 0;
412 vi->size = 1;
413 vi->fullsize = 2;
414 vi->is_full_var = true;
416 vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED");
417 vi2->offset = 1;
418 vi2->size = 1;
419 vi2->fullsize = 2;
420 vi2->is_full_var = true;
422 vi->next = vi2->id;
424 *slot_p = (void *) vi;
425 return vi;
428 /* Lookup the variable for the call statement CALL representing
429 the uses. Returns NULL if there is nothing special about this call. */
431 static varinfo_t
432 lookup_call_use_vi (gimple call)
434 void **slot_p;
436 slot_p = pointer_map_contains (call_stmt_vars, call);
437 if (slot_p)
438 return (varinfo_t) *slot_p;
440 return NULL;
443 /* Lookup the variable for the call statement CALL representing
444 the clobbers. Returns NULL if there is nothing special about this call. */
446 static varinfo_t
447 lookup_call_clobber_vi (gimple call)
449 varinfo_t uses = lookup_call_use_vi (call);
450 if (!uses)
451 return NULL;
453 return vi_next (uses);
456 /* Lookup or create the variable for the call statement CALL representing
457 the uses. */
459 static varinfo_t
460 get_call_use_vi (gimple call)
462 return get_call_vi (call);
465 /* Lookup or create the variable for the call statement CALL representing
466 the clobbers. */
468 static varinfo_t ATTRIBUTE_UNUSED
469 get_call_clobber_vi (gimple call)
471 return vi_next (get_call_vi (call));
475 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
477 /* An expression that appears in a constraint. */
479 struct constraint_expr
481 /* Constraint type. */
482 constraint_expr_type type;
484 /* Variable we are referring to in the constraint. */
485 unsigned int var;
487 /* Offset, in bits, of this constraint from the beginning of
488 variables it ends up referring to.
490 IOW, in a deref constraint, we would deref, get the result set,
491 then add OFFSET to each member. */
492 HOST_WIDE_INT offset;
495 /* Use 0x8000... as special unknown offset. */
496 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
498 typedef struct constraint_expr ce_s;
499 static void get_constraint_for_1 (tree, vec<ce_s> *, bool, bool);
500 static void get_constraint_for (tree, vec<ce_s> *);
501 static void get_constraint_for_rhs (tree, vec<ce_s> *);
502 static void do_deref (vec<ce_s> *);
504 /* Our set constraints are made up of two constraint expressions, one
505 LHS, and one RHS.
507 As described in the introduction, our set constraints each represent an
508 operation between set valued variables.
510 struct constraint
512 struct constraint_expr lhs;
513 struct constraint_expr rhs;
516 /* List of constraints that we use to build the constraint graph from. */
518 static vec<constraint_t> constraints;
519 static alloc_pool constraint_pool;
521 /* The constraint graph is represented as an array of bitmaps
522 containing successor nodes. */
524 struct constraint_graph
526 /* Size of this graph, which may be different than the number of
527 nodes in the variable map. */
528 unsigned int size;
530 /* Explicit successors of each node. */
531 bitmap *succs;
533 /* Implicit predecessors of each node (Used for variable
534 substitution). */
535 bitmap *implicit_preds;
537 /* Explicit predecessors of each node (Used for variable substitution). */
538 bitmap *preds;
540 /* Indirect cycle representatives, or -1 if the node has no indirect
541 cycles. */
542 int *indirect_cycles;
544 /* Representative node for a node. rep[a] == a unless the node has
545 been unified. */
546 unsigned int *rep;
548 /* Equivalence class representative for a label. This is used for
549 variable substitution. */
550 int *eq_rep;
552 /* Pointer equivalence label for a node. All nodes with the same
553 pointer equivalence label can be unified together at some point
554 (either during constraint optimization or after the constraint
555 graph is built). */
556 unsigned int *pe;
558 /* Pointer equivalence representative for a label. This is used to
559 handle nodes that are pointer equivalent but not location
560 equivalent. We can unite these once the addressof constraints
561 are transformed into initial points-to sets. */
562 int *pe_rep;
564 /* Pointer equivalence label for each node, used during variable
565 substitution. */
566 unsigned int *pointer_label;
568 /* Location equivalence label for each node, used during location
569 equivalence finding. */
570 unsigned int *loc_label;
572 /* Pointed-by set for each node, used during location equivalence
573 finding. This is pointed-by rather than pointed-to, because it
574 is constructed using the predecessor graph. */
575 bitmap *pointed_by;
577 /* Points to sets for pointer equivalence. This is *not* the actual
578 points-to sets for nodes. */
579 bitmap *points_to;
581 /* Bitmap of nodes where the bit is set if the node is a direct
582 node. Used for variable substitution. */
583 sbitmap direct_nodes;
585 /* Bitmap of nodes where the bit is set if the node is address
586 taken. Used for variable substitution. */
587 bitmap address_taken;
589 /* Vector of complex constraints for each graph node. Complex
590 constraints are those involving dereferences or offsets that are
591 not 0. */
592 vec<constraint_t> *complex;
595 static constraint_graph_t graph;
597 /* During variable substitution and the offline version of indirect
598 cycle finding, we create nodes to represent dereferences and
599 address taken constraints. These represent where these start and
600 end. */
601 #define FIRST_REF_NODE (varmap).length ()
602 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
604 /* Return the representative node for NODE, if NODE has been unioned
605 with another NODE.
606 This function performs path compression along the way to finding
607 the representative. */
609 static unsigned int
610 find (unsigned int node)
612 gcc_checking_assert (node < graph->size);
613 if (graph->rep[node] != node)
614 return graph->rep[node] = find (graph->rep[node]);
615 return node;
618 /* Union the TO and FROM nodes to the TO nodes.
619 Note that at some point in the future, we may want to do
620 union-by-rank, in which case we are going to have to return the
621 node we unified to. */
623 static bool
624 unite (unsigned int to, unsigned int from)
626 gcc_checking_assert (to < graph->size && from < graph->size);
627 if (to != from && graph->rep[from] != to)
629 graph->rep[from] = to;
630 return true;
632 return false;
635 /* Create a new constraint consisting of LHS and RHS expressions. */
637 static constraint_t
638 new_constraint (const struct constraint_expr lhs,
639 const struct constraint_expr rhs)
641 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
642 ret->lhs = lhs;
643 ret->rhs = rhs;
644 return ret;
647 /* Print out constraint C to FILE. */
649 static void
650 dump_constraint (FILE *file, constraint_t c)
652 if (c->lhs.type == ADDRESSOF)
653 fprintf (file, "&");
654 else if (c->lhs.type == DEREF)
655 fprintf (file, "*");
656 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
657 if (c->lhs.offset == UNKNOWN_OFFSET)
658 fprintf (file, " + UNKNOWN");
659 else if (c->lhs.offset != 0)
660 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
661 fprintf (file, " = ");
662 if (c->rhs.type == ADDRESSOF)
663 fprintf (file, "&");
664 else if (c->rhs.type == DEREF)
665 fprintf (file, "*");
666 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
667 if (c->rhs.offset == UNKNOWN_OFFSET)
668 fprintf (file, " + UNKNOWN");
669 else if (c->rhs.offset != 0)
670 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
674 void debug_constraint (constraint_t);
675 void debug_constraints (void);
676 void debug_constraint_graph (void);
677 void debug_solution_for_var (unsigned int);
678 void debug_sa_points_to_info (void);
680 /* Print out constraint C to stderr. */
682 DEBUG_FUNCTION void
683 debug_constraint (constraint_t c)
685 dump_constraint (stderr, c);
686 fprintf (stderr, "\n");
689 /* Print out all constraints to FILE */
691 static void
692 dump_constraints (FILE *file, int from)
694 int i;
695 constraint_t c;
696 for (i = from; constraints.iterate (i, &c); i++)
697 if (c)
699 dump_constraint (file, c);
700 fprintf (file, "\n");
704 /* Print out all constraints to stderr. */
706 DEBUG_FUNCTION void
707 debug_constraints (void)
709 dump_constraints (stderr, 0);
712 /* Print the constraint graph in dot format. */
714 static void
715 dump_constraint_graph (FILE *file)
717 unsigned int i;
719 /* Only print the graph if it has already been initialized: */
720 if (!graph)
721 return;
723 /* Prints the header of the dot file: */
724 fprintf (file, "strict digraph {\n");
725 fprintf (file, " node [\n shape = box\n ]\n");
726 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
727 fprintf (file, "\n // List of nodes and complex constraints in "
728 "the constraint graph:\n");
730 /* The next lines print the nodes in the graph together with the
731 complex constraints attached to them. */
732 for (i = 1; i < graph->size; i++)
734 if (i == FIRST_REF_NODE)
735 continue;
736 if (find (i) != i)
737 continue;
738 if (i < FIRST_REF_NODE)
739 fprintf (file, "\"%s\"", get_varinfo (i)->name);
740 else
741 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
742 if (graph->complex[i].exists ())
744 unsigned j;
745 constraint_t c;
746 fprintf (file, " [label=\"\\N\\n");
747 for (j = 0; graph->complex[i].iterate (j, &c); ++j)
749 dump_constraint (file, c);
750 fprintf (file, "\\l");
752 fprintf (file, "\"]");
754 fprintf (file, ";\n");
757 /* Go over the edges. */
758 fprintf (file, "\n // Edges in the constraint graph:\n");
759 for (i = 1; i < graph->size; i++)
761 unsigned j;
762 bitmap_iterator bi;
763 if (find (i) != i)
764 continue;
765 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
767 unsigned to = find (j);
768 if (i == to)
769 continue;
770 if (i < FIRST_REF_NODE)
771 fprintf (file, "\"%s\"", get_varinfo (i)->name);
772 else
773 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
774 fprintf (file, " -> ");
775 if (to < FIRST_REF_NODE)
776 fprintf (file, "\"%s\"", get_varinfo (to)->name);
777 else
778 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name);
779 fprintf (file, ";\n");
783 /* Prints the tail of the dot file. */
784 fprintf (file, "}\n");
787 /* Print out the constraint graph to stderr. */
789 DEBUG_FUNCTION void
790 debug_constraint_graph (void)
792 dump_constraint_graph (stderr);
795 /* SOLVER FUNCTIONS
797 The solver is a simple worklist solver, that works on the following
798 algorithm:
800 sbitmap changed_nodes = all zeroes;
801 changed_count = 0;
802 For each node that is not already collapsed:
803 changed_count++;
804 set bit in changed nodes
806 while (changed_count > 0)
808 compute topological ordering for constraint graph
810 find and collapse cycles in the constraint graph (updating
811 changed if necessary)
813 for each node (n) in the graph in topological order:
814 changed_count--;
816 Process each complex constraint associated with the node,
817 updating changed if necessary.
819 For each outgoing edge from n, propagate the solution from n to
820 the destination of the edge, updating changed as necessary.
822 } */
824 /* Return true if two constraint expressions A and B are equal. */
826 static bool
827 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
829 return a.type == b.type && a.var == b.var && a.offset == b.offset;
832 /* Return true if constraint expression A is less than constraint expression
833 B. This is just arbitrary, but consistent, in order to give them an
834 ordering. */
836 static bool
837 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
839 if (a.type == b.type)
841 if (a.var == b.var)
842 return a.offset < b.offset;
843 else
844 return a.var < b.var;
846 else
847 return a.type < b.type;
850 /* Return true if constraint A is less than constraint B. This is just
851 arbitrary, but consistent, in order to give them an ordering. */
853 static bool
854 constraint_less (const constraint_t &a, const constraint_t &b)
856 if (constraint_expr_less (a->lhs, b->lhs))
857 return true;
858 else if (constraint_expr_less (b->lhs, a->lhs))
859 return false;
860 else
861 return constraint_expr_less (a->rhs, b->rhs);
864 /* Return true if two constraints A and B are equal. */
866 static bool
867 constraint_equal (struct constraint a, struct constraint b)
869 return constraint_expr_equal (a.lhs, b.lhs)
870 && constraint_expr_equal (a.rhs, b.rhs);
874 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
876 static constraint_t
877 constraint_vec_find (vec<constraint_t> vec,
878 struct constraint lookfor)
880 unsigned int place;
881 constraint_t found;
883 if (!vec.exists ())
884 return NULL;
886 place = vec.lower_bound (&lookfor, constraint_less);
887 if (place >= vec.length ())
888 return NULL;
889 found = vec[place];
890 if (!constraint_equal (*found, lookfor))
891 return NULL;
892 return found;
895 /* Union two constraint vectors, TO and FROM. Put the result in TO.
896 Returns true of TO set is changed. */
898 static bool
899 constraint_set_union (vec<constraint_t> *to,
900 vec<constraint_t> *from)
902 int i;
903 constraint_t c;
904 bool any_change = false;
906 FOR_EACH_VEC_ELT (*from, i, c)
908 if (constraint_vec_find (*to, *c) == NULL)
910 unsigned int place = to->lower_bound (c, constraint_less);
911 to->safe_insert (place, c);
912 any_change = true;
915 return any_change;
918 /* Expands the solution in SET to all sub-fields of variables included. */
920 static bitmap
921 solution_set_expand (bitmap set, bitmap *expanded)
923 bitmap_iterator bi;
924 unsigned j;
926 if (*expanded)
927 return *expanded;
929 *expanded = BITMAP_ALLOC (&iteration_obstack);
931 /* In a first pass expand to the head of the variables we need to
932 add all sub-fields off. This avoids quadratic behavior. */
933 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
935 varinfo_t v = get_varinfo (j);
936 if (v->is_artificial_var
937 || v->is_full_var)
938 continue;
939 bitmap_set_bit (*expanded, v->head);
942 /* In the second pass now expand all head variables with subfields. */
943 EXECUTE_IF_SET_IN_BITMAP (*expanded, 0, j, bi)
945 varinfo_t v = get_varinfo (j);
946 if (v->head != j)
947 continue;
948 for (v = vi_next (v); v != NULL; v = vi_next (v))
949 bitmap_set_bit (*expanded, v->id);
952 /* And finally set the rest of the bits from SET. */
953 bitmap_ior_into (*expanded, set);
955 return *expanded;
958 /* Union solution sets TO and DELTA, and add INC to each member of DELTA in the
959 process. */
961 static bool
962 set_union_with_increment (bitmap to, bitmap delta, HOST_WIDE_INT inc,
963 bitmap *expanded_delta)
965 bool changed = false;
966 bitmap_iterator bi;
967 unsigned int i;
969 /* If the solution of DELTA contains anything it is good enough to transfer
970 this to TO. */
971 if (bitmap_bit_p (delta, anything_id))
972 return bitmap_set_bit (to, anything_id);
974 /* If the offset is unknown we have to expand the solution to
975 all subfields. */
976 if (inc == UNKNOWN_OFFSET)
978 delta = solution_set_expand (delta, expanded_delta);
979 changed |= bitmap_ior_into (to, delta);
980 return changed;
983 /* For non-zero offset union the offsetted solution into the destination. */
984 EXECUTE_IF_SET_IN_BITMAP (delta, 0, i, bi)
986 varinfo_t vi = get_varinfo (i);
988 /* If this is a variable with just one field just set its bit
989 in the result. */
990 if (vi->is_artificial_var
991 || vi->is_unknown_size_var
992 || vi->is_full_var)
993 changed |= bitmap_set_bit (to, i);
994 else
996 HOST_WIDE_INT fieldoffset = vi->offset + inc;
997 unsigned HOST_WIDE_INT size = vi->size;
999 /* If the offset makes the pointer point to before the
1000 variable use offset zero for the field lookup. */
1001 if (fieldoffset < 0)
1002 vi = get_varinfo (vi->head);
1003 else
1004 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
1008 changed |= bitmap_set_bit (to, vi->id);
1009 if (vi->is_full_var
1010 || vi->next == 0)
1011 break;
1013 /* We have to include all fields that overlap the current field
1014 shifted by inc. */
1015 vi = vi_next (vi);
1017 while (vi->offset < fieldoffset + size);
1021 return changed;
1024 /* Insert constraint C into the list of complex constraints for graph
1025 node VAR. */
1027 static void
1028 insert_into_complex (constraint_graph_t graph,
1029 unsigned int var, constraint_t c)
1031 vec<constraint_t> complex = graph->complex[var];
1032 unsigned int place = complex.lower_bound (c, constraint_less);
1034 /* Only insert constraints that do not already exist. */
1035 if (place >= complex.length ()
1036 || !constraint_equal (*c, *complex[place]))
1037 graph->complex[var].safe_insert (place, c);
1041 /* Condense two variable nodes into a single variable node, by moving
1042 all associated info from FROM to TO. Returns true if TO node's
1043 constraint set changes after the merge. */
1045 static bool
1046 merge_node_constraints (constraint_graph_t graph, unsigned int to,
1047 unsigned int from)
1049 unsigned int i;
1050 constraint_t c;
1051 bool any_change = false;
1053 gcc_checking_assert (find (from) == to);
1055 /* Move all complex constraints from src node into to node */
1056 FOR_EACH_VEC_ELT (graph->complex[from], i, c)
1058 /* In complex constraints for node FROM, we may have either
1059 a = *FROM, and *FROM = a, or an offseted constraint which are
1060 always added to the rhs node's constraints. */
1062 if (c->rhs.type == DEREF)
1063 c->rhs.var = to;
1064 else if (c->lhs.type == DEREF)
1065 c->lhs.var = to;
1066 else
1067 c->rhs.var = to;
1070 any_change = constraint_set_union (&graph->complex[to],
1071 &graph->complex[from]);
1072 graph->complex[from].release ();
1073 return any_change;
1077 /* Remove edges involving NODE from GRAPH. */
1079 static void
1080 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
1082 if (graph->succs[node])
1083 BITMAP_FREE (graph->succs[node]);
1086 /* Merge GRAPH nodes FROM and TO into node TO. */
1088 static void
1089 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1090 unsigned int from)
1092 if (graph->indirect_cycles[from] != -1)
1094 /* If we have indirect cycles with the from node, and we have
1095 none on the to node, the to node has indirect cycles from the
1096 from node now that they are unified.
1097 If indirect cycles exist on both, unify the nodes that they
1098 are in a cycle with, since we know they are in a cycle with
1099 each other. */
1100 if (graph->indirect_cycles[to] == -1)
1101 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1104 /* Merge all the successor edges. */
1105 if (graph->succs[from])
1107 if (!graph->succs[to])
1108 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1109 bitmap_ior_into (graph->succs[to],
1110 graph->succs[from]);
1113 clear_edges_for_node (graph, from);
1117 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1118 it doesn't exist in the graph already. */
1120 static void
1121 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1122 unsigned int from)
1124 if (to == from)
1125 return;
1127 if (!graph->implicit_preds[to])
1128 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1130 if (bitmap_set_bit (graph->implicit_preds[to], from))
1131 stats.num_implicit_edges++;
1134 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1135 it doesn't exist in the graph already.
1136 Return false if the edge already existed, true otherwise. */
1138 static void
1139 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1140 unsigned int from)
1142 if (!graph->preds[to])
1143 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1144 bitmap_set_bit (graph->preds[to], from);
1147 /* Add a graph edge to GRAPH, going from FROM to TO if
1148 it doesn't exist in the graph already.
1149 Return false if the edge already existed, true otherwise. */
1151 static bool
1152 add_graph_edge (constraint_graph_t graph, unsigned int to,
1153 unsigned int from)
1155 if (to == from)
1157 return false;
1159 else
1161 bool r = false;
1163 if (!graph->succs[from])
1164 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1165 if (bitmap_set_bit (graph->succs[from], to))
1167 r = true;
1168 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1169 stats.num_edges++;
1171 return r;
1176 /* Initialize the constraint graph structure to contain SIZE nodes. */
1178 static void
1179 init_graph (unsigned int size)
1181 unsigned int j;
1183 graph = XCNEW (struct constraint_graph);
1184 graph->size = size;
1185 graph->succs = XCNEWVEC (bitmap, graph->size);
1186 graph->indirect_cycles = XNEWVEC (int, graph->size);
1187 graph->rep = XNEWVEC (unsigned int, graph->size);
1188 /* ??? Macros do not support template types with multiple arguments,
1189 so we use a typedef to work around it. */
1190 typedef vec<constraint_t> vec_constraint_t_heap;
1191 graph->complex = XCNEWVEC (vec_constraint_t_heap, size);
1192 graph->pe = XCNEWVEC (unsigned int, graph->size);
1193 graph->pe_rep = XNEWVEC (int, graph->size);
1195 for (j = 0; j < graph->size; j++)
1197 graph->rep[j] = j;
1198 graph->pe_rep[j] = -1;
1199 graph->indirect_cycles[j] = -1;
1203 /* Build the constraint graph, adding only predecessor edges right now. */
1205 static void
1206 build_pred_graph (void)
1208 int i;
1209 constraint_t c;
1210 unsigned int j;
1212 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1213 graph->preds = XCNEWVEC (bitmap, graph->size);
1214 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1215 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1216 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1217 graph->points_to = XCNEWVEC (bitmap, graph->size);
1218 graph->eq_rep = XNEWVEC (int, graph->size);
1219 graph->direct_nodes = sbitmap_alloc (graph->size);
1220 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1221 bitmap_clear (graph->direct_nodes);
1223 for (j = 1; j < FIRST_REF_NODE; j++)
1225 if (!get_varinfo (j)->is_special_var)
1226 bitmap_set_bit (graph->direct_nodes, j);
1229 for (j = 0; j < graph->size; j++)
1230 graph->eq_rep[j] = -1;
1232 for (j = 0; j < varmap.length (); j++)
1233 graph->indirect_cycles[j] = -1;
1235 FOR_EACH_VEC_ELT (constraints, i, c)
1237 struct constraint_expr lhs = c->lhs;
1238 struct constraint_expr rhs = c->rhs;
1239 unsigned int lhsvar = lhs.var;
1240 unsigned int rhsvar = rhs.var;
1242 if (lhs.type == DEREF)
1244 /* *x = y. */
1245 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1246 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1248 else if (rhs.type == DEREF)
1250 /* x = *y */
1251 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1252 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1253 else
1254 bitmap_clear_bit (graph->direct_nodes, lhsvar);
1256 else if (rhs.type == ADDRESSOF)
1258 varinfo_t v;
1260 /* x = &y */
1261 if (graph->points_to[lhsvar] == NULL)
1262 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1263 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1265 if (graph->pointed_by[rhsvar] == NULL)
1266 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1267 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1269 /* Implicitly, *x = y */
1270 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1272 /* All related variables are no longer direct nodes. */
1273 bitmap_clear_bit (graph->direct_nodes, rhsvar);
1274 v = get_varinfo (rhsvar);
1275 if (!v->is_full_var)
1277 v = get_varinfo (v->head);
1280 bitmap_clear_bit (graph->direct_nodes, v->id);
1281 v = vi_next (v);
1283 while (v != NULL);
1285 bitmap_set_bit (graph->address_taken, rhsvar);
1287 else if (lhsvar > anything_id
1288 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1290 /* x = y */
1291 add_pred_graph_edge (graph, lhsvar, rhsvar);
1292 /* Implicitly, *x = *y */
1293 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1294 FIRST_REF_NODE + rhsvar);
1296 else if (lhs.offset != 0 || rhs.offset != 0)
1298 if (rhs.offset != 0)
1299 bitmap_clear_bit (graph->direct_nodes, lhs.var);
1300 else if (lhs.offset != 0)
1301 bitmap_clear_bit (graph->direct_nodes, rhs.var);
1306 /* Build the constraint graph, adding successor edges. */
1308 static void
1309 build_succ_graph (void)
1311 unsigned i, t;
1312 constraint_t c;
1314 FOR_EACH_VEC_ELT (constraints, i, c)
1316 struct constraint_expr lhs;
1317 struct constraint_expr rhs;
1318 unsigned int lhsvar;
1319 unsigned int rhsvar;
1321 if (!c)
1322 continue;
1324 lhs = c->lhs;
1325 rhs = c->rhs;
1326 lhsvar = find (lhs.var);
1327 rhsvar = find (rhs.var);
1329 if (lhs.type == DEREF)
1331 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1332 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1334 else if (rhs.type == DEREF)
1336 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1337 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1339 else if (rhs.type == ADDRESSOF)
1341 /* x = &y */
1342 gcc_checking_assert (find (rhs.var) == rhs.var);
1343 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1345 else if (lhsvar > anything_id
1346 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1348 add_graph_edge (graph, lhsvar, rhsvar);
1352 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1353 receive pointers. */
1354 t = find (storedanything_id);
1355 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1357 if (!bitmap_bit_p (graph->direct_nodes, i)
1358 && get_varinfo (i)->may_have_pointers)
1359 add_graph_edge (graph, find (i), t);
1362 /* Everything stored to ANYTHING also potentially escapes. */
1363 add_graph_edge (graph, find (escaped_id), t);
1367 /* Changed variables on the last iteration. */
1368 static bitmap changed;
1370 /* Strongly Connected Component visitation info. */
1372 struct scc_info
1374 sbitmap visited;
1375 sbitmap deleted;
1376 unsigned int *dfs;
1377 unsigned int *node_mapping;
1378 int current_index;
1379 vec<unsigned> scc_stack;
1383 /* Recursive routine to find strongly connected components in GRAPH.
1384 SI is the SCC info to store the information in, and N is the id of current
1385 graph node we are processing.
1387 This is Tarjan's strongly connected component finding algorithm, as
1388 modified by Nuutila to keep only non-root nodes on the stack.
1389 The algorithm can be found in "On finding the strongly connected
1390 connected components in a directed graph" by Esko Nuutila and Eljas
1391 Soisalon-Soininen, in Information Processing Letters volume 49,
1392 number 1, pages 9-14. */
1394 static void
1395 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1397 unsigned int i;
1398 bitmap_iterator bi;
1399 unsigned int my_dfs;
1401 bitmap_set_bit (si->visited, n);
1402 si->dfs[n] = si->current_index ++;
1403 my_dfs = si->dfs[n];
1405 /* Visit all the successors. */
1406 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1408 unsigned int w;
1410 if (i > LAST_REF_NODE)
1411 break;
1413 w = find (i);
1414 if (bitmap_bit_p (si->deleted, w))
1415 continue;
1417 if (!bitmap_bit_p (si->visited, w))
1418 scc_visit (graph, si, w);
1420 unsigned int t = find (w);
1421 gcc_checking_assert (find (n) == n);
1422 if (si->dfs[t] < si->dfs[n])
1423 si->dfs[n] = si->dfs[t];
1426 /* See if any components have been identified. */
1427 if (si->dfs[n] == my_dfs)
1429 if (si->scc_stack.length () > 0
1430 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1432 bitmap scc = BITMAP_ALLOC (NULL);
1433 unsigned int lowest_node;
1434 bitmap_iterator bi;
1436 bitmap_set_bit (scc, n);
1438 while (si->scc_stack.length () != 0
1439 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1441 unsigned int w = si->scc_stack.pop ();
1443 bitmap_set_bit (scc, w);
1446 lowest_node = bitmap_first_set_bit (scc);
1447 gcc_assert (lowest_node < FIRST_REF_NODE);
1449 /* Collapse the SCC nodes into a single node, and mark the
1450 indirect cycles. */
1451 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1453 if (i < FIRST_REF_NODE)
1455 if (unite (lowest_node, i))
1456 unify_nodes (graph, lowest_node, i, false);
1458 else
1460 unite (lowest_node, i);
1461 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1465 bitmap_set_bit (si->deleted, n);
1467 else
1468 si->scc_stack.safe_push (n);
1471 /* Unify node FROM into node TO, updating the changed count if
1472 necessary when UPDATE_CHANGED is true. */
1474 static void
1475 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1476 bool update_changed)
1478 gcc_checking_assert (to != from && find (to) == to);
1480 if (dump_file && (dump_flags & TDF_DETAILS))
1481 fprintf (dump_file, "Unifying %s to %s\n",
1482 get_varinfo (from)->name,
1483 get_varinfo (to)->name);
1485 if (update_changed)
1486 stats.unified_vars_dynamic++;
1487 else
1488 stats.unified_vars_static++;
1490 merge_graph_nodes (graph, to, from);
1491 if (merge_node_constraints (graph, to, from))
1493 if (update_changed)
1494 bitmap_set_bit (changed, to);
1497 /* Mark TO as changed if FROM was changed. If TO was already marked
1498 as changed, decrease the changed count. */
1500 if (update_changed
1501 && bitmap_clear_bit (changed, from))
1502 bitmap_set_bit (changed, to);
1503 varinfo_t fromvi = get_varinfo (from);
1504 if (fromvi->solution)
1506 /* If the solution changes because of the merging, we need to mark
1507 the variable as changed. */
1508 varinfo_t tovi = get_varinfo (to);
1509 if (bitmap_ior_into (tovi->solution, fromvi->solution))
1511 if (update_changed)
1512 bitmap_set_bit (changed, to);
1515 BITMAP_FREE (fromvi->solution);
1516 if (fromvi->oldsolution)
1517 BITMAP_FREE (fromvi->oldsolution);
1519 if (stats.iterations > 0
1520 && tovi->oldsolution)
1521 BITMAP_FREE (tovi->oldsolution);
1523 if (graph->succs[to])
1524 bitmap_clear_bit (graph->succs[to], to);
1527 /* Information needed to compute the topological ordering of a graph. */
1529 struct topo_info
1531 /* sbitmap of visited nodes. */
1532 sbitmap visited;
1533 /* Array that stores the topological order of the graph, *in
1534 reverse*. */
1535 vec<unsigned> topo_order;
1539 /* Initialize and return a topological info structure. */
1541 static struct topo_info *
1542 init_topo_info (void)
1544 size_t size = graph->size;
1545 struct topo_info *ti = XNEW (struct topo_info);
1546 ti->visited = sbitmap_alloc (size);
1547 bitmap_clear (ti->visited);
1548 ti->topo_order.create (1);
1549 return ti;
1553 /* Free the topological sort info pointed to by TI. */
1555 static void
1556 free_topo_info (struct topo_info *ti)
1558 sbitmap_free (ti->visited);
1559 ti->topo_order.release ();
1560 free (ti);
1563 /* Visit the graph in topological order, and store the order in the
1564 topo_info structure. */
1566 static void
1567 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1568 unsigned int n)
1570 bitmap_iterator bi;
1571 unsigned int j;
1573 bitmap_set_bit (ti->visited, n);
1575 if (graph->succs[n])
1576 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1578 if (!bitmap_bit_p (ti->visited, j))
1579 topo_visit (graph, ti, j);
1582 ti->topo_order.safe_push (n);
1585 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1586 starting solution for y. */
1588 static void
1589 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1590 bitmap delta, bitmap *expanded_delta)
1592 unsigned int lhs = c->lhs.var;
1593 bool flag = false;
1594 bitmap sol = get_varinfo (lhs)->solution;
1595 unsigned int j;
1596 bitmap_iterator bi;
1597 HOST_WIDE_INT roffset = c->rhs.offset;
1599 /* Our IL does not allow this. */
1600 gcc_checking_assert (c->lhs.offset == 0);
1602 /* If the solution of Y contains anything it is good enough to transfer
1603 this to the LHS. */
1604 if (bitmap_bit_p (delta, anything_id))
1606 flag |= bitmap_set_bit (sol, anything_id);
1607 goto done;
1610 /* If we do not know at with offset the rhs is dereferenced compute
1611 the reachability set of DELTA, conservatively assuming it is
1612 dereferenced at all valid offsets. */
1613 if (roffset == UNKNOWN_OFFSET)
1615 delta = solution_set_expand (delta, expanded_delta);
1616 /* No further offset processing is necessary. */
1617 roffset = 0;
1620 /* For each variable j in delta (Sol(y)), add
1621 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1622 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1624 varinfo_t v = get_varinfo (j);
1625 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1626 unsigned HOST_WIDE_INT size = v->size;
1627 unsigned int t;
1629 if (v->is_full_var)
1631 else if (roffset != 0)
1633 if (fieldoffset < 0)
1634 v = get_varinfo (v->head);
1635 else
1636 v = first_or_preceding_vi_for_offset (v, fieldoffset);
1639 /* We have to include all fields that overlap the current field
1640 shifted by roffset. */
1643 t = find (v->id);
1645 /* Adding edges from the special vars is pointless.
1646 They don't have sets that can change. */
1647 if (get_varinfo (t)->is_special_var)
1648 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1649 /* Merging the solution from ESCAPED needlessly increases
1650 the set. Use ESCAPED as representative instead. */
1651 else if (v->id == escaped_id)
1652 flag |= bitmap_set_bit (sol, escaped_id);
1653 else if (v->may_have_pointers
1654 && add_graph_edge (graph, lhs, t))
1655 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1657 if (v->is_full_var
1658 || v->next == 0)
1659 break;
1661 v = vi_next (v);
1663 while (v->offset < fieldoffset + size);
1666 done:
1667 /* If the LHS solution changed, mark the var as changed. */
1668 if (flag)
1670 get_varinfo (lhs)->solution = sol;
1671 bitmap_set_bit (changed, lhs);
1675 /* Process a constraint C that represents *(x + off) = y using DELTA
1676 as the starting solution for x. */
1678 static void
1679 do_ds_constraint (constraint_t c, bitmap delta, bitmap *expanded_delta)
1681 unsigned int rhs = c->rhs.var;
1682 bitmap sol = get_varinfo (rhs)->solution;
1683 unsigned int j;
1684 bitmap_iterator bi;
1685 HOST_WIDE_INT loff = c->lhs.offset;
1686 bool escaped_p = false;
1688 /* Our IL does not allow this. */
1689 gcc_checking_assert (c->rhs.offset == 0);
1691 /* If the solution of y contains ANYTHING simply use the ANYTHING
1692 solution. This avoids needlessly increasing the points-to sets. */
1693 if (bitmap_bit_p (sol, anything_id))
1694 sol = get_varinfo (find (anything_id))->solution;
1696 /* If the solution for x contains ANYTHING we have to merge the
1697 solution of y into all pointer variables which we do via
1698 STOREDANYTHING. */
1699 if (bitmap_bit_p (delta, anything_id))
1701 unsigned t = find (storedanything_id);
1702 if (add_graph_edge (graph, t, rhs))
1704 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1705 bitmap_set_bit (changed, t);
1707 return;
1710 /* If we do not know at with offset the rhs is dereferenced compute
1711 the reachability set of DELTA, conservatively assuming it is
1712 dereferenced at all valid offsets. */
1713 if (loff == UNKNOWN_OFFSET)
1715 delta = solution_set_expand (delta, expanded_delta);
1716 loff = 0;
1719 /* For each member j of delta (Sol(x)), add an edge from y to j and
1720 union Sol(y) into Sol(j) */
1721 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1723 varinfo_t v = get_varinfo (j);
1724 unsigned int t;
1725 HOST_WIDE_INT fieldoffset = v->offset + loff;
1726 unsigned HOST_WIDE_INT size = v->size;
1728 if (v->is_full_var)
1730 else if (loff != 0)
1732 if (fieldoffset < 0)
1733 v = get_varinfo (v->head);
1734 else
1735 v = first_or_preceding_vi_for_offset (v, fieldoffset);
1738 /* We have to include all fields that overlap the current field
1739 shifted by loff. */
1742 if (v->may_have_pointers)
1744 /* If v is a global variable then this is an escape point. */
1745 if (v->is_global_var
1746 && !escaped_p)
1748 t = find (escaped_id);
1749 if (add_graph_edge (graph, t, rhs)
1750 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1751 bitmap_set_bit (changed, t);
1752 /* Enough to let rhs escape once. */
1753 escaped_p = true;
1756 if (v->is_special_var)
1757 break;
1759 t = find (v->id);
1760 if (add_graph_edge (graph, t, rhs)
1761 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1762 bitmap_set_bit (changed, t);
1765 if (v->is_full_var
1766 || v->next == 0)
1767 break;
1769 v = vi_next (v);
1771 while (v->offset < fieldoffset + size);
1775 /* Handle a non-simple (simple meaning requires no iteration),
1776 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1778 static void
1779 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta,
1780 bitmap *expanded_delta)
1782 if (c->lhs.type == DEREF)
1784 if (c->rhs.type == ADDRESSOF)
1786 gcc_unreachable ();
1788 else
1790 /* *x = y */
1791 do_ds_constraint (c, delta, expanded_delta);
1794 else if (c->rhs.type == DEREF)
1796 /* x = *y */
1797 if (!(get_varinfo (c->lhs.var)->is_special_var))
1798 do_sd_constraint (graph, c, delta, expanded_delta);
1800 else
1802 bitmap tmp;
1803 bool flag = false;
1805 gcc_checking_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR
1806 && c->rhs.offset != 0 && c->lhs.offset == 0);
1807 tmp = get_varinfo (c->lhs.var)->solution;
1809 flag = set_union_with_increment (tmp, delta, c->rhs.offset,
1810 expanded_delta);
1812 if (flag)
1813 bitmap_set_bit (changed, c->lhs.var);
1817 /* Initialize and return a new SCC info structure. */
1819 static struct scc_info *
1820 init_scc_info (size_t size)
1822 struct scc_info *si = XNEW (struct scc_info);
1823 size_t i;
1825 si->current_index = 0;
1826 si->visited = sbitmap_alloc (size);
1827 bitmap_clear (si->visited);
1828 si->deleted = sbitmap_alloc (size);
1829 bitmap_clear (si->deleted);
1830 si->node_mapping = XNEWVEC (unsigned int, size);
1831 si->dfs = XCNEWVEC (unsigned int, size);
1833 for (i = 0; i < size; i++)
1834 si->node_mapping[i] = i;
1836 si->scc_stack.create (1);
1837 return si;
1840 /* Free an SCC info structure pointed to by SI */
1842 static void
1843 free_scc_info (struct scc_info *si)
1845 sbitmap_free (si->visited);
1846 sbitmap_free (si->deleted);
1847 free (si->node_mapping);
1848 free (si->dfs);
1849 si->scc_stack.release ();
1850 free (si);
1854 /* Find indirect cycles in GRAPH that occur, using strongly connected
1855 components, and note them in the indirect cycles map.
1857 This technique comes from Ben Hardekopf and Calvin Lin,
1858 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1859 Lines of Code", submitted to PLDI 2007. */
1861 static void
1862 find_indirect_cycles (constraint_graph_t graph)
1864 unsigned int i;
1865 unsigned int size = graph->size;
1866 struct scc_info *si = init_scc_info (size);
1868 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1869 if (!bitmap_bit_p (si->visited, i) && find (i) == i)
1870 scc_visit (graph, si, i);
1872 free_scc_info (si);
1875 /* Compute a topological ordering for GRAPH, and store the result in the
1876 topo_info structure TI. */
1878 static void
1879 compute_topo_order (constraint_graph_t graph,
1880 struct topo_info *ti)
1882 unsigned int i;
1883 unsigned int size = graph->size;
1885 for (i = 0; i != size; ++i)
1886 if (!bitmap_bit_p (ti->visited, i) && find (i) == i)
1887 topo_visit (graph, ti, i);
1890 /* Structure used to for hash value numbering of pointer equivalence
1891 classes. */
1893 typedef struct equiv_class_label
1895 hashval_t hashcode;
1896 unsigned int equivalence_class;
1897 bitmap labels;
1898 } *equiv_class_label_t;
1899 typedef const struct equiv_class_label *const_equiv_class_label_t;
1901 /* Equiv_class_label hashtable helpers. */
1903 struct equiv_class_hasher : typed_free_remove <equiv_class_label>
1905 typedef equiv_class_label value_type;
1906 typedef equiv_class_label compare_type;
1907 static inline hashval_t hash (const value_type *);
1908 static inline bool equal (const value_type *, const compare_type *);
1911 /* Hash function for a equiv_class_label_t */
1913 inline hashval_t
1914 equiv_class_hasher::hash (const value_type *ecl)
1916 return ecl->hashcode;
1919 /* Equality function for two equiv_class_label_t's. */
1921 inline bool
1922 equiv_class_hasher::equal (const value_type *eql1, const compare_type *eql2)
1924 return (eql1->hashcode == eql2->hashcode
1925 && bitmap_equal_p (eql1->labels, eql2->labels));
1928 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1929 classes. */
1930 static hash_table <equiv_class_hasher> pointer_equiv_class_table;
1932 /* A hashtable for mapping a bitmap of labels->location equivalence
1933 classes. */
1934 static hash_table <equiv_class_hasher> location_equiv_class_table;
1936 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1937 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1938 is equivalent to. */
1940 static equiv_class_label *
1941 equiv_class_lookup_or_add (hash_table <equiv_class_hasher> table, bitmap labels)
1943 equiv_class_label **slot;
1944 equiv_class_label ecl;
1946 ecl.labels = labels;
1947 ecl.hashcode = bitmap_hash (labels);
1948 slot = table.find_slot_with_hash (&ecl, ecl.hashcode, INSERT);
1949 if (!*slot)
1951 *slot = XNEW (struct equiv_class_label);
1952 (*slot)->labels = labels;
1953 (*slot)->hashcode = ecl.hashcode;
1954 (*slot)->equivalence_class = 0;
1957 return *slot;
1960 /* Perform offline variable substitution.
1962 This is a worst case quadratic time way of identifying variables
1963 that must have equivalent points-to sets, including those caused by
1964 static cycles, and single entry subgraphs, in the constraint graph.
1966 The technique is described in "Exploiting Pointer and Location
1967 Equivalence to Optimize Pointer Analysis. In the 14th International
1968 Static Analysis Symposium (SAS), August 2007." It is known as the
1969 "HU" algorithm, and is equivalent to value numbering the collapsed
1970 constraint graph including evaluating unions.
1972 The general method of finding equivalence classes is as follows:
1973 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1974 Initialize all non-REF nodes to be direct nodes.
1975 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1976 variable}
1977 For each constraint containing the dereference, we also do the same
1978 thing.
1980 We then compute SCC's in the graph and unify nodes in the same SCC,
1981 including pts sets.
1983 For each non-collapsed node x:
1984 Visit all unvisited explicit incoming edges.
1985 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1986 where y->x.
1987 Lookup the equivalence class for pts(x).
1988 If we found one, equivalence_class(x) = found class.
1989 Otherwise, equivalence_class(x) = new class, and new_class is
1990 added to the lookup table.
1992 All direct nodes with the same equivalence class can be replaced
1993 with a single representative node.
1994 All unlabeled nodes (label == 0) are not pointers and all edges
1995 involving them can be eliminated.
1996 We perform these optimizations during rewrite_constraints
1998 In addition to pointer equivalence class finding, we also perform
1999 location equivalence class finding. This is the set of variables
2000 that always appear together in points-to sets. We use this to
2001 compress the size of the points-to sets. */
2003 /* Current maximum pointer equivalence class id. */
2004 static int pointer_equiv_class;
2006 /* Current maximum location equivalence class id. */
2007 static int location_equiv_class;
2009 /* Recursive routine to find strongly connected components in GRAPH,
2010 and label it's nodes with DFS numbers. */
2012 static void
2013 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2015 unsigned int i;
2016 bitmap_iterator bi;
2017 unsigned int my_dfs;
2019 gcc_checking_assert (si->node_mapping[n] == n);
2020 bitmap_set_bit (si->visited, n);
2021 si->dfs[n] = si->current_index ++;
2022 my_dfs = si->dfs[n];
2024 /* Visit all the successors. */
2025 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2027 unsigned int w = si->node_mapping[i];
2029 if (bitmap_bit_p (si->deleted, w))
2030 continue;
2032 if (!bitmap_bit_p (si->visited, w))
2033 condense_visit (graph, si, w);
2035 unsigned int t = si->node_mapping[w];
2036 gcc_checking_assert (si->node_mapping[n] == n);
2037 if (si->dfs[t] < si->dfs[n])
2038 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 gcc_assert (si->node_mapping[n] == n);
2054 if (si->dfs[t] < si->dfs[n])
2055 si->dfs[n] = si->dfs[t];
2058 /* See if any components have been identified. */
2059 if (si->dfs[n] == my_dfs)
2061 while (si->scc_stack.length () != 0
2062 && si->dfs[si->scc_stack.last ()] >= my_dfs)
2064 unsigned int w = si->scc_stack.pop ();
2065 si->node_mapping[w] = n;
2067 if (!bitmap_bit_p (graph->direct_nodes, w))
2068 bitmap_clear_bit (graph->direct_nodes, n);
2070 /* Unify our nodes. */
2071 if (graph->preds[w])
2073 if (!graph->preds[n])
2074 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2075 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2077 if (graph->implicit_preds[w])
2079 if (!graph->implicit_preds[n])
2080 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2081 bitmap_ior_into (graph->implicit_preds[n],
2082 graph->implicit_preds[w]);
2084 if (graph->points_to[w])
2086 if (!graph->points_to[n])
2087 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2088 bitmap_ior_into (graph->points_to[n],
2089 graph->points_to[w]);
2092 bitmap_set_bit (si->deleted, n);
2094 else
2095 si->scc_stack.safe_push (n);
2098 /* Label pointer equivalences.
2100 This performs a value numbering of the constraint graph to
2101 discover which variables will always have the same points-to sets
2102 under the current set of constraints.
2104 The way it value numbers is to store the set of points-to bits
2105 generated by the constraints and graph edges. This is just used as a
2106 hash and equality comparison. The *actual set of points-to bits* is
2107 completely irrelevant, in that we don't care about being able to
2108 extract them later.
2110 The equality values (currently bitmaps) just have to satisfy a few
2111 constraints, the main ones being:
2112 1. The combining operation must be order independent.
2113 2. The end result of a given set of operations must be unique iff the
2114 combination of input values is unique
2115 3. Hashable. */
2117 static void
2118 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2120 unsigned int i, first_pred;
2121 bitmap_iterator bi;
2123 bitmap_set_bit (si->visited, n);
2125 /* Label and union our incoming edges's points to sets. */
2126 first_pred = -1U;
2127 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2129 unsigned int w = si->node_mapping[i];
2130 if (!bitmap_bit_p (si->visited, w))
2131 label_visit (graph, si, w);
2133 /* Skip unused edges */
2134 if (w == n || graph->pointer_label[w] == 0)
2135 continue;
2137 if (graph->points_to[w])
2139 if (!graph->points_to[n])
2141 if (first_pred == -1U)
2142 first_pred = w;
2143 else
2145 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2146 bitmap_ior (graph->points_to[n],
2147 graph->points_to[first_pred],
2148 graph->points_to[w]);
2151 else
2152 bitmap_ior_into (graph->points_to[n], graph->points_to[w]);
2156 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2157 if (!bitmap_bit_p (graph->direct_nodes, n))
2159 if (!graph->points_to[n])
2161 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2162 if (first_pred != -1U)
2163 bitmap_copy (graph->points_to[n], graph->points_to[first_pred]);
2165 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2166 graph->pointer_label[n] = pointer_equiv_class++;
2167 equiv_class_label_t ecl;
2168 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2169 graph->points_to[n]);
2170 ecl->equivalence_class = graph->pointer_label[n];
2171 return;
2174 /* If there was only a single non-empty predecessor the pointer equiv
2175 class is the same. */
2176 if (!graph->points_to[n])
2178 if (first_pred != -1U)
2180 graph->pointer_label[n] = graph->pointer_label[first_pred];
2181 graph->points_to[n] = graph->points_to[first_pred];
2183 return;
2186 if (!bitmap_empty_p (graph->points_to[n]))
2188 equiv_class_label_t ecl;
2189 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2190 graph->points_to[n]);
2191 if (ecl->equivalence_class == 0)
2192 ecl->equivalence_class = pointer_equiv_class++;
2193 else
2195 BITMAP_FREE (graph->points_to[n]);
2196 graph->points_to[n] = ecl->labels;
2198 graph->pointer_label[n] = ecl->equivalence_class;
2202 /* Print the pred graph in dot format. */
2204 static void
2205 dump_pred_graph (struct scc_info *si, FILE *file)
2207 unsigned int i;
2209 /* Only print the graph if it has already been initialized: */
2210 if (!graph)
2211 return;
2213 /* Prints the header of the dot file: */
2214 fprintf (file, "strict digraph {\n");
2215 fprintf (file, " node [\n shape = box\n ]\n");
2216 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
2217 fprintf (file, "\n // List of nodes and complex constraints in "
2218 "the constraint graph:\n");
2220 /* The next lines print the nodes in the graph together with the
2221 complex constraints attached to them. */
2222 for (i = 1; i < graph->size; i++)
2224 if (i == FIRST_REF_NODE)
2225 continue;
2226 if (si->node_mapping[i] != i)
2227 continue;
2228 if (i < FIRST_REF_NODE)
2229 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2230 else
2231 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2232 if (graph->points_to[i]
2233 && !bitmap_empty_p (graph->points_to[i]))
2235 fprintf (file, "[label=\"%s = {", get_varinfo (i)->name);
2236 unsigned j;
2237 bitmap_iterator bi;
2238 EXECUTE_IF_SET_IN_BITMAP (graph->points_to[i], 0, j, bi)
2239 fprintf (file, " %d", j);
2240 fprintf (file, " }\"]");
2242 fprintf (file, ";\n");
2245 /* Go over the edges. */
2246 fprintf (file, "\n // Edges in the constraint graph:\n");
2247 for (i = 1; i < graph->size; i++)
2249 unsigned j;
2250 bitmap_iterator bi;
2251 if (si->node_mapping[i] != i)
2252 continue;
2253 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[i], 0, j, bi)
2255 unsigned from = si->node_mapping[j];
2256 if (from < FIRST_REF_NODE)
2257 fprintf (file, "\"%s\"", get_varinfo (from)->name);
2258 else
2259 fprintf (file, "\"*%s\"", get_varinfo (from - FIRST_REF_NODE)->name);
2260 fprintf (file, " -> ");
2261 if (i < FIRST_REF_NODE)
2262 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2263 else
2264 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2265 fprintf (file, ";\n");
2269 /* Prints the tail of the dot file. */
2270 fprintf (file, "}\n");
2273 /* Perform offline variable substitution, discovering equivalence
2274 classes, and eliminating non-pointer variables. */
2276 static struct scc_info *
2277 perform_var_substitution (constraint_graph_t graph)
2279 unsigned int i;
2280 unsigned int size = graph->size;
2281 struct scc_info *si = init_scc_info (size);
2283 bitmap_obstack_initialize (&iteration_obstack);
2284 pointer_equiv_class_table.create (511);
2285 location_equiv_class_table.create (511);
2286 pointer_equiv_class = 1;
2287 location_equiv_class = 1;
2289 /* Condense the nodes, which means to find SCC's, count incoming
2290 predecessors, and unite nodes in SCC's. */
2291 for (i = 1; i < FIRST_REF_NODE; i++)
2292 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2293 condense_visit (graph, si, si->node_mapping[i]);
2295 if (dump_file && (dump_flags & TDF_GRAPH))
2297 fprintf (dump_file, "\n\n// The constraint graph before var-substitution "
2298 "in dot format:\n");
2299 dump_pred_graph (si, dump_file);
2300 fprintf (dump_file, "\n\n");
2303 bitmap_clear (si->visited);
2304 /* Actually the label the nodes for pointer equivalences */
2305 for (i = 1; i < FIRST_REF_NODE; i++)
2306 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2307 label_visit (graph, si, si->node_mapping[i]);
2309 /* Calculate location equivalence labels. */
2310 for (i = 1; i < FIRST_REF_NODE; i++)
2312 bitmap pointed_by;
2313 bitmap_iterator bi;
2314 unsigned int j;
2316 if (!graph->pointed_by[i])
2317 continue;
2318 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2320 /* Translate the pointed-by mapping for pointer equivalence
2321 labels. */
2322 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2324 bitmap_set_bit (pointed_by,
2325 graph->pointer_label[si->node_mapping[j]]);
2327 /* The original pointed_by is now dead. */
2328 BITMAP_FREE (graph->pointed_by[i]);
2330 /* Look up the location equivalence label if one exists, or make
2331 one otherwise. */
2332 equiv_class_label_t ecl;
2333 ecl = equiv_class_lookup_or_add (location_equiv_class_table, pointed_by);
2334 if (ecl->equivalence_class == 0)
2335 ecl->equivalence_class = location_equiv_class++;
2336 else
2338 if (dump_file && (dump_flags & TDF_DETAILS))
2339 fprintf (dump_file, "Found location equivalence for node %s\n",
2340 get_varinfo (i)->name);
2341 BITMAP_FREE (pointed_by);
2343 graph->loc_label[i] = ecl->equivalence_class;
2347 if (dump_file && (dump_flags & TDF_DETAILS))
2348 for (i = 1; i < FIRST_REF_NODE; i++)
2350 unsigned j = si->node_mapping[i];
2351 if (j != i)
2353 fprintf (dump_file, "%s node id %d ",
2354 bitmap_bit_p (graph->direct_nodes, i)
2355 ? "Direct" : "Indirect", i);
2356 if (i < FIRST_REF_NODE)
2357 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2358 else
2359 fprintf (dump_file, "\"*%s\"",
2360 get_varinfo (i - FIRST_REF_NODE)->name);
2361 fprintf (dump_file, " mapped to SCC leader node id %d ", j);
2362 if (j < FIRST_REF_NODE)
2363 fprintf (dump_file, "\"%s\"\n", get_varinfo (j)->name);
2364 else
2365 fprintf (dump_file, "\"*%s\"\n",
2366 get_varinfo (j - FIRST_REF_NODE)->name);
2368 else
2370 fprintf (dump_file,
2371 "Equivalence classes for %s node id %d ",
2372 bitmap_bit_p (graph->direct_nodes, i)
2373 ? "direct" : "indirect", i);
2374 if (i < FIRST_REF_NODE)
2375 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2376 else
2377 fprintf (dump_file, "\"*%s\"",
2378 get_varinfo (i - FIRST_REF_NODE)->name);
2379 fprintf (dump_file,
2380 ": pointer %d, location %d\n",
2381 graph->pointer_label[i], graph->loc_label[i]);
2385 /* Quickly eliminate our non-pointer variables. */
2387 for (i = 1; i < FIRST_REF_NODE; i++)
2389 unsigned int node = si->node_mapping[i];
2391 if (graph->pointer_label[node] == 0)
2393 if (dump_file && (dump_flags & TDF_DETAILS))
2394 fprintf (dump_file,
2395 "%s is a non-pointer variable, eliminating edges.\n",
2396 get_varinfo (node)->name);
2397 stats.nonpointer_vars++;
2398 clear_edges_for_node (graph, node);
2402 return si;
2405 /* Free information that was only necessary for variable
2406 substitution. */
2408 static void
2409 free_var_substitution_info (struct scc_info *si)
2411 free_scc_info (si);
2412 free (graph->pointer_label);
2413 free (graph->loc_label);
2414 free (graph->pointed_by);
2415 free (graph->points_to);
2416 free (graph->eq_rep);
2417 sbitmap_free (graph->direct_nodes);
2418 pointer_equiv_class_table.dispose ();
2419 location_equiv_class_table.dispose ();
2420 bitmap_obstack_release (&iteration_obstack);
2423 /* Return an existing node that is equivalent to NODE, which has
2424 equivalence class LABEL, if one exists. Return NODE otherwise. */
2426 static unsigned int
2427 find_equivalent_node (constraint_graph_t graph,
2428 unsigned int node, unsigned int label)
2430 /* If the address version of this variable is unused, we can
2431 substitute it for anything else with the same label.
2432 Otherwise, we know the pointers are equivalent, but not the
2433 locations, and we can unite them later. */
2435 if (!bitmap_bit_p (graph->address_taken, node))
2437 gcc_checking_assert (label < graph->size);
2439 if (graph->eq_rep[label] != -1)
2441 /* Unify the two variables since we know they are equivalent. */
2442 if (unite (graph->eq_rep[label], node))
2443 unify_nodes (graph, graph->eq_rep[label], node, false);
2444 return graph->eq_rep[label];
2446 else
2448 graph->eq_rep[label] = node;
2449 graph->pe_rep[label] = node;
2452 else
2454 gcc_checking_assert (label < graph->size);
2455 graph->pe[node] = label;
2456 if (graph->pe_rep[label] == -1)
2457 graph->pe_rep[label] = node;
2460 return node;
2463 /* Unite pointer equivalent but not location equivalent nodes in
2464 GRAPH. This may only be performed once variable substitution is
2465 finished. */
2467 static void
2468 unite_pointer_equivalences (constraint_graph_t graph)
2470 unsigned int i;
2472 /* Go through the pointer equivalences and unite them to their
2473 representative, if they aren't already. */
2474 for (i = 1; i < FIRST_REF_NODE; i++)
2476 unsigned int label = graph->pe[i];
2477 if (label)
2479 int label_rep = graph->pe_rep[label];
2481 if (label_rep == -1)
2482 continue;
2484 label_rep = find (label_rep);
2485 if (label_rep >= 0 && unite (label_rep, find (i)))
2486 unify_nodes (graph, label_rep, i, false);
2491 /* Move complex constraints to the GRAPH nodes they belong to. */
2493 static void
2494 move_complex_constraints (constraint_graph_t graph)
2496 int i;
2497 constraint_t c;
2499 FOR_EACH_VEC_ELT (constraints, i, c)
2501 if (c)
2503 struct constraint_expr lhs = c->lhs;
2504 struct constraint_expr rhs = c->rhs;
2506 if (lhs.type == DEREF)
2508 insert_into_complex (graph, lhs.var, c);
2510 else if (rhs.type == DEREF)
2512 if (!(get_varinfo (lhs.var)->is_special_var))
2513 insert_into_complex (graph, rhs.var, c);
2515 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2516 && (lhs.offset != 0 || rhs.offset != 0))
2518 insert_into_complex (graph, rhs.var, c);
2525 /* Optimize and rewrite complex constraints while performing
2526 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2527 result of perform_variable_substitution. */
2529 static void
2530 rewrite_constraints (constraint_graph_t graph,
2531 struct scc_info *si)
2533 int i;
2534 constraint_t c;
2536 #ifdef ENABLE_CHECKING
2537 for (unsigned int j = 0; j < graph->size; j++)
2538 gcc_assert (find (j) == j);
2539 #endif
2541 FOR_EACH_VEC_ELT (constraints, i, c)
2543 struct constraint_expr lhs = c->lhs;
2544 struct constraint_expr rhs = c->rhs;
2545 unsigned int lhsvar = find (lhs.var);
2546 unsigned int rhsvar = find (rhs.var);
2547 unsigned int lhsnode, rhsnode;
2548 unsigned int lhslabel, rhslabel;
2550 lhsnode = si->node_mapping[lhsvar];
2551 rhsnode = si->node_mapping[rhsvar];
2552 lhslabel = graph->pointer_label[lhsnode];
2553 rhslabel = graph->pointer_label[rhsnode];
2555 /* See if it is really a non-pointer variable, and if so, ignore
2556 the constraint. */
2557 if (lhslabel == 0)
2559 if (dump_file && (dump_flags & TDF_DETAILS))
2562 fprintf (dump_file, "%s is a non-pointer variable,"
2563 "ignoring constraint:",
2564 get_varinfo (lhs.var)->name);
2565 dump_constraint (dump_file, c);
2566 fprintf (dump_file, "\n");
2568 constraints[i] = NULL;
2569 continue;
2572 if (rhslabel == 0)
2574 if (dump_file && (dump_flags & TDF_DETAILS))
2577 fprintf (dump_file, "%s is a non-pointer variable,"
2578 "ignoring constraint:",
2579 get_varinfo (rhs.var)->name);
2580 dump_constraint (dump_file, c);
2581 fprintf (dump_file, "\n");
2583 constraints[i] = NULL;
2584 continue;
2587 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2588 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2589 c->lhs.var = lhsvar;
2590 c->rhs.var = rhsvar;
2594 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2595 part of an SCC, false otherwise. */
2597 static bool
2598 eliminate_indirect_cycles (unsigned int node)
2600 if (graph->indirect_cycles[node] != -1
2601 && !bitmap_empty_p (get_varinfo (node)->solution))
2603 unsigned int i;
2604 auto_vec<unsigned> queue;
2605 int queuepos;
2606 unsigned int to = find (graph->indirect_cycles[node]);
2607 bitmap_iterator bi;
2609 /* We can't touch the solution set and call unify_nodes
2610 at the same time, because unify_nodes is going to do
2611 bitmap unions into it. */
2613 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2615 if (find (i) == i && i != to)
2617 if (unite (to, i))
2618 queue.safe_push (i);
2622 for (queuepos = 0;
2623 queue.iterate (queuepos, &i);
2624 queuepos++)
2626 unify_nodes (graph, to, i, true);
2628 return true;
2630 return false;
2633 /* Solve the constraint graph GRAPH using our worklist solver.
2634 This is based on the PW* family of solvers from the "Efficient Field
2635 Sensitive Pointer Analysis for C" paper.
2636 It works by iterating over all the graph nodes, processing the complex
2637 constraints and propagating the copy constraints, until everything stops
2638 changed. This corresponds to steps 6-8 in the solving list given above. */
2640 static void
2641 solve_graph (constraint_graph_t graph)
2643 unsigned int size = graph->size;
2644 unsigned int i;
2645 bitmap pts;
2647 changed = BITMAP_ALLOC (NULL);
2649 /* Mark all initial non-collapsed nodes as changed. */
2650 for (i = 1; i < size; i++)
2652 varinfo_t ivi = get_varinfo (i);
2653 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2654 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2655 || graph->complex[i].length () > 0))
2656 bitmap_set_bit (changed, i);
2659 /* Allocate a bitmap to be used to store the changed bits. */
2660 pts = BITMAP_ALLOC (&pta_obstack);
2662 while (!bitmap_empty_p (changed))
2664 unsigned int i;
2665 struct topo_info *ti = init_topo_info ();
2666 stats.iterations++;
2668 bitmap_obstack_initialize (&iteration_obstack);
2670 compute_topo_order (graph, ti);
2672 while (ti->topo_order.length () != 0)
2675 i = ti->topo_order.pop ();
2677 /* If this variable is not a representative, skip it. */
2678 if (find (i) != i)
2679 continue;
2681 /* In certain indirect cycle cases, we may merge this
2682 variable to another. */
2683 if (eliminate_indirect_cycles (i) && find (i) != i)
2684 continue;
2686 /* If the node has changed, we need to process the
2687 complex constraints and outgoing edges again. */
2688 if (bitmap_clear_bit (changed, i))
2690 unsigned int j;
2691 constraint_t c;
2692 bitmap solution;
2693 vec<constraint_t> complex = graph->complex[i];
2694 varinfo_t vi = get_varinfo (i);
2695 bool solution_empty;
2697 /* Compute the changed set of solution bits. If anything
2698 is in the solution just propagate that. */
2699 if (bitmap_bit_p (vi->solution, anything_id))
2701 /* If anything is also in the old solution there is
2702 nothing to do.
2703 ??? But we shouldn't ended up with "changed" set ... */
2704 if (vi->oldsolution
2705 && bitmap_bit_p (vi->oldsolution, anything_id))
2706 continue;
2707 bitmap_copy (pts, get_varinfo (find (anything_id))->solution);
2709 else if (vi->oldsolution)
2710 bitmap_and_compl (pts, vi->solution, vi->oldsolution);
2711 else
2712 bitmap_copy (pts, vi->solution);
2714 if (bitmap_empty_p (pts))
2715 continue;
2717 if (vi->oldsolution)
2718 bitmap_ior_into (vi->oldsolution, pts);
2719 else
2721 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
2722 bitmap_copy (vi->oldsolution, pts);
2725 solution = vi->solution;
2726 solution_empty = bitmap_empty_p (solution);
2728 /* Process the complex constraints */
2729 bitmap expanded_pts = NULL;
2730 FOR_EACH_VEC_ELT (complex, j, c)
2732 /* XXX: This is going to unsort the constraints in
2733 some cases, which will occasionally add duplicate
2734 constraints during unification. This does not
2735 affect correctness. */
2736 c->lhs.var = find (c->lhs.var);
2737 c->rhs.var = find (c->rhs.var);
2739 /* The only complex constraint that can change our
2740 solution to non-empty, given an empty solution,
2741 is a constraint where the lhs side is receiving
2742 some set from elsewhere. */
2743 if (!solution_empty || c->lhs.type != DEREF)
2744 do_complex_constraint (graph, c, pts, &expanded_pts);
2746 BITMAP_FREE (expanded_pts);
2748 solution_empty = bitmap_empty_p (solution);
2750 if (!solution_empty)
2752 bitmap_iterator bi;
2753 unsigned eff_escaped_id = find (escaped_id);
2755 /* Propagate solution to all successors. */
2756 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2757 0, j, bi)
2759 bitmap tmp;
2760 bool flag;
2762 unsigned int to = find (j);
2763 tmp = get_varinfo (to)->solution;
2764 flag = false;
2766 /* Don't try to propagate to ourselves. */
2767 if (to == i)
2768 continue;
2770 /* If we propagate from ESCAPED use ESCAPED as
2771 placeholder. */
2772 if (i == eff_escaped_id)
2773 flag = bitmap_set_bit (tmp, escaped_id);
2774 else
2775 flag = bitmap_ior_into (tmp, pts);
2777 if (flag)
2778 bitmap_set_bit (changed, to);
2783 free_topo_info (ti);
2784 bitmap_obstack_release (&iteration_obstack);
2787 BITMAP_FREE (pts);
2788 BITMAP_FREE (changed);
2789 bitmap_obstack_release (&oldpta_obstack);
2792 /* Map from trees to variable infos. */
2793 static struct pointer_map_t *vi_for_tree;
2796 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2798 static void
2799 insert_vi_for_tree (tree t, varinfo_t vi)
2801 void **slot = pointer_map_insert (vi_for_tree, t);
2802 gcc_assert (vi);
2803 gcc_assert (*slot == NULL);
2804 *slot = vi;
2807 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2808 exist in the map, return NULL, otherwise, return the varinfo we found. */
2810 static varinfo_t
2811 lookup_vi_for_tree (tree t)
2813 void **slot = pointer_map_contains (vi_for_tree, t);
2814 if (slot == NULL)
2815 return NULL;
2817 return (varinfo_t) *slot;
2820 /* Return a printable name for DECL */
2822 static const char *
2823 alias_get_name (tree decl)
2825 const char *res = NULL;
2826 char *temp;
2827 int num_printed = 0;
2829 if (!dump_file)
2830 return "NULL";
2832 if (TREE_CODE (decl) == SSA_NAME)
2834 res = get_name (decl);
2835 if (res)
2836 num_printed = asprintf (&temp, "%s_%u", res, SSA_NAME_VERSION (decl));
2837 else
2838 num_printed = asprintf (&temp, "_%u", SSA_NAME_VERSION (decl));
2839 if (num_printed > 0)
2841 res = ggc_strdup (temp);
2842 free (temp);
2845 else if (DECL_P (decl))
2847 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2848 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2849 else
2851 res = get_name (decl);
2852 if (!res)
2854 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2855 if (num_printed > 0)
2857 res = ggc_strdup (temp);
2858 free (temp);
2863 if (res != NULL)
2864 return res;
2866 return "NULL";
2869 /* Find the variable id for tree T in the map.
2870 If T doesn't exist in the map, create an entry for it and return it. */
2872 static varinfo_t
2873 get_vi_for_tree (tree t)
2875 void **slot = pointer_map_contains (vi_for_tree, t);
2876 if (slot == NULL)
2877 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2879 return (varinfo_t) *slot;
2882 /* Get a scalar constraint expression for a new temporary variable. */
2884 static struct constraint_expr
2885 new_scalar_tmp_constraint_exp (const char *name)
2887 struct constraint_expr tmp;
2888 varinfo_t vi;
2890 vi = new_var_info (NULL_TREE, name);
2891 vi->offset = 0;
2892 vi->size = -1;
2893 vi->fullsize = -1;
2894 vi->is_full_var = 1;
2896 tmp.var = vi->id;
2897 tmp.type = SCALAR;
2898 tmp.offset = 0;
2900 return tmp;
2903 /* Get a constraint expression vector from an SSA_VAR_P node.
2904 If address_p is true, the result will be taken its address of. */
2906 static void
2907 get_constraint_for_ssa_var (tree t, vec<ce_s> *results, bool address_p)
2909 struct constraint_expr cexpr;
2910 varinfo_t vi;
2912 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2913 gcc_assert (TREE_CODE (t) == SSA_NAME || DECL_P (t));
2915 /* For parameters, get at the points-to set for the actual parm
2916 decl. */
2917 if (TREE_CODE (t) == SSA_NAME
2918 && SSA_NAME_IS_DEFAULT_DEF (t)
2919 && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2920 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL))
2922 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2923 return;
2926 /* For global variables resort to the alias target. */
2927 if (TREE_CODE (t) == VAR_DECL
2928 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
2930 varpool_node *node = varpool_get_node (t);
2931 if (node && node->alias && node->analyzed)
2933 node = varpool_variable_node (node, NULL);
2934 t = node->decl;
2938 vi = get_vi_for_tree (t);
2939 cexpr.var = vi->id;
2940 cexpr.type = SCALAR;
2941 cexpr.offset = 0;
2942 /* If we determine the result is "anything", and we know this is readonly,
2943 say it points to readonly memory instead. */
2944 if (cexpr.var == anything_id && TREE_READONLY (t))
2946 gcc_unreachable ();
2947 cexpr.type = ADDRESSOF;
2948 cexpr.var = readonly_id;
2951 /* If we are not taking the address of the constraint expr, add all
2952 sub-fiels of the variable as well. */
2953 if (!address_p
2954 && !vi->is_full_var)
2956 for (; vi; vi = vi_next (vi))
2958 cexpr.var = vi->id;
2959 results->safe_push (cexpr);
2961 return;
2964 results->safe_push (cexpr);
2967 /* Process constraint T, performing various simplifications and then
2968 adding it to our list of overall constraints. */
2970 static void
2971 process_constraint (constraint_t t)
2973 struct constraint_expr rhs = t->rhs;
2974 struct constraint_expr lhs = t->lhs;
2976 gcc_assert (rhs.var < varmap.length ());
2977 gcc_assert (lhs.var < varmap.length ());
2979 /* If we didn't get any useful constraint from the lhs we get
2980 &ANYTHING as fallback from get_constraint_for. Deal with
2981 it here by turning it into *ANYTHING. */
2982 if (lhs.type == ADDRESSOF
2983 && lhs.var == anything_id)
2984 lhs.type = DEREF;
2986 /* ADDRESSOF on the lhs is invalid. */
2987 gcc_assert (lhs.type != ADDRESSOF);
2989 /* We shouldn't add constraints from things that cannot have pointers.
2990 It's not completely trivial to avoid in the callers, so do it here. */
2991 if (rhs.type != ADDRESSOF
2992 && !get_varinfo (rhs.var)->may_have_pointers)
2993 return;
2995 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2996 if (!get_varinfo (lhs.var)->may_have_pointers)
2997 return;
2999 /* This can happen in our IR with things like n->a = *p */
3000 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
3002 /* Split into tmp = *rhs, *lhs = tmp */
3003 struct constraint_expr tmplhs;
3004 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp");
3005 process_constraint (new_constraint (tmplhs, rhs));
3006 process_constraint (new_constraint (lhs, tmplhs));
3008 else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
3010 /* Split into tmp = &rhs, *lhs = tmp */
3011 struct constraint_expr tmplhs;
3012 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp");
3013 process_constraint (new_constraint (tmplhs, rhs));
3014 process_constraint (new_constraint (lhs, tmplhs));
3016 else
3018 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
3019 constraints.safe_push (t);
3024 /* Return the position, in bits, of FIELD_DECL from the beginning of its
3025 structure. */
3027 static HOST_WIDE_INT
3028 bitpos_of_field (const tree fdecl)
3030 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl))
3031 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl)))
3032 return -1;
3034 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
3035 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl)));
3039 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
3040 resulting constraint expressions in *RESULTS. */
3042 static void
3043 get_constraint_for_ptr_offset (tree ptr, tree offset,
3044 vec<ce_s> *results)
3046 struct constraint_expr c;
3047 unsigned int j, n;
3048 HOST_WIDE_INT rhsoffset;
3050 /* If we do not do field-sensitive PTA adding offsets to pointers
3051 does not change the points-to solution. */
3052 if (!use_field_sensitive)
3054 get_constraint_for_rhs (ptr, results);
3055 return;
3058 /* If the offset is not a non-negative integer constant that fits
3059 in a HOST_WIDE_INT, we have to fall back to a conservative
3060 solution which includes all sub-fields of all pointed-to
3061 variables of ptr. */
3062 if (offset == NULL_TREE
3063 || TREE_CODE (offset) != INTEGER_CST)
3064 rhsoffset = UNKNOWN_OFFSET;
3065 else
3067 /* Sign-extend the offset. */
3068 double_int soffset = tree_to_double_int (offset)
3069 .sext (TYPE_PRECISION (TREE_TYPE (offset)));
3070 if (!soffset.fits_shwi ())
3071 rhsoffset = UNKNOWN_OFFSET;
3072 else
3074 /* Make sure the bit-offset also fits. */
3075 HOST_WIDE_INT rhsunitoffset = soffset.low;
3076 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
3077 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
3078 rhsoffset = UNKNOWN_OFFSET;
3082 get_constraint_for_rhs (ptr, results);
3083 if (rhsoffset == 0)
3084 return;
3086 /* As we are eventually appending to the solution do not use
3087 vec::iterate here. */
3088 n = results->length ();
3089 for (j = 0; j < n; j++)
3091 varinfo_t curr;
3092 c = (*results)[j];
3093 curr = get_varinfo (c.var);
3095 if (c.type == ADDRESSOF
3096 /* If this varinfo represents a full variable just use it. */
3097 && curr->is_full_var)
3099 else if (c.type == ADDRESSOF
3100 /* If we do not know the offset add all subfields. */
3101 && rhsoffset == UNKNOWN_OFFSET)
3103 varinfo_t temp = get_varinfo (curr->head);
3106 struct constraint_expr c2;
3107 c2.var = temp->id;
3108 c2.type = ADDRESSOF;
3109 c2.offset = 0;
3110 if (c2.var != c.var)
3111 results->safe_push (c2);
3112 temp = vi_next (temp);
3114 while (temp);
3116 else if (c.type == ADDRESSOF)
3118 varinfo_t temp;
3119 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
3121 /* If curr->offset + rhsoffset is less than zero adjust it. */
3122 if (rhsoffset < 0
3123 && curr->offset < offset)
3124 offset = 0;
3126 /* We have to include all fields that overlap the current
3127 field shifted by rhsoffset. And we include at least
3128 the last or the first field of the variable to represent
3129 reachability of off-bound addresses, in particular &object + 1,
3130 conservatively correct. */
3131 temp = first_or_preceding_vi_for_offset (curr, offset);
3132 c.var = temp->id;
3133 c.offset = 0;
3134 temp = vi_next (temp);
3135 while (temp
3136 && temp->offset < offset + curr->size)
3138 struct constraint_expr c2;
3139 c2.var = temp->id;
3140 c2.type = ADDRESSOF;
3141 c2.offset = 0;
3142 results->safe_push (c2);
3143 temp = vi_next (temp);
3146 else if (c.type == SCALAR)
3148 gcc_assert (c.offset == 0);
3149 c.offset = rhsoffset;
3151 else
3152 /* We shouldn't get any DEREFs here. */
3153 gcc_unreachable ();
3155 (*results)[j] = c;
3160 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3161 If address_p is true the result will be taken its address of.
3162 If lhs_p is true then the constraint expression is assumed to be used
3163 as the lhs. */
3165 static void
3166 get_constraint_for_component_ref (tree t, vec<ce_s> *results,
3167 bool address_p, bool lhs_p)
3169 tree orig_t = t;
3170 HOST_WIDE_INT bitsize = -1;
3171 HOST_WIDE_INT bitmaxsize = -1;
3172 HOST_WIDE_INT bitpos;
3173 tree forzero;
3175 /* Some people like to do cute things like take the address of
3176 &0->a.b */
3177 forzero = t;
3178 while (handled_component_p (forzero)
3179 || INDIRECT_REF_P (forzero)
3180 || TREE_CODE (forzero) == MEM_REF)
3181 forzero = TREE_OPERAND (forzero, 0);
3183 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
3185 struct constraint_expr temp;
3187 temp.offset = 0;
3188 temp.var = integer_id;
3189 temp.type = SCALAR;
3190 results->safe_push (temp);
3191 return;
3194 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
3196 /* Pretend to take the address of the base, we'll take care of
3197 adding the required subset of sub-fields below. */
3198 get_constraint_for_1 (t, results, true, lhs_p);
3199 gcc_assert (results->length () == 1);
3200 struct constraint_expr &result = results->last ();
3202 if (result.type == SCALAR
3203 && get_varinfo (result.var)->is_full_var)
3204 /* For single-field vars do not bother about the offset. */
3205 result.offset = 0;
3206 else if (result.type == SCALAR)
3208 /* In languages like C, you can access one past the end of an
3209 array. You aren't allowed to dereference it, so we can
3210 ignore this constraint. When we handle pointer subtraction,
3211 we may have to do something cute here. */
3213 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result.var)->fullsize
3214 && bitmaxsize != 0)
3216 /* It's also not true that the constraint will actually start at the
3217 right offset, it may start in some padding. We only care about
3218 setting the constraint to the first actual field it touches, so
3219 walk to find it. */
3220 struct constraint_expr cexpr = result;
3221 varinfo_t curr;
3222 results->pop ();
3223 cexpr.offset = 0;
3224 for (curr = get_varinfo (cexpr.var); curr; curr = vi_next (curr))
3226 if (ranges_overlap_p (curr->offset, curr->size,
3227 bitpos, bitmaxsize))
3229 cexpr.var = curr->id;
3230 results->safe_push (cexpr);
3231 if (address_p)
3232 break;
3235 /* If we are going to take the address of this field then
3236 to be able to compute reachability correctly add at least
3237 the last field of the variable. */
3238 if (address_p && results->length () == 0)
3240 curr = get_varinfo (cexpr.var);
3241 while (curr->next != 0)
3242 curr = vi_next (curr);
3243 cexpr.var = curr->id;
3244 results->safe_push (cexpr);
3246 else if (results->length () == 0)
3247 /* Assert that we found *some* field there. The user couldn't be
3248 accessing *only* padding. */
3249 /* Still the user could access one past the end of an array
3250 embedded in a struct resulting in accessing *only* padding. */
3251 /* Or accessing only padding via type-punning to a type
3252 that has a filed just in padding space. */
3254 cexpr.type = SCALAR;
3255 cexpr.var = anything_id;
3256 cexpr.offset = 0;
3257 results->safe_push (cexpr);
3260 else if (bitmaxsize == 0)
3262 if (dump_file && (dump_flags & TDF_DETAILS))
3263 fprintf (dump_file, "Access to zero-sized part of variable,"
3264 "ignoring\n");
3266 else
3267 if (dump_file && (dump_flags & TDF_DETAILS))
3268 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3270 else if (result.type == DEREF)
3272 /* If we do not know exactly where the access goes say so. Note
3273 that only for non-structure accesses we know that we access
3274 at most one subfiled of any variable. */
3275 if (bitpos == -1
3276 || bitsize != bitmaxsize
3277 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
3278 || result.offset == UNKNOWN_OFFSET)
3279 result.offset = UNKNOWN_OFFSET;
3280 else
3281 result.offset += bitpos;
3283 else if (result.type == ADDRESSOF)
3285 /* We can end up here for component references on a
3286 VIEW_CONVERT_EXPR <>(&foobar). */
3287 result.type = SCALAR;
3288 result.var = anything_id;
3289 result.offset = 0;
3291 else
3292 gcc_unreachable ();
3296 /* Dereference the constraint expression CONS, and return the result.
3297 DEREF (ADDRESSOF) = SCALAR
3298 DEREF (SCALAR) = DEREF
3299 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3300 This is needed so that we can handle dereferencing DEREF constraints. */
3302 static void
3303 do_deref (vec<ce_s> *constraints)
3305 struct constraint_expr *c;
3306 unsigned int i = 0;
3308 FOR_EACH_VEC_ELT (*constraints, i, c)
3310 if (c->type == SCALAR)
3311 c->type = DEREF;
3312 else if (c->type == ADDRESSOF)
3313 c->type = SCALAR;
3314 else if (c->type == DEREF)
3316 struct constraint_expr tmplhs;
3317 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp");
3318 process_constraint (new_constraint (tmplhs, *c));
3319 c->var = tmplhs.var;
3321 else
3322 gcc_unreachable ();
3326 /* Given a tree T, return the constraint expression for taking the
3327 address of it. */
3329 static void
3330 get_constraint_for_address_of (tree t, vec<ce_s> *results)
3332 struct constraint_expr *c;
3333 unsigned int i;
3335 get_constraint_for_1 (t, results, true, true);
3337 FOR_EACH_VEC_ELT (*results, i, c)
3339 if (c->type == DEREF)
3340 c->type = SCALAR;
3341 else
3342 c->type = ADDRESSOF;
3346 /* Given a tree T, return the constraint expression for it. */
3348 static void
3349 get_constraint_for_1 (tree t, vec<ce_s> *results, bool address_p,
3350 bool lhs_p)
3352 struct constraint_expr temp;
3354 /* x = integer is all glommed to a single variable, which doesn't
3355 point to anything by itself. That is, of course, unless it is an
3356 integer constant being treated as a pointer, in which case, we
3357 will return that this is really the addressof anything. This
3358 happens below, since it will fall into the default case. The only
3359 case we know something about an integer treated like a pointer is
3360 when it is the NULL pointer, and then we just say it points to
3361 NULL.
3363 Do not do that if -fno-delete-null-pointer-checks though, because
3364 in that case *NULL does not fail, so it _should_ alias *anything.
3365 It is not worth adding a new option or renaming the existing one,
3366 since this case is relatively obscure. */
3367 if ((TREE_CODE (t) == INTEGER_CST
3368 && integer_zerop (t))
3369 /* The only valid CONSTRUCTORs in gimple with pointer typed
3370 elements are zero-initializer. But in IPA mode we also
3371 process global initializers, so verify at least. */
3372 || (TREE_CODE (t) == CONSTRUCTOR
3373 && CONSTRUCTOR_NELTS (t) == 0))
3375 if (flag_delete_null_pointer_checks)
3376 temp.var = nothing_id;
3377 else
3378 temp.var = nonlocal_id;
3379 temp.type = ADDRESSOF;
3380 temp.offset = 0;
3381 results->safe_push (temp);
3382 return;
3385 /* String constants are read-only. */
3386 if (TREE_CODE (t) == STRING_CST)
3388 temp.var = readonly_id;
3389 temp.type = SCALAR;
3390 temp.offset = 0;
3391 results->safe_push (temp);
3392 return;
3395 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3397 case tcc_expression:
3399 switch (TREE_CODE (t))
3401 case ADDR_EXPR:
3402 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3403 return;
3404 default:;
3406 break;
3408 case tcc_reference:
3410 switch (TREE_CODE (t))
3412 case MEM_REF:
3414 struct constraint_expr cs;
3415 varinfo_t vi, curr;
3416 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0),
3417 TREE_OPERAND (t, 1), results);
3418 do_deref (results);
3420 /* If we are not taking the address then make sure to process
3421 all subvariables we might access. */
3422 if (address_p)
3423 return;
3425 cs = results->last ();
3426 if (cs.type == DEREF
3427 && type_can_have_subvars (TREE_TYPE (t)))
3429 /* For dereferences this means we have to defer it
3430 to solving time. */
3431 results->last ().offset = UNKNOWN_OFFSET;
3432 return;
3434 if (cs.type != SCALAR)
3435 return;
3437 vi = get_varinfo (cs.var);
3438 curr = vi_next (vi);
3439 if (!vi->is_full_var
3440 && curr)
3442 unsigned HOST_WIDE_INT size;
3443 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t))))
3444 size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t)));
3445 else
3446 size = -1;
3447 for (; curr; curr = vi_next (curr))
3449 if (curr->offset - vi->offset < size)
3451 cs.var = curr->id;
3452 results->safe_push (cs);
3454 else
3455 break;
3458 return;
3460 case ARRAY_REF:
3461 case ARRAY_RANGE_REF:
3462 case COMPONENT_REF:
3463 get_constraint_for_component_ref (t, results, address_p, lhs_p);
3464 return;
3465 case VIEW_CONVERT_EXPR:
3466 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p,
3467 lhs_p);
3468 return;
3469 /* We are missing handling for TARGET_MEM_REF here. */
3470 default:;
3472 break;
3474 case tcc_exceptional:
3476 switch (TREE_CODE (t))
3478 case SSA_NAME:
3480 get_constraint_for_ssa_var (t, results, address_p);
3481 return;
3483 case CONSTRUCTOR:
3485 unsigned int i;
3486 tree val;
3487 auto_vec<ce_s> tmp;
3488 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
3490 struct constraint_expr *rhsp;
3491 unsigned j;
3492 get_constraint_for_1 (val, &tmp, address_p, lhs_p);
3493 FOR_EACH_VEC_ELT (tmp, j, rhsp)
3494 results->safe_push (*rhsp);
3495 tmp.truncate (0);
3497 /* We do not know whether the constructor was complete,
3498 so technically we have to add &NOTHING or &ANYTHING
3499 like we do for an empty constructor as well. */
3500 return;
3502 default:;
3504 break;
3506 case tcc_declaration:
3508 get_constraint_for_ssa_var (t, results, address_p);
3509 return;
3511 case tcc_constant:
3513 /* We cannot refer to automatic variables through constants. */
3514 temp.type = ADDRESSOF;
3515 temp.var = nonlocal_id;
3516 temp.offset = 0;
3517 results->safe_push (temp);
3518 return;
3520 default:;
3523 /* The default fallback is a constraint from anything. */
3524 temp.type = ADDRESSOF;
3525 temp.var = anything_id;
3526 temp.offset = 0;
3527 results->safe_push (temp);
3530 /* Given a gimple tree T, return the constraint expression vector for it. */
3532 static void
3533 get_constraint_for (tree t, vec<ce_s> *results)
3535 gcc_assert (results->length () == 0);
3537 get_constraint_for_1 (t, results, false, true);
3540 /* Given a gimple tree T, return the constraint expression vector for it
3541 to be used as the rhs of a constraint. */
3543 static void
3544 get_constraint_for_rhs (tree t, vec<ce_s> *results)
3546 gcc_assert (results->length () == 0);
3548 get_constraint_for_1 (t, results, false, false);
3552 /* Efficiently generates constraints from all entries in *RHSC to all
3553 entries in *LHSC. */
3555 static void
3556 process_all_all_constraints (vec<ce_s> lhsc,
3557 vec<ce_s> rhsc)
3559 struct constraint_expr *lhsp, *rhsp;
3560 unsigned i, j;
3562 if (lhsc.length () <= 1 || rhsc.length () <= 1)
3564 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3565 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
3566 process_constraint (new_constraint (*lhsp, *rhsp));
3568 else
3570 struct constraint_expr tmp;
3571 tmp = new_scalar_tmp_constraint_exp ("allalltmp");
3572 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
3573 process_constraint (new_constraint (tmp, *rhsp));
3574 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3575 process_constraint (new_constraint (*lhsp, tmp));
3579 /* Handle aggregate copies by expanding into copies of the respective
3580 fields of the structures. */
3582 static void
3583 do_structure_copy (tree lhsop, tree rhsop)
3585 struct constraint_expr *lhsp, *rhsp;
3586 auto_vec<ce_s> lhsc;
3587 auto_vec<ce_s> rhsc;
3588 unsigned j;
3590 get_constraint_for (lhsop, &lhsc);
3591 get_constraint_for_rhs (rhsop, &rhsc);
3592 lhsp = &lhsc[0];
3593 rhsp = &rhsc[0];
3594 if (lhsp->type == DEREF
3595 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3596 || rhsp->type == DEREF)
3598 if (lhsp->type == DEREF)
3600 gcc_assert (lhsc.length () == 1);
3601 lhsp->offset = UNKNOWN_OFFSET;
3603 if (rhsp->type == DEREF)
3605 gcc_assert (rhsc.length () == 1);
3606 rhsp->offset = UNKNOWN_OFFSET;
3608 process_all_all_constraints (lhsc, rhsc);
3610 else if (lhsp->type == SCALAR
3611 && (rhsp->type == SCALAR
3612 || rhsp->type == ADDRESSOF))
3614 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3615 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
3616 unsigned k = 0;
3617 get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize);
3618 get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize);
3619 for (j = 0; lhsc.iterate (j, &lhsp);)
3621 varinfo_t lhsv, rhsv;
3622 rhsp = &rhsc[k];
3623 lhsv = get_varinfo (lhsp->var);
3624 rhsv = get_varinfo (rhsp->var);
3625 if (lhsv->may_have_pointers
3626 && (lhsv->is_full_var
3627 || rhsv->is_full_var
3628 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3629 rhsv->offset + lhsoffset, rhsv->size)))
3630 process_constraint (new_constraint (*lhsp, *rhsp));
3631 if (!rhsv->is_full_var
3632 && (lhsv->is_full_var
3633 || (lhsv->offset + rhsoffset + lhsv->size
3634 > rhsv->offset + lhsoffset + rhsv->size)))
3636 ++k;
3637 if (k >= rhsc.length ())
3638 break;
3640 else
3641 ++j;
3644 else
3645 gcc_unreachable ();
3648 /* Create constraints ID = { rhsc }. */
3650 static void
3651 make_constraints_to (unsigned id, vec<ce_s> rhsc)
3653 struct constraint_expr *c;
3654 struct constraint_expr includes;
3655 unsigned int j;
3657 includes.var = id;
3658 includes.offset = 0;
3659 includes.type = SCALAR;
3661 FOR_EACH_VEC_ELT (rhsc, j, c)
3662 process_constraint (new_constraint (includes, *c));
3665 /* Create a constraint ID = OP. */
3667 static void
3668 make_constraint_to (unsigned id, tree op)
3670 auto_vec<ce_s> rhsc;
3671 get_constraint_for_rhs (op, &rhsc);
3672 make_constraints_to (id, rhsc);
3675 /* Create a constraint ID = &FROM. */
3677 static void
3678 make_constraint_from (varinfo_t vi, int from)
3680 struct constraint_expr lhs, rhs;
3682 lhs.var = vi->id;
3683 lhs.offset = 0;
3684 lhs.type = SCALAR;
3686 rhs.var = from;
3687 rhs.offset = 0;
3688 rhs.type = ADDRESSOF;
3689 process_constraint (new_constraint (lhs, rhs));
3692 /* Create a constraint ID = FROM. */
3694 static void
3695 make_copy_constraint (varinfo_t vi, int from)
3697 struct constraint_expr lhs, rhs;
3699 lhs.var = vi->id;
3700 lhs.offset = 0;
3701 lhs.type = SCALAR;
3703 rhs.var = from;
3704 rhs.offset = 0;
3705 rhs.type = SCALAR;
3706 process_constraint (new_constraint (lhs, rhs));
3709 /* Make constraints necessary to make OP escape. */
3711 static void
3712 make_escape_constraint (tree op)
3714 make_constraint_to (escaped_id, op);
3717 /* Add constraints to that the solution of VI is transitively closed. */
3719 static void
3720 make_transitive_closure_constraints (varinfo_t vi)
3722 struct constraint_expr lhs, rhs;
3724 /* VAR = *VAR; */
3725 lhs.type = SCALAR;
3726 lhs.var = vi->id;
3727 lhs.offset = 0;
3728 rhs.type = DEREF;
3729 rhs.var = vi->id;
3730 rhs.offset = UNKNOWN_OFFSET;
3731 process_constraint (new_constraint (lhs, rhs));
3734 /* Temporary storage for fake var decls. */
3735 struct obstack fake_var_decl_obstack;
3737 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3739 static tree
3740 build_fake_var_decl (tree type)
3742 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl);
3743 memset (decl, 0, sizeof (struct tree_var_decl));
3744 TREE_SET_CODE (decl, VAR_DECL);
3745 TREE_TYPE (decl) = type;
3746 DECL_UID (decl) = allocate_decl_uid ();
3747 SET_DECL_PT_UID (decl, -1);
3748 layout_decl (decl, 0);
3749 return decl;
3752 /* Create a new artificial heap variable with NAME.
3753 Return the created variable. */
3755 static varinfo_t
3756 make_heapvar (const char *name)
3758 varinfo_t vi;
3759 tree heapvar;
3761 heapvar = build_fake_var_decl (ptr_type_node);
3762 DECL_EXTERNAL (heapvar) = 1;
3764 vi = new_var_info (heapvar, name);
3765 vi->is_artificial_var = true;
3766 vi->is_heap_var = true;
3767 vi->is_unknown_size_var = true;
3768 vi->offset = 0;
3769 vi->fullsize = ~0;
3770 vi->size = ~0;
3771 vi->is_full_var = true;
3772 insert_vi_for_tree (heapvar, vi);
3774 return vi;
3777 /* Create a new artificial heap variable with NAME and make a
3778 constraint from it to LHS. Set flags according to a tag used
3779 for tracking restrict pointers. */
3781 static varinfo_t
3782 make_constraint_from_restrict (varinfo_t lhs, const char *name)
3784 varinfo_t vi = make_heapvar (name);
3785 vi->is_global_var = 1;
3786 vi->may_have_pointers = 1;
3787 make_constraint_from (lhs, vi->id);
3788 return vi;
3791 /* Create a new artificial heap variable with NAME and make a
3792 constraint from it to LHS. Set flags according to a tag used
3793 for tracking restrict pointers and make the artificial heap
3794 point to global memory. */
3796 static varinfo_t
3797 make_constraint_from_global_restrict (varinfo_t lhs, const char *name)
3799 varinfo_t vi = make_constraint_from_restrict (lhs, name);
3800 make_copy_constraint (vi, nonlocal_id);
3801 return vi;
3804 /* In IPA mode there are varinfos for different aspects of reach
3805 function designator. One for the points-to set of the return
3806 value, one for the variables that are clobbered by the function,
3807 one for its uses and one for each parameter (including a single
3808 glob for remaining variadic arguments). */
3810 enum { fi_clobbers = 1, fi_uses = 2,
3811 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 };
3813 /* Get a constraint for the requested part of a function designator FI
3814 when operating in IPA mode. */
3816 static struct constraint_expr
3817 get_function_part_constraint (varinfo_t fi, unsigned part)
3819 struct constraint_expr c;
3821 gcc_assert (in_ipa_mode);
3823 if (fi->id == anything_id)
3825 /* ??? We probably should have a ANYFN special variable. */
3826 c.var = anything_id;
3827 c.offset = 0;
3828 c.type = SCALAR;
3830 else if (TREE_CODE (fi->decl) == FUNCTION_DECL)
3832 varinfo_t ai = first_vi_for_offset (fi, part);
3833 if (ai)
3834 c.var = ai->id;
3835 else
3836 c.var = anything_id;
3837 c.offset = 0;
3838 c.type = SCALAR;
3840 else
3842 c.var = fi->id;
3843 c.offset = part;
3844 c.type = DEREF;
3847 return c;
3850 /* For non-IPA mode, generate constraints necessary for a call on the
3851 RHS. */
3853 static void
3854 handle_rhs_call (gimple stmt, vec<ce_s> *results)
3856 struct constraint_expr rhsc;
3857 unsigned i;
3858 bool returns_uses = false;
3860 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3862 tree arg = gimple_call_arg (stmt, i);
3863 int flags = gimple_call_arg_flags (stmt, i);
3865 /* If the argument is not used we can ignore it. */
3866 if (flags & EAF_UNUSED)
3867 continue;
3869 /* As we compute ESCAPED context-insensitive we do not gain
3870 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3871 set. The argument would still get clobbered through the
3872 escape solution. */
3873 if ((flags & EAF_NOCLOBBER)
3874 && (flags & EAF_NOESCAPE))
3876 varinfo_t uses = get_call_use_vi (stmt);
3877 if (!(flags & EAF_DIRECT))
3879 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3880 make_constraint_to (tem->id, arg);
3881 make_transitive_closure_constraints (tem);
3882 make_copy_constraint (uses, tem->id);
3884 else
3885 make_constraint_to (uses->id, arg);
3886 returns_uses = true;
3888 else if (flags & EAF_NOESCAPE)
3890 struct constraint_expr lhs, rhs;
3891 varinfo_t uses = get_call_use_vi (stmt);
3892 varinfo_t clobbers = get_call_clobber_vi (stmt);
3893 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3894 make_constraint_to (tem->id, arg);
3895 if (!(flags & EAF_DIRECT))
3896 make_transitive_closure_constraints (tem);
3897 make_copy_constraint (uses, tem->id);
3898 make_copy_constraint (clobbers, tem->id);
3899 /* Add *tem = nonlocal, do not add *tem = callused as
3900 EAF_NOESCAPE parameters do not escape to other parameters
3901 and all other uses appear in NONLOCAL as well. */
3902 lhs.type = DEREF;
3903 lhs.var = tem->id;
3904 lhs.offset = 0;
3905 rhs.type = SCALAR;
3906 rhs.var = nonlocal_id;
3907 rhs.offset = 0;
3908 process_constraint (new_constraint (lhs, rhs));
3909 returns_uses = true;
3911 else
3912 make_escape_constraint (arg);
3915 /* If we added to the calls uses solution make sure we account for
3916 pointers to it to be returned. */
3917 if (returns_uses)
3919 rhsc.var = get_call_use_vi (stmt)->id;
3920 rhsc.offset = 0;
3921 rhsc.type = SCALAR;
3922 results->safe_push (rhsc);
3925 /* The static chain escapes as well. */
3926 if (gimple_call_chain (stmt))
3927 make_escape_constraint (gimple_call_chain (stmt));
3929 /* And if we applied NRV the address of the return slot escapes as well. */
3930 if (gimple_call_return_slot_opt_p (stmt)
3931 && gimple_call_lhs (stmt) != NULL_TREE
3932 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3934 auto_vec<ce_s> tmpc;
3935 struct constraint_expr lhsc, *c;
3936 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3937 lhsc.var = escaped_id;
3938 lhsc.offset = 0;
3939 lhsc.type = SCALAR;
3940 FOR_EACH_VEC_ELT (tmpc, i, c)
3941 process_constraint (new_constraint (lhsc, *c));
3944 /* Regular functions return nonlocal memory. */
3945 rhsc.var = nonlocal_id;
3946 rhsc.offset = 0;
3947 rhsc.type = SCALAR;
3948 results->safe_push (rhsc);
3951 /* For non-IPA mode, generate constraints necessary for a call
3952 that returns a pointer and assigns it to LHS. This simply makes
3953 the LHS point to global and escaped variables. */
3955 static void
3956 handle_lhs_call (gimple stmt, tree lhs, int flags, vec<ce_s> rhsc,
3957 tree fndecl)
3959 auto_vec<ce_s> lhsc;
3961 get_constraint_for (lhs, &lhsc);
3962 /* If the store is to a global decl make sure to
3963 add proper escape constraints. */
3964 lhs = get_base_address (lhs);
3965 if (lhs
3966 && DECL_P (lhs)
3967 && is_global_var (lhs))
3969 struct constraint_expr tmpc;
3970 tmpc.var = escaped_id;
3971 tmpc.offset = 0;
3972 tmpc.type = SCALAR;
3973 lhsc.safe_push (tmpc);
3976 /* If the call returns an argument unmodified override the rhs
3977 constraints. */
3978 flags = gimple_call_return_flags (stmt);
3979 if (flags & ERF_RETURNS_ARG
3980 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
3982 tree arg;
3983 rhsc.create (0);
3984 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
3985 get_constraint_for (arg, &rhsc);
3986 process_all_all_constraints (lhsc, rhsc);
3987 rhsc.release ();
3989 else if (flags & ERF_NOALIAS)
3991 varinfo_t vi;
3992 struct constraint_expr tmpc;
3993 rhsc.create (0);
3994 vi = make_heapvar ("HEAP");
3995 /* We are marking allocated storage local, we deal with it becoming
3996 global by escaping and setting of vars_contains_escaped_heap. */
3997 DECL_EXTERNAL (vi->decl) = 0;
3998 vi->is_global_var = 0;
3999 /* If this is not a real malloc call assume the memory was
4000 initialized and thus may point to global memory. All
4001 builtin functions with the malloc attribute behave in a sane way. */
4002 if (!fndecl
4003 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
4004 make_constraint_from (vi, nonlocal_id);
4005 tmpc.var = vi->id;
4006 tmpc.offset = 0;
4007 tmpc.type = ADDRESSOF;
4008 rhsc.safe_push (tmpc);
4009 process_all_all_constraints (lhsc, rhsc);
4010 rhsc.release ();
4012 else
4013 process_all_all_constraints (lhsc, rhsc);
4016 /* For non-IPA mode, generate constraints necessary for a call of a
4017 const function that returns a pointer in the statement STMT. */
4019 static void
4020 handle_const_call (gimple stmt, vec<ce_s> *results)
4022 struct constraint_expr rhsc;
4023 unsigned int k;
4025 /* Treat nested const functions the same as pure functions as far
4026 as the static chain is concerned. */
4027 if (gimple_call_chain (stmt))
4029 varinfo_t uses = get_call_use_vi (stmt);
4030 make_transitive_closure_constraints (uses);
4031 make_constraint_to (uses->id, gimple_call_chain (stmt));
4032 rhsc.var = uses->id;
4033 rhsc.offset = 0;
4034 rhsc.type = SCALAR;
4035 results->safe_push (rhsc);
4038 /* May return arguments. */
4039 for (k = 0; k < gimple_call_num_args (stmt); ++k)
4041 tree arg = gimple_call_arg (stmt, k);
4042 auto_vec<ce_s> argc;
4043 unsigned i;
4044 struct constraint_expr *argp;
4045 get_constraint_for_rhs (arg, &argc);
4046 FOR_EACH_VEC_ELT (argc, i, argp)
4047 results->safe_push (*argp);
4050 /* May return addresses of globals. */
4051 rhsc.var = nonlocal_id;
4052 rhsc.offset = 0;
4053 rhsc.type = ADDRESSOF;
4054 results->safe_push (rhsc);
4057 /* For non-IPA mode, generate constraints necessary for a call to a
4058 pure function in statement STMT. */
4060 static void
4061 handle_pure_call (gimple stmt, vec<ce_s> *results)
4063 struct constraint_expr rhsc;
4064 unsigned i;
4065 varinfo_t uses = NULL;
4067 /* Memory reached from pointer arguments is call-used. */
4068 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4070 tree arg = gimple_call_arg (stmt, i);
4071 if (!uses)
4073 uses = get_call_use_vi (stmt);
4074 make_transitive_closure_constraints (uses);
4076 make_constraint_to (uses->id, arg);
4079 /* The static chain is used as well. */
4080 if (gimple_call_chain (stmt))
4082 if (!uses)
4084 uses = get_call_use_vi (stmt);
4085 make_transitive_closure_constraints (uses);
4087 make_constraint_to (uses->id, gimple_call_chain (stmt));
4090 /* Pure functions may return call-used and nonlocal memory. */
4091 if (uses)
4093 rhsc.var = uses->id;
4094 rhsc.offset = 0;
4095 rhsc.type = SCALAR;
4096 results->safe_push (rhsc);
4098 rhsc.var = nonlocal_id;
4099 rhsc.offset = 0;
4100 rhsc.type = SCALAR;
4101 results->safe_push (rhsc);
4105 /* Return the varinfo for the callee of CALL. */
4107 static varinfo_t
4108 get_fi_for_callee (gimple call)
4110 tree decl, fn = gimple_call_fn (call);
4112 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
4113 fn = OBJ_TYPE_REF_EXPR (fn);
4115 /* If we can directly resolve the function being called, do so.
4116 Otherwise, it must be some sort of indirect expression that
4117 we should still be able to handle. */
4118 decl = gimple_call_addr_fndecl (fn);
4119 if (decl)
4120 return get_vi_for_tree (decl);
4122 /* If the function is anything other than a SSA name pointer we have no
4123 clue and should be getting ANYFN (well, ANYTHING for now). */
4124 if (!fn || TREE_CODE (fn) != SSA_NAME)
4125 return get_varinfo (anything_id);
4127 if (SSA_NAME_IS_DEFAULT_DEF (fn)
4128 && (TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
4129 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL))
4130 fn = SSA_NAME_VAR (fn);
4132 return get_vi_for_tree (fn);
4135 /* Create constraints for the builtin call T. Return true if the call
4136 was handled, otherwise false. */
4138 static bool
4139 find_func_aliases_for_builtin_call (gimple t)
4141 tree fndecl = gimple_call_fndecl (t);
4142 vec<ce_s> lhsc = vNULL;
4143 vec<ce_s> rhsc = vNULL;
4144 varinfo_t fi;
4146 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4147 /* ??? All builtins that are handled here need to be handled
4148 in the alias-oracle query functions explicitly! */
4149 switch (DECL_FUNCTION_CODE (fndecl))
4151 /* All the following functions return a pointer to the same object
4152 as their first argument points to. The functions do not add
4153 to the ESCAPED solution. The functions make the first argument
4154 pointed to memory point to what the second argument pointed to
4155 memory points to. */
4156 case BUILT_IN_STRCPY:
4157 case BUILT_IN_STRNCPY:
4158 case BUILT_IN_BCOPY:
4159 case BUILT_IN_MEMCPY:
4160 case BUILT_IN_MEMMOVE:
4161 case BUILT_IN_MEMPCPY:
4162 case BUILT_IN_STPCPY:
4163 case BUILT_IN_STPNCPY:
4164 case BUILT_IN_STRCAT:
4165 case BUILT_IN_STRNCAT:
4166 case BUILT_IN_STRCPY_CHK:
4167 case BUILT_IN_STRNCPY_CHK:
4168 case BUILT_IN_MEMCPY_CHK:
4169 case BUILT_IN_MEMMOVE_CHK:
4170 case BUILT_IN_MEMPCPY_CHK:
4171 case BUILT_IN_STPCPY_CHK:
4172 case BUILT_IN_STPNCPY_CHK:
4173 case BUILT_IN_STRCAT_CHK:
4174 case BUILT_IN_STRNCAT_CHK:
4175 case BUILT_IN_TM_MEMCPY:
4176 case BUILT_IN_TM_MEMMOVE:
4178 tree res = gimple_call_lhs (t);
4179 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4180 == BUILT_IN_BCOPY ? 1 : 0));
4181 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4182 == BUILT_IN_BCOPY ? 0 : 1));
4183 if (res != NULL_TREE)
4185 get_constraint_for (res, &lhsc);
4186 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4187 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
4188 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4189 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
4190 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK
4191 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK)
4192 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4193 else
4194 get_constraint_for (dest, &rhsc);
4195 process_all_all_constraints (lhsc, rhsc);
4196 lhsc.release ();
4197 rhsc.release ();
4199 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4200 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4201 do_deref (&lhsc);
4202 do_deref (&rhsc);
4203 process_all_all_constraints (lhsc, rhsc);
4204 lhsc.release ();
4205 rhsc.release ();
4206 return true;
4208 case BUILT_IN_MEMSET:
4209 case BUILT_IN_MEMSET_CHK:
4210 case BUILT_IN_TM_MEMSET:
4212 tree res = gimple_call_lhs (t);
4213 tree dest = gimple_call_arg (t, 0);
4214 unsigned i;
4215 ce_s *lhsp;
4216 struct constraint_expr ac;
4217 if (res != NULL_TREE)
4219 get_constraint_for (res, &lhsc);
4220 get_constraint_for (dest, &rhsc);
4221 process_all_all_constraints (lhsc, rhsc);
4222 lhsc.release ();
4223 rhsc.release ();
4225 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4226 do_deref (&lhsc);
4227 if (flag_delete_null_pointer_checks
4228 && integer_zerop (gimple_call_arg (t, 1)))
4230 ac.type = ADDRESSOF;
4231 ac.var = nothing_id;
4233 else
4235 ac.type = SCALAR;
4236 ac.var = integer_id;
4238 ac.offset = 0;
4239 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4240 process_constraint (new_constraint (*lhsp, ac));
4241 lhsc.release ();
4242 return true;
4244 case BUILT_IN_POSIX_MEMALIGN:
4246 tree ptrptr = gimple_call_arg (t, 0);
4247 get_constraint_for (ptrptr, &lhsc);
4248 do_deref (&lhsc);
4249 varinfo_t vi = make_heapvar ("HEAP");
4250 /* We are marking allocated storage local, we deal with it becoming
4251 global by escaping and setting of vars_contains_escaped_heap. */
4252 DECL_EXTERNAL (vi->decl) = 0;
4253 vi->is_global_var = 0;
4254 struct constraint_expr tmpc;
4255 tmpc.var = vi->id;
4256 tmpc.offset = 0;
4257 tmpc.type = ADDRESSOF;
4258 rhsc.safe_push (tmpc);
4259 process_all_all_constraints (lhsc, rhsc);
4260 lhsc.release ();
4261 rhsc.release ();
4262 return true;
4264 case BUILT_IN_ASSUME_ALIGNED:
4266 tree res = gimple_call_lhs (t);
4267 tree dest = gimple_call_arg (t, 0);
4268 if (res != NULL_TREE)
4270 get_constraint_for (res, &lhsc);
4271 get_constraint_for (dest, &rhsc);
4272 process_all_all_constraints (lhsc, rhsc);
4273 lhsc.release ();
4274 rhsc.release ();
4276 return true;
4278 /* All the following functions do not return pointers, do not
4279 modify the points-to sets of memory reachable from their
4280 arguments and do not add to the ESCAPED solution. */
4281 case BUILT_IN_SINCOS:
4282 case BUILT_IN_SINCOSF:
4283 case BUILT_IN_SINCOSL:
4284 case BUILT_IN_FREXP:
4285 case BUILT_IN_FREXPF:
4286 case BUILT_IN_FREXPL:
4287 case BUILT_IN_GAMMA_R:
4288 case BUILT_IN_GAMMAF_R:
4289 case BUILT_IN_GAMMAL_R:
4290 case BUILT_IN_LGAMMA_R:
4291 case BUILT_IN_LGAMMAF_R:
4292 case BUILT_IN_LGAMMAL_R:
4293 case BUILT_IN_MODF:
4294 case BUILT_IN_MODFF:
4295 case BUILT_IN_MODFL:
4296 case BUILT_IN_REMQUO:
4297 case BUILT_IN_REMQUOF:
4298 case BUILT_IN_REMQUOL:
4299 case BUILT_IN_FREE:
4300 return true;
4301 case BUILT_IN_STRDUP:
4302 case BUILT_IN_STRNDUP:
4303 if (gimple_call_lhs (t))
4305 handle_lhs_call (t, gimple_call_lhs (t), gimple_call_flags (t),
4306 vNULL, fndecl);
4307 get_constraint_for_ptr_offset (gimple_call_lhs (t),
4308 NULL_TREE, &lhsc);
4309 get_constraint_for_ptr_offset (gimple_call_arg (t, 0),
4310 NULL_TREE, &rhsc);
4311 do_deref (&lhsc);
4312 do_deref (&rhsc);
4313 process_all_all_constraints (lhsc, rhsc);
4314 lhsc.release ();
4315 rhsc.release ();
4316 return true;
4318 break;
4319 /* String / character search functions return a pointer into the
4320 source string or NULL. */
4321 case BUILT_IN_INDEX:
4322 case BUILT_IN_STRCHR:
4323 case BUILT_IN_STRRCHR:
4324 case BUILT_IN_MEMCHR:
4325 case BUILT_IN_STRSTR:
4326 case BUILT_IN_STRPBRK:
4327 if (gimple_call_lhs (t))
4329 tree src = gimple_call_arg (t, 0);
4330 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4331 constraint_expr nul;
4332 nul.var = nothing_id;
4333 nul.offset = 0;
4334 nul.type = ADDRESSOF;
4335 rhsc.safe_push (nul);
4336 get_constraint_for (gimple_call_lhs (t), &lhsc);
4337 process_all_all_constraints (lhsc, rhsc);
4338 lhsc.release ();
4339 rhsc.release ();
4341 return true;
4342 /* Trampolines are special - they set up passing the static
4343 frame. */
4344 case BUILT_IN_INIT_TRAMPOLINE:
4346 tree tramp = gimple_call_arg (t, 0);
4347 tree nfunc = gimple_call_arg (t, 1);
4348 tree frame = gimple_call_arg (t, 2);
4349 unsigned i;
4350 struct constraint_expr lhs, *rhsp;
4351 if (in_ipa_mode)
4353 varinfo_t nfi = NULL;
4354 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR);
4355 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0));
4356 if (nfi)
4358 lhs = get_function_part_constraint (nfi, fi_static_chain);
4359 get_constraint_for (frame, &rhsc);
4360 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4361 process_constraint (new_constraint (lhs, *rhsp));
4362 rhsc.release ();
4364 /* Make the frame point to the function for
4365 the trampoline adjustment call. */
4366 get_constraint_for (tramp, &lhsc);
4367 do_deref (&lhsc);
4368 get_constraint_for (nfunc, &rhsc);
4369 process_all_all_constraints (lhsc, rhsc);
4370 rhsc.release ();
4371 lhsc.release ();
4373 return true;
4376 /* Else fallthru to generic handling which will let
4377 the frame escape. */
4378 break;
4380 case BUILT_IN_ADJUST_TRAMPOLINE:
4382 tree tramp = gimple_call_arg (t, 0);
4383 tree res = gimple_call_lhs (t);
4384 if (in_ipa_mode && res)
4386 get_constraint_for (res, &lhsc);
4387 get_constraint_for (tramp, &rhsc);
4388 do_deref (&rhsc);
4389 process_all_all_constraints (lhsc, rhsc);
4390 rhsc.release ();
4391 lhsc.release ();
4393 return true;
4395 CASE_BUILT_IN_TM_STORE (1):
4396 CASE_BUILT_IN_TM_STORE (2):
4397 CASE_BUILT_IN_TM_STORE (4):
4398 CASE_BUILT_IN_TM_STORE (8):
4399 CASE_BUILT_IN_TM_STORE (FLOAT):
4400 CASE_BUILT_IN_TM_STORE (DOUBLE):
4401 CASE_BUILT_IN_TM_STORE (LDOUBLE):
4402 CASE_BUILT_IN_TM_STORE (M64):
4403 CASE_BUILT_IN_TM_STORE (M128):
4404 CASE_BUILT_IN_TM_STORE (M256):
4406 tree addr = gimple_call_arg (t, 0);
4407 tree src = gimple_call_arg (t, 1);
4409 get_constraint_for (addr, &lhsc);
4410 do_deref (&lhsc);
4411 get_constraint_for (src, &rhsc);
4412 process_all_all_constraints (lhsc, rhsc);
4413 lhsc.release ();
4414 rhsc.release ();
4415 return true;
4417 CASE_BUILT_IN_TM_LOAD (1):
4418 CASE_BUILT_IN_TM_LOAD (2):
4419 CASE_BUILT_IN_TM_LOAD (4):
4420 CASE_BUILT_IN_TM_LOAD (8):
4421 CASE_BUILT_IN_TM_LOAD (FLOAT):
4422 CASE_BUILT_IN_TM_LOAD (DOUBLE):
4423 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
4424 CASE_BUILT_IN_TM_LOAD (M64):
4425 CASE_BUILT_IN_TM_LOAD (M128):
4426 CASE_BUILT_IN_TM_LOAD (M256):
4428 tree dest = gimple_call_lhs (t);
4429 tree addr = gimple_call_arg (t, 0);
4431 get_constraint_for (dest, &lhsc);
4432 get_constraint_for (addr, &rhsc);
4433 do_deref (&rhsc);
4434 process_all_all_constraints (lhsc, rhsc);
4435 lhsc.release ();
4436 rhsc.release ();
4437 return true;
4439 /* Variadic argument handling needs to be handled in IPA
4440 mode as well. */
4441 case BUILT_IN_VA_START:
4443 tree valist = gimple_call_arg (t, 0);
4444 struct constraint_expr rhs, *lhsp;
4445 unsigned i;
4446 get_constraint_for (valist, &lhsc);
4447 do_deref (&lhsc);
4448 /* The va_list gets access to pointers in variadic
4449 arguments. Which we know in the case of IPA analysis
4450 and otherwise are just all nonlocal variables. */
4451 if (in_ipa_mode)
4453 fi = lookup_vi_for_tree (cfun->decl);
4454 rhs = get_function_part_constraint (fi, ~0);
4455 rhs.type = ADDRESSOF;
4457 else
4459 rhs.var = nonlocal_id;
4460 rhs.type = ADDRESSOF;
4461 rhs.offset = 0;
4463 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4464 process_constraint (new_constraint (*lhsp, rhs));
4465 lhsc.release ();
4466 /* va_list is clobbered. */
4467 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4468 return true;
4470 /* va_end doesn't have any effect that matters. */
4471 case BUILT_IN_VA_END:
4472 return true;
4473 /* Alternate return. Simply give up for now. */
4474 case BUILT_IN_RETURN:
4476 fi = NULL;
4477 if (!in_ipa_mode
4478 || !(fi = get_vi_for_tree (cfun->decl)))
4479 make_constraint_from (get_varinfo (escaped_id), anything_id);
4480 else if (in_ipa_mode
4481 && fi != NULL)
4483 struct constraint_expr lhs, rhs;
4484 lhs = get_function_part_constraint (fi, fi_result);
4485 rhs.var = anything_id;
4486 rhs.offset = 0;
4487 rhs.type = SCALAR;
4488 process_constraint (new_constraint (lhs, rhs));
4490 return true;
4492 /* printf-style functions may have hooks to set pointers to
4493 point to somewhere into the generated string. Leave them
4494 for a later exercise... */
4495 default:
4496 /* Fallthru to general call handling. */;
4499 return false;
4502 /* Create constraints for the call T. */
4504 static void
4505 find_func_aliases_for_call (gimple t)
4507 tree fndecl = gimple_call_fndecl (t);
4508 vec<ce_s> lhsc = vNULL;
4509 vec<ce_s> rhsc = vNULL;
4510 varinfo_t fi;
4512 if (fndecl != NULL_TREE
4513 && DECL_BUILT_IN (fndecl)
4514 && find_func_aliases_for_builtin_call (t))
4515 return;
4517 fi = get_fi_for_callee (t);
4518 if (!in_ipa_mode
4519 || (fndecl && !fi->is_fn_info))
4521 vec<ce_s> rhsc = vNULL;
4522 int flags = gimple_call_flags (t);
4524 /* Const functions can return their arguments and addresses
4525 of global memory but not of escaped memory. */
4526 if (flags & (ECF_CONST|ECF_NOVOPS))
4528 if (gimple_call_lhs (t))
4529 handle_const_call (t, &rhsc);
4531 /* Pure functions can return addresses in and of memory
4532 reachable from their arguments, but they are not an escape
4533 point for reachable memory of their arguments. */
4534 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4535 handle_pure_call (t, &rhsc);
4536 else
4537 handle_rhs_call (t, &rhsc);
4538 if (gimple_call_lhs (t))
4539 handle_lhs_call (t, gimple_call_lhs (t), flags, rhsc, fndecl);
4540 rhsc.release ();
4542 else
4544 tree lhsop;
4545 unsigned j;
4547 /* Assign all the passed arguments to the appropriate incoming
4548 parameters of the function. */
4549 for (j = 0; j < gimple_call_num_args (t); j++)
4551 struct constraint_expr lhs ;
4552 struct constraint_expr *rhsp;
4553 tree arg = gimple_call_arg (t, j);
4555 get_constraint_for_rhs (arg, &rhsc);
4556 lhs = get_function_part_constraint (fi, fi_parm_base + j);
4557 while (rhsc.length () != 0)
4559 rhsp = &rhsc.last ();
4560 process_constraint (new_constraint (lhs, *rhsp));
4561 rhsc.pop ();
4565 /* If we are returning a value, assign it to the result. */
4566 lhsop = gimple_call_lhs (t);
4567 if (lhsop)
4569 struct constraint_expr rhs;
4570 struct constraint_expr *lhsp;
4572 get_constraint_for (lhsop, &lhsc);
4573 rhs = get_function_part_constraint (fi, fi_result);
4574 if (fndecl
4575 && DECL_RESULT (fndecl)
4576 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4578 vec<ce_s> tem = vNULL;
4579 tem.safe_push (rhs);
4580 do_deref (&tem);
4581 rhs = tem[0];
4582 tem.release ();
4584 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4585 process_constraint (new_constraint (*lhsp, rhs));
4588 /* If we pass the result decl by reference, honor that. */
4589 if (lhsop
4590 && fndecl
4591 && DECL_RESULT (fndecl)
4592 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4594 struct constraint_expr lhs;
4595 struct constraint_expr *rhsp;
4597 get_constraint_for_address_of (lhsop, &rhsc);
4598 lhs = get_function_part_constraint (fi, fi_result);
4599 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4600 process_constraint (new_constraint (lhs, *rhsp));
4601 rhsc.release ();
4604 /* If we use a static chain, pass it along. */
4605 if (gimple_call_chain (t))
4607 struct constraint_expr lhs;
4608 struct constraint_expr *rhsp;
4610 get_constraint_for (gimple_call_chain (t), &rhsc);
4611 lhs = get_function_part_constraint (fi, fi_static_chain);
4612 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4613 process_constraint (new_constraint (lhs, *rhsp));
4618 /* Walk statement T setting up aliasing constraints according to the
4619 references found in T. This function is the main part of the
4620 constraint builder. AI points to auxiliary alias information used
4621 when building alias sets and computing alias grouping heuristics. */
4623 static void
4624 find_func_aliases (gimple origt)
4626 gimple t = origt;
4627 vec<ce_s> lhsc = vNULL;
4628 vec<ce_s> rhsc = vNULL;
4629 struct constraint_expr *c;
4630 varinfo_t fi;
4632 /* Now build constraints expressions. */
4633 if (gimple_code (t) == GIMPLE_PHI)
4635 size_t i;
4636 unsigned int j;
4638 /* For a phi node, assign all the arguments to
4639 the result. */
4640 get_constraint_for (gimple_phi_result (t), &lhsc);
4641 for (i = 0; i < gimple_phi_num_args (t); i++)
4643 tree strippedrhs = PHI_ARG_DEF (t, i);
4645 STRIP_NOPS (strippedrhs);
4646 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
4648 FOR_EACH_VEC_ELT (lhsc, j, c)
4650 struct constraint_expr *c2;
4651 while (rhsc.length () > 0)
4653 c2 = &rhsc.last ();
4654 process_constraint (new_constraint (*c, *c2));
4655 rhsc.pop ();
4660 /* In IPA mode, we need to generate constraints to pass call
4661 arguments through their calls. There are two cases,
4662 either a GIMPLE_CALL returning a value, or just a plain
4663 GIMPLE_CALL when we are not.
4665 In non-ipa mode, we need to generate constraints for each
4666 pointer passed by address. */
4667 else if (is_gimple_call (t))
4668 find_func_aliases_for_call (t);
4670 /* Otherwise, just a regular assignment statement. Only care about
4671 operations with pointer result, others are dealt with as escape
4672 points if they have pointer operands. */
4673 else if (is_gimple_assign (t))
4675 /* Otherwise, just a regular assignment statement. */
4676 tree lhsop = gimple_assign_lhs (t);
4677 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
4679 if (rhsop && TREE_CLOBBER_P (rhsop))
4680 /* Ignore clobbers, they don't actually store anything into
4681 the LHS. */
4683 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
4684 do_structure_copy (lhsop, rhsop);
4685 else
4687 enum tree_code code = gimple_assign_rhs_code (t);
4689 get_constraint_for (lhsop, &lhsc);
4691 if (FLOAT_TYPE_P (TREE_TYPE (lhsop)))
4692 /* If the operation produces a floating point result then
4693 assume the value is not produced to transfer a pointer. */
4695 else if (code == POINTER_PLUS_EXPR)
4696 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4697 gimple_assign_rhs2 (t), &rhsc);
4698 else if (code == BIT_AND_EXPR
4699 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
4701 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4702 the pointer. Handle it by offsetting it by UNKNOWN. */
4703 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4704 NULL_TREE, &rhsc);
4706 else if ((CONVERT_EXPR_CODE_P (code)
4707 && !(POINTER_TYPE_P (gimple_expr_type (t))
4708 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
4709 || gimple_assign_single_p (t))
4710 get_constraint_for_rhs (rhsop, &rhsc);
4711 else if (code == COND_EXPR)
4713 /* The result is a merge of both COND_EXPR arms. */
4714 vec<ce_s> tmp = vNULL;
4715 struct constraint_expr *rhsp;
4716 unsigned i;
4717 get_constraint_for_rhs (gimple_assign_rhs2 (t), &rhsc);
4718 get_constraint_for_rhs (gimple_assign_rhs3 (t), &tmp);
4719 FOR_EACH_VEC_ELT (tmp, i, rhsp)
4720 rhsc.safe_push (*rhsp);
4721 tmp.release ();
4723 else if (truth_value_p (code))
4724 /* Truth value results are not pointer (parts). Or at least
4725 very very unreasonable obfuscation of a part. */
4727 else
4729 /* All other operations are merges. */
4730 vec<ce_s> tmp = vNULL;
4731 struct constraint_expr *rhsp;
4732 unsigned i, j;
4733 get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc);
4734 for (i = 2; i < gimple_num_ops (t); ++i)
4736 get_constraint_for_rhs (gimple_op (t, i), &tmp);
4737 FOR_EACH_VEC_ELT (tmp, j, rhsp)
4738 rhsc.safe_push (*rhsp);
4739 tmp.truncate (0);
4741 tmp.release ();
4743 process_all_all_constraints (lhsc, rhsc);
4745 /* If there is a store to a global variable the rhs escapes. */
4746 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
4747 && DECL_P (lhsop)
4748 && is_global_var (lhsop)
4749 && (!in_ipa_mode
4750 || DECL_EXTERNAL (lhsop) || TREE_PUBLIC (lhsop)))
4751 make_escape_constraint (rhsop);
4753 /* Handle escapes through return. */
4754 else if (gimple_code (t) == GIMPLE_RETURN
4755 && gimple_return_retval (t) != NULL_TREE)
4757 fi = NULL;
4758 if (!in_ipa_mode
4759 || !(fi = get_vi_for_tree (cfun->decl)))
4760 make_escape_constraint (gimple_return_retval (t));
4761 else if (in_ipa_mode
4762 && fi != NULL)
4764 struct constraint_expr lhs ;
4765 struct constraint_expr *rhsp;
4766 unsigned i;
4768 lhs = get_function_part_constraint (fi, fi_result);
4769 get_constraint_for_rhs (gimple_return_retval (t), &rhsc);
4770 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4771 process_constraint (new_constraint (lhs, *rhsp));
4774 /* Handle asms conservatively by adding escape constraints to everything. */
4775 else if (gimple_code (t) == GIMPLE_ASM)
4777 unsigned i, noutputs;
4778 const char **oconstraints;
4779 const char *constraint;
4780 bool allows_mem, allows_reg, is_inout;
4782 noutputs = gimple_asm_noutputs (t);
4783 oconstraints = XALLOCAVEC (const char *, noutputs);
4785 for (i = 0; i < noutputs; ++i)
4787 tree link = gimple_asm_output_op (t, i);
4788 tree op = TREE_VALUE (link);
4790 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4791 oconstraints[i] = constraint;
4792 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4793 &allows_reg, &is_inout);
4795 /* A memory constraint makes the address of the operand escape. */
4796 if (!allows_reg && allows_mem)
4797 make_escape_constraint (build_fold_addr_expr (op));
4799 /* The asm may read global memory, so outputs may point to
4800 any global memory. */
4801 if (op)
4803 vec<ce_s> lhsc = vNULL;
4804 struct constraint_expr rhsc, *lhsp;
4805 unsigned j;
4806 get_constraint_for (op, &lhsc);
4807 rhsc.var = nonlocal_id;
4808 rhsc.offset = 0;
4809 rhsc.type = SCALAR;
4810 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4811 process_constraint (new_constraint (*lhsp, rhsc));
4812 lhsc.release ();
4815 for (i = 0; i < gimple_asm_ninputs (t); ++i)
4817 tree link = gimple_asm_input_op (t, i);
4818 tree op = TREE_VALUE (link);
4820 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4822 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4823 &allows_mem, &allows_reg);
4825 /* A memory constraint makes the address of the operand escape. */
4826 if (!allows_reg && allows_mem)
4827 make_escape_constraint (build_fold_addr_expr (op));
4828 /* Strictly we'd only need the constraint to ESCAPED if
4829 the asm clobbers memory, otherwise using something
4830 along the lines of per-call clobbers/uses would be enough. */
4831 else if (op)
4832 make_escape_constraint (op);
4836 rhsc.release ();
4837 lhsc.release ();
4841 /* Create a constraint adding to the clobber set of FI the memory
4842 pointed to by PTR. */
4844 static void
4845 process_ipa_clobber (varinfo_t fi, tree ptr)
4847 vec<ce_s> ptrc = vNULL;
4848 struct constraint_expr *c, lhs;
4849 unsigned i;
4850 get_constraint_for_rhs (ptr, &ptrc);
4851 lhs = get_function_part_constraint (fi, fi_clobbers);
4852 FOR_EACH_VEC_ELT (ptrc, i, c)
4853 process_constraint (new_constraint (lhs, *c));
4854 ptrc.release ();
4857 /* Walk statement T setting up clobber and use constraints according to the
4858 references found in T. This function is a main part of the
4859 IPA constraint builder. */
4861 static void
4862 find_func_clobbers (gimple origt)
4864 gimple t = origt;
4865 vec<ce_s> lhsc = vNULL;
4866 auto_vec<ce_s> rhsc;
4867 varinfo_t fi;
4869 /* Add constraints for clobbered/used in IPA mode.
4870 We are not interested in what automatic variables are clobbered
4871 or used as we only use the information in the caller to which
4872 they do not escape. */
4873 gcc_assert (in_ipa_mode);
4875 /* If the stmt refers to memory in any way it better had a VUSE. */
4876 if (gimple_vuse (t) == NULL_TREE)
4877 return;
4879 /* We'd better have function information for the current function. */
4880 fi = lookup_vi_for_tree (cfun->decl);
4881 gcc_assert (fi != NULL);
4883 /* Account for stores in assignments and calls. */
4884 if (gimple_vdef (t) != NULL_TREE
4885 && gimple_has_lhs (t))
4887 tree lhs = gimple_get_lhs (t);
4888 tree tem = lhs;
4889 while (handled_component_p (tem))
4890 tem = TREE_OPERAND (tem, 0);
4891 if ((DECL_P (tem)
4892 && !auto_var_in_fn_p (tem, cfun->decl))
4893 || INDIRECT_REF_P (tem)
4894 || (TREE_CODE (tem) == MEM_REF
4895 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4896 && auto_var_in_fn_p
4897 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4899 struct constraint_expr lhsc, *rhsp;
4900 unsigned i;
4901 lhsc = get_function_part_constraint (fi, fi_clobbers);
4902 get_constraint_for_address_of (lhs, &rhsc);
4903 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4904 process_constraint (new_constraint (lhsc, *rhsp));
4905 rhsc.release ();
4909 /* Account for uses in assigments and returns. */
4910 if (gimple_assign_single_p (t)
4911 || (gimple_code (t) == GIMPLE_RETURN
4912 && gimple_return_retval (t) != NULL_TREE))
4914 tree rhs = (gimple_assign_single_p (t)
4915 ? gimple_assign_rhs1 (t) : gimple_return_retval (t));
4916 tree tem = rhs;
4917 while (handled_component_p (tem))
4918 tem = TREE_OPERAND (tem, 0);
4919 if ((DECL_P (tem)
4920 && !auto_var_in_fn_p (tem, cfun->decl))
4921 || INDIRECT_REF_P (tem)
4922 || (TREE_CODE (tem) == MEM_REF
4923 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4924 && auto_var_in_fn_p
4925 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4927 struct constraint_expr lhs, *rhsp;
4928 unsigned i;
4929 lhs = get_function_part_constraint (fi, fi_uses);
4930 get_constraint_for_address_of (rhs, &rhsc);
4931 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4932 process_constraint (new_constraint (lhs, *rhsp));
4933 rhsc.release ();
4937 if (is_gimple_call (t))
4939 varinfo_t cfi = NULL;
4940 tree decl = gimple_call_fndecl (t);
4941 struct constraint_expr lhs, rhs;
4942 unsigned i, j;
4944 /* For builtins we do not have separate function info. For those
4945 we do not generate escapes for we have to generate clobbers/uses. */
4946 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4947 switch (DECL_FUNCTION_CODE (decl))
4949 /* The following functions use and clobber memory pointed to
4950 by their arguments. */
4951 case BUILT_IN_STRCPY:
4952 case BUILT_IN_STRNCPY:
4953 case BUILT_IN_BCOPY:
4954 case BUILT_IN_MEMCPY:
4955 case BUILT_IN_MEMMOVE:
4956 case BUILT_IN_MEMPCPY:
4957 case BUILT_IN_STPCPY:
4958 case BUILT_IN_STPNCPY:
4959 case BUILT_IN_STRCAT:
4960 case BUILT_IN_STRNCAT:
4961 case BUILT_IN_STRCPY_CHK:
4962 case BUILT_IN_STRNCPY_CHK:
4963 case BUILT_IN_MEMCPY_CHK:
4964 case BUILT_IN_MEMMOVE_CHK:
4965 case BUILT_IN_MEMPCPY_CHK:
4966 case BUILT_IN_STPCPY_CHK:
4967 case BUILT_IN_STPNCPY_CHK:
4968 case BUILT_IN_STRCAT_CHK:
4969 case BUILT_IN_STRNCAT_CHK:
4971 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4972 == BUILT_IN_BCOPY ? 1 : 0));
4973 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4974 == BUILT_IN_BCOPY ? 0 : 1));
4975 unsigned i;
4976 struct constraint_expr *rhsp, *lhsp;
4977 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4978 lhs = get_function_part_constraint (fi, fi_clobbers);
4979 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4980 process_constraint (new_constraint (lhs, *lhsp));
4981 lhsc.release ();
4982 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4983 lhs = get_function_part_constraint (fi, fi_uses);
4984 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4985 process_constraint (new_constraint (lhs, *rhsp));
4986 rhsc.release ();
4987 return;
4989 /* The following function clobbers memory pointed to by
4990 its argument. */
4991 case BUILT_IN_MEMSET:
4992 case BUILT_IN_MEMSET_CHK:
4993 case BUILT_IN_POSIX_MEMALIGN:
4995 tree dest = gimple_call_arg (t, 0);
4996 unsigned i;
4997 ce_s *lhsp;
4998 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4999 lhs = get_function_part_constraint (fi, fi_clobbers);
5000 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
5001 process_constraint (new_constraint (lhs, *lhsp));
5002 lhsc.release ();
5003 return;
5005 /* The following functions clobber their second and third
5006 arguments. */
5007 case BUILT_IN_SINCOS:
5008 case BUILT_IN_SINCOSF:
5009 case BUILT_IN_SINCOSL:
5011 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5012 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5013 return;
5015 /* The following functions clobber their second argument. */
5016 case BUILT_IN_FREXP:
5017 case BUILT_IN_FREXPF:
5018 case BUILT_IN_FREXPL:
5019 case BUILT_IN_LGAMMA_R:
5020 case BUILT_IN_LGAMMAF_R:
5021 case BUILT_IN_LGAMMAL_R:
5022 case BUILT_IN_GAMMA_R:
5023 case BUILT_IN_GAMMAF_R:
5024 case BUILT_IN_GAMMAL_R:
5025 case BUILT_IN_MODF:
5026 case BUILT_IN_MODFF:
5027 case BUILT_IN_MODFL:
5029 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5030 return;
5032 /* The following functions clobber their third argument. */
5033 case BUILT_IN_REMQUO:
5034 case BUILT_IN_REMQUOF:
5035 case BUILT_IN_REMQUOL:
5037 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5038 return;
5040 /* The following functions neither read nor clobber memory. */
5041 case BUILT_IN_ASSUME_ALIGNED:
5042 case BUILT_IN_FREE:
5043 return;
5044 /* Trampolines are of no interest to us. */
5045 case BUILT_IN_INIT_TRAMPOLINE:
5046 case BUILT_IN_ADJUST_TRAMPOLINE:
5047 return;
5048 case BUILT_IN_VA_START:
5049 case BUILT_IN_VA_END:
5050 return;
5051 /* printf-style functions may have hooks to set pointers to
5052 point to somewhere into the generated string. Leave them
5053 for a later exercise... */
5054 default:
5055 /* Fallthru to general call handling. */;
5058 /* Parameters passed by value are used. */
5059 lhs = get_function_part_constraint (fi, fi_uses);
5060 for (i = 0; i < gimple_call_num_args (t); i++)
5062 struct constraint_expr *rhsp;
5063 tree arg = gimple_call_arg (t, i);
5065 if (TREE_CODE (arg) == SSA_NAME
5066 || is_gimple_min_invariant (arg))
5067 continue;
5069 get_constraint_for_address_of (arg, &rhsc);
5070 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5071 process_constraint (new_constraint (lhs, *rhsp));
5072 rhsc.release ();
5075 /* Build constraints for propagating clobbers/uses along the
5076 callgraph edges. */
5077 cfi = get_fi_for_callee (t);
5078 if (cfi->id == anything_id)
5080 if (gimple_vdef (t))
5081 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5082 anything_id);
5083 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5084 anything_id);
5085 return;
5088 /* For callees without function info (that's external functions),
5089 ESCAPED is clobbered and used. */
5090 if (gimple_call_fndecl (t)
5091 && !cfi->is_fn_info)
5093 varinfo_t vi;
5095 if (gimple_vdef (t))
5096 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5097 escaped_id);
5098 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
5100 /* Also honor the call statement use/clobber info. */
5101 if ((vi = lookup_call_clobber_vi (t)) != NULL)
5102 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5103 vi->id);
5104 if ((vi = lookup_call_use_vi (t)) != NULL)
5105 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
5106 vi->id);
5107 return;
5110 /* Otherwise the caller clobbers and uses what the callee does.
5111 ??? This should use a new complex constraint that filters
5112 local variables of the callee. */
5113 if (gimple_vdef (t))
5115 lhs = get_function_part_constraint (fi, fi_clobbers);
5116 rhs = get_function_part_constraint (cfi, fi_clobbers);
5117 process_constraint (new_constraint (lhs, rhs));
5119 lhs = get_function_part_constraint (fi, fi_uses);
5120 rhs = get_function_part_constraint (cfi, fi_uses);
5121 process_constraint (new_constraint (lhs, rhs));
5123 else if (gimple_code (t) == GIMPLE_ASM)
5125 /* ??? Ick. We can do better. */
5126 if (gimple_vdef (t))
5127 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5128 anything_id);
5129 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5130 anything_id);
5135 /* Find the first varinfo in the same variable as START that overlaps with
5136 OFFSET. Return NULL if we can't find one. */
5138 static varinfo_t
5139 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
5141 /* If the offset is outside of the variable, bail out. */
5142 if (offset >= start->fullsize)
5143 return NULL;
5145 /* If we cannot reach offset from start, lookup the first field
5146 and start from there. */
5147 if (start->offset > offset)
5148 start = get_varinfo (start->head);
5150 while (start)
5152 /* We may not find a variable in the field list with the actual
5153 offset when when we have glommed a structure to a variable.
5154 In that case, however, offset should still be within the size
5155 of the variable. */
5156 if (offset >= start->offset
5157 && (offset - start->offset) < start->size)
5158 return start;
5160 start = vi_next (start);
5163 return NULL;
5166 /* Find the first varinfo in the same variable as START that overlaps with
5167 OFFSET. If there is no such varinfo the varinfo directly preceding
5168 OFFSET is returned. */
5170 static varinfo_t
5171 first_or_preceding_vi_for_offset (varinfo_t start,
5172 unsigned HOST_WIDE_INT offset)
5174 /* If we cannot reach offset from start, lookup the first field
5175 and start from there. */
5176 if (start->offset > offset)
5177 start = get_varinfo (start->head);
5179 /* We may not find a variable in the field list with the actual
5180 offset when when we have glommed a structure to a variable.
5181 In that case, however, offset should still be within the size
5182 of the variable.
5183 If we got beyond the offset we look for return the field
5184 directly preceding offset which may be the last field. */
5185 while (start->next
5186 && offset >= start->offset
5187 && !((offset - start->offset) < start->size))
5188 start = vi_next (start);
5190 return start;
5194 /* This structure is used during pushing fields onto the fieldstack
5195 to track the offset of the field, since bitpos_of_field gives it
5196 relative to its immediate containing type, and we want it relative
5197 to the ultimate containing object. */
5199 struct fieldoff
5201 /* Offset from the base of the base containing object to this field. */
5202 HOST_WIDE_INT offset;
5204 /* Size, in bits, of the field. */
5205 unsigned HOST_WIDE_INT size;
5207 unsigned has_unknown_size : 1;
5209 unsigned must_have_pointers : 1;
5211 unsigned may_have_pointers : 1;
5213 unsigned only_restrict_pointers : 1;
5215 typedef struct fieldoff fieldoff_s;
5218 /* qsort comparison function for two fieldoff's PA and PB */
5220 static int
5221 fieldoff_compare (const void *pa, const void *pb)
5223 const fieldoff_s *foa = (const fieldoff_s *)pa;
5224 const fieldoff_s *fob = (const fieldoff_s *)pb;
5225 unsigned HOST_WIDE_INT foasize, fobsize;
5227 if (foa->offset < fob->offset)
5228 return -1;
5229 else if (foa->offset > fob->offset)
5230 return 1;
5232 foasize = foa->size;
5233 fobsize = fob->size;
5234 if (foasize < fobsize)
5235 return -1;
5236 else if (foasize > fobsize)
5237 return 1;
5238 return 0;
5241 /* Sort a fieldstack according to the field offset and sizes. */
5242 static void
5243 sort_fieldstack (vec<fieldoff_s> fieldstack)
5245 fieldstack.qsort (fieldoff_compare);
5248 /* Return true if T is a type that can have subvars. */
5250 static inline bool
5251 type_can_have_subvars (const_tree t)
5253 /* Aggregates without overlapping fields can have subvars. */
5254 return TREE_CODE (t) == RECORD_TYPE;
5257 /* Return true if V is a tree that we can have subvars for.
5258 Normally, this is any aggregate type. Also complex
5259 types which are not gimple registers can have subvars. */
5261 static inline bool
5262 var_can_have_subvars (const_tree v)
5264 /* Volatile variables should never have subvars. */
5265 if (TREE_THIS_VOLATILE (v))
5266 return false;
5268 /* Non decls or memory tags can never have subvars. */
5269 if (!DECL_P (v))
5270 return false;
5272 return type_can_have_subvars (TREE_TYPE (v));
5275 /* Return true if T is a type that does contain pointers. */
5277 static bool
5278 type_must_have_pointers (tree type)
5280 if (POINTER_TYPE_P (type))
5281 return true;
5283 if (TREE_CODE (type) == ARRAY_TYPE)
5284 return type_must_have_pointers (TREE_TYPE (type));
5286 /* A function or method can have pointers as arguments, so track
5287 those separately. */
5288 if (TREE_CODE (type) == FUNCTION_TYPE
5289 || TREE_CODE (type) == METHOD_TYPE)
5290 return true;
5292 return false;
5295 static bool
5296 field_must_have_pointers (tree t)
5298 return type_must_have_pointers (TREE_TYPE (t));
5301 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5302 the fields of TYPE onto fieldstack, recording their offsets along
5303 the way.
5305 OFFSET is used to keep track of the offset in this entire
5306 structure, rather than just the immediately containing structure.
5307 Returns false if the caller is supposed to handle the field we
5308 recursed for. */
5310 static bool
5311 push_fields_onto_fieldstack (tree type, vec<fieldoff_s> *fieldstack,
5312 HOST_WIDE_INT offset)
5314 tree field;
5315 bool empty_p = true;
5317 if (TREE_CODE (type) != RECORD_TYPE)
5318 return false;
5320 /* If the vector of fields is growing too big, bail out early.
5321 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5322 sure this fails. */
5323 if (fieldstack->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5324 return false;
5326 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5327 if (TREE_CODE (field) == FIELD_DECL)
5329 bool push = false;
5330 HOST_WIDE_INT foff = bitpos_of_field (field);
5332 if (!var_can_have_subvars (field)
5333 || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
5334 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5335 push = true;
5336 else if (!push_fields_onto_fieldstack
5337 (TREE_TYPE (field), fieldstack, offset + foff)
5338 && (DECL_SIZE (field)
5339 && !integer_zerop (DECL_SIZE (field))))
5340 /* Empty structures may have actual size, like in C++. So
5341 see if we didn't push any subfields and the size is
5342 nonzero, push the field onto the stack. */
5343 push = true;
5345 if (push)
5347 fieldoff_s *pair = NULL;
5348 bool has_unknown_size = false;
5349 bool must_have_pointers_p;
5351 if (!fieldstack->is_empty ())
5352 pair = &fieldstack->last ();
5354 /* If there isn't anything at offset zero, create sth. */
5355 if (!pair
5356 && offset + foff != 0)
5358 fieldoff_s e = {0, offset + foff, false, false, false, false};
5359 pair = fieldstack->safe_push (e);
5362 if (!DECL_SIZE (field)
5363 || !tree_fits_uhwi_p (DECL_SIZE (field)))
5364 has_unknown_size = true;
5366 /* If adjacent fields do not contain pointers merge them. */
5367 must_have_pointers_p = field_must_have_pointers (field);
5368 if (pair
5369 && !has_unknown_size
5370 && !must_have_pointers_p
5371 && !pair->must_have_pointers
5372 && !pair->has_unknown_size
5373 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5375 pair->size += tree_to_uhwi (DECL_SIZE (field));
5377 else
5379 fieldoff_s e;
5380 e.offset = offset + foff;
5381 e.has_unknown_size = has_unknown_size;
5382 if (!has_unknown_size)
5383 e.size = tree_to_uhwi (DECL_SIZE (field));
5384 else
5385 e.size = -1;
5386 e.must_have_pointers = must_have_pointers_p;
5387 e.may_have_pointers = true;
5388 e.only_restrict_pointers
5389 = (!has_unknown_size
5390 && POINTER_TYPE_P (TREE_TYPE (field))
5391 && TYPE_RESTRICT (TREE_TYPE (field)));
5392 fieldstack->safe_push (e);
5396 empty_p = false;
5399 return !empty_p;
5402 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5403 if it is a varargs function. */
5405 static unsigned int
5406 count_num_arguments (tree decl, bool *is_varargs)
5408 unsigned int num = 0;
5409 tree t;
5411 /* Capture named arguments for K&R functions. They do not
5412 have a prototype and thus no TYPE_ARG_TYPES. */
5413 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5414 ++num;
5416 /* Check if the function has variadic arguments. */
5417 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5418 if (TREE_VALUE (t) == void_type_node)
5419 break;
5420 if (!t)
5421 *is_varargs = true;
5423 return num;
5426 /* Creation function node for DECL, using NAME, and return the index
5427 of the variable we've created for the function. */
5429 static varinfo_t
5430 create_function_info_for (tree decl, const char *name)
5432 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5433 varinfo_t vi, prev_vi;
5434 tree arg;
5435 unsigned int i;
5436 bool is_varargs = false;
5437 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5439 /* Create the variable info. */
5441 vi = new_var_info (decl, name);
5442 vi->offset = 0;
5443 vi->size = 1;
5444 vi->fullsize = fi_parm_base + num_args;
5445 vi->is_fn_info = 1;
5446 vi->may_have_pointers = false;
5447 if (is_varargs)
5448 vi->fullsize = ~0;
5449 insert_vi_for_tree (vi->decl, vi);
5451 prev_vi = vi;
5453 /* Create a variable for things the function clobbers and one for
5454 things the function uses. */
5456 varinfo_t clobbervi, usevi;
5457 const char *newname;
5458 char *tempname;
5460 asprintf (&tempname, "%s.clobber", name);
5461 newname = ggc_strdup (tempname);
5462 free (tempname);
5464 clobbervi = new_var_info (NULL, newname);
5465 clobbervi->offset = fi_clobbers;
5466 clobbervi->size = 1;
5467 clobbervi->fullsize = vi->fullsize;
5468 clobbervi->is_full_var = true;
5469 clobbervi->is_global_var = false;
5470 gcc_assert (prev_vi->offset < clobbervi->offset);
5471 prev_vi->next = clobbervi->id;
5472 prev_vi = clobbervi;
5474 asprintf (&tempname, "%s.use", name);
5475 newname = ggc_strdup (tempname);
5476 free (tempname);
5478 usevi = new_var_info (NULL, newname);
5479 usevi->offset = fi_uses;
5480 usevi->size = 1;
5481 usevi->fullsize = vi->fullsize;
5482 usevi->is_full_var = true;
5483 usevi->is_global_var = false;
5484 gcc_assert (prev_vi->offset < usevi->offset);
5485 prev_vi->next = usevi->id;
5486 prev_vi = usevi;
5489 /* And one for the static chain. */
5490 if (fn->static_chain_decl != NULL_TREE)
5492 varinfo_t chainvi;
5493 const char *newname;
5494 char *tempname;
5496 asprintf (&tempname, "%s.chain", name);
5497 newname = ggc_strdup (tempname);
5498 free (tempname);
5500 chainvi = new_var_info (fn->static_chain_decl, newname);
5501 chainvi->offset = fi_static_chain;
5502 chainvi->size = 1;
5503 chainvi->fullsize = vi->fullsize;
5504 chainvi->is_full_var = true;
5505 chainvi->is_global_var = false;
5506 gcc_assert (prev_vi->offset < chainvi->offset);
5507 prev_vi->next = chainvi->id;
5508 prev_vi = chainvi;
5509 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5512 /* Create a variable for the return var. */
5513 if (DECL_RESULT (decl) != NULL
5514 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5516 varinfo_t resultvi;
5517 const char *newname;
5518 char *tempname;
5519 tree resultdecl = decl;
5521 if (DECL_RESULT (decl))
5522 resultdecl = DECL_RESULT (decl);
5524 asprintf (&tempname, "%s.result", name);
5525 newname = ggc_strdup (tempname);
5526 free (tempname);
5528 resultvi = new_var_info (resultdecl, newname);
5529 resultvi->offset = fi_result;
5530 resultvi->size = 1;
5531 resultvi->fullsize = vi->fullsize;
5532 resultvi->is_full_var = true;
5533 if (DECL_RESULT (decl))
5534 resultvi->may_have_pointers = true;
5535 gcc_assert (prev_vi->offset < resultvi->offset);
5536 prev_vi->next = resultvi->id;
5537 prev_vi = resultvi;
5538 if (DECL_RESULT (decl))
5539 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
5542 /* Set up variables for each argument. */
5543 arg = DECL_ARGUMENTS (decl);
5544 for (i = 0; i < num_args; i++)
5546 varinfo_t argvi;
5547 const char *newname;
5548 char *tempname;
5549 tree argdecl = decl;
5551 if (arg)
5552 argdecl = arg;
5554 asprintf (&tempname, "%s.arg%d", name, i);
5555 newname = ggc_strdup (tempname);
5556 free (tempname);
5558 argvi = new_var_info (argdecl, newname);
5559 argvi->offset = fi_parm_base + i;
5560 argvi->size = 1;
5561 argvi->is_full_var = true;
5562 argvi->fullsize = vi->fullsize;
5563 if (arg)
5564 argvi->may_have_pointers = true;
5565 gcc_assert (prev_vi->offset < argvi->offset);
5566 prev_vi->next = argvi->id;
5567 prev_vi = argvi;
5568 if (arg)
5570 insert_vi_for_tree (arg, argvi);
5571 arg = DECL_CHAIN (arg);
5575 /* Add one representative for all further args. */
5576 if (is_varargs)
5578 varinfo_t argvi;
5579 const char *newname;
5580 char *tempname;
5581 tree decl;
5583 asprintf (&tempname, "%s.varargs", name);
5584 newname = ggc_strdup (tempname);
5585 free (tempname);
5587 /* We need sth that can be pointed to for va_start. */
5588 decl = build_fake_var_decl (ptr_type_node);
5590 argvi = new_var_info (decl, newname);
5591 argvi->offset = fi_parm_base + num_args;
5592 argvi->size = ~0;
5593 argvi->is_full_var = true;
5594 argvi->is_heap_var = true;
5595 argvi->fullsize = vi->fullsize;
5596 gcc_assert (prev_vi->offset < argvi->offset);
5597 prev_vi->next = argvi->id;
5598 prev_vi = argvi;
5601 return vi;
5605 /* Return true if FIELDSTACK contains fields that overlap.
5606 FIELDSTACK is assumed to be sorted by offset. */
5608 static bool
5609 check_for_overlaps (vec<fieldoff_s> fieldstack)
5611 fieldoff_s *fo = NULL;
5612 unsigned int i;
5613 HOST_WIDE_INT lastoffset = -1;
5615 FOR_EACH_VEC_ELT (fieldstack, i, fo)
5617 if (fo->offset == lastoffset)
5618 return true;
5619 lastoffset = fo->offset;
5621 return false;
5624 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5625 This will also create any varinfo structures necessary for fields
5626 of DECL. */
5628 static varinfo_t
5629 create_variable_info_for_1 (tree decl, const char *name)
5631 varinfo_t vi, newvi;
5632 tree decl_type = TREE_TYPE (decl);
5633 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
5634 auto_vec<fieldoff_s> fieldstack;
5635 fieldoff_s *fo;
5636 unsigned int i;
5638 if (!declsize
5639 || !tree_fits_uhwi_p (declsize))
5641 vi = new_var_info (decl, name);
5642 vi->offset = 0;
5643 vi->size = ~0;
5644 vi->fullsize = ~0;
5645 vi->is_unknown_size_var = true;
5646 vi->is_full_var = true;
5647 vi->may_have_pointers = true;
5648 return vi;
5651 /* Collect field information. */
5652 if (use_field_sensitive
5653 && var_can_have_subvars (decl)
5654 /* ??? Force us to not use subfields for global initializers
5655 in IPA mode. Else we'd have to parse arbitrary initializers. */
5656 && !(in_ipa_mode
5657 && is_global_var (decl)
5658 && DECL_INITIAL (decl)))
5660 fieldoff_s *fo = NULL;
5661 bool notokay = false;
5662 unsigned int i;
5664 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
5666 for (i = 0; !notokay && fieldstack.iterate (i, &fo); i++)
5667 if (fo->has_unknown_size
5668 || fo->offset < 0)
5670 notokay = true;
5671 break;
5674 /* We can't sort them if we have a field with a variable sized type,
5675 which will make notokay = true. In that case, we are going to return
5676 without creating varinfos for the fields anyway, so sorting them is a
5677 waste to boot. */
5678 if (!notokay)
5680 sort_fieldstack (fieldstack);
5681 /* Due to some C++ FE issues, like PR 22488, we might end up
5682 what appear to be overlapping fields even though they,
5683 in reality, do not overlap. Until the C++ FE is fixed,
5684 we will simply disable field-sensitivity for these cases. */
5685 notokay = check_for_overlaps (fieldstack);
5688 if (notokay)
5689 fieldstack.release ();
5692 /* If we didn't end up collecting sub-variables create a full
5693 variable for the decl. */
5694 if (fieldstack.length () <= 1
5695 || fieldstack.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5697 vi = new_var_info (decl, name);
5698 vi->offset = 0;
5699 vi->may_have_pointers = true;
5700 vi->fullsize = tree_to_uhwi (declsize);
5701 vi->size = vi->fullsize;
5702 vi->is_full_var = true;
5703 fieldstack.release ();
5704 return vi;
5707 vi = new_var_info (decl, name);
5708 vi->fullsize = tree_to_uhwi (declsize);
5709 for (i = 0, newvi = vi;
5710 fieldstack.iterate (i, &fo);
5711 ++i, newvi = vi_next (newvi))
5713 const char *newname = "NULL";
5714 char *tempname;
5716 if (dump_file)
5718 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC
5719 "+" HOST_WIDE_INT_PRINT_DEC, name, fo->offset, fo->size);
5720 newname = ggc_strdup (tempname);
5721 free (tempname);
5723 newvi->name = newname;
5724 newvi->offset = fo->offset;
5725 newvi->size = fo->size;
5726 newvi->fullsize = vi->fullsize;
5727 newvi->may_have_pointers = fo->may_have_pointers;
5728 newvi->only_restrict_pointers = fo->only_restrict_pointers;
5729 if (i + 1 < fieldstack.length ())
5731 varinfo_t tem = new_var_info (decl, name);
5732 newvi->next = tem->id;
5733 tem->head = vi->id;
5737 return vi;
5740 static unsigned int
5741 create_variable_info_for (tree decl, const char *name)
5743 varinfo_t vi = create_variable_info_for_1 (decl, name);
5744 unsigned int id = vi->id;
5746 insert_vi_for_tree (decl, vi);
5748 if (TREE_CODE (decl) != VAR_DECL)
5749 return id;
5751 /* Create initial constraints for globals. */
5752 for (; vi; vi = vi_next (vi))
5754 if (!vi->may_have_pointers
5755 || !vi->is_global_var)
5756 continue;
5758 /* Mark global restrict qualified pointers. */
5759 if ((POINTER_TYPE_P (TREE_TYPE (decl))
5760 && TYPE_RESTRICT (TREE_TYPE (decl)))
5761 || vi->only_restrict_pointers)
5763 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5764 continue;
5767 /* In non-IPA mode the initializer from nonlocal is all we need. */
5768 if (!in_ipa_mode
5769 || DECL_HARD_REGISTER (decl))
5770 make_copy_constraint (vi, nonlocal_id);
5772 /* In IPA mode parse the initializer and generate proper constraints
5773 for it. */
5774 else
5776 varpool_node *vnode = varpool_get_node (decl);
5778 /* For escaped variables initialize them from nonlocal. */
5779 if (!varpool_all_refs_explicit_p (vnode))
5780 make_copy_constraint (vi, nonlocal_id);
5782 /* If this is a global variable with an initializer and we are in
5783 IPA mode generate constraints for it. */
5784 if (DECL_INITIAL (decl)
5785 && vnode->definition)
5787 auto_vec<ce_s> rhsc;
5788 struct constraint_expr lhs, *rhsp;
5789 unsigned i;
5790 get_constraint_for_rhs (DECL_INITIAL (decl), &rhsc);
5791 lhs.var = vi->id;
5792 lhs.offset = 0;
5793 lhs.type = SCALAR;
5794 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5795 process_constraint (new_constraint (lhs, *rhsp));
5796 /* If this is a variable that escapes from the unit
5797 the initializer escapes as well. */
5798 if (!varpool_all_refs_explicit_p (vnode))
5800 lhs.var = escaped_id;
5801 lhs.offset = 0;
5802 lhs.type = SCALAR;
5803 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5804 process_constraint (new_constraint (lhs, *rhsp));
5810 return id;
5813 /* Print out the points-to solution for VAR to FILE. */
5815 static void
5816 dump_solution_for_var (FILE *file, unsigned int var)
5818 varinfo_t vi = get_varinfo (var);
5819 unsigned int i;
5820 bitmap_iterator bi;
5822 /* Dump the solution for unified vars anyway, this avoids difficulties
5823 in scanning dumps in the testsuite. */
5824 fprintf (file, "%s = { ", vi->name);
5825 vi = get_varinfo (find (var));
5826 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5827 fprintf (file, "%s ", get_varinfo (i)->name);
5828 fprintf (file, "}");
5830 /* But note when the variable was unified. */
5831 if (vi->id != var)
5832 fprintf (file, " same as %s", vi->name);
5834 fprintf (file, "\n");
5837 /* Print the points-to solution for VAR to stdout. */
5839 DEBUG_FUNCTION void
5840 debug_solution_for_var (unsigned int var)
5842 dump_solution_for_var (stdout, var);
5845 /* Create varinfo structures for all of the variables in the
5846 function for intraprocedural mode. */
5848 static void
5849 intra_create_variable_infos (void)
5851 tree t;
5853 /* For each incoming pointer argument arg, create the constraint ARG
5854 = NONLOCAL or a dummy variable if it is a restrict qualified
5855 passed-by-reference argument. */
5856 for (t = DECL_ARGUMENTS (current_function_decl); t; t = DECL_CHAIN (t))
5858 varinfo_t p = get_vi_for_tree (t);
5860 /* For restrict qualified pointers to objects passed by
5861 reference build a real representative for the pointed-to object.
5862 Treat restrict qualified references the same. */
5863 if (TYPE_RESTRICT (TREE_TYPE (t))
5864 && ((DECL_BY_REFERENCE (t) && POINTER_TYPE_P (TREE_TYPE (t)))
5865 || TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5866 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t))))
5868 struct constraint_expr lhsc, rhsc;
5869 varinfo_t vi;
5870 tree heapvar = build_fake_var_decl (TREE_TYPE (TREE_TYPE (t)));
5871 DECL_EXTERNAL (heapvar) = 1;
5872 vi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS");
5873 insert_vi_for_tree (heapvar, vi);
5874 lhsc.var = p->id;
5875 lhsc.type = SCALAR;
5876 lhsc.offset = 0;
5877 rhsc.var = vi->id;
5878 rhsc.type = ADDRESSOF;
5879 rhsc.offset = 0;
5880 process_constraint (new_constraint (lhsc, rhsc));
5881 for (; vi; vi = vi_next (vi))
5882 if (vi->may_have_pointers)
5884 if (vi->only_restrict_pointers)
5885 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5886 else
5887 make_copy_constraint (vi, nonlocal_id);
5889 continue;
5892 if (POINTER_TYPE_P (TREE_TYPE (t))
5893 && TYPE_RESTRICT (TREE_TYPE (t)))
5894 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5895 else
5897 for (; p; p = vi_next (p))
5899 if (p->only_restrict_pointers)
5900 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5901 else if (p->may_have_pointers)
5902 make_constraint_from (p, nonlocal_id);
5907 /* Add a constraint for a result decl that is passed by reference. */
5908 if (DECL_RESULT (cfun->decl)
5909 && DECL_BY_REFERENCE (DECL_RESULT (cfun->decl)))
5911 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (cfun->decl));
5913 for (p = result_vi; p; p = vi_next (p))
5914 make_constraint_from (p, nonlocal_id);
5917 /* Add a constraint for the incoming static chain parameter. */
5918 if (cfun->static_chain_decl != NULL_TREE)
5920 varinfo_t p, chain_vi = get_vi_for_tree (cfun->static_chain_decl);
5922 for (p = chain_vi; p; p = vi_next (p))
5923 make_constraint_from (p, nonlocal_id);
5927 /* Structure used to put solution bitmaps in a hashtable so they can
5928 be shared among variables with the same points-to set. */
5930 typedef struct shared_bitmap_info
5932 bitmap pt_vars;
5933 hashval_t hashcode;
5934 } *shared_bitmap_info_t;
5935 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
5937 /* Shared_bitmap hashtable helpers. */
5939 struct shared_bitmap_hasher : typed_free_remove <shared_bitmap_info>
5941 typedef shared_bitmap_info value_type;
5942 typedef shared_bitmap_info compare_type;
5943 static inline hashval_t hash (const value_type *);
5944 static inline bool equal (const value_type *, const compare_type *);
5947 /* Hash function for a shared_bitmap_info_t */
5949 inline hashval_t
5950 shared_bitmap_hasher::hash (const value_type *bi)
5952 return bi->hashcode;
5955 /* Equality function for two shared_bitmap_info_t's. */
5957 inline bool
5958 shared_bitmap_hasher::equal (const value_type *sbi1, const compare_type *sbi2)
5960 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
5963 /* Shared_bitmap hashtable. */
5965 static hash_table <shared_bitmap_hasher> shared_bitmap_table;
5967 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5968 existing instance if there is one, NULL otherwise. */
5970 static bitmap
5971 shared_bitmap_lookup (bitmap pt_vars)
5973 shared_bitmap_info **slot;
5974 struct shared_bitmap_info sbi;
5976 sbi.pt_vars = pt_vars;
5977 sbi.hashcode = bitmap_hash (pt_vars);
5979 slot = shared_bitmap_table.find_slot_with_hash (&sbi, sbi.hashcode,
5980 NO_INSERT);
5981 if (!slot)
5982 return NULL;
5983 else
5984 return (*slot)->pt_vars;
5988 /* Add a bitmap to the shared bitmap hashtable. */
5990 static void
5991 shared_bitmap_add (bitmap pt_vars)
5993 shared_bitmap_info **slot;
5994 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
5996 sbi->pt_vars = pt_vars;
5997 sbi->hashcode = bitmap_hash (pt_vars);
5999 slot = shared_bitmap_table.find_slot_with_hash (sbi, sbi->hashcode, INSERT);
6000 gcc_assert (!*slot);
6001 *slot = sbi;
6005 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
6007 static void
6008 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt)
6010 unsigned int i;
6011 bitmap_iterator bi;
6012 varinfo_t escaped_vi = get_varinfo (find (escaped_id));
6013 bool everything_escaped
6014 = escaped_vi->solution && bitmap_bit_p (escaped_vi->solution, anything_id);
6016 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
6018 varinfo_t vi = get_varinfo (i);
6020 /* The only artificial variables that are allowed in a may-alias
6021 set are heap variables. */
6022 if (vi->is_artificial_var && !vi->is_heap_var)
6023 continue;
6025 if (everything_escaped
6026 || (escaped_vi->solution
6027 && bitmap_bit_p (escaped_vi->solution, i)))
6029 pt->vars_contains_escaped = true;
6030 pt->vars_contains_escaped_heap = vi->is_heap_var;
6033 if (TREE_CODE (vi->decl) == VAR_DECL
6034 || TREE_CODE (vi->decl) == PARM_DECL
6035 || TREE_CODE (vi->decl) == RESULT_DECL)
6037 /* If we are in IPA mode we will not recompute points-to
6038 sets after inlining so make sure they stay valid. */
6039 if (in_ipa_mode
6040 && !DECL_PT_UID_SET_P (vi->decl))
6041 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
6043 /* Add the decl to the points-to set. Note that the points-to
6044 set contains global variables. */
6045 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
6046 if (vi->is_global_var)
6047 pt->vars_contains_nonlocal = true;
6053 /* Compute the points-to solution *PT for the variable VI. */
6055 static struct pt_solution
6056 find_what_var_points_to (varinfo_t orig_vi)
6058 unsigned int i;
6059 bitmap_iterator bi;
6060 bitmap finished_solution;
6061 bitmap result;
6062 varinfo_t vi;
6063 void **slot;
6064 struct pt_solution *pt;
6066 /* This variable may have been collapsed, let's get the real
6067 variable. */
6068 vi = get_varinfo (find (orig_vi->id));
6070 /* See if we have already computed the solution and return it. */
6071 slot = pointer_map_insert (final_solutions, vi);
6072 if (*slot != NULL)
6073 return *(struct pt_solution *)*slot;
6075 *slot = pt = XOBNEW (&final_solutions_obstack, struct pt_solution);
6076 memset (pt, 0, sizeof (struct pt_solution));
6078 /* Translate artificial variables into SSA_NAME_PTR_INFO
6079 attributes. */
6080 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
6082 varinfo_t vi = get_varinfo (i);
6084 if (vi->is_artificial_var)
6086 if (vi->id == nothing_id)
6087 pt->null = 1;
6088 else if (vi->id == escaped_id)
6090 if (in_ipa_mode)
6091 pt->ipa_escaped = 1;
6092 else
6093 pt->escaped = 1;
6094 /* Expand some special vars of ESCAPED in-place here. */
6095 varinfo_t evi = get_varinfo (find (escaped_id));
6096 if (bitmap_bit_p (evi->solution, nonlocal_id))
6097 pt->nonlocal = 1;
6099 else if (vi->id == nonlocal_id)
6100 pt->nonlocal = 1;
6101 else if (vi->is_heap_var)
6102 /* We represent heapvars in the points-to set properly. */
6104 else if (vi->id == readonly_id)
6105 /* Nobody cares. */
6107 else if (vi->id == anything_id
6108 || vi->id == integer_id)
6109 pt->anything = 1;
6113 /* Instead of doing extra work, simply do not create
6114 elaborate points-to information for pt_anything pointers. */
6115 if (pt->anything)
6116 return *pt;
6118 /* Share the final set of variables when possible. */
6119 finished_solution = BITMAP_GGC_ALLOC ();
6120 stats.points_to_sets_created++;
6122 set_uids_in_ptset (finished_solution, vi->solution, pt);
6123 result = shared_bitmap_lookup (finished_solution);
6124 if (!result)
6126 shared_bitmap_add (finished_solution);
6127 pt->vars = finished_solution;
6129 else
6131 pt->vars = result;
6132 bitmap_clear (finished_solution);
6135 return *pt;
6138 /* Given a pointer variable P, fill in its points-to set. */
6140 static void
6141 find_what_p_points_to (tree p)
6143 struct ptr_info_def *pi;
6144 tree lookup_p = p;
6145 varinfo_t vi;
6147 /* For parameters, get at the points-to set for the actual parm
6148 decl. */
6149 if (TREE_CODE (p) == SSA_NAME
6150 && SSA_NAME_IS_DEFAULT_DEF (p)
6151 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
6152 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL))
6153 lookup_p = SSA_NAME_VAR (p);
6155 vi = lookup_vi_for_tree (lookup_p);
6156 if (!vi)
6157 return;
6159 pi = get_ptr_info (p);
6160 pi->pt = find_what_var_points_to (vi);
6164 /* Query statistics for points-to solutions. */
6166 static struct {
6167 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
6168 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
6169 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
6170 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
6171 } pta_stats;
6173 void
6174 dump_pta_stats (FILE *s)
6176 fprintf (s, "\nPTA query stats:\n");
6177 fprintf (s, " pt_solution_includes: "
6178 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6179 HOST_WIDE_INT_PRINT_DEC" queries\n",
6180 pta_stats.pt_solution_includes_no_alias,
6181 pta_stats.pt_solution_includes_no_alias
6182 + pta_stats.pt_solution_includes_may_alias);
6183 fprintf (s, " pt_solutions_intersect: "
6184 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6185 HOST_WIDE_INT_PRINT_DEC" queries\n",
6186 pta_stats.pt_solutions_intersect_no_alias,
6187 pta_stats.pt_solutions_intersect_no_alias
6188 + pta_stats.pt_solutions_intersect_may_alias);
6192 /* Reset the points-to solution *PT to a conservative default
6193 (point to anything). */
6195 void
6196 pt_solution_reset (struct pt_solution *pt)
6198 memset (pt, 0, sizeof (struct pt_solution));
6199 pt->anything = true;
6202 /* Set the points-to solution *PT to point only to the variables
6203 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6204 global variables and VARS_CONTAINS_RESTRICT specifies whether
6205 it contains restrict tag variables. */
6207 void
6208 pt_solution_set (struct pt_solution *pt, bitmap vars,
6209 bool vars_contains_nonlocal)
6211 memset (pt, 0, sizeof (struct pt_solution));
6212 pt->vars = vars;
6213 pt->vars_contains_nonlocal = vars_contains_nonlocal;
6214 pt->vars_contains_escaped
6215 = (cfun->gimple_df->escaped.anything
6216 || bitmap_intersect_p (cfun->gimple_df->escaped.vars, vars));
6219 /* Set the points-to solution *PT to point only to the variable VAR. */
6221 void
6222 pt_solution_set_var (struct pt_solution *pt, tree var)
6224 memset (pt, 0, sizeof (struct pt_solution));
6225 pt->vars = BITMAP_GGC_ALLOC ();
6226 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
6227 pt->vars_contains_nonlocal = is_global_var (var);
6228 pt->vars_contains_escaped
6229 = (cfun->gimple_df->escaped.anything
6230 || bitmap_bit_p (cfun->gimple_df->escaped.vars, DECL_PT_UID (var)));
6233 /* Computes the union of the points-to solutions *DEST and *SRC and
6234 stores the result in *DEST. This changes the points-to bitmap
6235 of *DEST and thus may not be used if that might be shared.
6236 The points-to bitmap of *SRC and *DEST will not be shared after
6237 this function if they were not before. */
6239 static void
6240 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6242 dest->anything |= src->anything;
6243 if (dest->anything)
6245 pt_solution_reset (dest);
6246 return;
6249 dest->nonlocal |= src->nonlocal;
6250 dest->escaped |= src->escaped;
6251 dest->ipa_escaped |= src->ipa_escaped;
6252 dest->null |= src->null;
6253 dest->vars_contains_nonlocal |= src->vars_contains_nonlocal;
6254 dest->vars_contains_escaped |= src->vars_contains_escaped;
6255 dest->vars_contains_escaped_heap |= src->vars_contains_escaped_heap;
6256 if (!src->vars)
6257 return;
6259 if (!dest->vars)
6260 dest->vars = BITMAP_GGC_ALLOC ();
6261 bitmap_ior_into (dest->vars, src->vars);
6264 /* Return true if the points-to solution *PT is empty. */
6266 bool
6267 pt_solution_empty_p (struct pt_solution *pt)
6269 if (pt->anything
6270 || pt->nonlocal)
6271 return false;
6273 if (pt->vars
6274 && !bitmap_empty_p (pt->vars))
6275 return false;
6277 /* If the solution includes ESCAPED, check if that is empty. */
6278 if (pt->escaped
6279 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6280 return false;
6282 /* If the solution includes ESCAPED, check if that is empty. */
6283 if (pt->ipa_escaped
6284 && !pt_solution_empty_p (&ipa_escaped_pt))
6285 return false;
6287 return true;
6290 /* Return true if the points-to solution *PT only point to a single var, and
6291 return the var uid in *UID. */
6293 bool
6294 pt_solution_singleton_p (struct pt_solution *pt, unsigned *uid)
6296 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
6297 || pt->null || pt->vars == NULL
6298 || !bitmap_single_bit_set_p (pt->vars))
6299 return false;
6301 *uid = bitmap_first_set_bit (pt->vars);
6302 return true;
6305 /* Return true if the points-to solution *PT includes global memory. */
6307 bool
6308 pt_solution_includes_global (struct pt_solution *pt)
6310 if (pt->anything
6311 || pt->nonlocal
6312 || pt->vars_contains_nonlocal
6313 /* The following is a hack to make the malloc escape hack work.
6314 In reality we'd need different sets for escaped-through-return
6315 and escaped-to-callees and passes would need to be updated. */
6316 || pt->vars_contains_escaped_heap)
6317 return true;
6319 /* 'escaped' is also a placeholder so we have to look into it. */
6320 if (pt->escaped)
6321 return pt_solution_includes_global (&cfun->gimple_df->escaped);
6323 if (pt->ipa_escaped)
6324 return pt_solution_includes_global (&ipa_escaped_pt);
6326 /* ??? This predicate is not correct for the IPA-PTA solution
6327 as we do not properly distinguish between unit escape points
6328 and global variables. */
6329 if (cfun->gimple_df->ipa_pta)
6330 return true;
6332 return false;
6335 /* Return true if the points-to solution *PT includes the variable
6336 declaration DECL. */
6338 static bool
6339 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6341 if (pt->anything)
6342 return true;
6344 if (pt->nonlocal
6345 && is_global_var (decl))
6346 return true;
6348 if (pt->vars
6349 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6350 return true;
6352 /* If the solution includes ESCAPED, check it. */
6353 if (pt->escaped
6354 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6355 return true;
6357 /* If the solution includes ESCAPED, check it. */
6358 if (pt->ipa_escaped
6359 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6360 return true;
6362 return false;
6365 bool
6366 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6368 bool res = pt_solution_includes_1 (pt, decl);
6369 if (res)
6370 ++pta_stats.pt_solution_includes_may_alias;
6371 else
6372 ++pta_stats.pt_solution_includes_no_alias;
6373 return res;
6376 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6377 intersection. */
6379 static bool
6380 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
6382 if (pt1->anything || pt2->anything)
6383 return true;
6385 /* If either points to unknown global memory and the other points to
6386 any global memory they alias. */
6387 if ((pt1->nonlocal
6388 && (pt2->nonlocal
6389 || pt2->vars_contains_nonlocal))
6390 || (pt2->nonlocal
6391 && pt1->vars_contains_nonlocal))
6392 return true;
6394 /* If either points to all escaped memory and the other points to
6395 any escaped memory they alias. */
6396 if ((pt1->escaped
6397 && (pt2->escaped
6398 || pt2->vars_contains_escaped))
6399 || (pt2->escaped
6400 && pt1->vars_contains_escaped))
6401 return true;
6403 /* Check the escaped solution if required.
6404 ??? Do we need to check the local against the IPA escaped sets? */
6405 if ((pt1->ipa_escaped || pt2->ipa_escaped)
6406 && !pt_solution_empty_p (&ipa_escaped_pt))
6408 /* If both point to escaped memory and that solution
6409 is not empty they alias. */
6410 if (pt1->ipa_escaped && pt2->ipa_escaped)
6411 return true;
6413 /* If either points to escaped memory see if the escaped solution
6414 intersects with the other. */
6415 if ((pt1->ipa_escaped
6416 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
6417 || (pt2->ipa_escaped
6418 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
6419 return true;
6422 /* Now both pointers alias if their points-to solution intersects. */
6423 return (pt1->vars
6424 && pt2->vars
6425 && bitmap_intersect_p (pt1->vars, pt2->vars));
6428 bool
6429 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
6431 bool res = pt_solutions_intersect_1 (pt1, pt2);
6432 if (res)
6433 ++pta_stats.pt_solutions_intersect_may_alias;
6434 else
6435 ++pta_stats.pt_solutions_intersect_no_alias;
6436 return res;
6440 /* Dump points-to information to OUTFILE. */
6442 static void
6443 dump_sa_points_to_info (FILE *outfile)
6445 unsigned int i;
6447 fprintf (outfile, "\nPoints-to sets\n\n");
6449 if (dump_flags & TDF_STATS)
6451 fprintf (outfile, "Stats:\n");
6452 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
6453 fprintf (outfile, "Non-pointer vars: %d\n",
6454 stats.nonpointer_vars);
6455 fprintf (outfile, "Statically unified vars: %d\n",
6456 stats.unified_vars_static);
6457 fprintf (outfile, "Dynamically unified vars: %d\n",
6458 stats.unified_vars_dynamic);
6459 fprintf (outfile, "Iterations: %d\n", stats.iterations);
6460 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
6461 fprintf (outfile, "Number of implicit edges: %d\n",
6462 stats.num_implicit_edges);
6465 for (i = 1; i < varmap.length (); i++)
6467 varinfo_t vi = get_varinfo (i);
6468 if (!vi->may_have_pointers)
6469 continue;
6470 dump_solution_for_var (outfile, i);
6475 /* Debug points-to information to stderr. */
6477 DEBUG_FUNCTION void
6478 debug_sa_points_to_info (void)
6480 dump_sa_points_to_info (stderr);
6484 /* Initialize the always-existing constraint variables for NULL
6485 ANYTHING, READONLY, and INTEGER */
6487 static void
6488 init_base_vars (void)
6490 struct constraint_expr lhs, rhs;
6491 varinfo_t var_anything;
6492 varinfo_t var_nothing;
6493 varinfo_t var_readonly;
6494 varinfo_t var_escaped;
6495 varinfo_t var_nonlocal;
6496 varinfo_t var_storedanything;
6497 varinfo_t var_integer;
6499 /* Variable ID zero is reserved and should be NULL. */
6500 varmap.safe_push (NULL);
6502 /* Create the NULL variable, used to represent that a variable points
6503 to NULL. */
6504 var_nothing = new_var_info (NULL_TREE, "NULL");
6505 gcc_assert (var_nothing->id == nothing_id);
6506 var_nothing->is_artificial_var = 1;
6507 var_nothing->offset = 0;
6508 var_nothing->size = ~0;
6509 var_nothing->fullsize = ~0;
6510 var_nothing->is_special_var = 1;
6511 var_nothing->may_have_pointers = 0;
6512 var_nothing->is_global_var = 0;
6514 /* Create the ANYTHING variable, used to represent that a variable
6515 points to some unknown piece of memory. */
6516 var_anything = new_var_info (NULL_TREE, "ANYTHING");
6517 gcc_assert (var_anything->id == anything_id);
6518 var_anything->is_artificial_var = 1;
6519 var_anything->size = ~0;
6520 var_anything->offset = 0;
6521 var_anything->fullsize = ~0;
6522 var_anything->is_special_var = 1;
6524 /* Anything points to anything. This makes deref constraints just
6525 work in the presence of linked list and other p = *p type loops,
6526 by saying that *ANYTHING = ANYTHING. */
6527 lhs.type = SCALAR;
6528 lhs.var = anything_id;
6529 lhs.offset = 0;
6530 rhs.type = ADDRESSOF;
6531 rhs.var = anything_id;
6532 rhs.offset = 0;
6534 /* This specifically does not use process_constraint because
6535 process_constraint ignores all anything = anything constraints, since all
6536 but this one are redundant. */
6537 constraints.safe_push (new_constraint (lhs, rhs));
6539 /* Create the READONLY variable, used to represent that a variable
6540 points to readonly memory. */
6541 var_readonly = new_var_info (NULL_TREE, "READONLY");
6542 gcc_assert (var_readonly->id == readonly_id);
6543 var_readonly->is_artificial_var = 1;
6544 var_readonly->offset = 0;
6545 var_readonly->size = ~0;
6546 var_readonly->fullsize = ~0;
6547 var_readonly->is_special_var = 1;
6549 /* readonly memory points to anything, in order to make deref
6550 easier. In reality, it points to anything the particular
6551 readonly variable can point to, but we don't track this
6552 separately. */
6553 lhs.type = SCALAR;
6554 lhs.var = readonly_id;
6555 lhs.offset = 0;
6556 rhs.type = ADDRESSOF;
6557 rhs.var = readonly_id; /* FIXME */
6558 rhs.offset = 0;
6559 process_constraint (new_constraint (lhs, rhs));
6561 /* Create the ESCAPED variable, used to represent the set of escaped
6562 memory. */
6563 var_escaped = new_var_info (NULL_TREE, "ESCAPED");
6564 gcc_assert (var_escaped->id == escaped_id);
6565 var_escaped->is_artificial_var = 1;
6566 var_escaped->offset = 0;
6567 var_escaped->size = ~0;
6568 var_escaped->fullsize = ~0;
6569 var_escaped->is_special_var = 0;
6571 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6572 memory. */
6573 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL");
6574 gcc_assert (var_nonlocal->id == nonlocal_id);
6575 var_nonlocal->is_artificial_var = 1;
6576 var_nonlocal->offset = 0;
6577 var_nonlocal->size = ~0;
6578 var_nonlocal->fullsize = ~0;
6579 var_nonlocal->is_special_var = 1;
6581 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6582 lhs.type = SCALAR;
6583 lhs.var = escaped_id;
6584 lhs.offset = 0;
6585 rhs.type = DEREF;
6586 rhs.var = escaped_id;
6587 rhs.offset = 0;
6588 process_constraint (new_constraint (lhs, rhs));
6590 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6591 whole variable escapes. */
6592 lhs.type = SCALAR;
6593 lhs.var = escaped_id;
6594 lhs.offset = 0;
6595 rhs.type = SCALAR;
6596 rhs.var = escaped_id;
6597 rhs.offset = UNKNOWN_OFFSET;
6598 process_constraint (new_constraint (lhs, rhs));
6600 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6601 everything pointed to by escaped points to what global memory can
6602 point to. */
6603 lhs.type = DEREF;
6604 lhs.var = escaped_id;
6605 lhs.offset = 0;
6606 rhs.type = SCALAR;
6607 rhs.var = nonlocal_id;
6608 rhs.offset = 0;
6609 process_constraint (new_constraint (lhs, rhs));
6611 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6612 global memory may point to global memory and escaped memory. */
6613 lhs.type = SCALAR;
6614 lhs.var = nonlocal_id;
6615 lhs.offset = 0;
6616 rhs.type = ADDRESSOF;
6617 rhs.var = nonlocal_id;
6618 rhs.offset = 0;
6619 process_constraint (new_constraint (lhs, rhs));
6620 rhs.type = ADDRESSOF;
6621 rhs.var = escaped_id;
6622 rhs.offset = 0;
6623 process_constraint (new_constraint (lhs, rhs));
6625 /* Create the STOREDANYTHING variable, used to represent the set of
6626 variables stored to *ANYTHING. */
6627 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING");
6628 gcc_assert (var_storedanything->id == storedanything_id);
6629 var_storedanything->is_artificial_var = 1;
6630 var_storedanything->offset = 0;
6631 var_storedanything->size = ~0;
6632 var_storedanything->fullsize = ~0;
6633 var_storedanything->is_special_var = 0;
6635 /* Create the INTEGER variable, used to represent that a variable points
6636 to what an INTEGER "points to". */
6637 var_integer = new_var_info (NULL_TREE, "INTEGER");
6638 gcc_assert (var_integer->id == integer_id);
6639 var_integer->is_artificial_var = 1;
6640 var_integer->size = ~0;
6641 var_integer->fullsize = ~0;
6642 var_integer->offset = 0;
6643 var_integer->is_special_var = 1;
6645 /* INTEGER = ANYTHING, because we don't know where a dereference of
6646 a random integer will point to. */
6647 lhs.type = SCALAR;
6648 lhs.var = integer_id;
6649 lhs.offset = 0;
6650 rhs.type = ADDRESSOF;
6651 rhs.var = anything_id;
6652 rhs.offset = 0;
6653 process_constraint (new_constraint (lhs, rhs));
6656 /* Initialize things necessary to perform PTA */
6658 static void
6659 init_alias_vars (void)
6661 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
6663 bitmap_obstack_initialize (&pta_obstack);
6664 bitmap_obstack_initialize (&oldpta_obstack);
6665 bitmap_obstack_initialize (&predbitmap_obstack);
6667 constraint_pool = create_alloc_pool ("Constraint pool",
6668 sizeof (struct constraint), 30);
6669 variable_info_pool = create_alloc_pool ("Variable info pool",
6670 sizeof (struct variable_info), 30);
6671 constraints.create (8);
6672 varmap.create (8);
6673 vi_for_tree = pointer_map_create ();
6674 call_stmt_vars = pointer_map_create ();
6676 memset (&stats, 0, sizeof (stats));
6677 shared_bitmap_table.create (511);
6678 init_base_vars ();
6680 gcc_obstack_init (&fake_var_decl_obstack);
6682 final_solutions = pointer_map_create ();
6683 gcc_obstack_init (&final_solutions_obstack);
6686 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6687 predecessor edges. */
6689 static void
6690 remove_preds_and_fake_succs (constraint_graph_t graph)
6692 unsigned int i;
6694 /* Clear the implicit ref and address nodes from the successor
6695 lists. */
6696 for (i = 1; i < FIRST_REF_NODE; i++)
6698 if (graph->succs[i])
6699 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
6700 FIRST_REF_NODE * 2);
6703 /* Free the successor list for the non-ref nodes. */
6704 for (i = FIRST_REF_NODE + 1; i < graph->size; i++)
6706 if (graph->succs[i])
6707 BITMAP_FREE (graph->succs[i]);
6710 /* Now reallocate the size of the successor list as, and blow away
6711 the predecessor bitmaps. */
6712 graph->size = varmap.length ();
6713 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
6715 free (graph->implicit_preds);
6716 graph->implicit_preds = NULL;
6717 free (graph->preds);
6718 graph->preds = NULL;
6719 bitmap_obstack_release (&predbitmap_obstack);
6722 /* Solve the constraint set. */
6724 static void
6725 solve_constraints (void)
6727 struct scc_info *si;
6729 if (dump_file)
6730 fprintf (dump_file,
6731 "\nCollapsing static cycles and doing variable "
6732 "substitution\n");
6734 init_graph (varmap.length () * 2);
6736 if (dump_file)
6737 fprintf (dump_file, "Building predecessor graph\n");
6738 build_pred_graph ();
6740 if (dump_file)
6741 fprintf (dump_file, "Detecting pointer and location "
6742 "equivalences\n");
6743 si = perform_var_substitution (graph);
6745 if (dump_file)
6746 fprintf (dump_file, "Rewriting constraints and unifying "
6747 "variables\n");
6748 rewrite_constraints (graph, si);
6750 build_succ_graph ();
6752 free_var_substitution_info (si);
6754 /* Attach complex constraints to graph nodes. */
6755 move_complex_constraints (graph);
6757 if (dump_file)
6758 fprintf (dump_file, "Uniting pointer but not location equivalent "
6759 "variables\n");
6760 unite_pointer_equivalences (graph);
6762 if (dump_file)
6763 fprintf (dump_file, "Finding indirect cycles\n");
6764 find_indirect_cycles (graph);
6766 /* Implicit nodes and predecessors are no longer necessary at this
6767 point. */
6768 remove_preds_and_fake_succs (graph);
6770 if (dump_file && (dump_flags & TDF_GRAPH))
6772 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
6773 "in dot format:\n");
6774 dump_constraint_graph (dump_file);
6775 fprintf (dump_file, "\n\n");
6778 if (dump_file)
6779 fprintf (dump_file, "Solving graph\n");
6781 solve_graph (graph);
6783 if (dump_file && (dump_flags & TDF_GRAPH))
6785 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
6786 "in dot format:\n");
6787 dump_constraint_graph (dump_file);
6788 fprintf (dump_file, "\n\n");
6791 if (dump_file)
6792 dump_sa_points_to_info (dump_file);
6795 /* Create points-to sets for the current function. See the comments
6796 at the start of the file for an algorithmic overview. */
6798 static void
6799 compute_points_to_sets (void)
6801 basic_block bb;
6802 unsigned i;
6803 varinfo_t vi;
6805 timevar_push (TV_TREE_PTA);
6807 init_alias_vars ();
6809 intra_create_variable_infos ();
6811 /* Now walk all statements and build the constraint set. */
6812 FOR_EACH_BB_FN (bb, cfun)
6814 gimple_stmt_iterator gsi;
6816 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6818 gimple phi = gsi_stmt (gsi);
6820 if (! virtual_operand_p (gimple_phi_result (phi)))
6821 find_func_aliases (phi);
6824 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6826 gimple stmt = gsi_stmt (gsi);
6828 find_func_aliases (stmt);
6832 if (dump_file)
6834 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
6835 dump_constraints (dump_file, 0);
6838 /* From the constraints compute the points-to sets. */
6839 solve_constraints ();
6841 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6842 cfun->gimple_df->escaped = find_what_var_points_to (get_varinfo (escaped_id));
6844 /* Make sure the ESCAPED solution (which is used as placeholder in
6845 other solutions) does not reference itself. This simplifies
6846 points-to solution queries. */
6847 cfun->gimple_df->escaped.escaped = 0;
6849 /* Compute the points-to sets for pointer SSA_NAMEs. */
6850 for (i = 0; i < num_ssa_names; ++i)
6852 tree ptr = ssa_name (i);
6853 if (ptr
6854 && POINTER_TYPE_P (TREE_TYPE (ptr)))
6855 find_what_p_points_to (ptr);
6858 /* Compute the call-used/clobbered sets. */
6859 FOR_EACH_BB_FN (bb, cfun)
6861 gimple_stmt_iterator gsi;
6863 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6865 gimple stmt = gsi_stmt (gsi);
6866 struct pt_solution *pt;
6867 if (!is_gimple_call (stmt))
6868 continue;
6870 pt = gimple_call_use_set (stmt);
6871 if (gimple_call_flags (stmt) & ECF_CONST)
6872 memset (pt, 0, sizeof (struct pt_solution));
6873 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
6875 *pt = find_what_var_points_to (vi);
6876 /* Escaped (and thus nonlocal) variables are always
6877 implicitly used by calls. */
6878 /* ??? ESCAPED can be empty even though NONLOCAL
6879 always escaped. */
6880 pt->nonlocal = 1;
6881 pt->escaped = 1;
6883 else
6885 /* If there is nothing special about this call then
6886 we have made everything that is used also escape. */
6887 *pt = cfun->gimple_df->escaped;
6888 pt->nonlocal = 1;
6891 pt = gimple_call_clobber_set (stmt);
6892 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
6893 memset (pt, 0, sizeof (struct pt_solution));
6894 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
6896 *pt = find_what_var_points_to (vi);
6897 /* Escaped (and thus nonlocal) variables are always
6898 implicitly clobbered by calls. */
6899 /* ??? ESCAPED can be empty even though NONLOCAL
6900 always escaped. */
6901 pt->nonlocal = 1;
6902 pt->escaped = 1;
6904 else
6906 /* If there is nothing special about this call then
6907 we have made everything that is used also escape. */
6908 *pt = cfun->gimple_df->escaped;
6909 pt->nonlocal = 1;
6914 timevar_pop (TV_TREE_PTA);
6918 /* Delete created points-to sets. */
6920 static void
6921 delete_points_to_sets (void)
6923 unsigned int i;
6925 shared_bitmap_table.dispose ();
6926 if (dump_file && (dump_flags & TDF_STATS))
6927 fprintf (dump_file, "Points to sets created:%d\n",
6928 stats.points_to_sets_created);
6930 pointer_map_destroy (vi_for_tree);
6931 pointer_map_destroy (call_stmt_vars);
6932 bitmap_obstack_release (&pta_obstack);
6933 constraints.release ();
6935 for (i = 0; i < graph->size; i++)
6936 graph->complex[i].release ();
6937 free (graph->complex);
6939 free (graph->rep);
6940 free (graph->succs);
6941 free (graph->pe);
6942 free (graph->pe_rep);
6943 free (graph->indirect_cycles);
6944 free (graph);
6946 varmap.release ();
6947 free_alloc_pool (variable_info_pool);
6948 free_alloc_pool (constraint_pool);
6950 obstack_free (&fake_var_decl_obstack, NULL);
6952 pointer_map_destroy (final_solutions);
6953 obstack_free (&final_solutions_obstack, NULL);
6957 /* Compute points-to information for every SSA_NAME pointer in the
6958 current function and compute the transitive closure of escaped
6959 variables to re-initialize the call-clobber states of local variables. */
6961 unsigned int
6962 compute_may_aliases (void)
6964 if (cfun->gimple_df->ipa_pta)
6966 if (dump_file)
6968 fprintf (dump_file, "\nNot re-computing points-to information "
6969 "because IPA points-to information is available.\n\n");
6971 /* But still dump what we have remaining it. */
6972 dump_alias_info (dump_file);
6975 return 0;
6978 /* For each pointer P_i, determine the sets of variables that P_i may
6979 point-to. Compute the reachability set of escaped and call-used
6980 variables. */
6981 compute_points_to_sets ();
6983 /* Debugging dumps. */
6984 if (dump_file)
6985 dump_alias_info (dump_file);
6987 /* Deallocate memory used by aliasing data structures and the internal
6988 points-to solution. */
6989 delete_points_to_sets ();
6991 gcc_assert (!need_ssa_update_p (cfun));
6993 return 0;
6996 static bool
6997 gate_tree_pta (void)
6999 return flag_tree_pta;
7002 /* A dummy pass to cause points-to information to be computed via
7003 TODO_rebuild_alias. */
7005 namespace {
7007 const pass_data pass_data_build_alias =
7009 GIMPLE_PASS, /* type */
7010 "alias", /* name */
7011 OPTGROUP_NONE, /* optinfo_flags */
7012 true, /* has_gate */
7013 false, /* has_execute */
7014 TV_NONE, /* tv_id */
7015 ( PROP_cfg | PROP_ssa ), /* properties_required */
7016 0, /* properties_provided */
7017 0, /* properties_destroyed */
7018 0, /* todo_flags_start */
7019 TODO_rebuild_alias, /* todo_flags_finish */
7022 class pass_build_alias : public gimple_opt_pass
7024 public:
7025 pass_build_alias (gcc::context *ctxt)
7026 : gimple_opt_pass (pass_data_build_alias, ctxt)
7029 /* opt_pass methods: */
7030 bool gate () { return gate_tree_pta (); }
7032 }; // class pass_build_alias
7034 } // anon namespace
7036 gimple_opt_pass *
7037 make_pass_build_alias (gcc::context *ctxt)
7039 return new pass_build_alias (ctxt);
7042 /* A dummy pass to cause points-to information to be computed via
7043 TODO_rebuild_alias. */
7045 namespace {
7047 const pass_data pass_data_build_ealias =
7049 GIMPLE_PASS, /* type */
7050 "ealias", /* name */
7051 OPTGROUP_NONE, /* optinfo_flags */
7052 true, /* has_gate */
7053 false, /* has_execute */
7054 TV_NONE, /* tv_id */
7055 ( PROP_cfg | PROP_ssa ), /* properties_required */
7056 0, /* properties_provided */
7057 0, /* properties_destroyed */
7058 0, /* todo_flags_start */
7059 TODO_rebuild_alias, /* todo_flags_finish */
7062 class pass_build_ealias : public gimple_opt_pass
7064 public:
7065 pass_build_ealias (gcc::context *ctxt)
7066 : gimple_opt_pass (pass_data_build_ealias, ctxt)
7069 /* opt_pass methods: */
7070 bool gate () { return gate_tree_pta (); }
7072 }; // class pass_build_ealias
7074 } // anon namespace
7076 gimple_opt_pass *
7077 make_pass_build_ealias (gcc::context *ctxt)
7079 return new pass_build_ealias (ctxt);
7083 /* Return true if we should execute IPA PTA. */
7084 static bool
7085 gate_ipa_pta (void)
7087 return (optimize
7088 && flag_ipa_pta
7089 /* Don't bother doing anything if the program has errors. */
7090 && !seen_error ());
7093 /* IPA PTA solutions for ESCAPED. */
7094 struct pt_solution ipa_escaped_pt
7095 = { true, false, false, false, false, false, false, false, NULL };
7097 /* Associate node with varinfo DATA. Worker for
7098 cgraph_for_node_and_aliases. */
7099 static bool
7100 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
7102 if ((node->alias || node->thunk.thunk_p)
7103 && node->analyzed)
7104 insert_vi_for_tree (node->decl, (varinfo_t)data);
7105 return false;
7108 /* Execute the driver for IPA PTA. */
7109 static unsigned int
7110 ipa_pta_execute (void)
7112 struct cgraph_node *node;
7113 varpool_node *var;
7114 int from;
7116 in_ipa_mode = 1;
7118 init_alias_vars ();
7120 if (dump_file && (dump_flags & TDF_DETAILS))
7122 dump_symtab (dump_file);
7123 fprintf (dump_file, "\n");
7126 /* Build the constraints. */
7127 FOR_EACH_DEFINED_FUNCTION (node)
7129 varinfo_t vi;
7130 /* Nodes without a body are not interesting. Especially do not
7131 visit clones at this point for now - we get duplicate decls
7132 there for inline clones at least. */
7133 if (!cgraph_function_with_gimple_body_p (node) || node->clone_of)
7134 continue;
7135 cgraph_get_body (node);
7137 gcc_assert (!node->clone_of);
7139 vi = create_function_info_for (node->decl,
7140 alias_get_name (node->decl));
7141 cgraph_for_node_and_aliases (node, associate_varinfo_to_alias, vi, true);
7144 /* Create constraints for global variables and their initializers. */
7145 FOR_EACH_VARIABLE (var)
7147 if (var->alias && var->analyzed)
7148 continue;
7150 get_vi_for_tree (var->decl);
7153 if (dump_file)
7155 fprintf (dump_file,
7156 "Generating constraints for global initializers\n\n");
7157 dump_constraints (dump_file, 0);
7158 fprintf (dump_file, "\n");
7160 from = constraints.length ();
7162 FOR_EACH_DEFINED_FUNCTION (node)
7164 struct function *func;
7165 basic_block bb;
7167 /* Nodes without a body are not interesting. */
7168 if (!cgraph_function_with_gimple_body_p (node) || node->clone_of)
7169 continue;
7171 if (dump_file)
7173 fprintf (dump_file,
7174 "Generating constraints for %s", node->name ());
7175 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
7176 fprintf (dump_file, " (%s)",
7177 IDENTIFIER_POINTER
7178 (DECL_ASSEMBLER_NAME (node->decl)));
7179 fprintf (dump_file, "\n");
7182 func = DECL_STRUCT_FUNCTION (node->decl);
7183 push_cfun (func);
7185 /* For externally visible or attribute used annotated functions use
7186 local constraints for their arguments.
7187 For local functions we see all callers and thus do not need initial
7188 constraints for parameters. */
7189 if (node->used_from_other_partition
7190 || node->externally_visible
7191 || node->force_output)
7193 intra_create_variable_infos ();
7195 /* We also need to make function return values escape. Nothing
7196 escapes by returning from main though. */
7197 if (!MAIN_NAME_P (DECL_NAME (node->decl)))
7199 varinfo_t fi, rvi;
7200 fi = lookup_vi_for_tree (node->decl);
7201 rvi = first_vi_for_offset (fi, fi_result);
7202 if (rvi && rvi->offset == fi_result)
7204 struct constraint_expr includes;
7205 struct constraint_expr var;
7206 includes.var = escaped_id;
7207 includes.offset = 0;
7208 includes.type = SCALAR;
7209 var.var = rvi->id;
7210 var.offset = 0;
7211 var.type = SCALAR;
7212 process_constraint (new_constraint (includes, var));
7217 /* Build constriants for the function body. */
7218 FOR_EACH_BB_FN (bb, func)
7220 gimple_stmt_iterator gsi;
7222 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7223 gsi_next (&gsi))
7225 gimple phi = gsi_stmt (gsi);
7227 if (! virtual_operand_p (gimple_phi_result (phi)))
7228 find_func_aliases (phi);
7231 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7233 gimple stmt = gsi_stmt (gsi);
7235 find_func_aliases (stmt);
7236 find_func_clobbers (stmt);
7240 pop_cfun ();
7242 if (dump_file)
7244 fprintf (dump_file, "\n");
7245 dump_constraints (dump_file, from);
7246 fprintf (dump_file, "\n");
7248 from = constraints.length ();
7251 /* From the constraints compute the points-to sets. */
7252 solve_constraints ();
7254 /* Compute the global points-to sets for ESCAPED.
7255 ??? Note that the computed escape set is not correct
7256 for the whole unit as we fail to consider graph edges to
7257 externally visible functions. */
7258 ipa_escaped_pt = find_what_var_points_to (get_varinfo (escaped_id));
7260 /* Make sure the ESCAPED solution (which is used as placeholder in
7261 other solutions) does not reference itself. This simplifies
7262 points-to solution queries. */
7263 ipa_escaped_pt.ipa_escaped = 0;
7265 /* Assign the points-to sets to the SSA names in the unit. */
7266 FOR_EACH_DEFINED_FUNCTION (node)
7268 tree ptr;
7269 struct function *fn;
7270 unsigned i;
7271 basic_block bb;
7273 /* Nodes without a body are not interesting. */
7274 if (!cgraph_function_with_gimple_body_p (node) || node->clone_of)
7275 continue;
7277 fn = DECL_STRUCT_FUNCTION (node->decl);
7279 /* Compute the points-to sets for pointer SSA_NAMEs. */
7280 FOR_EACH_VEC_ELT (*fn->gimple_df->ssa_names, i, ptr)
7282 if (ptr
7283 && POINTER_TYPE_P (TREE_TYPE (ptr)))
7284 find_what_p_points_to (ptr);
7287 /* Compute the call-use and call-clobber sets for indirect calls
7288 and calls to external functions. */
7289 FOR_EACH_BB_FN (bb, fn)
7291 gimple_stmt_iterator gsi;
7293 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7295 gimple stmt = gsi_stmt (gsi);
7296 struct pt_solution *pt;
7297 varinfo_t vi, fi;
7298 tree decl;
7300 if (!is_gimple_call (stmt))
7301 continue;
7303 /* Handle direct calls to functions with body. */
7304 decl = gimple_call_fndecl (stmt);
7305 if (decl
7306 && (fi = lookup_vi_for_tree (decl))
7307 && fi->is_fn_info)
7309 *gimple_call_clobber_set (stmt)
7310 = find_what_var_points_to
7311 (first_vi_for_offset (fi, fi_clobbers));
7312 *gimple_call_use_set (stmt)
7313 = find_what_var_points_to
7314 (first_vi_for_offset (fi, fi_uses));
7316 /* Handle direct calls to external functions. */
7317 else if (decl)
7319 pt = gimple_call_use_set (stmt);
7320 if (gimple_call_flags (stmt) & ECF_CONST)
7321 memset (pt, 0, sizeof (struct pt_solution));
7322 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7324 *pt = find_what_var_points_to (vi);
7325 /* Escaped (and thus nonlocal) variables are always
7326 implicitly used by calls. */
7327 /* ??? ESCAPED can be empty even though NONLOCAL
7328 always escaped. */
7329 pt->nonlocal = 1;
7330 pt->ipa_escaped = 1;
7332 else
7334 /* If there is nothing special about this call then
7335 we have made everything that is used also escape. */
7336 *pt = ipa_escaped_pt;
7337 pt->nonlocal = 1;
7340 pt = gimple_call_clobber_set (stmt);
7341 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7342 memset (pt, 0, sizeof (struct pt_solution));
7343 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7345 *pt = find_what_var_points_to (vi);
7346 /* Escaped (and thus nonlocal) variables are always
7347 implicitly clobbered by calls. */
7348 /* ??? ESCAPED can be empty even though NONLOCAL
7349 always escaped. */
7350 pt->nonlocal = 1;
7351 pt->ipa_escaped = 1;
7353 else
7355 /* If there is nothing special about this call then
7356 we have made everything that is used also escape. */
7357 *pt = ipa_escaped_pt;
7358 pt->nonlocal = 1;
7361 /* Handle indirect calls. */
7362 else if (!decl
7363 && (fi = get_fi_for_callee (stmt)))
7365 /* We need to accumulate all clobbers/uses of all possible
7366 callees. */
7367 fi = get_varinfo (find (fi->id));
7368 /* If we cannot constrain the set of functions we'll end up
7369 calling we end up using/clobbering everything. */
7370 if (bitmap_bit_p (fi->solution, anything_id)
7371 || bitmap_bit_p (fi->solution, nonlocal_id)
7372 || bitmap_bit_p (fi->solution, escaped_id))
7374 pt_solution_reset (gimple_call_clobber_set (stmt));
7375 pt_solution_reset (gimple_call_use_set (stmt));
7377 else
7379 bitmap_iterator bi;
7380 unsigned i;
7381 struct pt_solution *uses, *clobbers;
7383 uses = gimple_call_use_set (stmt);
7384 clobbers = gimple_call_clobber_set (stmt);
7385 memset (uses, 0, sizeof (struct pt_solution));
7386 memset (clobbers, 0, sizeof (struct pt_solution));
7387 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
7389 struct pt_solution sol;
7391 vi = get_varinfo (i);
7392 if (!vi->is_fn_info)
7394 /* ??? We could be more precise here? */
7395 uses->nonlocal = 1;
7396 uses->ipa_escaped = 1;
7397 clobbers->nonlocal = 1;
7398 clobbers->ipa_escaped = 1;
7399 continue;
7402 if (!uses->anything)
7404 sol = find_what_var_points_to
7405 (first_vi_for_offset (vi, fi_uses));
7406 pt_solution_ior_into (uses, &sol);
7408 if (!clobbers->anything)
7410 sol = find_what_var_points_to
7411 (first_vi_for_offset (vi, fi_clobbers));
7412 pt_solution_ior_into (clobbers, &sol);
7420 fn->gimple_df->ipa_pta = true;
7423 delete_points_to_sets ();
7425 in_ipa_mode = 0;
7427 return 0;
7430 namespace {
7432 const pass_data pass_data_ipa_pta =
7434 SIMPLE_IPA_PASS, /* type */
7435 "pta", /* name */
7436 OPTGROUP_NONE, /* optinfo_flags */
7437 true, /* has_gate */
7438 true, /* has_execute */
7439 TV_IPA_PTA, /* tv_id */
7440 0, /* properties_required */
7441 0, /* properties_provided */
7442 0, /* properties_destroyed */
7443 0, /* todo_flags_start */
7444 0, /* todo_flags_finish */
7447 class pass_ipa_pta : public simple_ipa_opt_pass
7449 public:
7450 pass_ipa_pta (gcc::context *ctxt)
7451 : simple_ipa_opt_pass (pass_data_ipa_pta, ctxt)
7454 /* opt_pass methods: */
7455 bool gate () { return gate_ipa_pta (); }
7456 unsigned int execute () { return ipa_pta_execute (); }
7458 }; // class pass_ipa_pta
7460 } // anon namespace
7462 simple_ipa_opt_pass *
7463 make_pass_ipa_pta (gcc::context *ctxt)
7465 return new pass_ipa_pta (ctxt);