Merge branch 'master' r217593-r217725 into gimple-classes-v2-option-3
[official-gcc.git] / gcc / tree-ssa-structalias.c
blobe065f1a1b4c81d421cc9b356eccb7b7bc7f7c2c8
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 "hash-map.h"
52 #include "plugin-api.h"
53 #include "ipa-ref.h"
54 #include "cgraph.h"
55 #include "stringpool.h"
56 #include "tree-ssanames.h"
57 #include "tree-into-ssa.h"
58 #include "expr.h"
59 #include "tree-dfa.h"
60 #include "tree-inline.h"
61 #include "diagnostic-core.h"
62 #include "tree-pass.h"
63 #include "alloc-pool.h"
64 #include "splay-tree.h"
65 #include "params.h"
66 #include "alias.h"
68 /* The idea behind this analyzer is to generate set constraints from the
69 program, then solve the resulting constraints in order to generate the
70 points-to sets.
72 Set constraints are a way of modeling program analysis problems that
73 involve sets. They consist of an inclusion constraint language,
74 describing the variables (each variable is a set) and operations that
75 are involved on the variables, and a set of rules that derive facts
76 from these operations. To solve a system of set constraints, you derive
77 all possible facts under the rules, which gives you the correct sets
78 as a consequence.
80 See "Efficient Field-sensitive pointer analysis for C" by "David
81 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
82 http://citeseer.ist.psu.edu/pearce04efficient.html
84 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
85 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
86 http://citeseer.ist.psu.edu/heintze01ultrafast.html
88 There are three types of real constraint expressions, DEREF,
89 ADDRESSOF, and SCALAR. Each constraint expression consists
90 of a constraint type, a variable, and an offset.
92 SCALAR is a constraint expression type used to represent x, whether
93 it appears on the LHS or the RHS of a statement.
94 DEREF is a constraint expression type used to represent *x, whether
95 it appears on the LHS or the RHS of a statement.
96 ADDRESSOF is a constraint expression used to represent &x, whether
97 it appears on the LHS or the RHS of a statement.
99 Each pointer variable in the program is assigned an integer id, and
100 each field of a structure variable is assigned an integer id as well.
102 Structure variables are linked to their list of fields through a "next
103 field" in each variable that points to the next field in offset
104 order.
105 Each variable for a structure field has
107 1. "size", that tells the size in bits of that field.
108 2. "fullsize, that tells the size in bits of the entire structure.
109 3. "offset", that tells the offset in bits from the beginning of the
110 structure to this field.
112 Thus,
113 struct f
115 int a;
116 int b;
117 } foo;
118 int *bar;
120 looks like
122 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
123 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
124 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
127 In order to solve the system of set constraints, the following is
128 done:
130 1. Each constraint variable x has a solution set associated with it,
131 Sol(x).
133 2. Constraints are separated into direct, copy, and complex.
134 Direct constraints are ADDRESSOF constraints that require no extra
135 processing, such as P = &Q
136 Copy constraints are those of the form P = Q.
137 Complex constraints are all the constraints involving dereferences
138 and offsets (including offsetted copies).
140 3. All direct constraints of the form P = &Q are processed, such
141 that Q is added to Sol(P)
143 4. All complex constraints for a given constraint variable are stored in a
144 linked list attached to that variable's node.
146 5. A directed graph is built out of the copy constraints. Each
147 constraint variable is a node in the graph, and an edge from
148 Q to P is added for each copy constraint of the form P = Q
150 6. The graph is then walked, and solution sets are
151 propagated along the copy edges, such that an edge from Q to P
152 causes Sol(P) <- Sol(P) union Sol(Q).
154 7. As we visit each node, all complex constraints associated with
155 that node are processed by adding appropriate copy edges to the graph, or the
156 appropriate variables to the solution set.
158 8. The process of walking the graph is iterated until no solution
159 sets change.
161 Prior to walking the graph in steps 6 and 7, We perform static
162 cycle elimination on the constraint graph, as well
163 as off-line variable substitution.
165 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
166 on and turned into anything), but isn't. You can just see what offset
167 inside the pointed-to struct it's going to access.
169 TODO: Constant bounded arrays can be handled as if they were structs of the
170 same number of elements.
172 TODO: Modeling heap and incoming pointers becomes much better if we
173 add fields to them as we discover them, which we could do.
175 TODO: We could handle unions, but to be honest, it's probably not
176 worth the pain or slowdown. */
178 /* IPA-PTA optimizations possible.
180 When the indirect function called is ANYTHING we can add disambiguation
181 based on the function signatures (or simply the parameter count which
182 is the varinfo size). We also do not need to consider functions that
183 do not have their address taken.
185 The is_global_var bit which marks escape points is overly conservative
186 in IPA mode. Split it to is_escape_point and is_global_var - only
187 externally visible globals are escape points in IPA mode. This is
188 also needed to fix the pt_solution_includes_global predicate
189 (and thus ptr_deref_may_alias_global_p).
191 The way we introduce DECL_PT_UID to avoid fixing up all points-to
192 sets in the translation unit when we copy a DECL during inlining
193 pessimizes precision. The advantage is that the DECL_PT_UID keeps
194 compile-time and memory usage overhead low - the points-to sets
195 do not grow or get unshared as they would during a fixup phase.
196 An alternative solution is to delay IPA PTA until after all
197 inlining transformations have been applied.
199 The way we propagate clobber/use information isn't optimized.
200 It should use a new complex constraint that properly filters
201 out local variables of the callee (though that would make
202 the sets invalid after inlining). OTOH we might as well
203 admit defeat to WHOPR and simply do all the clobber/use analysis
204 and propagation after PTA finished but before we threw away
205 points-to information for memory variables. WHOPR and PTA
206 do not play along well anyway - the whole constraint solving
207 would need to be done in WPA phase and it will be very interesting
208 to apply the results to local SSA names during LTRANS phase.
210 We probably should compute a per-function unit-ESCAPE solution
211 propagating it simply like the clobber / uses solutions. The
212 solution can go alongside the non-IPA espaced solution and be
213 used to query which vars escape the unit through a function.
215 We never put function decls in points-to sets so we do not
216 keep the set of called functions for indirect calls.
218 And probably more. */
220 static bool use_field_sensitive = true;
221 static int in_ipa_mode = 0;
223 /* Used for predecessor bitmaps. */
224 static bitmap_obstack predbitmap_obstack;
226 /* Used for points-to sets. */
227 static bitmap_obstack pta_obstack;
229 /* Used for oldsolution members of variables. */
230 static bitmap_obstack oldpta_obstack;
232 /* Used for per-solver-iteration bitmaps. */
233 static bitmap_obstack iteration_obstack;
235 static unsigned int create_variable_info_for (tree, const char *);
236 typedef struct constraint_graph *constraint_graph_t;
237 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
239 struct constraint;
240 typedef struct constraint *constraint_t;
243 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
244 if (a) \
245 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
247 static struct constraint_stats
249 unsigned int total_vars;
250 unsigned int nonpointer_vars;
251 unsigned int unified_vars_static;
252 unsigned int unified_vars_dynamic;
253 unsigned int iterations;
254 unsigned int num_edges;
255 unsigned int num_implicit_edges;
256 unsigned int points_to_sets_created;
257 } stats;
259 struct variable_info
261 /* ID of this variable */
262 unsigned int id;
264 /* True if this is a variable created by the constraint analysis, such as
265 heap variables and constraints we had to break up. */
266 unsigned int is_artificial_var : 1;
268 /* True if this is a special variable whose solution set should not be
269 changed. */
270 unsigned int is_special_var : 1;
272 /* True for variables whose size is not known or variable. */
273 unsigned int is_unknown_size_var : 1;
275 /* True for (sub-)fields that represent a whole variable. */
276 unsigned int is_full_var : 1;
278 /* True if this is a heap variable. */
279 unsigned int is_heap_var : 1;
281 /* True if this field may contain pointers. */
282 unsigned int may_have_pointers : 1;
284 /* True if this field has only restrict qualified pointers. */
285 unsigned int only_restrict_pointers : 1;
287 /* True if this represents a global variable. */
288 unsigned int is_global_var : 1;
290 /* True if this represents a IPA function info. */
291 unsigned int is_fn_info : 1;
293 /* The ID of the variable for the next field in this structure
294 or zero for the last field in this structure. */
295 unsigned next;
297 /* The ID of the variable for the first field in this structure. */
298 unsigned head;
300 /* Offset of this variable, in bits, from the base variable */
301 unsigned HOST_WIDE_INT offset;
303 /* Size of the variable, in bits. */
304 unsigned HOST_WIDE_INT size;
306 /* Full size of the base variable, in bits. */
307 unsigned HOST_WIDE_INT fullsize;
309 /* Name of this variable */
310 const char *name;
312 /* Tree that this variable is associated with. */
313 tree decl;
315 /* Points-to set for this variable. */
316 bitmap solution;
318 /* Old points-to set for this variable. */
319 bitmap oldsolution;
321 typedef struct variable_info *varinfo_t;
323 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
324 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
325 unsigned HOST_WIDE_INT);
326 static varinfo_t lookup_vi_for_tree (tree);
327 static inline bool type_can_have_subvars (const_tree);
329 /* Pool of variable info structures. */
330 static alloc_pool variable_info_pool;
332 /* Map varinfo to final pt_solution. */
333 static hash_map<varinfo_t, pt_solution *> *final_solutions;
334 struct obstack final_solutions_obstack;
336 /* Table of variable info structures for constraint variables.
337 Indexed directly by variable info id. */
338 static vec<varinfo_t> varmap;
340 /* Return the varmap element N */
342 static inline varinfo_t
343 get_varinfo (unsigned int n)
345 return varmap[n];
348 /* Return the next variable in the list of sub-variables of VI
349 or NULL if VI is the last sub-variable. */
351 static inline varinfo_t
352 vi_next (varinfo_t vi)
354 return get_varinfo (vi->next);
357 /* Static IDs for the special variables. Variable ID zero is unused
358 and used as terminator for the sub-variable chain. */
359 enum { nothing_id = 1, anything_id = 2, string_id = 3,
360 escaped_id = 4, nonlocal_id = 5,
361 storedanything_id = 6, integer_id = 7 };
363 /* Return a new variable info structure consisting for a variable
364 named NAME, and using constraint graph node NODE. Append it
365 to the vector of variable info structures. */
367 static varinfo_t
368 new_var_info (tree t, const char *name)
370 unsigned index = varmap.length ();
371 varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
373 ret->id = index;
374 ret->name = name;
375 ret->decl = t;
376 /* Vars without decl are artificial and do not have sub-variables. */
377 ret->is_artificial_var = (t == NULL_TREE);
378 ret->is_special_var = false;
379 ret->is_unknown_size_var = false;
380 ret->is_full_var = (t == NULL_TREE);
381 ret->is_heap_var = false;
382 ret->may_have_pointers = true;
383 ret->only_restrict_pointers = false;
384 ret->is_global_var = (t == NULL_TREE);
385 ret->is_fn_info = false;
386 if (t && DECL_P (t))
387 ret->is_global_var = (is_global_var (t)
388 /* We have to treat even local register variables
389 as escape points. */
390 || (TREE_CODE (t) == VAR_DECL
391 && DECL_HARD_REGISTER (t)));
392 ret->solution = BITMAP_ALLOC (&pta_obstack);
393 ret->oldsolution = NULL;
394 ret->next = 0;
395 ret->head = ret->id;
397 stats.total_vars++;
399 varmap.safe_push (ret);
401 return ret;
405 /* A map mapping call statements to per-stmt variables for uses
406 and clobbers specific to the call. */
407 static hash_map<gimple, varinfo_t> *call_stmt_vars;
409 /* Lookup or create the variable for the call statement CALL. */
411 static varinfo_t
412 get_call_vi (gcall *call)
414 varinfo_t vi, vi2;
416 bool existed;
417 varinfo_t *slot_p = &call_stmt_vars->get_or_insert (call, &existed);
418 if (existed)
419 return *slot_p;
421 vi = new_var_info (NULL_TREE, "CALLUSED");
422 vi->offset = 0;
423 vi->size = 1;
424 vi->fullsize = 2;
425 vi->is_full_var = true;
427 vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED");
428 vi2->offset = 1;
429 vi2->size = 1;
430 vi2->fullsize = 2;
431 vi2->is_full_var = true;
433 vi->next = vi2->id;
435 *slot_p = vi;
436 return vi;
439 /* Lookup the variable for the call statement CALL representing
440 the uses. Returns NULL if there is nothing special about this call. */
442 static varinfo_t
443 lookup_call_use_vi (gcall *call)
445 varinfo_t *slot_p = call_stmt_vars->get (call);
446 if (slot_p)
447 return *slot_p;
449 return NULL;
452 /* Lookup the variable for the call statement CALL representing
453 the clobbers. Returns NULL if there is nothing special about this call. */
455 static varinfo_t
456 lookup_call_clobber_vi (gcall *call)
458 varinfo_t uses = lookup_call_use_vi (call);
459 if (!uses)
460 return NULL;
462 return vi_next (uses);
465 /* Lookup or create the variable for the call statement CALL representing
466 the uses. */
468 static varinfo_t
469 get_call_use_vi (gcall *call)
471 return get_call_vi (call);
474 /* Lookup or create the variable for the call statement CALL representing
475 the clobbers. */
477 static varinfo_t ATTRIBUTE_UNUSED
478 get_call_clobber_vi (gcall *call)
480 return vi_next (get_call_vi (call));
484 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
486 /* An expression that appears in a constraint. */
488 struct constraint_expr
490 /* Constraint type. */
491 constraint_expr_type type;
493 /* Variable we are referring to in the constraint. */
494 unsigned int var;
496 /* Offset, in bits, of this constraint from the beginning of
497 variables it ends up referring to.
499 IOW, in a deref constraint, we would deref, get the result set,
500 then add OFFSET to each member. */
501 HOST_WIDE_INT offset;
504 /* Use 0x8000... as special unknown offset. */
505 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
507 typedef struct constraint_expr ce_s;
508 static void get_constraint_for_1 (tree, vec<ce_s> *, bool, bool);
509 static void get_constraint_for (tree, vec<ce_s> *);
510 static void get_constraint_for_rhs (tree, vec<ce_s> *);
511 static void do_deref (vec<ce_s> *);
513 /* Our set constraints are made up of two constraint expressions, one
514 LHS, and one RHS.
516 As described in the introduction, our set constraints each represent an
517 operation between set valued variables.
519 struct constraint
521 struct constraint_expr lhs;
522 struct constraint_expr rhs;
525 /* List of constraints that we use to build the constraint graph from. */
527 static vec<constraint_t> constraints;
528 static alloc_pool constraint_pool;
530 /* The constraint graph is represented as an array of bitmaps
531 containing successor nodes. */
533 struct constraint_graph
535 /* Size of this graph, which may be different than the number of
536 nodes in the variable map. */
537 unsigned int size;
539 /* Explicit successors of each node. */
540 bitmap *succs;
542 /* Implicit predecessors of each node (Used for variable
543 substitution). */
544 bitmap *implicit_preds;
546 /* Explicit predecessors of each node (Used for variable substitution). */
547 bitmap *preds;
549 /* Indirect cycle representatives, or -1 if the node has no indirect
550 cycles. */
551 int *indirect_cycles;
553 /* Representative node for a node. rep[a] == a unless the node has
554 been unified. */
555 unsigned int *rep;
557 /* Equivalence class representative for a label. This is used for
558 variable substitution. */
559 int *eq_rep;
561 /* Pointer equivalence label for a node. All nodes with the same
562 pointer equivalence label can be unified together at some point
563 (either during constraint optimization or after the constraint
564 graph is built). */
565 unsigned int *pe;
567 /* Pointer equivalence representative for a label. This is used to
568 handle nodes that are pointer equivalent but not location
569 equivalent. We can unite these once the addressof constraints
570 are transformed into initial points-to sets. */
571 int *pe_rep;
573 /* Pointer equivalence label for each node, used during variable
574 substitution. */
575 unsigned int *pointer_label;
577 /* Location equivalence label for each node, used during location
578 equivalence finding. */
579 unsigned int *loc_label;
581 /* Pointed-by set for each node, used during location equivalence
582 finding. This is pointed-by rather than pointed-to, because it
583 is constructed using the predecessor graph. */
584 bitmap *pointed_by;
586 /* Points to sets for pointer equivalence. This is *not* the actual
587 points-to sets for nodes. */
588 bitmap *points_to;
590 /* Bitmap of nodes where the bit is set if the node is a direct
591 node. Used for variable substitution. */
592 sbitmap direct_nodes;
594 /* Bitmap of nodes where the bit is set if the node is address
595 taken. Used for variable substitution. */
596 bitmap address_taken;
598 /* Vector of complex constraints for each graph node. Complex
599 constraints are those involving dereferences or offsets that are
600 not 0. */
601 vec<constraint_t> *complex;
604 static constraint_graph_t graph;
606 /* During variable substitution and the offline version of indirect
607 cycle finding, we create nodes to represent dereferences and
608 address taken constraints. These represent where these start and
609 end. */
610 #define FIRST_REF_NODE (varmap).length ()
611 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
613 /* Return the representative node for NODE, if NODE has been unioned
614 with another NODE.
615 This function performs path compression along the way to finding
616 the representative. */
618 static unsigned int
619 find (unsigned int node)
621 gcc_checking_assert (node < graph->size);
622 if (graph->rep[node] != node)
623 return graph->rep[node] = find (graph->rep[node]);
624 return node;
627 /* Union the TO and FROM nodes to the TO nodes.
628 Note that at some point in the future, we may want to do
629 union-by-rank, in which case we are going to have to return the
630 node we unified to. */
632 static bool
633 unite (unsigned int to, unsigned int from)
635 gcc_checking_assert (to < graph->size && from < graph->size);
636 if (to != from && graph->rep[from] != to)
638 graph->rep[from] = to;
639 return true;
641 return false;
644 /* Create a new constraint consisting of LHS and RHS expressions. */
646 static constraint_t
647 new_constraint (const struct constraint_expr lhs,
648 const struct constraint_expr rhs)
650 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
651 ret->lhs = lhs;
652 ret->rhs = rhs;
653 return ret;
656 /* Print out constraint C to FILE. */
658 static void
659 dump_constraint (FILE *file, constraint_t c)
661 if (c->lhs.type == ADDRESSOF)
662 fprintf (file, "&");
663 else if (c->lhs.type == DEREF)
664 fprintf (file, "*");
665 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
666 if (c->lhs.offset == UNKNOWN_OFFSET)
667 fprintf (file, " + UNKNOWN");
668 else if (c->lhs.offset != 0)
669 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
670 fprintf (file, " = ");
671 if (c->rhs.type == ADDRESSOF)
672 fprintf (file, "&");
673 else if (c->rhs.type == DEREF)
674 fprintf (file, "*");
675 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
676 if (c->rhs.offset == UNKNOWN_OFFSET)
677 fprintf (file, " + UNKNOWN");
678 else if (c->rhs.offset != 0)
679 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
683 void debug_constraint (constraint_t);
684 void debug_constraints (void);
685 void debug_constraint_graph (void);
686 void debug_solution_for_var (unsigned int);
687 void debug_sa_points_to_info (void);
689 /* Print out constraint C to stderr. */
691 DEBUG_FUNCTION void
692 debug_constraint (constraint_t c)
694 dump_constraint (stderr, c);
695 fprintf (stderr, "\n");
698 /* Print out all constraints to FILE */
700 static void
701 dump_constraints (FILE *file, int from)
703 int i;
704 constraint_t c;
705 for (i = from; constraints.iterate (i, &c); i++)
706 if (c)
708 dump_constraint (file, c);
709 fprintf (file, "\n");
713 /* Print out all constraints to stderr. */
715 DEBUG_FUNCTION void
716 debug_constraints (void)
718 dump_constraints (stderr, 0);
721 /* Print the constraint graph in dot format. */
723 static void
724 dump_constraint_graph (FILE *file)
726 unsigned int i;
728 /* Only print the graph if it has already been initialized: */
729 if (!graph)
730 return;
732 /* Prints the header of the dot file: */
733 fprintf (file, "strict digraph {\n");
734 fprintf (file, " node [\n shape = box\n ]\n");
735 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
736 fprintf (file, "\n // List of nodes and complex constraints in "
737 "the constraint graph:\n");
739 /* The next lines print the nodes in the graph together with the
740 complex constraints attached to them. */
741 for (i = 1; i < graph->size; i++)
743 if (i == FIRST_REF_NODE)
744 continue;
745 if (find (i) != i)
746 continue;
747 if (i < FIRST_REF_NODE)
748 fprintf (file, "\"%s\"", get_varinfo (i)->name);
749 else
750 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
751 if (graph->complex[i].exists ())
753 unsigned j;
754 constraint_t c;
755 fprintf (file, " [label=\"\\N\\n");
756 for (j = 0; graph->complex[i].iterate (j, &c); ++j)
758 dump_constraint (file, c);
759 fprintf (file, "\\l");
761 fprintf (file, "\"]");
763 fprintf (file, ";\n");
766 /* Go over the edges. */
767 fprintf (file, "\n // Edges in the constraint graph:\n");
768 for (i = 1; i < graph->size; i++)
770 unsigned j;
771 bitmap_iterator bi;
772 if (find (i) != i)
773 continue;
774 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
776 unsigned to = find (j);
777 if (i == to)
778 continue;
779 if (i < FIRST_REF_NODE)
780 fprintf (file, "\"%s\"", get_varinfo (i)->name);
781 else
782 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
783 fprintf (file, " -> ");
784 if (to < FIRST_REF_NODE)
785 fprintf (file, "\"%s\"", get_varinfo (to)->name);
786 else
787 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name);
788 fprintf (file, ";\n");
792 /* Prints the tail of the dot file. */
793 fprintf (file, "}\n");
796 /* Print out the constraint graph to stderr. */
798 DEBUG_FUNCTION void
799 debug_constraint_graph (void)
801 dump_constraint_graph (stderr);
804 /* SOLVER FUNCTIONS
806 The solver is a simple worklist solver, that works on the following
807 algorithm:
809 sbitmap changed_nodes = all zeroes;
810 changed_count = 0;
811 For each node that is not already collapsed:
812 changed_count++;
813 set bit in changed nodes
815 while (changed_count > 0)
817 compute topological ordering for constraint graph
819 find and collapse cycles in the constraint graph (updating
820 changed if necessary)
822 for each node (n) in the graph in topological order:
823 changed_count--;
825 Process each complex constraint associated with the node,
826 updating changed if necessary.
828 For each outgoing edge from n, propagate the solution from n to
829 the destination of the edge, updating changed as necessary.
831 } */
833 /* Return true if two constraint expressions A and B are equal. */
835 static bool
836 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
838 return a.type == b.type && a.var == b.var && a.offset == b.offset;
841 /* Return true if constraint expression A is less than constraint expression
842 B. This is just arbitrary, but consistent, in order to give them an
843 ordering. */
845 static bool
846 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
848 if (a.type == b.type)
850 if (a.var == b.var)
851 return a.offset < b.offset;
852 else
853 return a.var < b.var;
855 else
856 return a.type < b.type;
859 /* Return true if constraint A is less than constraint B. This is just
860 arbitrary, but consistent, in order to give them an ordering. */
862 static bool
863 constraint_less (const constraint_t &a, const constraint_t &b)
865 if (constraint_expr_less (a->lhs, b->lhs))
866 return true;
867 else if (constraint_expr_less (b->lhs, a->lhs))
868 return false;
869 else
870 return constraint_expr_less (a->rhs, b->rhs);
873 /* Return true if two constraints A and B are equal. */
875 static bool
876 constraint_equal (struct constraint a, struct constraint b)
878 return constraint_expr_equal (a.lhs, b.lhs)
879 && constraint_expr_equal (a.rhs, b.rhs);
883 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
885 static constraint_t
886 constraint_vec_find (vec<constraint_t> vec,
887 struct constraint lookfor)
889 unsigned int place;
890 constraint_t found;
892 if (!vec.exists ())
893 return NULL;
895 place = vec.lower_bound (&lookfor, constraint_less);
896 if (place >= vec.length ())
897 return NULL;
898 found = vec[place];
899 if (!constraint_equal (*found, lookfor))
900 return NULL;
901 return found;
904 /* Union two constraint vectors, TO and FROM. Put the result in TO.
905 Returns true of TO set is changed. */
907 static bool
908 constraint_set_union (vec<constraint_t> *to,
909 vec<constraint_t> *from)
911 int i;
912 constraint_t c;
913 bool any_change = false;
915 FOR_EACH_VEC_ELT (*from, i, c)
917 if (constraint_vec_find (*to, *c) == NULL)
919 unsigned int place = to->lower_bound (c, constraint_less);
920 to->safe_insert (place, c);
921 any_change = true;
924 return any_change;
927 /* Expands the solution in SET to all sub-fields of variables included. */
929 static bitmap
930 solution_set_expand (bitmap set, bitmap *expanded)
932 bitmap_iterator bi;
933 unsigned j;
935 if (*expanded)
936 return *expanded;
938 *expanded = BITMAP_ALLOC (&iteration_obstack);
940 /* In a first pass expand to the head of the variables we need to
941 add all sub-fields off. This avoids quadratic behavior. */
942 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
944 varinfo_t v = get_varinfo (j);
945 if (v->is_artificial_var
946 || v->is_full_var)
947 continue;
948 bitmap_set_bit (*expanded, v->head);
951 /* In the second pass now expand all head variables with subfields. */
952 EXECUTE_IF_SET_IN_BITMAP (*expanded, 0, j, bi)
954 varinfo_t v = get_varinfo (j);
955 if (v->head != j)
956 continue;
957 for (v = vi_next (v); v != NULL; v = vi_next (v))
958 bitmap_set_bit (*expanded, v->id);
961 /* And finally set the rest of the bits from SET. */
962 bitmap_ior_into (*expanded, set);
964 return *expanded;
967 /* Union solution sets TO and DELTA, and add INC to each member of DELTA in the
968 process. */
970 static bool
971 set_union_with_increment (bitmap to, bitmap delta, HOST_WIDE_INT inc,
972 bitmap *expanded_delta)
974 bool changed = false;
975 bitmap_iterator bi;
976 unsigned int i;
978 /* If the solution of DELTA contains anything it is good enough to transfer
979 this to TO. */
980 if (bitmap_bit_p (delta, anything_id))
981 return bitmap_set_bit (to, anything_id);
983 /* If the offset is unknown we have to expand the solution to
984 all subfields. */
985 if (inc == UNKNOWN_OFFSET)
987 delta = solution_set_expand (delta, expanded_delta);
988 changed |= bitmap_ior_into (to, delta);
989 return changed;
992 /* For non-zero offset union the offsetted solution into the destination. */
993 EXECUTE_IF_SET_IN_BITMAP (delta, 0, i, bi)
995 varinfo_t vi = get_varinfo (i);
997 /* If this is a variable with just one field just set its bit
998 in the result. */
999 if (vi->is_artificial_var
1000 || vi->is_unknown_size_var
1001 || vi->is_full_var)
1002 changed |= bitmap_set_bit (to, i);
1003 else
1005 HOST_WIDE_INT fieldoffset = vi->offset + inc;
1006 unsigned HOST_WIDE_INT size = vi->size;
1008 /* If the offset makes the pointer point to before the
1009 variable use offset zero for the field lookup. */
1010 if (fieldoffset < 0)
1011 vi = get_varinfo (vi->head);
1012 else
1013 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
1017 changed |= bitmap_set_bit (to, vi->id);
1018 if (vi->is_full_var
1019 || vi->next == 0)
1020 break;
1022 /* We have to include all fields that overlap the current field
1023 shifted by inc. */
1024 vi = vi_next (vi);
1026 while (vi->offset < fieldoffset + size);
1030 return changed;
1033 /* Insert constraint C into the list of complex constraints for graph
1034 node VAR. */
1036 static void
1037 insert_into_complex (constraint_graph_t graph,
1038 unsigned int var, constraint_t c)
1040 vec<constraint_t> complex = graph->complex[var];
1041 unsigned int place = complex.lower_bound (c, constraint_less);
1043 /* Only insert constraints that do not already exist. */
1044 if (place >= complex.length ()
1045 || !constraint_equal (*c, *complex[place]))
1046 graph->complex[var].safe_insert (place, c);
1050 /* Condense two variable nodes into a single variable node, by moving
1051 all associated info from FROM to TO. Returns true if TO node's
1052 constraint set changes after the merge. */
1054 static bool
1055 merge_node_constraints (constraint_graph_t graph, unsigned int to,
1056 unsigned int from)
1058 unsigned int i;
1059 constraint_t c;
1060 bool any_change = false;
1062 gcc_checking_assert (find (from) == to);
1064 /* Move all complex constraints from src node into to node */
1065 FOR_EACH_VEC_ELT (graph->complex[from], i, c)
1067 /* In complex constraints for node FROM, we may have either
1068 a = *FROM, and *FROM = a, or an offseted constraint which are
1069 always added to the rhs node's constraints. */
1071 if (c->rhs.type == DEREF)
1072 c->rhs.var = to;
1073 else if (c->lhs.type == DEREF)
1074 c->lhs.var = to;
1075 else
1076 c->rhs.var = to;
1079 any_change = constraint_set_union (&graph->complex[to],
1080 &graph->complex[from]);
1081 graph->complex[from].release ();
1082 return any_change;
1086 /* Remove edges involving NODE from GRAPH. */
1088 static void
1089 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
1091 if (graph->succs[node])
1092 BITMAP_FREE (graph->succs[node]);
1095 /* Merge GRAPH nodes FROM and TO into node TO. */
1097 static void
1098 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1099 unsigned int from)
1101 if (graph->indirect_cycles[from] != -1)
1103 /* If we have indirect cycles with the from node, and we have
1104 none on the to node, the to node has indirect cycles from the
1105 from node now that they are unified.
1106 If indirect cycles exist on both, unify the nodes that they
1107 are in a cycle with, since we know they are in a cycle with
1108 each other. */
1109 if (graph->indirect_cycles[to] == -1)
1110 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1113 /* Merge all the successor edges. */
1114 if (graph->succs[from])
1116 if (!graph->succs[to])
1117 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1118 bitmap_ior_into (graph->succs[to],
1119 graph->succs[from]);
1122 clear_edges_for_node (graph, from);
1126 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1127 it doesn't exist in the graph already. */
1129 static void
1130 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1131 unsigned int from)
1133 if (to == from)
1134 return;
1136 if (!graph->implicit_preds[to])
1137 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1139 if (bitmap_set_bit (graph->implicit_preds[to], from))
1140 stats.num_implicit_edges++;
1143 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1144 it doesn't exist in the graph already.
1145 Return false if the edge already existed, true otherwise. */
1147 static void
1148 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1149 unsigned int from)
1151 if (!graph->preds[to])
1152 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1153 bitmap_set_bit (graph->preds[to], from);
1156 /* Add a graph edge to GRAPH, going from FROM to TO if
1157 it doesn't exist in the graph already.
1158 Return false if the edge already existed, true otherwise. */
1160 static bool
1161 add_graph_edge (constraint_graph_t graph, unsigned int to,
1162 unsigned int from)
1164 if (to == from)
1166 return false;
1168 else
1170 bool r = false;
1172 if (!graph->succs[from])
1173 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1174 if (bitmap_set_bit (graph->succs[from], to))
1176 r = true;
1177 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1178 stats.num_edges++;
1180 return r;
1185 /* Initialize the constraint graph structure to contain SIZE nodes. */
1187 static void
1188 init_graph (unsigned int size)
1190 unsigned int j;
1192 graph = XCNEW (struct constraint_graph);
1193 graph->size = size;
1194 graph->succs = XCNEWVEC (bitmap, graph->size);
1195 graph->indirect_cycles = XNEWVEC (int, graph->size);
1196 graph->rep = XNEWVEC (unsigned int, graph->size);
1197 /* ??? Macros do not support template types with multiple arguments,
1198 so we use a typedef to work around it. */
1199 typedef vec<constraint_t> vec_constraint_t_heap;
1200 graph->complex = XCNEWVEC (vec_constraint_t_heap, size);
1201 graph->pe = XCNEWVEC (unsigned int, graph->size);
1202 graph->pe_rep = XNEWVEC (int, graph->size);
1204 for (j = 0; j < graph->size; j++)
1206 graph->rep[j] = j;
1207 graph->pe_rep[j] = -1;
1208 graph->indirect_cycles[j] = -1;
1212 /* Build the constraint graph, adding only predecessor edges right now. */
1214 static void
1215 build_pred_graph (void)
1217 int i;
1218 constraint_t c;
1219 unsigned int j;
1221 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1222 graph->preds = XCNEWVEC (bitmap, graph->size);
1223 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1224 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1225 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1226 graph->points_to = XCNEWVEC (bitmap, graph->size);
1227 graph->eq_rep = XNEWVEC (int, graph->size);
1228 graph->direct_nodes = sbitmap_alloc (graph->size);
1229 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1230 bitmap_clear (graph->direct_nodes);
1232 for (j = 1; j < FIRST_REF_NODE; j++)
1234 if (!get_varinfo (j)->is_special_var)
1235 bitmap_set_bit (graph->direct_nodes, j);
1238 for (j = 0; j < graph->size; j++)
1239 graph->eq_rep[j] = -1;
1241 for (j = 0; j < varmap.length (); j++)
1242 graph->indirect_cycles[j] = -1;
1244 FOR_EACH_VEC_ELT (constraints, i, c)
1246 struct constraint_expr lhs = c->lhs;
1247 struct constraint_expr rhs = c->rhs;
1248 unsigned int lhsvar = lhs.var;
1249 unsigned int rhsvar = rhs.var;
1251 if (lhs.type == DEREF)
1253 /* *x = y. */
1254 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1255 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1257 else if (rhs.type == DEREF)
1259 /* x = *y */
1260 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1261 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1262 else
1263 bitmap_clear_bit (graph->direct_nodes, lhsvar);
1265 else if (rhs.type == ADDRESSOF)
1267 varinfo_t v;
1269 /* x = &y */
1270 if (graph->points_to[lhsvar] == NULL)
1271 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1272 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1274 if (graph->pointed_by[rhsvar] == NULL)
1275 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1276 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1278 /* Implicitly, *x = y */
1279 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1281 /* All related variables are no longer direct nodes. */
1282 bitmap_clear_bit (graph->direct_nodes, rhsvar);
1283 v = get_varinfo (rhsvar);
1284 if (!v->is_full_var)
1286 v = get_varinfo (v->head);
1289 bitmap_clear_bit (graph->direct_nodes, v->id);
1290 v = vi_next (v);
1292 while (v != NULL);
1294 bitmap_set_bit (graph->address_taken, rhsvar);
1296 else if (lhsvar > anything_id
1297 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1299 /* x = y */
1300 add_pred_graph_edge (graph, lhsvar, rhsvar);
1301 /* Implicitly, *x = *y */
1302 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1303 FIRST_REF_NODE + rhsvar);
1305 else if (lhs.offset != 0 || rhs.offset != 0)
1307 if (rhs.offset != 0)
1308 bitmap_clear_bit (graph->direct_nodes, lhs.var);
1309 else if (lhs.offset != 0)
1310 bitmap_clear_bit (graph->direct_nodes, rhs.var);
1315 /* Build the constraint graph, adding successor edges. */
1317 static void
1318 build_succ_graph (void)
1320 unsigned i, t;
1321 constraint_t c;
1323 FOR_EACH_VEC_ELT (constraints, i, c)
1325 struct constraint_expr lhs;
1326 struct constraint_expr rhs;
1327 unsigned int lhsvar;
1328 unsigned int rhsvar;
1330 if (!c)
1331 continue;
1333 lhs = c->lhs;
1334 rhs = c->rhs;
1335 lhsvar = find (lhs.var);
1336 rhsvar = find (rhs.var);
1338 if (lhs.type == DEREF)
1340 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1341 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1343 else if (rhs.type == DEREF)
1345 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1346 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1348 else if (rhs.type == ADDRESSOF)
1350 /* x = &y */
1351 gcc_checking_assert (find (rhs.var) == rhs.var);
1352 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1354 else if (lhsvar > anything_id
1355 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1357 add_graph_edge (graph, lhsvar, rhsvar);
1361 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1362 receive pointers. */
1363 t = find (storedanything_id);
1364 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1366 if (!bitmap_bit_p (graph->direct_nodes, i)
1367 && get_varinfo (i)->may_have_pointers)
1368 add_graph_edge (graph, find (i), t);
1371 /* Everything stored to ANYTHING also potentially escapes. */
1372 add_graph_edge (graph, find (escaped_id), t);
1376 /* Changed variables on the last iteration. */
1377 static bitmap changed;
1379 /* Strongly Connected Component visitation info. */
1381 struct scc_info
1383 sbitmap visited;
1384 sbitmap deleted;
1385 unsigned int *dfs;
1386 unsigned int *node_mapping;
1387 int current_index;
1388 vec<unsigned> scc_stack;
1392 /* Recursive routine to find strongly connected components in GRAPH.
1393 SI is the SCC info to store the information in, and N is the id of current
1394 graph node we are processing.
1396 This is Tarjan's strongly connected component finding algorithm, as
1397 modified by Nuutila to keep only non-root nodes on the stack.
1398 The algorithm can be found in "On finding the strongly connected
1399 connected components in a directed graph" by Esko Nuutila and Eljas
1400 Soisalon-Soininen, in Information Processing Letters volume 49,
1401 number 1, pages 9-14. */
1403 static void
1404 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1406 unsigned int i;
1407 bitmap_iterator bi;
1408 unsigned int my_dfs;
1410 bitmap_set_bit (si->visited, n);
1411 si->dfs[n] = si->current_index ++;
1412 my_dfs = si->dfs[n];
1414 /* Visit all the successors. */
1415 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1417 unsigned int w;
1419 if (i > LAST_REF_NODE)
1420 break;
1422 w = find (i);
1423 if (bitmap_bit_p (si->deleted, w))
1424 continue;
1426 if (!bitmap_bit_p (si->visited, w))
1427 scc_visit (graph, si, w);
1429 unsigned int t = find (w);
1430 gcc_checking_assert (find (n) == n);
1431 if (si->dfs[t] < si->dfs[n])
1432 si->dfs[n] = si->dfs[t];
1435 /* See if any components have been identified. */
1436 if (si->dfs[n] == my_dfs)
1438 if (si->scc_stack.length () > 0
1439 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1441 bitmap scc = BITMAP_ALLOC (NULL);
1442 unsigned int lowest_node;
1443 bitmap_iterator bi;
1445 bitmap_set_bit (scc, n);
1447 while (si->scc_stack.length () != 0
1448 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1450 unsigned int w = si->scc_stack.pop ();
1452 bitmap_set_bit (scc, w);
1455 lowest_node = bitmap_first_set_bit (scc);
1456 gcc_assert (lowest_node < FIRST_REF_NODE);
1458 /* Collapse the SCC nodes into a single node, and mark the
1459 indirect cycles. */
1460 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1462 if (i < FIRST_REF_NODE)
1464 if (unite (lowest_node, i))
1465 unify_nodes (graph, lowest_node, i, false);
1467 else
1469 unite (lowest_node, i);
1470 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1474 bitmap_set_bit (si->deleted, n);
1476 else
1477 si->scc_stack.safe_push (n);
1480 /* Unify node FROM into node TO, updating the changed count if
1481 necessary when UPDATE_CHANGED is true. */
1483 static void
1484 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1485 bool update_changed)
1487 gcc_checking_assert (to != from && find (to) == to);
1489 if (dump_file && (dump_flags & TDF_DETAILS))
1490 fprintf (dump_file, "Unifying %s to %s\n",
1491 get_varinfo (from)->name,
1492 get_varinfo (to)->name);
1494 if (update_changed)
1495 stats.unified_vars_dynamic++;
1496 else
1497 stats.unified_vars_static++;
1499 merge_graph_nodes (graph, to, from);
1500 if (merge_node_constraints (graph, to, from))
1502 if (update_changed)
1503 bitmap_set_bit (changed, to);
1506 /* Mark TO as changed if FROM was changed. If TO was already marked
1507 as changed, decrease the changed count. */
1509 if (update_changed
1510 && bitmap_clear_bit (changed, from))
1511 bitmap_set_bit (changed, to);
1512 varinfo_t fromvi = get_varinfo (from);
1513 if (fromvi->solution)
1515 /* If the solution changes because of the merging, we need to mark
1516 the variable as changed. */
1517 varinfo_t tovi = get_varinfo (to);
1518 if (bitmap_ior_into (tovi->solution, fromvi->solution))
1520 if (update_changed)
1521 bitmap_set_bit (changed, to);
1524 BITMAP_FREE (fromvi->solution);
1525 if (fromvi->oldsolution)
1526 BITMAP_FREE (fromvi->oldsolution);
1528 if (stats.iterations > 0
1529 && tovi->oldsolution)
1530 BITMAP_FREE (tovi->oldsolution);
1532 if (graph->succs[to])
1533 bitmap_clear_bit (graph->succs[to], to);
1536 /* Information needed to compute the topological ordering of a graph. */
1538 struct topo_info
1540 /* sbitmap of visited nodes. */
1541 sbitmap visited;
1542 /* Array that stores the topological order of the graph, *in
1543 reverse*. */
1544 vec<unsigned> topo_order;
1548 /* Initialize and return a topological info structure. */
1550 static struct topo_info *
1551 init_topo_info (void)
1553 size_t size = graph->size;
1554 struct topo_info *ti = XNEW (struct topo_info);
1555 ti->visited = sbitmap_alloc (size);
1556 bitmap_clear (ti->visited);
1557 ti->topo_order.create (1);
1558 return ti;
1562 /* Free the topological sort info pointed to by TI. */
1564 static void
1565 free_topo_info (struct topo_info *ti)
1567 sbitmap_free (ti->visited);
1568 ti->topo_order.release ();
1569 free (ti);
1572 /* Visit the graph in topological order, and store the order in the
1573 topo_info structure. */
1575 static void
1576 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1577 unsigned int n)
1579 bitmap_iterator bi;
1580 unsigned int j;
1582 bitmap_set_bit (ti->visited, n);
1584 if (graph->succs[n])
1585 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1587 if (!bitmap_bit_p (ti->visited, j))
1588 topo_visit (graph, ti, j);
1591 ti->topo_order.safe_push (n);
1594 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1595 starting solution for y. */
1597 static void
1598 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1599 bitmap delta, bitmap *expanded_delta)
1601 unsigned int lhs = c->lhs.var;
1602 bool flag = false;
1603 bitmap sol = get_varinfo (lhs)->solution;
1604 unsigned int j;
1605 bitmap_iterator bi;
1606 HOST_WIDE_INT roffset = c->rhs.offset;
1608 /* Our IL does not allow this. */
1609 gcc_checking_assert (c->lhs.offset == 0);
1611 /* If the solution of Y contains anything it is good enough to transfer
1612 this to the LHS. */
1613 if (bitmap_bit_p (delta, anything_id))
1615 flag |= bitmap_set_bit (sol, anything_id);
1616 goto done;
1619 /* If we do not know at with offset the rhs is dereferenced compute
1620 the reachability set of DELTA, conservatively assuming it is
1621 dereferenced at all valid offsets. */
1622 if (roffset == UNKNOWN_OFFSET)
1624 delta = solution_set_expand (delta, expanded_delta);
1625 /* No further offset processing is necessary. */
1626 roffset = 0;
1629 /* For each variable j in delta (Sol(y)), add
1630 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1631 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1633 varinfo_t v = get_varinfo (j);
1634 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1635 unsigned HOST_WIDE_INT size = v->size;
1636 unsigned int t;
1638 if (v->is_full_var)
1640 else if (roffset != 0)
1642 if (fieldoffset < 0)
1643 v = get_varinfo (v->head);
1644 else
1645 v = first_or_preceding_vi_for_offset (v, fieldoffset);
1648 /* We have to include all fields that overlap the current field
1649 shifted by roffset. */
1652 t = find (v->id);
1654 /* Adding edges from the special vars is pointless.
1655 They don't have sets that can change. */
1656 if (get_varinfo (t)->is_special_var)
1657 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1658 /* Merging the solution from ESCAPED needlessly increases
1659 the set. Use ESCAPED as representative instead. */
1660 else if (v->id == escaped_id)
1661 flag |= bitmap_set_bit (sol, escaped_id);
1662 else if (v->may_have_pointers
1663 && add_graph_edge (graph, lhs, t))
1664 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1666 if (v->is_full_var
1667 || v->next == 0)
1668 break;
1670 v = vi_next (v);
1672 while (v->offset < fieldoffset + size);
1675 done:
1676 /* If the LHS solution changed, mark the var as changed. */
1677 if (flag)
1679 get_varinfo (lhs)->solution = sol;
1680 bitmap_set_bit (changed, lhs);
1684 /* Process a constraint C that represents *(x + off) = y using DELTA
1685 as the starting solution for x. */
1687 static void
1688 do_ds_constraint (constraint_t c, bitmap delta, bitmap *expanded_delta)
1690 unsigned int rhs = c->rhs.var;
1691 bitmap sol = get_varinfo (rhs)->solution;
1692 unsigned int j;
1693 bitmap_iterator bi;
1694 HOST_WIDE_INT loff = c->lhs.offset;
1695 bool escaped_p = false;
1697 /* Our IL does not allow this. */
1698 gcc_checking_assert (c->rhs.offset == 0);
1700 /* If the solution of y contains ANYTHING simply use the ANYTHING
1701 solution. This avoids needlessly increasing the points-to sets. */
1702 if (bitmap_bit_p (sol, anything_id))
1703 sol = get_varinfo (find (anything_id))->solution;
1705 /* If the solution for x contains ANYTHING we have to merge the
1706 solution of y into all pointer variables which we do via
1707 STOREDANYTHING. */
1708 if (bitmap_bit_p (delta, anything_id))
1710 unsigned t = find (storedanything_id);
1711 if (add_graph_edge (graph, t, rhs))
1713 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1714 bitmap_set_bit (changed, t);
1716 return;
1719 /* If we do not know at with offset the rhs is dereferenced compute
1720 the reachability set of DELTA, conservatively assuming it is
1721 dereferenced at all valid offsets. */
1722 if (loff == UNKNOWN_OFFSET)
1724 delta = solution_set_expand (delta, expanded_delta);
1725 loff = 0;
1728 /* For each member j of delta (Sol(x)), add an edge from y to j and
1729 union Sol(y) into Sol(j) */
1730 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1732 varinfo_t v = get_varinfo (j);
1733 unsigned int t;
1734 HOST_WIDE_INT fieldoffset = v->offset + loff;
1735 unsigned HOST_WIDE_INT size = v->size;
1737 if (v->is_full_var)
1739 else if (loff != 0)
1741 if (fieldoffset < 0)
1742 v = get_varinfo (v->head);
1743 else
1744 v = first_or_preceding_vi_for_offset (v, fieldoffset);
1747 /* We have to include all fields that overlap the current field
1748 shifted by loff. */
1751 if (v->may_have_pointers)
1753 /* If v is a global variable then this is an escape point. */
1754 if (v->is_global_var
1755 && !escaped_p)
1757 t = find (escaped_id);
1758 if (add_graph_edge (graph, t, rhs)
1759 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1760 bitmap_set_bit (changed, t);
1761 /* Enough to let rhs escape once. */
1762 escaped_p = true;
1765 if (v->is_special_var)
1766 break;
1768 t = find (v->id);
1769 if (add_graph_edge (graph, t, rhs)
1770 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1771 bitmap_set_bit (changed, t);
1774 if (v->is_full_var
1775 || v->next == 0)
1776 break;
1778 v = vi_next (v);
1780 while (v->offset < fieldoffset + size);
1784 /* Handle a non-simple (simple meaning requires no iteration),
1785 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1787 static void
1788 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta,
1789 bitmap *expanded_delta)
1791 if (c->lhs.type == DEREF)
1793 if (c->rhs.type == ADDRESSOF)
1795 gcc_unreachable ();
1797 else
1799 /* *x = y */
1800 do_ds_constraint (c, delta, expanded_delta);
1803 else if (c->rhs.type == DEREF)
1805 /* x = *y */
1806 if (!(get_varinfo (c->lhs.var)->is_special_var))
1807 do_sd_constraint (graph, c, delta, expanded_delta);
1809 else
1811 bitmap tmp;
1812 bool flag = false;
1814 gcc_checking_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR
1815 && c->rhs.offset != 0 && c->lhs.offset == 0);
1816 tmp = get_varinfo (c->lhs.var)->solution;
1818 flag = set_union_with_increment (tmp, delta, c->rhs.offset,
1819 expanded_delta);
1821 if (flag)
1822 bitmap_set_bit (changed, c->lhs.var);
1826 /* Initialize and return a new SCC info structure. */
1828 static struct scc_info *
1829 init_scc_info (size_t size)
1831 struct scc_info *si = XNEW (struct scc_info);
1832 size_t i;
1834 si->current_index = 0;
1835 si->visited = sbitmap_alloc (size);
1836 bitmap_clear (si->visited);
1837 si->deleted = sbitmap_alloc (size);
1838 bitmap_clear (si->deleted);
1839 si->node_mapping = XNEWVEC (unsigned int, size);
1840 si->dfs = XCNEWVEC (unsigned int, size);
1842 for (i = 0; i < size; i++)
1843 si->node_mapping[i] = i;
1845 si->scc_stack.create (1);
1846 return si;
1849 /* Free an SCC info structure pointed to by SI */
1851 static void
1852 free_scc_info (struct scc_info *si)
1854 sbitmap_free (si->visited);
1855 sbitmap_free (si->deleted);
1856 free (si->node_mapping);
1857 free (si->dfs);
1858 si->scc_stack.release ();
1859 free (si);
1863 /* Find indirect cycles in GRAPH that occur, using strongly connected
1864 components, and note them in the indirect cycles map.
1866 This technique comes from Ben Hardekopf and Calvin Lin,
1867 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1868 Lines of Code", submitted to PLDI 2007. */
1870 static void
1871 find_indirect_cycles (constraint_graph_t graph)
1873 unsigned int i;
1874 unsigned int size = graph->size;
1875 struct scc_info *si = init_scc_info (size);
1877 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1878 if (!bitmap_bit_p (si->visited, i) && find (i) == i)
1879 scc_visit (graph, si, i);
1881 free_scc_info (si);
1884 /* Compute a topological ordering for GRAPH, and store the result in the
1885 topo_info structure TI. */
1887 static void
1888 compute_topo_order (constraint_graph_t graph,
1889 struct topo_info *ti)
1891 unsigned int i;
1892 unsigned int size = graph->size;
1894 for (i = 0; i != size; ++i)
1895 if (!bitmap_bit_p (ti->visited, i) && find (i) == i)
1896 topo_visit (graph, ti, i);
1899 /* Structure used to for hash value numbering of pointer equivalence
1900 classes. */
1902 typedef struct equiv_class_label
1904 hashval_t hashcode;
1905 unsigned int equivalence_class;
1906 bitmap labels;
1907 } *equiv_class_label_t;
1908 typedef const struct equiv_class_label *const_equiv_class_label_t;
1910 /* Equiv_class_label hashtable helpers. */
1912 struct equiv_class_hasher : typed_free_remove <equiv_class_label>
1914 typedef equiv_class_label value_type;
1915 typedef equiv_class_label compare_type;
1916 static inline hashval_t hash (const value_type *);
1917 static inline bool equal (const value_type *, const compare_type *);
1920 /* Hash function for a equiv_class_label_t */
1922 inline hashval_t
1923 equiv_class_hasher::hash (const value_type *ecl)
1925 return ecl->hashcode;
1928 /* Equality function for two equiv_class_label_t's. */
1930 inline bool
1931 equiv_class_hasher::equal (const value_type *eql1, const compare_type *eql2)
1933 return (eql1->hashcode == eql2->hashcode
1934 && bitmap_equal_p (eql1->labels, eql2->labels));
1937 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1938 classes. */
1939 static hash_table<equiv_class_hasher> *pointer_equiv_class_table;
1941 /* A hashtable for mapping a bitmap of labels->location equivalence
1942 classes. */
1943 static hash_table<equiv_class_hasher> *location_equiv_class_table;
1945 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1946 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1947 is equivalent to. */
1949 static equiv_class_label *
1950 equiv_class_lookup_or_add (hash_table<equiv_class_hasher> *table,
1951 bitmap labels)
1953 equiv_class_label **slot;
1954 equiv_class_label ecl;
1956 ecl.labels = labels;
1957 ecl.hashcode = bitmap_hash (labels);
1958 slot = table->find_slot (&ecl, INSERT);
1959 if (!*slot)
1961 *slot = XNEW (struct equiv_class_label);
1962 (*slot)->labels = labels;
1963 (*slot)->hashcode = ecl.hashcode;
1964 (*slot)->equivalence_class = 0;
1967 return *slot;
1970 /* Perform offline variable substitution.
1972 This is a worst case quadratic time way of identifying variables
1973 that must have equivalent points-to sets, including those caused by
1974 static cycles, and single entry subgraphs, in the constraint graph.
1976 The technique is described in "Exploiting Pointer and Location
1977 Equivalence to Optimize Pointer Analysis. In the 14th International
1978 Static Analysis Symposium (SAS), August 2007." It is known as the
1979 "HU" algorithm, and is equivalent to value numbering the collapsed
1980 constraint graph including evaluating unions.
1982 The general method of finding equivalence classes is as follows:
1983 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1984 Initialize all non-REF nodes to be direct nodes.
1985 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1986 variable}
1987 For each constraint containing the dereference, we also do the same
1988 thing.
1990 We then compute SCC's in the graph and unify nodes in the same SCC,
1991 including pts sets.
1993 For each non-collapsed node x:
1994 Visit all unvisited explicit incoming edges.
1995 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1996 where y->x.
1997 Lookup the equivalence class for pts(x).
1998 If we found one, equivalence_class(x) = found class.
1999 Otherwise, equivalence_class(x) = new class, and new_class is
2000 added to the lookup table.
2002 All direct nodes with the same equivalence class can be replaced
2003 with a single representative node.
2004 All unlabeled nodes (label == 0) are not pointers and all edges
2005 involving them can be eliminated.
2006 We perform these optimizations during rewrite_constraints
2008 In addition to pointer equivalence class finding, we also perform
2009 location equivalence class finding. This is the set of variables
2010 that always appear together in points-to sets. We use this to
2011 compress the size of the points-to sets. */
2013 /* Current maximum pointer equivalence class id. */
2014 static int pointer_equiv_class;
2016 /* Current maximum location equivalence class id. */
2017 static int location_equiv_class;
2019 /* Recursive routine to find strongly connected components in GRAPH,
2020 and label it's nodes with DFS numbers. */
2022 static void
2023 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2025 unsigned int i;
2026 bitmap_iterator bi;
2027 unsigned int my_dfs;
2029 gcc_checking_assert (si->node_mapping[n] == n);
2030 bitmap_set_bit (si->visited, n);
2031 si->dfs[n] = si->current_index ++;
2032 my_dfs = si->dfs[n];
2034 /* Visit all the successors. */
2035 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2037 unsigned int w = si->node_mapping[i];
2039 if (bitmap_bit_p (si->deleted, w))
2040 continue;
2042 if (!bitmap_bit_p (si->visited, w))
2043 condense_visit (graph, si, w);
2045 unsigned int t = si->node_mapping[w];
2046 gcc_checking_assert (si->node_mapping[n] == n);
2047 if (si->dfs[t] < si->dfs[n])
2048 si->dfs[n] = si->dfs[t];
2051 /* Visit all the implicit predecessors. */
2052 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2054 unsigned int w = si->node_mapping[i];
2056 if (bitmap_bit_p (si->deleted, w))
2057 continue;
2059 if (!bitmap_bit_p (si->visited, w))
2060 condense_visit (graph, si, w);
2062 unsigned int t = si->node_mapping[w];
2063 gcc_assert (si->node_mapping[n] == n);
2064 if (si->dfs[t] < si->dfs[n])
2065 si->dfs[n] = si->dfs[t];
2068 /* See if any components have been identified. */
2069 if (si->dfs[n] == my_dfs)
2071 while (si->scc_stack.length () != 0
2072 && si->dfs[si->scc_stack.last ()] >= my_dfs)
2074 unsigned int w = si->scc_stack.pop ();
2075 si->node_mapping[w] = n;
2077 if (!bitmap_bit_p (graph->direct_nodes, w))
2078 bitmap_clear_bit (graph->direct_nodes, n);
2080 /* Unify our nodes. */
2081 if (graph->preds[w])
2083 if (!graph->preds[n])
2084 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2085 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2087 if (graph->implicit_preds[w])
2089 if (!graph->implicit_preds[n])
2090 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2091 bitmap_ior_into (graph->implicit_preds[n],
2092 graph->implicit_preds[w]);
2094 if (graph->points_to[w])
2096 if (!graph->points_to[n])
2097 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2098 bitmap_ior_into (graph->points_to[n],
2099 graph->points_to[w]);
2102 bitmap_set_bit (si->deleted, n);
2104 else
2105 si->scc_stack.safe_push (n);
2108 /* Label pointer equivalences.
2110 This performs a value numbering of the constraint graph to
2111 discover which variables will always have the same points-to sets
2112 under the current set of constraints.
2114 The way it value numbers is to store the set of points-to bits
2115 generated by the constraints and graph edges. This is just used as a
2116 hash and equality comparison. The *actual set of points-to bits* is
2117 completely irrelevant, in that we don't care about being able to
2118 extract them later.
2120 The equality values (currently bitmaps) just have to satisfy a few
2121 constraints, the main ones being:
2122 1. The combining operation must be order independent.
2123 2. The end result of a given set of operations must be unique iff the
2124 combination of input values is unique
2125 3. Hashable. */
2127 static void
2128 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2130 unsigned int i, first_pred;
2131 bitmap_iterator bi;
2133 bitmap_set_bit (si->visited, n);
2135 /* Label and union our incoming edges's points to sets. */
2136 first_pred = -1U;
2137 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2139 unsigned int w = si->node_mapping[i];
2140 if (!bitmap_bit_p (si->visited, w))
2141 label_visit (graph, si, w);
2143 /* Skip unused edges */
2144 if (w == n || graph->pointer_label[w] == 0)
2145 continue;
2147 if (graph->points_to[w])
2149 if (!graph->points_to[n])
2151 if (first_pred == -1U)
2152 first_pred = w;
2153 else
2155 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2156 bitmap_ior (graph->points_to[n],
2157 graph->points_to[first_pred],
2158 graph->points_to[w]);
2161 else
2162 bitmap_ior_into (graph->points_to[n], graph->points_to[w]);
2166 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2167 if (!bitmap_bit_p (graph->direct_nodes, n))
2169 if (!graph->points_to[n])
2171 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2172 if (first_pred != -1U)
2173 bitmap_copy (graph->points_to[n], graph->points_to[first_pred]);
2175 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2176 graph->pointer_label[n] = pointer_equiv_class++;
2177 equiv_class_label_t ecl;
2178 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2179 graph->points_to[n]);
2180 ecl->equivalence_class = graph->pointer_label[n];
2181 return;
2184 /* If there was only a single non-empty predecessor the pointer equiv
2185 class is the same. */
2186 if (!graph->points_to[n])
2188 if (first_pred != -1U)
2190 graph->pointer_label[n] = graph->pointer_label[first_pred];
2191 graph->points_to[n] = graph->points_to[first_pred];
2193 return;
2196 if (!bitmap_empty_p (graph->points_to[n]))
2198 equiv_class_label_t ecl;
2199 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2200 graph->points_to[n]);
2201 if (ecl->equivalence_class == 0)
2202 ecl->equivalence_class = pointer_equiv_class++;
2203 else
2205 BITMAP_FREE (graph->points_to[n]);
2206 graph->points_to[n] = ecl->labels;
2208 graph->pointer_label[n] = ecl->equivalence_class;
2212 /* Print the pred graph in dot format. */
2214 static void
2215 dump_pred_graph (struct scc_info *si, FILE *file)
2217 unsigned int i;
2219 /* Only print the graph if it has already been initialized: */
2220 if (!graph)
2221 return;
2223 /* Prints the header of the dot file: */
2224 fprintf (file, "strict digraph {\n");
2225 fprintf (file, " node [\n shape = box\n ]\n");
2226 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
2227 fprintf (file, "\n // List of nodes and complex constraints in "
2228 "the constraint graph:\n");
2230 /* The next lines print the nodes in the graph together with the
2231 complex constraints attached to them. */
2232 for (i = 1; i < graph->size; i++)
2234 if (i == FIRST_REF_NODE)
2235 continue;
2236 if (si->node_mapping[i] != i)
2237 continue;
2238 if (i < FIRST_REF_NODE)
2239 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2240 else
2241 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2242 if (graph->points_to[i]
2243 && !bitmap_empty_p (graph->points_to[i]))
2245 fprintf (file, "[label=\"%s = {", get_varinfo (i)->name);
2246 unsigned j;
2247 bitmap_iterator bi;
2248 EXECUTE_IF_SET_IN_BITMAP (graph->points_to[i], 0, j, bi)
2249 fprintf (file, " %d", j);
2250 fprintf (file, " }\"]");
2252 fprintf (file, ";\n");
2255 /* Go over the edges. */
2256 fprintf (file, "\n // Edges in the constraint graph:\n");
2257 for (i = 1; i < graph->size; i++)
2259 unsigned j;
2260 bitmap_iterator bi;
2261 if (si->node_mapping[i] != i)
2262 continue;
2263 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[i], 0, j, bi)
2265 unsigned from = si->node_mapping[j];
2266 if (from < FIRST_REF_NODE)
2267 fprintf (file, "\"%s\"", get_varinfo (from)->name);
2268 else
2269 fprintf (file, "\"*%s\"", get_varinfo (from - FIRST_REF_NODE)->name);
2270 fprintf (file, " -> ");
2271 if (i < FIRST_REF_NODE)
2272 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2273 else
2274 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2275 fprintf (file, ";\n");
2279 /* Prints the tail of the dot file. */
2280 fprintf (file, "}\n");
2283 /* Perform offline variable substitution, discovering equivalence
2284 classes, and eliminating non-pointer variables. */
2286 static struct scc_info *
2287 perform_var_substitution (constraint_graph_t graph)
2289 unsigned int i;
2290 unsigned int size = graph->size;
2291 struct scc_info *si = init_scc_info (size);
2293 bitmap_obstack_initialize (&iteration_obstack);
2294 pointer_equiv_class_table = new hash_table<equiv_class_hasher> (511);
2295 location_equiv_class_table
2296 = new hash_table<equiv_class_hasher> (511);
2297 pointer_equiv_class = 1;
2298 location_equiv_class = 1;
2300 /* Condense the nodes, which means to find SCC's, count incoming
2301 predecessors, and unite nodes in SCC's. */
2302 for (i = 1; i < FIRST_REF_NODE; i++)
2303 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2304 condense_visit (graph, si, si->node_mapping[i]);
2306 if (dump_file && (dump_flags & TDF_GRAPH))
2308 fprintf (dump_file, "\n\n// The constraint graph before var-substitution "
2309 "in dot format:\n");
2310 dump_pred_graph (si, dump_file);
2311 fprintf (dump_file, "\n\n");
2314 bitmap_clear (si->visited);
2315 /* Actually the label the nodes for pointer equivalences */
2316 for (i = 1; i < FIRST_REF_NODE; i++)
2317 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2318 label_visit (graph, si, si->node_mapping[i]);
2320 /* Calculate location equivalence labels. */
2321 for (i = 1; i < FIRST_REF_NODE; i++)
2323 bitmap pointed_by;
2324 bitmap_iterator bi;
2325 unsigned int j;
2327 if (!graph->pointed_by[i])
2328 continue;
2329 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2331 /* Translate the pointed-by mapping for pointer equivalence
2332 labels. */
2333 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2335 bitmap_set_bit (pointed_by,
2336 graph->pointer_label[si->node_mapping[j]]);
2338 /* The original pointed_by is now dead. */
2339 BITMAP_FREE (graph->pointed_by[i]);
2341 /* Look up the location equivalence label if one exists, or make
2342 one otherwise. */
2343 equiv_class_label_t ecl;
2344 ecl = equiv_class_lookup_or_add (location_equiv_class_table, pointed_by);
2345 if (ecl->equivalence_class == 0)
2346 ecl->equivalence_class = location_equiv_class++;
2347 else
2349 if (dump_file && (dump_flags & TDF_DETAILS))
2350 fprintf (dump_file, "Found location equivalence for node %s\n",
2351 get_varinfo (i)->name);
2352 BITMAP_FREE (pointed_by);
2354 graph->loc_label[i] = ecl->equivalence_class;
2358 if (dump_file && (dump_flags & TDF_DETAILS))
2359 for (i = 1; i < FIRST_REF_NODE; i++)
2361 unsigned j = si->node_mapping[i];
2362 if (j != i)
2364 fprintf (dump_file, "%s node id %d ",
2365 bitmap_bit_p (graph->direct_nodes, i)
2366 ? "Direct" : "Indirect", i);
2367 if (i < FIRST_REF_NODE)
2368 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2369 else
2370 fprintf (dump_file, "\"*%s\"",
2371 get_varinfo (i - FIRST_REF_NODE)->name);
2372 fprintf (dump_file, " mapped to SCC leader node id %d ", j);
2373 if (j < FIRST_REF_NODE)
2374 fprintf (dump_file, "\"%s\"\n", get_varinfo (j)->name);
2375 else
2376 fprintf (dump_file, "\"*%s\"\n",
2377 get_varinfo (j - FIRST_REF_NODE)->name);
2379 else
2381 fprintf (dump_file,
2382 "Equivalence classes for %s node id %d ",
2383 bitmap_bit_p (graph->direct_nodes, i)
2384 ? "direct" : "indirect", i);
2385 if (i < FIRST_REF_NODE)
2386 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2387 else
2388 fprintf (dump_file, "\"*%s\"",
2389 get_varinfo (i - FIRST_REF_NODE)->name);
2390 fprintf (dump_file,
2391 ": pointer %d, location %d\n",
2392 graph->pointer_label[i], graph->loc_label[i]);
2396 /* Quickly eliminate our non-pointer variables. */
2398 for (i = 1; i < FIRST_REF_NODE; i++)
2400 unsigned int node = si->node_mapping[i];
2402 if (graph->pointer_label[node] == 0)
2404 if (dump_file && (dump_flags & TDF_DETAILS))
2405 fprintf (dump_file,
2406 "%s is a non-pointer variable, eliminating edges.\n",
2407 get_varinfo (node)->name);
2408 stats.nonpointer_vars++;
2409 clear_edges_for_node (graph, node);
2413 return si;
2416 /* Free information that was only necessary for variable
2417 substitution. */
2419 static void
2420 free_var_substitution_info (struct scc_info *si)
2422 free_scc_info (si);
2423 free (graph->pointer_label);
2424 free (graph->loc_label);
2425 free (graph->pointed_by);
2426 free (graph->points_to);
2427 free (graph->eq_rep);
2428 sbitmap_free (graph->direct_nodes);
2429 delete pointer_equiv_class_table;
2430 pointer_equiv_class_table = NULL;
2431 delete location_equiv_class_table;
2432 location_equiv_class_table = NULL;
2433 bitmap_obstack_release (&iteration_obstack);
2436 /* Return an existing node that is equivalent to NODE, which has
2437 equivalence class LABEL, if one exists. Return NODE otherwise. */
2439 static unsigned int
2440 find_equivalent_node (constraint_graph_t graph,
2441 unsigned int node, unsigned int label)
2443 /* If the address version of this variable is unused, we can
2444 substitute it for anything else with the same label.
2445 Otherwise, we know the pointers are equivalent, but not the
2446 locations, and we can unite them later. */
2448 if (!bitmap_bit_p (graph->address_taken, node))
2450 gcc_checking_assert (label < graph->size);
2452 if (graph->eq_rep[label] != -1)
2454 /* Unify the two variables since we know they are equivalent. */
2455 if (unite (graph->eq_rep[label], node))
2456 unify_nodes (graph, graph->eq_rep[label], node, false);
2457 return graph->eq_rep[label];
2459 else
2461 graph->eq_rep[label] = node;
2462 graph->pe_rep[label] = node;
2465 else
2467 gcc_checking_assert (label < graph->size);
2468 graph->pe[node] = label;
2469 if (graph->pe_rep[label] == -1)
2470 graph->pe_rep[label] = node;
2473 return node;
2476 /* Unite pointer equivalent but not location equivalent nodes in
2477 GRAPH. This may only be performed once variable substitution is
2478 finished. */
2480 static void
2481 unite_pointer_equivalences (constraint_graph_t graph)
2483 unsigned int i;
2485 /* Go through the pointer equivalences and unite them to their
2486 representative, if they aren't already. */
2487 for (i = 1; i < FIRST_REF_NODE; i++)
2489 unsigned int label = graph->pe[i];
2490 if (label)
2492 int label_rep = graph->pe_rep[label];
2494 if (label_rep == -1)
2495 continue;
2497 label_rep = find (label_rep);
2498 if (label_rep >= 0 && unite (label_rep, find (i)))
2499 unify_nodes (graph, label_rep, i, false);
2504 /* Move complex constraints to the GRAPH nodes they belong to. */
2506 static void
2507 move_complex_constraints (constraint_graph_t graph)
2509 int i;
2510 constraint_t c;
2512 FOR_EACH_VEC_ELT (constraints, i, c)
2514 if (c)
2516 struct constraint_expr lhs = c->lhs;
2517 struct constraint_expr rhs = c->rhs;
2519 if (lhs.type == DEREF)
2521 insert_into_complex (graph, lhs.var, c);
2523 else if (rhs.type == DEREF)
2525 if (!(get_varinfo (lhs.var)->is_special_var))
2526 insert_into_complex (graph, rhs.var, c);
2528 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2529 && (lhs.offset != 0 || rhs.offset != 0))
2531 insert_into_complex (graph, rhs.var, c);
2538 /* Optimize and rewrite complex constraints while performing
2539 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2540 result of perform_variable_substitution. */
2542 static void
2543 rewrite_constraints (constraint_graph_t graph,
2544 struct scc_info *si)
2546 int i;
2547 constraint_t c;
2549 #ifdef ENABLE_CHECKING
2550 for (unsigned int j = 0; j < graph->size; j++)
2551 gcc_assert (find (j) == j);
2552 #endif
2554 FOR_EACH_VEC_ELT (constraints, i, c)
2556 struct constraint_expr lhs = c->lhs;
2557 struct constraint_expr rhs = c->rhs;
2558 unsigned int lhsvar = find (lhs.var);
2559 unsigned int rhsvar = find (rhs.var);
2560 unsigned int lhsnode, rhsnode;
2561 unsigned int lhslabel, rhslabel;
2563 lhsnode = si->node_mapping[lhsvar];
2564 rhsnode = si->node_mapping[rhsvar];
2565 lhslabel = graph->pointer_label[lhsnode];
2566 rhslabel = graph->pointer_label[rhsnode];
2568 /* See if it is really a non-pointer variable, and if so, ignore
2569 the constraint. */
2570 if (lhslabel == 0)
2572 if (dump_file && (dump_flags & TDF_DETAILS))
2575 fprintf (dump_file, "%s is a non-pointer variable,"
2576 "ignoring constraint:",
2577 get_varinfo (lhs.var)->name);
2578 dump_constraint (dump_file, c);
2579 fprintf (dump_file, "\n");
2581 constraints[i] = NULL;
2582 continue;
2585 if (rhslabel == 0)
2587 if (dump_file && (dump_flags & TDF_DETAILS))
2590 fprintf (dump_file, "%s is a non-pointer variable,"
2591 "ignoring constraint:",
2592 get_varinfo (rhs.var)->name);
2593 dump_constraint (dump_file, c);
2594 fprintf (dump_file, "\n");
2596 constraints[i] = NULL;
2597 continue;
2600 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2601 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2602 c->lhs.var = lhsvar;
2603 c->rhs.var = rhsvar;
2607 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2608 part of an SCC, false otherwise. */
2610 static bool
2611 eliminate_indirect_cycles (unsigned int node)
2613 if (graph->indirect_cycles[node] != -1
2614 && !bitmap_empty_p (get_varinfo (node)->solution))
2616 unsigned int i;
2617 auto_vec<unsigned> queue;
2618 int queuepos;
2619 unsigned int to = find (graph->indirect_cycles[node]);
2620 bitmap_iterator bi;
2622 /* We can't touch the solution set and call unify_nodes
2623 at the same time, because unify_nodes is going to do
2624 bitmap unions into it. */
2626 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2628 if (find (i) == i && i != to)
2630 if (unite (to, i))
2631 queue.safe_push (i);
2635 for (queuepos = 0;
2636 queue.iterate (queuepos, &i);
2637 queuepos++)
2639 unify_nodes (graph, to, i, true);
2641 return true;
2643 return false;
2646 /* Solve the constraint graph GRAPH using our worklist solver.
2647 This is based on the PW* family of solvers from the "Efficient Field
2648 Sensitive Pointer Analysis for C" paper.
2649 It works by iterating over all the graph nodes, processing the complex
2650 constraints and propagating the copy constraints, until everything stops
2651 changed. This corresponds to steps 6-8 in the solving list given above. */
2653 static void
2654 solve_graph (constraint_graph_t graph)
2656 unsigned int size = graph->size;
2657 unsigned int i;
2658 bitmap pts;
2660 changed = BITMAP_ALLOC (NULL);
2662 /* Mark all initial non-collapsed nodes as changed. */
2663 for (i = 1; i < size; i++)
2665 varinfo_t ivi = get_varinfo (i);
2666 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2667 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2668 || graph->complex[i].length () > 0))
2669 bitmap_set_bit (changed, i);
2672 /* Allocate a bitmap to be used to store the changed bits. */
2673 pts = BITMAP_ALLOC (&pta_obstack);
2675 while (!bitmap_empty_p (changed))
2677 unsigned int i;
2678 struct topo_info *ti = init_topo_info ();
2679 stats.iterations++;
2681 bitmap_obstack_initialize (&iteration_obstack);
2683 compute_topo_order (graph, ti);
2685 while (ti->topo_order.length () != 0)
2688 i = ti->topo_order.pop ();
2690 /* If this variable is not a representative, skip it. */
2691 if (find (i) != i)
2692 continue;
2694 /* In certain indirect cycle cases, we may merge this
2695 variable to another. */
2696 if (eliminate_indirect_cycles (i) && find (i) != i)
2697 continue;
2699 /* If the node has changed, we need to process the
2700 complex constraints and outgoing edges again. */
2701 if (bitmap_clear_bit (changed, i))
2703 unsigned int j;
2704 constraint_t c;
2705 bitmap solution;
2706 vec<constraint_t> complex = graph->complex[i];
2707 varinfo_t vi = get_varinfo (i);
2708 bool solution_empty;
2710 /* Compute the changed set of solution bits. If anything
2711 is in the solution just propagate that. */
2712 if (bitmap_bit_p (vi->solution, anything_id))
2714 /* If anything is also in the old solution there is
2715 nothing to do.
2716 ??? But we shouldn't ended up with "changed" set ... */
2717 if (vi->oldsolution
2718 && bitmap_bit_p (vi->oldsolution, anything_id))
2719 continue;
2720 bitmap_copy (pts, get_varinfo (find (anything_id))->solution);
2722 else if (vi->oldsolution)
2723 bitmap_and_compl (pts, vi->solution, vi->oldsolution);
2724 else
2725 bitmap_copy (pts, vi->solution);
2727 if (bitmap_empty_p (pts))
2728 continue;
2730 if (vi->oldsolution)
2731 bitmap_ior_into (vi->oldsolution, pts);
2732 else
2734 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
2735 bitmap_copy (vi->oldsolution, pts);
2738 solution = vi->solution;
2739 solution_empty = bitmap_empty_p (solution);
2741 /* Process the complex constraints */
2742 bitmap expanded_pts = NULL;
2743 FOR_EACH_VEC_ELT (complex, j, c)
2745 /* XXX: This is going to unsort the constraints in
2746 some cases, which will occasionally add duplicate
2747 constraints during unification. This does not
2748 affect correctness. */
2749 c->lhs.var = find (c->lhs.var);
2750 c->rhs.var = find (c->rhs.var);
2752 /* The only complex constraint that can change our
2753 solution to non-empty, given an empty solution,
2754 is a constraint where the lhs side is receiving
2755 some set from elsewhere. */
2756 if (!solution_empty || c->lhs.type != DEREF)
2757 do_complex_constraint (graph, c, pts, &expanded_pts);
2759 BITMAP_FREE (expanded_pts);
2761 solution_empty = bitmap_empty_p (solution);
2763 if (!solution_empty)
2765 bitmap_iterator bi;
2766 unsigned eff_escaped_id = find (escaped_id);
2768 /* Propagate solution to all successors. */
2769 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2770 0, j, bi)
2772 bitmap tmp;
2773 bool flag;
2775 unsigned int to = find (j);
2776 tmp = get_varinfo (to)->solution;
2777 flag = false;
2779 /* Don't try to propagate to ourselves. */
2780 if (to == i)
2781 continue;
2783 /* If we propagate from ESCAPED use ESCAPED as
2784 placeholder. */
2785 if (i == eff_escaped_id)
2786 flag = bitmap_set_bit (tmp, escaped_id);
2787 else
2788 flag = bitmap_ior_into (tmp, pts);
2790 if (flag)
2791 bitmap_set_bit (changed, to);
2796 free_topo_info (ti);
2797 bitmap_obstack_release (&iteration_obstack);
2800 BITMAP_FREE (pts);
2801 BITMAP_FREE (changed);
2802 bitmap_obstack_release (&oldpta_obstack);
2805 /* Map from trees to variable infos. */
2806 static hash_map<tree, varinfo_t> *vi_for_tree;
2809 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2811 static void
2812 insert_vi_for_tree (tree t, varinfo_t vi)
2814 gcc_assert (vi);
2815 gcc_assert (!vi_for_tree->put (t, vi));
2818 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2819 exist in the map, return NULL, otherwise, return the varinfo we found. */
2821 static varinfo_t
2822 lookup_vi_for_tree (tree t)
2824 varinfo_t *slot = vi_for_tree->get (t);
2825 if (slot == NULL)
2826 return NULL;
2828 return *slot;
2831 /* Return a printable name for DECL */
2833 static const char *
2834 alias_get_name (tree decl)
2836 const char *res = NULL;
2837 char *temp;
2838 int num_printed = 0;
2840 if (!dump_file)
2841 return "NULL";
2843 if (TREE_CODE (decl) == SSA_NAME)
2845 res = get_name (decl);
2846 if (res)
2847 num_printed = asprintf (&temp, "%s_%u", res, SSA_NAME_VERSION (decl));
2848 else
2849 num_printed = asprintf (&temp, "_%u", SSA_NAME_VERSION (decl));
2850 if (num_printed > 0)
2852 res = ggc_strdup (temp);
2853 free (temp);
2856 else if (DECL_P (decl))
2858 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2859 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2860 else
2862 res = get_name (decl);
2863 if (!res)
2865 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2866 if (num_printed > 0)
2868 res = ggc_strdup (temp);
2869 free (temp);
2874 if (res != NULL)
2875 return res;
2877 return "NULL";
2880 /* Find the variable id for tree T in the map.
2881 If T doesn't exist in the map, create an entry for it and return it. */
2883 static varinfo_t
2884 get_vi_for_tree (tree t)
2886 varinfo_t *slot = vi_for_tree->get (t);
2887 if (slot == NULL)
2888 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2890 return *slot;
2893 /* Get a scalar constraint expression for a new temporary variable. */
2895 static struct constraint_expr
2896 new_scalar_tmp_constraint_exp (const char *name)
2898 struct constraint_expr tmp;
2899 varinfo_t vi;
2901 vi = new_var_info (NULL_TREE, name);
2902 vi->offset = 0;
2903 vi->size = -1;
2904 vi->fullsize = -1;
2905 vi->is_full_var = 1;
2907 tmp.var = vi->id;
2908 tmp.type = SCALAR;
2909 tmp.offset = 0;
2911 return tmp;
2914 /* Get a constraint expression vector from an SSA_VAR_P node.
2915 If address_p is true, the result will be taken its address of. */
2917 static void
2918 get_constraint_for_ssa_var (tree t, vec<ce_s> *results, bool address_p)
2920 struct constraint_expr cexpr;
2921 varinfo_t vi;
2923 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2924 gcc_assert (TREE_CODE (t) == SSA_NAME || DECL_P (t));
2926 /* For parameters, get at the points-to set for the actual parm
2927 decl. */
2928 if (TREE_CODE (t) == SSA_NAME
2929 && SSA_NAME_IS_DEFAULT_DEF (t)
2930 && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2931 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL))
2933 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2934 return;
2937 /* For global variables resort to the alias target. */
2938 if (TREE_CODE (t) == VAR_DECL
2939 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
2941 varpool_node *node = varpool_node::get (t);
2942 if (node && node->alias && node->analyzed)
2944 node = node->ultimate_alias_target ();
2945 t = node->decl;
2949 vi = get_vi_for_tree (t);
2950 cexpr.var = vi->id;
2951 cexpr.type = SCALAR;
2952 cexpr.offset = 0;
2954 /* If we are not taking the address of the constraint expr, add all
2955 sub-fiels of the variable as well. */
2956 if (!address_p
2957 && !vi->is_full_var)
2959 for (; vi; vi = vi_next (vi))
2961 cexpr.var = vi->id;
2962 results->safe_push (cexpr);
2964 return;
2967 results->safe_push (cexpr);
2970 /* Process constraint T, performing various simplifications and then
2971 adding it to our list of overall constraints. */
2973 static void
2974 process_constraint (constraint_t t)
2976 struct constraint_expr rhs = t->rhs;
2977 struct constraint_expr lhs = t->lhs;
2979 gcc_assert (rhs.var < varmap.length ());
2980 gcc_assert (lhs.var < varmap.length ());
2982 /* If we didn't get any useful constraint from the lhs we get
2983 &ANYTHING as fallback from get_constraint_for. Deal with
2984 it here by turning it into *ANYTHING. */
2985 if (lhs.type == ADDRESSOF
2986 && lhs.var == anything_id)
2987 lhs.type = DEREF;
2989 /* ADDRESSOF on the lhs is invalid. */
2990 gcc_assert (lhs.type != ADDRESSOF);
2992 /* We shouldn't add constraints from things that cannot have pointers.
2993 It's not completely trivial to avoid in the callers, so do it here. */
2994 if (rhs.type != ADDRESSOF
2995 && !get_varinfo (rhs.var)->may_have_pointers)
2996 return;
2998 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2999 if (!get_varinfo (lhs.var)->may_have_pointers)
3000 return;
3002 /* This can happen in our IR with things like n->a = *p */
3003 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
3005 /* Split into tmp = *rhs, *lhs = tmp */
3006 struct constraint_expr tmplhs;
3007 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp");
3008 process_constraint (new_constraint (tmplhs, rhs));
3009 process_constraint (new_constraint (lhs, tmplhs));
3011 else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
3013 /* Split into tmp = &rhs, *lhs = tmp */
3014 struct constraint_expr tmplhs;
3015 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp");
3016 process_constraint (new_constraint (tmplhs, rhs));
3017 process_constraint (new_constraint (lhs, tmplhs));
3019 else
3021 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
3022 constraints.safe_push (t);
3027 /* Return the position, in bits, of FIELD_DECL from the beginning of its
3028 structure. */
3030 static HOST_WIDE_INT
3031 bitpos_of_field (const tree fdecl)
3033 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl))
3034 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl)))
3035 return -1;
3037 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
3038 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl)));
3042 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
3043 resulting constraint expressions in *RESULTS. */
3045 static void
3046 get_constraint_for_ptr_offset (tree ptr, tree offset,
3047 vec<ce_s> *results)
3049 struct constraint_expr c;
3050 unsigned int j, n;
3051 HOST_WIDE_INT rhsoffset;
3053 /* If we do not do field-sensitive PTA adding offsets to pointers
3054 does not change the points-to solution. */
3055 if (!use_field_sensitive)
3057 get_constraint_for_rhs (ptr, results);
3058 return;
3061 /* If the offset is not a non-negative integer constant that fits
3062 in a HOST_WIDE_INT, we have to fall back to a conservative
3063 solution which includes all sub-fields of all pointed-to
3064 variables of ptr. */
3065 if (offset == NULL_TREE
3066 || TREE_CODE (offset) != INTEGER_CST)
3067 rhsoffset = UNKNOWN_OFFSET;
3068 else
3070 /* Sign-extend the offset. */
3071 offset_int soffset = offset_int::from (offset, SIGNED);
3072 if (!wi::fits_shwi_p (soffset))
3073 rhsoffset = UNKNOWN_OFFSET;
3074 else
3076 /* Make sure the bit-offset also fits. */
3077 HOST_WIDE_INT rhsunitoffset = soffset.to_shwi ();
3078 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
3079 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
3080 rhsoffset = UNKNOWN_OFFSET;
3084 get_constraint_for_rhs (ptr, results);
3085 if (rhsoffset == 0)
3086 return;
3088 /* As we are eventually appending to the solution do not use
3089 vec::iterate here. */
3090 n = results->length ();
3091 for (j = 0; j < n; j++)
3093 varinfo_t curr;
3094 c = (*results)[j];
3095 curr = get_varinfo (c.var);
3097 if (c.type == ADDRESSOF
3098 /* If this varinfo represents a full variable just use it. */
3099 && curr->is_full_var)
3101 else if (c.type == ADDRESSOF
3102 /* If we do not know the offset add all subfields. */
3103 && rhsoffset == UNKNOWN_OFFSET)
3105 varinfo_t temp = get_varinfo (curr->head);
3108 struct constraint_expr c2;
3109 c2.var = temp->id;
3110 c2.type = ADDRESSOF;
3111 c2.offset = 0;
3112 if (c2.var != c.var)
3113 results->safe_push (c2);
3114 temp = vi_next (temp);
3116 while (temp);
3118 else if (c.type == ADDRESSOF)
3120 varinfo_t temp;
3121 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
3123 /* If curr->offset + rhsoffset is less than zero adjust it. */
3124 if (rhsoffset < 0
3125 && curr->offset < offset)
3126 offset = 0;
3128 /* We have to include all fields that overlap the current
3129 field shifted by rhsoffset. And we include at least
3130 the last or the first field of the variable to represent
3131 reachability of off-bound addresses, in particular &object + 1,
3132 conservatively correct. */
3133 temp = first_or_preceding_vi_for_offset (curr, offset);
3134 c.var = temp->id;
3135 c.offset = 0;
3136 temp = vi_next (temp);
3137 while (temp
3138 && temp->offset < offset + curr->size)
3140 struct constraint_expr c2;
3141 c2.var = temp->id;
3142 c2.type = ADDRESSOF;
3143 c2.offset = 0;
3144 results->safe_push (c2);
3145 temp = vi_next (temp);
3148 else if (c.type == SCALAR)
3150 gcc_assert (c.offset == 0);
3151 c.offset = rhsoffset;
3153 else
3154 /* We shouldn't get any DEREFs here. */
3155 gcc_unreachable ();
3157 (*results)[j] = c;
3162 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3163 If address_p is true the result will be taken its address of.
3164 If lhs_p is true then the constraint expression is assumed to be used
3165 as the lhs. */
3167 static void
3168 get_constraint_for_component_ref (tree t, vec<ce_s> *results,
3169 bool address_p, bool lhs_p)
3171 tree orig_t = t;
3172 HOST_WIDE_INT bitsize = -1;
3173 HOST_WIDE_INT bitmaxsize = -1;
3174 HOST_WIDE_INT bitpos;
3175 tree forzero;
3177 /* Some people like to do cute things like take the address of
3178 &0->a.b */
3179 forzero = t;
3180 while (handled_component_p (forzero)
3181 || INDIRECT_REF_P (forzero)
3182 || TREE_CODE (forzero) == MEM_REF)
3183 forzero = TREE_OPERAND (forzero, 0);
3185 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
3187 struct constraint_expr temp;
3189 temp.offset = 0;
3190 temp.var = integer_id;
3191 temp.type = SCALAR;
3192 results->safe_push (temp);
3193 return;
3196 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
3198 /* Pretend to take the address of the base, we'll take care of
3199 adding the required subset of sub-fields below. */
3200 get_constraint_for_1 (t, results, true, lhs_p);
3201 gcc_assert (results->length () == 1);
3202 struct constraint_expr &result = results->last ();
3204 if (result.type == SCALAR
3205 && get_varinfo (result.var)->is_full_var)
3206 /* For single-field vars do not bother about the offset. */
3207 result.offset = 0;
3208 else if (result.type == SCALAR)
3210 /* In languages like C, you can access one past the end of an
3211 array. You aren't allowed to dereference it, so we can
3212 ignore this constraint. When we handle pointer subtraction,
3213 we may have to do something cute here. */
3215 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result.var)->fullsize
3216 && bitmaxsize != 0)
3218 /* It's also not true that the constraint will actually start at the
3219 right offset, it may start in some padding. We only care about
3220 setting the constraint to the first actual field it touches, so
3221 walk to find it. */
3222 struct constraint_expr cexpr = result;
3223 varinfo_t curr;
3224 results->pop ();
3225 cexpr.offset = 0;
3226 for (curr = get_varinfo (cexpr.var); curr; curr = vi_next (curr))
3228 if (ranges_overlap_p (curr->offset, curr->size,
3229 bitpos, bitmaxsize))
3231 cexpr.var = curr->id;
3232 results->safe_push (cexpr);
3233 if (address_p)
3234 break;
3237 /* If we are going to take the address of this field then
3238 to be able to compute reachability correctly add at least
3239 the last field of the variable. */
3240 if (address_p && results->length () == 0)
3242 curr = get_varinfo (cexpr.var);
3243 while (curr->next != 0)
3244 curr = vi_next (curr);
3245 cexpr.var = curr->id;
3246 results->safe_push (cexpr);
3248 else if (results->length () == 0)
3249 /* Assert that we found *some* field there. The user couldn't be
3250 accessing *only* padding. */
3251 /* Still the user could access one past the end of an array
3252 embedded in a struct resulting in accessing *only* padding. */
3253 /* Or accessing only padding via type-punning to a type
3254 that has a filed just in padding space. */
3256 cexpr.type = SCALAR;
3257 cexpr.var = anything_id;
3258 cexpr.offset = 0;
3259 results->safe_push (cexpr);
3262 else if (bitmaxsize == 0)
3264 if (dump_file && (dump_flags & TDF_DETAILS))
3265 fprintf (dump_file, "Access to zero-sized part of variable,"
3266 "ignoring\n");
3268 else
3269 if (dump_file && (dump_flags & TDF_DETAILS))
3270 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3272 else if (result.type == DEREF)
3274 /* If we do not know exactly where the access goes say so. Note
3275 that only for non-structure accesses we know that we access
3276 at most one subfiled of any variable. */
3277 if (bitpos == -1
3278 || bitsize != bitmaxsize
3279 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
3280 || result.offset == UNKNOWN_OFFSET)
3281 result.offset = UNKNOWN_OFFSET;
3282 else
3283 result.offset += bitpos;
3285 else if (result.type == ADDRESSOF)
3287 /* We can end up here for component references on a
3288 VIEW_CONVERT_EXPR <>(&foobar). */
3289 result.type = SCALAR;
3290 result.var = anything_id;
3291 result.offset = 0;
3293 else
3294 gcc_unreachable ();
3298 /* Dereference the constraint expression CONS, and return the result.
3299 DEREF (ADDRESSOF) = SCALAR
3300 DEREF (SCALAR) = DEREF
3301 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3302 This is needed so that we can handle dereferencing DEREF constraints. */
3304 static void
3305 do_deref (vec<ce_s> *constraints)
3307 struct constraint_expr *c;
3308 unsigned int i = 0;
3310 FOR_EACH_VEC_ELT (*constraints, i, c)
3312 if (c->type == SCALAR)
3313 c->type = DEREF;
3314 else if (c->type == ADDRESSOF)
3315 c->type = SCALAR;
3316 else if (c->type == DEREF)
3318 struct constraint_expr tmplhs;
3319 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp");
3320 process_constraint (new_constraint (tmplhs, *c));
3321 c->var = tmplhs.var;
3323 else
3324 gcc_unreachable ();
3328 /* Given a tree T, return the constraint expression for taking the
3329 address of it. */
3331 static void
3332 get_constraint_for_address_of (tree t, vec<ce_s> *results)
3334 struct constraint_expr *c;
3335 unsigned int i;
3337 get_constraint_for_1 (t, results, true, true);
3339 FOR_EACH_VEC_ELT (*results, i, c)
3341 if (c->type == DEREF)
3342 c->type = SCALAR;
3343 else
3344 c->type = ADDRESSOF;
3348 /* Given a tree T, return the constraint expression for it. */
3350 static void
3351 get_constraint_for_1 (tree t, vec<ce_s> *results, bool address_p,
3352 bool lhs_p)
3354 struct constraint_expr temp;
3356 /* x = integer is all glommed to a single variable, which doesn't
3357 point to anything by itself. That is, of course, unless it is an
3358 integer constant being treated as a pointer, in which case, we
3359 will return that this is really the addressof anything. This
3360 happens below, since it will fall into the default case. The only
3361 case we know something about an integer treated like a pointer is
3362 when it is the NULL pointer, and then we just say it points to
3363 NULL.
3365 Do not do that if -fno-delete-null-pointer-checks though, because
3366 in that case *NULL does not fail, so it _should_ alias *anything.
3367 It is not worth adding a new option or renaming the existing one,
3368 since this case is relatively obscure. */
3369 if ((TREE_CODE (t) == INTEGER_CST
3370 && integer_zerop (t))
3371 /* The only valid CONSTRUCTORs in gimple with pointer typed
3372 elements are zero-initializer. But in IPA mode we also
3373 process global initializers, so verify at least. */
3374 || (TREE_CODE (t) == CONSTRUCTOR
3375 && CONSTRUCTOR_NELTS (t) == 0))
3377 if (flag_delete_null_pointer_checks)
3378 temp.var = nothing_id;
3379 else
3380 temp.var = nonlocal_id;
3381 temp.type = ADDRESSOF;
3382 temp.offset = 0;
3383 results->safe_push (temp);
3384 return;
3387 /* String constants are read-only, ideally we'd have a CONST_DECL
3388 for those. */
3389 if (TREE_CODE (t) == STRING_CST)
3391 temp.var = string_id;
3392 temp.type = SCALAR;
3393 temp.offset = 0;
3394 results->safe_push (temp);
3395 return;
3398 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3400 case tcc_expression:
3402 switch (TREE_CODE (t))
3404 case ADDR_EXPR:
3405 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3406 return;
3407 default:;
3409 break;
3411 case tcc_reference:
3413 switch (TREE_CODE (t))
3415 case MEM_REF:
3417 struct constraint_expr cs;
3418 varinfo_t vi, curr;
3419 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0),
3420 TREE_OPERAND (t, 1), results);
3421 do_deref (results);
3423 /* If we are not taking the address then make sure to process
3424 all subvariables we might access. */
3425 if (address_p)
3426 return;
3428 cs = results->last ();
3429 if (cs.type == DEREF
3430 && type_can_have_subvars (TREE_TYPE (t)))
3432 /* For dereferences this means we have to defer it
3433 to solving time. */
3434 results->last ().offset = UNKNOWN_OFFSET;
3435 return;
3437 if (cs.type != SCALAR)
3438 return;
3440 vi = get_varinfo (cs.var);
3441 curr = vi_next (vi);
3442 if (!vi->is_full_var
3443 && curr)
3445 unsigned HOST_WIDE_INT size;
3446 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t))))
3447 size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t)));
3448 else
3449 size = -1;
3450 for (; curr; curr = vi_next (curr))
3452 if (curr->offset - vi->offset < size)
3454 cs.var = curr->id;
3455 results->safe_push (cs);
3457 else
3458 break;
3461 return;
3463 case ARRAY_REF:
3464 case ARRAY_RANGE_REF:
3465 case COMPONENT_REF:
3466 get_constraint_for_component_ref (t, results, address_p, lhs_p);
3467 return;
3468 case VIEW_CONVERT_EXPR:
3469 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p,
3470 lhs_p);
3471 return;
3472 /* We are missing handling for TARGET_MEM_REF here. */
3473 default:;
3475 break;
3477 case tcc_exceptional:
3479 switch (TREE_CODE (t))
3481 case SSA_NAME:
3483 get_constraint_for_ssa_var (t, results, address_p);
3484 return;
3486 case CONSTRUCTOR:
3488 unsigned int i;
3489 tree val;
3490 auto_vec<ce_s> tmp;
3491 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
3493 struct constraint_expr *rhsp;
3494 unsigned j;
3495 get_constraint_for_1 (val, &tmp, address_p, lhs_p);
3496 FOR_EACH_VEC_ELT (tmp, j, rhsp)
3497 results->safe_push (*rhsp);
3498 tmp.truncate (0);
3500 /* We do not know whether the constructor was complete,
3501 so technically we have to add &NOTHING or &ANYTHING
3502 like we do for an empty constructor as well. */
3503 return;
3505 default:;
3507 break;
3509 case tcc_declaration:
3511 get_constraint_for_ssa_var (t, results, address_p);
3512 return;
3514 case tcc_constant:
3516 /* We cannot refer to automatic variables through constants. */
3517 temp.type = ADDRESSOF;
3518 temp.var = nonlocal_id;
3519 temp.offset = 0;
3520 results->safe_push (temp);
3521 return;
3523 default:;
3526 /* The default fallback is a constraint from anything. */
3527 temp.type = ADDRESSOF;
3528 temp.var = anything_id;
3529 temp.offset = 0;
3530 results->safe_push (temp);
3533 /* Given a gimple tree T, return the constraint expression vector for it. */
3535 static void
3536 get_constraint_for (tree t, vec<ce_s> *results)
3538 gcc_assert (results->length () == 0);
3540 get_constraint_for_1 (t, results, false, true);
3543 /* Given a gimple tree T, return the constraint expression vector for it
3544 to be used as the rhs of a constraint. */
3546 static void
3547 get_constraint_for_rhs (tree t, vec<ce_s> *results)
3549 gcc_assert (results->length () == 0);
3551 get_constraint_for_1 (t, results, false, false);
3555 /* Efficiently generates constraints from all entries in *RHSC to all
3556 entries in *LHSC. */
3558 static void
3559 process_all_all_constraints (vec<ce_s> lhsc,
3560 vec<ce_s> rhsc)
3562 struct constraint_expr *lhsp, *rhsp;
3563 unsigned i, j;
3565 if (lhsc.length () <= 1 || rhsc.length () <= 1)
3567 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3568 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
3569 process_constraint (new_constraint (*lhsp, *rhsp));
3571 else
3573 struct constraint_expr tmp;
3574 tmp = new_scalar_tmp_constraint_exp ("allalltmp");
3575 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
3576 process_constraint (new_constraint (tmp, *rhsp));
3577 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3578 process_constraint (new_constraint (*lhsp, tmp));
3582 /* Handle aggregate copies by expanding into copies of the respective
3583 fields of the structures. */
3585 static void
3586 do_structure_copy (tree lhsop, tree rhsop)
3588 struct constraint_expr *lhsp, *rhsp;
3589 auto_vec<ce_s> lhsc;
3590 auto_vec<ce_s> rhsc;
3591 unsigned j;
3593 get_constraint_for (lhsop, &lhsc);
3594 get_constraint_for_rhs (rhsop, &rhsc);
3595 lhsp = &lhsc[0];
3596 rhsp = &rhsc[0];
3597 if (lhsp->type == DEREF
3598 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3599 || rhsp->type == DEREF)
3601 if (lhsp->type == DEREF)
3603 gcc_assert (lhsc.length () == 1);
3604 lhsp->offset = UNKNOWN_OFFSET;
3606 if (rhsp->type == DEREF)
3608 gcc_assert (rhsc.length () == 1);
3609 rhsp->offset = UNKNOWN_OFFSET;
3611 process_all_all_constraints (lhsc, rhsc);
3613 else if (lhsp->type == SCALAR
3614 && (rhsp->type == SCALAR
3615 || rhsp->type == ADDRESSOF))
3617 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3618 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
3619 unsigned k = 0;
3620 get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize);
3621 get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize);
3622 for (j = 0; lhsc.iterate (j, &lhsp);)
3624 varinfo_t lhsv, rhsv;
3625 rhsp = &rhsc[k];
3626 lhsv = get_varinfo (lhsp->var);
3627 rhsv = get_varinfo (rhsp->var);
3628 if (lhsv->may_have_pointers
3629 && (lhsv->is_full_var
3630 || rhsv->is_full_var
3631 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3632 rhsv->offset + lhsoffset, rhsv->size)))
3633 process_constraint (new_constraint (*lhsp, *rhsp));
3634 if (!rhsv->is_full_var
3635 && (lhsv->is_full_var
3636 || (lhsv->offset + rhsoffset + lhsv->size
3637 > rhsv->offset + lhsoffset + rhsv->size)))
3639 ++k;
3640 if (k >= rhsc.length ())
3641 break;
3643 else
3644 ++j;
3647 else
3648 gcc_unreachable ();
3651 /* Create constraints ID = { rhsc }. */
3653 static void
3654 make_constraints_to (unsigned id, vec<ce_s> rhsc)
3656 struct constraint_expr *c;
3657 struct constraint_expr includes;
3658 unsigned int j;
3660 includes.var = id;
3661 includes.offset = 0;
3662 includes.type = SCALAR;
3664 FOR_EACH_VEC_ELT (rhsc, j, c)
3665 process_constraint (new_constraint (includes, *c));
3668 /* Create a constraint ID = OP. */
3670 static void
3671 make_constraint_to (unsigned id, tree op)
3673 auto_vec<ce_s> rhsc;
3674 get_constraint_for_rhs (op, &rhsc);
3675 make_constraints_to (id, rhsc);
3678 /* Create a constraint ID = &FROM. */
3680 static void
3681 make_constraint_from (varinfo_t vi, int from)
3683 struct constraint_expr lhs, rhs;
3685 lhs.var = vi->id;
3686 lhs.offset = 0;
3687 lhs.type = SCALAR;
3689 rhs.var = from;
3690 rhs.offset = 0;
3691 rhs.type = ADDRESSOF;
3692 process_constraint (new_constraint (lhs, rhs));
3695 /* Create a constraint ID = FROM. */
3697 static void
3698 make_copy_constraint (varinfo_t vi, int from)
3700 struct constraint_expr lhs, rhs;
3702 lhs.var = vi->id;
3703 lhs.offset = 0;
3704 lhs.type = SCALAR;
3706 rhs.var = from;
3707 rhs.offset = 0;
3708 rhs.type = SCALAR;
3709 process_constraint (new_constraint (lhs, rhs));
3712 /* Make constraints necessary to make OP escape. */
3714 static void
3715 make_escape_constraint (tree op)
3717 make_constraint_to (escaped_id, op);
3720 /* Add constraints to that the solution of VI is transitively closed. */
3722 static void
3723 make_transitive_closure_constraints (varinfo_t vi)
3725 struct constraint_expr lhs, rhs;
3727 /* VAR = *VAR; */
3728 lhs.type = SCALAR;
3729 lhs.var = vi->id;
3730 lhs.offset = 0;
3731 rhs.type = DEREF;
3732 rhs.var = vi->id;
3733 rhs.offset = UNKNOWN_OFFSET;
3734 process_constraint (new_constraint (lhs, rhs));
3737 /* Temporary storage for fake var decls. */
3738 struct obstack fake_var_decl_obstack;
3740 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3742 static tree
3743 build_fake_var_decl (tree type)
3745 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl);
3746 memset (decl, 0, sizeof (struct tree_var_decl));
3747 TREE_SET_CODE (decl, VAR_DECL);
3748 TREE_TYPE (decl) = type;
3749 DECL_UID (decl) = allocate_decl_uid ();
3750 SET_DECL_PT_UID (decl, -1);
3751 layout_decl (decl, 0);
3752 return decl;
3755 /* Create a new artificial heap variable with NAME.
3756 Return the created variable. */
3758 static varinfo_t
3759 make_heapvar (const char *name)
3761 varinfo_t vi;
3762 tree heapvar;
3764 heapvar = build_fake_var_decl (ptr_type_node);
3765 DECL_EXTERNAL (heapvar) = 1;
3767 vi = new_var_info (heapvar, name);
3768 vi->is_artificial_var = true;
3769 vi->is_heap_var = true;
3770 vi->is_unknown_size_var = true;
3771 vi->offset = 0;
3772 vi->fullsize = ~0;
3773 vi->size = ~0;
3774 vi->is_full_var = true;
3775 insert_vi_for_tree (heapvar, vi);
3777 return vi;
3780 /* Create a new artificial heap variable with NAME and make a
3781 constraint from it to LHS. Set flags according to a tag used
3782 for tracking restrict pointers. */
3784 static varinfo_t
3785 make_constraint_from_restrict (varinfo_t lhs, const char *name)
3787 varinfo_t vi = make_heapvar (name);
3788 vi->is_global_var = 1;
3789 vi->may_have_pointers = 1;
3790 make_constraint_from (lhs, vi->id);
3791 return vi;
3794 /* Create a new artificial heap variable with NAME and make a
3795 constraint from it to LHS. Set flags according to a tag used
3796 for tracking restrict pointers and make the artificial heap
3797 point to global memory. */
3799 static varinfo_t
3800 make_constraint_from_global_restrict (varinfo_t lhs, const char *name)
3802 varinfo_t vi = make_constraint_from_restrict (lhs, name);
3803 make_copy_constraint (vi, nonlocal_id);
3804 return vi;
3807 /* In IPA mode there are varinfos for different aspects of reach
3808 function designator. One for the points-to set of the return
3809 value, one for the variables that are clobbered by the function,
3810 one for its uses and one for each parameter (including a single
3811 glob for remaining variadic arguments). */
3813 enum { fi_clobbers = 1, fi_uses = 2,
3814 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 };
3816 /* Get a constraint for the requested part of a function designator FI
3817 when operating in IPA mode. */
3819 static struct constraint_expr
3820 get_function_part_constraint (varinfo_t fi, unsigned part)
3822 struct constraint_expr c;
3824 gcc_assert (in_ipa_mode);
3826 if (fi->id == anything_id)
3828 /* ??? We probably should have a ANYFN special variable. */
3829 c.var = anything_id;
3830 c.offset = 0;
3831 c.type = SCALAR;
3833 else if (TREE_CODE (fi->decl) == FUNCTION_DECL)
3835 varinfo_t ai = first_vi_for_offset (fi, part);
3836 if (ai)
3837 c.var = ai->id;
3838 else
3839 c.var = anything_id;
3840 c.offset = 0;
3841 c.type = SCALAR;
3843 else
3845 c.var = fi->id;
3846 c.offset = part;
3847 c.type = DEREF;
3850 return c;
3853 /* For non-IPA mode, generate constraints necessary for a call on the
3854 RHS. */
3856 static void
3857 handle_rhs_call (gcall *stmt, vec<ce_s> *results)
3859 struct constraint_expr rhsc;
3860 unsigned i;
3861 bool returns_uses = false;
3863 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3865 tree arg = gimple_call_arg (stmt, i);
3866 int flags = gimple_call_arg_flags (stmt, i);
3868 /* If the argument is not used we can ignore it. */
3869 if (flags & EAF_UNUSED)
3870 continue;
3872 /* As we compute ESCAPED context-insensitive we do not gain
3873 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3874 set. The argument would still get clobbered through the
3875 escape solution. */
3876 if ((flags & EAF_NOCLOBBER)
3877 && (flags & EAF_NOESCAPE))
3879 varinfo_t uses = get_call_use_vi (stmt);
3880 if (!(flags & EAF_DIRECT))
3882 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3883 make_constraint_to (tem->id, arg);
3884 make_transitive_closure_constraints (tem);
3885 make_copy_constraint (uses, tem->id);
3887 else
3888 make_constraint_to (uses->id, arg);
3889 returns_uses = true;
3891 else if (flags & EAF_NOESCAPE)
3893 struct constraint_expr lhs, rhs;
3894 varinfo_t uses = get_call_use_vi (stmt);
3895 varinfo_t clobbers = get_call_clobber_vi (stmt);
3896 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3897 make_constraint_to (tem->id, arg);
3898 if (!(flags & EAF_DIRECT))
3899 make_transitive_closure_constraints (tem);
3900 make_copy_constraint (uses, tem->id);
3901 make_copy_constraint (clobbers, tem->id);
3902 /* Add *tem = nonlocal, do not add *tem = callused as
3903 EAF_NOESCAPE parameters do not escape to other parameters
3904 and all other uses appear in NONLOCAL as well. */
3905 lhs.type = DEREF;
3906 lhs.var = tem->id;
3907 lhs.offset = 0;
3908 rhs.type = SCALAR;
3909 rhs.var = nonlocal_id;
3910 rhs.offset = 0;
3911 process_constraint (new_constraint (lhs, rhs));
3912 returns_uses = true;
3914 else
3915 make_escape_constraint (arg);
3918 /* If we added to the calls uses solution make sure we account for
3919 pointers to it to be returned. */
3920 if (returns_uses)
3922 rhsc.var = get_call_use_vi (stmt)->id;
3923 rhsc.offset = 0;
3924 rhsc.type = SCALAR;
3925 results->safe_push (rhsc);
3928 /* The static chain escapes as well. */
3929 if (gimple_call_chain (stmt))
3930 make_escape_constraint (gimple_call_chain (stmt));
3932 /* And if we applied NRV the address of the return slot escapes as well. */
3933 if (gimple_call_return_slot_opt_p (stmt)
3934 && gimple_call_lhs (stmt) != NULL_TREE
3935 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3937 auto_vec<ce_s> tmpc;
3938 struct constraint_expr lhsc, *c;
3939 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3940 lhsc.var = escaped_id;
3941 lhsc.offset = 0;
3942 lhsc.type = SCALAR;
3943 FOR_EACH_VEC_ELT (tmpc, i, c)
3944 process_constraint (new_constraint (lhsc, *c));
3947 /* Regular functions return nonlocal memory. */
3948 rhsc.var = nonlocal_id;
3949 rhsc.offset = 0;
3950 rhsc.type = SCALAR;
3951 results->safe_push (rhsc);
3954 /* For non-IPA mode, generate constraints necessary for a call
3955 that returns a pointer and assigns it to LHS. This simply makes
3956 the LHS point to global and escaped variables. */
3958 static void
3959 handle_lhs_call (gcall *stmt, tree lhs, int flags, vec<ce_s> rhsc,
3960 tree fndecl)
3962 auto_vec<ce_s> lhsc;
3964 get_constraint_for (lhs, &lhsc);
3965 /* If the store is to a global decl make sure to
3966 add proper escape constraints. */
3967 lhs = get_base_address (lhs);
3968 if (lhs
3969 && DECL_P (lhs)
3970 && is_global_var (lhs))
3972 struct constraint_expr tmpc;
3973 tmpc.var = escaped_id;
3974 tmpc.offset = 0;
3975 tmpc.type = SCALAR;
3976 lhsc.safe_push (tmpc);
3979 /* If the call returns an argument unmodified override the rhs
3980 constraints. */
3981 if (flags & ERF_RETURNS_ARG
3982 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
3984 tree arg;
3985 rhsc.create (0);
3986 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
3987 get_constraint_for (arg, &rhsc);
3988 process_all_all_constraints (lhsc, rhsc);
3989 rhsc.release ();
3991 else if (flags & ERF_NOALIAS)
3993 varinfo_t vi;
3994 struct constraint_expr tmpc;
3995 rhsc.create (0);
3996 vi = make_heapvar ("HEAP");
3997 /* We are marking allocated storage local, we deal with it becoming
3998 global by escaping and setting of vars_contains_escaped_heap. */
3999 DECL_EXTERNAL (vi->decl) = 0;
4000 vi->is_global_var = 0;
4001 /* If this is not a real malloc call assume the memory was
4002 initialized and thus may point to global memory. All
4003 builtin functions with the malloc attribute behave in a sane way. */
4004 if (!fndecl
4005 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
4006 make_constraint_from (vi, nonlocal_id);
4007 tmpc.var = vi->id;
4008 tmpc.offset = 0;
4009 tmpc.type = ADDRESSOF;
4010 rhsc.safe_push (tmpc);
4011 process_all_all_constraints (lhsc, rhsc);
4012 rhsc.release ();
4014 else
4015 process_all_all_constraints (lhsc, rhsc);
4018 /* For non-IPA mode, generate constraints necessary for a call of a
4019 const function that returns a pointer in the statement STMT. */
4021 static void
4022 handle_const_call (gcall *stmt, vec<ce_s> *results)
4024 struct constraint_expr rhsc;
4025 unsigned int k;
4027 /* Treat nested const functions the same as pure functions as far
4028 as the static chain is concerned. */
4029 if (gimple_call_chain (stmt))
4031 varinfo_t uses = get_call_use_vi (stmt);
4032 make_transitive_closure_constraints (uses);
4033 make_constraint_to (uses->id, gimple_call_chain (stmt));
4034 rhsc.var = uses->id;
4035 rhsc.offset = 0;
4036 rhsc.type = SCALAR;
4037 results->safe_push (rhsc);
4040 /* May return arguments. */
4041 for (k = 0; k < gimple_call_num_args (stmt); ++k)
4043 tree arg = gimple_call_arg (stmt, k);
4044 auto_vec<ce_s> argc;
4045 unsigned i;
4046 struct constraint_expr *argp;
4047 get_constraint_for_rhs (arg, &argc);
4048 FOR_EACH_VEC_ELT (argc, i, argp)
4049 results->safe_push (*argp);
4052 /* May return addresses of globals. */
4053 rhsc.var = nonlocal_id;
4054 rhsc.offset = 0;
4055 rhsc.type = ADDRESSOF;
4056 results->safe_push (rhsc);
4059 /* For non-IPA mode, generate constraints necessary for a call to a
4060 pure function in statement STMT. */
4062 static void
4063 handle_pure_call (gcall *stmt, vec<ce_s> *results)
4065 struct constraint_expr rhsc;
4066 unsigned i;
4067 varinfo_t uses = NULL;
4069 /* Memory reached from pointer arguments is call-used. */
4070 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4072 tree arg = gimple_call_arg (stmt, i);
4073 if (!uses)
4075 uses = get_call_use_vi (stmt);
4076 make_transitive_closure_constraints (uses);
4078 make_constraint_to (uses->id, arg);
4081 /* The static chain is used as well. */
4082 if (gimple_call_chain (stmt))
4084 if (!uses)
4086 uses = get_call_use_vi (stmt);
4087 make_transitive_closure_constraints (uses);
4089 make_constraint_to (uses->id, gimple_call_chain (stmt));
4092 /* Pure functions may return call-used and nonlocal memory. */
4093 if (uses)
4095 rhsc.var = uses->id;
4096 rhsc.offset = 0;
4097 rhsc.type = SCALAR;
4098 results->safe_push (rhsc);
4100 rhsc.var = nonlocal_id;
4101 rhsc.offset = 0;
4102 rhsc.type = SCALAR;
4103 results->safe_push (rhsc);
4107 /* Return the varinfo for the callee of CALL. */
4109 static varinfo_t
4110 get_fi_for_callee (gcall *call)
4112 tree decl, fn = gimple_call_fn (call);
4114 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
4115 fn = OBJ_TYPE_REF_EXPR (fn);
4117 /* If we can directly resolve the function being called, do so.
4118 Otherwise, it must be some sort of indirect expression that
4119 we should still be able to handle. */
4120 decl = gimple_call_addr_fndecl (fn);
4121 if (decl)
4122 return get_vi_for_tree (decl);
4124 /* If the function is anything other than a SSA name pointer we have no
4125 clue and should be getting ANYFN (well, ANYTHING for now). */
4126 if (!fn || TREE_CODE (fn) != SSA_NAME)
4127 return get_varinfo (anything_id);
4129 if (SSA_NAME_IS_DEFAULT_DEF (fn)
4130 && (TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
4131 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL))
4132 fn = SSA_NAME_VAR (fn);
4134 return get_vi_for_tree (fn);
4137 /* Create constraints for the builtin call T. Return true if the call
4138 was handled, otherwise false. */
4140 static bool
4141 find_func_aliases_for_builtin_call (struct function *fn, gcall *t)
4143 tree fndecl = gimple_call_fndecl (t);
4144 auto_vec<ce_s, 2> lhsc;
4145 auto_vec<ce_s, 4> rhsc;
4146 varinfo_t fi;
4148 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4149 /* ??? All builtins that are handled here need to be handled
4150 in the alias-oracle query functions explicitly! */
4151 switch (DECL_FUNCTION_CODE (fndecl))
4153 /* All the following functions return a pointer to the same object
4154 as their first argument points to. The functions do not add
4155 to the ESCAPED solution. The functions make the first argument
4156 pointed to memory point to what the second argument pointed to
4157 memory points to. */
4158 case BUILT_IN_STRCPY:
4159 case BUILT_IN_STRNCPY:
4160 case BUILT_IN_BCOPY:
4161 case BUILT_IN_MEMCPY:
4162 case BUILT_IN_MEMMOVE:
4163 case BUILT_IN_MEMPCPY:
4164 case BUILT_IN_STPCPY:
4165 case BUILT_IN_STPNCPY:
4166 case BUILT_IN_STRCAT:
4167 case BUILT_IN_STRNCAT:
4168 case BUILT_IN_STRCPY_CHK:
4169 case BUILT_IN_STRNCPY_CHK:
4170 case BUILT_IN_MEMCPY_CHK:
4171 case BUILT_IN_MEMMOVE_CHK:
4172 case BUILT_IN_MEMPCPY_CHK:
4173 case BUILT_IN_STPCPY_CHK:
4174 case BUILT_IN_STPNCPY_CHK:
4175 case BUILT_IN_STRCAT_CHK:
4176 case BUILT_IN_STRNCAT_CHK:
4177 case BUILT_IN_TM_MEMCPY:
4178 case BUILT_IN_TM_MEMMOVE:
4180 tree res = gimple_call_lhs (t);
4181 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4182 == BUILT_IN_BCOPY ? 1 : 0));
4183 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4184 == BUILT_IN_BCOPY ? 0 : 1));
4185 if (res != NULL_TREE)
4187 get_constraint_for (res, &lhsc);
4188 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4189 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
4190 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4191 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
4192 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK
4193 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK)
4194 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4195 else
4196 get_constraint_for (dest, &rhsc);
4197 process_all_all_constraints (lhsc, rhsc);
4198 lhsc.truncate (0);
4199 rhsc.truncate (0);
4201 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4202 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4203 do_deref (&lhsc);
4204 do_deref (&rhsc);
4205 process_all_all_constraints (lhsc, rhsc);
4206 return true;
4208 case BUILT_IN_MEMSET:
4209 case BUILT_IN_MEMSET_CHK:
4210 case BUILT_IN_TM_MEMSET:
4212 tree res = gimple_call_lhs (t);
4213 tree dest = gimple_call_arg (t, 0);
4214 unsigned i;
4215 ce_s *lhsp;
4216 struct constraint_expr ac;
4217 if (res != NULL_TREE)
4219 get_constraint_for (res, &lhsc);
4220 get_constraint_for (dest, &rhsc);
4221 process_all_all_constraints (lhsc, rhsc);
4222 lhsc.truncate (0);
4224 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4225 do_deref (&lhsc);
4226 if (flag_delete_null_pointer_checks
4227 && integer_zerop (gimple_call_arg (t, 1)))
4229 ac.type = ADDRESSOF;
4230 ac.var = nothing_id;
4232 else
4234 ac.type = SCALAR;
4235 ac.var = integer_id;
4237 ac.offset = 0;
4238 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4239 process_constraint (new_constraint (*lhsp, ac));
4240 return true;
4242 case BUILT_IN_POSIX_MEMALIGN:
4244 tree ptrptr = gimple_call_arg (t, 0);
4245 get_constraint_for (ptrptr, &lhsc);
4246 do_deref (&lhsc);
4247 varinfo_t vi = make_heapvar ("HEAP");
4248 /* We are marking allocated storage local, we deal with it becoming
4249 global by escaping and setting of vars_contains_escaped_heap. */
4250 DECL_EXTERNAL (vi->decl) = 0;
4251 vi->is_global_var = 0;
4252 struct constraint_expr tmpc;
4253 tmpc.var = vi->id;
4254 tmpc.offset = 0;
4255 tmpc.type = ADDRESSOF;
4256 rhsc.safe_push (tmpc);
4257 process_all_all_constraints (lhsc, rhsc);
4258 return true;
4260 case BUILT_IN_ASSUME_ALIGNED:
4262 tree res = gimple_call_lhs (t);
4263 tree dest = gimple_call_arg (t, 0);
4264 if (res != NULL_TREE)
4266 get_constraint_for (res, &lhsc);
4267 get_constraint_for (dest, &rhsc);
4268 process_all_all_constraints (lhsc, rhsc);
4270 return true;
4272 /* All the following functions do not return pointers, do not
4273 modify the points-to sets of memory reachable from their
4274 arguments and do not add to the ESCAPED solution. */
4275 case BUILT_IN_SINCOS:
4276 case BUILT_IN_SINCOSF:
4277 case BUILT_IN_SINCOSL:
4278 case BUILT_IN_FREXP:
4279 case BUILT_IN_FREXPF:
4280 case BUILT_IN_FREXPL:
4281 case BUILT_IN_GAMMA_R:
4282 case BUILT_IN_GAMMAF_R:
4283 case BUILT_IN_GAMMAL_R:
4284 case BUILT_IN_LGAMMA_R:
4285 case BUILT_IN_LGAMMAF_R:
4286 case BUILT_IN_LGAMMAL_R:
4287 case BUILT_IN_MODF:
4288 case BUILT_IN_MODFF:
4289 case BUILT_IN_MODFL:
4290 case BUILT_IN_REMQUO:
4291 case BUILT_IN_REMQUOF:
4292 case BUILT_IN_REMQUOL:
4293 case BUILT_IN_FREE:
4294 return true;
4295 case BUILT_IN_STRDUP:
4296 case BUILT_IN_STRNDUP:
4297 case BUILT_IN_REALLOC:
4298 if (gimple_call_lhs (t))
4300 handle_lhs_call (t, gimple_call_lhs (t),
4301 gimple_call_return_flags (t) | ERF_NOALIAS,
4302 vNULL, fndecl);
4303 get_constraint_for_ptr_offset (gimple_call_lhs (t),
4304 NULL_TREE, &lhsc);
4305 get_constraint_for_ptr_offset (gimple_call_arg (t, 0),
4306 NULL_TREE, &rhsc);
4307 do_deref (&lhsc);
4308 do_deref (&rhsc);
4309 process_all_all_constraints (lhsc, rhsc);
4310 lhsc.truncate (0);
4311 rhsc.truncate (0);
4312 /* For realloc the resulting pointer can be equal to the
4313 argument as well. But only doing this wouldn't be
4314 correct because with ptr == 0 realloc behaves like malloc. */
4315 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_REALLOC)
4317 get_constraint_for (gimple_call_lhs (t), &lhsc);
4318 get_constraint_for (gimple_call_arg (t, 0), &rhsc);
4319 process_all_all_constraints (lhsc, rhsc);
4321 return true;
4323 break;
4324 /* String / character search functions return a pointer into the
4325 source string or NULL. */
4326 case BUILT_IN_INDEX:
4327 case BUILT_IN_STRCHR:
4328 case BUILT_IN_STRRCHR:
4329 case BUILT_IN_MEMCHR:
4330 case BUILT_IN_STRSTR:
4331 case BUILT_IN_STRPBRK:
4332 if (gimple_call_lhs (t))
4334 tree src = gimple_call_arg (t, 0);
4335 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4336 constraint_expr nul;
4337 nul.var = nothing_id;
4338 nul.offset = 0;
4339 nul.type = ADDRESSOF;
4340 rhsc.safe_push (nul);
4341 get_constraint_for (gimple_call_lhs (t), &lhsc);
4342 process_all_all_constraints (lhsc, rhsc);
4344 return true;
4345 /* Trampolines are special - they set up passing the static
4346 frame. */
4347 case BUILT_IN_INIT_TRAMPOLINE:
4349 tree tramp = gimple_call_arg (t, 0);
4350 tree nfunc = gimple_call_arg (t, 1);
4351 tree frame = gimple_call_arg (t, 2);
4352 unsigned i;
4353 struct constraint_expr lhs, *rhsp;
4354 if (in_ipa_mode)
4356 varinfo_t nfi = NULL;
4357 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR);
4358 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0));
4359 if (nfi)
4361 lhs = get_function_part_constraint (nfi, fi_static_chain);
4362 get_constraint_for (frame, &rhsc);
4363 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4364 process_constraint (new_constraint (lhs, *rhsp));
4365 rhsc.truncate (0);
4367 /* Make the frame point to the function for
4368 the trampoline adjustment call. */
4369 get_constraint_for (tramp, &lhsc);
4370 do_deref (&lhsc);
4371 get_constraint_for (nfunc, &rhsc);
4372 process_all_all_constraints (lhsc, rhsc);
4374 return true;
4377 /* Else fallthru to generic handling which will let
4378 the frame escape. */
4379 break;
4381 case BUILT_IN_ADJUST_TRAMPOLINE:
4383 tree tramp = gimple_call_arg (t, 0);
4384 tree res = gimple_call_lhs (t);
4385 if (in_ipa_mode && res)
4387 get_constraint_for (res, &lhsc);
4388 get_constraint_for (tramp, &rhsc);
4389 do_deref (&rhsc);
4390 process_all_all_constraints (lhsc, rhsc);
4392 return true;
4394 CASE_BUILT_IN_TM_STORE (1):
4395 CASE_BUILT_IN_TM_STORE (2):
4396 CASE_BUILT_IN_TM_STORE (4):
4397 CASE_BUILT_IN_TM_STORE (8):
4398 CASE_BUILT_IN_TM_STORE (FLOAT):
4399 CASE_BUILT_IN_TM_STORE (DOUBLE):
4400 CASE_BUILT_IN_TM_STORE (LDOUBLE):
4401 CASE_BUILT_IN_TM_STORE (M64):
4402 CASE_BUILT_IN_TM_STORE (M128):
4403 CASE_BUILT_IN_TM_STORE (M256):
4405 tree addr = gimple_call_arg (t, 0);
4406 tree src = gimple_call_arg (t, 1);
4408 get_constraint_for (addr, &lhsc);
4409 do_deref (&lhsc);
4410 get_constraint_for (src, &rhsc);
4411 process_all_all_constraints (lhsc, rhsc);
4412 return true;
4414 CASE_BUILT_IN_TM_LOAD (1):
4415 CASE_BUILT_IN_TM_LOAD (2):
4416 CASE_BUILT_IN_TM_LOAD (4):
4417 CASE_BUILT_IN_TM_LOAD (8):
4418 CASE_BUILT_IN_TM_LOAD (FLOAT):
4419 CASE_BUILT_IN_TM_LOAD (DOUBLE):
4420 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
4421 CASE_BUILT_IN_TM_LOAD (M64):
4422 CASE_BUILT_IN_TM_LOAD (M128):
4423 CASE_BUILT_IN_TM_LOAD (M256):
4425 tree dest = gimple_call_lhs (t);
4426 tree addr = gimple_call_arg (t, 0);
4428 get_constraint_for (dest, &lhsc);
4429 get_constraint_for (addr, &rhsc);
4430 do_deref (&rhsc);
4431 process_all_all_constraints (lhsc, rhsc);
4432 return true;
4434 /* Variadic argument handling needs to be handled in IPA
4435 mode as well. */
4436 case BUILT_IN_VA_START:
4438 tree valist = gimple_call_arg (t, 0);
4439 struct constraint_expr rhs, *lhsp;
4440 unsigned i;
4441 get_constraint_for (valist, &lhsc);
4442 do_deref (&lhsc);
4443 /* The va_list gets access to pointers in variadic
4444 arguments. Which we know in the case of IPA analysis
4445 and otherwise are just all nonlocal variables. */
4446 if (in_ipa_mode)
4448 fi = lookup_vi_for_tree (fn->decl);
4449 rhs = get_function_part_constraint (fi, ~0);
4450 rhs.type = ADDRESSOF;
4452 else
4454 rhs.var = nonlocal_id;
4455 rhs.type = ADDRESSOF;
4456 rhs.offset = 0;
4458 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4459 process_constraint (new_constraint (*lhsp, rhs));
4460 /* va_list is clobbered. */
4461 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4462 return true;
4464 /* va_end doesn't have any effect that matters. */
4465 case BUILT_IN_VA_END:
4466 return true;
4467 /* Alternate return. Simply give up for now. */
4468 case BUILT_IN_RETURN:
4470 fi = NULL;
4471 if (!in_ipa_mode
4472 || !(fi = get_vi_for_tree (fn->decl)))
4473 make_constraint_from (get_varinfo (escaped_id), anything_id);
4474 else if (in_ipa_mode
4475 && fi != NULL)
4477 struct constraint_expr lhs, rhs;
4478 lhs = get_function_part_constraint (fi, fi_result);
4479 rhs.var = anything_id;
4480 rhs.offset = 0;
4481 rhs.type = SCALAR;
4482 process_constraint (new_constraint (lhs, rhs));
4484 return true;
4486 /* printf-style functions may have hooks to set pointers to
4487 point to somewhere into the generated string. Leave them
4488 for a later exercise... */
4489 default:
4490 /* Fallthru to general call handling. */;
4493 return false;
4496 /* Create constraints for the call T. */
4498 static void
4499 find_func_aliases_for_call (struct function *fn, gcall *t)
4501 tree fndecl = gimple_call_fndecl (t);
4502 varinfo_t fi;
4504 if (fndecl != NULL_TREE
4505 && DECL_BUILT_IN (fndecl)
4506 && find_func_aliases_for_builtin_call (fn, t))
4507 return;
4509 fi = get_fi_for_callee (t);
4510 if (!in_ipa_mode
4511 || (fndecl && !fi->is_fn_info))
4513 auto_vec<ce_s, 16> rhsc;
4514 int flags = gimple_call_flags (t);
4516 /* Const functions can return their arguments and addresses
4517 of global memory but not of escaped memory. */
4518 if (flags & (ECF_CONST|ECF_NOVOPS))
4520 if (gimple_call_lhs (t))
4521 handle_const_call (t, &rhsc);
4523 /* Pure functions can return addresses in and of memory
4524 reachable from their arguments, but they are not an escape
4525 point for reachable memory of their arguments. */
4526 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4527 handle_pure_call (t, &rhsc);
4528 else
4529 handle_rhs_call (t, &rhsc);
4530 if (gimple_call_lhs (t))
4531 handle_lhs_call (t, gimple_call_lhs (t),
4532 gimple_call_return_flags (t), rhsc, fndecl);
4534 else
4536 auto_vec<ce_s, 2> rhsc;
4537 tree lhsop;
4538 unsigned j;
4540 /* Assign all the passed arguments to the appropriate incoming
4541 parameters of the function. */
4542 for (j = 0; j < gimple_call_num_args (t); j++)
4544 struct constraint_expr lhs ;
4545 struct constraint_expr *rhsp;
4546 tree arg = gimple_call_arg (t, j);
4548 get_constraint_for_rhs (arg, &rhsc);
4549 lhs = get_function_part_constraint (fi, fi_parm_base + j);
4550 while (rhsc.length () != 0)
4552 rhsp = &rhsc.last ();
4553 process_constraint (new_constraint (lhs, *rhsp));
4554 rhsc.pop ();
4558 /* If we are returning a value, assign it to the result. */
4559 lhsop = gimple_call_lhs (t);
4560 if (lhsop)
4562 auto_vec<ce_s, 2> lhsc;
4563 struct constraint_expr rhs;
4564 struct constraint_expr *lhsp;
4566 get_constraint_for (lhsop, &lhsc);
4567 rhs = get_function_part_constraint (fi, fi_result);
4568 if (fndecl
4569 && DECL_RESULT (fndecl)
4570 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4572 auto_vec<ce_s, 2> tem;
4573 tem.quick_push (rhs);
4574 do_deref (&tem);
4575 gcc_checking_assert (tem.length () == 1);
4576 rhs = tem[0];
4578 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4579 process_constraint (new_constraint (*lhsp, rhs));
4582 /* If we pass the result decl by reference, honor that. */
4583 if (lhsop
4584 && fndecl
4585 && DECL_RESULT (fndecl)
4586 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4588 struct constraint_expr lhs;
4589 struct constraint_expr *rhsp;
4591 get_constraint_for_address_of (lhsop, &rhsc);
4592 lhs = get_function_part_constraint (fi, fi_result);
4593 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4594 process_constraint (new_constraint (lhs, *rhsp));
4595 rhsc.truncate (0);
4598 /* If we use a static chain, pass it along. */
4599 if (gimple_call_chain (t))
4601 struct constraint_expr lhs;
4602 struct constraint_expr *rhsp;
4604 get_constraint_for (gimple_call_chain (t), &rhsc);
4605 lhs = get_function_part_constraint (fi, fi_static_chain);
4606 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4607 process_constraint (new_constraint (lhs, *rhsp));
4612 /* Walk statement T setting up aliasing constraints according to the
4613 references found in T. This function is the main part of the
4614 constraint builder. AI points to auxiliary alias information used
4615 when building alias sets and computing alias grouping heuristics. */
4617 static void
4618 find_func_aliases (struct function *fn, gimple origt)
4620 gimple t = origt;
4621 auto_vec<ce_s, 16> lhsc;
4622 auto_vec<ce_s, 16> rhsc;
4623 struct constraint_expr *c;
4624 varinfo_t fi;
4626 /* Now build constraints expressions. */
4627 if (gimple_code (t) == GIMPLE_PHI)
4629 size_t i;
4630 unsigned int j;
4632 /* For a phi node, assign all the arguments to
4633 the result. */
4634 get_constraint_for (gimple_phi_result (t), &lhsc);
4635 for (i = 0; i < gimple_phi_num_args (t); i++)
4637 tree strippedrhs = PHI_ARG_DEF (t, i);
4639 STRIP_NOPS (strippedrhs);
4640 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
4642 FOR_EACH_VEC_ELT (lhsc, j, c)
4644 struct constraint_expr *c2;
4645 while (rhsc.length () > 0)
4647 c2 = &rhsc.last ();
4648 process_constraint (new_constraint (*c, *c2));
4649 rhsc.pop ();
4654 /* In IPA mode, we need to generate constraints to pass call
4655 arguments through their calls. There are two cases,
4656 either a GIMPLE_CALL returning a value, or just a plain
4657 GIMPLE_CALL when we are not.
4659 In non-ipa mode, we need to generate constraints for each
4660 pointer passed by address. */
4661 else if (is_gimple_call (t))
4662 find_func_aliases_for_call (fn, as_a <gcall *> (t));
4664 /* Otherwise, just a regular assignment statement. Only care about
4665 operations with pointer result, others are dealt with as escape
4666 points if they have pointer operands. */
4667 else if (is_gimple_assign (t))
4669 /* Otherwise, just a regular assignment statement. */
4670 tree lhsop = gimple_assign_lhs (t);
4671 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
4673 if (rhsop && TREE_CLOBBER_P (rhsop))
4674 /* Ignore clobbers, they don't actually store anything into
4675 the LHS. */
4677 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
4678 do_structure_copy (lhsop, rhsop);
4679 else
4681 enum tree_code code = gimple_assign_rhs_code (t);
4683 get_constraint_for (lhsop, &lhsc);
4685 if (FLOAT_TYPE_P (TREE_TYPE (lhsop)))
4686 /* If the operation produces a floating point result then
4687 assume the value is not produced to transfer a pointer. */
4689 else if (code == POINTER_PLUS_EXPR)
4690 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4691 gimple_assign_rhs2 (t), &rhsc);
4692 else if (code == BIT_AND_EXPR
4693 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
4695 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4696 the pointer. Handle it by offsetting it by UNKNOWN. */
4697 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4698 NULL_TREE, &rhsc);
4700 else if ((CONVERT_EXPR_CODE_P (code)
4701 && !(POINTER_TYPE_P (gimple_expr_type (t))
4702 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
4703 || gimple_assign_single_p (t))
4704 get_constraint_for_rhs (rhsop, &rhsc);
4705 else if (code == COND_EXPR)
4707 /* The result is a merge of both COND_EXPR arms. */
4708 auto_vec<ce_s, 2> tmp;
4709 struct constraint_expr *rhsp;
4710 unsigned i;
4711 get_constraint_for_rhs (gimple_assign_rhs2 (t), &rhsc);
4712 get_constraint_for_rhs (gimple_assign_rhs3 (t), &tmp);
4713 FOR_EACH_VEC_ELT (tmp, i, rhsp)
4714 rhsc.safe_push (*rhsp);
4716 else if (truth_value_p (code))
4717 /* Truth value results are not pointer (parts). Or at least
4718 very very unreasonable obfuscation of a part. */
4720 else
4722 /* All other operations are merges. */
4723 auto_vec<ce_s, 4> tmp;
4724 struct constraint_expr *rhsp;
4725 unsigned i, j;
4726 get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc);
4727 for (i = 2; i < gimple_num_ops (t); ++i)
4729 get_constraint_for_rhs (gimple_op (t, i), &tmp);
4730 FOR_EACH_VEC_ELT (tmp, j, rhsp)
4731 rhsc.safe_push (*rhsp);
4732 tmp.truncate (0);
4735 process_all_all_constraints (lhsc, rhsc);
4737 /* If there is a store to a global variable the rhs escapes. */
4738 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
4739 && DECL_P (lhsop)
4740 && is_global_var (lhsop)
4741 && (!in_ipa_mode
4742 || DECL_EXTERNAL (lhsop) || TREE_PUBLIC (lhsop)))
4743 make_escape_constraint (rhsop);
4745 /* Handle escapes through return. */
4746 else if (gimple_code (t) == GIMPLE_RETURN
4747 && gimple_return_retval (as_a <greturn *> (t)) != NULL_TREE)
4749 greturn *return_stmt = as_a <greturn *> (t);
4750 fi = NULL;
4751 if (!in_ipa_mode
4752 || !(fi = get_vi_for_tree (fn->decl)))
4753 make_escape_constraint (gimple_return_retval (return_stmt));
4754 else if (in_ipa_mode
4755 && fi != NULL)
4757 struct constraint_expr lhs ;
4758 struct constraint_expr *rhsp;
4759 unsigned i;
4761 lhs = get_function_part_constraint (fi, fi_result);
4762 get_constraint_for_rhs (gimple_return_retval (return_stmt), &rhsc);
4763 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4764 process_constraint (new_constraint (lhs, *rhsp));
4767 /* Handle asms conservatively by adding escape constraints to everything. */
4768 else if (gasm *asm_stmt = dyn_cast <gasm *> (t))
4770 unsigned i, noutputs;
4771 const char **oconstraints;
4772 const char *constraint;
4773 bool allows_mem, allows_reg, is_inout;
4775 noutputs = gimple_asm_noutputs (asm_stmt);
4776 oconstraints = XALLOCAVEC (const char *, noutputs);
4778 for (i = 0; i < noutputs; ++i)
4780 tree link = gimple_asm_output_op (asm_stmt, i);
4781 tree op = TREE_VALUE (link);
4783 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4784 oconstraints[i] = constraint;
4785 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4786 &allows_reg, &is_inout);
4788 /* A memory constraint makes the address of the operand escape. */
4789 if (!allows_reg && allows_mem)
4790 make_escape_constraint (build_fold_addr_expr (op));
4792 /* The asm may read global memory, so outputs may point to
4793 any global memory. */
4794 if (op)
4796 auto_vec<ce_s, 2> lhsc;
4797 struct constraint_expr rhsc, *lhsp;
4798 unsigned j;
4799 get_constraint_for (op, &lhsc);
4800 rhsc.var = nonlocal_id;
4801 rhsc.offset = 0;
4802 rhsc.type = SCALAR;
4803 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4804 process_constraint (new_constraint (*lhsp, rhsc));
4807 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
4809 tree link = gimple_asm_input_op (asm_stmt, i);
4810 tree op = TREE_VALUE (link);
4812 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4814 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4815 &allows_mem, &allows_reg);
4817 /* A memory constraint makes the address of the operand escape. */
4818 if (!allows_reg && allows_mem)
4819 make_escape_constraint (build_fold_addr_expr (op));
4820 /* Strictly we'd only need the constraint to ESCAPED if
4821 the asm clobbers memory, otherwise using something
4822 along the lines of per-call clobbers/uses would be enough. */
4823 else if (op)
4824 make_escape_constraint (op);
4830 /* Create a constraint adding to the clobber set of FI the memory
4831 pointed to by PTR. */
4833 static void
4834 process_ipa_clobber (varinfo_t fi, tree ptr)
4836 vec<ce_s> ptrc = vNULL;
4837 struct constraint_expr *c, lhs;
4838 unsigned i;
4839 get_constraint_for_rhs (ptr, &ptrc);
4840 lhs = get_function_part_constraint (fi, fi_clobbers);
4841 FOR_EACH_VEC_ELT (ptrc, i, c)
4842 process_constraint (new_constraint (lhs, *c));
4843 ptrc.release ();
4846 /* Walk statement T setting up clobber and use constraints according to the
4847 references found in T. This function is a main part of the
4848 IPA constraint builder. */
4850 static void
4851 find_func_clobbers (struct function *fn, gimple origt)
4853 gimple t = origt;
4854 auto_vec<ce_s, 16> lhsc;
4855 auto_vec<ce_s, 16> rhsc;
4856 varinfo_t fi;
4858 /* Add constraints for clobbered/used in IPA mode.
4859 We are not interested in what automatic variables are clobbered
4860 or used as we only use the information in the caller to which
4861 they do not escape. */
4862 gcc_assert (in_ipa_mode);
4864 /* If the stmt refers to memory in any way it better had a VUSE. */
4865 if (gimple_vuse (t) == NULL_TREE)
4866 return;
4868 /* We'd better have function information for the current function. */
4869 fi = lookup_vi_for_tree (fn->decl);
4870 gcc_assert (fi != NULL);
4872 /* Account for stores in assignments and calls. */
4873 if (gimple_vdef (t) != NULL_TREE
4874 && gimple_has_lhs (t))
4876 tree lhs = gimple_get_lhs (t);
4877 tree tem = lhs;
4878 while (handled_component_p (tem))
4879 tem = TREE_OPERAND (tem, 0);
4880 if ((DECL_P (tem)
4881 && !auto_var_in_fn_p (tem, fn->decl))
4882 || INDIRECT_REF_P (tem)
4883 || (TREE_CODE (tem) == MEM_REF
4884 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4885 && auto_var_in_fn_p
4886 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl))))
4888 struct constraint_expr lhsc, *rhsp;
4889 unsigned i;
4890 lhsc = get_function_part_constraint (fi, fi_clobbers);
4891 get_constraint_for_address_of (lhs, &rhsc);
4892 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4893 process_constraint (new_constraint (lhsc, *rhsp));
4894 rhsc.truncate (0);
4898 /* Account for uses in assigments and returns. */
4899 if (gimple_assign_single_p (t)
4900 || (gimple_code (t) == GIMPLE_RETURN
4901 && gimple_return_retval (as_a <greturn *> (t)) != NULL_TREE))
4903 tree rhs = (gimple_assign_single_p (t)
4904 ? gimple_assign_rhs1 (t)
4905 : gimple_return_retval (as_a <greturn *> (t)));
4906 tree tem = rhs;
4907 while (handled_component_p (tem))
4908 tem = TREE_OPERAND (tem, 0);
4909 if ((DECL_P (tem)
4910 && !auto_var_in_fn_p (tem, fn->decl))
4911 || INDIRECT_REF_P (tem)
4912 || (TREE_CODE (tem) == MEM_REF
4913 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4914 && auto_var_in_fn_p
4915 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl))))
4917 struct constraint_expr lhs, *rhsp;
4918 unsigned i;
4919 lhs = get_function_part_constraint (fi, fi_uses);
4920 get_constraint_for_address_of (rhs, &rhsc);
4921 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4922 process_constraint (new_constraint (lhs, *rhsp));
4923 rhsc.truncate (0);
4927 if (gcall *call_stmt = dyn_cast <gcall *> (t))
4929 varinfo_t cfi = NULL;
4930 tree decl = gimple_call_fndecl (t);
4931 struct constraint_expr lhs, rhs;
4932 unsigned i, j;
4934 /* For builtins we do not have separate function info. For those
4935 we do not generate escapes for we have to generate clobbers/uses. */
4936 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4937 switch (DECL_FUNCTION_CODE (decl))
4939 /* The following functions use and clobber memory pointed to
4940 by their arguments. */
4941 case BUILT_IN_STRCPY:
4942 case BUILT_IN_STRNCPY:
4943 case BUILT_IN_BCOPY:
4944 case BUILT_IN_MEMCPY:
4945 case BUILT_IN_MEMMOVE:
4946 case BUILT_IN_MEMPCPY:
4947 case BUILT_IN_STPCPY:
4948 case BUILT_IN_STPNCPY:
4949 case BUILT_IN_STRCAT:
4950 case BUILT_IN_STRNCAT:
4951 case BUILT_IN_STRCPY_CHK:
4952 case BUILT_IN_STRNCPY_CHK:
4953 case BUILT_IN_MEMCPY_CHK:
4954 case BUILT_IN_MEMMOVE_CHK:
4955 case BUILT_IN_MEMPCPY_CHK:
4956 case BUILT_IN_STPCPY_CHK:
4957 case BUILT_IN_STPNCPY_CHK:
4958 case BUILT_IN_STRCAT_CHK:
4959 case BUILT_IN_STRNCAT_CHK:
4961 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4962 == BUILT_IN_BCOPY ? 1 : 0));
4963 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4964 == BUILT_IN_BCOPY ? 0 : 1));
4965 unsigned i;
4966 struct constraint_expr *rhsp, *lhsp;
4967 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4968 lhs = get_function_part_constraint (fi, fi_clobbers);
4969 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4970 process_constraint (new_constraint (lhs, *lhsp));
4971 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4972 lhs = get_function_part_constraint (fi, fi_uses);
4973 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4974 process_constraint (new_constraint (lhs, *rhsp));
4975 return;
4977 /* The following function clobbers memory pointed to by
4978 its argument. */
4979 case BUILT_IN_MEMSET:
4980 case BUILT_IN_MEMSET_CHK:
4981 case BUILT_IN_POSIX_MEMALIGN:
4983 tree dest = gimple_call_arg (t, 0);
4984 unsigned i;
4985 ce_s *lhsp;
4986 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4987 lhs = get_function_part_constraint (fi, fi_clobbers);
4988 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4989 process_constraint (new_constraint (lhs, *lhsp));
4990 return;
4992 /* The following functions clobber their second and third
4993 arguments. */
4994 case BUILT_IN_SINCOS:
4995 case BUILT_IN_SINCOSF:
4996 case BUILT_IN_SINCOSL:
4998 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4999 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5000 return;
5002 /* The following functions clobber their second argument. */
5003 case BUILT_IN_FREXP:
5004 case BUILT_IN_FREXPF:
5005 case BUILT_IN_FREXPL:
5006 case BUILT_IN_LGAMMA_R:
5007 case BUILT_IN_LGAMMAF_R:
5008 case BUILT_IN_LGAMMAL_R:
5009 case BUILT_IN_GAMMA_R:
5010 case BUILT_IN_GAMMAF_R:
5011 case BUILT_IN_GAMMAL_R:
5012 case BUILT_IN_MODF:
5013 case BUILT_IN_MODFF:
5014 case BUILT_IN_MODFL:
5016 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5017 return;
5019 /* The following functions clobber their third argument. */
5020 case BUILT_IN_REMQUO:
5021 case BUILT_IN_REMQUOF:
5022 case BUILT_IN_REMQUOL:
5024 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5025 return;
5027 /* The following functions neither read nor clobber memory. */
5028 case BUILT_IN_ASSUME_ALIGNED:
5029 case BUILT_IN_FREE:
5030 return;
5031 /* Trampolines are of no interest to us. */
5032 case BUILT_IN_INIT_TRAMPOLINE:
5033 case BUILT_IN_ADJUST_TRAMPOLINE:
5034 return;
5035 case BUILT_IN_VA_START:
5036 case BUILT_IN_VA_END:
5037 return;
5038 /* printf-style functions may have hooks to set pointers to
5039 point to somewhere into the generated string. Leave them
5040 for a later exercise... */
5041 default:
5042 /* Fallthru to general call handling. */;
5045 /* Parameters passed by value are used. */
5046 lhs = get_function_part_constraint (fi, fi_uses);
5047 for (i = 0; i < gimple_call_num_args (t); i++)
5049 struct constraint_expr *rhsp;
5050 tree arg = gimple_call_arg (t, i);
5052 if (TREE_CODE (arg) == SSA_NAME
5053 || is_gimple_min_invariant (arg))
5054 continue;
5056 get_constraint_for_address_of (arg, &rhsc);
5057 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5058 process_constraint (new_constraint (lhs, *rhsp));
5059 rhsc.truncate (0);
5062 /* Build constraints for propagating clobbers/uses along the
5063 callgraph edges. */
5064 cfi = get_fi_for_callee (call_stmt);
5065 if (cfi->id == anything_id)
5067 if (gimple_vdef (t))
5068 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5069 anything_id);
5070 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5071 anything_id);
5072 return;
5075 /* For callees without function info (that's external functions),
5076 ESCAPED is clobbered and used. */
5077 if (gimple_call_fndecl (t)
5078 && !cfi->is_fn_info)
5080 varinfo_t vi;
5082 if (gimple_vdef (t))
5083 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5084 escaped_id);
5085 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
5087 /* Also honor the call statement use/clobber info. */
5088 if ((vi = lookup_call_clobber_vi (call_stmt)) != NULL)
5089 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5090 vi->id);
5091 if ((vi = lookup_call_use_vi (call_stmt)) != NULL)
5092 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
5093 vi->id);
5094 return;
5097 /* Otherwise the caller clobbers and uses what the callee does.
5098 ??? This should use a new complex constraint that filters
5099 local variables of the callee. */
5100 if (gimple_vdef (t))
5102 lhs = get_function_part_constraint (fi, fi_clobbers);
5103 rhs = get_function_part_constraint (cfi, fi_clobbers);
5104 process_constraint (new_constraint (lhs, rhs));
5106 lhs = get_function_part_constraint (fi, fi_uses);
5107 rhs = get_function_part_constraint (cfi, fi_uses);
5108 process_constraint (new_constraint (lhs, rhs));
5110 else if (gimple_code (t) == GIMPLE_ASM)
5112 /* ??? Ick. We can do better. */
5113 if (gimple_vdef (t))
5114 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5115 anything_id);
5116 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5117 anything_id);
5122 /* Find the first varinfo in the same variable as START that overlaps with
5123 OFFSET. Return NULL if we can't find one. */
5125 static varinfo_t
5126 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
5128 /* If the offset is outside of the variable, bail out. */
5129 if (offset >= start->fullsize)
5130 return NULL;
5132 /* If we cannot reach offset from start, lookup the first field
5133 and start from there. */
5134 if (start->offset > offset)
5135 start = get_varinfo (start->head);
5137 while (start)
5139 /* We may not find a variable in the field list with the actual
5140 offset when when we have glommed a structure to a variable.
5141 In that case, however, offset should still be within the size
5142 of the variable. */
5143 if (offset >= start->offset
5144 && (offset - start->offset) < start->size)
5145 return start;
5147 start = vi_next (start);
5150 return NULL;
5153 /* Find the first varinfo in the same variable as START that overlaps with
5154 OFFSET. If there is no such varinfo the varinfo directly preceding
5155 OFFSET is returned. */
5157 static varinfo_t
5158 first_or_preceding_vi_for_offset (varinfo_t start,
5159 unsigned HOST_WIDE_INT offset)
5161 /* If we cannot reach offset from start, lookup the first field
5162 and start from there. */
5163 if (start->offset > offset)
5164 start = get_varinfo (start->head);
5166 /* We may not find a variable in the field list with the actual
5167 offset when when we have glommed a structure to a variable.
5168 In that case, however, offset should still be within the size
5169 of the variable.
5170 If we got beyond the offset we look for return the field
5171 directly preceding offset which may be the last field. */
5172 while (start->next
5173 && offset >= start->offset
5174 && !((offset - start->offset) < start->size))
5175 start = vi_next (start);
5177 return start;
5181 /* This structure is used during pushing fields onto the fieldstack
5182 to track the offset of the field, since bitpos_of_field gives it
5183 relative to its immediate containing type, and we want it relative
5184 to the ultimate containing object. */
5186 struct fieldoff
5188 /* Offset from the base of the base containing object to this field. */
5189 HOST_WIDE_INT offset;
5191 /* Size, in bits, of the field. */
5192 unsigned HOST_WIDE_INT size;
5194 unsigned has_unknown_size : 1;
5196 unsigned must_have_pointers : 1;
5198 unsigned may_have_pointers : 1;
5200 unsigned only_restrict_pointers : 1;
5202 typedef struct fieldoff fieldoff_s;
5205 /* qsort comparison function for two fieldoff's PA and PB */
5207 static int
5208 fieldoff_compare (const void *pa, const void *pb)
5210 const fieldoff_s *foa = (const fieldoff_s *)pa;
5211 const fieldoff_s *fob = (const fieldoff_s *)pb;
5212 unsigned HOST_WIDE_INT foasize, fobsize;
5214 if (foa->offset < fob->offset)
5215 return -1;
5216 else if (foa->offset > fob->offset)
5217 return 1;
5219 foasize = foa->size;
5220 fobsize = fob->size;
5221 if (foasize < fobsize)
5222 return -1;
5223 else if (foasize > fobsize)
5224 return 1;
5225 return 0;
5228 /* Sort a fieldstack according to the field offset and sizes. */
5229 static void
5230 sort_fieldstack (vec<fieldoff_s> fieldstack)
5232 fieldstack.qsort (fieldoff_compare);
5235 /* Return true if T is a type that can have subvars. */
5237 static inline bool
5238 type_can_have_subvars (const_tree t)
5240 /* Aggregates without overlapping fields can have subvars. */
5241 return TREE_CODE (t) == RECORD_TYPE;
5244 /* Return true if V is a tree that we can have subvars for.
5245 Normally, this is any aggregate type. Also complex
5246 types which are not gimple registers can have subvars. */
5248 static inline bool
5249 var_can_have_subvars (const_tree v)
5251 /* Volatile variables should never have subvars. */
5252 if (TREE_THIS_VOLATILE (v))
5253 return false;
5255 /* Non decls or memory tags can never have subvars. */
5256 if (!DECL_P (v))
5257 return false;
5259 return type_can_have_subvars (TREE_TYPE (v));
5262 /* Return true if T is a type that does contain pointers. */
5264 static bool
5265 type_must_have_pointers (tree type)
5267 if (POINTER_TYPE_P (type))
5268 return true;
5270 if (TREE_CODE (type) == ARRAY_TYPE)
5271 return type_must_have_pointers (TREE_TYPE (type));
5273 /* A function or method can have pointers as arguments, so track
5274 those separately. */
5275 if (TREE_CODE (type) == FUNCTION_TYPE
5276 || TREE_CODE (type) == METHOD_TYPE)
5277 return true;
5279 return false;
5282 static bool
5283 field_must_have_pointers (tree t)
5285 return type_must_have_pointers (TREE_TYPE (t));
5288 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5289 the fields of TYPE onto fieldstack, recording their offsets along
5290 the way.
5292 OFFSET is used to keep track of the offset in this entire
5293 structure, rather than just the immediately containing structure.
5294 Returns false if the caller is supposed to handle the field we
5295 recursed for. */
5297 static bool
5298 push_fields_onto_fieldstack (tree type, vec<fieldoff_s> *fieldstack,
5299 HOST_WIDE_INT offset)
5301 tree field;
5302 bool empty_p = true;
5304 if (TREE_CODE (type) != RECORD_TYPE)
5305 return false;
5307 /* If the vector of fields is growing too big, bail out early.
5308 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5309 sure this fails. */
5310 if (fieldstack->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5311 return false;
5313 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5314 if (TREE_CODE (field) == FIELD_DECL)
5316 bool push = false;
5317 HOST_WIDE_INT foff = bitpos_of_field (field);
5319 if (!var_can_have_subvars (field)
5320 || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
5321 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5322 push = true;
5323 else if (!push_fields_onto_fieldstack
5324 (TREE_TYPE (field), fieldstack, offset + foff)
5325 && (DECL_SIZE (field)
5326 && !integer_zerop (DECL_SIZE (field))))
5327 /* Empty structures may have actual size, like in C++. So
5328 see if we didn't push any subfields and the size is
5329 nonzero, push the field onto the stack. */
5330 push = true;
5332 if (push)
5334 fieldoff_s *pair = NULL;
5335 bool has_unknown_size = false;
5336 bool must_have_pointers_p;
5338 if (!fieldstack->is_empty ())
5339 pair = &fieldstack->last ();
5341 /* If there isn't anything at offset zero, create sth. */
5342 if (!pair
5343 && offset + foff != 0)
5345 fieldoff_s e = {0, offset + foff, false, false, false, false};
5346 pair = fieldstack->safe_push (e);
5349 if (!DECL_SIZE (field)
5350 || !tree_fits_uhwi_p (DECL_SIZE (field)))
5351 has_unknown_size = true;
5353 /* If adjacent fields do not contain pointers merge them. */
5354 must_have_pointers_p = field_must_have_pointers (field);
5355 if (pair
5356 && !has_unknown_size
5357 && !must_have_pointers_p
5358 && !pair->must_have_pointers
5359 && !pair->has_unknown_size
5360 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5362 pair->size += tree_to_uhwi (DECL_SIZE (field));
5364 else
5366 fieldoff_s e;
5367 e.offset = offset + foff;
5368 e.has_unknown_size = has_unknown_size;
5369 if (!has_unknown_size)
5370 e.size = tree_to_uhwi (DECL_SIZE (field));
5371 else
5372 e.size = -1;
5373 e.must_have_pointers = must_have_pointers_p;
5374 e.may_have_pointers = true;
5375 e.only_restrict_pointers
5376 = (!has_unknown_size
5377 && POINTER_TYPE_P (TREE_TYPE (field))
5378 && TYPE_RESTRICT (TREE_TYPE (field)));
5379 fieldstack->safe_push (e);
5383 empty_p = false;
5386 return !empty_p;
5389 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5390 if it is a varargs function. */
5392 static unsigned int
5393 count_num_arguments (tree decl, bool *is_varargs)
5395 unsigned int num = 0;
5396 tree t;
5398 /* Capture named arguments for K&R functions. They do not
5399 have a prototype and thus no TYPE_ARG_TYPES. */
5400 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5401 ++num;
5403 /* Check if the function has variadic arguments. */
5404 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5405 if (TREE_VALUE (t) == void_type_node)
5406 break;
5407 if (!t)
5408 *is_varargs = true;
5410 return num;
5413 /* Creation function node for DECL, using NAME, and return the index
5414 of the variable we've created for the function. */
5416 static varinfo_t
5417 create_function_info_for (tree decl, const char *name)
5419 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5420 varinfo_t vi, prev_vi;
5421 tree arg;
5422 unsigned int i;
5423 bool is_varargs = false;
5424 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5426 /* Create the variable info. */
5428 vi = new_var_info (decl, name);
5429 vi->offset = 0;
5430 vi->size = 1;
5431 vi->fullsize = fi_parm_base + num_args;
5432 vi->is_fn_info = 1;
5433 vi->may_have_pointers = false;
5434 if (is_varargs)
5435 vi->fullsize = ~0;
5436 insert_vi_for_tree (vi->decl, vi);
5438 prev_vi = vi;
5440 /* Create a variable for things the function clobbers and one for
5441 things the function uses. */
5443 varinfo_t clobbervi, usevi;
5444 const char *newname;
5445 char *tempname;
5447 asprintf (&tempname, "%s.clobber", name);
5448 newname = ggc_strdup (tempname);
5449 free (tempname);
5451 clobbervi = new_var_info (NULL, newname);
5452 clobbervi->offset = fi_clobbers;
5453 clobbervi->size = 1;
5454 clobbervi->fullsize = vi->fullsize;
5455 clobbervi->is_full_var = true;
5456 clobbervi->is_global_var = false;
5457 gcc_assert (prev_vi->offset < clobbervi->offset);
5458 prev_vi->next = clobbervi->id;
5459 prev_vi = clobbervi;
5461 asprintf (&tempname, "%s.use", name);
5462 newname = ggc_strdup (tempname);
5463 free (tempname);
5465 usevi = new_var_info (NULL, newname);
5466 usevi->offset = fi_uses;
5467 usevi->size = 1;
5468 usevi->fullsize = vi->fullsize;
5469 usevi->is_full_var = true;
5470 usevi->is_global_var = false;
5471 gcc_assert (prev_vi->offset < usevi->offset);
5472 prev_vi->next = usevi->id;
5473 prev_vi = usevi;
5476 /* And one for the static chain. */
5477 if (fn->static_chain_decl != NULL_TREE)
5479 varinfo_t chainvi;
5480 const char *newname;
5481 char *tempname;
5483 asprintf (&tempname, "%s.chain", name);
5484 newname = ggc_strdup (tempname);
5485 free (tempname);
5487 chainvi = new_var_info (fn->static_chain_decl, newname);
5488 chainvi->offset = fi_static_chain;
5489 chainvi->size = 1;
5490 chainvi->fullsize = vi->fullsize;
5491 chainvi->is_full_var = true;
5492 chainvi->is_global_var = false;
5493 gcc_assert (prev_vi->offset < chainvi->offset);
5494 prev_vi->next = chainvi->id;
5495 prev_vi = chainvi;
5496 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5499 /* Create a variable for the return var. */
5500 if (DECL_RESULT (decl) != NULL
5501 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5503 varinfo_t resultvi;
5504 const char *newname;
5505 char *tempname;
5506 tree resultdecl = decl;
5508 if (DECL_RESULT (decl))
5509 resultdecl = DECL_RESULT (decl);
5511 asprintf (&tempname, "%s.result", name);
5512 newname = ggc_strdup (tempname);
5513 free (tempname);
5515 resultvi = new_var_info (resultdecl, newname);
5516 resultvi->offset = fi_result;
5517 resultvi->size = 1;
5518 resultvi->fullsize = vi->fullsize;
5519 resultvi->is_full_var = true;
5520 if (DECL_RESULT (decl))
5521 resultvi->may_have_pointers = true;
5522 gcc_assert (prev_vi->offset < resultvi->offset);
5523 prev_vi->next = resultvi->id;
5524 prev_vi = resultvi;
5525 if (DECL_RESULT (decl))
5526 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
5529 /* Set up variables for each argument. */
5530 arg = DECL_ARGUMENTS (decl);
5531 for (i = 0; i < num_args; i++)
5533 varinfo_t argvi;
5534 const char *newname;
5535 char *tempname;
5536 tree argdecl = decl;
5538 if (arg)
5539 argdecl = arg;
5541 asprintf (&tempname, "%s.arg%d", name, i);
5542 newname = ggc_strdup (tempname);
5543 free (tempname);
5545 argvi = new_var_info (argdecl, newname);
5546 argvi->offset = fi_parm_base + i;
5547 argvi->size = 1;
5548 argvi->is_full_var = true;
5549 argvi->fullsize = vi->fullsize;
5550 if (arg)
5551 argvi->may_have_pointers = true;
5552 gcc_assert (prev_vi->offset < argvi->offset);
5553 prev_vi->next = argvi->id;
5554 prev_vi = argvi;
5555 if (arg)
5557 insert_vi_for_tree (arg, argvi);
5558 arg = DECL_CHAIN (arg);
5562 /* Add one representative for all further args. */
5563 if (is_varargs)
5565 varinfo_t argvi;
5566 const char *newname;
5567 char *tempname;
5568 tree decl;
5570 asprintf (&tempname, "%s.varargs", name);
5571 newname = ggc_strdup (tempname);
5572 free (tempname);
5574 /* We need sth that can be pointed to for va_start. */
5575 decl = build_fake_var_decl (ptr_type_node);
5577 argvi = new_var_info (decl, newname);
5578 argvi->offset = fi_parm_base + num_args;
5579 argvi->size = ~0;
5580 argvi->is_full_var = true;
5581 argvi->is_heap_var = true;
5582 argvi->fullsize = vi->fullsize;
5583 gcc_assert (prev_vi->offset < argvi->offset);
5584 prev_vi->next = argvi->id;
5585 prev_vi = argvi;
5588 return vi;
5592 /* Return true if FIELDSTACK contains fields that overlap.
5593 FIELDSTACK is assumed to be sorted by offset. */
5595 static bool
5596 check_for_overlaps (vec<fieldoff_s> fieldstack)
5598 fieldoff_s *fo = NULL;
5599 unsigned int i;
5600 HOST_WIDE_INT lastoffset = -1;
5602 FOR_EACH_VEC_ELT (fieldstack, i, fo)
5604 if (fo->offset == lastoffset)
5605 return true;
5606 lastoffset = fo->offset;
5608 return false;
5611 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5612 This will also create any varinfo structures necessary for fields
5613 of DECL. */
5615 static varinfo_t
5616 create_variable_info_for_1 (tree decl, const char *name)
5618 varinfo_t vi, newvi;
5619 tree decl_type = TREE_TYPE (decl);
5620 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
5621 auto_vec<fieldoff_s> fieldstack;
5622 fieldoff_s *fo;
5623 unsigned int i;
5624 varpool_node *vnode;
5626 if (!declsize
5627 || !tree_fits_uhwi_p (declsize))
5629 vi = new_var_info (decl, name);
5630 vi->offset = 0;
5631 vi->size = ~0;
5632 vi->fullsize = ~0;
5633 vi->is_unknown_size_var = true;
5634 vi->is_full_var = true;
5635 vi->may_have_pointers = true;
5636 return vi;
5639 /* Collect field information. */
5640 if (use_field_sensitive
5641 && var_can_have_subvars (decl)
5642 /* ??? Force us to not use subfields for global initializers
5643 in IPA mode. Else we'd have to parse arbitrary initializers. */
5644 && !(in_ipa_mode
5645 && is_global_var (decl)
5646 && (vnode = varpool_node::get (decl))
5647 && vnode->get_constructor ()))
5649 fieldoff_s *fo = NULL;
5650 bool notokay = false;
5651 unsigned int i;
5653 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
5655 for (i = 0; !notokay && fieldstack.iterate (i, &fo); i++)
5656 if (fo->has_unknown_size
5657 || fo->offset < 0)
5659 notokay = true;
5660 break;
5663 /* We can't sort them if we have a field with a variable sized type,
5664 which will make notokay = true. In that case, we are going to return
5665 without creating varinfos for the fields anyway, so sorting them is a
5666 waste to boot. */
5667 if (!notokay)
5669 sort_fieldstack (fieldstack);
5670 /* Due to some C++ FE issues, like PR 22488, we might end up
5671 what appear to be overlapping fields even though they,
5672 in reality, do not overlap. Until the C++ FE is fixed,
5673 we will simply disable field-sensitivity for these cases. */
5674 notokay = check_for_overlaps (fieldstack);
5677 if (notokay)
5678 fieldstack.release ();
5681 /* If we didn't end up collecting sub-variables create a full
5682 variable for the decl. */
5683 if (fieldstack.length () <= 1
5684 || fieldstack.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5686 vi = new_var_info (decl, name);
5687 vi->offset = 0;
5688 vi->may_have_pointers = true;
5689 vi->fullsize = tree_to_uhwi (declsize);
5690 vi->size = vi->fullsize;
5691 vi->is_full_var = true;
5692 fieldstack.release ();
5693 return vi;
5696 vi = new_var_info (decl, name);
5697 vi->fullsize = tree_to_uhwi (declsize);
5698 for (i = 0, newvi = vi;
5699 fieldstack.iterate (i, &fo);
5700 ++i, newvi = vi_next (newvi))
5702 const char *newname = "NULL";
5703 char *tempname;
5705 if (dump_file)
5707 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC
5708 "+" HOST_WIDE_INT_PRINT_DEC, name, fo->offset, fo->size);
5709 newname = ggc_strdup (tempname);
5710 free (tempname);
5712 newvi->name = newname;
5713 newvi->offset = fo->offset;
5714 newvi->size = fo->size;
5715 newvi->fullsize = vi->fullsize;
5716 newvi->may_have_pointers = fo->may_have_pointers;
5717 newvi->only_restrict_pointers = fo->only_restrict_pointers;
5718 if (i + 1 < fieldstack.length ())
5720 varinfo_t tem = new_var_info (decl, name);
5721 newvi->next = tem->id;
5722 tem->head = vi->id;
5726 return vi;
5729 static unsigned int
5730 create_variable_info_for (tree decl, const char *name)
5732 varinfo_t vi = create_variable_info_for_1 (decl, name);
5733 unsigned int id = vi->id;
5735 insert_vi_for_tree (decl, vi);
5737 if (TREE_CODE (decl) != VAR_DECL)
5738 return id;
5740 /* Create initial constraints for globals. */
5741 for (; vi; vi = vi_next (vi))
5743 if (!vi->may_have_pointers
5744 || !vi->is_global_var)
5745 continue;
5747 /* Mark global restrict qualified pointers. */
5748 if ((POINTER_TYPE_P (TREE_TYPE (decl))
5749 && TYPE_RESTRICT (TREE_TYPE (decl)))
5750 || vi->only_restrict_pointers)
5752 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5753 continue;
5756 /* In non-IPA mode the initializer from nonlocal is all we need. */
5757 if (!in_ipa_mode
5758 || DECL_HARD_REGISTER (decl))
5759 make_copy_constraint (vi, nonlocal_id);
5761 /* In IPA mode parse the initializer and generate proper constraints
5762 for it. */
5763 else
5765 varpool_node *vnode = varpool_node::get (decl);
5767 /* For escaped variables initialize them from nonlocal. */
5768 if (!vnode->all_refs_explicit_p ())
5769 make_copy_constraint (vi, nonlocal_id);
5771 /* If this is a global variable with an initializer and we are in
5772 IPA mode generate constraints for it. */
5773 if (vnode->get_constructor ()
5774 && vnode->definition)
5776 auto_vec<ce_s> rhsc;
5777 struct constraint_expr lhs, *rhsp;
5778 unsigned i;
5779 get_constraint_for_rhs (vnode->get_constructor (), &rhsc);
5780 lhs.var = vi->id;
5781 lhs.offset = 0;
5782 lhs.type = SCALAR;
5783 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5784 process_constraint (new_constraint (lhs, *rhsp));
5785 /* If this is a variable that escapes from the unit
5786 the initializer escapes as well. */
5787 if (!vnode->all_refs_explicit_p ())
5789 lhs.var = escaped_id;
5790 lhs.offset = 0;
5791 lhs.type = SCALAR;
5792 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5793 process_constraint (new_constraint (lhs, *rhsp));
5799 return id;
5802 /* Print out the points-to solution for VAR to FILE. */
5804 static void
5805 dump_solution_for_var (FILE *file, unsigned int var)
5807 varinfo_t vi = get_varinfo (var);
5808 unsigned int i;
5809 bitmap_iterator bi;
5811 /* Dump the solution for unified vars anyway, this avoids difficulties
5812 in scanning dumps in the testsuite. */
5813 fprintf (file, "%s = { ", vi->name);
5814 vi = get_varinfo (find (var));
5815 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5816 fprintf (file, "%s ", get_varinfo (i)->name);
5817 fprintf (file, "}");
5819 /* But note when the variable was unified. */
5820 if (vi->id != var)
5821 fprintf (file, " same as %s", vi->name);
5823 fprintf (file, "\n");
5826 /* Print the points-to solution for VAR to stderr. */
5828 DEBUG_FUNCTION void
5829 debug_solution_for_var (unsigned int var)
5831 dump_solution_for_var (stderr, var);
5834 /* Create varinfo structures for all of the variables in the
5835 function for intraprocedural mode. */
5837 static void
5838 intra_create_variable_infos (struct function *fn)
5840 tree t;
5842 /* For each incoming pointer argument arg, create the constraint ARG
5843 = NONLOCAL or a dummy variable if it is a restrict qualified
5844 passed-by-reference argument. */
5845 for (t = DECL_ARGUMENTS (fn->decl); t; t = DECL_CHAIN (t))
5847 varinfo_t p = get_vi_for_tree (t);
5849 /* For restrict qualified pointers to objects passed by
5850 reference build a real representative for the pointed-to object.
5851 Treat restrict qualified references the same. */
5852 if (TYPE_RESTRICT (TREE_TYPE (t))
5853 && ((DECL_BY_REFERENCE (t) && POINTER_TYPE_P (TREE_TYPE (t)))
5854 || TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5855 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t))))
5857 struct constraint_expr lhsc, rhsc;
5858 varinfo_t vi;
5859 tree heapvar = build_fake_var_decl (TREE_TYPE (TREE_TYPE (t)));
5860 DECL_EXTERNAL (heapvar) = 1;
5861 vi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS");
5862 insert_vi_for_tree (heapvar, vi);
5863 lhsc.var = p->id;
5864 lhsc.type = SCALAR;
5865 lhsc.offset = 0;
5866 rhsc.var = vi->id;
5867 rhsc.type = ADDRESSOF;
5868 rhsc.offset = 0;
5869 process_constraint (new_constraint (lhsc, rhsc));
5870 for (; vi; vi = vi_next (vi))
5871 if (vi->may_have_pointers)
5873 if (vi->only_restrict_pointers)
5874 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5875 else
5876 make_copy_constraint (vi, nonlocal_id);
5878 continue;
5881 if (POINTER_TYPE_P (TREE_TYPE (t))
5882 && TYPE_RESTRICT (TREE_TYPE (t)))
5883 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5884 else
5886 for (; p; p = vi_next (p))
5888 if (p->only_restrict_pointers)
5889 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5890 else if (p->may_have_pointers)
5891 make_constraint_from (p, nonlocal_id);
5896 /* Add a constraint for a result decl that is passed by reference. */
5897 if (DECL_RESULT (fn->decl)
5898 && DECL_BY_REFERENCE (DECL_RESULT (fn->decl)))
5900 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (fn->decl));
5902 for (p = result_vi; p; p = vi_next (p))
5903 make_constraint_from (p, nonlocal_id);
5906 /* Add a constraint for the incoming static chain parameter. */
5907 if (fn->static_chain_decl != NULL_TREE)
5909 varinfo_t p, chain_vi = get_vi_for_tree (fn->static_chain_decl);
5911 for (p = chain_vi; p; p = vi_next (p))
5912 make_constraint_from (p, nonlocal_id);
5916 /* Structure used to put solution bitmaps in a hashtable so they can
5917 be shared among variables with the same points-to set. */
5919 typedef struct shared_bitmap_info
5921 bitmap pt_vars;
5922 hashval_t hashcode;
5923 } *shared_bitmap_info_t;
5924 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
5926 /* Shared_bitmap hashtable helpers. */
5928 struct shared_bitmap_hasher : typed_free_remove <shared_bitmap_info>
5930 typedef shared_bitmap_info value_type;
5931 typedef shared_bitmap_info compare_type;
5932 static inline hashval_t hash (const value_type *);
5933 static inline bool equal (const value_type *, const compare_type *);
5936 /* Hash function for a shared_bitmap_info_t */
5938 inline hashval_t
5939 shared_bitmap_hasher::hash (const value_type *bi)
5941 return bi->hashcode;
5944 /* Equality function for two shared_bitmap_info_t's. */
5946 inline bool
5947 shared_bitmap_hasher::equal (const value_type *sbi1, const compare_type *sbi2)
5949 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
5952 /* Shared_bitmap hashtable. */
5954 static hash_table<shared_bitmap_hasher> *shared_bitmap_table;
5956 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5957 existing instance if there is one, NULL otherwise. */
5959 static bitmap
5960 shared_bitmap_lookup (bitmap pt_vars)
5962 shared_bitmap_info **slot;
5963 struct shared_bitmap_info sbi;
5965 sbi.pt_vars = pt_vars;
5966 sbi.hashcode = bitmap_hash (pt_vars);
5968 slot = shared_bitmap_table->find_slot (&sbi, NO_INSERT);
5969 if (!slot)
5970 return NULL;
5971 else
5972 return (*slot)->pt_vars;
5976 /* Add a bitmap to the shared bitmap hashtable. */
5978 static void
5979 shared_bitmap_add (bitmap pt_vars)
5981 shared_bitmap_info **slot;
5982 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
5984 sbi->pt_vars = pt_vars;
5985 sbi->hashcode = bitmap_hash (pt_vars);
5987 slot = shared_bitmap_table->find_slot (sbi, INSERT);
5988 gcc_assert (!*slot);
5989 *slot = sbi;
5993 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
5995 static void
5996 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt)
5998 unsigned int i;
5999 bitmap_iterator bi;
6000 varinfo_t escaped_vi = get_varinfo (find (escaped_id));
6001 bool everything_escaped
6002 = escaped_vi->solution && bitmap_bit_p (escaped_vi->solution, anything_id);
6004 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
6006 varinfo_t vi = get_varinfo (i);
6008 /* The only artificial variables that are allowed in a may-alias
6009 set are heap variables. */
6010 if (vi->is_artificial_var && !vi->is_heap_var)
6011 continue;
6013 if (everything_escaped
6014 || (escaped_vi->solution
6015 && bitmap_bit_p (escaped_vi->solution, i)))
6017 pt->vars_contains_escaped = true;
6018 pt->vars_contains_escaped_heap = vi->is_heap_var;
6021 if (TREE_CODE (vi->decl) == VAR_DECL
6022 || TREE_CODE (vi->decl) == PARM_DECL
6023 || TREE_CODE (vi->decl) == RESULT_DECL)
6025 /* If we are in IPA mode we will not recompute points-to
6026 sets after inlining so make sure they stay valid. */
6027 if (in_ipa_mode
6028 && !DECL_PT_UID_SET_P (vi->decl))
6029 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
6031 /* Add the decl to the points-to set. Note that the points-to
6032 set contains global variables. */
6033 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
6034 if (vi->is_global_var)
6035 pt->vars_contains_nonlocal = true;
6041 /* Compute the points-to solution *PT for the variable VI. */
6043 static struct pt_solution
6044 find_what_var_points_to (varinfo_t orig_vi)
6046 unsigned int i;
6047 bitmap_iterator bi;
6048 bitmap finished_solution;
6049 bitmap result;
6050 varinfo_t vi;
6051 struct pt_solution *pt;
6053 /* This variable may have been collapsed, let's get the real
6054 variable. */
6055 vi = get_varinfo (find (orig_vi->id));
6057 /* See if we have already computed the solution and return it. */
6058 pt_solution **slot = &final_solutions->get_or_insert (vi);
6059 if (*slot != NULL)
6060 return **slot;
6062 *slot = pt = XOBNEW (&final_solutions_obstack, struct pt_solution);
6063 memset (pt, 0, sizeof (struct pt_solution));
6065 /* Translate artificial variables into SSA_NAME_PTR_INFO
6066 attributes. */
6067 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
6069 varinfo_t vi = get_varinfo (i);
6071 if (vi->is_artificial_var)
6073 if (vi->id == nothing_id)
6074 pt->null = 1;
6075 else if (vi->id == escaped_id)
6077 if (in_ipa_mode)
6078 pt->ipa_escaped = 1;
6079 else
6080 pt->escaped = 1;
6081 /* Expand some special vars of ESCAPED in-place here. */
6082 varinfo_t evi = get_varinfo (find (escaped_id));
6083 if (bitmap_bit_p (evi->solution, nonlocal_id))
6084 pt->nonlocal = 1;
6086 else if (vi->id == nonlocal_id)
6087 pt->nonlocal = 1;
6088 else if (vi->is_heap_var)
6089 /* We represent heapvars in the points-to set properly. */
6091 else if (vi->id == string_id)
6092 /* Nobody cares - STRING_CSTs are read-only entities. */
6094 else if (vi->id == anything_id
6095 || vi->id == integer_id)
6096 pt->anything = 1;
6100 /* Instead of doing extra work, simply do not create
6101 elaborate points-to information for pt_anything pointers. */
6102 if (pt->anything)
6103 return *pt;
6105 /* Share the final set of variables when possible. */
6106 finished_solution = BITMAP_GGC_ALLOC ();
6107 stats.points_to_sets_created++;
6109 set_uids_in_ptset (finished_solution, vi->solution, pt);
6110 result = shared_bitmap_lookup (finished_solution);
6111 if (!result)
6113 shared_bitmap_add (finished_solution);
6114 pt->vars = finished_solution;
6116 else
6118 pt->vars = result;
6119 bitmap_clear (finished_solution);
6122 return *pt;
6125 /* Given a pointer variable P, fill in its points-to set. */
6127 static void
6128 find_what_p_points_to (tree p)
6130 struct ptr_info_def *pi;
6131 tree lookup_p = p;
6132 varinfo_t vi;
6134 /* For parameters, get at the points-to set for the actual parm
6135 decl. */
6136 if (TREE_CODE (p) == SSA_NAME
6137 && SSA_NAME_IS_DEFAULT_DEF (p)
6138 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
6139 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL))
6140 lookup_p = SSA_NAME_VAR (p);
6142 vi = lookup_vi_for_tree (lookup_p);
6143 if (!vi)
6144 return;
6146 pi = get_ptr_info (p);
6147 pi->pt = find_what_var_points_to (vi);
6151 /* Query statistics for points-to solutions. */
6153 static struct {
6154 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
6155 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
6156 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
6157 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
6158 } pta_stats;
6160 void
6161 dump_pta_stats (FILE *s)
6163 fprintf (s, "\nPTA query stats:\n");
6164 fprintf (s, " pt_solution_includes: "
6165 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6166 HOST_WIDE_INT_PRINT_DEC" queries\n",
6167 pta_stats.pt_solution_includes_no_alias,
6168 pta_stats.pt_solution_includes_no_alias
6169 + pta_stats.pt_solution_includes_may_alias);
6170 fprintf (s, " pt_solutions_intersect: "
6171 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6172 HOST_WIDE_INT_PRINT_DEC" queries\n",
6173 pta_stats.pt_solutions_intersect_no_alias,
6174 pta_stats.pt_solutions_intersect_no_alias
6175 + pta_stats.pt_solutions_intersect_may_alias);
6179 /* Reset the points-to solution *PT to a conservative default
6180 (point to anything). */
6182 void
6183 pt_solution_reset (struct pt_solution *pt)
6185 memset (pt, 0, sizeof (struct pt_solution));
6186 pt->anything = true;
6189 /* Set the points-to solution *PT to point only to the variables
6190 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6191 global variables and VARS_CONTAINS_RESTRICT specifies whether
6192 it contains restrict tag variables. */
6194 void
6195 pt_solution_set (struct pt_solution *pt, bitmap vars,
6196 bool vars_contains_nonlocal)
6198 memset (pt, 0, sizeof (struct pt_solution));
6199 pt->vars = vars;
6200 pt->vars_contains_nonlocal = vars_contains_nonlocal;
6201 pt->vars_contains_escaped
6202 = (cfun->gimple_df->escaped.anything
6203 || bitmap_intersect_p (cfun->gimple_df->escaped.vars, vars));
6206 /* Set the points-to solution *PT to point only to the variable VAR. */
6208 void
6209 pt_solution_set_var (struct pt_solution *pt, tree var)
6211 memset (pt, 0, sizeof (struct pt_solution));
6212 pt->vars = BITMAP_GGC_ALLOC ();
6213 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
6214 pt->vars_contains_nonlocal = is_global_var (var);
6215 pt->vars_contains_escaped
6216 = (cfun->gimple_df->escaped.anything
6217 || bitmap_bit_p (cfun->gimple_df->escaped.vars, DECL_PT_UID (var)));
6220 /* Computes the union of the points-to solutions *DEST and *SRC and
6221 stores the result in *DEST. This changes the points-to bitmap
6222 of *DEST and thus may not be used if that might be shared.
6223 The points-to bitmap of *SRC and *DEST will not be shared after
6224 this function if they were not before. */
6226 static void
6227 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6229 dest->anything |= src->anything;
6230 if (dest->anything)
6232 pt_solution_reset (dest);
6233 return;
6236 dest->nonlocal |= src->nonlocal;
6237 dest->escaped |= src->escaped;
6238 dest->ipa_escaped |= src->ipa_escaped;
6239 dest->null |= src->null;
6240 dest->vars_contains_nonlocal |= src->vars_contains_nonlocal;
6241 dest->vars_contains_escaped |= src->vars_contains_escaped;
6242 dest->vars_contains_escaped_heap |= src->vars_contains_escaped_heap;
6243 if (!src->vars)
6244 return;
6246 if (!dest->vars)
6247 dest->vars = BITMAP_GGC_ALLOC ();
6248 bitmap_ior_into (dest->vars, src->vars);
6251 /* Return true if the points-to solution *PT is empty. */
6253 bool
6254 pt_solution_empty_p (struct pt_solution *pt)
6256 if (pt->anything
6257 || pt->nonlocal)
6258 return false;
6260 if (pt->vars
6261 && !bitmap_empty_p (pt->vars))
6262 return false;
6264 /* If the solution includes ESCAPED, check if that is empty. */
6265 if (pt->escaped
6266 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6267 return false;
6269 /* If the solution includes ESCAPED, check if that is empty. */
6270 if (pt->ipa_escaped
6271 && !pt_solution_empty_p (&ipa_escaped_pt))
6272 return false;
6274 return true;
6277 /* Return true if the points-to solution *PT only point to a single var, and
6278 return the var uid in *UID. */
6280 bool
6281 pt_solution_singleton_p (struct pt_solution *pt, unsigned *uid)
6283 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
6284 || pt->null || pt->vars == NULL
6285 || !bitmap_single_bit_set_p (pt->vars))
6286 return false;
6288 *uid = bitmap_first_set_bit (pt->vars);
6289 return true;
6292 /* Return true if the points-to solution *PT includes global memory. */
6294 bool
6295 pt_solution_includes_global (struct pt_solution *pt)
6297 if (pt->anything
6298 || pt->nonlocal
6299 || pt->vars_contains_nonlocal
6300 /* The following is a hack to make the malloc escape hack work.
6301 In reality we'd need different sets for escaped-through-return
6302 and escaped-to-callees and passes would need to be updated. */
6303 || pt->vars_contains_escaped_heap)
6304 return true;
6306 /* 'escaped' is also a placeholder so we have to look into it. */
6307 if (pt->escaped)
6308 return pt_solution_includes_global (&cfun->gimple_df->escaped);
6310 if (pt->ipa_escaped)
6311 return pt_solution_includes_global (&ipa_escaped_pt);
6313 /* ??? This predicate is not correct for the IPA-PTA solution
6314 as we do not properly distinguish between unit escape points
6315 and global variables. */
6316 if (cfun->gimple_df->ipa_pta)
6317 return true;
6319 return false;
6322 /* Return true if the points-to solution *PT includes the variable
6323 declaration DECL. */
6325 static bool
6326 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6328 if (pt->anything)
6329 return true;
6331 if (pt->nonlocal
6332 && is_global_var (decl))
6333 return true;
6335 if (pt->vars
6336 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6337 return true;
6339 /* If the solution includes ESCAPED, check it. */
6340 if (pt->escaped
6341 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6342 return true;
6344 /* If the solution includes ESCAPED, check it. */
6345 if (pt->ipa_escaped
6346 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6347 return true;
6349 return false;
6352 bool
6353 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6355 bool res = pt_solution_includes_1 (pt, decl);
6356 if (res)
6357 ++pta_stats.pt_solution_includes_may_alias;
6358 else
6359 ++pta_stats.pt_solution_includes_no_alias;
6360 return res;
6363 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6364 intersection. */
6366 static bool
6367 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
6369 if (pt1->anything || pt2->anything)
6370 return true;
6372 /* If either points to unknown global memory and the other points to
6373 any global memory they alias. */
6374 if ((pt1->nonlocal
6375 && (pt2->nonlocal
6376 || pt2->vars_contains_nonlocal))
6377 || (pt2->nonlocal
6378 && pt1->vars_contains_nonlocal))
6379 return true;
6381 /* If either points to all escaped memory and the other points to
6382 any escaped memory they alias. */
6383 if ((pt1->escaped
6384 && (pt2->escaped
6385 || pt2->vars_contains_escaped))
6386 || (pt2->escaped
6387 && pt1->vars_contains_escaped))
6388 return true;
6390 /* Check the escaped solution if required.
6391 ??? Do we need to check the local against the IPA escaped sets? */
6392 if ((pt1->ipa_escaped || pt2->ipa_escaped)
6393 && !pt_solution_empty_p (&ipa_escaped_pt))
6395 /* If both point to escaped memory and that solution
6396 is not empty they alias. */
6397 if (pt1->ipa_escaped && pt2->ipa_escaped)
6398 return true;
6400 /* If either points to escaped memory see if the escaped solution
6401 intersects with the other. */
6402 if ((pt1->ipa_escaped
6403 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
6404 || (pt2->ipa_escaped
6405 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
6406 return true;
6409 /* Now both pointers alias if their points-to solution intersects. */
6410 return (pt1->vars
6411 && pt2->vars
6412 && bitmap_intersect_p (pt1->vars, pt2->vars));
6415 bool
6416 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
6418 bool res = pt_solutions_intersect_1 (pt1, pt2);
6419 if (res)
6420 ++pta_stats.pt_solutions_intersect_may_alias;
6421 else
6422 ++pta_stats.pt_solutions_intersect_no_alias;
6423 return res;
6427 /* Dump points-to information to OUTFILE. */
6429 static void
6430 dump_sa_points_to_info (FILE *outfile)
6432 unsigned int i;
6434 fprintf (outfile, "\nPoints-to sets\n\n");
6436 if (dump_flags & TDF_STATS)
6438 fprintf (outfile, "Stats:\n");
6439 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
6440 fprintf (outfile, "Non-pointer vars: %d\n",
6441 stats.nonpointer_vars);
6442 fprintf (outfile, "Statically unified vars: %d\n",
6443 stats.unified_vars_static);
6444 fprintf (outfile, "Dynamically unified vars: %d\n",
6445 stats.unified_vars_dynamic);
6446 fprintf (outfile, "Iterations: %d\n", stats.iterations);
6447 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
6448 fprintf (outfile, "Number of implicit edges: %d\n",
6449 stats.num_implicit_edges);
6452 for (i = 1; i < varmap.length (); i++)
6454 varinfo_t vi = get_varinfo (i);
6455 if (!vi->may_have_pointers)
6456 continue;
6457 dump_solution_for_var (outfile, i);
6462 /* Debug points-to information to stderr. */
6464 DEBUG_FUNCTION void
6465 debug_sa_points_to_info (void)
6467 dump_sa_points_to_info (stderr);
6471 /* Initialize the always-existing constraint variables for NULL
6472 ANYTHING, READONLY, and INTEGER */
6474 static void
6475 init_base_vars (void)
6477 struct constraint_expr lhs, rhs;
6478 varinfo_t var_anything;
6479 varinfo_t var_nothing;
6480 varinfo_t var_string;
6481 varinfo_t var_escaped;
6482 varinfo_t var_nonlocal;
6483 varinfo_t var_storedanything;
6484 varinfo_t var_integer;
6486 /* Variable ID zero is reserved and should be NULL. */
6487 varmap.safe_push (NULL);
6489 /* Create the NULL variable, used to represent that a variable points
6490 to NULL. */
6491 var_nothing = new_var_info (NULL_TREE, "NULL");
6492 gcc_assert (var_nothing->id == nothing_id);
6493 var_nothing->is_artificial_var = 1;
6494 var_nothing->offset = 0;
6495 var_nothing->size = ~0;
6496 var_nothing->fullsize = ~0;
6497 var_nothing->is_special_var = 1;
6498 var_nothing->may_have_pointers = 0;
6499 var_nothing->is_global_var = 0;
6501 /* Create the ANYTHING variable, used to represent that a variable
6502 points to some unknown piece of memory. */
6503 var_anything = new_var_info (NULL_TREE, "ANYTHING");
6504 gcc_assert (var_anything->id == anything_id);
6505 var_anything->is_artificial_var = 1;
6506 var_anything->size = ~0;
6507 var_anything->offset = 0;
6508 var_anything->fullsize = ~0;
6509 var_anything->is_special_var = 1;
6511 /* Anything points to anything. This makes deref constraints just
6512 work in the presence of linked list and other p = *p type loops,
6513 by saying that *ANYTHING = ANYTHING. */
6514 lhs.type = SCALAR;
6515 lhs.var = anything_id;
6516 lhs.offset = 0;
6517 rhs.type = ADDRESSOF;
6518 rhs.var = anything_id;
6519 rhs.offset = 0;
6521 /* This specifically does not use process_constraint because
6522 process_constraint ignores all anything = anything constraints, since all
6523 but this one are redundant. */
6524 constraints.safe_push (new_constraint (lhs, rhs));
6526 /* Create the STRING variable, used to represent that a variable
6527 points to a string literal. String literals don't contain
6528 pointers so STRING doesn't point to anything. */
6529 var_string = new_var_info (NULL_TREE, "STRING");
6530 gcc_assert (var_string->id == string_id);
6531 var_string->is_artificial_var = 1;
6532 var_string->offset = 0;
6533 var_string->size = ~0;
6534 var_string->fullsize = ~0;
6535 var_string->is_special_var = 1;
6536 var_string->may_have_pointers = 0;
6538 /* Create the ESCAPED variable, used to represent the set of escaped
6539 memory. */
6540 var_escaped = new_var_info (NULL_TREE, "ESCAPED");
6541 gcc_assert (var_escaped->id == escaped_id);
6542 var_escaped->is_artificial_var = 1;
6543 var_escaped->offset = 0;
6544 var_escaped->size = ~0;
6545 var_escaped->fullsize = ~0;
6546 var_escaped->is_special_var = 0;
6548 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6549 memory. */
6550 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL");
6551 gcc_assert (var_nonlocal->id == nonlocal_id);
6552 var_nonlocal->is_artificial_var = 1;
6553 var_nonlocal->offset = 0;
6554 var_nonlocal->size = ~0;
6555 var_nonlocal->fullsize = ~0;
6556 var_nonlocal->is_special_var = 1;
6558 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6559 lhs.type = SCALAR;
6560 lhs.var = escaped_id;
6561 lhs.offset = 0;
6562 rhs.type = DEREF;
6563 rhs.var = escaped_id;
6564 rhs.offset = 0;
6565 process_constraint (new_constraint (lhs, rhs));
6567 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6568 whole variable escapes. */
6569 lhs.type = SCALAR;
6570 lhs.var = escaped_id;
6571 lhs.offset = 0;
6572 rhs.type = SCALAR;
6573 rhs.var = escaped_id;
6574 rhs.offset = UNKNOWN_OFFSET;
6575 process_constraint (new_constraint (lhs, rhs));
6577 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6578 everything pointed to by escaped points to what global memory can
6579 point to. */
6580 lhs.type = DEREF;
6581 lhs.var = escaped_id;
6582 lhs.offset = 0;
6583 rhs.type = SCALAR;
6584 rhs.var = nonlocal_id;
6585 rhs.offset = 0;
6586 process_constraint (new_constraint (lhs, rhs));
6588 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6589 global memory may point to global memory and escaped memory. */
6590 lhs.type = SCALAR;
6591 lhs.var = nonlocal_id;
6592 lhs.offset = 0;
6593 rhs.type = ADDRESSOF;
6594 rhs.var = nonlocal_id;
6595 rhs.offset = 0;
6596 process_constraint (new_constraint (lhs, rhs));
6597 rhs.type = ADDRESSOF;
6598 rhs.var = escaped_id;
6599 rhs.offset = 0;
6600 process_constraint (new_constraint (lhs, rhs));
6602 /* Create the STOREDANYTHING variable, used to represent the set of
6603 variables stored to *ANYTHING. */
6604 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING");
6605 gcc_assert (var_storedanything->id == storedanything_id);
6606 var_storedanything->is_artificial_var = 1;
6607 var_storedanything->offset = 0;
6608 var_storedanything->size = ~0;
6609 var_storedanything->fullsize = ~0;
6610 var_storedanything->is_special_var = 0;
6612 /* Create the INTEGER variable, used to represent that a variable points
6613 to what an INTEGER "points to". */
6614 var_integer = new_var_info (NULL_TREE, "INTEGER");
6615 gcc_assert (var_integer->id == integer_id);
6616 var_integer->is_artificial_var = 1;
6617 var_integer->size = ~0;
6618 var_integer->fullsize = ~0;
6619 var_integer->offset = 0;
6620 var_integer->is_special_var = 1;
6622 /* INTEGER = ANYTHING, because we don't know where a dereference of
6623 a random integer will point to. */
6624 lhs.type = SCALAR;
6625 lhs.var = integer_id;
6626 lhs.offset = 0;
6627 rhs.type = ADDRESSOF;
6628 rhs.var = anything_id;
6629 rhs.offset = 0;
6630 process_constraint (new_constraint (lhs, rhs));
6633 /* Initialize things necessary to perform PTA */
6635 static void
6636 init_alias_vars (void)
6638 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
6640 bitmap_obstack_initialize (&pta_obstack);
6641 bitmap_obstack_initialize (&oldpta_obstack);
6642 bitmap_obstack_initialize (&predbitmap_obstack);
6644 constraint_pool = create_alloc_pool ("Constraint pool",
6645 sizeof (struct constraint), 30);
6646 variable_info_pool = create_alloc_pool ("Variable info pool",
6647 sizeof (struct variable_info), 30);
6648 constraints.create (8);
6649 varmap.create (8);
6650 vi_for_tree = new hash_map<tree, varinfo_t>;
6651 call_stmt_vars = new hash_map<gimple, varinfo_t>;
6653 memset (&stats, 0, sizeof (stats));
6654 shared_bitmap_table = new hash_table<shared_bitmap_hasher> (511);
6655 init_base_vars ();
6657 gcc_obstack_init (&fake_var_decl_obstack);
6659 final_solutions = new hash_map<varinfo_t, pt_solution *>;
6660 gcc_obstack_init (&final_solutions_obstack);
6663 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6664 predecessor edges. */
6666 static void
6667 remove_preds_and_fake_succs (constraint_graph_t graph)
6669 unsigned int i;
6671 /* Clear the implicit ref and address nodes from the successor
6672 lists. */
6673 for (i = 1; i < FIRST_REF_NODE; i++)
6675 if (graph->succs[i])
6676 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
6677 FIRST_REF_NODE * 2);
6680 /* Free the successor list for the non-ref nodes. */
6681 for (i = FIRST_REF_NODE + 1; i < graph->size; i++)
6683 if (graph->succs[i])
6684 BITMAP_FREE (graph->succs[i]);
6687 /* Now reallocate the size of the successor list as, and blow away
6688 the predecessor bitmaps. */
6689 graph->size = varmap.length ();
6690 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
6692 free (graph->implicit_preds);
6693 graph->implicit_preds = NULL;
6694 free (graph->preds);
6695 graph->preds = NULL;
6696 bitmap_obstack_release (&predbitmap_obstack);
6699 /* Solve the constraint set. */
6701 static void
6702 solve_constraints (void)
6704 struct scc_info *si;
6706 if (dump_file)
6707 fprintf (dump_file,
6708 "\nCollapsing static cycles and doing variable "
6709 "substitution\n");
6711 init_graph (varmap.length () * 2);
6713 if (dump_file)
6714 fprintf (dump_file, "Building predecessor graph\n");
6715 build_pred_graph ();
6717 if (dump_file)
6718 fprintf (dump_file, "Detecting pointer and location "
6719 "equivalences\n");
6720 si = perform_var_substitution (graph);
6722 if (dump_file)
6723 fprintf (dump_file, "Rewriting constraints and unifying "
6724 "variables\n");
6725 rewrite_constraints (graph, si);
6727 build_succ_graph ();
6729 free_var_substitution_info (si);
6731 /* Attach complex constraints to graph nodes. */
6732 move_complex_constraints (graph);
6734 if (dump_file)
6735 fprintf (dump_file, "Uniting pointer but not location equivalent "
6736 "variables\n");
6737 unite_pointer_equivalences (graph);
6739 if (dump_file)
6740 fprintf (dump_file, "Finding indirect cycles\n");
6741 find_indirect_cycles (graph);
6743 /* Implicit nodes and predecessors are no longer necessary at this
6744 point. */
6745 remove_preds_and_fake_succs (graph);
6747 if (dump_file && (dump_flags & TDF_GRAPH))
6749 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
6750 "in dot format:\n");
6751 dump_constraint_graph (dump_file);
6752 fprintf (dump_file, "\n\n");
6755 if (dump_file)
6756 fprintf (dump_file, "Solving graph\n");
6758 solve_graph (graph);
6760 if (dump_file && (dump_flags & TDF_GRAPH))
6762 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
6763 "in dot format:\n");
6764 dump_constraint_graph (dump_file);
6765 fprintf (dump_file, "\n\n");
6768 if (dump_file)
6769 dump_sa_points_to_info (dump_file);
6772 /* Create points-to sets for the current function. See the comments
6773 at the start of the file for an algorithmic overview. */
6775 static void
6776 compute_points_to_sets (void)
6778 basic_block bb;
6779 unsigned i;
6780 varinfo_t vi;
6782 timevar_push (TV_TREE_PTA);
6784 init_alias_vars ();
6786 intra_create_variable_infos (cfun);
6788 /* Now walk all statements and build the constraint set. */
6789 FOR_EACH_BB_FN (bb, cfun)
6791 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
6792 gsi_next (&gsi))
6794 gphi *phi = gsi.phi ();
6796 if (! virtual_operand_p (gimple_phi_result (phi)))
6797 find_func_aliases (cfun, phi);
6800 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
6801 gsi_next (&gsi))
6803 gimple stmt = gsi_stmt (gsi);
6805 find_func_aliases (cfun, stmt);
6809 if (dump_file)
6811 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
6812 dump_constraints (dump_file, 0);
6815 /* From the constraints compute the points-to sets. */
6816 solve_constraints ();
6818 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6819 cfun->gimple_df->escaped = find_what_var_points_to (get_varinfo (escaped_id));
6821 /* Make sure the ESCAPED solution (which is used as placeholder in
6822 other solutions) does not reference itself. This simplifies
6823 points-to solution queries. */
6824 cfun->gimple_df->escaped.escaped = 0;
6826 /* Compute the points-to sets for pointer SSA_NAMEs. */
6827 for (i = 0; i < num_ssa_names; ++i)
6829 tree ptr = ssa_name (i);
6830 if (ptr
6831 && POINTER_TYPE_P (TREE_TYPE (ptr)))
6832 find_what_p_points_to (ptr);
6835 /* Compute the call-used/clobbered sets. */
6836 FOR_EACH_BB_FN (bb, cfun)
6838 gimple_stmt_iterator gsi;
6840 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6842 gcall *stmt;
6843 struct pt_solution *pt;
6845 stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
6846 if (!stmt)
6847 continue;
6849 pt = gimple_call_use_set (stmt);
6850 if (gimple_call_flags (stmt) & ECF_CONST)
6851 memset (pt, 0, sizeof (struct pt_solution));
6852 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
6854 *pt = find_what_var_points_to (vi);
6855 /* Escaped (and thus nonlocal) variables are always
6856 implicitly used by calls. */
6857 /* ??? ESCAPED can be empty even though NONLOCAL
6858 always escaped. */
6859 pt->nonlocal = 1;
6860 pt->escaped = 1;
6862 else
6864 /* If there is nothing special about this call then
6865 we have made everything that is used also escape. */
6866 *pt = cfun->gimple_df->escaped;
6867 pt->nonlocal = 1;
6870 pt = gimple_call_clobber_set (stmt);
6871 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
6872 memset (pt, 0, sizeof (struct pt_solution));
6873 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
6875 *pt = find_what_var_points_to (vi);
6876 /* Escaped (and thus nonlocal) variables are always
6877 implicitly clobbered by calls. */
6878 /* ??? ESCAPED can be empty even though NONLOCAL
6879 always escaped. */
6880 pt->nonlocal = 1;
6881 pt->escaped = 1;
6883 else
6885 /* If there is nothing special about this call then
6886 we have made everything that is used also escape. */
6887 *pt = cfun->gimple_df->escaped;
6888 pt->nonlocal = 1;
6893 timevar_pop (TV_TREE_PTA);
6897 /* Delete created points-to sets. */
6899 static void
6900 delete_points_to_sets (void)
6902 unsigned int i;
6904 delete shared_bitmap_table;
6905 shared_bitmap_table = NULL;
6906 if (dump_file && (dump_flags & TDF_STATS))
6907 fprintf (dump_file, "Points to sets created:%d\n",
6908 stats.points_to_sets_created);
6910 delete vi_for_tree;
6911 delete call_stmt_vars;
6912 bitmap_obstack_release (&pta_obstack);
6913 constraints.release ();
6915 for (i = 0; i < graph->size; i++)
6916 graph->complex[i].release ();
6917 free (graph->complex);
6919 free (graph->rep);
6920 free (graph->succs);
6921 free (graph->pe);
6922 free (graph->pe_rep);
6923 free (graph->indirect_cycles);
6924 free (graph);
6926 varmap.release ();
6927 free_alloc_pool (variable_info_pool);
6928 free_alloc_pool (constraint_pool);
6930 obstack_free (&fake_var_decl_obstack, NULL);
6932 delete final_solutions;
6933 obstack_free (&final_solutions_obstack, NULL);
6937 /* Compute points-to information for every SSA_NAME pointer in the
6938 current function and compute the transitive closure of escaped
6939 variables to re-initialize the call-clobber states of local variables. */
6941 unsigned int
6942 compute_may_aliases (void)
6944 if (cfun->gimple_df->ipa_pta)
6946 if (dump_file)
6948 fprintf (dump_file, "\nNot re-computing points-to information "
6949 "because IPA points-to information is available.\n\n");
6951 /* But still dump what we have remaining it. */
6952 dump_alias_info (dump_file);
6955 return 0;
6958 /* For each pointer P_i, determine the sets of variables that P_i may
6959 point-to. Compute the reachability set of escaped and call-used
6960 variables. */
6961 compute_points_to_sets ();
6963 /* Debugging dumps. */
6964 if (dump_file)
6965 dump_alias_info (dump_file);
6967 /* Deallocate memory used by aliasing data structures and the internal
6968 points-to solution. */
6969 delete_points_to_sets ();
6971 gcc_assert (!need_ssa_update_p (cfun));
6973 return 0;
6976 /* A dummy pass to cause points-to information to be computed via
6977 TODO_rebuild_alias. */
6979 namespace {
6981 const pass_data pass_data_build_alias =
6983 GIMPLE_PASS, /* type */
6984 "alias", /* name */
6985 OPTGROUP_NONE, /* optinfo_flags */
6986 TV_NONE, /* tv_id */
6987 ( PROP_cfg | PROP_ssa ), /* properties_required */
6988 0, /* properties_provided */
6989 0, /* properties_destroyed */
6990 0, /* todo_flags_start */
6991 TODO_rebuild_alias, /* todo_flags_finish */
6994 class pass_build_alias : public gimple_opt_pass
6996 public:
6997 pass_build_alias (gcc::context *ctxt)
6998 : gimple_opt_pass (pass_data_build_alias, ctxt)
7001 /* opt_pass methods: */
7002 virtual bool gate (function *) { return flag_tree_pta; }
7004 }; // class pass_build_alias
7006 } // anon namespace
7008 gimple_opt_pass *
7009 make_pass_build_alias (gcc::context *ctxt)
7011 return new pass_build_alias (ctxt);
7014 /* A dummy pass to cause points-to information to be computed via
7015 TODO_rebuild_alias. */
7017 namespace {
7019 const pass_data pass_data_build_ealias =
7021 GIMPLE_PASS, /* type */
7022 "ealias", /* name */
7023 OPTGROUP_NONE, /* optinfo_flags */
7024 TV_NONE, /* tv_id */
7025 ( PROP_cfg | PROP_ssa ), /* properties_required */
7026 0, /* properties_provided */
7027 0, /* properties_destroyed */
7028 0, /* todo_flags_start */
7029 TODO_rebuild_alias, /* todo_flags_finish */
7032 class pass_build_ealias : public gimple_opt_pass
7034 public:
7035 pass_build_ealias (gcc::context *ctxt)
7036 : gimple_opt_pass (pass_data_build_ealias, ctxt)
7039 /* opt_pass methods: */
7040 virtual bool gate (function *) { return flag_tree_pta; }
7042 }; // class pass_build_ealias
7044 } // anon namespace
7046 gimple_opt_pass *
7047 make_pass_build_ealias (gcc::context *ctxt)
7049 return new pass_build_ealias (ctxt);
7053 /* IPA PTA solutions for ESCAPED. */
7054 struct pt_solution ipa_escaped_pt
7055 = { true, false, false, false, false, false, false, false, NULL };
7057 /* Associate node with varinfo DATA. Worker for
7058 cgraph_for_node_and_aliases. */
7059 static bool
7060 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
7062 if ((node->alias || node->thunk.thunk_p)
7063 && node->analyzed)
7064 insert_vi_for_tree (node->decl, (varinfo_t)data);
7065 return false;
7068 /* Execute the driver for IPA PTA. */
7069 static unsigned int
7070 ipa_pta_execute (void)
7072 struct cgraph_node *node;
7073 varpool_node *var;
7074 int from;
7076 in_ipa_mode = 1;
7078 init_alias_vars ();
7080 if (dump_file && (dump_flags & TDF_DETAILS))
7082 symtab_node::dump_table (dump_file);
7083 fprintf (dump_file, "\n");
7086 /* Build the constraints. */
7087 FOR_EACH_DEFINED_FUNCTION (node)
7089 varinfo_t vi;
7090 /* Nodes without a body are not interesting. Especially do not
7091 visit clones at this point for now - we get duplicate decls
7092 there for inline clones at least. */
7093 if (!node->has_gimple_body_p () || node->global.inlined_to)
7094 continue;
7095 node->get_body ();
7097 gcc_assert (!node->clone_of);
7099 vi = create_function_info_for (node->decl,
7100 alias_get_name (node->decl));
7101 node->call_for_symbol_thunks_and_aliases
7102 (associate_varinfo_to_alias, vi, true);
7105 /* Create constraints for global variables and their initializers. */
7106 FOR_EACH_VARIABLE (var)
7108 if (var->alias && var->analyzed)
7109 continue;
7111 get_vi_for_tree (var->decl);
7114 if (dump_file)
7116 fprintf (dump_file,
7117 "Generating constraints for global initializers\n\n");
7118 dump_constraints (dump_file, 0);
7119 fprintf (dump_file, "\n");
7121 from = constraints.length ();
7123 FOR_EACH_DEFINED_FUNCTION (node)
7125 struct function *func;
7126 basic_block bb;
7128 /* Nodes without a body are not interesting. */
7129 if (!node->has_gimple_body_p () || node->clone_of)
7130 continue;
7132 if (dump_file)
7134 fprintf (dump_file,
7135 "Generating constraints for %s", node->name ());
7136 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
7137 fprintf (dump_file, " (%s)",
7138 IDENTIFIER_POINTER
7139 (DECL_ASSEMBLER_NAME (node->decl)));
7140 fprintf (dump_file, "\n");
7143 func = DECL_STRUCT_FUNCTION (node->decl);
7144 gcc_assert (cfun == NULL);
7146 /* For externally visible or attribute used annotated functions use
7147 local constraints for their arguments.
7148 For local functions we see all callers and thus do not need initial
7149 constraints for parameters. */
7150 if (node->used_from_other_partition
7151 || node->externally_visible
7152 || node->force_output)
7154 intra_create_variable_infos (func);
7156 /* We also need to make function return values escape. Nothing
7157 escapes by returning from main though. */
7158 if (!MAIN_NAME_P (DECL_NAME (node->decl)))
7160 varinfo_t fi, rvi;
7161 fi = lookup_vi_for_tree (node->decl);
7162 rvi = first_vi_for_offset (fi, fi_result);
7163 if (rvi && rvi->offset == fi_result)
7165 struct constraint_expr includes;
7166 struct constraint_expr var;
7167 includes.var = escaped_id;
7168 includes.offset = 0;
7169 includes.type = SCALAR;
7170 var.var = rvi->id;
7171 var.offset = 0;
7172 var.type = SCALAR;
7173 process_constraint (new_constraint (includes, var));
7178 /* Build constriants for the function body. */
7179 FOR_EACH_BB_FN (bb, func)
7181 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7182 gsi_next (&gsi))
7184 gphi *phi = gsi.phi ();
7186 if (! virtual_operand_p (gimple_phi_result (phi)))
7187 find_func_aliases (func, phi);
7190 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
7191 gsi_next (&gsi))
7193 gimple stmt = gsi_stmt (gsi);
7195 find_func_aliases (func, stmt);
7196 find_func_clobbers (func, stmt);
7200 if (dump_file)
7202 fprintf (dump_file, "\n");
7203 dump_constraints (dump_file, from);
7204 fprintf (dump_file, "\n");
7206 from = constraints.length ();
7209 /* From the constraints compute the points-to sets. */
7210 solve_constraints ();
7212 /* Compute the global points-to sets for ESCAPED.
7213 ??? Note that the computed escape set is not correct
7214 for the whole unit as we fail to consider graph edges to
7215 externally visible functions. */
7216 ipa_escaped_pt = find_what_var_points_to (get_varinfo (escaped_id));
7218 /* Make sure the ESCAPED solution (which is used as placeholder in
7219 other solutions) does not reference itself. This simplifies
7220 points-to solution queries. */
7221 ipa_escaped_pt.ipa_escaped = 0;
7223 /* Assign the points-to sets to the SSA names in the unit. */
7224 FOR_EACH_DEFINED_FUNCTION (node)
7226 tree ptr;
7227 struct function *fn;
7228 unsigned i;
7229 basic_block bb;
7231 /* Nodes without a body are not interesting. */
7232 if (!node->has_gimple_body_p () || node->clone_of)
7233 continue;
7235 fn = DECL_STRUCT_FUNCTION (node->decl);
7237 /* Compute the points-to sets for pointer SSA_NAMEs. */
7238 FOR_EACH_VEC_ELT (*fn->gimple_df->ssa_names, i, ptr)
7240 if (ptr
7241 && POINTER_TYPE_P (TREE_TYPE (ptr)))
7242 find_what_p_points_to (ptr);
7245 /* Compute the call-use and call-clobber sets for indirect calls
7246 and calls to external functions. */
7247 FOR_EACH_BB_FN (bb, fn)
7249 gimple_stmt_iterator gsi;
7251 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7253 gcall *stmt;
7254 struct pt_solution *pt;
7255 varinfo_t vi, fi;
7256 tree decl;
7258 stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
7259 if (!stmt)
7260 continue;
7262 /* Handle direct calls to functions with body. */
7263 decl = gimple_call_fndecl (stmt);
7264 if (decl
7265 && (fi = lookup_vi_for_tree (decl))
7266 && fi->is_fn_info)
7268 *gimple_call_clobber_set (stmt)
7269 = find_what_var_points_to
7270 (first_vi_for_offset (fi, fi_clobbers));
7271 *gimple_call_use_set (stmt)
7272 = find_what_var_points_to
7273 (first_vi_for_offset (fi, fi_uses));
7275 /* Handle direct calls to external functions. */
7276 else if (decl)
7278 pt = gimple_call_use_set (stmt);
7279 if (gimple_call_flags (stmt) & ECF_CONST)
7280 memset (pt, 0, sizeof (struct pt_solution));
7281 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7283 *pt = find_what_var_points_to (vi);
7284 /* Escaped (and thus nonlocal) variables are always
7285 implicitly used by calls. */
7286 /* ??? ESCAPED can be empty even though NONLOCAL
7287 always escaped. */
7288 pt->nonlocal = 1;
7289 pt->ipa_escaped = 1;
7291 else
7293 /* If there is nothing special about this call then
7294 we have made everything that is used also escape. */
7295 *pt = ipa_escaped_pt;
7296 pt->nonlocal = 1;
7299 pt = gimple_call_clobber_set (stmt);
7300 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7301 memset (pt, 0, sizeof (struct pt_solution));
7302 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7304 *pt = find_what_var_points_to (vi);
7305 /* Escaped (and thus nonlocal) variables are always
7306 implicitly clobbered by calls. */
7307 /* ??? ESCAPED can be empty even though NONLOCAL
7308 always escaped. */
7309 pt->nonlocal = 1;
7310 pt->ipa_escaped = 1;
7312 else
7314 /* If there is nothing special about this call then
7315 we have made everything that is used also escape. */
7316 *pt = ipa_escaped_pt;
7317 pt->nonlocal = 1;
7320 /* Handle indirect calls. */
7321 else if (!decl
7322 && (fi = get_fi_for_callee (stmt)))
7324 /* We need to accumulate all clobbers/uses of all possible
7325 callees. */
7326 fi = get_varinfo (find (fi->id));
7327 /* If we cannot constrain the set of functions we'll end up
7328 calling we end up using/clobbering everything. */
7329 if (bitmap_bit_p (fi->solution, anything_id)
7330 || bitmap_bit_p (fi->solution, nonlocal_id)
7331 || bitmap_bit_p (fi->solution, escaped_id))
7333 pt_solution_reset (gimple_call_clobber_set (stmt));
7334 pt_solution_reset (gimple_call_use_set (stmt));
7336 else
7338 bitmap_iterator bi;
7339 unsigned i;
7340 struct pt_solution *uses, *clobbers;
7342 uses = gimple_call_use_set (stmt);
7343 clobbers = gimple_call_clobber_set (stmt);
7344 memset (uses, 0, sizeof (struct pt_solution));
7345 memset (clobbers, 0, sizeof (struct pt_solution));
7346 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
7348 struct pt_solution sol;
7350 vi = get_varinfo (i);
7351 if (!vi->is_fn_info)
7353 /* ??? We could be more precise here? */
7354 uses->nonlocal = 1;
7355 uses->ipa_escaped = 1;
7356 clobbers->nonlocal = 1;
7357 clobbers->ipa_escaped = 1;
7358 continue;
7361 if (!uses->anything)
7363 sol = find_what_var_points_to
7364 (first_vi_for_offset (vi, fi_uses));
7365 pt_solution_ior_into (uses, &sol);
7367 if (!clobbers->anything)
7369 sol = find_what_var_points_to
7370 (first_vi_for_offset (vi, fi_clobbers));
7371 pt_solution_ior_into (clobbers, &sol);
7379 fn->gimple_df->ipa_pta = true;
7382 delete_points_to_sets ();
7384 in_ipa_mode = 0;
7386 return 0;
7389 namespace {
7391 const pass_data pass_data_ipa_pta =
7393 SIMPLE_IPA_PASS, /* type */
7394 "pta", /* name */
7395 OPTGROUP_NONE, /* optinfo_flags */
7396 TV_IPA_PTA, /* tv_id */
7397 0, /* properties_required */
7398 0, /* properties_provided */
7399 0, /* properties_destroyed */
7400 0, /* todo_flags_start */
7401 0, /* todo_flags_finish */
7404 class pass_ipa_pta : public simple_ipa_opt_pass
7406 public:
7407 pass_ipa_pta (gcc::context *ctxt)
7408 : simple_ipa_opt_pass (pass_data_ipa_pta, ctxt)
7411 /* opt_pass methods: */
7412 virtual bool gate (function *)
7414 return (optimize
7415 && flag_ipa_pta
7416 /* Don't bother doing anything if the program has errors. */
7417 && !seen_error ());
7420 virtual unsigned int execute (function *) { return ipa_pta_execute (); }
7422 }; // class pass_ipa_pta
7424 } // anon namespace
7426 simple_ipa_opt_pass *
7427 make_pass_ipa_pta (gcc::context *ctxt)
7429 return new pass_ipa_pta (ctxt);