Merge from trunk:
[official-gcc.git] / main / gcc / tree-ssa-structalias.c
blob521d77863760837f7802f6eccfe46a38323a365b
1 /* Tree based points-to analysis
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dberlin@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "obstack.h"
26 #include "bitmap.h"
27 #include "sbitmap.h"
28 #include "flags.h"
29 #include "basic-block.h"
30 #include "tree.h"
31 #include "stor-layout.h"
32 #include "stmt.h"
33 #include "pointer-set.h"
34 #include "hash-table.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-expr.h"
38 #include "is-a.h"
39 #include "gimple.h"
40 #include "gimple-iterator.h"
41 #include "gimple-ssa.h"
42 #include "cgraph.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
45 #include "tree-into-ssa.h"
46 #include "expr.h"
47 #include "tree-dfa.h"
48 #include "tree-inline.h"
49 #include "diagnostic-core.h"
50 #include "function.h"
51 #include "tree-pass.h"
52 #include "alloc-pool.h"
53 #include "splay-tree.h"
54 #include "params.h"
55 #include "alias.h"
57 /* The idea behind this analyzer is to generate set constraints from the
58 program, then solve the resulting constraints in order to generate the
59 points-to sets.
61 Set constraints are a way of modeling program analysis problems that
62 involve sets. They consist of an inclusion constraint language,
63 describing the variables (each variable is a set) and operations that
64 are involved on the variables, and a set of rules that derive facts
65 from these operations. To solve a system of set constraints, you derive
66 all possible facts under the rules, which gives you the correct sets
67 as a consequence.
69 See "Efficient Field-sensitive pointer analysis for C" by "David
70 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
71 http://citeseer.ist.psu.edu/pearce04efficient.html
73 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
74 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
75 http://citeseer.ist.psu.edu/heintze01ultrafast.html
77 There are three types of real constraint expressions, DEREF,
78 ADDRESSOF, and SCALAR. Each constraint expression consists
79 of a constraint type, a variable, and an offset.
81 SCALAR is a constraint expression type used to represent x, whether
82 it appears on the LHS or the RHS of a statement.
83 DEREF is a constraint expression type used to represent *x, whether
84 it appears on the LHS or the RHS of a statement.
85 ADDRESSOF is a constraint expression used to represent &x, whether
86 it appears on the LHS or the RHS of a statement.
88 Each pointer variable in the program is assigned an integer id, and
89 each field of a structure variable is assigned an integer id as well.
91 Structure variables are linked to their list of fields through a "next
92 field" in each variable that points to the next field in offset
93 order.
94 Each variable for a structure field has
96 1. "size", that tells the size in bits of that field.
97 2. "fullsize, that tells the size in bits of the entire structure.
98 3. "offset", that tells the offset in bits from the beginning of the
99 structure to this field.
101 Thus,
102 struct f
104 int a;
105 int b;
106 } foo;
107 int *bar;
109 looks like
111 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
112 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
113 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
116 In order to solve the system of set constraints, the following is
117 done:
119 1. Each constraint variable x has a solution set associated with it,
120 Sol(x).
122 2. Constraints are separated into direct, copy, and complex.
123 Direct constraints are ADDRESSOF constraints that require no extra
124 processing, such as P = &Q
125 Copy constraints are those of the form P = Q.
126 Complex constraints are all the constraints involving dereferences
127 and offsets (including offsetted copies).
129 3. All direct constraints of the form P = &Q are processed, such
130 that Q is added to Sol(P)
132 4. All complex constraints for a given constraint variable are stored in a
133 linked list attached to that variable's node.
135 5. A directed graph is built out of the copy constraints. Each
136 constraint variable is a node in the graph, and an edge from
137 Q to P is added for each copy constraint of the form P = Q
139 6. The graph is then walked, and solution sets are
140 propagated along the copy edges, such that an edge from Q to P
141 causes Sol(P) <- Sol(P) union Sol(Q).
143 7. As we visit each node, all complex constraints associated with
144 that node are processed by adding appropriate copy edges to the graph, or the
145 appropriate variables to the solution set.
147 8. The process of walking the graph is iterated until no solution
148 sets change.
150 Prior to walking the graph in steps 6 and 7, We perform static
151 cycle elimination on the constraint graph, as well
152 as off-line variable substitution.
154 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
155 on and turned into anything), but isn't. You can just see what offset
156 inside the pointed-to struct it's going to access.
158 TODO: Constant bounded arrays can be handled as if they were structs of the
159 same number of elements.
161 TODO: Modeling heap and incoming pointers becomes much better if we
162 add fields to them as we discover them, which we could do.
164 TODO: We could handle unions, but to be honest, it's probably not
165 worth the pain or slowdown. */
167 /* IPA-PTA optimizations possible.
169 When the indirect function called is ANYTHING we can add disambiguation
170 based on the function signatures (or simply the parameter count which
171 is the varinfo size). We also do not need to consider functions that
172 do not have their address taken.
174 The is_global_var bit which marks escape points is overly conservative
175 in IPA mode. Split it to is_escape_point and is_global_var - only
176 externally visible globals are escape points in IPA mode. This is
177 also needed to fix the pt_solution_includes_global predicate
178 (and thus ptr_deref_may_alias_global_p).
180 The way we introduce DECL_PT_UID to avoid fixing up all points-to
181 sets in the translation unit when we copy a DECL during inlining
182 pessimizes precision. The advantage is that the DECL_PT_UID keeps
183 compile-time and memory usage overhead low - the points-to sets
184 do not grow or get unshared as they would during a fixup phase.
185 An alternative solution is to delay IPA PTA until after all
186 inlining transformations have been applied.
188 The way we propagate clobber/use information isn't optimized.
189 It should use a new complex constraint that properly filters
190 out local variables of the callee (though that would make
191 the sets invalid after inlining). OTOH we might as well
192 admit defeat to WHOPR and simply do all the clobber/use analysis
193 and propagation after PTA finished but before we threw away
194 points-to information for memory variables. WHOPR and PTA
195 do not play along well anyway - the whole constraint solving
196 would need to be done in WPA phase and it will be very interesting
197 to apply the results to local SSA names during LTRANS phase.
199 We probably should compute a per-function unit-ESCAPE solution
200 propagating it simply like the clobber / uses solutions. The
201 solution can go alongside the non-IPA espaced solution and be
202 used to query which vars escape the unit through a function.
204 We never put function decls in points-to sets so we do not
205 keep the set of called functions for indirect calls.
207 And probably more. */
209 static bool use_field_sensitive = true;
210 static int in_ipa_mode = 0;
212 /* Used for predecessor bitmaps. */
213 static bitmap_obstack predbitmap_obstack;
215 /* Used for points-to sets. */
216 static bitmap_obstack pta_obstack;
218 /* Used for oldsolution members of variables. */
219 static bitmap_obstack oldpta_obstack;
221 /* Used for per-solver-iteration bitmaps. */
222 static bitmap_obstack iteration_obstack;
224 static unsigned int create_variable_info_for (tree, const char *);
225 typedef struct constraint_graph *constraint_graph_t;
226 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
228 struct constraint;
229 typedef struct constraint *constraint_t;
232 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
233 if (a) \
234 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
236 static struct constraint_stats
238 unsigned int total_vars;
239 unsigned int nonpointer_vars;
240 unsigned int unified_vars_static;
241 unsigned int unified_vars_dynamic;
242 unsigned int iterations;
243 unsigned int num_edges;
244 unsigned int num_implicit_edges;
245 unsigned int points_to_sets_created;
246 } stats;
248 struct variable_info
250 /* ID of this variable */
251 unsigned int id;
253 /* True if this is a variable created by the constraint analysis, such as
254 heap variables and constraints we had to break up. */
255 unsigned int is_artificial_var : 1;
257 /* True if this is a special variable whose solution set should not be
258 changed. */
259 unsigned int is_special_var : 1;
261 /* True for variables whose size is not known or variable. */
262 unsigned int is_unknown_size_var : 1;
264 /* True for (sub-)fields that represent a whole variable. */
265 unsigned int is_full_var : 1;
267 /* True if this is a heap variable. */
268 unsigned int is_heap_var : 1;
270 /* True if this field may contain pointers. */
271 unsigned int may_have_pointers : 1;
273 /* True if this field has only restrict qualified pointers. */
274 unsigned int only_restrict_pointers : 1;
276 /* True if this represents a global variable. */
277 unsigned int is_global_var : 1;
279 /* True if this represents a IPA function info. */
280 unsigned int is_fn_info : 1;
282 /* The ID of the variable for the next field in this structure
283 or zero for the last field in this structure. */
284 unsigned next;
286 /* The ID of the variable for the first field in this structure. */
287 unsigned head;
289 /* Offset of this variable, in bits, from the base variable */
290 unsigned HOST_WIDE_INT offset;
292 /* Size of the variable, in bits. */
293 unsigned HOST_WIDE_INT size;
295 /* Full size of the base variable, in bits. */
296 unsigned HOST_WIDE_INT fullsize;
298 /* Name of this variable */
299 const char *name;
301 /* Tree that this variable is associated with. */
302 tree decl;
304 /* Points-to set for this variable. */
305 bitmap solution;
307 /* Old points-to set for this variable. */
308 bitmap oldsolution;
310 typedef struct variable_info *varinfo_t;
312 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
313 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
314 unsigned HOST_WIDE_INT);
315 static varinfo_t lookup_vi_for_tree (tree);
316 static inline bool type_can_have_subvars (const_tree);
318 /* Pool of variable info structures. */
319 static alloc_pool variable_info_pool;
321 /* Map varinfo to final pt_solution. */
322 static hash_map<varinfo_t, pt_solution *> *final_solutions;
323 struct obstack final_solutions_obstack;
325 /* Table of variable info structures for constraint variables.
326 Indexed directly by variable info id. */
327 static vec<varinfo_t> varmap;
329 /* Return the varmap element N */
331 static inline varinfo_t
332 get_varinfo (unsigned int n)
334 return varmap[n];
337 /* Return the next variable in the list of sub-variables of VI
338 or NULL if VI is the last sub-variable. */
340 static inline varinfo_t
341 vi_next (varinfo_t vi)
343 return get_varinfo (vi->next);
346 /* Static IDs for the special variables. Variable ID zero is unused
347 and used as terminator for the sub-variable chain. */
348 enum { nothing_id = 1, anything_id = 2, readonly_id = 3,
349 escaped_id = 4, nonlocal_id = 5,
350 storedanything_id = 6, integer_id = 7 };
352 /* Return a new variable info structure consisting for a variable
353 named NAME, and using constraint graph node NODE. Append it
354 to the vector of variable info structures. */
356 static varinfo_t
357 new_var_info (tree t, const char *name)
359 unsigned index = varmap.length ();
360 varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
362 ret->id = index;
363 ret->name = name;
364 ret->decl = t;
365 /* Vars without decl are artificial and do not have sub-variables. */
366 ret->is_artificial_var = (t == NULL_TREE);
367 ret->is_special_var = false;
368 ret->is_unknown_size_var = false;
369 ret->is_full_var = (t == NULL_TREE);
370 ret->is_heap_var = false;
371 ret->may_have_pointers = true;
372 ret->only_restrict_pointers = false;
373 ret->is_global_var = (t == NULL_TREE);
374 ret->is_fn_info = false;
375 if (t && DECL_P (t))
376 ret->is_global_var = (is_global_var (t)
377 /* We have to treat even local register variables
378 as escape points. */
379 || (TREE_CODE (t) == VAR_DECL
380 && DECL_HARD_REGISTER (t)));
381 ret->solution = BITMAP_ALLOC (&pta_obstack);
382 ret->oldsolution = NULL;
383 ret->next = 0;
384 ret->head = ret->id;
386 stats.total_vars++;
388 varmap.safe_push (ret);
390 return ret;
394 /* A map mapping call statements to per-stmt variables for uses
395 and clobbers specific to the call. */
396 static hash_map<gimple, varinfo_t> *call_stmt_vars;
398 /* Lookup or create the variable for the call statement CALL. */
400 static varinfo_t
401 get_call_vi (gimple call)
403 varinfo_t vi, vi2;
405 bool existed;
406 varinfo_t *slot_p = &call_stmt_vars->get_or_insert (call, &existed);
407 if (existed)
408 return *slot_p;
410 vi = new_var_info (NULL_TREE, "CALLUSED");
411 vi->offset = 0;
412 vi->size = 1;
413 vi->fullsize = 2;
414 vi->is_full_var = true;
416 vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED");
417 vi2->offset = 1;
418 vi2->size = 1;
419 vi2->fullsize = 2;
420 vi2->is_full_var = true;
422 vi->next = vi2->id;
424 *slot_p = vi;
425 return vi;
428 /* Lookup the variable for the call statement CALL representing
429 the uses. Returns NULL if there is nothing special about this call. */
431 static varinfo_t
432 lookup_call_use_vi (gimple call)
434 varinfo_t *slot_p = call_stmt_vars->get (call);
435 if (slot_p)
436 return *slot_p;
438 return NULL;
441 /* Lookup the variable for the call statement CALL representing
442 the clobbers. Returns NULL if there is nothing special about this call. */
444 static varinfo_t
445 lookup_call_clobber_vi (gimple call)
447 varinfo_t uses = lookup_call_use_vi (call);
448 if (!uses)
449 return NULL;
451 return vi_next (uses);
454 /* Lookup or create the variable for the call statement CALL representing
455 the uses. */
457 static varinfo_t
458 get_call_use_vi (gimple call)
460 return get_call_vi (call);
463 /* Lookup or create the variable for the call statement CALL representing
464 the clobbers. */
466 static varinfo_t ATTRIBUTE_UNUSED
467 get_call_clobber_vi (gimple call)
469 return vi_next (get_call_vi (call));
473 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
475 /* An expression that appears in a constraint. */
477 struct constraint_expr
479 /* Constraint type. */
480 constraint_expr_type type;
482 /* Variable we are referring to in the constraint. */
483 unsigned int var;
485 /* Offset, in bits, of this constraint from the beginning of
486 variables it ends up referring to.
488 IOW, in a deref constraint, we would deref, get the result set,
489 then add OFFSET to each member. */
490 HOST_WIDE_INT offset;
493 /* Use 0x8000... as special unknown offset. */
494 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
496 typedef struct constraint_expr ce_s;
497 static void get_constraint_for_1 (tree, vec<ce_s> *, bool, bool);
498 static void get_constraint_for (tree, vec<ce_s> *);
499 static void get_constraint_for_rhs (tree, vec<ce_s> *);
500 static void do_deref (vec<ce_s> *);
502 /* Our set constraints are made up of two constraint expressions, one
503 LHS, and one RHS.
505 As described in the introduction, our set constraints each represent an
506 operation between set valued variables.
508 struct constraint
510 struct constraint_expr lhs;
511 struct constraint_expr rhs;
514 /* List of constraints that we use to build the constraint graph from. */
516 static vec<constraint_t> constraints;
517 static alloc_pool constraint_pool;
519 /* The constraint graph is represented as an array of bitmaps
520 containing successor nodes. */
522 struct constraint_graph
524 /* Size of this graph, which may be different than the number of
525 nodes in the variable map. */
526 unsigned int size;
528 /* Explicit successors of each node. */
529 bitmap *succs;
531 /* Implicit predecessors of each node (Used for variable
532 substitution). */
533 bitmap *implicit_preds;
535 /* Explicit predecessors of each node (Used for variable substitution). */
536 bitmap *preds;
538 /* Indirect cycle representatives, or -1 if the node has no indirect
539 cycles. */
540 int *indirect_cycles;
542 /* Representative node for a node. rep[a] == a unless the node has
543 been unified. */
544 unsigned int *rep;
546 /* Equivalence class representative for a label. This is used for
547 variable substitution. */
548 int *eq_rep;
550 /* Pointer equivalence label for a node. All nodes with the same
551 pointer equivalence label can be unified together at some point
552 (either during constraint optimization or after the constraint
553 graph is built). */
554 unsigned int *pe;
556 /* Pointer equivalence representative for a label. This is used to
557 handle nodes that are pointer equivalent but not location
558 equivalent. We can unite these once the addressof constraints
559 are transformed into initial points-to sets. */
560 int *pe_rep;
562 /* Pointer equivalence label for each node, used during variable
563 substitution. */
564 unsigned int *pointer_label;
566 /* Location equivalence label for each node, used during location
567 equivalence finding. */
568 unsigned int *loc_label;
570 /* Pointed-by set for each node, used during location equivalence
571 finding. This is pointed-by rather than pointed-to, because it
572 is constructed using the predecessor graph. */
573 bitmap *pointed_by;
575 /* Points to sets for pointer equivalence. This is *not* the actual
576 points-to sets for nodes. */
577 bitmap *points_to;
579 /* Bitmap of nodes where the bit is set if the node is a direct
580 node. Used for variable substitution. */
581 sbitmap direct_nodes;
583 /* Bitmap of nodes where the bit is set if the node is address
584 taken. Used for variable substitution. */
585 bitmap address_taken;
587 /* Vector of complex constraints for each graph node. Complex
588 constraints are those involving dereferences or offsets that are
589 not 0. */
590 vec<constraint_t> *complex;
593 static constraint_graph_t graph;
595 /* During variable substitution and the offline version of indirect
596 cycle finding, we create nodes to represent dereferences and
597 address taken constraints. These represent where these start and
598 end. */
599 #define FIRST_REF_NODE (varmap).length ()
600 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
602 /* Return the representative node for NODE, if NODE has been unioned
603 with another NODE.
604 This function performs path compression along the way to finding
605 the representative. */
607 static unsigned int
608 find (unsigned int node)
610 gcc_checking_assert (node < graph->size);
611 if (graph->rep[node] != node)
612 return graph->rep[node] = find (graph->rep[node]);
613 return node;
616 /* Union the TO and FROM nodes to the TO nodes.
617 Note that at some point in the future, we may want to do
618 union-by-rank, in which case we are going to have to return the
619 node we unified to. */
621 static bool
622 unite (unsigned int to, unsigned int from)
624 gcc_checking_assert (to < graph->size && from < graph->size);
625 if (to != from && graph->rep[from] != to)
627 graph->rep[from] = to;
628 return true;
630 return false;
633 /* Create a new constraint consisting of LHS and RHS expressions. */
635 static constraint_t
636 new_constraint (const struct constraint_expr lhs,
637 const struct constraint_expr rhs)
639 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
640 ret->lhs = lhs;
641 ret->rhs = rhs;
642 return ret;
645 /* Print out constraint C to FILE. */
647 static void
648 dump_constraint (FILE *file, constraint_t c)
650 if (c->lhs.type == ADDRESSOF)
651 fprintf (file, "&");
652 else if (c->lhs.type == DEREF)
653 fprintf (file, "*");
654 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
655 if (c->lhs.offset == UNKNOWN_OFFSET)
656 fprintf (file, " + UNKNOWN");
657 else if (c->lhs.offset != 0)
658 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
659 fprintf (file, " = ");
660 if (c->rhs.type == ADDRESSOF)
661 fprintf (file, "&");
662 else if (c->rhs.type == DEREF)
663 fprintf (file, "*");
664 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
665 if (c->rhs.offset == UNKNOWN_OFFSET)
666 fprintf (file, " + UNKNOWN");
667 else if (c->rhs.offset != 0)
668 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
672 void debug_constraint (constraint_t);
673 void debug_constraints (void);
674 void debug_constraint_graph (void);
675 void debug_solution_for_var (unsigned int);
676 void debug_sa_points_to_info (void);
678 /* Print out constraint C to stderr. */
680 DEBUG_FUNCTION void
681 debug_constraint (constraint_t c)
683 dump_constraint (stderr, c);
684 fprintf (stderr, "\n");
687 /* Print out all constraints to FILE */
689 static void
690 dump_constraints (FILE *file, int from)
692 int i;
693 constraint_t c;
694 for (i = from; constraints.iterate (i, &c); i++)
695 if (c)
697 dump_constraint (file, c);
698 fprintf (file, "\n");
702 /* Print out all constraints to stderr. */
704 DEBUG_FUNCTION void
705 debug_constraints (void)
707 dump_constraints (stderr, 0);
710 /* Print the constraint graph in dot format. */
712 static void
713 dump_constraint_graph (FILE *file)
715 unsigned int i;
717 /* Only print the graph if it has already been initialized: */
718 if (!graph)
719 return;
721 /* Prints the header of the dot file: */
722 fprintf (file, "strict digraph {\n");
723 fprintf (file, " node [\n shape = box\n ]\n");
724 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
725 fprintf (file, "\n // List of nodes and complex constraints in "
726 "the constraint graph:\n");
728 /* The next lines print the nodes in the graph together with the
729 complex constraints attached to them. */
730 for (i = 1; i < graph->size; i++)
732 if (i == FIRST_REF_NODE)
733 continue;
734 if (find (i) != i)
735 continue;
736 if (i < FIRST_REF_NODE)
737 fprintf (file, "\"%s\"", get_varinfo (i)->name);
738 else
739 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
740 if (graph->complex[i].exists ())
742 unsigned j;
743 constraint_t c;
744 fprintf (file, " [label=\"\\N\\n");
745 for (j = 0; graph->complex[i].iterate (j, &c); ++j)
747 dump_constraint (file, c);
748 fprintf (file, "\\l");
750 fprintf (file, "\"]");
752 fprintf (file, ";\n");
755 /* Go over the edges. */
756 fprintf (file, "\n // Edges in the constraint graph:\n");
757 for (i = 1; i < graph->size; i++)
759 unsigned j;
760 bitmap_iterator bi;
761 if (find (i) != i)
762 continue;
763 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
765 unsigned to = find (j);
766 if (i == to)
767 continue;
768 if (i < FIRST_REF_NODE)
769 fprintf (file, "\"%s\"", get_varinfo (i)->name);
770 else
771 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
772 fprintf (file, " -> ");
773 if (to < FIRST_REF_NODE)
774 fprintf (file, "\"%s\"", get_varinfo (to)->name);
775 else
776 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name);
777 fprintf (file, ";\n");
781 /* Prints the tail of the dot file. */
782 fprintf (file, "}\n");
785 /* Print out the constraint graph to stderr. */
787 DEBUG_FUNCTION void
788 debug_constraint_graph (void)
790 dump_constraint_graph (stderr);
793 /* SOLVER FUNCTIONS
795 The solver is a simple worklist solver, that works on the following
796 algorithm:
798 sbitmap changed_nodes = all zeroes;
799 changed_count = 0;
800 For each node that is not already collapsed:
801 changed_count++;
802 set bit in changed nodes
804 while (changed_count > 0)
806 compute topological ordering for constraint graph
808 find and collapse cycles in the constraint graph (updating
809 changed if necessary)
811 for each node (n) in the graph in topological order:
812 changed_count--;
814 Process each complex constraint associated with the node,
815 updating changed if necessary.
817 For each outgoing edge from n, propagate the solution from n to
818 the destination of the edge, updating changed as necessary.
820 } */
822 /* Return true if two constraint expressions A and B are equal. */
824 static bool
825 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
827 return a.type == b.type && a.var == b.var && a.offset == b.offset;
830 /* Return true if constraint expression A is less than constraint expression
831 B. This is just arbitrary, but consistent, in order to give them an
832 ordering. */
834 static bool
835 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
837 if (a.type == b.type)
839 if (a.var == b.var)
840 return a.offset < b.offset;
841 else
842 return a.var < b.var;
844 else
845 return a.type < b.type;
848 /* Return true if constraint A is less than constraint B. This is just
849 arbitrary, but consistent, in order to give them an ordering. */
851 static bool
852 constraint_less (const constraint_t &a, const constraint_t &b)
854 if (constraint_expr_less (a->lhs, b->lhs))
855 return true;
856 else if (constraint_expr_less (b->lhs, a->lhs))
857 return false;
858 else
859 return constraint_expr_less (a->rhs, b->rhs);
862 /* Return true if two constraints A and B are equal. */
864 static bool
865 constraint_equal (struct constraint a, struct constraint b)
867 return constraint_expr_equal (a.lhs, b.lhs)
868 && constraint_expr_equal (a.rhs, b.rhs);
872 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
874 static constraint_t
875 constraint_vec_find (vec<constraint_t> vec,
876 struct constraint lookfor)
878 unsigned int place;
879 constraint_t found;
881 if (!vec.exists ())
882 return NULL;
884 place = vec.lower_bound (&lookfor, constraint_less);
885 if (place >= vec.length ())
886 return NULL;
887 found = vec[place];
888 if (!constraint_equal (*found, lookfor))
889 return NULL;
890 return found;
893 /* Union two constraint vectors, TO and FROM. Put the result in TO.
894 Returns true of TO set is changed. */
896 static bool
897 constraint_set_union (vec<constraint_t> *to,
898 vec<constraint_t> *from)
900 int i;
901 constraint_t c;
902 bool any_change = false;
904 FOR_EACH_VEC_ELT (*from, i, c)
906 if (constraint_vec_find (*to, *c) == NULL)
908 unsigned int place = to->lower_bound (c, constraint_less);
909 to->safe_insert (place, c);
910 any_change = true;
913 return any_change;
916 /* Expands the solution in SET to all sub-fields of variables included. */
918 static bitmap
919 solution_set_expand (bitmap set, bitmap *expanded)
921 bitmap_iterator bi;
922 unsigned j;
924 if (*expanded)
925 return *expanded;
927 *expanded = BITMAP_ALLOC (&iteration_obstack);
929 /* In a first pass expand to the head of the variables we need to
930 add all sub-fields off. This avoids quadratic behavior. */
931 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
933 varinfo_t v = get_varinfo (j);
934 if (v->is_artificial_var
935 || v->is_full_var)
936 continue;
937 bitmap_set_bit (*expanded, v->head);
940 /* In the second pass now expand all head variables with subfields. */
941 EXECUTE_IF_SET_IN_BITMAP (*expanded, 0, j, bi)
943 varinfo_t v = get_varinfo (j);
944 if (v->head != j)
945 continue;
946 for (v = vi_next (v); v != NULL; v = vi_next (v))
947 bitmap_set_bit (*expanded, v->id);
950 /* And finally set the rest of the bits from SET. */
951 bitmap_ior_into (*expanded, set);
953 return *expanded;
956 /* Union solution sets TO and DELTA, and add INC to each member of DELTA in the
957 process. */
959 static bool
960 set_union_with_increment (bitmap to, bitmap delta, HOST_WIDE_INT inc,
961 bitmap *expanded_delta)
963 bool changed = false;
964 bitmap_iterator bi;
965 unsigned int i;
967 /* If the solution of DELTA contains anything it is good enough to transfer
968 this to TO. */
969 if (bitmap_bit_p (delta, anything_id))
970 return bitmap_set_bit (to, anything_id);
972 /* If the offset is unknown we have to expand the solution to
973 all subfields. */
974 if (inc == UNKNOWN_OFFSET)
976 delta = solution_set_expand (delta, expanded_delta);
977 changed |= bitmap_ior_into (to, delta);
978 return changed;
981 /* For non-zero offset union the offsetted solution into the destination. */
982 EXECUTE_IF_SET_IN_BITMAP (delta, 0, i, bi)
984 varinfo_t vi = get_varinfo (i);
986 /* If this is a variable with just one field just set its bit
987 in the result. */
988 if (vi->is_artificial_var
989 || vi->is_unknown_size_var
990 || vi->is_full_var)
991 changed |= bitmap_set_bit (to, i);
992 else
994 HOST_WIDE_INT fieldoffset = vi->offset + inc;
995 unsigned HOST_WIDE_INT size = vi->size;
997 /* If the offset makes the pointer point to before the
998 variable use offset zero for the field lookup. */
999 if (fieldoffset < 0)
1000 vi = get_varinfo (vi->head);
1001 else
1002 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
1006 changed |= bitmap_set_bit (to, vi->id);
1007 if (vi->is_full_var
1008 || vi->next == 0)
1009 break;
1011 /* We have to include all fields that overlap the current field
1012 shifted by inc. */
1013 vi = vi_next (vi);
1015 while (vi->offset < fieldoffset + size);
1019 return changed;
1022 /* Insert constraint C into the list of complex constraints for graph
1023 node VAR. */
1025 static void
1026 insert_into_complex (constraint_graph_t graph,
1027 unsigned int var, constraint_t c)
1029 vec<constraint_t> complex = graph->complex[var];
1030 unsigned int place = complex.lower_bound (c, constraint_less);
1032 /* Only insert constraints that do not already exist. */
1033 if (place >= complex.length ()
1034 || !constraint_equal (*c, *complex[place]))
1035 graph->complex[var].safe_insert (place, c);
1039 /* Condense two variable nodes into a single variable node, by moving
1040 all associated info from FROM to TO. Returns true if TO node's
1041 constraint set changes after the merge. */
1043 static bool
1044 merge_node_constraints (constraint_graph_t graph, unsigned int to,
1045 unsigned int from)
1047 unsigned int i;
1048 constraint_t c;
1049 bool any_change = false;
1051 gcc_checking_assert (find (from) == to);
1053 /* Move all complex constraints from src node into to node */
1054 FOR_EACH_VEC_ELT (graph->complex[from], i, c)
1056 /* In complex constraints for node FROM, we may have either
1057 a = *FROM, and *FROM = a, or an offseted constraint which are
1058 always added to the rhs node's constraints. */
1060 if (c->rhs.type == DEREF)
1061 c->rhs.var = to;
1062 else if (c->lhs.type == DEREF)
1063 c->lhs.var = to;
1064 else
1065 c->rhs.var = to;
1068 any_change = constraint_set_union (&graph->complex[to],
1069 &graph->complex[from]);
1070 graph->complex[from].release ();
1071 return any_change;
1075 /* Remove edges involving NODE from GRAPH. */
1077 static void
1078 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
1080 if (graph->succs[node])
1081 BITMAP_FREE (graph->succs[node]);
1084 /* Merge GRAPH nodes FROM and TO into node TO. */
1086 static void
1087 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1088 unsigned int from)
1090 if (graph->indirect_cycles[from] != -1)
1092 /* If we have indirect cycles with the from node, and we have
1093 none on the to node, the to node has indirect cycles from the
1094 from node now that they are unified.
1095 If indirect cycles exist on both, unify the nodes that they
1096 are in a cycle with, since we know they are in a cycle with
1097 each other. */
1098 if (graph->indirect_cycles[to] == -1)
1099 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1102 /* Merge all the successor edges. */
1103 if (graph->succs[from])
1105 if (!graph->succs[to])
1106 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1107 bitmap_ior_into (graph->succs[to],
1108 graph->succs[from]);
1111 clear_edges_for_node (graph, from);
1115 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1116 it doesn't exist in the graph already. */
1118 static void
1119 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1120 unsigned int from)
1122 if (to == from)
1123 return;
1125 if (!graph->implicit_preds[to])
1126 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1128 if (bitmap_set_bit (graph->implicit_preds[to], from))
1129 stats.num_implicit_edges++;
1132 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1133 it doesn't exist in the graph already.
1134 Return false if the edge already existed, true otherwise. */
1136 static void
1137 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1138 unsigned int from)
1140 if (!graph->preds[to])
1141 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1142 bitmap_set_bit (graph->preds[to], from);
1145 /* Add a graph edge to GRAPH, going from FROM to TO if
1146 it doesn't exist in the graph already.
1147 Return false if the edge already existed, true otherwise. */
1149 static bool
1150 add_graph_edge (constraint_graph_t graph, unsigned int to,
1151 unsigned int from)
1153 if (to == from)
1155 return false;
1157 else
1159 bool r = false;
1161 if (!graph->succs[from])
1162 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1163 if (bitmap_set_bit (graph->succs[from], to))
1165 r = true;
1166 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1167 stats.num_edges++;
1169 return r;
1174 /* Initialize the constraint graph structure to contain SIZE nodes. */
1176 static void
1177 init_graph (unsigned int size)
1179 unsigned int j;
1181 graph = XCNEW (struct constraint_graph);
1182 graph->size = size;
1183 graph->succs = XCNEWVEC (bitmap, graph->size);
1184 graph->indirect_cycles = XNEWVEC (int, graph->size);
1185 graph->rep = XNEWVEC (unsigned int, graph->size);
1186 /* ??? Macros do not support template types with multiple arguments,
1187 so we use a typedef to work around it. */
1188 typedef vec<constraint_t> vec_constraint_t_heap;
1189 graph->complex = XCNEWVEC (vec_constraint_t_heap, size);
1190 graph->pe = XCNEWVEC (unsigned int, graph->size);
1191 graph->pe_rep = XNEWVEC (int, graph->size);
1193 for (j = 0; j < graph->size; j++)
1195 graph->rep[j] = j;
1196 graph->pe_rep[j] = -1;
1197 graph->indirect_cycles[j] = -1;
1201 /* Build the constraint graph, adding only predecessor edges right now. */
1203 static void
1204 build_pred_graph (void)
1206 int i;
1207 constraint_t c;
1208 unsigned int j;
1210 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1211 graph->preds = XCNEWVEC (bitmap, graph->size);
1212 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1213 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1214 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1215 graph->points_to = XCNEWVEC (bitmap, graph->size);
1216 graph->eq_rep = XNEWVEC (int, graph->size);
1217 graph->direct_nodes = sbitmap_alloc (graph->size);
1218 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1219 bitmap_clear (graph->direct_nodes);
1221 for (j = 1; j < FIRST_REF_NODE; j++)
1223 if (!get_varinfo (j)->is_special_var)
1224 bitmap_set_bit (graph->direct_nodes, j);
1227 for (j = 0; j < graph->size; j++)
1228 graph->eq_rep[j] = -1;
1230 for (j = 0; j < varmap.length (); j++)
1231 graph->indirect_cycles[j] = -1;
1233 FOR_EACH_VEC_ELT (constraints, i, c)
1235 struct constraint_expr lhs = c->lhs;
1236 struct constraint_expr rhs = c->rhs;
1237 unsigned int lhsvar = lhs.var;
1238 unsigned int rhsvar = rhs.var;
1240 if (lhs.type == DEREF)
1242 /* *x = y. */
1243 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1244 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1246 else if (rhs.type == DEREF)
1248 /* x = *y */
1249 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1250 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1251 else
1252 bitmap_clear_bit (graph->direct_nodes, lhsvar);
1254 else if (rhs.type == ADDRESSOF)
1256 varinfo_t v;
1258 /* x = &y */
1259 if (graph->points_to[lhsvar] == NULL)
1260 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1261 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1263 if (graph->pointed_by[rhsvar] == NULL)
1264 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1265 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1267 /* Implicitly, *x = y */
1268 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1270 /* All related variables are no longer direct nodes. */
1271 bitmap_clear_bit (graph->direct_nodes, rhsvar);
1272 v = get_varinfo (rhsvar);
1273 if (!v->is_full_var)
1275 v = get_varinfo (v->head);
1278 bitmap_clear_bit (graph->direct_nodes, v->id);
1279 v = vi_next (v);
1281 while (v != NULL);
1283 bitmap_set_bit (graph->address_taken, rhsvar);
1285 else if (lhsvar > anything_id
1286 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1288 /* x = y */
1289 add_pred_graph_edge (graph, lhsvar, rhsvar);
1290 /* Implicitly, *x = *y */
1291 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1292 FIRST_REF_NODE + rhsvar);
1294 else if (lhs.offset != 0 || rhs.offset != 0)
1296 if (rhs.offset != 0)
1297 bitmap_clear_bit (graph->direct_nodes, lhs.var);
1298 else if (lhs.offset != 0)
1299 bitmap_clear_bit (graph->direct_nodes, rhs.var);
1304 /* Build the constraint graph, adding successor edges. */
1306 static void
1307 build_succ_graph (void)
1309 unsigned i, t;
1310 constraint_t c;
1312 FOR_EACH_VEC_ELT (constraints, i, c)
1314 struct constraint_expr lhs;
1315 struct constraint_expr rhs;
1316 unsigned int lhsvar;
1317 unsigned int rhsvar;
1319 if (!c)
1320 continue;
1322 lhs = c->lhs;
1323 rhs = c->rhs;
1324 lhsvar = find (lhs.var);
1325 rhsvar = find (rhs.var);
1327 if (lhs.type == DEREF)
1329 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1330 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1332 else if (rhs.type == DEREF)
1334 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1335 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1337 else if (rhs.type == ADDRESSOF)
1339 /* x = &y */
1340 gcc_checking_assert (find (rhs.var) == rhs.var);
1341 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1343 else if (lhsvar > anything_id
1344 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1346 add_graph_edge (graph, lhsvar, rhsvar);
1350 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1351 receive pointers. */
1352 t = find (storedanything_id);
1353 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1355 if (!bitmap_bit_p (graph->direct_nodes, i)
1356 && get_varinfo (i)->may_have_pointers)
1357 add_graph_edge (graph, find (i), t);
1360 /* Everything stored to ANYTHING also potentially escapes. */
1361 add_graph_edge (graph, find (escaped_id), t);
1365 /* Changed variables on the last iteration. */
1366 static bitmap changed;
1368 /* Strongly Connected Component visitation info. */
1370 struct scc_info
1372 sbitmap visited;
1373 sbitmap deleted;
1374 unsigned int *dfs;
1375 unsigned int *node_mapping;
1376 int current_index;
1377 vec<unsigned> scc_stack;
1381 /* Recursive routine to find strongly connected components in GRAPH.
1382 SI is the SCC info to store the information in, and N is the id of current
1383 graph node we are processing.
1385 This is Tarjan's strongly connected component finding algorithm, as
1386 modified by Nuutila to keep only non-root nodes on the stack.
1387 The algorithm can be found in "On finding the strongly connected
1388 connected components in a directed graph" by Esko Nuutila and Eljas
1389 Soisalon-Soininen, in Information Processing Letters volume 49,
1390 number 1, pages 9-14. */
1392 static void
1393 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1395 unsigned int i;
1396 bitmap_iterator bi;
1397 unsigned int my_dfs;
1399 bitmap_set_bit (si->visited, n);
1400 si->dfs[n] = si->current_index ++;
1401 my_dfs = si->dfs[n];
1403 /* Visit all the successors. */
1404 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1406 unsigned int w;
1408 if (i > LAST_REF_NODE)
1409 break;
1411 w = find (i);
1412 if (bitmap_bit_p (si->deleted, w))
1413 continue;
1415 if (!bitmap_bit_p (si->visited, w))
1416 scc_visit (graph, si, w);
1418 unsigned int t = find (w);
1419 gcc_checking_assert (find (n) == n);
1420 if (si->dfs[t] < si->dfs[n])
1421 si->dfs[n] = si->dfs[t];
1424 /* See if any components have been identified. */
1425 if (si->dfs[n] == my_dfs)
1427 if (si->scc_stack.length () > 0
1428 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1430 bitmap scc = BITMAP_ALLOC (NULL);
1431 unsigned int lowest_node;
1432 bitmap_iterator bi;
1434 bitmap_set_bit (scc, n);
1436 while (si->scc_stack.length () != 0
1437 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1439 unsigned int w = si->scc_stack.pop ();
1441 bitmap_set_bit (scc, w);
1444 lowest_node = bitmap_first_set_bit (scc);
1445 gcc_assert (lowest_node < FIRST_REF_NODE);
1447 /* Collapse the SCC nodes into a single node, and mark the
1448 indirect cycles. */
1449 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1451 if (i < FIRST_REF_NODE)
1453 if (unite (lowest_node, i))
1454 unify_nodes (graph, lowest_node, i, false);
1456 else
1458 unite (lowest_node, i);
1459 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1463 bitmap_set_bit (si->deleted, n);
1465 else
1466 si->scc_stack.safe_push (n);
1469 /* Unify node FROM into node TO, updating the changed count if
1470 necessary when UPDATE_CHANGED is true. */
1472 static void
1473 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1474 bool update_changed)
1476 gcc_checking_assert (to != from && find (to) == to);
1478 if (dump_file && (dump_flags & TDF_DETAILS))
1479 fprintf (dump_file, "Unifying %s to %s\n",
1480 get_varinfo (from)->name,
1481 get_varinfo (to)->name);
1483 if (update_changed)
1484 stats.unified_vars_dynamic++;
1485 else
1486 stats.unified_vars_static++;
1488 merge_graph_nodes (graph, to, from);
1489 if (merge_node_constraints (graph, to, from))
1491 if (update_changed)
1492 bitmap_set_bit (changed, to);
1495 /* Mark TO as changed if FROM was changed. If TO was already marked
1496 as changed, decrease the changed count. */
1498 if (update_changed
1499 && bitmap_clear_bit (changed, from))
1500 bitmap_set_bit (changed, to);
1501 varinfo_t fromvi = get_varinfo (from);
1502 if (fromvi->solution)
1504 /* If the solution changes because of the merging, we need to mark
1505 the variable as changed. */
1506 varinfo_t tovi = get_varinfo (to);
1507 if (bitmap_ior_into (tovi->solution, fromvi->solution))
1509 if (update_changed)
1510 bitmap_set_bit (changed, to);
1513 BITMAP_FREE (fromvi->solution);
1514 if (fromvi->oldsolution)
1515 BITMAP_FREE (fromvi->oldsolution);
1517 if (stats.iterations > 0
1518 && tovi->oldsolution)
1519 BITMAP_FREE (tovi->oldsolution);
1521 if (graph->succs[to])
1522 bitmap_clear_bit (graph->succs[to], to);
1525 /* Information needed to compute the topological ordering of a graph. */
1527 struct topo_info
1529 /* sbitmap of visited nodes. */
1530 sbitmap visited;
1531 /* Array that stores the topological order of the graph, *in
1532 reverse*. */
1533 vec<unsigned> topo_order;
1537 /* Initialize and return a topological info structure. */
1539 static struct topo_info *
1540 init_topo_info (void)
1542 size_t size = graph->size;
1543 struct topo_info *ti = XNEW (struct topo_info);
1544 ti->visited = sbitmap_alloc (size);
1545 bitmap_clear (ti->visited);
1546 ti->topo_order.create (1);
1547 return ti;
1551 /* Free the topological sort info pointed to by TI. */
1553 static void
1554 free_topo_info (struct topo_info *ti)
1556 sbitmap_free (ti->visited);
1557 ti->topo_order.release ();
1558 free (ti);
1561 /* Visit the graph in topological order, and store the order in the
1562 topo_info structure. */
1564 static void
1565 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1566 unsigned int n)
1568 bitmap_iterator bi;
1569 unsigned int j;
1571 bitmap_set_bit (ti->visited, n);
1573 if (graph->succs[n])
1574 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1576 if (!bitmap_bit_p (ti->visited, j))
1577 topo_visit (graph, ti, j);
1580 ti->topo_order.safe_push (n);
1583 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1584 starting solution for y. */
1586 static void
1587 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1588 bitmap delta, bitmap *expanded_delta)
1590 unsigned int lhs = c->lhs.var;
1591 bool flag = false;
1592 bitmap sol = get_varinfo (lhs)->solution;
1593 unsigned int j;
1594 bitmap_iterator bi;
1595 HOST_WIDE_INT roffset = c->rhs.offset;
1597 /* Our IL does not allow this. */
1598 gcc_checking_assert (c->lhs.offset == 0);
1600 /* If the solution of Y contains anything it is good enough to transfer
1601 this to the LHS. */
1602 if (bitmap_bit_p (delta, anything_id))
1604 flag |= bitmap_set_bit (sol, anything_id);
1605 goto done;
1608 /* If we do not know at with offset the rhs is dereferenced compute
1609 the reachability set of DELTA, conservatively assuming it is
1610 dereferenced at all valid offsets. */
1611 if (roffset == UNKNOWN_OFFSET)
1613 delta = solution_set_expand (delta, expanded_delta);
1614 /* No further offset processing is necessary. */
1615 roffset = 0;
1618 /* For each variable j in delta (Sol(y)), add
1619 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1620 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1622 varinfo_t v = get_varinfo (j);
1623 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1624 unsigned HOST_WIDE_INT size = v->size;
1625 unsigned int t;
1627 if (v->is_full_var)
1629 else if (roffset != 0)
1631 if (fieldoffset < 0)
1632 v = get_varinfo (v->head);
1633 else
1634 v = first_or_preceding_vi_for_offset (v, fieldoffset);
1637 /* We have to include all fields that overlap the current field
1638 shifted by roffset. */
1641 t = find (v->id);
1643 /* Adding edges from the special vars is pointless.
1644 They don't have sets that can change. */
1645 if (get_varinfo (t)->is_special_var)
1646 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1647 /* Merging the solution from ESCAPED needlessly increases
1648 the set. Use ESCAPED as representative instead. */
1649 else if (v->id == escaped_id)
1650 flag |= bitmap_set_bit (sol, escaped_id);
1651 else if (v->may_have_pointers
1652 && add_graph_edge (graph, lhs, t))
1653 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1655 if (v->is_full_var
1656 || v->next == 0)
1657 break;
1659 v = vi_next (v);
1661 while (v->offset < fieldoffset + size);
1664 done:
1665 /* If the LHS solution changed, mark the var as changed. */
1666 if (flag)
1668 get_varinfo (lhs)->solution = sol;
1669 bitmap_set_bit (changed, lhs);
1673 /* Process a constraint C that represents *(x + off) = y using DELTA
1674 as the starting solution for x. */
1676 static void
1677 do_ds_constraint (constraint_t c, bitmap delta, bitmap *expanded_delta)
1679 unsigned int rhs = c->rhs.var;
1680 bitmap sol = get_varinfo (rhs)->solution;
1681 unsigned int j;
1682 bitmap_iterator bi;
1683 HOST_WIDE_INT loff = c->lhs.offset;
1684 bool escaped_p = false;
1686 /* Our IL does not allow this. */
1687 gcc_checking_assert (c->rhs.offset == 0);
1689 /* If the solution of y contains ANYTHING simply use the ANYTHING
1690 solution. This avoids needlessly increasing the points-to sets. */
1691 if (bitmap_bit_p (sol, anything_id))
1692 sol = get_varinfo (find (anything_id))->solution;
1694 /* If the solution for x contains ANYTHING we have to merge the
1695 solution of y into all pointer variables which we do via
1696 STOREDANYTHING. */
1697 if (bitmap_bit_p (delta, anything_id))
1699 unsigned t = find (storedanything_id);
1700 if (add_graph_edge (graph, t, rhs))
1702 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1703 bitmap_set_bit (changed, t);
1705 return;
1708 /* If we do not know at with offset the rhs is dereferenced compute
1709 the reachability set of DELTA, conservatively assuming it is
1710 dereferenced at all valid offsets. */
1711 if (loff == UNKNOWN_OFFSET)
1713 delta = solution_set_expand (delta, expanded_delta);
1714 loff = 0;
1717 /* For each member j of delta (Sol(x)), add an edge from y to j and
1718 union Sol(y) into Sol(j) */
1719 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1721 varinfo_t v = get_varinfo (j);
1722 unsigned int t;
1723 HOST_WIDE_INT fieldoffset = v->offset + loff;
1724 unsigned HOST_WIDE_INT size = v->size;
1726 if (v->is_full_var)
1728 else if (loff != 0)
1730 if (fieldoffset < 0)
1731 v = get_varinfo (v->head);
1732 else
1733 v = first_or_preceding_vi_for_offset (v, fieldoffset);
1736 /* We have to include all fields that overlap the current field
1737 shifted by loff. */
1740 if (v->may_have_pointers)
1742 /* If v is a global variable then this is an escape point. */
1743 if (v->is_global_var
1744 && !escaped_p)
1746 t = find (escaped_id);
1747 if (add_graph_edge (graph, t, rhs)
1748 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1749 bitmap_set_bit (changed, t);
1750 /* Enough to let rhs escape once. */
1751 escaped_p = true;
1754 if (v->is_special_var)
1755 break;
1757 t = find (v->id);
1758 if (add_graph_edge (graph, t, rhs)
1759 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1760 bitmap_set_bit (changed, t);
1763 if (v->is_full_var
1764 || v->next == 0)
1765 break;
1767 v = vi_next (v);
1769 while (v->offset < fieldoffset + size);
1773 /* Handle a non-simple (simple meaning requires no iteration),
1774 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1776 static void
1777 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta,
1778 bitmap *expanded_delta)
1780 if (c->lhs.type == DEREF)
1782 if (c->rhs.type == ADDRESSOF)
1784 gcc_unreachable ();
1786 else
1788 /* *x = y */
1789 do_ds_constraint (c, delta, expanded_delta);
1792 else if (c->rhs.type == DEREF)
1794 /* x = *y */
1795 if (!(get_varinfo (c->lhs.var)->is_special_var))
1796 do_sd_constraint (graph, c, delta, expanded_delta);
1798 else
1800 bitmap tmp;
1801 bool flag = false;
1803 gcc_checking_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR
1804 && c->rhs.offset != 0 && c->lhs.offset == 0);
1805 tmp = get_varinfo (c->lhs.var)->solution;
1807 flag = set_union_with_increment (tmp, delta, c->rhs.offset,
1808 expanded_delta);
1810 if (flag)
1811 bitmap_set_bit (changed, c->lhs.var);
1815 /* Initialize and return a new SCC info structure. */
1817 static struct scc_info *
1818 init_scc_info (size_t size)
1820 struct scc_info *si = XNEW (struct scc_info);
1821 size_t i;
1823 si->current_index = 0;
1824 si->visited = sbitmap_alloc (size);
1825 bitmap_clear (si->visited);
1826 si->deleted = sbitmap_alloc (size);
1827 bitmap_clear (si->deleted);
1828 si->node_mapping = XNEWVEC (unsigned int, size);
1829 si->dfs = XCNEWVEC (unsigned int, size);
1831 for (i = 0; i < size; i++)
1832 si->node_mapping[i] = i;
1834 si->scc_stack.create (1);
1835 return si;
1838 /* Free an SCC info structure pointed to by SI */
1840 static void
1841 free_scc_info (struct scc_info *si)
1843 sbitmap_free (si->visited);
1844 sbitmap_free (si->deleted);
1845 free (si->node_mapping);
1846 free (si->dfs);
1847 si->scc_stack.release ();
1848 free (si);
1852 /* Find indirect cycles in GRAPH that occur, using strongly connected
1853 components, and note them in the indirect cycles map.
1855 This technique comes from Ben Hardekopf and Calvin Lin,
1856 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1857 Lines of Code", submitted to PLDI 2007. */
1859 static void
1860 find_indirect_cycles (constraint_graph_t graph)
1862 unsigned int i;
1863 unsigned int size = graph->size;
1864 struct scc_info *si = init_scc_info (size);
1866 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1867 if (!bitmap_bit_p (si->visited, i) && find (i) == i)
1868 scc_visit (graph, si, i);
1870 free_scc_info (si);
1873 /* Compute a topological ordering for GRAPH, and store the result in the
1874 topo_info structure TI. */
1876 static void
1877 compute_topo_order (constraint_graph_t graph,
1878 struct topo_info *ti)
1880 unsigned int i;
1881 unsigned int size = graph->size;
1883 for (i = 0; i != size; ++i)
1884 if (!bitmap_bit_p (ti->visited, i) && find (i) == i)
1885 topo_visit (graph, ti, i);
1888 /* Structure used to for hash value numbering of pointer equivalence
1889 classes. */
1891 typedef struct equiv_class_label
1893 hashval_t hashcode;
1894 unsigned int equivalence_class;
1895 bitmap labels;
1896 } *equiv_class_label_t;
1897 typedef const struct equiv_class_label *const_equiv_class_label_t;
1899 /* Equiv_class_label hashtable helpers. */
1901 struct equiv_class_hasher : typed_free_remove <equiv_class_label>
1903 typedef equiv_class_label value_type;
1904 typedef equiv_class_label compare_type;
1905 static inline hashval_t hash (const value_type *);
1906 static inline bool equal (const value_type *, const compare_type *);
1909 /* Hash function for a equiv_class_label_t */
1911 inline hashval_t
1912 equiv_class_hasher::hash (const value_type *ecl)
1914 return ecl->hashcode;
1917 /* Equality function for two equiv_class_label_t's. */
1919 inline bool
1920 equiv_class_hasher::equal (const value_type *eql1, const compare_type *eql2)
1922 return (eql1->hashcode == eql2->hashcode
1923 && bitmap_equal_p (eql1->labels, eql2->labels));
1926 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1927 classes. */
1928 static hash_table<equiv_class_hasher> *pointer_equiv_class_table;
1930 /* A hashtable for mapping a bitmap of labels->location equivalence
1931 classes. */
1932 static hash_table<equiv_class_hasher> *location_equiv_class_table;
1934 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1935 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1936 is equivalent to. */
1938 static equiv_class_label *
1939 equiv_class_lookup_or_add (hash_table<equiv_class_hasher> *table,
1940 bitmap labels)
1942 equiv_class_label **slot;
1943 equiv_class_label ecl;
1945 ecl.labels = labels;
1946 ecl.hashcode = bitmap_hash (labels);
1947 slot = table->find_slot (&ecl, INSERT);
1948 if (!*slot)
1950 *slot = XNEW (struct equiv_class_label);
1951 (*slot)->labels = labels;
1952 (*slot)->hashcode = ecl.hashcode;
1953 (*slot)->equivalence_class = 0;
1956 return *slot;
1959 /* Perform offline variable substitution.
1961 This is a worst case quadratic time way of identifying variables
1962 that must have equivalent points-to sets, including those caused by
1963 static cycles, and single entry subgraphs, in the constraint graph.
1965 The technique is described in "Exploiting Pointer and Location
1966 Equivalence to Optimize Pointer Analysis. In the 14th International
1967 Static Analysis Symposium (SAS), August 2007." It is known as the
1968 "HU" algorithm, and is equivalent to value numbering the collapsed
1969 constraint graph including evaluating unions.
1971 The general method of finding equivalence classes is as follows:
1972 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1973 Initialize all non-REF nodes to be direct nodes.
1974 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1975 variable}
1976 For each constraint containing the dereference, we also do the same
1977 thing.
1979 We then compute SCC's in the graph and unify nodes in the same SCC,
1980 including pts sets.
1982 For each non-collapsed node x:
1983 Visit all unvisited explicit incoming edges.
1984 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1985 where y->x.
1986 Lookup the equivalence class for pts(x).
1987 If we found one, equivalence_class(x) = found class.
1988 Otherwise, equivalence_class(x) = new class, and new_class is
1989 added to the lookup table.
1991 All direct nodes with the same equivalence class can be replaced
1992 with a single representative node.
1993 All unlabeled nodes (label == 0) are not pointers and all edges
1994 involving them can be eliminated.
1995 We perform these optimizations during rewrite_constraints
1997 In addition to pointer equivalence class finding, we also perform
1998 location equivalence class finding. This is the set of variables
1999 that always appear together in points-to sets. We use this to
2000 compress the size of the points-to sets. */
2002 /* Current maximum pointer equivalence class id. */
2003 static int pointer_equiv_class;
2005 /* Current maximum location equivalence class id. */
2006 static int location_equiv_class;
2008 /* Recursive routine to find strongly connected components in GRAPH,
2009 and label it's nodes with DFS numbers. */
2011 static void
2012 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2014 unsigned int i;
2015 bitmap_iterator bi;
2016 unsigned int my_dfs;
2018 gcc_checking_assert (si->node_mapping[n] == n);
2019 bitmap_set_bit (si->visited, n);
2020 si->dfs[n] = si->current_index ++;
2021 my_dfs = si->dfs[n];
2023 /* Visit all the successors. */
2024 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2026 unsigned int w = si->node_mapping[i];
2028 if (bitmap_bit_p (si->deleted, w))
2029 continue;
2031 if (!bitmap_bit_p (si->visited, w))
2032 condense_visit (graph, si, w);
2034 unsigned int t = si->node_mapping[w];
2035 gcc_checking_assert (si->node_mapping[n] == n);
2036 if (si->dfs[t] < si->dfs[n])
2037 si->dfs[n] = si->dfs[t];
2040 /* Visit all the implicit predecessors. */
2041 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2043 unsigned int w = si->node_mapping[i];
2045 if (bitmap_bit_p (si->deleted, w))
2046 continue;
2048 if (!bitmap_bit_p (si->visited, w))
2049 condense_visit (graph, si, w);
2051 unsigned int t = si->node_mapping[w];
2052 gcc_assert (si->node_mapping[n] == n);
2053 if (si->dfs[t] < si->dfs[n])
2054 si->dfs[n] = si->dfs[t];
2057 /* See if any components have been identified. */
2058 if (si->dfs[n] == my_dfs)
2060 while (si->scc_stack.length () != 0
2061 && si->dfs[si->scc_stack.last ()] >= my_dfs)
2063 unsigned int w = si->scc_stack.pop ();
2064 si->node_mapping[w] = n;
2066 if (!bitmap_bit_p (graph->direct_nodes, w))
2067 bitmap_clear_bit (graph->direct_nodes, n);
2069 /* Unify our nodes. */
2070 if (graph->preds[w])
2072 if (!graph->preds[n])
2073 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2074 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2076 if (graph->implicit_preds[w])
2078 if (!graph->implicit_preds[n])
2079 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2080 bitmap_ior_into (graph->implicit_preds[n],
2081 graph->implicit_preds[w]);
2083 if (graph->points_to[w])
2085 if (!graph->points_to[n])
2086 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2087 bitmap_ior_into (graph->points_to[n],
2088 graph->points_to[w]);
2091 bitmap_set_bit (si->deleted, n);
2093 else
2094 si->scc_stack.safe_push (n);
2097 /* Label pointer equivalences.
2099 This performs a value numbering of the constraint graph to
2100 discover which variables will always have the same points-to sets
2101 under the current set of constraints.
2103 The way it value numbers is to store the set of points-to bits
2104 generated by the constraints and graph edges. This is just used as a
2105 hash and equality comparison. The *actual set of points-to bits* is
2106 completely irrelevant, in that we don't care about being able to
2107 extract them later.
2109 The equality values (currently bitmaps) just have to satisfy a few
2110 constraints, the main ones being:
2111 1. The combining operation must be order independent.
2112 2. The end result of a given set of operations must be unique iff the
2113 combination of input values is unique
2114 3. Hashable. */
2116 static void
2117 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2119 unsigned int i, first_pred;
2120 bitmap_iterator bi;
2122 bitmap_set_bit (si->visited, n);
2124 /* Label and union our incoming edges's points to sets. */
2125 first_pred = -1U;
2126 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2128 unsigned int w = si->node_mapping[i];
2129 if (!bitmap_bit_p (si->visited, w))
2130 label_visit (graph, si, w);
2132 /* Skip unused edges */
2133 if (w == n || graph->pointer_label[w] == 0)
2134 continue;
2136 if (graph->points_to[w])
2138 if (!graph->points_to[n])
2140 if (first_pred == -1U)
2141 first_pred = w;
2142 else
2144 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2145 bitmap_ior (graph->points_to[n],
2146 graph->points_to[first_pred],
2147 graph->points_to[w]);
2150 else
2151 bitmap_ior_into (graph->points_to[n], graph->points_to[w]);
2155 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2156 if (!bitmap_bit_p (graph->direct_nodes, n))
2158 if (!graph->points_to[n])
2160 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2161 if (first_pred != -1U)
2162 bitmap_copy (graph->points_to[n], graph->points_to[first_pred]);
2164 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2165 graph->pointer_label[n] = pointer_equiv_class++;
2166 equiv_class_label_t ecl;
2167 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2168 graph->points_to[n]);
2169 ecl->equivalence_class = graph->pointer_label[n];
2170 return;
2173 /* If there was only a single non-empty predecessor the pointer equiv
2174 class is the same. */
2175 if (!graph->points_to[n])
2177 if (first_pred != -1U)
2179 graph->pointer_label[n] = graph->pointer_label[first_pred];
2180 graph->points_to[n] = graph->points_to[first_pred];
2182 return;
2185 if (!bitmap_empty_p (graph->points_to[n]))
2187 equiv_class_label_t ecl;
2188 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2189 graph->points_to[n]);
2190 if (ecl->equivalence_class == 0)
2191 ecl->equivalence_class = pointer_equiv_class++;
2192 else
2194 BITMAP_FREE (graph->points_to[n]);
2195 graph->points_to[n] = ecl->labels;
2197 graph->pointer_label[n] = ecl->equivalence_class;
2201 /* Print the pred graph in dot format. */
2203 static void
2204 dump_pred_graph (struct scc_info *si, FILE *file)
2206 unsigned int i;
2208 /* Only print the graph if it has already been initialized: */
2209 if (!graph)
2210 return;
2212 /* Prints the header of the dot file: */
2213 fprintf (file, "strict digraph {\n");
2214 fprintf (file, " node [\n shape = box\n ]\n");
2215 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
2216 fprintf (file, "\n // List of nodes and complex constraints in "
2217 "the constraint graph:\n");
2219 /* The next lines print the nodes in the graph together with the
2220 complex constraints attached to them. */
2221 for (i = 1; i < graph->size; i++)
2223 if (i == FIRST_REF_NODE)
2224 continue;
2225 if (si->node_mapping[i] != i)
2226 continue;
2227 if (i < FIRST_REF_NODE)
2228 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2229 else
2230 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2231 if (graph->points_to[i]
2232 && !bitmap_empty_p (graph->points_to[i]))
2234 fprintf (file, "[label=\"%s = {", get_varinfo (i)->name);
2235 unsigned j;
2236 bitmap_iterator bi;
2237 EXECUTE_IF_SET_IN_BITMAP (graph->points_to[i], 0, j, bi)
2238 fprintf (file, " %d", j);
2239 fprintf (file, " }\"]");
2241 fprintf (file, ";\n");
2244 /* Go over the edges. */
2245 fprintf (file, "\n // Edges in the constraint graph:\n");
2246 for (i = 1; i < graph->size; i++)
2248 unsigned j;
2249 bitmap_iterator bi;
2250 if (si->node_mapping[i] != i)
2251 continue;
2252 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[i], 0, j, bi)
2254 unsigned from = si->node_mapping[j];
2255 if (from < FIRST_REF_NODE)
2256 fprintf (file, "\"%s\"", get_varinfo (from)->name);
2257 else
2258 fprintf (file, "\"*%s\"", get_varinfo (from - FIRST_REF_NODE)->name);
2259 fprintf (file, " -> ");
2260 if (i < FIRST_REF_NODE)
2261 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2262 else
2263 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2264 fprintf (file, ";\n");
2268 /* Prints the tail of the dot file. */
2269 fprintf (file, "}\n");
2272 /* Perform offline variable substitution, discovering equivalence
2273 classes, and eliminating non-pointer variables. */
2275 static struct scc_info *
2276 perform_var_substitution (constraint_graph_t graph)
2278 unsigned int i;
2279 unsigned int size = graph->size;
2280 struct scc_info *si = init_scc_info (size);
2282 bitmap_obstack_initialize (&iteration_obstack);
2283 pointer_equiv_class_table = new hash_table<equiv_class_hasher> (511);
2284 location_equiv_class_table
2285 = new hash_table<equiv_class_hasher> (511);
2286 pointer_equiv_class = 1;
2287 location_equiv_class = 1;
2289 /* Condense the nodes, which means to find SCC's, count incoming
2290 predecessors, and unite nodes in SCC's. */
2291 for (i = 1; i < FIRST_REF_NODE; i++)
2292 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2293 condense_visit (graph, si, si->node_mapping[i]);
2295 if (dump_file && (dump_flags & TDF_GRAPH))
2297 fprintf (dump_file, "\n\n// The constraint graph before var-substitution "
2298 "in dot format:\n");
2299 dump_pred_graph (si, dump_file);
2300 fprintf (dump_file, "\n\n");
2303 bitmap_clear (si->visited);
2304 /* Actually the label the nodes for pointer equivalences */
2305 for (i = 1; i < FIRST_REF_NODE; i++)
2306 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2307 label_visit (graph, si, si->node_mapping[i]);
2309 /* Calculate location equivalence labels. */
2310 for (i = 1; i < FIRST_REF_NODE; i++)
2312 bitmap pointed_by;
2313 bitmap_iterator bi;
2314 unsigned int j;
2316 if (!graph->pointed_by[i])
2317 continue;
2318 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2320 /* Translate the pointed-by mapping for pointer equivalence
2321 labels. */
2322 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2324 bitmap_set_bit (pointed_by,
2325 graph->pointer_label[si->node_mapping[j]]);
2327 /* The original pointed_by is now dead. */
2328 BITMAP_FREE (graph->pointed_by[i]);
2330 /* Look up the location equivalence label if one exists, or make
2331 one otherwise. */
2332 equiv_class_label_t ecl;
2333 ecl = equiv_class_lookup_or_add (location_equiv_class_table, pointed_by);
2334 if (ecl->equivalence_class == 0)
2335 ecl->equivalence_class = location_equiv_class++;
2336 else
2338 if (dump_file && (dump_flags & TDF_DETAILS))
2339 fprintf (dump_file, "Found location equivalence for node %s\n",
2340 get_varinfo (i)->name);
2341 BITMAP_FREE (pointed_by);
2343 graph->loc_label[i] = ecl->equivalence_class;
2347 if (dump_file && (dump_flags & TDF_DETAILS))
2348 for (i = 1; i < FIRST_REF_NODE; i++)
2350 unsigned j = si->node_mapping[i];
2351 if (j != i)
2353 fprintf (dump_file, "%s node id %d ",
2354 bitmap_bit_p (graph->direct_nodes, i)
2355 ? "Direct" : "Indirect", i);
2356 if (i < FIRST_REF_NODE)
2357 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2358 else
2359 fprintf (dump_file, "\"*%s\"",
2360 get_varinfo (i - FIRST_REF_NODE)->name);
2361 fprintf (dump_file, " mapped to SCC leader node id %d ", j);
2362 if (j < FIRST_REF_NODE)
2363 fprintf (dump_file, "\"%s\"\n", get_varinfo (j)->name);
2364 else
2365 fprintf (dump_file, "\"*%s\"\n",
2366 get_varinfo (j - FIRST_REF_NODE)->name);
2368 else
2370 fprintf (dump_file,
2371 "Equivalence classes for %s node id %d ",
2372 bitmap_bit_p (graph->direct_nodes, i)
2373 ? "direct" : "indirect", i);
2374 if (i < FIRST_REF_NODE)
2375 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2376 else
2377 fprintf (dump_file, "\"*%s\"",
2378 get_varinfo (i - FIRST_REF_NODE)->name);
2379 fprintf (dump_file,
2380 ": pointer %d, location %d\n",
2381 graph->pointer_label[i], graph->loc_label[i]);
2385 /* Quickly eliminate our non-pointer variables. */
2387 for (i = 1; i < FIRST_REF_NODE; i++)
2389 unsigned int node = si->node_mapping[i];
2391 if (graph->pointer_label[node] == 0)
2393 if (dump_file && (dump_flags & TDF_DETAILS))
2394 fprintf (dump_file,
2395 "%s is a non-pointer variable, eliminating edges.\n",
2396 get_varinfo (node)->name);
2397 stats.nonpointer_vars++;
2398 clear_edges_for_node (graph, node);
2402 return si;
2405 /* Free information that was only necessary for variable
2406 substitution. */
2408 static void
2409 free_var_substitution_info (struct scc_info *si)
2411 free_scc_info (si);
2412 free (graph->pointer_label);
2413 free (graph->loc_label);
2414 free (graph->pointed_by);
2415 free (graph->points_to);
2416 free (graph->eq_rep);
2417 sbitmap_free (graph->direct_nodes);
2418 delete pointer_equiv_class_table;
2419 pointer_equiv_class_table = NULL;
2420 delete location_equiv_class_table;
2421 location_equiv_class_table = NULL;
2422 bitmap_obstack_release (&iteration_obstack);
2425 /* Return an existing node that is equivalent to NODE, which has
2426 equivalence class LABEL, if one exists. Return NODE otherwise. */
2428 static unsigned int
2429 find_equivalent_node (constraint_graph_t graph,
2430 unsigned int node, unsigned int label)
2432 /* If the address version of this variable is unused, we can
2433 substitute it for anything else with the same label.
2434 Otherwise, we know the pointers are equivalent, but not the
2435 locations, and we can unite them later. */
2437 if (!bitmap_bit_p (graph->address_taken, node))
2439 gcc_checking_assert (label < graph->size);
2441 if (graph->eq_rep[label] != -1)
2443 /* Unify the two variables since we know they are equivalent. */
2444 if (unite (graph->eq_rep[label], node))
2445 unify_nodes (graph, graph->eq_rep[label], node, false);
2446 return graph->eq_rep[label];
2448 else
2450 graph->eq_rep[label] = node;
2451 graph->pe_rep[label] = node;
2454 else
2456 gcc_checking_assert (label < graph->size);
2457 graph->pe[node] = label;
2458 if (graph->pe_rep[label] == -1)
2459 graph->pe_rep[label] = node;
2462 return node;
2465 /* Unite pointer equivalent but not location equivalent nodes in
2466 GRAPH. This may only be performed once variable substitution is
2467 finished. */
2469 static void
2470 unite_pointer_equivalences (constraint_graph_t graph)
2472 unsigned int i;
2474 /* Go through the pointer equivalences and unite them to their
2475 representative, if they aren't already. */
2476 for (i = 1; i < FIRST_REF_NODE; i++)
2478 unsigned int label = graph->pe[i];
2479 if (label)
2481 int label_rep = graph->pe_rep[label];
2483 if (label_rep == -1)
2484 continue;
2486 label_rep = find (label_rep);
2487 if (label_rep >= 0 && unite (label_rep, find (i)))
2488 unify_nodes (graph, label_rep, i, false);
2493 /* Move complex constraints to the GRAPH nodes they belong to. */
2495 static void
2496 move_complex_constraints (constraint_graph_t graph)
2498 int i;
2499 constraint_t c;
2501 FOR_EACH_VEC_ELT (constraints, i, c)
2503 if (c)
2505 struct constraint_expr lhs = c->lhs;
2506 struct constraint_expr rhs = c->rhs;
2508 if (lhs.type == DEREF)
2510 insert_into_complex (graph, lhs.var, c);
2512 else if (rhs.type == DEREF)
2514 if (!(get_varinfo (lhs.var)->is_special_var))
2515 insert_into_complex (graph, rhs.var, c);
2517 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2518 && (lhs.offset != 0 || rhs.offset != 0))
2520 insert_into_complex (graph, rhs.var, c);
2527 /* Optimize and rewrite complex constraints while performing
2528 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2529 result of perform_variable_substitution. */
2531 static void
2532 rewrite_constraints (constraint_graph_t graph,
2533 struct scc_info *si)
2535 int i;
2536 constraint_t c;
2538 #ifdef ENABLE_CHECKING
2539 for (unsigned int j = 0; j < graph->size; j++)
2540 gcc_assert (find (j) == j);
2541 #endif
2543 FOR_EACH_VEC_ELT (constraints, i, c)
2545 struct constraint_expr lhs = c->lhs;
2546 struct constraint_expr rhs = c->rhs;
2547 unsigned int lhsvar = find (lhs.var);
2548 unsigned int rhsvar = find (rhs.var);
2549 unsigned int lhsnode, rhsnode;
2550 unsigned int lhslabel, rhslabel;
2552 lhsnode = si->node_mapping[lhsvar];
2553 rhsnode = si->node_mapping[rhsvar];
2554 lhslabel = graph->pointer_label[lhsnode];
2555 rhslabel = graph->pointer_label[rhsnode];
2557 /* See if it is really a non-pointer variable, and if so, ignore
2558 the constraint. */
2559 if (lhslabel == 0)
2561 if (dump_file && (dump_flags & TDF_DETAILS))
2564 fprintf (dump_file, "%s is a non-pointer variable,"
2565 "ignoring constraint:",
2566 get_varinfo (lhs.var)->name);
2567 dump_constraint (dump_file, c);
2568 fprintf (dump_file, "\n");
2570 constraints[i] = NULL;
2571 continue;
2574 if (rhslabel == 0)
2576 if (dump_file && (dump_flags & TDF_DETAILS))
2579 fprintf (dump_file, "%s is a non-pointer variable,"
2580 "ignoring constraint:",
2581 get_varinfo (rhs.var)->name);
2582 dump_constraint (dump_file, c);
2583 fprintf (dump_file, "\n");
2585 constraints[i] = NULL;
2586 continue;
2589 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2590 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2591 c->lhs.var = lhsvar;
2592 c->rhs.var = rhsvar;
2596 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2597 part of an SCC, false otherwise. */
2599 static bool
2600 eliminate_indirect_cycles (unsigned int node)
2602 if (graph->indirect_cycles[node] != -1
2603 && !bitmap_empty_p (get_varinfo (node)->solution))
2605 unsigned int i;
2606 auto_vec<unsigned> queue;
2607 int queuepos;
2608 unsigned int to = find (graph->indirect_cycles[node]);
2609 bitmap_iterator bi;
2611 /* We can't touch the solution set and call unify_nodes
2612 at the same time, because unify_nodes is going to do
2613 bitmap unions into it. */
2615 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2617 if (find (i) == i && i != to)
2619 if (unite (to, i))
2620 queue.safe_push (i);
2624 for (queuepos = 0;
2625 queue.iterate (queuepos, &i);
2626 queuepos++)
2628 unify_nodes (graph, to, i, true);
2630 return true;
2632 return false;
2635 /* Solve the constraint graph GRAPH using our worklist solver.
2636 This is based on the PW* family of solvers from the "Efficient Field
2637 Sensitive Pointer Analysis for C" paper.
2638 It works by iterating over all the graph nodes, processing the complex
2639 constraints and propagating the copy constraints, until everything stops
2640 changed. This corresponds to steps 6-8 in the solving list given above. */
2642 static void
2643 solve_graph (constraint_graph_t graph)
2645 unsigned int size = graph->size;
2646 unsigned int i;
2647 bitmap pts;
2649 changed = BITMAP_ALLOC (NULL);
2651 /* Mark all initial non-collapsed nodes as changed. */
2652 for (i = 1; i < size; i++)
2654 varinfo_t ivi = get_varinfo (i);
2655 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2656 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2657 || graph->complex[i].length () > 0))
2658 bitmap_set_bit (changed, i);
2661 /* Allocate a bitmap to be used to store the changed bits. */
2662 pts = BITMAP_ALLOC (&pta_obstack);
2664 while (!bitmap_empty_p (changed))
2666 unsigned int i;
2667 struct topo_info *ti = init_topo_info ();
2668 stats.iterations++;
2670 bitmap_obstack_initialize (&iteration_obstack);
2672 compute_topo_order (graph, ti);
2674 while (ti->topo_order.length () != 0)
2677 i = ti->topo_order.pop ();
2679 /* If this variable is not a representative, skip it. */
2680 if (find (i) != i)
2681 continue;
2683 /* In certain indirect cycle cases, we may merge this
2684 variable to another. */
2685 if (eliminate_indirect_cycles (i) && find (i) != i)
2686 continue;
2688 /* If the node has changed, we need to process the
2689 complex constraints and outgoing edges again. */
2690 if (bitmap_clear_bit (changed, i))
2692 unsigned int j;
2693 constraint_t c;
2694 bitmap solution;
2695 vec<constraint_t> complex = graph->complex[i];
2696 varinfo_t vi = get_varinfo (i);
2697 bool solution_empty;
2699 /* Compute the changed set of solution bits. If anything
2700 is in the solution just propagate that. */
2701 if (bitmap_bit_p (vi->solution, anything_id))
2703 /* If anything is also in the old solution there is
2704 nothing to do.
2705 ??? But we shouldn't ended up with "changed" set ... */
2706 if (vi->oldsolution
2707 && bitmap_bit_p (vi->oldsolution, anything_id))
2708 continue;
2709 bitmap_copy (pts, get_varinfo (find (anything_id))->solution);
2711 else if (vi->oldsolution)
2712 bitmap_and_compl (pts, vi->solution, vi->oldsolution);
2713 else
2714 bitmap_copy (pts, vi->solution);
2716 if (bitmap_empty_p (pts))
2717 continue;
2719 if (vi->oldsolution)
2720 bitmap_ior_into (vi->oldsolution, pts);
2721 else
2723 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
2724 bitmap_copy (vi->oldsolution, pts);
2727 solution = vi->solution;
2728 solution_empty = bitmap_empty_p (solution);
2730 /* Process the complex constraints */
2731 bitmap expanded_pts = NULL;
2732 FOR_EACH_VEC_ELT (complex, j, c)
2734 /* XXX: This is going to unsort the constraints in
2735 some cases, which will occasionally add duplicate
2736 constraints during unification. This does not
2737 affect correctness. */
2738 c->lhs.var = find (c->lhs.var);
2739 c->rhs.var = find (c->rhs.var);
2741 /* The only complex constraint that can change our
2742 solution to non-empty, given an empty solution,
2743 is a constraint where the lhs side is receiving
2744 some set from elsewhere. */
2745 if (!solution_empty || c->lhs.type != DEREF)
2746 do_complex_constraint (graph, c, pts, &expanded_pts);
2748 BITMAP_FREE (expanded_pts);
2750 solution_empty = bitmap_empty_p (solution);
2752 if (!solution_empty)
2754 bitmap_iterator bi;
2755 unsigned eff_escaped_id = find (escaped_id);
2757 /* Propagate solution to all successors. */
2758 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2759 0, j, bi)
2761 bitmap tmp;
2762 bool flag;
2764 unsigned int to = find (j);
2765 tmp = get_varinfo (to)->solution;
2766 flag = false;
2768 /* Don't try to propagate to ourselves. */
2769 if (to == i)
2770 continue;
2772 /* If we propagate from ESCAPED use ESCAPED as
2773 placeholder. */
2774 if (i == eff_escaped_id)
2775 flag = bitmap_set_bit (tmp, escaped_id);
2776 else
2777 flag = bitmap_ior_into (tmp, pts);
2779 if (flag)
2780 bitmap_set_bit (changed, to);
2785 free_topo_info (ti);
2786 bitmap_obstack_release (&iteration_obstack);
2789 BITMAP_FREE (pts);
2790 BITMAP_FREE (changed);
2791 bitmap_obstack_release (&oldpta_obstack);
2794 /* Map from trees to variable infos. */
2795 static hash_map<tree, varinfo_t> *vi_for_tree;
2798 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2800 static void
2801 insert_vi_for_tree (tree t, varinfo_t vi)
2803 gcc_assert (vi);
2804 gcc_assert (!vi_for_tree->put (t, vi));
2807 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2808 exist in the map, return NULL, otherwise, return the varinfo we found. */
2810 static varinfo_t
2811 lookup_vi_for_tree (tree t)
2813 varinfo_t *slot = vi_for_tree->get (t);
2814 if (slot == NULL)
2815 return NULL;
2817 return *slot;
2820 /* Return a printable name for DECL */
2822 static const char *
2823 alias_get_name (tree decl)
2825 const char *res = NULL;
2826 char *temp;
2827 int num_printed = 0;
2829 if (!dump_file)
2830 return "NULL";
2832 if (TREE_CODE (decl) == SSA_NAME)
2834 res = get_name (decl);
2835 if (res)
2836 num_printed = asprintf (&temp, "%s_%u", res, SSA_NAME_VERSION (decl));
2837 else
2838 num_printed = asprintf (&temp, "_%u", SSA_NAME_VERSION (decl));
2839 if (num_printed > 0)
2841 res = ggc_strdup (temp);
2842 free (temp);
2845 else if (DECL_P (decl))
2847 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2848 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2849 else
2851 res = get_name (decl);
2852 if (!res)
2854 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2855 if (num_printed > 0)
2857 res = ggc_strdup (temp);
2858 free (temp);
2863 if (res != NULL)
2864 return res;
2866 return "NULL";
2869 /* Find the variable id for tree T in the map.
2870 If T doesn't exist in the map, create an entry for it and return it. */
2872 static varinfo_t
2873 get_vi_for_tree (tree t)
2875 varinfo_t *slot = vi_for_tree->get (t);
2876 if (slot == NULL)
2877 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2879 return *slot;
2882 /* Get a scalar constraint expression for a new temporary variable. */
2884 static struct constraint_expr
2885 new_scalar_tmp_constraint_exp (const char *name)
2887 struct constraint_expr tmp;
2888 varinfo_t vi;
2890 vi = new_var_info (NULL_TREE, name);
2891 vi->offset = 0;
2892 vi->size = -1;
2893 vi->fullsize = -1;
2894 vi->is_full_var = 1;
2896 tmp.var = vi->id;
2897 tmp.type = SCALAR;
2898 tmp.offset = 0;
2900 return tmp;
2903 /* Get a constraint expression vector from an SSA_VAR_P node.
2904 If address_p is true, the result will be taken its address of. */
2906 static void
2907 get_constraint_for_ssa_var (tree t, vec<ce_s> *results, bool address_p)
2909 struct constraint_expr cexpr;
2910 varinfo_t vi;
2912 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2913 gcc_assert (TREE_CODE (t) == SSA_NAME || DECL_P (t));
2915 /* For parameters, get at the points-to set for the actual parm
2916 decl. */
2917 if (TREE_CODE (t) == SSA_NAME
2918 && SSA_NAME_IS_DEFAULT_DEF (t)
2919 && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2920 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL))
2922 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2923 return;
2926 /* For global variables resort to the alias target. */
2927 if (TREE_CODE (t) == VAR_DECL
2928 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
2930 varpool_node *node = varpool_node::get (t);
2931 if (node && node->alias && node->analyzed)
2933 node = node->ultimate_alias_target ();
2934 t = node->decl;
2938 vi = get_vi_for_tree (t);
2939 cexpr.var = vi->id;
2940 cexpr.type = SCALAR;
2941 cexpr.offset = 0;
2942 /* If we determine the result is "anything", and we know this is readonly,
2943 say it points to readonly memory instead. */
2944 if (cexpr.var == anything_id && TREE_READONLY (t))
2946 gcc_unreachable ();
2947 cexpr.type = ADDRESSOF;
2948 cexpr.var = readonly_id;
2951 /* If we are not taking the address of the constraint expr, add all
2952 sub-fiels of the variable as well. */
2953 if (!address_p
2954 && !vi->is_full_var)
2956 for (; vi; vi = vi_next (vi))
2958 cexpr.var = vi->id;
2959 results->safe_push (cexpr);
2961 return;
2964 results->safe_push (cexpr);
2967 /* Process constraint T, performing various simplifications and then
2968 adding it to our list of overall constraints. */
2970 static void
2971 process_constraint (constraint_t t)
2973 struct constraint_expr rhs = t->rhs;
2974 struct constraint_expr lhs = t->lhs;
2976 gcc_assert (rhs.var < varmap.length ());
2977 gcc_assert (lhs.var < varmap.length ());
2979 /* If we didn't get any useful constraint from the lhs we get
2980 &ANYTHING as fallback from get_constraint_for. Deal with
2981 it here by turning it into *ANYTHING. */
2982 if (lhs.type == ADDRESSOF
2983 && lhs.var == anything_id)
2984 lhs.type = DEREF;
2986 /* ADDRESSOF on the lhs is invalid. */
2987 gcc_assert (lhs.type != ADDRESSOF);
2989 /* We shouldn't add constraints from things that cannot have pointers.
2990 It's not completely trivial to avoid in the callers, so do it here. */
2991 if (rhs.type != ADDRESSOF
2992 && !get_varinfo (rhs.var)->may_have_pointers)
2993 return;
2995 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2996 if (!get_varinfo (lhs.var)->may_have_pointers)
2997 return;
2999 /* This can happen in our IR with things like n->a = *p */
3000 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
3002 /* Split into tmp = *rhs, *lhs = tmp */
3003 struct constraint_expr tmplhs;
3004 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp");
3005 process_constraint (new_constraint (tmplhs, rhs));
3006 process_constraint (new_constraint (lhs, tmplhs));
3008 else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
3010 /* Split into tmp = &rhs, *lhs = tmp */
3011 struct constraint_expr tmplhs;
3012 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp");
3013 process_constraint (new_constraint (tmplhs, rhs));
3014 process_constraint (new_constraint (lhs, tmplhs));
3016 else
3018 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
3019 constraints.safe_push (t);
3024 /* Return the position, in bits, of FIELD_DECL from the beginning of its
3025 structure. */
3027 static HOST_WIDE_INT
3028 bitpos_of_field (const tree fdecl)
3030 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl))
3031 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl)))
3032 return -1;
3034 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
3035 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl)));
3039 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
3040 resulting constraint expressions in *RESULTS. */
3042 static void
3043 get_constraint_for_ptr_offset (tree ptr, tree offset,
3044 vec<ce_s> *results)
3046 struct constraint_expr c;
3047 unsigned int j, n;
3048 HOST_WIDE_INT rhsoffset;
3050 /* If we do not do field-sensitive PTA adding offsets to pointers
3051 does not change the points-to solution. */
3052 if (!use_field_sensitive)
3054 get_constraint_for_rhs (ptr, results);
3055 return;
3058 /* If the offset is not a non-negative integer constant that fits
3059 in a HOST_WIDE_INT, we have to fall back to a conservative
3060 solution which includes all sub-fields of all pointed-to
3061 variables of ptr. */
3062 if (offset == NULL_TREE
3063 || TREE_CODE (offset) != INTEGER_CST)
3064 rhsoffset = UNKNOWN_OFFSET;
3065 else
3067 /* Sign-extend the offset. */
3068 offset_int soffset = offset_int::from (offset, SIGNED);
3069 if (!wi::fits_shwi_p (soffset))
3070 rhsoffset = UNKNOWN_OFFSET;
3071 else
3073 /* Make sure the bit-offset also fits. */
3074 HOST_WIDE_INT rhsunitoffset = soffset.to_shwi ();
3075 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
3076 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
3077 rhsoffset = UNKNOWN_OFFSET;
3081 get_constraint_for_rhs (ptr, results);
3082 if (rhsoffset == 0)
3083 return;
3085 /* As we are eventually appending to the solution do not use
3086 vec::iterate here. */
3087 n = results->length ();
3088 for (j = 0; j < n; j++)
3090 varinfo_t curr;
3091 c = (*results)[j];
3092 curr = get_varinfo (c.var);
3094 if (c.type == ADDRESSOF
3095 /* If this varinfo represents a full variable just use it. */
3096 && curr->is_full_var)
3098 else if (c.type == ADDRESSOF
3099 /* If we do not know the offset add all subfields. */
3100 && rhsoffset == UNKNOWN_OFFSET)
3102 varinfo_t temp = get_varinfo (curr->head);
3105 struct constraint_expr c2;
3106 c2.var = temp->id;
3107 c2.type = ADDRESSOF;
3108 c2.offset = 0;
3109 if (c2.var != c.var)
3110 results->safe_push (c2);
3111 temp = vi_next (temp);
3113 while (temp);
3115 else if (c.type == ADDRESSOF)
3117 varinfo_t temp;
3118 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
3120 /* If curr->offset + rhsoffset is less than zero adjust it. */
3121 if (rhsoffset < 0
3122 && curr->offset < offset)
3123 offset = 0;
3125 /* We have to include all fields that overlap the current
3126 field shifted by rhsoffset. And we include at least
3127 the last or the first field of the variable to represent
3128 reachability of off-bound addresses, in particular &object + 1,
3129 conservatively correct. */
3130 temp = first_or_preceding_vi_for_offset (curr, offset);
3131 c.var = temp->id;
3132 c.offset = 0;
3133 temp = vi_next (temp);
3134 while (temp
3135 && temp->offset < offset + curr->size)
3137 struct constraint_expr c2;
3138 c2.var = temp->id;
3139 c2.type = ADDRESSOF;
3140 c2.offset = 0;
3141 results->safe_push (c2);
3142 temp = vi_next (temp);
3145 else if (c.type == SCALAR)
3147 gcc_assert (c.offset == 0);
3148 c.offset = rhsoffset;
3150 else
3151 /* We shouldn't get any DEREFs here. */
3152 gcc_unreachable ();
3154 (*results)[j] = c;
3159 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3160 If address_p is true the result will be taken its address of.
3161 If lhs_p is true then the constraint expression is assumed to be used
3162 as the lhs. */
3164 static void
3165 get_constraint_for_component_ref (tree t, vec<ce_s> *results,
3166 bool address_p, bool lhs_p)
3168 tree orig_t = t;
3169 HOST_WIDE_INT bitsize = -1;
3170 HOST_WIDE_INT bitmaxsize = -1;
3171 HOST_WIDE_INT bitpos;
3172 tree forzero;
3174 /* Some people like to do cute things like take the address of
3175 &0->a.b */
3176 forzero = t;
3177 while (handled_component_p (forzero)
3178 || INDIRECT_REF_P (forzero)
3179 || TREE_CODE (forzero) == MEM_REF)
3180 forzero = TREE_OPERAND (forzero, 0);
3182 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
3184 struct constraint_expr temp;
3186 temp.offset = 0;
3187 temp.var = integer_id;
3188 temp.type = SCALAR;
3189 results->safe_push (temp);
3190 return;
3193 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
3195 /* Pretend to take the address of the base, we'll take care of
3196 adding the required subset of sub-fields below. */
3197 get_constraint_for_1 (t, results, true, lhs_p);
3198 gcc_assert (results->length () == 1);
3199 struct constraint_expr &result = results->last ();
3201 if (result.type == SCALAR
3202 && get_varinfo (result.var)->is_full_var)
3203 /* For single-field vars do not bother about the offset. */
3204 result.offset = 0;
3205 else if (result.type == SCALAR)
3207 /* In languages like C, you can access one past the end of an
3208 array. You aren't allowed to dereference it, so we can
3209 ignore this constraint. When we handle pointer subtraction,
3210 we may have to do something cute here. */
3212 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result.var)->fullsize
3213 && bitmaxsize != 0)
3215 /* It's also not true that the constraint will actually start at the
3216 right offset, it may start in some padding. We only care about
3217 setting the constraint to the first actual field it touches, so
3218 walk to find it. */
3219 struct constraint_expr cexpr = result;
3220 varinfo_t curr;
3221 results->pop ();
3222 cexpr.offset = 0;
3223 for (curr = get_varinfo (cexpr.var); curr; curr = vi_next (curr))
3225 if (ranges_overlap_p (curr->offset, curr->size,
3226 bitpos, bitmaxsize))
3228 cexpr.var = curr->id;
3229 results->safe_push (cexpr);
3230 if (address_p)
3231 break;
3234 /* If we are going to take the address of this field then
3235 to be able to compute reachability correctly add at least
3236 the last field of the variable. */
3237 if (address_p && results->length () == 0)
3239 curr = get_varinfo (cexpr.var);
3240 while (curr->next != 0)
3241 curr = vi_next (curr);
3242 cexpr.var = curr->id;
3243 results->safe_push (cexpr);
3245 else if (results->length () == 0)
3246 /* Assert that we found *some* field there. The user couldn't be
3247 accessing *only* padding. */
3248 /* Still the user could access one past the end of an array
3249 embedded in a struct resulting in accessing *only* padding. */
3250 /* Or accessing only padding via type-punning to a type
3251 that has a filed just in padding space. */
3253 cexpr.type = SCALAR;
3254 cexpr.var = anything_id;
3255 cexpr.offset = 0;
3256 results->safe_push (cexpr);
3259 else if (bitmaxsize == 0)
3261 if (dump_file && (dump_flags & TDF_DETAILS))
3262 fprintf (dump_file, "Access to zero-sized part of variable,"
3263 "ignoring\n");
3265 else
3266 if (dump_file && (dump_flags & TDF_DETAILS))
3267 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3269 else if (result.type == DEREF)
3271 /* If we do not know exactly where the access goes say so. Note
3272 that only for non-structure accesses we know that we access
3273 at most one subfiled of any variable. */
3274 if (bitpos == -1
3275 || bitsize != bitmaxsize
3276 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
3277 || result.offset == UNKNOWN_OFFSET)
3278 result.offset = UNKNOWN_OFFSET;
3279 else
3280 result.offset += bitpos;
3282 else if (result.type == ADDRESSOF)
3284 /* We can end up here for component references on a
3285 VIEW_CONVERT_EXPR <>(&foobar). */
3286 result.type = SCALAR;
3287 result.var = anything_id;
3288 result.offset = 0;
3290 else
3291 gcc_unreachable ();
3295 /* Dereference the constraint expression CONS, and return the result.
3296 DEREF (ADDRESSOF) = SCALAR
3297 DEREF (SCALAR) = DEREF
3298 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3299 This is needed so that we can handle dereferencing DEREF constraints. */
3301 static void
3302 do_deref (vec<ce_s> *constraints)
3304 struct constraint_expr *c;
3305 unsigned int i = 0;
3307 FOR_EACH_VEC_ELT (*constraints, i, c)
3309 if (c->type == SCALAR)
3310 c->type = DEREF;
3311 else if (c->type == ADDRESSOF)
3312 c->type = SCALAR;
3313 else if (c->type == DEREF)
3315 struct constraint_expr tmplhs;
3316 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp");
3317 process_constraint (new_constraint (tmplhs, *c));
3318 c->var = tmplhs.var;
3320 else
3321 gcc_unreachable ();
3325 /* Given a tree T, return the constraint expression for taking the
3326 address of it. */
3328 static void
3329 get_constraint_for_address_of (tree t, vec<ce_s> *results)
3331 struct constraint_expr *c;
3332 unsigned int i;
3334 get_constraint_for_1 (t, results, true, true);
3336 FOR_EACH_VEC_ELT (*results, i, c)
3338 if (c->type == DEREF)
3339 c->type = SCALAR;
3340 else
3341 c->type = ADDRESSOF;
3345 /* Given a tree T, return the constraint expression for it. */
3347 static void
3348 get_constraint_for_1 (tree t, vec<ce_s> *results, bool address_p,
3349 bool lhs_p)
3351 struct constraint_expr temp;
3353 /* x = integer is all glommed to a single variable, which doesn't
3354 point to anything by itself. That is, of course, unless it is an
3355 integer constant being treated as a pointer, in which case, we
3356 will return that this is really the addressof anything. This
3357 happens below, since it will fall into the default case. The only
3358 case we know something about an integer treated like a pointer is
3359 when it is the NULL pointer, and then we just say it points to
3360 NULL.
3362 Do not do that if -fno-delete-null-pointer-checks though, because
3363 in that case *NULL does not fail, so it _should_ alias *anything.
3364 It is not worth adding a new option or renaming the existing one,
3365 since this case is relatively obscure. */
3366 if ((TREE_CODE (t) == INTEGER_CST
3367 && integer_zerop (t))
3368 /* The only valid CONSTRUCTORs in gimple with pointer typed
3369 elements are zero-initializer. But in IPA mode we also
3370 process global initializers, so verify at least. */
3371 || (TREE_CODE (t) == CONSTRUCTOR
3372 && CONSTRUCTOR_NELTS (t) == 0))
3374 if (flag_delete_null_pointer_checks)
3375 temp.var = nothing_id;
3376 else
3377 temp.var = nonlocal_id;
3378 temp.type = ADDRESSOF;
3379 temp.offset = 0;
3380 results->safe_push (temp);
3381 return;
3384 /* String constants are read-only. */
3385 if (TREE_CODE (t) == STRING_CST)
3387 temp.var = readonly_id;
3388 temp.type = SCALAR;
3389 temp.offset = 0;
3390 results->safe_push (temp);
3391 return;
3394 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3396 case tcc_expression:
3398 switch (TREE_CODE (t))
3400 case ADDR_EXPR:
3401 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3402 return;
3403 default:;
3405 break;
3407 case tcc_reference:
3409 switch (TREE_CODE (t))
3411 case MEM_REF:
3413 struct constraint_expr cs;
3414 varinfo_t vi, curr;
3415 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0),
3416 TREE_OPERAND (t, 1), results);
3417 do_deref (results);
3419 /* If we are not taking the address then make sure to process
3420 all subvariables we might access. */
3421 if (address_p)
3422 return;
3424 cs = results->last ();
3425 if (cs.type == DEREF
3426 && type_can_have_subvars (TREE_TYPE (t)))
3428 /* For dereferences this means we have to defer it
3429 to solving time. */
3430 results->last ().offset = UNKNOWN_OFFSET;
3431 return;
3433 if (cs.type != SCALAR)
3434 return;
3436 vi = get_varinfo (cs.var);
3437 curr = vi_next (vi);
3438 if (!vi->is_full_var
3439 && curr)
3441 unsigned HOST_WIDE_INT size;
3442 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t))))
3443 size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t)));
3444 else
3445 size = -1;
3446 for (; curr; curr = vi_next (curr))
3448 if (curr->offset - vi->offset < size)
3450 cs.var = curr->id;
3451 results->safe_push (cs);
3453 else
3454 break;
3457 return;
3459 case ARRAY_REF:
3460 case ARRAY_RANGE_REF:
3461 case COMPONENT_REF:
3462 get_constraint_for_component_ref (t, results, address_p, lhs_p);
3463 return;
3464 case VIEW_CONVERT_EXPR:
3465 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p,
3466 lhs_p);
3467 return;
3468 /* We are missing handling for TARGET_MEM_REF here. */
3469 default:;
3471 break;
3473 case tcc_exceptional:
3475 switch (TREE_CODE (t))
3477 case SSA_NAME:
3479 get_constraint_for_ssa_var (t, results, address_p);
3480 return;
3482 case CONSTRUCTOR:
3484 unsigned int i;
3485 tree val;
3486 auto_vec<ce_s> tmp;
3487 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
3489 struct constraint_expr *rhsp;
3490 unsigned j;
3491 get_constraint_for_1 (val, &tmp, address_p, lhs_p);
3492 FOR_EACH_VEC_ELT (tmp, j, rhsp)
3493 results->safe_push (*rhsp);
3494 tmp.truncate (0);
3496 /* We do not know whether the constructor was complete,
3497 so technically we have to add &NOTHING or &ANYTHING
3498 like we do for an empty constructor as well. */
3499 return;
3501 default:;
3503 break;
3505 case tcc_declaration:
3507 get_constraint_for_ssa_var (t, results, address_p);
3508 return;
3510 case tcc_constant:
3512 /* We cannot refer to automatic variables through constants. */
3513 temp.type = ADDRESSOF;
3514 temp.var = nonlocal_id;
3515 temp.offset = 0;
3516 results->safe_push (temp);
3517 return;
3519 default:;
3522 /* The default fallback is a constraint from anything. */
3523 temp.type = ADDRESSOF;
3524 temp.var = anything_id;
3525 temp.offset = 0;
3526 results->safe_push (temp);
3529 /* Given a gimple tree T, return the constraint expression vector for it. */
3531 static void
3532 get_constraint_for (tree t, vec<ce_s> *results)
3534 gcc_assert (results->length () == 0);
3536 get_constraint_for_1 (t, results, false, true);
3539 /* Given a gimple tree T, return the constraint expression vector for it
3540 to be used as the rhs of a constraint. */
3542 static void
3543 get_constraint_for_rhs (tree t, vec<ce_s> *results)
3545 gcc_assert (results->length () == 0);
3547 get_constraint_for_1 (t, results, false, false);
3551 /* Efficiently generates constraints from all entries in *RHSC to all
3552 entries in *LHSC. */
3554 static void
3555 process_all_all_constraints (vec<ce_s> lhsc,
3556 vec<ce_s> rhsc)
3558 struct constraint_expr *lhsp, *rhsp;
3559 unsigned i, j;
3561 if (lhsc.length () <= 1 || rhsc.length () <= 1)
3563 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3564 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
3565 process_constraint (new_constraint (*lhsp, *rhsp));
3567 else
3569 struct constraint_expr tmp;
3570 tmp = new_scalar_tmp_constraint_exp ("allalltmp");
3571 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
3572 process_constraint (new_constraint (tmp, *rhsp));
3573 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3574 process_constraint (new_constraint (*lhsp, tmp));
3578 /* Handle aggregate copies by expanding into copies of the respective
3579 fields of the structures. */
3581 static void
3582 do_structure_copy (tree lhsop, tree rhsop)
3584 struct constraint_expr *lhsp, *rhsp;
3585 auto_vec<ce_s> lhsc;
3586 auto_vec<ce_s> rhsc;
3587 unsigned j;
3589 get_constraint_for (lhsop, &lhsc);
3590 get_constraint_for_rhs (rhsop, &rhsc);
3591 lhsp = &lhsc[0];
3592 rhsp = &rhsc[0];
3593 if (lhsp->type == DEREF
3594 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3595 || rhsp->type == DEREF)
3597 if (lhsp->type == DEREF)
3599 gcc_assert (lhsc.length () == 1);
3600 lhsp->offset = UNKNOWN_OFFSET;
3602 if (rhsp->type == DEREF)
3604 gcc_assert (rhsc.length () == 1);
3605 rhsp->offset = UNKNOWN_OFFSET;
3607 process_all_all_constraints (lhsc, rhsc);
3609 else if (lhsp->type == SCALAR
3610 && (rhsp->type == SCALAR
3611 || rhsp->type == ADDRESSOF))
3613 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3614 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
3615 unsigned k = 0;
3616 get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize);
3617 get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize);
3618 for (j = 0; lhsc.iterate (j, &lhsp);)
3620 varinfo_t lhsv, rhsv;
3621 rhsp = &rhsc[k];
3622 lhsv = get_varinfo (lhsp->var);
3623 rhsv = get_varinfo (rhsp->var);
3624 if (lhsv->may_have_pointers
3625 && (lhsv->is_full_var
3626 || rhsv->is_full_var
3627 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3628 rhsv->offset + lhsoffset, rhsv->size)))
3629 process_constraint (new_constraint (*lhsp, *rhsp));
3630 if (!rhsv->is_full_var
3631 && (lhsv->is_full_var
3632 || (lhsv->offset + rhsoffset + lhsv->size
3633 > rhsv->offset + lhsoffset + rhsv->size)))
3635 ++k;
3636 if (k >= rhsc.length ())
3637 break;
3639 else
3640 ++j;
3643 else
3644 gcc_unreachable ();
3647 /* Create constraints ID = { rhsc }. */
3649 static void
3650 make_constraints_to (unsigned id, vec<ce_s> rhsc)
3652 struct constraint_expr *c;
3653 struct constraint_expr includes;
3654 unsigned int j;
3656 includes.var = id;
3657 includes.offset = 0;
3658 includes.type = SCALAR;
3660 FOR_EACH_VEC_ELT (rhsc, j, c)
3661 process_constraint (new_constraint (includes, *c));
3664 /* Create a constraint ID = OP. */
3666 static void
3667 make_constraint_to (unsigned id, tree op)
3669 auto_vec<ce_s> rhsc;
3670 get_constraint_for_rhs (op, &rhsc);
3671 make_constraints_to (id, rhsc);
3674 /* Create a constraint ID = &FROM. */
3676 static void
3677 make_constraint_from (varinfo_t vi, int from)
3679 struct constraint_expr lhs, rhs;
3681 lhs.var = vi->id;
3682 lhs.offset = 0;
3683 lhs.type = SCALAR;
3685 rhs.var = from;
3686 rhs.offset = 0;
3687 rhs.type = ADDRESSOF;
3688 process_constraint (new_constraint (lhs, rhs));
3691 /* Create a constraint ID = FROM. */
3693 static void
3694 make_copy_constraint (varinfo_t vi, int from)
3696 struct constraint_expr lhs, rhs;
3698 lhs.var = vi->id;
3699 lhs.offset = 0;
3700 lhs.type = SCALAR;
3702 rhs.var = from;
3703 rhs.offset = 0;
3704 rhs.type = SCALAR;
3705 process_constraint (new_constraint (lhs, rhs));
3708 /* Make constraints necessary to make OP escape. */
3710 static void
3711 make_escape_constraint (tree op)
3713 make_constraint_to (escaped_id, op);
3716 /* Add constraints to that the solution of VI is transitively closed. */
3718 static void
3719 make_transitive_closure_constraints (varinfo_t vi)
3721 struct constraint_expr lhs, rhs;
3723 /* VAR = *VAR; */
3724 lhs.type = SCALAR;
3725 lhs.var = vi->id;
3726 lhs.offset = 0;
3727 rhs.type = DEREF;
3728 rhs.var = vi->id;
3729 rhs.offset = UNKNOWN_OFFSET;
3730 process_constraint (new_constraint (lhs, rhs));
3733 /* Temporary storage for fake var decls. */
3734 struct obstack fake_var_decl_obstack;
3736 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3738 static tree
3739 build_fake_var_decl (tree type)
3741 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl);
3742 memset (decl, 0, sizeof (struct tree_var_decl));
3743 TREE_SET_CODE (decl, VAR_DECL);
3744 TREE_TYPE (decl) = type;
3745 DECL_UID (decl) = allocate_decl_uid ();
3746 SET_DECL_PT_UID (decl, -1);
3747 layout_decl (decl, 0);
3748 return decl;
3751 /* Create a new artificial heap variable with NAME.
3752 Return the created variable. */
3754 static varinfo_t
3755 make_heapvar (const char *name)
3757 varinfo_t vi;
3758 tree heapvar;
3760 heapvar = build_fake_var_decl (ptr_type_node);
3761 DECL_EXTERNAL (heapvar) = 1;
3763 vi = new_var_info (heapvar, name);
3764 vi->is_artificial_var = true;
3765 vi->is_heap_var = true;
3766 vi->is_unknown_size_var = true;
3767 vi->offset = 0;
3768 vi->fullsize = ~0;
3769 vi->size = ~0;
3770 vi->is_full_var = true;
3771 insert_vi_for_tree (heapvar, vi);
3773 return vi;
3776 /* Create a new artificial heap variable with NAME and make a
3777 constraint from it to LHS. Set flags according to a tag used
3778 for tracking restrict pointers. */
3780 static varinfo_t
3781 make_constraint_from_restrict (varinfo_t lhs, const char *name)
3783 varinfo_t vi = make_heapvar (name);
3784 vi->is_global_var = 1;
3785 vi->may_have_pointers = 1;
3786 make_constraint_from (lhs, vi->id);
3787 return vi;
3790 /* Create a new artificial heap variable with NAME and make a
3791 constraint from it to LHS. Set flags according to a tag used
3792 for tracking restrict pointers and make the artificial heap
3793 point to global memory. */
3795 static varinfo_t
3796 make_constraint_from_global_restrict (varinfo_t lhs, const char *name)
3798 varinfo_t vi = make_constraint_from_restrict (lhs, name);
3799 make_copy_constraint (vi, nonlocal_id);
3800 return vi;
3803 /* In IPA mode there are varinfos for different aspects of reach
3804 function designator. One for the points-to set of the return
3805 value, one for the variables that are clobbered by the function,
3806 one for its uses and one for each parameter (including a single
3807 glob for remaining variadic arguments). */
3809 enum { fi_clobbers = 1, fi_uses = 2,
3810 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 };
3812 /* Get a constraint for the requested part of a function designator FI
3813 when operating in IPA mode. */
3815 static struct constraint_expr
3816 get_function_part_constraint (varinfo_t fi, unsigned part)
3818 struct constraint_expr c;
3820 gcc_assert (in_ipa_mode);
3822 if (fi->id == anything_id)
3824 /* ??? We probably should have a ANYFN special variable. */
3825 c.var = anything_id;
3826 c.offset = 0;
3827 c.type = SCALAR;
3829 else if (TREE_CODE (fi->decl) == FUNCTION_DECL)
3831 varinfo_t ai = first_vi_for_offset (fi, part);
3832 if (ai)
3833 c.var = ai->id;
3834 else
3835 c.var = anything_id;
3836 c.offset = 0;
3837 c.type = SCALAR;
3839 else
3841 c.var = fi->id;
3842 c.offset = part;
3843 c.type = DEREF;
3846 return c;
3849 /* For non-IPA mode, generate constraints necessary for a call on the
3850 RHS. */
3852 static void
3853 handle_rhs_call (gimple stmt, vec<ce_s> *results)
3855 struct constraint_expr rhsc;
3856 unsigned i;
3857 bool returns_uses = false;
3859 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3861 tree arg = gimple_call_arg (stmt, i);
3862 int flags = gimple_call_arg_flags (stmt, i);
3864 /* If the argument is not used we can ignore it. */
3865 if (flags & EAF_UNUSED)
3866 continue;
3868 /* As we compute ESCAPED context-insensitive we do not gain
3869 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3870 set. The argument would still get clobbered through the
3871 escape solution. */
3872 if ((flags & EAF_NOCLOBBER)
3873 && (flags & EAF_NOESCAPE))
3875 varinfo_t uses = get_call_use_vi (stmt);
3876 if (!(flags & EAF_DIRECT))
3878 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3879 make_constraint_to (tem->id, arg);
3880 make_transitive_closure_constraints (tem);
3881 make_copy_constraint (uses, tem->id);
3883 else
3884 make_constraint_to (uses->id, arg);
3885 returns_uses = true;
3887 else if (flags & EAF_NOESCAPE)
3889 struct constraint_expr lhs, rhs;
3890 varinfo_t uses = get_call_use_vi (stmt);
3891 varinfo_t clobbers = get_call_clobber_vi (stmt);
3892 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3893 make_constraint_to (tem->id, arg);
3894 if (!(flags & EAF_DIRECT))
3895 make_transitive_closure_constraints (tem);
3896 make_copy_constraint (uses, tem->id);
3897 make_copy_constraint (clobbers, tem->id);
3898 /* Add *tem = nonlocal, do not add *tem = callused as
3899 EAF_NOESCAPE parameters do not escape to other parameters
3900 and all other uses appear in NONLOCAL as well. */
3901 lhs.type = DEREF;
3902 lhs.var = tem->id;
3903 lhs.offset = 0;
3904 rhs.type = SCALAR;
3905 rhs.var = nonlocal_id;
3906 rhs.offset = 0;
3907 process_constraint (new_constraint (lhs, rhs));
3908 returns_uses = true;
3910 else
3911 make_escape_constraint (arg);
3914 /* If we added to the calls uses solution make sure we account for
3915 pointers to it to be returned. */
3916 if (returns_uses)
3918 rhsc.var = get_call_use_vi (stmt)->id;
3919 rhsc.offset = 0;
3920 rhsc.type = SCALAR;
3921 results->safe_push (rhsc);
3924 /* The static chain escapes as well. */
3925 if (gimple_call_chain (stmt))
3926 make_escape_constraint (gimple_call_chain (stmt));
3928 /* And if we applied NRV the address of the return slot escapes as well. */
3929 if (gimple_call_return_slot_opt_p (stmt)
3930 && gimple_call_lhs (stmt) != NULL_TREE
3931 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3933 auto_vec<ce_s> tmpc;
3934 struct constraint_expr lhsc, *c;
3935 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3936 lhsc.var = escaped_id;
3937 lhsc.offset = 0;
3938 lhsc.type = SCALAR;
3939 FOR_EACH_VEC_ELT (tmpc, i, c)
3940 process_constraint (new_constraint (lhsc, *c));
3943 /* Regular functions return nonlocal memory. */
3944 rhsc.var = nonlocal_id;
3945 rhsc.offset = 0;
3946 rhsc.type = SCALAR;
3947 results->safe_push (rhsc);
3950 /* For non-IPA mode, generate constraints necessary for a call
3951 that returns a pointer and assigns it to LHS. This simply makes
3952 the LHS point to global and escaped variables. */
3954 static void
3955 handle_lhs_call (gimple stmt, tree lhs, int flags, vec<ce_s> rhsc,
3956 tree fndecl)
3958 auto_vec<ce_s> lhsc;
3960 get_constraint_for (lhs, &lhsc);
3961 /* If the store is to a global decl make sure to
3962 add proper escape constraints. */
3963 lhs = get_base_address (lhs);
3964 if (lhs
3965 && DECL_P (lhs)
3966 && is_global_var (lhs))
3968 struct constraint_expr tmpc;
3969 tmpc.var = escaped_id;
3970 tmpc.offset = 0;
3971 tmpc.type = SCALAR;
3972 lhsc.safe_push (tmpc);
3975 /* If the call returns an argument unmodified override the rhs
3976 constraints. */
3977 if (flags & ERF_RETURNS_ARG
3978 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
3980 tree arg;
3981 rhsc.create (0);
3982 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
3983 get_constraint_for (arg, &rhsc);
3984 process_all_all_constraints (lhsc, rhsc);
3985 rhsc.release ();
3987 else if (flags & ERF_NOALIAS)
3989 varinfo_t vi;
3990 struct constraint_expr tmpc;
3991 rhsc.create (0);
3992 vi = make_heapvar ("HEAP");
3993 /* We are marking allocated storage local, we deal with it becoming
3994 global by escaping and setting of vars_contains_escaped_heap. */
3995 DECL_EXTERNAL (vi->decl) = 0;
3996 vi->is_global_var = 0;
3997 /* If this is not a real malloc call assume the memory was
3998 initialized and thus may point to global memory. All
3999 builtin functions with the malloc attribute behave in a sane way. */
4000 if (!fndecl
4001 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
4002 make_constraint_from (vi, nonlocal_id);
4003 tmpc.var = vi->id;
4004 tmpc.offset = 0;
4005 tmpc.type = ADDRESSOF;
4006 rhsc.safe_push (tmpc);
4007 process_all_all_constraints (lhsc, rhsc);
4008 rhsc.release ();
4010 else
4011 process_all_all_constraints (lhsc, rhsc);
4014 /* For non-IPA mode, generate constraints necessary for a call of a
4015 const function that returns a pointer in the statement STMT. */
4017 static void
4018 handle_const_call (gimple stmt, vec<ce_s> *results)
4020 struct constraint_expr rhsc;
4021 unsigned int k;
4023 /* Treat nested const functions the same as pure functions as far
4024 as the static chain is concerned. */
4025 if (gimple_call_chain (stmt))
4027 varinfo_t uses = get_call_use_vi (stmt);
4028 make_transitive_closure_constraints (uses);
4029 make_constraint_to (uses->id, gimple_call_chain (stmt));
4030 rhsc.var = uses->id;
4031 rhsc.offset = 0;
4032 rhsc.type = SCALAR;
4033 results->safe_push (rhsc);
4036 /* May return arguments. */
4037 for (k = 0; k < gimple_call_num_args (stmt); ++k)
4039 tree arg = gimple_call_arg (stmt, k);
4040 auto_vec<ce_s> argc;
4041 unsigned i;
4042 struct constraint_expr *argp;
4043 get_constraint_for_rhs (arg, &argc);
4044 FOR_EACH_VEC_ELT (argc, i, argp)
4045 results->safe_push (*argp);
4048 /* May return addresses of globals. */
4049 rhsc.var = nonlocal_id;
4050 rhsc.offset = 0;
4051 rhsc.type = ADDRESSOF;
4052 results->safe_push (rhsc);
4055 /* For non-IPA mode, generate constraints necessary for a call to a
4056 pure function in statement STMT. */
4058 static void
4059 handle_pure_call (gimple stmt, vec<ce_s> *results)
4061 struct constraint_expr rhsc;
4062 unsigned i;
4063 varinfo_t uses = NULL;
4065 /* Memory reached from pointer arguments is call-used. */
4066 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4068 tree arg = gimple_call_arg (stmt, i);
4069 if (!uses)
4071 uses = get_call_use_vi (stmt);
4072 make_transitive_closure_constraints (uses);
4074 make_constraint_to (uses->id, arg);
4077 /* The static chain is used as well. */
4078 if (gimple_call_chain (stmt))
4080 if (!uses)
4082 uses = get_call_use_vi (stmt);
4083 make_transitive_closure_constraints (uses);
4085 make_constraint_to (uses->id, gimple_call_chain (stmt));
4088 /* Pure functions may return call-used and nonlocal memory. */
4089 if (uses)
4091 rhsc.var = uses->id;
4092 rhsc.offset = 0;
4093 rhsc.type = SCALAR;
4094 results->safe_push (rhsc);
4096 rhsc.var = nonlocal_id;
4097 rhsc.offset = 0;
4098 rhsc.type = SCALAR;
4099 results->safe_push (rhsc);
4103 /* Return the varinfo for the callee of CALL. */
4105 static varinfo_t
4106 get_fi_for_callee (gimple call)
4108 tree decl, fn = gimple_call_fn (call);
4110 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
4111 fn = OBJ_TYPE_REF_EXPR (fn);
4113 /* If we can directly resolve the function being called, do so.
4114 Otherwise, it must be some sort of indirect expression that
4115 we should still be able to handle. */
4116 decl = gimple_call_addr_fndecl (fn);
4117 if (decl)
4118 return get_vi_for_tree (decl);
4120 /* If the function is anything other than a SSA name pointer we have no
4121 clue and should be getting ANYFN (well, ANYTHING for now). */
4122 if (!fn || TREE_CODE (fn) != SSA_NAME)
4123 return get_varinfo (anything_id);
4125 if (SSA_NAME_IS_DEFAULT_DEF (fn)
4126 && (TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
4127 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL))
4128 fn = SSA_NAME_VAR (fn);
4130 return get_vi_for_tree (fn);
4133 /* Create constraints for the builtin call T. Return true if the call
4134 was handled, otherwise false. */
4136 static bool
4137 find_func_aliases_for_builtin_call (struct function *fn, gimple t)
4139 tree fndecl = gimple_call_fndecl (t);
4140 vec<ce_s> lhsc = vNULL;
4141 vec<ce_s> rhsc = vNULL;
4142 varinfo_t fi;
4144 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4145 /* ??? All builtins that are handled here need to be handled
4146 in the alias-oracle query functions explicitly! */
4147 switch (DECL_FUNCTION_CODE (fndecl))
4149 /* All the following functions return a pointer to the same object
4150 as their first argument points to. The functions do not add
4151 to the ESCAPED solution. The functions make the first argument
4152 pointed to memory point to what the second argument pointed to
4153 memory points to. */
4154 case BUILT_IN_STRCPY:
4155 case BUILT_IN_STRNCPY:
4156 case BUILT_IN_BCOPY:
4157 case BUILT_IN_MEMCPY:
4158 case BUILT_IN_MEMMOVE:
4159 case BUILT_IN_MEMPCPY:
4160 case BUILT_IN_STPCPY:
4161 case BUILT_IN_STPNCPY:
4162 case BUILT_IN_STRCAT:
4163 case BUILT_IN_STRNCAT:
4164 case BUILT_IN_STRCPY_CHK:
4165 case BUILT_IN_STRNCPY_CHK:
4166 case BUILT_IN_MEMCPY_CHK:
4167 case BUILT_IN_MEMMOVE_CHK:
4168 case BUILT_IN_MEMPCPY_CHK:
4169 case BUILT_IN_STPCPY_CHK:
4170 case BUILT_IN_STPNCPY_CHK:
4171 case BUILT_IN_STRCAT_CHK:
4172 case BUILT_IN_STRNCAT_CHK:
4173 case BUILT_IN_TM_MEMCPY:
4174 case BUILT_IN_TM_MEMMOVE:
4176 tree res = gimple_call_lhs (t);
4177 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4178 == BUILT_IN_BCOPY ? 1 : 0));
4179 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4180 == BUILT_IN_BCOPY ? 0 : 1));
4181 if (res != NULL_TREE)
4183 get_constraint_for (res, &lhsc);
4184 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4185 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
4186 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4187 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
4188 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK
4189 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK)
4190 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4191 else
4192 get_constraint_for (dest, &rhsc);
4193 process_all_all_constraints (lhsc, rhsc);
4194 lhsc.release ();
4195 rhsc.release ();
4197 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4198 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4199 do_deref (&lhsc);
4200 do_deref (&rhsc);
4201 process_all_all_constraints (lhsc, rhsc);
4202 lhsc.release ();
4203 rhsc.release ();
4204 return true;
4206 case BUILT_IN_MEMSET:
4207 case BUILT_IN_MEMSET_CHK:
4208 case BUILT_IN_TM_MEMSET:
4210 tree res = gimple_call_lhs (t);
4211 tree dest = gimple_call_arg (t, 0);
4212 unsigned i;
4213 ce_s *lhsp;
4214 struct constraint_expr ac;
4215 if (res != NULL_TREE)
4217 get_constraint_for (res, &lhsc);
4218 get_constraint_for (dest, &rhsc);
4219 process_all_all_constraints (lhsc, rhsc);
4220 lhsc.release ();
4221 rhsc.release ();
4223 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4224 do_deref (&lhsc);
4225 if (flag_delete_null_pointer_checks
4226 && integer_zerop (gimple_call_arg (t, 1)))
4228 ac.type = ADDRESSOF;
4229 ac.var = nothing_id;
4231 else
4233 ac.type = SCALAR;
4234 ac.var = integer_id;
4236 ac.offset = 0;
4237 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4238 process_constraint (new_constraint (*lhsp, ac));
4239 lhsc.release ();
4240 return true;
4242 case BUILT_IN_POSIX_MEMALIGN:
4244 tree ptrptr = gimple_call_arg (t, 0);
4245 get_constraint_for (ptrptr, &lhsc);
4246 do_deref (&lhsc);
4247 varinfo_t vi = make_heapvar ("HEAP");
4248 /* We are marking allocated storage local, we deal with it becoming
4249 global by escaping and setting of vars_contains_escaped_heap. */
4250 DECL_EXTERNAL (vi->decl) = 0;
4251 vi->is_global_var = 0;
4252 struct constraint_expr tmpc;
4253 tmpc.var = vi->id;
4254 tmpc.offset = 0;
4255 tmpc.type = ADDRESSOF;
4256 rhsc.safe_push (tmpc);
4257 process_all_all_constraints (lhsc, rhsc);
4258 lhsc.release ();
4259 rhsc.release ();
4260 return true;
4262 case BUILT_IN_ASSUME_ALIGNED:
4264 tree res = gimple_call_lhs (t);
4265 tree dest = gimple_call_arg (t, 0);
4266 if (res != NULL_TREE)
4268 get_constraint_for (res, &lhsc);
4269 get_constraint_for (dest, &rhsc);
4270 process_all_all_constraints (lhsc, rhsc);
4271 lhsc.release ();
4272 rhsc.release ();
4274 return true;
4276 /* All the following functions do not return pointers, do not
4277 modify the points-to sets of memory reachable from their
4278 arguments and do not add to the ESCAPED solution. */
4279 case BUILT_IN_SINCOS:
4280 case BUILT_IN_SINCOSF:
4281 case BUILT_IN_SINCOSL:
4282 case BUILT_IN_FREXP:
4283 case BUILT_IN_FREXPF:
4284 case BUILT_IN_FREXPL:
4285 case BUILT_IN_GAMMA_R:
4286 case BUILT_IN_GAMMAF_R:
4287 case BUILT_IN_GAMMAL_R:
4288 case BUILT_IN_LGAMMA_R:
4289 case BUILT_IN_LGAMMAF_R:
4290 case BUILT_IN_LGAMMAL_R:
4291 case BUILT_IN_MODF:
4292 case BUILT_IN_MODFF:
4293 case BUILT_IN_MODFL:
4294 case BUILT_IN_REMQUO:
4295 case BUILT_IN_REMQUOF:
4296 case BUILT_IN_REMQUOL:
4297 case BUILT_IN_FREE:
4298 return true;
4299 case BUILT_IN_STRDUP:
4300 case BUILT_IN_STRNDUP:
4301 case BUILT_IN_REALLOC:
4302 if (gimple_call_lhs (t))
4304 handle_lhs_call (t, gimple_call_lhs (t),
4305 gimple_call_return_flags (t) | ERF_NOALIAS,
4306 vNULL, fndecl);
4307 get_constraint_for_ptr_offset (gimple_call_lhs (t),
4308 NULL_TREE, &lhsc);
4309 get_constraint_for_ptr_offset (gimple_call_arg (t, 0),
4310 NULL_TREE, &rhsc);
4311 do_deref (&lhsc);
4312 do_deref (&rhsc);
4313 process_all_all_constraints (lhsc, rhsc);
4314 lhsc.release ();
4315 rhsc.release ();
4316 /* For realloc the resulting pointer can be equal to the
4317 argument as well. But only doing this wouldn't be
4318 correct because with ptr == 0 realloc behaves like malloc. */
4319 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_REALLOC)
4321 get_constraint_for (gimple_call_lhs (t), &lhsc);
4322 get_constraint_for (gimple_call_arg (t, 0), &rhsc);
4323 process_all_all_constraints (lhsc, rhsc);
4324 lhsc.release ();
4325 rhsc.release ();
4327 return true;
4329 break;
4330 /* String / character search functions return a pointer into the
4331 source string or NULL. */
4332 case BUILT_IN_INDEX:
4333 case BUILT_IN_STRCHR:
4334 case BUILT_IN_STRRCHR:
4335 case BUILT_IN_MEMCHR:
4336 case BUILT_IN_STRSTR:
4337 case BUILT_IN_STRPBRK:
4338 if (gimple_call_lhs (t))
4340 tree src = gimple_call_arg (t, 0);
4341 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4342 constraint_expr nul;
4343 nul.var = nothing_id;
4344 nul.offset = 0;
4345 nul.type = ADDRESSOF;
4346 rhsc.safe_push (nul);
4347 get_constraint_for (gimple_call_lhs (t), &lhsc);
4348 process_all_all_constraints (lhsc, rhsc);
4349 lhsc.release ();
4350 rhsc.release ();
4352 return true;
4353 /* Trampolines are special - they set up passing the static
4354 frame. */
4355 case BUILT_IN_INIT_TRAMPOLINE:
4357 tree tramp = gimple_call_arg (t, 0);
4358 tree nfunc = gimple_call_arg (t, 1);
4359 tree frame = gimple_call_arg (t, 2);
4360 unsigned i;
4361 struct constraint_expr lhs, *rhsp;
4362 if (in_ipa_mode)
4364 varinfo_t nfi = NULL;
4365 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR);
4366 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0));
4367 if (nfi)
4369 lhs = get_function_part_constraint (nfi, fi_static_chain);
4370 get_constraint_for (frame, &rhsc);
4371 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4372 process_constraint (new_constraint (lhs, *rhsp));
4373 rhsc.release ();
4375 /* Make the frame point to the function for
4376 the trampoline adjustment call. */
4377 get_constraint_for (tramp, &lhsc);
4378 do_deref (&lhsc);
4379 get_constraint_for (nfunc, &rhsc);
4380 process_all_all_constraints (lhsc, rhsc);
4381 rhsc.release ();
4382 lhsc.release ();
4384 return true;
4387 /* Else fallthru to generic handling which will let
4388 the frame escape. */
4389 break;
4391 case BUILT_IN_ADJUST_TRAMPOLINE:
4393 tree tramp = gimple_call_arg (t, 0);
4394 tree res = gimple_call_lhs (t);
4395 if (in_ipa_mode && res)
4397 get_constraint_for (res, &lhsc);
4398 get_constraint_for (tramp, &rhsc);
4399 do_deref (&rhsc);
4400 process_all_all_constraints (lhsc, rhsc);
4401 rhsc.release ();
4402 lhsc.release ();
4404 return true;
4406 CASE_BUILT_IN_TM_STORE (1):
4407 CASE_BUILT_IN_TM_STORE (2):
4408 CASE_BUILT_IN_TM_STORE (4):
4409 CASE_BUILT_IN_TM_STORE (8):
4410 CASE_BUILT_IN_TM_STORE (FLOAT):
4411 CASE_BUILT_IN_TM_STORE (DOUBLE):
4412 CASE_BUILT_IN_TM_STORE (LDOUBLE):
4413 CASE_BUILT_IN_TM_STORE (M64):
4414 CASE_BUILT_IN_TM_STORE (M128):
4415 CASE_BUILT_IN_TM_STORE (M256):
4417 tree addr = gimple_call_arg (t, 0);
4418 tree src = gimple_call_arg (t, 1);
4420 get_constraint_for (addr, &lhsc);
4421 do_deref (&lhsc);
4422 get_constraint_for (src, &rhsc);
4423 process_all_all_constraints (lhsc, rhsc);
4424 lhsc.release ();
4425 rhsc.release ();
4426 return true;
4428 CASE_BUILT_IN_TM_LOAD (1):
4429 CASE_BUILT_IN_TM_LOAD (2):
4430 CASE_BUILT_IN_TM_LOAD (4):
4431 CASE_BUILT_IN_TM_LOAD (8):
4432 CASE_BUILT_IN_TM_LOAD (FLOAT):
4433 CASE_BUILT_IN_TM_LOAD (DOUBLE):
4434 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
4435 CASE_BUILT_IN_TM_LOAD (M64):
4436 CASE_BUILT_IN_TM_LOAD (M128):
4437 CASE_BUILT_IN_TM_LOAD (M256):
4439 tree dest = gimple_call_lhs (t);
4440 tree addr = gimple_call_arg (t, 0);
4442 get_constraint_for (dest, &lhsc);
4443 get_constraint_for (addr, &rhsc);
4444 do_deref (&rhsc);
4445 process_all_all_constraints (lhsc, rhsc);
4446 lhsc.release ();
4447 rhsc.release ();
4448 return true;
4450 /* Variadic argument handling needs to be handled in IPA
4451 mode as well. */
4452 case BUILT_IN_VA_START:
4454 tree valist = gimple_call_arg (t, 0);
4455 struct constraint_expr rhs, *lhsp;
4456 unsigned i;
4457 get_constraint_for (valist, &lhsc);
4458 do_deref (&lhsc);
4459 /* The va_list gets access to pointers in variadic
4460 arguments. Which we know in the case of IPA analysis
4461 and otherwise are just all nonlocal variables. */
4462 if (in_ipa_mode)
4464 fi = lookup_vi_for_tree (fn->decl);
4465 rhs = get_function_part_constraint (fi, ~0);
4466 rhs.type = ADDRESSOF;
4468 else
4470 rhs.var = nonlocal_id;
4471 rhs.type = ADDRESSOF;
4472 rhs.offset = 0;
4474 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4475 process_constraint (new_constraint (*lhsp, rhs));
4476 lhsc.release ();
4477 /* va_list is clobbered. */
4478 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4479 return true;
4481 /* va_end doesn't have any effect that matters. */
4482 case BUILT_IN_VA_END:
4483 return true;
4484 /* Alternate return. Simply give up for now. */
4485 case BUILT_IN_RETURN:
4487 fi = NULL;
4488 if (!in_ipa_mode
4489 || !(fi = get_vi_for_tree (fn->decl)))
4490 make_constraint_from (get_varinfo (escaped_id), anything_id);
4491 else if (in_ipa_mode
4492 && fi != NULL)
4494 struct constraint_expr lhs, rhs;
4495 lhs = get_function_part_constraint (fi, fi_result);
4496 rhs.var = anything_id;
4497 rhs.offset = 0;
4498 rhs.type = SCALAR;
4499 process_constraint (new_constraint (lhs, rhs));
4501 return true;
4503 /* printf-style functions may have hooks to set pointers to
4504 point to somewhere into the generated string. Leave them
4505 for a later exercise... */
4506 default:
4507 /* Fallthru to general call handling. */;
4510 return false;
4513 /* Create constraints for the call T. */
4515 static void
4516 find_func_aliases_for_call (struct function *fn, gimple t)
4518 tree fndecl = gimple_call_fndecl (t);
4519 vec<ce_s> lhsc = vNULL;
4520 vec<ce_s> rhsc = vNULL;
4521 varinfo_t fi;
4523 if (fndecl != NULL_TREE
4524 && DECL_BUILT_IN (fndecl)
4525 && find_func_aliases_for_builtin_call (fn, t))
4526 return;
4528 fi = get_fi_for_callee (t);
4529 if (!in_ipa_mode
4530 || (fndecl && !fi->is_fn_info))
4532 vec<ce_s> rhsc = vNULL;
4533 int flags = gimple_call_flags (t);
4535 /* Const functions can return their arguments and addresses
4536 of global memory but not of escaped memory. */
4537 if (flags & (ECF_CONST|ECF_NOVOPS))
4539 if (gimple_call_lhs (t))
4540 handle_const_call (t, &rhsc);
4542 /* Pure functions can return addresses in and of memory
4543 reachable from their arguments, but they are not an escape
4544 point for reachable memory of their arguments. */
4545 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4546 handle_pure_call (t, &rhsc);
4547 else
4548 handle_rhs_call (t, &rhsc);
4549 if (gimple_call_lhs (t))
4550 handle_lhs_call (t, gimple_call_lhs (t),
4551 gimple_call_return_flags (t), rhsc, fndecl);
4552 rhsc.release ();
4554 else
4556 tree lhsop;
4557 unsigned j;
4559 /* Assign all the passed arguments to the appropriate incoming
4560 parameters of the function. */
4561 for (j = 0; j < gimple_call_num_args (t); j++)
4563 struct constraint_expr lhs ;
4564 struct constraint_expr *rhsp;
4565 tree arg = gimple_call_arg (t, j);
4567 get_constraint_for_rhs (arg, &rhsc);
4568 lhs = get_function_part_constraint (fi, fi_parm_base + j);
4569 while (rhsc.length () != 0)
4571 rhsp = &rhsc.last ();
4572 process_constraint (new_constraint (lhs, *rhsp));
4573 rhsc.pop ();
4577 /* If we are returning a value, assign it to the result. */
4578 lhsop = gimple_call_lhs (t);
4579 if (lhsop)
4581 struct constraint_expr rhs;
4582 struct constraint_expr *lhsp;
4584 get_constraint_for (lhsop, &lhsc);
4585 rhs = get_function_part_constraint (fi, fi_result);
4586 if (fndecl
4587 && DECL_RESULT (fndecl)
4588 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4590 vec<ce_s> tem = vNULL;
4591 tem.safe_push (rhs);
4592 do_deref (&tem);
4593 rhs = tem[0];
4594 tem.release ();
4596 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4597 process_constraint (new_constraint (*lhsp, rhs));
4600 /* If we pass the result decl by reference, honor that. */
4601 if (lhsop
4602 && fndecl
4603 && DECL_RESULT (fndecl)
4604 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4606 struct constraint_expr lhs;
4607 struct constraint_expr *rhsp;
4609 get_constraint_for_address_of (lhsop, &rhsc);
4610 lhs = get_function_part_constraint (fi, fi_result);
4611 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4612 process_constraint (new_constraint (lhs, *rhsp));
4613 rhsc.release ();
4616 /* If we use a static chain, pass it along. */
4617 if (gimple_call_chain (t))
4619 struct constraint_expr lhs;
4620 struct constraint_expr *rhsp;
4622 get_constraint_for (gimple_call_chain (t), &rhsc);
4623 lhs = get_function_part_constraint (fi, fi_static_chain);
4624 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4625 process_constraint (new_constraint (lhs, *rhsp));
4630 /* Walk statement T setting up aliasing constraints according to the
4631 references found in T. This function is the main part of the
4632 constraint builder. AI points to auxiliary alias information used
4633 when building alias sets and computing alias grouping heuristics. */
4635 static void
4636 find_func_aliases (struct function *fn, gimple origt)
4638 gimple t = origt;
4639 vec<ce_s> lhsc = vNULL;
4640 vec<ce_s> rhsc = vNULL;
4641 struct constraint_expr *c;
4642 varinfo_t fi;
4644 /* Now build constraints expressions. */
4645 if (gimple_code (t) == GIMPLE_PHI)
4647 size_t i;
4648 unsigned int j;
4650 /* For a phi node, assign all the arguments to
4651 the result. */
4652 get_constraint_for (gimple_phi_result (t), &lhsc);
4653 for (i = 0; i < gimple_phi_num_args (t); i++)
4655 tree strippedrhs = PHI_ARG_DEF (t, i);
4657 STRIP_NOPS (strippedrhs);
4658 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
4660 FOR_EACH_VEC_ELT (lhsc, j, c)
4662 struct constraint_expr *c2;
4663 while (rhsc.length () > 0)
4665 c2 = &rhsc.last ();
4666 process_constraint (new_constraint (*c, *c2));
4667 rhsc.pop ();
4672 /* In IPA mode, we need to generate constraints to pass call
4673 arguments through their calls. There are two cases,
4674 either a GIMPLE_CALL returning a value, or just a plain
4675 GIMPLE_CALL when we are not.
4677 In non-ipa mode, we need to generate constraints for each
4678 pointer passed by address. */
4679 else if (is_gimple_call (t))
4680 find_func_aliases_for_call (fn, t);
4682 /* Otherwise, just a regular assignment statement. Only care about
4683 operations with pointer result, others are dealt with as escape
4684 points if they have pointer operands. */
4685 else if (is_gimple_assign (t))
4687 /* Otherwise, just a regular assignment statement. */
4688 tree lhsop = gimple_assign_lhs (t);
4689 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
4691 if (rhsop && TREE_CLOBBER_P (rhsop))
4692 /* Ignore clobbers, they don't actually store anything into
4693 the LHS. */
4695 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
4696 do_structure_copy (lhsop, rhsop);
4697 else
4699 enum tree_code code = gimple_assign_rhs_code (t);
4701 get_constraint_for (lhsop, &lhsc);
4703 if (FLOAT_TYPE_P (TREE_TYPE (lhsop)))
4704 /* If the operation produces a floating point result then
4705 assume the value is not produced to transfer a pointer. */
4707 else if (code == POINTER_PLUS_EXPR)
4708 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4709 gimple_assign_rhs2 (t), &rhsc);
4710 else if (code == BIT_AND_EXPR
4711 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
4713 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4714 the pointer. Handle it by offsetting it by UNKNOWN. */
4715 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4716 NULL_TREE, &rhsc);
4718 else if ((CONVERT_EXPR_CODE_P (code)
4719 && !(POINTER_TYPE_P (gimple_expr_type (t))
4720 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
4721 || gimple_assign_single_p (t))
4722 get_constraint_for_rhs (rhsop, &rhsc);
4723 else if (code == COND_EXPR)
4725 /* The result is a merge of both COND_EXPR arms. */
4726 vec<ce_s> tmp = vNULL;
4727 struct constraint_expr *rhsp;
4728 unsigned i;
4729 get_constraint_for_rhs (gimple_assign_rhs2 (t), &rhsc);
4730 get_constraint_for_rhs (gimple_assign_rhs3 (t), &tmp);
4731 FOR_EACH_VEC_ELT (tmp, i, rhsp)
4732 rhsc.safe_push (*rhsp);
4733 tmp.release ();
4735 else if (truth_value_p (code))
4736 /* Truth value results are not pointer (parts). Or at least
4737 very very unreasonable obfuscation of a part. */
4739 else
4741 /* All other operations are merges. */
4742 vec<ce_s> tmp = vNULL;
4743 struct constraint_expr *rhsp;
4744 unsigned i, j;
4745 get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc);
4746 for (i = 2; i < gimple_num_ops (t); ++i)
4748 get_constraint_for_rhs (gimple_op (t, i), &tmp);
4749 FOR_EACH_VEC_ELT (tmp, j, rhsp)
4750 rhsc.safe_push (*rhsp);
4751 tmp.truncate (0);
4753 tmp.release ();
4755 process_all_all_constraints (lhsc, rhsc);
4757 /* If there is a store to a global variable the rhs escapes. */
4758 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
4759 && DECL_P (lhsop)
4760 && is_global_var (lhsop)
4761 && (!in_ipa_mode
4762 || DECL_EXTERNAL (lhsop) || TREE_PUBLIC (lhsop)))
4763 make_escape_constraint (rhsop);
4765 /* Handle escapes through return. */
4766 else if (gimple_code (t) == GIMPLE_RETURN
4767 && gimple_return_retval (t) != NULL_TREE)
4769 fi = NULL;
4770 if (!in_ipa_mode
4771 || !(fi = get_vi_for_tree (fn->decl)))
4772 make_escape_constraint (gimple_return_retval (t));
4773 else if (in_ipa_mode
4774 && fi != NULL)
4776 struct constraint_expr lhs ;
4777 struct constraint_expr *rhsp;
4778 unsigned i;
4780 lhs = get_function_part_constraint (fi, fi_result);
4781 get_constraint_for_rhs (gimple_return_retval (t), &rhsc);
4782 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4783 process_constraint (new_constraint (lhs, *rhsp));
4786 /* Handle asms conservatively by adding escape constraints to everything. */
4787 else if (gimple_code (t) == GIMPLE_ASM)
4789 unsigned i, noutputs;
4790 const char **oconstraints;
4791 const char *constraint;
4792 bool allows_mem, allows_reg, is_inout;
4794 noutputs = gimple_asm_noutputs (t);
4795 oconstraints = XALLOCAVEC (const char *, noutputs);
4797 for (i = 0; i < noutputs; ++i)
4799 tree link = gimple_asm_output_op (t, i);
4800 tree op = TREE_VALUE (link);
4802 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4803 oconstraints[i] = constraint;
4804 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4805 &allows_reg, &is_inout);
4807 /* A memory constraint makes the address of the operand escape. */
4808 if (!allows_reg && allows_mem)
4809 make_escape_constraint (build_fold_addr_expr (op));
4811 /* The asm may read global memory, so outputs may point to
4812 any global memory. */
4813 if (op)
4815 vec<ce_s> lhsc = vNULL;
4816 struct constraint_expr rhsc, *lhsp;
4817 unsigned j;
4818 get_constraint_for (op, &lhsc);
4819 rhsc.var = nonlocal_id;
4820 rhsc.offset = 0;
4821 rhsc.type = SCALAR;
4822 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4823 process_constraint (new_constraint (*lhsp, rhsc));
4824 lhsc.release ();
4827 for (i = 0; i < gimple_asm_ninputs (t); ++i)
4829 tree link = gimple_asm_input_op (t, i);
4830 tree op = TREE_VALUE (link);
4832 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4834 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4835 &allows_mem, &allows_reg);
4837 /* A memory constraint makes the address of the operand escape. */
4838 if (!allows_reg && allows_mem)
4839 make_escape_constraint (build_fold_addr_expr (op));
4840 /* Strictly we'd only need the constraint to ESCAPED if
4841 the asm clobbers memory, otherwise using something
4842 along the lines of per-call clobbers/uses would be enough. */
4843 else if (op)
4844 make_escape_constraint (op);
4848 rhsc.release ();
4849 lhsc.release ();
4853 /* Create a constraint adding to the clobber set of FI the memory
4854 pointed to by PTR. */
4856 static void
4857 process_ipa_clobber (varinfo_t fi, tree ptr)
4859 vec<ce_s> ptrc = vNULL;
4860 struct constraint_expr *c, lhs;
4861 unsigned i;
4862 get_constraint_for_rhs (ptr, &ptrc);
4863 lhs = get_function_part_constraint (fi, fi_clobbers);
4864 FOR_EACH_VEC_ELT (ptrc, i, c)
4865 process_constraint (new_constraint (lhs, *c));
4866 ptrc.release ();
4869 /* Walk statement T setting up clobber and use constraints according to the
4870 references found in T. This function is a main part of the
4871 IPA constraint builder. */
4873 static void
4874 find_func_clobbers (struct function *fn, gimple origt)
4876 gimple t = origt;
4877 vec<ce_s> lhsc = vNULL;
4878 auto_vec<ce_s> rhsc;
4879 varinfo_t fi;
4881 /* Add constraints for clobbered/used in IPA mode.
4882 We are not interested in what automatic variables are clobbered
4883 or used as we only use the information in the caller to which
4884 they do not escape. */
4885 gcc_assert (in_ipa_mode);
4887 /* If the stmt refers to memory in any way it better had a VUSE. */
4888 if (gimple_vuse (t) == NULL_TREE)
4889 return;
4891 /* We'd better have function information for the current function. */
4892 fi = lookup_vi_for_tree (fn->decl);
4893 gcc_assert (fi != NULL);
4895 /* Account for stores in assignments and calls. */
4896 if (gimple_vdef (t) != NULL_TREE
4897 && gimple_has_lhs (t))
4899 tree lhs = gimple_get_lhs (t);
4900 tree tem = lhs;
4901 while (handled_component_p (tem))
4902 tem = TREE_OPERAND (tem, 0);
4903 if ((DECL_P (tem)
4904 && !auto_var_in_fn_p (tem, fn->decl))
4905 || INDIRECT_REF_P (tem)
4906 || (TREE_CODE (tem) == MEM_REF
4907 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4908 && auto_var_in_fn_p
4909 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl))))
4911 struct constraint_expr lhsc, *rhsp;
4912 unsigned i;
4913 lhsc = get_function_part_constraint (fi, fi_clobbers);
4914 get_constraint_for_address_of (lhs, &rhsc);
4915 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4916 process_constraint (new_constraint (lhsc, *rhsp));
4917 rhsc.release ();
4921 /* Account for uses in assigments and returns. */
4922 if (gimple_assign_single_p (t)
4923 || (gimple_code (t) == GIMPLE_RETURN
4924 && gimple_return_retval (t) != NULL_TREE))
4926 tree rhs = (gimple_assign_single_p (t)
4927 ? gimple_assign_rhs1 (t) : gimple_return_retval (t));
4928 tree tem = rhs;
4929 while (handled_component_p (tem))
4930 tem = TREE_OPERAND (tem, 0);
4931 if ((DECL_P (tem)
4932 && !auto_var_in_fn_p (tem, fn->decl))
4933 || INDIRECT_REF_P (tem)
4934 || (TREE_CODE (tem) == MEM_REF
4935 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4936 && auto_var_in_fn_p
4937 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl))))
4939 struct constraint_expr lhs, *rhsp;
4940 unsigned i;
4941 lhs = get_function_part_constraint (fi, fi_uses);
4942 get_constraint_for_address_of (rhs, &rhsc);
4943 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4944 process_constraint (new_constraint (lhs, *rhsp));
4945 rhsc.release ();
4949 if (is_gimple_call (t))
4951 varinfo_t cfi = NULL;
4952 tree decl = gimple_call_fndecl (t);
4953 struct constraint_expr lhs, rhs;
4954 unsigned i, j;
4956 /* For builtins we do not have separate function info. For those
4957 we do not generate escapes for we have to generate clobbers/uses. */
4958 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4959 switch (DECL_FUNCTION_CODE (decl))
4961 /* The following functions use and clobber memory pointed to
4962 by their arguments. */
4963 case BUILT_IN_STRCPY:
4964 case BUILT_IN_STRNCPY:
4965 case BUILT_IN_BCOPY:
4966 case BUILT_IN_MEMCPY:
4967 case BUILT_IN_MEMMOVE:
4968 case BUILT_IN_MEMPCPY:
4969 case BUILT_IN_STPCPY:
4970 case BUILT_IN_STPNCPY:
4971 case BUILT_IN_STRCAT:
4972 case BUILT_IN_STRNCAT:
4973 case BUILT_IN_STRCPY_CHK:
4974 case BUILT_IN_STRNCPY_CHK:
4975 case BUILT_IN_MEMCPY_CHK:
4976 case BUILT_IN_MEMMOVE_CHK:
4977 case BUILT_IN_MEMPCPY_CHK:
4978 case BUILT_IN_STPCPY_CHK:
4979 case BUILT_IN_STPNCPY_CHK:
4980 case BUILT_IN_STRCAT_CHK:
4981 case BUILT_IN_STRNCAT_CHK:
4983 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4984 == BUILT_IN_BCOPY ? 1 : 0));
4985 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4986 == BUILT_IN_BCOPY ? 0 : 1));
4987 unsigned i;
4988 struct constraint_expr *rhsp, *lhsp;
4989 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4990 lhs = get_function_part_constraint (fi, fi_clobbers);
4991 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4992 process_constraint (new_constraint (lhs, *lhsp));
4993 lhsc.release ();
4994 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4995 lhs = get_function_part_constraint (fi, fi_uses);
4996 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4997 process_constraint (new_constraint (lhs, *rhsp));
4998 rhsc.release ();
4999 return;
5001 /* The following function clobbers memory pointed to by
5002 its argument. */
5003 case BUILT_IN_MEMSET:
5004 case BUILT_IN_MEMSET_CHK:
5005 case BUILT_IN_POSIX_MEMALIGN:
5007 tree dest = gimple_call_arg (t, 0);
5008 unsigned i;
5009 ce_s *lhsp;
5010 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
5011 lhs = get_function_part_constraint (fi, fi_clobbers);
5012 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
5013 process_constraint (new_constraint (lhs, *lhsp));
5014 lhsc.release ();
5015 return;
5017 /* The following functions clobber their second and third
5018 arguments. */
5019 case BUILT_IN_SINCOS:
5020 case BUILT_IN_SINCOSF:
5021 case BUILT_IN_SINCOSL:
5023 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5024 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5025 return;
5027 /* The following functions clobber their second argument. */
5028 case BUILT_IN_FREXP:
5029 case BUILT_IN_FREXPF:
5030 case BUILT_IN_FREXPL:
5031 case BUILT_IN_LGAMMA_R:
5032 case BUILT_IN_LGAMMAF_R:
5033 case BUILT_IN_LGAMMAL_R:
5034 case BUILT_IN_GAMMA_R:
5035 case BUILT_IN_GAMMAF_R:
5036 case BUILT_IN_GAMMAL_R:
5037 case BUILT_IN_MODF:
5038 case BUILT_IN_MODFF:
5039 case BUILT_IN_MODFL:
5041 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5042 return;
5044 /* The following functions clobber their third argument. */
5045 case BUILT_IN_REMQUO:
5046 case BUILT_IN_REMQUOF:
5047 case BUILT_IN_REMQUOL:
5049 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5050 return;
5052 /* The following functions neither read nor clobber memory. */
5053 case BUILT_IN_ASSUME_ALIGNED:
5054 case BUILT_IN_FREE:
5055 return;
5056 /* Trampolines are of no interest to us. */
5057 case BUILT_IN_INIT_TRAMPOLINE:
5058 case BUILT_IN_ADJUST_TRAMPOLINE:
5059 return;
5060 case BUILT_IN_VA_START:
5061 case BUILT_IN_VA_END:
5062 return;
5063 /* printf-style functions may have hooks to set pointers to
5064 point to somewhere into the generated string. Leave them
5065 for a later exercise... */
5066 default:
5067 /* Fallthru to general call handling. */;
5070 /* Parameters passed by value are used. */
5071 lhs = get_function_part_constraint (fi, fi_uses);
5072 for (i = 0; i < gimple_call_num_args (t); i++)
5074 struct constraint_expr *rhsp;
5075 tree arg = gimple_call_arg (t, i);
5077 if (TREE_CODE (arg) == SSA_NAME
5078 || is_gimple_min_invariant (arg))
5079 continue;
5081 get_constraint_for_address_of (arg, &rhsc);
5082 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5083 process_constraint (new_constraint (lhs, *rhsp));
5084 rhsc.release ();
5087 /* Build constraints for propagating clobbers/uses along the
5088 callgraph edges. */
5089 cfi = get_fi_for_callee (t);
5090 if (cfi->id == anything_id)
5092 if (gimple_vdef (t))
5093 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5094 anything_id);
5095 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5096 anything_id);
5097 return;
5100 /* For callees without function info (that's external functions),
5101 ESCAPED is clobbered and used. */
5102 if (gimple_call_fndecl (t)
5103 && !cfi->is_fn_info)
5105 varinfo_t vi;
5107 if (gimple_vdef (t))
5108 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5109 escaped_id);
5110 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
5112 /* Also honor the call statement use/clobber info. */
5113 if ((vi = lookup_call_clobber_vi (t)) != NULL)
5114 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5115 vi->id);
5116 if ((vi = lookup_call_use_vi (t)) != NULL)
5117 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
5118 vi->id);
5119 return;
5122 /* Otherwise the caller clobbers and uses what the callee does.
5123 ??? This should use a new complex constraint that filters
5124 local variables of the callee. */
5125 if (gimple_vdef (t))
5127 lhs = get_function_part_constraint (fi, fi_clobbers);
5128 rhs = get_function_part_constraint (cfi, fi_clobbers);
5129 process_constraint (new_constraint (lhs, rhs));
5131 lhs = get_function_part_constraint (fi, fi_uses);
5132 rhs = get_function_part_constraint (cfi, fi_uses);
5133 process_constraint (new_constraint (lhs, rhs));
5135 else if (gimple_code (t) == GIMPLE_ASM)
5137 /* ??? Ick. We can do better. */
5138 if (gimple_vdef (t))
5139 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5140 anything_id);
5141 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5142 anything_id);
5147 /* Find the first varinfo in the same variable as START that overlaps with
5148 OFFSET. Return NULL if we can't find one. */
5150 static varinfo_t
5151 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
5153 /* If the offset is outside of the variable, bail out. */
5154 if (offset >= start->fullsize)
5155 return NULL;
5157 /* If we cannot reach offset from start, lookup the first field
5158 and start from there. */
5159 if (start->offset > offset)
5160 start = get_varinfo (start->head);
5162 while (start)
5164 /* We may not find a variable in the field list with the actual
5165 offset when when we have glommed a structure to a variable.
5166 In that case, however, offset should still be within the size
5167 of the variable. */
5168 if (offset >= start->offset
5169 && (offset - start->offset) < start->size)
5170 return start;
5172 start = vi_next (start);
5175 return NULL;
5178 /* Find the first varinfo in the same variable as START that overlaps with
5179 OFFSET. If there is no such varinfo the varinfo directly preceding
5180 OFFSET is returned. */
5182 static varinfo_t
5183 first_or_preceding_vi_for_offset (varinfo_t start,
5184 unsigned HOST_WIDE_INT offset)
5186 /* If we cannot reach offset from start, lookup the first field
5187 and start from there. */
5188 if (start->offset > offset)
5189 start = get_varinfo (start->head);
5191 /* We may not find a variable in the field list with the actual
5192 offset when when we have glommed a structure to a variable.
5193 In that case, however, offset should still be within the size
5194 of the variable.
5195 If we got beyond the offset we look for return the field
5196 directly preceding offset which may be the last field. */
5197 while (start->next
5198 && offset >= start->offset
5199 && !((offset - start->offset) < start->size))
5200 start = vi_next (start);
5202 return start;
5206 /* This structure is used during pushing fields onto the fieldstack
5207 to track the offset of the field, since bitpos_of_field gives it
5208 relative to its immediate containing type, and we want it relative
5209 to the ultimate containing object. */
5211 struct fieldoff
5213 /* Offset from the base of the base containing object to this field. */
5214 HOST_WIDE_INT offset;
5216 /* Size, in bits, of the field. */
5217 unsigned HOST_WIDE_INT size;
5219 unsigned has_unknown_size : 1;
5221 unsigned must_have_pointers : 1;
5223 unsigned may_have_pointers : 1;
5225 unsigned only_restrict_pointers : 1;
5227 typedef struct fieldoff fieldoff_s;
5230 /* qsort comparison function for two fieldoff's PA and PB */
5232 static int
5233 fieldoff_compare (const void *pa, const void *pb)
5235 const fieldoff_s *foa = (const fieldoff_s *)pa;
5236 const fieldoff_s *fob = (const fieldoff_s *)pb;
5237 unsigned HOST_WIDE_INT foasize, fobsize;
5239 if (foa->offset < fob->offset)
5240 return -1;
5241 else if (foa->offset > fob->offset)
5242 return 1;
5244 foasize = foa->size;
5245 fobsize = fob->size;
5246 if (foasize < fobsize)
5247 return -1;
5248 else if (foasize > fobsize)
5249 return 1;
5250 return 0;
5253 /* Sort a fieldstack according to the field offset and sizes. */
5254 static void
5255 sort_fieldstack (vec<fieldoff_s> fieldstack)
5257 fieldstack.qsort (fieldoff_compare);
5260 /* Return true if T is a type that can have subvars. */
5262 static inline bool
5263 type_can_have_subvars (const_tree t)
5265 /* Aggregates without overlapping fields can have subvars. */
5266 return TREE_CODE (t) == RECORD_TYPE;
5269 /* Return true if V is a tree that we can have subvars for.
5270 Normally, this is any aggregate type. Also complex
5271 types which are not gimple registers can have subvars. */
5273 static inline bool
5274 var_can_have_subvars (const_tree v)
5276 /* Volatile variables should never have subvars. */
5277 if (TREE_THIS_VOLATILE (v))
5278 return false;
5280 /* Non decls or memory tags can never have subvars. */
5281 if (!DECL_P (v))
5282 return false;
5284 return type_can_have_subvars (TREE_TYPE (v));
5287 /* Return true if T is a type that does contain pointers. */
5289 static bool
5290 type_must_have_pointers (tree type)
5292 if (POINTER_TYPE_P (type))
5293 return true;
5295 if (TREE_CODE (type) == ARRAY_TYPE)
5296 return type_must_have_pointers (TREE_TYPE (type));
5298 /* A function or method can have pointers as arguments, so track
5299 those separately. */
5300 if (TREE_CODE (type) == FUNCTION_TYPE
5301 || TREE_CODE (type) == METHOD_TYPE)
5302 return true;
5304 return false;
5307 static bool
5308 field_must_have_pointers (tree t)
5310 return type_must_have_pointers (TREE_TYPE (t));
5313 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5314 the fields of TYPE onto fieldstack, recording their offsets along
5315 the way.
5317 OFFSET is used to keep track of the offset in this entire
5318 structure, rather than just the immediately containing structure.
5319 Returns false if the caller is supposed to handle the field we
5320 recursed for. */
5322 static bool
5323 push_fields_onto_fieldstack (tree type, vec<fieldoff_s> *fieldstack,
5324 HOST_WIDE_INT offset)
5326 tree field;
5327 bool empty_p = true;
5329 if (TREE_CODE (type) != RECORD_TYPE)
5330 return false;
5332 /* If the vector of fields is growing too big, bail out early.
5333 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5334 sure this fails. */
5335 if (fieldstack->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5336 return false;
5338 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5339 if (TREE_CODE (field) == FIELD_DECL)
5341 bool push = false;
5342 HOST_WIDE_INT foff = bitpos_of_field (field);
5344 if (!var_can_have_subvars (field)
5345 || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
5346 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5347 push = true;
5348 else if (!push_fields_onto_fieldstack
5349 (TREE_TYPE (field), fieldstack, offset + foff)
5350 && (DECL_SIZE (field)
5351 && !integer_zerop (DECL_SIZE (field))))
5352 /* Empty structures may have actual size, like in C++. So
5353 see if we didn't push any subfields and the size is
5354 nonzero, push the field onto the stack. */
5355 push = true;
5357 if (push)
5359 fieldoff_s *pair = NULL;
5360 bool has_unknown_size = false;
5361 bool must_have_pointers_p;
5363 if (!fieldstack->is_empty ())
5364 pair = &fieldstack->last ();
5366 /* If there isn't anything at offset zero, create sth. */
5367 if (!pair
5368 && offset + foff != 0)
5370 fieldoff_s e = {0, offset + foff, false, false, false, false};
5371 pair = fieldstack->safe_push (e);
5374 if (!DECL_SIZE (field)
5375 || !tree_fits_uhwi_p (DECL_SIZE (field)))
5376 has_unknown_size = true;
5378 /* If adjacent fields do not contain pointers merge them. */
5379 must_have_pointers_p = field_must_have_pointers (field);
5380 if (pair
5381 && !has_unknown_size
5382 && !must_have_pointers_p
5383 && !pair->must_have_pointers
5384 && !pair->has_unknown_size
5385 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5387 pair->size += tree_to_uhwi (DECL_SIZE (field));
5389 else
5391 fieldoff_s e;
5392 e.offset = offset + foff;
5393 e.has_unknown_size = has_unknown_size;
5394 if (!has_unknown_size)
5395 e.size = tree_to_uhwi (DECL_SIZE (field));
5396 else
5397 e.size = -1;
5398 e.must_have_pointers = must_have_pointers_p;
5399 e.may_have_pointers = true;
5400 e.only_restrict_pointers
5401 = (!has_unknown_size
5402 && POINTER_TYPE_P (TREE_TYPE (field))
5403 && TYPE_RESTRICT (TREE_TYPE (field)));
5404 fieldstack->safe_push (e);
5408 empty_p = false;
5411 return !empty_p;
5414 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5415 if it is a varargs function. */
5417 static unsigned int
5418 count_num_arguments (tree decl, bool *is_varargs)
5420 unsigned int num = 0;
5421 tree t;
5423 /* Capture named arguments for K&R functions. They do not
5424 have a prototype and thus no TYPE_ARG_TYPES. */
5425 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5426 ++num;
5428 /* Check if the function has variadic arguments. */
5429 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5430 if (TREE_VALUE (t) == void_type_node)
5431 break;
5432 if (!t)
5433 *is_varargs = true;
5435 return num;
5438 /* Creation function node for DECL, using NAME, and return the index
5439 of the variable we've created for the function. */
5441 static varinfo_t
5442 create_function_info_for (tree decl, const char *name)
5444 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5445 varinfo_t vi, prev_vi;
5446 tree arg;
5447 unsigned int i;
5448 bool is_varargs = false;
5449 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5451 /* Create the variable info. */
5453 vi = new_var_info (decl, name);
5454 vi->offset = 0;
5455 vi->size = 1;
5456 vi->fullsize = fi_parm_base + num_args;
5457 vi->is_fn_info = 1;
5458 vi->may_have_pointers = false;
5459 if (is_varargs)
5460 vi->fullsize = ~0;
5461 insert_vi_for_tree (vi->decl, vi);
5463 prev_vi = vi;
5465 /* Create a variable for things the function clobbers and one for
5466 things the function uses. */
5468 varinfo_t clobbervi, usevi;
5469 const char *newname;
5470 char *tempname;
5472 asprintf (&tempname, "%s.clobber", name);
5473 newname = ggc_strdup (tempname);
5474 free (tempname);
5476 clobbervi = new_var_info (NULL, newname);
5477 clobbervi->offset = fi_clobbers;
5478 clobbervi->size = 1;
5479 clobbervi->fullsize = vi->fullsize;
5480 clobbervi->is_full_var = true;
5481 clobbervi->is_global_var = false;
5482 gcc_assert (prev_vi->offset < clobbervi->offset);
5483 prev_vi->next = clobbervi->id;
5484 prev_vi = clobbervi;
5486 asprintf (&tempname, "%s.use", name);
5487 newname = ggc_strdup (tempname);
5488 free (tempname);
5490 usevi = new_var_info (NULL, newname);
5491 usevi->offset = fi_uses;
5492 usevi->size = 1;
5493 usevi->fullsize = vi->fullsize;
5494 usevi->is_full_var = true;
5495 usevi->is_global_var = false;
5496 gcc_assert (prev_vi->offset < usevi->offset);
5497 prev_vi->next = usevi->id;
5498 prev_vi = usevi;
5501 /* And one for the static chain. */
5502 if (fn->static_chain_decl != NULL_TREE)
5504 varinfo_t chainvi;
5505 const char *newname;
5506 char *tempname;
5508 asprintf (&tempname, "%s.chain", name);
5509 newname = ggc_strdup (tempname);
5510 free (tempname);
5512 chainvi = new_var_info (fn->static_chain_decl, newname);
5513 chainvi->offset = fi_static_chain;
5514 chainvi->size = 1;
5515 chainvi->fullsize = vi->fullsize;
5516 chainvi->is_full_var = true;
5517 chainvi->is_global_var = false;
5518 gcc_assert (prev_vi->offset < chainvi->offset);
5519 prev_vi->next = chainvi->id;
5520 prev_vi = chainvi;
5521 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5524 /* Create a variable for the return var. */
5525 if (DECL_RESULT (decl) != NULL
5526 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5528 varinfo_t resultvi;
5529 const char *newname;
5530 char *tempname;
5531 tree resultdecl = decl;
5533 if (DECL_RESULT (decl))
5534 resultdecl = DECL_RESULT (decl);
5536 asprintf (&tempname, "%s.result", name);
5537 newname = ggc_strdup (tempname);
5538 free (tempname);
5540 resultvi = new_var_info (resultdecl, newname);
5541 resultvi->offset = fi_result;
5542 resultvi->size = 1;
5543 resultvi->fullsize = vi->fullsize;
5544 resultvi->is_full_var = true;
5545 if (DECL_RESULT (decl))
5546 resultvi->may_have_pointers = true;
5547 gcc_assert (prev_vi->offset < resultvi->offset);
5548 prev_vi->next = resultvi->id;
5549 prev_vi = resultvi;
5550 if (DECL_RESULT (decl))
5551 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
5554 /* Set up variables for each argument. */
5555 arg = DECL_ARGUMENTS (decl);
5556 for (i = 0; i < num_args; i++)
5558 varinfo_t argvi;
5559 const char *newname;
5560 char *tempname;
5561 tree argdecl = decl;
5563 if (arg)
5564 argdecl = arg;
5566 asprintf (&tempname, "%s.arg%d", name, i);
5567 newname = ggc_strdup (tempname);
5568 free (tempname);
5570 argvi = new_var_info (argdecl, newname);
5571 argvi->offset = fi_parm_base + i;
5572 argvi->size = 1;
5573 argvi->is_full_var = true;
5574 argvi->fullsize = vi->fullsize;
5575 if (arg)
5576 argvi->may_have_pointers = true;
5577 gcc_assert (prev_vi->offset < argvi->offset);
5578 prev_vi->next = argvi->id;
5579 prev_vi = argvi;
5580 if (arg)
5582 insert_vi_for_tree (arg, argvi);
5583 arg = DECL_CHAIN (arg);
5587 /* Add one representative for all further args. */
5588 if (is_varargs)
5590 varinfo_t argvi;
5591 const char *newname;
5592 char *tempname;
5593 tree decl;
5595 asprintf (&tempname, "%s.varargs", name);
5596 newname = ggc_strdup (tempname);
5597 free (tempname);
5599 /* We need sth that can be pointed to for va_start. */
5600 decl = build_fake_var_decl (ptr_type_node);
5602 argvi = new_var_info (decl, newname);
5603 argvi->offset = fi_parm_base + num_args;
5604 argvi->size = ~0;
5605 argvi->is_full_var = true;
5606 argvi->is_heap_var = true;
5607 argvi->fullsize = vi->fullsize;
5608 gcc_assert (prev_vi->offset < argvi->offset);
5609 prev_vi->next = argvi->id;
5610 prev_vi = argvi;
5613 return vi;
5617 /* Return true if FIELDSTACK contains fields that overlap.
5618 FIELDSTACK is assumed to be sorted by offset. */
5620 static bool
5621 check_for_overlaps (vec<fieldoff_s> fieldstack)
5623 fieldoff_s *fo = NULL;
5624 unsigned int i;
5625 HOST_WIDE_INT lastoffset = -1;
5627 FOR_EACH_VEC_ELT (fieldstack, i, fo)
5629 if (fo->offset == lastoffset)
5630 return true;
5631 lastoffset = fo->offset;
5633 return false;
5636 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5637 This will also create any varinfo structures necessary for fields
5638 of DECL. */
5640 static varinfo_t
5641 create_variable_info_for_1 (tree decl, const char *name)
5643 varinfo_t vi, newvi;
5644 tree decl_type = TREE_TYPE (decl);
5645 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
5646 auto_vec<fieldoff_s> fieldstack;
5647 fieldoff_s *fo;
5648 unsigned int i;
5649 varpool_node *vnode;
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 && (vnode = varpool_node::get (decl))
5672 && vnode->get_constructor ()))
5674 fieldoff_s *fo = NULL;
5675 bool notokay = false;
5676 unsigned int i;
5678 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
5680 for (i = 0; !notokay && fieldstack.iterate (i, &fo); i++)
5681 if (fo->has_unknown_size
5682 || fo->offset < 0)
5684 notokay = true;
5685 break;
5688 /* We can't sort them if we have a field with a variable sized type,
5689 which will make notokay = true. In that case, we are going to return
5690 without creating varinfos for the fields anyway, so sorting them is a
5691 waste to boot. */
5692 if (!notokay)
5694 sort_fieldstack (fieldstack);
5695 /* Due to some C++ FE issues, like PR 22488, we might end up
5696 what appear to be overlapping fields even though they,
5697 in reality, do not overlap. Until the C++ FE is fixed,
5698 we will simply disable field-sensitivity for these cases. */
5699 notokay = check_for_overlaps (fieldstack);
5702 if (notokay)
5703 fieldstack.release ();
5706 /* If we didn't end up collecting sub-variables create a full
5707 variable for the decl. */
5708 if (fieldstack.length () <= 1
5709 || fieldstack.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5711 vi = new_var_info (decl, name);
5712 vi->offset = 0;
5713 vi->may_have_pointers = true;
5714 vi->fullsize = tree_to_uhwi (declsize);
5715 vi->size = vi->fullsize;
5716 vi->is_full_var = true;
5717 fieldstack.release ();
5718 return vi;
5721 vi = new_var_info (decl, name);
5722 vi->fullsize = tree_to_uhwi (declsize);
5723 for (i = 0, newvi = vi;
5724 fieldstack.iterate (i, &fo);
5725 ++i, newvi = vi_next (newvi))
5727 const char *newname = "NULL";
5728 char *tempname;
5730 if (dump_file)
5732 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC
5733 "+" HOST_WIDE_INT_PRINT_DEC, name, fo->offset, fo->size);
5734 newname = ggc_strdup (tempname);
5735 free (tempname);
5737 newvi->name = newname;
5738 newvi->offset = fo->offset;
5739 newvi->size = fo->size;
5740 newvi->fullsize = vi->fullsize;
5741 newvi->may_have_pointers = fo->may_have_pointers;
5742 newvi->only_restrict_pointers = fo->only_restrict_pointers;
5743 if (i + 1 < fieldstack.length ())
5745 varinfo_t tem = new_var_info (decl, name);
5746 newvi->next = tem->id;
5747 tem->head = vi->id;
5751 return vi;
5754 static unsigned int
5755 create_variable_info_for (tree decl, const char *name)
5757 varinfo_t vi = create_variable_info_for_1 (decl, name);
5758 unsigned int id = vi->id;
5760 insert_vi_for_tree (decl, vi);
5762 if (TREE_CODE (decl) != VAR_DECL)
5763 return id;
5765 /* Create initial constraints for globals. */
5766 for (; vi; vi = vi_next (vi))
5768 if (!vi->may_have_pointers
5769 || !vi->is_global_var)
5770 continue;
5772 /* Mark global restrict qualified pointers. */
5773 if ((POINTER_TYPE_P (TREE_TYPE (decl))
5774 && TYPE_RESTRICT (TREE_TYPE (decl)))
5775 || vi->only_restrict_pointers)
5777 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5778 continue;
5781 /* In non-IPA mode the initializer from nonlocal is all we need. */
5782 if (!in_ipa_mode
5783 || DECL_HARD_REGISTER (decl))
5784 make_copy_constraint (vi, nonlocal_id);
5786 /* In IPA mode parse the initializer and generate proper constraints
5787 for it. */
5788 else
5790 varpool_node *vnode = varpool_node::get (decl);
5792 /* For escaped variables initialize them from nonlocal. */
5793 if (!vnode->all_refs_explicit_p ())
5794 make_copy_constraint (vi, nonlocal_id);
5796 /* If this is a global variable with an initializer and we are in
5797 IPA mode generate constraints for it. */
5798 if (vnode->get_constructor ()
5799 && vnode->definition)
5801 auto_vec<ce_s> rhsc;
5802 struct constraint_expr lhs, *rhsp;
5803 unsigned i;
5804 get_constraint_for_rhs (vnode->get_constructor (), &rhsc);
5805 lhs.var = vi->id;
5806 lhs.offset = 0;
5807 lhs.type = SCALAR;
5808 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5809 process_constraint (new_constraint (lhs, *rhsp));
5810 /* If this is a variable that escapes from the unit
5811 the initializer escapes as well. */
5812 if (!vnode->all_refs_explicit_p ())
5814 lhs.var = escaped_id;
5815 lhs.offset = 0;
5816 lhs.type = SCALAR;
5817 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5818 process_constraint (new_constraint (lhs, *rhsp));
5824 return id;
5827 /* Print out the points-to solution for VAR to FILE. */
5829 static void
5830 dump_solution_for_var (FILE *file, unsigned int var)
5832 varinfo_t vi = get_varinfo (var);
5833 unsigned int i;
5834 bitmap_iterator bi;
5836 /* Dump the solution for unified vars anyway, this avoids difficulties
5837 in scanning dumps in the testsuite. */
5838 fprintf (file, "%s = { ", vi->name);
5839 vi = get_varinfo (find (var));
5840 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5841 fprintf (file, "%s ", get_varinfo (i)->name);
5842 fprintf (file, "}");
5844 /* But note when the variable was unified. */
5845 if (vi->id != var)
5846 fprintf (file, " same as %s", vi->name);
5848 fprintf (file, "\n");
5851 /* Print the points-to solution for VAR to stderr. */
5853 DEBUG_FUNCTION void
5854 debug_solution_for_var (unsigned int var)
5856 dump_solution_for_var (stderr, var);
5859 /* Create varinfo structures for all of the variables in the
5860 function for intraprocedural mode. */
5862 static void
5863 intra_create_variable_infos (struct function *fn)
5865 tree t;
5867 /* For each incoming pointer argument arg, create the constraint ARG
5868 = NONLOCAL or a dummy variable if it is a restrict qualified
5869 passed-by-reference argument. */
5870 for (t = DECL_ARGUMENTS (fn->decl); t; t = DECL_CHAIN (t))
5872 varinfo_t p = get_vi_for_tree (t);
5874 /* For restrict qualified pointers to objects passed by
5875 reference build a real representative for the pointed-to object.
5876 Treat restrict qualified references the same. */
5877 if (TYPE_RESTRICT (TREE_TYPE (t))
5878 && ((DECL_BY_REFERENCE (t) && POINTER_TYPE_P (TREE_TYPE (t)))
5879 || TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5880 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t))))
5882 struct constraint_expr lhsc, rhsc;
5883 varinfo_t vi;
5884 tree heapvar = build_fake_var_decl (TREE_TYPE (TREE_TYPE (t)));
5885 DECL_EXTERNAL (heapvar) = 1;
5886 vi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS");
5887 insert_vi_for_tree (heapvar, vi);
5888 lhsc.var = p->id;
5889 lhsc.type = SCALAR;
5890 lhsc.offset = 0;
5891 rhsc.var = vi->id;
5892 rhsc.type = ADDRESSOF;
5893 rhsc.offset = 0;
5894 process_constraint (new_constraint (lhsc, rhsc));
5895 for (; vi; vi = vi_next (vi))
5896 if (vi->may_have_pointers)
5898 if (vi->only_restrict_pointers)
5899 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5900 else
5901 make_copy_constraint (vi, nonlocal_id);
5903 continue;
5906 if (POINTER_TYPE_P (TREE_TYPE (t))
5907 && TYPE_RESTRICT (TREE_TYPE (t)))
5908 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5909 else
5911 for (; p; p = vi_next (p))
5913 if (p->only_restrict_pointers)
5914 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5915 else if (p->may_have_pointers)
5916 make_constraint_from (p, nonlocal_id);
5921 /* Add a constraint for a result decl that is passed by reference. */
5922 if (DECL_RESULT (fn->decl)
5923 && DECL_BY_REFERENCE (DECL_RESULT (fn->decl)))
5925 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (fn->decl));
5927 for (p = result_vi; p; p = vi_next (p))
5928 make_constraint_from (p, nonlocal_id);
5931 /* Add a constraint for the incoming static chain parameter. */
5932 if (fn->static_chain_decl != NULL_TREE)
5934 varinfo_t p, chain_vi = get_vi_for_tree (fn->static_chain_decl);
5936 for (p = chain_vi; p; p = vi_next (p))
5937 make_constraint_from (p, nonlocal_id);
5941 /* Structure used to put solution bitmaps in a hashtable so they can
5942 be shared among variables with the same points-to set. */
5944 typedef struct shared_bitmap_info
5946 bitmap pt_vars;
5947 hashval_t hashcode;
5948 } *shared_bitmap_info_t;
5949 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
5951 /* Shared_bitmap hashtable helpers. */
5953 struct shared_bitmap_hasher : typed_free_remove <shared_bitmap_info>
5955 typedef shared_bitmap_info value_type;
5956 typedef shared_bitmap_info compare_type;
5957 static inline hashval_t hash (const value_type *);
5958 static inline bool equal (const value_type *, const compare_type *);
5961 /* Hash function for a shared_bitmap_info_t */
5963 inline hashval_t
5964 shared_bitmap_hasher::hash (const value_type *bi)
5966 return bi->hashcode;
5969 /* Equality function for two shared_bitmap_info_t's. */
5971 inline bool
5972 shared_bitmap_hasher::equal (const value_type *sbi1, const compare_type *sbi2)
5974 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
5977 /* Shared_bitmap hashtable. */
5979 static hash_table<shared_bitmap_hasher> *shared_bitmap_table;
5981 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5982 existing instance if there is one, NULL otherwise. */
5984 static bitmap
5985 shared_bitmap_lookup (bitmap pt_vars)
5987 shared_bitmap_info **slot;
5988 struct shared_bitmap_info sbi;
5990 sbi.pt_vars = pt_vars;
5991 sbi.hashcode = bitmap_hash (pt_vars);
5993 slot = shared_bitmap_table->find_slot (&sbi, NO_INSERT);
5994 if (!slot)
5995 return NULL;
5996 else
5997 return (*slot)->pt_vars;
6001 /* Add a bitmap to the shared bitmap hashtable. */
6003 static void
6004 shared_bitmap_add (bitmap pt_vars)
6006 shared_bitmap_info **slot;
6007 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
6009 sbi->pt_vars = pt_vars;
6010 sbi->hashcode = bitmap_hash (pt_vars);
6012 slot = shared_bitmap_table->find_slot (sbi, INSERT);
6013 gcc_assert (!*slot);
6014 *slot = sbi;
6018 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
6020 static void
6021 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt)
6023 unsigned int i;
6024 bitmap_iterator bi;
6025 varinfo_t escaped_vi = get_varinfo (find (escaped_id));
6026 bool everything_escaped
6027 = escaped_vi->solution && bitmap_bit_p (escaped_vi->solution, anything_id);
6029 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
6031 varinfo_t vi = get_varinfo (i);
6033 /* The only artificial variables that are allowed in a may-alias
6034 set are heap variables. */
6035 if (vi->is_artificial_var && !vi->is_heap_var)
6036 continue;
6038 if (everything_escaped
6039 || (escaped_vi->solution
6040 && bitmap_bit_p (escaped_vi->solution, i)))
6042 pt->vars_contains_escaped = true;
6043 pt->vars_contains_escaped_heap = vi->is_heap_var;
6046 if (TREE_CODE (vi->decl) == VAR_DECL
6047 || TREE_CODE (vi->decl) == PARM_DECL
6048 || TREE_CODE (vi->decl) == RESULT_DECL)
6050 /* If we are in IPA mode we will not recompute points-to
6051 sets after inlining so make sure they stay valid. */
6052 if (in_ipa_mode
6053 && !DECL_PT_UID_SET_P (vi->decl))
6054 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
6056 /* Add the decl to the points-to set. Note that the points-to
6057 set contains global variables. */
6058 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
6059 if (vi->is_global_var)
6060 pt->vars_contains_nonlocal = true;
6066 /* Compute the points-to solution *PT for the variable VI. */
6068 static struct pt_solution
6069 find_what_var_points_to (varinfo_t orig_vi)
6071 unsigned int i;
6072 bitmap_iterator bi;
6073 bitmap finished_solution;
6074 bitmap result;
6075 varinfo_t vi;
6076 struct pt_solution *pt;
6078 /* This variable may have been collapsed, let's get the real
6079 variable. */
6080 vi = get_varinfo (find (orig_vi->id));
6082 /* See if we have already computed the solution and return it. */
6083 pt_solution **slot = &final_solutions->get_or_insert (vi);
6084 if (*slot != NULL)
6085 return **slot;
6087 *slot = pt = XOBNEW (&final_solutions_obstack, struct pt_solution);
6088 memset (pt, 0, sizeof (struct pt_solution));
6090 /* Translate artificial variables into SSA_NAME_PTR_INFO
6091 attributes. */
6092 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
6094 varinfo_t vi = get_varinfo (i);
6096 if (vi->is_artificial_var)
6098 if (vi->id == nothing_id)
6099 pt->null = 1;
6100 else if (vi->id == escaped_id)
6102 if (in_ipa_mode)
6103 pt->ipa_escaped = 1;
6104 else
6105 pt->escaped = 1;
6106 /* Expand some special vars of ESCAPED in-place here. */
6107 varinfo_t evi = get_varinfo (find (escaped_id));
6108 if (bitmap_bit_p (evi->solution, nonlocal_id))
6109 pt->nonlocal = 1;
6111 else if (vi->id == nonlocal_id)
6112 pt->nonlocal = 1;
6113 else if (vi->is_heap_var)
6114 /* We represent heapvars in the points-to set properly. */
6116 else if (vi->id == readonly_id)
6117 /* Nobody cares. */
6119 else if (vi->id == anything_id
6120 || vi->id == integer_id)
6121 pt->anything = 1;
6125 /* Instead of doing extra work, simply do not create
6126 elaborate points-to information for pt_anything pointers. */
6127 if (pt->anything)
6128 return *pt;
6130 /* Share the final set of variables when possible. */
6131 finished_solution = BITMAP_GGC_ALLOC ();
6132 stats.points_to_sets_created++;
6134 set_uids_in_ptset (finished_solution, vi->solution, pt);
6135 result = shared_bitmap_lookup (finished_solution);
6136 if (!result)
6138 shared_bitmap_add (finished_solution);
6139 pt->vars = finished_solution;
6141 else
6143 pt->vars = result;
6144 bitmap_clear (finished_solution);
6147 return *pt;
6150 /* Given a pointer variable P, fill in its points-to set. */
6152 static void
6153 find_what_p_points_to (tree p)
6155 struct ptr_info_def *pi;
6156 tree lookup_p = p;
6157 varinfo_t vi;
6159 /* For parameters, get at the points-to set for the actual parm
6160 decl. */
6161 if (TREE_CODE (p) == SSA_NAME
6162 && SSA_NAME_IS_DEFAULT_DEF (p)
6163 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
6164 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL))
6165 lookup_p = SSA_NAME_VAR (p);
6167 vi = lookup_vi_for_tree (lookup_p);
6168 if (!vi)
6169 return;
6171 pi = get_ptr_info (p);
6172 pi->pt = find_what_var_points_to (vi);
6176 /* Query statistics for points-to solutions. */
6178 static struct {
6179 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
6180 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
6181 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
6182 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
6183 } pta_stats;
6185 void
6186 dump_pta_stats (FILE *s)
6188 fprintf (s, "\nPTA query stats:\n");
6189 fprintf (s, " pt_solution_includes: "
6190 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6191 HOST_WIDE_INT_PRINT_DEC" queries\n",
6192 pta_stats.pt_solution_includes_no_alias,
6193 pta_stats.pt_solution_includes_no_alias
6194 + pta_stats.pt_solution_includes_may_alias);
6195 fprintf (s, " pt_solutions_intersect: "
6196 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6197 HOST_WIDE_INT_PRINT_DEC" queries\n",
6198 pta_stats.pt_solutions_intersect_no_alias,
6199 pta_stats.pt_solutions_intersect_no_alias
6200 + pta_stats.pt_solutions_intersect_may_alias);
6204 /* Reset the points-to solution *PT to a conservative default
6205 (point to anything). */
6207 void
6208 pt_solution_reset (struct pt_solution *pt)
6210 memset (pt, 0, sizeof (struct pt_solution));
6211 pt->anything = true;
6214 /* Set the points-to solution *PT to point only to the variables
6215 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6216 global variables and VARS_CONTAINS_RESTRICT specifies whether
6217 it contains restrict tag variables. */
6219 void
6220 pt_solution_set (struct pt_solution *pt, bitmap vars,
6221 bool vars_contains_nonlocal)
6223 memset (pt, 0, sizeof (struct pt_solution));
6224 pt->vars = vars;
6225 pt->vars_contains_nonlocal = vars_contains_nonlocal;
6226 pt->vars_contains_escaped
6227 = (cfun->gimple_df->escaped.anything
6228 || bitmap_intersect_p (cfun->gimple_df->escaped.vars, vars));
6231 /* Set the points-to solution *PT to point only to the variable VAR. */
6233 void
6234 pt_solution_set_var (struct pt_solution *pt, tree var)
6236 memset (pt, 0, sizeof (struct pt_solution));
6237 pt->vars = BITMAP_GGC_ALLOC ();
6238 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
6239 pt->vars_contains_nonlocal = is_global_var (var);
6240 pt->vars_contains_escaped
6241 = (cfun->gimple_df->escaped.anything
6242 || bitmap_bit_p (cfun->gimple_df->escaped.vars, DECL_PT_UID (var)));
6245 /* Computes the union of the points-to solutions *DEST and *SRC and
6246 stores the result in *DEST. This changes the points-to bitmap
6247 of *DEST and thus may not be used if that might be shared.
6248 The points-to bitmap of *SRC and *DEST will not be shared after
6249 this function if they were not before. */
6251 static void
6252 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6254 dest->anything |= src->anything;
6255 if (dest->anything)
6257 pt_solution_reset (dest);
6258 return;
6261 dest->nonlocal |= src->nonlocal;
6262 dest->escaped |= src->escaped;
6263 dest->ipa_escaped |= src->ipa_escaped;
6264 dest->null |= src->null;
6265 dest->vars_contains_nonlocal |= src->vars_contains_nonlocal;
6266 dest->vars_contains_escaped |= src->vars_contains_escaped;
6267 dest->vars_contains_escaped_heap |= src->vars_contains_escaped_heap;
6268 if (!src->vars)
6269 return;
6271 if (!dest->vars)
6272 dest->vars = BITMAP_GGC_ALLOC ();
6273 bitmap_ior_into (dest->vars, src->vars);
6276 /* Return true if the points-to solution *PT is empty. */
6278 bool
6279 pt_solution_empty_p (struct pt_solution *pt)
6281 if (pt->anything
6282 || pt->nonlocal)
6283 return false;
6285 if (pt->vars
6286 && !bitmap_empty_p (pt->vars))
6287 return false;
6289 /* If the solution includes ESCAPED, check if that is empty. */
6290 if (pt->escaped
6291 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6292 return false;
6294 /* If the solution includes ESCAPED, check if that is empty. */
6295 if (pt->ipa_escaped
6296 && !pt_solution_empty_p (&ipa_escaped_pt))
6297 return false;
6299 return true;
6302 /* Return true if the points-to solution *PT only point to a single var, and
6303 return the var uid in *UID. */
6305 bool
6306 pt_solution_singleton_p (struct pt_solution *pt, unsigned *uid)
6308 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
6309 || pt->null || pt->vars == NULL
6310 || !bitmap_single_bit_set_p (pt->vars))
6311 return false;
6313 *uid = bitmap_first_set_bit (pt->vars);
6314 return true;
6317 /* Return true if the points-to solution *PT includes global memory. */
6319 bool
6320 pt_solution_includes_global (struct pt_solution *pt)
6322 if (pt->anything
6323 || pt->nonlocal
6324 || pt->vars_contains_nonlocal
6325 /* The following is a hack to make the malloc escape hack work.
6326 In reality we'd need different sets for escaped-through-return
6327 and escaped-to-callees and passes would need to be updated. */
6328 || pt->vars_contains_escaped_heap)
6329 return true;
6331 /* 'escaped' is also a placeholder so we have to look into it. */
6332 if (pt->escaped)
6333 return pt_solution_includes_global (&cfun->gimple_df->escaped);
6335 if (pt->ipa_escaped)
6336 return pt_solution_includes_global (&ipa_escaped_pt);
6338 /* ??? This predicate is not correct for the IPA-PTA solution
6339 as we do not properly distinguish between unit escape points
6340 and global variables. */
6341 if (cfun->gimple_df->ipa_pta)
6342 return true;
6344 return false;
6347 /* Return true if the points-to solution *PT includes the variable
6348 declaration DECL. */
6350 static bool
6351 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6353 if (pt->anything)
6354 return true;
6356 if (pt->nonlocal
6357 && is_global_var (decl))
6358 return true;
6360 if (pt->vars
6361 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6362 return true;
6364 /* If the solution includes ESCAPED, check it. */
6365 if (pt->escaped
6366 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6367 return true;
6369 /* If the solution includes ESCAPED, check it. */
6370 if (pt->ipa_escaped
6371 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6372 return true;
6374 return false;
6377 bool
6378 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6380 bool res = pt_solution_includes_1 (pt, decl);
6381 if (res)
6382 ++pta_stats.pt_solution_includes_may_alias;
6383 else
6384 ++pta_stats.pt_solution_includes_no_alias;
6385 return res;
6388 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6389 intersection. */
6391 static bool
6392 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
6394 if (pt1->anything || pt2->anything)
6395 return true;
6397 /* If either points to unknown global memory and the other points to
6398 any global memory they alias. */
6399 if ((pt1->nonlocal
6400 && (pt2->nonlocal
6401 || pt2->vars_contains_nonlocal))
6402 || (pt2->nonlocal
6403 && pt1->vars_contains_nonlocal))
6404 return true;
6406 /* If either points to all escaped memory and the other points to
6407 any escaped memory they alias. */
6408 if ((pt1->escaped
6409 && (pt2->escaped
6410 || pt2->vars_contains_escaped))
6411 || (pt2->escaped
6412 && pt1->vars_contains_escaped))
6413 return true;
6415 /* Check the escaped solution if required.
6416 ??? Do we need to check the local against the IPA escaped sets? */
6417 if ((pt1->ipa_escaped || pt2->ipa_escaped)
6418 && !pt_solution_empty_p (&ipa_escaped_pt))
6420 /* If both point to escaped memory and that solution
6421 is not empty they alias. */
6422 if (pt1->ipa_escaped && pt2->ipa_escaped)
6423 return true;
6425 /* If either points to escaped memory see if the escaped solution
6426 intersects with the other. */
6427 if ((pt1->ipa_escaped
6428 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
6429 || (pt2->ipa_escaped
6430 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
6431 return true;
6434 /* Now both pointers alias if their points-to solution intersects. */
6435 return (pt1->vars
6436 && pt2->vars
6437 && bitmap_intersect_p (pt1->vars, pt2->vars));
6440 bool
6441 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
6443 bool res = pt_solutions_intersect_1 (pt1, pt2);
6444 if (res)
6445 ++pta_stats.pt_solutions_intersect_may_alias;
6446 else
6447 ++pta_stats.pt_solutions_intersect_no_alias;
6448 return res;
6452 /* Dump points-to information to OUTFILE. */
6454 static void
6455 dump_sa_points_to_info (FILE *outfile)
6457 unsigned int i;
6459 fprintf (outfile, "\nPoints-to sets\n\n");
6461 if (dump_flags & TDF_STATS)
6463 fprintf (outfile, "Stats:\n");
6464 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
6465 fprintf (outfile, "Non-pointer vars: %d\n",
6466 stats.nonpointer_vars);
6467 fprintf (outfile, "Statically unified vars: %d\n",
6468 stats.unified_vars_static);
6469 fprintf (outfile, "Dynamically unified vars: %d\n",
6470 stats.unified_vars_dynamic);
6471 fprintf (outfile, "Iterations: %d\n", stats.iterations);
6472 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
6473 fprintf (outfile, "Number of implicit edges: %d\n",
6474 stats.num_implicit_edges);
6477 for (i = 1; i < varmap.length (); i++)
6479 varinfo_t vi = get_varinfo (i);
6480 if (!vi->may_have_pointers)
6481 continue;
6482 dump_solution_for_var (outfile, i);
6487 /* Debug points-to information to stderr. */
6489 DEBUG_FUNCTION void
6490 debug_sa_points_to_info (void)
6492 dump_sa_points_to_info (stderr);
6496 /* Initialize the always-existing constraint variables for NULL
6497 ANYTHING, READONLY, and INTEGER */
6499 static void
6500 init_base_vars (void)
6502 struct constraint_expr lhs, rhs;
6503 varinfo_t var_anything;
6504 varinfo_t var_nothing;
6505 varinfo_t var_readonly;
6506 varinfo_t var_escaped;
6507 varinfo_t var_nonlocal;
6508 varinfo_t var_storedanything;
6509 varinfo_t var_integer;
6511 /* Variable ID zero is reserved and should be NULL. */
6512 varmap.safe_push (NULL);
6514 /* Create the NULL variable, used to represent that a variable points
6515 to NULL. */
6516 var_nothing = new_var_info (NULL_TREE, "NULL");
6517 gcc_assert (var_nothing->id == nothing_id);
6518 var_nothing->is_artificial_var = 1;
6519 var_nothing->offset = 0;
6520 var_nothing->size = ~0;
6521 var_nothing->fullsize = ~0;
6522 var_nothing->is_special_var = 1;
6523 var_nothing->may_have_pointers = 0;
6524 var_nothing->is_global_var = 0;
6526 /* Create the ANYTHING variable, used to represent that a variable
6527 points to some unknown piece of memory. */
6528 var_anything = new_var_info (NULL_TREE, "ANYTHING");
6529 gcc_assert (var_anything->id == anything_id);
6530 var_anything->is_artificial_var = 1;
6531 var_anything->size = ~0;
6532 var_anything->offset = 0;
6533 var_anything->fullsize = ~0;
6534 var_anything->is_special_var = 1;
6536 /* Anything points to anything. This makes deref constraints just
6537 work in the presence of linked list and other p = *p type loops,
6538 by saying that *ANYTHING = ANYTHING. */
6539 lhs.type = SCALAR;
6540 lhs.var = anything_id;
6541 lhs.offset = 0;
6542 rhs.type = ADDRESSOF;
6543 rhs.var = anything_id;
6544 rhs.offset = 0;
6546 /* This specifically does not use process_constraint because
6547 process_constraint ignores all anything = anything constraints, since all
6548 but this one are redundant. */
6549 constraints.safe_push (new_constraint (lhs, rhs));
6551 /* Create the READONLY variable, used to represent that a variable
6552 points to readonly memory. */
6553 var_readonly = new_var_info (NULL_TREE, "READONLY");
6554 gcc_assert (var_readonly->id == readonly_id);
6555 var_readonly->is_artificial_var = 1;
6556 var_readonly->offset = 0;
6557 var_readonly->size = ~0;
6558 var_readonly->fullsize = ~0;
6559 var_readonly->is_special_var = 1;
6561 /* readonly memory points to anything, in order to make deref
6562 easier. In reality, it points to anything the particular
6563 readonly variable can point to, but we don't track this
6564 separately. */
6565 lhs.type = SCALAR;
6566 lhs.var = readonly_id;
6567 lhs.offset = 0;
6568 rhs.type = ADDRESSOF;
6569 rhs.var = readonly_id; /* FIXME */
6570 rhs.offset = 0;
6571 process_constraint (new_constraint (lhs, rhs));
6573 /* Create the ESCAPED variable, used to represent the set of escaped
6574 memory. */
6575 var_escaped = new_var_info (NULL_TREE, "ESCAPED");
6576 gcc_assert (var_escaped->id == escaped_id);
6577 var_escaped->is_artificial_var = 1;
6578 var_escaped->offset = 0;
6579 var_escaped->size = ~0;
6580 var_escaped->fullsize = ~0;
6581 var_escaped->is_special_var = 0;
6583 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6584 memory. */
6585 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL");
6586 gcc_assert (var_nonlocal->id == nonlocal_id);
6587 var_nonlocal->is_artificial_var = 1;
6588 var_nonlocal->offset = 0;
6589 var_nonlocal->size = ~0;
6590 var_nonlocal->fullsize = ~0;
6591 var_nonlocal->is_special_var = 1;
6593 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6594 lhs.type = SCALAR;
6595 lhs.var = escaped_id;
6596 lhs.offset = 0;
6597 rhs.type = DEREF;
6598 rhs.var = escaped_id;
6599 rhs.offset = 0;
6600 process_constraint (new_constraint (lhs, rhs));
6602 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6603 whole variable escapes. */
6604 lhs.type = SCALAR;
6605 lhs.var = escaped_id;
6606 lhs.offset = 0;
6607 rhs.type = SCALAR;
6608 rhs.var = escaped_id;
6609 rhs.offset = UNKNOWN_OFFSET;
6610 process_constraint (new_constraint (lhs, rhs));
6612 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6613 everything pointed to by escaped points to what global memory can
6614 point to. */
6615 lhs.type = DEREF;
6616 lhs.var = escaped_id;
6617 lhs.offset = 0;
6618 rhs.type = SCALAR;
6619 rhs.var = nonlocal_id;
6620 rhs.offset = 0;
6621 process_constraint (new_constraint (lhs, rhs));
6623 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6624 global memory may point to global memory and escaped memory. */
6625 lhs.type = SCALAR;
6626 lhs.var = nonlocal_id;
6627 lhs.offset = 0;
6628 rhs.type = ADDRESSOF;
6629 rhs.var = nonlocal_id;
6630 rhs.offset = 0;
6631 process_constraint (new_constraint (lhs, rhs));
6632 rhs.type = ADDRESSOF;
6633 rhs.var = escaped_id;
6634 rhs.offset = 0;
6635 process_constraint (new_constraint (lhs, rhs));
6637 /* Create the STOREDANYTHING variable, used to represent the set of
6638 variables stored to *ANYTHING. */
6639 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING");
6640 gcc_assert (var_storedanything->id == storedanything_id);
6641 var_storedanything->is_artificial_var = 1;
6642 var_storedanything->offset = 0;
6643 var_storedanything->size = ~0;
6644 var_storedanything->fullsize = ~0;
6645 var_storedanything->is_special_var = 0;
6647 /* Create the INTEGER variable, used to represent that a variable points
6648 to what an INTEGER "points to". */
6649 var_integer = new_var_info (NULL_TREE, "INTEGER");
6650 gcc_assert (var_integer->id == integer_id);
6651 var_integer->is_artificial_var = 1;
6652 var_integer->size = ~0;
6653 var_integer->fullsize = ~0;
6654 var_integer->offset = 0;
6655 var_integer->is_special_var = 1;
6657 /* INTEGER = ANYTHING, because we don't know where a dereference of
6658 a random integer will point to. */
6659 lhs.type = SCALAR;
6660 lhs.var = integer_id;
6661 lhs.offset = 0;
6662 rhs.type = ADDRESSOF;
6663 rhs.var = anything_id;
6664 rhs.offset = 0;
6665 process_constraint (new_constraint (lhs, rhs));
6668 /* Initialize things necessary to perform PTA */
6670 static void
6671 init_alias_vars (void)
6673 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
6675 bitmap_obstack_initialize (&pta_obstack);
6676 bitmap_obstack_initialize (&oldpta_obstack);
6677 bitmap_obstack_initialize (&predbitmap_obstack);
6679 constraint_pool = create_alloc_pool ("Constraint pool",
6680 sizeof (struct constraint), 30);
6681 variable_info_pool = create_alloc_pool ("Variable info pool",
6682 sizeof (struct variable_info), 30);
6683 constraints.create (8);
6684 varmap.create (8);
6685 vi_for_tree = new hash_map<tree, varinfo_t>;
6686 call_stmt_vars = new hash_map<gimple, varinfo_t>;
6688 memset (&stats, 0, sizeof (stats));
6689 shared_bitmap_table = new hash_table<shared_bitmap_hasher> (511);
6690 init_base_vars ();
6692 gcc_obstack_init (&fake_var_decl_obstack);
6694 final_solutions = new hash_map<varinfo_t, pt_solution *>;
6695 gcc_obstack_init (&final_solutions_obstack);
6698 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6699 predecessor edges. */
6701 static void
6702 remove_preds_and_fake_succs (constraint_graph_t graph)
6704 unsigned int i;
6706 /* Clear the implicit ref and address nodes from the successor
6707 lists. */
6708 for (i = 1; i < FIRST_REF_NODE; i++)
6710 if (graph->succs[i])
6711 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
6712 FIRST_REF_NODE * 2);
6715 /* Free the successor list for the non-ref nodes. */
6716 for (i = FIRST_REF_NODE + 1; i < graph->size; i++)
6718 if (graph->succs[i])
6719 BITMAP_FREE (graph->succs[i]);
6722 /* Now reallocate the size of the successor list as, and blow away
6723 the predecessor bitmaps. */
6724 graph->size = varmap.length ();
6725 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
6727 free (graph->implicit_preds);
6728 graph->implicit_preds = NULL;
6729 free (graph->preds);
6730 graph->preds = NULL;
6731 bitmap_obstack_release (&predbitmap_obstack);
6734 /* Solve the constraint set. */
6736 static void
6737 solve_constraints (void)
6739 struct scc_info *si;
6741 if (dump_file)
6742 fprintf (dump_file,
6743 "\nCollapsing static cycles and doing variable "
6744 "substitution\n");
6746 init_graph (varmap.length () * 2);
6748 if (dump_file)
6749 fprintf (dump_file, "Building predecessor graph\n");
6750 build_pred_graph ();
6752 if (dump_file)
6753 fprintf (dump_file, "Detecting pointer and location "
6754 "equivalences\n");
6755 si = perform_var_substitution (graph);
6757 if (dump_file)
6758 fprintf (dump_file, "Rewriting constraints and unifying "
6759 "variables\n");
6760 rewrite_constraints (graph, si);
6762 build_succ_graph ();
6764 free_var_substitution_info (si);
6766 /* Attach complex constraints to graph nodes. */
6767 move_complex_constraints (graph);
6769 if (dump_file)
6770 fprintf (dump_file, "Uniting pointer but not location equivalent "
6771 "variables\n");
6772 unite_pointer_equivalences (graph);
6774 if (dump_file)
6775 fprintf (dump_file, "Finding indirect cycles\n");
6776 find_indirect_cycles (graph);
6778 /* Implicit nodes and predecessors are no longer necessary at this
6779 point. */
6780 remove_preds_and_fake_succs (graph);
6782 if (dump_file && (dump_flags & TDF_GRAPH))
6784 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
6785 "in dot format:\n");
6786 dump_constraint_graph (dump_file);
6787 fprintf (dump_file, "\n\n");
6790 if (dump_file)
6791 fprintf (dump_file, "Solving graph\n");
6793 solve_graph (graph);
6795 if (dump_file && (dump_flags & TDF_GRAPH))
6797 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
6798 "in dot format:\n");
6799 dump_constraint_graph (dump_file);
6800 fprintf (dump_file, "\n\n");
6803 if (dump_file)
6804 dump_sa_points_to_info (dump_file);
6807 /* Create points-to sets for the current function. See the comments
6808 at the start of the file for an algorithmic overview. */
6810 static void
6811 compute_points_to_sets (void)
6813 basic_block bb;
6814 unsigned i;
6815 varinfo_t vi;
6817 timevar_push (TV_TREE_PTA);
6819 init_alias_vars ();
6821 intra_create_variable_infos (cfun);
6823 /* Now walk all statements and build the constraint set. */
6824 FOR_EACH_BB_FN (bb, cfun)
6826 gimple_stmt_iterator gsi;
6828 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6830 gimple phi = gsi_stmt (gsi);
6832 if (! virtual_operand_p (gimple_phi_result (phi)))
6833 find_func_aliases (cfun, phi);
6836 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6838 gimple stmt = gsi_stmt (gsi);
6840 find_func_aliases (cfun, stmt);
6844 if (dump_file)
6846 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
6847 dump_constraints (dump_file, 0);
6850 /* From the constraints compute the points-to sets. */
6851 solve_constraints ();
6853 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6854 cfun->gimple_df->escaped = find_what_var_points_to (get_varinfo (escaped_id));
6856 /* Make sure the ESCAPED solution (which is used as placeholder in
6857 other solutions) does not reference itself. This simplifies
6858 points-to solution queries. */
6859 cfun->gimple_df->escaped.escaped = 0;
6861 /* Compute the points-to sets for pointer SSA_NAMEs. */
6862 for (i = 0; i < num_ssa_names; ++i)
6864 tree ptr = ssa_name (i);
6865 if (ptr
6866 && POINTER_TYPE_P (TREE_TYPE (ptr)))
6867 find_what_p_points_to (ptr);
6870 /* Compute the call-used/clobbered sets. */
6871 FOR_EACH_BB_FN (bb, cfun)
6873 gimple_stmt_iterator gsi;
6875 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6877 gimple stmt = gsi_stmt (gsi);
6878 struct pt_solution *pt;
6879 if (!is_gimple_call (stmt))
6880 continue;
6882 pt = gimple_call_use_set (stmt);
6883 if (gimple_call_flags (stmt) & ECF_CONST)
6884 memset (pt, 0, sizeof (struct pt_solution));
6885 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
6887 *pt = find_what_var_points_to (vi);
6888 /* Escaped (and thus nonlocal) variables are always
6889 implicitly used by calls. */
6890 /* ??? ESCAPED can be empty even though NONLOCAL
6891 always escaped. */
6892 pt->nonlocal = 1;
6893 pt->escaped = 1;
6895 else
6897 /* If there is nothing special about this call then
6898 we have made everything that is used also escape. */
6899 *pt = cfun->gimple_df->escaped;
6900 pt->nonlocal = 1;
6903 pt = gimple_call_clobber_set (stmt);
6904 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
6905 memset (pt, 0, sizeof (struct pt_solution));
6906 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
6908 *pt = find_what_var_points_to (vi);
6909 /* Escaped (and thus nonlocal) variables are always
6910 implicitly clobbered by calls. */
6911 /* ??? ESCAPED can be empty even though NONLOCAL
6912 always escaped. */
6913 pt->nonlocal = 1;
6914 pt->escaped = 1;
6916 else
6918 /* If there is nothing special about this call then
6919 we have made everything that is used also escape. */
6920 *pt = cfun->gimple_df->escaped;
6921 pt->nonlocal = 1;
6926 timevar_pop (TV_TREE_PTA);
6930 /* Delete created points-to sets. */
6932 static void
6933 delete_points_to_sets (void)
6935 unsigned int i;
6937 delete shared_bitmap_table;
6938 shared_bitmap_table = NULL;
6939 if (dump_file && (dump_flags & TDF_STATS))
6940 fprintf (dump_file, "Points to sets created:%d\n",
6941 stats.points_to_sets_created);
6943 delete vi_for_tree;
6944 delete call_stmt_vars;
6945 bitmap_obstack_release (&pta_obstack);
6946 constraints.release ();
6948 for (i = 0; i < graph->size; i++)
6949 graph->complex[i].release ();
6950 free (graph->complex);
6952 free (graph->rep);
6953 free (graph->succs);
6954 free (graph->pe);
6955 free (graph->pe_rep);
6956 free (graph->indirect_cycles);
6957 free (graph);
6959 varmap.release ();
6960 free_alloc_pool (variable_info_pool);
6961 free_alloc_pool (constraint_pool);
6963 obstack_free (&fake_var_decl_obstack, NULL);
6965 delete final_solutions;
6966 obstack_free (&final_solutions_obstack, NULL);
6970 /* Compute points-to information for every SSA_NAME pointer in the
6971 current function and compute the transitive closure of escaped
6972 variables to re-initialize the call-clobber states of local variables. */
6974 unsigned int
6975 compute_may_aliases (void)
6977 if (cfun->gimple_df->ipa_pta)
6979 if (dump_file)
6981 fprintf (dump_file, "\nNot re-computing points-to information "
6982 "because IPA points-to information is available.\n\n");
6984 /* But still dump what we have remaining it. */
6985 dump_alias_info (dump_file);
6988 return 0;
6991 /* For each pointer P_i, determine the sets of variables that P_i may
6992 point-to. Compute the reachability set of escaped and call-used
6993 variables. */
6994 compute_points_to_sets ();
6996 /* Debugging dumps. */
6997 if (dump_file)
6998 dump_alias_info (dump_file);
7000 /* Deallocate memory used by aliasing data structures and the internal
7001 points-to solution. */
7002 delete_points_to_sets ();
7004 gcc_assert (!need_ssa_update_p (cfun));
7006 return 0;
7009 /* A dummy pass to cause points-to information to be computed via
7010 TODO_rebuild_alias. */
7012 namespace {
7014 const pass_data pass_data_build_alias =
7016 GIMPLE_PASS, /* type */
7017 "alias", /* name */
7018 OPTGROUP_NONE, /* optinfo_flags */
7019 TV_NONE, /* tv_id */
7020 ( PROP_cfg | PROP_ssa ), /* properties_required */
7021 0, /* properties_provided */
7022 0, /* properties_destroyed */
7023 0, /* todo_flags_start */
7024 TODO_rebuild_alias, /* todo_flags_finish */
7027 class pass_build_alias : public gimple_opt_pass
7029 public:
7030 pass_build_alias (gcc::context *ctxt)
7031 : gimple_opt_pass (pass_data_build_alias, ctxt)
7034 /* opt_pass methods: */
7035 virtual bool gate (function *) { return flag_tree_pta; }
7037 }; // class pass_build_alias
7039 } // anon namespace
7041 gimple_opt_pass *
7042 make_pass_build_alias (gcc::context *ctxt)
7044 return new pass_build_alias (ctxt);
7047 /* A dummy pass to cause points-to information to be computed via
7048 TODO_rebuild_alias. */
7050 namespace {
7052 const pass_data pass_data_build_ealias =
7054 GIMPLE_PASS, /* type */
7055 "ealias", /* name */
7056 OPTGROUP_NONE, /* optinfo_flags */
7057 TV_NONE, /* tv_id */
7058 ( PROP_cfg | PROP_ssa ), /* properties_required */
7059 0, /* properties_provided */
7060 0, /* properties_destroyed */
7061 0, /* todo_flags_start */
7062 TODO_rebuild_alias, /* todo_flags_finish */
7065 class pass_build_ealias : public gimple_opt_pass
7067 public:
7068 pass_build_ealias (gcc::context *ctxt)
7069 : gimple_opt_pass (pass_data_build_ealias, ctxt)
7072 /* opt_pass methods: */
7073 virtual bool gate (function *) { return flag_tree_pta; }
7075 }; // class pass_build_ealias
7077 } // anon namespace
7079 gimple_opt_pass *
7080 make_pass_build_ealias (gcc::context *ctxt)
7082 return new pass_build_ealias (ctxt);
7086 /* IPA PTA solutions for ESCAPED. */
7087 struct pt_solution ipa_escaped_pt
7088 = { true, false, false, false, false, false, false, false, NULL };
7090 /* Associate node with varinfo DATA. Worker for
7091 cgraph_for_node_and_aliases. */
7092 static bool
7093 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
7095 if ((node->alias || node->thunk.thunk_p)
7096 && node->analyzed)
7097 insert_vi_for_tree (node->decl, (varinfo_t)data);
7098 return false;
7101 /* Execute the driver for IPA PTA. */
7102 static unsigned int
7103 ipa_pta_execute (void)
7105 struct cgraph_node *node;
7106 varpool_node *var;
7107 int from;
7109 in_ipa_mode = 1;
7111 init_alias_vars ();
7113 if (dump_file && (dump_flags & TDF_DETAILS))
7115 symtab_node::dump_table (dump_file);
7116 fprintf (dump_file, "\n");
7119 /* Build the constraints. */
7120 FOR_EACH_DEFINED_FUNCTION (node)
7122 varinfo_t vi;
7123 /* Nodes without a body are not interesting. Especially do not
7124 visit clones at this point for now - we get duplicate decls
7125 there for inline clones at least. */
7126 if (!node->has_gimple_body_p () || node->clone_of)
7127 continue;
7128 node->get_body ();
7130 gcc_assert (!node->clone_of);
7132 vi = create_function_info_for (node->decl,
7133 alias_get_name (node->decl));
7134 node->call_for_symbol_thunks_and_aliases
7135 (associate_varinfo_to_alias, vi, true);
7138 /* Create constraints for global variables and their initializers. */
7139 FOR_EACH_VARIABLE (var)
7141 if (var->alias && var->analyzed)
7142 continue;
7144 get_vi_for_tree (var->decl);
7147 if (dump_file)
7149 fprintf (dump_file,
7150 "Generating constraints for global initializers\n\n");
7151 dump_constraints (dump_file, 0);
7152 fprintf (dump_file, "\n");
7154 from = constraints.length ();
7156 FOR_EACH_DEFINED_FUNCTION (node)
7158 struct function *func;
7159 basic_block bb;
7161 /* Nodes without a body are not interesting. */
7162 if (!node->has_gimple_body_p () || node->clone_of)
7163 continue;
7165 if (dump_file)
7167 fprintf (dump_file,
7168 "Generating constraints for %s", node->name ());
7169 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
7170 fprintf (dump_file, " (%s)",
7171 IDENTIFIER_POINTER
7172 (DECL_ASSEMBLER_NAME (node->decl)));
7173 fprintf (dump_file, "\n");
7176 func = DECL_STRUCT_FUNCTION (node->decl);
7177 gcc_assert (cfun == NULL);
7179 /* For externally visible or attribute used annotated functions use
7180 local constraints for their arguments.
7181 For local functions we see all callers and thus do not need initial
7182 constraints for parameters. */
7183 if (node->used_from_other_partition
7184 || node->externally_visible
7185 || node->force_output)
7187 intra_create_variable_infos (func);
7189 /* We also need to make function return values escape. Nothing
7190 escapes by returning from main though. */
7191 if (!MAIN_NAME_P (DECL_NAME (node->decl)))
7193 varinfo_t fi, rvi;
7194 fi = lookup_vi_for_tree (node->decl);
7195 rvi = first_vi_for_offset (fi, fi_result);
7196 if (rvi && rvi->offset == fi_result)
7198 struct constraint_expr includes;
7199 struct constraint_expr var;
7200 includes.var = escaped_id;
7201 includes.offset = 0;
7202 includes.type = SCALAR;
7203 var.var = rvi->id;
7204 var.offset = 0;
7205 var.type = SCALAR;
7206 process_constraint (new_constraint (includes, var));
7211 /* Build constriants for the function body. */
7212 FOR_EACH_BB_FN (bb, func)
7214 gimple_stmt_iterator gsi;
7216 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7217 gsi_next (&gsi))
7219 gimple phi = gsi_stmt (gsi);
7221 if (! virtual_operand_p (gimple_phi_result (phi)))
7222 find_func_aliases (func, phi);
7225 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7227 gimple stmt = gsi_stmt (gsi);
7229 find_func_aliases (func, stmt);
7230 find_func_clobbers (func, stmt);
7234 if (dump_file)
7236 fprintf (dump_file, "\n");
7237 dump_constraints (dump_file, from);
7238 fprintf (dump_file, "\n");
7240 from = constraints.length ();
7243 /* From the constraints compute the points-to sets. */
7244 solve_constraints ();
7246 /* Compute the global points-to sets for ESCAPED.
7247 ??? Note that the computed escape set is not correct
7248 for the whole unit as we fail to consider graph edges to
7249 externally visible functions. */
7250 ipa_escaped_pt = find_what_var_points_to (get_varinfo (escaped_id));
7252 /* Make sure the ESCAPED solution (which is used as placeholder in
7253 other solutions) does not reference itself. This simplifies
7254 points-to solution queries. */
7255 ipa_escaped_pt.ipa_escaped = 0;
7257 /* Assign the points-to sets to the SSA names in the unit. */
7258 FOR_EACH_DEFINED_FUNCTION (node)
7260 tree ptr;
7261 struct function *fn;
7262 unsigned i;
7263 basic_block bb;
7265 /* Nodes without a body are not interesting. */
7266 if (!node->has_gimple_body_p () || node->clone_of)
7267 continue;
7269 fn = DECL_STRUCT_FUNCTION (node->decl);
7271 /* Compute the points-to sets for pointer SSA_NAMEs. */
7272 FOR_EACH_VEC_ELT (*fn->gimple_df->ssa_names, i, ptr)
7274 if (ptr
7275 && POINTER_TYPE_P (TREE_TYPE (ptr)))
7276 find_what_p_points_to (ptr);
7279 /* Compute the call-use and call-clobber sets for indirect calls
7280 and calls to external functions. */
7281 FOR_EACH_BB_FN (bb, fn)
7283 gimple_stmt_iterator gsi;
7285 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7287 gimple stmt = gsi_stmt (gsi);
7288 struct pt_solution *pt;
7289 varinfo_t vi, fi;
7290 tree decl;
7292 if (!is_gimple_call (stmt))
7293 continue;
7295 /* Handle direct calls to functions with body. */
7296 decl = gimple_call_fndecl (stmt);
7297 if (decl
7298 && (fi = lookup_vi_for_tree (decl))
7299 && fi->is_fn_info)
7301 *gimple_call_clobber_set (stmt)
7302 = find_what_var_points_to
7303 (first_vi_for_offset (fi, fi_clobbers));
7304 *gimple_call_use_set (stmt)
7305 = find_what_var_points_to
7306 (first_vi_for_offset (fi, fi_uses));
7308 /* Handle direct calls to external functions. */
7309 else if (decl)
7311 pt = gimple_call_use_set (stmt);
7312 if (gimple_call_flags (stmt) & ECF_CONST)
7313 memset (pt, 0, sizeof (struct pt_solution));
7314 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7316 *pt = find_what_var_points_to (vi);
7317 /* Escaped (and thus nonlocal) variables are always
7318 implicitly used by calls. */
7319 /* ??? ESCAPED can be empty even though NONLOCAL
7320 always escaped. */
7321 pt->nonlocal = 1;
7322 pt->ipa_escaped = 1;
7324 else
7326 /* If there is nothing special about this call then
7327 we have made everything that is used also escape. */
7328 *pt = ipa_escaped_pt;
7329 pt->nonlocal = 1;
7332 pt = gimple_call_clobber_set (stmt);
7333 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7334 memset (pt, 0, sizeof (struct pt_solution));
7335 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7337 *pt = find_what_var_points_to (vi);
7338 /* Escaped (and thus nonlocal) variables are always
7339 implicitly clobbered by calls. */
7340 /* ??? ESCAPED can be empty even though NONLOCAL
7341 always escaped. */
7342 pt->nonlocal = 1;
7343 pt->ipa_escaped = 1;
7345 else
7347 /* If there is nothing special about this call then
7348 we have made everything that is used also escape. */
7349 *pt = ipa_escaped_pt;
7350 pt->nonlocal = 1;
7353 /* Handle indirect calls. */
7354 else if (!decl
7355 && (fi = get_fi_for_callee (stmt)))
7357 /* We need to accumulate all clobbers/uses of all possible
7358 callees. */
7359 fi = get_varinfo (find (fi->id));
7360 /* If we cannot constrain the set of functions we'll end up
7361 calling we end up using/clobbering everything. */
7362 if (bitmap_bit_p (fi->solution, anything_id)
7363 || bitmap_bit_p (fi->solution, nonlocal_id)
7364 || bitmap_bit_p (fi->solution, escaped_id))
7366 pt_solution_reset (gimple_call_clobber_set (stmt));
7367 pt_solution_reset (gimple_call_use_set (stmt));
7369 else
7371 bitmap_iterator bi;
7372 unsigned i;
7373 struct pt_solution *uses, *clobbers;
7375 uses = gimple_call_use_set (stmt);
7376 clobbers = gimple_call_clobber_set (stmt);
7377 memset (uses, 0, sizeof (struct pt_solution));
7378 memset (clobbers, 0, sizeof (struct pt_solution));
7379 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
7381 struct pt_solution sol;
7383 vi = get_varinfo (i);
7384 if (!vi->is_fn_info)
7386 /* ??? We could be more precise here? */
7387 uses->nonlocal = 1;
7388 uses->ipa_escaped = 1;
7389 clobbers->nonlocal = 1;
7390 clobbers->ipa_escaped = 1;
7391 continue;
7394 if (!uses->anything)
7396 sol = find_what_var_points_to
7397 (first_vi_for_offset (vi, fi_uses));
7398 pt_solution_ior_into (uses, &sol);
7400 if (!clobbers->anything)
7402 sol = find_what_var_points_to
7403 (first_vi_for_offset (vi, fi_clobbers));
7404 pt_solution_ior_into (clobbers, &sol);
7412 fn->gimple_df->ipa_pta = true;
7415 delete_points_to_sets ();
7417 in_ipa_mode = 0;
7419 return 0;
7422 namespace {
7424 const pass_data pass_data_ipa_pta =
7426 SIMPLE_IPA_PASS, /* type */
7427 "pta", /* name */
7428 OPTGROUP_NONE, /* optinfo_flags */
7429 TV_IPA_PTA, /* tv_id */
7430 0, /* properties_required */
7431 0, /* properties_provided */
7432 0, /* properties_destroyed */
7433 0, /* todo_flags_start */
7434 0, /* todo_flags_finish */
7437 class pass_ipa_pta : public simple_ipa_opt_pass
7439 public:
7440 pass_ipa_pta (gcc::context *ctxt)
7441 : simple_ipa_opt_pass (pass_data_ipa_pta, ctxt)
7444 /* opt_pass methods: */
7445 virtual bool gate (function *)
7447 return (optimize
7448 && flag_ipa_pta
7449 /* Don't bother doing anything if the program has errors. */
7450 && !seen_error ());
7453 virtual unsigned int execute (function *) { return ipa_pta_execute (); }
7455 }; // class pass_ipa_pta
7457 } // anon namespace
7459 simple_ipa_opt_pass *
7460 make_pass_ipa_pta (gcc::context *ctxt)
7462 return new pass_ipa_pta (ctxt);