Merge aosp-toolchain/gcc/gcc-4_9 changes.
[official-gcc.git] / gcc-4_9 / gcc / tree-ssa-structalias.c
blobf44667b578975d9855b5d90b71f42020904944da
1 /* Tree based points-to analysis
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dberlin@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "obstack.h"
26 #include "bitmap.h"
27 #include "sbitmap.h"
28 #include "flags.h"
29 #include "basic-block.h"
30 #include "tree.h"
31 #include "stor-layout.h"
32 #include "stmt.h"
33 #include "pointer-set.h"
34 #include "hash-table.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-expr.h"
38 #include "is-a.h"
39 #include "gimple.h"
40 #include "gimple-iterator.h"
41 #include "gimple-ssa.h"
42 #include "cgraph.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
45 #include "tree-into-ssa.h"
46 #include "expr.h"
47 #include "tree-dfa.h"
48 #include "tree-inline.h"
49 #include "diagnostic-core.h"
50 #include "function.h"
51 #include "tree-pass.h"
52 #include "alloc-pool.h"
53 #include "splay-tree.h"
54 #include "params.h"
55 #include "alias.h"
56 #include "tree-phinodes.h"
57 #include "ssa-iterators.h"
58 #include "tree-pretty-print.h"
59 #include "gimple-walk.h"
61 /* The idea behind this analyzer is to generate set constraints from the
62 program, then solve the resulting constraints in order to generate the
63 points-to sets.
65 Set constraints are a way of modeling program analysis problems that
66 involve sets. They consist of an inclusion constraint language,
67 describing the variables (each variable is a set) and operations that
68 are involved on the variables, and a set of rules that derive facts
69 from these operations. To solve a system of set constraints, you derive
70 all possible facts under the rules, which gives you the correct sets
71 as a consequence.
73 See "Efficient Field-sensitive pointer analysis for C" by "David
74 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
75 http://citeseer.ist.psu.edu/pearce04efficient.html
77 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
78 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
79 http://citeseer.ist.psu.edu/heintze01ultrafast.html
81 There are three types of real constraint expressions, DEREF,
82 ADDRESSOF, and SCALAR. Each constraint expression consists
83 of a constraint type, a variable, and an offset.
85 SCALAR is a constraint expression type used to represent x, whether
86 it appears on the LHS or the RHS of a statement.
87 DEREF is a constraint expression type used to represent *x, whether
88 it appears on the LHS or the RHS of a statement.
89 ADDRESSOF is a constraint expression used to represent &x, whether
90 it appears on the LHS or the RHS of a statement.
92 Each pointer variable in the program is assigned an integer id, and
93 each field of a structure variable is assigned an integer id as well.
95 Structure variables are linked to their list of fields through a "next
96 field" in each variable that points to the next field in offset
97 order.
98 Each variable for a structure field has
100 1. "size", that tells the size in bits of that field.
101 2. "fullsize, that tells the size in bits of the entire structure.
102 3. "offset", that tells the offset in bits from the beginning of the
103 structure to this field.
105 Thus,
106 struct f
108 int a;
109 int b;
110 } foo;
111 int *bar;
113 looks like
115 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
116 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
117 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
120 In order to solve the system of set constraints, the following is
121 done:
123 1. Each constraint variable x has a solution set associated with it,
124 Sol(x).
126 2. Constraints are separated into direct, copy, and complex.
127 Direct constraints are ADDRESSOF constraints that require no extra
128 processing, such as P = &Q
129 Copy constraints are those of the form P = Q.
130 Complex constraints are all the constraints involving dereferences
131 and offsets (including offsetted copies).
133 3. All direct constraints of the form P = &Q are processed, such
134 that Q is added to Sol(P)
136 4. All complex constraints for a given constraint variable are stored in a
137 linked list attached to that variable's node.
139 5. A directed graph is built out of the copy constraints. Each
140 constraint variable is a node in the graph, and an edge from
141 Q to P is added for each copy constraint of the form P = Q
143 6. The graph is then walked, and solution sets are
144 propagated along the copy edges, such that an edge from Q to P
145 causes Sol(P) <- Sol(P) union Sol(Q).
147 7. As we visit each node, all complex constraints associated with
148 that node are processed by adding appropriate copy edges to the graph, or the
149 appropriate variables to the solution set.
151 8. The process of walking the graph is iterated until no solution
152 sets change.
154 Prior to walking the graph in steps 6 and 7, We perform static
155 cycle elimination on the constraint graph, as well
156 as off-line variable substitution.
158 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
159 on and turned into anything), but isn't. You can just see what offset
160 inside the pointed-to struct it's going to access.
162 TODO: Constant bounded arrays can be handled as if they were structs of the
163 same number of elements.
165 TODO: Modeling heap and incoming pointers becomes much better if we
166 add fields to them as we discover them, which we could do.
168 TODO: We could handle unions, but to be honest, it's probably not
169 worth the pain or slowdown. */
171 /* IPA-PTA optimizations possible.
173 When the indirect function called is ANYTHING we can add disambiguation
174 based on the function signatures (or simply the parameter count which
175 is the varinfo size). We also do not need to consider functions that
176 do not have their address taken.
178 The is_global_var bit which marks escape points is overly conservative
179 in IPA mode. Split it to is_escape_point and is_global_var - only
180 externally visible globals are escape points in IPA mode. This is
181 also needed to fix the pt_solution_includes_global predicate
182 (and thus ptr_deref_may_alias_global_p).
184 The way we introduce DECL_PT_UID to avoid fixing up all points-to
185 sets in the translation unit when we copy a DECL during inlining
186 pessimizes precision. The advantage is that the DECL_PT_UID keeps
187 compile-time and memory usage overhead low - the points-to sets
188 do not grow or get unshared as they would during a fixup phase.
189 An alternative solution is to delay IPA PTA until after all
190 inlining transformations have been applied.
192 The way we propagate clobber/use information isn't optimized.
193 It should use a new complex constraint that properly filters
194 out local variables of the callee (though that would make
195 the sets invalid after inlining). OTOH we might as well
196 admit defeat to WHOPR and simply do all the clobber/use analysis
197 and propagation after PTA finished but before we threw away
198 points-to information for memory variables. WHOPR and PTA
199 do not play along well anyway - the whole constraint solving
200 would need to be done in WPA phase and it will be very interesting
201 to apply the results to local SSA names during LTRANS phase.
203 We probably should compute a per-function unit-ESCAPE solution
204 propagating it simply like the clobber / uses solutions. The
205 solution can go alongside the non-IPA espaced solution and be
206 used to query which vars escape the unit through a function.
208 We never put function decls in points-to sets so we do not
209 keep the set of called functions for indirect calls.
211 And probably more. */
213 static bool use_field_sensitive = true;
214 static int in_ipa_mode = 0;
216 /* Used for predecessor bitmaps. */
217 static bitmap_obstack predbitmap_obstack;
219 /* Used for points-to sets. */
220 static bitmap_obstack pta_obstack;
222 /* Used for oldsolution members of variables. */
223 static bitmap_obstack oldpta_obstack;
225 /* Used for per-solver-iteration bitmaps. */
226 static bitmap_obstack iteration_obstack;
228 static unsigned int create_variable_info_for (tree, const char *);
229 typedef struct constraint_graph *constraint_graph_t;
230 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
232 struct constraint;
233 typedef struct constraint *constraint_t;
236 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
237 if (a) \
238 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
240 static struct constraint_stats
242 unsigned int total_vars;
243 unsigned int nonpointer_vars;
244 unsigned int unified_vars_static;
245 unsigned int unified_vars_dynamic;
246 unsigned int iterations;
247 unsigned int num_edges;
248 unsigned int num_implicit_edges;
249 unsigned int points_to_sets_created;
250 } stats;
252 struct variable_info
254 /* ID of this variable */
255 unsigned int id;
257 /* True if this is a variable created by the constraint analysis, such as
258 heap variables and constraints we had to break up. */
259 unsigned int is_artificial_var : 1;
261 /* True if this is a special variable whose solution set should not be
262 changed. */
263 unsigned int is_special_var : 1;
265 /* True for variables whose size is not known or variable. */
266 unsigned int is_unknown_size_var : 1;
268 /* True for (sub-)fields that represent a whole variable. */
269 unsigned int is_full_var : 1;
271 /* True if this is a heap variable. */
272 unsigned int is_heap_var : 1;
274 /* True if this field may contain pointers. */
275 unsigned int may_have_pointers : 1;
277 /* True if this field has only restrict qualified pointers. */
278 unsigned int only_restrict_pointers : 1;
280 /* True if this represents a heap var created for a restrict qualified
281 pointer. */
282 unsigned int is_restrict_var : 1;
284 /* True if this represents a global variable. */
285 unsigned int is_global_var : 1;
287 /* True if this represents a IPA function info. */
288 unsigned int is_fn_info : 1;
290 /* ??? Store somewhere better. */
291 unsigned short ruid;
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 pointer_map_t *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, readonly_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_restrict_var = false;
385 ret->is_global_var = (t == NULL_TREE);
386 ret->is_fn_info = false;
387 if (t && DECL_P (t))
388 ret->is_global_var = (is_global_var (t)
389 /* We have to treat even local register variables
390 as escape points. */
391 || (TREE_CODE (t) == VAR_DECL
392 && DECL_HARD_REGISTER (t)));
393 ret->solution = BITMAP_ALLOC (&pta_obstack);
394 ret->oldsolution = NULL;
395 ret->next = 0;
396 ret->head = ret->id;
398 stats.total_vars++;
400 varmap.safe_push (ret);
402 return ret;
406 /* A map mapping call statements to per-stmt variables for uses
407 and clobbers specific to the call. */
408 static struct pointer_map_t *call_stmt_vars;
410 /* Lookup or create the variable for the call statement CALL. */
412 static varinfo_t
413 get_call_vi (gimple call)
415 void **slot_p;
416 varinfo_t vi, vi2;
418 slot_p = pointer_map_insert (call_stmt_vars, call);
419 if (*slot_p)
420 return (varinfo_t) *slot_p;
422 vi = new_var_info (NULL_TREE, "CALLUSED");
423 vi->offset = 0;
424 vi->size = 1;
425 vi->fullsize = 2;
426 vi->is_full_var = true;
428 vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED");
429 vi2->offset = 1;
430 vi2->size = 1;
431 vi2->fullsize = 2;
432 vi2->is_full_var = true;
434 vi->next = vi2->id;
436 *slot_p = (void *) vi;
437 return vi;
440 /* Lookup the variable for the call statement CALL representing
441 the uses. Returns NULL if there is nothing special about this call. */
443 static varinfo_t
444 lookup_call_use_vi (gimple call)
446 void **slot_p;
448 slot_p = pointer_map_contains (call_stmt_vars, call);
449 if (slot_p)
450 return (varinfo_t) *slot_p;
452 return NULL;
455 /* Lookup the variable for the call statement CALL representing
456 the clobbers. Returns NULL if there is nothing special about this call. */
458 static varinfo_t
459 lookup_call_clobber_vi (gimple call)
461 varinfo_t uses = lookup_call_use_vi (call);
462 if (!uses)
463 return NULL;
465 return vi_next (uses);
468 /* Lookup or create the variable for the call statement CALL representing
469 the uses. */
471 static varinfo_t
472 get_call_use_vi (gimple call)
474 return get_call_vi (call);
477 /* Lookup or create the variable for the call statement CALL representing
478 the clobbers. */
480 static varinfo_t ATTRIBUTE_UNUSED
481 get_call_clobber_vi (gimple call)
483 return vi_next (get_call_vi (call));
487 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
489 /* An expression that appears in a constraint. */
491 struct constraint_expr
493 /* Constraint type. */
494 constraint_expr_type type;
496 /* Variable we are referring to in the constraint. */
497 unsigned int var;
499 /* Offset, in bits, of this constraint from the beginning of
500 variables it ends up referring to.
502 IOW, in a deref constraint, we would deref, get the result set,
503 then add OFFSET to each member. */
504 HOST_WIDE_INT offset;
507 /* Use 0x8000... as special unknown offset. */
508 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
510 typedef struct constraint_expr ce_s;
511 static void get_constraint_for_1 (tree, vec<ce_s> *, bool, bool);
512 static void get_constraint_for (tree, vec<ce_s> *);
513 static void get_constraint_for_rhs (tree, vec<ce_s> *);
514 static void do_deref (vec<ce_s> *);
516 /* Our set constraints are made up of two constraint expressions, one
517 LHS, and one RHS.
519 As described in the introduction, our set constraints each represent an
520 operation between set valued variables.
522 struct constraint
524 struct constraint_expr lhs;
525 struct constraint_expr rhs;
528 /* List of constraints that we use to build the constraint graph from. */
530 static vec<constraint_t> constraints;
531 static alloc_pool constraint_pool;
533 /* The constraint graph is represented as an array of bitmaps
534 containing successor nodes. */
536 struct constraint_graph
538 /* Size of this graph, which may be different than the number of
539 nodes in the variable map. */
540 unsigned int size;
542 /* Explicit successors of each node. */
543 bitmap *succs;
545 /* Implicit predecessors of each node (Used for variable
546 substitution). */
547 bitmap *implicit_preds;
549 /* Explicit predecessors of each node (Used for variable substitution). */
550 bitmap *preds;
552 /* Indirect cycle representatives, or -1 if the node has no indirect
553 cycles. */
554 int *indirect_cycles;
556 /* Representative node for a node. rep[a] == a unless the node has
557 been unified. */
558 unsigned int *rep;
560 /* Equivalence class representative for a label. This is used for
561 variable substitution. */
562 int *eq_rep;
564 /* Pointer equivalence label for a node. All nodes with the same
565 pointer equivalence label can be unified together at some point
566 (either during constraint optimization or after the constraint
567 graph is built). */
568 unsigned int *pe;
570 /* Pointer equivalence representative for a label. This is used to
571 handle nodes that are pointer equivalent but not location
572 equivalent. We can unite these once the addressof constraints
573 are transformed into initial points-to sets. */
574 int *pe_rep;
576 /* Pointer equivalence label for each node, used during variable
577 substitution. */
578 unsigned int *pointer_label;
580 /* Location equivalence label for each node, used during location
581 equivalence finding. */
582 unsigned int *loc_label;
584 /* Pointed-by set for each node, used during location equivalence
585 finding. This is pointed-by rather than pointed-to, because it
586 is constructed using the predecessor graph. */
587 bitmap *pointed_by;
589 /* Points to sets for pointer equivalence. This is *not* the actual
590 points-to sets for nodes. */
591 bitmap *points_to;
593 /* Bitmap of nodes where the bit is set if the node is a direct
594 node. Used for variable substitution. */
595 sbitmap direct_nodes;
597 /* Bitmap of nodes where the bit is set if the node is address
598 taken. Used for variable substitution. */
599 bitmap address_taken;
601 /* Vector of complex constraints for each graph node. Complex
602 constraints are those involving dereferences or offsets that are
603 not 0. */
604 vec<constraint_t> *complex;
607 static constraint_graph_t graph;
609 /* During variable substitution and the offline version of indirect
610 cycle finding, we create nodes to represent dereferences and
611 address taken constraints. These represent where these start and
612 end. */
613 #define FIRST_REF_NODE (varmap).length ()
614 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
616 /* Return the representative node for NODE, if NODE has been unioned
617 with another NODE.
618 This function performs path compression along the way to finding
619 the representative. */
621 static unsigned int
622 find (unsigned int node)
624 gcc_checking_assert (node < graph->size);
625 if (graph->rep[node] != node)
626 return graph->rep[node] = find (graph->rep[node]);
627 return node;
630 /* Union the TO and FROM nodes to the TO nodes.
631 Note that at some point in the future, we may want to do
632 union-by-rank, in which case we are going to have to return the
633 node we unified to. */
635 static bool
636 unite (unsigned int to, unsigned int from)
638 gcc_checking_assert (to < graph->size && from < graph->size);
639 if (to != from && graph->rep[from] != to)
641 graph->rep[from] = to;
642 return true;
644 return false;
647 /* Create a new constraint consisting of LHS and RHS expressions. */
649 static constraint_t
650 new_constraint (const struct constraint_expr lhs,
651 const struct constraint_expr rhs)
653 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
654 ret->lhs = lhs;
655 ret->rhs = rhs;
656 return ret;
659 /* Print out constraint C to FILE. */
661 static void
662 dump_constraint (FILE *file, constraint_t c)
664 if (c->lhs.type == ADDRESSOF)
665 fprintf (file, "&");
666 else if (c->lhs.type == DEREF)
667 fprintf (file, "*");
668 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
669 if (c->lhs.offset == UNKNOWN_OFFSET)
670 fprintf (file, " + UNKNOWN");
671 else if (c->lhs.offset != 0)
672 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
673 fprintf (file, " = ");
674 if (c->rhs.type == ADDRESSOF)
675 fprintf (file, "&");
676 else if (c->rhs.type == DEREF)
677 fprintf (file, "*");
678 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
679 if (c->rhs.offset == UNKNOWN_OFFSET)
680 fprintf (file, " + UNKNOWN");
681 else if (c->rhs.offset != 0)
682 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
686 void debug_constraint (constraint_t);
687 void debug_constraints (void);
688 void debug_constraint_graph (void);
689 void debug_solution_for_var (unsigned int);
690 void debug_sa_points_to_info (void);
692 /* Print out constraint C to stderr. */
694 DEBUG_FUNCTION void
695 debug_constraint (constraint_t c)
697 dump_constraint (stderr, c);
698 fprintf (stderr, "\n");
701 /* Print out all constraints to FILE */
703 static void
704 dump_constraints (FILE *file, int from)
706 int i;
707 constraint_t c;
708 for (i = from; constraints.iterate (i, &c); i++)
709 if (c)
711 dump_constraint (file, c);
712 fprintf (file, "\n");
716 /* Print out all constraints to stderr. */
718 DEBUG_FUNCTION void
719 debug_constraints (void)
721 dump_constraints (stderr, 0);
724 /* Print the constraint graph in dot format. */
726 static void
727 dump_constraint_graph (FILE *file)
729 unsigned int i;
731 /* Only print the graph if it has already been initialized: */
732 if (!graph)
733 return;
735 /* Prints the header of the dot file: */
736 fprintf (file, "strict digraph {\n");
737 fprintf (file, " node [\n shape = box\n ]\n");
738 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
739 fprintf (file, "\n // List of nodes and complex constraints in "
740 "the constraint graph:\n");
742 /* The next lines print the nodes in the graph together with the
743 complex constraints attached to them. */
744 for (i = 1; i < graph->size; i++)
746 if (i == FIRST_REF_NODE)
747 continue;
748 if (find (i) != i)
749 continue;
750 if (i < FIRST_REF_NODE)
751 fprintf (file, "\"%s\"", get_varinfo (i)->name);
752 else
753 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
754 if (graph->complex[i].exists ())
756 unsigned j;
757 constraint_t c;
758 fprintf (file, " [label=\"\\N\\n");
759 for (j = 0; graph->complex[i].iterate (j, &c); ++j)
761 dump_constraint (file, c);
762 fprintf (file, "\\l");
764 fprintf (file, "\"]");
766 fprintf (file, ";\n");
769 /* Go over the edges. */
770 fprintf (file, "\n // Edges in the constraint graph:\n");
771 for (i = 1; i < graph->size; i++)
773 unsigned j;
774 bitmap_iterator bi;
775 if (find (i) != i)
776 continue;
777 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
779 unsigned to = find (j);
780 if (i == to)
781 continue;
782 if (i < FIRST_REF_NODE)
783 fprintf (file, "\"%s\"", get_varinfo (i)->name);
784 else
785 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
786 fprintf (file, " -> ");
787 if (to < FIRST_REF_NODE)
788 fprintf (file, "\"%s\"", get_varinfo (to)->name);
789 else
790 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name);
791 fprintf (file, ";\n");
795 /* Prints the tail of the dot file. */
796 fprintf (file, "}\n");
799 /* Print out the constraint graph to stderr. */
801 DEBUG_FUNCTION void
802 debug_constraint_graph (void)
804 dump_constraint_graph (stderr);
807 /* SOLVER FUNCTIONS
809 The solver is a simple worklist solver, that works on the following
810 algorithm:
812 sbitmap changed_nodes = all zeroes;
813 changed_count = 0;
814 For each node that is not already collapsed:
815 changed_count++;
816 set bit in changed nodes
818 while (changed_count > 0)
820 compute topological ordering for constraint graph
822 find and collapse cycles in the constraint graph (updating
823 changed if necessary)
825 for each node (n) in the graph in topological order:
826 changed_count--;
828 Process each complex constraint associated with the node,
829 updating changed if necessary.
831 For each outgoing edge from n, propagate the solution from n to
832 the destination of the edge, updating changed as necessary.
834 } */
836 /* Return true if two constraint expressions A and B are equal. */
838 static bool
839 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
841 return a.type == b.type && a.var == b.var && a.offset == b.offset;
844 /* Return true if constraint expression A is less than constraint expression
845 B. This is just arbitrary, but consistent, in order to give them an
846 ordering. */
848 static bool
849 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
851 if (a.type == b.type)
853 if (a.var == b.var)
854 return a.offset < b.offset;
855 else
856 return a.var < b.var;
858 else
859 return a.type < b.type;
862 /* Return true if constraint A is less than constraint B. This is just
863 arbitrary, but consistent, in order to give them an ordering. */
865 static bool
866 constraint_less (const constraint_t &a, const constraint_t &b)
868 if (constraint_expr_less (a->lhs, b->lhs))
869 return true;
870 else if (constraint_expr_less (b->lhs, a->lhs))
871 return false;
872 else
873 return constraint_expr_less (a->rhs, b->rhs);
876 /* Return true if two constraints A and B are equal. */
878 static bool
879 constraint_equal (struct constraint a, struct constraint b)
881 return constraint_expr_equal (a.lhs, b.lhs)
882 && constraint_expr_equal (a.rhs, b.rhs);
886 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
888 static constraint_t
889 constraint_vec_find (vec<constraint_t> vec,
890 struct constraint lookfor)
892 unsigned int place;
893 constraint_t found;
895 if (!vec.exists ())
896 return NULL;
898 place = vec.lower_bound (&lookfor, constraint_less);
899 if (place >= vec.length ())
900 return NULL;
901 found = vec[place];
902 if (!constraint_equal (*found, lookfor))
903 return NULL;
904 return found;
907 /* Union two constraint vectors, TO and FROM. Put the result in TO.
908 Returns true of TO set is changed. */
910 static bool
911 constraint_set_union (vec<constraint_t> *to,
912 vec<constraint_t> *from)
914 int i;
915 constraint_t c;
916 bool any_change = false;
918 FOR_EACH_VEC_ELT (*from, i, c)
920 if (constraint_vec_find (*to, *c) == NULL)
922 unsigned int place = to->lower_bound (c, constraint_less);
923 to->safe_insert (place, c);
924 any_change = true;
927 return any_change;
930 /* Expands the solution in SET to all sub-fields of variables included. */
932 static bitmap
933 solution_set_expand (bitmap set, bitmap *expanded)
935 bitmap_iterator bi;
936 unsigned j;
938 if (*expanded)
939 return *expanded;
941 *expanded = BITMAP_ALLOC (&iteration_obstack);
943 /* In a first pass expand to the head of the variables we need to
944 add all sub-fields off. This avoids quadratic behavior. */
945 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
947 varinfo_t v = get_varinfo (j);
948 if (v->is_artificial_var
949 || v->is_full_var)
950 continue;
951 bitmap_set_bit (*expanded, v->head);
954 /* In the second pass now expand all head variables with subfields. */
955 EXECUTE_IF_SET_IN_BITMAP (*expanded, 0, j, bi)
957 varinfo_t v = get_varinfo (j);
958 if (v->head != j)
959 continue;
960 for (v = vi_next (v); v != NULL; v = vi_next (v))
961 bitmap_set_bit (*expanded, v->id);
964 /* And finally set the rest of the bits from SET. */
965 bitmap_ior_into (*expanded, set);
967 return *expanded;
970 /* Union solution sets TO and DELTA, and add INC to each member of DELTA in the
971 process. */
973 static bool
974 set_union_with_increment (bitmap to, bitmap delta, HOST_WIDE_INT inc,
975 bitmap *expanded_delta)
977 bool changed = false;
978 bitmap_iterator bi;
979 unsigned int i;
981 /* If the solution of DELTA contains anything it is good enough to transfer
982 this to TO. */
983 if (bitmap_bit_p (delta, anything_id))
984 return bitmap_set_bit (to, anything_id);
986 /* If the offset is unknown we have to expand the solution to
987 all subfields. */
988 if (inc == UNKNOWN_OFFSET)
990 delta = solution_set_expand (delta, expanded_delta);
991 changed |= bitmap_ior_into (to, delta);
992 return changed;
995 /* For non-zero offset union the offsetted solution into the destination. */
996 EXECUTE_IF_SET_IN_BITMAP (delta, 0, i, bi)
998 varinfo_t vi = get_varinfo (i);
1000 /* If this is a variable with just one field just set its bit
1001 in the result. */
1002 if (vi->is_artificial_var
1003 || vi->is_unknown_size_var
1004 || vi->is_full_var)
1005 changed |= bitmap_set_bit (to, i);
1006 else
1008 HOST_WIDE_INT fieldoffset = vi->offset + inc;
1009 unsigned HOST_WIDE_INT size = vi->size;
1011 /* If the offset makes the pointer point to before the
1012 variable use offset zero for the field lookup. */
1013 if (fieldoffset < 0)
1014 vi = get_varinfo (vi->head);
1015 else
1016 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
1020 changed |= bitmap_set_bit (to, vi->id);
1021 if (vi->is_full_var
1022 || vi->next == 0)
1023 break;
1025 /* We have to include all fields that overlap the current field
1026 shifted by inc. */
1027 vi = vi_next (vi);
1029 while (vi->offset < fieldoffset + size);
1033 return changed;
1036 /* Insert constraint C into the list of complex constraints for graph
1037 node VAR. */
1039 static void
1040 insert_into_complex (constraint_graph_t graph,
1041 unsigned int var, constraint_t c)
1043 vec<constraint_t> complex = graph->complex[var];
1044 unsigned int place = complex.lower_bound (c, constraint_less);
1046 /* Only insert constraints that do not already exist. */
1047 if (place >= complex.length ()
1048 || !constraint_equal (*c, *complex[place]))
1049 graph->complex[var].safe_insert (place, c);
1053 /* Condense two variable nodes into a single variable node, by moving
1054 all associated info from FROM to TO. Returns true if TO node's
1055 constraint set changes after the merge. */
1057 static bool
1058 merge_node_constraints (constraint_graph_t graph, unsigned int to,
1059 unsigned int from)
1061 unsigned int i;
1062 constraint_t c;
1063 bool any_change = false;
1065 gcc_checking_assert (find (from) == to);
1067 /* Move all complex constraints from src node into to node */
1068 FOR_EACH_VEC_ELT (graph->complex[from], i, c)
1070 /* In complex constraints for node FROM, we may have either
1071 a = *FROM, and *FROM = a, or an offseted constraint which are
1072 always added to the rhs node's constraints. */
1074 if (c->rhs.type == DEREF)
1075 c->rhs.var = to;
1076 else if (c->lhs.type == DEREF)
1077 c->lhs.var = to;
1078 else
1079 c->rhs.var = to;
1082 any_change = constraint_set_union (&graph->complex[to],
1083 &graph->complex[from]);
1084 graph->complex[from].release ();
1085 return any_change;
1089 /* Remove edges involving NODE from GRAPH. */
1091 static void
1092 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
1094 if (graph->succs[node])
1095 BITMAP_FREE (graph->succs[node]);
1098 /* Merge GRAPH nodes FROM and TO into node TO. */
1100 static void
1101 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1102 unsigned int from)
1104 if (graph->indirect_cycles[from] != -1)
1106 /* If we have indirect cycles with the from node, and we have
1107 none on the to node, the to node has indirect cycles from the
1108 from node now that they are unified.
1109 If indirect cycles exist on both, unify the nodes that they
1110 are in a cycle with, since we know they are in a cycle with
1111 each other. */
1112 if (graph->indirect_cycles[to] == -1)
1113 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1116 /* Merge all the successor edges. */
1117 if (graph->succs[from])
1119 if (!graph->succs[to])
1120 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1121 bitmap_ior_into (graph->succs[to],
1122 graph->succs[from]);
1125 clear_edges_for_node (graph, from);
1129 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1130 it doesn't exist in the graph already. */
1132 static void
1133 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1134 unsigned int from)
1136 if (to == from)
1137 return;
1139 if (!graph->implicit_preds[to])
1140 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1142 if (bitmap_set_bit (graph->implicit_preds[to], from))
1143 stats.num_implicit_edges++;
1146 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1147 it doesn't exist in the graph already.
1148 Return false if the edge already existed, true otherwise. */
1150 static void
1151 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1152 unsigned int from)
1154 if (!graph->preds[to])
1155 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1156 bitmap_set_bit (graph->preds[to], from);
1159 /* Add a graph edge to GRAPH, going from FROM to TO if
1160 it doesn't exist in the graph already.
1161 Return false if the edge already existed, true otherwise. */
1163 static bool
1164 add_graph_edge (constraint_graph_t graph, unsigned int to,
1165 unsigned int from)
1167 if (to == from)
1169 return false;
1171 else
1173 bool r = false;
1175 if (!graph->succs[from])
1176 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1177 if (bitmap_set_bit (graph->succs[from], to))
1179 r = true;
1180 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1181 stats.num_edges++;
1183 return r;
1188 /* Initialize the constraint graph structure to contain SIZE nodes. */
1190 static void
1191 init_graph (unsigned int size)
1193 unsigned int j;
1195 graph = XCNEW (struct constraint_graph);
1196 graph->size = size;
1197 graph->succs = XCNEWVEC (bitmap, graph->size);
1198 graph->indirect_cycles = XNEWVEC (int, graph->size);
1199 graph->rep = XNEWVEC (unsigned int, graph->size);
1200 /* ??? Macros do not support template types with multiple arguments,
1201 so we use a typedef to work around it. */
1202 typedef vec<constraint_t> vec_constraint_t_heap;
1203 graph->complex = XCNEWVEC (vec_constraint_t_heap, size);
1204 graph->pe = XCNEWVEC (unsigned int, graph->size);
1205 graph->pe_rep = XNEWVEC (int, graph->size);
1207 for (j = 0; j < graph->size; j++)
1209 graph->rep[j] = j;
1210 graph->pe_rep[j] = -1;
1211 graph->indirect_cycles[j] = -1;
1215 /* Build the constraint graph, adding only predecessor edges right now. */
1217 static void
1218 build_pred_graph (void)
1220 int i;
1221 constraint_t c;
1222 unsigned int j;
1224 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1225 graph->preds = XCNEWVEC (bitmap, graph->size);
1226 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1227 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1228 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1229 graph->points_to = XCNEWVEC (bitmap, graph->size);
1230 graph->eq_rep = XNEWVEC (int, graph->size);
1231 graph->direct_nodes = sbitmap_alloc (graph->size);
1232 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1233 bitmap_clear (graph->direct_nodes);
1235 for (j = 1; j < FIRST_REF_NODE; j++)
1237 if (!get_varinfo (j)->is_special_var)
1238 bitmap_set_bit (graph->direct_nodes, j);
1241 for (j = 0; j < graph->size; j++)
1242 graph->eq_rep[j] = -1;
1244 for (j = 0; j < varmap.length (); j++)
1245 graph->indirect_cycles[j] = -1;
1247 FOR_EACH_VEC_ELT (constraints, i, c)
1249 struct constraint_expr lhs = c->lhs;
1250 struct constraint_expr rhs = c->rhs;
1251 unsigned int lhsvar = lhs.var;
1252 unsigned int rhsvar = rhs.var;
1254 if (lhs.type == DEREF)
1256 /* *x = y. */
1257 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1258 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1260 else if (rhs.type == DEREF)
1262 /* x = *y */
1263 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1264 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1265 else
1266 bitmap_clear_bit (graph->direct_nodes, lhsvar);
1268 else if (rhs.type == ADDRESSOF)
1270 varinfo_t v;
1272 /* x = &y */
1273 if (graph->points_to[lhsvar] == NULL)
1274 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1275 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1277 if (graph->pointed_by[rhsvar] == NULL)
1278 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1279 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1281 /* Implicitly, *x = y */
1282 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1284 /* All related variables are no longer direct nodes. */
1285 bitmap_clear_bit (graph->direct_nodes, rhsvar);
1286 v = get_varinfo (rhsvar);
1287 if (!v->is_full_var)
1289 v = get_varinfo (v->head);
1292 bitmap_clear_bit (graph->direct_nodes, v->id);
1293 v = vi_next (v);
1295 while (v != NULL);
1297 bitmap_set_bit (graph->address_taken, rhsvar);
1299 else if (lhsvar > anything_id
1300 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1302 /* x = y */
1303 add_pred_graph_edge (graph, lhsvar, rhsvar);
1304 /* Implicitly, *x = *y */
1305 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1306 FIRST_REF_NODE + rhsvar);
1308 else if (lhs.offset != 0 || rhs.offset != 0)
1310 if (rhs.offset != 0)
1311 bitmap_clear_bit (graph->direct_nodes, lhs.var);
1312 else if (lhs.offset != 0)
1313 bitmap_clear_bit (graph->direct_nodes, rhs.var);
1318 /* Build the constraint graph, adding successor edges. */
1320 static void
1321 build_succ_graph (void)
1323 unsigned i, t;
1324 constraint_t c;
1326 FOR_EACH_VEC_ELT (constraints, i, c)
1328 struct constraint_expr lhs;
1329 struct constraint_expr rhs;
1330 unsigned int lhsvar;
1331 unsigned int rhsvar;
1333 if (!c)
1334 continue;
1336 lhs = c->lhs;
1337 rhs = c->rhs;
1338 lhsvar = find (lhs.var);
1339 rhsvar = find (rhs.var);
1341 if (lhs.type == DEREF)
1343 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1344 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1346 else if (rhs.type == DEREF)
1348 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1349 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1351 else if (rhs.type == ADDRESSOF)
1353 /* x = &y */
1354 gcc_checking_assert (find (rhs.var) == rhs.var);
1355 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1357 else if (lhsvar > anything_id
1358 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1360 add_graph_edge (graph, lhsvar, rhsvar);
1364 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1365 receive pointers. */
1366 t = find (storedanything_id);
1367 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1369 if (!bitmap_bit_p (graph->direct_nodes, i)
1370 && get_varinfo (i)->may_have_pointers)
1371 add_graph_edge (graph, find (i), t);
1374 /* Everything stored to ANYTHING also potentially escapes. */
1375 add_graph_edge (graph, find (escaped_id), t);
1379 /* Changed variables on the last iteration. */
1380 static bitmap changed;
1382 /* Strongly Connected Component visitation info. */
1384 struct scc_info
1386 sbitmap visited;
1387 sbitmap deleted;
1388 unsigned int *dfs;
1389 unsigned int *node_mapping;
1390 int current_index;
1391 vec<unsigned> scc_stack;
1395 /* Recursive routine to find strongly connected components in GRAPH.
1396 SI is the SCC info to store the information in, and N is the id of current
1397 graph node we are processing.
1399 This is Tarjan's strongly connected component finding algorithm, as
1400 modified by Nuutila to keep only non-root nodes on the stack.
1401 The algorithm can be found in "On finding the strongly connected
1402 connected components in a directed graph" by Esko Nuutila and Eljas
1403 Soisalon-Soininen, in Information Processing Letters volume 49,
1404 number 1, pages 9-14. */
1406 static void
1407 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1409 unsigned int i;
1410 bitmap_iterator bi;
1411 unsigned int my_dfs;
1413 bitmap_set_bit (si->visited, n);
1414 si->dfs[n] = si->current_index ++;
1415 my_dfs = si->dfs[n];
1417 /* Visit all the successors. */
1418 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1420 unsigned int w;
1422 if (i > LAST_REF_NODE)
1423 break;
1425 w = find (i);
1426 if (bitmap_bit_p (si->deleted, w))
1427 continue;
1429 if (!bitmap_bit_p (si->visited, w))
1430 scc_visit (graph, si, w);
1432 unsigned int t = find (w);
1433 gcc_checking_assert (find (n) == n);
1434 if (si->dfs[t] < si->dfs[n])
1435 si->dfs[n] = si->dfs[t];
1438 /* See if any components have been identified. */
1439 if (si->dfs[n] == my_dfs)
1441 if (si->scc_stack.length () > 0
1442 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1444 bitmap scc = BITMAP_ALLOC (NULL);
1445 unsigned int lowest_node;
1446 bitmap_iterator bi;
1448 bitmap_set_bit (scc, n);
1450 while (si->scc_stack.length () != 0
1451 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1453 unsigned int w = si->scc_stack.pop ();
1455 bitmap_set_bit (scc, w);
1458 lowest_node = bitmap_first_set_bit (scc);
1459 gcc_assert (lowest_node < FIRST_REF_NODE);
1461 /* Collapse the SCC nodes into a single node, and mark the
1462 indirect cycles. */
1463 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1465 if (i < FIRST_REF_NODE)
1467 if (unite (lowest_node, i))
1468 unify_nodes (graph, lowest_node, i, false);
1470 else
1472 unite (lowest_node, i);
1473 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1477 bitmap_set_bit (si->deleted, n);
1479 else
1480 si->scc_stack.safe_push (n);
1483 /* Unify node FROM into node TO, updating the changed count if
1484 necessary when UPDATE_CHANGED is true. */
1486 static void
1487 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1488 bool update_changed)
1490 gcc_checking_assert (to != from && find (to) == to);
1492 if (dump_file && (dump_flags & TDF_DETAILS))
1493 fprintf (dump_file, "Unifying %s to %s\n",
1494 get_varinfo (from)->name,
1495 get_varinfo (to)->name);
1497 if (update_changed)
1498 stats.unified_vars_dynamic++;
1499 else
1500 stats.unified_vars_static++;
1502 merge_graph_nodes (graph, to, from);
1503 if (merge_node_constraints (graph, to, from))
1505 if (update_changed)
1506 bitmap_set_bit (changed, to);
1509 /* Mark TO as changed if FROM was changed. If TO was already marked
1510 as changed, decrease the changed count. */
1512 if (update_changed
1513 && bitmap_clear_bit (changed, from))
1514 bitmap_set_bit (changed, to);
1515 varinfo_t fromvi = get_varinfo (from);
1516 if (fromvi->solution)
1518 /* If the solution changes because of the merging, we need to mark
1519 the variable as changed. */
1520 varinfo_t tovi = get_varinfo (to);
1521 if (bitmap_ior_into (tovi->solution, fromvi->solution))
1523 if (update_changed)
1524 bitmap_set_bit (changed, to);
1527 BITMAP_FREE (fromvi->solution);
1528 if (fromvi->oldsolution)
1529 BITMAP_FREE (fromvi->oldsolution);
1531 if (stats.iterations > 0
1532 && tovi->oldsolution)
1533 BITMAP_FREE (tovi->oldsolution);
1535 if (graph->succs[to])
1536 bitmap_clear_bit (graph->succs[to], to);
1539 /* Information needed to compute the topological ordering of a graph. */
1541 struct topo_info
1543 /* sbitmap of visited nodes. */
1544 sbitmap visited;
1545 /* Array that stores the topological order of the graph, *in
1546 reverse*. */
1547 vec<unsigned> topo_order;
1551 /* Initialize and return a topological info structure. */
1553 static struct topo_info *
1554 init_topo_info (void)
1556 size_t size = graph->size;
1557 struct topo_info *ti = XNEW (struct topo_info);
1558 ti->visited = sbitmap_alloc (size);
1559 bitmap_clear (ti->visited);
1560 ti->topo_order.create (1);
1561 return ti;
1565 /* Free the topological sort info pointed to by TI. */
1567 static void
1568 free_topo_info (struct topo_info *ti)
1570 sbitmap_free (ti->visited);
1571 ti->topo_order.release ();
1572 free (ti);
1575 /* Visit the graph in topological order, and store the order in the
1576 topo_info structure. */
1578 static void
1579 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1580 unsigned int n)
1582 bitmap_iterator bi;
1583 unsigned int j;
1585 bitmap_set_bit (ti->visited, n);
1587 if (graph->succs[n])
1588 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1590 if (!bitmap_bit_p (ti->visited, j))
1591 topo_visit (graph, ti, j);
1594 ti->topo_order.safe_push (n);
1597 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1598 starting solution for y. */
1600 static void
1601 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1602 bitmap delta, bitmap *expanded_delta)
1604 unsigned int lhs = c->lhs.var;
1605 bool flag = false;
1606 bitmap sol = get_varinfo (lhs)->solution;
1607 unsigned int j;
1608 bitmap_iterator bi;
1609 HOST_WIDE_INT roffset = c->rhs.offset;
1611 /* Our IL does not allow this. */
1612 gcc_checking_assert (c->lhs.offset == 0);
1614 /* If the solution of Y contains anything it is good enough to transfer
1615 this to the LHS. */
1616 if (bitmap_bit_p (delta, anything_id))
1618 flag |= bitmap_set_bit (sol, anything_id);
1619 goto done;
1622 /* If we do not know at with offset the rhs is dereferenced compute
1623 the reachability set of DELTA, conservatively assuming it is
1624 dereferenced at all valid offsets. */
1625 if (roffset == UNKNOWN_OFFSET)
1627 delta = solution_set_expand (delta, expanded_delta);
1628 /* No further offset processing is necessary. */
1629 roffset = 0;
1632 /* For each variable j in delta (Sol(y)), add
1633 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1634 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1636 varinfo_t v = get_varinfo (j);
1637 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1638 unsigned HOST_WIDE_INT size = v->size;
1639 unsigned int t;
1641 if (v->is_full_var)
1643 else if (roffset != 0)
1645 if (fieldoffset < 0)
1646 v = get_varinfo (v->head);
1647 else
1648 v = first_or_preceding_vi_for_offset (v, fieldoffset);
1651 /* We have to include all fields that overlap the current field
1652 shifted by roffset. */
1655 t = find (v->id);
1657 /* Adding edges from the special vars is pointless.
1658 They don't have sets that can change. */
1659 if (get_varinfo (t)->is_special_var)
1660 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1661 /* Merging the solution from ESCAPED needlessly increases
1662 the set. Use ESCAPED as representative instead. */
1663 else if (v->id == escaped_id)
1664 flag |= bitmap_set_bit (sol, escaped_id);
1665 else if (v->may_have_pointers
1666 && add_graph_edge (graph, lhs, t))
1667 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1669 if (v->is_full_var
1670 || v->next == 0)
1671 break;
1673 v = vi_next (v);
1675 while (v->offset < fieldoffset + size);
1678 done:
1679 /* If the LHS solution changed, mark the var as changed. */
1680 if (flag)
1682 get_varinfo (lhs)->solution = sol;
1683 bitmap_set_bit (changed, lhs);
1687 /* Process a constraint C that represents *(x + off) = y using DELTA
1688 as the starting solution for x. */
1690 static void
1691 do_ds_constraint (constraint_t c, bitmap delta, bitmap *expanded_delta)
1693 unsigned int rhs = c->rhs.var;
1694 bitmap sol = get_varinfo (rhs)->solution;
1695 unsigned int j;
1696 bitmap_iterator bi;
1697 HOST_WIDE_INT loff = c->lhs.offset;
1698 bool escaped_p = false;
1700 /* Our IL does not allow this. */
1701 gcc_checking_assert (c->rhs.offset == 0);
1703 /* If the solution of y contains ANYTHING simply use the ANYTHING
1704 solution. This avoids needlessly increasing the points-to sets. */
1705 if (bitmap_bit_p (sol, anything_id))
1706 sol = get_varinfo (find (anything_id))->solution;
1708 /* If the solution for x contains ANYTHING we have to merge the
1709 solution of y into all pointer variables which we do via
1710 STOREDANYTHING. */
1711 if (bitmap_bit_p (delta, anything_id))
1713 unsigned t = find (storedanything_id);
1714 if (add_graph_edge (graph, t, rhs))
1716 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1717 bitmap_set_bit (changed, t);
1719 return;
1722 /* If we do not know at with offset the rhs is dereferenced compute
1723 the reachability set of DELTA, conservatively assuming it is
1724 dereferenced at all valid offsets. */
1725 if (loff == UNKNOWN_OFFSET)
1727 delta = solution_set_expand (delta, expanded_delta);
1728 loff = 0;
1731 /* For each member j of delta (Sol(x)), add an edge from y to j and
1732 union Sol(y) into Sol(j) */
1733 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1735 varinfo_t v = get_varinfo (j);
1736 unsigned int t;
1737 HOST_WIDE_INT fieldoffset = v->offset + loff;
1738 unsigned HOST_WIDE_INT size = v->size;
1740 if (v->is_full_var)
1742 else if (loff != 0)
1744 if (fieldoffset < 0)
1745 v = get_varinfo (v->head);
1746 else
1747 v = first_or_preceding_vi_for_offset (v, fieldoffset);
1750 /* We have to include all fields that overlap the current field
1751 shifted by loff. */
1754 if (v->may_have_pointers)
1756 /* If v is a global variable then this is an escape point. */
1757 if (v->is_global_var
1758 && !escaped_p)
1760 t = find (escaped_id);
1761 if (add_graph_edge (graph, t, rhs)
1762 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1763 bitmap_set_bit (changed, t);
1764 /* Enough to let rhs escape once. */
1765 escaped_p = true;
1768 if (v->is_special_var)
1769 break;
1771 t = find (v->id);
1772 if (add_graph_edge (graph, t, rhs)
1773 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1774 bitmap_set_bit (changed, t);
1777 if (v->is_full_var
1778 || v->next == 0)
1779 break;
1781 v = vi_next (v);
1783 while (v->offset < fieldoffset + size);
1787 /* Handle a non-simple (simple meaning requires no iteration),
1788 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1790 static void
1791 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta,
1792 bitmap *expanded_delta)
1794 if (c->lhs.type == DEREF)
1796 if (c->rhs.type == ADDRESSOF)
1798 gcc_unreachable ();
1800 else
1802 /* *x = y */
1803 do_ds_constraint (c, delta, expanded_delta);
1806 else if (c->rhs.type == DEREF)
1808 /* x = *y */
1809 if (!(get_varinfo (c->lhs.var)->is_special_var))
1810 do_sd_constraint (graph, c, delta, expanded_delta);
1812 else
1814 bitmap tmp;
1815 bool flag = false;
1817 gcc_checking_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR
1818 && c->rhs.offset != 0 && c->lhs.offset == 0);
1819 tmp = get_varinfo (c->lhs.var)->solution;
1821 flag = set_union_with_increment (tmp, delta, c->rhs.offset,
1822 expanded_delta);
1824 if (flag)
1825 bitmap_set_bit (changed, c->lhs.var);
1829 /* Initialize and return a new SCC info structure. */
1831 static struct scc_info *
1832 init_scc_info (size_t size)
1834 struct scc_info *si = XNEW (struct scc_info);
1835 size_t i;
1837 si->current_index = 0;
1838 si->visited = sbitmap_alloc (size);
1839 bitmap_clear (si->visited);
1840 si->deleted = sbitmap_alloc (size);
1841 bitmap_clear (si->deleted);
1842 si->node_mapping = XNEWVEC (unsigned int, size);
1843 si->dfs = XCNEWVEC (unsigned int, size);
1845 for (i = 0; i < size; i++)
1846 si->node_mapping[i] = i;
1848 si->scc_stack.create (1);
1849 return si;
1852 /* Free an SCC info structure pointed to by SI */
1854 static void
1855 free_scc_info (struct scc_info *si)
1857 sbitmap_free (si->visited);
1858 sbitmap_free (si->deleted);
1859 free (si->node_mapping);
1860 free (si->dfs);
1861 si->scc_stack.release ();
1862 free (si);
1866 /* Find indirect cycles in GRAPH that occur, using strongly connected
1867 components, and note them in the indirect cycles map.
1869 This technique comes from Ben Hardekopf and Calvin Lin,
1870 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1871 Lines of Code", submitted to PLDI 2007. */
1873 static void
1874 find_indirect_cycles (constraint_graph_t graph)
1876 unsigned int i;
1877 unsigned int size = graph->size;
1878 struct scc_info *si = init_scc_info (size);
1880 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1881 if (!bitmap_bit_p (si->visited, i) && find (i) == i)
1882 scc_visit (graph, si, i);
1884 free_scc_info (si);
1887 /* Compute a topological ordering for GRAPH, and store the result in the
1888 topo_info structure TI. */
1890 static void
1891 compute_topo_order (constraint_graph_t graph,
1892 struct topo_info *ti)
1894 unsigned int i;
1895 unsigned int size = graph->size;
1897 for (i = 0; i != size; ++i)
1898 if (!bitmap_bit_p (ti->visited, i) && find (i) == i)
1899 topo_visit (graph, ti, i);
1902 /* Structure used to for hash value numbering of pointer equivalence
1903 classes. */
1905 typedef struct equiv_class_label
1907 hashval_t hashcode;
1908 unsigned int equivalence_class;
1909 bitmap labels;
1910 } *equiv_class_label_t;
1911 typedef const struct equiv_class_label *const_equiv_class_label_t;
1913 /* Equiv_class_label hashtable helpers. */
1915 struct equiv_class_hasher : typed_free_remove <equiv_class_label>
1917 typedef equiv_class_label value_type;
1918 typedef equiv_class_label compare_type;
1919 static inline hashval_t hash (const value_type *);
1920 static inline bool equal (const value_type *, const compare_type *);
1923 /* Hash function for a equiv_class_label_t */
1925 inline hashval_t
1926 equiv_class_hasher::hash (const value_type *ecl)
1928 return ecl->hashcode;
1931 /* Equality function for two equiv_class_label_t's. */
1933 inline bool
1934 equiv_class_hasher::equal (const value_type *eql1, const compare_type *eql2)
1936 return (eql1->hashcode == eql2->hashcode
1937 && bitmap_equal_p (eql1->labels, eql2->labels));
1940 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1941 classes. */
1942 static hash_table <equiv_class_hasher> pointer_equiv_class_table;
1944 /* A hashtable for mapping a bitmap of labels->location equivalence
1945 classes. */
1946 static hash_table <equiv_class_hasher> location_equiv_class_table;
1948 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1949 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1950 is equivalent to. */
1952 static equiv_class_label *
1953 equiv_class_lookup_or_add (hash_table <equiv_class_hasher> table, bitmap labels)
1955 equiv_class_label **slot;
1956 equiv_class_label ecl;
1958 ecl.labels = labels;
1959 ecl.hashcode = bitmap_hash (labels);
1960 slot = table.find_slot_with_hash (&ecl, ecl.hashcode, INSERT);
1961 if (!*slot)
1963 *slot = XNEW (struct equiv_class_label);
1964 (*slot)->labels = labels;
1965 (*slot)->hashcode = ecl.hashcode;
1966 (*slot)->equivalence_class = 0;
1969 return *slot;
1972 /* Perform offline variable substitution.
1974 This is a worst case quadratic time way of identifying variables
1975 that must have equivalent points-to sets, including those caused by
1976 static cycles, and single entry subgraphs, in the constraint graph.
1978 The technique is described in "Exploiting Pointer and Location
1979 Equivalence to Optimize Pointer Analysis. In the 14th International
1980 Static Analysis Symposium (SAS), August 2007." It is known as the
1981 "HU" algorithm, and is equivalent to value numbering the collapsed
1982 constraint graph including evaluating unions.
1984 The general method of finding equivalence classes is as follows:
1985 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1986 Initialize all non-REF nodes to be direct nodes.
1987 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1988 variable}
1989 For each constraint containing the dereference, we also do the same
1990 thing.
1992 We then compute SCC's in the graph and unify nodes in the same SCC,
1993 including pts sets.
1995 For each non-collapsed node x:
1996 Visit all unvisited explicit incoming edges.
1997 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1998 where y->x.
1999 Lookup the equivalence class for pts(x).
2000 If we found one, equivalence_class(x) = found class.
2001 Otherwise, equivalence_class(x) = new class, and new_class is
2002 added to the lookup table.
2004 All direct nodes with the same equivalence class can be replaced
2005 with a single representative node.
2006 All unlabeled nodes (label == 0) are not pointers and all edges
2007 involving them can be eliminated.
2008 We perform these optimizations during rewrite_constraints
2010 In addition to pointer equivalence class finding, we also perform
2011 location equivalence class finding. This is the set of variables
2012 that always appear together in points-to sets. We use this to
2013 compress the size of the points-to sets. */
2015 /* Current maximum pointer equivalence class id. */
2016 static int pointer_equiv_class;
2018 /* Current maximum location equivalence class id. */
2019 static int location_equiv_class;
2021 /* Recursive routine to find strongly connected components in GRAPH,
2022 and label it's nodes with DFS numbers. */
2024 static void
2025 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2027 unsigned int i;
2028 bitmap_iterator bi;
2029 unsigned int my_dfs;
2031 gcc_checking_assert (si->node_mapping[n] == n);
2032 bitmap_set_bit (si->visited, n);
2033 si->dfs[n] = si->current_index ++;
2034 my_dfs = si->dfs[n];
2036 /* Visit all the successors. */
2037 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2039 unsigned int w = si->node_mapping[i];
2041 if (bitmap_bit_p (si->deleted, w))
2042 continue;
2044 if (!bitmap_bit_p (si->visited, w))
2045 condense_visit (graph, si, w);
2047 unsigned int t = si->node_mapping[w];
2048 gcc_checking_assert (si->node_mapping[n] == n);
2049 if (si->dfs[t] < si->dfs[n])
2050 si->dfs[n] = si->dfs[t];
2053 /* Visit all the implicit predecessors. */
2054 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2056 unsigned int w = si->node_mapping[i];
2058 if (bitmap_bit_p (si->deleted, w))
2059 continue;
2061 if (!bitmap_bit_p (si->visited, w))
2062 condense_visit (graph, si, w);
2064 unsigned int t = si->node_mapping[w];
2065 gcc_assert (si->node_mapping[n] == n);
2066 if (si->dfs[t] < si->dfs[n])
2067 si->dfs[n] = si->dfs[t];
2070 /* See if any components have been identified. */
2071 if (si->dfs[n] == my_dfs)
2073 while (si->scc_stack.length () != 0
2074 && si->dfs[si->scc_stack.last ()] >= my_dfs)
2076 unsigned int w = si->scc_stack.pop ();
2077 si->node_mapping[w] = n;
2079 if (!bitmap_bit_p (graph->direct_nodes, w))
2080 bitmap_clear_bit (graph->direct_nodes, n);
2082 /* Unify our nodes. */
2083 if (graph->preds[w])
2085 if (!graph->preds[n])
2086 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2087 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2089 if (graph->implicit_preds[w])
2091 if (!graph->implicit_preds[n])
2092 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2093 bitmap_ior_into (graph->implicit_preds[n],
2094 graph->implicit_preds[w]);
2096 if (graph->points_to[w])
2098 if (!graph->points_to[n])
2099 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2100 bitmap_ior_into (graph->points_to[n],
2101 graph->points_to[w]);
2104 bitmap_set_bit (si->deleted, n);
2106 else
2107 si->scc_stack.safe_push (n);
2110 /* Label pointer equivalences.
2112 This performs a value numbering of the constraint graph to
2113 discover which variables will always have the same points-to sets
2114 under the current set of constraints.
2116 The way it value numbers is to store the set of points-to bits
2117 generated by the constraints and graph edges. This is just used as a
2118 hash and equality comparison. The *actual set of points-to bits* is
2119 completely irrelevant, in that we don't care about being able to
2120 extract them later.
2122 The equality values (currently bitmaps) just have to satisfy a few
2123 constraints, the main ones being:
2124 1. The combining operation must be order independent.
2125 2. The end result of a given set of operations must be unique iff the
2126 combination of input values is unique
2127 3. Hashable. */
2129 static void
2130 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2132 unsigned int i, first_pred;
2133 bitmap_iterator bi;
2135 bitmap_set_bit (si->visited, n);
2137 /* Label and union our incoming edges's points to sets. */
2138 first_pred = -1U;
2139 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2141 unsigned int w = si->node_mapping[i];
2142 if (!bitmap_bit_p (si->visited, w))
2143 label_visit (graph, si, w);
2145 /* Skip unused edges */
2146 if (w == n || graph->pointer_label[w] == 0)
2147 continue;
2149 if (graph->points_to[w])
2151 if (!graph->points_to[n])
2153 if (first_pred == -1U)
2154 first_pred = w;
2155 else
2157 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2158 bitmap_ior (graph->points_to[n],
2159 graph->points_to[first_pred],
2160 graph->points_to[w]);
2163 else
2164 bitmap_ior_into (graph->points_to[n], graph->points_to[w]);
2168 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2169 if (!bitmap_bit_p (graph->direct_nodes, n))
2171 if (!graph->points_to[n])
2173 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2174 if (first_pred != -1U)
2175 bitmap_copy (graph->points_to[n], graph->points_to[first_pred]);
2177 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2178 graph->pointer_label[n] = pointer_equiv_class++;
2179 equiv_class_label_t ecl;
2180 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2181 graph->points_to[n]);
2182 ecl->equivalence_class = graph->pointer_label[n];
2183 return;
2186 /* If there was only a single non-empty predecessor the pointer equiv
2187 class is the same. */
2188 if (!graph->points_to[n])
2190 if (first_pred != -1U)
2192 graph->pointer_label[n] = graph->pointer_label[first_pred];
2193 graph->points_to[n] = graph->points_to[first_pred];
2195 return;
2198 if (!bitmap_empty_p (graph->points_to[n]))
2200 equiv_class_label_t ecl;
2201 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2202 graph->points_to[n]);
2203 if (ecl->equivalence_class == 0)
2204 ecl->equivalence_class = pointer_equiv_class++;
2205 else
2207 BITMAP_FREE (graph->points_to[n]);
2208 graph->points_to[n] = ecl->labels;
2210 graph->pointer_label[n] = ecl->equivalence_class;
2214 /* Print the pred graph in dot format. */
2216 static void
2217 dump_pred_graph (struct scc_info *si, FILE *file)
2219 unsigned int i;
2221 /* Only print the graph if it has already been initialized: */
2222 if (!graph)
2223 return;
2225 /* Prints the header of the dot file: */
2226 fprintf (file, "strict digraph {\n");
2227 fprintf (file, " node [\n shape = box\n ]\n");
2228 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
2229 fprintf (file, "\n // List of nodes and complex constraints in "
2230 "the constraint graph:\n");
2232 /* The next lines print the nodes in the graph together with the
2233 complex constraints attached to them. */
2234 for (i = 1; i < graph->size; i++)
2236 if (i == FIRST_REF_NODE)
2237 continue;
2238 if (si->node_mapping[i] != i)
2239 continue;
2240 if (i < FIRST_REF_NODE)
2241 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2242 else
2243 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2244 if (graph->points_to[i]
2245 && !bitmap_empty_p (graph->points_to[i]))
2247 fprintf (file, "[label=\"%s = {", get_varinfo (i)->name);
2248 unsigned j;
2249 bitmap_iterator bi;
2250 EXECUTE_IF_SET_IN_BITMAP (graph->points_to[i], 0, j, bi)
2251 fprintf (file, " %d", j);
2252 fprintf (file, " }\"]");
2254 fprintf (file, ";\n");
2257 /* Go over the edges. */
2258 fprintf (file, "\n // Edges in the constraint graph:\n");
2259 for (i = 1; i < graph->size; i++)
2261 unsigned j;
2262 bitmap_iterator bi;
2263 if (si->node_mapping[i] != i)
2264 continue;
2265 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[i], 0, j, bi)
2267 unsigned from = si->node_mapping[j];
2268 if (from < FIRST_REF_NODE)
2269 fprintf (file, "\"%s\"", get_varinfo (from)->name);
2270 else
2271 fprintf (file, "\"*%s\"", get_varinfo (from - FIRST_REF_NODE)->name);
2272 fprintf (file, " -> ");
2273 if (i < FIRST_REF_NODE)
2274 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2275 else
2276 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2277 fprintf (file, ";\n");
2281 /* Prints the tail of the dot file. */
2282 fprintf (file, "}\n");
2285 /* Perform offline variable substitution, discovering equivalence
2286 classes, and eliminating non-pointer variables. */
2288 static struct scc_info *
2289 perform_var_substitution (constraint_graph_t graph)
2291 unsigned int i;
2292 unsigned int size = graph->size;
2293 struct scc_info *si = init_scc_info (size);
2295 bitmap_obstack_initialize (&iteration_obstack);
2296 pointer_equiv_class_table.create (511);
2297 location_equiv_class_table.create (511);
2298 pointer_equiv_class = 1;
2299 location_equiv_class = 1;
2301 /* Condense the nodes, which means to find SCC's, count incoming
2302 predecessors, and unite nodes in SCC's. */
2303 for (i = 1; i < FIRST_REF_NODE; i++)
2304 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2305 condense_visit (graph, si, si->node_mapping[i]);
2307 if (dump_file && (dump_flags & TDF_GRAPH))
2309 fprintf (dump_file, "\n\n// The constraint graph before var-substitution "
2310 "in dot format:\n");
2311 dump_pred_graph (si, dump_file);
2312 fprintf (dump_file, "\n\n");
2315 bitmap_clear (si->visited);
2316 /* Actually the label the nodes for pointer equivalences */
2317 for (i = 1; i < FIRST_REF_NODE; i++)
2318 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2319 label_visit (graph, si, si->node_mapping[i]);
2321 /* Calculate location equivalence labels. */
2322 for (i = 1; i < FIRST_REF_NODE; i++)
2324 bitmap pointed_by;
2325 bitmap_iterator bi;
2326 unsigned int j;
2328 if (!graph->pointed_by[i])
2329 continue;
2330 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2332 /* Translate the pointed-by mapping for pointer equivalence
2333 labels. */
2334 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2336 bitmap_set_bit (pointed_by,
2337 graph->pointer_label[si->node_mapping[j]]);
2339 /* The original pointed_by is now dead. */
2340 BITMAP_FREE (graph->pointed_by[i]);
2342 /* Look up the location equivalence label if one exists, or make
2343 one otherwise. */
2344 equiv_class_label_t ecl;
2345 ecl = equiv_class_lookup_or_add (location_equiv_class_table, pointed_by);
2346 if (ecl->equivalence_class == 0)
2347 ecl->equivalence_class = location_equiv_class++;
2348 else
2350 if (dump_file && (dump_flags & TDF_DETAILS))
2351 fprintf (dump_file, "Found location equivalence for node %s\n",
2352 get_varinfo (i)->name);
2353 BITMAP_FREE (pointed_by);
2355 graph->loc_label[i] = ecl->equivalence_class;
2359 if (dump_file && (dump_flags & TDF_DETAILS))
2360 for (i = 1; i < FIRST_REF_NODE; i++)
2362 unsigned j = si->node_mapping[i];
2363 if (j != i)
2365 fprintf (dump_file, "%s node id %d ",
2366 bitmap_bit_p (graph->direct_nodes, i)
2367 ? "Direct" : "Indirect", i);
2368 if (i < FIRST_REF_NODE)
2369 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2370 else
2371 fprintf (dump_file, "\"*%s\"",
2372 get_varinfo (i - FIRST_REF_NODE)->name);
2373 fprintf (dump_file, " mapped to SCC leader node id %d ", j);
2374 if (j < FIRST_REF_NODE)
2375 fprintf (dump_file, "\"%s\"\n", get_varinfo (j)->name);
2376 else
2377 fprintf (dump_file, "\"*%s\"\n",
2378 get_varinfo (j - FIRST_REF_NODE)->name);
2380 else
2382 fprintf (dump_file,
2383 "Equivalence classes for %s node id %d ",
2384 bitmap_bit_p (graph->direct_nodes, i)
2385 ? "direct" : "indirect", i);
2386 if (i < FIRST_REF_NODE)
2387 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2388 else
2389 fprintf (dump_file, "\"*%s\"",
2390 get_varinfo (i - FIRST_REF_NODE)->name);
2391 fprintf (dump_file,
2392 ": pointer %d, location %d\n",
2393 graph->pointer_label[i], graph->loc_label[i]);
2397 /* Quickly eliminate our non-pointer variables. */
2399 for (i = 1; i < FIRST_REF_NODE; i++)
2401 unsigned int node = si->node_mapping[i];
2403 if (graph->pointer_label[node] == 0)
2405 if (dump_file && (dump_flags & TDF_DETAILS))
2406 fprintf (dump_file,
2407 "%s is a non-pointer variable, eliminating edges.\n",
2408 get_varinfo (node)->name);
2409 stats.nonpointer_vars++;
2410 clear_edges_for_node (graph, node);
2414 return si;
2417 /* Free information that was only necessary for variable
2418 substitution. */
2420 static void
2421 free_var_substitution_info (struct scc_info *si)
2423 free_scc_info (si);
2424 free (graph->pointer_label);
2425 free (graph->loc_label);
2426 free (graph->pointed_by);
2427 free (graph->points_to);
2428 free (graph->eq_rep);
2429 sbitmap_free (graph->direct_nodes);
2430 pointer_equiv_class_table.dispose ();
2431 location_equiv_class_table.dispose ();
2432 bitmap_obstack_release (&iteration_obstack);
2435 /* Return an existing node that is equivalent to NODE, which has
2436 equivalence class LABEL, if one exists. Return NODE otherwise. */
2438 static unsigned int
2439 find_equivalent_node (constraint_graph_t graph,
2440 unsigned int node, unsigned int label)
2442 /* If the address version of this variable is unused, we can
2443 substitute it for anything else with the same label.
2444 Otherwise, we know the pointers are equivalent, but not the
2445 locations, and we can unite them later. */
2447 if (!bitmap_bit_p (graph->address_taken, node))
2449 gcc_checking_assert (label < graph->size);
2451 if (graph->eq_rep[label] != -1)
2453 /* Unify the two variables since we know they are equivalent. */
2454 if (unite (graph->eq_rep[label], node))
2455 unify_nodes (graph, graph->eq_rep[label], node, false);
2456 return graph->eq_rep[label];
2458 else
2460 graph->eq_rep[label] = node;
2461 graph->pe_rep[label] = node;
2464 else
2466 gcc_checking_assert (label < graph->size);
2467 graph->pe[node] = label;
2468 if (graph->pe_rep[label] == -1)
2469 graph->pe_rep[label] = node;
2472 return node;
2475 /* Unite pointer equivalent but not location equivalent nodes in
2476 GRAPH. This may only be performed once variable substitution is
2477 finished. */
2479 static void
2480 unite_pointer_equivalences (constraint_graph_t graph)
2482 unsigned int i;
2484 /* Go through the pointer equivalences and unite them to their
2485 representative, if they aren't already. */
2486 for (i = 1; i < FIRST_REF_NODE; i++)
2488 unsigned int label = graph->pe[i];
2489 if (label)
2491 int label_rep = graph->pe_rep[label];
2493 if (label_rep == -1)
2494 continue;
2496 label_rep = find (label_rep);
2497 if (label_rep >= 0 && unite (label_rep, find (i)))
2498 unify_nodes (graph, label_rep, i, false);
2503 /* Move complex constraints to the GRAPH nodes they belong to. */
2505 static void
2506 move_complex_constraints (constraint_graph_t graph)
2508 int i;
2509 constraint_t c;
2511 FOR_EACH_VEC_ELT (constraints, i, c)
2513 if (c)
2515 struct constraint_expr lhs = c->lhs;
2516 struct constraint_expr rhs = c->rhs;
2518 if (lhs.type == DEREF)
2520 insert_into_complex (graph, lhs.var, c);
2522 else if (rhs.type == DEREF)
2524 if (!(get_varinfo (lhs.var)->is_special_var))
2525 insert_into_complex (graph, rhs.var, c);
2527 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2528 && (lhs.offset != 0 || rhs.offset != 0))
2530 insert_into_complex (graph, rhs.var, c);
2537 /* Optimize and rewrite complex constraints while performing
2538 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2539 result of perform_variable_substitution. */
2541 static void
2542 rewrite_constraints (constraint_graph_t graph,
2543 struct scc_info *si)
2545 int i;
2546 constraint_t c;
2548 #ifdef ENABLE_CHECKING
2549 for (unsigned int j = 0; j < graph->size; j++)
2550 gcc_assert (find (j) == j);
2551 #endif
2553 FOR_EACH_VEC_ELT (constraints, i, c)
2555 struct constraint_expr lhs = c->lhs;
2556 struct constraint_expr rhs = c->rhs;
2557 unsigned int lhsvar = find (lhs.var);
2558 unsigned int rhsvar = find (rhs.var);
2559 unsigned int lhsnode, rhsnode;
2560 unsigned int lhslabel, rhslabel;
2562 lhsnode = si->node_mapping[lhsvar];
2563 rhsnode = si->node_mapping[rhsvar];
2564 lhslabel = graph->pointer_label[lhsnode];
2565 rhslabel = graph->pointer_label[rhsnode];
2567 /* See if it is really a non-pointer variable, and if so, ignore
2568 the constraint. */
2569 if (lhslabel == 0)
2571 if (dump_file && (dump_flags & TDF_DETAILS))
2574 fprintf (dump_file, "%s is a non-pointer variable,"
2575 "ignoring constraint:",
2576 get_varinfo (lhs.var)->name);
2577 dump_constraint (dump_file, c);
2578 fprintf (dump_file, "\n");
2580 constraints[i] = NULL;
2581 continue;
2584 if (rhslabel == 0)
2586 if (dump_file && (dump_flags & TDF_DETAILS))
2589 fprintf (dump_file, "%s is a non-pointer variable,"
2590 "ignoring constraint:",
2591 get_varinfo (rhs.var)->name);
2592 dump_constraint (dump_file, c);
2593 fprintf (dump_file, "\n");
2595 constraints[i] = NULL;
2596 continue;
2599 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2600 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2601 c->lhs.var = lhsvar;
2602 c->rhs.var = rhsvar;
2606 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2607 part of an SCC, false otherwise. */
2609 static bool
2610 eliminate_indirect_cycles (unsigned int node)
2612 if (graph->indirect_cycles[node] != -1
2613 && !bitmap_empty_p (get_varinfo (node)->solution))
2615 unsigned int i;
2616 auto_vec<unsigned> queue;
2617 int queuepos;
2618 unsigned int to = find (graph->indirect_cycles[node]);
2619 bitmap_iterator bi;
2621 /* We can't touch the solution set and call unify_nodes
2622 at the same time, because unify_nodes is going to do
2623 bitmap unions into it. */
2625 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2627 if (find (i) == i && i != to)
2629 if (unite (to, i))
2630 queue.safe_push (i);
2634 for (queuepos = 0;
2635 queue.iterate (queuepos, &i);
2636 queuepos++)
2638 unify_nodes (graph, to, i, true);
2640 return true;
2642 return false;
2645 /* Solve the constraint graph GRAPH using our worklist solver.
2646 This is based on the PW* family of solvers from the "Efficient Field
2647 Sensitive Pointer Analysis for C" paper.
2648 It works by iterating over all the graph nodes, processing the complex
2649 constraints and propagating the copy constraints, until everything stops
2650 changed. This corresponds to steps 6-8 in the solving list given above. */
2652 static void
2653 solve_graph (constraint_graph_t graph)
2655 unsigned int size = graph->size;
2656 unsigned int i;
2657 bitmap pts;
2659 changed = BITMAP_ALLOC (NULL);
2661 /* Mark all initial non-collapsed nodes as changed. */
2662 for (i = 1; i < size; i++)
2664 varinfo_t ivi = get_varinfo (i);
2665 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2666 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2667 || graph->complex[i].length () > 0))
2668 bitmap_set_bit (changed, i);
2671 /* Allocate a bitmap to be used to store the changed bits. */
2672 pts = BITMAP_ALLOC (&pta_obstack);
2674 while (!bitmap_empty_p (changed))
2676 unsigned int i;
2677 struct topo_info *ti = init_topo_info ();
2678 stats.iterations++;
2680 bitmap_obstack_initialize (&iteration_obstack);
2682 compute_topo_order (graph, ti);
2684 while (ti->topo_order.length () != 0)
2687 i = ti->topo_order.pop ();
2689 /* If this variable is not a representative, skip it. */
2690 if (find (i) != i)
2691 continue;
2693 /* In certain indirect cycle cases, we may merge this
2694 variable to another. */
2695 if (eliminate_indirect_cycles (i) && find (i) != i)
2696 continue;
2698 /* If the node has changed, we need to process the
2699 complex constraints and outgoing edges again. */
2700 if (bitmap_clear_bit (changed, i))
2702 unsigned int j;
2703 constraint_t c;
2704 bitmap solution;
2705 vec<constraint_t> complex = graph->complex[i];
2706 varinfo_t vi = get_varinfo (i);
2707 bool solution_empty;
2709 /* Compute the changed set of solution bits. If anything
2710 is in the solution just propagate that. */
2711 if (bitmap_bit_p (vi->solution, anything_id))
2713 /* If anything is also in the old solution there is
2714 nothing to do.
2715 ??? But we shouldn't ended up with "changed" set ... */
2716 if (vi->oldsolution
2717 && bitmap_bit_p (vi->oldsolution, anything_id))
2718 continue;
2719 bitmap_copy (pts, get_varinfo (find (anything_id))->solution);
2721 else if (vi->oldsolution)
2722 bitmap_and_compl (pts, vi->solution, vi->oldsolution);
2723 else
2724 bitmap_copy (pts, vi->solution);
2726 if (bitmap_empty_p (pts))
2727 continue;
2729 if (vi->oldsolution)
2730 bitmap_ior_into (vi->oldsolution, pts);
2731 else
2733 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
2734 bitmap_copy (vi->oldsolution, pts);
2737 solution = vi->solution;
2738 solution_empty = bitmap_empty_p (solution);
2740 /* Process the complex constraints */
2741 bitmap expanded_pts = NULL;
2742 FOR_EACH_VEC_ELT (complex, j, c)
2744 /* XXX: This is going to unsort the constraints in
2745 some cases, which will occasionally add duplicate
2746 constraints during unification. This does not
2747 affect correctness. */
2748 c->lhs.var = find (c->lhs.var);
2749 c->rhs.var = find (c->rhs.var);
2751 /* The only complex constraint that can change our
2752 solution to non-empty, given an empty solution,
2753 is a constraint where the lhs side is receiving
2754 some set from elsewhere. */
2755 if (!solution_empty || c->lhs.type != DEREF)
2756 do_complex_constraint (graph, c, pts, &expanded_pts);
2758 BITMAP_FREE (expanded_pts);
2760 solution_empty = bitmap_empty_p (solution);
2762 if (!solution_empty)
2764 bitmap_iterator bi;
2765 unsigned eff_escaped_id = find (escaped_id);
2767 /* Propagate solution to all successors. */
2768 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2769 0, j, bi)
2771 bitmap tmp;
2772 bool flag;
2774 unsigned int to = find (j);
2775 tmp = get_varinfo (to)->solution;
2776 flag = false;
2778 /* Don't try to propagate to ourselves. */
2779 if (to == i)
2780 continue;
2782 /* If we propagate from ESCAPED use ESCAPED as
2783 placeholder. */
2784 if (i == eff_escaped_id)
2785 flag = bitmap_set_bit (tmp, escaped_id);
2786 else
2787 flag = bitmap_ior_into (tmp, pts);
2789 if (flag)
2790 bitmap_set_bit (changed, to);
2795 free_topo_info (ti);
2796 bitmap_obstack_release (&iteration_obstack);
2799 BITMAP_FREE (pts);
2800 BITMAP_FREE (changed);
2801 bitmap_obstack_release (&oldpta_obstack);
2804 /* Map from trees to variable infos. */
2805 static struct pointer_map_t *vi_for_tree;
2808 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2810 static void
2811 insert_vi_for_tree (tree t, varinfo_t vi)
2813 void **slot = pointer_map_insert (vi_for_tree, t);
2814 gcc_assert (vi);
2815 gcc_assert (*slot == NULL);
2816 *slot = vi;
2819 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2820 exist in the map, return NULL, otherwise, return the varinfo we found. */
2822 static varinfo_t
2823 lookup_vi_for_tree (tree t)
2825 void **slot = pointer_map_contains (vi_for_tree, t);
2826 if (slot == NULL)
2827 return NULL;
2829 return (varinfo_t) *slot;
2832 /* Return a printable name for DECL */
2834 static const char *
2835 alias_get_name (tree decl)
2837 const char *res = NULL;
2838 char *temp;
2839 int num_printed = 0;
2841 if (!dump_file)
2842 return "NULL";
2844 if (TREE_CODE (decl) == SSA_NAME)
2846 res = get_name (decl);
2847 if (res)
2848 num_printed = asprintf (&temp, "%s_%u", res, SSA_NAME_VERSION (decl));
2849 else
2850 num_printed = asprintf (&temp, "_%u", SSA_NAME_VERSION (decl));
2851 if (num_printed > 0)
2853 res = ggc_strdup (temp);
2854 free (temp);
2857 else if (DECL_P (decl))
2859 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2860 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2861 else
2863 res = get_name (decl);
2864 if (!res)
2866 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2867 if (num_printed > 0)
2869 res = ggc_strdup (temp);
2870 free (temp);
2875 if (res != NULL)
2876 return res;
2878 return "NULL";
2881 /* Find the variable id for tree T in the map.
2882 If T doesn't exist in the map, create an entry for it and return it. */
2884 static varinfo_t
2885 get_vi_for_tree (tree t)
2887 void **slot = pointer_map_contains (vi_for_tree, t);
2888 if (slot == NULL)
2889 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2891 return (varinfo_t) *slot;
2894 /* Get a scalar constraint expression for a new temporary variable. */
2896 static struct constraint_expr
2897 new_scalar_tmp_constraint_exp (const char *name)
2899 struct constraint_expr tmp;
2900 varinfo_t vi;
2902 vi = new_var_info (NULL_TREE, name);
2903 vi->offset = 0;
2904 vi->size = -1;
2905 vi->fullsize = -1;
2906 vi->is_full_var = 1;
2908 tmp.var = vi->id;
2909 tmp.type = SCALAR;
2910 tmp.offset = 0;
2912 return tmp;
2915 /* Get a constraint expression vector from an SSA_VAR_P node.
2916 If address_p is true, the result will be taken its address of. */
2918 static void
2919 get_constraint_for_ssa_var (tree t, vec<ce_s> *results, bool address_p)
2921 struct constraint_expr cexpr;
2922 varinfo_t vi;
2924 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2925 gcc_assert (TREE_CODE (t) == SSA_NAME || DECL_P (t));
2927 /* For parameters, get at the points-to set for the actual parm
2928 decl. */
2929 if (TREE_CODE (t) == SSA_NAME
2930 && SSA_NAME_IS_DEFAULT_DEF (t)
2931 && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2932 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL))
2934 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2935 return;
2938 /* For global variables resort to the alias target. */
2939 if (TREE_CODE (t) == VAR_DECL
2940 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
2942 varpool_node *node = varpool_get_node (t);
2943 if (node && node->alias && node->analyzed)
2945 node = varpool_variable_node (node, NULL);
2946 t = node->decl;
2950 vi = get_vi_for_tree (t);
2951 cexpr.var = vi->id;
2952 cexpr.type = SCALAR;
2953 cexpr.offset = 0;
2954 /* If we determine the result is "anything", and we know this is readonly,
2955 say it points to readonly memory instead. */
2956 if (cexpr.var == anything_id && TREE_READONLY (t))
2958 gcc_unreachable ();
2959 cexpr.type = ADDRESSOF;
2960 cexpr.var = readonly_id;
2963 /* If we are not taking the address of the constraint expr, add all
2964 sub-fiels of the variable as well. */
2965 if (!address_p
2966 && !vi->is_full_var)
2968 for (; vi; vi = vi_next (vi))
2970 cexpr.var = vi->id;
2971 results->safe_push (cexpr);
2973 return;
2976 results->safe_push (cexpr);
2979 /* Process constraint T, performing various simplifications and then
2980 adding it to our list of overall constraints. */
2982 static void
2983 process_constraint (constraint_t t)
2985 struct constraint_expr rhs = t->rhs;
2986 struct constraint_expr lhs = t->lhs;
2988 gcc_assert (rhs.var < varmap.length ());
2989 gcc_assert (lhs.var < varmap.length ());
2991 /* If we didn't get any useful constraint from the lhs we get
2992 &ANYTHING as fallback from get_constraint_for. Deal with
2993 it here by turning it into *ANYTHING. */
2994 if (lhs.type == ADDRESSOF
2995 && lhs.var == anything_id)
2996 lhs.type = DEREF;
2998 /* ADDRESSOF on the lhs is invalid. */
2999 gcc_assert (lhs.type != ADDRESSOF);
3001 /* We shouldn't add constraints from things that cannot have pointers.
3002 It's not completely trivial to avoid in the callers, so do it here. */
3003 if (rhs.type != ADDRESSOF
3004 && !get_varinfo (rhs.var)->may_have_pointers)
3005 return;
3007 /* Likewise adding to the solution of a non-pointer var isn't useful. */
3008 if (!get_varinfo (lhs.var)->may_have_pointers)
3009 return;
3011 /* This can happen in our IR with things like n->a = *p */
3012 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
3014 /* Split into tmp = *rhs, *lhs = tmp */
3015 struct constraint_expr tmplhs;
3016 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp");
3017 process_constraint (new_constraint (tmplhs, rhs));
3018 process_constraint (new_constraint (lhs, tmplhs));
3020 else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
3022 /* Split into tmp = &rhs, *lhs = tmp */
3023 struct constraint_expr tmplhs;
3024 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp");
3025 process_constraint (new_constraint (tmplhs, rhs));
3026 process_constraint (new_constraint (lhs, tmplhs));
3028 else
3030 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
3031 constraints.safe_push (t);
3036 /* Return the position, in bits, of FIELD_DECL from the beginning of its
3037 structure. */
3039 static HOST_WIDE_INT
3040 bitpos_of_field (const tree fdecl)
3042 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl))
3043 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl)))
3044 return -1;
3046 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
3047 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl)));
3051 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
3052 resulting constraint expressions in *RESULTS. */
3054 static void
3055 get_constraint_for_ptr_offset (tree ptr, tree offset,
3056 vec<ce_s> *results)
3058 struct constraint_expr c;
3059 unsigned int j, n;
3060 HOST_WIDE_INT rhsoffset;
3062 /* If we do not do field-sensitive PTA adding offsets to pointers
3063 does not change the points-to solution. */
3064 if (!use_field_sensitive)
3066 get_constraint_for_rhs (ptr, results);
3067 return;
3070 /* If the offset is not a non-negative integer constant that fits
3071 in a HOST_WIDE_INT, we have to fall back to a conservative
3072 solution which includes all sub-fields of all pointed-to
3073 variables of ptr. */
3074 if (offset == NULL_TREE
3075 || TREE_CODE (offset) != INTEGER_CST)
3076 rhsoffset = UNKNOWN_OFFSET;
3077 else
3079 /* Sign-extend the offset. */
3080 double_int soffset = tree_to_double_int (offset)
3081 .sext (TYPE_PRECISION (TREE_TYPE (offset)));
3082 if (!soffset.fits_shwi ())
3083 rhsoffset = UNKNOWN_OFFSET;
3084 else
3086 /* Make sure the bit-offset also fits. */
3087 HOST_WIDE_INT rhsunitoffset = soffset.low;
3088 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
3089 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
3090 rhsoffset = UNKNOWN_OFFSET;
3094 get_constraint_for_rhs (ptr, results);
3095 if (rhsoffset == 0)
3096 return;
3098 /* As we are eventually appending to the solution do not use
3099 vec::iterate here. */
3100 n = results->length ();
3101 for (j = 0; j < n; j++)
3103 varinfo_t curr;
3104 c = (*results)[j];
3105 curr = get_varinfo (c.var);
3107 if (c.type == ADDRESSOF
3108 /* If this varinfo represents a full variable just use it. */
3109 && curr->is_full_var)
3111 else if (c.type == ADDRESSOF
3112 /* If we do not know the offset add all subfields. */
3113 && rhsoffset == UNKNOWN_OFFSET)
3115 varinfo_t temp = get_varinfo (curr->head);
3118 struct constraint_expr c2;
3119 c2.var = temp->id;
3120 c2.type = ADDRESSOF;
3121 c2.offset = 0;
3122 if (c2.var != c.var)
3123 results->safe_push (c2);
3124 temp = vi_next (temp);
3126 while (temp);
3128 else if (c.type == ADDRESSOF)
3130 varinfo_t temp;
3131 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
3133 /* If curr->offset + rhsoffset is less than zero adjust it. */
3134 if (rhsoffset < 0
3135 && curr->offset < offset)
3136 offset = 0;
3138 /* We have to include all fields that overlap the current
3139 field shifted by rhsoffset. And we include at least
3140 the last or the first field of the variable to represent
3141 reachability of off-bound addresses, in particular &object + 1,
3142 conservatively correct. */
3143 temp = first_or_preceding_vi_for_offset (curr, offset);
3144 c.var = temp->id;
3145 c.offset = 0;
3146 temp = vi_next (temp);
3147 while (temp
3148 && temp->offset < offset + curr->size)
3150 struct constraint_expr c2;
3151 c2.var = temp->id;
3152 c2.type = ADDRESSOF;
3153 c2.offset = 0;
3154 results->safe_push (c2);
3155 temp = vi_next (temp);
3158 else if (c.type == SCALAR)
3160 gcc_assert (c.offset == 0);
3161 c.offset = rhsoffset;
3163 else
3164 /* We shouldn't get any DEREFs here. */
3165 gcc_unreachable ();
3167 (*results)[j] = c;
3172 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3173 If address_p is true the result will be taken its address of.
3174 If lhs_p is true then the constraint expression is assumed to be used
3175 as the lhs. */
3177 static void
3178 get_constraint_for_component_ref (tree t, vec<ce_s> *results,
3179 bool address_p, bool lhs_p)
3181 tree orig_t = t;
3182 HOST_WIDE_INT bitsize = -1;
3183 HOST_WIDE_INT bitmaxsize = -1;
3184 HOST_WIDE_INT bitpos;
3185 tree forzero;
3187 /* Some people like to do cute things like take the address of
3188 &0->a.b */
3189 forzero = t;
3190 while (handled_component_p (forzero)
3191 || INDIRECT_REF_P (forzero)
3192 || TREE_CODE (forzero) == MEM_REF)
3193 forzero = TREE_OPERAND (forzero, 0);
3195 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
3197 struct constraint_expr temp;
3199 temp.offset = 0;
3200 temp.var = integer_id;
3201 temp.type = SCALAR;
3202 results->safe_push (temp);
3203 return;
3206 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
3208 /* Pretend to take the address of the base, we'll take care of
3209 adding the required subset of sub-fields below. */
3210 get_constraint_for_1 (t, results, true, lhs_p);
3211 gcc_assert (results->length () == 1);
3212 struct constraint_expr &result = results->last ();
3214 if (result.type == SCALAR
3215 && get_varinfo (result.var)->is_full_var)
3216 /* For single-field vars do not bother about the offset. */
3217 result.offset = 0;
3218 else if (result.type == SCALAR)
3220 /* In languages like C, you can access one past the end of an
3221 array. You aren't allowed to dereference it, so we can
3222 ignore this constraint. When we handle pointer subtraction,
3223 we may have to do something cute here. */
3225 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result.var)->fullsize
3226 && bitmaxsize != 0)
3228 /* It's also not true that the constraint will actually start at the
3229 right offset, it may start in some padding. We only care about
3230 setting the constraint to the first actual field it touches, so
3231 walk to find it. */
3232 struct constraint_expr cexpr = result;
3233 varinfo_t curr;
3234 results->pop ();
3235 cexpr.offset = 0;
3236 for (curr = get_varinfo (cexpr.var); curr; curr = vi_next (curr))
3238 if (ranges_overlap_p (curr->offset, curr->size,
3239 bitpos, bitmaxsize))
3241 cexpr.var = curr->id;
3242 results->safe_push (cexpr);
3243 if (address_p)
3244 break;
3247 /* If we are going to take the address of this field then
3248 to be able to compute reachability correctly add at least
3249 the last field of the variable. */
3250 if (address_p && results->length () == 0)
3252 curr = get_varinfo (cexpr.var);
3253 while (curr->next != 0)
3254 curr = vi_next (curr);
3255 cexpr.var = curr->id;
3256 results->safe_push (cexpr);
3258 else if (results->length () == 0)
3259 /* Assert that we found *some* field there. The user couldn't be
3260 accessing *only* padding. */
3261 /* Still the user could access one past the end of an array
3262 embedded in a struct resulting in accessing *only* padding. */
3263 /* Or accessing only padding via type-punning to a type
3264 that has a filed just in padding space. */
3266 cexpr.type = SCALAR;
3267 cexpr.var = anything_id;
3268 cexpr.offset = 0;
3269 results->safe_push (cexpr);
3272 else if (bitmaxsize == 0)
3274 if (dump_file && (dump_flags & TDF_DETAILS))
3275 fprintf (dump_file, "Access to zero-sized part of variable,"
3276 "ignoring\n");
3278 else
3279 if (dump_file && (dump_flags & TDF_DETAILS))
3280 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3282 else if (result.type == DEREF)
3284 /* If we do not know exactly where the access goes say so. Note
3285 that only for non-structure accesses we know that we access
3286 at most one subfiled of any variable. */
3287 if (bitpos == -1
3288 || bitsize != bitmaxsize
3289 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
3290 || result.offset == UNKNOWN_OFFSET)
3291 result.offset = UNKNOWN_OFFSET;
3292 else
3293 result.offset += bitpos;
3295 else if (result.type == ADDRESSOF)
3297 /* We can end up here for component references on a
3298 VIEW_CONVERT_EXPR <>(&foobar). */
3299 result.type = SCALAR;
3300 result.var = anything_id;
3301 result.offset = 0;
3303 else
3304 gcc_unreachable ();
3308 /* Dereference the constraint expression CONS, and return the result.
3309 DEREF (ADDRESSOF) = SCALAR
3310 DEREF (SCALAR) = DEREF
3311 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3312 This is needed so that we can handle dereferencing DEREF constraints. */
3314 static void
3315 do_deref (vec<ce_s> *constraints)
3317 struct constraint_expr *c;
3318 unsigned int i = 0;
3320 FOR_EACH_VEC_ELT (*constraints, i, c)
3322 if (c->type == SCALAR)
3323 c->type = DEREF;
3324 else if (c->type == ADDRESSOF)
3325 c->type = SCALAR;
3326 else if (c->type == DEREF)
3328 struct constraint_expr tmplhs;
3329 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp");
3330 process_constraint (new_constraint (tmplhs, *c));
3331 c->var = tmplhs.var;
3333 else
3334 gcc_unreachable ();
3338 /* Given a tree T, return the constraint expression for taking the
3339 address of it. */
3341 static void
3342 get_constraint_for_address_of (tree t, vec<ce_s> *results)
3344 struct constraint_expr *c;
3345 unsigned int i;
3347 get_constraint_for_1 (t, results, true, true);
3349 FOR_EACH_VEC_ELT (*results, i, c)
3351 if (c->type == DEREF)
3352 c->type = SCALAR;
3353 else
3354 c->type = ADDRESSOF;
3358 /* Given a tree T, return the constraint expression for it. */
3360 static void
3361 get_constraint_for_1 (tree t, vec<ce_s> *results, bool address_p,
3362 bool lhs_p)
3364 struct constraint_expr temp;
3366 /* x = integer is all glommed to a single variable, which doesn't
3367 point to anything by itself. That is, of course, unless it is an
3368 integer constant being treated as a pointer, in which case, we
3369 will return that this is really the addressof anything. This
3370 happens below, since it will fall into the default case. The only
3371 case we know something about an integer treated like a pointer is
3372 when it is the NULL pointer, and then we just say it points to
3373 NULL.
3375 Do not do that if -fno-delete-null-pointer-checks though, because
3376 in that case *NULL does not fail, so it _should_ alias *anything.
3377 It is not worth adding a new option or renaming the existing one,
3378 since this case is relatively obscure. */
3379 if ((TREE_CODE (t) == INTEGER_CST
3380 && integer_zerop (t))
3381 /* The only valid CONSTRUCTORs in gimple with pointer typed
3382 elements are zero-initializer. But in IPA mode we also
3383 process global initializers, so verify at least. */
3384 || (TREE_CODE (t) == CONSTRUCTOR
3385 && CONSTRUCTOR_NELTS (t) == 0))
3387 if (flag_delete_null_pointer_checks)
3388 temp.var = nothing_id;
3389 else
3390 temp.var = nonlocal_id;
3391 temp.type = ADDRESSOF;
3392 temp.offset = 0;
3393 results->safe_push (temp);
3394 return;
3397 /* String constants are read-only. */
3398 if (TREE_CODE (t) == STRING_CST)
3400 temp.var = readonly_id;
3401 temp.type = SCALAR;
3402 temp.offset = 0;
3403 results->safe_push (temp);
3404 return;
3407 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3409 case tcc_expression:
3411 switch (TREE_CODE (t))
3413 case ADDR_EXPR:
3414 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3415 return;
3416 default:;
3418 break;
3420 case tcc_reference:
3422 switch (TREE_CODE (t))
3424 case MEM_REF:
3426 struct constraint_expr cs;
3427 varinfo_t vi, curr;
3428 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0),
3429 TREE_OPERAND (t, 1), results);
3430 do_deref (results);
3432 /* If we are not taking the address then make sure to process
3433 all subvariables we might access. */
3434 if (address_p)
3435 return;
3437 cs = results->last ();
3438 if (cs.type == DEREF
3439 && type_can_have_subvars (TREE_TYPE (t)))
3441 /* For dereferences this means we have to defer it
3442 to solving time. */
3443 results->last ().offset = UNKNOWN_OFFSET;
3444 return;
3446 if (cs.type != SCALAR)
3447 return;
3449 vi = get_varinfo (cs.var);
3450 curr = vi_next (vi);
3451 if (!vi->is_full_var
3452 && curr)
3454 unsigned HOST_WIDE_INT size;
3455 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t))))
3456 size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t)));
3457 else
3458 size = -1;
3459 for (; curr; curr = vi_next (curr))
3461 if (curr->offset - vi->offset < size)
3463 cs.var = curr->id;
3464 results->safe_push (cs);
3466 else
3467 break;
3470 return;
3472 case ARRAY_REF:
3473 case ARRAY_RANGE_REF:
3474 case COMPONENT_REF:
3475 get_constraint_for_component_ref (t, results, address_p, lhs_p);
3476 return;
3477 case VIEW_CONVERT_EXPR:
3478 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p,
3479 lhs_p);
3480 return;
3481 /* We are missing handling for TARGET_MEM_REF here. */
3482 default:;
3484 break;
3486 case tcc_exceptional:
3488 switch (TREE_CODE (t))
3490 case SSA_NAME:
3492 get_constraint_for_ssa_var (t, results, address_p);
3493 return;
3495 case CONSTRUCTOR:
3497 unsigned int i;
3498 tree val;
3499 auto_vec<ce_s> tmp;
3500 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
3502 struct constraint_expr *rhsp;
3503 unsigned j;
3504 get_constraint_for_1 (val, &tmp, address_p, lhs_p);
3505 FOR_EACH_VEC_ELT (tmp, j, rhsp)
3506 results->safe_push (*rhsp);
3507 tmp.truncate (0);
3509 /* We do not know whether the constructor was complete,
3510 so technically we have to add &NOTHING or &ANYTHING
3511 like we do for an empty constructor as well. */
3512 return;
3514 default:;
3516 break;
3518 case tcc_declaration:
3520 get_constraint_for_ssa_var (t, results, address_p);
3521 return;
3523 case tcc_constant:
3525 /* We cannot refer to automatic variables through constants. */
3526 temp.type = ADDRESSOF;
3527 temp.var = nonlocal_id;
3528 temp.offset = 0;
3529 results->safe_push (temp);
3530 return;
3532 default:;
3535 /* The default fallback is a constraint from anything. */
3536 temp.type = ADDRESSOF;
3537 temp.var = anything_id;
3538 temp.offset = 0;
3539 results->safe_push (temp);
3542 /* Given a gimple tree T, return the constraint expression vector for it. */
3544 static void
3545 get_constraint_for (tree t, vec<ce_s> *results)
3547 gcc_assert (results->length () == 0);
3549 get_constraint_for_1 (t, results, false, true);
3552 /* Given a gimple tree T, return the constraint expression vector for it
3553 to be used as the rhs of a constraint. */
3555 static void
3556 get_constraint_for_rhs (tree t, vec<ce_s> *results)
3558 gcc_assert (results->length () == 0);
3560 get_constraint_for_1 (t, results, false, false);
3564 /* Efficiently generates constraints from all entries in *RHSC to all
3565 entries in *LHSC. */
3567 static void
3568 process_all_all_constraints (vec<ce_s> lhsc,
3569 vec<ce_s> rhsc)
3571 struct constraint_expr *lhsp, *rhsp;
3572 unsigned i, j;
3574 if (lhsc.length () <= 1 || rhsc.length () <= 1)
3576 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3577 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
3578 process_constraint (new_constraint (*lhsp, *rhsp));
3580 else
3582 struct constraint_expr tmp;
3583 tmp = new_scalar_tmp_constraint_exp ("allalltmp");
3584 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
3585 process_constraint (new_constraint (tmp, *rhsp));
3586 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3587 process_constraint (new_constraint (*lhsp, tmp));
3591 /* Handle aggregate copies by expanding into copies of the respective
3592 fields of the structures. */
3594 static void
3595 do_structure_copy (tree lhsop, tree rhsop)
3597 struct constraint_expr *lhsp, *rhsp;
3598 auto_vec<ce_s> lhsc;
3599 auto_vec<ce_s> rhsc;
3600 unsigned j;
3602 get_constraint_for (lhsop, &lhsc);
3603 get_constraint_for_rhs (rhsop, &rhsc);
3604 lhsp = &lhsc[0];
3605 rhsp = &rhsc[0];
3606 if (lhsp->type == DEREF
3607 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3608 || rhsp->type == DEREF)
3610 if (lhsp->type == DEREF)
3612 gcc_assert (lhsc.length () == 1);
3613 lhsp->offset = UNKNOWN_OFFSET;
3615 if (rhsp->type == DEREF)
3617 gcc_assert (rhsc.length () == 1);
3618 rhsp->offset = UNKNOWN_OFFSET;
3620 process_all_all_constraints (lhsc, rhsc);
3622 else if (lhsp->type == SCALAR
3623 && (rhsp->type == SCALAR
3624 || rhsp->type == ADDRESSOF))
3626 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3627 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
3628 unsigned k = 0;
3629 get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize);
3630 get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize);
3631 for (j = 0; lhsc.iterate (j, &lhsp);)
3633 varinfo_t lhsv, rhsv;
3634 rhsp = &rhsc[k];
3635 lhsv = get_varinfo (lhsp->var);
3636 rhsv = get_varinfo (rhsp->var);
3637 if (lhsv->may_have_pointers
3638 && (lhsv->is_full_var
3639 || rhsv->is_full_var
3640 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3641 rhsv->offset + lhsoffset, rhsv->size)))
3642 process_constraint (new_constraint (*lhsp, *rhsp));
3643 if (!rhsv->is_full_var
3644 && (lhsv->is_full_var
3645 || (lhsv->offset + rhsoffset + lhsv->size
3646 > rhsv->offset + lhsoffset + rhsv->size)))
3648 ++k;
3649 if (k >= rhsc.length ())
3650 break;
3652 else
3653 ++j;
3656 else
3657 gcc_unreachable ();
3660 /* Create constraints ID = { rhsc }. */
3662 static void
3663 make_constraints_to (unsigned id, vec<ce_s> rhsc)
3665 struct constraint_expr *c;
3666 struct constraint_expr includes;
3667 unsigned int j;
3669 includes.var = id;
3670 includes.offset = 0;
3671 includes.type = SCALAR;
3673 FOR_EACH_VEC_ELT (rhsc, j, c)
3674 process_constraint (new_constraint (includes, *c));
3677 /* Create a constraint ID = OP. */
3679 static void
3680 make_constraint_to (unsigned id, tree op)
3682 auto_vec<ce_s> rhsc;
3683 get_constraint_for_rhs (op, &rhsc);
3684 make_constraints_to (id, rhsc);
3687 /* Create a constraint ID = &FROM. */
3689 static void
3690 make_constraint_from (varinfo_t vi, int from)
3692 struct constraint_expr lhs, rhs;
3694 lhs.var = vi->id;
3695 lhs.offset = 0;
3696 lhs.type = SCALAR;
3698 rhs.var = from;
3699 rhs.offset = 0;
3700 rhs.type = ADDRESSOF;
3701 process_constraint (new_constraint (lhs, rhs));
3704 /* Create a constraint ID = FROM. */
3706 static void
3707 make_copy_constraint (varinfo_t vi, int from)
3709 struct constraint_expr lhs, rhs;
3711 lhs.var = vi->id;
3712 lhs.offset = 0;
3713 lhs.type = SCALAR;
3715 rhs.var = from;
3716 rhs.offset = 0;
3717 rhs.type = SCALAR;
3718 process_constraint (new_constraint (lhs, rhs));
3721 /* Make constraints necessary to make OP escape. */
3723 static void
3724 make_escape_constraint (tree op)
3726 make_constraint_to (escaped_id, op);
3729 /* Add constraints to that the solution of VI is transitively closed. */
3731 static void
3732 make_transitive_closure_constraints (varinfo_t vi)
3734 struct constraint_expr lhs, rhs;
3736 /* VAR = *VAR; */
3737 lhs.type = SCALAR;
3738 lhs.var = vi->id;
3739 lhs.offset = 0;
3740 rhs.type = DEREF;
3741 rhs.var = vi->id;
3742 rhs.offset = UNKNOWN_OFFSET;
3743 process_constraint (new_constraint (lhs, rhs));
3746 /* Temporary storage for fake var decls. */
3747 struct obstack fake_var_decl_obstack;
3749 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3751 static tree
3752 build_fake_var_decl (tree type)
3754 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl);
3755 memset (decl, 0, sizeof (struct tree_var_decl));
3756 TREE_SET_CODE (decl, VAR_DECL);
3757 TREE_TYPE (decl) = type;
3758 DECL_UID (decl) = allocate_decl_uid ();
3759 SET_DECL_PT_UID (decl, -1);
3760 layout_decl (decl, 0);
3761 return decl;
3764 /* Create a new artificial heap variable with NAME.
3765 Return the created variable. */
3767 static varinfo_t
3768 make_heapvar (const char *name)
3770 varinfo_t vi;
3771 tree heapvar;
3773 heapvar = build_fake_var_decl (ptr_type_node);
3774 DECL_EXTERNAL (heapvar) = 1;
3776 vi = new_var_info (heapvar, name);
3777 vi->is_artificial_var = true;
3778 vi->is_heap_var = true;
3779 vi->is_unknown_size_var = true;
3780 vi->offset = 0;
3781 vi->fullsize = ~0;
3782 vi->size = ~0;
3783 vi->is_full_var = true;
3784 insert_vi_for_tree (heapvar, vi);
3786 return vi;
3789 /* Create a new artificial heap variable with NAME and make a
3790 constraint from it to LHS. Set flags according to a tag used
3791 for tracking restrict pointers. */
3793 static varinfo_t
3794 make_constraint_from_restrict (varinfo_t lhs, const char *name)
3796 varinfo_t vi = make_heapvar (name);
3797 vi->is_restrict_var = 1;
3798 vi->is_global_var = 1;
3799 vi->may_have_pointers = 1;
3800 make_constraint_from (lhs, vi->id);
3801 return vi;
3804 /* Create a new artificial heap variable with NAME and make a
3805 constraint from it to LHS. Set flags according to a tag used
3806 for tracking restrict pointers and make the artificial heap
3807 point to global memory. */
3809 static varinfo_t
3810 make_constraint_from_global_restrict (varinfo_t lhs, const char *name)
3812 varinfo_t vi = make_constraint_from_restrict (lhs, name);
3813 make_copy_constraint (vi, nonlocal_id);
3814 return vi;
3817 /* In IPA mode there are varinfos for different aspects of reach
3818 function designator. One for the points-to set of the return
3819 value, one for the variables that are clobbered by the function,
3820 one for its uses and one for each parameter (including a single
3821 glob for remaining variadic arguments). */
3823 enum { fi_clobbers = 1, fi_uses = 2,
3824 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 };
3826 /* Get a constraint for the requested part of a function designator FI
3827 when operating in IPA mode. */
3829 static struct constraint_expr
3830 get_function_part_constraint (varinfo_t fi, unsigned part)
3832 struct constraint_expr c;
3834 gcc_assert (in_ipa_mode);
3836 if (fi->id == anything_id)
3838 /* ??? We probably should have a ANYFN special variable. */
3839 c.var = anything_id;
3840 c.offset = 0;
3841 c.type = SCALAR;
3843 else if (TREE_CODE (fi->decl) == FUNCTION_DECL)
3845 varinfo_t ai = first_vi_for_offset (fi, part);
3846 if (ai)
3847 c.var = ai->id;
3848 else
3849 c.var = anything_id;
3850 c.offset = 0;
3851 c.type = SCALAR;
3853 else
3855 c.var = fi->id;
3856 c.offset = part;
3857 c.type = DEREF;
3860 return c;
3863 /* For non-IPA mode, generate constraints necessary for a call on the
3864 RHS. */
3866 static void
3867 handle_rhs_call (gimple stmt, vec<ce_s> *results)
3869 struct constraint_expr rhsc;
3870 unsigned i;
3871 bool returns_uses = false;
3873 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3875 tree arg = gimple_call_arg (stmt, i);
3876 int flags = gimple_call_arg_flags (stmt, i);
3878 /* If the argument is not used we can ignore it. */
3879 if (flags & EAF_UNUSED)
3880 continue;
3882 /* As we compute ESCAPED context-insensitive we do not gain
3883 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3884 set. The argument would still get clobbered through the
3885 escape solution. */
3886 if ((flags & EAF_NOCLOBBER)
3887 && (flags & EAF_NOESCAPE))
3889 varinfo_t uses = get_call_use_vi (stmt);
3890 if (!(flags & EAF_DIRECT))
3892 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3893 make_constraint_to (tem->id, arg);
3894 make_transitive_closure_constraints (tem);
3895 make_copy_constraint (uses, tem->id);
3897 else
3898 make_constraint_to (uses->id, arg);
3899 returns_uses = true;
3901 else if (flags & EAF_NOESCAPE)
3903 struct constraint_expr lhs, rhs;
3904 varinfo_t uses = get_call_use_vi (stmt);
3905 varinfo_t clobbers = get_call_clobber_vi (stmt);
3906 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3907 make_constraint_to (tem->id, arg);
3908 if (!(flags & EAF_DIRECT))
3909 make_transitive_closure_constraints (tem);
3910 make_copy_constraint (uses, tem->id);
3911 make_copy_constraint (clobbers, tem->id);
3912 /* Add *tem = nonlocal, do not add *tem = callused as
3913 EAF_NOESCAPE parameters do not escape to other parameters
3914 and all other uses appear in NONLOCAL as well. */
3915 lhs.type = DEREF;
3916 lhs.var = tem->id;
3917 lhs.offset = 0;
3918 rhs.type = SCALAR;
3919 rhs.var = nonlocal_id;
3920 rhs.offset = 0;
3921 process_constraint (new_constraint (lhs, rhs));
3922 returns_uses = true;
3924 else
3925 make_escape_constraint (arg);
3928 /* If we added to the calls uses solution make sure we account for
3929 pointers to it to be returned. */
3930 if (returns_uses)
3932 rhsc.var = get_call_use_vi (stmt)->id;
3933 rhsc.offset = 0;
3934 rhsc.type = SCALAR;
3935 results->safe_push (rhsc);
3938 /* The static chain escapes as well. */
3939 if (gimple_call_chain (stmt))
3940 make_escape_constraint (gimple_call_chain (stmt));
3942 /* And if we applied NRV the address of the return slot escapes as well. */
3943 if (gimple_call_return_slot_opt_p (stmt)
3944 && gimple_call_lhs (stmt) != NULL_TREE
3945 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3947 auto_vec<ce_s> tmpc;
3948 struct constraint_expr lhsc, *c;
3949 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3950 lhsc.var = escaped_id;
3951 lhsc.offset = 0;
3952 lhsc.type = SCALAR;
3953 FOR_EACH_VEC_ELT (tmpc, i, c)
3954 process_constraint (new_constraint (lhsc, *c));
3957 /* Regular functions return nonlocal memory. */
3958 rhsc.var = nonlocal_id;
3959 rhsc.offset = 0;
3960 rhsc.type = SCALAR;
3961 results->safe_push (rhsc);
3964 /* For non-IPA mode, generate constraints necessary for a call
3965 that returns a pointer and assigns it to LHS. This simply makes
3966 the LHS point to global and escaped variables. */
3968 static void
3969 handle_lhs_call (gimple stmt, tree lhs, int flags, vec<ce_s> rhsc,
3970 tree fndecl)
3972 auto_vec<ce_s> lhsc;
3974 get_constraint_for (lhs, &lhsc);
3975 /* If the store is to a global decl make sure to
3976 add proper escape constraints. */
3977 lhs = get_base_address (lhs);
3978 if (lhs
3979 && DECL_P (lhs)
3980 && is_global_var (lhs))
3982 struct constraint_expr tmpc;
3983 tmpc.var = escaped_id;
3984 tmpc.offset = 0;
3985 tmpc.type = SCALAR;
3986 lhsc.safe_push (tmpc);
3989 /* If the call returns an argument unmodified override the rhs
3990 constraints. */
3991 flags = gimple_call_return_flags (stmt);
3992 if (flags & ERF_RETURNS_ARG
3993 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
3995 tree arg;
3996 rhsc.create (0);
3997 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
3998 get_constraint_for (arg, &rhsc);
3999 process_all_all_constraints (lhsc, rhsc);
4000 rhsc.release ();
4002 else if (flags & ERF_NOALIAS)
4004 varinfo_t vi;
4005 struct constraint_expr tmpc;
4006 rhsc.create (0);
4007 vi = make_heapvar ("HEAP");
4008 /* We are marking allocated storage local, we deal with it becoming
4009 global by escaping and setting of vars_contains_escaped_heap. */
4010 DECL_EXTERNAL (vi->decl) = 0;
4011 vi->is_global_var = 0;
4012 /* If this is not a real malloc call assume the memory was
4013 initialized and thus may point to global memory. All
4014 builtin functions with the malloc attribute behave in a sane way. */
4015 if (!fndecl
4016 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
4017 make_constraint_from (vi, nonlocal_id);
4018 tmpc.var = vi->id;
4019 tmpc.offset = 0;
4020 tmpc.type = ADDRESSOF;
4021 rhsc.safe_push (tmpc);
4022 process_all_all_constraints (lhsc, rhsc);
4023 rhsc.release ();
4025 else
4026 process_all_all_constraints (lhsc, rhsc);
4029 /* For non-IPA mode, generate constraints necessary for a call of a
4030 const function that returns a pointer in the statement STMT. */
4032 static void
4033 handle_const_call (gimple stmt, vec<ce_s> *results)
4035 struct constraint_expr rhsc;
4036 unsigned int k;
4038 /* Treat nested const functions the same as pure functions as far
4039 as the static chain is concerned. */
4040 if (gimple_call_chain (stmt))
4042 varinfo_t uses = get_call_use_vi (stmt);
4043 make_transitive_closure_constraints (uses);
4044 make_constraint_to (uses->id, gimple_call_chain (stmt));
4045 rhsc.var = uses->id;
4046 rhsc.offset = 0;
4047 rhsc.type = SCALAR;
4048 results->safe_push (rhsc);
4051 /* May return arguments. */
4052 for (k = 0; k < gimple_call_num_args (stmt); ++k)
4054 tree arg = gimple_call_arg (stmt, k);
4055 auto_vec<ce_s> argc;
4056 unsigned i;
4057 struct constraint_expr *argp;
4058 get_constraint_for_rhs (arg, &argc);
4059 FOR_EACH_VEC_ELT (argc, i, argp)
4060 results->safe_push (*argp);
4063 /* May return addresses of globals. */
4064 rhsc.var = nonlocal_id;
4065 rhsc.offset = 0;
4066 rhsc.type = ADDRESSOF;
4067 results->safe_push (rhsc);
4070 /* For non-IPA mode, generate constraints necessary for a call to a
4071 pure function in statement STMT. */
4073 static void
4074 handle_pure_call (gimple stmt, vec<ce_s> *results)
4076 struct constraint_expr rhsc;
4077 unsigned i;
4078 varinfo_t uses = NULL;
4080 /* Memory reached from pointer arguments is call-used. */
4081 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4083 tree arg = gimple_call_arg (stmt, i);
4084 if (!uses)
4086 uses = get_call_use_vi (stmt);
4087 make_transitive_closure_constraints (uses);
4089 make_constraint_to (uses->id, arg);
4092 /* The static chain is used as well. */
4093 if (gimple_call_chain (stmt))
4095 if (!uses)
4097 uses = get_call_use_vi (stmt);
4098 make_transitive_closure_constraints (uses);
4100 make_constraint_to (uses->id, gimple_call_chain (stmt));
4103 /* Pure functions may return call-used and nonlocal memory. */
4104 if (uses)
4106 rhsc.var = uses->id;
4107 rhsc.offset = 0;
4108 rhsc.type = SCALAR;
4109 results->safe_push (rhsc);
4111 rhsc.var = nonlocal_id;
4112 rhsc.offset = 0;
4113 rhsc.type = SCALAR;
4114 results->safe_push (rhsc);
4118 /* Return the varinfo for the callee of CALL. */
4120 static varinfo_t
4121 get_fi_for_callee (gimple call)
4123 tree decl, fn = gimple_call_fn (call);
4125 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
4126 fn = OBJ_TYPE_REF_EXPR (fn);
4128 /* If we can directly resolve the function being called, do so.
4129 Otherwise, it must be some sort of indirect expression that
4130 we should still be able to handle. */
4131 decl = gimple_call_addr_fndecl (fn);
4132 if (decl)
4133 return get_vi_for_tree (decl);
4135 /* If the function is anything other than a SSA name pointer we have no
4136 clue and should be getting ANYFN (well, ANYTHING for now). */
4137 if (!fn || TREE_CODE (fn) != SSA_NAME)
4138 return get_varinfo (anything_id);
4140 if (SSA_NAME_IS_DEFAULT_DEF (fn)
4141 && (TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
4142 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL))
4143 fn = SSA_NAME_VAR (fn);
4145 return get_vi_for_tree (fn);
4148 /* Create constraints for the builtin call T. Return true if the call
4149 was handled, otherwise false. */
4151 static bool
4152 find_func_aliases_for_builtin_call (gimple t)
4154 tree fndecl = gimple_call_fndecl (t);
4155 vec<ce_s> lhsc = vNULL;
4156 vec<ce_s> rhsc = vNULL;
4157 varinfo_t fi;
4159 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4160 /* ??? All builtins that are handled here need to be handled
4161 in the alias-oracle query functions explicitly! */
4162 switch (DECL_FUNCTION_CODE (fndecl))
4164 /* All the following functions return a pointer to the same object
4165 as their first argument points to. The functions do not add
4166 to the ESCAPED solution. The functions make the first argument
4167 pointed to memory point to what the second argument pointed to
4168 memory points to. */
4169 case BUILT_IN_STRCPY:
4170 case BUILT_IN_STRNCPY:
4171 case BUILT_IN_BCOPY:
4172 case BUILT_IN_MEMCPY:
4173 case BUILT_IN_MEMMOVE:
4174 case BUILT_IN_MEMPCPY:
4175 case BUILT_IN_STPCPY:
4176 case BUILT_IN_STPNCPY:
4177 case BUILT_IN_STRCAT:
4178 case BUILT_IN_STRNCAT:
4179 case BUILT_IN_STRCPY_CHK:
4180 case BUILT_IN_STRNCPY_CHK:
4181 case BUILT_IN_MEMCPY_CHK:
4182 case BUILT_IN_MEMMOVE_CHK:
4183 case BUILT_IN_MEMPCPY_CHK:
4184 case BUILT_IN_STPCPY_CHK:
4185 case BUILT_IN_STPNCPY_CHK:
4186 case BUILT_IN_STRCAT_CHK:
4187 case BUILT_IN_STRNCAT_CHK:
4188 case BUILT_IN_TM_MEMCPY:
4189 case BUILT_IN_TM_MEMMOVE:
4191 tree res = gimple_call_lhs (t);
4192 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4193 == BUILT_IN_BCOPY ? 1 : 0));
4194 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4195 == BUILT_IN_BCOPY ? 0 : 1));
4196 if (res != NULL_TREE)
4198 get_constraint_for (res, &lhsc);
4199 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4200 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
4201 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4202 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
4203 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK
4204 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK)
4205 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4206 else
4207 get_constraint_for (dest, &rhsc);
4208 process_all_all_constraints (lhsc, rhsc);
4209 lhsc.release ();
4210 rhsc.release ();
4212 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4213 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4214 do_deref (&lhsc);
4215 do_deref (&rhsc);
4216 process_all_all_constraints (lhsc, rhsc);
4217 lhsc.release ();
4218 rhsc.release ();
4219 return true;
4221 case BUILT_IN_MEMSET:
4222 case BUILT_IN_MEMSET_CHK:
4223 case BUILT_IN_TM_MEMSET:
4225 tree res = gimple_call_lhs (t);
4226 tree dest = gimple_call_arg (t, 0);
4227 unsigned i;
4228 ce_s *lhsp;
4229 struct constraint_expr ac;
4230 if (res != NULL_TREE)
4232 get_constraint_for (res, &lhsc);
4233 get_constraint_for (dest, &rhsc);
4234 process_all_all_constraints (lhsc, rhsc);
4235 lhsc.release ();
4236 rhsc.release ();
4238 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4239 do_deref (&lhsc);
4240 if (flag_delete_null_pointer_checks
4241 && integer_zerop (gimple_call_arg (t, 1)))
4243 ac.type = ADDRESSOF;
4244 ac.var = nothing_id;
4246 else
4248 ac.type = SCALAR;
4249 ac.var = integer_id;
4251 ac.offset = 0;
4252 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4253 process_constraint (new_constraint (*lhsp, ac));
4254 lhsc.release ();
4255 return true;
4257 case BUILT_IN_POSIX_MEMALIGN:
4259 tree ptrptr = gimple_call_arg (t, 0);
4260 get_constraint_for (ptrptr, &lhsc);
4261 do_deref (&lhsc);
4262 varinfo_t vi = make_heapvar ("HEAP");
4263 /* We are marking allocated storage local, we deal with it becoming
4264 global by escaping and setting of vars_contains_escaped_heap. */
4265 DECL_EXTERNAL (vi->decl) = 0;
4266 vi->is_global_var = 0;
4267 struct constraint_expr tmpc;
4268 tmpc.var = vi->id;
4269 tmpc.offset = 0;
4270 tmpc.type = ADDRESSOF;
4271 rhsc.safe_push (tmpc);
4272 process_all_all_constraints (lhsc, rhsc);
4273 lhsc.release ();
4274 rhsc.release ();
4275 return true;
4277 case BUILT_IN_ASSUME_ALIGNED:
4279 tree res = gimple_call_lhs (t);
4280 tree dest = gimple_call_arg (t, 0);
4281 if (res != NULL_TREE)
4283 get_constraint_for (res, &lhsc);
4284 get_constraint_for (dest, &rhsc);
4285 process_all_all_constraints (lhsc, rhsc);
4286 lhsc.release ();
4287 rhsc.release ();
4289 return true;
4291 /* All the following functions do not return pointers, do not
4292 modify the points-to sets of memory reachable from their
4293 arguments and do not add to the ESCAPED solution. */
4294 case BUILT_IN_SINCOS:
4295 case BUILT_IN_SINCOSF:
4296 case BUILT_IN_SINCOSL:
4297 case BUILT_IN_FREXP:
4298 case BUILT_IN_FREXPF:
4299 case BUILT_IN_FREXPL:
4300 case BUILT_IN_GAMMA_R:
4301 case BUILT_IN_GAMMAF_R:
4302 case BUILT_IN_GAMMAL_R:
4303 case BUILT_IN_LGAMMA_R:
4304 case BUILT_IN_LGAMMAF_R:
4305 case BUILT_IN_LGAMMAL_R:
4306 case BUILT_IN_MODF:
4307 case BUILT_IN_MODFF:
4308 case BUILT_IN_MODFL:
4309 case BUILT_IN_REMQUO:
4310 case BUILT_IN_REMQUOF:
4311 case BUILT_IN_REMQUOL:
4312 case BUILT_IN_FREE:
4313 return true;
4314 case BUILT_IN_STRDUP:
4315 case BUILT_IN_STRNDUP:
4316 if (gimple_call_lhs (t))
4318 handle_lhs_call (t, gimple_call_lhs (t), gimple_call_flags (t),
4319 vNULL, fndecl);
4320 get_constraint_for_ptr_offset (gimple_call_lhs (t),
4321 NULL_TREE, &lhsc);
4322 get_constraint_for_ptr_offset (gimple_call_arg (t, 0),
4323 NULL_TREE, &rhsc);
4324 do_deref (&lhsc);
4325 do_deref (&rhsc);
4326 process_all_all_constraints (lhsc, rhsc);
4327 lhsc.release ();
4328 rhsc.release ();
4329 return true;
4331 break;
4332 /* String / character search functions return a pointer into the
4333 source string or NULL. */
4334 case BUILT_IN_INDEX:
4335 case BUILT_IN_STRCHR:
4336 case BUILT_IN_STRRCHR:
4337 case BUILT_IN_MEMCHR:
4338 case BUILT_IN_STRSTR:
4339 case BUILT_IN_STRPBRK:
4340 if (gimple_call_lhs (t))
4342 tree src = gimple_call_arg (t, 0);
4343 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4344 constraint_expr nul;
4345 nul.var = nothing_id;
4346 nul.offset = 0;
4347 nul.type = ADDRESSOF;
4348 rhsc.safe_push (nul);
4349 get_constraint_for (gimple_call_lhs (t), &lhsc);
4350 process_all_all_constraints (lhsc, rhsc);
4351 lhsc.release ();
4352 rhsc.release ();
4354 return true;
4355 /* Trampolines are special - they set up passing the static
4356 frame. */
4357 case BUILT_IN_INIT_TRAMPOLINE:
4359 tree tramp = gimple_call_arg (t, 0);
4360 tree nfunc = gimple_call_arg (t, 1);
4361 tree frame = gimple_call_arg (t, 2);
4362 unsigned i;
4363 struct constraint_expr lhs, *rhsp;
4364 if (in_ipa_mode)
4366 varinfo_t nfi = NULL;
4367 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR);
4368 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0));
4369 if (nfi)
4371 lhs = get_function_part_constraint (nfi, fi_static_chain);
4372 get_constraint_for (frame, &rhsc);
4373 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4374 process_constraint (new_constraint (lhs, *rhsp));
4375 rhsc.release ();
4377 /* Make the frame point to the function for
4378 the trampoline adjustment call. */
4379 get_constraint_for (tramp, &lhsc);
4380 do_deref (&lhsc);
4381 get_constraint_for (nfunc, &rhsc);
4382 process_all_all_constraints (lhsc, rhsc);
4383 rhsc.release ();
4384 lhsc.release ();
4386 return true;
4389 /* Else fallthru to generic handling which will let
4390 the frame escape. */
4391 break;
4393 case BUILT_IN_ADJUST_TRAMPOLINE:
4395 tree tramp = gimple_call_arg (t, 0);
4396 tree res = gimple_call_lhs (t);
4397 if (in_ipa_mode && res)
4399 get_constraint_for (res, &lhsc);
4400 get_constraint_for (tramp, &rhsc);
4401 do_deref (&rhsc);
4402 process_all_all_constraints (lhsc, rhsc);
4403 rhsc.release ();
4404 lhsc.release ();
4406 return true;
4408 CASE_BUILT_IN_TM_STORE (1):
4409 CASE_BUILT_IN_TM_STORE (2):
4410 CASE_BUILT_IN_TM_STORE (4):
4411 CASE_BUILT_IN_TM_STORE (8):
4412 CASE_BUILT_IN_TM_STORE (FLOAT):
4413 CASE_BUILT_IN_TM_STORE (DOUBLE):
4414 CASE_BUILT_IN_TM_STORE (LDOUBLE):
4415 CASE_BUILT_IN_TM_STORE (M64):
4416 CASE_BUILT_IN_TM_STORE (M128):
4417 CASE_BUILT_IN_TM_STORE (M256):
4419 tree addr = gimple_call_arg (t, 0);
4420 tree src = gimple_call_arg (t, 1);
4422 get_constraint_for (addr, &lhsc);
4423 do_deref (&lhsc);
4424 get_constraint_for (src, &rhsc);
4425 process_all_all_constraints (lhsc, rhsc);
4426 lhsc.release ();
4427 rhsc.release ();
4428 return true;
4430 CASE_BUILT_IN_TM_LOAD (1):
4431 CASE_BUILT_IN_TM_LOAD (2):
4432 CASE_BUILT_IN_TM_LOAD (4):
4433 CASE_BUILT_IN_TM_LOAD (8):
4434 CASE_BUILT_IN_TM_LOAD (FLOAT):
4435 CASE_BUILT_IN_TM_LOAD (DOUBLE):
4436 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
4437 CASE_BUILT_IN_TM_LOAD (M64):
4438 CASE_BUILT_IN_TM_LOAD (M128):
4439 CASE_BUILT_IN_TM_LOAD (M256):
4441 tree dest = gimple_call_lhs (t);
4442 tree addr = gimple_call_arg (t, 0);
4444 get_constraint_for (dest, &lhsc);
4445 get_constraint_for (addr, &rhsc);
4446 do_deref (&rhsc);
4447 process_all_all_constraints (lhsc, rhsc);
4448 lhsc.release ();
4449 rhsc.release ();
4450 return true;
4452 /* Variadic argument handling needs to be handled in IPA
4453 mode as well. */
4454 case BUILT_IN_VA_START:
4456 tree valist = gimple_call_arg (t, 0);
4457 struct constraint_expr rhs, *lhsp;
4458 unsigned i;
4459 get_constraint_for (valist, &lhsc);
4460 do_deref (&lhsc);
4461 /* The va_list gets access to pointers in variadic
4462 arguments. Which we know in the case of IPA analysis
4463 and otherwise are just all nonlocal variables. */
4464 if (in_ipa_mode)
4466 fi = lookup_vi_for_tree (cfun->decl);
4467 rhs = get_function_part_constraint (fi, ~0);
4468 rhs.type = ADDRESSOF;
4470 else
4472 rhs.var = nonlocal_id;
4473 rhs.type = ADDRESSOF;
4474 rhs.offset = 0;
4476 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4477 process_constraint (new_constraint (*lhsp, rhs));
4478 lhsc.release ();
4479 /* va_list is clobbered. */
4480 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4481 return true;
4483 /* va_end doesn't have any effect that matters. */
4484 case BUILT_IN_VA_END:
4485 return true;
4486 /* Alternate return. Simply give up for now. */
4487 case BUILT_IN_RETURN:
4489 fi = NULL;
4490 if (!in_ipa_mode
4491 || !(fi = get_vi_for_tree (cfun->decl)))
4492 make_constraint_from (get_varinfo (escaped_id), anything_id);
4493 else if (in_ipa_mode
4494 && fi != NULL)
4496 struct constraint_expr lhs, rhs;
4497 lhs = get_function_part_constraint (fi, fi_result);
4498 rhs.var = anything_id;
4499 rhs.offset = 0;
4500 rhs.type = SCALAR;
4501 process_constraint (new_constraint (lhs, rhs));
4503 return true;
4505 /* printf-style functions may have hooks to set pointers to
4506 point to somewhere into the generated string. Leave them
4507 for a later exercise... */
4508 default:
4509 /* Fallthru to general call handling. */;
4512 return false;
4515 /* Create constraints for the call T. */
4517 static void
4518 find_func_aliases_for_call (gimple t)
4520 tree fndecl = gimple_call_fndecl (t);
4521 vec<ce_s> lhsc = vNULL;
4522 vec<ce_s> rhsc = vNULL;
4523 varinfo_t fi;
4525 if (fndecl != NULL_TREE
4526 && DECL_BUILT_IN (fndecl)
4527 && find_func_aliases_for_builtin_call (t))
4528 return;
4530 fi = get_fi_for_callee (t);
4531 if (!in_ipa_mode
4532 || (fndecl && !fi->is_fn_info))
4534 vec<ce_s> rhsc = vNULL;
4535 int flags = gimple_call_flags (t);
4537 /* Const functions can return their arguments and addresses
4538 of global memory but not of escaped memory. */
4539 if (flags & (ECF_CONST|ECF_NOVOPS))
4541 if (gimple_call_lhs (t))
4542 handle_const_call (t, &rhsc);
4544 /* Pure functions can return addresses in and of memory
4545 reachable from their arguments, but they are not an escape
4546 point for reachable memory of their arguments. */
4547 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4548 handle_pure_call (t, &rhsc);
4549 else
4550 handle_rhs_call (t, &rhsc);
4551 if (gimple_call_lhs (t))
4552 handle_lhs_call (t, gimple_call_lhs (t), flags, rhsc, fndecl);
4553 rhsc.release ();
4555 else
4557 tree lhsop;
4558 unsigned j;
4560 /* Assign all the passed arguments to the appropriate incoming
4561 parameters of the function. */
4562 for (j = 0; j < gimple_call_num_args (t); j++)
4564 struct constraint_expr lhs ;
4565 struct constraint_expr *rhsp;
4566 tree arg = gimple_call_arg (t, j);
4568 get_constraint_for_rhs (arg, &rhsc);
4569 lhs = get_function_part_constraint (fi, fi_parm_base + j);
4570 while (rhsc.length () != 0)
4572 rhsp = &rhsc.last ();
4573 process_constraint (new_constraint (lhs, *rhsp));
4574 rhsc.pop ();
4578 /* If we are returning a value, assign it to the result. */
4579 lhsop = gimple_call_lhs (t);
4580 if (lhsop)
4582 struct constraint_expr rhs;
4583 struct constraint_expr *lhsp;
4585 get_constraint_for (lhsop, &lhsc);
4586 rhs = get_function_part_constraint (fi, fi_result);
4587 if (fndecl
4588 && DECL_RESULT (fndecl)
4589 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4591 vec<ce_s> tem = vNULL;
4592 tem.safe_push (rhs);
4593 do_deref (&tem);
4594 rhs = tem[0];
4595 tem.release ();
4597 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4598 process_constraint (new_constraint (*lhsp, rhs));
4601 /* If we pass the result decl by reference, honor that. */
4602 if (lhsop
4603 && fndecl
4604 && DECL_RESULT (fndecl)
4605 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4607 struct constraint_expr lhs;
4608 struct constraint_expr *rhsp;
4610 get_constraint_for_address_of (lhsop, &rhsc);
4611 lhs = get_function_part_constraint (fi, fi_result);
4612 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4613 process_constraint (new_constraint (lhs, *rhsp));
4614 rhsc.release ();
4617 /* If we use a static chain, pass it along. */
4618 if (gimple_call_chain (t))
4620 struct constraint_expr lhs;
4621 struct constraint_expr *rhsp;
4623 get_constraint_for (gimple_call_chain (t), &rhsc);
4624 lhs = get_function_part_constraint (fi, fi_static_chain);
4625 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4626 process_constraint (new_constraint (lhs, *rhsp));
4631 /* Walk statement T setting up aliasing constraints according to the
4632 references found in T. This function is the main part of the
4633 constraint builder. AI points to auxiliary alias information used
4634 when building alias sets and computing alias grouping heuristics. */
4636 static void
4637 find_func_aliases (gimple origt)
4639 gimple t = origt;
4640 vec<ce_s> lhsc = vNULL;
4641 vec<ce_s> rhsc = vNULL;
4642 struct constraint_expr *c;
4643 varinfo_t fi;
4645 /* Now build constraints expressions. */
4646 if (gimple_code (t) == GIMPLE_PHI)
4648 size_t i;
4649 unsigned int j;
4651 /* For a phi node, assign all the arguments to
4652 the result. */
4653 get_constraint_for (gimple_phi_result (t), &lhsc);
4654 for (i = 0; i < gimple_phi_num_args (t); i++)
4656 tree strippedrhs = PHI_ARG_DEF (t, i);
4658 STRIP_NOPS (strippedrhs);
4659 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
4661 FOR_EACH_VEC_ELT (lhsc, j, c)
4663 struct constraint_expr *c2;
4664 while (rhsc.length () > 0)
4666 c2 = &rhsc.last ();
4667 process_constraint (new_constraint (*c, *c2));
4668 rhsc.pop ();
4673 /* In IPA mode, we need to generate constraints to pass call
4674 arguments through their calls. There are two cases,
4675 either a GIMPLE_CALL returning a value, or just a plain
4676 GIMPLE_CALL when we are not.
4678 In non-ipa mode, we need to generate constraints for each
4679 pointer passed by address. */
4680 else if (is_gimple_call (t))
4681 find_func_aliases_for_call (t);
4683 /* Otherwise, just a regular assignment statement. Only care about
4684 operations with pointer result, others are dealt with as escape
4685 points if they have pointer operands. */
4686 else if (is_gimple_assign (t))
4688 /* Otherwise, just a regular assignment statement. */
4689 tree lhsop = gimple_assign_lhs (t);
4690 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
4692 if (rhsop && TREE_CLOBBER_P (rhsop))
4693 /* Ignore clobbers, they don't actually store anything into
4694 the LHS. */
4696 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
4697 do_structure_copy (lhsop, rhsop);
4698 else
4700 enum tree_code code = gimple_assign_rhs_code (t);
4702 get_constraint_for (lhsop, &lhsc);
4704 if (FLOAT_TYPE_P (TREE_TYPE (lhsop)))
4705 /* If the operation produces a floating point result then
4706 assume the value is not produced to transfer a pointer. */
4708 else if (code == POINTER_PLUS_EXPR)
4709 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4710 gimple_assign_rhs2 (t), &rhsc);
4711 else if (code == BIT_AND_EXPR
4712 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
4714 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4715 the pointer. Handle it by offsetting it by UNKNOWN. */
4716 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4717 NULL_TREE, &rhsc);
4719 else if ((CONVERT_EXPR_CODE_P (code)
4720 && !(POINTER_TYPE_P (gimple_expr_type (t))
4721 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
4722 || gimple_assign_single_p (t))
4723 get_constraint_for_rhs (rhsop, &rhsc);
4724 else if (code == COND_EXPR)
4726 /* The result is a merge of both COND_EXPR arms. */
4727 vec<ce_s> tmp = vNULL;
4728 struct constraint_expr *rhsp;
4729 unsigned i;
4730 get_constraint_for_rhs (gimple_assign_rhs2 (t), &rhsc);
4731 get_constraint_for_rhs (gimple_assign_rhs3 (t), &tmp);
4732 FOR_EACH_VEC_ELT (tmp, i, rhsp)
4733 rhsc.safe_push (*rhsp);
4734 tmp.release ();
4736 else if (truth_value_p (code))
4737 /* Truth value results are not pointer (parts). Or at least
4738 very very unreasonable obfuscation of a part. */
4740 else
4742 /* All other operations are merges. */
4743 vec<ce_s> tmp = vNULL;
4744 struct constraint_expr *rhsp;
4745 unsigned i, j;
4746 get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc);
4747 for (i = 2; i < gimple_num_ops (t); ++i)
4749 get_constraint_for_rhs (gimple_op (t, i), &tmp);
4750 FOR_EACH_VEC_ELT (tmp, j, rhsp)
4751 rhsc.safe_push (*rhsp);
4752 tmp.truncate (0);
4754 tmp.release ();
4756 process_all_all_constraints (lhsc, rhsc);
4758 /* If there is a store to a global variable the rhs escapes. */
4759 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
4760 && DECL_P (lhsop)
4761 && is_global_var (lhsop)
4762 && (!in_ipa_mode
4763 || DECL_EXTERNAL (lhsop) || TREE_PUBLIC (lhsop)))
4764 make_escape_constraint (rhsop);
4766 /* Handle escapes through return. */
4767 else if (gimple_code (t) == GIMPLE_RETURN
4768 && gimple_return_retval (t) != NULL_TREE)
4770 fi = NULL;
4771 if (!in_ipa_mode
4772 || !(fi = get_vi_for_tree (cfun->decl)))
4773 make_escape_constraint (gimple_return_retval (t));
4774 else if (in_ipa_mode
4775 && fi != NULL)
4777 struct constraint_expr lhs ;
4778 struct constraint_expr *rhsp;
4779 unsigned i;
4781 lhs = get_function_part_constraint (fi, fi_result);
4782 get_constraint_for_rhs (gimple_return_retval (t), &rhsc);
4783 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4784 process_constraint (new_constraint (lhs, *rhsp));
4787 /* Handle asms conservatively by adding escape constraints to everything. */
4788 else if (gimple_code (t) == GIMPLE_ASM)
4790 unsigned i, noutputs;
4791 const char **oconstraints;
4792 const char *constraint;
4793 bool allows_mem, allows_reg, is_inout;
4795 noutputs = gimple_asm_noutputs (t);
4796 oconstraints = XALLOCAVEC (const char *, noutputs);
4798 for (i = 0; i < noutputs; ++i)
4800 tree link = gimple_asm_output_op (t, i);
4801 tree op = TREE_VALUE (link);
4803 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4804 oconstraints[i] = constraint;
4805 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4806 &allows_reg, &is_inout);
4808 /* A memory constraint makes the address of the operand escape. */
4809 if (!allows_reg && allows_mem)
4810 make_escape_constraint (build_fold_addr_expr (op));
4812 /* The asm may read global memory, so outputs may point to
4813 any global memory. */
4814 if (op)
4816 vec<ce_s> lhsc = vNULL;
4817 struct constraint_expr rhsc, *lhsp;
4818 unsigned j;
4819 get_constraint_for (op, &lhsc);
4820 rhsc.var = nonlocal_id;
4821 rhsc.offset = 0;
4822 rhsc.type = SCALAR;
4823 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4824 process_constraint (new_constraint (*lhsp, rhsc));
4825 lhsc.release ();
4828 for (i = 0; i < gimple_asm_ninputs (t); ++i)
4830 tree link = gimple_asm_input_op (t, i);
4831 tree op = TREE_VALUE (link);
4833 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4835 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4836 &allows_mem, &allows_reg);
4838 /* A memory constraint makes the address of the operand escape. */
4839 if (!allows_reg && allows_mem)
4840 make_escape_constraint (build_fold_addr_expr (op));
4841 /* Strictly we'd only need the constraint to ESCAPED if
4842 the asm clobbers memory, otherwise using something
4843 along the lines of per-call clobbers/uses would be enough. */
4844 else if (op)
4845 make_escape_constraint (op);
4849 rhsc.release ();
4850 lhsc.release ();
4854 /* Create a constraint adding to the clobber set of FI the memory
4855 pointed to by PTR. */
4857 static void
4858 process_ipa_clobber (varinfo_t fi, tree ptr)
4860 vec<ce_s> ptrc = vNULL;
4861 struct constraint_expr *c, lhs;
4862 unsigned i;
4863 get_constraint_for_rhs (ptr, &ptrc);
4864 lhs = get_function_part_constraint (fi, fi_clobbers);
4865 FOR_EACH_VEC_ELT (ptrc, i, c)
4866 process_constraint (new_constraint (lhs, *c));
4867 ptrc.release ();
4870 /* Walk statement T setting up clobber and use constraints according to the
4871 references found in T. This function is a main part of the
4872 IPA constraint builder. */
4874 static void
4875 find_func_clobbers (gimple origt)
4877 gimple t = origt;
4878 vec<ce_s> lhsc = vNULL;
4879 auto_vec<ce_s> rhsc;
4880 varinfo_t fi;
4882 /* Add constraints for clobbered/used in IPA mode.
4883 We are not interested in what automatic variables are clobbered
4884 or used as we only use the information in the caller to which
4885 they do not escape. */
4886 gcc_assert (in_ipa_mode);
4888 /* If the stmt refers to memory in any way it better had a VUSE. */
4889 if (gimple_vuse (t) == NULL_TREE)
4890 return;
4892 /* We'd better have function information for the current function. */
4893 fi = lookup_vi_for_tree (cfun->decl);
4894 gcc_assert (fi != NULL);
4896 /* Account for stores in assignments and calls. */
4897 if (gimple_vdef (t) != NULL_TREE
4898 && gimple_has_lhs (t))
4900 tree lhs = gimple_get_lhs (t);
4901 tree tem = lhs;
4902 while (handled_component_p (tem))
4903 tem = TREE_OPERAND (tem, 0);
4904 if ((DECL_P (tem)
4905 && !auto_var_in_fn_p (tem, cfun->decl))
4906 || INDIRECT_REF_P (tem)
4907 || (TREE_CODE (tem) == MEM_REF
4908 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4909 && auto_var_in_fn_p
4910 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4912 struct constraint_expr lhsc, *rhsp;
4913 unsigned i;
4914 lhsc = get_function_part_constraint (fi, fi_clobbers);
4915 get_constraint_for_address_of (lhs, &rhsc);
4916 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4917 process_constraint (new_constraint (lhsc, *rhsp));
4918 rhsc.release ();
4922 /* Account for uses in assigments and returns. */
4923 if (gimple_assign_single_p (t)
4924 || (gimple_code (t) == GIMPLE_RETURN
4925 && gimple_return_retval (t) != NULL_TREE))
4927 tree rhs = (gimple_assign_single_p (t)
4928 ? gimple_assign_rhs1 (t) : gimple_return_retval (t));
4929 tree tem = rhs;
4930 while (handled_component_p (tem))
4931 tem = TREE_OPERAND (tem, 0);
4932 if ((DECL_P (tem)
4933 && !auto_var_in_fn_p (tem, cfun->decl))
4934 || INDIRECT_REF_P (tem)
4935 || (TREE_CODE (tem) == MEM_REF
4936 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4937 && auto_var_in_fn_p
4938 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4940 struct constraint_expr lhs, *rhsp;
4941 unsigned i;
4942 lhs = get_function_part_constraint (fi, fi_uses);
4943 get_constraint_for_address_of (rhs, &rhsc);
4944 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4945 process_constraint (new_constraint (lhs, *rhsp));
4946 rhsc.release ();
4950 if (is_gimple_call (t))
4952 varinfo_t cfi = NULL;
4953 tree decl = gimple_call_fndecl (t);
4954 struct constraint_expr lhs, rhs;
4955 unsigned i, j;
4957 /* For builtins we do not have separate function info. For those
4958 we do not generate escapes for we have to generate clobbers/uses. */
4959 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4960 switch (DECL_FUNCTION_CODE (decl))
4962 /* The following functions use and clobber memory pointed to
4963 by their arguments. */
4964 case BUILT_IN_STRCPY:
4965 case BUILT_IN_STRNCPY:
4966 case BUILT_IN_BCOPY:
4967 case BUILT_IN_MEMCPY:
4968 case BUILT_IN_MEMMOVE:
4969 case BUILT_IN_MEMPCPY:
4970 case BUILT_IN_STPCPY:
4971 case BUILT_IN_STPNCPY:
4972 case BUILT_IN_STRCAT:
4973 case BUILT_IN_STRNCAT:
4974 case BUILT_IN_STRCPY_CHK:
4975 case BUILT_IN_STRNCPY_CHK:
4976 case BUILT_IN_MEMCPY_CHK:
4977 case BUILT_IN_MEMMOVE_CHK:
4978 case BUILT_IN_MEMPCPY_CHK:
4979 case BUILT_IN_STPCPY_CHK:
4980 case BUILT_IN_STPNCPY_CHK:
4981 case BUILT_IN_STRCAT_CHK:
4982 case BUILT_IN_STRNCAT_CHK:
4984 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4985 == BUILT_IN_BCOPY ? 1 : 0));
4986 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4987 == BUILT_IN_BCOPY ? 0 : 1));
4988 unsigned i;
4989 struct constraint_expr *rhsp, *lhsp;
4990 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4991 lhs = get_function_part_constraint (fi, fi_clobbers);
4992 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4993 process_constraint (new_constraint (lhs, *lhsp));
4994 lhsc.release ();
4995 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4996 lhs = get_function_part_constraint (fi, fi_uses);
4997 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4998 process_constraint (new_constraint (lhs, *rhsp));
4999 rhsc.release ();
5000 return;
5002 /* The following function clobbers memory pointed to by
5003 its argument. */
5004 case BUILT_IN_MEMSET:
5005 case BUILT_IN_MEMSET_CHK:
5006 case BUILT_IN_POSIX_MEMALIGN:
5008 tree dest = gimple_call_arg (t, 0);
5009 unsigned i;
5010 ce_s *lhsp;
5011 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
5012 lhs = get_function_part_constraint (fi, fi_clobbers);
5013 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
5014 process_constraint (new_constraint (lhs, *lhsp));
5015 lhsc.release ();
5016 return;
5018 /* The following functions clobber their second and third
5019 arguments. */
5020 case BUILT_IN_SINCOS:
5021 case BUILT_IN_SINCOSF:
5022 case BUILT_IN_SINCOSL:
5024 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5025 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5026 return;
5028 /* The following functions clobber their second argument. */
5029 case BUILT_IN_FREXP:
5030 case BUILT_IN_FREXPF:
5031 case BUILT_IN_FREXPL:
5032 case BUILT_IN_LGAMMA_R:
5033 case BUILT_IN_LGAMMAF_R:
5034 case BUILT_IN_LGAMMAL_R:
5035 case BUILT_IN_GAMMA_R:
5036 case BUILT_IN_GAMMAF_R:
5037 case BUILT_IN_GAMMAL_R:
5038 case BUILT_IN_MODF:
5039 case BUILT_IN_MODFF:
5040 case BUILT_IN_MODFL:
5042 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5043 return;
5045 /* The following functions clobber their third argument. */
5046 case BUILT_IN_REMQUO:
5047 case BUILT_IN_REMQUOF:
5048 case BUILT_IN_REMQUOL:
5050 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5051 return;
5053 /* The following functions neither read nor clobber memory. */
5054 case BUILT_IN_ASSUME_ALIGNED:
5055 case BUILT_IN_FREE:
5056 return;
5057 /* Trampolines are of no interest to us. */
5058 case BUILT_IN_INIT_TRAMPOLINE:
5059 case BUILT_IN_ADJUST_TRAMPOLINE:
5060 return;
5061 case BUILT_IN_VA_START:
5062 case BUILT_IN_VA_END:
5063 return;
5064 /* printf-style functions may have hooks to set pointers to
5065 point to somewhere into the generated string. Leave them
5066 for a later exercise... */
5067 default:
5068 /* Fallthru to general call handling. */;
5071 /* Parameters passed by value are used. */
5072 lhs = get_function_part_constraint (fi, fi_uses);
5073 for (i = 0; i < gimple_call_num_args (t); i++)
5075 struct constraint_expr *rhsp;
5076 tree arg = gimple_call_arg (t, i);
5078 if (TREE_CODE (arg) == SSA_NAME
5079 || is_gimple_min_invariant (arg))
5080 continue;
5082 get_constraint_for_address_of (arg, &rhsc);
5083 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5084 process_constraint (new_constraint (lhs, *rhsp));
5085 rhsc.release ();
5088 /* Build constraints for propagating clobbers/uses along the
5089 callgraph edges. */
5090 cfi = get_fi_for_callee (t);
5091 if (cfi->id == anything_id)
5093 if (gimple_vdef (t))
5094 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5095 anything_id);
5096 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5097 anything_id);
5098 return;
5101 /* For callees without function info (that's external functions),
5102 ESCAPED is clobbered and used. */
5103 if (gimple_call_fndecl (t)
5104 && !cfi->is_fn_info)
5106 varinfo_t vi;
5108 if (gimple_vdef (t))
5109 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5110 escaped_id);
5111 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
5113 /* Also honor the call statement use/clobber info. */
5114 if ((vi = lookup_call_clobber_vi (t)) != NULL)
5115 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5116 vi->id);
5117 if ((vi = lookup_call_use_vi (t)) != NULL)
5118 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
5119 vi->id);
5120 return;
5123 /* Otherwise the caller clobbers and uses what the callee does.
5124 ??? This should use a new complex constraint that filters
5125 local variables of the callee. */
5126 if (gimple_vdef (t))
5128 lhs = get_function_part_constraint (fi, fi_clobbers);
5129 rhs = get_function_part_constraint (cfi, fi_clobbers);
5130 process_constraint (new_constraint (lhs, rhs));
5132 lhs = get_function_part_constraint (fi, fi_uses);
5133 rhs = get_function_part_constraint (cfi, fi_uses);
5134 process_constraint (new_constraint (lhs, rhs));
5136 else if (gimple_code (t) == GIMPLE_ASM)
5138 /* ??? Ick. We can do better. */
5139 if (gimple_vdef (t))
5140 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5141 anything_id);
5142 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5143 anything_id);
5148 /* Find the first varinfo in the same variable as START that overlaps with
5149 OFFSET. Return NULL if we can't find one. */
5151 static varinfo_t
5152 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
5154 /* If the offset is outside of the variable, bail out. */
5155 if (offset >= start->fullsize)
5156 return NULL;
5158 /* If we cannot reach offset from start, lookup the first field
5159 and start from there. */
5160 if (start->offset > offset)
5161 start = get_varinfo (start->head);
5163 while (start)
5165 /* We may not find a variable in the field list with the actual
5166 offset when when we have glommed a structure to a variable.
5167 In that case, however, offset should still be within the size
5168 of the variable. */
5169 if (offset >= start->offset
5170 && (offset - start->offset) < start->size)
5171 return start;
5173 start = vi_next (start);
5176 return NULL;
5179 /* Find the first varinfo in the same variable as START that overlaps with
5180 OFFSET. If there is no such varinfo the varinfo directly preceding
5181 OFFSET is returned. */
5183 static varinfo_t
5184 first_or_preceding_vi_for_offset (varinfo_t start,
5185 unsigned HOST_WIDE_INT offset)
5187 /* If we cannot reach offset from start, lookup the first field
5188 and start from there. */
5189 if (start->offset > offset)
5190 start = get_varinfo (start->head);
5192 /* We may not find a variable in the field list with the actual
5193 offset when when we have glommed a structure to a variable.
5194 In that case, however, offset should still be within the size
5195 of the variable.
5196 If we got beyond the offset we look for return the field
5197 directly preceding offset which may be the last field. */
5198 while (start->next
5199 && offset >= start->offset
5200 && !((offset - start->offset) < start->size))
5201 start = vi_next (start);
5203 return start;
5207 /* This structure is used during pushing fields onto the fieldstack
5208 to track the offset of the field, since bitpos_of_field gives it
5209 relative to its immediate containing type, and we want it relative
5210 to the ultimate containing object. */
5212 struct fieldoff
5214 /* Offset from the base of the base containing object to this field. */
5215 HOST_WIDE_INT offset;
5217 /* Size, in bits, of the field. */
5218 unsigned HOST_WIDE_INT size;
5220 unsigned has_unknown_size : 1;
5222 unsigned must_have_pointers : 1;
5224 unsigned may_have_pointers : 1;
5226 unsigned only_restrict_pointers : 1;
5228 typedef struct fieldoff fieldoff_s;
5231 /* qsort comparison function for two fieldoff's PA and PB */
5233 static int
5234 fieldoff_compare (const void *pa, const void *pb)
5236 const fieldoff_s *foa = (const fieldoff_s *)pa;
5237 const fieldoff_s *fob = (const fieldoff_s *)pb;
5238 unsigned HOST_WIDE_INT foasize, fobsize;
5240 if (foa->offset < fob->offset)
5241 return -1;
5242 else if (foa->offset > fob->offset)
5243 return 1;
5245 foasize = foa->size;
5246 fobsize = fob->size;
5247 if (foasize < fobsize)
5248 return -1;
5249 else if (foasize > fobsize)
5250 return 1;
5251 return 0;
5254 /* Sort a fieldstack according to the field offset and sizes. */
5255 static void
5256 sort_fieldstack (vec<fieldoff_s> fieldstack)
5258 fieldstack.qsort (fieldoff_compare);
5261 /* Return true if T is a type that can have subvars. */
5263 static inline bool
5264 type_can_have_subvars (const_tree t)
5266 /* Aggregates without overlapping fields can have subvars. */
5267 return TREE_CODE (t) == RECORD_TYPE;
5270 /* Return true if V is a tree that we can have subvars for.
5271 Normally, this is any aggregate type. Also complex
5272 types which are not gimple registers can have subvars. */
5274 static inline bool
5275 var_can_have_subvars (const_tree v)
5277 /* Volatile variables should never have subvars. */
5278 if (TREE_THIS_VOLATILE (v))
5279 return false;
5281 /* Non decls or memory tags can never have subvars. */
5282 if (!DECL_P (v))
5283 return false;
5285 return type_can_have_subvars (TREE_TYPE (v));
5288 /* Return true if T is a type that does contain pointers. */
5290 static bool
5291 type_must_have_pointers (tree type)
5293 if (POINTER_TYPE_P (type))
5294 return true;
5296 if (TREE_CODE (type) == ARRAY_TYPE)
5297 return type_must_have_pointers (TREE_TYPE (type));
5299 /* A function or method can have pointers as arguments, so track
5300 those separately. */
5301 if (TREE_CODE (type) == FUNCTION_TYPE
5302 || TREE_CODE (type) == METHOD_TYPE)
5303 return true;
5305 return false;
5308 static bool
5309 field_must_have_pointers (tree t)
5311 return type_must_have_pointers (TREE_TYPE (t));
5314 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5315 the fields of TYPE onto fieldstack, recording their offsets along
5316 the way.
5318 OFFSET is used to keep track of the offset in this entire
5319 structure, rather than just the immediately containing structure.
5320 Returns false if the caller is supposed to handle the field we
5321 recursed for. */
5323 static bool
5324 push_fields_onto_fieldstack (tree type, vec<fieldoff_s> *fieldstack,
5325 HOST_WIDE_INT offset)
5327 tree field;
5328 bool empty_p = true;
5330 if (TREE_CODE (type) != RECORD_TYPE)
5331 return false;
5333 /* If the vector of fields is growing too big, bail out early.
5334 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5335 sure this fails. */
5336 if (fieldstack->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5337 return false;
5339 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5340 if (TREE_CODE (field) == FIELD_DECL)
5342 bool push = false;
5343 HOST_WIDE_INT foff = bitpos_of_field (field);
5345 if (!var_can_have_subvars (field)
5346 || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
5347 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5348 push = true;
5349 else if (!push_fields_onto_fieldstack
5350 (TREE_TYPE (field), fieldstack, offset + foff)
5351 && (DECL_SIZE (field)
5352 && !integer_zerop (DECL_SIZE (field))))
5353 /* Empty structures may have actual size, like in C++. So
5354 see if we didn't push any subfields and the size is
5355 nonzero, push the field onto the stack. */
5356 push = true;
5358 if (push)
5360 fieldoff_s *pair = NULL;
5361 bool has_unknown_size = false;
5362 bool must_have_pointers_p;
5364 if (!fieldstack->is_empty ())
5365 pair = &fieldstack->last ();
5367 /* If there isn't anything at offset zero, create sth. */
5368 if (!pair
5369 && offset + foff != 0)
5371 fieldoff_s e = {0, offset + foff, false, false, false, false};
5372 pair = fieldstack->safe_push (e);
5375 if (!DECL_SIZE (field)
5376 || !tree_fits_uhwi_p (DECL_SIZE (field)))
5377 has_unknown_size = true;
5379 /* If adjacent fields do not contain pointers merge them. */
5380 must_have_pointers_p = field_must_have_pointers (field);
5381 if (pair
5382 && !has_unknown_size
5383 && !must_have_pointers_p
5384 && !pair->must_have_pointers
5385 && !pair->has_unknown_size
5386 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5388 pair->size += tree_to_uhwi (DECL_SIZE (field));
5390 else
5392 fieldoff_s e;
5393 e.offset = offset + foff;
5394 e.has_unknown_size = has_unknown_size;
5395 if (!has_unknown_size)
5396 e.size = tree_to_uhwi (DECL_SIZE (field));
5397 else
5398 e.size = -1;
5399 e.must_have_pointers = must_have_pointers_p;
5400 e.may_have_pointers = true;
5401 e.only_restrict_pointers
5402 = (!has_unknown_size
5403 && POINTER_TYPE_P (TREE_TYPE (field))
5404 && TYPE_RESTRICT (TREE_TYPE (field)));
5405 fieldstack->safe_push (e);
5409 empty_p = false;
5412 return !empty_p;
5415 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5416 if it is a varargs function. */
5418 static unsigned int
5419 count_num_arguments (tree decl, bool *is_varargs)
5421 unsigned int num = 0;
5422 tree t;
5424 /* Capture named arguments for K&R functions. They do not
5425 have a prototype and thus no TYPE_ARG_TYPES. */
5426 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5427 ++num;
5429 /* Check if the function has variadic arguments. */
5430 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5431 if (TREE_VALUE (t) == void_type_node)
5432 break;
5433 if (!t)
5434 *is_varargs = true;
5436 return num;
5439 /* Creation function node for DECL, using NAME, and return the index
5440 of the variable we've created for the function. */
5442 static varinfo_t
5443 create_function_info_for (tree decl, const char *name)
5445 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5446 varinfo_t vi, prev_vi;
5447 tree arg;
5448 unsigned int i;
5449 bool is_varargs = false;
5450 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5452 /* Create the variable info. */
5454 vi = new_var_info (decl, name);
5455 vi->offset = 0;
5456 vi->size = 1;
5457 vi->fullsize = fi_parm_base + num_args;
5458 vi->is_fn_info = 1;
5459 vi->may_have_pointers = false;
5460 if (is_varargs)
5461 vi->fullsize = ~0;
5462 insert_vi_for_tree (vi->decl, vi);
5464 prev_vi = vi;
5466 /* Create a variable for things the function clobbers and one for
5467 things the function uses. */
5469 varinfo_t clobbervi, usevi;
5470 const char *newname;
5471 char *tempname;
5473 asprintf (&tempname, "%s.clobber", name);
5474 newname = ggc_strdup (tempname);
5475 free (tempname);
5477 clobbervi = new_var_info (NULL, newname);
5478 clobbervi->offset = fi_clobbers;
5479 clobbervi->size = 1;
5480 clobbervi->fullsize = vi->fullsize;
5481 clobbervi->is_full_var = true;
5482 clobbervi->is_global_var = false;
5483 gcc_assert (prev_vi->offset < clobbervi->offset);
5484 prev_vi->next = clobbervi->id;
5485 prev_vi = clobbervi;
5487 asprintf (&tempname, "%s.use", name);
5488 newname = ggc_strdup (tempname);
5489 free (tempname);
5491 usevi = new_var_info (NULL, newname);
5492 usevi->offset = fi_uses;
5493 usevi->size = 1;
5494 usevi->fullsize = vi->fullsize;
5495 usevi->is_full_var = true;
5496 usevi->is_global_var = false;
5497 gcc_assert (prev_vi->offset < usevi->offset);
5498 prev_vi->next = usevi->id;
5499 prev_vi = usevi;
5502 /* And one for the static chain. */
5503 if (fn->static_chain_decl != NULL_TREE)
5505 varinfo_t chainvi;
5506 const char *newname;
5507 char *tempname;
5509 asprintf (&tempname, "%s.chain", name);
5510 newname = ggc_strdup (tempname);
5511 free (tempname);
5513 chainvi = new_var_info (fn->static_chain_decl, newname);
5514 chainvi->offset = fi_static_chain;
5515 chainvi->size = 1;
5516 chainvi->fullsize = vi->fullsize;
5517 chainvi->is_full_var = true;
5518 chainvi->is_global_var = false;
5519 gcc_assert (prev_vi->offset < chainvi->offset);
5520 prev_vi->next = chainvi->id;
5521 prev_vi = chainvi;
5522 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5525 /* Create a variable for the return var. */
5526 if (DECL_RESULT (decl) != NULL
5527 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5529 varinfo_t resultvi;
5530 const char *newname;
5531 char *tempname;
5532 tree resultdecl = decl;
5534 if (DECL_RESULT (decl))
5535 resultdecl = DECL_RESULT (decl);
5537 asprintf (&tempname, "%s.result", name);
5538 newname = ggc_strdup (tempname);
5539 free (tempname);
5541 resultvi = new_var_info (resultdecl, newname);
5542 resultvi->offset = fi_result;
5543 resultvi->size = 1;
5544 resultvi->fullsize = vi->fullsize;
5545 resultvi->is_full_var = true;
5546 if (DECL_RESULT (decl))
5547 resultvi->may_have_pointers = true;
5548 gcc_assert (prev_vi->offset < resultvi->offset);
5549 prev_vi->next = resultvi->id;
5550 prev_vi = resultvi;
5551 if (DECL_RESULT (decl))
5552 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
5555 /* Set up variables for each argument. */
5556 arg = DECL_ARGUMENTS (decl);
5557 for (i = 0; i < num_args; i++)
5559 varinfo_t argvi;
5560 const char *newname;
5561 char *tempname;
5562 tree argdecl = decl;
5564 if (arg)
5565 argdecl = arg;
5567 asprintf (&tempname, "%s.arg%d", name, i);
5568 newname = ggc_strdup (tempname);
5569 free (tempname);
5571 argvi = new_var_info (argdecl, newname);
5572 argvi->offset = fi_parm_base + i;
5573 argvi->size = 1;
5574 argvi->is_full_var = true;
5575 argvi->fullsize = vi->fullsize;
5576 if (arg)
5577 argvi->may_have_pointers = true;
5578 gcc_assert (prev_vi->offset < argvi->offset);
5579 prev_vi->next = argvi->id;
5580 prev_vi = argvi;
5581 if (arg)
5583 insert_vi_for_tree (arg, argvi);
5584 arg = DECL_CHAIN (arg);
5588 /* Add one representative for all further args. */
5589 if (is_varargs)
5591 varinfo_t argvi;
5592 const char *newname;
5593 char *tempname;
5594 tree decl;
5596 asprintf (&tempname, "%s.varargs", name);
5597 newname = ggc_strdup (tempname);
5598 free (tempname);
5600 /* We need sth that can be pointed to for va_start. */
5601 decl = build_fake_var_decl (ptr_type_node);
5603 argvi = new_var_info (decl, newname);
5604 argvi->offset = fi_parm_base + num_args;
5605 argvi->size = ~0;
5606 argvi->is_full_var = true;
5607 argvi->is_heap_var = true;
5608 argvi->fullsize = vi->fullsize;
5609 gcc_assert (prev_vi->offset < argvi->offset);
5610 prev_vi->next = argvi->id;
5611 prev_vi = argvi;
5614 return vi;
5618 /* Return true if FIELDSTACK contains fields that overlap.
5619 FIELDSTACK is assumed to be sorted by offset. */
5621 static bool
5622 check_for_overlaps (vec<fieldoff_s> fieldstack)
5624 fieldoff_s *fo = NULL;
5625 unsigned int i;
5626 HOST_WIDE_INT lastoffset = -1;
5628 FOR_EACH_VEC_ELT (fieldstack, i, fo)
5630 if (fo->offset == lastoffset)
5631 return true;
5632 lastoffset = fo->offset;
5634 return false;
5637 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5638 This will also create any varinfo structures necessary for fields
5639 of DECL. */
5641 static varinfo_t
5642 create_variable_info_for_1 (tree decl, const char *name)
5644 varinfo_t vi, newvi;
5645 tree decl_type = TREE_TYPE (decl);
5646 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
5647 auto_vec<fieldoff_s> fieldstack;
5648 fieldoff_s *fo;
5649 unsigned int i;
5651 if (!declsize
5652 || !tree_fits_uhwi_p (declsize))
5654 vi = new_var_info (decl, name);
5655 vi->offset = 0;
5656 vi->size = ~0;
5657 vi->fullsize = ~0;
5658 vi->is_unknown_size_var = true;
5659 vi->is_full_var = true;
5660 vi->may_have_pointers = true;
5661 return vi;
5664 /* Collect field information. */
5665 if (use_field_sensitive
5666 && var_can_have_subvars (decl)
5667 /* ??? Force us to not use subfields for global initializers
5668 in IPA mode. Else we'd have to parse arbitrary initializers. */
5669 && !(in_ipa_mode
5670 && is_global_var (decl)
5671 && DECL_INITIAL (decl)))
5673 fieldoff_s *fo = NULL;
5674 bool notokay = false;
5675 unsigned int i;
5677 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
5679 for (i = 0; !notokay && fieldstack.iterate (i, &fo); i++)
5680 if (fo->has_unknown_size
5681 || fo->offset < 0)
5683 notokay = true;
5684 break;
5687 /* We can't sort them if we have a field with a variable sized type,
5688 which will make notokay = true. In that case, we are going to return
5689 without creating varinfos for the fields anyway, so sorting them is a
5690 waste to boot. */
5691 if (!notokay)
5693 sort_fieldstack (fieldstack);
5694 /* Due to some C++ FE issues, like PR 22488, we might end up
5695 what appear to be overlapping fields even though they,
5696 in reality, do not overlap. Until the C++ FE is fixed,
5697 we will simply disable field-sensitivity for these cases. */
5698 notokay = check_for_overlaps (fieldstack);
5701 if (notokay)
5702 fieldstack.release ();
5705 /* If we didn't end up collecting sub-variables create a full
5706 variable for the decl. */
5707 if (fieldstack.length () <= 1
5708 || fieldstack.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5710 vi = new_var_info (decl, name);
5711 vi->offset = 0;
5712 vi->may_have_pointers = true;
5713 vi->fullsize = tree_to_uhwi (declsize);
5714 vi->size = vi->fullsize;
5715 vi->is_full_var = true;
5716 fieldstack.release ();
5717 return vi;
5720 vi = new_var_info (decl, name);
5721 vi->fullsize = tree_to_uhwi (declsize);
5722 for (i = 0, newvi = vi;
5723 fieldstack.iterate (i, &fo);
5724 ++i, newvi = vi_next (newvi))
5726 const char *newname = "NULL";
5727 char *tempname;
5729 if (dump_file)
5731 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC
5732 "+" HOST_WIDE_INT_PRINT_DEC, name, fo->offset, fo->size);
5733 newname = ggc_strdup (tempname);
5734 free (tempname);
5736 newvi->name = newname;
5737 newvi->offset = fo->offset;
5738 newvi->size = fo->size;
5739 newvi->fullsize = vi->fullsize;
5740 newvi->may_have_pointers = fo->may_have_pointers;
5741 newvi->only_restrict_pointers = fo->only_restrict_pointers;
5742 if (i + 1 < fieldstack.length ())
5744 varinfo_t tem = new_var_info (decl, name);
5745 newvi->next = tem->id;
5746 tem->head = vi->id;
5750 return vi;
5753 static unsigned int
5754 create_variable_info_for (tree decl, const char *name)
5756 varinfo_t vi = create_variable_info_for_1 (decl, name);
5757 unsigned int id = vi->id;
5759 insert_vi_for_tree (decl, vi);
5761 if (TREE_CODE (decl) != VAR_DECL)
5762 return id;
5764 /* Create initial constraints for globals. */
5765 for (; vi; vi = vi_next (vi))
5767 if (!vi->may_have_pointers
5768 || !vi->is_global_var)
5769 continue;
5771 /* Mark global restrict qualified pointers. */
5772 if ((POINTER_TYPE_P (TREE_TYPE (decl))
5773 && TYPE_RESTRICT (TREE_TYPE (decl)))
5774 || vi->only_restrict_pointers)
5776 varinfo_t rvi
5777 = make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5778 /* ??? For now exclude reads from globals as restrict sources
5779 if those are not (indirectly) from incoming parameters. */
5780 rvi->is_restrict_var = false;
5781 continue;
5784 /* In non-IPA mode the initializer from nonlocal is all we need. */
5785 if (!in_ipa_mode
5786 || DECL_HARD_REGISTER (decl))
5787 make_copy_constraint (vi, nonlocal_id);
5789 /* In IPA mode parse the initializer and generate proper constraints
5790 for it. */
5791 else
5793 varpool_node *vnode = varpool_get_node (decl);
5795 /* For escaped variables initialize them from nonlocal. */
5796 if (!varpool_all_refs_explicit_p (vnode))
5797 make_copy_constraint (vi, nonlocal_id);
5799 /* If this is a global variable with an initializer and we are in
5800 IPA mode generate constraints for it. */
5801 if (DECL_INITIAL (decl)
5802 && vnode->definition)
5804 auto_vec<ce_s> rhsc;
5805 struct constraint_expr lhs, *rhsp;
5806 unsigned i;
5807 get_constraint_for_rhs (DECL_INITIAL (decl), &rhsc);
5808 lhs.var = vi->id;
5809 lhs.offset = 0;
5810 lhs.type = SCALAR;
5811 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5812 process_constraint (new_constraint (lhs, *rhsp));
5813 /* If this is a variable that escapes from the unit
5814 the initializer escapes as well. */
5815 if (!varpool_all_refs_explicit_p (vnode))
5817 lhs.var = escaped_id;
5818 lhs.offset = 0;
5819 lhs.type = SCALAR;
5820 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5821 process_constraint (new_constraint (lhs, *rhsp));
5827 return id;
5830 /* Print out the points-to solution for VAR to FILE. */
5832 static void
5833 dump_solution_for_var (FILE *file, unsigned int var)
5835 varinfo_t vi = get_varinfo (var);
5836 unsigned int i;
5837 bitmap_iterator bi;
5839 /* Dump the solution for unified vars anyway, this avoids difficulties
5840 in scanning dumps in the testsuite. */
5841 fprintf (file, "%s = { ", vi->name);
5842 vi = get_varinfo (find (var));
5843 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5844 fprintf (file, "%s ", get_varinfo (i)->name);
5845 fprintf (file, "}");
5847 /* But note when the variable was unified. */
5848 if (vi->id != var)
5849 fprintf (file, " same as %s", vi->name);
5851 fprintf (file, "\n");
5854 /* Print the points-to solution for VAR to stdout. */
5856 DEBUG_FUNCTION void
5857 debug_solution_for_var (unsigned int var)
5859 dump_solution_for_var (stdout, var);
5862 /* Create varinfo structures for all of the variables in the
5863 function for intraprocedural mode. */
5865 static void
5866 intra_create_variable_infos (void)
5868 tree t;
5870 /* For each incoming pointer argument arg, create the constraint ARG
5871 = NONLOCAL or a dummy variable if it is a restrict qualified
5872 passed-by-reference argument. */
5873 for (t = DECL_ARGUMENTS (current_function_decl); t; t = DECL_CHAIN (t))
5875 varinfo_t p = get_vi_for_tree (t);
5877 /* For restrict qualified pointers to objects passed by
5878 reference build a real representative for the pointed-to object.
5879 Treat restrict qualified references the same. */
5880 if (TYPE_RESTRICT (TREE_TYPE (t))
5881 && ((DECL_BY_REFERENCE (t) && POINTER_TYPE_P (TREE_TYPE (t)))
5882 || TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5883 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t))))
5885 struct constraint_expr lhsc, rhsc;
5886 varinfo_t vi;
5887 tree heapvar = build_fake_var_decl (TREE_TYPE (TREE_TYPE (t)));
5888 DECL_EXTERNAL (heapvar) = 1;
5889 vi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS");
5890 vi->is_restrict_var = 1;
5891 insert_vi_for_tree (heapvar, vi);
5892 lhsc.var = p->id;
5893 lhsc.type = SCALAR;
5894 lhsc.offset = 0;
5895 rhsc.var = vi->id;
5896 rhsc.type = ADDRESSOF;
5897 rhsc.offset = 0;
5898 process_constraint (new_constraint (lhsc, rhsc));
5899 for (; vi; vi = vi_next (vi))
5900 if (vi->may_have_pointers)
5902 if (vi->only_restrict_pointers)
5903 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5904 else
5905 make_copy_constraint (vi, nonlocal_id);
5907 continue;
5910 if (POINTER_TYPE_P (TREE_TYPE (t))
5911 && TYPE_RESTRICT (TREE_TYPE (t)))
5912 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5913 else
5915 for (; p; p = vi_next (p))
5917 if (p->only_restrict_pointers)
5918 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5919 else if (p->may_have_pointers)
5920 make_constraint_from (p, nonlocal_id);
5925 /* Add a constraint for a result decl that is passed by reference. */
5926 if (DECL_RESULT (cfun->decl)
5927 && DECL_BY_REFERENCE (DECL_RESULT (cfun->decl)))
5929 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (cfun->decl));
5931 for (p = result_vi; p; p = vi_next (p))
5932 make_constraint_from (p, nonlocal_id);
5935 /* Add a constraint for the incoming static chain parameter. */
5936 if (cfun->static_chain_decl != NULL_TREE)
5938 varinfo_t p, chain_vi = get_vi_for_tree (cfun->static_chain_decl);
5940 for (p = chain_vi; p; p = vi_next (p))
5941 make_constraint_from (p, nonlocal_id);
5945 /* Structure used to put solution bitmaps in a hashtable so they can
5946 be shared among variables with the same points-to set. */
5948 typedef struct shared_bitmap_info
5950 bitmap pt_vars;
5951 hashval_t hashcode;
5952 } *shared_bitmap_info_t;
5953 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
5955 /* Shared_bitmap hashtable helpers. */
5957 struct shared_bitmap_hasher : typed_free_remove <shared_bitmap_info>
5959 typedef shared_bitmap_info value_type;
5960 typedef shared_bitmap_info compare_type;
5961 static inline hashval_t hash (const value_type *);
5962 static inline bool equal (const value_type *, const compare_type *);
5965 /* Hash function for a shared_bitmap_info_t */
5967 inline hashval_t
5968 shared_bitmap_hasher::hash (const value_type *bi)
5970 return bi->hashcode;
5973 /* Equality function for two shared_bitmap_info_t's. */
5975 inline bool
5976 shared_bitmap_hasher::equal (const value_type *sbi1, const compare_type *sbi2)
5978 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
5981 /* Shared_bitmap hashtable. */
5983 static hash_table <shared_bitmap_hasher> shared_bitmap_table;
5985 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5986 existing instance if there is one, NULL otherwise. */
5988 static bitmap
5989 shared_bitmap_lookup (bitmap pt_vars)
5991 shared_bitmap_info **slot;
5992 struct shared_bitmap_info sbi;
5994 sbi.pt_vars = pt_vars;
5995 sbi.hashcode = bitmap_hash (pt_vars);
5997 slot = shared_bitmap_table.find_slot_with_hash (&sbi, sbi.hashcode,
5998 NO_INSERT);
5999 if (!slot)
6000 return NULL;
6001 else
6002 return (*slot)->pt_vars;
6006 /* Add a bitmap to the shared bitmap hashtable. */
6008 static void
6009 shared_bitmap_add (bitmap pt_vars)
6011 shared_bitmap_info **slot;
6012 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
6014 sbi->pt_vars = pt_vars;
6015 sbi->hashcode = bitmap_hash (pt_vars);
6017 slot = shared_bitmap_table.find_slot_with_hash (sbi, sbi->hashcode, INSERT);
6018 gcc_assert (!*slot);
6019 *slot = sbi;
6023 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
6025 static void
6026 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt)
6028 unsigned int i;
6029 bitmap_iterator bi;
6030 varinfo_t escaped_vi = get_varinfo (find (escaped_id));
6031 bool everything_escaped
6032 = escaped_vi->solution && bitmap_bit_p (escaped_vi->solution, anything_id);
6034 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
6036 varinfo_t vi = get_varinfo (i);
6038 /* The only artificial variables that are allowed in a may-alias
6039 set are heap variables. */
6040 if (vi->is_artificial_var && !vi->is_heap_var)
6041 continue;
6043 if (everything_escaped
6044 || (escaped_vi->solution
6045 && bitmap_bit_p (escaped_vi->solution, i)))
6047 pt->vars_contains_escaped = true;
6048 pt->vars_contains_escaped_heap = vi->is_heap_var;
6051 if (TREE_CODE (vi->decl) == VAR_DECL
6052 || TREE_CODE (vi->decl) == PARM_DECL
6053 || TREE_CODE (vi->decl) == RESULT_DECL)
6055 /* If we are in IPA mode we will not recompute points-to
6056 sets after inlining so make sure they stay valid. */
6057 if (in_ipa_mode
6058 && !DECL_PT_UID_SET_P (vi->decl))
6059 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
6061 /* Add the decl to the points-to set. Note that the points-to
6062 set contains global variables. */
6063 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
6064 if (vi->is_global_var)
6065 pt->vars_contains_nonlocal = true;
6071 /* Compute the points-to solution *PT for the variable VI. */
6073 static struct pt_solution
6074 find_what_var_points_to (varinfo_t orig_vi)
6076 unsigned int i;
6077 bitmap_iterator bi;
6078 bitmap finished_solution;
6079 bitmap result;
6080 varinfo_t vi;
6081 void **slot;
6082 struct pt_solution *pt;
6084 /* This variable may have been collapsed, let's get the real
6085 variable. */
6086 vi = get_varinfo (find (orig_vi->id));
6088 /* See if we have already computed the solution and return it. */
6089 slot = pointer_map_insert (final_solutions, vi);
6090 if (*slot != NULL)
6091 return *(struct pt_solution *)*slot;
6093 *slot = pt = XOBNEW (&final_solutions_obstack, struct pt_solution);
6094 memset (pt, 0, sizeof (struct pt_solution));
6096 /* Translate artificial variables into SSA_NAME_PTR_INFO
6097 attributes. */
6098 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
6100 varinfo_t vi = get_varinfo (i);
6102 if (vi->is_artificial_var)
6104 if (vi->id == nothing_id)
6105 pt->null = 1;
6106 else if (vi->id == escaped_id)
6108 if (in_ipa_mode)
6109 pt->ipa_escaped = 1;
6110 else
6111 pt->escaped = 1;
6112 /* Expand some special vars of ESCAPED in-place here. */
6113 varinfo_t evi = get_varinfo (find (escaped_id));
6114 if (bitmap_bit_p (evi->solution, nonlocal_id))
6115 pt->nonlocal = 1;
6117 else if (vi->id == nonlocal_id)
6118 pt->nonlocal = 1;
6119 else if (vi->is_heap_var)
6120 /* We represent heapvars in the points-to set properly. */
6122 else if (vi->id == readonly_id)
6123 /* Nobody cares. */
6125 else if (vi->id == anything_id
6126 || vi->id == integer_id)
6127 pt->anything = 1;
6131 /* Instead of doing extra work, simply do not create
6132 elaborate points-to information for pt_anything pointers. */
6133 if (pt->anything)
6134 return *pt;
6136 /* Share the final set of variables when possible. */
6137 finished_solution = BITMAP_GGC_ALLOC ();
6138 stats.points_to_sets_created++;
6140 set_uids_in_ptset (finished_solution, vi->solution, pt);
6141 result = shared_bitmap_lookup (finished_solution);
6142 if (!result)
6144 shared_bitmap_add (finished_solution);
6145 pt->vars = finished_solution;
6147 else
6149 pt->vars = result;
6150 bitmap_clear (finished_solution);
6153 return *pt;
6156 /* Given a pointer variable P, fill in its points-to set. */
6158 static void
6159 find_what_p_points_to (tree p)
6161 struct ptr_info_def *pi;
6162 tree lookup_p = p;
6163 varinfo_t vi;
6165 /* For parameters, get at the points-to set for the actual parm
6166 decl. */
6167 if (TREE_CODE (p) == SSA_NAME
6168 && SSA_NAME_IS_DEFAULT_DEF (p)
6169 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
6170 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL))
6171 lookup_p = SSA_NAME_VAR (p);
6173 vi = lookup_vi_for_tree (lookup_p);
6174 if (!vi)
6175 return;
6177 pi = get_ptr_info (p);
6178 pi->pt = find_what_var_points_to (vi);
6182 /* Query statistics for points-to solutions. */
6184 static struct {
6185 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
6186 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
6187 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
6188 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
6189 } pta_stats;
6191 void
6192 dump_pta_stats (FILE *s)
6194 fprintf (s, "\nPTA query stats:\n");
6195 fprintf (s, " pt_solution_includes: "
6196 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6197 HOST_WIDE_INT_PRINT_DEC" queries\n",
6198 pta_stats.pt_solution_includes_no_alias,
6199 pta_stats.pt_solution_includes_no_alias
6200 + pta_stats.pt_solution_includes_may_alias);
6201 fprintf (s, " pt_solutions_intersect: "
6202 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6203 HOST_WIDE_INT_PRINT_DEC" queries\n",
6204 pta_stats.pt_solutions_intersect_no_alias,
6205 pta_stats.pt_solutions_intersect_no_alias
6206 + pta_stats.pt_solutions_intersect_may_alias);
6210 /* Reset the points-to solution *PT to a conservative default
6211 (point to anything). */
6213 void
6214 pt_solution_reset (struct pt_solution *pt)
6216 memset (pt, 0, sizeof (struct pt_solution));
6217 pt->anything = true;
6220 /* Set the points-to solution *PT to point only to the variables
6221 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6222 global variables and VARS_CONTAINS_RESTRICT specifies whether
6223 it contains restrict tag variables. */
6225 void
6226 pt_solution_set (struct pt_solution *pt, bitmap vars,
6227 bool vars_contains_nonlocal)
6229 memset (pt, 0, sizeof (struct pt_solution));
6230 pt->vars = vars;
6231 pt->vars_contains_nonlocal = vars_contains_nonlocal;
6232 pt->vars_contains_escaped
6233 = (cfun->gimple_df->escaped.anything
6234 || bitmap_intersect_p (cfun->gimple_df->escaped.vars, vars));
6237 /* Set the points-to solution *PT to point only to the variable VAR. */
6239 void
6240 pt_solution_set_var (struct pt_solution *pt, tree var)
6242 memset (pt, 0, sizeof (struct pt_solution));
6243 pt->vars = BITMAP_GGC_ALLOC ();
6244 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
6245 pt->vars_contains_nonlocal = is_global_var (var);
6246 pt->vars_contains_escaped
6247 = (cfun->gimple_df->escaped.anything
6248 || bitmap_bit_p (cfun->gimple_df->escaped.vars, DECL_PT_UID (var)));
6251 /* Computes the union of the points-to solutions *DEST and *SRC and
6252 stores the result in *DEST. This changes the points-to bitmap
6253 of *DEST and thus may not be used if that might be shared.
6254 The points-to bitmap of *SRC and *DEST will not be shared after
6255 this function if they were not before. */
6257 static void
6258 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6260 dest->anything |= src->anything;
6261 if (dest->anything)
6263 pt_solution_reset (dest);
6264 return;
6267 dest->nonlocal |= src->nonlocal;
6268 dest->escaped |= src->escaped;
6269 dest->ipa_escaped |= src->ipa_escaped;
6270 dest->null |= src->null;
6271 dest->vars_contains_nonlocal |= src->vars_contains_nonlocal;
6272 dest->vars_contains_escaped |= src->vars_contains_escaped;
6273 dest->vars_contains_escaped_heap |= src->vars_contains_escaped_heap;
6274 if (!src->vars)
6275 return;
6277 if (!dest->vars)
6278 dest->vars = BITMAP_GGC_ALLOC ();
6279 bitmap_ior_into (dest->vars, src->vars);
6282 /* Return true if the points-to solution *PT is empty. */
6284 bool
6285 pt_solution_empty_p (struct pt_solution *pt)
6287 if (pt->anything
6288 || pt->nonlocal)
6289 return false;
6291 if (pt->vars
6292 && !bitmap_empty_p (pt->vars))
6293 return false;
6295 /* If the solution includes ESCAPED, check if that is empty. */
6296 if (pt->escaped
6297 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6298 return false;
6300 /* If the solution includes ESCAPED, check if that is empty. */
6301 if (pt->ipa_escaped
6302 && !pt_solution_empty_p (&ipa_escaped_pt))
6303 return false;
6305 return true;
6308 /* Return true if the points-to solution *PT only point to a single var, and
6309 return the var uid in *UID. */
6311 bool
6312 pt_solution_singleton_p (struct pt_solution *pt, unsigned *uid)
6314 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
6315 || pt->null || pt->vars == NULL
6316 || !bitmap_single_bit_set_p (pt->vars))
6317 return false;
6319 *uid = bitmap_first_set_bit (pt->vars);
6320 return true;
6323 /* Return true if the points-to solution *PT includes global memory. */
6325 bool
6326 pt_solution_includes_global (struct pt_solution *pt)
6328 if (pt->anything
6329 || pt->nonlocal
6330 || pt->vars_contains_nonlocal
6331 /* The following is a hack to make the malloc escape hack work.
6332 In reality we'd need different sets for escaped-through-return
6333 and escaped-to-callees and passes would need to be updated. */
6334 || pt->vars_contains_escaped_heap)
6335 return true;
6337 /* 'escaped' is also a placeholder so we have to look into it. */
6338 if (pt->escaped)
6339 return pt_solution_includes_global (&cfun->gimple_df->escaped);
6341 if (pt->ipa_escaped)
6342 return pt_solution_includes_global (&ipa_escaped_pt);
6344 /* ??? This predicate is not correct for the IPA-PTA solution
6345 as we do not properly distinguish between unit escape points
6346 and global variables. */
6347 if (cfun->gimple_df->ipa_pta)
6348 return true;
6350 return false;
6353 /* Return true if the points-to solution *PT includes the variable
6354 declaration DECL. */
6356 static bool
6357 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6359 if (pt->anything)
6360 return true;
6362 if (pt->nonlocal
6363 && is_global_var (decl))
6364 return true;
6366 if (pt->vars
6367 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6368 return true;
6370 /* If the solution includes ESCAPED, check it. */
6371 if (pt->escaped
6372 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6373 return true;
6375 /* If the solution includes ESCAPED, check it. */
6376 if (pt->ipa_escaped
6377 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6378 return true;
6380 return false;
6383 bool
6384 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6386 bool res = pt_solution_includes_1 (pt, decl);
6387 if (res)
6388 ++pta_stats.pt_solution_includes_may_alias;
6389 else
6390 ++pta_stats.pt_solution_includes_no_alias;
6391 return res;
6394 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6395 intersection. */
6397 static bool
6398 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
6400 if (pt1->anything || pt2->anything)
6401 return true;
6403 /* If either points to unknown global memory and the other points to
6404 any global memory they alias. */
6405 if ((pt1->nonlocal
6406 && (pt2->nonlocal
6407 || pt2->vars_contains_nonlocal))
6408 || (pt2->nonlocal
6409 && pt1->vars_contains_nonlocal))
6410 return true;
6412 /* If either points to all escaped memory and the other points to
6413 any escaped memory they alias. */
6414 if ((pt1->escaped
6415 && (pt2->escaped
6416 || pt2->vars_contains_escaped))
6417 || (pt2->escaped
6418 && pt1->vars_contains_escaped))
6419 return true;
6421 /* Check the escaped solution if required.
6422 ??? Do we need to check the local against the IPA escaped sets? */
6423 if ((pt1->ipa_escaped || pt2->ipa_escaped)
6424 && !pt_solution_empty_p (&ipa_escaped_pt))
6426 /* If both point to escaped memory and that solution
6427 is not empty they alias. */
6428 if (pt1->ipa_escaped && pt2->ipa_escaped)
6429 return true;
6431 /* If either points to escaped memory see if the escaped solution
6432 intersects with the other. */
6433 if ((pt1->ipa_escaped
6434 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
6435 || (pt2->ipa_escaped
6436 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
6437 return true;
6440 /* Now both pointers alias if their points-to solution intersects. */
6441 return (pt1->vars
6442 && pt2->vars
6443 && bitmap_intersect_p (pt1->vars, pt2->vars));
6446 bool
6447 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
6449 bool res = pt_solutions_intersect_1 (pt1, pt2);
6450 if (res)
6451 ++pta_stats.pt_solutions_intersect_may_alias;
6452 else
6453 ++pta_stats.pt_solutions_intersect_no_alias;
6454 return res;
6458 /* Dump points-to information to OUTFILE. */
6460 static void
6461 dump_sa_points_to_info (FILE *outfile)
6463 unsigned int i;
6465 fprintf (outfile, "\nPoints-to sets\n\n");
6467 if (dump_flags & TDF_STATS)
6469 fprintf (outfile, "Stats:\n");
6470 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
6471 fprintf (outfile, "Non-pointer vars: %d\n",
6472 stats.nonpointer_vars);
6473 fprintf (outfile, "Statically unified vars: %d\n",
6474 stats.unified_vars_static);
6475 fprintf (outfile, "Dynamically unified vars: %d\n",
6476 stats.unified_vars_dynamic);
6477 fprintf (outfile, "Iterations: %d\n", stats.iterations);
6478 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
6479 fprintf (outfile, "Number of implicit edges: %d\n",
6480 stats.num_implicit_edges);
6483 for (i = 1; i < varmap.length (); i++)
6485 varinfo_t vi = get_varinfo (i);
6486 if (!vi->may_have_pointers)
6487 continue;
6488 dump_solution_for_var (outfile, i);
6493 /* Debug points-to information to stderr. */
6495 DEBUG_FUNCTION void
6496 debug_sa_points_to_info (void)
6498 dump_sa_points_to_info (stderr);
6502 /* Initialize the always-existing constraint variables for NULL
6503 ANYTHING, READONLY, and INTEGER */
6505 static void
6506 init_base_vars (void)
6508 struct constraint_expr lhs, rhs;
6509 varinfo_t var_anything;
6510 varinfo_t var_nothing;
6511 varinfo_t var_readonly;
6512 varinfo_t var_escaped;
6513 varinfo_t var_nonlocal;
6514 varinfo_t var_storedanything;
6515 varinfo_t var_integer;
6517 /* Variable ID zero is reserved and should be NULL. */
6518 varmap.safe_push (NULL);
6520 /* Create the NULL variable, used to represent that a variable points
6521 to NULL. */
6522 var_nothing = new_var_info (NULL_TREE, "NULL");
6523 gcc_assert (var_nothing->id == nothing_id);
6524 var_nothing->is_artificial_var = 1;
6525 var_nothing->offset = 0;
6526 var_nothing->size = ~0;
6527 var_nothing->fullsize = ~0;
6528 var_nothing->is_special_var = 1;
6529 var_nothing->may_have_pointers = 0;
6530 var_nothing->is_global_var = 0;
6532 /* Create the ANYTHING variable, used to represent that a variable
6533 points to some unknown piece of memory. */
6534 var_anything = new_var_info (NULL_TREE, "ANYTHING");
6535 gcc_assert (var_anything->id == anything_id);
6536 var_anything->is_artificial_var = 1;
6537 var_anything->size = ~0;
6538 var_anything->offset = 0;
6539 var_anything->fullsize = ~0;
6540 var_anything->is_special_var = 1;
6542 /* Anything points to anything. This makes deref constraints just
6543 work in the presence of linked list and other p = *p type loops,
6544 by saying that *ANYTHING = ANYTHING. */
6545 lhs.type = SCALAR;
6546 lhs.var = anything_id;
6547 lhs.offset = 0;
6548 rhs.type = ADDRESSOF;
6549 rhs.var = anything_id;
6550 rhs.offset = 0;
6552 /* This specifically does not use process_constraint because
6553 process_constraint ignores all anything = anything constraints, since all
6554 but this one are redundant. */
6555 constraints.safe_push (new_constraint (lhs, rhs));
6557 /* Create the READONLY variable, used to represent that a variable
6558 points to readonly memory. */
6559 var_readonly = new_var_info (NULL_TREE, "READONLY");
6560 gcc_assert (var_readonly->id == readonly_id);
6561 var_readonly->is_artificial_var = 1;
6562 var_readonly->offset = 0;
6563 var_readonly->size = ~0;
6564 var_readonly->fullsize = ~0;
6565 var_readonly->is_special_var = 1;
6567 /* readonly memory points to anything, in order to make deref
6568 easier. In reality, it points to anything the particular
6569 readonly variable can point to, but we don't track this
6570 separately. */
6571 lhs.type = SCALAR;
6572 lhs.var = readonly_id;
6573 lhs.offset = 0;
6574 rhs.type = ADDRESSOF;
6575 rhs.var = readonly_id; /* FIXME */
6576 rhs.offset = 0;
6577 process_constraint (new_constraint (lhs, rhs));
6579 /* Create the ESCAPED variable, used to represent the set of escaped
6580 memory. */
6581 var_escaped = new_var_info (NULL_TREE, "ESCAPED");
6582 gcc_assert (var_escaped->id == escaped_id);
6583 var_escaped->is_artificial_var = 1;
6584 var_escaped->offset = 0;
6585 var_escaped->size = ~0;
6586 var_escaped->fullsize = ~0;
6587 var_escaped->is_special_var = 0;
6589 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6590 memory. */
6591 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL");
6592 gcc_assert (var_nonlocal->id == nonlocal_id);
6593 var_nonlocal->is_artificial_var = 1;
6594 var_nonlocal->offset = 0;
6595 var_nonlocal->size = ~0;
6596 var_nonlocal->fullsize = ~0;
6597 var_nonlocal->is_special_var = 1;
6599 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6600 lhs.type = SCALAR;
6601 lhs.var = escaped_id;
6602 lhs.offset = 0;
6603 rhs.type = DEREF;
6604 rhs.var = escaped_id;
6605 rhs.offset = 0;
6606 process_constraint (new_constraint (lhs, rhs));
6608 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6609 whole variable escapes. */
6610 lhs.type = SCALAR;
6611 lhs.var = escaped_id;
6612 lhs.offset = 0;
6613 rhs.type = SCALAR;
6614 rhs.var = escaped_id;
6615 rhs.offset = UNKNOWN_OFFSET;
6616 process_constraint (new_constraint (lhs, rhs));
6618 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6619 everything pointed to by escaped points to what global memory can
6620 point to. */
6621 lhs.type = DEREF;
6622 lhs.var = escaped_id;
6623 lhs.offset = 0;
6624 rhs.type = SCALAR;
6625 rhs.var = nonlocal_id;
6626 rhs.offset = 0;
6627 process_constraint (new_constraint (lhs, rhs));
6629 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6630 global memory may point to global memory and escaped memory. */
6631 lhs.type = SCALAR;
6632 lhs.var = nonlocal_id;
6633 lhs.offset = 0;
6634 rhs.type = ADDRESSOF;
6635 rhs.var = nonlocal_id;
6636 rhs.offset = 0;
6637 process_constraint (new_constraint (lhs, rhs));
6638 rhs.type = ADDRESSOF;
6639 rhs.var = escaped_id;
6640 rhs.offset = 0;
6641 process_constraint (new_constraint (lhs, rhs));
6643 /* Create the STOREDANYTHING variable, used to represent the set of
6644 variables stored to *ANYTHING. */
6645 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING");
6646 gcc_assert (var_storedanything->id == storedanything_id);
6647 var_storedanything->is_artificial_var = 1;
6648 var_storedanything->offset = 0;
6649 var_storedanything->size = ~0;
6650 var_storedanything->fullsize = ~0;
6651 var_storedanything->is_special_var = 0;
6653 /* Create the INTEGER variable, used to represent that a variable points
6654 to what an INTEGER "points to". */
6655 var_integer = new_var_info (NULL_TREE, "INTEGER");
6656 gcc_assert (var_integer->id == integer_id);
6657 var_integer->is_artificial_var = 1;
6658 var_integer->size = ~0;
6659 var_integer->fullsize = ~0;
6660 var_integer->offset = 0;
6661 var_integer->is_special_var = 1;
6663 /* INTEGER = ANYTHING, because we don't know where a dereference of
6664 a random integer will point to. */
6665 lhs.type = SCALAR;
6666 lhs.var = integer_id;
6667 lhs.offset = 0;
6668 rhs.type = ADDRESSOF;
6669 rhs.var = anything_id;
6670 rhs.offset = 0;
6671 process_constraint (new_constraint (lhs, rhs));
6674 /* Initialize things necessary to perform PTA */
6676 static void
6677 init_alias_vars (void)
6679 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
6681 bitmap_obstack_initialize (&pta_obstack);
6682 bitmap_obstack_initialize (&oldpta_obstack);
6683 bitmap_obstack_initialize (&predbitmap_obstack);
6685 constraint_pool = create_alloc_pool ("Constraint pool",
6686 sizeof (struct constraint), 30);
6687 variable_info_pool = create_alloc_pool ("Variable info pool",
6688 sizeof (struct variable_info), 30);
6689 constraints.create (8);
6690 varmap.create (8);
6691 vi_for_tree = pointer_map_create ();
6692 call_stmt_vars = pointer_map_create ();
6694 memset (&stats, 0, sizeof (stats));
6695 shared_bitmap_table.create (511);
6696 init_base_vars ();
6698 gcc_obstack_init (&fake_var_decl_obstack);
6700 final_solutions = pointer_map_create ();
6701 gcc_obstack_init (&final_solutions_obstack);
6704 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6705 predecessor edges. */
6707 static void
6708 remove_preds_and_fake_succs (constraint_graph_t graph)
6710 unsigned int i;
6712 /* Clear the implicit ref and address nodes from the successor
6713 lists. */
6714 for (i = 1; i < FIRST_REF_NODE; i++)
6716 if (graph->succs[i])
6717 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
6718 FIRST_REF_NODE * 2);
6721 /* Free the successor list for the non-ref nodes. */
6722 for (i = FIRST_REF_NODE + 1; i < graph->size; i++)
6724 if (graph->succs[i])
6725 BITMAP_FREE (graph->succs[i]);
6728 /* Now reallocate the size of the successor list as, and blow away
6729 the predecessor bitmaps. */
6730 graph->size = varmap.length ();
6731 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
6733 free (graph->implicit_preds);
6734 graph->implicit_preds = NULL;
6735 free (graph->preds);
6736 graph->preds = NULL;
6737 bitmap_obstack_release (&predbitmap_obstack);
6740 /* Solve the constraint set. */
6742 static void
6743 solve_constraints (void)
6745 struct scc_info *si;
6747 if (dump_file)
6748 fprintf (dump_file,
6749 "\nCollapsing static cycles and doing variable "
6750 "substitution\n");
6752 init_graph (varmap.length () * 2);
6754 if (dump_file)
6755 fprintf (dump_file, "Building predecessor graph\n");
6756 build_pred_graph ();
6758 if (dump_file)
6759 fprintf (dump_file, "Detecting pointer and location "
6760 "equivalences\n");
6761 si = perform_var_substitution (graph);
6763 if (dump_file)
6764 fprintf (dump_file, "Rewriting constraints and unifying "
6765 "variables\n");
6766 rewrite_constraints (graph, si);
6768 build_succ_graph ();
6770 free_var_substitution_info (si);
6772 /* Attach complex constraints to graph nodes. */
6773 move_complex_constraints (graph);
6775 if (dump_file)
6776 fprintf (dump_file, "Uniting pointer but not location equivalent "
6777 "variables\n");
6778 unite_pointer_equivalences (graph);
6780 if (dump_file)
6781 fprintf (dump_file, "Finding indirect cycles\n");
6782 find_indirect_cycles (graph);
6784 /* Implicit nodes and predecessors are no longer necessary at this
6785 point. */
6786 remove_preds_and_fake_succs (graph);
6788 if (dump_file && (dump_flags & TDF_GRAPH))
6790 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
6791 "in dot format:\n");
6792 dump_constraint_graph (dump_file);
6793 fprintf (dump_file, "\n\n");
6796 if (dump_file)
6797 fprintf (dump_file, "Solving graph\n");
6799 solve_graph (graph);
6801 if (dump_file && (dump_flags & TDF_GRAPH))
6803 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
6804 "in dot format:\n");
6805 dump_constraint_graph (dump_file);
6806 fprintf (dump_file, "\n\n");
6809 if (dump_file)
6810 dump_sa_points_to_info (dump_file);
6813 /* Create points-to sets for the current function. See the comments
6814 at the start of the file for an algorithmic overview. */
6816 static void
6817 compute_points_to_sets (void)
6819 basic_block bb;
6820 unsigned i;
6821 varinfo_t vi;
6823 timevar_push (TV_TREE_PTA);
6825 init_alias_vars ();
6827 intra_create_variable_infos ();
6829 /* Now walk all statements and build the constraint set. */
6830 FOR_EACH_BB_FN (bb, cfun)
6832 gimple_stmt_iterator gsi;
6834 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6836 gimple phi = gsi_stmt (gsi);
6838 if (! virtual_operand_p (gimple_phi_result (phi)))
6839 find_func_aliases (phi);
6842 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6844 gimple stmt = gsi_stmt (gsi);
6846 find_func_aliases (stmt);
6850 if (dump_file)
6852 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
6853 dump_constraints (dump_file, 0);
6856 /* From the constraints compute the points-to sets. */
6857 solve_constraints ();
6859 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6860 cfun->gimple_df->escaped = find_what_var_points_to (get_varinfo (escaped_id));
6862 /* Make sure the ESCAPED solution (which is used as placeholder in
6863 other solutions) does not reference itself. This simplifies
6864 points-to solution queries. */
6865 cfun->gimple_df->escaped.escaped = 0;
6867 /* Compute the points-to sets for pointer SSA_NAMEs. */
6868 for (i = 0; i < num_ssa_names; ++i)
6870 tree ptr = ssa_name (i);
6871 if (ptr
6872 && POINTER_TYPE_P (TREE_TYPE (ptr)))
6873 find_what_p_points_to (ptr);
6876 /* Compute the call-used/clobbered sets. */
6877 FOR_EACH_BB_FN (bb, cfun)
6879 gimple_stmt_iterator gsi;
6881 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6883 gimple stmt = gsi_stmt (gsi);
6884 struct pt_solution *pt;
6885 if (!is_gimple_call (stmt))
6886 continue;
6888 pt = gimple_call_use_set (stmt);
6889 if (gimple_call_flags (stmt) & ECF_CONST)
6890 memset (pt, 0, sizeof (struct pt_solution));
6891 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
6893 *pt = find_what_var_points_to (vi);
6894 /* Escaped (and thus nonlocal) variables are always
6895 implicitly used by calls. */
6896 /* ??? ESCAPED can be empty even though NONLOCAL
6897 always escaped. */
6898 pt->nonlocal = 1;
6899 pt->escaped = 1;
6901 else
6903 /* If there is nothing special about this call then
6904 we have made everything that is used also escape. */
6905 *pt = cfun->gimple_df->escaped;
6906 pt->nonlocal = 1;
6909 pt = gimple_call_clobber_set (stmt);
6910 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
6911 memset (pt, 0, sizeof (struct pt_solution));
6912 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
6914 *pt = find_what_var_points_to (vi);
6915 /* Escaped (and thus nonlocal) variables are always
6916 implicitly clobbered by calls. */
6917 /* ??? ESCAPED can be empty even though NONLOCAL
6918 always escaped. */
6919 pt->nonlocal = 1;
6920 pt->escaped = 1;
6922 else
6924 /* If there is nothing special about this call then
6925 we have made everything that is used also escape. */
6926 *pt = cfun->gimple_df->escaped;
6927 pt->nonlocal = 1;
6932 timevar_pop (TV_TREE_PTA);
6936 /* Delete created points-to sets. */
6938 static void
6939 delete_points_to_sets (void)
6941 unsigned int i;
6943 shared_bitmap_table.dispose ();
6944 if (dump_file && (dump_flags & TDF_STATS))
6945 fprintf (dump_file, "Points to sets created:%d\n",
6946 stats.points_to_sets_created);
6948 pointer_map_destroy (vi_for_tree);
6949 pointer_map_destroy (call_stmt_vars);
6950 bitmap_obstack_release (&pta_obstack);
6951 constraints.release ();
6953 for (i = 0; i < graph->size; i++)
6954 graph->complex[i].release ();
6955 free (graph->complex);
6957 free (graph->rep);
6958 free (graph->succs);
6959 free (graph->pe);
6960 free (graph->pe_rep);
6961 free (graph->indirect_cycles);
6962 free (graph);
6964 varmap.release ();
6965 free_alloc_pool (variable_info_pool);
6966 free_alloc_pool (constraint_pool);
6968 obstack_free (&fake_var_decl_obstack, NULL);
6970 pointer_map_destroy (final_solutions);
6971 obstack_free (&final_solutions_obstack, NULL);
6974 /* Mark "other" loads and stores as belonging to CLIQUE and with
6975 base zero. */
6977 static bool
6978 visit_loadstore (gimple, tree base, tree ref, void *clique_)
6980 unsigned short clique = (uintptr_t)clique_;
6981 if (TREE_CODE (base) == MEM_REF
6982 || TREE_CODE (base) == TARGET_MEM_REF)
6984 tree ptr = TREE_OPERAND (base, 0);
6985 if (TREE_CODE (ptr) == SSA_NAME)
6987 /* ??? We need to make sure 'ptr' doesn't include any of
6988 the restrict tags in its points-to set. */
6989 return false;
6992 /* For now let decls through. */
6994 /* Do not overwrite existing cliques (that includes clique, base
6995 pairs we just set). */
6996 if (MR_DEPENDENCE_CLIQUE (base) == 0)
6998 MR_DEPENDENCE_CLIQUE (base) = clique;
6999 MR_DEPENDENCE_BASE (base) = 0;
7003 /* For plain decl accesses see whether they are accesses to globals
7004 and rewrite them to MEM_REFs with { clique, 0 }. */
7005 if (TREE_CODE (base) == VAR_DECL
7006 && is_global_var (base)
7007 /* ??? We can't rewrite a plain decl with the walk_stmt_load_store
7008 ops callback. */
7009 && base != ref)
7011 tree *basep = &ref;
7012 while (handled_component_p (*basep))
7013 basep = &TREE_OPERAND (*basep, 0);
7014 gcc_assert (TREE_CODE (*basep) == VAR_DECL);
7015 tree ptr = build_fold_addr_expr (*basep);
7016 tree zero = build_int_cst (TREE_TYPE (ptr), 0);
7017 *basep = build2 (MEM_REF, TREE_TYPE (*basep), ptr, zero);
7018 MR_DEPENDENCE_CLIQUE (*basep) = clique;
7019 MR_DEPENDENCE_BASE (*basep) = 0;
7022 return false;
7025 /* If REF is a MEM_REF then assign a clique, base pair to it, updating
7026 CLIQUE, *RESTRICT_VAR and LAST_RUID. Return whether dependence info
7027 was assigned to REF. */
7029 static bool
7030 maybe_set_dependence_info (tree ref, tree ptr,
7031 unsigned short &clique, varinfo_t restrict_var,
7032 unsigned short &last_ruid)
7034 while (handled_component_p (ref))
7035 ref = TREE_OPERAND (ref, 0);
7036 if ((TREE_CODE (ref) == MEM_REF
7037 || TREE_CODE (ref) == TARGET_MEM_REF)
7038 && TREE_OPERAND (ref, 0) == ptr)
7040 /* Do not overwrite existing cliques. This avoids overwriting dependence
7041 info inlined from a function with restrict parameters inlined
7042 into a function with restrict parameters. This usually means we
7043 prefer to be precise in innermost loops. */
7044 if (MR_DEPENDENCE_CLIQUE (ref) == 0)
7046 if (clique == 0)
7047 clique = ++cfun->last_clique;
7048 if (restrict_var->ruid == 0)
7049 restrict_var->ruid = ++last_ruid;
7050 MR_DEPENDENCE_CLIQUE (ref) = clique;
7051 MR_DEPENDENCE_BASE (ref) = restrict_var->ruid;
7052 return true;
7055 return false;
7058 /* Compute the set of independend memory references based on restrict
7059 tags and their conservative propagation to the points-to sets. */
7061 static void
7062 compute_dependence_clique (void)
7064 unsigned short clique = 0;
7065 unsigned short last_ruid = 0;
7066 for (unsigned i = 0; i < num_ssa_names; ++i)
7068 tree ptr = ssa_name (i);
7069 if (!ptr || !POINTER_TYPE_P (TREE_TYPE (ptr)))
7070 continue;
7072 /* Avoid all this when ptr is not dereferenced? */
7073 tree p = ptr;
7074 if (SSA_NAME_IS_DEFAULT_DEF (ptr)
7075 && (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
7076 || TREE_CODE (SSA_NAME_VAR (ptr)) == RESULT_DECL))
7077 p = SSA_NAME_VAR (ptr);
7078 varinfo_t vi = lookup_vi_for_tree (p);
7079 if (!vi)
7080 continue;
7081 vi = get_varinfo (find (vi->id));
7082 bitmap_iterator bi;
7083 unsigned j;
7084 varinfo_t restrict_var = NULL;
7085 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, j, bi)
7087 varinfo_t oi = get_varinfo (j);
7088 if (oi->is_restrict_var)
7090 if (restrict_var)
7092 if (dump_file && (dump_flags & TDF_DETAILS))
7094 fprintf (dump_file, "found restrict pointed-to "
7095 "for ");
7096 print_generic_expr (dump_file, ptr, 0);
7097 fprintf (dump_file, " but not exclusively\n");
7099 restrict_var = NULL;
7100 break;
7102 restrict_var = oi;
7104 /* NULL is the only other valid points-to entry. */
7105 else if (oi->id != nothing_id)
7107 restrict_var = NULL;
7108 break;
7111 /* Ok, found that ptr must(!) point to a single(!) restrict
7112 variable. */
7113 /* ??? PTA isn't really a proper propagation engine to compute
7114 this property.
7115 ??? We could handle merging of two restricts by unifying them. */
7116 if (restrict_var)
7118 /* Now look at possible dereferences of ptr. */
7119 imm_use_iterator ui;
7120 gimple use_stmt;
7121 FOR_EACH_IMM_USE_STMT (use_stmt, ui, ptr)
7123 /* ??? Calls and asms. */
7124 if (!gimple_assign_single_p (use_stmt))
7125 continue;
7126 maybe_set_dependence_info (gimple_assign_lhs (use_stmt), ptr,
7127 clique, restrict_var, last_ruid);
7128 maybe_set_dependence_info (gimple_assign_rhs1 (use_stmt), ptr,
7129 clique, restrict_var, last_ruid);
7134 if (clique == 0)
7135 return;
7137 /* Assign the BASE id zero to all accesses not based on a restrict
7138 pointer. That way they get disabiguated against restrict
7139 accesses but not against each other. */
7140 /* ??? For restricts derived from globals (thus not incoming
7141 parameters) we can't restrict scoping properly thus the following
7142 is too aggressive there. For now we have excluded those globals from
7143 getting into the MR_DEPENDENCE machinery. */
7144 basic_block bb;
7145 FOR_EACH_BB_FN (bb, cfun)
7146 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
7147 !gsi_end_p (gsi); gsi_next (&gsi))
7149 gimple stmt = gsi_stmt (gsi);
7150 walk_stmt_load_store_ops (stmt, (void *)(uintptr_t)clique,
7151 visit_loadstore, visit_loadstore);
7155 /* Compute points-to information for every SSA_NAME pointer in the
7156 current function and compute the transitive closure of escaped
7157 variables to re-initialize the call-clobber states of local variables. */
7159 unsigned int
7160 compute_may_aliases (void)
7162 if (cfun->gimple_df->ipa_pta)
7164 if (dump_file)
7166 fprintf (dump_file, "\nNot re-computing points-to information "
7167 "because IPA points-to information is available.\n\n");
7169 /* But still dump what we have remaining it. */
7170 dump_alias_info (dump_file);
7173 return 0;
7176 /* For each pointer P_i, determine the sets of variables that P_i may
7177 point-to. Compute the reachability set of escaped and call-used
7178 variables. */
7179 compute_points_to_sets ();
7181 /* Debugging dumps. */
7182 if (dump_file)
7183 dump_alias_info (dump_file);
7185 /* Compute restrict-based memory disambiguations. */
7186 compute_dependence_clique ();
7188 /* Deallocate memory used by aliasing data structures and the internal
7189 points-to solution. */
7190 delete_points_to_sets ();
7192 gcc_assert (!need_ssa_update_p (cfun));
7194 return 0;
7197 static bool
7198 gate_tree_pta (void)
7200 return flag_tree_pta;
7203 /* A dummy pass to cause points-to information to be computed via
7204 TODO_rebuild_alias. */
7206 namespace {
7208 const pass_data pass_data_build_alias =
7210 GIMPLE_PASS, /* type */
7211 "alias", /* name */
7212 OPTGROUP_NONE, /* optinfo_flags */
7213 true, /* has_gate */
7214 false, /* has_execute */
7215 TV_NONE, /* tv_id */
7216 ( PROP_cfg | PROP_ssa ), /* properties_required */
7217 0, /* properties_provided */
7218 0, /* properties_destroyed */
7219 0, /* todo_flags_start */
7220 TODO_rebuild_alias, /* todo_flags_finish */
7223 class pass_build_alias : public gimple_opt_pass
7225 public:
7226 pass_build_alias (gcc::context *ctxt)
7227 : gimple_opt_pass (pass_data_build_alias, ctxt)
7230 /* opt_pass methods: */
7231 bool gate () { return gate_tree_pta (); }
7233 }; // class pass_build_alias
7235 } // anon namespace
7237 gimple_opt_pass *
7238 make_pass_build_alias (gcc::context *ctxt)
7240 return new pass_build_alias (ctxt);
7243 /* A dummy pass to cause points-to information to be computed via
7244 TODO_rebuild_alias. */
7246 namespace {
7248 const pass_data pass_data_build_ealias =
7250 GIMPLE_PASS, /* type */
7251 "ealias", /* name */
7252 OPTGROUP_NONE, /* optinfo_flags */
7253 true, /* has_gate */
7254 false, /* has_execute */
7255 TV_NONE, /* tv_id */
7256 ( PROP_cfg | PROP_ssa ), /* properties_required */
7257 0, /* properties_provided */
7258 0, /* properties_destroyed */
7259 0, /* todo_flags_start */
7260 TODO_rebuild_alias, /* todo_flags_finish */
7263 class pass_build_ealias : public gimple_opt_pass
7265 public:
7266 pass_build_ealias (gcc::context *ctxt)
7267 : gimple_opt_pass (pass_data_build_ealias, ctxt)
7270 /* opt_pass methods: */
7271 bool gate () { return gate_tree_pta (); }
7273 }; // class pass_build_ealias
7275 } // anon namespace
7277 gimple_opt_pass *
7278 make_pass_build_ealias (gcc::context *ctxt)
7280 return new pass_build_ealias (ctxt);
7284 /* Return true if we should execute IPA PTA. */
7285 static bool
7286 gate_ipa_pta (void)
7288 return (optimize
7289 && flag_ipa_pta
7290 /* Don't bother doing anything if the program has errors. */
7291 && !seen_error ());
7294 /* IPA PTA solutions for ESCAPED. */
7295 struct pt_solution ipa_escaped_pt
7296 = { true, false, false, false, false, false, false, false, NULL };
7298 /* Associate node with varinfo DATA. Worker for
7299 cgraph_for_node_and_aliases. */
7300 static bool
7301 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
7303 if ((node->alias || node->thunk.thunk_p)
7304 && node->analyzed)
7305 insert_vi_for_tree (node->decl, (varinfo_t)data);
7306 return false;
7309 /* Execute the driver for IPA PTA. */
7310 static unsigned int
7311 ipa_pta_execute (void)
7313 struct cgraph_node *node;
7314 varpool_node *var;
7315 int from;
7317 in_ipa_mode = 1;
7319 init_alias_vars ();
7321 if (dump_file && (dump_flags & TDF_DETAILS))
7323 dump_symtab (dump_file);
7324 fprintf (dump_file, "\n");
7327 /* Build the constraints. */
7328 FOR_EACH_DEFINED_FUNCTION (node)
7330 varinfo_t vi;
7331 /* Nodes without a body are not interesting. Especially do not
7332 visit clones at this point for now - we get duplicate decls
7333 there for inline clones at least. */
7334 if (!cgraph_function_with_gimple_body_p (node) || node->clone_of)
7335 continue;
7336 cgraph_get_body (node);
7338 gcc_assert (!node->clone_of);
7340 vi = create_function_info_for (node->decl,
7341 alias_get_name (node->decl));
7342 cgraph_for_node_and_aliases (node, associate_varinfo_to_alias, vi, true);
7345 /* Create constraints for global variables and their initializers. */
7346 FOR_EACH_VARIABLE (var)
7348 if (var->alias && var->analyzed)
7349 continue;
7351 get_vi_for_tree (var->decl);
7354 if (dump_file)
7356 fprintf (dump_file,
7357 "Generating constraints for global initializers\n\n");
7358 dump_constraints (dump_file, 0);
7359 fprintf (dump_file, "\n");
7361 from = constraints.length ();
7363 FOR_EACH_DEFINED_FUNCTION (node)
7365 struct function *func;
7366 basic_block bb;
7368 /* Nodes without a body are not interesting. */
7369 if (!cgraph_function_with_gimple_body_p (node) || node->clone_of)
7370 continue;
7372 if (dump_file)
7374 fprintf (dump_file,
7375 "Generating constraints for %s", node->name ());
7376 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
7377 fprintf (dump_file, " (%s)",
7378 IDENTIFIER_POINTER
7379 (DECL_ASSEMBLER_NAME (node->decl)));
7380 fprintf (dump_file, "\n");
7383 func = DECL_STRUCT_FUNCTION (node->decl);
7384 push_cfun (func);
7386 /* For externally visible or attribute used annotated functions use
7387 local constraints for their arguments.
7388 For local functions we see all callers and thus do not need initial
7389 constraints for parameters. */
7390 if (node->used_from_other_partition
7391 || node->externally_visible
7392 || node->force_output)
7394 intra_create_variable_infos ();
7396 /* We also need to make function return values escape. Nothing
7397 escapes by returning from main though. */
7398 if (!MAIN_NAME_P (DECL_NAME (node->decl)))
7400 varinfo_t fi, rvi;
7401 fi = lookup_vi_for_tree (node->decl);
7402 rvi = first_vi_for_offset (fi, fi_result);
7403 if (rvi && rvi->offset == fi_result)
7405 struct constraint_expr includes;
7406 struct constraint_expr var;
7407 includes.var = escaped_id;
7408 includes.offset = 0;
7409 includes.type = SCALAR;
7410 var.var = rvi->id;
7411 var.offset = 0;
7412 var.type = SCALAR;
7413 process_constraint (new_constraint (includes, var));
7418 /* Build constriants for the function body. */
7419 FOR_EACH_BB_FN (bb, func)
7421 gimple_stmt_iterator gsi;
7423 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7424 gsi_next (&gsi))
7426 gimple phi = gsi_stmt (gsi);
7428 if (! virtual_operand_p (gimple_phi_result (phi)))
7429 find_func_aliases (phi);
7432 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7434 gimple stmt = gsi_stmt (gsi);
7436 find_func_aliases (stmt);
7437 find_func_clobbers (stmt);
7441 pop_cfun ();
7443 if (dump_file)
7445 fprintf (dump_file, "\n");
7446 dump_constraints (dump_file, from);
7447 fprintf (dump_file, "\n");
7449 from = constraints.length ();
7452 /* From the constraints compute the points-to sets. */
7453 solve_constraints ();
7455 /* Compute the global points-to sets for ESCAPED.
7456 ??? Note that the computed escape set is not correct
7457 for the whole unit as we fail to consider graph edges to
7458 externally visible functions. */
7459 ipa_escaped_pt = find_what_var_points_to (get_varinfo (escaped_id));
7461 /* Make sure the ESCAPED solution (which is used as placeholder in
7462 other solutions) does not reference itself. This simplifies
7463 points-to solution queries. */
7464 ipa_escaped_pt.ipa_escaped = 0;
7466 /* Assign the points-to sets to the SSA names in the unit. */
7467 FOR_EACH_DEFINED_FUNCTION (node)
7469 tree ptr;
7470 struct function *fn;
7471 unsigned i;
7472 basic_block bb;
7474 /* Nodes without a body are not interesting. */
7475 if (!cgraph_function_with_gimple_body_p (node) || node->clone_of)
7476 continue;
7478 fn = DECL_STRUCT_FUNCTION (node->decl);
7480 /* Compute the points-to sets for pointer SSA_NAMEs. */
7481 FOR_EACH_VEC_ELT (*fn->gimple_df->ssa_names, i, ptr)
7483 if (ptr
7484 && POINTER_TYPE_P (TREE_TYPE (ptr)))
7485 find_what_p_points_to (ptr);
7488 /* Compute the call-use and call-clobber sets for indirect calls
7489 and calls to external functions. */
7490 FOR_EACH_BB_FN (bb, fn)
7492 gimple_stmt_iterator gsi;
7494 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7496 gimple stmt = gsi_stmt (gsi);
7497 struct pt_solution *pt;
7498 varinfo_t vi, fi;
7499 tree decl;
7501 if (!is_gimple_call (stmt))
7502 continue;
7504 /* Handle direct calls to functions with body. */
7505 decl = gimple_call_fndecl (stmt);
7506 if (decl
7507 && (fi = lookup_vi_for_tree (decl))
7508 && fi->is_fn_info)
7510 *gimple_call_clobber_set (stmt)
7511 = find_what_var_points_to
7512 (first_vi_for_offset (fi, fi_clobbers));
7513 *gimple_call_use_set (stmt)
7514 = find_what_var_points_to
7515 (first_vi_for_offset (fi, fi_uses));
7517 /* Handle direct calls to external functions. */
7518 else if (decl)
7520 pt = gimple_call_use_set (stmt);
7521 if (gimple_call_flags (stmt) & ECF_CONST)
7522 memset (pt, 0, sizeof (struct pt_solution));
7523 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7525 *pt = find_what_var_points_to (vi);
7526 /* Escaped (and thus nonlocal) variables are always
7527 implicitly used by calls. */
7528 /* ??? ESCAPED can be empty even though NONLOCAL
7529 always escaped. */
7530 pt->nonlocal = 1;
7531 pt->ipa_escaped = 1;
7533 else
7535 /* If there is nothing special about this call then
7536 we have made everything that is used also escape. */
7537 *pt = ipa_escaped_pt;
7538 pt->nonlocal = 1;
7541 pt = gimple_call_clobber_set (stmt);
7542 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7543 memset (pt, 0, sizeof (struct pt_solution));
7544 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7546 *pt = find_what_var_points_to (vi);
7547 /* Escaped (and thus nonlocal) variables are always
7548 implicitly clobbered by calls. */
7549 /* ??? ESCAPED can be empty even though NONLOCAL
7550 always escaped. */
7551 pt->nonlocal = 1;
7552 pt->ipa_escaped = 1;
7554 else
7556 /* If there is nothing special about this call then
7557 we have made everything that is used also escape. */
7558 *pt = ipa_escaped_pt;
7559 pt->nonlocal = 1;
7562 /* Handle indirect calls. */
7563 else if (!decl
7564 && (fi = get_fi_for_callee (stmt)))
7566 /* We need to accumulate all clobbers/uses of all possible
7567 callees. */
7568 fi = get_varinfo (find (fi->id));
7569 /* If we cannot constrain the set of functions we'll end up
7570 calling we end up using/clobbering everything. */
7571 if (bitmap_bit_p (fi->solution, anything_id)
7572 || bitmap_bit_p (fi->solution, nonlocal_id)
7573 || bitmap_bit_p (fi->solution, escaped_id))
7575 pt_solution_reset (gimple_call_clobber_set (stmt));
7576 pt_solution_reset (gimple_call_use_set (stmt));
7578 else
7580 bitmap_iterator bi;
7581 unsigned i;
7582 struct pt_solution *uses, *clobbers;
7584 uses = gimple_call_use_set (stmt);
7585 clobbers = gimple_call_clobber_set (stmt);
7586 memset (uses, 0, sizeof (struct pt_solution));
7587 memset (clobbers, 0, sizeof (struct pt_solution));
7588 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
7590 struct pt_solution sol;
7592 vi = get_varinfo (i);
7593 if (!vi->is_fn_info)
7595 /* ??? We could be more precise here? */
7596 uses->nonlocal = 1;
7597 uses->ipa_escaped = 1;
7598 clobbers->nonlocal = 1;
7599 clobbers->ipa_escaped = 1;
7600 continue;
7603 if (!uses->anything)
7605 sol = find_what_var_points_to
7606 (first_vi_for_offset (vi, fi_uses));
7607 pt_solution_ior_into (uses, &sol);
7609 if (!clobbers->anything)
7611 sol = find_what_var_points_to
7612 (first_vi_for_offset (vi, fi_clobbers));
7613 pt_solution_ior_into (clobbers, &sol);
7621 fn->gimple_df->ipa_pta = true;
7624 delete_points_to_sets ();
7626 in_ipa_mode = 0;
7628 return 0;
7631 namespace {
7633 const pass_data pass_data_ipa_pta =
7635 SIMPLE_IPA_PASS, /* type */
7636 "pta", /* name */
7637 OPTGROUP_NONE, /* optinfo_flags */
7638 true, /* has_gate */
7639 true, /* has_execute */
7640 TV_IPA_PTA, /* tv_id */
7641 0, /* properties_required */
7642 0, /* properties_provided */
7643 0, /* properties_destroyed */
7644 0, /* todo_flags_start */
7645 0, /* todo_flags_finish */
7648 class pass_ipa_pta : public simple_ipa_opt_pass
7650 public:
7651 pass_ipa_pta (gcc::context *ctxt)
7652 : simple_ipa_opt_pass (pass_data_ipa_pta, ctxt)
7655 /* opt_pass methods: */
7656 bool gate () { return gate_ipa_pta (); }
7657 unsigned int execute () { return ipa_pta_execute (); }
7659 }; // class pass_ipa_pta
7661 } // anon namespace
7663 simple_ipa_opt_pass *
7664 make_pass_ipa_pta (gcc::context *ctxt)
7666 return new pass_ipa_pta (ctxt);