Don't warn when alignment of global common data exceeds maximum alignment.
[official-gcc.git] / gcc / tree-ssa-structalias.c
blobfb0e429970325b58901ccc260db040f7d36be4ff
1 /* Tree based points-to analysis
2 Copyright (C) 2005-2021 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 "backend.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "alloc-pool.h"
29 #include "tree-pass.h"
30 #include "ssa.h"
31 #include "cgraph.h"
32 #include "tree-pretty-print.h"
33 #include "diagnostic-core.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
36 #include "stmt.h"
37 #include "gimple-iterator.h"
38 #include "tree-into-ssa.h"
39 #include "tree-dfa.h"
40 #include "gimple-walk.h"
41 #include "varasm.h"
42 #include "stringpool.h"
43 #include "attribs.h"
44 #include "tree-ssa.h"
45 #include "tree-cfg.h"
46 #include "gimple-range.h"
48 /* The idea behind this analyzer is to generate set constraints from the
49 program, then solve the resulting constraints in order to generate the
50 points-to sets.
52 Set constraints are a way of modeling program analysis problems that
53 involve sets. They consist of an inclusion constraint language,
54 describing the variables (each variable is a set) and operations that
55 are involved on the variables, and a set of rules that derive facts
56 from these operations. To solve a system of set constraints, you derive
57 all possible facts under the rules, which gives you the correct sets
58 as a consequence.
60 See "Efficient Field-sensitive pointer analysis for C" by "David
61 J. Pearce and Paul H. J. Kelly and Chris Hankin", at
62 http://citeseer.ist.psu.edu/pearce04efficient.html
64 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
65 of C Code in a Second" by "Nevin Heintze and Olivier Tardieu" at
66 http://citeseer.ist.psu.edu/heintze01ultrafast.html
68 There are three types of real constraint expressions, DEREF,
69 ADDRESSOF, and SCALAR. Each constraint expression consists
70 of a constraint type, a variable, and an offset.
72 SCALAR is a constraint expression type used to represent x, whether
73 it appears on the LHS or the RHS of a statement.
74 DEREF is a constraint expression type used to represent *x, whether
75 it appears on the LHS or the RHS of a statement.
76 ADDRESSOF is a constraint expression used to represent &x, whether
77 it appears on the LHS or the RHS of a statement.
79 Each pointer variable in the program is assigned an integer id, and
80 each field of a structure variable is assigned an integer id as well.
82 Structure variables are linked to their list of fields through a "next
83 field" in each variable that points to the next field in offset
84 order.
85 Each variable for a structure field has
87 1. "size", that tells the size in bits of that field.
88 2. "fullsize", that tells the size in bits of the entire structure.
89 3. "offset", that tells the offset in bits from the beginning of the
90 structure to this field.
92 Thus,
93 struct f
95 int a;
96 int b;
97 } foo;
98 int *bar;
100 looks like
102 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
103 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
104 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
107 In order to solve the system of set constraints, the following is
108 done:
110 1. Each constraint variable x has a solution set associated with it,
111 Sol(x).
113 2. Constraints are separated into direct, copy, and complex.
114 Direct constraints are ADDRESSOF constraints that require no extra
115 processing, such as P = &Q
116 Copy constraints are those of the form P = Q.
117 Complex constraints are all the constraints involving dereferences
118 and offsets (including offsetted copies).
120 3. All direct constraints of the form P = &Q are processed, such
121 that Q is added to Sol(P)
123 4. All complex constraints for a given constraint variable are stored in a
124 linked list attached to that variable's node.
126 5. A directed graph is built out of the copy constraints. Each
127 constraint variable is a node in the graph, and an edge from
128 Q to P is added for each copy constraint of the form P = Q
130 6. The graph is then walked, and solution sets are
131 propagated along the copy edges, such that an edge from Q to P
132 causes Sol(P) <- Sol(P) union Sol(Q).
134 7. As we visit each node, all complex constraints associated with
135 that node are processed by adding appropriate copy edges to the graph, or the
136 appropriate variables to the solution set.
138 8. The process of walking the graph is iterated until no solution
139 sets change.
141 Prior to walking the graph in steps 6 and 7, We perform static
142 cycle elimination on the constraint graph, as well
143 as off-line variable substitution.
145 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
146 on and turned into anything), but isn't. You can just see what offset
147 inside the pointed-to struct it's going to access.
149 TODO: Constant bounded arrays can be handled as if they were structs of the
150 same number of elements.
152 TODO: Modeling heap and incoming pointers becomes much better if we
153 add fields to them as we discover them, which we could do.
155 TODO: We could handle unions, but to be honest, it's probably not
156 worth the pain or slowdown. */
158 /* IPA-PTA optimizations possible.
160 When the indirect function called is ANYTHING we can add disambiguation
161 based on the function signatures (or simply the parameter count which
162 is the varinfo size). We also do not need to consider functions that
163 do not have their address taken.
165 The is_global_var bit which marks escape points is overly conservative
166 in IPA mode. Split it to is_escape_point and is_global_var - only
167 externally visible globals are escape points in IPA mode.
168 There is now is_ipa_escape_point but this is only used in a few
169 selected places.
171 The way we introduce DECL_PT_UID to avoid fixing up all points-to
172 sets in the translation unit when we copy a DECL during inlining
173 pessimizes precision. The advantage is that the DECL_PT_UID keeps
174 compile-time and memory usage overhead low - the points-to sets
175 do not grow or get unshared as they would during a fixup phase.
176 An alternative solution is to delay IPA PTA until after all
177 inlining transformations have been applied.
179 The way we propagate clobber/use information isn't optimized.
180 It should use a new complex constraint that properly filters
181 out local variables of the callee (though that would make
182 the sets invalid after inlining). OTOH we might as well
183 admit defeat to WHOPR and simply do all the clobber/use analysis
184 and propagation after PTA finished but before we threw away
185 points-to information for memory variables. WHOPR and PTA
186 do not play along well anyway - the whole constraint solving
187 would need to be done in WPA phase and it will be very interesting
188 to apply the results to local SSA names during LTRANS phase.
190 We probably should compute a per-function unit-ESCAPE solution
191 propagating it simply like the clobber / uses solutions. The
192 solution can go alongside the non-IPA escaped solution and be
193 used to query which vars escape the unit through a function.
194 This is also required to make the escaped-HEAP trick work in IPA mode.
196 We never put function decls in points-to sets so we do not
197 keep the set of called functions for indirect calls.
199 And probably more. */
201 static bool use_field_sensitive = true;
202 static int in_ipa_mode = 0;
204 /* Used for predecessor bitmaps. */
205 static bitmap_obstack predbitmap_obstack;
207 /* Used for points-to sets. */
208 static bitmap_obstack pta_obstack;
210 /* Used for oldsolution members of variables. */
211 static bitmap_obstack oldpta_obstack;
213 /* Used for per-solver-iteration bitmaps. */
214 static bitmap_obstack iteration_obstack;
216 static unsigned int create_variable_info_for (tree, const char *, bool);
217 typedef struct constraint_graph *constraint_graph_t;
218 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
220 struct constraint;
221 typedef struct constraint *constraint_t;
224 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
225 if (a) \
226 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
228 static struct constraint_stats
230 unsigned int total_vars;
231 unsigned int nonpointer_vars;
232 unsigned int unified_vars_static;
233 unsigned int unified_vars_dynamic;
234 unsigned int iterations;
235 unsigned int num_edges;
236 unsigned int num_implicit_edges;
237 unsigned int points_to_sets_created;
238 } stats;
240 struct variable_info
242 /* ID of this variable */
243 unsigned int id;
245 /* True if this is a variable created by the constraint analysis, such as
246 heap variables and constraints we had to break up. */
247 unsigned int is_artificial_var : 1;
249 /* True if this is a special variable whose solution set should not be
250 changed. */
251 unsigned int is_special_var : 1;
253 /* True for variables whose size is not known or variable. */
254 unsigned int is_unknown_size_var : 1;
256 /* True for (sub-)fields that represent a whole variable. */
257 unsigned int is_full_var : 1;
259 /* True if this is a heap variable. */
260 unsigned int is_heap_var : 1;
262 /* True if this is a register variable. */
263 unsigned int is_reg_var : 1;
265 /* True if this field may contain pointers. */
266 unsigned int may_have_pointers : 1;
268 /* True if this field has only restrict qualified pointers. */
269 unsigned int only_restrict_pointers : 1;
271 /* True if this represents a heap var created for a restrict qualified
272 pointer. */
273 unsigned int is_restrict_var : 1;
275 /* True if this represents a global variable. */
276 unsigned int is_global_var : 1;
278 /* True if this represents a module escape point for IPA analysis. */
279 unsigned int is_ipa_escape_point : 1;
281 /* True if this represents a IPA function info. */
282 unsigned int is_fn_info : 1;
284 /* True if this appears as RHS in a ADDRESSOF constraint. */
285 unsigned int address_taken : 1;
287 /* ??? Store somewhere better. */
288 unsigned short ruid;
290 /* The ID of the variable for the next field in this structure
291 or zero for the last field in this structure. */
292 unsigned next;
294 /* The ID of the variable for the first field in this structure. */
295 unsigned head;
297 /* Offset of this variable, in bits, from the base variable */
298 unsigned HOST_WIDE_INT offset;
300 /* Size of the variable, in bits. */
301 unsigned HOST_WIDE_INT size;
303 /* Full size of the base variable, in bits. */
304 unsigned HOST_WIDE_INT fullsize;
306 /* In IPA mode the shadow UID in case the variable needs to be duplicated in
307 the final points-to solution because it reaches its containing
308 function recursively. Zero if none is needed. */
309 unsigned int shadow_var_uid;
311 /* Name of this variable */
312 const char *name;
314 /* Tree that this variable is associated with. */
315 tree decl;
317 /* Points-to set for this variable. */
318 bitmap solution;
320 /* Old points-to set for this variable. */
321 bitmap oldsolution;
323 typedef struct variable_info *varinfo_t;
325 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
326 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
327 unsigned HOST_WIDE_INT);
328 static varinfo_t lookup_vi_for_tree (tree);
329 static inline bool type_can_have_subvars (const_tree);
330 static void make_param_constraints (varinfo_t);
332 /* Pool of variable info structures. */
333 static object_allocator<variable_info> variable_info_pool
334 ("Variable info pool");
336 /* Map varinfo to final pt_solution. */
337 static hash_map<varinfo_t, pt_solution *> *final_solutions;
338 struct obstack final_solutions_obstack;
340 /* Table of variable info structures for constraint variables.
341 Indexed directly by variable info id. */
342 static vec<varinfo_t> varmap;
344 /* Return the varmap element N */
346 static inline varinfo_t
347 get_varinfo (unsigned int n)
349 return varmap[n];
352 /* Return the next variable in the list of sub-variables of VI
353 or NULL if VI is the last sub-variable. */
355 static inline varinfo_t
356 vi_next (varinfo_t vi)
358 return get_varinfo (vi->next);
361 /* Static IDs for the special variables. Variable ID zero is unused
362 and used as terminator for the sub-variable chain. */
363 enum { nothing_id = 1, anything_id = 2, string_id = 3,
364 escaped_id = 4, nonlocal_id = 5,
365 storedanything_id = 6, integer_id = 7 };
367 /* Return a new variable info structure consisting for a variable
368 named NAME, and using constraint graph node NODE. Append it
369 to the vector of variable info structures. */
371 static varinfo_t
372 new_var_info (tree t, const char *name, bool add_id)
374 unsigned index = varmap.length ();
375 varinfo_t ret = variable_info_pool.allocate ();
377 if (dump_file && add_id)
379 char *tempname = xasprintf ("%s(%d)", name, index);
380 name = ggc_strdup (tempname);
381 free (tempname);
384 ret->id = index;
385 ret->name = name;
386 ret->decl = t;
387 /* Vars without decl are artificial and do not have sub-variables. */
388 ret->is_artificial_var = (t == NULL_TREE);
389 ret->is_special_var = false;
390 ret->is_unknown_size_var = false;
391 ret->is_full_var = (t == NULL_TREE);
392 ret->is_heap_var = false;
393 ret->may_have_pointers = true;
394 ret->only_restrict_pointers = false;
395 ret->is_restrict_var = false;
396 ret->ruid = 0;
397 ret->is_global_var = (t == NULL_TREE);
398 ret->is_ipa_escape_point = false;
399 ret->is_fn_info = false;
400 ret->address_taken = false;
401 if (t && DECL_P (t))
402 ret->is_global_var = (is_global_var (t)
403 /* We have to treat even local register variables
404 as escape points. */
405 || (VAR_P (t) && DECL_HARD_REGISTER (t)));
406 ret->is_reg_var = (t && TREE_CODE (t) == SSA_NAME);
407 ret->solution = BITMAP_ALLOC (&pta_obstack);
408 ret->oldsolution = NULL;
409 ret->next = 0;
410 ret->shadow_var_uid = 0;
411 ret->head = ret->id;
413 stats.total_vars++;
415 varmap.safe_push (ret);
417 return ret;
420 /* A map mapping call statements to per-stmt variables for uses
421 and clobbers specific to the call. */
422 static hash_map<gimple *, varinfo_t> *call_stmt_vars;
424 /* Lookup or create the variable for the call statement CALL. */
426 static varinfo_t
427 get_call_vi (gcall *call)
429 varinfo_t vi, vi2;
431 bool existed;
432 varinfo_t *slot_p = &call_stmt_vars->get_or_insert (call, &existed);
433 if (existed)
434 return *slot_p;
436 vi = new_var_info (NULL_TREE, "CALLUSED", true);
437 vi->offset = 0;
438 vi->size = 1;
439 vi->fullsize = 2;
440 vi->is_full_var = true;
441 vi->is_reg_var = true;
443 vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED", true);
444 vi2->offset = 1;
445 vi2->size = 1;
446 vi2->fullsize = 2;
447 vi2->is_full_var = true;
448 vi2->is_reg_var = true;
450 vi->next = vi2->id;
452 *slot_p = vi;
453 return vi;
456 /* Lookup the variable for the call statement CALL representing
457 the uses. Returns NULL if there is nothing special about this call. */
459 static varinfo_t
460 lookup_call_use_vi (gcall *call)
462 varinfo_t *slot_p = call_stmt_vars->get (call);
463 if (slot_p)
464 return *slot_p;
466 return NULL;
469 /* Lookup the variable for the call statement CALL representing
470 the clobbers. Returns NULL if there is nothing special about this call. */
472 static varinfo_t
473 lookup_call_clobber_vi (gcall *call)
475 varinfo_t uses = lookup_call_use_vi (call);
476 if (!uses)
477 return NULL;
479 return vi_next (uses);
482 /* Lookup or create the variable for the call statement CALL representing
483 the uses. */
485 static varinfo_t
486 get_call_use_vi (gcall *call)
488 return get_call_vi (call);
491 /* Lookup or create the variable for the call statement CALL representing
492 the clobbers. */
494 static varinfo_t ATTRIBUTE_UNUSED
495 get_call_clobber_vi (gcall *call)
497 return vi_next (get_call_vi (call));
501 enum constraint_expr_type {SCALAR, DEREF, ADDRESSOF};
503 /* An expression that appears in a constraint. */
505 struct constraint_expr
507 /* Constraint type. */
508 constraint_expr_type type;
510 /* Variable we are referring to in the constraint. */
511 unsigned int var;
513 /* Offset, in bits, of this constraint from the beginning of
514 variables it ends up referring to.
516 IOW, in a deref constraint, we would deref, get the result set,
517 then add OFFSET to each member. */
518 HOST_WIDE_INT offset;
521 /* Use 0x8000... as special unknown offset. */
522 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
524 typedef struct constraint_expr ce_s;
525 static void get_constraint_for_1 (tree, vec<ce_s> *, bool, bool);
526 static void get_constraint_for (tree, vec<ce_s> *);
527 static void get_constraint_for_rhs (tree, vec<ce_s> *);
528 static void do_deref (vec<ce_s> *);
530 /* Our set constraints are made up of two constraint expressions, one
531 LHS, and one RHS.
533 As described in the introduction, our set constraints each represent an
534 operation between set valued variables.
536 struct constraint
538 struct constraint_expr lhs;
539 struct constraint_expr rhs;
542 /* List of constraints that we use to build the constraint graph from. */
544 static vec<constraint_t> constraints;
545 static object_allocator<constraint> constraint_pool ("Constraint pool");
547 /* The constraint graph is represented as an array of bitmaps
548 containing successor nodes. */
550 struct constraint_graph
552 /* Size of this graph, which may be different than the number of
553 nodes in the variable map. */
554 unsigned int size;
556 /* Explicit successors of each node. */
557 bitmap *succs;
559 /* Implicit predecessors of each node (Used for variable
560 substitution). */
561 bitmap *implicit_preds;
563 /* Explicit predecessors of each node (Used for variable substitution). */
564 bitmap *preds;
566 /* Indirect cycle representatives, or -1 if the node has no indirect
567 cycles. */
568 int *indirect_cycles;
570 /* Representative node for a node. rep[a] == a unless the node has
571 been unified. */
572 unsigned int *rep;
574 /* Equivalence class representative for a label. This is used for
575 variable substitution. */
576 int *eq_rep;
578 /* Pointer equivalence label for a node. All nodes with the same
579 pointer equivalence label can be unified together at some point
580 (either during constraint optimization or after the constraint
581 graph is built). */
582 unsigned int *pe;
584 /* Pointer equivalence representative for a label. This is used to
585 handle nodes that are pointer equivalent but not location
586 equivalent. We can unite these once the addressof constraints
587 are transformed into initial points-to sets. */
588 int *pe_rep;
590 /* Pointer equivalence label for each node, used during variable
591 substitution. */
592 unsigned int *pointer_label;
594 /* Location equivalence label for each node, used during location
595 equivalence finding. */
596 unsigned int *loc_label;
598 /* Pointed-by set for each node, used during location equivalence
599 finding. This is pointed-by rather than pointed-to, because it
600 is constructed using the predecessor graph. */
601 bitmap *pointed_by;
603 /* Points to sets for pointer equivalence. This is *not* the actual
604 points-to sets for nodes. */
605 bitmap *points_to;
607 /* Bitmap of nodes where the bit is set if the node is a direct
608 node. Used for variable substitution. */
609 sbitmap direct_nodes;
611 /* Bitmap of nodes where the bit is set if the node is address
612 taken. Used for variable substitution. */
613 bitmap address_taken;
615 /* Vector of complex constraints for each graph node. Complex
616 constraints are those involving dereferences or offsets that are
617 not 0. */
618 vec<constraint_t> *complex;
621 static constraint_graph_t graph;
623 /* During variable substitution and the offline version of indirect
624 cycle finding, we create nodes to represent dereferences and
625 address taken constraints. These represent where these start and
626 end. */
627 #define FIRST_REF_NODE (varmap).length ()
628 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
630 /* Return the representative node for NODE, if NODE has been unioned
631 with another NODE.
632 This function performs path compression along the way to finding
633 the representative. */
635 static unsigned int
636 find (unsigned int node)
638 gcc_checking_assert (node < graph->size);
639 if (graph->rep[node] != node)
640 return graph->rep[node] = find (graph->rep[node]);
641 return node;
644 /* Union the TO and FROM nodes to the TO nodes.
645 Note that at some point in the future, we may want to do
646 union-by-rank, in which case we are going to have to return the
647 node we unified to. */
649 static bool
650 unite (unsigned int to, unsigned int from)
652 gcc_checking_assert (to < graph->size && from < graph->size);
653 if (to != from && graph->rep[from] != to)
655 graph->rep[from] = to;
656 return true;
658 return false;
661 /* Create a new constraint consisting of LHS and RHS expressions. */
663 static constraint_t
664 new_constraint (const struct constraint_expr lhs,
665 const struct constraint_expr rhs)
667 constraint_t ret = constraint_pool.allocate ();
668 ret->lhs = lhs;
669 ret->rhs = rhs;
670 return ret;
673 /* Print out constraint C to FILE. */
675 static void
676 dump_constraint (FILE *file, constraint_t c)
678 if (c->lhs.type == ADDRESSOF)
679 fprintf (file, "&");
680 else if (c->lhs.type == DEREF)
681 fprintf (file, "*");
682 if (dump_file)
683 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
684 else
685 fprintf (file, "V%d", c->lhs.var);
686 if (c->lhs.offset == UNKNOWN_OFFSET)
687 fprintf (file, " + UNKNOWN");
688 else if (c->lhs.offset != 0)
689 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
690 fprintf (file, " = ");
691 if (c->rhs.type == ADDRESSOF)
692 fprintf (file, "&");
693 else if (c->rhs.type == DEREF)
694 fprintf (file, "*");
695 if (dump_file)
696 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
697 else
698 fprintf (file, "V%d", c->rhs.var);
699 if (c->rhs.offset == UNKNOWN_OFFSET)
700 fprintf (file, " + UNKNOWN");
701 else if (c->rhs.offset != 0)
702 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
706 void debug_constraint (constraint_t);
707 void debug_constraints (void);
708 void debug_constraint_graph (void);
709 void debug_solution_for_var (unsigned int);
710 void debug_sa_points_to_info (void);
711 void debug_varinfo (varinfo_t);
712 void debug_varmap (void);
714 /* Print out constraint C to stderr. */
716 DEBUG_FUNCTION void
717 debug_constraint (constraint_t c)
719 dump_constraint (stderr, c);
720 fprintf (stderr, "\n");
723 /* Print out all constraints to FILE */
725 static void
726 dump_constraints (FILE *file, int from)
728 int i;
729 constraint_t c;
730 for (i = from; constraints.iterate (i, &c); i++)
731 if (c)
733 dump_constraint (file, c);
734 fprintf (file, "\n");
738 /* Print out all constraints to stderr. */
740 DEBUG_FUNCTION void
741 debug_constraints (void)
743 dump_constraints (stderr, 0);
746 /* Print the constraint graph in dot format. */
748 static void
749 dump_constraint_graph (FILE *file)
751 unsigned int i;
753 /* Only print the graph if it has already been initialized: */
754 if (!graph)
755 return;
757 /* Prints the header of the dot file: */
758 fprintf (file, "strict digraph {\n");
759 fprintf (file, " node [\n shape = box\n ]\n");
760 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
761 fprintf (file, "\n // List of nodes and complex constraints in "
762 "the constraint graph:\n");
764 /* The next lines print the nodes in the graph together with the
765 complex constraints attached to them. */
766 for (i = 1; i < graph->size; i++)
768 if (i == FIRST_REF_NODE)
769 continue;
770 if (find (i) != i)
771 continue;
772 if (i < FIRST_REF_NODE)
773 fprintf (file, "\"%s\"", get_varinfo (i)->name);
774 else
775 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
776 if (graph->complex[i].exists ())
778 unsigned j;
779 constraint_t c;
780 fprintf (file, " [label=\"\\N\\n");
781 for (j = 0; graph->complex[i].iterate (j, &c); ++j)
783 dump_constraint (file, c);
784 fprintf (file, "\\l");
786 fprintf (file, "\"]");
788 fprintf (file, ";\n");
791 /* Go over the edges. */
792 fprintf (file, "\n // Edges in the constraint graph:\n");
793 for (i = 1; i < graph->size; i++)
795 unsigned j;
796 bitmap_iterator bi;
797 if (find (i) != i)
798 continue;
799 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
801 unsigned to = find (j);
802 if (i == to)
803 continue;
804 if (i < FIRST_REF_NODE)
805 fprintf (file, "\"%s\"", get_varinfo (i)->name);
806 else
807 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
808 fprintf (file, " -> ");
809 if (to < FIRST_REF_NODE)
810 fprintf (file, "\"%s\"", get_varinfo (to)->name);
811 else
812 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name);
813 fprintf (file, ";\n");
817 /* Prints the tail of the dot file. */
818 fprintf (file, "}\n");
821 /* Print out the constraint graph to stderr. */
823 DEBUG_FUNCTION void
824 debug_constraint_graph (void)
826 dump_constraint_graph (stderr);
829 /* SOLVER FUNCTIONS
831 The solver is a simple worklist solver, that works on the following
832 algorithm:
834 sbitmap changed_nodes = all zeroes;
835 changed_count = 0;
836 For each node that is not already collapsed:
837 changed_count++;
838 set bit in changed nodes
840 while (changed_count > 0)
842 compute topological ordering for constraint graph
844 find and collapse cycles in the constraint graph (updating
845 changed if necessary)
847 for each node (n) in the graph in topological order:
848 changed_count--;
850 Process each complex constraint associated with the node,
851 updating changed if necessary.
853 For each outgoing edge from n, propagate the solution from n to
854 the destination of the edge, updating changed as necessary.
856 } */
858 /* Return true if two constraint expressions A and B are equal. */
860 static bool
861 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
863 return a.type == b.type && a.var == b.var && a.offset == b.offset;
866 /* Return true if constraint expression A is less than constraint expression
867 B. This is just arbitrary, but consistent, in order to give them an
868 ordering. */
870 static bool
871 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
873 if (a.type == b.type)
875 if (a.var == b.var)
876 return a.offset < b.offset;
877 else
878 return a.var < b.var;
880 else
881 return a.type < b.type;
884 /* Return true if constraint A is less than constraint B. This is just
885 arbitrary, but consistent, in order to give them an ordering. */
887 static bool
888 constraint_less (const constraint_t &a, const constraint_t &b)
890 if (constraint_expr_less (a->lhs, b->lhs))
891 return true;
892 else if (constraint_expr_less (b->lhs, a->lhs))
893 return false;
894 else
895 return constraint_expr_less (a->rhs, b->rhs);
898 /* Return true if two constraints A and B are equal. */
900 static bool
901 constraint_equal (struct constraint a, struct constraint b)
903 return constraint_expr_equal (a.lhs, b.lhs)
904 && constraint_expr_equal (a.rhs, b.rhs);
908 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
910 static constraint_t
911 constraint_vec_find (vec<constraint_t> vec,
912 struct constraint lookfor)
914 unsigned int place;
915 constraint_t found;
917 if (!vec.exists ())
918 return NULL;
920 place = vec.lower_bound (&lookfor, constraint_less);
921 if (place >= vec.length ())
922 return NULL;
923 found = vec[place];
924 if (!constraint_equal (*found, lookfor))
925 return NULL;
926 return found;
929 /* Union two constraint vectors, TO and FROM. Put the result in TO.
930 Returns true of TO set is changed. */
932 static bool
933 constraint_set_union (vec<constraint_t> *to,
934 vec<constraint_t> *from)
936 int i;
937 constraint_t c;
938 bool any_change = false;
940 FOR_EACH_VEC_ELT (*from, i, c)
942 if (constraint_vec_find (*to, *c) == NULL)
944 unsigned int place = to->lower_bound (c, constraint_less);
945 to->safe_insert (place, c);
946 any_change = true;
949 return any_change;
952 /* Expands the solution in SET to all sub-fields of variables included. */
954 static bitmap
955 solution_set_expand (bitmap set, bitmap *expanded)
957 bitmap_iterator bi;
958 unsigned j;
960 if (*expanded)
961 return *expanded;
963 *expanded = BITMAP_ALLOC (&iteration_obstack);
965 /* In a first pass expand to the head of the variables we need to
966 add all sub-fields off. This avoids quadratic behavior. */
967 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
969 varinfo_t v = get_varinfo (j);
970 if (v->is_artificial_var
971 || v->is_full_var)
972 continue;
973 bitmap_set_bit (*expanded, v->head);
976 /* In the second pass now expand all head variables with subfields. */
977 EXECUTE_IF_SET_IN_BITMAP (*expanded, 0, j, bi)
979 varinfo_t v = get_varinfo (j);
980 if (v->head != j)
981 continue;
982 for (v = vi_next (v); v != NULL; v = vi_next (v))
983 bitmap_set_bit (*expanded, v->id);
986 /* And finally set the rest of the bits from SET. */
987 bitmap_ior_into (*expanded, set);
989 return *expanded;
992 /* Union solution sets TO and DELTA, and add INC to each member of DELTA in the
993 process. */
995 static bool
996 set_union_with_increment (bitmap to, bitmap delta, HOST_WIDE_INT inc,
997 bitmap *expanded_delta)
999 bool changed = false;
1000 bitmap_iterator bi;
1001 unsigned int i;
1003 /* If the solution of DELTA contains anything it is good enough to transfer
1004 this to TO. */
1005 if (bitmap_bit_p (delta, anything_id))
1006 return bitmap_set_bit (to, anything_id);
1008 /* If the offset is unknown we have to expand the solution to
1009 all subfields. */
1010 if (inc == UNKNOWN_OFFSET)
1012 delta = solution_set_expand (delta, expanded_delta);
1013 changed |= bitmap_ior_into (to, delta);
1014 return changed;
1017 /* For non-zero offset union the offsetted solution into the destination. */
1018 EXECUTE_IF_SET_IN_BITMAP (delta, 0, i, bi)
1020 varinfo_t vi = get_varinfo (i);
1022 /* If this is a variable with just one field just set its bit
1023 in the result. */
1024 if (vi->is_artificial_var
1025 || vi->is_unknown_size_var
1026 || vi->is_full_var)
1027 changed |= bitmap_set_bit (to, i);
1028 else
1030 HOST_WIDE_INT fieldoffset = vi->offset + inc;
1031 unsigned HOST_WIDE_INT size = vi->size;
1033 /* If the offset makes the pointer point to before the
1034 variable use offset zero for the field lookup. */
1035 if (fieldoffset < 0)
1036 vi = get_varinfo (vi->head);
1037 else
1038 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
1042 changed |= bitmap_set_bit (to, vi->id);
1043 if (vi->is_full_var
1044 || vi->next == 0)
1045 break;
1047 /* We have to include all fields that overlap the current field
1048 shifted by inc. */
1049 vi = vi_next (vi);
1051 while (vi->offset < fieldoffset + size);
1055 return changed;
1058 /* Insert constraint C into the list of complex constraints for graph
1059 node VAR. */
1061 static void
1062 insert_into_complex (constraint_graph_t graph,
1063 unsigned int var, constraint_t c)
1065 vec<constraint_t> complex = graph->complex[var];
1066 unsigned int place = complex.lower_bound (c, constraint_less);
1068 /* Only insert constraints that do not already exist. */
1069 if (place >= complex.length ()
1070 || !constraint_equal (*c, *complex[place]))
1071 graph->complex[var].safe_insert (place, c);
1075 /* Condense two variable nodes into a single variable node, by moving
1076 all associated info from FROM to TO. Returns true if TO node's
1077 constraint set changes after the merge. */
1079 static bool
1080 merge_node_constraints (constraint_graph_t graph, unsigned int to,
1081 unsigned int from)
1083 unsigned int i;
1084 constraint_t c;
1085 bool any_change = false;
1087 gcc_checking_assert (find (from) == to);
1089 /* Move all complex constraints from src node into to node */
1090 FOR_EACH_VEC_ELT (graph->complex[from], i, c)
1092 /* In complex constraints for node FROM, we may have either
1093 a = *FROM, and *FROM = a, or an offseted constraint which are
1094 always added to the rhs node's constraints. */
1096 if (c->rhs.type == DEREF)
1097 c->rhs.var = to;
1098 else if (c->lhs.type == DEREF)
1099 c->lhs.var = to;
1100 else
1101 c->rhs.var = to;
1104 any_change = constraint_set_union (&graph->complex[to],
1105 &graph->complex[from]);
1106 graph->complex[from].release ();
1107 return any_change;
1111 /* Remove edges involving NODE from GRAPH. */
1113 static void
1114 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
1116 if (graph->succs[node])
1117 BITMAP_FREE (graph->succs[node]);
1120 /* Merge GRAPH nodes FROM and TO into node TO. */
1122 static void
1123 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1124 unsigned int from)
1126 if (graph->indirect_cycles[from] != -1)
1128 /* If we have indirect cycles with the from node, and we have
1129 none on the to node, the to node has indirect cycles from the
1130 from node now that they are unified.
1131 If indirect cycles exist on both, unify the nodes that they
1132 are in a cycle with, since we know they are in a cycle with
1133 each other. */
1134 if (graph->indirect_cycles[to] == -1)
1135 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1138 /* Merge all the successor edges. */
1139 if (graph->succs[from])
1141 if (!graph->succs[to])
1142 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1143 bitmap_ior_into (graph->succs[to],
1144 graph->succs[from]);
1147 clear_edges_for_node (graph, from);
1151 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1152 it doesn't exist in the graph already. */
1154 static void
1155 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1156 unsigned int from)
1158 if (to == from)
1159 return;
1161 if (!graph->implicit_preds[to])
1162 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1164 if (bitmap_set_bit (graph->implicit_preds[to], from))
1165 stats.num_implicit_edges++;
1168 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1169 it doesn't exist in the graph already.
1170 Return false if the edge already existed, true otherwise. */
1172 static void
1173 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1174 unsigned int from)
1176 if (!graph->preds[to])
1177 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1178 bitmap_set_bit (graph->preds[to], from);
1181 /* Add a graph edge to GRAPH, going from FROM to TO if
1182 it doesn't exist in the graph already.
1183 Return false if the edge already existed, true otherwise. */
1185 static bool
1186 add_graph_edge (constraint_graph_t graph, unsigned int to,
1187 unsigned int from)
1189 if (to == from)
1191 return false;
1193 else
1195 bool r = false;
1197 if (!graph->succs[from])
1198 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1200 /* The graph solving process does not avoid "triangles", thus
1201 there can be multiple paths from a node to another involving
1202 intermediate other nodes. That causes extra copying which is
1203 most difficult to avoid when the intermediate node is ESCAPED
1204 because there are no edges added from ESCAPED. Avoid
1205 adding the direct edge FROM -> TO when we have FROM -> ESCAPED
1206 and TO contains ESCAPED.
1207 ??? Note this is only a heuristic, it does not prevent the
1208 situation from occuring. The heuristic helps PR38474 and
1209 PR99912 significantly. */
1210 if (to < FIRST_REF_NODE
1211 && bitmap_bit_p (graph->succs[from], find (escaped_id))
1212 && bitmap_bit_p (get_varinfo (find (to))->solution, escaped_id))
1213 return false;
1215 if (bitmap_set_bit (graph->succs[from], to))
1217 r = true;
1218 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1219 stats.num_edges++;
1221 return r;
1226 /* Initialize the constraint graph structure to contain SIZE nodes. */
1228 static void
1229 init_graph (unsigned int size)
1231 unsigned int j;
1233 graph = XCNEW (struct constraint_graph);
1234 graph->size = size;
1235 graph->succs = XCNEWVEC (bitmap, graph->size);
1236 graph->indirect_cycles = XNEWVEC (int, graph->size);
1237 graph->rep = XNEWVEC (unsigned int, graph->size);
1238 /* ??? Macros do not support template types with multiple arguments,
1239 so we use a typedef to work around it. */
1240 typedef vec<constraint_t> vec_constraint_t_heap;
1241 graph->complex = XCNEWVEC (vec_constraint_t_heap, size);
1242 graph->pe = XCNEWVEC (unsigned int, graph->size);
1243 graph->pe_rep = XNEWVEC (int, graph->size);
1245 for (j = 0; j < graph->size; j++)
1247 graph->rep[j] = j;
1248 graph->pe_rep[j] = -1;
1249 graph->indirect_cycles[j] = -1;
1253 /* Build the constraint graph, adding only predecessor edges right now. */
1255 static void
1256 build_pred_graph (void)
1258 int i;
1259 constraint_t c;
1260 unsigned int j;
1262 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1263 graph->preds = XCNEWVEC (bitmap, graph->size);
1264 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1265 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1266 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1267 graph->points_to = XCNEWVEC (bitmap, graph->size);
1268 graph->eq_rep = XNEWVEC (int, graph->size);
1269 graph->direct_nodes = sbitmap_alloc (graph->size);
1270 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1271 bitmap_clear (graph->direct_nodes);
1273 for (j = 1; j < FIRST_REF_NODE; j++)
1275 if (!get_varinfo (j)->is_special_var)
1276 bitmap_set_bit (graph->direct_nodes, j);
1279 for (j = 0; j < graph->size; j++)
1280 graph->eq_rep[j] = -1;
1282 for (j = 0; j < varmap.length (); j++)
1283 graph->indirect_cycles[j] = -1;
1285 FOR_EACH_VEC_ELT (constraints, i, c)
1287 struct constraint_expr lhs = c->lhs;
1288 struct constraint_expr rhs = c->rhs;
1289 unsigned int lhsvar = lhs.var;
1290 unsigned int rhsvar = rhs.var;
1292 if (lhs.type == DEREF)
1294 /* *x = y. */
1295 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1296 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1298 else if (rhs.type == DEREF)
1300 /* x = *y */
1301 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1302 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1303 else
1304 bitmap_clear_bit (graph->direct_nodes, lhsvar);
1306 else if (rhs.type == ADDRESSOF)
1308 varinfo_t v;
1310 /* x = &y */
1311 if (graph->points_to[lhsvar] == NULL)
1312 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1313 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1315 if (graph->pointed_by[rhsvar] == NULL)
1316 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1317 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1319 /* Implicitly, *x = y */
1320 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1322 /* All related variables are no longer direct nodes. */
1323 bitmap_clear_bit (graph->direct_nodes, rhsvar);
1324 v = get_varinfo (rhsvar);
1325 if (!v->is_full_var)
1327 v = get_varinfo (v->head);
1330 bitmap_clear_bit (graph->direct_nodes, v->id);
1331 v = vi_next (v);
1333 while (v != NULL);
1335 bitmap_set_bit (graph->address_taken, rhsvar);
1337 else if (lhsvar > anything_id
1338 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1340 /* x = y */
1341 add_pred_graph_edge (graph, lhsvar, rhsvar);
1342 /* Implicitly, *x = *y */
1343 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1344 FIRST_REF_NODE + rhsvar);
1346 else if (lhs.offset != 0 || rhs.offset != 0)
1348 if (rhs.offset != 0)
1349 bitmap_clear_bit (graph->direct_nodes, lhs.var);
1350 else if (lhs.offset != 0)
1351 bitmap_clear_bit (graph->direct_nodes, rhs.var);
1356 /* Build the constraint graph, adding successor edges. */
1358 static void
1359 build_succ_graph (void)
1361 unsigned i, t;
1362 constraint_t c;
1364 FOR_EACH_VEC_ELT (constraints, i, c)
1366 struct constraint_expr lhs;
1367 struct constraint_expr rhs;
1368 unsigned int lhsvar;
1369 unsigned int rhsvar;
1371 if (!c)
1372 continue;
1374 lhs = c->lhs;
1375 rhs = c->rhs;
1376 lhsvar = find (lhs.var);
1377 rhsvar = find (rhs.var);
1379 if (lhs.type == DEREF)
1381 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1382 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1384 else if (rhs.type == DEREF)
1386 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1387 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1389 else if (rhs.type == ADDRESSOF)
1391 /* x = &y */
1392 gcc_checking_assert (find (rhs.var) == rhs.var);
1393 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1395 else if (lhsvar > anything_id
1396 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1398 add_graph_edge (graph, lhsvar, rhsvar);
1402 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1403 receive pointers. */
1404 t = find (storedanything_id);
1405 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1407 if (!bitmap_bit_p (graph->direct_nodes, i)
1408 && get_varinfo (i)->may_have_pointers)
1409 add_graph_edge (graph, find (i), t);
1412 /* Everything stored to ANYTHING also potentially escapes. */
1413 add_graph_edge (graph, find (escaped_id), t);
1417 /* Changed variables on the last iteration. */
1418 static bitmap changed;
1420 /* Strongly Connected Component visitation info. */
1422 class scc_info
1424 public:
1425 scc_info (size_t size);
1426 ~scc_info ();
1428 auto_sbitmap visited;
1429 auto_sbitmap deleted;
1430 unsigned int *dfs;
1431 unsigned int *node_mapping;
1432 int current_index;
1433 auto_vec<unsigned> scc_stack;
1437 /* Recursive routine to find strongly connected components in GRAPH.
1438 SI is the SCC info to store the information in, and N is the id of current
1439 graph node we are processing.
1441 This is Tarjan's strongly connected component finding algorithm, as
1442 modified by Nuutila to keep only non-root nodes on the stack.
1443 The algorithm can be found in "On finding the strongly connected
1444 connected components in a directed graph" by Esko Nuutila and Eljas
1445 Soisalon-Soininen, in Information Processing Letters volume 49,
1446 number 1, pages 9-14. */
1448 static void
1449 scc_visit (constraint_graph_t graph, class scc_info *si, unsigned int n)
1451 unsigned int i;
1452 bitmap_iterator bi;
1453 unsigned int my_dfs;
1455 bitmap_set_bit (si->visited, n);
1456 si->dfs[n] = si->current_index ++;
1457 my_dfs = si->dfs[n];
1459 /* Visit all the successors. */
1460 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1462 unsigned int w;
1464 if (i > LAST_REF_NODE)
1465 break;
1467 w = find (i);
1468 if (bitmap_bit_p (si->deleted, w))
1469 continue;
1471 if (!bitmap_bit_p (si->visited, w))
1472 scc_visit (graph, si, w);
1474 unsigned int t = find (w);
1475 gcc_checking_assert (find (n) == n);
1476 if (si->dfs[t] < si->dfs[n])
1477 si->dfs[n] = si->dfs[t];
1480 /* See if any components have been identified. */
1481 if (si->dfs[n] == my_dfs)
1483 if (si->scc_stack.length () > 0
1484 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1486 bitmap scc = BITMAP_ALLOC (NULL);
1487 unsigned int lowest_node;
1488 bitmap_iterator bi;
1490 bitmap_set_bit (scc, n);
1492 while (si->scc_stack.length () != 0
1493 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1495 unsigned int w = si->scc_stack.pop ();
1497 bitmap_set_bit (scc, w);
1500 lowest_node = bitmap_first_set_bit (scc);
1501 gcc_assert (lowest_node < FIRST_REF_NODE);
1503 /* Collapse the SCC nodes into a single node, and mark the
1504 indirect cycles. */
1505 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1507 if (i < FIRST_REF_NODE)
1509 if (unite (lowest_node, i))
1510 unify_nodes (graph, lowest_node, i, false);
1512 else
1514 unite (lowest_node, i);
1515 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1519 bitmap_set_bit (si->deleted, n);
1521 else
1522 si->scc_stack.safe_push (n);
1525 /* Unify node FROM into node TO, updating the changed count if
1526 necessary when UPDATE_CHANGED is true. */
1528 static void
1529 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1530 bool update_changed)
1532 gcc_checking_assert (to != from && find (to) == to);
1534 if (dump_file && (dump_flags & TDF_DETAILS))
1535 fprintf (dump_file, "Unifying %s to %s\n",
1536 get_varinfo (from)->name,
1537 get_varinfo (to)->name);
1539 if (update_changed)
1540 stats.unified_vars_dynamic++;
1541 else
1542 stats.unified_vars_static++;
1544 merge_graph_nodes (graph, to, from);
1545 if (merge_node_constraints (graph, to, from))
1547 if (update_changed)
1548 bitmap_set_bit (changed, to);
1551 /* Mark TO as changed if FROM was changed. If TO was already marked
1552 as changed, decrease the changed count. */
1554 if (update_changed
1555 && bitmap_clear_bit (changed, from))
1556 bitmap_set_bit (changed, to);
1557 varinfo_t fromvi = get_varinfo (from);
1558 if (fromvi->solution)
1560 /* If the solution changes because of the merging, we need to mark
1561 the variable as changed. */
1562 varinfo_t tovi = get_varinfo (to);
1563 if (bitmap_ior_into (tovi->solution, fromvi->solution))
1565 if (update_changed)
1566 bitmap_set_bit (changed, to);
1569 BITMAP_FREE (fromvi->solution);
1570 if (fromvi->oldsolution)
1571 BITMAP_FREE (fromvi->oldsolution);
1573 if (stats.iterations > 0
1574 && tovi->oldsolution)
1575 BITMAP_FREE (tovi->oldsolution);
1577 if (graph->succs[to])
1578 bitmap_clear_bit (graph->succs[to], to);
1581 /* Information needed to compute the topological ordering of a graph. */
1583 struct topo_info
1585 /* sbitmap of visited nodes. */
1586 sbitmap visited;
1587 /* Array that stores the topological order of the graph, *in
1588 reverse*. */
1589 vec<unsigned> topo_order;
1593 /* Initialize and return a topological info structure. */
1595 static struct topo_info *
1596 init_topo_info (void)
1598 size_t size = graph->size;
1599 struct topo_info *ti = XNEW (struct topo_info);
1600 ti->visited = sbitmap_alloc (size);
1601 bitmap_clear (ti->visited);
1602 ti->topo_order.create (1);
1603 return ti;
1607 /* Free the topological sort info pointed to by TI. */
1609 static void
1610 free_topo_info (struct topo_info *ti)
1612 sbitmap_free (ti->visited);
1613 ti->topo_order.release ();
1614 free (ti);
1617 /* Visit the graph in topological order, and store the order in the
1618 topo_info structure. */
1620 static void
1621 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1622 unsigned int n)
1624 bitmap_iterator bi;
1625 unsigned int j;
1627 bitmap_set_bit (ti->visited, n);
1629 if (graph->succs[n])
1630 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1632 if (!bitmap_bit_p (ti->visited, j))
1633 topo_visit (graph, ti, j);
1636 ti->topo_order.safe_push (n);
1639 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1640 starting solution for y. */
1642 static void
1643 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1644 bitmap delta, bitmap *expanded_delta)
1646 unsigned int lhs = c->lhs.var;
1647 bool flag = false;
1648 bitmap sol = get_varinfo (lhs)->solution;
1649 unsigned int j;
1650 bitmap_iterator bi;
1651 HOST_WIDE_INT roffset = c->rhs.offset;
1653 /* Our IL does not allow this. */
1654 gcc_checking_assert (c->lhs.offset == 0);
1656 /* If the solution of Y contains anything it is good enough to transfer
1657 this to the LHS. */
1658 if (bitmap_bit_p (delta, anything_id))
1660 flag |= bitmap_set_bit (sol, anything_id);
1661 goto done;
1664 /* If we do not know at with offset the rhs is dereferenced compute
1665 the reachability set of DELTA, conservatively assuming it is
1666 dereferenced at all valid offsets. */
1667 if (roffset == UNKNOWN_OFFSET)
1669 delta = solution_set_expand (delta, expanded_delta);
1670 /* No further offset processing is necessary. */
1671 roffset = 0;
1674 /* For each variable j in delta (Sol(y)), add
1675 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1676 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1678 varinfo_t v = get_varinfo (j);
1679 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1680 unsigned HOST_WIDE_INT size = v->size;
1681 unsigned int t;
1683 if (v->is_full_var)
1685 else if (roffset != 0)
1687 if (fieldoffset < 0)
1688 v = get_varinfo (v->head);
1689 else
1690 v = first_or_preceding_vi_for_offset (v, fieldoffset);
1693 /* We have to include all fields that overlap the current field
1694 shifted by roffset. */
1697 t = find (v->id);
1699 /* Adding edges from the special vars is pointless.
1700 They don't have sets that can change. */
1701 if (get_varinfo (t)->is_special_var)
1702 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1703 /* Merging the solution from ESCAPED needlessly increases
1704 the set. Use ESCAPED as representative instead. */
1705 else if (v->id == escaped_id)
1706 flag |= bitmap_set_bit (sol, escaped_id);
1707 else if (v->may_have_pointers
1708 && add_graph_edge (graph, lhs, t))
1709 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1711 if (v->is_full_var
1712 || v->next == 0)
1713 break;
1715 v = vi_next (v);
1717 while (v->offset < fieldoffset + size);
1720 done:
1721 /* If the LHS solution changed, mark the var as changed. */
1722 if (flag)
1724 get_varinfo (lhs)->solution = sol;
1725 bitmap_set_bit (changed, lhs);
1729 /* Process a constraint C that represents *(x + off) = y using DELTA
1730 as the starting solution for x. */
1732 static void
1733 do_ds_constraint (constraint_t c, bitmap delta, bitmap *expanded_delta)
1735 unsigned int rhs = c->rhs.var;
1736 bitmap sol = get_varinfo (rhs)->solution;
1737 unsigned int j;
1738 bitmap_iterator bi;
1739 HOST_WIDE_INT loff = c->lhs.offset;
1740 bool escaped_p = false;
1742 /* Our IL does not allow this. */
1743 gcc_checking_assert (c->rhs.offset == 0);
1745 /* If the solution of y contains ANYTHING simply use the ANYTHING
1746 solution. This avoids needlessly increasing the points-to sets. */
1747 if (bitmap_bit_p (sol, anything_id))
1748 sol = get_varinfo (find (anything_id))->solution;
1750 /* If the solution for x contains ANYTHING we have to merge the
1751 solution of y into all pointer variables which we do via
1752 STOREDANYTHING. */
1753 if (bitmap_bit_p (delta, anything_id))
1755 unsigned t = find (storedanything_id);
1756 if (add_graph_edge (graph, t, rhs))
1758 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1759 bitmap_set_bit (changed, t);
1761 return;
1764 /* If we do not know at with offset the rhs is dereferenced compute
1765 the reachability set of DELTA, conservatively assuming it is
1766 dereferenced at all valid offsets. */
1767 if (loff == UNKNOWN_OFFSET)
1769 delta = solution_set_expand (delta, expanded_delta);
1770 loff = 0;
1773 /* For each member j of delta (Sol(x)), add an edge from y to j and
1774 union Sol(y) into Sol(j) */
1775 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1777 varinfo_t v = get_varinfo (j);
1778 unsigned int t;
1779 HOST_WIDE_INT fieldoffset = v->offset + loff;
1780 unsigned HOST_WIDE_INT size = v->size;
1782 if (v->is_full_var)
1784 else if (loff != 0)
1786 if (fieldoffset < 0)
1787 v = get_varinfo (v->head);
1788 else
1789 v = first_or_preceding_vi_for_offset (v, fieldoffset);
1792 /* We have to include all fields that overlap the current field
1793 shifted by loff. */
1796 if (v->may_have_pointers)
1798 /* If v is a global variable then this is an escape point. */
1799 if (v->is_global_var
1800 && !escaped_p)
1802 t = find (escaped_id);
1803 if (add_graph_edge (graph, t, rhs)
1804 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1805 bitmap_set_bit (changed, t);
1806 /* Enough to let rhs escape once. */
1807 escaped_p = true;
1810 if (v->is_special_var)
1811 break;
1813 t = find (v->id);
1814 if (add_graph_edge (graph, t, rhs)
1815 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1816 bitmap_set_bit (changed, t);
1819 if (v->is_full_var
1820 || v->next == 0)
1821 break;
1823 v = vi_next (v);
1825 while (v->offset < fieldoffset + size);
1829 /* Handle a non-simple (simple meaning requires no iteration),
1830 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1832 static void
1833 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta,
1834 bitmap *expanded_delta)
1836 if (c->lhs.type == DEREF)
1838 if (c->rhs.type == ADDRESSOF)
1840 gcc_unreachable ();
1842 else
1844 /* *x = y */
1845 do_ds_constraint (c, delta, expanded_delta);
1848 else if (c->rhs.type == DEREF)
1850 /* x = *y */
1851 if (!(get_varinfo (c->lhs.var)->is_special_var))
1852 do_sd_constraint (graph, c, delta, expanded_delta);
1854 else
1856 bitmap tmp;
1857 bool flag = false;
1859 gcc_checking_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR
1860 && c->rhs.offset != 0 && c->lhs.offset == 0);
1861 tmp = get_varinfo (c->lhs.var)->solution;
1863 flag = set_union_with_increment (tmp, delta, c->rhs.offset,
1864 expanded_delta);
1866 if (flag)
1867 bitmap_set_bit (changed, c->lhs.var);
1871 /* Initialize and return a new SCC info structure. */
1873 scc_info::scc_info (size_t size) :
1874 visited (size), deleted (size), current_index (0), scc_stack (1)
1876 bitmap_clear (visited);
1877 bitmap_clear (deleted);
1878 node_mapping = XNEWVEC (unsigned int, size);
1879 dfs = XCNEWVEC (unsigned int, size);
1881 for (size_t i = 0; i < size; i++)
1882 node_mapping[i] = i;
1885 /* Free an SCC info structure pointed to by SI */
1887 scc_info::~scc_info ()
1889 free (node_mapping);
1890 free (dfs);
1894 /* Find indirect cycles in GRAPH that occur, using strongly connected
1895 components, and note them in the indirect cycles map.
1897 This technique comes from Ben Hardekopf and Calvin Lin,
1898 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1899 Lines of Code", submitted to PLDI 2007. */
1901 static void
1902 find_indirect_cycles (constraint_graph_t graph)
1904 unsigned int i;
1905 unsigned int size = graph->size;
1906 scc_info si (size);
1908 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1909 if (!bitmap_bit_p (si.visited, i) && find (i) == i)
1910 scc_visit (graph, &si, i);
1913 /* Compute a topological ordering for GRAPH, and store the result in the
1914 topo_info structure TI. */
1916 static void
1917 compute_topo_order (constraint_graph_t graph,
1918 struct topo_info *ti)
1920 unsigned int i;
1921 unsigned int size = graph->size;
1923 for (i = 0; i != size; ++i)
1924 if (!bitmap_bit_p (ti->visited, i) && find (i) == i)
1925 topo_visit (graph, ti, i);
1928 /* Structure used to for hash value numbering of pointer equivalence
1929 classes. */
1931 typedef struct equiv_class_label
1933 hashval_t hashcode;
1934 unsigned int equivalence_class;
1935 bitmap labels;
1936 } *equiv_class_label_t;
1937 typedef const struct equiv_class_label *const_equiv_class_label_t;
1939 /* Equiv_class_label hashtable helpers. */
1941 struct equiv_class_hasher : nofree_ptr_hash <equiv_class_label>
1943 static inline hashval_t hash (const equiv_class_label *);
1944 static inline bool equal (const equiv_class_label *,
1945 const equiv_class_label *);
1948 /* Hash function for a equiv_class_label_t */
1950 inline hashval_t
1951 equiv_class_hasher::hash (const equiv_class_label *ecl)
1953 return ecl->hashcode;
1956 /* Equality function for two equiv_class_label_t's. */
1958 inline bool
1959 equiv_class_hasher::equal (const equiv_class_label *eql1,
1960 const equiv_class_label *eql2)
1962 return (eql1->hashcode == eql2->hashcode
1963 && bitmap_equal_p (eql1->labels, eql2->labels));
1966 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1967 classes. */
1968 static hash_table<equiv_class_hasher> *pointer_equiv_class_table;
1970 /* A hashtable for mapping a bitmap of labels->location equivalence
1971 classes. */
1972 static hash_table<equiv_class_hasher> *location_equiv_class_table;
1974 struct obstack equiv_class_obstack;
1976 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1977 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1978 is equivalent to. */
1980 static equiv_class_label *
1981 equiv_class_lookup_or_add (hash_table<equiv_class_hasher> *table,
1982 bitmap labels)
1984 equiv_class_label **slot;
1985 equiv_class_label ecl;
1987 ecl.labels = labels;
1988 ecl.hashcode = bitmap_hash (labels);
1989 slot = table->find_slot (&ecl, INSERT);
1990 if (!*slot)
1992 *slot = XOBNEW (&equiv_class_obstack, struct equiv_class_label);
1993 (*slot)->labels = labels;
1994 (*slot)->hashcode = ecl.hashcode;
1995 (*slot)->equivalence_class = 0;
1998 return *slot;
2001 /* Perform offline variable substitution.
2003 This is a worst case quadratic time way of identifying variables
2004 that must have equivalent points-to sets, including those caused by
2005 static cycles, and single entry subgraphs, in the constraint graph.
2007 The technique is described in "Exploiting Pointer and Location
2008 Equivalence to Optimize Pointer Analysis. In the 14th International
2009 Static Analysis Symposium (SAS), August 2007." It is known as the
2010 "HU" algorithm, and is equivalent to value numbering the collapsed
2011 constraint graph including evaluating unions.
2013 The general method of finding equivalence classes is as follows:
2014 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
2015 Initialize all non-REF nodes to be direct nodes.
2016 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
2017 variable}
2018 For each constraint containing the dereference, we also do the same
2019 thing.
2021 We then compute SCC's in the graph and unify nodes in the same SCC,
2022 including pts sets.
2024 For each non-collapsed node x:
2025 Visit all unvisited explicit incoming edges.
2026 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
2027 where y->x.
2028 Lookup the equivalence class for pts(x).
2029 If we found one, equivalence_class(x) = found class.
2030 Otherwise, equivalence_class(x) = new class, and new_class is
2031 added to the lookup table.
2033 All direct nodes with the same equivalence class can be replaced
2034 with a single representative node.
2035 All unlabeled nodes (label == 0) are not pointers and all edges
2036 involving them can be eliminated.
2037 We perform these optimizations during rewrite_constraints
2039 In addition to pointer equivalence class finding, we also perform
2040 location equivalence class finding. This is the set of variables
2041 that always appear together in points-to sets. We use this to
2042 compress the size of the points-to sets. */
2044 /* Current maximum pointer equivalence class id. */
2045 static int pointer_equiv_class;
2047 /* Current maximum location equivalence class id. */
2048 static int location_equiv_class;
2050 /* Recursive routine to find strongly connected components in GRAPH,
2051 and label it's nodes with DFS numbers. */
2053 static void
2054 condense_visit (constraint_graph_t graph, class scc_info *si, unsigned int n)
2056 unsigned int i;
2057 bitmap_iterator bi;
2058 unsigned int my_dfs;
2060 gcc_checking_assert (si->node_mapping[n] == n);
2061 bitmap_set_bit (si->visited, n);
2062 si->dfs[n] = si->current_index ++;
2063 my_dfs = si->dfs[n];
2065 /* Visit all the successors. */
2066 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2068 unsigned int w = si->node_mapping[i];
2070 if (bitmap_bit_p (si->deleted, w))
2071 continue;
2073 if (!bitmap_bit_p (si->visited, w))
2074 condense_visit (graph, si, w);
2076 unsigned int t = si->node_mapping[w];
2077 gcc_checking_assert (si->node_mapping[n] == n);
2078 if (si->dfs[t] < si->dfs[n])
2079 si->dfs[n] = si->dfs[t];
2082 /* Visit all the implicit predecessors. */
2083 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2085 unsigned int w = si->node_mapping[i];
2087 if (bitmap_bit_p (si->deleted, w))
2088 continue;
2090 if (!bitmap_bit_p (si->visited, w))
2091 condense_visit (graph, si, w);
2093 unsigned int t = si->node_mapping[w];
2094 gcc_assert (si->node_mapping[n] == n);
2095 if (si->dfs[t] < si->dfs[n])
2096 si->dfs[n] = si->dfs[t];
2099 /* See if any components have been identified. */
2100 if (si->dfs[n] == my_dfs)
2102 if (si->scc_stack.length () != 0
2103 && si->dfs[si->scc_stack.last ()] >= my_dfs)
2105 /* Find the first node of the SCC and do non-bitmap work. */
2106 bool direct_p = true;
2107 unsigned first = si->scc_stack.length ();
2110 --first;
2111 unsigned int w = si->scc_stack[first];
2112 si->node_mapping[w] = n;
2113 if (!bitmap_bit_p (graph->direct_nodes, w))
2114 direct_p = false;
2116 while (first > 0
2117 && si->dfs[si->scc_stack[first - 1]] >= my_dfs);
2118 if (!direct_p)
2119 bitmap_clear_bit (graph->direct_nodes, n);
2121 /* Want to reduce to node n, push that first. */
2122 si->scc_stack.reserve (1);
2123 si->scc_stack.quick_push (si->scc_stack[first]);
2124 si->scc_stack[first] = n;
2126 unsigned scc_size = si->scc_stack.length () - first;
2127 unsigned split = scc_size / 2;
2128 unsigned carry = scc_size - split * 2;
2129 while (split > 0)
2131 for (unsigned i = 0; i < split; ++i)
2133 unsigned a = si->scc_stack[first + i];
2134 unsigned b = si->scc_stack[first + split + carry + i];
2136 /* Unify our nodes. */
2137 if (graph->preds[b])
2139 if (!graph->preds[a])
2140 std::swap (graph->preds[a], graph->preds[b]);
2141 else
2142 bitmap_ior_into_and_free (graph->preds[a],
2143 &graph->preds[b]);
2145 if (graph->implicit_preds[b])
2147 if (!graph->implicit_preds[a])
2148 std::swap (graph->implicit_preds[a],
2149 graph->implicit_preds[b]);
2150 else
2151 bitmap_ior_into_and_free (graph->implicit_preds[a],
2152 &graph->implicit_preds[b]);
2154 if (graph->points_to[b])
2156 if (!graph->points_to[a])
2157 std::swap (graph->points_to[a], graph->points_to[b]);
2158 else
2159 bitmap_ior_into_and_free (graph->points_to[a],
2160 &graph->points_to[b]);
2163 unsigned remain = split + carry;
2164 split = remain / 2;
2165 carry = remain - split * 2;
2167 /* Actually pop the SCC. */
2168 si->scc_stack.truncate (first);
2170 bitmap_set_bit (si->deleted, n);
2172 else
2173 si->scc_stack.safe_push (n);
2176 /* Label pointer equivalences.
2178 This performs a value numbering of the constraint graph to
2179 discover which variables will always have the same points-to sets
2180 under the current set of constraints.
2182 The way it value numbers is to store the set of points-to bits
2183 generated by the constraints and graph edges. This is just used as a
2184 hash and equality comparison. The *actual set of points-to bits* is
2185 completely irrelevant, in that we don't care about being able to
2186 extract them later.
2188 The equality values (currently bitmaps) just have to satisfy a few
2189 constraints, the main ones being:
2190 1. The combining operation must be order independent.
2191 2. The end result of a given set of operations must be unique iff the
2192 combination of input values is unique
2193 3. Hashable. */
2195 static void
2196 label_visit (constraint_graph_t graph, class scc_info *si, unsigned int n)
2198 unsigned int i, first_pred;
2199 bitmap_iterator bi;
2201 bitmap_set_bit (si->visited, n);
2203 /* Label and union our incoming edges's points to sets. */
2204 first_pred = -1U;
2205 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2207 unsigned int w = si->node_mapping[i];
2208 if (!bitmap_bit_p (si->visited, w))
2209 label_visit (graph, si, w);
2211 /* Skip unused edges */
2212 if (w == n || graph->pointer_label[w] == 0)
2213 continue;
2215 if (graph->points_to[w])
2217 if (!graph->points_to[n])
2219 if (first_pred == -1U)
2220 first_pred = w;
2221 else
2223 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2224 bitmap_ior (graph->points_to[n],
2225 graph->points_to[first_pred],
2226 graph->points_to[w]);
2229 else
2230 bitmap_ior_into (graph->points_to[n], graph->points_to[w]);
2234 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2235 if (!bitmap_bit_p (graph->direct_nodes, n))
2237 if (!graph->points_to[n])
2239 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2240 if (first_pred != -1U)
2241 bitmap_copy (graph->points_to[n], graph->points_to[first_pred]);
2243 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2244 graph->pointer_label[n] = pointer_equiv_class++;
2245 equiv_class_label_t ecl;
2246 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2247 graph->points_to[n]);
2248 ecl->equivalence_class = graph->pointer_label[n];
2249 return;
2252 /* If there was only a single non-empty predecessor the pointer equiv
2253 class is the same. */
2254 if (!graph->points_to[n])
2256 if (first_pred != -1U)
2258 graph->pointer_label[n] = graph->pointer_label[first_pred];
2259 graph->points_to[n] = graph->points_to[first_pred];
2261 return;
2264 if (!bitmap_empty_p (graph->points_to[n]))
2266 equiv_class_label_t ecl;
2267 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2268 graph->points_to[n]);
2269 if (ecl->equivalence_class == 0)
2270 ecl->equivalence_class = pointer_equiv_class++;
2271 else
2273 BITMAP_FREE (graph->points_to[n]);
2274 graph->points_to[n] = ecl->labels;
2276 graph->pointer_label[n] = ecl->equivalence_class;
2280 /* Print the pred graph in dot format. */
2282 static void
2283 dump_pred_graph (class scc_info *si, FILE *file)
2285 unsigned int i;
2287 /* Only print the graph if it has already been initialized: */
2288 if (!graph)
2289 return;
2291 /* Prints the header of the dot file: */
2292 fprintf (file, "strict digraph {\n");
2293 fprintf (file, " node [\n shape = box\n ]\n");
2294 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
2295 fprintf (file, "\n // List of nodes and complex constraints in "
2296 "the constraint graph:\n");
2298 /* The next lines print the nodes in the graph together with the
2299 complex constraints attached to them. */
2300 for (i = 1; i < graph->size; i++)
2302 if (i == FIRST_REF_NODE)
2303 continue;
2304 if (si->node_mapping[i] != i)
2305 continue;
2306 if (i < FIRST_REF_NODE)
2307 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2308 else
2309 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2310 if (graph->points_to[i]
2311 && !bitmap_empty_p (graph->points_to[i]))
2313 if (i < FIRST_REF_NODE)
2314 fprintf (file, "[label=\"%s = {", get_varinfo (i)->name);
2315 else
2316 fprintf (file, "[label=\"*%s = {",
2317 get_varinfo (i - FIRST_REF_NODE)->name);
2318 unsigned j;
2319 bitmap_iterator bi;
2320 EXECUTE_IF_SET_IN_BITMAP (graph->points_to[i], 0, j, bi)
2321 fprintf (file, " %d", j);
2322 fprintf (file, " }\"]");
2324 fprintf (file, ";\n");
2327 /* Go over the edges. */
2328 fprintf (file, "\n // Edges in the constraint graph:\n");
2329 for (i = 1; i < graph->size; i++)
2331 unsigned j;
2332 bitmap_iterator bi;
2333 if (si->node_mapping[i] != i)
2334 continue;
2335 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[i], 0, j, bi)
2337 unsigned from = si->node_mapping[j];
2338 if (from < FIRST_REF_NODE)
2339 fprintf (file, "\"%s\"", get_varinfo (from)->name);
2340 else
2341 fprintf (file, "\"*%s\"", get_varinfo (from - FIRST_REF_NODE)->name);
2342 fprintf (file, " -> ");
2343 if (i < FIRST_REF_NODE)
2344 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2345 else
2346 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2347 fprintf (file, ";\n");
2351 /* Prints the tail of the dot file. */
2352 fprintf (file, "}\n");
2355 /* Perform offline variable substitution, discovering equivalence
2356 classes, and eliminating non-pointer variables. */
2358 static class scc_info *
2359 perform_var_substitution (constraint_graph_t graph)
2361 unsigned int i;
2362 unsigned int size = graph->size;
2363 scc_info *si = new scc_info (size);
2365 bitmap_obstack_initialize (&iteration_obstack);
2366 gcc_obstack_init (&equiv_class_obstack);
2367 pointer_equiv_class_table = new hash_table<equiv_class_hasher> (511);
2368 location_equiv_class_table
2369 = new hash_table<equiv_class_hasher> (511);
2370 pointer_equiv_class = 1;
2371 location_equiv_class = 1;
2373 /* Condense the nodes, which means to find SCC's, count incoming
2374 predecessors, and unite nodes in SCC's. */
2375 for (i = 1; i < FIRST_REF_NODE; i++)
2376 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2377 condense_visit (graph, si, si->node_mapping[i]);
2379 if (dump_file && (dump_flags & TDF_GRAPH))
2381 fprintf (dump_file, "\n\n// The constraint graph before var-substitution "
2382 "in dot format:\n");
2383 dump_pred_graph (si, dump_file);
2384 fprintf (dump_file, "\n\n");
2387 bitmap_clear (si->visited);
2388 /* Actually the label the nodes for pointer equivalences */
2389 for (i = 1; i < FIRST_REF_NODE; i++)
2390 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2391 label_visit (graph, si, si->node_mapping[i]);
2393 /* Calculate location equivalence labels. */
2394 for (i = 1; i < FIRST_REF_NODE; i++)
2396 bitmap pointed_by;
2397 bitmap_iterator bi;
2398 unsigned int j;
2400 if (!graph->pointed_by[i])
2401 continue;
2402 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2404 /* Translate the pointed-by mapping for pointer equivalence
2405 labels. */
2406 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2408 bitmap_set_bit (pointed_by,
2409 graph->pointer_label[si->node_mapping[j]]);
2411 /* The original pointed_by is now dead. */
2412 BITMAP_FREE (graph->pointed_by[i]);
2414 /* Look up the location equivalence label if one exists, or make
2415 one otherwise. */
2416 equiv_class_label_t ecl;
2417 ecl = equiv_class_lookup_or_add (location_equiv_class_table, pointed_by);
2418 if (ecl->equivalence_class == 0)
2419 ecl->equivalence_class = location_equiv_class++;
2420 else
2422 if (dump_file && (dump_flags & TDF_DETAILS))
2423 fprintf (dump_file, "Found location equivalence for node %s\n",
2424 get_varinfo (i)->name);
2425 BITMAP_FREE (pointed_by);
2427 graph->loc_label[i] = ecl->equivalence_class;
2431 if (dump_file && (dump_flags & TDF_DETAILS))
2432 for (i = 1; i < FIRST_REF_NODE; i++)
2434 unsigned j = si->node_mapping[i];
2435 if (j != i)
2437 fprintf (dump_file, "%s node id %d ",
2438 bitmap_bit_p (graph->direct_nodes, i)
2439 ? "Direct" : "Indirect", i);
2440 if (i < FIRST_REF_NODE)
2441 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2442 else
2443 fprintf (dump_file, "\"*%s\"",
2444 get_varinfo (i - FIRST_REF_NODE)->name);
2445 fprintf (dump_file, " mapped to SCC leader node id %d ", j);
2446 if (j < FIRST_REF_NODE)
2447 fprintf (dump_file, "\"%s\"\n", get_varinfo (j)->name);
2448 else
2449 fprintf (dump_file, "\"*%s\"\n",
2450 get_varinfo (j - FIRST_REF_NODE)->name);
2452 else
2454 fprintf (dump_file,
2455 "Equivalence classes for %s node id %d ",
2456 bitmap_bit_p (graph->direct_nodes, i)
2457 ? "direct" : "indirect", i);
2458 if (i < FIRST_REF_NODE)
2459 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2460 else
2461 fprintf (dump_file, "\"*%s\"",
2462 get_varinfo (i - FIRST_REF_NODE)->name);
2463 fprintf (dump_file,
2464 ": pointer %d, location %d\n",
2465 graph->pointer_label[i], graph->loc_label[i]);
2469 /* Quickly eliminate our non-pointer variables. */
2471 for (i = 1; i < FIRST_REF_NODE; i++)
2473 unsigned int node = si->node_mapping[i];
2475 if (graph->pointer_label[node] == 0)
2477 if (dump_file && (dump_flags & TDF_DETAILS))
2478 fprintf (dump_file,
2479 "%s is a non-pointer variable, eliminating edges.\n",
2480 get_varinfo (node)->name);
2481 stats.nonpointer_vars++;
2482 clear_edges_for_node (graph, node);
2486 return si;
2489 /* Free information that was only necessary for variable
2490 substitution. */
2492 static void
2493 free_var_substitution_info (class scc_info *si)
2495 delete si;
2496 free (graph->pointer_label);
2497 free (graph->loc_label);
2498 free (graph->pointed_by);
2499 free (graph->points_to);
2500 free (graph->eq_rep);
2501 sbitmap_free (graph->direct_nodes);
2502 delete pointer_equiv_class_table;
2503 pointer_equiv_class_table = NULL;
2504 delete location_equiv_class_table;
2505 location_equiv_class_table = NULL;
2506 obstack_free (&equiv_class_obstack, NULL);
2507 bitmap_obstack_release (&iteration_obstack);
2510 /* Return an existing node that is equivalent to NODE, which has
2511 equivalence class LABEL, if one exists. Return NODE otherwise. */
2513 static unsigned int
2514 find_equivalent_node (constraint_graph_t graph,
2515 unsigned int node, unsigned int label)
2517 /* If the address version of this variable is unused, we can
2518 substitute it for anything else with the same label.
2519 Otherwise, we know the pointers are equivalent, but not the
2520 locations, and we can unite them later. */
2522 if (!bitmap_bit_p (graph->address_taken, node))
2524 gcc_checking_assert (label < graph->size);
2526 if (graph->eq_rep[label] != -1)
2528 /* Unify the two variables since we know they are equivalent. */
2529 if (unite (graph->eq_rep[label], node))
2530 unify_nodes (graph, graph->eq_rep[label], node, false);
2531 return graph->eq_rep[label];
2533 else
2535 graph->eq_rep[label] = node;
2536 graph->pe_rep[label] = node;
2539 else
2541 gcc_checking_assert (label < graph->size);
2542 graph->pe[node] = label;
2543 if (graph->pe_rep[label] == -1)
2544 graph->pe_rep[label] = node;
2547 return node;
2550 /* Unite pointer equivalent but not location equivalent nodes in
2551 GRAPH. This may only be performed once variable substitution is
2552 finished. */
2554 static void
2555 unite_pointer_equivalences (constraint_graph_t graph)
2557 unsigned int i;
2559 /* Go through the pointer equivalences and unite them to their
2560 representative, if they aren't already. */
2561 for (i = 1; i < FIRST_REF_NODE; i++)
2563 unsigned int label = graph->pe[i];
2564 if (label)
2566 int label_rep = graph->pe_rep[label];
2568 if (label_rep == -1)
2569 continue;
2571 label_rep = find (label_rep);
2572 if (label_rep >= 0 && unite (label_rep, find (i)))
2573 unify_nodes (graph, label_rep, i, false);
2578 /* Move complex constraints to the GRAPH nodes they belong to. */
2580 static void
2581 move_complex_constraints (constraint_graph_t graph)
2583 int i;
2584 constraint_t c;
2586 FOR_EACH_VEC_ELT (constraints, i, c)
2588 if (c)
2590 struct constraint_expr lhs = c->lhs;
2591 struct constraint_expr rhs = c->rhs;
2593 if (lhs.type == DEREF)
2595 insert_into_complex (graph, lhs.var, c);
2597 else if (rhs.type == DEREF)
2599 if (!(get_varinfo (lhs.var)->is_special_var))
2600 insert_into_complex (graph, rhs.var, c);
2602 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2603 && (lhs.offset != 0 || rhs.offset != 0))
2605 insert_into_complex (graph, rhs.var, c);
2612 /* Optimize and rewrite complex constraints while performing
2613 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2614 result of perform_variable_substitution. */
2616 static void
2617 rewrite_constraints (constraint_graph_t graph,
2618 class scc_info *si)
2620 int i;
2621 constraint_t c;
2623 if (flag_checking)
2625 for (unsigned int j = 0; j < graph->size; j++)
2626 gcc_assert (find (j) == j);
2629 FOR_EACH_VEC_ELT (constraints, i, c)
2631 struct constraint_expr lhs = c->lhs;
2632 struct constraint_expr rhs = c->rhs;
2633 unsigned int lhsvar = find (lhs.var);
2634 unsigned int rhsvar = find (rhs.var);
2635 unsigned int lhsnode, rhsnode;
2636 unsigned int lhslabel, rhslabel;
2638 lhsnode = si->node_mapping[lhsvar];
2639 rhsnode = si->node_mapping[rhsvar];
2640 lhslabel = graph->pointer_label[lhsnode];
2641 rhslabel = graph->pointer_label[rhsnode];
2643 /* See if it is really a non-pointer variable, and if so, ignore
2644 the constraint. */
2645 if (lhslabel == 0)
2647 if (dump_file && (dump_flags & TDF_DETAILS))
2650 fprintf (dump_file, "%s is a non-pointer variable, "
2651 "ignoring constraint:",
2652 get_varinfo (lhs.var)->name);
2653 dump_constraint (dump_file, c);
2654 fprintf (dump_file, "\n");
2656 constraints[i] = NULL;
2657 continue;
2660 if (rhslabel == 0)
2662 if (dump_file && (dump_flags & TDF_DETAILS))
2665 fprintf (dump_file, "%s is a non-pointer variable, "
2666 "ignoring constraint:",
2667 get_varinfo (rhs.var)->name);
2668 dump_constraint (dump_file, c);
2669 fprintf (dump_file, "\n");
2671 constraints[i] = NULL;
2672 continue;
2675 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2676 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2677 c->lhs.var = lhsvar;
2678 c->rhs.var = rhsvar;
2682 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2683 part of an SCC, false otherwise. */
2685 static bool
2686 eliminate_indirect_cycles (unsigned int node)
2688 if (graph->indirect_cycles[node] != -1
2689 && !bitmap_empty_p (get_varinfo (node)->solution))
2691 unsigned int i;
2692 auto_vec<unsigned> queue;
2693 int queuepos;
2694 unsigned int to = find (graph->indirect_cycles[node]);
2695 bitmap_iterator bi;
2697 /* We can't touch the solution set and call unify_nodes
2698 at the same time, because unify_nodes is going to do
2699 bitmap unions into it. */
2701 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2703 if (find (i) == i && i != to)
2705 if (unite (to, i))
2706 queue.safe_push (i);
2710 for (queuepos = 0;
2711 queue.iterate (queuepos, &i);
2712 queuepos++)
2714 unify_nodes (graph, to, i, true);
2716 return true;
2718 return false;
2721 /* Solve the constraint graph GRAPH using our worklist solver.
2722 This is based on the PW* family of solvers from the "Efficient Field
2723 Sensitive Pointer Analysis for C" paper.
2724 It works by iterating over all the graph nodes, processing the complex
2725 constraints and propagating the copy constraints, until everything stops
2726 changed. This corresponds to steps 6-8 in the solving list given above. */
2728 static void
2729 solve_graph (constraint_graph_t graph)
2731 unsigned int size = graph->size;
2732 unsigned int i;
2733 bitmap pts;
2735 changed = BITMAP_ALLOC (NULL);
2737 /* Mark all initial non-collapsed nodes as changed. */
2738 for (i = 1; i < size; i++)
2740 varinfo_t ivi = get_varinfo (i);
2741 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2742 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2743 || graph->complex[i].length () > 0))
2744 bitmap_set_bit (changed, i);
2747 /* Allocate a bitmap to be used to store the changed bits. */
2748 pts = BITMAP_ALLOC (&pta_obstack);
2750 while (!bitmap_empty_p (changed))
2752 unsigned int i;
2753 struct topo_info *ti = init_topo_info ();
2754 stats.iterations++;
2756 bitmap_obstack_initialize (&iteration_obstack);
2758 compute_topo_order (graph, ti);
2760 while (ti->topo_order.length () != 0)
2763 i = ti->topo_order.pop ();
2765 /* If this variable is not a representative, skip it. */
2766 if (find (i) != i)
2767 continue;
2769 /* In certain indirect cycle cases, we may merge this
2770 variable to another. */
2771 if (eliminate_indirect_cycles (i) && find (i) != i)
2772 continue;
2774 /* If the node has changed, we need to process the
2775 complex constraints and outgoing edges again. */
2776 if (bitmap_clear_bit (changed, i))
2778 unsigned int j;
2779 constraint_t c;
2780 bitmap solution;
2781 vec<constraint_t> complex = graph->complex[i];
2782 varinfo_t vi = get_varinfo (i);
2783 bool solution_empty;
2785 /* Compute the changed set of solution bits. If anything
2786 is in the solution just propagate that. */
2787 if (bitmap_bit_p (vi->solution, anything_id))
2789 /* If anything is also in the old solution there is
2790 nothing to do.
2791 ??? But we shouldn't ended up with "changed" set ... */
2792 if (vi->oldsolution
2793 && bitmap_bit_p (vi->oldsolution, anything_id))
2794 continue;
2795 bitmap_copy (pts, get_varinfo (find (anything_id))->solution);
2797 else if (vi->oldsolution)
2798 bitmap_and_compl (pts, vi->solution, vi->oldsolution);
2799 else
2800 bitmap_copy (pts, vi->solution);
2802 if (bitmap_empty_p (pts))
2803 continue;
2805 if (vi->oldsolution)
2806 bitmap_ior_into (vi->oldsolution, pts);
2807 else
2809 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
2810 bitmap_copy (vi->oldsolution, pts);
2813 solution = vi->solution;
2814 solution_empty = bitmap_empty_p (solution);
2816 /* Process the complex constraints */
2817 bitmap expanded_pts = NULL;
2818 FOR_EACH_VEC_ELT (complex, j, c)
2820 /* XXX: This is going to unsort the constraints in
2821 some cases, which will occasionally add duplicate
2822 constraints during unification. This does not
2823 affect correctness. */
2824 c->lhs.var = find (c->lhs.var);
2825 c->rhs.var = find (c->rhs.var);
2827 /* The only complex constraint that can change our
2828 solution to non-empty, given an empty solution,
2829 is a constraint where the lhs side is receiving
2830 some set from elsewhere. */
2831 if (!solution_empty || c->lhs.type != DEREF)
2832 do_complex_constraint (graph, c, pts, &expanded_pts);
2834 BITMAP_FREE (expanded_pts);
2836 solution_empty = bitmap_empty_p (solution);
2838 if (!solution_empty)
2840 bitmap_iterator bi;
2841 unsigned eff_escaped_id = find (escaped_id);
2843 /* Propagate solution to all successors. */
2844 unsigned to_remove = ~0U;
2845 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2846 0, j, bi)
2848 if (to_remove != ~0U)
2850 bitmap_clear_bit (graph->succs[i], to_remove);
2851 to_remove = ~0U;
2853 unsigned int to = find (j);
2854 if (to != j)
2856 /* Update the succ graph, avoiding duplicate
2857 work. */
2858 to_remove = j;
2859 if (! bitmap_set_bit (graph->succs[i], to))
2860 continue;
2861 /* We eventually end up processing 'to' twice
2862 as it is undefined whether bitmap iteration
2863 iterates over bits set during iteration.
2864 Play safe instead of doing tricks. */
2866 /* Don't try to propagate to ourselves. */
2867 if (to == i)
2868 continue;
2870 bitmap tmp = get_varinfo (to)->solution;
2871 bool flag = false;
2873 /* If we propagate from ESCAPED use ESCAPED as
2874 placeholder. */
2875 if (i == eff_escaped_id)
2876 flag = bitmap_set_bit (tmp, escaped_id);
2877 else
2878 flag = bitmap_ior_into (tmp, pts);
2880 if (flag)
2881 bitmap_set_bit (changed, to);
2883 if (to_remove != ~0U)
2884 bitmap_clear_bit (graph->succs[i], to_remove);
2888 free_topo_info (ti);
2889 bitmap_obstack_release (&iteration_obstack);
2892 BITMAP_FREE (pts);
2893 BITMAP_FREE (changed);
2894 bitmap_obstack_release (&oldpta_obstack);
2897 /* Map from trees to variable infos. */
2898 static hash_map<tree, varinfo_t> *vi_for_tree;
2901 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2903 static void
2904 insert_vi_for_tree (tree t, varinfo_t vi)
2906 gcc_assert (vi);
2907 gcc_assert (!vi_for_tree->put (t, vi));
2910 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2911 exist in the map, return NULL, otherwise, return the varinfo we found. */
2913 static varinfo_t
2914 lookup_vi_for_tree (tree t)
2916 varinfo_t *slot = vi_for_tree->get (t);
2917 if (slot == NULL)
2918 return NULL;
2920 return *slot;
2923 /* Return a printable name for DECL */
2925 static const char *
2926 alias_get_name (tree decl)
2928 const char *res = "NULL";
2929 if (dump_file)
2931 char *temp = NULL;
2932 if (TREE_CODE (decl) == SSA_NAME)
2934 res = get_name (decl);
2935 temp = xasprintf ("%s_%u", res ? res : "", SSA_NAME_VERSION (decl));
2937 else if (HAS_DECL_ASSEMBLER_NAME_P (decl)
2938 && DECL_ASSEMBLER_NAME_SET_P (decl))
2939 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME_RAW (decl));
2940 else if (DECL_P (decl))
2942 res = get_name (decl);
2943 if (!res)
2944 temp = xasprintf ("D.%u", DECL_UID (decl));
2947 if (temp)
2949 res = ggc_strdup (temp);
2950 free (temp);
2954 return res;
2957 /* Find the variable id for tree T in the map.
2958 If T doesn't exist in the map, create an entry for it and return it. */
2960 static varinfo_t
2961 get_vi_for_tree (tree t)
2963 varinfo_t *slot = vi_for_tree->get (t);
2964 if (slot == NULL)
2966 unsigned int id = create_variable_info_for (t, alias_get_name (t), false);
2967 return get_varinfo (id);
2970 return *slot;
2973 /* Get a scalar constraint expression for a new temporary variable. */
2975 static struct constraint_expr
2976 new_scalar_tmp_constraint_exp (const char *name, bool add_id)
2978 struct constraint_expr tmp;
2979 varinfo_t vi;
2981 vi = new_var_info (NULL_TREE, name, add_id);
2982 vi->offset = 0;
2983 vi->size = -1;
2984 vi->fullsize = -1;
2985 vi->is_full_var = 1;
2986 vi->is_reg_var = 1;
2988 tmp.var = vi->id;
2989 tmp.type = SCALAR;
2990 tmp.offset = 0;
2992 return tmp;
2995 /* Get a constraint expression vector from an SSA_VAR_P node.
2996 If address_p is true, the result will be taken its address of. */
2998 static void
2999 get_constraint_for_ssa_var (tree t, vec<ce_s> *results, bool address_p)
3001 struct constraint_expr cexpr;
3002 varinfo_t vi;
3004 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
3005 gcc_assert (TREE_CODE (t) == SSA_NAME || DECL_P (t));
3007 if (TREE_CODE (t) == SSA_NAME
3008 && SSA_NAME_IS_DEFAULT_DEF (t))
3010 /* For parameters, get at the points-to set for the actual parm
3011 decl. */
3012 if (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
3013 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL)
3015 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
3016 return;
3018 /* For undefined SSA names return nothing. */
3019 else if (!ssa_defined_default_def_p (t))
3021 cexpr.var = nothing_id;
3022 cexpr.type = SCALAR;
3023 cexpr.offset = 0;
3024 results->safe_push (cexpr);
3025 return;
3029 /* For global variables resort to the alias target. */
3030 if (VAR_P (t) && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3032 varpool_node *node = varpool_node::get (t);
3033 if (node && node->alias && node->analyzed)
3035 node = node->ultimate_alias_target ();
3036 /* Canonicalize the PT uid of all aliases to the ultimate target.
3037 ??? Hopefully the set of aliases can't change in a way that
3038 changes the ultimate alias target. */
3039 gcc_assert ((! DECL_PT_UID_SET_P (node->decl)
3040 || DECL_PT_UID (node->decl) == DECL_UID (node->decl))
3041 && (! DECL_PT_UID_SET_P (t)
3042 || DECL_PT_UID (t) == DECL_UID (node->decl)));
3043 DECL_PT_UID (t) = DECL_UID (node->decl);
3044 t = node->decl;
3047 /* If this is decl may bind to NULL note that. */
3048 if (address_p
3049 && (! node || ! node->nonzero_address ()))
3051 cexpr.var = nothing_id;
3052 cexpr.type = SCALAR;
3053 cexpr.offset = 0;
3054 results->safe_push (cexpr);
3058 vi = get_vi_for_tree (t);
3059 cexpr.var = vi->id;
3060 cexpr.type = SCALAR;
3061 cexpr.offset = 0;
3063 /* If we are not taking the address of the constraint expr, add all
3064 sub-fiels of the variable as well. */
3065 if (!address_p
3066 && !vi->is_full_var)
3068 for (; vi; vi = vi_next (vi))
3070 cexpr.var = vi->id;
3071 results->safe_push (cexpr);
3073 return;
3076 results->safe_push (cexpr);
3079 /* Process constraint T, performing various simplifications and then
3080 adding it to our list of overall constraints. */
3082 static void
3083 process_constraint (constraint_t t)
3085 struct constraint_expr rhs = t->rhs;
3086 struct constraint_expr lhs = t->lhs;
3088 gcc_assert (rhs.var < varmap.length ());
3089 gcc_assert (lhs.var < varmap.length ());
3091 /* If we didn't get any useful constraint from the lhs we get
3092 &ANYTHING as fallback from get_constraint_for. Deal with
3093 it here by turning it into *ANYTHING. */
3094 if (lhs.type == ADDRESSOF
3095 && lhs.var == anything_id)
3096 lhs.type = DEREF;
3098 /* ADDRESSOF on the lhs is invalid. */
3099 gcc_assert (lhs.type != ADDRESSOF);
3101 /* We shouldn't add constraints from things that cannot have pointers.
3102 It's not completely trivial to avoid in the callers, so do it here. */
3103 if (rhs.type != ADDRESSOF
3104 && !get_varinfo (rhs.var)->may_have_pointers)
3105 return;
3107 /* Likewise adding to the solution of a non-pointer var isn't useful. */
3108 if (!get_varinfo (lhs.var)->may_have_pointers)
3109 return;
3111 /* This can happen in our IR with things like n->a = *p */
3112 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
3114 /* Split into tmp = *rhs, *lhs = tmp */
3115 struct constraint_expr tmplhs;
3116 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp", true);
3117 process_constraint (new_constraint (tmplhs, rhs));
3118 process_constraint (new_constraint (lhs, tmplhs));
3120 else if ((rhs.type != SCALAR || rhs.offset != 0) && lhs.type == DEREF)
3122 /* Split into tmp = &rhs, *lhs = tmp */
3123 struct constraint_expr tmplhs;
3124 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp", true);
3125 process_constraint (new_constraint (tmplhs, rhs));
3126 process_constraint (new_constraint (lhs, tmplhs));
3128 else
3130 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
3131 if (rhs.type == ADDRESSOF)
3132 get_varinfo (get_varinfo (rhs.var)->head)->address_taken = true;
3133 constraints.safe_push (t);
3138 /* Return the position, in bits, of FIELD_DECL from the beginning of its
3139 structure. */
3141 static HOST_WIDE_INT
3142 bitpos_of_field (const tree fdecl)
3144 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl))
3145 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl)))
3146 return -1;
3148 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
3149 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl)));
3153 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
3154 resulting constraint expressions in *RESULTS. */
3156 static void
3157 get_constraint_for_ptr_offset (tree ptr, tree offset,
3158 vec<ce_s> *results)
3160 struct constraint_expr c;
3161 unsigned int j, n;
3162 HOST_WIDE_INT rhsoffset;
3164 /* If we do not do field-sensitive PTA adding offsets to pointers
3165 does not change the points-to solution. */
3166 if (!use_field_sensitive)
3168 get_constraint_for_rhs (ptr, results);
3169 return;
3172 /* If the offset is not a non-negative integer constant that fits
3173 in a HOST_WIDE_INT, we have to fall back to a conservative
3174 solution which includes all sub-fields of all pointed-to
3175 variables of ptr. */
3176 if (offset == NULL_TREE
3177 || TREE_CODE (offset) != INTEGER_CST)
3178 rhsoffset = UNKNOWN_OFFSET;
3179 else
3181 /* Sign-extend the offset. */
3182 offset_int soffset = offset_int::from (wi::to_wide (offset), SIGNED);
3183 if (!wi::fits_shwi_p (soffset))
3184 rhsoffset = UNKNOWN_OFFSET;
3185 else
3187 /* Make sure the bit-offset also fits. */
3188 HOST_WIDE_INT rhsunitoffset = soffset.to_shwi ();
3189 rhsoffset = rhsunitoffset * (unsigned HOST_WIDE_INT) BITS_PER_UNIT;
3190 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
3191 rhsoffset = UNKNOWN_OFFSET;
3195 get_constraint_for_rhs (ptr, results);
3196 if (rhsoffset == 0)
3197 return;
3199 /* As we are eventually appending to the solution do not use
3200 vec::iterate here. */
3201 n = results->length ();
3202 for (j = 0; j < n; j++)
3204 varinfo_t curr;
3205 c = (*results)[j];
3206 curr = get_varinfo (c.var);
3208 if (c.type == ADDRESSOF
3209 /* If this varinfo represents a full variable just use it. */
3210 && curr->is_full_var)
3212 else if (c.type == ADDRESSOF
3213 /* If we do not know the offset add all subfields. */
3214 && rhsoffset == UNKNOWN_OFFSET)
3216 varinfo_t temp = get_varinfo (curr->head);
3219 struct constraint_expr c2;
3220 c2.var = temp->id;
3221 c2.type = ADDRESSOF;
3222 c2.offset = 0;
3223 if (c2.var != c.var)
3224 results->safe_push (c2);
3225 temp = vi_next (temp);
3227 while (temp);
3229 else if (c.type == ADDRESSOF)
3231 varinfo_t temp;
3232 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
3234 /* If curr->offset + rhsoffset is less than zero adjust it. */
3235 if (rhsoffset < 0
3236 && curr->offset < offset)
3237 offset = 0;
3239 /* We have to include all fields that overlap the current
3240 field shifted by rhsoffset. And we include at least
3241 the last or the first field of the variable to represent
3242 reachability of off-bound addresses, in particular &object + 1,
3243 conservatively correct. */
3244 temp = first_or_preceding_vi_for_offset (curr, offset);
3245 c.var = temp->id;
3246 c.offset = 0;
3247 temp = vi_next (temp);
3248 while (temp
3249 && temp->offset < offset + curr->size)
3251 struct constraint_expr c2;
3252 c2.var = temp->id;
3253 c2.type = ADDRESSOF;
3254 c2.offset = 0;
3255 results->safe_push (c2);
3256 temp = vi_next (temp);
3259 else if (c.type == SCALAR)
3261 gcc_assert (c.offset == 0);
3262 c.offset = rhsoffset;
3264 else
3265 /* We shouldn't get any DEREFs here. */
3266 gcc_unreachable ();
3268 (*results)[j] = c;
3273 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3274 If address_p is true the result will be taken its address of.
3275 If lhs_p is true then the constraint expression is assumed to be used
3276 as the lhs. */
3278 static void
3279 get_constraint_for_component_ref (tree t, vec<ce_s> *results,
3280 bool address_p, bool lhs_p)
3282 tree orig_t = t;
3283 poly_int64 bitsize = -1;
3284 poly_int64 bitmaxsize = -1;
3285 poly_int64 bitpos;
3286 bool reverse;
3287 tree forzero;
3289 /* Some people like to do cute things like take the address of
3290 &0->a.b */
3291 forzero = t;
3292 while (handled_component_p (forzero)
3293 || INDIRECT_REF_P (forzero)
3294 || TREE_CODE (forzero) == MEM_REF)
3295 forzero = TREE_OPERAND (forzero, 0);
3297 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
3299 struct constraint_expr temp;
3301 temp.offset = 0;
3302 temp.var = integer_id;
3303 temp.type = SCALAR;
3304 results->safe_push (temp);
3305 return;
3308 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize, &reverse);
3310 /* We can end up here for component references on a
3311 VIEW_CONVERT_EXPR <>(&foobar) or things like a
3312 BIT_FIELD_REF <&MEM[(void *)&b + 4B], ...>. So for
3313 symbolic constants simply give up. */
3314 if (TREE_CODE (t) == ADDR_EXPR)
3316 constraint_expr result;
3317 result.type = SCALAR;
3318 result.var = anything_id;
3319 result.offset = 0;
3320 results->safe_push (result);
3321 return;
3324 /* Avoid creating pointer-offset constraints, so handle MEM_REF
3325 offsets directly. Pretend to take the address of the base,
3326 we'll take care of adding the required subset of sub-fields below. */
3327 if (TREE_CODE (t) == MEM_REF
3328 && !integer_zerop (TREE_OPERAND (t, 0)))
3330 poly_offset_int off = mem_ref_offset (t);
3331 off <<= LOG2_BITS_PER_UNIT;
3332 off += bitpos;
3333 poly_int64 off_hwi;
3334 if (off.to_shwi (&off_hwi))
3335 bitpos = off_hwi;
3336 else
3338 bitpos = 0;
3339 bitmaxsize = -1;
3341 get_constraint_for_1 (TREE_OPERAND (t, 0), results, false, lhs_p);
3342 do_deref (results);
3344 else
3345 get_constraint_for_1 (t, results, true, lhs_p);
3347 /* Strip off nothing_id. */
3348 if (results->length () == 2)
3350 gcc_assert ((*results)[0].var == nothing_id);
3351 results->unordered_remove (0);
3353 gcc_assert (results->length () == 1);
3354 struct constraint_expr &result = results->last ();
3356 if (result.type == SCALAR
3357 && get_varinfo (result.var)->is_full_var)
3358 /* For single-field vars do not bother about the offset. */
3359 result.offset = 0;
3360 else if (result.type == SCALAR)
3362 /* In languages like C, you can access one past the end of an
3363 array. You aren't allowed to dereference it, so we can
3364 ignore this constraint. When we handle pointer subtraction,
3365 we may have to do something cute here. */
3367 if (maybe_lt (poly_uint64 (bitpos), get_varinfo (result.var)->fullsize)
3368 && maybe_ne (bitmaxsize, 0))
3370 /* It's also not true that the constraint will actually start at the
3371 right offset, it may start in some padding. We only care about
3372 setting the constraint to the first actual field it touches, so
3373 walk to find it. */
3374 struct constraint_expr cexpr = result;
3375 varinfo_t curr;
3376 results->pop ();
3377 cexpr.offset = 0;
3378 for (curr = get_varinfo (cexpr.var); curr; curr = vi_next (curr))
3380 if (ranges_maybe_overlap_p (poly_int64 (curr->offset),
3381 curr->size, bitpos, bitmaxsize))
3383 cexpr.var = curr->id;
3384 results->safe_push (cexpr);
3385 if (address_p)
3386 break;
3389 /* If we are going to take the address of this field then
3390 to be able to compute reachability correctly add at least
3391 the last field of the variable. */
3392 if (address_p && results->length () == 0)
3394 curr = get_varinfo (cexpr.var);
3395 while (curr->next != 0)
3396 curr = vi_next (curr);
3397 cexpr.var = curr->id;
3398 results->safe_push (cexpr);
3400 else if (results->length () == 0)
3401 /* Assert that we found *some* field there. The user couldn't be
3402 accessing *only* padding. */
3403 /* Still the user could access one past the end of an array
3404 embedded in a struct resulting in accessing *only* padding. */
3405 /* Or accessing only padding via type-punning to a type
3406 that has a filed just in padding space. */
3408 cexpr.type = SCALAR;
3409 cexpr.var = anything_id;
3410 cexpr.offset = 0;
3411 results->safe_push (cexpr);
3414 else if (known_eq (bitmaxsize, 0))
3416 if (dump_file && (dump_flags & TDF_DETAILS))
3417 fprintf (dump_file, "Access to zero-sized part of variable, "
3418 "ignoring\n");
3420 else
3421 if (dump_file && (dump_flags & TDF_DETAILS))
3422 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3424 else if (result.type == DEREF)
3426 /* If we do not know exactly where the access goes say so. Note
3427 that only for non-structure accesses we know that we access
3428 at most one subfiled of any variable. */
3429 HOST_WIDE_INT const_bitpos;
3430 if (!bitpos.is_constant (&const_bitpos)
3431 || const_bitpos == -1
3432 || maybe_ne (bitsize, bitmaxsize)
3433 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
3434 || result.offset == UNKNOWN_OFFSET)
3435 result.offset = UNKNOWN_OFFSET;
3436 else
3437 result.offset += const_bitpos;
3439 else if (result.type == ADDRESSOF)
3441 /* We can end up here for component references on constants like
3442 VIEW_CONVERT_EXPR <>({ 0, 1, 2, 3 })[i]. */
3443 result.type = SCALAR;
3444 result.var = anything_id;
3445 result.offset = 0;
3447 else
3448 gcc_unreachable ();
3452 /* Dereference the constraint expression CONS, and return the result.
3453 DEREF (ADDRESSOF) = SCALAR
3454 DEREF (SCALAR) = DEREF
3455 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3456 This is needed so that we can handle dereferencing DEREF constraints. */
3458 static void
3459 do_deref (vec<ce_s> *constraints)
3461 struct constraint_expr *c;
3462 unsigned int i = 0;
3464 FOR_EACH_VEC_ELT (*constraints, i, c)
3466 if (c->type == SCALAR)
3467 c->type = DEREF;
3468 else if (c->type == ADDRESSOF)
3469 c->type = SCALAR;
3470 else if (c->type == DEREF)
3472 struct constraint_expr tmplhs;
3473 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp", true);
3474 process_constraint (new_constraint (tmplhs, *c));
3475 c->var = tmplhs.var;
3477 else
3478 gcc_unreachable ();
3482 /* Given a tree T, return the constraint expression for taking the
3483 address of it. */
3485 static void
3486 get_constraint_for_address_of (tree t, vec<ce_s> *results)
3488 struct constraint_expr *c;
3489 unsigned int i;
3491 get_constraint_for_1 (t, results, true, true);
3493 FOR_EACH_VEC_ELT (*results, i, c)
3495 if (c->type == DEREF)
3496 c->type = SCALAR;
3497 else
3498 c->type = ADDRESSOF;
3502 /* Given a tree T, return the constraint expression for it. */
3504 static void
3505 get_constraint_for_1 (tree t, vec<ce_s> *results, bool address_p,
3506 bool lhs_p)
3508 struct constraint_expr temp;
3510 /* x = integer is all glommed to a single variable, which doesn't
3511 point to anything by itself. That is, of course, unless it is an
3512 integer constant being treated as a pointer, in which case, we
3513 will return that this is really the addressof anything. This
3514 happens below, since it will fall into the default case. The only
3515 case we know something about an integer treated like a pointer is
3516 when it is the NULL pointer, and then we just say it points to
3517 NULL.
3519 Do not do that if -fno-delete-null-pointer-checks though, because
3520 in that case *NULL does not fail, so it _should_ alias *anything.
3521 It is not worth adding a new option or renaming the existing one,
3522 since this case is relatively obscure. */
3523 if ((TREE_CODE (t) == INTEGER_CST
3524 && integer_zerop (t))
3525 /* The only valid CONSTRUCTORs in gimple with pointer typed
3526 elements are zero-initializer. But in IPA mode we also
3527 process global initializers, so verify at least. */
3528 || (TREE_CODE (t) == CONSTRUCTOR
3529 && CONSTRUCTOR_NELTS (t) == 0))
3531 if (flag_delete_null_pointer_checks)
3532 temp.var = nothing_id;
3533 else
3534 temp.var = nonlocal_id;
3535 temp.type = ADDRESSOF;
3536 temp.offset = 0;
3537 results->safe_push (temp);
3538 return;
3541 /* String constants are read-only, ideally we'd have a CONST_DECL
3542 for those. */
3543 if (TREE_CODE (t) == STRING_CST)
3545 temp.var = string_id;
3546 temp.type = SCALAR;
3547 temp.offset = 0;
3548 results->safe_push (temp);
3549 return;
3552 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3554 case tcc_expression:
3556 switch (TREE_CODE (t))
3558 case ADDR_EXPR:
3559 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3560 return;
3561 default:;
3563 break;
3565 case tcc_reference:
3567 switch (TREE_CODE (t))
3569 case MEM_REF:
3571 struct constraint_expr cs;
3572 varinfo_t vi, curr;
3573 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0),
3574 TREE_OPERAND (t, 1), results);
3575 do_deref (results);
3577 /* If we are not taking the address then make sure to process
3578 all subvariables we might access. */
3579 if (address_p)
3580 return;
3582 cs = results->last ();
3583 if (cs.type == DEREF
3584 && type_can_have_subvars (TREE_TYPE (t)))
3586 /* For dereferences this means we have to defer it
3587 to solving time. */
3588 results->last ().offset = UNKNOWN_OFFSET;
3589 return;
3591 if (cs.type != SCALAR)
3592 return;
3594 vi = get_varinfo (cs.var);
3595 curr = vi_next (vi);
3596 if (!vi->is_full_var
3597 && curr)
3599 unsigned HOST_WIDE_INT size;
3600 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t))))
3601 size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t)));
3602 else
3603 size = -1;
3604 for (; curr; curr = vi_next (curr))
3606 if (curr->offset - vi->offset < size)
3608 cs.var = curr->id;
3609 results->safe_push (cs);
3611 else
3612 break;
3615 return;
3617 case ARRAY_REF:
3618 case ARRAY_RANGE_REF:
3619 case COMPONENT_REF:
3620 case IMAGPART_EXPR:
3621 case REALPART_EXPR:
3622 case BIT_FIELD_REF:
3623 get_constraint_for_component_ref (t, results, address_p, lhs_p);
3624 return;
3625 case VIEW_CONVERT_EXPR:
3626 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p,
3627 lhs_p);
3628 return;
3629 /* We are missing handling for TARGET_MEM_REF here. */
3630 default:;
3632 break;
3634 case tcc_exceptional:
3636 switch (TREE_CODE (t))
3638 case SSA_NAME:
3640 get_constraint_for_ssa_var (t, results, address_p);
3641 return;
3643 case CONSTRUCTOR:
3645 unsigned int i;
3646 tree val;
3647 auto_vec<ce_s> tmp;
3648 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
3650 struct constraint_expr *rhsp;
3651 unsigned j;
3652 get_constraint_for_1 (val, &tmp, address_p, lhs_p);
3653 FOR_EACH_VEC_ELT (tmp, j, rhsp)
3654 results->safe_push (*rhsp);
3655 tmp.truncate (0);
3657 /* We do not know whether the constructor was complete,
3658 so technically we have to add &NOTHING or &ANYTHING
3659 like we do for an empty constructor as well. */
3660 return;
3662 default:;
3664 break;
3666 case tcc_declaration:
3668 get_constraint_for_ssa_var (t, results, address_p);
3669 return;
3671 case tcc_constant:
3673 /* We cannot refer to automatic variables through constants. */
3674 temp.type = ADDRESSOF;
3675 temp.var = nonlocal_id;
3676 temp.offset = 0;
3677 results->safe_push (temp);
3678 return;
3680 default:;
3683 /* The default fallback is a constraint from anything. */
3684 temp.type = ADDRESSOF;
3685 temp.var = anything_id;
3686 temp.offset = 0;
3687 results->safe_push (temp);
3690 /* Given a gimple tree T, return the constraint expression vector for it. */
3692 static void
3693 get_constraint_for (tree t, vec<ce_s> *results)
3695 gcc_assert (results->length () == 0);
3697 get_constraint_for_1 (t, results, false, true);
3700 /* Given a gimple tree T, return the constraint expression vector for it
3701 to be used as the rhs of a constraint. */
3703 static void
3704 get_constraint_for_rhs (tree t, vec<ce_s> *results)
3706 gcc_assert (results->length () == 0);
3708 get_constraint_for_1 (t, results, false, false);
3712 /* Efficiently generates constraints from all entries in *RHSC to all
3713 entries in *LHSC. */
3715 static void
3716 process_all_all_constraints (const vec<ce_s> &lhsc,
3717 const vec<ce_s> &rhsc)
3719 struct constraint_expr *lhsp, *rhsp;
3720 unsigned i, j;
3722 if (lhsc.length () <= 1 || rhsc.length () <= 1)
3724 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3725 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
3726 process_constraint (new_constraint (*lhsp, *rhsp));
3728 else
3730 struct constraint_expr tmp;
3731 tmp = new_scalar_tmp_constraint_exp ("allalltmp", true);
3732 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
3733 process_constraint (new_constraint (tmp, *rhsp));
3734 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3735 process_constraint (new_constraint (*lhsp, tmp));
3739 /* Handle aggregate copies by expanding into copies of the respective
3740 fields of the structures. */
3742 static void
3743 do_structure_copy (tree lhsop, tree rhsop)
3745 struct constraint_expr *lhsp, *rhsp;
3746 auto_vec<ce_s> lhsc;
3747 auto_vec<ce_s> rhsc;
3748 unsigned j;
3750 get_constraint_for (lhsop, &lhsc);
3751 get_constraint_for_rhs (rhsop, &rhsc);
3752 lhsp = &lhsc[0];
3753 rhsp = &rhsc[0];
3754 if (lhsp->type == DEREF
3755 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3756 || rhsp->type == DEREF)
3758 if (lhsp->type == DEREF)
3760 gcc_assert (lhsc.length () == 1);
3761 lhsp->offset = UNKNOWN_OFFSET;
3763 if (rhsp->type == DEREF)
3765 gcc_assert (rhsc.length () == 1);
3766 rhsp->offset = UNKNOWN_OFFSET;
3768 process_all_all_constraints (lhsc, rhsc);
3770 else if (lhsp->type == SCALAR
3771 && (rhsp->type == SCALAR
3772 || rhsp->type == ADDRESSOF))
3774 HOST_WIDE_INT lhssize, lhsoffset;
3775 HOST_WIDE_INT rhssize, rhsoffset;
3776 bool reverse;
3777 unsigned k = 0;
3778 if (!get_ref_base_and_extent_hwi (lhsop, &lhsoffset, &lhssize, &reverse)
3779 || !get_ref_base_and_extent_hwi (rhsop, &rhsoffset, &rhssize,
3780 &reverse))
3782 process_all_all_constraints (lhsc, rhsc);
3783 return;
3785 for (j = 0; lhsc.iterate (j, &lhsp);)
3787 varinfo_t lhsv, rhsv;
3788 rhsp = &rhsc[k];
3789 lhsv = get_varinfo (lhsp->var);
3790 rhsv = get_varinfo (rhsp->var);
3791 if (lhsv->may_have_pointers
3792 && (lhsv->is_full_var
3793 || rhsv->is_full_var
3794 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3795 rhsv->offset + lhsoffset, rhsv->size)))
3796 process_constraint (new_constraint (*lhsp, *rhsp));
3797 if (!rhsv->is_full_var
3798 && (lhsv->is_full_var
3799 || (lhsv->offset + rhsoffset + lhsv->size
3800 > rhsv->offset + lhsoffset + rhsv->size)))
3802 ++k;
3803 if (k >= rhsc.length ())
3804 break;
3806 else
3807 ++j;
3810 else
3811 gcc_unreachable ();
3814 /* Create constraints ID = { rhsc }. */
3816 static void
3817 make_constraints_to (unsigned id, const vec<ce_s> &rhsc)
3819 struct constraint_expr *c;
3820 struct constraint_expr includes;
3821 unsigned int j;
3823 includes.var = id;
3824 includes.offset = 0;
3825 includes.type = SCALAR;
3827 FOR_EACH_VEC_ELT (rhsc, j, c)
3828 process_constraint (new_constraint (includes, *c));
3831 /* Create a constraint ID = OP. */
3833 static void
3834 make_constraint_to (unsigned id, tree op)
3836 auto_vec<ce_s> rhsc;
3837 get_constraint_for_rhs (op, &rhsc);
3838 make_constraints_to (id, rhsc);
3841 /* Create a constraint ID = &FROM. */
3843 static void
3844 make_constraint_from (varinfo_t vi, int from)
3846 struct constraint_expr lhs, rhs;
3848 lhs.var = vi->id;
3849 lhs.offset = 0;
3850 lhs.type = SCALAR;
3852 rhs.var = from;
3853 rhs.offset = 0;
3854 rhs.type = ADDRESSOF;
3855 process_constraint (new_constraint (lhs, rhs));
3858 /* Create a constraint ID = FROM. */
3860 static void
3861 make_copy_constraint (varinfo_t vi, int from)
3863 struct constraint_expr lhs, rhs;
3865 lhs.var = vi->id;
3866 lhs.offset = 0;
3867 lhs.type = SCALAR;
3869 rhs.var = from;
3870 rhs.offset = 0;
3871 rhs.type = SCALAR;
3872 process_constraint (new_constraint (lhs, rhs));
3875 /* Make constraints necessary to make OP escape. */
3877 static void
3878 make_escape_constraint (tree op)
3880 make_constraint_to (escaped_id, op);
3883 /* Make constraint necessary to make all indirect references
3884 from VI escape. */
3886 static void
3887 make_indirect_escape_constraint (varinfo_t vi)
3889 struct constraint_expr lhs, rhs;
3890 /* escaped = *(VAR + UNKNOWN); */
3891 lhs.type = SCALAR;
3892 lhs.var = escaped_id;
3893 lhs.offset = 0;
3894 rhs.type = DEREF;
3895 rhs.var = vi->id;
3896 rhs.offset = UNKNOWN_OFFSET;
3897 process_constraint (new_constraint (lhs, rhs));
3900 /* Add constraints to that the solution of VI is transitively closed. */
3902 static void
3903 make_transitive_closure_constraints (varinfo_t vi)
3905 struct constraint_expr lhs, rhs;
3907 /* VAR = *(VAR + UNKNOWN); */
3908 lhs.type = SCALAR;
3909 lhs.var = vi->id;
3910 lhs.offset = 0;
3911 rhs.type = DEREF;
3912 rhs.var = vi->id;
3913 rhs.offset = UNKNOWN_OFFSET;
3914 process_constraint (new_constraint (lhs, rhs));
3917 /* Add constraints to that the solution of VI has all subvariables added. */
3919 static void
3920 make_any_offset_constraints (varinfo_t vi)
3922 struct constraint_expr lhs, rhs;
3924 /* VAR = VAR + UNKNOWN; */
3925 lhs.type = SCALAR;
3926 lhs.var = vi->id;
3927 lhs.offset = 0;
3928 rhs.type = SCALAR;
3929 rhs.var = vi->id;
3930 rhs.offset = UNKNOWN_OFFSET;
3931 process_constraint (new_constraint (lhs, rhs));
3934 /* Temporary storage for fake var decls. */
3935 struct obstack fake_var_decl_obstack;
3937 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3939 static tree
3940 build_fake_var_decl (tree type)
3942 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl);
3943 memset (decl, 0, sizeof (struct tree_var_decl));
3944 TREE_SET_CODE (decl, VAR_DECL);
3945 TREE_TYPE (decl) = type;
3946 DECL_UID (decl) = allocate_decl_uid ();
3947 SET_DECL_PT_UID (decl, -1);
3948 layout_decl (decl, 0);
3949 return decl;
3952 /* Create a new artificial heap variable with NAME.
3953 Return the created variable. */
3955 static varinfo_t
3956 make_heapvar (const char *name, bool add_id)
3958 varinfo_t vi;
3959 tree heapvar;
3961 heapvar = build_fake_var_decl (ptr_type_node);
3962 DECL_EXTERNAL (heapvar) = 1;
3964 vi = new_var_info (heapvar, name, add_id);
3965 vi->is_heap_var = true;
3966 vi->is_unknown_size_var = true;
3967 vi->offset = 0;
3968 vi->fullsize = ~0;
3969 vi->size = ~0;
3970 vi->is_full_var = true;
3971 insert_vi_for_tree (heapvar, vi);
3973 return vi;
3976 /* Create a new artificial heap variable with NAME and make a
3977 constraint from it to LHS. Set flags according to a tag used
3978 for tracking restrict pointers. */
3980 static varinfo_t
3981 make_constraint_from_restrict (varinfo_t lhs, const char *name, bool add_id)
3983 varinfo_t vi = make_heapvar (name, add_id);
3984 vi->is_restrict_var = 1;
3985 vi->is_global_var = 1;
3986 vi->may_have_pointers = 1;
3987 make_constraint_from (lhs, vi->id);
3988 return vi;
3991 /* Create a new artificial heap variable with NAME and make a
3992 constraint from it to LHS. Set flags according to a tag used
3993 for tracking restrict pointers and make the artificial heap
3994 point to global memory. */
3996 static varinfo_t
3997 make_constraint_from_global_restrict (varinfo_t lhs, const char *name,
3998 bool add_id)
4000 varinfo_t vi = make_constraint_from_restrict (lhs, name, add_id);
4001 make_copy_constraint (vi, nonlocal_id);
4002 return vi;
4005 /* In IPA mode there are varinfos for different aspects of reach
4006 function designator. One for the points-to set of the return
4007 value, one for the variables that are clobbered by the function,
4008 one for its uses and one for each parameter (including a single
4009 glob for remaining variadic arguments). */
4011 enum { fi_clobbers = 1, fi_uses = 2,
4012 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 };
4014 /* Get a constraint for the requested part of a function designator FI
4015 when operating in IPA mode. */
4017 static struct constraint_expr
4018 get_function_part_constraint (varinfo_t fi, unsigned part)
4020 struct constraint_expr c;
4022 gcc_assert (in_ipa_mode);
4024 if (fi->id == anything_id)
4026 /* ??? We probably should have a ANYFN special variable. */
4027 c.var = anything_id;
4028 c.offset = 0;
4029 c.type = SCALAR;
4031 else if (fi->decl && TREE_CODE (fi->decl) == FUNCTION_DECL)
4033 varinfo_t ai = first_vi_for_offset (fi, part);
4034 if (ai)
4035 c.var = ai->id;
4036 else
4037 c.var = anything_id;
4038 c.offset = 0;
4039 c.type = SCALAR;
4041 else
4043 c.var = fi->id;
4044 c.offset = part;
4045 c.type = DEREF;
4048 return c;
4051 /* For non-IPA mode, generate constraints necessary for a call on the
4052 RHS. */
4054 static void
4055 handle_rhs_call (gcall *stmt, vec<ce_s> *results)
4057 struct constraint_expr rhsc;
4058 unsigned i;
4059 bool returns_uses = false;
4061 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4063 tree arg = gimple_call_arg (stmt, i);
4064 int flags = gimple_call_arg_flags (stmt, i);
4066 /* If the argument is not used we can ignore it.
4067 Similarly argument is invisile for us if it not clobbered, does not
4068 escape, is not read and can not be returned. */
4069 if ((flags & EAF_UNUSED)
4070 || ((flags & (EAF_NOCLOBBER | EAF_NOESCAPE | EAF_NOREAD
4071 | EAF_NOT_RETURNED))
4072 == (EAF_NOCLOBBER | EAF_NOESCAPE | EAF_NOREAD
4073 | EAF_NOT_RETURNED)))
4074 continue;
4076 /* As we compute ESCAPED context-insensitive we do not gain
4077 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
4078 set. The argument would still get clobbered through the
4079 escape solution. */
4080 if ((flags & EAF_NOCLOBBER)
4081 && (flags & (EAF_NOESCAPE | EAF_NODIRECTESCAPE)))
4083 varinfo_t uses = get_call_use_vi (stmt);
4084 varinfo_t tem = new_var_info (NULL_TREE, "callarg", true);
4085 tem->is_reg_var = true;
4086 make_constraint_to (tem->id, arg);
4087 make_any_offset_constraints (tem);
4088 if (!(flags & EAF_DIRECT))
4089 make_transitive_closure_constraints (tem);
4090 make_copy_constraint (uses, tem->id);
4091 /* TODO: This is overly conservative when some parameters are
4092 returned while others are not. */
4093 if (!(flags & EAF_NOT_RETURNED))
4094 returns_uses = true;
4095 if (!(flags & (EAF_NOESCAPE | EAF_DIRECT)))
4096 make_indirect_escape_constraint (tem);
4098 else if (flags & (EAF_NOESCAPE | EAF_NODIRECTESCAPE))
4100 struct constraint_expr lhs, rhs;
4101 varinfo_t uses = get_call_use_vi (stmt);
4102 varinfo_t clobbers = get_call_clobber_vi (stmt);
4103 varinfo_t tem = new_var_info (NULL_TREE, "callarg", true);
4104 tem->is_reg_var = true;
4105 make_constraint_to (tem->id, arg);
4106 make_any_offset_constraints (tem);
4107 if (!(flags & EAF_DIRECT))
4108 make_transitive_closure_constraints (tem);
4109 make_copy_constraint (uses, tem->id);
4110 if (!(flags & EAF_NOT_RETURNED))
4111 returns_uses = true;
4112 make_copy_constraint (clobbers, tem->id);
4113 /* Add *tem = nonlocal, do not add *tem = callused as
4114 EAF_NOESCAPE parameters do not escape to other parameters
4115 and all other uses appear in NONLOCAL as well. */
4116 lhs.type = DEREF;
4117 lhs.var = tem->id;
4118 lhs.offset = 0;
4119 rhs.type = SCALAR;
4120 rhs.var = nonlocal_id;
4121 rhs.offset = 0;
4122 process_constraint (new_constraint (lhs, rhs));
4123 if (!(flags & (EAF_NOESCAPE | EAF_DIRECT)))
4124 make_indirect_escape_constraint (tem);
4126 else
4127 make_escape_constraint (arg);
4130 /* If we added to the calls uses solution make sure we account for
4131 pointers to it to be returned. */
4132 if (returns_uses)
4134 rhsc.var = get_call_use_vi (stmt)->id;
4135 rhsc.offset = UNKNOWN_OFFSET;
4136 rhsc.type = SCALAR;
4137 results->safe_push (rhsc);
4140 /* The static chain escapes as well. */
4141 if (gimple_call_chain (stmt))
4142 make_escape_constraint (gimple_call_chain (stmt));
4144 /* And if we applied NRV the address of the return slot escapes as well. */
4145 if (gimple_call_return_slot_opt_p (stmt)
4146 && gimple_call_lhs (stmt) != NULL_TREE
4147 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
4149 auto_vec<ce_s> tmpc;
4150 struct constraint_expr lhsc, *c;
4151 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
4152 lhsc.var = escaped_id;
4153 lhsc.offset = 0;
4154 lhsc.type = SCALAR;
4155 FOR_EACH_VEC_ELT (tmpc, i, c)
4156 process_constraint (new_constraint (lhsc, *c));
4159 /* Regular functions return nonlocal memory. */
4160 rhsc.var = nonlocal_id;
4161 rhsc.offset = 0;
4162 rhsc.type = SCALAR;
4163 results->safe_push (rhsc);
4166 /* For non-IPA mode, generate constraints necessary for a call
4167 that returns a pointer and assigns it to LHS. This simply makes
4168 the LHS point to global and escaped variables. */
4170 static void
4171 handle_lhs_call (gcall *stmt, tree lhs, int flags, vec<ce_s> &rhsc,
4172 tree fndecl)
4174 auto_vec<ce_s> lhsc;
4176 get_constraint_for (lhs, &lhsc);
4177 /* If the store is to a global decl make sure to
4178 add proper escape constraints. */
4179 lhs = get_base_address (lhs);
4180 if (lhs
4181 && DECL_P (lhs)
4182 && is_global_var (lhs))
4184 struct constraint_expr tmpc;
4185 tmpc.var = escaped_id;
4186 tmpc.offset = 0;
4187 tmpc.type = SCALAR;
4188 lhsc.safe_push (tmpc);
4191 /* If the call returns an argument unmodified override the rhs
4192 constraints. */
4193 if (flags & ERF_RETURNS_ARG
4194 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
4196 tree arg;
4197 rhsc.create (0);
4198 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
4199 get_constraint_for (arg, &rhsc);
4200 process_all_all_constraints (lhsc, rhsc);
4201 rhsc.release ();
4203 else if (flags & ERF_NOALIAS)
4205 varinfo_t vi;
4206 struct constraint_expr tmpc;
4207 rhsc.create (0);
4208 vi = make_heapvar ("HEAP", true);
4209 /* We are marking allocated storage local, we deal with it becoming
4210 global by escaping and setting of vars_contains_escaped_heap. */
4211 DECL_EXTERNAL (vi->decl) = 0;
4212 vi->is_global_var = 0;
4213 /* If this is not a real malloc call assume the memory was
4214 initialized and thus may point to global memory. All
4215 builtin functions with the malloc attribute behave in a sane way. */
4216 if (!fndecl
4217 || !fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
4218 make_constraint_from (vi, nonlocal_id);
4219 tmpc.var = vi->id;
4220 tmpc.offset = 0;
4221 tmpc.type = ADDRESSOF;
4222 rhsc.safe_push (tmpc);
4223 process_all_all_constraints (lhsc, rhsc);
4224 rhsc.release ();
4226 else
4227 process_all_all_constraints (lhsc, rhsc);
4230 /* For non-IPA mode, generate constraints necessary for a call of a
4231 const function that returns a pointer in the statement STMT. */
4233 static void
4234 handle_const_call (gcall *stmt, vec<ce_s> *results)
4236 struct constraint_expr rhsc;
4237 unsigned int k;
4238 bool need_uses = false;
4240 /* Treat nested const functions the same as pure functions as far
4241 as the static chain is concerned. */
4242 if (gimple_call_chain (stmt))
4244 varinfo_t uses = get_call_use_vi (stmt);
4245 make_constraint_to (uses->id, gimple_call_chain (stmt));
4246 need_uses = true;
4249 /* And if we applied NRV the address of the return slot escapes as well. */
4250 if (gimple_call_return_slot_opt_p (stmt)
4251 && gimple_call_lhs (stmt) != NULL_TREE
4252 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
4254 varinfo_t uses = get_call_use_vi (stmt);
4255 auto_vec<ce_s> tmpc;
4256 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
4257 make_constraints_to (uses->id, tmpc);
4258 need_uses = true;
4261 if (need_uses)
4263 varinfo_t uses = get_call_use_vi (stmt);
4264 make_any_offset_constraints (uses);
4265 make_transitive_closure_constraints (uses);
4266 rhsc.var = uses->id;
4267 rhsc.offset = 0;
4268 rhsc.type = SCALAR;
4269 results->safe_push (rhsc);
4272 /* May return offsetted arguments. */
4273 varinfo_t tem = NULL;
4274 for (k = 0; k < gimple_call_num_args (stmt); ++k)
4276 int flags = gimple_call_arg_flags (stmt, k);
4278 /* If the argument is not used or not returned we can ignore it. */
4279 if (flags & (EAF_UNUSED | EAF_NOT_RETURNED))
4280 continue;
4281 if (!tem)
4283 tem = new_var_info (NULL_TREE, "callarg", true);
4284 tem->is_reg_var = true;
4286 tree arg = gimple_call_arg (stmt, k);
4287 auto_vec<ce_s> argc;
4288 get_constraint_for_rhs (arg, &argc);
4289 make_constraints_to (tem->id, argc);
4291 if (tem)
4293 ce_s ce;
4294 ce.type = SCALAR;
4295 ce.var = tem->id;
4296 ce.offset = UNKNOWN_OFFSET;
4297 results->safe_push (ce);
4300 /* May return addresses of globals. */
4301 rhsc.var = nonlocal_id;
4302 rhsc.offset = 0;
4303 rhsc.type = ADDRESSOF;
4304 results->safe_push (rhsc);
4307 /* For non-IPA mode, generate constraints necessary for a call to a
4308 pure function in statement STMT. */
4310 static void
4311 handle_pure_call (gcall *stmt, vec<ce_s> *results)
4313 struct constraint_expr rhsc;
4314 unsigned i;
4315 varinfo_t uses = NULL;
4316 bool record_uses = false;
4318 /* Memory reached from pointer arguments is call-used. */
4319 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4321 tree arg = gimple_call_arg (stmt, i);
4322 int flags = gimple_call_arg_flags (stmt, i);
4324 /* If the argument is not used we can ignore it. */
4325 if ((flags & EAF_UNUSED)
4326 || (flags & (EAF_NOT_RETURNED | EAF_NOREAD))
4327 == (EAF_NOT_RETURNED | EAF_NOREAD))
4328 continue;
4329 if (!uses)
4331 uses = get_call_use_vi (stmt);
4332 make_any_offset_constraints (uses);
4333 make_transitive_closure_constraints (uses);
4335 make_constraint_to (uses->id, arg);
4336 if (!(flags & EAF_NOT_RETURNED))
4337 record_uses = true;
4340 /* The static chain is used as well. */
4341 if (gimple_call_chain (stmt))
4343 if (!uses)
4345 uses = get_call_use_vi (stmt);
4346 make_any_offset_constraints (uses);
4347 make_transitive_closure_constraints (uses);
4349 make_constraint_to (uses->id, gimple_call_chain (stmt));
4350 record_uses = true;
4353 /* And if we applied NRV the address of the return slot. */
4354 if (gimple_call_return_slot_opt_p (stmt)
4355 && gimple_call_lhs (stmt) != NULL_TREE
4356 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
4358 if (!uses)
4360 uses = get_call_use_vi (stmt);
4361 make_any_offset_constraints (uses);
4362 make_transitive_closure_constraints (uses);
4364 auto_vec<ce_s> tmpc;
4365 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
4366 make_constraints_to (uses->id, tmpc);
4367 record_uses = true;
4370 /* Pure functions may return call-used and nonlocal memory. */
4371 if (record_uses)
4373 rhsc.var = uses->id;
4374 rhsc.offset = 0;
4375 rhsc.type = SCALAR;
4376 results->safe_push (rhsc);
4378 rhsc.var = nonlocal_id;
4379 rhsc.offset = 0;
4380 rhsc.type = SCALAR;
4381 results->safe_push (rhsc);
4385 /* Return the varinfo for the callee of CALL. */
4387 static varinfo_t
4388 get_fi_for_callee (gcall *call)
4390 tree decl, fn = gimple_call_fn (call);
4392 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
4393 fn = OBJ_TYPE_REF_EXPR (fn);
4395 /* If we can directly resolve the function being called, do so.
4396 Otherwise, it must be some sort of indirect expression that
4397 we should still be able to handle. */
4398 decl = gimple_call_addr_fndecl (fn);
4399 if (decl)
4400 return get_vi_for_tree (decl);
4402 /* If the function is anything other than a SSA name pointer we have no
4403 clue and should be getting ANYFN (well, ANYTHING for now). */
4404 if (!fn || TREE_CODE (fn) != SSA_NAME)
4405 return get_varinfo (anything_id);
4407 if (SSA_NAME_IS_DEFAULT_DEF (fn)
4408 && (TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
4409 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL))
4410 fn = SSA_NAME_VAR (fn);
4412 return get_vi_for_tree (fn);
4415 /* Create constraints for assigning call argument ARG to the incoming parameter
4416 INDEX of function FI. */
4418 static void
4419 find_func_aliases_for_call_arg (varinfo_t fi, unsigned index, tree arg)
4421 struct constraint_expr lhs;
4422 lhs = get_function_part_constraint (fi, fi_parm_base + index);
4424 auto_vec<ce_s, 2> rhsc;
4425 get_constraint_for_rhs (arg, &rhsc);
4427 unsigned j;
4428 struct constraint_expr *rhsp;
4429 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4430 process_constraint (new_constraint (lhs, *rhsp));
4433 /* Return true if FNDECL may be part of another lto partition. */
4435 static bool
4436 fndecl_maybe_in_other_partition (tree fndecl)
4438 cgraph_node *fn_node = cgraph_node::get (fndecl);
4439 if (fn_node == NULL)
4440 return true;
4442 return fn_node->in_other_partition;
4445 /* Create constraints for the builtin call T. Return true if the call
4446 was handled, otherwise false. */
4448 static bool
4449 find_func_aliases_for_builtin_call (struct function *fn, gcall *t)
4451 tree fndecl = gimple_call_fndecl (t);
4452 auto_vec<ce_s, 2> lhsc;
4453 auto_vec<ce_s, 4> rhsc;
4454 varinfo_t fi;
4456 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4457 /* ??? All builtins that are handled here need to be handled
4458 in the alias-oracle query functions explicitly! */
4459 switch (DECL_FUNCTION_CODE (fndecl))
4461 /* All the following functions return a pointer to the same object
4462 as their first argument points to. The functions do not add
4463 to the ESCAPED solution. The functions make the first argument
4464 pointed to memory point to what the second argument pointed to
4465 memory points to. */
4466 case BUILT_IN_STRCPY:
4467 case BUILT_IN_STRNCPY:
4468 case BUILT_IN_BCOPY:
4469 case BUILT_IN_MEMCPY:
4470 case BUILT_IN_MEMMOVE:
4471 case BUILT_IN_MEMPCPY:
4472 case BUILT_IN_STPCPY:
4473 case BUILT_IN_STPNCPY:
4474 case BUILT_IN_STRCAT:
4475 case BUILT_IN_STRNCAT:
4476 case BUILT_IN_STRCPY_CHK:
4477 case BUILT_IN_STRNCPY_CHK:
4478 case BUILT_IN_MEMCPY_CHK:
4479 case BUILT_IN_MEMMOVE_CHK:
4480 case BUILT_IN_MEMPCPY_CHK:
4481 case BUILT_IN_STPCPY_CHK:
4482 case BUILT_IN_STPNCPY_CHK:
4483 case BUILT_IN_STRCAT_CHK:
4484 case BUILT_IN_STRNCAT_CHK:
4485 case BUILT_IN_TM_MEMCPY:
4486 case BUILT_IN_TM_MEMMOVE:
4488 tree res = gimple_call_lhs (t);
4489 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4490 == BUILT_IN_BCOPY ? 1 : 0));
4491 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4492 == BUILT_IN_BCOPY ? 0 : 1));
4493 if (res != NULL_TREE)
4495 get_constraint_for (res, &lhsc);
4496 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4497 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
4498 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4499 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
4500 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK
4501 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK)
4502 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4503 else
4504 get_constraint_for (dest, &rhsc);
4505 process_all_all_constraints (lhsc, rhsc);
4506 lhsc.truncate (0);
4507 rhsc.truncate (0);
4509 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4510 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4511 do_deref (&lhsc);
4512 do_deref (&rhsc);
4513 process_all_all_constraints (lhsc, rhsc);
4514 return true;
4516 case BUILT_IN_MEMSET:
4517 case BUILT_IN_MEMSET_CHK:
4518 case BUILT_IN_TM_MEMSET:
4520 tree res = gimple_call_lhs (t);
4521 tree dest = gimple_call_arg (t, 0);
4522 unsigned i;
4523 ce_s *lhsp;
4524 struct constraint_expr ac;
4525 if (res != NULL_TREE)
4527 get_constraint_for (res, &lhsc);
4528 get_constraint_for (dest, &rhsc);
4529 process_all_all_constraints (lhsc, rhsc);
4530 lhsc.truncate (0);
4532 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4533 do_deref (&lhsc);
4534 if (flag_delete_null_pointer_checks
4535 && integer_zerop (gimple_call_arg (t, 1)))
4537 ac.type = ADDRESSOF;
4538 ac.var = nothing_id;
4540 else
4542 ac.type = SCALAR;
4543 ac.var = integer_id;
4545 ac.offset = 0;
4546 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4547 process_constraint (new_constraint (*lhsp, ac));
4548 return true;
4550 case BUILT_IN_STACK_SAVE:
4551 case BUILT_IN_STACK_RESTORE:
4552 /* Nothing interesting happens. */
4553 return true;
4554 case BUILT_IN_ALLOCA:
4555 case BUILT_IN_ALLOCA_WITH_ALIGN:
4556 case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
4558 tree ptr = gimple_call_lhs (t);
4559 if (ptr == NULL_TREE)
4560 return true;
4561 get_constraint_for (ptr, &lhsc);
4562 varinfo_t vi = make_heapvar ("HEAP", true);
4563 /* Alloca storage is never global. To exempt it from escaped
4564 handling make it a non-heap var. */
4565 DECL_EXTERNAL (vi->decl) = 0;
4566 vi->is_global_var = 0;
4567 vi->is_heap_var = 0;
4568 struct constraint_expr tmpc;
4569 tmpc.var = vi->id;
4570 tmpc.offset = 0;
4571 tmpc.type = ADDRESSOF;
4572 rhsc.safe_push (tmpc);
4573 process_all_all_constraints (lhsc, rhsc);
4574 return true;
4576 case BUILT_IN_POSIX_MEMALIGN:
4578 tree ptrptr = gimple_call_arg (t, 0);
4579 get_constraint_for (ptrptr, &lhsc);
4580 do_deref (&lhsc);
4581 varinfo_t vi = make_heapvar ("HEAP", true);
4582 /* We are marking allocated storage local, we deal with it becoming
4583 global by escaping and setting of vars_contains_escaped_heap. */
4584 DECL_EXTERNAL (vi->decl) = 0;
4585 vi->is_global_var = 0;
4586 struct constraint_expr tmpc;
4587 tmpc.var = vi->id;
4588 tmpc.offset = 0;
4589 tmpc.type = ADDRESSOF;
4590 rhsc.safe_push (tmpc);
4591 process_all_all_constraints (lhsc, rhsc);
4592 return true;
4594 case BUILT_IN_ASSUME_ALIGNED:
4596 tree res = gimple_call_lhs (t);
4597 tree dest = gimple_call_arg (t, 0);
4598 if (res != NULL_TREE)
4600 get_constraint_for (res, &lhsc);
4601 get_constraint_for (dest, &rhsc);
4602 process_all_all_constraints (lhsc, rhsc);
4604 return true;
4606 /* All the following functions do not return pointers, do not
4607 modify the points-to sets of memory reachable from their
4608 arguments and do not add to the ESCAPED solution. */
4609 case BUILT_IN_SINCOS:
4610 case BUILT_IN_SINCOSF:
4611 case BUILT_IN_SINCOSL:
4612 case BUILT_IN_FREXP:
4613 case BUILT_IN_FREXPF:
4614 case BUILT_IN_FREXPL:
4615 case BUILT_IN_GAMMA_R:
4616 case BUILT_IN_GAMMAF_R:
4617 case BUILT_IN_GAMMAL_R:
4618 case BUILT_IN_LGAMMA_R:
4619 case BUILT_IN_LGAMMAF_R:
4620 case BUILT_IN_LGAMMAL_R:
4621 case BUILT_IN_MODF:
4622 case BUILT_IN_MODFF:
4623 case BUILT_IN_MODFL:
4624 case BUILT_IN_REMQUO:
4625 case BUILT_IN_REMQUOF:
4626 case BUILT_IN_REMQUOL:
4627 case BUILT_IN_FREE:
4628 return true;
4629 case BUILT_IN_STRDUP:
4630 case BUILT_IN_STRNDUP:
4631 case BUILT_IN_REALLOC:
4632 if (gimple_call_lhs (t))
4634 auto_vec<ce_s> rhsc;
4635 handle_lhs_call (t, gimple_call_lhs (t),
4636 gimple_call_return_flags (t) | ERF_NOALIAS,
4637 rhsc, fndecl);
4638 get_constraint_for_ptr_offset (gimple_call_lhs (t),
4639 NULL_TREE, &lhsc);
4640 get_constraint_for_ptr_offset (gimple_call_arg (t, 0),
4641 NULL_TREE, &rhsc);
4642 do_deref (&lhsc);
4643 do_deref (&rhsc);
4644 process_all_all_constraints (lhsc, rhsc);
4645 lhsc.truncate (0);
4646 rhsc.truncate (0);
4647 /* For realloc the resulting pointer can be equal to the
4648 argument as well. But only doing this wouldn't be
4649 correct because with ptr == 0 realloc behaves like malloc. */
4650 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_REALLOC)
4652 get_constraint_for (gimple_call_lhs (t), &lhsc);
4653 get_constraint_for (gimple_call_arg (t, 0), &rhsc);
4654 process_all_all_constraints (lhsc, rhsc);
4656 return true;
4658 break;
4659 /* String / character search functions return a pointer into the
4660 source string or NULL. */
4661 case BUILT_IN_INDEX:
4662 case BUILT_IN_STRCHR:
4663 case BUILT_IN_STRRCHR:
4664 case BUILT_IN_MEMCHR:
4665 case BUILT_IN_STRSTR:
4666 case BUILT_IN_STRPBRK:
4667 if (gimple_call_lhs (t))
4669 tree src = gimple_call_arg (t, 0);
4670 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4671 constraint_expr nul;
4672 nul.var = nothing_id;
4673 nul.offset = 0;
4674 nul.type = ADDRESSOF;
4675 rhsc.safe_push (nul);
4676 get_constraint_for (gimple_call_lhs (t), &lhsc);
4677 process_all_all_constraints (lhsc, rhsc);
4679 return true;
4680 /* Pure functions that return something not based on any object and
4681 that use the memory pointed to by their arguments (but not
4682 transitively). */
4683 case BUILT_IN_STRCMP:
4684 case BUILT_IN_STRCMP_EQ:
4685 case BUILT_IN_STRNCMP:
4686 case BUILT_IN_STRNCMP_EQ:
4687 case BUILT_IN_STRCASECMP:
4688 case BUILT_IN_STRNCASECMP:
4689 case BUILT_IN_MEMCMP:
4690 case BUILT_IN_BCMP:
4691 case BUILT_IN_STRSPN:
4692 case BUILT_IN_STRCSPN:
4694 varinfo_t uses = get_call_use_vi (t);
4695 make_any_offset_constraints (uses);
4696 make_constraint_to (uses->id, gimple_call_arg (t, 0));
4697 make_constraint_to (uses->id, gimple_call_arg (t, 1));
4698 /* No constraints are necessary for the return value. */
4699 return true;
4701 case BUILT_IN_STRLEN:
4703 varinfo_t uses = get_call_use_vi (t);
4704 make_any_offset_constraints (uses);
4705 make_constraint_to (uses->id, gimple_call_arg (t, 0));
4706 /* No constraints are necessary for the return value. */
4707 return true;
4709 case BUILT_IN_OBJECT_SIZE:
4710 case BUILT_IN_CONSTANT_P:
4712 /* No constraints are necessary for the return value or the
4713 arguments. */
4714 return true;
4716 /* Trampolines are special - they set up passing the static
4717 frame. */
4718 case BUILT_IN_INIT_TRAMPOLINE:
4720 tree tramp = gimple_call_arg (t, 0);
4721 tree nfunc = gimple_call_arg (t, 1);
4722 tree frame = gimple_call_arg (t, 2);
4723 unsigned i;
4724 struct constraint_expr lhs, *rhsp;
4725 if (in_ipa_mode)
4727 varinfo_t nfi = NULL;
4728 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR);
4729 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0));
4730 if (nfi)
4732 lhs = get_function_part_constraint (nfi, fi_static_chain);
4733 get_constraint_for (frame, &rhsc);
4734 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4735 process_constraint (new_constraint (lhs, *rhsp));
4736 rhsc.truncate (0);
4738 /* Make the frame point to the function for
4739 the trampoline adjustment call. */
4740 get_constraint_for (tramp, &lhsc);
4741 do_deref (&lhsc);
4742 get_constraint_for (nfunc, &rhsc);
4743 process_all_all_constraints (lhsc, rhsc);
4745 return true;
4748 /* Else fallthru to generic handling which will let
4749 the frame escape. */
4750 break;
4752 case BUILT_IN_ADJUST_TRAMPOLINE:
4754 tree tramp = gimple_call_arg (t, 0);
4755 tree res = gimple_call_lhs (t);
4756 if (in_ipa_mode && res)
4758 get_constraint_for (res, &lhsc);
4759 get_constraint_for (tramp, &rhsc);
4760 do_deref (&rhsc);
4761 process_all_all_constraints (lhsc, rhsc);
4763 return true;
4765 CASE_BUILT_IN_TM_STORE (1):
4766 CASE_BUILT_IN_TM_STORE (2):
4767 CASE_BUILT_IN_TM_STORE (4):
4768 CASE_BUILT_IN_TM_STORE (8):
4769 CASE_BUILT_IN_TM_STORE (FLOAT):
4770 CASE_BUILT_IN_TM_STORE (DOUBLE):
4771 CASE_BUILT_IN_TM_STORE (LDOUBLE):
4772 CASE_BUILT_IN_TM_STORE (M64):
4773 CASE_BUILT_IN_TM_STORE (M128):
4774 CASE_BUILT_IN_TM_STORE (M256):
4776 tree addr = gimple_call_arg (t, 0);
4777 tree src = gimple_call_arg (t, 1);
4779 get_constraint_for (addr, &lhsc);
4780 do_deref (&lhsc);
4781 get_constraint_for (src, &rhsc);
4782 process_all_all_constraints (lhsc, rhsc);
4783 return true;
4785 CASE_BUILT_IN_TM_LOAD (1):
4786 CASE_BUILT_IN_TM_LOAD (2):
4787 CASE_BUILT_IN_TM_LOAD (4):
4788 CASE_BUILT_IN_TM_LOAD (8):
4789 CASE_BUILT_IN_TM_LOAD (FLOAT):
4790 CASE_BUILT_IN_TM_LOAD (DOUBLE):
4791 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
4792 CASE_BUILT_IN_TM_LOAD (M64):
4793 CASE_BUILT_IN_TM_LOAD (M128):
4794 CASE_BUILT_IN_TM_LOAD (M256):
4796 tree dest = gimple_call_lhs (t);
4797 tree addr = gimple_call_arg (t, 0);
4799 get_constraint_for (dest, &lhsc);
4800 get_constraint_for (addr, &rhsc);
4801 do_deref (&rhsc);
4802 process_all_all_constraints (lhsc, rhsc);
4803 return true;
4805 /* Variadic argument handling needs to be handled in IPA
4806 mode as well. */
4807 case BUILT_IN_VA_START:
4809 tree valist = gimple_call_arg (t, 0);
4810 struct constraint_expr rhs, *lhsp;
4811 unsigned i;
4812 get_constraint_for_ptr_offset (valist, NULL_TREE, &lhsc);
4813 do_deref (&lhsc);
4814 /* The va_list gets access to pointers in variadic
4815 arguments. Which we know in the case of IPA analysis
4816 and otherwise are just all nonlocal variables. */
4817 if (in_ipa_mode)
4819 fi = lookup_vi_for_tree (fn->decl);
4820 rhs = get_function_part_constraint (fi, ~0);
4821 rhs.type = ADDRESSOF;
4823 else
4825 rhs.var = nonlocal_id;
4826 rhs.type = ADDRESSOF;
4827 rhs.offset = 0;
4829 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4830 process_constraint (new_constraint (*lhsp, rhs));
4831 /* va_list is clobbered. */
4832 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4833 return true;
4835 /* va_end doesn't have any effect that matters. */
4836 case BUILT_IN_VA_END:
4837 return true;
4838 /* Alternate return. Simply give up for now. */
4839 case BUILT_IN_RETURN:
4841 fi = NULL;
4842 if (!in_ipa_mode
4843 || !(fi = get_vi_for_tree (fn->decl)))
4844 make_constraint_from (get_varinfo (escaped_id), anything_id);
4845 else if (in_ipa_mode
4846 && fi != NULL)
4848 struct constraint_expr lhs, rhs;
4849 lhs = get_function_part_constraint (fi, fi_result);
4850 rhs.var = anything_id;
4851 rhs.offset = 0;
4852 rhs.type = SCALAR;
4853 process_constraint (new_constraint (lhs, rhs));
4855 return true;
4857 case BUILT_IN_GOMP_PARALLEL:
4858 case BUILT_IN_GOACC_PARALLEL:
4860 if (in_ipa_mode)
4862 unsigned int fnpos, argpos;
4863 switch (DECL_FUNCTION_CODE (fndecl))
4865 case BUILT_IN_GOMP_PARALLEL:
4866 /* __builtin_GOMP_parallel (fn, data, num_threads, flags). */
4867 fnpos = 0;
4868 argpos = 1;
4869 break;
4870 case BUILT_IN_GOACC_PARALLEL:
4871 /* __builtin_GOACC_parallel (flags_m, fn, mapnum, hostaddrs,
4872 sizes, kinds, ...). */
4873 fnpos = 1;
4874 argpos = 3;
4875 break;
4876 default:
4877 gcc_unreachable ();
4880 tree fnarg = gimple_call_arg (t, fnpos);
4881 gcc_assert (TREE_CODE (fnarg) == ADDR_EXPR);
4882 tree fndecl = TREE_OPERAND (fnarg, 0);
4883 if (fndecl_maybe_in_other_partition (fndecl))
4884 /* Fallthru to general call handling. */
4885 break;
4887 tree arg = gimple_call_arg (t, argpos);
4889 varinfo_t fi = get_vi_for_tree (fndecl);
4890 find_func_aliases_for_call_arg (fi, 0, arg);
4891 return true;
4893 /* Else fallthru to generic call handling. */
4894 break;
4896 /* printf-style functions may have hooks to set pointers to
4897 point to somewhere into the generated string. Leave them
4898 for a later exercise... */
4899 default:
4900 /* Fallthru to general call handling. */;
4903 return false;
4906 /* Create constraints for the call T. */
4908 static void
4909 find_func_aliases_for_call (struct function *fn, gcall *t)
4911 tree fndecl = gimple_call_fndecl (t);
4912 varinfo_t fi;
4914 if (fndecl != NULL_TREE
4915 && fndecl_built_in_p (fndecl)
4916 && find_func_aliases_for_builtin_call (fn, t))
4917 return;
4919 fi = get_fi_for_callee (t);
4920 if (!in_ipa_mode
4921 || (fi->decl && fndecl && !fi->is_fn_info))
4923 auto_vec<ce_s, 16> rhsc;
4924 int flags = gimple_call_flags (t);
4926 /* Const functions can return their arguments and addresses
4927 of global memory but not of escaped memory. */
4928 if (flags & (ECF_CONST|ECF_NOVOPS))
4930 if (gimple_call_lhs (t))
4931 handle_const_call (t, &rhsc);
4933 /* Pure functions can return addresses in and of memory
4934 reachable from their arguments, but they are not an escape
4935 point for reachable memory of their arguments. */
4936 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4937 handle_pure_call (t, &rhsc);
4938 /* If the call is to a replaceable operator delete and results
4939 from a delete expression as opposed to a direct call to
4940 such operator, then the effects for PTA (in particular
4941 the escaping of the pointer) can be ignored. */
4942 else if (fndecl
4943 && DECL_IS_OPERATOR_DELETE_P (fndecl)
4944 && gimple_call_from_new_or_delete (t))
4946 else
4947 handle_rhs_call (t, &rhsc);
4948 if (gimple_call_lhs (t))
4949 handle_lhs_call (t, gimple_call_lhs (t),
4950 gimple_call_return_flags (t), rhsc, fndecl);
4952 else
4954 auto_vec<ce_s, 2> rhsc;
4955 tree lhsop;
4956 unsigned j;
4958 /* Assign all the passed arguments to the appropriate incoming
4959 parameters of the function. */
4960 for (j = 0; j < gimple_call_num_args (t); j++)
4962 tree arg = gimple_call_arg (t, j);
4963 find_func_aliases_for_call_arg (fi, j, arg);
4966 /* If we are returning a value, assign it to the result. */
4967 lhsop = gimple_call_lhs (t);
4968 if (lhsop)
4970 auto_vec<ce_s, 2> lhsc;
4971 struct constraint_expr rhs;
4972 struct constraint_expr *lhsp;
4973 bool aggr_p = aggregate_value_p (lhsop, gimple_call_fntype (t));
4975 get_constraint_for (lhsop, &lhsc);
4976 rhs = get_function_part_constraint (fi, fi_result);
4977 if (aggr_p)
4979 auto_vec<ce_s, 2> tem;
4980 tem.quick_push (rhs);
4981 do_deref (&tem);
4982 gcc_checking_assert (tem.length () == 1);
4983 rhs = tem[0];
4985 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4986 process_constraint (new_constraint (*lhsp, rhs));
4988 /* If we pass the result decl by reference, honor that. */
4989 if (aggr_p)
4991 struct constraint_expr lhs;
4992 struct constraint_expr *rhsp;
4994 get_constraint_for_address_of (lhsop, &rhsc);
4995 lhs = get_function_part_constraint (fi, fi_result);
4996 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4997 process_constraint (new_constraint (lhs, *rhsp));
4998 rhsc.truncate (0);
5002 /* If we use a static chain, pass it along. */
5003 if (gimple_call_chain (t))
5005 struct constraint_expr lhs;
5006 struct constraint_expr *rhsp;
5008 get_constraint_for (gimple_call_chain (t), &rhsc);
5009 lhs = get_function_part_constraint (fi, fi_static_chain);
5010 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5011 process_constraint (new_constraint (lhs, *rhsp));
5016 /* Walk statement T setting up aliasing constraints according to the
5017 references found in T. This function is the main part of the
5018 constraint builder. AI points to auxiliary alias information used
5019 when building alias sets and computing alias grouping heuristics. */
5021 static void
5022 find_func_aliases (struct function *fn, gimple *origt)
5024 gimple *t = origt;
5025 auto_vec<ce_s, 16> lhsc;
5026 auto_vec<ce_s, 16> rhsc;
5027 varinfo_t fi;
5029 /* Now build constraints expressions. */
5030 if (gimple_code (t) == GIMPLE_PHI)
5032 /* For a phi node, assign all the arguments to
5033 the result. */
5034 get_constraint_for (gimple_phi_result (t), &lhsc);
5035 for (unsigned i = 0; i < gimple_phi_num_args (t); i++)
5037 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
5038 process_all_all_constraints (lhsc, rhsc);
5039 rhsc.truncate (0);
5042 /* In IPA mode, we need to generate constraints to pass call
5043 arguments through their calls. There are two cases,
5044 either a GIMPLE_CALL returning a value, or just a plain
5045 GIMPLE_CALL when we are not.
5047 In non-ipa mode, we need to generate constraints for each
5048 pointer passed by address. */
5049 else if (is_gimple_call (t))
5050 find_func_aliases_for_call (fn, as_a <gcall *> (t));
5052 /* Otherwise, just a regular assignment statement. Only care about
5053 operations with pointer result, others are dealt with as escape
5054 points if they have pointer operands. */
5055 else if (is_gimple_assign (t))
5057 /* Otherwise, just a regular assignment statement. */
5058 tree lhsop = gimple_assign_lhs (t);
5059 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
5061 if (rhsop && TREE_CLOBBER_P (rhsop))
5062 /* Ignore clobbers, they don't actually store anything into
5063 the LHS. */
5065 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
5066 do_structure_copy (lhsop, rhsop);
5067 else
5069 enum tree_code code = gimple_assign_rhs_code (t);
5071 get_constraint_for (lhsop, &lhsc);
5073 if (code == POINTER_PLUS_EXPR)
5074 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
5075 gimple_assign_rhs2 (t), &rhsc);
5076 else if (code == POINTER_DIFF_EXPR)
5077 /* The result is not a pointer (part). */
5079 else if (code == BIT_AND_EXPR
5080 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
5082 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
5083 the pointer. Handle it by offsetting it by UNKNOWN. */
5084 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
5085 NULL_TREE, &rhsc);
5087 else if (code == TRUNC_DIV_EXPR
5088 || code == CEIL_DIV_EXPR
5089 || code == FLOOR_DIV_EXPR
5090 || code == ROUND_DIV_EXPR
5091 || code == EXACT_DIV_EXPR
5092 || code == TRUNC_MOD_EXPR
5093 || code == CEIL_MOD_EXPR
5094 || code == FLOOR_MOD_EXPR
5095 || code == ROUND_MOD_EXPR)
5096 /* Division and modulo transfer the pointer from the LHS. */
5097 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
5098 NULL_TREE, &rhsc);
5099 else if (CONVERT_EXPR_CODE_P (code)
5100 || gimple_assign_single_p (t))
5101 /* See through conversions, single RHS are handled by
5102 get_constraint_for_rhs. */
5103 get_constraint_for_rhs (rhsop, &rhsc);
5104 else if (code == COND_EXPR)
5106 /* The result is a merge of both COND_EXPR arms. */
5107 auto_vec<ce_s, 2> tmp;
5108 struct constraint_expr *rhsp;
5109 unsigned i;
5110 get_constraint_for_rhs (gimple_assign_rhs2 (t), &rhsc);
5111 get_constraint_for_rhs (gimple_assign_rhs3 (t), &tmp);
5112 FOR_EACH_VEC_ELT (tmp, i, rhsp)
5113 rhsc.safe_push (*rhsp);
5115 else if (truth_value_p (code))
5116 /* Truth value results are not pointer (parts). Or at least
5117 very unreasonable obfuscation of a part. */
5119 else
5121 /* All other operations are possibly offsetting merges. */
5122 auto_vec<ce_s, 4> tmp;
5123 struct constraint_expr *rhsp;
5124 unsigned i, j;
5125 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
5126 NULL_TREE, &rhsc);
5127 for (i = 2; i < gimple_num_ops (t); ++i)
5129 get_constraint_for_ptr_offset (gimple_op (t, i),
5130 NULL_TREE, &tmp);
5131 FOR_EACH_VEC_ELT (tmp, j, rhsp)
5132 rhsc.safe_push (*rhsp);
5133 tmp.truncate (0);
5136 process_all_all_constraints (lhsc, rhsc);
5138 /* If there is a store to a global variable the rhs escapes. */
5139 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
5140 && DECL_P (lhsop))
5142 varinfo_t vi = get_vi_for_tree (lhsop);
5143 if ((! in_ipa_mode && vi->is_global_var)
5144 || vi->is_ipa_escape_point)
5145 make_escape_constraint (rhsop);
5148 /* Handle escapes through return. */
5149 else if (gimple_code (t) == GIMPLE_RETURN
5150 && gimple_return_retval (as_a <greturn *> (t)) != NULL_TREE)
5152 greturn *return_stmt = as_a <greturn *> (t);
5153 fi = NULL;
5154 if (!in_ipa_mode
5155 && SSA_VAR_P (gimple_return_retval (return_stmt)))
5157 /* We handle simple returns by post-processing the solutions. */
5160 if (!(fi = get_vi_for_tree (fn->decl)))
5161 make_escape_constraint (gimple_return_retval (return_stmt));
5162 else if (in_ipa_mode)
5164 struct constraint_expr lhs ;
5165 struct constraint_expr *rhsp;
5166 unsigned i;
5168 lhs = get_function_part_constraint (fi, fi_result);
5169 get_constraint_for_rhs (gimple_return_retval (return_stmt), &rhsc);
5170 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5171 process_constraint (new_constraint (lhs, *rhsp));
5174 /* Handle asms conservatively by adding escape constraints to everything. */
5175 else if (gasm *asm_stmt = dyn_cast <gasm *> (t))
5177 unsigned i, noutputs;
5178 const char **oconstraints;
5179 const char *constraint;
5180 bool allows_mem, allows_reg, is_inout;
5182 noutputs = gimple_asm_noutputs (asm_stmt);
5183 oconstraints = XALLOCAVEC (const char *, noutputs);
5185 for (i = 0; i < noutputs; ++i)
5187 tree link = gimple_asm_output_op (asm_stmt, i);
5188 tree op = TREE_VALUE (link);
5190 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5191 oconstraints[i] = constraint;
5192 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
5193 &allows_reg, &is_inout);
5195 /* A memory constraint makes the address of the operand escape. */
5196 if (!allows_reg && allows_mem)
5197 make_escape_constraint (build_fold_addr_expr (op));
5199 /* The asm may read global memory, so outputs may point to
5200 any global memory. */
5201 if (op)
5203 auto_vec<ce_s, 2> lhsc;
5204 struct constraint_expr rhsc, *lhsp;
5205 unsigned j;
5206 get_constraint_for (op, &lhsc);
5207 rhsc.var = nonlocal_id;
5208 rhsc.offset = 0;
5209 rhsc.type = SCALAR;
5210 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
5211 process_constraint (new_constraint (*lhsp, rhsc));
5214 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
5216 tree link = gimple_asm_input_op (asm_stmt, i);
5217 tree op = TREE_VALUE (link);
5219 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5221 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
5222 &allows_mem, &allows_reg);
5224 /* A memory constraint makes the address of the operand escape. */
5225 if (!allows_reg && allows_mem)
5226 make_escape_constraint (build_fold_addr_expr (op));
5227 /* Strictly we'd only need the constraint to ESCAPED if
5228 the asm clobbers memory, otherwise using something
5229 along the lines of per-call clobbers/uses would be enough. */
5230 else if (op)
5231 make_escape_constraint (op);
5237 /* Create a constraint adding to the clobber set of FI the memory
5238 pointed to by PTR. */
5240 static void
5241 process_ipa_clobber (varinfo_t fi, tree ptr)
5243 vec<ce_s> ptrc = vNULL;
5244 struct constraint_expr *c, lhs;
5245 unsigned i;
5246 get_constraint_for_rhs (ptr, &ptrc);
5247 lhs = get_function_part_constraint (fi, fi_clobbers);
5248 FOR_EACH_VEC_ELT (ptrc, i, c)
5249 process_constraint (new_constraint (lhs, *c));
5250 ptrc.release ();
5253 /* Walk statement T setting up clobber and use constraints according to the
5254 references found in T. This function is a main part of the
5255 IPA constraint builder. */
5257 static void
5258 find_func_clobbers (struct function *fn, gimple *origt)
5260 gimple *t = origt;
5261 auto_vec<ce_s, 16> lhsc;
5262 auto_vec<ce_s, 16> rhsc;
5263 varinfo_t fi;
5265 /* Add constraints for clobbered/used in IPA mode.
5266 We are not interested in what automatic variables are clobbered
5267 or used as we only use the information in the caller to which
5268 they do not escape. */
5269 gcc_assert (in_ipa_mode);
5271 /* If the stmt refers to memory in any way it better had a VUSE. */
5272 if (gimple_vuse (t) == NULL_TREE)
5273 return;
5275 /* We'd better have function information for the current function. */
5276 fi = lookup_vi_for_tree (fn->decl);
5277 gcc_assert (fi != NULL);
5279 /* Account for stores in assignments and calls. */
5280 if (gimple_vdef (t) != NULL_TREE
5281 && gimple_has_lhs (t))
5283 tree lhs = gimple_get_lhs (t);
5284 tree tem = lhs;
5285 while (handled_component_p (tem))
5286 tem = TREE_OPERAND (tem, 0);
5287 if ((DECL_P (tem)
5288 && !auto_var_in_fn_p (tem, fn->decl))
5289 || INDIRECT_REF_P (tem)
5290 || (TREE_CODE (tem) == MEM_REF
5291 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
5292 && auto_var_in_fn_p
5293 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl))))
5295 struct constraint_expr lhsc, *rhsp;
5296 unsigned i;
5297 lhsc = get_function_part_constraint (fi, fi_clobbers);
5298 get_constraint_for_address_of (lhs, &rhsc);
5299 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5300 process_constraint (new_constraint (lhsc, *rhsp));
5301 rhsc.truncate (0);
5305 /* Account for uses in assigments and returns. */
5306 if (gimple_assign_single_p (t)
5307 || (gimple_code (t) == GIMPLE_RETURN
5308 && gimple_return_retval (as_a <greturn *> (t)) != NULL_TREE))
5310 tree rhs = (gimple_assign_single_p (t)
5311 ? gimple_assign_rhs1 (t)
5312 : gimple_return_retval (as_a <greturn *> (t)));
5313 tree tem = rhs;
5314 while (handled_component_p (tem))
5315 tem = TREE_OPERAND (tem, 0);
5316 if ((DECL_P (tem)
5317 && !auto_var_in_fn_p (tem, fn->decl))
5318 || INDIRECT_REF_P (tem)
5319 || (TREE_CODE (tem) == MEM_REF
5320 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
5321 && auto_var_in_fn_p
5322 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl))))
5324 struct constraint_expr lhs, *rhsp;
5325 unsigned i;
5326 lhs = get_function_part_constraint (fi, fi_uses);
5327 get_constraint_for_address_of (rhs, &rhsc);
5328 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5329 process_constraint (new_constraint (lhs, *rhsp));
5330 rhsc.truncate (0);
5334 if (gcall *call_stmt = dyn_cast <gcall *> (t))
5336 varinfo_t cfi = NULL;
5337 tree decl = gimple_call_fndecl (t);
5338 struct constraint_expr lhs, rhs;
5339 unsigned i, j;
5341 /* For builtins we do not have separate function info. For those
5342 we do not generate escapes for we have to generate clobbers/uses. */
5343 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
5344 switch (DECL_FUNCTION_CODE (decl))
5346 /* The following functions use and clobber memory pointed to
5347 by their arguments. */
5348 case BUILT_IN_STRCPY:
5349 case BUILT_IN_STRNCPY:
5350 case BUILT_IN_BCOPY:
5351 case BUILT_IN_MEMCPY:
5352 case BUILT_IN_MEMMOVE:
5353 case BUILT_IN_MEMPCPY:
5354 case BUILT_IN_STPCPY:
5355 case BUILT_IN_STPNCPY:
5356 case BUILT_IN_STRCAT:
5357 case BUILT_IN_STRNCAT:
5358 case BUILT_IN_STRCPY_CHK:
5359 case BUILT_IN_STRNCPY_CHK:
5360 case BUILT_IN_MEMCPY_CHK:
5361 case BUILT_IN_MEMMOVE_CHK:
5362 case BUILT_IN_MEMPCPY_CHK:
5363 case BUILT_IN_STPCPY_CHK:
5364 case BUILT_IN_STPNCPY_CHK:
5365 case BUILT_IN_STRCAT_CHK:
5366 case BUILT_IN_STRNCAT_CHK:
5368 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
5369 == BUILT_IN_BCOPY ? 1 : 0));
5370 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
5371 == BUILT_IN_BCOPY ? 0 : 1));
5372 unsigned i;
5373 struct constraint_expr *rhsp, *lhsp;
5374 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
5375 lhs = get_function_part_constraint (fi, fi_clobbers);
5376 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
5377 process_constraint (new_constraint (lhs, *lhsp));
5378 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
5379 lhs = get_function_part_constraint (fi, fi_uses);
5380 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5381 process_constraint (new_constraint (lhs, *rhsp));
5382 return;
5384 /* The following function clobbers memory pointed to by
5385 its argument. */
5386 case BUILT_IN_MEMSET:
5387 case BUILT_IN_MEMSET_CHK:
5388 case BUILT_IN_POSIX_MEMALIGN:
5390 tree dest = gimple_call_arg (t, 0);
5391 unsigned i;
5392 ce_s *lhsp;
5393 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
5394 lhs = get_function_part_constraint (fi, fi_clobbers);
5395 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
5396 process_constraint (new_constraint (lhs, *lhsp));
5397 return;
5399 /* The following functions clobber their second and third
5400 arguments. */
5401 case BUILT_IN_SINCOS:
5402 case BUILT_IN_SINCOSF:
5403 case BUILT_IN_SINCOSL:
5405 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5406 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5407 return;
5409 /* The following functions clobber their second argument. */
5410 case BUILT_IN_FREXP:
5411 case BUILT_IN_FREXPF:
5412 case BUILT_IN_FREXPL:
5413 case BUILT_IN_LGAMMA_R:
5414 case BUILT_IN_LGAMMAF_R:
5415 case BUILT_IN_LGAMMAL_R:
5416 case BUILT_IN_GAMMA_R:
5417 case BUILT_IN_GAMMAF_R:
5418 case BUILT_IN_GAMMAL_R:
5419 case BUILT_IN_MODF:
5420 case BUILT_IN_MODFF:
5421 case BUILT_IN_MODFL:
5423 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5424 return;
5426 /* The following functions clobber their third argument. */
5427 case BUILT_IN_REMQUO:
5428 case BUILT_IN_REMQUOF:
5429 case BUILT_IN_REMQUOL:
5431 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5432 return;
5434 /* The following functions neither read nor clobber memory. */
5435 case BUILT_IN_ASSUME_ALIGNED:
5436 case BUILT_IN_FREE:
5437 return;
5438 /* Trampolines are of no interest to us. */
5439 case BUILT_IN_INIT_TRAMPOLINE:
5440 case BUILT_IN_ADJUST_TRAMPOLINE:
5441 return;
5442 case BUILT_IN_VA_START:
5443 case BUILT_IN_VA_END:
5444 return;
5445 case BUILT_IN_GOMP_PARALLEL:
5446 case BUILT_IN_GOACC_PARALLEL:
5448 unsigned int fnpos, argpos;
5449 unsigned int implicit_use_args[2];
5450 unsigned int num_implicit_use_args = 0;
5451 switch (DECL_FUNCTION_CODE (decl))
5453 case BUILT_IN_GOMP_PARALLEL:
5454 /* __builtin_GOMP_parallel (fn, data, num_threads, flags). */
5455 fnpos = 0;
5456 argpos = 1;
5457 break;
5458 case BUILT_IN_GOACC_PARALLEL:
5459 /* __builtin_GOACC_parallel (flags_m, fn, mapnum, hostaddrs,
5460 sizes, kinds, ...). */
5461 fnpos = 1;
5462 argpos = 3;
5463 implicit_use_args[num_implicit_use_args++] = 4;
5464 implicit_use_args[num_implicit_use_args++] = 5;
5465 break;
5466 default:
5467 gcc_unreachable ();
5470 tree fnarg = gimple_call_arg (t, fnpos);
5471 gcc_assert (TREE_CODE (fnarg) == ADDR_EXPR);
5472 tree fndecl = TREE_OPERAND (fnarg, 0);
5473 if (fndecl_maybe_in_other_partition (fndecl))
5474 /* Fallthru to general call handling. */
5475 break;
5477 varinfo_t cfi = get_vi_for_tree (fndecl);
5479 tree arg = gimple_call_arg (t, argpos);
5481 /* Parameter passed by value is used. */
5482 lhs = get_function_part_constraint (fi, fi_uses);
5483 struct constraint_expr *rhsp;
5484 get_constraint_for (arg, &rhsc);
5485 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5486 process_constraint (new_constraint (lhs, *rhsp));
5487 rhsc.truncate (0);
5489 /* Handle parameters used by the call, but not used in cfi, as
5490 implicitly used by cfi. */
5491 lhs = get_function_part_constraint (cfi, fi_uses);
5492 for (unsigned i = 0; i < num_implicit_use_args; ++i)
5494 tree arg = gimple_call_arg (t, implicit_use_args[i]);
5495 get_constraint_for (arg, &rhsc);
5496 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5497 process_constraint (new_constraint (lhs, *rhsp));
5498 rhsc.truncate (0);
5501 /* The caller clobbers what the callee does. */
5502 lhs = get_function_part_constraint (fi, fi_clobbers);
5503 rhs = get_function_part_constraint (cfi, fi_clobbers);
5504 process_constraint (new_constraint (lhs, rhs));
5506 /* The caller uses what the callee does. */
5507 lhs = get_function_part_constraint (fi, fi_uses);
5508 rhs = get_function_part_constraint (cfi, fi_uses);
5509 process_constraint (new_constraint (lhs, rhs));
5511 return;
5513 /* printf-style functions may have hooks to set pointers to
5514 point to somewhere into the generated string. Leave them
5515 for a later exercise... */
5516 default:
5517 /* Fallthru to general call handling. */;
5520 /* Parameters passed by value are used. */
5521 lhs = get_function_part_constraint (fi, fi_uses);
5522 for (i = 0; i < gimple_call_num_args (t); i++)
5524 struct constraint_expr *rhsp;
5525 tree arg = gimple_call_arg (t, i);
5527 if (TREE_CODE (arg) == SSA_NAME
5528 || is_gimple_min_invariant (arg))
5529 continue;
5531 get_constraint_for_address_of (arg, &rhsc);
5532 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5533 process_constraint (new_constraint (lhs, *rhsp));
5534 rhsc.truncate (0);
5537 /* Build constraints for propagating clobbers/uses along the
5538 callgraph edges. */
5539 cfi = get_fi_for_callee (call_stmt);
5540 if (cfi->id == anything_id)
5542 if (gimple_vdef (t))
5543 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5544 anything_id);
5545 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5546 anything_id);
5547 return;
5550 /* For callees without function info (that's external functions),
5551 ESCAPED is clobbered and used. */
5552 if (cfi->decl
5553 && TREE_CODE (cfi->decl) == FUNCTION_DECL
5554 && !cfi->is_fn_info)
5556 varinfo_t vi;
5558 if (gimple_vdef (t))
5559 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5560 escaped_id);
5561 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
5563 /* Also honor the call statement use/clobber info. */
5564 if ((vi = lookup_call_clobber_vi (call_stmt)) != NULL)
5565 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5566 vi->id);
5567 if ((vi = lookup_call_use_vi (call_stmt)) != NULL)
5568 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
5569 vi->id);
5570 return;
5573 /* Otherwise the caller clobbers and uses what the callee does.
5574 ??? This should use a new complex constraint that filters
5575 local variables of the callee. */
5576 if (gimple_vdef (t))
5578 lhs = get_function_part_constraint (fi, fi_clobbers);
5579 rhs = get_function_part_constraint (cfi, fi_clobbers);
5580 process_constraint (new_constraint (lhs, rhs));
5582 lhs = get_function_part_constraint (fi, fi_uses);
5583 rhs = get_function_part_constraint (cfi, fi_uses);
5584 process_constraint (new_constraint (lhs, rhs));
5586 else if (gimple_code (t) == GIMPLE_ASM)
5588 /* ??? Ick. We can do better. */
5589 if (gimple_vdef (t))
5590 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5591 anything_id);
5592 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5593 anything_id);
5598 /* Find the first varinfo in the same variable as START that overlaps with
5599 OFFSET. Return NULL if we can't find one. */
5601 static varinfo_t
5602 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
5604 /* If the offset is outside of the variable, bail out. */
5605 if (offset >= start->fullsize)
5606 return NULL;
5608 /* If we cannot reach offset from start, lookup the first field
5609 and start from there. */
5610 if (start->offset > offset)
5611 start = get_varinfo (start->head);
5613 while (start)
5615 /* We may not find a variable in the field list with the actual
5616 offset when we have glommed a structure to a variable.
5617 In that case, however, offset should still be within the size
5618 of the variable. */
5619 if (offset >= start->offset
5620 && (offset - start->offset) < start->size)
5621 return start;
5623 start = vi_next (start);
5626 return NULL;
5629 /* Find the first varinfo in the same variable as START that overlaps with
5630 OFFSET. If there is no such varinfo the varinfo directly preceding
5631 OFFSET is returned. */
5633 static varinfo_t
5634 first_or_preceding_vi_for_offset (varinfo_t start,
5635 unsigned HOST_WIDE_INT offset)
5637 /* If we cannot reach offset from start, lookup the first field
5638 and start from there. */
5639 if (start->offset > offset)
5640 start = get_varinfo (start->head);
5642 /* We may not find a variable in the field list with the actual
5643 offset when we have glommed a structure to a variable.
5644 In that case, however, offset should still be within the size
5645 of the variable.
5646 If we got beyond the offset we look for return the field
5647 directly preceding offset which may be the last field. */
5648 while (start->next
5649 && offset >= start->offset
5650 && !((offset - start->offset) < start->size))
5651 start = vi_next (start);
5653 return start;
5657 /* This structure is used during pushing fields onto the fieldstack
5658 to track the offset of the field, since bitpos_of_field gives it
5659 relative to its immediate containing type, and we want it relative
5660 to the ultimate containing object. */
5662 struct fieldoff
5664 /* Offset from the base of the base containing object to this field. */
5665 HOST_WIDE_INT offset;
5667 /* Size, in bits, of the field. */
5668 unsigned HOST_WIDE_INT size;
5670 unsigned has_unknown_size : 1;
5672 unsigned must_have_pointers : 1;
5674 unsigned may_have_pointers : 1;
5676 unsigned only_restrict_pointers : 1;
5678 tree restrict_pointed_type;
5680 typedef struct fieldoff fieldoff_s;
5683 /* qsort comparison function for two fieldoff's PA and PB */
5685 static int
5686 fieldoff_compare (const void *pa, const void *pb)
5688 const fieldoff_s *foa = (const fieldoff_s *)pa;
5689 const fieldoff_s *fob = (const fieldoff_s *)pb;
5690 unsigned HOST_WIDE_INT foasize, fobsize;
5692 if (foa->offset < fob->offset)
5693 return -1;
5694 else if (foa->offset > fob->offset)
5695 return 1;
5697 foasize = foa->size;
5698 fobsize = fob->size;
5699 if (foasize < fobsize)
5700 return -1;
5701 else if (foasize > fobsize)
5702 return 1;
5703 return 0;
5706 /* Sort a fieldstack according to the field offset and sizes. */
5707 static void
5708 sort_fieldstack (vec<fieldoff_s> &fieldstack)
5710 fieldstack.qsort (fieldoff_compare);
5713 /* Return true if T is a type that can have subvars. */
5715 static inline bool
5716 type_can_have_subvars (const_tree t)
5718 /* Aggregates without overlapping fields can have subvars. */
5719 return TREE_CODE (t) == RECORD_TYPE;
5722 /* Return true if V is a tree that we can have subvars for.
5723 Normally, this is any aggregate type. Also complex
5724 types which are not gimple registers can have subvars. */
5726 static inline bool
5727 var_can_have_subvars (const_tree v)
5729 /* Volatile variables should never have subvars. */
5730 if (TREE_THIS_VOLATILE (v))
5731 return false;
5733 /* Non decls or memory tags can never have subvars. */
5734 if (!DECL_P (v))
5735 return false;
5737 return type_can_have_subvars (TREE_TYPE (v));
5740 /* Return true if T is a type that does contain pointers. */
5742 static bool
5743 type_must_have_pointers (tree type)
5745 if (POINTER_TYPE_P (type))
5746 return true;
5748 if (TREE_CODE (type) == ARRAY_TYPE)
5749 return type_must_have_pointers (TREE_TYPE (type));
5751 /* A function or method can have pointers as arguments, so track
5752 those separately. */
5753 if (TREE_CODE (type) == FUNCTION_TYPE
5754 || TREE_CODE (type) == METHOD_TYPE)
5755 return true;
5757 return false;
5760 static bool
5761 field_must_have_pointers (tree t)
5763 return type_must_have_pointers (TREE_TYPE (t));
5766 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5767 the fields of TYPE onto fieldstack, recording their offsets along
5768 the way.
5770 OFFSET is used to keep track of the offset in this entire
5771 structure, rather than just the immediately containing structure.
5772 Returns false if the caller is supposed to handle the field we
5773 recursed for. */
5775 static bool
5776 push_fields_onto_fieldstack (tree type, vec<fieldoff_s> *fieldstack,
5777 HOST_WIDE_INT offset)
5779 tree field;
5780 bool empty_p = true;
5782 if (TREE_CODE (type) != RECORD_TYPE)
5783 return false;
5785 /* If the vector of fields is growing too big, bail out early.
5786 Callers check for vec::length <= param_max_fields_for_field_sensitive, make
5787 sure this fails. */
5788 if (fieldstack->length () > (unsigned)param_max_fields_for_field_sensitive)
5789 return false;
5791 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5792 if (TREE_CODE (field) == FIELD_DECL)
5794 bool push = false;
5795 HOST_WIDE_INT foff = bitpos_of_field (field);
5796 tree field_type = TREE_TYPE (field);
5798 if (!var_can_have_subvars (field)
5799 || TREE_CODE (field_type) == QUAL_UNION_TYPE
5800 || TREE_CODE (field_type) == UNION_TYPE)
5801 push = true;
5802 else if (!push_fields_onto_fieldstack
5803 (field_type, fieldstack, offset + foff)
5804 && (DECL_SIZE (field)
5805 && !integer_zerop (DECL_SIZE (field))))
5806 /* Empty structures may have actual size, like in C++. So
5807 see if we didn't push any subfields and the size is
5808 nonzero, push the field onto the stack. */
5809 push = true;
5811 if (push)
5813 fieldoff_s *pair = NULL;
5814 bool has_unknown_size = false;
5815 bool must_have_pointers_p;
5817 if (!fieldstack->is_empty ())
5818 pair = &fieldstack->last ();
5820 /* If there isn't anything at offset zero, create sth. */
5821 if (!pair
5822 && offset + foff != 0)
5824 fieldoff_s e
5825 = {0, offset + foff, false, false, true, false, NULL_TREE};
5826 pair = fieldstack->safe_push (e);
5829 if (!DECL_SIZE (field)
5830 || !tree_fits_uhwi_p (DECL_SIZE (field)))
5831 has_unknown_size = true;
5833 /* If adjacent fields do not contain pointers merge them. */
5834 must_have_pointers_p = field_must_have_pointers (field);
5835 if (pair
5836 && !has_unknown_size
5837 && !must_have_pointers_p
5838 && !pair->must_have_pointers
5839 && !pair->has_unknown_size
5840 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5842 pair->size += tree_to_uhwi (DECL_SIZE (field));
5844 else
5846 fieldoff_s e;
5847 e.offset = offset + foff;
5848 e.has_unknown_size = has_unknown_size;
5849 if (!has_unknown_size)
5850 e.size = tree_to_uhwi (DECL_SIZE (field));
5851 else
5852 e.size = -1;
5853 e.must_have_pointers = must_have_pointers_p;
5854 e.may_have_pointers = true;
5855 e.only_restrict_pointers
5856 = (!has_unknown_size
5857 && POINTER_TYPE_P (field_type)
5858 && TYPE_RESTRICT (field_type));
5859 if (e.only_restrict_pointers)
5860 e.restrict_pointed_type = TREE_TYPE (field_type);
5861 fieldstack->safe_push (e);
5865 empty_p = false;
5868 return !empty_p;
5871 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5872 if it is a varargs function. */
5874 static unsigned int
5875 count_num_arguments (tree decl, bool *is_varargs)
5877 unsigned int num = 0;
5878 tree t;
5880 /* Capture named arguments for K&R functions. They do not
5881 have a prototype and thus no TYPE_ARG_TYPES. */
5882 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5883 ++num;
5885 /* Check if the function has variadic arguments. */
5886 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5887 if (TREE_VALUE (t) == void_type_node)
5888 break;
5889 if (!t)
5890 *is_varargs = true;
5892 return num;
5895 /* Creation function node for DECL, using NAME, and return the index
5896 of the variable we've created for the function. If NONLOCAL_p, create
5897 initial constraints. */
5899 static varinfo_t
5900 create_function_info_for (tree decl, const char *name, bool add_id,
5901 bool nonlocal_p)
5903 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5904 varinfo_t vi, prev_vi;
5905 tree arg;
5906 unsigned int i;
5907 bool is_varargs = false;
5908 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5910 /* Create the variable info. */
5912 vi = new_var_info (decl, name, add_id);
5913 vi->offset = 0;
5914 vi->size = 1;
5915 vi->fullsize = fi_parm_base + num_args;
5916 vi->is_fn_info = 1;
5917 vi->may_have_pointers = false;
5918 if (is_varargs)
5919 vi->fullsize = ~0;
5920 insert_vi_for_tree (vi->decl, vi);
5922 prev_vi = vi;
5924 /* Create a variable for things the function clobbers and one for
5925 things the function uses. */
5927 varinfo_t clobbervi, usevi;
5928 const char *newname;
5929 char *tempname;
5931 tempname = xasprintf ("%s.clobber", name);
5932 newname = ggc_strdup (tempname);
5933 free (tempname);
5935 clobbervi = new_var_info (NULL, newname, false);
5936 clobbervi->offset = fi_clobbers;
5937 clobbervi->size = 1;
5938 clobbervi->fullsize = vi->fullsize;
5939 clobbervi->is_full_var = true;
5940 clobbervi->is_global_var = false;
5941 clobbervi->is_reg_var = true;
5943 gcc_assert (prev_vi->offset < clobbervi->offset);
5944 prev_vi->next = clobbervi->id;
5945 prev_vi = clobbervi;
5947 tempname = xasprintf ("%s.use", name);
5948 newname = ggc_strdup (tempname);
5949 free (tempname);
5951 usevi = new_var_info (NULL, newname, false);
5952 usevi->offset = fi_uses;
5953 usevi->size = 1;
5954 usevi->fullsize = vi->fullsize;
5955 usevi->is_full_var = true;
5956 usevi->is_global_var = false;
5957 usevi->is_reg_var = true;
5959 gcc_assert (prev_vi->offset < usevi->offset);
5960 prev_vi->next = usevi->id;
5961 prev_vi = usevi;
5964 /* And one for the static chain. */
5965 if (fn->static_chain_decl != NULL_TREE)
5967 varinfo_t chainvi;
5968 const char *newname;
5969 char *tempname;
5971 tempname = xasprintf ("%s.chain", name);
5972 newname = ggc_strdup (tempname);
5973 free (tempname);
5975 chainvi = new_var_info (fn->static_chain_decl, newname, false);
5976 chainvi->offset = fi_static_chain;
5977 chainvi->size = 1;
5978 chainvi->fullsize = vi->fullsize;
5979 chainvi->is_full_var = true;
5980 chainvi->is_global_var = false;
5982 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5984 if (nonlocal_p
5985 && chainvi->may_have_pointers)
5986 make_constraint_from (chainvi, nonlocal_id);
5988 gcc_assert (prev_vi->offset < chainvi->offset);
5989 prev_vi->next = chainvi->id;
5990 prev_vi = chainvi;
5993 /* Create a variable for the return var. */
5994 if (DECL_RESULT (decl) != NULL
5995 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5997 varinfo_t resultvi;
5998 const char *newname;
5999 char *tempname;
6000 tree resultdecl = decl;
6002 if (DECL_RESULT (decl))
6003 resultdecl = DECL_RESULT (decl);
6005 tempname = xasprintf ("%s.result", name);
6006 newname = ggc_strdup (tempname);
6007 free (tempname);
6009 resultvi = new_var_info (resultdecl, newname, false);
6010 resultvi->offset = fi_result;
6011 resultvi->size = 1;
6012 resultvi->fullsize = vi->fullsize;
6013 resultvi->is_full_var = true;
6014 if (DECL_RESULT (decl))
6015 resultvi->may_have_pointers = true;
6017 if (DECL_RESULT (decl))
6018 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
6020 if (nonlocal_p
6021 && DECL_RESULT (decl)
6022 && DECL_BY_REFERENCE (DECL_RESULT (decl)))
6023 make_constraint_from (resultvi, nonlocal_id);
6025 gcc_assert (prev_vi->offset < resultvi->offset);
6026 prev_vi->next = resultvi->id;
6027 prev_vi = resultvi;
6030 /* We also need to make function return values escape. Nothing
6031 escapes by returning from main though. */
6032 if (nonlocal_p
6033 && !MAIN_NAME_P (DECL_NAME (decl)))
6035 varinfo_t fi, rvi;
6036 fi = lookup_vi_for_tree (decl);
6037 rvi = first_vi_for_offset (fi, fi_result);
6038 if (rvi && rvi->offset == fi_result)
6039 make_copy_constraint (get_varinfo (escaped_id), rvi->id);
6042 /* Set up variables for each argument. */
6043 arg = DECL_ARGUMENTS (decl);
6044 for (i = 0; i < num_args; i++)
6046 varinfo_t argvi;
6047 const char *newname;
6048 char *tempname;
6049 tree argdecl = decl;
6051 if (arg)
6052 argdecl = arg;
6054 tempname = xasprintf ("%s.arg%d", name, i);
6055 newname = ggc_strdup (tempname);
6056 free (tempname);
6058 argvi = new_var_info (argdecl, newname, false);
6059 argvi->offset = fi_parm_base + i;
6060 argvi->size = 1;
6061 argvi->is_full_var = true;
6062 argvi->fullsize = vi->fullsize;
6063 if (arg)
6064 argvi->may_have_pointers = true;
6066 if (arg)
6067 insert_vi_for_tree (arg, argvi);
6069 if (nonlocal_p
6070 && argvi->may_have_pointers)
6071 make_constraint_from (argvi, nonlocal_id);
6073 gcc_assert (prev_vi->offset < argvi->offset);
6074 prev_vi->next = argvi->id;
6075 prev_vi = argvi;
6076 if (arg)
6077 arg = DECL_CHAIN (arg);
6080 /* Add one representative for all further args. */
6081 if (is_varargs)
6083 varinfo_t argvi;
6084 const char *newname;
6085 char *tempname;
6086 tree decl;
6088 tempname = xasprintf ("%s.varargs", name);
6089 newname = ggc_strdup (tempname);
6090 free (tempname);
6092 /* We need sth that can be pointed to for va_start. */
6093 decl = build_fake_var_decl (ptr_type_node);
6095 argvi = new_var_info (decl, newname, false);
6096 argvi->offset = fi_parm_base + num_args;
6097 argvi->size = ~0;
6098 argvi->is_full_var = true;
6099 argvi->is_heap_var = true;
6100 argvi->fullsize = vi->fullsize;
6102 if (nonlocal_p
6103 && argvi->may_have_pointers)
6104 make_constraint_from (argvi, nonlocal_id);
6106 gcc_assert (prev_vi->offset < argvi->offset);
6107 prev_vi->next = argvi->id;
6110 return vi;
6114 /* Return true if FIELDSTACK contains fields that overlap.
6115 FIELDSTACK is assumed to be sorted by offset. */
6117 static bool
6118 check_for_overlaps (const vec<fieldoff_s> &fieldstack)
6120 fieldoff_s *fo = NULL;
6121 unsigned int i;
6122 HOST_WIDE_INT lastoffset = -1;
6124 FOR_EACH_VEC_ELT (fieldstack, i, fo)
6126 if (fo->offset == lastoffset)
6127 return true;
6128 lastoffset = fo->offset;
6130 return false;
6133 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
6134 This will also create any varinfo structures necessary for fields
6135 of DECL. DECL is a function parameter if HANDLE_PARAM is set.
6136 HANDLED_STRUCT_TYPE is used to register struct types reached by following
6137 restrict pointers. This is needed to prevent infinite recursion.
6138 If ADD_RESTRICT, pretend that the pointer NAME is restrict even if DECL
6139 does not advertise it. */
6141 static varinfo_t
6142 create_variable_info_for_1 (tree decl, const char *name, bool add_id,
6143 bool handle_param, bitmap handled_struct_type,
6144 bool add_restrict = false)
6146 varinfo_t vi, newvi;
6147 tree decl_type = TREE_TYPE (decl);
6148 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
6149 auto_vec<fieldoff_s> fieldstack;
6150 fieldoff_s *fo;
6151 unsigned int i;
6153 if (!declsize
6154 || !tree_fits_uhwi_p (declsize))
6156 vi = new_var_info (decl, name, add_id);
6157 vi->offset = 0;
6158 vi->size = ~0;
6159 vi->fullsize = ~0;
6160 vi->is_unknown_size_var = true;
6161 vi->is_full_var = true;
6162 vi->may_have_pointers = true;
6163 return vi;
6166 /* Collect field information. */
6167 if (use_field_sensitive
6168 && var_can_have_subvars (decl)
6169 /* ??? Force us to not use subfields for globals in IPA mode.
6170 Else we'd have to parse arbitrary initializers. */
6171 && !(in_ipa_mode
6172 && is_global_var (decl)))
6174 fieldoff_s *fo = NULL;
6175 bool notokay = false;
6176 unsigned int i;
6178 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
6180 for (i = 0; !notokay && fieldstack.iterate (i, &fo); i++)
6181 if (fo->has_unknown_size
6182 || fo->offset < 0)
6184 notokay = true;
6185 break;
6188 /* We can't sort them if we have a field with a variable sized type,
6189 which will make notokay = true. In that case, we are going to return
6190 without creating varinfos for the fields anyway, so sorting them is a
6191 waste to boot. */
6192 if (!notokay)
6194 sort_fieldstack (fieldstack);
6195 /* Due to some C++ FE issues, like PR 22488, we might end up
6196 what appear to be overlapping fields even though they,
6197 in reality, do not overlap. Until the C++ FE is fixed,
6198 we will simply disable field-sensitivity for these cases. */
6199 notokay = check_for_overlaps (fieldstack);
6202 if (notokay)
6203 fieldstack.release ();
6206 /* If we didn't end up collecting sub-variables create a full
6207 variable for the decl. */
6208 if (fieldstack.length () == 0
6209 || fieldstack.length () > (unsigned)param_max_fields_for_field_sensitive)
6211 vi = new_var_info (decl, name, add_id);
6212 vi->offset = 0;
6213 vi->may_have_pointers = true;
6214 vi->fullsize = tree_to_uhwi (declsize);
6215 vi->size = vi->fullsize;
6216 vi->is_full_var = true;
6217 if (POINTER_TYPE_P (decl_type)
6218 && (TYPE_RESTRICT (decl_type) || add_restrict))
6219 vi->only_restrict_pointers = 1;
6220 if (vi->only_restrict_pointers
6221 && !type_contains_placeholder_p (TREE_TYPE (decl_type))
6222 && handle_param
6223 && !bitmap_bit_p (handled_struct_type,
6224 TYPE_UID (TREE_TYPE (decl_type))))
6226 varinfo_t rvi;
6227 tree heapvar = build_fake_var_decl (TREE_TYPE (decl_type));
6228 DECL_EXTERNAL (heapvar) = 1;
6229 if (var_can_have_subvars (heapvar))
6230 bitmap_set_bit (handled_struct_type,
6231 TYPE_UID (TREE_TYPE (decl_type)));
6232 rvi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS", true,
6233 true, handled_struct_type);
6234 if (var_can_have_subvars (heapvar))
6235 bitmap_clear_bit (handled_struct_type,
6236 TYPE_UID (TREE_TYPE (decl_type)));
6237 rvi->is_restrict_var = 1;
6238 insert_vi_for_tree (heapvar, rvi);
6239 make_constraint_from (vi, rvi->id);
6240 make_param_constraints (rvi);
6242 fieldstack.release ();
6243 return vi;
6246 vi = new_var_info (decl, name, add_id);
6247 vi->fullsize = tree_to_uhwi (declsize);
6248 if (fieldstack.length () == 1)
6249 vi->is_full_var = true;
6250 for (i = 0, newvi = vi;
6251 fieldstack.iterate (i, &fo);
6252 ++i, newvi = vi_next (newvi))
6254 const char *newname = NULL;
6255 char *tempname;
6257 if (dump_file)
6259 if (fieldstack.length () != 1)
6261 tempname
6262 = xasprintf ("%s." HOST_WIDE_INT_PRINT_DEC
6263 "+" HOST_WIDE_INT_PRINT_DEC, name,
6264 fo->offset, fo->size);
6265 newname = ggc_strdup (tempname);
6266 free (tempname);
6269 else
6270 newname = "NULL";
6272 if (newname)
6273 newvi->name = newname;
6274 newvi->offset = fo->offset;
6275 newvi->size = fo->size;
6276 newvi->fullsize = vi->fullsize;
6277 newvi->may_have_pointers = fo->may_have_pointers;
6278 newvi->only_restrict_pointers = fo->only_restrict_pointers;
6279 if (handle_param
6280 && newvi->only_restrict_pointers
6281 && !type_contains_placeholder_p (fo->restrict_pointed_type)
6282 && !bitmap_bit_p (handled_struct_type,
6283 TYPE_UID (fo->restrict_pointed_type)))
6285 varinfo_t rvi;
6286 tree heapvar = build_fake_var_decl (fo->restrict_pointed_type);
6287 DECL_EXTERNAL (heapvar) = 1;
6288 if (var_can_have_subvars (heapvar))
6289 bitmap_set_bit (handled_struct_type,
6290 TYPE_UID (fo->restrict_pointed_type));
6291 rvi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS", true,
6292 true, handled_struct_type);
6293 if (var_can_have_subvars (heapvar))
6294 bitmap_clear_bit (handled_struct_type,
6295 TYPE_UID (fo->restrict_pointed_type));
6296 rvi->is_restrict_var = 1;
6297 insert_vi_for_tree (heapvar, rvi);
6298 make_constraint_from (newvi, rvi->id);
6299 make_param_constraints (rvi);
6301 if (i + 1 < fieldstack.length ())
6303 varinfo_t tem = new_var_info (decl, name, false);
6304 newvi->next = tem->id;
6305 tem->head = vi->id;
6309 return vi;
6312 static unsigned int
6313 create_variable_info_for (tree decl, const char *name, bool add_id)
6315 /* First see if we are dealing with an ifunc resolver call and
6316 assiociate that with a call to the resolver function result. */
6317 cgraph_node *node;
6318 if (in_ipa_mode
6319 && TREE_CODE (decl) == FUNCTION_DECL
6320 && (node = cgraph_node::get (decl))
6321 && node->ifunc_resolver)
6323 varinfo_t fi = get_vi_for_tree (node->get_alias_target ()->decl);
6324 constraint_expr rhs
6325 = get_function_part_constraint (fi, fi_result);
6326 fi = new_var_info (NULL_TREE, "ifuncres", true);
6327 fi->is_reg_var = true;
6328 constraint_expr lhs;
6329 lhs.type = SCALAR;
6330 lhs.var = fi->id;
6331 lhs.offset = 0;
6332 process_constraint (new_constraint (lhs, rhs));
6333 insert_vi_for_tree (decl, fi);
6334 return fi->id;
6337 varinfo_t vi = create_variable_info_for_1 (decl, name, add_id, false, NULL);
6338 unsigned int id = vi->id;
6340 insert_vi_for_tree (decl, vi);
6342 if (!VAR_P (decl))
6343 return id;
6345 /* Create initial constraints for globals. */
6346 for (; vi; vi = vi_next (vi))
6348 if (!vi->may_have_pointers
6349 || !vi->is_global_var)
6350 continue;
6352 /* Mark global restrict qualified pointers. */
6353 if ((POINTER_TYPE_P (TREE_TYPE (decl))
6354 && TYPE_RESTRICT (TREE_TYPE (decl)))
6355 || vi->only_restrict_pointers)
6357 varinfo_t rvi
6358 = make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT",
6359 true);
6360 /* ??? For now exclude reads from globals as restrict sources
6361 if those are not (indirectly) from incoming parameters. */
6362 rvi->is_restrict_var = false;
6363 continue;
6366 /* In non-IPA mode the initializer from nonlocal is all we need. */
6367 if (!in_ipa_mode
6368 || DECL_HARD_REGISTER (decl))
6369 make_copy_constraint (vi, nonlocal_id);
6371 /* In IPA mode parse the initializer and generate proper constraints
6372 for it. */
6373 else
6375 varpool_node *vnode = varpool_node::get (decl);
6377 /* For escaped variables initialize them from nonlocal. */
6378 if (!vnode->all_refs_explicit_p ())
6379 make_copy_constraint (vi, nonlocal_id);
6381 /* If this is a global variable with an initializer and we are in
6382 IPA mode generate constraints for it. */
6383 ipa_ref *ref;
6384 for (unsigned idx = 0; vnode->iterate_reference (idx, ref); ++idx)
6386 auto_vec<ce_s> rhsc;
6387 struct constraint_expr lhs, *rhsp;
6388 unsigned i;
6389 get_constraint_for_address_of (ref->referred->decl, &rhsc);
6390 lhs.var = vi->id;
6391 lhs.offset = 0;
6392 lhs.type = SCALAR;
6393 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
6394 process_constraint (new_constraint (lhs, *rhsp));
6395 /* If this is a variable that escapes from the unit
6396 the initializer escapes as well. */
6397 if (!vnode->all_refs_explicit_p ())
6399 lhs.var = escaped_id;
6400 lhs.offset = 0;
6401 lhs.type = SCALAR;
6402 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
6403 process_constraint (new_constraint (lhs, *rhsp));
6409 return id;
6412 /* Print out the points-to solution for VAR to FILE. */
6414 static void
6415 dump_solution_for_var (FILE *file, unsigned int var)
6417 varinfo_t vi = get_varinfo (var);
6418 unsigned int i;
6419 bitmap_iterator bi;
6421 /* Dump the solution for unified vars anyway, this avoids difficulties
6422 in scanning dumps in the testsuite. */
6423 fprintf (file, "%s = { ", vi->name);
6424 vi = get_varinfo (find (var));
6425 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
6426 fprintf (file, "%s ", get_varinfo (i)->name);
6427 fprintf (file, "}");
6429 /* But note when the variable was unified. */
6430 if (vi->id != var)
6431 fprintf (file, " same as %s", vi->name);
6433 fprintf (file, "\n");
6436 /* Print the points-to solution for VAR to stderr. */
6438 DEBUG_FUNCTION void
6439 debug_solution_for_var (unsigned int var)
6441 dump_solution_for_var (stderr, var);
6444 /* Register the constraints for function parameter related VI. */
6446 static void
6447 make_param_constraints (varinfo_t vi)
6449 for (; vi; vi = vi_next (vi))
6451 if (vi->only_restrict_pointers)
6453 else if (vi->may_have_pointers)
6454 make_constraint_from (vi, nonlocal_id);
6456 if (vi->is_full_var)
6457 break;
6461 /* Create varinfo structures for all of the variables in the
6462 function for intraprocedural mode. */
6464 static void
6465 intra_create_variable_infos (struct function *fn)
6467 tree t;
6468 bitmap handled_struct_type = NULL;
6469 bool this_parm_in_ctor = DECL_CXX_CONSTRUCTOR_P (fn->decl);
6471 /* For each incoming pointer argument arg, create the constraint ARG
6472 = NONLOCAL or a dummy variable if it is a restrict qualified
6473 passed-by-reference argument. */
6474 for (t = DECL_ARGUMENTS (fn->decl); t; t = DECL_CHAIN (t))
6476 if (handled_struct_type == NULL)
6477 handled_struct_type = BITMAP_ALLOC (NULL);
6479 varinfo_t p
6480 = create_variable_info_for_1 (t, alias_get_name (t), false, true,
6481 handled_struct_type, this_parm_in_ctor);
6482 insert_vi_for_tree (t, p);
6484 make_param_constraints (p);
6486 this_parm_in_ctor = false;
6489 if (handled_struct_type != NULL)
6490 BITMAP_FREE (handled_struct_type);
6492 /* Add a constraint for a result decl that is passed by reference. */
6493 if (DECL_RESULT (fn->decl)
6494 && DECL_BY_REFERENCE (DECL_RESULT (fn->decl)))
6496 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (fn->decl));
6498 for (p = result_vi; p; p = vi_next (p))
6499 make_constraint_from (p, nonlocal_id);
6502 /* Add a constraint for the incoming static chain parameter. */
6503 if (fn->static_chain_decl != NULL_TREE)
6505 varinfo_t p, chain_vi = get_vi_for_tree (fn->static_chain_decl);
6507 for (p = chain_vi; p; p = vi_next (p))
6508 make_constraint_from (p, nonlocal_id);
6512 /* Structure used to put solution bitmaps in a hashtable so they can
6513 be shared among variables with the same points-to set. */
6515 typedef struct shared_bitmap_info
6517 bitmap pt_vars;
6518 hashval_t hashcode;
6519 } *shared_bitmap_info_t;
6520 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
6522 /* Shared_bitmap hashtable helpers. */
6524 struct shared_bitmap_hasher : free_ptr_hash <shared_bitmap_info>
6526 static inline hashval_t hash (const shared_bitmap_info *);
6527 static inline bool equal (const shared_bitmap_info *,
6528 const shared_bitmap_info *);
6531 /* Hash function for a shared_bitmap_info_t */
6533 inline hashval_t
6534 shared_bitmap_hasher::hash (const shared_bitmap_info *bi)
6536 return bi->hashcode;
6539 /* Equality function for two shared_bitmap_info_t's. */
6541 inline bool
6542 shared_bitmap_hasher::equal (const shared_bitmap_info *sbi1,
6543 const shared_bitmap_info *sbi2)
6545 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
6548 /* Shared_bitmap hashtable. */
6550 static hash_table<shared_bitmap_hasher> *shared_bitmap_table;
6552 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
6553 existing instance if there is one, NULL otherwise. */
6555 static bitmap
6556 shared_bitmap_lookup (bitmap pt_vars)
6558 shared_bitmap_info **slot;
6559 struct shared_bitmap_info sbi;
6561 sbi.pt_vars = pt_vars;
6562 sbi.hashcode = bitmap_hash (pt_vars);
6564 slot = shared_bitmap_table->find_slot (&sbi, NO_INSERT);
6565 if (!slot)
6566 return NULL;
6567 else
6568 return (*slot)->pt_vars;
6572 /* Add a bitmap to the shared bitmap hashtable. */
6574 static void
6575 shared_bitmap_add (bitmap pt_vars)
6577 shared_bitmap_info **slot;
6578 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
6580 sbi->pt_vars = pt_vars;
6581 sbi->hashcode = bitmap_hash (pt_vars);
6583 slot = shared_bitmap_table->find_slot (sbi, INSERT);
6584 gcc_assert (!*slot);
6585 *slot = sbi;
6589 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
6591 static void
6592 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt,
6593 tree fndecl)
6595 unsigned int i;
6596 bitmap_iterator bi;
6597 varinfo_t escaped_vi = get_varinfo (find (escaped_id));
6598 bool everything_escaped
6599 = escaped_vi->solution && bitmap_bit_p (escaped_vi->solution, anything_id);
6601 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
6603 varinfo_t vi = get_varinfo (i);
6605 if (vi->is_artificial_var)
6606 continue;
6608 if (everything_escaped
6609 || (escaped_vi->solution
6610 && bitmap_bit_p (escaped_vi->solution, i)))
6612 pt->vars_contains_escaped = true;
6613 pt->vars_contains_escaped_heap |= vi->is_heap_var;
6616 if (vi->is_restrict_var)
6617 pt->vars_contains_restrict = true;
6619 if (VAR_P (vi->decl)
6620 || TREE_CODE (vi->decl) == PARM_DECL
6621 || TREE_CODE (vi->decl) == RESULT_DECL)
6623 /* If we are in IPA mode we will not recompute points-to
6624 sets after inlining so make sure they stay valid. */
6625 if (in_ipa_mode
6626 && !DECL_PT_UID_SET_P (vi->decl))
6627 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
6629 /* Add the decl to the points-to set. Note that the points-to
6630 set contains global variables. */
6631 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
6632 if (vi->is_global_var
6633 /* In IPA mode the escaped_heap trick doesn't work as
6634 ESCAPED is escaped from the unit but
6635 pt_solution_includes_global needs to answer true for
6636 all variables not automatic within a function.
6637 For the same reason is_global_var is not the
6638 correct flag to track - local variables from other
6639 functions also need to be considered global.
6640 Conveniently all HEAP vars are not put in function
6641 scope. */
6642 || (in_ipa_mode
6643 && fndecl
6644 && ! auto_var_in_fn_p (vi->decl, fndecl)))
6645 pt->vars_contains_nonlocal = true;
6647 /* If we have a variable that is interposable record that fact
6648 for pointer comparison simplification. */
6649 if (VAR_P (vi->decl)
6650 && (TREE_STATIC (vi->decl) || DECL_EXTERNAL (vi->decl))
6651 && ! decl_binds_to_current_def_p (vi->decl))
6652 pt->vars_contains_interposable = true;
6654 /* If this is a local variable we can have overlapping lifetime
6655 of different function invocations through recursion duplicate
6656 it with its shadow variable. */
6657 if (in_ipa_mode
6658 && vi->shadow_var_uid != 0)
6660 bitmap_set_bit (into, vi->shadow_var_uid);
6661 pt->vars_contains_nonlocal = true;
6665 else if (TREE_CODE (vi->decl) == FUNCTION_DECL
6666 || TREE_CODE (vi->decl) == LABEL_DECL)
6668 /* Nothing should read/write from/to code so we can
6669 save bits by not including them in the points-to bitmaps.
6670 Still mark the points-to set as containing global memory
6671 to make code-patching possible - see PR70128. */
6672 pt->vars_contains_nonlocal = true;
6678 /* Compute the points-to solution *PT for the variable VI. */
6680 static struct pt_solution
6681 find_what_var_points_to (tree fndecl, varinfo_t orig_vi)
6683 unsigned int i;
6684 bitmap_iterator bi;
6685 bitmap finished_solution;
6686 bitmap result;
6687 varinfo_t vi;
6688 struct pt_solution *pt;
6690 /* This variable may have been collapsed, let's get the real
6691 variable. */
6692 vi = get_varinfo (find (orig_vi->id));
6694 /* See if we have already computed the solution and return it. */
6695 pt_solution **slot = &final_solutions->get_or_insert (vi);
6696 if (*slot != NULL)
6697 return **slot;
6699 *slot = pt = XOBNEW (&final_solutions_obstack, struct pt_solution);
6700 memset (pt, 0, sizeof (struct pt_solution));
6702 /* Translate artificial variables into SSA_NAME_PTR_INFO
6703 attributes. */
6704 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
6706 varinfo_t vi = get_varinfo (i);
6708 if (vi->is_artificial_var)
6710 if (vi->id == nothing_id)
6711 pt->null = 1;
6712 else if (vi->id == escaped_id)
6714 if (in_ipa_mode)
6715 pt->ipa_escaped = 1;
6716 else
6717 pt->escaped = 1;
6718 /* Expand some special vars of ESCAPED in-place here. */
6719 varinfo_t evi = get_varinfo (find (escaped_id));
6720 if (bitmap_bit_p (evi->solution, nonlocal_id))
6721 pt->nonlocal = 1;
6723 else if (vi->id == nonlocal_id)
6724 pt->nonlocal = 1;
6725 else if (vi->id == string_id)
6726 /* Nobody cares - STRING_CSTs are read-only entities. */
6728 else if (vi->id == anything_id
6729 || vi->id == integer_id)
6730 pt->anything = 1;
6734 /* Instead of doing extra work, simply do not create
6735 elaborate points-to information for pt_anything pointers. */
6736 if (pt->anything)
6737 return *pt;
6739 /* Share the final set of variables when possible. */
6740 finished_solution = BITMAP_GGC_ALLOC ();
6741 stats.points_to_sets_created++;
6743 set_uids_in_ptset (finished_solution, vi->solution, pt, fndecl);
6744 result = shared_bitmap_lookup (finished_solution);
6745 if (!result)
6747 shared_bitmap_add (finished_solution);
6748 pt->vars = finished_solution;
6750 else
6752 pt->vars = result;
6753 bitmap_clear (finished_solution);
6756 return *pt;
6759 /* Given a pointer variable P, fill in its points-to set. */
6761 static void
6762 find_what_p_points_to (tree fndecl, tree p)
6764 struct ptr_info_def *pi;
6765 tree lookup_p = p;
6766 varinfo_t vi;
6767 value_range vr;
6768 get_range_query (DECL_STRUCT_FUNCTION (fndecl))->range_of_expr (vr, p);
6769 bool nonnull = vr.nonzero_p ();
6771 /* For parameters, get at the points-to set for the actual parm
6772 decl. */
6773 if (TREE_CODE (p) == SSA_NAME
6774 && SSA_NAME_IS_DEFAULT_DEF (p)
6775 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
6776 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL))
6777 lookup_p = SSA_NAME_VAR (p);
6779 vi = lookup_vi_for_tree (lookup_p);
6780 if (!vi)
6781 return;
6783 pi = get_ptr_info (p);
6784 pi->pt = find_what_var_points_to (fndecl, vi);
6785 /* Conservatively set to NULL from PTA (to true). */
6786 pi->pt.null = 1;
6787 /* Preserve pointer nonnull globally computed. */
6788 if (nonnull)
6789 set_ptr_nonnull (p);
6793 /* Query statistics for points-to solutions. */
6795 static struct {
6796 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
6797 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
6798 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
6799 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
6800 } pta_stats;
6802 void
6803 dump_pta_stats (FILE *s)
6805 fprintf (s, "\nPTA query stats:\n");
6806 fprintf (s, " pt_solution_includes: "
6807 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6808 HOST_WIDE_INT_PRINT_DEC" queries\n",
6809 pta_stats.pt_solution_includes_no_alias,
6810 pta_stats.pt_solution_includes_no_alias
6811 + pta_stats.pt_solution_includes_may_alias);
6812 fprintf (s, " pt_solutions_intersect: "
6813 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6814 HOST_WIDE_INT_PRINT_DEC" queries\n",
6815 pta_stats.pt_solutions_intersect_no_alias,
6816 pta_stats.pt_solutions_intersect_no_alias
6817 + pta_stats.pt_solutions_intersect_may_alias);
6821 /* Reset the points-to solution *PT to a conservative default
6822 (point to anything). */
6824 void
6825 pt_solution_reset (struct pt_solution *pt)
6827 memset (pt, 0, sizeof (struct pt_solution));
6828 pt->anything = true;
6829 pt->null = true;
6832 /* Set the points-to solution *PT to point only to the variables
6833 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6834 global variables and VARS_CONTAINS_RESTRICT specifies whether
6835 it contains restrict tag variables. */
6837 void
6838 pt_solution_set (struct pt_solution *pt, bitmap vars,
6839 bool vars_contains_nonlocal)
6841 memset (pt, 0, sizeof (struct pt_solution));
6842 pt->vars = vars;
6843 pt->vars_contains_nonlocal = vars_contains_nonlocal;
6844 pt->vars_contains_escaped
6845 = (cfun->gimple_df->escaped.anything
6846 || bitmap_intersect_p (cfun->gimple_df->escaped.vars, vars));
6849 /* Set the points-to solution *PT to point only to the variable VAR. */
6851 void
6852 pt_solution_set_var (struct pt_solution *pt, tree var)
6854 memset (pt, 0, sizeof (struct pt_solution));
6855 pt->vars = BITMAP_GGC_ALLOC ();
6856 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
6857 pt->vars_contains_nonlocal = is_global_var (var);
6858 pt->vars_contains_escaped
6859 = (cfun->gimple_df->escaped.anything
6860 || bitmap_bit_p (cfun->gimple_df->escaped.vars, DECL_PT_UID (var)));
6863 /* Computes the union of the points-to solutions *DEST and *SRC and
6864 stores the result in *DEST. This changes the points-to bitmap
6865 of *DEST and thus may not be used if that might be shared.
6866 The points-to bitmap of *SRC and *DEST will not be shared after
6867 this function if they were not before. */
6869 static void
6870 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6872 dest->anything |= src->anything;
6873 if (dest->anything)
6875 pt_solution_reset (dest);
6876 return;
6879 dest->nonlocal |= src->nonlocal;
6880 dest->escaped |= src->escaped;
6881 dest->ipa_escaped |= src->ipa_escaped;
6882 dest->null |= src->null;
6883 dest->vars_contains_nonlocal |= src->vars_contains_nonlocal;
6884 dest->vars_contains_escaped |= src->vars_contains_escaped;
6885 dest->vars_contains_escaped_heap |= src->vars_contains_escaped_heap;
6886 if (!src->vars)
6887 return;
6889 if (!dest->vars)
6890 dest->vars = BITMAP_GGC_ALLOC ();
6891 bitmap_ior_into (dest->vars, src->vars);
6894 /* Return true if the points-to solution *PT is empty. */
6896 bool
6897 pt_solution_empty_p (const pt_solution *pt)
6899 if (pt->anything
6900 || pt->nonlocal)
6901 return false;
6903 if (pt->vars
6904 && !bitmap_empty_p (pt->vars))
6905 return false;
6907 /* If the solution includes ESCAPED, check if that is empty. */
6908 if (pt->escaped
6909 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6910 return false;
6912 /* If the solution includes ESCAPED, check if that is empty. */
6913 if (pt->ipa_escaped
6914 && !pt_solution_empty_p (&ipa_escaped_pt))
6915 return false;
6917 return true;
6920 /* Return true if the points-to solution *PT only point to a single var, and
6921 return the var uid in *UID. */
6923 bool
6924 pt_solution_singleton_or_null_p (struct pt_solution *pt, unsigned *uid)
6926 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
6927 || pt->vars == NULL
6928 || !bitmap_single_bit_set_p (pt->vars))
6929 return false;
6931 *uid = bitmap_first_set_bit (pt->vars);
6932 return true;
6935 /* Return true if the points-to solution *PT includes global memory. */
6937 bool
6938 pt_solution_includes_global (struct pt_solution *pt)
6940 if (pt->anything
6941 || pt->nonlocal
6942 || pt->vars_contains_nonlocal
6943 /* The following is a hack to make the malloc escape hack work.
6944 In reality we'd need different sets for escaped-through-return
6945 and escaped-to-callees and passes would need to be updated. */
6946 || pt->vars_contains_escaped_heap)
6947 return true;
6949 /* 'escaped' is also a placeholder so we have to look into it. */
6950 if (pt->escaped)
6951 return pt_solution_includes_global (&cfun->gimple_df->escaped);
6953 if (pt->ipa_escaped)
6954 return pt_solution_includes_global (&ipa_escaped_pt);
6956 return false;
6959 /* Return true if the points-to solution *PT includes the variable
6960 declaration DECL. */
6962 static bool
6963 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6965 if (pt->anything)
6966 return true;
6968 if (pt->nonlocal
6969 && is_global_var (decl))
6970 return true;
6972 if (pt->vars
6973 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6974 return true;
6976 /* If the solution includes ESCAPED, check it. */
6977 if (pt->escaped
6978 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6979 return true;
6981 /* If the solution includes ESCAPED, check it. */
6982 if (pt->ipa_escaped
6983 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6984 return true;
6986 return false;
6989 bool
6990 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6992 bool res = pt_solution_includes_1 (pt, decl);
6993 if (res)
6994 ++pta_stats.pt_solution_includes_may_alias;
6995 else
6996 ++pta_stats.pt_solution_includes_no_alias;
6997 return res;
7000 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
7001 intersection. */
7003 static bool
7004 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
7006 if (pt1->anything || pt2->anything)
7007 return true;
7009 /* If either points to unknown global memory and the other points to
7010 any global memory they alias. */
7011 if ((pt1->nonlocal
7012 && (pt2->nonlocal
7013 || pt2->vars_contains_nonlocal))
7014 || (pt2->nonlocal
7015 && pt1->vars_contains_nonlocal))
7016 return true;
7018 /* If either points to all escaped memory and the other points to
7019 any escaped memory they alias. */
7020 if ((pt1->escaped
7021 && (pt2->escaped
7022 || pt2->vars_contains_escaped))
7023 || (pt2->escaped
7024 && pt1->vars_contains_escaped))
7025 return true;
7027 /* Check the escaped solution if required.
7028 ??? Do we need to check the local against the IPA escaped sets? */
7029 if ((pt1->ipa_escaped || pt2->ipa_escaped)
7030 && !pt_solution_empty_p (&ipa_escaped_pt))
7032 /* If both point to escaped memory and that solution
7033 is not empty they alias. */
7034 if (pt1->ipa_escaped && pt2->ipa_escaped)
7035 return true;
7037 /* If either points to escaped memory see if the escaped solution
7038 intersects with the other. */
7039 if ((pt1->ipa_escaped
7040 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
7041 || (pt2->ipa_escaped
7042 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
7043 return true;
7046 /* Now both pointers alias if their points-to solution intersects. */
7047 return (pt1->vars
7048 && pt2->vars
7049 && bitmap_intersect_p (pt1->vars, pt2->vars));
7052 bool
7053 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
7055 bool res = pt_solutions_intersect_1 (pt1, pt2);
7056 if (res)
7057 ++pta_stats.pt_solutions_intersect_may_alias;
7058 else
7059 ++pta_stats.pt_solutions_intersect_no_alias;
7060 return res;
7064 /* Dump points-to information to OUTFILE. */
7066 static void
7067 dump_sa_points_to_info (FILE *outfile)
7069 unsigned int i;
7071 fprintf (outfile, "\nPoints-to sets\n\n");
7073 if (dump_flags & TDF_STATS)
7075 fprintf (outfile, "Stats:\n");
7076 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
7077 fprintf (outfile, "Non-pointer vars: %d\n",
7078 stats.nonpointer_vars);
7079 fprintf (outfile, "Statically unified vars: %d\n",
7080 stats.unified_vars_static);
7081 fprintf (outfile, "Dynamically unified vars: %d\n",
7082 stats.unified_vars_dynamic);
7083 fprintf (outfile, "Iterations: %d\n", stats.iterations);
7084 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
7085 fprintf (outfile, "Number of implicit edges: %d\n",
7086 stats.num_implicit_edges);
7089 for (i = 1; i < varmap.length (); i++)
7091 varinfo_t vi = get_varinfo (i);
7092 if (!vi->may_have_pointers)
7093 continue;
7094 dump_solution_for_var (outfile, i);
7099 /* Debug points-to information to stderr. */
7101 DEBUG_FUNCTION void
7102 debug_sa_points_to_info (void)
7104 dump_sa_points_to_info (stderr);
7108 /* Initialize the always-existing constraint variables for NULL
7109 ANYTHING, READONLY, and INTEGER */
7111 static void
7112 init_base_vars (void)
7114 struct constraint_expr lhs, rhs;
7115 varinfo_t var_anything;
7116 varinfo_t var_nothing;
7117 varinfo_t var_string;
7118 varinfo_t var_escaped;
7119 varinfo_t var_nonlocal;
7120 varinfo_t var_storedanything;
7121 varinfo_t var_integer;
7123 /* Variable ID zero is reserved and should be NULL. */
7124 varmap.safe_push (NULL);
7126 /* Create the NULL variable, used to represent that a variable points
7127 to NULL. */
7128 var_nothing = new_var_info (NULL_TREE, "NULL", false);
7129 gcc_assert (var_nothing->id == nothing_id);
7130 var_nothing->is_artificial_var = 1;
7131 var_nothing->offset = 0;
7132 var_nothing->size = ~0;
7133 var_nothing->fullsize = ~0;
7134 var_nothing->is_special_var = 1;
7135 var_nothing->may_have_pointers = 0;
7136 var_nothing->is_global_var = 0;
7138 /* Create the ANYTHING variable, used to represent that a variable
7139 points to some unknown piece of memory. */
7140 var_anything = new_var_info (NULL_TREE, "ANYTHING", false);
7141 gcc_assert (var_anything->id == anything_id);
7142 var_anything->is_artificial_var = 1;
7143 var_anything->size = ~0;
7144 var_anything->offset = 0;
7145 var_anything->fullsize = ~0;
7146 var_anything->is_special_var = 1;
7148 /* Anything points to anything. This makes deref constraints just
7149 work in the presence of linked list and other p = *p type loops,
7150 by saying that *ANYTHING = ANYTHING. */
7151 lhs.type = SCALAR;
7152 lhs.var = anything_id;
7153 lhs.offset = 0;
7154 rhs.type = ADDRESSOF;
7155 rhs.var = anything_id;
7156 rhs.offset = 0;
7158 /* This specifically does not use process_constraint because
7159 process_constraint ignores all anything = anything constraints, since all
7160 but this one are redundant. */
7161 constraints.safe_push (new_constraint (lhs, rhs));
7163 /* Create the STRING variable, used to represent that a variable
7164 points to a string literal. String literals don't contain
7165 pointers so STRING doesn't point to anything. */
7166 var_string = new_var_info (NULL_TREE, "STRING", false);
7167 gcc_assert (var_string->id == string_id);
7168 var_string->is_artificial_var = 1;
7169 var_string->offset = 0;
7170 var_string->size = ~0;
7171 var_string->fullsize = ~0;
7172 var_string->is_special_var = 1;
7173 var_string->may_have_pointers = 0;
7175 /* Create the ESCAPED variable, used to represent the set of escaped
7176 memory. */
7177 var_escaped = new_var_info (NULL_TREE, "ESCAPED", false);
7178 gcc_assert (var_escaped->id == escaped_id);
7179 var_escaped->is_artificial_var = 1;
7180 var_escaped->offset = 0;
7181 var_escaped->size = ~0;
7182 var_escaped->fullsize = ~0;
7183 var_escaped->is_special_var = 0;
7185 /* Create the NONLOCAL variable, used to represent the set of nonlocal
7186 memory. */
7187 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL", false);
7188 gcc_assert (var_nonlocal->id == nonlocal_id);
7189 var_nonlocal->is_artificial_var = 1;
7190 var_nonlocal->offset = 0;
7191 var_nonlocal->size = ~0;
7192 var_nonlocal->fullsize = ~0;
7193 var_nonlocal->is_special_var = 1;
7195 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
7196 lhs.type = SCALAR;
7197 lhs.var = escaped_id;
7198 lhs.offset = 0;
7199 rhs.type = DEREF;
7200 rhs.var = escaped_id;
7201 rhs.offset = 0;
7202 process_constraint (new_constraint (lhs, rhs));
7204 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
7205 whole variable escapes. */
7206 lhs.type = SCALAR;
7207 lhs.var = escaped_id;
7208 lhs.offset = 0;
7209 rhs.type = SCALAR;
7210 rhs.var = escaped_id;
7211 rhs.offset = UNKNOWN_OFFSET;
7212 process_constraint (new_constraint (lhs, rhs));
7214 /* *ESCAPED = NONLOCAL. This is true because we have to assume
7215 everything pointed to by escaped points to what global memory can
7216 point to. */
7217 lhs.type = DEREF;
7218 lhs.var = escaped_id;
7219 lhs.offset = 0;
7220 rhs.type = SCALAR;
7221 rhs.var = nonlocal_id;
7222 rhs.offset = 0;
7223 process_constraint (new_constraint (lhs, rhs));
7225 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
7226 global memory may point to global memory and escaped memory. */
7227 lhs.type = SCALAR;
7228 lhs.var = nonlocal_id;
7229 lhs.offset = 0;
7230 rhs.type = ADDRESSOF;
7231 rhs.var = nonlocal_id;
7232 rhs.offset = 0;
7233 process_constraint (new_constraint (lhs, rhs));
7234 rhs.type = ADDRESSOF;
7235 rhs.var = escaped_id;
7236 rhs.offset = 0;
7237 process_constraint (new_constraint (lhs, rhs));
7239 /* Create the STOREDANYTHING variable, used to represent the set of
7240 variables stored to *ANYTHING. */
7241 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING", false);
7242 gcc_assert (var_storedanything->id == storedanything_id);
7243 var_storedanything->is_artificial_var = 1;
7244 var_storedanything->offset = 0;
7245 var_storedanything->size = ~0;
7246 var_storedanything->fullsize = ~0;
7247 var_storedanything->is_special_var = 0;
7249 /* Create the INTEGER variable, used to represent that a variable points
7250 to what an INTEGER "points to". */
7251 var_integer = new_var_info (NULL_TREE, "INTEGER", false);
7252 gcc_assert (var_integer->id == integer_id);
7253 var_integer->is_artificial_var = 1;
7254 var_integer->size = ~0;
7255 var_integer->fullsize = ~0;
7256 var_integer->offset = 0;
7257 var_integer->is_special_var = 1;
7259 /* INTEGER = ANYTHING, because we don't know where a dereference of
7260 a random integer will point to. */
7261 lhs.type = SCALAR;
7262 lhs.var = integer_id;
7263 lhs.offset = 0;
7264 rhs.type = ADDRESSOF;
7265 rhs.var = anything_id;
7266 rhs.offset = 0;
7267 process_constraint (new_constraint (lhs, rhs));
7270 /* Initialize things necessary to perform PTA */
7272 static void
7273 init_alias_vars (void)
7275 use_field_sensitive = (param_max_fields_for_field_sensitive > 1);
7277 bitmap_obstack_initialize (&pta_obstack);
7278 bitmap_obstack_initialize (&oldpta_obstack);
7279 bitmap_obstack_initialize (&predbitmap_obstack);
7281 constraints.create (8);
7282 varmap.create (8);
7283 vi_for_tree = new hash_map<tree, varinfo_t>;
7284 call_stmt_vars = new hash_map<gimple *, varinfo_t>;
7286 memset (&stats, 0, sizeof (stats));
7287 shared_bitmap_table = new hash_table<shared_bitmap_hasher> (511);
7288 init_base_vars ();
7290 gcc_obstack_init (&fake_var_decl_obstack);
7292 final_solutions = new hash_map<varinfo_t, pt_solution *>;
7293 gcc_obstack_init (&final_solutions_obstack);
7296 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
7297 predecessor edges. */
7299 static void
7300 remove_preds_and_fake_succs (constraint_graph_t graph)
7302 unsigned int i;
7304 /* Clear the implicit ref and address nodes from the successor
7305 lists. */
7306 for (i = 1; i < FIRST_REF_NODE; i++)
7308 if (graph->succs[i])
7309 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
7310 FIRST_REF_NODE * 2);
7313 /* Free the successor list for the non-ref nodes. */
7314 for (i = FIRST_REF_NODE + 1; i < graph->size; i++)
7316 if (graph->succs[i])
7317 BITMAP_FREE (graph->succs[i]);
7320 /* Now reallocate the size of the successor list as, and blow away
7321 the predecessor bitmaps. */
7322 graph->size = varmap.length ();
7323 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
7325 free (graph->implicit_preds);
7326 graph->implicit_preds = NULL;
7327 free (graph->preds);
7328 graph->preds = NULL;
7329 bitmap_obstack_release (&predbitmap_obstack);
7332 /* Solve the constraint set. */
7334 static void
7335 solve_constraints (void)
7337 class scc_info *si;
7339 /* Sort varinfos so that ones that cannot be pointed to are last.
7340 This makes bitmaps more efficient. */
7341 unsigned int *map = XNEWVEC (unsigned int, varmap.length ());
7342 for (unsigned i = 0; i < integer_id + 1; ++i)
7343 map[i] = i;
7344 /* Start with address-taken vars, followed by not address-taken vars
7345 to move vars never appearing in the points-to solution bitmaps last. */
7346 unsigned j = integer_id + 1;
7347 for (unsigned i = integer_id + 1; i < varmap.length (); ++i)
7348 if (varmap[varmap[i]->head]->address_taken)
7349 map[i] = j++;
7350 for (unsigned i = integer_id + 1; i < varmap.length (); ++i)
7351 if (! varmap[varmap[i]->head]->address_taken)
7352 map[i] = j++;
7353 /* Shuffle varmap according to map. */
7354 for (unsigned i = integer_id + 1; i < varmap.length (); ++i)
7356 while (map[varmap[i]->id] != i)
7357 std::swap (varmap[i], varmap[map[varmap[i]->id]]);
7358 gcc_assert (bitmap_empty_p (varmap[i]->solution));
7359 varmap[i]->id = i;
7360 varmap[i]->next = map[varmap[i]->next];
7361 varmap[i]->head = map[varmap[i]->head];
7363 /* Finally rewrite constraints. */
7364 for (unsigned i = 0; i < constraints.length (); ++i)
7366 constraints[i]->lhs.var = map[constraints[i]->lhs.var];
7367 constraints[i]->rhs.var = map[constraints[i]->rhs.var];
7369 free (map);
7371 if (dump_file)
7372 fprintf (dump_file,
7373 "\nCollapsing static cycles and doing variable "
7374 "substitution\n");
7376 init_graph (varmap.length () * 2);
7378 if (dump_file)
7379 fprintf (dump_file, "Building predecessor graph\n");
7380 build_pred_graph ();
7382 if (dump_file)
7383 fprintf (dump_file, "Detecting pointer and location "
7384 "equivalences\n");
7385 si = perform_var_substitution (graph);
7387 if (dump_file)
7388 fprintf (dump_file, "Rewriting constraints and unifying "
7389 "variables\n");
7390 rewrite_constraints (graph, si);
7392 build_succ_graph ();
7394 free_var_substitution_info (si);
7396 /* Attach complex constraints to graph nodes. */
7397 move_complex_constraints (graph);
7399 if (dump_file)
7400 fprintf (dump_file, "Uniting pointer but not location equivalent "
7401 "variables\n");
7402 unite_pointer_equivalences (graph);
7404 if (dump_file)
7405 fprintf (dump_file, "Finding indirect cycles\n");
7406 find_indirect_cycles (graph);
7408 /* Implicit nodes and predecessors are no longer necessary at this
7409 point. */
7410 remove_preds_and_fake_succs (graph);
7412 if (dump_file && (dump_flags & TDF_GRAPH))
7414 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
7415 "in dot format:\n");
7416 dump_constraint_graph (dump_file);
7417 fprintf (dump_file, "\n\n");
7420 if (dump_file)
7421 fprintf (dump_file, "Solving graph\n");
7423 solve_graph (graph);
7425 if (dump_file && (dump_flags & TDF_GRAPH))
7427 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
7428 "in dot format:\n");
7429 dump_constraint_graph (dump_file);
7430 fprintf (dump_file, "\n\n");
7434 /* Create points-to sets for the current function. See the comments
7435 at the start of the file for an algorithmic overview. */
7437 static void
7438 compute_points_to_sets (void)
7440 basic_block bb;
7441 varinfo_t vi;
7443 timevar_push (TV_TREE_PTA);
7445 init_alias_vars ();
7447 intra_create_variable_infos (cfun);
7449 /* Now walk all statements and build the constraint set. */
7450 FOR_EACH_BB_FN (bb, cfun)
7452 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7453 gsi_next (&gsi))
7455 gphi *phi = gsi.phi ();
7457 if (! virtual_operand_p (gimple_phi_result (phi)))
7458 find_func_aliases (cfun, phi);
7461 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
7462 gsi_next (&gsi))
7464 gimple *stmt = gsi_stmt (gsi);
7466 find_func_aliases (cfun, stmt);
7470 if (dump_file)
7472 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
7473 dump_constraints (dump_file, 0);
7476 /* From the constraints compute the points-to sets. */
7477 solve_constraints ();
7479 /* Post-process solutions for escapes through returns. */
7480 edge_iterator ei;
7481 edge e;
7482 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
7483 if (greturn *ret = safe_dyn_cast <greturn *> (last_stmt (e->src)))
7485 tree val = gimple_return_retval (ret);
7486 /* ??? Easy to handle simple indirections with some work.
7487 Arbitrary references like foo.bar.baz are more difficult
7488 (but conservatively easy enough with just looking at the base).
7489 Mind to fixup find_func_aliases as well. */
7490 if (!val || !SSA_VAR_P (val))
7491 continue;
7492 /* returns happen last in non-IPA so they only influence
7493 the ESCAPED solution and we can filter local variables. */
7494 varinfo_t escaped_vi = get_varinfo (find (escaped_id));
7495 varinfo_t vi = lookup_vi_for_tree (val);
7496 bitmap delta = BITMAP_ALLOC (&pta_obstack);
7497 bitmap_iterator bi;
7498 unsigned i;
7499 for (; vi; vi = vi_next (vi))
7501 varinfo_t part_vi = get_varinfo (find (vi->id));
7502 EXECUTE_IF_AND_COMPL_IN_BITMAP (part_vi->solution,
7503 escaped_vi->solution, 0, i, bi)
7505 varinfo_t pointed_to_vi = get_varinfo (i);
7506 if (pointed_to_vi->is_global_var
7507 /* We delay marking of heap memory as global. */
7508 || pointed_to_vi->is_heap_var)
7509 bitmap_set_bit (delta, i);
7513 /* Now compute the transitive closure. */
7514 bitmap_ior_into (escaped_vi->solution, delta);
7515 bitmap new_delta = BITMAP_ALLOC (&pta_obstack);
7516 while (!bitmap_empty_p (delta))
7518 EXECUTE_IF_SET_IN_BITMAP (delta, 0, i, bi)
7520 varinfo_t pointed_to_vi = get_varinfo (i);
7521 pointed_to_vi = get_varinfo (find (pointed_to_vi->id));
7522 unsigned j;
7523 bitmap_iterator bi2;
7524 EXECUTE_IF_AND_COMPL_IN_BITMAP (pointed_to_vi->solution,
7525 escaped_vi->solution,
7526 0, j, bi2)
7528 varinfo_t pointed_to_vi2 = get_varinfo (j);
7529 if (pointed_to_vi2->is_global_var
7530 /* We delay marking of heap memory as global. */
7531 || pointed_to_vi2->is_heap_var)
7532 bitmap_set_bit (new_delta, j);
7535 bitmap_ior_into (escaped_vi->solution, new_delta);
7536 bitmap_clear (delta);
7537 std::swap (delta, new_delta);
7539 BITMAP_FREE (delta);
7540 BITMAP_FREE (new_delta);
7543 if (dump_file)
7544 dump_sa_points_to_info (dump_file);
7546 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
7547 cfun->gimple_df->escaped = find_what_var_points_to (cfun->decl,
7548 get_varinfo (escaped_id));
7550 /* Make sure the ESCAPED solution (which is used as placeholder in
7551 other solutions) does not reference itself. This simplifies
7552 points-to solution queries. */
7553 cfun->gimple_df->escaped.escaped = 0;
7555 /* Compute the points-to sets for pointer SSA_NAMEs. */
7556 unsigned i;
7557 tree ptr;
7559 FOR_EACH_SSA_NAME (i, ptr, cfun)
7561 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
7562 find_what_p_points_to (cfun->decl, ptr);
7565 /* Compute the call-used/clobbered sets. */
7566 FOR_EACH_BB_FN (bb, cfun)
7568 gimple_stmt_iterator gsi;
7570 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7572 gcall *stmt;
7573 struct pt_solution *pt;
7575 stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
7576 if (!stmt)
7577 continue;
7579 pt = gimple_call_use_set (stmt);
7580 if (gimple_call_flags (stmt) & ECF_CONST)
7581 memset (pt, 0, sizeof (struct pt_solution));
7582 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7584 *pt = find_what_var_points_to (cfun->decl, vi);
7585 /* Escaped (and thus nonlocal) variables are always
7586 implicitly used by calls. */
7587 /* ??? ESCAPED can be empty even though NONLOCAL
7588 always escaped. */
7589 pt->nonlocal = 1;
7590 pt->escaped = 1;
7592 else
7594 /* If there is nothing special about this call then
7595 we have made everything that is used also escape. */
7596 *pt = cfun->gimple_df->escaped;
7597 pt->nonlocal = 1;
7600 pt = gimple_call_clobber_set (stmt);
7601 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7602 memset (pt, 0, sizeof (struct pt_solution));
7603 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7605 *pt = find_what_var_points_to (cfun->decl, vi);
7606 /* Escaped (and thus nonlocal) variables are always
7607 implicitly clobbered by calls. */
7608 /* ??? ESCAPED can be empty even though NONLOCAL
7609 always escaped. */
7610 pt->nonlocal = 1;
7611 pt->escaped = 1;
7613 else
7615 /* If there is nothing special about this call then
7616 we have made everything that is used also escape. */
7617 *pt = cfun->gimple_df->escaped;
7618 pt->nonlocal = 1;
7623 timevar_pop (TV_TREE_PTA);
7627 /* Delete created points-to sets. */
7629 static void
7630 delete_points_to_sets (void)
7632 unsigned int i;
7634 delete shared_bitmap_table;
7635 shared_bitmap_table = NULL;
7636 if (dump_file && (dump_flags & TDF_STATS))
7637 fprintf (dump_file, "Points to sets created:%d\n",
7638 stats.points_to_sets_created);
7640 delete vi_for_tree;
7641 delete call_stmt_vars;
7642 bitmap_obstack_release (&pta_obstack);
7643 constraints.release ();
7645 for (i = 0; i < graph->size; i++)
7646 graph->complex[i].release ();
7647 free (graph->complex);
7649 free (graph->rep);
7650 free (graph->succs);
7651 free (graph->pe);
7652 free (graph->pe_rep);
7653 free (graph->indirect_cycles);
7654 free (graph);
7656 varmap.release ();
7657 variable_info_pool.release ();
7658 constraint_pool.release ();
7660 obstack_free (&fake_var_decl_obstack, NULL);
7662 delete final_solutions;
7663 obstack_free (&final_solutions_obstack, NULL);
7666 struct vls_data
7668 unsigned short clique;
7669 bool escaped_p;
7670 bitmap rvars;
7673 /* Mark "other" loads and stores as belonging to CLIQUE and with
7674 base zero. */
7676 static bool
7677 visit_loadstore (gimple *, tree base, tree ref, void *data)
7679 unsigned short clique = ((vls_data *) data)->clique;
7680 bitmap rvars = ((vls_data *) data)->rvars;
7681 bool escaped_p = ((vls_data *) data)->escaped_p;
7682 if (TREE_CODE (base) == MEM_REF
7683 || TREE_CODE (base) == TARGET_MEM_REF)
7685 tree ptr = TREE_OPERAND (base, 0);
7686 if (TREE_CODE (ptr) == SSA_NAME)
7688 /* For parameters, get at the points-to set for the actual parm
7689 decl. */
7690 if (SSA_NAME_IS_DEFAULT_DEF (ptr)
7691 && (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
7692 || TREE_CODE (SSA_NAME_VAR (ptr)) == RESULT_DECL))
7693 ptr = SSA_NAME_VAR (ptr);
7695 /* We need to make sure 'ptr' doesn't include any of
7696 the restrict tags we added bases for in its points-to set. */
7697 varinfo_t vi = lookup_vi_for_tree (ptr);
7698 if (! vi)
7699 return false;
7701 vi = get_varinfo (find (vi->id));
7702 if (bitmap_intersect_p (rvars, vi->solution)
7703 || (escaped_p && bitmap_bit_p (vi->solution, escaped_id)))
7704 return false;
7707 /* Do not overwrite existing cliques (that includes clique, base
7708 pairs we just set). */
7709 if (MR_DEPENDENCE_CLIQUE (base) == 0)
7711 MR_DEPENDENCE_CLIQUE (base) = clique;
7712 MR_DEPENDENCE_BASE (base) = 0;
7716 /* For plain decl accesses see whether they are accesses to globals
7717 and rewrite them to MEM_REFs with { clique, 0 }. */
7718 if (VAR_P (base)
7719 && is_global_var (base)
7720 /* ??? We can't rewrite a plain decl with the walk_stmt_load_store
7721 ops callback. */
7722 && base != ref)
7724 tree *basep = &ref;
7725 while (handled_component_p (*basep))
7726 basep = &TREE_OPERAND (*basep, 0);
7727 gcc_assert (VAR_P (*basep));
7728 tree ptr = build_fold_addr_expr (*basep);
7729 tree zero = build_int_cst (TREE_TYPE (ptr), 0);
7730 *basep = build2 (MEM_REF, TREE_TYPE (*basep), ptr, zero);
7731 MR_DEPENDENCE_CLIQUE (*basep) = clique;
7732 MR_DEPENDENCE_BASE (*basep) = 0;
7735 return false;
7738 struct msdi_data {
7739 tree ptr;
7740 unsigned short *clique;
7741 unsigned short *last_ruid;
7742 varinfo_t restrict_var;
7745 /* If BASE is a MEM_REF then assign a clique, base pair to it, updating
7746 CLIQUE, *RESTRICT_VAR and LAST_RUID as passed via DATA.
7747 Return whether dependence info was assigned to BASE. */
7749 static bool
7750 maybe_set_dependence_info (gimple *, tree base, tree, void *data)
7752 tree ptr = ((msdi_data *)data)->ptr;
7753 unsigned short &clique = *((msdi_data *)data)->clique;
7754 unsigned short &last_ruid = *((msdi_data *)data)->last_ruid;
7755 varinfo_t restrict_var = ((msdi_data *)data)->restrict_var;
7756 if ((TREE_CODE (base) == MEM_REF
7757 || TREE_CODE (base) == TARGET_MEM_REF)
7758 && TREE_OPERAND (base, 0) == ptr)
7760 /* Do not overwrite existing cliques. This avoids overwriting dependence
7761 info inlined from a function with restrict parameters inlined
7762 into a function with restrict parameters. This usually means we
7763 prefer to be precise in innermost loops. */
7764 if (MR_DEPENDENCE_CLIQUE (base) == 0)
7766 if (clique == 0)
7768 if (cfun->last_clique == 0)
7769 cfun->last_clique = 1;
7770 clique = 1;
7772 if (restrict_var->ruid == 0)
7773 restrict_var->ruid = ++last_ruid;
7774 MR_DEPENDENCE_CLIQUE (base) = clique;
7775 MR_DEPENDENCE_BASE (base) = restrict_var->ruid;
7776 return true;
7779 return false;
7782 /* Clear dependence info for the clique DATA. */
7784 static bool
7785 clear_dependence_clique (gimple *, tree base, tree, void *data)
7787 unsigned short clique = (uintptr_t)data;
7788 if ((TREE_CODE (base) == MEM_REF
7789 || TREE_CODE (base) == TARGET_MEM_REF)
7790 && MR_DEPENDENCE_CLIQUE (base) == clique)
7792 MR_DEPENDENCE_CLIQUE (base) = 0;
7793 MR_DEPENDENCE_BASE (base) = 0;
7796 return false;
7799 /* Compute the set of independend memory references based on restrict
7800 tags and their conservative propagation to the points-to sets. */
7802 static void
7803 compute_dependence_clique (void)
7805 /* First clear the special "local" clique. */
7806 basic_block bb;
7807 if (cfun->last_clique != 0)
7808 FOR_EACH_BB_FN (bb, cfun)
7809 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
7810 !gsi_end_p (gsi); gsi_next (&gsi))
7812 gimple *stmt = gsi_stmt (gsi);
7813 walk_stmt_load_store_ops (stmt, (void *)(uintptr_t) 1,
7814 clear_dependence_clique,
7815 clear_dependence_clique);
7818 unsigned short clique = 0;
7819 unsigned short last_ruid = 0;
7820 bitmap rvars = BITMAP_ALLOC (NULL);
7821 bool escaped_p = false;
7822 for (unsigned i = 0; i < num_ssa_names; ++i)
7824 tree ptr = ssa_name (i);
7825 if (!ptr || !POINTER_TYPE_P (TREE_TYPE (ptr)))
7826 continue;
7828 /* Avoid all this when ptr is not dereferenced? */
7829 tree p = ptr;
7830 if (SSA_NAME_IS_DEFAULT_DEF (ptr)
7831 && (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
7832 || TREE_CODE (SSA_NAME_VAR (ptr)) == RESULT_DECL))
7833 p = SSA_NAME_VAR (ptr);
7834 varinfo_t vi = lookup_vi_for_tree (p);
7835 if (!vi)
7836 continue;
7837 vi = get_varinfo (find (vi->id));
7838 bitmap_iterator bi;
7839 unsigned j;
7840 varinfo_t restrict_var = NULL;
7841 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, j, bi)
7843 varinfo_t oi = get_varinfo (j);
7844 if (oi->head != j)
7845 oi = get_varinfo (oi->head);
7846 if (oi->is_restrict_var)
7848 if (restrict_var
7849 && restrict_var != oi)
7851 if (dump_file && (dump_flags & TDF_DETAILS))
7853 fprintf (dump_file, "found restrict pointed-to "
7854 "for ");
7855 print_generic_expr (dump_file, ptr);
7856 fprintf (dump_file, " but not exclusively\n");
7858 restrict_var = NULL;
7859 break;
7861 restrict_var = oi;
7863 /* NULL is the only other valid points-to entry. */
7864 else if (oi->id != nothing_id)
7866 restrict_var = NULL;
7867 break;
7870 /* Ok, found that ptr must(!) point to a single(!) restrict
7871 variable. */
7872 /* ??? PTA isn't really a proper propagation engine to compute
7873 this property.
7874 ??? We could handle merging of two restricts by unifying them. */
7875 if (restrict_var)
7877 /* Now look at possible dereferences of ptr. */
7878 imm_use_iterator ui;
7879 gimple *use_stmt;
7880 bool used = false;
7881 msdi_data data = { ptr, &clique, &last_ruid, restrict_var };
7882 FOR_EACH_IMM_USE_STMT (use_stmt, ui, ptr)
7883 used |= walk_stmt_load_store_ops (use_stmt, &data,
7884 maybe_set_dependence_info,
7885 maybe_set_dependence_info);
7886 if (used)
7888 /* Add all subvars to the set of restrict pointed-to set. */
7889 for (unsigned sv = restrict_var->head; sv != 0;
7890 sv = get_varinfo (sv)->next)
7891 bitmap_set_bit (rvars, sv);
7892 varinfo_t escaped = get_varinfo (find (escaped_id));
7893 if (bitmap_bit_p (escaped->solution, restrict_var->id))
7894 escaped_p = true;
7899 if (clique != 0)
7901 /* Assign the BASE id zero to all accesses not based on a restrict
7902 pointer. That way they get disambiguated against restrict
7903 accesses but not against each other. */
7904 /* ??? For restricts derived from globals (thus not incoming
7905 parameters) we can't restrict scoping properly thus the following
7906 is too aggressive there. For now we have excluded those globals from
7907 getting into the MR_DEPENDENCE machinery. */
7908 vls_data data = { clique, escaped_p, rvars };
7909 basic_block bb;
7910 FOR_EACH_BB_FN (bb, cfun)
7911 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
7912 !gsi_end_p (gsi); gsi_next (&gsi))
7914 gimple *stmt = gsi_stmt (gsi);
7915 walk_stmt_load_store_ops (stmt, &data,
7916 visit_loadstore, visit_loadstore);
7920 BITMAP_FREE (rvars);
7923 /* Compute points-to information for every SSA_NAME pointer in the
7924 current function and compute the transitive closure of escaped
7925 variables to re-initialize the call-clobber states of local variables. */
7927 unsigned int
7928 compute_may_aliases (void)
7930 if (cfun->gimple_df->ipa_pta)
7932 if (dump_file)
7934 fprintf (dump_file, "\nNot re-computing points-to information "
7935 "because IPA points-to information is available.\n\n");
7937 /* But still dump what we have remaining it. */
7938 dump_alias_info (dump_file);
7941 return 0;
7944 /* For each pointer P_i, determine the sets of variables that P_i may
7945 point-to. Compute the reachability set of escaped and call-used
7946 variables. */
7947 compute_points_to_sets ();
7949 /* Debugging dumps. */
7950 if (dump_file)
7951 dump_alias_info (dump_file);
7953 /* Compute restrict-based memory disambiguations. */
7954 compute_dependence_clique ();
7956 /* Deallocate memory used by aliasing data structures and the internal
7957 points-to solution. */
7958 delete_points_to_sets ();
7960 gcc_assert (!need_ssa_update_p (cfun));
7962 return 0;
7965 /* A dummy pass to cause points-to information to be computed via
7966 TODO_rebuild_alias. */
7968 namespace {
7970 const pass_data pass_data_build_alias =
7972 GIMPLE_PASS, /* type */
7973 "alias", /* name */
7974 OPTGROUP_NONE, /* optinfo_flags */
7975 TV_NONE, /* tv_id */
7976 ( PROP_cfg | PROP_ssa ), /* properties_required */
7977 0, /* properties_provided */
7978 0, /* properties_destroyed */
7979 0, /* todo_flags_start */
7980 TODO_rebuild_alias, /* todo_flags_finish */
7983 class pass_build_alias : public gimple_opt_pass
7985 public:
7986 pass_build_alias (gcc::context *ctxt)
7987 : gimple_opt_pass (pass_data_build_alias, ctxt)
7990 /* opt_pass methods: */
7991 virtual bool gate (function *) { return flag_tree_pta; }
7993 }; // class pass_build_alias
7995 } // anon namespace
7997 gimple_opt_pass *
7998 make_pass_build_alias (gcc::context *ctxt)
8000 return new pass_build_alias (ctxt);
8003 /* A dummy pass to cause points-to information to be computed via
8004 TODO_rebuild_alias. */
8006 namespace {
8008 const pass_data pass_data_build_ealias =
8010 GIMPLE_PASS, /* type */
8011 "ealias", /* name */
8012 OPTGROUP_NONE, /* optinfo_flags */
8013 TV_NONE, /* tv_id */
8014 ( PROP_cfg | PROP_ssa ), /* properties_required */
8015 0, /* properties_provided */
8016 0, /* properties_destroyed */
8017 0, /* todo_flags_start */
8018 TODO_rebuild_alias, /* todo_flags_finish */
8021 class pass_build_ealias : public gimple_opt_pass
8023 public:
8024 pass_build_ealias (gcc::context *ctxt)
8025 : gimple_opt_pass (pass_data_build_ealias, ctxt)
8028 /* opt_pass methods: */
8029 virtual bool gate (function *) { return flag_tree_pta; }
8031 }; // class pass_build_ealias
8033 } // anon namespace
8035 gimple_opt_pass *
8036 make_pass_build_ealias (gcc::context *ctxt)
8038 return new pass_build_ealias (ctxt);
8042 /* IPA PTA solutions for ESCAPED. */
8043 struct pt_solution ipa_escaped_pt
8044 = { true, false, false, false, false,
8045 false, false, false, false, false, NULL };
8047 /* Associate node with varinfo DATA. Worker for
8048 cgraph_for_symbol_thunks_and_aliases. */
8049 static bool
8050 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
8052 if ((node->alias
8053 || (node->thunk
8054 && ! node->inlined_to))
8055 && node->analyzed
8056 && !node->ifunc_resolver)
8057 insert_vi_for_tree (node->decl, (varinfo_t)data);
8058 return false;
8061 /* Dump varinfo VI to FILE. */
8063 static void
8064 dump_varinfo (FILE *file, varinfo_t vi)
8066 if (vi == NULL)
8067 return;
8069 fprintf (file, "%u: %s\n", vi->id, vi->name);
8071 const char *sep = " ";
8072 if (vi->is_artificial_var)
8073 fprintf (file, "%sartificial", sep);
8074 if (vi->is_special_var)
8075 fprintf (file, "%sspecial", sep);
8076 if (vi->is_unknown_size_var)
8077 fprintf (file, "%sunknown-size", sep);
8078 if (vi->is_full_var)
8079 fprintf (file, "%sfull", sep);
8080 if (vi->is_heap_var)
8081 fprintf (file, "%sheap", sep);
8082 if (vi->may_have_pointers)
8083 fprintf (file, "%smay-have-pointers", sep);
8084 if (vi->only_restrict_pointers)
8085 fprintf (file, "%sonly-restrict-pointers", sep);
8086 if (vi->is_restrict_var)
8087 fprintf (file, "%sis-restrict-var", sep);
8088 if (vi->is_global_var)
8089 fprintf (file, "%sglobal", sep);
8090 if (vi->is_ipa_escape_point)
8091 fprintf (file, "%sipa-escape-point", sep);
8092 if (vi->is_fn_info)
8093 fprintf (file, "%sfn-info", sep);
8094 if (vi->ruid)
8095 fprintf (file, "%srestrict-uid:%u", sep, vi->ruid);
8096 if (vi->next)
8097 fprintf (file, "%snext:%u", sep, vi->next);
8098 if (vi->head != vi->id)
8099 fprintf (file, "%shead:%u", sep, vi->head);
8100 if (vi->offset)
8101 fprintf (file, "%soffset:" HOST_WIDE_INT_PRINT_DEC, sep, vi->offset);
8102 if (vi->size != ~(unsigned HOST_WIDE_INT)0)
8103 fprintf (file, "%ssize:" HOST_WIDE_INT_PRINT_DEC, sep, vi->size);
8104 if (vi->fullsize != ~(unsigned HOST_WIDE_INT)0
8105 && vi->fullsize != vi->size)
8106 fprintf (file, "%sfullsize:" HOST_WIDE_INT_PRINT_DEC, sep,
8107 vi->fullsize);
8108 fprintf (file, "\n");
8110 if (vi->solution && !bitmap_empty_p (vi->solution))
8112 bitmap_iterator bi;
8113 unsigned i;
8114 fprintf (file, " solution: {");
8115 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
8116 fprintf (file, " %u", i);
8117 fprintf (file, " }\n");
8120 if (vi->oldsolution && !bitmap_empty_p (vi->oldsolution)
8121 && !bitmap_equal_p (vi->solution, vi->oldsolution))
8123 bitmap_iterator bi;
8124 unsigned i;
8125 fprintf (file, " oldsolution: {");
8126 EXECUTE_IF_SET_IN_BITMAP (vi->oldsolution, 0, i, bi)
8127 fprintf (file, " %u", i);
8128 fprintf (file, " }\n");
8132 /* Dump varinfo VI to stderr. */
8134 DEBUG_FUNCTION void
8135 debug_varinfo (varinfo_t vi)
8137 dump_varinfo (stderr, vi);
8140 /* Dump varmap to FILE. */
8142 static void
8143 dump_varmap (FILE *file)
8145 if (varmap.length () == 0)
8146 return;
8148 fprintf (file, "variables:\n");
8150 for (unsigned int i = 0; i < varmap.length (); ++i)
8152 varinfo_t vi = get_varinfo (i);
8153 dump_varinfo (file, vi);
8156 fprintf (file, "\n");
8159 /* Dump varmap to stderr. */
8161 DEBUG_FUNCTION void
8162 debug_varmap (void)
8164 dump_varmap (stderr);
8167 /* Compute whether node is refered to non-locally. Worker for
8168 cgraph_for_symbol_thunks_and_aliases. */
8169 static bool
8170 refered_from_nonlocal_fn (struct cgraph_node *node, void *data)
8172 bool *nonlocal_p = (bool *)data;
8173 *nonlocal_p |= (node->used_from_other_partition
8174 || DECL_EXTERNAL (node->decl)
8175 || TREE_PUBLIC (node->decl)
8176 || node->force_output
8177 || lookup_attribute ("noipa", DECL_ATTRIBUTES (node->decl)));
8178 return false;
8181 /* Same for varpool nodes. */
8182 static bool
8183 refered_from_nonlocal_var (struct varpool_node *node, void *data)
8185 bool *nonlocal_p = (bool *)data;
8186 *nonlocal_p |= (node->used_from_other_partition
8187 || DECL_EXTERNAL (node->decl)
8188 || TREE_PUBLIC (node->decl)
8189 || node->force_output);
8190 return false;
8193 /* Execute the driver for IPA PTA. */
8194 static unsigned int
8195 ipa_pta_execute (void)
8197 struct cgraph_node *node;
8198 varpool_node *var;
8199 unsigned int from = 0;
8201 in_ipa_mode = 1;
8203 init_alias_vars ();
8205 if (dump_file && (dump_flags & TDF_DETAILS))
8207 symtab->dump (dump_file);
8208 fprintf (dump_file, "\n");
8211 if (dump_file)
8213 fprintf (dump_file, "Generating generic constraints\n\n");
8214 dump_constraints (dump_file, from);
8215 fprintf (dump_file, "\n");
8216 from = constraints.length ();
8219 /* Build the constraints. */
8220 FOR_EACH_DEFINED_FUNCTION (node)
8222 varinfo_t vi;
8223 /* Nodes without a body are not interesting. Especially do not
8224 visit clones at this point for now - we get duplicate decls
8225 there for inline clones at least. */
8226 if (!node->has_gimple_body_p () || node->inlined_to)
8227 continue;
8228 node->get_body ();
8230 gcc_assert (!node->clone_of);
8232 /* For externally visible or attribute used annotated functions use
8233 local constraints for their arguments.
8234 For local functions we see all callers and thus do not need initial
8235 constraints for parameters. */
8236 bool nonlocal_p = (node->used_from_other_partition
8237 || DECL_EXTERNAL (node->decl)
8238 || TREE_PUBLIC (node->decl)
8239 || node->force_output
8240 || lookup_attribute ("noipa",
8241 DECL_ATTRIBUTES (node->decl)));
8242 node->call_for_symbol_thunks_and_aliases (refered_from_nonlocal_fn,
8243 &nonlocal_p, true);
8245 vi = create_function_info_for (node->decl,
8246 alias_get_name (node->decl), false,
8247 nonlocal_p);
8248 if (dump_file
8249 && from != constraints.length ())
8251 fprintf (dump_file,
8252 "Generating initial constraints for %s",
8253 node->dump_name ());
8254 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
8255 fprintf (dump_file, " (%s)",
8256 IDENTIFIER_POINTER
8257 (DECL_ASSEMBLER_NAME (node->decl)));
8258 fprintf (dump_file, "\n\n");
8259 dump_constraints (dump_file, from);
8260 fprintf (dump_file, "\n");
8262 from = constraints.length ();
8265 node->call_for_symbol_thunks_and_aliases
8266 (associate_varinfo_to_alias, vi, true);
8269 /* Create constraints for global variables and their initializers. */
8270 FOR_EACH_VARIABLE (var)
8272 if (var->alias && var->analyzed)
8273 continue;
8275 varinfo_t vi = get_vi_for_tree (var->decl);
8277 /* For the purpose of IPA PTA unit-local globals are not
8278 escape points. */
8279 bool nonlocal_p = (DECL_EXTERNAL (var->decl)
8280 || TREE_PUBLIC (var->decl)
8281 || var->used_from_other_partition
8282 || var->force_output);
8283 var->call_for_symbol_and_aliases (refered_from_nonlocal_var,
8284 &nonlocal_p, true);
8285 if (nonlocal_p)
8286 vi->is_ipa_escape_point = true;
8289 if (dump_file
8290 && from != constraints.length ())
8292 fprintf (dump_file,
8293 "Generating constraints for global initializers\n\n");
8294 dump_constraints (dump_file, from);
8295 fprintf (dump_file, "\n");
8296 from = constraints.length ();
8299 FOR_EACH_DEFINED_FUNCTION (node)
8301 struct function *func;
8302 basic_block bb;
8304 /* Nodes without a body are not interesting. */
8305 if (!node->has_gimple_body_p () || node->clone_of)
8306 continue;
8308 if (dump_file)
8310 fprintf (dump_file,
8311 "Generating constraints for %s", node->dump_name ());
8312 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
8313 fprintf (dump_file, " (%s)",
8314 IDENTIFIER_POINTER
8315 (DECL_ASSEMBLER_NAME (node->decl)));
8316 fprintf (dump_file, "\n");
8319 func = DECL_STRUCT_FUNCTION (node->decl);
8320 gcc_assert (cfun == NULL);
8322 /* Build constriants for the function body. */
8323 FOR_EACH_BB_FN (bb, func)
8325 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
8326 gsi_next (&gsi))
8328 gphi *phi = gsi.phi ();
8330 if (! virtual_operand_p (gimple_phi_result (phi)))
8331 find_func_aliases (func, phi);
8334 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
8335 gsi_next (&gsi))
8337 gimple *stmt = gsi_stmt (gsi);
8339 find_func_aliases (func, stmt);
8340 find_func_clobbers (func, stmt);
8344 if (dump_file)
8346 fprintf (dump_file, "\n");
8347 dump_constraints (dump_file, from);
8348 fprintf (dump_file, "\n");
8349 from = constraints.length ();
8353 /* From the constraints compute the points-to sets. */
8354 solve_constraints ();
8356 if (dump_file)
8357 dump_sa_points_to_info (dump_file);
8359 /* Now post-process solutions to handle locals from different
8360 runtime instantiations coming in through recursive invocations. */
8361 unsigned shadow_var_cnt = 0;
8362 for (unsigned i = 1; i < varmap.length (); ++i)
8364 varinfo_t fi = get_varinfo (i);
8365 if (fi->is_fn_info
8366 && fi->decl)
8367 /* Automatic variables pointed to by their containing functions
8368 parameters need this treatment. */
8369 for (varinfo_t ai = first_vi_for_offset (fi, fi_parm_base);
8370 ai; ai = vi_next (ai))
8372 varinfo_t vi = get_varinfo (find (ai->id));
8373 bitmap_iterator bi;
8374 unsigned j;
8375 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, j, bi)
8377 varinfo_t pt = get_varinfo (j);
8378 if (pt->shadow_var_uid == 0
8379 && pt->decl
8380 && auto_var_in_fn_p (pt->decl, fi->decl))
8382 pt->shadow_var_uid = allocate_decl_uid ();
8383 shadow_var_cnt++;
8387 /* As well as global variables which are another way of passing
8388 arguments to recursive invocations. */
8389 else if (fi->is_global_var)
8391 for (varinfo_t ai = fi; ai; ai = vi_next (ai))
8393 varinfo_t vi = get_varinfo (find (ai->id));
8394 bitmap_iterator bi;
8395 unsigned j;
8396 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, j, bi)
8398 varinfo_t pt = get_varinfo (j);
8399 if (pt->shadow_var_uid == 0
8400 && pt->decl
8401 && auto_var_p (pt->decl))
8403 pt->shadow_var_uid = allocate_decl_uid ();
8404 shadow_var_cnt++;
8410 if (shadow_var_cnt && dump_file && (dump_flags & TDF_DETAILS))
8411 fprintf (dump_file, "Allocated %u shadow variables for locals "
8412 "maybe leaking into recursive invocations of their containing "
8413 "functions\n", shadow_var_cnt);
8415 /* Compute the global points-to sets for ESCAPED.
8416 ??? Note that the computed escape set is not correct
8417 for the whole unit as we fail to consider graph edges to
8418 externally visible functions. */
8419 ipa_escaped_pt = find_what_var_points_to (NULL, get_varinfo (escaped_id));
8421 /* Make sure the ESCAPED solution (which is used as placeholder in
8422 other solutions) does not reference itself. This simplifies
8423 points-to solution queries. */
8424 ipa_escaped_pt.ipa_escaped = 0;
8426 /* Assign the points-to sets to the SSA names in the unit. */
8427 FOR_EACH_DEFINED_FUNCTION (node)
8429 tree ptr;
8430 struct function *fn;
8431 unsigned i;
8432 basic_block bb;
8434 /* Nodes without a body are not interesting. */
8435 if (!node->has_gimple_body_p () || node->clone_of)
8436 continue;
8438 fn = DECL_STRUCT_FUNCTION (node->decl);
8440 /* Compute the points-to sets for pointer SSA_NAMEs. */
8441 FOR_EACH_VEC_ELT (*fn->gimple_df->ssa_names, i, ptr)
8443 if (ptr
8444 && POINTER_TYPE_P (TREE_TYPE (ptr)))
8445 find_what_p_points_to (node->decl, ptr);
8448 /* Compute the call-use and call-clobber sets for indirect calls
8449 and calls to external functions. */
8450 FOR_EACH_BB_FN (bb, fn)
8452 gimple_stmt_iterator gsi;
8454 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
8456 gcall *stmt;
8457 struct pt_solution *pt;
8458 varinfo_t vi, fi;
8459 tree decl;
8461 stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
8462 if (!stmt)
8463 continue;
8465 /* Handle direct calls to functions with body. */
8466 decl = gimple_call_fndecl (stmt);
8469 tree called_decl = NULL_TREE;
8470 if (gimple_call_builtin_p (stmt, BUILT_IN_GOMP_PARALLEL))
8471 called_decl = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
8472 else if (gimple_call_builtin_p (stmt, BUILT_IN_GOACC_PARALLEL))
8473 called_decl = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
8475 if (called_decl != NULL_TREE
8476 && !fndecl_maybe_in_other_partition (called_decl))
8477 decl = called_decl;
8480 if (decl
8481 && (fi = lookup_vi_for_tree (decl))
8482 && fi->is_fn_info)
8484 *gimple_call_clobber_set (stmt)
8485 = find_what_var_points_to
8486 (node->decl, first_vi_for_offset (fi, fi_clobbers));
8487 *gimple_call_use_set (stmt)
8488 = find_what_var_points_to
8489 (node->decl, first_vi_for_offset (fi, fi_uses));
8491 /* Handle direct calls to external functions. */
8492 else if (decl && (!fi || fi->decl))
8494 pt = gimple_call_use_set (stmt);
8495 if (gimple_call_flags (stmt) & ECF_CONST)
8496 memset (pt, 0, sizeof (struct pt_solution));
8497 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
8499 *pt = find_what_var_points_to (node->decl, vi);
8500 /* Escaped (and thus nonlocal) variables are always
8501 implicitly used by calls. */
8502 /* ??? ESCAPED can be empty even though NONLOCAL
8503 always escaped. */
8504 pt->nonlocal = 1;
8505 pt->ipa_escaped = 1;
8507 else
8509 /* If there is nothing special about this call then
8510 we have made everything that is used also escape. */
8511 *pt = ipa_escaped_pt;
8512 pt->nonlocal = 1;
8515 pt = gimple_call_clobber_set (stmt);
8516 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
8517 memset (pt, 0, sizeof (struct pt_solution));
8518 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
8520 *pt = find_what_var_points_to (node->decl, vi);
8521 /* Escaped (and thus nonlocal) variables are always
8522 implicitly clobbered by calls. */
8523 /* ??? ESCAPED can be empty even though NONLOCAL
8524 always escaped. */
8525 pt->nonlocal = 1;
8526 pt->ipa_escaped = 1;
8528 else
8530 /* If there is nothing special about this call then
8531 we have made everything that is used also escape. */
8532 *pt = ipa_escaped_pt;
8533 pt->nonlocal = 1;
8536 /* Handle indirect calls. */
8537 else if ((fi = get_fi_for_callee (stmt)))
8539 /* We need to accumulate all clobbers/uses of all possible
8540 callees. */
8541 fi = get_varinfo (find (fi->id));
8542 /* If we cannot constrain the set of functions we'll end up
8543 calling we end up using/clobbering everything. */
8544 if (bitmap_bit_p (fi->solution, anything_id)
8545 || bitmap_bit_p (fi->solution, nonlocal_id)
8546 || bitmap_bit_p (fi->solution, escaped_id))
8548 pt_solution_reset (gimple_call_clobber_set (stmt));
8549 pt_solution_reset (gimple_call_use_set (stmt));
8551 else
8553 bitmap_iterator bi;
8554 unsigned i;
8555 struct pt_solution *uses, *clobbers;
8557 uses = gimple_call_use_set (stmt);
8558 clobbers = gimple_call_clobber_set (stmt);
8559 memset (uses, 0, sizeof (struct pt_solution));
8560 memset (clobbers, 0, sizeof (struct pt_solution));
8561 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
8563 struct pt_solution sol;
8565 vi = get_varinfo (i);
8566 if (!vi->is_fn_info)
8568 /* ??? We could be more precise here? */
8569 uses->nonlocal = 1;
8570 uses->ipa_escaped = 1;
8571 clobbers->nonlocal = 1;
8572 clobbers->ipa_escaped = 1;
8573 continue;
8576 if (!uses->anything)
8578 sol = find_what_var_points_to
8579 (node->decl,
8580 first_vi_for_offset (vi, fi_uses));
8581 pt_solution_ior_into (uses, &sol);
8583 if (!clobbers->anything)
8585 sol = find_what_var_points_to
8586 (node->decl,
8587 first_vi_for_offset (vi, fi_clobbers));
8588 pt_solution_ior_into (clobbers, &sol);
8593 else
8594 gcc_unreachable ();
8598 fn->gimple_df->ipa_pta = true;
8600 /* We have to re-set the final-solution cache after each function
8601 because what is a "global" is dependent on function context. */
8602 final_solutions->empty ();
8603 obstack_free (&final_solutions_obstack, NULL);
8604 gcc_obstack_init (&final_solutions_obstack);
8607 delete_points_to_sets ();
8609 in_ipa_mode = 0;
8611 return 0;
8614 namespace {
8616 const pass_data pass_data_ipa_pta =
8618 SIMPLE_IPA_PASS, /* type */
8619 "pta", /* name */
8620 OPTGROUP_NONE, /* optinfo_flags */
8621 TV_IPA_PTA, /* tv_id */
8622 0, /* properties_required */
8623 0, /* properties_provided */
8624 0, /* properties_destroyed */
8625 0, /* todo_flags_start */
8626 0, /* todo_flags_finish */
8629 class pass_ipa_pta : public simple_ipa_opt_pass
8631 public:
8632 pass_ipa_pta (gcc::context *ctxt)
8633 : simple_ipa_opt_pass (pass_data_ipa_pta, ctxt)
8636 /* opt_pass methods: */
8637 virtual bool gate (function *)
8639 return (optimize
8640 && flag_ipa_pta
8641 /* Don't bother doing anything if the program has errors. */
8642 && !seen_error ());
8645 opt_pass * clone () { return new pass_ipa_pta (m_ctxt); }
8647 virtual unsigned int execute (function *) { return ipa_pta_execute (); }
8649 }; // class pass_ipa_pta
8651 } // anon namespace
8653 simple_ipa_opt_pass *
8654 make_pass_ipa_pta (gcc::context *ctxt)
8656 return new pass_ipa_pta (ctxt);