gcc/
[official-gcc.git] / gcc / tree-ssa-structalias.c
blob129798ae21ab7426117d36c6e6888221ce45470e
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 "predict.h"
30 #include "vec.h"
31 #include "hashtab.h"
32 #include "hash-set.h"
33 #include "machmode.h"
34 #include "hard-reg-set.h"
35 #include "input.h"
36 #include "function.h"
37 #include "dominance.h"
38 #include "cfg.h"
39 #include "basic-block.h"
40 #include "tree.h"
41 #include "stor-layout.h"
42 #include "stmt.h"
43 #include "hash-table.h"
44 #include "tree-ssa-alias.h"
45 #include "internal-fn.h"
46 #include "gimple-expr.h"
47 #include "is-a.h"
48 #include "gimple.h"
49 #include "gimple-iterator.h"
50 #include "gimple-ssa.h"
51 #include "cgraph.h"
52 #include "stringpool.h"
53 #include "tree-ssanames.h"
54 #include "tree-into-ssa.h"
55 #include "expr.h"
56 #include "tree-dfa.h"
57 #include "tree-inline.h"
58 #include "diagnostic-core.h"
59 #include "tree-pass.h"
60 #include "alloc-pool.h"
61 #include "splay-tree.h"
62 #include "params.h"
63 #include "alias.h"
65 /* The idea behind this analyzer is to generate set constraints from the
66 program, then solve the resulting constraints in order to generate the
67 points-to sets.
69 Set constraints are a way of modeling program analysis problems that
70 involve sets. They consist of an inclusion constraint language,
71 describing the variables (each variable is a set) and operations that
72 are involved on the variables, and a set of rules that derive facts
73 from these operations. To solve a system of set constraints, you derive
74 all possible facts under the rules, which gives you the correct sets
75 as a consequence.
77 See "Efficient Field-sensitive pointer analysis for C" by "David
78 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
79 http://citeseer.ist.psu.edu/pearce04efficient.html
81 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
82 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
83 http://citeseer.ist.psu.edu/heintze01ultrafast.html
85 There are three types of real constraint expressions, DEREF,
86 ADDRESSOF, and SCALAR. Each constraint expression consists
87 of a constraint type, a variable, and an offset.
89 SCALAR is a constraint expression type used to represent x, whether
90 it appears on the LHS or the RHS of a statement.
91 DEREF is a constraint expression type used to represent *x, whether
92 it appears on the LHS or the RHS of a statement.
93 ADDRESSOF is a constraint expression used to represent &x, whether
94 it appears on the LHS or the RHS of a statement.
96 Each pointer variable in the program is assigned an integer id, and
97 each field of a structure variable is assigned an integer id as well.
99 Structure variables are linked to their list of fields through a "next
100 field" in each variable that points to the next field in offset
101 order.
102 Each variable for a structure field has
104 1. "size", that tells the size in bits of that field.
105 2. "fullsize, that tells the size in bits of the entire structure.
106 3. "offset", that tells the offset in bits from the beginning of the
107 structure to this field.
109 Thus,
110 struct f
112 int a;
113 int b;
114 } foo;
115 int *bar;
117 looks like
119 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
120 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
121 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
124 In order to solve the system of set constraints, the following is
125 done:
127 1. Each constraint variable x has a solution set associated with it,
128 Sol(x).
130 2. Constraints are separated into direct, copy, and complex.
131 Direct constraints are ADDRESSOF constraints that require no extra
132 processing, such as P = &Q
133 Copy constraints are those of the form P = Q.
134 Complex constraints are all the constraints involving dereferences
135 and offsets (including offsetted copies).
137 3. All direct constraints of the form P = &Q are processed, such
138 that Q is added to Sol(P)
140 4. All complex constraints for a given constraint variable are stored in a
141 linked list attached to that variable's node.
143 5. A directed graph is built out of the copy constraints. Each
144 constraint variable is a node in the graph, and an edge from
145 Q to P is added for each copy constraint of the form P = Q
147 6. The graph is then walked, and solution sets are
148 propagated along the copy edges, such that an edge from Q to P
149 causes Sol(P) <- Sol(P) union Sol(Q).
151 7. As we visit each node, all complex constraints associated with
152 that node are processed by adding appropriate copy edges to the graph, or the
153 appropriate variables to the solution set.
155 8. The process of walking the graph is iterated until no solution
156 sets change.
158 Prior to walking the graph in steps 6 and 7, We perform static
159 cycle elimination on the constraint graph, as well
160 as off-line variable substitution.
162 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
163 on and turned into anything), but isn't. You can just see what offset
164 inside the pointed-to struct it's going to access.
166 TODO: Constant bounded arrays can be handled as if they were structs of the
167 same number of elements.
169 TODO: Modeling heap and incoming pointers becomes much better if we
170 add fields to them as we discover them, which we could do.
172 TODO: We could handle unions, but to be honest, it's probably not
173 worth the pain or slowdown. */
175 /* IPA-PTA optimizations possible.
177 When the indirect function called is ANYTHING we can add disambiguation
178 based on the function signatures (or simply the parameter count which
179 is the varinfo size). We also do not need to consider functions that
180 do not have their address taken.
182 The is_global_var bit which marks escape points is overly conservative
183 in IPA mode. Split it to is_escape_point and is_global_var - only
184 externally visible globals are escape points in IPA mode. This is
185 also needed to fix the pt_solution_includes_global predicate
186 (and thus ptr_deref_may_alias_global_p).
188 The way we introduce DECL_PT_UID to avoid fixing up all points-to
189 sets in the translation unit when we copy a DECL during inlining
190 pessimizes precision. The advantage is that the DECL_PT_UID keeps
191 compile-time and memory usage overhead low - the points-to sets
192 do not grow or get unshared as they would during a fixup phase.
193 An alternative solution is to delay IPA PTA until after all
194 inlining transformations have been applied.
196 The way we propagate clobber/use information isn't optimized.
197 It should use a new complex constraint that properly filters
198 out local variables of the callee (though that would make
199 the sets invalid after inlining). OTOH we might as well
200 admit defeat to WHOPR and simply do all the clobber/use analysis
201 and propagation after PTA finished but before we threw away
202 points-to information for memory variables. WHOPR and PTA
203 do not play along well anyway - the whole constraint solving
204 would need to be done in WPA phase and it will be very interesting
205 to apply the results to local SSA names during LTRANS phase.
207 We probably should compute a per-function unit-ESCAPE solution
208 propagating it simply like the clobber / uses solutions. The
209 solution can go alongside the non-IPA espaced solution and be
210 used to query which vars escape the unit through a function.
212 We never put function decls in points-to sets so we do not
213 keep the set of called functions for indirect calls.
215 And probably more. */
217 static bool use_field_sensitive = true;
218 static int in_ipa_mode = 0;
220 /* Used for predecessor bitmaps. */
221 static bitmap_obstack predbitmap_obstack;
223 /* Used for points-to sets. */
224 static bitmap_obstack pta_obstack;
226 /* Used for oldsolution members of variables. */
227 static bitmap_obstack oldpta_obstack;
229 /* Used for per-solver-iteration bitmaps. */
230 static bitmap_obstack iteration_obstack;
232 static unsigned int create_variable_info_for (tree, const char *);
233 typedef struct constraint_graph *constraint_graph_t;
234 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
236 struct constraint;
237 typedef struct constraint *constraint_t;
240 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
241 if (a) \
242 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
244 static struct constraint_stats
246 unsigned int total_vars;
247 unsigned int nonpointer_vars;
248 unsigned int unified_vars_static;
249 unsigned int unified_vars_dynamic;
250 unsigned int iterations;
251 unsigned int num_edges;
252 unsigned int num_implicit_edges;
253 unsigned int points_to_sets_created;
254 } stats;
256 struct variable_info
258 /* ID of this variable */
259 unsigned int id;
261 /* True if this is a variable created by the constraint analysis, such as
262 heap variables and constraints we had to break up. */
263 unsigned int is_artificial_var : 1;
265 /* True if this is a special variable whose solution set should not be
266 changed. */
267 unsigned int is_special_var : 1;
269 /* True for variables whose size is not known or variable. */
270 unsigned int is_unknown_size_var : 1;
272 /* True for (sub-)fields that represent a whole variable. */
273 unsigned int is_full_var : 1;
275 /* True if this is a heap variable. */
276 unsigned int is_heap_var : 1;
278 /* True if this field may contain pointers. */
279 unsigned int may_have_pointers : 1;
281 /* True if this field has only restrict qualified pointers. */
282 unsigned int only_restrict_pointers : 1;
284 /* True if this represents a global variable. */
285 unsigned int is_global_var : 1;
287 /* True if this represents a IPA function info. */
288 unsigned int is_fn_info : 1;
290 /* The ID of the variable for the next field in this structure
291 or zero for the last field in this structure. */
292 unsigned next;
294 /* The ID of the variable for the first field in this structure. */
295 unsigned head;
297 /* Offset of this variable, in bits, from the base variable */
298 unsigned HOST_WIDE_INT offset;
300 /* Size of the variable, in bits. */
301 unsigned HOST_WIDE_INT size;
303 /* Full size of the base variable, in bits. */
304 unsigned HOST_WIDE_INT fullsize;
306 /* Name of this variable */
307 const char *name;
309 /* Tree that this variable is associated with. */
310 tree decl;
312 /* Points-to set for this variable. */
313 bitmap solution;
315 /* Old points-to set for this variable. */
316 bitmap oldsolution;
318 typedef struct variable_info *varinfo_t;
320 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
321 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
322 unsigned HOST_WIDE_INT);
323 static varinfo_t lookup_vi_for_tree (tree);
324 static inline bool type_can_have_subvars (const_tree);
326 /* Pool of variable info structures. */
327 static alloc_pool variable_info_pool;
329 /* Map varinfo to final pt_solution. */
330 static hash_map<varinfo_t, pt_solution *> *final_solutions;
331 struct obstack final_solutions_obstack;
333 /* Table of variable info structures for constraint variables.
334 Indexed directly by variable info id. */
335 static vec<varinfo_t> varmap;
337 /* Return the varmap element N */
339 static inline varinfo_t
340 get_varinfo (unsigned int n)
342 return varmap[n];
345 /* Return the next variable in the list of sub-variables of VI
346 or NULL if VI is the last sub-variable. */
348 static inline varinfo_t
349 vi_next (varinfo_t vi)
351 return get_varinfo (vi->next);
354 /* Static IDs for the special variables. Variable ID zero is unused
355 and used as terminator for the sub-variable chain. */
356 enum { nothing_id = 1, anything_id = 2, string_id = 3,
357 escaped_id = 4, nonlocal_id = 5,
358 storedanything_id = 6, integer_id = 7 };
360 /* Return a new variable info structure consisting for a variable
361 named NAME, and using constraint graph node NODE. Append it
362 to the vector of variable info structures. */
364 static varinfo_t
365 new_var_info (tree t, const char *name)
367 unsigned index = varmap.length ();
368 varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
370 ret->id = index;
371 ret->name = name;
372 ret->decl = t;
373 /* Vars without decl are artificial and do not have sub-variables. */
374 ret->is_artificial_var = (t == NULL_TREE);
375 ret->is_special_var = false;
376 ret->is_unknown_size_var = false;
377 ret->is_full_var = (t == NULL_TREE);
378 ret->is_heap_var = false;
379 ret->may_have_pointers = true;
380 ret->only_restrict_pointers = false;
381 ret->is_global_var = (t == NULL_TREE);
382 ret->is_fn_info = false;
383 if (t && DECL_P (t))
384 ret->is_global_var = (is_global_var (t)
385 /* We have to treat even local register variables
386 as escape points. */
387 || (TREE_CODE (t) == VAR_DECL
388 && DECL_HARD_REGISTER (t)));
389 ret->solution = BITMAP_ALLOC (&pta_obstack);
390 ret->oldsolution = NULL;
391 ret->next = 0;
392 ret->head = ret->id;
394 stats.total_vars++;
396 varmap.safe_push (ret);
398 return ret;
402 /* A map mapping call statements to per-stmt variables for uses
403 and clobbers specific to the call. */
404 static hash_map<gimple, varinfo_t> *call_stmt_vars;
406 /* Lookup or create the variable for the call statement CALL. */
408 static varinfo_t
409 get_call_vi (gimple call)
411 varinfo_t vi, vi2;
413 bool existed;
414 varinfo_t *slot_p = &call_stmt_vars->get_or_insert (call, &existed);
415 if (existed)
416 return *slot_p;
418 vi = new_var_info (NULL_TREE, "CALLUSED");
419 vi->offset = 0;
420 vi->size = 1;
421 vi->fullsize = 2;
422 vi->is_full_var = true;
424 vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED");
425 vi2->offset = 1;
426 vi2->size = 1;
427 vi2->fullsize = 2;
428 vi2->is_full_var = true;
430 vi->next = vi2->id;
432 *slot_p = vi;
433 return vi;
436 /* Lookup the variable for the call statement CALL representing
437 the uses. Returns NULL if there is nothing special about this call. */
439 static varinfo_t
440 lookup_call_use_vi (gimple call)
442 varinfo_t *slot_p = call_stmt_vars->get (call);
443 if (slot_p)
444 return *slot_p;
446 return NULL;
449 /* Lookup the variable for the call statement CALL representing
450 the clobbers. Returns NULL if there is nothing special about this call. */
452 static varinfo_t
453 lookup_call_clobber_vi (gimple call)
455 varinfo_t uses = lookup_call_use_vi (call);
456 if (!uses)
457 return NULL;
459 return vi_next (uses);
462 /* Lookup or create the variable for the call statement CALL representing
463 the uses. */
465 static varinfo_t
466 get_call_use_vi (gimple call)
468 return get_call_vi (call);
471 /* Lookup or create the variable for the call statement CALL representing
472 the clobbers. */
474 static varinfo_t ATTRIBUTE_UNUSED
475 get_call_clobber_vi (gimple call)
477 return vi_next (get_call_vi (call));
481 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
483 /* An expression that appears in a constraint. */
485 struct constraint_expr
487 /* Constraint type. */
488 constraint_expr_type type;
490 /* Variable we are referring to in the constraint. */
491 unsigned int var;
493 /* Offset, in bits, of this constraint from the beginning of
494 variables it ends up referring to.
496 IOW, in a deref constraint, we would deref, get the result set,
497 then add OFFSET to each member. */
498 HOST_WIDE_INT offset;
501 /* Use 0x8000... as special unknown offset. */
502 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
504 typedef struct constraint_expr ce_s;
505 static void get_constraint_for_1 (tree, vec<ce_s> *, bool, bool);
506 static void get_constraint_for (tree, vec<ce_s> *);
507 static void get_constraint_for_rhs (tree, vec<ce_s> *);
508 static void do_deref (vec<ce_s> *);
510 /* Our set constraints are made up of two constraint expressions, one
511 LHS, and one RHS.
513 As described in the introduction, our set constraints each represent an
514 operation between set valued variables.
516 struct constraint
518 struct constraint_expr lhs;
519 struct constraint_expr rhs;
522 /* List of constraints that we use to build the constraint graph from. */
524 static vec<constraint_t> constraints;
525 static alloc_pool constraint_pool;
527 /* The constraint graph is represented as an array of bitmaps
528 containing successor nodes. */
530 struct constraint_graph
532 /* Size of this graph, which may be different than the number of
533 nodes in the variable map. */
534 unsigned int size;
536 /* Explicit successors of each node. */
537 bitmap *succs;
539 /* Implicit predecessors of each node (Used for variable
540 substitution). */
541 bitmap *implicit_preds;
543 /* Explicit predecessors of each node (Used for variable substitution). */
544 bitmap *preds;
546 /* Indirect cycle representatives, or -1 if the node has no indirect
547 cycles. */
548 int *indirect_cycles;
550 /* Representative node for a node. rep[a] == a unless the node has
551 been unified. */
552 unsigned int *rep;
554 /* Equivalence class representative for a label. This is used for
555 variable substitution. */
556 int *eq_rep;
558 /* Pointer equivalence label for a node. All nodes with the same
559 pointer equivalence label can be unified together at some point
560 (either during constraint optimization or after the constraint
561 graph is built). */
562 unsigned int *pe;
564 /* Pointer equivalence representative for a label. This is used to
565 handle nodes that are pointer equivalent but not location
566 equivalent. We can unite these once the addressof constraints
567 are transformed into initial points-to sets. */
568 int *pe_rep;
570 /* Pointer equivalence label for each node, used during variable
571 substitution. */
572 unsigned int *pointer_label;
574 /* Location equivalence label for each node, used during location
575 equivalence finding. */
576 unsigned int *loc_label;
578 /* Pointed-by set for each node, used during location equivalence
579 finding. This is pointed-by rather than pointed-to, because it
580 is constructed using the predecessor graph. */
581 bitmap *pointed_by;
583 /* Points to sets for pointer equivalence. This is *not* the actual
584 points-to sets for nodes. */
585 bitmap *points_to;
587 /* Bitmap of nodes where the bit is set if the node is a direct
588 node. Used for variable substitution. */
589 sbitmap direct_nodes;
591 /* Bitmap of nodes where the bit is set if the node is address
592 taken. Used for variable substitution. */
593 bitmap address_taken;
595 /* Vector of complex constraints for each graph node. Complex
596 constraints are those involving dereferences or offsets that are
597 not 0. */
598 vec<constraint_t> *complex;
601 static constraint_graph_t graph;
603 /* During variable substitution and the offline version of indirect
604 cycle finding, we create nodes to represent dereferences and
605 address taken constraints. These represent where these start and
606 end. */
607 #define FIRST_REF_NODE (varmap).length ()
608 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
610 /* Return the representative node for NODE, if NODE has been unioned
611 with another NODE.
612 This function performs path compression along the way to finding
613 the representative. */
615 static unsigned int
616 find (unsigned int node)
618 gcc_checking_assert (node < graph->size);
619 if (graph->rep[node] != node)
620 return graph->rep[node] = find (graph->rep[node]);
621 return node;
624 /* Union the TO and FROM nodes to the TO nodes.
625 Note that at some point in the future, we may want to do
626 union-by-rank, in which case we are going to have to return the
627 node we unified to. */
629 static bool
630 unite (unsigned int to, unsigned int from)
632 gcc_checking_assert (to < graph->size && from < graph->size);
633 if (to != from && graph->rep[from] != to)
635 graph->rep[from] = to;
636 return true;
638 return false;
641 /* Create a new constraint consisting of LHS and RHS expressions. */
643 static constraint_t
644 new_constraint (const struct constraint_expr lhs,
645 const struct constraint_expr rhs)
647 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
648 ret->lhs = lhs;
649 ret->rhs = rhs;
650 return ret;
653 /* Print out constraint C to FILE. */
655 static void
656 dump_constraint (FILE *file, constraint_t c)
658 if (c->lhs.type == ADDRESSOF)
659 fprintf (file, "&");
660 else if (c->lhs.type == DEREF)
661 fprintf (file, "*");
662 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
663 if (c->lhs.offset == UNKNOWN_OFFSET)
664 fprintf (file, " + UNKNOWN");
665 else if (c->lhs.offset != 0)
666 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
667 fprintf (file, " = ");
668 if (c->rhs.type == ADDRESSOF)
669 fprintf (file, "&");
670 else if (c->rhs.type == DEREF)
671 fprintf (file, "*");
672 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
673 if (c->rhs.offset == UNKNOWN_OFFSET)
674 fprintf (file, " + UNKNOWN");
675 else if (c->rhs.offset != 0)
676 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
680 void debug_constraint (constraint_t);
681 void debug_constraints (void);
682 void debug_constraint_graph (void);
683 void debug_solution_for_var (unsigned int);
684 void debug_sa_points_to_info (void);
686 /* Print out constraint C to stderr. */
688 DEBUG_FUNCTION void
689 debug_constraint (constraint_t c)
691 dump_constraint (stderr, c);
692 fprintf (stderr, "\n");
695 /* Print out all constraints to FILE */
697 static void
698 dump_constraints (FILE *file, int from)
700 int i;
701 constraint_t c;
702 for (i = from; constraints.iterate (i, &c); i++)
703 if (c)
705 dump_constraint (file, c);
706 fprintf (file, "\n");
710 /* Print out all constraints to stderr. */
712 DEBUG_FUNCTION void
713 debug_constraints (void)
715 dump_constraints (stderr, 0);
718 /* Print the constraint graph in dot format. */
720 static void
721 dump_constraint_graph (FILE *file)
723 unsigned int i;
725 /* Only print the graph if it has already been initialized: */
726 if (!graph)
727 return;
729 /* Prints the header of the dot file: */
730 fprintf (file, "strict digraph {\n");
731 fprintf (file, " node [\n shape = box\n ]\n");
732 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
733 fprintf (file, "\n // List of nodes and complex constraints in "
734 "the constraint graph:\n");
736 /* The next lines print the nodes in the graph together with the
737 complex constraints attached to them. */
738 for (i = 1; i < graph->size; i++)
740 if (i == FIRST_REF_NODE)
741 continue;
742 if (find (i) != i)
743 continue;
744 if (i < FIRST_REF_NODE)
745 fprintf (file, "\"%s\"", get_varinfo (i)->name);
746 else
747 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
748 if (graph->complex[i].exists ())
750 unsigned j;
751 constraint_t c;
752 fprintf (file, " [label=\"\\N\\n");
753 for (j = 0; graph->complex[i].iterate (j, &c); ++j)
755 dump_constraint (file, c);
756 fprintf (file, "\\l");
758 fprintf (file, "\"]");
760 fprintf (file, ";\n");
763 /* Go over the edges. */
764 fprintf (file, "\n // Edges in the constraint graph:\n");
765 for (i = 1; i < graph->size; i++)
767 unsigned j;
768 bitmap_iterator bi;
769 if (find (i) != i)
770 continue;
771 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
773 unsigned to = find (j);
774 if (i == to)
775 continue;
776 if (i < FIRST_REF_NODE)
777 fprintf (file, "\"%s\"", get_varinfo (i)->name);
778 else
779 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
780 fprintf (file, " -> ");
781 if (to < FIRST_REF_NODE)
782 fprintf (file, "\"%s\"", get_varinfo (to)->name);
783 else
784 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name);
785 fprintf (file, ";\n");
789 /* Prints the tail of the dot file. */
790 fprintf (file, "}\n");
793 /* Print out the constraint graph to stderr. */
795 DEBUG_FUNCTION void
796 debug_constraint_graph (void)
798 dump_constraint_graph (stderr);
801 /* SOLVER FUNCTIONS
803 The solver is a simple worklist solver, that works on the following
804 algorithm:
806 sbitmap changed_nodes = all zeroes;
807 changed_count = 0;
808 For each node that is not already collapsed:
809 changed_count++;
810 set bit in changed nodes
812 while (changed_count > 0)
814 compute topological ordering for constraint graph
816 find and collapse cycles in the constraint graph (updating
817 changed if necessary)
819 for each node (n) in the graph in topological order:
820 changed_count--;
822 Process each complex constraint associated with the node,
823 updating changed if necessary.
825 For each outgoing edge from n, propagate the solution from n to
826 the destination of the edge, updating changed as necessary.
828 } */
830 /* Return true if two constraint expressions A and B are equal. */
832 static bool
833 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
835 return a.type == b.type && a.var == b.var && a.offset == b.offset;
838 /* Return true if constraint expression A is less than constraint expression
839 B. This is just arbitrary, but consistent, in order to give them an
840 ordering. */
842 static bool
843 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
845 if (a.type == b.type)
847 if (a.var == b.var)
848 return a.offset < b.offset;
849 else
850 return a.var < b.var;
852 else
853 return a.type < b.type;
856 /* Return true if constraint A is less than constraint B. This is just
857 arbitrary, but consistent, in order to give them an ordering. */
859 static bool
860 constraint_less (const constraint_t &a, const constraint_t &b)
862 if (constraint_expr_less (a->lhs, b->lhs))
863 return true;
864 else if (constraint_expr_less (b->lhs, a->lhs))
865 return false;
866 else
867 return constraint_expr_less (a->rhs, b->rhs);
870 /* Return true if two constraints A and B are equal. */
872 static bool
873 constraint_equal (struct constraint a, struct constraint b)
875 return constraint_expr_equal (a.lhs, b.lhs)
876 && constraint_expr_equal (a.rhs, b.rhs);
880 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
882 static constraint_t
883 constraint_vec_find (vec<constraint_t> vec,
884 struct constraint lookfor)
886 unsigned int place;
887 constraint_t found;
889 if (!vec.exists ())
890 return NULL;
892 place = vec.lower_bound (&lookfor, constraint_less);
893 if (place >= vec.length ())
894 return NULL;
895 found = vec[place];
896 if (!constraint_equal (*found, lookfor))
897 return NULL;
898 return found;
901 /* Union two constraint vectors, TO and FROM. Put the result in TO.
902 Returns true of TO set is changed. */
904 static bool
905 constraint_set_union (vec<constraint_t> *to,
906 vec<constraint_t> *from)
908 int i;
909 constraint_t c;
910 bool any_change = false;
912 FOR_EACH_VEC_ELT (*from, i, c)
914 if (constraint_vec_find (*to, *c) == NULL)
916 unsigned int place = to->lower_bound (c, constraint_less);
917 to->safe_insert (place, c);
918 any_change = true;
921 return any_change;
924 /* Expands the solution in SET to all sub-fields of variables included. */
926 static bitmap
927 solution_set_expand (bitmap set, bitmap *expanded)
929 bitmap_iterator bi;
930 unsigned j;
932 if (*expanded)
933 return *expanded;
935 *expanded = BITMAP_ALLOC (&iteration_obstack);
937 /* In a first pass expand to the head of the variables we need to
938 add all sub-fields off. This avoids quadratic behavior. */
939 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
941 varinfo_t v = get_varinfo (j);
942 if (v->is_artificial_var
943 || v->is_full_var)
944 continue;
945 bitmap_set_bit (*expanded, v->head);
948 /* In the second pass now expand all head variables with subfields. */
949 EXECUTE_IF_SET_IN_BITMAP (*expanded, 0, j, bi)
951 varinfo_t v = get_varinfo (j);
952 if (v->head != j)
953 continue;
954 for (v = vi_next (v); v != NULL; v = vi_next (v))
955 bitmap_set_bit (*expanded, v->id);
958 /* And finally set the rest of the bits from SET. */
959 bitmap_ior_into (*expanded, set);
961 return *expanded;
964 /* Union solution sets TO and DELTA, and add INC to each member of DELTA in the
965 process. */
967 static bool
968 set_union_with_increment (bitmap to, bitmap delta, HOST_WIDE_INT inc,
969 bitmap *expanded_delta)
971 bool changed = false;
972 bitmap_iterator bi;
973 unsigned int i;
975 /* If the solution of DELTA contains anything it is good enough to transfer
976 this to TO. */
977 if (bitmap_bit_p (delta, anything_id))
978 return bitmap_set_bit (to, anything_id);
980 /* If the offset is unknown we have to expand the solution to
981 all subfields. */
982 if (inc == UNKNOWN_OFFSET)
984 delta = solution_set_expand (delta, expanded_delta);
985 changed |= bitmap_ior_into (to, delta);
986 return changed;
989 /* For non-zero offset union the offsetted solution into the destination. */
990 EXECUTE_IF_SET_IN_BITMAP (delta, 0, i, bi)
992 varinfo_t vi = get_varinfo (i);
994 /* If this is a variable with just one field just set its bit
995 in the result. */
996 if (vi->is_artificial_var
997 || vi->is_unknown_size_var
998 || vi->is_full_var)
999 changed |= bitmap_set_bit (to, i);
1000 else
1002 HOST_WIDE_INT fieldoffset = vi->offset + inc;
1003 unsigned HOST_WIDE_INT size = vi->size;
1005 /* If the offset makes the pointer point to before the
1006 variable use offset zero for the field lookup. */
1007 if (fieldoffset < 0)
1008 vi = get_varinfo (vi->head);
1009 else
1010 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
1014 changed |= bitmap_set_bit (to, vi->id);
1015 if (vi->is_full_var
1016 || vi->next == 0)
1017 break;
1019 /* We have to include all fields that overlap the current field
1020 shifted by inc. */
1021 vi = vi_next (vi);
1023 while (vi->offset < fieldoffset + size);
1027 return changed;
1030 /* Insert constraint C into the list of complex constraints for graph
1031 node VAR. */
1033 static void
1034 insert_into_complex (constraint_graph_t graph,
1035 unsigned int var, constraint_t c)
1037 vec<constraint_t> complex = graph->complex[var];
1038 unsigned int place = complex.lower_bound (c, constraint_less);
1040 /* Only insert constraints that do not already exist. */
1041 if (place >= complex.length ()
1042 || !constraint_equal (*c, *complex[place]))
1043 graph->complex[var].safe_insert (place, c);
1047 /* Condense two variable nodes into a single variable node, by moving
1048 all associated info from FROM to TO. Returns true if TO node's
1049 constraint set changes after the merge. */
1051 static bool
1052 merge_node_constraints (constraint_graph_t graph, unsigned int to,
1053 unsigned int from)
1055 unsigned int i;
1056 constraint_t c;
1057 bool any_change = false;
1059 gcc_checking_assert (find (from) == to);
1061 /* Move all complex constraints from src node into to node */
1062 FOR_EACH_VEC_ELT (graph->complex[from], i, c)
1064 /* In complex constraints for node FROM, we may have either
1065 a = *FROM, and *FROM = a, or an offseted constraint which are
1066 always added to the rhs node's constraints. */
1068 if (c->rhs.type == DEREF)
1069 c->rhs.var = to;
1070 else if (c->lhs.type == DEREF)
1071 c->lhs.var = to;
1072 else
1073 c->rhs.var = to;
1076 any_change = constraint_set_union (&graph->complex[to],
1077 &graph->complex[from]);
1078 graph->complex[from].release ();
1079 return any_change;
1083 /* Remove edges involving NODE from GRAPH. */
1085 static void
1086 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
1088 if (graph->succs[node])
1089 BITMAP_FREE (graph->succs[node]);
1092 /* Merge GRAPH nodes FROM and TO into node TO. */
1094 static void
1095 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1096 unsigned int from)
1098 if (graph->indirect_cycles[from] != -1)
1100 /* If we have indirect cycles with the from node, and we have
1101 none on the to node, the to node has indirect cycles from the
1102 from node now that they are unified.
1103 If indirect cycles exist on both, unify the nodes that they
1104 are in a cycle with, since we know they are in a cycle with
1105 each other. */
1106 if (graph->indirect_cycles[to] == -1)
1107 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1110 /* Merge all the successor edges. */
1111 if (graph->succs[from])
1113 if (!graph->succs[to])
1114 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1115 bitmap_ior_into (graph->succs[to],
1116 graph->succs[from]);
1119 clear_edges_for_node (graph, from);
1123 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1124 it doesn't exist in the graph already. */
1126 static void
1127 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1128 unsigned int from)
1130 if (to == from)
1131 return;
1133 if (!graph->implicit_preds[to])
1134 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1136 if (bitmap_set_bit (graph->implicit_preds[to], from))
1137 stats.num_implicit_edges++;
1140 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1141 it doesn't exist in the graph already.
1142 Return false if the edge already existed, true otherwise. */
1144 static void
1145 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1146 unsigned int from)
1148 if (!graph->preds[to])
1149 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1150 bitmap_set_bit (graph->preds[to], from);
1153 /* Add a graph edge to GRAPH, going from FROM to TO if
1154 it doesn't exist in the graph already.
1155 Return false if the edge already existed, true otherwise. */
1157 static bool
1158 add_graph_edge (constraint_graph_t graph, unsigned int to,
1159 unsigned int from)
1161 if (to == from)
1163 return false;
1165 else
1167 bool r = false;
1169 if (!graph->succs[from])
1170 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1171 if (bitmap_set_bit (graph->succs[from], to))
1173 r = true;
1174 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1175 stats.num_edges++;
1177 return r;
1182 /* Initialize the constraint graph structure to contain SIZE nodes. */
1184 static void
1185 init_graph (unsigned int size)
1187 unsigned int j;
1189 graph = XCNEW (struct constraint_graph);
1190 graph->size = size;
1191 graph->succs = XCNEWVEC (bitmap, graph->size);
1192 graph->indirect_cycles = XNEWVEC (int, graph->size);
1193 graph->rep = XNEWVEC (unsigned int, graph->size);
1194 /* ??? Macros do not support template types with multiple arguments,
1195 so we use a typedef to work around it. */
1196 typedef vec<constraint_t> vec_constraint_t_heap;
1197 graph->complex = XCNEWVEC (vec_constraint_t_heap, size);
1198 graph->pe = XCNEWVEC (unsigned int, graph->size);
1199 graph->pe_rep = XNEWVEC (int, graph->size);
1201 for (j = 0; j < graph->size; j++)
1203 graph->rep[j] = j;
1204 graph->pe_rep[j] = -1;
1205 graph->indirect_cycles[j] = -1;
1209 /* Build the constraint graph, adding only predecessor edges right now. */
1211 static void
1212 build_pred_graph (void)
1214 int i;
1215 constraint_t c;
1216 unsigned int j;
1218 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1219 graph->preds = XCNEWVEC (bitmap, graph->size);
1220 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1221 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1222 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1223 graph->points_to = XCNEWVEC (bitmap, graph->size);
1224 graph->eq_rep = XNEWVEC (int, graph->size);
1225 graph->direct_nodes = sbitmap_alloc (graph->size);
1226 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1227 bitmap_clear (graph->direct_nodes);
1229 for (j = 1; j < FIRST_REF_NODE; j++)
1231 if (!get_varinfo (j)->is_special_var)
1232 bitmap_set_bit (graph->direct_nodes, j);
1235 for (j = 0; j < graph->size; j++)
1236 graph->eq_rep[j] = -1;
1238 for (j = 0; j < varmap.length (); j++)
1239 graph->indirect_cycles[j] = -1;
1241 FOR_EACH_VEC_ELT (constraints, i, c)
1243 struct constraint_expr lhs = c->lhs;
1244 struct constraint_expr rhs = c->rhs;
1245 unsigned int lhsvar = lhs.var;
1246 unsigned int rhsvar = rhs.var;
1248 if (lhs.type == DEREF)
1250 /* *x = y. */
1251 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1252 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1254 else if (rhs.type == DEREF)
1256 /* x = *y */
1257 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1258 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1259 else
1260 bitmap_clear_bit (graph->direct_nodes, lhsvar);
1262 else if (rhs.type == ADDRESSOF)
1264 varinfo_t v;
1266 /* x = &y */
1267 if (graph->points_to[lhsvar] == NULL)
1268 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1269 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1271 if (graph->pointed_by[rhsvar] == NULL)
1272 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1273 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1275 /* Implicitly, *x = y */
1276 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1278 /* All related variables are no longer direct nodes. */
1279 bitmap_clear_bit (graph->direct_nodes, rhsvar);
1280 v = get_varinfo (rhsvar);
1281 if (!v->is_full_var)
1283 v = get_varinfo (v->head);
1286 bitmap_clear_bit (graph->direct_nodes, v->id);
1287 v = vi_next (v);
1289 while (v != NULL);
1291 bitmap_set_bit (graph->address_taken, rhsvar);
1293 else if (lhsvar > anything_id
1294 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1296 /* x = y */
1297 add_pred_graph_edge (graph, lhsvar, rhsvar);
1298 /* Implicitly, *x = *y */
1299 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1300 FIRST_REF_NODE + rhsvar);
1302 else if (lhs.offset != 0 || rhs.offset != 0)
1304 if (rhs.offset != 0)
1305 bitmap_clear_bit (graph->direct_nodes, lhs.var);
1306 else if (lhs.offset != 0)
1307 bitmap_clear_bit (graph->direct_nodes, rhs.var);
1312 /* Build the constraint graph, adding successor edges. */
1314 static void
1315 build_succ_graph (void)
1317 unsigned i, t;
1318 constraint_t c;
1320 FOR_EACH_VEC_ELT (constraints, i, c)
1322 struct constraint_expr lhs;
1323 struct constraint_expr rhs;
1324 unsigned int lhsvar;
1325 unsigned int rhsvar;
1327 if (!c)
1328 continue;
1330 lhs = c->lhs;
1331 rhs = c->rhs;
1332 lhsvar = find (lhs.var);
1333 rhsvar = find (rhs.var);
1335 if (lhs.type == DEREF)
1337 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1338 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1340 else if (rhs.type == DEREF)
1342 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1343 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1345 else if (rhs.type == ADDRESSOF)
1347 /* x = &y */
1348 gcc_checking_assert (find (rhs.var) == rhs.var);
1349 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1351 else if (lhsvar > anything_id
1352 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1354 add_graph_edge (graph, lhsvar, rhsvar);
1358 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1359 receive pointers. */
1360 t = find (storedanything_id);
1361 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1363 if (!bitmap_bit_p (graph->direct_nodes, i)
1364 && get_varinfo (i)->may_have_pointers)
1365 add_graph_edge (graph, find (i), t);
1368 /* Everything stored to ANYTHING also potentially escapes. */
1369 add_graph_edge (graph, find (escaped_id), t);
1373 /* Changed variables on the last iteration. */
1374 static bitmap changed;
1376 /* Strongly Connected Component visitation info. */
1378 struct scc_info
1380 sbitmap visited;
1381 sbitmap deleted;
1382 unsigned int *dfs;
1383 unsigned int *node_mapping;
1384 int current_index;
1385 vec<unsigned> scc_stack;
1389 /* Recursive routine to find strongly connected components in GRAPH.
1390 SI is the SCC info to store the information in, and N is the id of current
1391 graph node we are processing.
1393 This is Tarjan's strongly connected component finding algorithm, as
1394 modified by Nuutila to keep only non-root nodes on the stack.
1395 The algorithm can be found in "On finding the strongly connected
1396 connected components in a directed graph" by Esko Nuutila and Eljas
1397 Soisalon-Soininen, in Information Processing Letters volume 49,
1398 number 1, pages 9-14. */
1400 static void
1401 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1403 unsigned int i;
1404 bitmap_iterator bi;
1405 unsigned int my_dfs;
1407 bitmap_set_bit (si->visited, n);
1408 si->dfs[n] = si->current_index ++;
1409 my_dfs = si->dfs[n];
1411 /* Visit all the successors. */
1412 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1414 unsigned int w;
1416 if (i > LAST_REF_NODE)
1417 break;
1419 w = find (i);
1420 if (bitmap_bit_p (si->deleted, w))
1421 continue;
1423 if (!bitmap_bit_p (si->visited, w))
1424 scc_visit (graph, si, w);
1426 unsigned int t = find (w);
1427 gcc_checking_assert (find (n) == n);
1428 if (si->dfs[t] < si->dfs[n])
1429 si->dfs[n] = si->dfs[t];
1432 /* See if any components have been identified. */
1433 if (si->dfs[n] == my_dfs)
1435 if (si->scc_stack.length () > 0
1436 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1438 bitmap scc = BITMAP_ALLOC (NULL);
1439 unsigned int lowest_node;
1440 bitmap_iterator bi;
1442 bitmap_set_bit (scc, n);
1444 while (si->scc_stack.length () != 0
1445 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1447 unsigned int w = si->scc_stack.pop ();
1449 bitmap_set_bit (scc, w);
1452 lowest_node = bitmap_first_set_bit (scc);
1453 gcc_assert (lowest_node < FIRST_REF_NODE);
1455 /* Collapse the SCC nodes into a single node, and mark the
1456 indirect cycles. */
1457 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1459 if (i < FIRST_REF_NODE)
1461 if (unite (lowest_node, i))
1462 unify_nodes (graph, lowest_node, i, false);
1464 else
1466 unite (lowest_node, i);
1467 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1471 bitmap_set_bit (si->deleted, n);
1473 else
1474 si->scc_stack.safe_push (n);
1477 /* Unify node FROM into node TO, updating the changed count if
1478 necessary when UPDATE_CHANGED is true. */
1480 static void
1481 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1482 bool update_changed)
1484 gcc_checking_assert (to != from && find (to) == to);
1486 if (dump_file && (dump_flags & TDF_DETAILS))
1487 fprintf (dump_file, "Unifying %s to %s\n",
1488 get_varinfo (from)->name,
1489 get_varinfo (to)->name);
1491 if (update_changed)
1492 stats.unified_vars_dynamic++;
1493 else
1494 stats.unified_vars_static++;
1496 merge_graph_nodes (graph, to, from);
1497 if (merge_node_constraints (graph, to, from))
1499 if (update_changed)
1500 bitmap_set_bit (changed, to);
1503 /* Mark TO as changed if FROM was changed. If TO was already marked
1504 as changed, decrease the changed count. */
1506 if (update_changed
1507 && bitmap_clear_bit (changed, from))
1508 bitmap_set_bit (changed, to);
1509 varinfo_t fromvi = get_varinfo (from);
1510 if (fromvi->solution)
1512 /* If the solution changes because of the merging, we need to mark
1513 the variable as changed. */
1514 varinfo_t tovi = get_varinfo (to);
1515 if (bitmap_ior_into (tovi->solution, fromvi->solution))
1517 if (update_changed)
1518 bitmap_set_bit (changed, to);
1521 BITMAP_FREE (fromvi->solution);
1522 if (fromvi->oldsolution)
1523 BITMAP_FREE (fromvi->oldsolution);
1525 if (stats.iterations > 0
1526 && tovi->oldsolution)
1527 BITMAP_FREE (tovi->oldsolution);
1529 if (graph->succs[to])
1530 bitmap_clear_bit (graph->succs[to], to);
1533 /* Information needed to compute the topological ordering of a graph. */
1535 struct topo_info
1537 /* sbitmap of visited nodes. */
1538 sbitmap visited;
1539 /* Array that stores the topological order of the graph, *in
1540 reverse*. */
1541 vec<unsigned> topo_order;
1545 /* Initialize and return a topological info structure. */
1547 static struct topo_info *
1548 init_topo_info (void)
1550 size_t size = graph->size;
1551 struct topo_info *ti = XNEW (struct topo_info);
1552 ti->visited = sbitmap_alloc (size);
1553 bitmap_clear (ti->visited);
1554 ti->topo_order.create (1);
1555 return ti;
1559 /* Free the topological sort info pointed to by TI. */
1561 static void
1562 free_topo_info (struct topo_info *ti)
1564 sbitmap_free (ti->visited);
1565 ti->topo_order.release ();
1566 free (ti);
1569 /* Visit the graph in topological order, and store the order in the
1570 topo_info structure. */
1572 static void
1573 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1574 unsigned int n)
1576 bitmap_iterator bi;
1577 unsigned int j;
1579 bitmap_set_bit (ti->visited, n);
1581 if (graph->succs[n])
1582 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1584 if (!bitmap_bit_p (ti->visited, j))
1585 topo_visit (graph, ti, j);
1588 ti->topo_order.safe_push (n);
1591 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1592 starting solution for y. */
1594 static void
1595 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1596 bitmap delta, bitmap *expanded_delta)
1598 unsigned int lhs = c->lhs.var;
1599 bool flag = false;
1600 bitmap sol = get_varinfo (lhs)->solution;
1601 unsigned int j;
1602 bitmap_iterator bi;
1603 HOST_WIDE_INT roffset = c->rhs.offset;
1605 /* Our IL does not allow this. */
1606 gcc_checking_assert (c->lhs.offset == 0);
1608 /* If the solution of Y contains anything it is good enough to transfer
1609 this to the LHS. */
1610 if (bitmap_bit_p (delta, anything_id))
1612 flag |= bitmap_set_bit (sol, anything_id);
1613 goto done;
1616 /* If we do not know at with offset the rhs is dereferenced compute
1617 the reachability set of DELTA, conservatively assuming it is
1618 dereferenced at all valid offsets. */
1619 if (roffset == UNKNOWN_OFFSET)
1621 delta = solution_set_expand (delta, expanded_delta);
1622 /* No further offset processing is necessary. */
1623 roffset = 0;
1626 /* For each variable j in delta (Sol(y)), add
1627 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1628 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1630 varinfo_t v = get_varinfo (j);
1631 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1632 unsigned HOST_WIDE_INT size = v->size;
1633 unsigned int t;
1635 if (v->is_full_var)
1637 else if (roffset != 0)
1639 if (fieldoffset < 0)
1640 v = get_varinfo (v->head);
1641 else
1642 v = first_or_preceding_vi_for_offset (v, fieldoffset);
1645 /* We have to include all fields that overlap the current field
1646 shifted by roffset. */
1649 t = find (v->id);
1651 /* Adding edges from the special vars is pointless.
1652 They don't have sets that can change. */
1653 if (get_varinfo (t)->is_special_var)
1654 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1655 /* Merging the solution from ESCAPED needlessly increases
1656 the set. Use ESCAPED as representative instead. */
1657 else if (v->id == escaped_id)
1658 flag |= bitmap_set_bit (sol, escaped_id);
1659 else if (v->may_have_pointers
1660 && add_graph_edge (graph, lhs, t))
1661 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1663 if (v->is_full_var
1664 || v->next == 0)
1665 break;
1667 v = vi_next (v);
1669 while (v->offset < fieldoffset + size);
1672 done:
1673 /* If the LHS solution changed, mark the var as changed. */
1674 if (flag)
1676 get_varinfo (lhs)->solution = sol;
1677 bitmap_set_bit (changed, lhs);
1681 /* Process a constraint C that represents *(x + off) = y using DELTA
1682 as the starting solution for x. */
1684 static void
1685 do_ds_constraint (constraint_t c, bitmap delta, bitmap *expanded_delta)
1687 unsigned int rhs = c->rhs.var;
1688 bitmap sol = get_varinfo (rhs)->solution;
1689 unsigned int j;
1690 bitmap_iterator bi;
1691 HOST_WIDE_INT loff = c->lhs.offset;
1692 bool escaped_p = false;
1694 /* Our IL does not allow this. */
1695 gcc_checking_assert (c->rhs.offset == 0);
1697 /* If the solution of y contains ANYTHING simply use the ANYTHING
1698 solution. This avoids needlessly increasing the points-to sets. */
1699 if (bitmap_bit_p (sol, anything_id))
1700 sol = get_varinfo (find (anything_id))->solution;
1702 /* If the solution for x contains ANYTHING we have to merge the
1703 solution of y into all pointer variables which we do via
1704 STOREDANYTHING. */
1705 if (bitmap_bit_p (delta, anything_id))
1707 unsigned t = find (storedanything_id);
1708 if (add_graph_edge (graph, t, rhs))
1710 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1711 bitmap_set_bit (changed, t);
1713 return;
1716 /* If we do not know at with offset the rhs is dereferenced compute
1717 the reachability set of DELTA, conservatively assuming it is
1718 dereferenced at all valid offsets. */
1719 if (loff == UNKNOWN_OFFSET)
1721 delta = solution_set_expand (delta, expanded_delta);
1722 loff = 0;
1725 /* For each member j of delta (Sol(x)), add an edge from y to j and
1726 union Sol(y) into Sol(j) */
1727 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1729 varinfo_t v = get_varinfo (j);
1730 unsigned int t;
1731 HOST_WIDE_INT fieldoffset = v->offset + loff;
1732 unsigned HOST_WIDE_INT size = v->size;
1734 if (v->is_full_var)
1736 else if (loff != 0)
1738 if (fieldoffset < 0)
1739 v = get_varinfo (v->head);
1740 else
1741 v = first_or_preceding_vi_for_offset (v, fieldoffset);
1744 /* We have to include all fields that overlap the current field
1745 shifted by loff. */
1748 if (v->may_have_pointers)
1750 /* If v is a global variable then this is an escape point. */
1751 if (v->is_global_var
1752 && !escaped_p)
1754 t = find (escaped_id);
1755 if (add_graph_edge (graph, t, rhs)
1756 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1757 bitmap_set_bit (changed, t);
1758 /* Enough to let rhs escape once. */
1759 escaped_p = true;
1762 if (v->is_special_var)
1763 break;
1765 t = find (v->id);
1766 if (add_graph_edge (graph, t, rhs)
1767 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1768 bitmap_set_bit (changed, t);
1771 if (v->is_full_var
1772 || v->next == 0)
1773 break;
1775 v = vi_next (v);
1777 while (v->offset < fieldoffset + size);
1781 /* Handle a non-simple (simple meaning requires no iteration),
1782 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1784 static void
1785 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta,
1786 bitmap *expanded_delta)
1788 if (c->lhs.type == DEREF)
1790 if (c->rhs.type == ADDRESSOF)
1792 gcc_unreachable ();
1794 else
1796 /* *x = y */
1797 do_ds_constraint (c, delta, expanded_delta);
1800 else if (c->rhs.type == DEREF)
1802 /* x = *y */
1803 if (!(get_varinfo (c->lhs.var)->is_special_var))
1804 do_sd_constraint (graph, c, delta, expanded_delta);
1806 else
1808 bitmap tmp;
1809 bool flag = false;
1811 gcc_checking_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR
1812 && c->rhs.offset != 0 && c->lhs.offset == 0);
1813 tmp = get_varinfo (c->lhs.var)->solution;
1815 flag = set_union_with_increment (tmp, delta, c->rhs.offset,
1816 expanded_delta);
1818 if (flag)
1819 bitmap_set_bit (changed, c->lhs.var);
1823 /* Initialize and return a new SCC info structure. */
1825 static struct scc_info *
1826 init_scc_info (size_t size)
1828 struct scc_info *si = XNEW (struct scc_info);
1829 size_t i;
1831 si->current_index = 0;
1832 si->visited = sbitmap_alloc (size);
1833 bitmap_clear (si->visited);
1834 si->deleted = sbitmap_alloc (size);
1835 bitmap_clear (si->deleted);
1836 si->node_mapping = XNEWVEC (unsigned int, size);
1837 si->dfs = XCNEWVEC (unsigned int, size);
1839 for (i = 0; i < size; i++)
1840 si->node_mapping[i] = i;
1842 si->scc_stack.create (1);
1843 return si;
1846 /* Free an SCC info structure pointed to by SI */
1848 static void
1849 free_scc_info (struct scc_info *si)
1851 sbitmap_free (si->visited);
1852 sbitmap_free (si->deleted);
1853 free (si->node_mapping);
1854 free (si->dfs);
1855 si->scc_stack.release ();
1856 free (si);
1860 /* Find indirect cycles in GRAPH that occur, using strongly connected
1861 components, and note them in the indirect cycles map.
1863 This technique comes from Ben Hardekopf and Calvin Lin,
1864 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1865 Lines of Code", submitted to PLDI 2007. */
1867 static void
1868 find_indirect_cycles (constraint_graph_t graph)
1870 unsigned int i;
1871 unsigned int size = graph->size;
1872 struct scc_info *si = init_scc_info (size);
1874 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1875 if (!bitmap_bit_p (si->visited, i) && find (i) == i)
1876 scc_visit (graph, si, i);
1878 free_scc_info (si);
1881 /* Compute a topological ordering for GRAPH, and store the result in the
1882 topo_info structure TI. */
1884 static void
1885 compute_topo_order (constraint_graph_t graph,
1886 struct topo_info *ti)
1888 unsigned int i;
1889 unsigned int size = graph->size;
1891 for (i = 0; i != size; ++i)
1892 if (!bitmap_bit_p (ti->visited, i) && find (i) == i)
1893 topo_visit (graph, ti, i);
1896 /* Structure used to for hash value numbering of pointer equivalence
1897 classes. */
1899 typedef struct equiv_class_label
1901 hashval_t hashcode;
1902 unsigned int equivalence_class;
1903 bitmap labels;
1904 } *equiv_class_label_t;
1905 typedef const struct equiv_class_label *const_equiv_class_label_t;
1907 /* Equiv_class_label hashtable helpers. */
1909 struct equiv_class_hasher : typed_free_remove <equiv_class_label>
1911 typedef equiv_class_label value_type;
1912 typedef equiv_class_label compare_type;
1913 static inline hashval_t hash (const value_type *);
1914 static inline bool equal (const value_type *, const compare_type *);
1917 /* Hash function for a equiv_class_label_t */
1919 inline hashval_t
1920 equiv_class_hasher::hash (const value_type *ecl)
1922 return ecl->hashcode;
1925 /* Equality function for two equiv_class_label_t's. */
1927 inline bool
1928 equiv_class_hasher::equal (const value_type *eql1, const compare_type *eql2)
1930 return (eql1->hashcode == eql2->hashcode
1931 && bitmap_equal_p (eql1->labels, eql2->labels));
1934 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1935 classes. */
1936 static hash_table<equiv_class_hasher> *pointer_equiv_class_table;
1938 /* A hashtable for mapping a bitmap of labels->location equivalence
1939 classes. */
1940 static hash_table<equiv_class_hasher> *location_equiv_class_table;
1942 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1943 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1944 is equivalent to. */
1946 static equiv_class_label *
1947 equiv_class_lookup_or_add (hash_table<equiv_class_hasher> *table,
1948 bitmap labels)
1950 equiv_class_label **slot;
1951 equiv_class_label ecl;
1953 ecl.labels = labels;
1954 ecl.hashcode = bitmap_hash (labels);
1955 slot = table->find_slot (&ecl, INSERT);
1956 if (!*slot)
1958 *slot = XNEW (struct equiv_class_label);
1959 (*slot)->labels = labels;
1960 (*slot)->hashcode = ecl.hashcode;
1961 (*slot)->equivalence_class = 0;
1964 return *slot;
1967 /* Perform offline variable substitution.
1969 This is a worst case quadratic time way of identifying variables
1970 that must have equivalent points-to sets, including those caused by
1971 static cycles, and single entry subgraphs, in the constraint graph.
1973 The technique is described in "Exploiting Pointer and Location
1974 Equivalence to Optimize Pointer Analysis. In the 14th International
1975 Static Analysis Symposium (SAS), August 2007." It is known as the
1976 "HU" algorithm, and is equivalent to value numbering the collapsed
1977 constraint graph including evaluating unions.
1979 The general method of finding equivalence classes is as follows:
1980 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1981 Initialize all non-REF nodes to be direct nodes.
1982 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1983 variable}
1984 For each constraint containing the dereference, we also do the same
1985 thing.
1987 We then compute SCC's in the graph and unify nodes in the same SCC,
1988 including pts sets.
1990 For each non-collapsed node x:
1991 Visit all unvisited explicit incoming edges.
1992 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1993 where y->x.
1994 Lookup the equivalence class for pts(x).
1995 If we found one, equivalence_class(x) = found class.
1996 Otherwise, equivalence_class(x) = new class, and new_class is
1997 added to the lookup table.
1999 All direct nodes with the same equivalence class can be replaced
2000 with a single representative node.
2001 All unlabeled nodes (label == 0) are not pointers and all edges
2002 involving them can be eliminated.
2003 We perform these optimizations during rewrite_constraints
2005 In addition to pointer equivalence class finding, we also perform
2006 location equivalence class finding. This is the set of variables
2007 that always appear together in points-to sets. We use this to
2008 compress the size of the points-to sets. */
2010 /* Current maximum pointer equivalence class id. */
2011 static int pointer_equiv_class;
2013 /* Current maximum location equivalence class id. */
2014 static int location_equiv_class;
2016 /* Recursive routine to find strongly connected components in GRAPH,
2017 and label it's nodes with DFS numbers. */
2019 static void
2020 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2022 unsigned int i;
2023 bitmap_iterator bi;
2024 unsigned int my_dfs;
2026 gcc_checking_assert (si->node_mapping[n] == n);
2027 bitmap_set_bit (si->visited, n);
2028 si->dfs[n] = si->current_index ++;
2029 my_dfs = si->dfs[n];
2031 /* Visit all the successors. */
2032 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2034 unsigned int w = si->node_mapping[i];
2036 if (bitmap_bit_p (si->deleted, w))
2037 continue;
2039 if (!bitmap_bit_p (si->visited, w))
2040 condense_visit (graph, si, w);
2042 unsigned int t = si->node_mapping[w];
2043 gcc_checking_assert (si->node_mapping[n] == n);
2044 if (si->dfs[t] < si->dfs[n])
2045 si->dfs[n] = si->dfs[t];
2048 /* Visit all the implicit predecessors. */
2049 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2051 unsigned int w = si->node_mapping[i];
2053 if (bitmap_bit_p (si->deleted, w))
2054 continue;
2056 if (!bitmap_bit_p (si->visited, w))
2057 condense_visit (graph, si, w);
2059 unsigned int t = si->node_mapping[w];
2060 gcc_assert (si->node_mapping[n] == n);
2061 if (si->dfs[t] < si->dfs[n])
2062 si->dfs[n] = si->dfs[t];
2065 /* See if any components have been identified. */
2066 if (si->dfs[n] == my_dfs)
2068 while (si->scc_stack.length () != 0
2069 && si->dfs[si->scc_stack.last ()] >= my_dfs)
2071 unsigned int w = si->scc_stack.pop ();
2072 si->node_mapping[w] = n;
2074 if (!bitmap_bit_p (graph->direct_nodes, w))
2075 bitmap_clear_bit (graph->direct_nodes, n);
2077 /* Unify our nodes. */
2078 if (graph->preds[w])
2080 if (!graph->preds[n])
2081 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2082 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2084 if (graph->implicit_preds[w])
2086 if (!graph->implicit_preds[n])
2087 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2088 bitmap_ior_into (graph->implicit_preds[n],
2089 graph->implicit_preds[w]);
2091 if (graph->points_to[w])
2093 if (!graph->points_to[n])
2094 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2095 bitmap_ior_into (graph->points_to[n],
2096 graph->points_to[w]);
2099 bitmap_set_bit (si->deleted, n);
2101 else
2102 si->scc_stack.safe_push (n);
2105 /* Label pointer equivalences.
2107 This performs a value numbering of the constraint graph to
2108 discover which variables will always have the same points-to sets
2109 under the current set of constraints.
2111 The way it value numbers is to store the set of points-to bits
2112 generated by the constraints and graph edges. This is just used as a
2113 hash and equality comparison. The *actual set of points-to bits* is
2114 completely irrelevant, in that we don't care about being able to
2115 extract them later.
2117 The equality values (currently bitmaps) just have to satisfy a few
2118 constraints, the main ones being:
2119 1. The combining operation must be order independent.
2120 2. The end result of a given set of operations must be unique iff the
2121 combination of input values is unique
2122 3. Hashable. */
2124 static void
2125 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2127 unsigned int i, first_pred;
2128 bitmap_iterator bi;
2130 bitmap_set_bit (si->visited, n);
2132 /* Label and union our incoming edges's points to sets. */
2133 first_pred = -1U;
2134 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2136 unsigned int w = si->node_mapping[i];
2137 if (!bitmap_bit_p (si->visited, w))
2138 label_visit (graph, si, w);
2140 /* Skip unused edges */
2141 if (w == n || graph->pointer_label[w] == 0)
2142 continue;
2144 if (graph->points_to[w])
2146 if (!graph->points_to[n])
2148 if (first_pred == -1U)
2149 first_pred = w;
2150 else
2152 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2153 bitmap_ior (graph->points_to[n],
2154 graph->points_to[first_pred],
2155 graph->points_to[w]);
2158 else
2159 bitmap_ior_into (graph->points_to[n], graph->points_to[w]);
2163 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2164 if (!bitmap_bit_p (graph->direct_nodes, n))
2166 if (!graph->points_to[n])
2168 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2169 if (first_pred != -1U)
2170 bitmap_copy (graph->points_to[n], graph->points_to[first_pred]);
2172 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2173 graph->pointer_label[n] = pointer_equiv_class++;
2174 equiv_class_label_t ecl;
2175 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2176 graph->points_to[n]);
2177 ecl->equivalence_class = graph->pointer_label[n];
2178 return;
2181 /* If there was only a single non-empty predecessor the pointer equiv
2182 class is the same. */
2183 if (!graph->points_to[n])
2185 if (first_pred != -1U)
2187 graph->pointer_label[n] = graph->pointer_label[first_pred];
2188 graph->points_to[n] = graph->points_to[first_pred];
2190 return;
2193 if (!bitmap_empty_p (graph->points_to[n]))
2195 equiv_class_label_t ecl;
2196 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2197 graph->points_to[n]);
2198 if (ecl->equivalence_class == 0)
2199 ecl->equivalence_class = pointer_equiv_class++;
2200 else
2202 BITMAP_FREE (graph->points_to[n]);
2203 graph->points_to[n] = ecl->labels;
2205 graph->pointer_label[n] = ecl->equivalence_class;
2209 /* Print the pred graph in dot format. */
2211 static void
2212 dump_pred_graph (struct scc_info *si, FILE *file)
2214 unsigned int i;
2216 /* Only print the graph if it has already been initialized: */
2217 if (!graph)
2218 return;
2220 /* Prints the header of the dot file: */
2221 fprintf (file, "strict digraph {\n");
2222 fprintf (file, " node [\n shape = box\n ]\n");
2223 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
2224 fprintf (file, "\n // List of nodes and complex constraints in "
2225 "the constraint graph:\n");
2227 /* The next lines print the nodes in the graph together with the
2228 complex constraints attached to them. */
2229 for (i = 1; i < graph->size; i++)
2231 if (i == FIRST_REF_NODE)
2232 continue;
2233 if (si->node_mapping[i] != i)
2234 continue;
2235 if (i < FIRST_REF_NODE)
2236 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2237 else
2238 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2239 if (graph->points_to[i]
2240 && !bitmap_empty_p (graph->points_to[i]))
2242 fprintf (file, "[label=\"%s = {", get_varinfo (i)->name);
2243 unsigned j;
2244 bitmap_iterator bi;
2245 EXECUTE_IF_SET_IN_BITMAP (graph->points_to[i], 0, j, bi)
2246 fprintf (file, " %d", j);
2247 fprintf (file, " }\"]");
2249 fprintf (file, ";\n");
2252 /* Go over the edges. */
2253 fprintf (file, "\n // Edges in the constraint graph:\n");
2254 for (i = 1; i < graph->size; i++)
2256 unsigned j;
2257 bitmap_iterator bi;
2258 if (si->node_mapping[i] != i)
2259 continue;
2260 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[i], 0, j, bi)
2262 unsigned from = si->node_mapping[j];
2263 if (from < FIRST_REF_NODE)
2264 fprintf (file, "\"%s\"", get_varinfo (from)->name);
2265 else
2266 fprintf (file, "\"*%s\"", get_varinfo (from - FIRST_REF_NODE)->name);
2267 fprintf (file, " -> ");
2268 if (i < FIRST_REF_NODE)
2269 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2270 else
2271 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2272 fprintf (file, ";\n");
2276 /* Prints the tail of the dot file. */
2277 fprintf (file, "}\n");
2280 /* Perform offline variable substitution, discovering equivalence
2281 classes, and eliminating non-pointer variables. */
2283 static struct scc_info *
2284 perform_var_substitution (constraint_graph_t graph)
2286 unsigned int i;
2287 unsigned int size = graph->size;
2288 struct scc_info *si = init_scc_info (size);
2290 bitmap_obstack_initialize (&iteration_obstack);
2291 pointer_equiv_class_table = new hash_table<equiv_class_hasher> (511);
2292 location_equiv_class_table
2293 = new hash_table<equiv_class_hasher> (511);
2294 pointer_equiv_class = 1;
2295 location_equiv_class = 1;
2297 /* Condense the nodes, which means to find SCC's, count incoming
2298 predecessors, and unite nodes in SCC's. */
2299 for (i = 1; i < FIRST_REF_NODE; i++)
2300 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2301 condense_visit (graph, si, si->node_mapping[i]);
2303 if (dump_file && (dump_flags & TDF_GRAPH))
2305 fprintf (dump_file, "\n\n// The constraint graph before var-substitution "
2306 "in dot format:\n");
2307 dump_pred_graph (si, dump_file);
2308 fprintf (dump_file, "\n\n");
2311 bitmap_clear (si->visited);
2312 /* Actually the label the nodes for pointer equivalences */
2313 for (i = 1; i < FIRST_REF_NODE; i++)
2314 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2315 label_visit (graph, si, si->node_mapping[i]);
2317 /* Calculate location equivalence labels. */
2318 for (i = 1; i < FIRST_REF_NODE; i++)
2320 bitmap pointed_by;
2321 bitmap_iterator bi;
2322 unsigned int j;
2324 if (!graph->pointed_by[i])
2325 continue;
2326 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2328 /* Translate the pointed-by mapping for pointer equivalence
2329 labels. */
2330 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2332 bitmap_set_bit (pointed_by,
2333 graph->pointer_label[si->node_mapping[j]]);
2335 /* The original pointed_by is now dead. */
2336 BITMAP_FREE (graph->pointed_by[i]);
2338 /* Look up the location equivalence label if one exists, or make
2339 one otherwise. */
2340 equiv_class_label_t ecl;
2341 ecl = equiv_class_lookup_or_add (location_equiv_class_table, pointed_by);
2342 if (ecl->equivalence_class == 0)
2343 ecl->equivalence_class = location_equiv_class++;
2344 else
2346 if (dump_file && (dump_flags & TDF_DETAILS))
2347 fprintf (dump_file, "Found location equivalence for node %s\n",
2348 get_varinfo (i)->name);
2349 BITMAP_FREE (pointed_by);
2351 graph->loc_label[i] = ecl->equivalence_class;
2355 if (dump_file && (dump_flags & TDF_DETAILS))
2356 for (i = 1; i < FIRST_REF_NODE; i++)
2358 unsigned j = si->node_mapping[i];
2359 if (j != i)
2361 fprintf (dump_file, "%s node id %d ",
2362 bitmap_bit_p (graph->direct_nodes, i)
2363 ? "Direct" : "Indirect", i);
2364 if (i < FIRST_REF_NODE)
2365 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2366 else
2367 fprintf (dump_file, "\"*%s\"",
2368 get_varinfo (i - FIRST_REF_NODE)->name);
2369 fprintf (dump_file, " mapped to SCC leader node id %d ", j);
2370 if (j < FIRST_REF_NODE)
2371 fprintf (dump_file, "\"%s\"\n", get_varinfo (j)->name);
2372 else
2373 fprintf (dump_file, "\"*%s\"\n",
2374 get_varinfo (j - FIRST_REF_NODE)->name);
2376 else
2378 fprintf (dump_file,
2379 "Equivalence classes for %s node id %d ",
2380 bitmap_bit_p (graph->direct_nodes, i)
2381 ? "direct" : "indirect", i);
2382 if (i < FIRST_REF_NODE)
2383 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2384 else
2385 fprintf (dump_file, "\"*%s\"",
2386 get_varinfo (i - FIRST_REF_NODE)->name);
2387 fprintf (dump_file,
2388 ": pointer %d, location %d\n",
2389 graph->pointer_label[i], graph->loc_label[i]);
2393 /* Quickly eliminate our non-pointer variables. */
2395 for (i = 1; i < FIRST_REF_NODE; i++)
2397 unsigned int node = si->node_mapping[i];
2399 if (graph->pointer_label[node] == 0)
2401 if (dump_file && (dump_flags & TDF_DETAILS))
2402 fprintf (dump_file,
2403 "%s is a non-pointer variable, eliminating edges.\n",
2404 get_varinfo (node)->name);
2405 stats.nonpointer_vars++;
2406 clear_edges_for_node (graph, node);
2410 return si;
2413 /* Free information that was only necessary for variable
2414 substitution. */
2416 static void
2417 free_var_substitution_info (struct scc_info *si)
2419 free_scc_info (si);
2420 free (graph->pointer_label);
2421 free (graph->loc_label);
2422 free (graph->pointed_by);
2423 free (graph->points_to);
2424 free (graph->eq_rep);
2425 sbitmap_free (graph->direct_nodes);
2426 delete pointer_equiv_class_table;
2427 pointer_equiv_class_table = NULL;
2428 delete location_equiv_class_table;
2429 location_equiv_class_table = NULL;
2430 bitmap_obstack_release (&iteration_obstack);
2433 /* Return an existing node that is equivalent to NODE, which has
2434 equivalence class LABEL, if one exists. Return NODE otherwise. */
2436 static unsigned int
2437 find_equivalent_node (constraint_graph_t graph,
2438 unsigned int node, unsigned int label)
2440 /* If the address version of this variable is unused, we can
2441 substitute it for anything else with the same label.
2442 Otherwise, we know the pointers are equivalent, but not the
2443 locations, and we can unite them later. */
2445 if (!bitmap_bit_p (graph->address_taken, node))
2447 gcc_checking_assert (label < graph->size);
2449 if (graph->eq_rep[label] != -1)
2451 /* Unify the two variables since we know they are equivalent. */
2452 if (unite (graph->eq_rep[label], node))
2453 unify_nodes (graph, graph->eq_rep[label], node, false);
2454 return graph->eq_rep[label];
2456 else
2458 graph->eq_rep[label] = node;
2459 graph->pe_rep[label] = node;
2462 else
2464 gcc_checking_assert (label < graph->size);
2465 graph->pe[node] = label;
2466 if (graph->pe_rep[label] == -1)
2467 graph->pe_rep[label] = node;
2470 return node;
2473 /* Unite pointer equivalent but not location equivalent nodes in
2474 GRAPH. This may only be performed once variable substitution is
2475 finished. */
2477 static void
2478 unite_pointer_equivalences (constraint_graph_t graph)
2480 unsigned int i;
2482 /* Go through the pointer equivalences and unite them to their
2483 representative, if they aren't already. */
2484 for (i = 1; i < FIRST_REF_NODE; i++)
2486 unsigned int label = graph->pe[i];
2487 if (label)
2489 int label_rep = graph->pe_rep[label];
2491 if (label_rep == -1)
2492 continue;
2494 label_rep = find (label_rep);
2495 if (label_rep >= 0 && unite (label_rep, find (i)))
2496 unify_nodes (graph, label_rep, i, false);
2501 /* Move complex constraints to the GRAPH nodes they belong to. */
2503 static void
2504 move_complex_constraints (constraint_graph_t graph)
2506 int i;
2507 constraint_t c;
2509 FOR_EACH_VEC_ELT (constraints, i, c)
2511 if (c)
2513 struct constraint_expr lhs = c->lhs;
2514 struct constraint_expr rhs = c->rhs;
2516 if (lhs.type == DEREF)
2518 insert_into_complex (graph, lhs.var, c);
2520 else if (rhs.type == DEREF)
2522 if (!(get_varinfo (lhs.var)->is_special_var))
2523 insert_into_complex (graph, rhs.var, c);
2525 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2526 && (lhs.offset != 0 || rhs.offset != 0))
2528 insert_into_complex (graph, rhs.var, c);
2535 /* Optimize and rewrite complex constraints while performing
2536 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2537 result of perform_variable_substitution. */
2539 static void
2540 rewrite_constraints (constraint_graph_t graph,
2541 struct scc_info *si)
2543 int i;
2544 constraint_t c;
2546 #ifdef ENABLE_CHECKING
2547 for (unsigned int j = 0; j < graph->size; j++)
2548 gcc_assert (find (j) == j);
2549 #endif
2551 FOR_EACH_VEC_ELT (constraints, i, c)
2553 struct constraint_expr lhs = c->lhs;
2554 struct constraint_expr rhs = c->rhs;
2555 unsigned int lhsvar = find (lhs.var);
2556 unsigned int rhsvar = find (rhs.var);
2557 unsigned int lhsnode, rhsnode;
2558 unsigned int lhslabel, rhslabel;
2560 lhsnode = si->node_mapping[lhsvar];
2561 rhsnode = si->node_mapping[rhsvar];
2562 lhslabel = graph->pointer_label[lhsnode];
2563 rhslabel = graph->pointer_label[rhsnode];
2565 /* See if it is really a non-pointer variable, and if so, ignore
2566 the constraint. */
2567 if (lhslabel == 0)
2569 if (dump_file && (dump_flags & TDF_DETAILS))
2572 fprintf (dump_file, "%s is a non-pointer variable,"
2573 "ignoring constraint:",
2574 get_varinfo (lhs.var)->name);
2575 dump_constraint (dump_file, c);
2576 fprintf (dump_file, "\n");
2578 constraints[i] = NULL;
2579 continue;
2582 if (rhslabel == 0)
2584 if (dump_file && (dump_flags & TDF_DETAILS))
2587 fprintf (dump_file, "%s is a non-pointer variable,"
2588 "ignoring constraint:",
2589 get_varinfo (rhs.var)->name);
2590 dump_constraint (dump_file, c);
2591 fprintf (dump_file, "\n");
2593 constraints[i] = NULL;
2594 continue;
2597 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2598 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2599 c->lhs.var = lhsvar;
2600 c->rhs.var = rhsvar;
2604 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2605 part of an SCC, false otherwise. */
2607 static bool
2608 eliminate_indirect_cycles (unsigned int node)
2610 if (graph->indirect_cycles[node] != -1
2611 && !bitmap_empty_p (get_varinfo (node)->solution))
2613 unsigned int i;
2614 auto_vec<unsigned> queue;
2615 int queuepos;
2616 unsigned int to = find (graph->indirect_cycles[node]);
2617 bitmap_iterator bi;
2619 /* We can't touch the solution set and call unify_nodes
2620 at the same time, because unify_nodes is going to do
2621 bitmap unions into it. */
2623 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2625 if (find (i) == i && i != to)
2627 if (unite (to, i))
2628 queue.safe_push (i);
2632 for (queuepos = 0;
2633 queue.iterate (queuepos, &i);
2634 queuepos++)
2636 unify_nodes (graph, to, i, true);
2638 return true;
2640 return false;
2643 /* Solve the constraint graph GRAPH using our worklist solver.
2644 This is based on the PW* family of solvers from the "Efficient Field
2645 Sensitive Pointer Analysis for C" paper.
2646 It works by iterating over all the graph nodes, processing the complex
2647 constraints and propagating the copy constraints, until everything stops
2648 changed. This corresponds to steps 6-8 in the solving list given above. */
2650 static void
2651 solve_graph (constraint_graph_t graph)
2653 unsigned int size = graph->size;
2654 unsigned int i;
2655 bitmap pts;
2657 changed = BITMAP_ALLOC (NULL);
2659 /* Mark all initial non-collapsed nodes as changed. */
2660 for (i = 1; i < size; i++)
2662 varinfo_t ivi = get_varinfo (i);
2663 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2664 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2665 || graph->complex[i].length () > 0))
2666 bitmap_set_bit (changed, i);
2669 /* Allocate a bitmap to be used to store the changed bits. */
2670 pts = BITMAP_ALLOC (&pta_obstack);
2672 while (!bitmap_empty_p (changed))
2674 unsigned int i;
2675 struct topo_info *ti = init_topo_info ();
2676 stats.iterations++;
2678 bitmap_obstack_initialize (&iteration_obstack);
2680 compute_topo_order (graph, ti);
2682 while (ti->topo_order.length () != 0)
2685 i = ti->topo_order.pop ();
2687 /* If this variable is not a representative, skip it. */
2688 if (find (i) != i)
2689 continue;
2691 /* In certain indirect cycle cases, we may merge this
2692 variable to another. */
2693 if (eliminate_indirect_cycles (i) && find (i) != i)
2694 continue;
2696 /* If the node has changed, we need to process the
2697 complex constraints and outgoing edges again. */
2698 if (bitmap_clear_bit (changed, i))
2700 unsigned int j;
2701 constraint_t c;
2702 bitmap solution;
2703 vec<constraint_t> complex = graph->complex[i];
2704 varinfo_t vi = get_varinfo (i);
2705 bool solution_empty;
2707 /* Compute the changed set of solution bits. If anything
2708 is in the solution just propagate that. */
2709 if (bitmap_bit_p (vi->solution, anything_id))
2711 /* If anything is also in the old solution there is
2712 nothing to do.
2713 ??? But we shouldn't ended up with "changed" set ... */
2714 if (vi->oldsolution
2715 && bitmap_bit_p (vi->oldsolution, anything_id))
2716 continue;
2717 bitmap_copy (pts, get_varinfo (find (anything_id))->solution);
2719 else if (vi->oldsolution)
2720 bitmap_and_compl (pts, vi->solution, vi->oldsolution);
2721 else
2722 bitmap_copy (pts, vi->solution);
2724 if (bitmap_empty_p (pts))
2725 continue;
2727 if (vi->oldsolution)
2728 bitmap_ior_into (vi->oldsolution, pts);
2729 else
2731 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
2732 bitmap_copy (vi->oldsolution, pts);
2735 solution = vi->solution;
2736 solution_empty = bitmap_empty_p (solution);
2738 /* Process the complex constraints */
2739 bitmap expanded_pts = NULL;
2740 FOR_EACH_VEC_ELT (complex, j, c)
2742 /* XXX: This is going to unsort the constraints in
2743 some cases, which will occasionally add duplicate
2744 constraints during unification. This does not
2745 affect correctness. */
2746 c->lhs.var = find (c->lhs.var);
2747 c->rhs.var = find (c->rhs.var);
2749 /* The only complex constraint that can change our
2750 solution to non-empty, given an empty solution,
2751 is a constraint where the lhs side is receiving
2752 some set from elsewhere. */
2753 if (!solution_empty || c->lhs.type != DEREF)
2754 do_complex_constraint (graph, c, pts, &expanded_pts);
2756 BITMAP_FREE (expanded_pts);
2758 solution_empty = bitmap_empty_p (solution);
2760 if (!solution_empty)
2762 bitmap_iterator bi;
2763 unsigned eff_escaped_id = find (escaped_id);
2765 /* Propagate solution to all successors. */
2766 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2767 0, j, bi)
2769 bitmap tmp;
2770 bool flag;
2772 unsigned int to = find (j);
2773 tmp = get_varinfo (to)->solution;
2774 flag = false;
2776 /* Don't try to propagate to ourselves. */
2777 if (to == i)
2778 continue;
2780 /* If we propagate from ESCAPED use ESCAPED as
2781 placeholder. */
2782 if (i == eff_escaped_id)
2783 flag = bitmap_set_bit (tmp, escaped_id);
2784 else
2785 flag = bitmap_ior_into (tmp, pts);
2787 if (flag)
2788 bitmap_set_bit (changed, to);
2793 free_topo_info (ti);
2794 bitmap_obstack_release (&iteration_obstack);
2797 BITMAP_FREE (pts);
2798 BITMAP_FREE (changed);
2799 bitmap_obstack_release (&oldpta_obstack);
2802 /* Map from trees to variable infos. */
2803 static hash_map<tree, varinfo_t> *vi_for_tree;
2806 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2808 static void
2809 insert_vi_for_tree (tree t, varinfo_t vi)
2811 gcc_assert (vi);
2812 gcc_assert (!vi_for_tree->put (t, vi));
2815 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2816 exist in the map, return NULL, otherwise, return the varinfo we found. */
2818 static varinfo_t
2819 lookup_vi_for_tree (tree t)
2821 varinfo_t *slot = vi_for_tree->get (t);
2822 if (slot == NULL)
2823 return NULL;
2825 return *slot;
2828 /* Return a printable name for DECL */
2830 static const char *
2831 alias_get_name (tree decl)
2833 const char *res = NULL;
2834 char *temp;
2835 int num_printed = 0;
2837 if (!dump_file)
2838 return "NULL";
2840 if (TREE_CODE (decl) == SSA_NAME)
2842 res = get_name (decl);
2843 if (res)
2844 num_printed = asprintf (&temp, "%s_%u", res, SSA_NAME_VERSION (decl));
2845 else
2846 num_printed = asprintf (&temp, "_%u", SSA_NAME_VERSION (decl));
2847 if (num_printed > 0)
2849 res = ggc_strdup (temp);
2850 free (temp);
2853 else if (DECL_P (decl))
2855 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2856 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2857 else
2859 res = get_name (decl);
2860 if (!res)
2862 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2863 if (num_printed > 0)
2865 res = ggc_strdup (temp);
2866 free (temp);
2871 if (res != NULL)
2872 return res;
2874 return "NULL";
2877 /* Find the variable id for tree T in the map.
2878 If T doesn't exist in the map, create an entry for it and return it. */
2880 static varinfo_t
2881 get_vi_for_tree (tree t)
2883 varinfo_t *slot = vi_for_tree->get (t);
2884 if (slot == NULL)
2885 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2887 return *slot;
2890 /* Get a scalar constraint expression for a new temporary variable. */
2892 static struct constraint_expr
2893 new_scalar_tmp_constraint_exp (const char *name)
2895 struct constraint_expr tmp;
2896 varinfo_t vi;
2898 vi = new_var_info (NULL_TREE, name);
2899 vi->offset = 0;
2900 vi->size = -1;
2901 vi->fullsize = -1;
2902 vi->is_full_var = 1;
2904 tmp.var = vi->id;
2905 tmp.type = SCALAR;
2906 tmp.offset = 0;
2908 return tmp;
2911 /* Get a constraint expression vector from an SSA_VAR_P node.
2912 If address_p is true, the result will be taken its address of. */
2914 static void
2915 get_constraint_for_ssa_var (tree t, vec<ce_s> *results, bool address_p)
2917 struct constraint_expr cexpr;
2918 varinfo_t vi;
2920 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2921 gcc_assert (TREE_CODE (t) == SSA_NAME || DECL_P (t));
2923 /* For parameters, get at the points-to set for the actual parm
2924 decl. */
2925 if (TREE_CODE (t) == SSA_NAME
2926 && SSA_NAME_IS_DEFAULT_DEF (t)
2927 && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2928 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL))
2930 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2931 return;
2934 /* For global variables resort to the alias target. */
2935 if (TREE_CODE (t) == VAR_DECL
2936 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
2938 varpool_node *node = varpool_node::get (t);
2939 if (node && node->alias && node->analyzed)
2941 node = node->ultimate_alias_target ();
2942 t = node->decl;
2946 vi = get_vi_for_tree (t);
2947 cexpr.var = vi->id;
2948 cexpr.type = SCALAR;
2949 cexpr.offset = 0;
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 offset_int soffset = offset_int::from (offset, SIGNED);
3069 if (!wi::fits_shwi_p (soffset))
3070 rhsoffset = UNKNOWN_OFFSET;
3071 else
3073 /* Make sure the bit-offset also fits. */
3074 HOST_WIDE_INT rhsunitoffset = soffset.to_shwi ();
3075 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
3076 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
3077 rhsoffset = UNKNOWN_OFFSET;
3081 get_constraint_for_rhs (ptr, results);
3082 if (rhsoffset == 0)
3083 return;
3085 /* As we are eventually appending to the solution do not use
3086 vec::iterate here. */
3087 n = results->length ();
3088 for (j = 0; j < n; j++)
3090 varinfo_t curr;
3091 c = (*results)[j];
3092 curr = get_varinfo (c.var);
3094 if (c.type == ADDRESSOF
3095 /* If this varinfo represents a full variable just use it. */
3096 && curr->is_full_var)
3098 else if (c.type == ADDRESSOF
3099 /* If we do not know the offset add all subfields. */
3100 && rhsoffset == UNKNOWN_OFFSET)
3102 varinfo_t temp = get_varinfo (curr->head);
3105 struct constraint_expr c2;
3106 c2.var = temp->id;
3107 c2.type = ADDRESSOF;
3108 c2.offset = 0;
3109 if (c2.var != c.var)
3110 results->safe_push (c2);
3111 temp = vi_next (temp);
3113 while (temp);
3115 else if (c.type == ADDRESSOF)
3117 varinfo_t temp;
3118 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
3120 /* If curr->offset + rhsoffset is less than zero adjust it. */
3121 if (rhsoffset < 0
3122 && curr->offset < offset)
3123 offset = 0;
3125 /* We have to include all fields that overlap the current
3126 field shifted by rhsoffset. And we include at least
3127 the last or the first field of the variable to represent
3128 reachability of off-bound addresses, in particular &object + 1,
3129 conservatively correct. */
3130 temp = first_or_preceding_vi_for_offset (curr, offset);
3131 c.var = temp->id;
3132 c.offset = 0;
3133 temp = vi_next (temp);
3134 while (temp
3135 && temp->offset < offset + curr->size)
3137 struct constraint_expr c2;
3138 c2.var = temp->id;
3139 c2.type = ADDRESSOF;
3140 c2.offset = 0;
3141 results->safe_push (c2);
3142 temp = vi_next (temp);
3145 else if (c.type == SCALAR)
3147 gcc_assert (c.offset == 0);
3148 c.offset = rhsoffset;
3150 else
3151 /* We shouldn't get any DEREFs here. */
3152 gcc_unreachable ();
3154 (*results)[j] = c;
3159 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3160 If address_p is true the result will be taken its address of.
3161 If lhs_p is true then the constraint expression is assumed to be used
3162 as the lhs. */
3164 static void
3165 get_constraint_for_component_ref (tree t, vec<ce_s> *results,
3166 bool address_p, bool lhs_p)
3168 tree orig_t = t;
3169 HOST_WIDE_INT bitsize = -1;
3170 HOST_WIDE_INT bitmaxsize = -1;
3171 HOST_WIDE_INT bitpos;
3172 tree forzero;
3174 /* Some people like to do cute things like take the address of
3175 &0->a.b */
3176 forzero = t;
3177 while (handled_component_p (forzero)
3178 || INDIRECT_REF_P (forzero)
3179 || TREE_CODE (forzero) == MEM_REF)
3180 forzero = TREE_OPERAND (forzero, 0);
3182 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
3184 struct constraint_expr temp;
3186 temp.offset = 0;
3187 temp.var = integer_id;
3188 temp.type = SCALAR;
3189 results->safe_push (temp);
3190 return;
3193 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
3195 /* Pretend to take the address of the base, we'll take care of
3196 adding the required subset of sub-fields below. */
3197 get_constraint_for_1 (t, results, true, lhs_p);
3198 gcc_assert (results->length () == 1);
3199 struct constraint_expr &result = results->last ();
3201 if (result.type == SCALAR
3202 && get_varinfo (result.var)->is_full_var)
3203 /* For single-field vars do not bother about the offset. */
3204 result.offset = 0;
3205 else if (result.type == SCALAR)
3207 /* In languages like C, you can access one past the end of an
3208 array. You aren't allowed to dereference it, so we can
3209 ignore this constraint. When we handle pointer subtraction,
3210 we may have to do something cute here. */
3212 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result.var)->fullsize
3213 && bitmaxsize != 0)
3215 /* It's also not true that the constraint will actually start at the
3216 right offset, it may start in some padding. We only care about
3217 setting the constraint to the first actual field it touches, so
3218 walk to find it. */
3219 struct constraint_expr cexpr = result;
3220 varinfo_t curr;
3221 results->pop ();
3222 cexpr.offset = 0;
3223 for (curr = get_varinfo (cexpr.var); curr; curr = vi_next (curr))
3225 if (ranges_overlap_p (curr->offset, curr->size,
3226 bitpos, bitmaxsize))
3228 cexpr.var = curr->id;
3229 results->safe_push (cexpr);
3230 if (address_p)
3231 break;
3234 /* If we are going to take the address of this field then
3235 to be able to compute reachability correctly add at least
3236 the last field of the variable. */
3237 if (address_p && results->length () == 0)
3239 curr = get_varinfo (cexpr.var);
3240 while (curr->next != 0)
3241 curr = vi_next (curr);
3242 cexpr.var = curr->id;
3243 results->safe_push (cexpr);
3245 else if (results->length () == 0)
3246 /* Assert that we found *some* field there. The user couldn't be
3247 accessing *only* padding. */
3248 /* Still the user could access one past the end of an array
3249 embedded in a struct resulting in accessing *only* padding. */
3250 /* Or accessing only padding via type-punning to a type
3251 that has a filed just in padding space. */
3253 cexpr.type = SCALAR;
3254 cexpr.var = anything_id;
3255 cexpr.offset = 0;
3256 results->safe_push (cexpr);
3259 else if (bitmaxsize == 0)
3261 if (dump_file && (dump_flags & TDF_DETAILS))
3262 fprintf (dump_file, "Access to zero-sized part of variable,"
3263 "ignoring\n");
3265 else
3266 if (dump_file && (dump_flags & TDF_DETAILS))
3267 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3269 else if (result.type == DEREF)
3271 /* If we do not know exactly where the access goes say so. Note
3272 that only for non-structure accesses we know that we access
3273 at most one subfiled of any variable. */
3274 if (bitpos == -1
3275 || bitsize != bitmaxsize
3276 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
3277 || result.offset == UNKNOWN_OFFSET)
3278 result.offset = UNKNOWN_OFFSET;
3279 else
3280 result.offset += bitpos;
3282 else if (result.type == ADDRESSOF)
3284 /* We can end up here for component references on a
3285 VIEW_CONVERT_EXPR <>(&foobar). */
3286 result.type = SCALAR;
3287 result.var = anything_id;
3288 result.offset = 0;
3290 else
3291 gcc_unreachable ();
3295 /* Dereference the constraint expression CONS, and return the result.
3296 DEREF (ADDRESSOF) = SCALAR
3297 DEREF (SCALAR) = DEREF
3298 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3299 This is needed so that we can handle dereferencing DEREF constraints. */
3301 static void
3302 do_deref (vec<ce_s> *constraints)
3304 struct constraint_expr *c;
3305 unsigned int i = 0;
3307 FOR_EACH_VEC_ELT (*constraints, i, c)
3309 if (c->type == SCALAR)
3310 c->type = DEREF;
3311 else if (c->type == ADDRESSOF)
3312 c->type = SCALAR;
3313 else if (c->type == DEREF)
3315 struct constraint_expr tmplhs;
3316 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp");
3317 process_constraint (new_constraint (tmplhs, *c));
3318 c->var = tmplhs.var;
3320 else
3321 gcc_unreachable ();
3325 /* Given a tree T, return the constraint expression for taking the
3326 address of it. */
3328 static void
3329 get_constraint_for_address_of (tree t, vec<ce_s> *results)
3331 struct constraint_expr *c;
3332 unsigned int i;
3334 get_constraint_for_1 (t, results, true, true);
3336 FOR_EACH_VEC_ELT (*results, i, c)
3338 if (c->type == DEREF)
3339 c->type = SCALAR;
3340 else
3341 c->type = ADDRESSOF;
3345 /* Given a tree T, return the constraint expression for it. */
3347 static void
3348 get_constraint_for_1 (tree t, vec<ce_s> *results, bool address_p,
3349 bool lhs_p)
3351 struct constraint_expr temp;
3353 /* x = integer is all glommed to a single variable, which doesn't
3354 point to anything by itself. That is, of course, unless it is an
3355 integer constant being treated as a pointer, in which case, we
3356 will return that this is really the addressof anything. This
3357 happens below, since it will fall into the default case. The only
3358 case we know something about an integer treated like a pointer is
3359 when it is the NULL pointer, and then we just say it points to
3360 NULL.
3362 Do not do that if -fno-delete-null-pointer-checks though, because
3363 in that case *NULL does not fail, so it _should_ alias *anything.
3364 It is not worth adding a new option or renaming the existing one,
3365 since this case is relatively obscure. */
3366 if ((TREE_CODE (t) == INTEGER_CST
3367 && integer_zerop (t))
3368 /* The only valid CONSTRUCTORs in gimple with pointer typed
3369 elements are zero-initializer. But in IPA mode we also
3370 process global initializers, so verify at least. */
3371 || (TREE_CODE (t) == CONSTRUCTOR
3372 && CONSTRUCTOR_NELTS (t) == 0))
3374 if (flag_delete_null_pointer_checks)
3375 temp.var = nothing_id;
3376 else
3377 temp.var = nonlocal_id;
3378 temp.type = ADDRESSOF;
3379 temp.offset = 0;
3380 results->safe_push (temp);
3381 return;
3384 /* String constants are read-only, ideally we'd have a CONST_DECL
3385 for those. */
3386 if (TREE_CODE (t) == STRING_CST)
3388 temp.var = string_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 if (flags & ERF_RETURNS_ARG
3979 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
3981 tree arg;
3982 rhsc.create (0);
3983 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
3984 get_constraint_for (arg, &rhsc);
3985 process_all_all_constraints (lhsc, rhsc);
3986 rhsc.release ();
3988 else if (flags & ERF_NOALIAS)
3990 varinfo_t vi;
3991 struct constraint_expr tmpc;
3992 rhsc.create (0);
3993 vi = make_heapvar ("HEAP");
3994 /* We are marking allocated storage local, we deal with it becoming
3995 global by escaping and setting of vars_contains_escaped_heap. */
3996 DECL_EXTERNAL (vi->decl) = 0;
3997 vi->is_global_var = 0;
3998 /* If this is not a real malloc call assume the memory was
3999 initialized and thus may point to global memory. All
4000 builtin functions with the malloc attribute behave in a sane way. */
4001 if (!fndecl
4002 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
4003 make_constraint_from (vi, nonlocal_id);
4004 tmpc.var = vi->id;
4005 tmpc.offset = 0;
4006 tmpc.type = ADDRESSOF;
4007 rhsc.safe_push (tmpc);
4008 process_all_all_constraints (lhsc, rhsc);
4009 rhsc.release ();
4011 else
4012 process_all_all_constraints (lhsc, rhsc);
4015 /* For non-IPA mode, generate constraints necessary for a call of a
4016 const function that returns a pointer in the statement STMT. */
4018 static void
4019 handle_const_call (gimple stmt, vec<ce_s> *results)
4021 struct constraint_expr rhsc;
4022 unsigned int k;
4024 /* Treat nested const functions the same as pure functions as far
4025 as the static chain is concerned. */
4026 if (gimple_call_chain (stmt))
4028 varinfo_t uses = get_call_use_vi (stmt);
4029 make_transitive_closure_constraints (uses);
4030 make_constraint_to (uses->id, gimple_call_chain (stmt));
4031 rhsc.var = uses->id;
4032 rhsc.offset = 0;
4033 rhsc.type = SCALAR;
4034 results->safe_push (rhsc);
4037 /* May return arguments. */
4038 for (k = 0; k < gimple_call_num_args (stmt); ++k)
4040 tree arg = gimple_call_arg (stmt, k);
4041 auto_vec<ce_s> argc;
4042 unsigned i;
4043 struct constraint_expr *argp;
4044 get_constraint_for_rhs (arg, &argc);
4045 FOR_EACH_VEC_ELT (argc, i, argp)
4046 results->safe_push (*argp);
4049 /* May return addresses of globals. */
4050 rhsc.var = nonlocal_id;
4051 rhsc.offset = 0;
4052 rhsc.type = ADDRESSOF;
4053 results->safe_push (rhsc);
4056 /* For non-IPA mode, generate constraints necessary for a call to a
4057 pure function in statement STMT. */
4059 static void
4060 handle_pure_call (gimple stmt, vec<ce_s> *results)
4062 struct constraint_expr rhsc;
4063 unsigned i;
4064 varinfo_t uses = NULL;
4066 /* Memory reached from pointer arguments is call-used. */
4067 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4069 tree arg = gimple_call_arg (stmt, i);
4070 if (!uses)
4072 uses = get_call_use_vi (stmt);
4073 make_transitive_closure_constraints (uses);
4075 make_constraint_to (uses->id, arg);
4078 /* The static chain is used as well. */
4079 if (gimple_call_chain (stmt))
4081 if (!uses)
4083 uses = get_call_use_vi (stmt);
4084 make_transitive_closure_constraints (uses);
4086 make_constraint_to (uses->id, gimple_call_chain (stmt));
4089 /* Pure functions may return call-used and nonlocal memory. */
4090 if (uses)
4092 rhsc.var = uses->id;
4093 rhsc.offset = 0;
4094 rhsc.type = SCALAR;
4095 results->safe_push (rhsc);
4097 rhsc.var = nonlocal_id;
4098 rhsc.offset = 0;
4099 rhsc.type = SCALAR;
4100 results->safe_push (rhsc);
4104 /* Return the varinfo for the callee of CALL. */
4106 static varinfo_t
4107 get_fi_for_callee (gimple call)
4109 tree decl, fn = gimple_call_fn (call);
4111 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
4112 fn = OBJ_TYPE_REF_EXPR (fn);
4114 /* If we can directly resolve the function being called, do so.
4115 Otherwise, it must be some sort of indirect expression that
4116 we should still be able to handle. */
4117 decl = gimple_call_addr_fndecl (fn);
4118 if (decl)
4119 return get_vi_for_tree (decl);
4121 /* If the function is anything other than a SSA name pointer we have no
4122 clue and should be getting ANYFN (well, ANYTHING for now). */
4123 if (!fn || TREE_CODE (fn) != SSA_NAME)
4124 return get_varinfo (anything_id);
4126 if (SSA_NAME_IS_DEFAULT_DEF (fn)
4127 && (TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
4128 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL))
4129 fn = SSA_NAME_VAR (fn);
4131 return get_vi_for_tree (fn);
4134 /* Create constraints for the builtin call T. Return true if the call
4135 was handled, otherwise false. */
4137 static bool
4138 find_func_aliases_for_builtin_call (struct function *fn, gimple t)
4140 tree fndecl = gimple_call_fndecl (t);
4141 auto_vec<ce_s, 2> lhsc;
4142 auto_vec<ce_s, 4> rhsc;
4143 varinfo_t fi;
4145 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4146 /* ??? All builtins that are handled here need to be handled
4147 in the alias-oracle query functions explicitly! */
4148 switch (DECL_FUNCTION_CODE (fndecl))
4150 /* All the following functions return a pointer to the same object
4151 as their first argument points to. The functions do not add
4152 to the ESCAPED solution. The functions make the first argument
4153 pointed to memory point to what the second argument pointed to
4154 memory points to. */
4155 case BUILT_IN_STRCPY:
4156 case BUILT_IN_STRNCPY:
4157 case BUILT_IN_BCOPY:
4158 case BUILT_IN_MEMCPY:
4159 case BUILT_IN_MEMMOVE:
4160 case BUILT_IN_MEMPCPY:
4161 case BUILT_IN_STPCPY:
4162 case BUILT_IN_STPNCPY:
4163 case BUILT_IN_STRCAT:
4164 case BUILT_IN_STRNCAT:
4165 case BUILT_IN_STRCPY_CHK:
4166 case BUILT_IN_STRNCPY_CHK:
4167 case BUILT_IN_MEMCPY_CHK:
4168 case BUILT_IN_MEMMOVE_CHK:
4169 case BUILT_IN_MEMPCPY_CHK:
4170 case BUILT_IN_STPCPY_CHK:
4171 case BUILT_IN_STPNCPY_CHK:
4172 case BUILT_IN_STRCAT_CHK:
4173 case BUILT_IN_STRNCAT_CHK:
4174 case BUILT_IN_TM_MEMCPY:
4175 case BUILT_IN_TM_MEMMOVE:
4177 tree res = gimple_call_lhs (t);
4178 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4179 == BUILT_IN_BCOPY ? 1 : 0));
4180 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4181 == BUILT_IN_BCOPY ? 0 : 1));
4182 if (res != NULL_TREE)
4184 get_constraint_for (res, &lhsc);
4185 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4186 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
4187 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4188 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
4189 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK
4190 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK)
4191 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4192 else
4193 get_constraint_for (dest, &rhsc);
4194 process_all_all_constraints (lhsc, rhsc);
4195 lhsc.truncate (0);
4196 rhsc.truncate (0);
4198 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4199 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4200 do_deref (&lhsc);
4201 do_deref (&rhsc);
4202 process_all_all_constraints (lhsc, rhsc);
4203 return true;
4205 case BUILT_IN_MEMSET:
4206 case BUILT_IN_MEMSET_CHK:
4207 case BUILT_IN_TM_MEMSET:
4209 tree res = gimple_call_lhs (t);
4210 tree dest = gimple_call_arg (t, 0);
4211 unsigned i;
4212 ce_s *lhsp;
4213 struct constraint_expr ac;
4214 if (res != NULL_TREE)
4216 get_constraint_for (res, &lhsc);
4217 get_constraint_for (dest, &rhsc);
4218 process_all_all_constraints (lhsc, rhsc);
4219 lhsc.truncate (0);
4221 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4222 do_deref (&lhsc);
4223 if (flag_delete_null_pointer_checks
4224 && integer_zerop (gimple_call_arg (t, 1)))
4226 ac.type = ADDRESSOF;
4227 ac.var = nothing_id;
4229 else
4231 ac.type = SCALAR;
4232 ac.var = integer_id;
4234 ac.offset = 0;
4235 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4236 process_constraint (new_constraint (*lhsp, ac));
4237 return true;
4239 case BUILT_IN_POSIX_MEMALIGN:
4241 tree ptrptr = gimple_call_arg (t, 0);
4242 get_constraint_for (ptrptr, &lhsc);
4243 do_deref (&lhsc);
4244 varinfo_t vi = make_heapvar ("HEAP");
4245 /* We are marking allocated storage local, we deal with it becoming
4246 global by escaping and setting of vars_contains_escaped_heap. */
4247 DECL_EXTERNAL (vi->decl) = 0;
4248 vi->is_global_var = 0;
4249 struct constraint_expr tmpc;
4250 tmpc.var = vi->id;
4251 tmpc.offset = 0;
4252 tmpc.type = ADDRESSOF;
4253 rhsc.safe_push (tmpc);
4254 process_all_all_constraints (lhsc, rhsc);
4255 return true;
4257 case BUILT_IN_ASSUME_ALIGNED:
4259 tree res = gimple_call_lhs (t);
4260 tree dest = gimple_call_arg (t, 0);
4261 if (res != NULL_TREE)
4263 get_constraint_for (res, &lhsc);
4264 get_constraint_for (dest, &rhsc);
4265 process_all_all_constraints (lhsc, rhsc);
4267 return true;
4269 /* All the following functions do not return pointers, do not
4270 modify the points-to sets of memory reachable from their
4271 arguments and do not add to the ESCAPED solution. */
4272 case BUILT_IN_SINCOS:
4273 case BUILT_IN_SINCOSF:
4274 case BUILT_IN_SINCOSL:
4275 case BUILT_IN_FREXP:
4276 case BUILT_IN_FREXPF:
4277 case BUILT_IN_FREXPL:
4278 case BUILT_IN_GAMMA_R:
4279 case BUILT_IN_GAMMAF_R:
4280 case BUILT_IN_GAMMAL_R:
4281 case BUILT_IN_LGAMMA_R:
4282 case BUILT_IN_LGAMMAF_R:
4283 case BUILT_IN_LGAMMAL_R:
4284 case BUILT_IN_MODF:
4285 case BUILT_IN_MODFF:
4286 case BUILT_IN_MODFL:
4287 case BUILT_IN_REMQUO:
4288 case BUILT_IN_REMQUOF:
4289 case BUILT_IN_REMQUOL:
4290 case BUILT_IN_FREE:
4291 return true;
4292 case BUILT_IN_STRDUP:
4293 case BUILT_IN_STRNDUP:
4294 case BUILT_IN_REALLOC:
4295 if (gimple_call_lhs (t))
4297 handle_lhs_call (t, gimple_call_lhs (t),
4298 gimple_call_return_flags (t) | ERF_NOALIAS,
4299 vNULL, fndecl);
4300 get_constraint_for_ptr_offset (gimple_call_lhs (t),
4301 NULL_TREE, &lhsc);
4302 get_constraint_for_ptr_offset (gimple_call_arg (t, 0),
4303 NULL_TREE, &rhsc);
4304 do_deref (&lhsc);
4305 do_deref (&rhsc);
4306 process_all_all_constraints (lhsc, rhsc);
4307 lhsc.truncate (0);
4308 rhsc.truncate (0);
4309 /* For realloc the resulting pointer can be equal to the
4310 argument as well. But only doing this wouldn't be
4311 correct because with ptr == 0 realloc behaves like malloc. */
4312 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_REALLOC)
4314 get_constraint_for (gimple_call_lhs (t), &lhsc);
4315 get_constraint_for (gimple_call_arg (t, 0), &rhsc);
4316 process_all_all_constraints (lhsc, rhsc);
4318 return true;
4320 break;
4321 /* String / character search functions return a pointer into the
4322 source string or NULL. */
4323 case BUILT_IN_INDEX:
4324 case BUILT_IN_STRCHR:
4325 case BUILT_IN_STRRCHR:
4326 case BUILT_IN_MEMCHR:
4327 case BUILT_IN_STRSTR:
4328 case BUILT_IN_STRPBRK:
4329 if (gimple_call_lhs (t))
4331 tree src = gimple_call_arg (t, 0);
4332 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4333 constraint_expr nul;
4334 nul.var = nothing_id;
4335 nul.offset = 0;
4336 nul.type = ADDRESSOF;
4337 rhsc.safe_push (nul);
4338 get_constraint_for (gimple_call_lhs (t), &lhsc);
4339 process_all_all_constraints (lhsc, rhsc);
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.truncate (0);
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);
4371 return true;
4374 /* Else fallthru to generic handling which will let
4375 the frame escape. */
4376 break;
4378 case BUILT_IN_ADJUST_TRAMPOLINE:
4380 tree tramp = gimple_call_arg (t, 0);
4381 tree res = gimple_call_lhs (t);
4382 if (in_ipa_mode && res)
4384 get_constraint_for (res, &lhsc);
4385 get_constraint_for (tramp, &rhsc);
4386 do_deref (&rhsc);
4387 process_all_all_constraints (lhsc, rhsc);
4389 return true;
4391 CASE_BUILT_IN_TM_STORE (1):
4392 CASE_BUILT_IN_TM_STORE (2):
4393 CASE_BUILT_IN_TM_STORE (4):
4394 CASE_BUILT_IN_TM_STORE (8):
4395 CASE_BUILT_IN_TM_STORE (FLOAT):
4396 CASE_BUILT_IN_TM_STORE (DOUBLE):
4397 CASE_BUILT_IN_TM_STORE (LDOUBLE):
4398 CASE_BUILT_IN_TM_STORE (M64):
4399 CASE_BUILT_IN_TM_STORE (M128):
4400 CASE_BUILT_IN_TM_STORE (M256):
4402 tree addr = gimple_call_arg (t, 0);
4403 tree src = gimple_call_arg (t, 1);
4405 get_constraint_for (addr, &lhsc);
4406 do_deref (&lhsc);
4407 get_constraint_for (src, &rhsc);
4408 process_all_all_constraints (lhsc, rhsc);
4409 return true;
4411 CASE_BUILT_IN_TM_LOAD (1):
4412 CASE_BUILT_IN_TM_LOAD (2):
4413 CASE_BUILT_IN_TM_LOAD (4):
4414 CASE_BUILT_IN_TM_LOAD (8):
4415 CASE_BUILT_IN_TM_LOAD (FLOAT):
4416 CASE_BUILT_IN_TM_LOAD (DOUBLE):
4417 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
4418 CASE_BUILT_IN_TM_LOAD (M64):
4419 CASE_BUILT_IN_TM_LOAD (M128):
4420 CASE_BUILT_IN_TM_LOAD (M256):
4422 tree dest = gimple_call_lhs (t);
4423 tree addr = gimple_call_arg (t, 0);
4425 get_constraint_for (dest, &lhsc);
4426 get_constraint_for (addr, &rhsc);
4427 do_deref (&rhsc);
4428 process_all_all_constraints (lhsc, rhsc);
4429 return true;
4431 /* Variadic argument handling needs to be handled in IPA
4432 mode as well. */
4433 case BUILT_IN_VA_START:
4435 tree valist = gimple_call_arg (t, 0);
4436 struct constraint_expr rhs, *lhsp;
4437 unsigned i;
4438 get_constraint_for (valist, &lhsc);
4439 do_deref (&lhsc);
4440 /* The va_list gets access to pointers in variadic
4441 arguments. Which we know in the case of IPA analysis
4442 and otherwise are just all nonlocal variables. */
4443 if (in_ipa_mode)
4445 fi = lookup_vi_for_tree (fn->decl);
4446 rhs = get_function_part_constraint (fi, ~0);
4447 rhs.type = ADDRESSOF;
4449 else
4451 rhs.var = nonlocal_id;
4452 rhs.type = ADDRESSOF;
4453 rhs.offset = 0;
4455 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4456 process_constraint (new_constraint (*lhsp, rhs));
4457 /* va_list is clobbered. */
4458 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4459 return true;
4461 /* va_end doesn't have any effect that matters. */
4462 case BUILT_IN_VA_END:
4463 return true;
4464 /* Alternate return. Simply give up for now. */
4465 case BUILT_IN_RETURN:
4467 fi = NULL;
4468 if (!in_ipa_mode
4469 || !(fi = get_vi_for_tree (fn->decl)))
4470 make_constraint_from (get_varinfo (escaped_id), anything_id);
4471 else if (in_ipa_mode
4472 && fi != NULL)
4474 struct constraint_expr lhs, rhs;
4475 lhs = get_function_part_constraint (fi, fi_result);
4476 rhs.var = anything_id;
4477 rhs.offset = 0;
4478 rhs.type = SCALAR;
4479 process_constraint (new_constraint (lhs, rhs));
4481 return true;
4483 /* printf-style functions may have hooks to set pointers to
4484 point to somewhere into the generated string. Leave them
4485 for a later exercise... */
4486 default:
4487 /* Fallthru to general call handling. */;
4490 return false;
4493 /* Create constraints for the call T. */
4495 static void
4496 find_func_aliases_for_call (struct function *fn, gimple t)
4498 tree fndecl = gimple_call_fndecl (t);
4499 varinfo_t fi;
4501 if (fndecl != NULL_TREE
4502 && DECL_BUILT_IN (fndecl)
4503 && find_func_aliases_for_builtin_call (fn, t))
4504 return;
4506 fi = get_fi_for_callee (t);
4507 if (!in_ipa_mode
4508 || (fndecl && !fi->is_fn_info))
4510 auto_vec<ce_s, 16> rhsc;
4511 int flags = gimple_call_flags (t);
4513 /* Const functions can return their arguments and addresses
4514 of global memory but not of escaped memory. */
4515 if (flags & (ECF_CONST|ECF_NOVOPS))
4517 if (gimple_call_lhs (t))
4518 handle_const_call (t, &rhsc);
4520 /* Pure functions can return addresses in and of memory
4521 reachable from their arguments, but they are not an escape
4522 point for reachable memory of their arguments. */
4523 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4524 handle_pure_call (t, &rhsc);
4525 else
4526 handle_rhs_call (t, &rhsc);
4527 if (gimple_call_lhs (t))
4528 handle_lhs_call (t, gimple_call_lhs (t),
4529 gimple_call_return_flags (t), rhsc, fndecl);
4531 else
4533 auto_vec<ce_s, 2> rhsc;
4534 tree lhsop;
4535 unsigned j;
4537 /* Assign all the passed arguments to the appropriate incoming
4538 parameters of the function. */
4539 for (j = 0; j < gimple_call_num_args (t); j++)
4541 struct constraint_expr lhs ;
4542 struct constraint_expr *rhsp;
4543 tree arg = gimple_call_arg (t, j);
4545 get_constraint_for_rhs (arg, &rhsc);
4546 lhs = get_function_part_constraint (fi, fi_parm_base + j);
4547 while (rhsc.length () != 0)
4549 rhsp = &rhsc.last ();
4550 process_constraint (new_constraint (lhs, *rhsp));
4551 rhsc.pop ();
4555 /* If we are returning a value, assign it to the result. */
4556 lhsop = gimple_call_lhs (t);
4557 if (lhsop)
4559 auto_vec<ce_s, 2> lhsc;
4560 struct constraint_expr rhs;
4561 struct constraint_expr *lhsp;
4563 get_constraint_for (lhsop, &lhsc);
4564 rhs = get_function_part_constraint (fi, fi_result);
4565 if (fndecl
4566 && DECL_RESULT (fndecl)
4567 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4569 auto_vec<ce_s, 2> tem;
4570 tem.quick_push (rhs);
4571 do_deref (&tem);
4572 gcc_checking_assert (tem.length () == 1);
4573 rhs = tem[0];
4575 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4576 process_constraint (new_constraint (*lhsp, rhs));
4579 /* If we pass the result decl by reference, honor that. */
4580 if (lhsop
4581 && fndecl
4582 && DECL_RESULT (fndecl)
4583 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4585 struct constraint_expr lhs;
4586 struct constraint_expr *rhsp;
4588 get_constraint_for_address_of (lhsop, &rhsc);
4589 lhs = get_function_part_constraint (fi, fi_result);
4590 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4591 process_constraint (new_constraint (lhs, *rhsp));
4592 rhsc.truncate (0);
4595 /* If we use a static chain, pass it along. */
4596 if (gimple_call_chain (t))
4598 struct constraint_expr lhs;
4599 struct constraint_expr *rhsp;
4601 get_constraint_for (gimple_call_chain (t), &rhsc);
4602 lhs = get_function_part_constraint (fi, fi_static_chain);
4603 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4604 process_constraint (new_constraint (lhs, *rhsp));
4609 /* Walk statement T setting up aliasing constraints according to the
4610 references found in T. This function is the main part of the
4611 constraint builder. AI points to auxiliary alias information used
4612 when building alias sets and computing alias grouping heuristics. */
4614 static void
4615 find_func_aliases (struct function *fn, gimple origt)
4617 gimple t = origt;
4618 auto_vec<ce_s, 16> lhsc;
4619 auto_vec<ce_s, 16> rhsc;
4620 struct constraint_expr *c;
4621 varinfo_t fi;
4623 /* Now build constraints expressions. */
4624 if (gimple_code (t) == GIMPLE_PHI)
4626 size_t i;
4627 unsigned int j;
4629 /* For a phi node, assign all the arguments to
4630 the result. */
4631 get_constraint_for (gimple_phi_result (t), &lhsc);
4632 for (i = 0; i < gimple_phi_num_args (t); i++)
4634 tree strippedrhs = PHI_ARG_DEF (t, i);
4636 STRIP_NOPS (strippedrhs);
4637 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
4639 FOR_EACH_VEC_ELT (lhsc, j, c)
4641 struct constraint_expr *c2;
4642 while (rhsc.length () > 0)
4644 c2 = &rhsc.last ();
4645 process_constraint (new_constraint (*c, *c2));
4646 rhsc.pop ();
4651 /* In IPA mode, we need to generate constraints to pass call
4652 arguments through their calls. There are two cases,
4653 either a GIMPLE_CALL returning a value, or just a plain
4654 GIMPLE_CALL when we are not.
4656 In non-ipa mode, we need to generate constraints for each
4657 pointer passed by address. */
4658 else if (is_gimple_call (t))
4659 find_func_aliases_for_call (fn, t);
4661 /* Otherwise, just a regular assignment statement. Only care about
4662 operations with pointer result, others are dealt with as escape
4663 points if they have pointer operands. */
4664 else if (is_gimple_assign (t))
4666 /* Otherwise, just a regular assignment statement. */
4667 tree lhsop = gimple_assign_lhs (t);
4668 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
4670 if (rhsop && TREE_CLOBBER_P (rhsop))
4671 /* Ignore clobbers, they don't actually store anything into
4672 the LHS. */
4674 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
4675 do_structure_copy (lhsop, rhsop);
4676 else
4678 enum tree_code code = gimple_assign_rhs_code (t);
4680 get_constraint_for (lhsop, &lhsc);
4682 if (FLOAT_TYPE_P (TREE_TYPE (lhsop)))
4683 /* If the operation produces a floating point result then
4684 assume the value is not produced to transfer a pointer. */
4686 else if (code == POINTER_PLUS_EXPR)
4687 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4688 gimple_assign_rhs2 (t), &rhsc);
4689 else if (code == BIT_AND_EXPR
4690 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
4692 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4693 the pointer. Handle it by offsetting it by UNKNOWN. */
4694 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4695 NULL_TREE, &rhsc);
4697 else if ((CONVERT_EXPR_CODE_P (code)
4698 && !(POINTER_TYPE_P (gimple_expr_type (t))
4699 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
4700 || gimple_assign_single_p (t))
4701 get_constraint_for_rhs (rhsop, &rhsc);
4702 else if (code == COND_EXPR)
4704 /* The result is a merge of both COND_EXPR arms. */
4705 auto_vec<ce_s, 2> tmp;
4706 struct constraint_expr *rhsp;
4707 unsigned i;
4708 get_constraint_for_rhs (gimple_assign_rhs2 (t), &rhsc);
4709 get_constraint_for_rhs (gimple_assign_rhs3 (t), &tmp);
4710 FOR_EACH_VEC_ELT (tmp, i, rhsp)
4711 rhsc.safe_push (*rhsp);
4713 else if (truth_value_p (code))
4714 /* Truth value results are not pointer (parts). Or at least
4715 very very unreasonable obfuscation of a part. */
4717 else
4719 /* All other operations are merges. */
4720 auto_vec<ce_s, 4> tmp;
4721 struct constraint_expr *rhsp;
4722 unsigned i, j;
4723 get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc);
4724 for (i = 2; i < gimple_num_ops (t); ++i)
4726 get_constraint_for_rhs (gimple_op (t, i), &tmp);
4727 FOR_EACH_VEC_ELT (tmp, j, rhsp)
4728 rhsc.safe_push (*rhsp);
4729 tmp.truncate (0);
4732 process_all_all_constraints (lhsc, rhsc);
4734 /* If there is a store to a global variable the rhs escapes. */
4735 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
4736 && DECL_P (lhsop)
4737 && is_global_var (lhsop)
4738 && (!in_ipa_mode
4739 || DECL_EXTERNAL (lhsop) || TREE_PUBLIC (lhsop)))
4740 make_escape_constraint (rhsop);
4742 /* Handle escapes through return. */
4743 else if (gimple_code (t) == GIMPLE_RETURN
4744 && gimple_return_retval (t) != NULL_TREE)
4746 fi = NULL;
4747 if (!in_ipa_mode
4748 || !(fi = get_vi_for_tree (fn->decl)))
4749 make_escape_constraint (gimple_return_retval (t));
4750 else if (in_ipa_mode
4751 && fi != NULL)
4753 struct constraint_expr lhs ;
4754 struct constraint_expr *rhsp;
4755 unsigned i;
4757 lhs = get_function_part_constraint (fi, fi_result);
4758 get_constraint_for_rhs (gimple_return_retval (t), &rhsc);
4759 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4760 process_constraint (new_constraint (lhs, *rhsp));
4763 /* Handle asms conservatively by adding escape constraints to everything. */
4764 else if (gimple_code (t) == GIMPLE_ASM)
4766 unsigned i, noutputs;
4767 const char **oconstraints;
4768 const char *constraint;
4769 bool allows_mem, allows_reg, is_inout;
4771 noutputs = gimple_asm_noutputs (t);
4772 oconstraints = XALLOCAVEC (const char *, noutputs);
4774 for (i = 0; i < noutputs; ++i)
4776 tree link = gimple_asm_output_op (t, i);
4777 tree op = TREE_VALUE (link);
4779 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4780 oconstraints[i] = constraint;
4781 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4782 &allows_reg, &is_inout);
4784 /* A memory constraint makes the address of the operand escape. */
4785 if (!allows_reg && allows_mem)
4786 make_escape_constraint (build_fold_addr_expr (op));
4788 /* The asm may read global memory, so outputs may point to
4789 any global memory. */
4790 if (op)
4792 auto_vec<ce_s, 2> lhsc;
4793 struct constraint_expr rhsc, *lhsp;
4794 unsigned j;
4795 get_constraint_for (op, &lhsc);
4796 rhsc.var = nonlocal_id;
4797 rhsc.offset = 0;
4798 rhsc.type = SCALAR;
4799 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4800 process_constraint (new_constraint (*lhsp, rhsc));
4803 for (i = 0; i < gimple_asm_ninputs (t); ++i)
4805 tree link = gimple_asm_input_op (t, i);
4806 tree op = TREE_VALUE (link);
4808 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4810 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4811 &allows_mem, &allows_reg);
4813 /* A memory constraint makes the address of the operand escape. */
4814 if (!allows_reg && allows_mem)
4815 make_escape_constraint (build_fold_addr_expr (op));
4816 /* Strictly we'd only need the constraint to ESCAPED if
4817 the asm clobbers memory, otherwise using something
4818 along the lines of per-call clobbers/uses would be enough. */
4819 else if (op)
4820 make_escape_constraint (op);
4826 /* Create a constraint adding to the clobber set of FI the memory
4827 pointed to by PTR. */
4829 static void
4830 process_ipa_clobber (varinfo_t fi, tree ptr)
4832 vec<ce_s> ptrc = vNULL;
4833 struct constraint_expr *c, lhs;
4834 unsigned i;
4835 get_constraint_for_rhs (ptr, &ptrc);
4836 lhs = get_function_part_constraint (fi, fi_clobbers);
4837 FOR_EACH_VEC_ELT (ptrc, i, c)
4838 process_constraint (new_constraint (lhs, *c));
4839 ptrc.release ();
4842 /* Walk statement T setting up clobber and use constraints according to the
4843 references found in T. This function is a main part of the
4844 IPA constraint builder. */
4846 static void
4847 find_func_clobbers (struct function *fn, gimple origt)
4849 gimple t = origt;
4850 auto_vec<ce_s, 16> lhsc;
4851 auto_vec<ce_s, 16> rhsc;
4852 varinfo_t fi;
4854 /* Add constraints for clobbered/used in IPA mode.
4855 We are not interested in what automatic variables are clobbered
4856 or used as we only use the information in the caller to which
4857 they do not escape. */
4858 gcc_assert (in_ipa_mode);
4860 /* If the stmt refers to memory in any way it better had a VUSE. */
4861 if (gimple_vuse (t) == NULL_TREE)
4862 return;
4864 /* We'd better have function information for the current function. */
4865 fi = lookup_vi_for_tree (fn->decl);
4866 gcc_assert (fi != NULL);
4868 /* Account for stores in assignments and calls. */
4869 if (gimple_vdef (t) != NULL_TREE
4870 && gimple_has_lhs (t))
4872 tree lhs = gimple_get_lhs (t);
4873 tree tem = lhs;
4874 while (handled_component_p (tem))
4875 tem = TREE_OPERAND (tem, 0);
4876 if ((DECL_P (tem)
4877 && !auto_var_in_fn_p (tem, fn->decl))
4878 || INDIRECT_REF_P (tem)
4879 || (TREE_CODE (tem) == MEM_REF
4880 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4881 && auto_var_in_fn_p
4882 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl))))
4884 struct constraint_expr lhsc, *rhsp;
4885 unsigned i;
4886 lhsc = get_function_part_constraint (fi, fi_clobbers);
4887 get_constraint_for_address_of (lhs, &rhsc);
4888 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4889 process_constraint (new_constraint (lhsc, *rhsp));
4890 rhsc.truncate (0);
4894 /* Account for uses in assigments and returns. */
4895 if (gimple_assign_single_p (t)
4896 || (gimple_code (t) == GIMPLE_RETURN
4897 && gimple_return_retval (t) != NULL_TREE))
4899 tree rhs = (gimple_assign_single_p (t)
4900 ? gimple_assign_rhs1 (t) : gimple_return_retval (t));
4901 tree tem = rhs;
4902 while (handled_component_p (tem))
4903 tem = TREE_OPERAND (tem, 0);
4904 if ((DECL_P (tem)
4905 && !auto_var_in_fn_p (tem, fn->decl))
4906 || INDIRECT_REF_P (tem)
4907 || (TREE_CODE (tem) == MEM_REF
4908 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4909 && auto_var_in_fn_p
4910 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl))))
4912 struct constraint_expr lhs, *rhsp;
4913 unsigned i;
4914 lhs = get_function_part_constraint (fi, fi_uses);
4915 get_constraint_for_address_of (rhs, &rhsc);
4916 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4917 process_constraint (new_constraint (lhs, *rhsp));
4918 rhsc.truncate (0);
4922 if (is_gimple_call (t))
4924 varinfo_t cfi = NULL;
4925 tree decl = gimple_call_fndecl (t);
4926 struct constraint_expr lhs, rhs;
4927 unsigned i, j;
4929 /* For builtins we do not have separate function info. For those
4930 we do not generate escapes for we have to generate clobbers/uses. */
4931 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4932 switch (DECL_FUNCTION_CODE (decl))
4934 /* The following functions use and clobber memory pointed to
4935 by their arguments. */
4936 case BUILT_IN_STRCPY:
4937 case BUILT_IN_STRNCPY:
4938 case BUILT_IN_BCOPY:
4939 case BUILT_IN_MEMCPY:
4940 case BUILT_IN_MEMMOVE:
4941 case BUILT_IN_MEMPCPY:
4942 case BUILT_IN_STPCPY:
4943 case BUILT_IN_STPNCPY:
4944 case BUILT_IN_STRCAT:
4945 case BUILT_IN_STRNCAT:
4946 case BUILT_IN_STRCPY_CHK:
4947 case BUILT_IN_STRNCPY_CHK:
4948 case BUILT_IN_MEMCPY_CHK:
4949 case BUILT_IN_MEMMOVE_CHK:
4950 case BUILT_IN_MEMPCPY_CHK:
4951 case BUILT_IN_STPCPY_CHK:
4952 case BUILT_IN_STPNCPY_CHK:
4953 case BUILT_IN_STRCAT_CHK:
4954 case BUILT_IN_STRNCAT_CHK:
4956 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4957 == BUILT_IN_BCOPY ? 1 : 0));
4958 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4959 == BUILT_IN_BCOPY ? 0 : 1));
4960 unsigned i;
4961 struct constraint_expr *rhsp, *lhsp;
4962 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4963 lhs = get_function_part_constraint (fi, fi_clobbers);
4964 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4965 process_constraint (new_constraint (lhs, *lhsp));
4966 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4967 lhs = get_function_part_constraint (fi, fi_uses);
4968 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4969 process_constraint (new_constraint (lhs, *rhsp));
4970 return;
4972 /* The following function clobbers memory pointed to by
4973 its argument. */
4974 case BUILT_IN_MEMSET:
4975 case BUILT_IN_MEMSET_CHK:
4976 case BUILT_IN_POSIX_MEMALIGN:
4978 tree dest = gimple_call_arg (t, 0);
4979 unsigned i;
4980 ce_s *lhsp;
4981 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4982 lhs = get_function_part_constraint (fi, fi_clobbers);
4983 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4984 process_constraint (new_constraint (lhs, *lhsp));
4985 return;
4987 /* The following functions clobber their second and third
4988 arguments. */
4989 case BUILT_IN_SINCOS:
4990 case BUILT_IN_SINCOSF:
4991 case BUILT_IN_SINCOSL:
4993 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4994 process_ipa_clobber (fi, gimple_call_arg (t, 2));
4995 return;
4997 /* The following functions clobber their second argument. */
4998 case BUILT_IN_FREXP:
4999 case BUILT_IN_FREXPF:
5000 case BUILT_IN_FREXPL:
5001 case BUILT_IN_LGAMMA_R:
5002 case BUILT_IN_LGAMMAF_R:
5003 case BUILT_IN_LGAMMAL_R:
5004 case BUILT_IN_GAMMA_R:
5005 case BUILT_IN_GAMMAF_R:
5006 case BUILT_IN_GAMMAL_R:
5007 case BUILT_IN_MODF:
5008 case BUILT_IN_MODFF:
5009 case BUILT_IN_MODFL:
5011 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5012 return;
5014 /* The following functions clobber their third argument. */
5015 case BUILT_IN_REMQUO:
5016 case BUILT_IN_REMQUOF:
5017 case BUILT_IN_REMQUOL:
5019 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5020 return;
5022 /* The following functions neither read nor clobber memory. */
5023 case BUILT_IN_ASSUME_ALIGNED:
5024 case BUILT_IN_FREE:
5025 return;
5026 /* Trampolines are of no interest to us. */
5027 case BUILT_IN_INIT_TRAMPOLINE:
5028 case BUILT_IN_ADJUST_TRAMPOLINE:
5029 return;
5030 case BUILT_IN_VA_START:
5031 case BUILT_IN_VA_END:
5032 return;
5033 /* printf-style functions may have hooks to set pointers to
5034 point to somewhere into the generated string. Leave them
5035 for a later exercise... */
5036 default:
5037 /* Fallthru to general call handling. */;
5040 /* Parameters passed by value are used. */
5041 lhs = get_function_part_constraint (fi, fi_uses);
5042 for (i = 0; i < gimple_call_num_args (t); i++)
5044 struct constraint_expr *rhsp;
5045 tree arg = gimple_call_arg (t, i);
5047 if (TREE_CODE (arg) == SSA_NAME
5048 || is_gimple_min_invariant (arg))
5049 continue;
5051 get_constraint_for_address_of (arg, &rhsc);
5052 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5053 process_constraint (new_constraint (lhs, *rhsp));
5054 rhsc.truncate (0);
5057 /* Build constraints for propagating clobbers/uses along the
5058 callgraph edges. */
5059 cfi = get_fi_for_callee (t);
5060 if (cfi->id == anything_id)
5062 if (gimple_vdef (t))
5063 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5064 anything_id);
5065 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5066 anything_id);
5067 return;
5070 /* For callees without function info (that's external functions),
5071 ESCAPED is clobbered and used. */
5072 if (gimple_call_fndecl (t)
5073 && !cfi->is_fn_info)
5075 varinfo_t vi;
5077 if (gimple_vdef (t))
5078 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5079 escaped_id);
5080 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
5082 /* Also honor the call statement use/clobber info. */
5083 if ((vi = lookup_call_clobber_vi (t)) != NULL)
5084 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5085 vi->id);
5086 if ((vi = lookup_call_use_vi (t)) != NULL)
5087 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
5088 vi->id);
5089 return;
5092 /* Otherwise the caller clobbers and uses what the callee does.
5093 ??? This should use a new complex constraint that filters
5094 local variables of the callee. */
5095 if (gimple_vdef (t))
5097 lhs = get_function_part_constraint (fi, fi_clobbers);
5098 rhs = get_function_part_constraint (cfi, fi_clobbers);
5099 process_constraint (new_constraint (lhs, rhs));
5101 lhs = get_function_part_constraint (fi, fi_uses);
5102 rhs = get_function_part_constraint (cfi, fi_uses);
5103 process_constraint (new_constraint (lhs, rhs));
5105 else if (gimple_code (t) == GIMPLE_ASM)
5107 /* ??? Ick. We can do better. */
5108 if (gimple_vdef (t))
5109 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5110 anything_id);
5111 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5112 anything_id);
5117 /* Find the first varinfo in the same variable as START that overlaps with
5118 OFFSET. Return NULL if we can't find one. */
5120 static varinfo_t
5121 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
5123 /* If the offset is outside of the variable, bail out. */
5124 if (offset >= start->fullsize)
5125 return NULL;
5127 /* If we cannot reach offset from start, lookup the first field
5128 and start from there. */
5129 if (start->offset > offset)
5130 start = get_varinfo (start->head);
5132 while (start)
5134 /* We may not find a variable in the field list with the actual
5135 offset when when we have glommed a structure to a variable.
5136 In that case, however, offset should still be within the size
5137 of the variable. */
5138 if (offset >= start->offset
5139 && (offset - start->offset) < start->size)
5140 return start;
5142 start = vi_next (start);
5145 return NULL;
5148 /* Find the first varinfo in the same variable as START that overlaps with
5149 OFFSET. If there is no such varinfo the varinfo directly preceding
5150 OFFSET is returned. */
5152 static varinfo_t
5153 first_or_preceding_vi_for_offset (varinfo_t start,
5154 unsigned HOST_WIDE_INT offset)
5156 /* If we cannot reach offset from start, lookup the first field
5157 and start from there. */
5158 if (start->offset > offset)
5159 start = get_varinfo (start->head);
5161 /* We may not find a variable in the field list with the actual
5162 offset when when we have glommed a structure to a variable.
5163 In that case, however, offset should still be within the size
5164 of the variable.
5165 If we got beyond the offset we look for return the field
5166 directly preceding offset which may be the last field. */
5167 while (start->next
5168 && offset >= start->offset
5169 && !((offset - start->offset) < start->size))
5170 start = vi_next (start);
5172 return start;
5176 /* This structure is used during pushing fields onto the fieldstack
5177 to track the offset of the field, since bitpos_of_field gives it
5178 relative to its immediate containing type, and we want it relative
5179 to the ultimate containing object. */
5181 struct fieldoff
5183 /* Offset from the base of the base containing object to this field. */
5184 HOST_WIDE_INT offset;
5186 /* Size, in bits, of the field. */
5187 unsigned HOST_WIDE_INT size;
5189 unsigned has_unknown_size : 1;
5191 unsigned must_have_pointers : 1;
5193 unsigned may_have_pointers : 1;
5195 unsigned only_restrict_pointers : 1;
5197 typedef struct fieldoff fieldoff_s;
5200 /* qsort comparison function for two fieldoff's PA and PB */
5202 static int
5203 fieldoff_compare (const void *pa, const void *pb)
5205 const fieldoff_s *foa = (const fieldoff_s *)pa;
5206 const fieldoff_s *fob = (const fieldoff_s *)pb;
5207 unsigned HOST_WIDE_INT foasize, fobsize;
5209 if (foa->offset < fob->offset)
5210 return -1;
5211 else if (foa->offset > fob->offset)
5212 return 1;
5214 foasize = foa->size;
5215 fobsize = fob->size;
5216 if (foasize < fobsize)
5217 return -1;
5218 else if (foasize > fobsize)
5219 return 1;
5220 return 0;
5223 /* Sort a fieldstack according to the field offset and sizes. */
5224 static void
5225 sort_fieldstack (vec<fieldoff_s> fieldstack)
5227 fieldstack.qsort (fieldoff_compare);
5230 /* Return true if T is a type that can have subvars. */
5232 static inline bool
5233 type_can_have_subvars (const_tree t)
5235 /* Aggregates without overlapping fields can have subvars. */
5236 return TREE_CODE (t) == RECORD_TYPE;
5239 /* Return true if V is a tree that we can have subvars for.
5240 Normally, this is any aggregate type. Also complex
5241 types which are not gimple registers can have subvars. */
5243 static inline bool
5244 var_can_have_subvars (const_tree v)
5246 /* Volatile variables should never have subvars. */
5247 if (TREE_THIS_VOLATILE (v))
5248 return false;
5250 /* Non decls or memory tags can never have subvars. */
5251 if (!DECL_P (v))
5252 return false;
5254 return type_can_have_subvars (TREE_TYPE (v));
5257 /* Return true if T is a type that does contain pointers. */
5259 static bool
5260 type_must_have_pointers (tree type)
5262 if (POINTER_TYPE_P (type))
5263 return true;
5265 if (TREE_CODE (type) == ARRAY_TYPE)
5266 return type_must_have_pointers (TREE_TYPE (type));
5268 /* A function or method can have pointers as arguments, so track
5269 those separately. */
5270 if (TREE_CODE (type) == FUNCTION_TYPE
5271 || TREE_CODE (type) == METHOD_TYPE)
5272 return true;
5274 return false;
5277 static bool
5278 field_must_have_pointers (tree t)
5280 return type_must_have_pointers (TREE_TYPE (t));
5283 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5284 the fields of TYPE onto fieldstack, recording their offsets along
5285 the way.
5287 OFFSET is used to keep track of the offset in this entire
5288 structure, rather than just the immediately containing structure.
5289 Returns false if the caller is supposed to handle the field we
5290 recursed for. */
5292 static bool
5293 push_fields_onto_fieldstack (tree type, vec<fieldoff_s> *fieldstack,
5294 HOST_WIDE_INT offset)
5296 tree field;
5297 bool empty_p = true;
5299 if (TREE_CODE (type) != RECORD_TYPE)
5300 return false;
5302 /* If the vector of fields is growing too big, bail out early.
5303 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5304 sure this fails. */
5305 if (fieldstack->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5306 return false;
5308 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5309 if (TREE_CODE (field) == FIELD_DECL)
5311 bool push = false;
5312 HOST_WIDE_INT foff = bitpos_of_field (field);
5314 if (!var_can_have_subvars (field)
5315 || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
5316 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5317 push = true;
5318 else if (!push_fields_onto_fieldstack
5319 (TREE_TYPE (field), fieldstack, offset + foff)
5320 && (DECL_SIZE (field)
5321 && !integer_zerop (DECL_SIZE (field))))
5322 /* Empty structures may have actual size, like in C++. So
5323 see if we didn't push any subfields and the size is
5324 nonzero, push the field onto the stack. */
5325 push = true;
5327 if (push)
5329 fieldoff_s *pair = NULL;
5330 bool has_unknown_size = false;
5331 bool must_have_pointers_p;
5333 if (!fieldstack->is_empty ())
5334 pair = &fieldstack->last ();
5336 /* If there isn't anything at offset zero, create sth. */
5337 if (!pair
5338 && offset + foff != 0)
5340 fieldoff_s e = {0, offset + foff, false, false, false, false};
5341 pair = fieldstack->safe_push (e);
5344 if (!DECL_SIZE (field)
5345 || !tree_fits_uhwi_p (DECL_SIZE (field)))
5346 has_unknown_size = true;
5348 /* If adjacent fields do not contain pointers merge them. */
5349 must_have_pointers_p = field_must_have_pointers (field);
5350 if (pair
5351 && !has_unknown_size
5352 && !must_have_pointers_p
5353 && !pair->must_have_pointers
5354 && !pair->has_unknown_size
5355 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5357 pair->size += tree_to_uhwi (DECL_SIZE (field));
5359 else
5361 fieldoff_s e;
5362 e.offset = offset + foff;
5363 e.has_unknown_size = has_unknown_size;
5364 if (!has_unknown_size)
5365 e.size = tree_to_uhwi (DECL_SIZE (field));
5366 else
5367 e.size = -1;
5368 e.must_have_pointers = must_have_pointers_p;
5369 e.may_have_pointers = true;
5370 e.only_restrict_pointers
5371 = (!has_unknown_size
5372 && POINTER_TYPE_P (TREE_TYPE (field))
5373 && TYPE_RESTRICT (TREE_TYPE (field)));
5374 fieldstack->safe_push (e);
5378 empty_p = false;
5381 return !empty_p;
5384 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5385 if it is a varargs function. */
5387 static unsigned int
5388 count_num_arguments (tree decl, bool *is_varargs)
5390 unsigned int num = 0;
5391 tree t;
5393 /* Capture named arguments for K&R functions. They do not
5394 have a prototype and thus no TYPE_ARG_TYPES. */
5395 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5396 ++num;
5398 /* Check if the function has variadic arguments. */
5399 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5400 if (TREE_VALUE (t) == void_type_node)
5401 break;
5402 if (!t)
5403 *is_varargs = true;
5405 return num;
5408 /* Creation function node for DECL, using NAME, and return the index
5409 of the variable we've created for the function. */
5411 static varinfo_t
5412 create_function_info_for (tree decl, const char *name)
5414 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5415 varinfo_t vi, prev_vi;
5416 tree arg;
5417 unsigned int i;
5418 bool is_varargs = false;
5419 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5421 /* Create the variable info. */
5423 vi = new_var_info (decl, name);
5424 vi->offset = 0;
5425 vi->size = 1;
5426 vi->fullsize = fi_parm_base + num_args;
5427 vi->is_fn_info = 1;
5428 vi->may_have_pointers = false;
5429 if (is_varargs)
5430 vi->fullsize = ~0;
5431 insert_vi_for_tree (vi->decl, vi);
5433 prev_vi = vi;
5435 /* Create a variable for things the function clobbers and one for
5436 things the function uses. */
5438 varinfo_t clobbervi, usevi;
5439 const char *newname;
5440 char *tempname;
5442 asprintf (&tempname, "%s.clobber", name);
5443 newname = ggc_strdup (tempname);
5444 free (tempname);
5446 clobbervi = new_var_info (NULL, newname);
5447 clobbervi->offset = fi_clobbers;
5448 clobbervi->size = 1;
5449 clobbervi->fullsize = vi->fullsize;
5450 clobbervi->is_full_var = true;
5451 clobbervi->is_global_var = false;
5452 gcc_assert (prev_vi->offset < clobbervi->offset);
5453 prev_vi->next = clobbervi->id;
5454 prev_vi = clobbervi;
5456 asprintf (&tempname, "%s.use", name);
5457 newname = ggc_strdup (tempname);
5458 free (tempname);
5460 usevi = new_var_info (NULL, newname);
5461 usevi->offset = fi_uses;
5462 usevi->size = 1;
5463 usevi->fullsize = vi->fullsize;
5464 usevi->is_full_var = true;
5465 usevi->is_global_var = false;
5466 gcc_assert (prev_vi->offset < usevi->offset);
5467 prev_vi->next = usevi->id;
5468 prev_vi = usevi;
5471 /* And one for the static chain. */
5472 if (fn->static_chain_decl != NULL_TREE)
5474 varinfo_t chainvi;
5475 const char *newname;
5476 char *tempname;
5478 asprintf (&tempname, "%s.chain", name);
5479 newname = ggc_strdup (tempname);
5480 free (tempname);
5482 chainvi = new_var_info (fn->static_chain_decl, newname);
5483 chainvi->offset = fi_static_chain;
5484 chainvi->size = 1;
5485 chainvi->fullsize = vi->fullsize;
5486 chainvi->is_full_var = true;
5487 chainvi->is_global_var = false;
5488 gcc_assert (prev_vi->offset < chainvi->offset);
5489 prev_vi->next = chainvi->id;
5490 prev_vi = chainvi;
5491 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5494 /* Create a variable for the return var. */
5495 if (DECL_RESULT (decl) != NULL
5496 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5498 varinfo_t resultvi;
5499 const char *newname;
5500 char *tempname;
5501 tree resultdecl = decl;
5503 if (DECL_RESULT (decl))
5504 resultdecl = DECL_RESULT (decl);
5506 asprintf (&tempname, "%s.result", name);
5507 newname = ggc_strdup (tempname);
5508 free (tempname);
5510 resultvi = new_var_info (resultdecl, newname);
5511 resultvi->offset = fi_result;
5512 resultvi->size = 1;
5513 resultvi->fullsize = vi->fullsize;
5514 resultvi->is_full_var = true;
5515 if (DECL_RESULT (decl))
5516 resultvi->may_have_pointers = true;
5517 gcc_assert (prev_vi->offset < resultvi->offset);
5518 prev_vi->next = resultvi->id;
5519 prev_vi = resultvi;
5520 if (DECL_RESULT (decl))
5521 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
5524 /* Set up variables for each argument. */
5525 arg = DECL_ARGUMENTS (decl);
5526 for (i = 0; i < num_args; i++)
5528 varinfo_t argvi;
5529 const char *newname;
5530 char *tempname;
5531 tree argdecl = decl;
5533 if (arg)
5534 argdecl = arg;
5536 asprintf (&tempname, "%s.arg%d", name, i);
5537 newname = ggc_strdup (tempname);
5538 free (tempname);
5540 argvi = new_var_info (argdecl, newname);
5541 argvi->offset = fi_parm_base + i;
5542 argvi->size = 1;
5543 argvi->is_full_var = true;
5544 argvi->fullsize = vi->fullsize;
5545 if (arg)
5546 argvi->may_have_pointers = true;
5547 gcc_assert (prev_vi->offset < argvi->offset);
5548 prev_vi->next = argvi->id;
5549 prev_vi = argvi;
5550 if (arg)
5552 insert_vi_for_tree (arg, argvi);
5553 arg = DECL_CHAIN (arg);
5557 /* Add one representative for all further args. */
5558 if (is_varargs)
5560 varinfo_t argvi;
5561 const char *newname;
5562 char *tempname;
5563 tree decl;
5565 asprintf (&tempname, "%s.varargs", name);
5566 newname = ggc_strdup (tempname);
5567 free (tempname);
5569 /* We need sth that can be pointed to for va_start. */
5570 decl = build_fake_var_decl (ptr_type_node);
5572 argvi = new_var_info (decl, newname);
5573 argvi->offset = fi_parm_base + num_args;
5574 argvi->size = ~0;
5575 argvi->is_full_var = true;
5576 argvi->is_heap_var = true;
5577 argvi->fullsize = vi->fullsize;
5578 gcc_assert (prev_vi->offset < argvi->offset);
5579 prev_vi->next = argvi->id;
5580 prev_vi = argvi;
5583 return vi;
5587 /* Return true if FIELDSTACK contains fields that overlap.
5588 FIELDSTACK is assumed to be sorted by offset. */
5590 static bool
5591 check_for_overlaps (vec<fieldoff_s> fieldstack)
5593 fieldoff_s *fo = NULL;
5594 unsigned int i;
5595 HOST_WIDE_INT lastoffset = -1;
5597 FOR_EACH_VEC_ELT (fieldstack, i, fo)
5599 if (fo->offset == lastoffset)
5600 return true;
5601 lastoffset = fo->offset;
5603 return false;
5606 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5607 This will also create any varinfo structures necessary for fields
5608 of DECL. */
5610 static varinfo_t
5611 create_variable_info_for_1 (tree decl, const char *name)
5613 varinfo_t vi, newvi;
5614 tree decl_type = TREE_TYPE (decl);
5615 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
5616 auto_vec<fieldoff_s> fieldstack;
5617 fieldoff_s *fo;
5618 unsigned int i;
5619 varpool_node *vnode;
5621 if (!declsize
5622 || !tree_fits_uhwi_p (declsize))
5624 vi = new_var_info (decl, name);
5625 vi->offset = 0;
5626 vi->size = ~0;
5627 vi->fullsize = ~0;
5628 vi->is_unknown_size_var = true;
5629 vi->is_full_var = true;
5630 vi->may_have_pointers = true;
5631 return vi;
5634 /* Collect field information. */
5635 if (use_field_sensitive
5636 && var_can_have_subvars (decl)
5637 /* ??? Force us to not use subfields for global initializers
5638 in IPA mode. Else we'd have to parse arbitrary initializers. */
5639 && !(in_ipa_mode
5640 && is_global_var (decl)
5641 && (vnode = varpool_node::get (decl))
5642 && vnode->get_constructor ()))
5644 fieldoff_s *fo = NULL;
5645 bool notokay = false;
5646 unsigned int i;
5648 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
5650 for (i = 0; !notokay && fieldstack.iterate (i, &fo); i++)
5651 if (fo->has_unknown_size
5652 || fo->offset < 0)
5654 notokay = true;
5655 break;
5658 /* We can't sort them if we have a field with a variable sized type,
5659 which will make notokay = true. In that case, we are going to return
5660 without creating varinfos for the fields anyway, so sorting them is a
5661 waste to boot. */
5662 if (!notokay)
5664 sort_fieldstack (fieldstack);
5665 /* Due to some C++ FE issues, like PR 22488, we might end up
5666 what appear to be overlapping fields even though they,
5667 in reality, do not overlap. Until the C++ FE is fixed,
5668 we will simply disable field-sensitivity for these cases. */
5669 notokay = check_for_overlaps (fieldstack);
5672 if (notokay)
5673 fieldstack.release ();
5676 /* If we didn't end up collecting sub-variables create a full
5677 variable for the decl. */
5678 if (fieldstack.length () <= 1
5679 || fieldstack.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5681 vi = new_var_info (decl, name);
5682 vi->offset = 0;
5683 vi->may_have_pointers = true;
5684 vi->fullsize = tree_to_uhwi (declsize);
5685 vi->size = vi->fullsize;
5686 vi->is_full_var = true;
5687 fieldstack.release ();
5688 return vi;
5691 vi = new_var_info (decl, name);
5692 vi->fullsize = tree_to_uhwi (declsize);
5693 for (i = 0, newvi = vi;
5694 fieldstack.iterate (i, &fo);
5695 ++i, newvi = vi_next (newvi))
5697 const char *newname = "NULL";
5698 char *tempname;
5700 if (dump_file)
5702 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC
5703 "+" HOST_WIDE_INT_PRINT_DEC, name, fo->offset, fo->size);
5704 newname = ggc_strdup (tempname);
5705 free (tempname);
5707 newvi->name = newname;
5708 newvi->offset = fo->offset;
5709 newvi->size = fo->size;
5710 newvi->fullsize = vi->fullsize;
5711 newvi->may_have_pointers = fo->may_have_pointers;
5712 newvi->only_restrict_pointers = fo->only_restrict_pointers;
5713 if (i + 1 < fieldstack.length ())
5715 varinfo_t tem = new_var_info (decl, name);
5716 newvi->next = tem->id;
5717 tem->head = vi->id;
5721 return vi;
5724 static unsigned int
5725 create_variable_info_for (tree decl, const char *name)
5727 varinfo_t vi = create_variable_info_for_1 (decl, name);
5728 unsigned int id = vi->id;
5730 insert_vi_for_tree (decl, vi);
5732 if (TREE_CODE (decl) != VAR_DECL)
5733 return id;
5735 /* Create initial constraints for globals. */
5736 for (; vi; vi = vi_next (vi))
5738 if (!vi->may_have_pointers
5739 || !vi->is_global_var)
5740 continue;
5742 /* Mark global restrict qualified pointers. */
5743 if ((POINTER_TYPE_P (TREE_TYPE (decl))
5744 && TYPE_RESTRICT (TREE_TYPE (decl)))
5745 || vi->only_restrict_pointers)
5747 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5748 continue;
5751 /* In non-IPA mode the initializer from nonlocal is all we need. */
5752 if (!in_ipa_mode
5753 || DECL_HARD_REGISTER (decl))
5754 make_copy_constraint (vi, nonlocal_id);
5756 /* In IPA mode parse the initializer and generate proper constraints
5757 for it. */
5758 else
5760 varpool_node *vnode = varpool_node::get (decl);
5762 /* For escaped variables initialize them from nonlocal. */
5763 if (!vnode->all_refs_explicit_p ())
5764 make_copy_constraint (vi, nonlocal_id);
5766 /* If this is a global variable with an initializer and we are in
5767 IPA mode generate constraints for it. */
5768 if (vnode->get_constructor ()
5769 && vnode->definition)
5771 auto_vec<ce_s> rhsc;
5772 struct constraint_expr lhs, *rhsp;
5773 unsigned i;
5774 get_constraint_for_rhs (vnode->get_constructor (), &rhsc);
5775 lhs.var = vi->id;
5776 lhs.offset = 0;
5777 lhs.type = SCALAR;
5778 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5779 process_constraint (new_constraint (lhs, *rhsp));
5780 /* If this is a variable that escapes from the unit
5781 the initializer escapes as well. */
5782 if (!vnode->all_refs_explicit_p ())
5784 lhs.var = escaped_id;
5785 lhs.offset = 0;
5786 lhs.type = SCALAR;
5787 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5788 process_constraint (new_constraint (lhs, *rhsp));
5794 return id;
5797 /* Print out the points-to solution for VAR to FILE. */
5799 static void
5800 dump_solution_for_var (FILE *file, unsigned int var)
5802 varinfo_t vi = get_varinfo (var);
5803 unsigned int i;
5804 bitmap_iterator bi;
5806 /* Dump the solution for unified vars anyway, this avoids difficulties
5807 in scanning dumps in the testsuite. */
5808 fprintf (file, "%s = { ", vi->name);
5809 vi = get_varinfo (find (var));
5810 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5811 fprintf (file, "%s ", get_varinfo (i)->name);
5812 fprintf (file, "}");
5814 /* But note when the variable was unified. */
5815 if (vi->id != var)
5816 fprintf (file, " same as %s", vi->name);
5818 fprintf (file, "\n");
5821 /* Print the points-to solution for VAR to stderr. */
5823 DEBUG_FUNCTION void
5824 debug_solution_for_var (unsigned int var)
5826 dump_solution_for_var (stderr, var);
5829 /* Create varinfo structures for all of the variables in the
5830 function for intraprocedural mode. */
5832 static void
5833 intra_create_variable_infos (struct function *fn)
5835 tree t;
5837 /* For each incoming pointer argument arg, create the constraint ARG
5838 = NONLOCAL or a dummy variable if it is a restrict qualified
5839 passed-by-reference argument. */
5840 for (t = DECL_ARGUMENTS (fn->decl); t; t = DECL_CHAIN (t))
5842 varinfo_t p = get_vi_for_tree (t);
5844 /* For restrict qualified pointers to objects passed by
5845 reference build a real representative for the pointed-to object.
5846 Treat restrict qualified references the same. */
5847 if (TYPE_RESTRICT (TREE_TYPE (t))
5848 && ((DECL_BY_REFERENCE (t) && POINTER_TYPE_P (TREE_TYPE (t)))
5849 || TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5850 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t))))
5852 struct constraint_expr lhsc, rhsc;
5853 varinfo_t vi;
5854 tree heapvar = build_fake_var_decl (TREE_TYPE (TREE_TYPE (t)));
5855 DECL_EXTERNAL (heapvar) = 1;
5856 vi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS");
5857 insert_vi_for_tree (heapvar, vi);
5858 lhsc.var = p->id;
5859 lhsc.type = SCALAR;
5860 lhsc.offset = 0;
5861 rhsc.var = vi->id;
5862 rhsc.type = ADDRESSOF;
5863 rhsc.offset = 0;
5864 process_constraint (new_constraint (lhsc, rhsc));
5865 for (; vi; vi = vi_next (vi))
5866 if (vi->may_have_pointers)
5868 if (vi->only_restrict_pointers)
5869 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5870 else
5871 make_copy_constraint (vi, nonlocal_id);
5873 continue;
5876 if (POINTER_TYPE_P (TREE_TYPE (t))
5877 && TYPE_RESTRICT (TREE_TYPE (t)))
5878 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5879 else
5881 for (; p; p = vi_next (p))
5883 if (p->only_restrict_pointers)
5884 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5885 else if (p->may_have_pointers)
5886 make_constraint_from (p, nonlocal_id);
5891 /* Add a constraint for a result decl that is passed by reference. */
5892 if (DECL_RESULT (fn->decl)
5893 && DECL_BY_REFERENCE (DECL_RESULT (fn->decl)))
5895 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (fn->decl));
5897 for (p = result_vi; p; p = vi_next (p))
5898 make_constraint_from (p, nonlocal_id);
5901 /* Add a constraint for the incoming static chain parameter. */
5902 if (fn->static_chain_decl != NULL_TREE)
5904 varinfo_t p, chain_vi = get_vi_for_tree (fn->static_chain_decl);
5906 for (p = chain_vi; p; p = vi_next (p))
5907 make_constraint_from (p, nonlocal_id);
5911 /* Structure used to put solution bitmaps in a hashtable so they can
5912 be shared among variables with the same points-to set. */
5914 typedef struct shared_bitmap_info
5916 bitmap pt_vars;
5917 hashval_t hashcode;
5918 } *shared_bitmap_info_t;
5919 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
5921 /* Shared_bitmap hashtable helpers. */
5923 struct shared_bitmap_hasher : typed_free_remove <shared_bitmap_info>
5925 typedef shared_bitmap_info value_type;
5926 typedef shared_bitmap_info compare_type;
5927 static inline hashval_t hash (const value_type *);
5928 static inline bool equal (const value_type *, const compare_type *);
5931 /* Hash function for a shared_bitmap_info_t */
5933 inline hashval_t
5934 shared_bitmap_hasher::hash (const value_type *bi)
5936 return bi->hashcode;
5939 /* Equality function for two shared_bitmap_info_t's. */
5941 inline bool
5942 shared_bitmap_hasher::equal (const value_type *sbi1, const compare_type *sbi2)
5944 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
5947 /* Shared_bitmap hashtable. */
5949 static hash_table<shared_bitmap_hasher> *shared_bitmap_table;
5951 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5952 existing instance if there is one, NULL otherwise. */
5954 static bitmap
5955 shared_bitmap_lookup (bitmap pt_vars)
5957 shared_bitmap_info **slot;
5958 struct shared_bitmap_info sbi;
5960 sbi.pt_vars = pt_vars;
5961 sbi.hashcode = bitmap_hash (pt_vars);
5963 slot = shared_bitmap_table->find_slot (&sbi, NO_INSERT);
5964 if (!slot)
5965 return NULL;
5966 else
5967 return (*slot)->pt_vars;
5971 /* Add a bitmap to the shared bitmap hashtable. */
5973 static void
5974 shared_bitmap_add (bitmap pt_vars)
5976 shared_bitmap_info **slot;
5977 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
5979 sbi->pt_vars = pt_vars;
5980 sbi->hashcode = bitmap_hash (pt_vars);
5982 slot = shared_bitmap_table->find_slot (sbi, INSERT);
5983 gcc_assert (!*slot);
5984 *slot = sbi;
5988 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
5990 static void
5991 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt)
5993 unsigned int i;
5994 bitmap_iterator bi;
5995 varinfo_t escaped_vi = get_varinfo (find (escaped_id));
5996 bool everything_escaped
5997 = escaped_vi->solution && bitmap_bit_p (escaped_vi->solution, anything_id);
5999 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
6001 varinfo_t vi = get_varinfo (i);
6003 /* The only artificial variables that are allowed in a may-alias
6004 set are heap variables. */
6005 if (vi->is_artificial_var && !vi->is_heap_var)
6006 continue;
6008 if (everything_escaped
6009 || (escaped_vi->solution
6010 && bitmap_bit_p (escaped_vi->solution, i)))
6012 pt->vars_contains_escaped = true;
6013 pt->vars_contains_escaped_heap = vi->is_heap_var;
6016 if (TREE_CODE (vi->decl) == VAR_DECL
6017 || TREE_CODE (vi->decl) == PARM_DECL
6018 || TREE_CODE (vi->decl) == RESULT_DECL)
6020 /* If we are in IPA mode we will not recompute points-to
6021 sets after inlining so make sure they stay valid. */
6022 if (in_ipa_mode
6023 && !DECL_PT_UID_SET_P (vi->decl))
6024 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
6026 /* Add the decl to the points-to set. Note that the points-to
6027 set contains global variables. */
6028 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
6029 if (vi->is_global_var)
6030 pt->vars_contains_nonlocal = true;
6036 /* Compute the points-to solution *PT for the variable VI. */
6038 static struct pt_solution
6039 find_what_var_points_to (varinfo_t orig_vi)
6041 unsigned int i;
6042 bitmap_iterator bi;
6043 bitmap finished_solution;
6044 bitmap result;
6045 varinfo_t vi;
6046 struct pt_solution *pt;
6048 /* This variable may have been collapsed, let's get the real
6049 variable. */
6050 vi = get_varinfo (find (orig_vi->id));
6052 /* See if we have already computed the solution and return it. */
6053 pt_solution **slot = &final_solutions->get_or_insert (vi);
6054 if (*slot != NULL)
6055 return **slot;
6057 *slot = pt = XOBNEW (&final_solutions_obstack, struct pt_solution);
6058 memset (pt, 0, sizeof (struct pt_solution));
6060 /* Translate artificial variables into SSA_NAME_PTR_INFO
6061 attributes. */
6062 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
6064 varinfo_t vi = get_varinfo (i);
6066 if (vi->is_artificial_var)
6068 if (vi->id == nothing_id)
6069 pt->null = 1;
6070 else if (vi->id == escaped_id)
6072 if (in_ipa_mode)
6073 pt->ipa_escaped = 1;
6074 else
6075 pt->escaped = 1;
6076 /* Expand some special vars of ESCAPED in-place here. */
6077 varinfo_t evi = get_varinfo (find (escaped_id));
6078 if (bitmap_bit_p (evi->solution, nonlocal_id))
6079 pt->nonlocal = 1;
6081 else if (vi->id == nonlocal_id)
6082 pt->nonlocal = 1;
6083 else if (vi->is_heap_var)
6084 /* We represent heapvars in the points-to set properly. */
6086 else if (vi->id == string_id)
6087 /* Nobody cares - STRING_CSTs are read-only entities. */
6089 else if (vi->id == anything_id
6090 || vi->id == integer_id)
6091 pt->anything = 1;
6095 /* Instead of doing extra work, simply do not create
6096 elaborate points-to information for pt_anything pointers. */
6097 if (pt->anything)
6098 return *pt;
6100 /* Share the final set of variables when possible. */
6101 finished_solution = BITMAP_GGC_ALLOC ();
6102 stats.points_to_sets_created++;
6104 set_uids_in_ptset (finished_solution, vi->solution, pt);
6105 result = shared_bitmap_lookup (finished_solution);
6106 if (!result)
6108 shared_bitmap_add (finished_solution);
6109 pt->vars = finished_solution;
6111 else
6113 pt->vars = result;
6114 bitmap_clear (finished_solution);
6117 return *pt;
6120 /* Given a pointer variable P, fill in its points-to set. */
6122 static void
6123 find_what_p_points_to (tree p)
6125 struct ptr_info_def *pi;
6126 tree lookup_p = p;
6127 varinfo_t vi;
6129 /* For parameters, get at the points-to set for the actual parm
6130 decl. */
6131 if (TREE_CODE (p) == SSA_NAME
6132 && SSA_NAME_IS_DEFAULT_DEF (p)
6133 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
6134 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL))
6135 lookup_p = SSA_NAME_VAR (p);
6137 vi = lookup_vi_for_tree (lookup_p);
6138 if (!vi)
6139 return;
6141 pi = get_ptr_info (p);
6142 pi->pt = find_what_var_points_to (vi);
6146 /* Query statistics for points-to solutions. */
6148 static struct {
6149 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
6150 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
6151 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
6152 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
6153 } pta_stats;
6155 void
6156 dump_pta_stats (FILE *s)
6158 fprintf (s, "\nPTA query stats:\n");
6159 fprintf (s, " pt_solution_includes: "
6160 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6161 HOST_WIDE_INT_PRINT_DEC" queries\n",
6162 pta_stats.pt_solution_includes_no_alias,
6163 pta_stats.pt_solution_includes_no_alias
6164 + pta_stats.pt_solution_includes_may_alias);
6165 fprintf (s, " pt_solutions_intersect: "
6166 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6167 HOST_WIDE_INT_PRINT_DEC" queries\n",
6168 pta_stats.pt_solutions_intersect_no_alias,
6169 pta_stats.pt_solutions_intersect_no_alias
6170 + pta_stats.pt_solutions_intersect_may_alias);
6174 /* Reset the points-to solution *PT to a conservative default
6175 (point to anything). */
6177 void
6178 pt_solution_reset (struct pt_solution *pt)
6180 memset (pt, 0, sizeof (struct pt_solution));
6181 pt->anything = true;
6184 /* Set the points-to solution *PT to point only to the variables
6185 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6186 global variables and VARS_CONTAINS_RESTRICT specifies whether
6187 it contains restrict tag variables. */
6189 void
6190 pt_solution_set (struct pt_solution *pt, bitmap vars,
6191 bool vars_contains_nonlocal)
6193 memset (pt, 0, sizeof (struct pt_solution));
6194 pt->vars = vars;
6195 pt->vars_contains_nonlocal = vars_contains_nonlocal;
6196 pt->vars_contains_escaped
6197 = (cfun->gimple_df->escaped.anything
6198 || bitmap_intersect_p (cfun->gimple_df->escaped.vars, vars));
6201 /* Set the points-to solution *PT to point only to the variable VAR. */
6203 void
6204 pt_solution_set_var (struct pt_solution *pt, tree var)
6206 memset (pt, 0, sizeof (struct pt_solution));
6207 pt->vars = BITMAP_GGC_ALLOC ();
6208 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
6209 pt->vars_contains_nonlocal = is_global_var (var);
6210 pt->vars_contains_escaped
6211 = (cfun->gimple_df->escaped.anything
6212 || bitmap_bit_p (cfun->gimple_df->escaped.vars, DECL_PT_UID (var)));
6215 /* Computes the union of the points-to solutions *DEST and *SRC and
6216 stores the result in *DEST. This changes the points-to bitmap
6217 of *DEST and thus may not be used if that might be shared.
6218 The points-to bitmap of *SRC and *DEST will not be shared after
6219 this function if they were not before. */
6221 static void
6222 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6224 dest->anything |= src->anything;
6225 if (dest->anything)
6227 pt_solution_reset (dest);
6228 return;
6231 dest->nonlocal |= src->nonlocal;
6232 dest->escaped |= src->escaped;
6233 dest->ipa_escaped |= src->ipa_escaped;
6234 dest->null |= src->null;
6235 dest->vars_contains_nonlocal |= src->vars_contains_nonlocal;
6236 dest->vars_contains_escaped |= src->vars_contains_escaped;
6237 dest->vars_contains_escaped_heap |= src->vars_contains_escaped_heap;
6238 if (!src->vars)
6239 return;
6241 if (!dest->vars)
6242 dest->vars = BITMAP_GGC_ALLOC ();
6243 bitmap_ior_into (dest->vars, src->vars);
6246 /* Return true if the points-to solution *PT is empty. */
6248 bool
6249 pt_solution_empty_p (struct pt_solution *pt)
6251 if (pt->anything
6252 || pt->nonlocal)
6253 return false;
6255 if (pt->vars
6256 && !bitmap_empty_p (pt->vars))
6257 return false;
6259 /* If the solution includes ESCAPED, check if that is empty. */
6260 if (pt->escaped
6261 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6262 return false;
6264 /* If the solution includes ESCAPED, check if that is empty. */
6265 if (pt->ipa_escaped
6266 && !pt_solution_empty_p (&ipa_escaped_pt))
6267 return false;
6269 return true;
6272 /* Return true if the points-to solution *PT only point to a single var, and
6273 return the var uid in *UID. */
6275 bool
6276 pt_solution_singleton_p (struct pt_solution *pt, unsigned *uid)
6278 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
6279 || pt->null || pt->vars == NULL
6280 || !bitmap_single_bit_set_p (pt->vars))
6281 return false;
6283 *uid = bitmap_first_set_bit (pt->vars);
6284 return true;
6287 /* Return true if the points-to solution *PT includes global memory. */
6289 bool
6290 pt_solution_includes_global (struct pt_solution *pt)
6292 if (pt->anything
6293 || pt->nonlocal
6294 || pt->vars_contains_nonlocal
6295 /* The following is a hack to make the malloc escape hack work.
6296 In reality we'd need different sets for escaped-through-return
6297 and escaped-to-callees and passes would need to be updated. */
6298 || pt->vars_contains_escaped_heap)
6299 return true;
6301 /* 'escaped' is also a placeholder so we have to look into it. */
6302 if (pt->escaped)
6303 return pt_solution_includes_global (&cfun->gimple_df->escaped);
6305 if (pt->ipa_escaped)
6306 return pt_solution_includes_global (&ipa_escaped_pt);
6308 /* ??? This predicate is not correct for the IPA-PTA solution
6309 as we do not properly distinguish between unit escape points
6310 and global variables. */
6311 if (cfun->gimple_df->ipa_pta)
6312 return true;
6314 return false;
6317 /* Return true if the points-to solution *PT includes the variable
6318 declaration DECL. */
6320 static bool
6321 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6323 if (pt->anything)
6324 return true;
6326 if (pt->nonlocal
6327 && is_global_var (decl))
6328 return true;
6330 if (pt->vars
6331 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6332 return true;
6334 /* If the solution includes ESCAPED, check it. */
6335 if (pt->escaped
6336 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6337 return true;
6339 /* If the solution includes ESCAPED, check it. */
6340 if (pt->ipa_escaped
6341 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6342 return true;
6344 return false;
6347 bool
6348 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6350 bool res = pt_solution_includes_1 (pt, decl);
6351 if (res)
6352 ++pta_stats.pt_solution_includes_may_alias;
6353 else
6354 ++pta_stats.pt_solution_includes_no_alias;
6355 return res;
6358 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6359 intersection. */
6361 static bool
6362 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
6364 if (pt1->anything || pt2->anything)
6365 return true;
6367 /* If either points to unknown global memory and the other points to
6368 any global memory they alias. */
6369 if ((pt1->nonlocal
6370 && (pt2->nonlocal
6371 || pt2->vars_contains_nonlocal))
6372 || (pt2->nonlocal
6373 && pt1->vars_contains_nonlocal))
6374 return true;
6376 /* If either points to all escaped memory and the other points to
6377 any escaped memory they alias. */
6378 if ((pt1->escaped
6379 && (pt2->escaped
6380 || pt2->vars_contains_escaped))
6381 || (pt2->escaped
6382 && pt1->vars_contains_escaped))
6383 return true;
6385 /* Check the escaped solution if required.
6386 ??? Do we need to check the local against the IPA escaped sets? */
6387 if ((pt1->ipa_escaped || pt2->ipa_escaped)
6388 && !pt_solution_empty_p (&ipa_escaped_pt))
6390 /* If both point to escaped memory and that solution
6391 is not empty they alias. */
6392 if (pt1->ipa_escaped && pt2->ipa_escaped)
6393 return true;
6395 /* If either points to escaped memory see if the escaped solution
6396 intersects with the other. */
6397 if ((pt1->ipa_escaped
6398 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
6399 || (pt2->ipa_escaped
6400 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
6401 return true;
6404 /* Now both pointers alias if their points-to solution intersects. */
6405 return (pt1->vars
6406 && pt2->vars
6407 && bitmap_intersect_p (pt1->vars, pt2->vars));
6410 bool
6411 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
6413 bool res = pt_solutions_intersect_1 (pt1, pt2);
6414 if (res)
6415 ++pta_stats.pt_solutions_intersect_may_alias;
6416 else
6417 ++pta_stats.pt_solutions_intersect_no_alias;
6418 return res;
6422 /* Dump points-to information to OUTFILE. */
6424 static void
6425 dump_sa_points_to_info (FILE *outfile)
6427 unsigned int i;
6429 fprintf (outfile, "\nPoints-to sets\n\n");
6431 if (dump_flags & TDF_STATS)
6433 fprintf (outfile, "Stats:\n");
6434 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
6435 fprintf (outfile, "Non-pointer vars: %d\n",
6436 stats.nonpointer_vars);
6437 fprintf (outfile, "Statically unified vars: %d\n",
6438 stats.unified_vars_static);
6439 fprintf (outfile, "Dynamically unified vars: %d\n",
6440 stats.unified_vars_dynamic);
6441 fprintf (outfile, "Iterations: %d\n", stats.iterations);
6442 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
6443 fprintf (outfile, "Number of implicit edges: %d\n",
6444 stats.num_implicit_edges);
6447 for (i = 1; i < varmap.length (); i++)
6449 varinfo_t vi = get_varinfo (i);
6450 if (!vi->may_have_pointers)
6451 continue;
6452 dump_solution_for_var (outfile, i);
6457 /* Debug points-to information to stderr. */
6459 DEBUG_FUNCTION void
6460 debug_sa_points_to_info (void)
6462 dump_sa_points_to_info (stderr);
6466 /* Initialize the always-existing constraint variables for NULL
6467 ANYTHING, READONLY, and INTEGER */
6469 static void
6470 init_base_vars (void)
6472 struct constraint_expr lhs, rhs;
6473 varinfo_t var_anything;
6474 varinfo_t var_nothing;
6475 varinfo_t var_string;
6476 varinfo_t var_escaped;
6477 varinfo_t var_nonlocal;
6478 varinfo_t var_storedanything;
6479 varinfo_t var_integer;
6481 /* Variable ID zero is reserved and should be NULL. */
6482 varmap.safe_push (NULL);
6484 /* Create the NULL variable, used to represent that a variable points
6485 to NULL. */
6486 var_nothing = new_var_info (NULL_TREE, "NULL");
6487 gcc_assert (var_nothing->id == nothing_id);
6488 var_nothing->is_artificial_var = 1;
6489 var_nothing->offset = 0;
6490 var_nothing->size = ~0;
6491 var_nothing->fullsize = ~0;
6492 var_nothing->is_special_var = 1;
6493 var_nothing->may_have_pointers = 0;
6494 var_nothing->is_global_var = 0;
6496 /* Create the ANYTHING variable, used to represent that a variable
6497 points to some unknown piece of memory. */
6498 var_anything = new_var_info (NULL_TREE, "ANYTHING");
6499 gcc_assert (var_anything->id == anything_id);
6500 var_anything->is_artificial_var = 1;
6501 var_anything->size = ~0;
6502 var_anything->offset = 0;
6503 var_anything->fullsize = ~0;
6504 var_anything->is_special_var = 1;
6506 /* Anything points to anything. This makes deref constraints just
6507 work in the presence of linked list and other p = *p type loops,
6508 by saying that *ANYTHING = ANYTHING. */
6509 lhs.type = SCALAR;
6510 lhs.var = anything_id;
6511 lhs.offset = 0;
6512 rhs.type = ADDRESSOF;
6513 rhs.var = anything_id;
6514 rhs.offset = 0;
6516 /* This specifically does not use process_constraint because
6517 process_constraint ignores all anything = anything constraints, since all
6518 but this one are redundant. */
6519 constraints.safe_push (new_constraint (lhs, rhs));
6521 /* Create the STRING variable, used to represent that a variable
6522 points to a string literal. String literals don't contain
6523 pointers so STRING doesn't point to anything. */
6524 var_string = new_var_info (NULL_TREE, "STRING");
6525 gcc_assert (var_string->id == string_id);
6526 var_string->is_artificial_var = 1;
6527 var_string->offset = 0;
6528 var_string->size = ~0;
6529 var_string->fullsize = ~0;
6530 var_string->is_special_var = 1;
6531 var_string->may_have_pointers = 0;
6533 /* Create the ESCAPED variable, used to represent the set of escaped
6534 memory. */
6535 var_escaped = new_var_info (NULL_TREE, "ESCAPED");
6536 gcc_assert (var_escaped->id == escaped_id);
6537 var_escaped->is_artificial_var = 1;
6538 var_escaped->offset = 0;
6539 var_escaped->size = ~0;
6540 var_escaped->fullsize = ~0;
6541 var_escaped->is_special_var = 0;
6543 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6544 memory. */
6545 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL");
6546 gcc_assert (var_nonlocal->id == nonlocal_id);
6547 var_nonlocal->is_artificial_var = 1;
6548 var_nonlocal->offset = 0;
6549 var_nonlocal->size = ~0;
6550 var_nonlocal->fullsize = ~0;
6551 var_nonlocal->is_special_var = 1;
6553 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6554 lhs.type = SCALAR;
6555 lhs.var = escaped_id;
6556 lhs.offset = 0;
6557 rhs.type = DEREF;
6558 rhs.var = escaped_id;
6559 rhs.offset = 0;
6560 process_constraint (new_constraint (lhs, rhs));
6562 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6563 whole variable escapes. */
6564 lhs.type = SCALAR;
6565 lhs.var = escaped_id;
6566 lhs.offset = 0;
6567 rhs.type = SCALAR;
6568 rhs.var = escaped_id;
6569 rhs.offset = UNKNOWN_OFFSET;
6570 process_constraint (new_constraint (lhs, rhs));
6572 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6573 everything pointed to by escaped points to what global memory can
6574 point to. */
6575 lhs.type = DEREF;
6576 lhs.var = escaped_id;
6577 lhs.offset = 0;
6578 rhs.type = SCALAR;
6579 rhs.var = nonlocal_id;
6580 rhs.offset = 0;
6581 process_constraint (new_constraint (lhs, rhs));
6583 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6584 global memory may point to global memory and escaped memory. */
6585 lhs.type = SCALAR;
6586 lhs.var = nonlocal_id;
6587 lhs.offset = 0;
6588 rhs.type = ADDRESSOF;
6589 rhs.var = nonlocal_id;
6590 rhs.offset = 0;
6591 process_constraint (new_constraint (lhs, rhs));
6592 rhs.type = ADDRESSOF;
6593 rhs.var = escaped_id;
6594 rhs.offset = 0;
6595 process_constraint (new_constraint (lhs, rhs));
6597 /* Create the STOREDANYTHING variable, used to represent the set of
6598 variables stored to *ANYTHING. */
6599 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING");
6600 gcc_assert (var_storedanything->id == storedanything_id);
6601 var_storedanything->is_artificial_var = 1;
6602 var_storedanything->offset = 0;
6603 var_storedanything->size = ~0;
6604 var_storedanything->fullsize = ~0;
6605 var_storedanything->is_special_var = 0;
6607 /* Create the INTEGER variable, used to represent that a variable points
6608 to what an INTEGER "points to". */
6609 var_integer = new_var_info (NULL_TREE, "INTEGER");
6610 gcc_assert (var_integer->id == integer_id);
6611 var_integer->is_artificial_var = 1;
6612 var_integer->size = ~0;
6613 var_integer->fullsize = ~0;
6614 var_integer->offset = 0;
6615 var_integer->is_special_var = 1;
6617 /* INTEGER = ANYTHING, because we don't know where a dereference of
6618 a random integer will point to. */
6619 lhs.type = SCALAR;
6620 lhs.var = integer_id;
6621 lhs.offset = 0;
6622 rhs.type = ADDRESSOF;
6623 rhs.var = anything_id;
6624 rhs.offset = 0;
6625 process_constraint (new_constraint (lhs, rhs));
6628 /* Initialize things necessary to perform PTA */
6630 static void
6631 init_alias_vars (void)
6633 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
6635 bitmap_obstack_initialize (&pta_obstack);
6636 bitmap_obstack_initialize (&oldpta_obstack);
6637 bitmap_obstack_initialize (&predbitmap_obstack);
6639 constraint_pool = create_alloc_pool ("Constraint pool",
6640 sizeof (struct constraint), 30);
6641 variable_info_pool = create_alloc_pool ("Variable info pool",
6642 sizeof (struct variable_info), 30);
6643 constraints.create (8);
6644 varmap.create (8);
6645 vi_for_tree = new hash_map<tree, varinfo_t>;
6646 call_stmt_vars = new hash_map<gimple, varinfo_t>;
6648 memset (&stats, 0, sizeof (stats));
6649 shared_bitmap_table = new hash_table<shared_bitmap_hasher> (511);
6650 init_base_vars ();
6652 gcc_obstack_init (&fake_var_decl_obstack);
6654 final_solutions = new hash_map<varinfo_t, pt_solution *>;
6655 gcc_obstack_init (&final_solutions_obstack);
6658 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6659 predecessor edges. */
6661 static void
6662 remove_preds_and_fake_succs (constraint_graph_t graph)
6664 unsigned int i;
6666 /* Clear the implicit ref and address nodes from the successor
6667 lists. */
6668 for (i = 1; i < FIRST_REF_NODE; i++)
6670 if (graph->succs[i])
6671 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
6672 FIRST_REF_NODE * 2);
6675 /* Free the successor list for the non-ref nodes. */
6676 for (i = FIRST_REF_NODE + 1; i < graph->size; i++)
6678 if (graph->succs[i])
6679 BITMAP_FREE (graph->succs[i]);
6682 /* Now reallocate the size of the successor list as, and blow away
6683 the predecessor bitmaps. */
6684 graph->size = varmap.length ();
6685 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
6687 free (graph->implicit_preds);
6688 graph->implicit_preds = NULL;
6689 free (graph->preds);
6690 graph->preds = NULL;
6691 bitmap_obstack_release (&predbitmap_obstack);
6694 /* Solve the constraint set. */
6696 static void
6697 solve_constraints (void)
6699 struct scc_info *si;
6701 if (dump_file)
6702 fprintf (dump_file,
6703 "\nCollapsing static cycles and doing variable "
6704 "substitution\n");
6706 init_graph (varmap.length () * 2);
6708 if (dump_file)
6709 fprintf (dump_file, "Building predecessor graph\n");
6710 build_pred_graph ();
6712 if (dump_file)
6713 fprintf (dump_file, "Detecting pointer and location "
6714 "equivalences\n");
6715 si = perform_var_substitution (graph);
6717 if (dump_file)
6718 fprintf (dump_file, "Rewriting constraints and unifying "
6719 "variables\n");
6720 rewrite_constraints (graph, si);
6722 build_succ_graph ();
6724 free_var_substitution_info (si);
6726 /* Attach complex constraints to graph nodes. */
6727 move_complex_constraints (graph);
6729 if (dump_file)
6730 fprintf (dump_file, "Uniting pointer but not location equivalent "
6731 "variables\n");
6732 unite_pointer_equivalences (graph);
6734 if (dump_file)
6735 fprintf (dump_file, "Finding indirect cycles\n");
6736 find_indirect_cycles (graph);
6738 /* Implicit nodes and predecessors are no longer necessary at this
6739 point. */
6740 remove_preds_and_fake_succs (graph);
6742 if (dump_file && (dump_flags & TDF_GRAPH))
6744 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
6745 "in dot format:\n");
6746 dump_constraint_graph (dump_file);
6747 fprintf (dump_file, "\n\n");
6750 if (dump_file)
6751 fprintf (dump_file, "Solving graph\n");
6753 solve_graph (graph);
6755 if (dump_file && (dump_flags & TDF_GRAPH))
6757 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
6758 "in dot format:\n");
6759 dump_constraint_graph (dump_file);
6760 fprintf (dump_file, "\n\n");
6763 if (dump_file)
6764 dump_sa_points_to_info (dump_file);
6767 /* Create points-to sets for the current function. See the comments
6768 at the start of the file for an algorithmic overview. */
6770 static void
6771 compute_points_to_sets (void)
6773 basic_block bb;
6774 unsigned i;
6775 varinfo_t vi;
6777 timevar_push (TV_TREE_PTA);
6779 init_alias_vars ();
6781 intra_create_variable_infos (cfun);
6783 /* Now walk all statements and build the constraint set. */
6784 FOR_EACH_BB_FN (bb, cfun)
6786 gimple_stmt_iterator gsi;
6788 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6790 gimple phi = gsi_stmt (gsi);
6792 if (! virtual_operand_p (gimple_phi_result (phi)))
6793 find_func_aliases (cfun, phi);
6796 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6798 gimple stmt = gsi_stmt (gsi);
6800 find_func_aliases (cfun, stmt);
6804 if (dump_file)
6806 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
6807 dump_constraints (dump_file, 0);
6810 /* From the constraints compute the points-to sets. */
6811 solve_constraints ();
6813 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6814 cfun->gimple_df->escaped = find_what_var_points_to (get_varinfo (escaped_id));
6816 /* Make sure the ESCAPED solution (which is used as placeholder in
6817 other solutions) does not reference itself. This simplifies
6818 points-to solution queries. */
6819 cfun->gimple_df->escaped.escaped = 0;
6821 /* Compute the points-to sets for pointer SSA_NAMEs. */
6822 for (i = 0; i < num_ssa_names; ++i)
6824 tree ptr = ssa_name (i);
6825 if (ptr
6826 && POINTER_TYPE_P (TREE_TYPE (ptr)))
6827 find_what_p_points_to (ptr);
6830 /* Compute the call-used/clobbered sets. */
6831 FOR_EACH_BB_FN (bb, cfun)
6833 gimple_stmt_iterator gsi;
6835 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6837 gimple stmt = gsi_stmt (gsi);
6838 struct pt_solution *pt;
6839 if (!is_gimple_call (stmt))
6840 continue;
6842 pt = gimple_call_use_set (stmt);
6843 if (gimple_call_flags (stmt) & ECF_CONST)
6844 memset (pt, 0, sizeof (struct pt_solution));
6845 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
6847 *pt = find_what_var_points_to (vi);
6848 /* Escaped (and thus nonlocal) variables are always
6849 implicitly used by calls. */
6850 /* ??? ESCAPED can be empty even though NONLOCAL
6851 always escaped. */
6852 pt->nonlocal = 1;
6853 pt->escaped = 1;
6855 else
6857 /* If there is nothing special about this call then
6858 we have made everything that is used also escape. */
6859 *pt = cfun->gimple_df->escaped;
6860 pt->nonlocal = 1;
6863 pt = gimple_call_clobber_set (stmt);
6864 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
6865 memset (pt, 0, sizeof (struct pt_solution));
6866 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
6868 *pt = find_what_var_points_to (vi);
6869 /* Escaped (and thus nonlocal) variables are always
6870 implicitly clobbered by calls. */
6871 /* ??? ESCAPED can be empty even though NONLOCAL
6872 always escaped. */
6873 pt->nonlocal = 1;
6874 pt->escaped = 1;
6876 else
6878 /* If there is nothing special about this call then
6879 we have made everything that is used also escape. */
6880 *pt = cfun->gimple_df->escaped;
6881 pt->nonlocal = 1;
6886 timevar_pop (TV_TREE_PTA);
6890 /* Delete created points-to sets. */
6892 static void
6893 delete_points_to_sets (void)
6895 unsigned int i;
6897 delete shared_bitmap_table;
6898 shared_bitmap_table = NULL;
6899 if (dump_file && (dump_flags & TDF_STATS))
6900 fprintf (dump_file, "Points to sets created:%d\n",
6901 stats.points_to_sets_created);
6903 delete vi_for_tree;
6904 delete call_stmt_vars;
6905 bitmap_obstack_release (&pta_obstack);
6906 constraints.release ();
6908 for (i = 0; i < graph->size; i++)
6909 graph->complex[i].release ();
6910 free (graph->complex);
6912 free (graph->rep);
6913 free (graph->succs);
6914 free (graph->pe);
6915 free (graph->pe_rep);
6916 free (graph->indirect_cycles);
6917 free (graph);
6919 varmap.release ();
6920 free_alloc_pool (variable_info_pool);
6921 free_alloc_pool (constraint_pool);
6923 obstack_free (&fake_var_decl_obstack, NULL);
6925 delete final_solutions;
6926 obstack_free (&final_solutions_obstack, NULL);
6930 /* Compute points-to information for every SSA_NAME pointer in the
6931 current function and compute the transitive closure of escaped
6932 variables to re-initialize the call-clobber states of local variables. */
6934 unsigned int
6935 compute_may_aliases (void)
6937 if (cfun->gimple_df->ipa_pta)
6939 if (dump_file)
6941 fprintf (dump_file, "\nNot re-computing points-to information "
6942 "because IPA points-to information is available.\n\n");
6944 /* But still dump what we have remaining it. */
6945 dump_alias_info (dump_file);
6948 return 0;
6951 /* For each pointer P_i, determine the sets of variables that P_i may
6952 point-to. Compute the reachability set of escaped and call-used
6953 variables. */
6954 compute_points_to_sets ();
6956 /* Debugging dumps. */
6957 if (dump_file)
6958 dump_alias_info (dump_file);
6960 /* Deallocate memory used by aliasing data structures and the internal
6961 points-to solution. */
6962 delete_points_to_sets ();
6964 gcc_assert (!need_ssa_update_p (cfun));
6966 return 0;
6969 /* A dummy pass to cause points-to information to be computed via
6970 TODO_rebuild_alias. */
6972 namespace {
6974 const pass_data pass_data_build_alias =
6976 GIMPLE_PASS, /* type */
6977 "alias", /* name */
6978 OPTGROUP_NONE, /* optinfo_flags */
6979 TV_NONE, /* tv_id */
6980 ( PROP_cfg | PROP_ssa ), /* properties_required */
6981 0, /* properties_provided */
6982 0, /* properties_destroyed */
6983 0, /* todo_flags_start */
6984 TODO_rebuild_alias, /* todo_flags_finish */
6987 class pass_build_alias : public gimple_opt_pass
6989 public:
6990 pass_build_alias (gcc::context *ctxt)
6991 : gimple_opt_pass (pass_data_build_alias, ctxt)
6994 /* opt_pass methods: */
6995 virtual bool gate (function *) { return flag_tree_pta; }
6997 }; // class pass_build_alias
6999 } // anon namespace
7001 gimple_opt_pass *
7002 make_pass_build_alias (gcc::context *ctxt)
7004 return new pass_build_alias (ctxt);
7007 /* A dummy pass to cause points-to information to be computed via
7008 TODO_rebuild_alias. */
7010 namespace {
7012 const pass_data pass_data_build_ealias =
7014 GIMPLE_PASS, /* type */
7015 "ealias", /* name */
7016 OPTGROUP_NONE, /* optinfo_flags */
7017 TV_NONE, /* tv_id */
7018 ( PROP_cfg | PROP_ssa ), /* properties_required */
7019 0, /* properties_provided */
7020 0, /* properties_destroyed */
7021 0, /* todo_flags_start */
7022 TODO_rebuild_alias, /* todo_flags_finish */
7025 class pass_build_ealias : public gimple_opt_pass
7027 public:
7028 pass_build_ealias (gcc::context *ctxt)
7029 : gimple_opt_pass (pass_data_build_ealias, ctxt)
7032 /* opt_pass methods: */
7033 virtual bool gate (function *) { return flag_tree_pta; }
7035 }; // class pass_build_ealias
7037 } // anon namespace
7039 gimple_opt_pass *
7040 make_pass_build_ealias (gcc::context *ctxt)
7042 return new pass_build_ealias (ctxt);
7046 /* IPA PTA solutions for ESCAPED. */
7047 struct pt_solution ipa_escaped_pt
7048 = { true, false, false, false, false, false, false, false, NULL };
7050 /* Associate node with varinfo DATA. Worker for
7051 cgraph_for_node_and_aliases. */
7052 static bool
7053 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
7055 if ((node->alias || node->thunk.thunk_p)
7056 && node->analyzed)
7057 insert_vi_for_tree (node->decl, (varinfo_t)data);
7058 return false;
7061 /* Execute the driver for IPA PTA. */
7062 static unsigned int
7063 ipa_pta_execute (void)
7065 struct cgraph_node *node;
7066 varpool_node *var;
7067 int from;
7069 in_ipa_mode = 1;
7071 init_alias_vars ();
7073 if (dump_file && (dump_flags & TDF_DETAILS))
7075 symtab_node::dump_table (dump_file);
7076 fprintf (dump_file, "\n");
7079 /* Build the constraints. */
7080 FOR_EACH_DEFINED_FUNCTION (node)
7082 varinfo_t vi;
7083 /* Nodes without a body are not interesting. Especially do not
7084 visit clones at this point for now - we get duplicate decls
7085 there for inline clones at least. */
7086 if (!node->has_gimple_body_p () || node->clone_of)
7087 continue;
7088 node->get_body ();
7090 gcc_assert (!node->clone_of);
7092 vi = create_function_info_for (node->decl,
7093 alias_get_name (node->decl));
7094 node->call_for_symbol_thunks_and_aliases
7095 (associate_varinfo_to_alias, vi, true);
7098 /* Create constraints for global variables and their initializers. */
7099 FOR_EACH_VARIABLE (var)
7101 if (var->alias && var->analyzed)
7102 continue;
7104 get_vi_for_tree (var->decl);
7107 if (dump_file)
7109 fprintf (dump_file,
7110 "Generating constraints for global initializers\n\n");
7111 dump_constraints (dump_file, 0);
7112 fprintf (dump_file, "\n");
7114 from = constraints.length ();
7116 FOR_EACH_DEFINED_FUNCTION (node)
7118 struct function *func;
7119 basic_block bb;
7121 /* Nodes without a body are not interesting. */
7122 if (!node->has_gimple_body_p () || node->clone_of)
7123 continue;
7125 if (dump_file)
7127 fprintf (dump_file,
7128 "Generating constraints for %s", node->name ());
7129 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
7130 fprintf (dump_file, " (%s)",
7131 IDENTIFIER_POINTER
7132 (DECL_ASSEMBLER_NAME (node->decl)));
7133 fprintf (dump_file, "\n");
7136 func = DECL_STRUCT_FUNCTION (node->decl);
7137 gcc_assert (cfun == NULL);
7139 /* For externally visible or attribute used annotated functions use
7140 local constraints for their arguments.
7141 For local functions we see all callers and thus do not need initial
7142 constraints for parameters. */
7143 if (node->used_from_other_partition
7144 || node->externally_visible
7145 || node->force_output)
7147 intra_create_variable_infos (func);
7149 /* We also need to make function return values escape. Nothing
7150 escapes by returning from main though. */
7151 if (!MAIN_NAME_P (DECL_NAME (node->decl)))
7153 varinfo_t fi, rvi;
7154 fi = lookup_vi_for_tree (node->decl);
7155 rvi = first_vi_for_offset (fi, fi_result);
7156 if (rvi && rvi->offset == fi_result)
7158 struct constraint_expr includes;
7159 struct constraint_expr var;
7160 includes.var = escaped_id;
7161 includes.offset = 0;
7162 includes.type = SCALAR;
7163 var.var = rvi->id;
7164 var.offset = 0;
7165 var.type = SCALAR;
7166 process_constraint (new_constraint (includes, var));
7171 /* Build constriants for the function body. */
7172 FOR_EACH_BB_FN (bb, func)
7174 gimple_stmt_iterator gsi;
7176 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7177 gsi_next (&gsi))
7179 gimple phi = gsi_stmt (gsi);
7181 if (! virtual_operand_p (gimple_phi_result (phi)))
7182 find_func_aliases (func, phi);
7185 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7187 gimple stmt = gsi_stmt (gsi);
7189 find_func_aliases (func, stmt);
7190 find_func_clobbers (func, stmt);
7194 if (dump_file)
7196 fprintf (dump_file, "\n");
7197 dump_constraints (dump_file, from);
7198 fprintf (dump_file, "\n");
7200 from = constraints.length ();
7203 /* From the constraints compute the points-to sets. */
7204 solve_constraints ();
7206 /* Compute the global points-to sets for ESCAPED.
7207 ??? Note that the computed escape set is not correct
7208 for the whole unit as we fail to consider graph edges to
7209 externally visible functions. */
7210 ipa_escaped_pt = find_what_var_points_to (get_varinfo (escaped_id));
7212 /* Make sure the ESCAPED solution (which is used as placeholder in
7213 other solutions) does not reference itself. This simplifies
7214 points-to solution queries. */
7215 ipa_escaped_pt.ipa_escaped = 0;
7217 /* Assign the points-to sets to the SSA names in the unit. */
7218 FOR_EACH_DEFINED_FUNCTION (node)
7220 tree ptr;
7221 struct function *fn;
7222 unsigned i;
7223 basic_block bb;
7225 /* Nodes without a body are not interesting. */
7226 if (!node->has_gimple_body_p () || node->clone_of)
7227 continue;
7229 fn = DECL_STRUCT_FUNCTION (node->decl);
7231 /* Compute the points-to sets for pointer SSA_NAMEs. */
7232 FOR_EACH_VEC_ELT (*fn->gimple_df->ssa_names, i, ptr)
7234 if (ptr
7235 && POINTER_TYPE_P (TREE_TYPE (ptr)))
7236 find_what_p_points_to (ptr);
7239 /* Compute the call-use and call-clobber sets for indirect calls
7240 and calls to external functions. */
7241 FOR_EACH_BB_FN (bb, fn)
7243 gimple_stmt_iterator gsi;
7245 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7247 gimple stmt = gsi_stmt (gsi);
7248 struct pt_solution *pt;
7249 varinfo_t vi, fi;
7250 tree decl;
7252 if (!is_gimple_call (stmt))
7253 continue;
7255 /* Handle direct calls to functions with body. */
7256 decl = gimple_call_fndecl (stmt);
7257 if (decl
7258 && (fi = lookup_vi_for_tree (decl))
7259 && fi->is_fn_info)
7261 *gimple_call_clobber_set (stmt)
7262 = find_what_var_points_to
7263 (first_vi_for_offset (fi, fi_clobbers));
7264 *gimple_call_use_set (stmt)
7265 = find_what_var_points_to
7266 (first_vi_for_offset (fi, fi_uses));
7268 /* Handle direct calls to external functions. */
7269 else if (decl)
7271 pt = gimple_call_use_set (stmt);
7272 if (gimple_call_flags (stmt) & ECF_CONST)
7273 memset (pt, 0, sizeof (struct pt_solution));
7274 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7276 *pt = find_what_var_points_to (vi);
7277 /* Escaped (and thus nonlocal) variables are always
7278 implicitly used by calls. */
7279 /* ??? ESCAPED can be empty even though NONLOCAL
7280 always escaped. */
7281 pt->nonlocal = 1;
7282 pt->ipa_escaped = 1;
7284 else
7286 /* If there is nothing special about this call then
7287 we have made everything that is used also escape. */
7288 *pt = ipa_escaped_pt;
7289 pt->nonlocal = 1;
7292 pt = gimple_call_clobber_set (stmt);
7293 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7294 memset (pt, 0, sizeof (struct pt_solution));
7295 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7297 *pt = find_what_var_points_to (vi);
7298 /* Escaped (and thus nonlocal) variables are always
7299 implicitly clobbered by calls. */
7300 /* ??? ESCAPED can be empty even though NONLOCAL
7301 always escaped. */
7302 pt->nonlocal = 1;
7303 pt->ipa_escaped = 1;
7305 else
7307 /* If there is nothing special about this call then
7308 we have made everything that is used also escape. */
7309 *pt = ipa_escaped_pt;
7310 pt->nonlocal = 1;
7313 /* Handle indirect calls. */
7314 else if (!decl
7315 && (fi = get_fi_for_callee (stmt)))
7317 /* We need to accumulate all clobbers/uses of all possible
7318 callees. */
7319 fi = get_varinfo (find (fi->id));
7320 /* If we cannot constrain the set of functions we'll end up
7321 calling we end up using/clobbering everything. */
7322 if (bitmap_bit_p (fi->solution, anything_id)
7323 || bitmap_bit_p (fi->solution, nonlocal_id)
7324 || bitmap_bit_p (fi->solution, escaped_id))
7326 pt_solution_reset (gimple_call_clobber_set (stmt));
7327 pt_solution_reset (gimple_call_use_set (stmt));
7329 else
7331 bitmap_iterator bi;
7332 unsigned i;
7333 struct pt_solution *uses, *clobbers;
7335 uses = gimple_call_use_set (stmt);
7336 clobbers = gimple_call_clobber_set (stmt);
7337 memset (uses, 0, sizeof (struct pt_solution));
7338 memset (clobbers, 0, sizeof (struct pt_solution));
7339 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
7341 struct pt_solution sol;
7343 vi = get_varinfo (i);
7344 if (!vi->is_fn_info)
7346 /* ??? We could be more precise here? */
7347 uses->nonlocal = 1;
7348 uses->ipa_escaped = 1;
7349 clobbers->nonlocal = 1;
7350 clobbers->ipa_escaped = 1;
7351 continue;
7354 if (!uses->anything)
7356 sol = find_what_var_points_to
7357 (first_vi_for_offset (vi, fi_uses));
7358 pt_solution_ior_into (uses, &sol);
7360 if (!clobbers->anything)
7362 sol = find_what_var_points_to
7363 (first_vi_for_offset (vi, fi_clobbers));
7364 pt_solution_ior_into (clobbers, &sol);
7372 fn->gimple_df->ipa_pta = true;
7375 delete_points_to_sets ();
7377 in_ipa_mode = 0;
7379 return 0;
7382 namespace {
7384 const pass_data pass_data_ipa_pta =
7386 SIMPLE_IPA_PASS, /* type */
7387 "pta", /* name */
7388 OPTGROUP_NONE, /* optinfo_flags */
7389 TV_IPA_PTA, /* tv_id */
7390 0, /* properties_required */
7391 0, /* properties_provided */
7392 0, /* properties_destroyed */
7393 0, /* todo_flags_start */
7394 0, /* todo_flags_finish */
7397 class pass_ipa_pta : public simple_ipa_opt_pass
7399 public:
7400 pass_ipa_pta (gcc::context *ctxt)
7401 : simple_ipa_opt_pass (pass_data_ipa_pta, ctxt)
7404 /* opt_pass methods: */
7405 virtual bool gate (function *)
7407 return (optimize
7408 && flag_ipa_pta
7409 /* Don't bother doing anything if the program has errors. */
7410 && !seen_error ());
7413 virtual unsigned int execute (function *) { return ipa_pta_execute (); }
7415 }; // class pass_ipa_pta
7417 } // anon namespace
7419 simple_ipa_opt_pass *
7420 make_pass_ipa_pta (gcc::context *ctxt)
7422 return new pass_ipa_pta (ctxt);