Default to dwarf version 4 on hppa64-hpux
[official-gcc.git] / gcc / tree-ssa-structalias.c
blob0b8a81ff1135d6819acb5d891f9877a19e966b90
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 if (gimple_call_internal_p (t, IFN_DEFERRED_INIT))
4920 return;
4922 fi = get_fi_for_callee (t);
4923 if (!in_ipa_mode
4924 || (fi->decl && fndecl && !fi->is_fn_info))
4926 auto_vec<ce_s, 16> rhsc;
4927 int flags = gimple_call_flags (t);
4929 /* Const functions can return their arguments and addresses
4930 of global memory but not of escaped memory. */
4931 if (flags & (ECF_CONST|ECF_NOVOPS))
4933 if (gimple_call_lhs (t))
4934 handle_const_call (t, &rhsc);
4936 /* Pure functions can return addresses in and of memory
4937 reachable from their arguments, but they are not an escape
4938 point for reachable memory of their arguments. */
4939 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4940 handle_pure_call (t, &rhsc);
4941 /* If the call is to a replaceable operator delete and results
4942 from a delete expression as opposed to a direct call to
4943 such operator, then the effects for PTA (in particular
4944 the escaping of the pointer) can be ignored. */
4945 else if (fndecl
4946 && DECL_IS_OPERATOR_DELETE_P (fndecl)
4947 && gimple_call_from_new_or_delete (t))
4949 else
4950 handle_rhs_call (t, &rhsc);
4951 if (gimple_call_lhs (t))
4952 handle_lhs_call (t, gimple_call_lhs (t),
4953 gimple_call_return_flags (t), rhsc, fndecl);
4955 else
4957 auto_vec<ce_s, 2> rhsc;
4958 tree lhsop;
4959 unsigned j;
4961 /* Assign all the passed arguments to the appropriate incoming
4962 parameters of the function. */
4963 for (j = 0; j < gimple_call_num_args (t); j++)
4965 tree arg = gimple_call_arg (t, j);
4966 find_func_aliases_for_call_arg (fi, j, arg);
4969 /* If we are returning a value, assign it to the result. */
4970 lhsop = gimple_call_lhs (t);
4971 if (lhsop)
4973 auto_vec<ce_s, 2> lhsc;
4974 struct constraint_expr rhs;
4975 struct constraint_expr *lhsp;
4976 bool aggr_p = aggregate_value_p (lhsop, gimple_call_fntype (t));
4978 get_constraint_for (lhsop, &lhsc);
4979 rhs = get_function_part_constraint (fi, fi_result);
4980 if (aggr_p)
4982 auto_vec<ce_s, 2> tem;
4983 tem.quick_push (rhs);
4984 do_deref (&tem);
4985 gcc_checking_assert (tem.length () == 1);
4986 rhs = tem[0];
4988 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4989 process_constraint (new_constraint (*lhsp, rhs));
4991 /* If we pass the result decl by reference, honor that. */
4992 if (aggr_p)
4994 struct constraint_expr lhs;
4995 struct constraint_expr *rhsp;
4997 get_constraint_for_address_of (lhsop, &rhsc);
4998 lhs = get_function_part_constraint (fi, fi_result);
4999 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5000 process_constraint (new_constraint (lhs, *rhsp));
5001 rhsc.truncate (0);
5005 /* If we use a static chain, pass it along. */
5006 if (gimple_call_chain (t))
5008 struct constraint_expr lhs;
5009 struct constraint_expr *rhsp;
5011 get_constraint_for (gimple_call_chain (t), &rhsc);
5012 lhs = get_function_part_constraint (fi, fi_static_chain);
5013 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5014 process_constraint (new_constraint (lhs, *rhsp));
5019 /* Walk statement T setting up aliasing constraints according to the
5020 references found in T. This function is the main part of the
5021 constraint builder. AI points to auxiliary alias information used
5022 when building alias sets and computing alias grouping heuristics. */
5024 static void
5025 find_func_aliases (struct function *fn, gimple *origt)
5027 gimple *t = origt;
5028 auto_vec<ce_s, 16> lhsc;
5029 auto_vec<ce_s, 16> rhsc;
5030 varinfo_t fi;
5032 /* Now build constraints expressions. */
5033 if (gimple_code (t) == GIMPLE_PHI)
5035 /* For a phi node, assign all the arguments to
5036 the result. */
5037 get_constraint_for (gimple_phi_result (t), &lhsc);
5038 for (unsigned i = 0; i < gimple_phi_num_args (t); i++)
5040 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
5041 process_all_all_constraints (lhsc, rhsc);
5042 rhsc.truncate (0);
5045 /* In IPA mode, we need to generate constraints to pass call
5046 arguments through their calls. There are two cases,
5047 either a GIMPLE_CALL returning a value, or just a plain
5048 GIMPLE_CALL when we are not.
5050 In non-ipa mode, we need to generate constraints for each
5051 pointer passed by address. */
5052 else if (is_gimple_call (t))
5053 find_func_aliases_for_call (fn, as_a <gcall *> (t));
5055 /* Otherwise, just a regular assignment statement. Only care about
5056 operations with pointer result, others are dealt with as escape
5057 points if they have pointer operands. */
5058 else if (is_gimple_assign (t))
5060 /* Otherwise, just a regular assignment statement. */
5061 tree lhsop = gimple_assign_lhs (t);
5062 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
5064 if (rhsop && TREE_CLOBBER_P (rhsop))
5065 /* Ignore clobbers, they don't actually store anything into
5066 the LHS. */
5068 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
5069 do_structure_copy (lhsop, rhsop);
5070 else
5072 enum tree_code code = gimple_assign_rhs_code (t);
5074 get_constraint_for (lhsop, &lhsc);
5076 if (code == POINTER_PLUS_EXPR)
5077 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
5078 gimple_assign_rhs2 (t), &rhsc);
5079 else if (code == POINTER_DIFF_EXPR)
5080 /* The result is not a pointer (part). */
5082 else if (code == BIT_AND_EXPR
5083 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
5085 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
5086 the pointer. Handle it by offsetting it by UNKNOWN. */
5087 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
5088 NULL_TREE, &rhsc);
5090 else if (code == TRUNC_DIV_EXPR
5091 || code == CEIL_DIV_EXPR
5092 || code == FLOOR_DIV_EXPR
5093 || code == ROUND_DIV_EXPR
5094 || code == EXACT_DIV_EXPR
5095 || code == TRUNC_MOD_EXPR
5096 || code == CEIL_MOD_EXPR
5097 || code == FLOOR_MOD_EXPR
5098 || code == ROUND_MOD_EXPR)
5099 /* Division and modulo transfer the pointer from the LHS. */
5100 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
5101 NULL_TREE, &rhsc);
5102 else if (CONVERT_EXPR_CODE_P (code)
5103 || gimple_assign_single_p (t))
5104 /* See through conversions, single RHS are handled by
5105 get_constraint_for_rhs. */
5106 get_constraint_for_rhs (rhsop, &rhsc);
5107 else if (code == COND_EXPR)
5109 /* The result is a merge of both COND_EXPR arms. */
5110 auto_vec<ce_s, 2> tmp;
5111 struct constraint_expr *rhsp;
5112 unsigned i;
5113 get_constraint_for_rhs (gimple_assign_rhs2 (t), &rhsc);
5114 get_constraint_for_rhs (gimple_assign_rhs3 (t), &tmp);
5115 FOR_EACH_VEC_ELT (tmp, i, rhsp)
5116 rhsc.safe_push (*rhsp);
5118 else if (truth_value_p (code))
5119 /* Truth value results are not pointer (parts). Or at least
5120 very unreasonable obfuscation of a part. */
5122 else
5124 /* All other operations are possibly offsetting merges. */
5125 auto_vec<ce_s, 4> tmp;
5126 struct constraint_expr *rhsp;
5127 unsigned i, j;
5128 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
5129 NULL_TREE, &rhsc);
5130 for (i = 2; i < gimple_num_ops (t); ++i)
5132 get_constraint_for_ptr_offset (gimple_op (t, i),
5133 NULL_TREE, &tmp);
5134 FOR_EACH_VEC_ELT (tmp, j, rhsp)
5135 rhsc.safe_push (*rhsp);
5136 tmp.truncate (0);
5139 process_all_all_constraints (lhsc, rhsc);
5141 /* If there is a store to a global variable the rhs escapes. */
5142 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
5143 && DECL_P (lhsop))
5145 varinfo_t vi = get_vi_for_tree (lhsop);
5146 if ((! in_ipa_mode && vi->is_global_var)
5147 || vi->is_ipa_escape_point)
5148 make_escape_constraint (rhsop);
5151 /* Handle escapes through return. */
5152 else if (gimple_code (t) == GIMPLE_RETURN
5153 && gimple_return_retval (as_a <greturn *> (t)) != NULL_TREE)
5155 greturn *return_stmt = as_a <greturn *> (t);
5156 fi = NULL;
5157 if (!in_ipa_mode
5158 && SSA_VAR_P (gimple_return_retval (return_stmt)))
5160 /* We handle simple returns by post-processing the solutions. */
5163 if (!(fi = get_vi_for_tree (fn->decl)))
5164 make_escape_constraint (gimple_return_retval (return_stmt));
5165 else if (in_ipa_mode)
5167 struct constraint_expr lhs ;
5168 struct constraint_expr *rhsp;
5169 unsigned i;
5171 lhs = get_function_part_constraint (fi, fi_result);
5172 get_constraint_for_rhs (gimple_return_retval (return_stmt), &rhsc);
5173 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5174 process_constraint (new_constraint (lhs, *rhsp));
5177 /* Handle asms conservatively by adding escape constraints to everything. */
5178 else if (gasm *asm_stmt = dyn_cast <gasm *> (t))
5180 unsigned i, noutputs;
5181 const char **oconstraints;
5182 const char *constraint;
5183 bool allows_mem, allows_reg, is_inout;
5185 noutputs = gimple_asm_noutputs (asm_stmt);
5186 oconstraints = XALLOCAVEC (const char *, noutputs);
5188 for (i = 0; i < noutputs; ++i)
5190 tree link = gimple_asm_output_op (asm_stmt, i);
5191 tree op = TREE_VALUE (link);
5193 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5194 oconstraints[i] = constraint;
5195 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
5196 &allows_reg, &is_inout);
5198 /* A memory constraint makes the address of the operand escape. */
5199 if (!allows_reg && allows_mem)
5200 make_escape_constraint (build_fold_addr_expr (op));
5202 /* The asm may read global memory, so outputs may point to
5203 any global memory. */
5204 if (op)
5206 auto_vec<ce_s, 2> lhsc;
5207 struct constraint_expr rhsc, *lhsp;
5208 unsigned j;
5209 get_constraint_for (op, &lhsc);
5210 rhsc.var = nonlocal_id;
5211 rhsc.offset = 0;
5212 rhsc.type = SCALAR;
5213 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
5214 process_constraint (new_constraint (*lhsp, rhsc));
5217 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
5219 tree link = gimple_asm_input_op (asm_stmt, i);
5220 tree op = TREE_VALUE (link);
5222 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
5224 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
5225 &allows_mem, &allows_reg);
5227 /* A memory constraint makes the address of the operand escape. */
5228 if (!allows_reg && allows_mem)
5229 make_escape_constraint (build_fold_addr_expr (op));
5230 /* Strictly we'd only need the constraint to ESCAPED if
5231 the asm clobbers memory, otherwise using something
5232 along the lines of per-call clobbers/uses would be enough. */
5233 else if (op)
5234 make_escape_constraint (op);
5240 /* Create a constraint adding to the clobber set of FI the memory
5241 pointed to by PTR. */
5243 static void
5244 process_ipa_clobber (varinfo_t fi, tree ptr)
5246 vec<ce_s> ptrc = vNULL;
5247 struct constraint_expr *c, lhs;
5248 unsigned i;
5249 get_constraint_for_rhs (ptr, &ptrc);
5250 lhs = get_function_part_constraint (fi, fi_clobbers);
5251 FOR_EACH_VEC_ELT (ptrc, i, c)
5252 process_constraint (new_constraint (lhs, *c));
5253 ptrc.release ();
5256 /* Walk statement T setting up clobber and use constraints according to the
5257 references found in T. This function is a main part of the
5258 IPA constraint builder. */
5260 static void
5261 find_func_clobbers (struct function *fn, gimple *origt)
5263 gimple *t = origt;
5264 auto_vec<ce_s, 16> lhsc;
5265 auto_vec<ce_s, 16> rhsc;
5266 varinfo_t fi;
5268 /* Add constraints for clobbered/used in IPA mode.
5269 We are not interested in what automatic variables are clobbered
5270 or used as we only use the information in the caller to which
5271 they do not escape. */
5272 gcc_assert (in_ipa_mode);
5274 /* If the stmt refers to memory in any way it better had a VUSE. */
5275 if (gimple_vuse (t) == NULL_TREE)
5276 return;
5278 /* We'd better have function information for the current function. */
5279 fi = lookup_vi_for_tree (fn->decl);
5280 gcc_assert (fi != NULL);
5282 /* Account for stores in assignments and calls. */
5283 if (gimple_vdef (t) != NULL_TREE
5284 && gimple_has_lhs (t))
5286 tree lhs = gimple_get_lhs (t);
5287 tree tem = lhs;
5288 while (handled_component_p (tem))
5289 tem = TREE_OPERAND (tem, 0);
5290 if ((DECL_P (tem)
5291 && !auto_var_in_fn_p (tem, fn->decl))
5292 || INDIRECT_REF_P (tem)
5293 || (TREE_CODE (tem) == MEM_REF
5294 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
5295 && auto_var_in_fn_p
5296 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl))))
5298 struct constraint_expr lhsc, *rhsp;
5299 unsigned i;
5300 lhsc = get_function_part_constraint (fi, fi_clobbers);
5301 get_constraint_for_address_of (lhs, &rhsc);
5302 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5303 process_constraint (new_constraint (lhsc, *rhsp));
5304 rhsc.truncate (0);
5308 /* Account for uses in assigments and returns. */
5309 if (gimple_assign_single_p (t)
5310 || (gimple_code (t) == GIMPLE_RETURN
5311 && gimple_return_retval (as_a <greturn *> (t)) != NULL_TREE))
5313 tree rhs = (gimple_assign_single_p (t)
5314 ? gimple_assign_rhs1 (t)
5315 : gimple_return_retval (as_a <greturn *> (t)));
5316 tree tem = rhs;
5317 while (handled_component_p (tem))
5318 tem = TREE_OPERAND (tem, 0);
5319 if ((DECL_P (tem)
5320 && !auto_var_in_fn_p (tem, fn->decl))
5321 || INDIRECT_REF_P (tem)
5322 || (TREE_CODE (tem) == MEM_REF
5323 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
5324 && auto_var_in_fn_p
5325 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl))))
5327 struct constraint_expr lhs, *rhsp;
5328 unsigned i;
5329 lhs = get_function_part_constraint (fi, fi_uses);
5330 get_constraint_for_address_of (rhs, &rhsc);
5331 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5332 process_constraint (new_constraint (lhs, *rhsp));
5333 rhsc.truncate (0);
5337 if (gcall *call_stmt = dyn_cast <gcall *> (t))
5339 varinfo_t cfi = NULL;
5340 tree decl = gimple_call_fndecl (t);
5341 struct constraint_expr lhs, rhs;
5342 unsigned i, j;
5344 /* For builtins we do not have separate function info. For those
5345 we do not generate escapes for we have to generate clobbers/uses. */
5346 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
5347 switch (DECL_FUNCTION_CODE (decl))
5349 /* The following functions use and clobber memory pointed to
5350 by their arguments. */
5351 case BUILT_IN_STRCPY:
5352 case BUILT_IN_STRNCPY:
5353 case BUILT_IN_BCOPY:
5354 case BUILT_IN_MEMCPY:
5355 case BUILT_IN_MEMMOVE:
5356 case BUILT_IN_MEMPCPY:
5357 case BUILT_IN_STPCPY:
5358 case BUILT_IN_STPNCPY:
5359 case BUILT_IN_STRCAT:
5360 case BUILT_IN_STRNCAT:
5361 case BUILT_IN_STRCPY_CHK:
5362 case BUILT_IN_STRNCPY_CHK:
5363 case BUILT_IN_MEMCPY_CHK:
5364 case BUILT_IN_MEMMOVE_CHK:
5365 case BUILT_IN_MEMPCPY_CHK:
5366 case BUILT_IN_STPCPY_CHK:
5367 case BUILT_IN_STPNCPY_CHK:
5368 case BUILT_IN_STRCAT_CHK:
5369 case BUILT_IN_STRNCAT_CHK:
5371 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
5372 == BUILT_IN_BCOPY ? 1 : 0));
5373 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
5374 == BUILT_IN_BCOPY ? 0 : 1));
5375 unsigned i;
5376 struct constraint_expr *rhsp, *lhsp;
5377 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
5378 lhs = get_function_part_constraint (fi, fi_clobbers);
5379 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
5380 process_constraint (new_constraint (lhs, *lhsp));
5381 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
5382 lhs = get_function_part_constraint (fi, fi_uses);
5383 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5384 process_constraint (new_constraint (lhs, *rhsp));
5385 return;
5387 /* The following function clobbers memory pointed to by
5388 its argument. */
5389 case BUILT_IN_MEMSET:
5390 case BUILT_IN_MEMSET_CHK:
5391 case BUILT_IN_POSIX_MEMALIGN:
5393 tree dest = gimple_call_arg (t, 0);
5394 unsigned i;
5395 ce_s *lhsp;
5396 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
5397 lhs = get_function_part_constraint (fi, fi_clobbers);
5398 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
5399 process_constraint (new_constraint (lhs, *lhsp));
5400 return;
5402 /* The following functions clobber their second and third
5403 arguments. */
5404 case BUILT_IN_SINCOS:
5405 case BUILT_IN_SINCOSF:
5406 case BUILT_IN_SINCOSL:
5408 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5409 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5410 return;
5412 /* The following functions clobber their second argument. */
5413 case BUILT_IN_FREXP:
5414 case BUILT_IN_FREXPF:
5415 case BUILT_IN_FREXPL:
5416 case BUILT_IN_LGAMMA_R:
5417 case BUILT_IN_LGAMMAF_R:
5418 case BUILT_IN_LGAMMAL_R:
5419 case BUILT_IN_GAMMA_R:
5420 case BUILT_IN_GAMMAF_R:
5421 case BUILT_IN_GAMMAL_R:
5422 case BUILT_IN_MODF:
5423 case BUILT_IN_MODFF:
5424 case BUILT_IN_MODFL:
5426 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5427 return;
5429 /* The following functions clobber their third argument. */
5430 case BUILT_IN_REMQUO:
5431 case BUILT_IN_REMQUOF:
5432 case BUILT_IN_REMQUOL:
5434 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5435 return;
5437 /* The following functions neither read nor clobber memory. */
5438 case BUILT_IN_ASSUME_ALIGNED:
5439 case BUILT_IN_FREE:
5440 return;
5441 /* Trampolines are of no interest to us. */
5442 case BUILT_IN_INIT_TRAMPOLINE:
5443 case BUILT_IN_ADJUST_TRAMPOLINE:
5444 return;
5445 case BUILT_IN_VA_START:
5446 case BUILT_IN_VA_END:
5447 return;
5448 case BUILT_IN_GOMP_PARALLEL:
5449 case BUILT_IN_GOACC_PARALLEL:
5451 unsigned int fnpos, argpos;
5452 unsigned int implicit_use_args[2];
5453 unsigned int num_implicit_use_args = 0;
5454 switch (DECL_FUNCTION_CODE (decl))
5456 case BUILT_IN_GOMP_PARALLEL:
5457 /* __builtin_GOMP_parallel (fn, data, num_threads, flags). */
5458 fnpos = 0;
5459 argpos = 1;
5460 break;
5461 case BUILT_IN_GOACC_PARALLEL:
5462 /* __builtin_GOACC_parallel (flags_m, fn, mapnum, hostaddrs,
5463 sizes, kinds, ...). */
5464 fnpos = 1;
5465 argpos = 3;
5466 implicit_use_args[num_implicit_use_args++] = 4;
5467 implicit_use_args[num_implicit_use_args++] = 5;
5468 break;
5469 default:
5470 gcc_unreachable ();
5473 tree fnarg = gimple_call_arg (t, fnpos);
5474 gcc_assert (TREE_CODE (fnarg) == ADDR_EXPR);
5475 tree fndecl = TREE_OPERAND (fnarg, 0);
5476 if (fndecl_maybe_in_other_partition (fndecl))
5477 /* Fallthru to general call handling. */
5478 break;
5480 varinfo_t cfi = get_vi_for_tree (fndecl);
5482 tree arg = gimple_call_arg (t, argpos);
5484 /* Parameter passed by value is used. */
5485 lhs = get_function_part_constraint (fi, fi_uses);
5486 struct constraint_expr *rhsp;
5487 get_constraint_for (arg, &rhsc);
5488 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5489 process_constraint (new_constraint (lhs, *rhsp));
5490 rhsc.truncate (0);
5492 /* Handle parameters used by the call, but not used in cfi, as
5493 implicitly used by cfi. */
5494 lhs = get_function_part_constraint (cfi, fi_uses);
5495 for (unsigned i = 0; i < num_implicit_use_args; ++i)
5497 tree arg = gimple_call_arg (t, implicit_use_args[i]);
5498 get_constraint_for (arg, &rhsc);
5499 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5500 process_constraint (new_constraint (lhs, *rhsp));
5501 rhsc.truncate (0);
5504 /* The caller clobbers what the callee does. */
5505 lhs = get_function_part_constraint (fi, fi_clobbers);
5506 rhs = get_function_part_constraint (cfi, fi_clobbers);
5507 process_constraint (new_constraint (lhs, rhs));
5509 /* The caller uses what the callee does. */
5510 lhs = get_function_part_constraint (fi, fi_uses);
5511 rhs = get_function_part_constraint (cfi, fi_uses);
5512 process_constraint (new_constraint (lhs, rhs));
5514 return;
5516 /* printf-style functions may have hooks to set pointers to
5517 point to somewhere into the generated string. Leave them
5518 for a later exercise... */
5519 default:
5520 /* Fallthru to general call handling. */;
5523 /* Parameters passed by value are used. */
5524 lhs = get_function_part_constraint (fi, fi_uses);
5525 for (i = 0; i < gimple_call_num_args (t); i++)
5527 struct constraint_expr *rhsp;
5528 tree arg = gimple_call_arg (t, i);
5530 if (TREE_CODE (arg) == SSA_NAME
5531 || is_gimple_min_invariant (arg))
5532 continue;
5534 get_constraint_for_address_of (arg, &rhsc);
5535 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5536 process_constraint (new_constraint (lhs, *rhsp));
5537 rhsc.truncate (0);
5540 /* Build constraints for propagating clobbers/uses along the
5541 callgraph edges. */
5542 cfi = get_fi_for_callee (call_stmt);
5543 if (cfi->id == anything_id)
5545 if (gimple_vdef (t))
5546 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5547 anything_id);
5548 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5549 anything_id);
5550 return;
5553 /* For callees without function info (that's external functions),
5554 ESCAPED is clobbered and used. */
5555 if (cfi->decl
5556 && TREE_CODE (cfi->decl) == FUNCTION_DECL
5557 && !cfi->is_fn_info)
5559 varinfo_t vi;
5561 if (gimple_vdef (t))
5562 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5563 escaped_id);
5564 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
5566 /* Also honor the call statement use/clobber info. */
5567 if ((vi = lookup_call_clobber_vi (call_stmt)) != NULL)
5568 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5569 vi->id);
5570 if ((vi = lookup_call_use_vi (call_stmt)) != NULL)
5571 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
5572 vi->id);
5573 return;
5576 /* Otherwise the caller clobbers and uses what the callee does.
5577 ??? This should use a new complex constraint that filters
5578 local variables of the callee. */
5579 if (gimple_vdef (t))
5581 lhs = get_function_part_constraint (fi, fi_clobbers);
5582 rhs = get_function_part_constraint (cfi, fi_clobbers);
5583 process_constraint (new_constraint (lhs, rhs));
5585 lhs = get_function_part_constraint (fi, fi_uses);
5586 rhs = get_function_part_constraint (cfi, fi_uses);
5587 process_constraint (new_constraint (lhs, rhs));
5589 else if (gimple_code (t) == GIMPLE_ASM)
5591 /* ??? Ick. We can do better. */
5592 if (gimple_vdef (t))
5593 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5594 anything_id);
5595 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5596 anything_id);
5601 /* Find the first varinfo in the same variable as START that overlaps with
5602 OFFSET. Return NULL if we can't find one. */
5604 static varinfo_t
5605 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
5607 /* If the offset is outside of the variable, bail out. */
5608 if (offset >= start->fullsize)
5609 return NULL;
5611 /* If we cannot reach offset from start, lookup the first field
5612 and start from there. */
5613 if (start->offset > offset)
5614 start = get_varinfo (start->head);
5616 while (start)
5618 /* We may not find a variable in the field list with the actual
5619 offset when we have glommed a structure to a variable.
5620 In that case, however, offset should still be within the size
5621 of the variable. */
5622 if (offset >= start->offset
5623 && (offset - start->offset) < start->size)
5624 return start;
5626 start = vi_next (start);
5629 return NULL;
5632 /* Find the first varinfo in the same variable as START that overlaps with
5633 OFFSET. If there is no such varinfo the varinfo directly preceding
5634 OFFSET is returned. */
5636 static varinfo_t
5637 first_or_preceding_vi_for_offset (varinfo_t start,
5638 unsigned HOST_WIDE_INT offset)
5640 /* If we cannot reach offset from start, lookup the first field
5641 and start from there. */
5642 if (start->offset > offset)
5643 start = get_varinfo (start->head);
5645 /* We may not find a variable in the field list with the actual
5646 offset when we have glommed a structure to a variable.
5647 In that case, however, offset should still be within the size
5648 of the variable.
5649 If we got beyond the offset we look for return the field
5650 directly preceding offset which may be the last field. */
5651 while (start->next
5652 && offset >= start->offset
5653 && !((offset - start->offset) < start->size))
5654 start = vi_next (start);
5656 return start;
5660 /* This structure is used during pushing fields onto the fieldstack
5661 to track the offset of the field, since bitpos_of_field gives it
5662 relative to its immediate containing type, and we want it relative
5663 to the ultimate containing object. */
5665 struct fieldoff
5667 /* Offset from the base of the base containing object to this field. */
5668 HOST_WIDE_INT offset;
5670 /* Size, in bits, of the field. */
5671 unsigned HOST_WIDE_INT size;
5673 unsigned has_unknown_size : 1;
5675 unsigned must_have_pointers : 1;
5677 unsigned may_have_pointers : 1;
5679 unsigned only_restrict_pointers : 1;
5681 tree restrict_pointed_type;
5683 typedef struct fieldoff fieldoff_s;
5686 /* qsort comparison function for two fieldoff's PA and PB */
5688 static int
5689 fieldoff_compare (const void *pa, const void *pb)
5691 const fieldoff_s *foa = (const fieldoff_s *)pa;
5692 const fieldoff_s *fob = (const fieldoff_s *)pb;
5693 unsigned HOST_WIDE_INT foasize, fobsize;
5695 if (foa->offset < fob->offset)
5696 return -1;
5697 else if (foa->offset > fob->offset)
5698 return 1;
5700 foasize = foa->size;
5701 fobsize = fob->size;
5702 if (foasize < fobsize)
5703 return -1;
5704 else if (foasize > fobsize)
5705 return 1;
5706 return 0;
5709 /* Sort a fieldstack according to the field offset and sizes. */
5710 static void
5711 sort_fieldstack (vec<fieldoff_s> &fieldstack)
5713 fieldstack.qsort (fieldoff_compare);
5716 /* Return true if T is a type that can have subvars. */
5718 static inline bool
5719 type_can_have_subvars (const_tree t)
5721 /* Aggregates without overlapping fields can have subvars. */
5722 return TREE_CODE (t) == RECORD_TYPE;
5725 /* Return true if V is a tree that we can have subvars for.
5726 Normally, this is any aggregate type. Also complex
5727 types which are not gimple registers can have subvars. */
5729 static inline bool
5730 var_can_have_subvars (const_tree v)
5732 /* Volatile variables should never have subvars. */
5733 if (TREE_THIS_VOLATILE (v))
5734 return false;
5736 /* Non decls or memory tags can never have subvars. */
5737 if (!DECL_P (v))
5738 return false;
5740 return type_can_have_subvars (TREE_TYPE (v));
5743 /* Return true if T is a type that does contain pointers. */
5745 static bool
5746 type_must_have_pointers (tree type)
5748 if (POINTER_TYPE_P (type))
5749 return true;
5751 if (TREE_CODE (type) == ARRAY_TYPE)
5752 return type_must_have_pointers (TREE_TYPE (type));
5754 /* A function or method can have pointers as arguments, so track
5755 those separately. */
5756 if (TREE_CODE (type) == FUNCTION_TYPE
5757 || TREE_CODE (type) == METHOD_TYPE)
5758 return true;
5760 return false;
5763 static bool
5764 field_must_have_pointers (tree t)
5766 return type_must_have_pointers (TREE_TYPE (t));
5769 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5770 the fields of TYPE onto fieldstack, recording their offsets along
5771 the way.
5773 OFFSET is used to keep track of the offset in this entire
5774 structure, rather than just the immediately containing structure.
5775 Returns false if the caller is supposed to handle the field we
5776 recursed for. */
5778 static bool
5779 push_fields_onto_fieldstack (tree type, vec<fieldoff_s> *fieldstack,
5780 HOST_WIDE_INT offset)
5782 tree field;
5783 bool empty_p = true;
5785 if (TREE_CODE (type) != RECORD_TYPE)
5786 return false;
5788 /* If the vector of fields is growing too big, bail out early.
5789 Callers check for vec::length <= param_max_fields_for_field_sensitive, make
5790 sure this fails. */
5791 if (fieldstack->length () > (unsigned)param_max_fields_for_field_sensitive)
5792 return false;
5794 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5795 if (TREE_CODE (field) == FIELD_DECL)
5797 bool push = false;
5798 HOST_WIDE_INT foff = bitpos_of_field (field);
5799 tree field_type = TREE_TYPE (field);
5801 if (!var_can_have_subvars (field)
5802 || TREE_CODE (field_type) == QUAL_UNION_TYPE
5803 || TREE_CODE (field_type) == UNION_TYPE)
5804 push = true;
5805 else if (!push_fields_onto_fieldstack
5806 (field_type, fieldstack, offset + foff)
5807 && (DECL_SIZE (field)
5808 && !integer_zerop (DECL_SIZE (field))))
5809 /* Empty structures may have actual size, like in C++. So
5810 see if we didn't push any subfields and the size is
5811 nonzero, push the field onto the stack. */
5812 push = true;
5814 if (push)
5816 fieldoff_s *pair = NULL;
5817 bool has_unknown_size = false;
5818 bool must_have_pointers_p;
5820 if (!fieldstack->is_empty ())
5821 pair = &fieldstack->last ();
5823 /* If there isn't anything at offset zero, create sth. */
5824 if (!pair
5825 && offset + foff != 0)
5827 fieldoff_s e
5828 = {0, offset + foff, false, false, true, false, NULL_TREE};
5829 pair = fieldstack->safe_push (e);
5832 if (!DECL_SIZE (field)
5833 || !tree_fits_uhwi_p (DECL_SIZE (field)))
5834 has_unknown_size = true;
5836 /* If adjacent fields do not contain pointers merge them. */
5837 must_have_pointers_p = field_must_have_pointers (field);
5838 if (pair
5839 && !has_unknown_size
5840 && !must_have_pointers_p
5841 && !pair->must_have_pointers
5842 && !pair->has_unknown_size
5843 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5845 pair->size += tree_to_uhwi (DECL_SIZE (field));
5847 else
5849 fieldoff_s e;
5850 e.offset = offset + foff;
5851 e.has_unknown_size = has_unknown_size;
5852 if (!has_unknown_size)
5853 e.size = tree_to_uhwi (DECL_SIZE (field));
5854 else
5855 e.size = -1;
5856 e.must_have_pointers = must_have_pointers_p;
5857 e.may_have_pointers = true;
5858 e.only_restrict_pointers
5859 = (!has_unknown_size
5860 && POINTER_TYPE_P (field_type)
5861 && TYPE_RESTRICT (field_type));
5862 if (e.only_restrict_pointers)
5863 e.restrict_pointed_type = TREE_TYPE (field_type);
5864 fieldstack->safe_push (e);
5868 empty_p = false;
5871 return !empty_p;
5874 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5875 if it is a varargs function. */
5877 static unsigned int
5878 count_num_arguments (tree decl, bool *is_varargs)
5880 unsigned int num = 0;
5881 tree t;
5883 /* Capture named arguments for K&R functions. They do not
5884 have a prototype and thus no TYPE_ARG_TYPES. */
5885 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5886 ++num;
5888 /* Check if the function has variadic arguments. */
5889 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5890 if (TREE_VALUE (t) == void_type_node)
5891 break;
5892 if (!t)
5893 *is_varargs = true;
5895 return num;
5898 /* Creation function node for DECL, using NAME, and return the index
5899 of the variable we've created for the function. If NONLOCAL_p, create
5900 initial constraints. */
5902 static varinfo_t
5903 create_function_info_for (tree decl, const char *name, bool add_id,
5904 bool nonlocal_p)
5906 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5907 varinfo_t vi, prev_vi;
5908 tree arg;
5909 unsigned int i;
5910 bool is_varargs = false;
5911 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5913 /* Create the variable info. */
5915 vi = new_var_info (decl, name, add_id);
5916 vi->offset = 0;
5917 vi->size = 1;
5918 vi->fullsize = fi_parm_base + num_args;
5919 vi->is_fn_info = 1;
5920 vi->may_have_pointers = false;
5921 if (is_varargs)
5922 vi->fullsize = ~0;
5923 insert_vi_for_tree (vi->decl, vi);
5925 prev_vi = vi;
5927 /* Create a variable for things the function clobbers and one for
5928 things the function uses. */
5930 varinfo_t clobbervi, usevi;
5931 const char *newname;
5932 char *tempname;
5934 tempname = xasprintf ("%s.clobber", name);
5935 newname = ggc_strdup (tempname);
5936 free (tempname);
5938 clobbervi = new_var_info (NULL, newname, false);
5939 clobbervi->offset = fi_clobbers;
5940 clobbervi->size = 1;
5941 clobbervi->fullsize = vi->fullsize;
5942 clobbervi->is_full_var = true;
5943 clobbervi->is_global_var = false;
5944 clobbervi->is_reg_var = true;
5946 gcc_assert (prev_vi->offset < clobbervi->offset);
5947 prev_vi->next = clobbervi->id;
5948 prev_vi = clobbervi;
5950 tempname = xasprintf ("%s.use", name);
5951 newname = ggc_strdup (tempname);
5952 free (tempname);
5954 usevi = new_var_info (NULL, newname, false);
5955 usevi->offset = fi_uses;
5956 usevi->size = 1;
5957 usevi->fullsize = vi->fullsize;
5958 usevi->is_full_var = true;
5959 usevi->is_global_var = false;
5960 usevi->is_reg_var = true;
5962 gcc_assert (prev_vi->offset < usevi->offset);
5963 prev_vi->next = usevi->id;
5964 prev_vi = usevi;
5967 /* And one for the static chain. */
5968 if (fn->static_chain_decl != NULL_TREE)
5970 varinfo_t chainvi;
5971 const char *newname;
5972 char *tempname;
5974 tempname = xasprintf ("%s.chain", name);
5975 newname = ggc_strdup (tempname);
5976 free (tempname);
5978 chainvi = new_var_info (fn->static_chain_decl, newname, false);
5979 chainvi->offset = fi_static_chain;
5980 chainvi->size = 1;
5981 chainvi->fullsize = vi->fullsize;
5982 chainvi->is_full_var = true;
5983 chainvi->is_global_var = false;
5985 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5987 if (nonlocal_p
5988 && chainvi->may_have_pointers)
5989 make_constraint_from (chainvi, nonlocal_id);
5991 gcc_assert (prev_vi->offset < chainvi->offset);
5992 prev_vi->next = chainvi->id;
5993 prev_vi = chainvi;
5996 /* Create a variable for the return var. */
5997 if (DECL_RESULT (decl) != NULL
5998 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
6000 varinfo_t resultvi;
6001 const char *newname;
6002 char *tempname;
6003 tree resultdecl = decl;
6005 if (DECL_RESULT (decl))
6006 resultdecl = DECL_RESULT (decl);
6008 tempname = xasprintf ("%s.result", name);
6009 newname = ggc_strdup (tempname);
6010 free (tempname);
6012 resultvi = new_var_info (resultdecl, newname, false);
6013 resultvi->offset = fi_result;
6014 resultvi->size = 1;
6015 resultvi->fullsize = vi->fullsize;
6016 resultvi->is_full_var = true;
6017 if (DECL_RESULT (decl))
6018 resultvi->may_have_pointers = true;
6020 if (DECL_RESULT (decl))
6021 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
6023 if (nonlocal_p
6024 && DECL_RESULT (decl)
6025 && DECL_BY_REFERENCE (DECL_RESULT (decl)))
6026 make_constraint_from (resultvi, nonlocal_id);
6028 gcc_assert (prev_vi->offset < resultvi->offset);
6029 prev_vi->next = resultvi->id;
6030 prev_vi = resultvi;
6033 /* We also need to make function return values escape. Nothing
6034 escapes by returning from main though. */
6035 if (nonlocal_p
6036 && !MAIN_NAME_P (DECL_NAME (decl)))
6038 varinfo_t fi, rvi;
6039 fi = lookup_vi_for_tree (decl);
6040 rvi = first_vi_for_offset (fi, fi_result);
6041 if (rvi && rvi->offset == fi_result)
6042 make_copy_constraint (get_varinfo (escaped_id), rvi->id);
6045 /* Set up variables for each argument. */
6046 arg = DECL_ARGUMENTS (decl);
6047 for (i = 0; i < num_args; i++)
6049 varinfo_t argvi;
6050 const char *newname;
6051 char *tempname;
6052 tree argdecl = decl;
6054 if (arg)
6055 argdecl = arg;
6057 tempname = xasprintf ("%s.arg%d", name, i);
6058 newname = ggc_strdup (tempname);
6059 free (tempname);
6061 argvi = new_var_info (argdecl, newname, false);
6062 argvi->offset = fi_parm_base + i;
6063 argvi->size = 1;
6064 argvi->is_full_var = true;
6065 argvi->fullsize = vi->fullsize;
6066 if (arg)
6067 argvi->may_have_pointers = true;
6069 if (arg)
6070 insert_vi_for_tree (arg, argvi);
6072 if (nonlocal_p
6073 && argvi->may_have_pointers)
6074 make_constraint_from (argvi, nonlocal_id);
6076 gcc_assert (prev_vi->offset < argvi->offset);
6077 prev_vi->next = argvi->id;
6078 prev_vi = argvi;
6079 if (arg)
6080 arg = DECL_CHAIN (arg);
6083 /* Add one representative for all further args. */
6084 if (is_varargs)
6086 varinfo_t argvi;
6087 const char *newname;
6088 char *tempname;
6089 tree decl;
6091 tempname = xasprintf ("%s.varargs", name);
6092 newname = ggc_strdup (tempname);
6093 free (tempname);
6095 /* We need sth that can be pointed to for va_start. */
6096 decl = build_fake_var_decl (ptr_type_node);
6098 argvi = new_var_info (decl, newname, false);
6099 argvi->offset = fi_parm_base + num_args;
6100 argvi->size = ~0;
6101 argvi->is_full_var = true;
6102 argvi->is_heap_var = true;
6103 argvi->fullsize = vi->fullsize;
6105 if (nonlocal_p
6106 && argvi->may_have_pointers)
6107 make_constraint_from (argvi, nonlocal_id);
6109 gcc_assert (prev_vi->offset < argvi->offset);
6110 prev_vi->next = argvi->id;
6113 return vi;
6117 /* Return true if FIELDSTACK contains fields that overlap.
6118 FIELDSTACK is assumed to be sorted by offset. */
6120 static bool
6121 check_for_overlaps (const vec<fieldoff_s> &fieldstack)
6123 fieldoff_s *fo = NULL;
6124 unsigned int i;
6125 HOST_WIDE_INT lastoffset = -1;
6127 FOR_EACH_VEC_ELT (fieldstack, i, fo)
6129 if (fo->offset == lastoffset)
6130 return true;
6131 lastoffset = fo->offset;
6133 return false;
6136 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
6137 This will also create any varinfo structures necessary for fields
6138 of DECL. DECL is a function parameter if HANDLE_PARAM is set.
6139 HANDLED_STRUCT_TYPE is used to register struct types reached by following
6140 restrict pointers. This is needed to prevent infinite recursion.
6141 If ADD_RESTRICT, pretend that the pointer NAME is restrict even if DECL
6142 does not advertise it. */
6144 static varinfo_t
6145 create_variable_info_for_1 (tree decl, const char *name, bool add_id,
6146 bool handle_param, bitmap handled_struct_type,
6147 bool add_restrict = false)
6149 varinfo_t vi, newvi;
6150 tree decl_type = TREE_TYPE (decl);
6151 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
6152 auto_vec<fieldoff_s> fieldstack;
6153 fieldoff_s *fo;
6154 unsigned int i;
6156 if (!declsize
6157 || !tree_fits_uhwi_p (declsize))
6159 vi = new_var_info (decl, name, add_id);
6160 vi->offset = 0;
6161 vi->size = ~0;
6162 vi->fullsize = ~0;
6163 vi->is_unknown_size_var = true;
6164 vi->is_full_var = true;
6165 vi->may_have_pointers = true;
6166 return vi;
6169 /* Collect field information. */
6170 if (use_field_sensitive
6171 && var_can_have_subvars (decl)
6172 /* ??? Force us to not use subfields for globals in IPA mode.
6173 Else we'd have to parse arbitrary initializers. */
6174 && !(in_ipa_mode
6175 && is_global_var (decl)))
6177 fieldoff_s *fo = NULL;
6178 bool notokay = false;
6179 unsigned int i;
6181 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
6183 for (i = 0; !notokay && fieldstack.iterate (i, &fo); i++)
6184 if (fo->has_unknown_size
6185 || fo->offset < 0)
6187 notokay = true;
6188 break;
6191 /* We can't sort them if we have a field with a variable sized type,
6192 which will make notokay = true. In that case, we are going to return
6193 without creating varinfos for the fields anyway, so sorting them is a
6194 waste to boot. */
6195 if (!notokay)
6197 sort_fieldstack (fieldstack);
6198 /* Due to some C++ FE issues, like PR 22488, we might end up
6199 what appear to be overlapping fields even though they,
6200 in reality, do not overlap. Until the C++ FE is fixed,
6201 we will simply disable field-sensitivity for these cases. */
6202 notokay = check_for_overlaps (fieldstack);
6205 if (notokay)
6206 fieldstack.release ();
6209 /* If we didn't end up collecting sub-variables create a full
6210 variable for the decl. */
6211 if (fieldstack.length () == 0
6212 || fieldstack.length () > (unsigned)param_max_fields_for_field_sensitive)
6214 vi = new_var_info (decl, name, add_id);
6215 vi->offset = 0;
6216 vi->may_have_pointers = true;
6217 vi->fullsize = tree_to_uhwi (declsize);
6218 vi->size = vi->fullsize;
6219 vi->is_full_var = true;
6220 if (POINTER_TYPE_P (decl_type)
6221 && (TYPE_RESTRICT (decl_type) || add_restrict))
6222 vi->only_restrict_pointers = 1;
6223 if (vi->only_restrict_pointers
6224 && !type_contains_placeholder_p (TREE_TYPE (decl_type))
6225 && handle_param
6226 && !bitmap_bit_p (handled_struct_type,
6227 TYPE_UID (TREE_TYPE (decl_type))))
6229 varinfo_t rvi;
6230 tree heapvar = build_fake_var_decl (TREE_TYPE (decl_type));
6231 DECL_EXTERNAL (heapvar) = 1;
6232 if (var_can_have_subvars (heapvar))
6233 bitmap_set_bit (handled_struct_type,
6234 TYPE_UID (TREE_TYPE (decl_type)));
6235 rvi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS", true,
6236 true, handled_struct_type);
6237 if (var_can_have_subvars (heapvar))
6238 bitmap_clear_bit (handled_struct_type,
6239 TYPE_UID (TREE_TYPE (decl_type)));
6240 rvi->is_restrict_var = 1;
6241 insert_vi_for_tree (heapvar, rvi);
6242 make_constraint_from (vi, rvi->id);
6243 make_param_constraints (rvi);
6245 fieldstack.release ();
6246 return vi;
6249 vi = new_var_info (decl, name, add_id);
6250 vi->fullsize = tree_to_uhwi (declsize);
6251 if (fieldstack.length () == 1)
6252 vi->is_full_var = true;
6253 for (i = 0, newvi = vi;
6254 fieldstack.iterate (i, &fo);
6255 ++i, newvi = vi_next (newvi))
6257 const char *newname = NULL;
6258 char *tempname;
6260 if (dump_file)
6262 if (fieldstack.length () != 1)
6264 tempname
6265 = xasprintf ("%s." HOST_WIDE_INT_PRINT_DEC
6266 "+" HOST_WIDE_INT_PRINT_DEC, name,
6267 fo->offset, fo->size);
6268 newname = ggc_strdup (tempname);
6269 free (tempname);
6272 else
6273 newname = "NULL";
6275 if (newname)
6276 newvi->name = newname;
6277 newvi->offset = fo->offset;
6278 newvi->size = fo->size;
6279 newvi->fullsize = vi->fullsize;
6280 newvi->may_have_pointers = fo->may_have_pointers;
6281 newvi->only_restrict_pointers = fo->only_restrict_pointers;
6282 if (handle_param
6283 && newvi->only_restrict_pointers
6284 && !type_contains_placeholder_p (fo->restrict_pointed_type)
6285 && !bitmap_bit_p (handled_struct_type,
6286 TYPE_UID (fo->restrict_pointed_type)))
6288 varinfo_t rvi;
6289 tree heapvar = build_fake_var_decl (fo->restrict_pointed_type);
6290 DECL_EXTERNAL (heapvar) = 1;
6291 if (var_can_have_subvars (heapvar))
6292 bitmap_set_bit (handled_struct_type,
6293 TYPE_UID (fo->restrict_pointed_type));
6294 rvi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS", true,
6295 true, handled_struct_type);
6296 if (var_can_have_subvars (heapvar))
6297 bitmap_clear_bit (handled_struct_type,
6298 TYPE_UID (fo->restrict_pointed_type));
6299 rvi->is_restrict_var = 1;
6300 insert_vi_for_tree (heapvar, rvi);
6301 make_constraint_from (newvi, rvi->id);
6302 make_param_constraints (rvi);
6304 if (i + 1 < fieldstack.length ())
6306 varinfo_t tem = new_var_info (decl, name, false);
6307 newvi->next = tem->id;
6308 tem->head = vi->id;
6312 return vi;
6315 static unsigned int
6316 create_variable_info_for (tree decl, const char *name, bool add_id)
6318 /* First see if we are dealing with an ifunc resolver call and
6319 assiociate that with a call to the resolver function result. */
6320 cgraph_node *node;
6321 if (in_ipa_mode
6322 && TREE_CODE (decl) == FUNCTION_DECL
6323 && (node = cgraph_node::get (decl))
6324 && node->ifunc_resolver)
6326 varinfo_t fi = get_vi_for_tree (node->get_alias_target ()->decl);
6327 constraint_expr rhs
6328 = get_function_part_constraint (fi, fi_result);
6329 fi = new_var_info (NULL_TREE, "ifuncres", true);
6330 fi->is_reg_var = true;
6331 constraint_expr lhs;
6332 lhs.type = SCALAR;
6333 lhs.var = fi->id;
6334 lhs.offset = 0;
6335 process_constraint (new_constraint (lhs, rhs));
6336 insert_vi_for_tree (decl, fi);
6337 return fi->id;
6340 varinfo_t vi = create_variable_info_for_1 (decl, name, add_id, false, NULL);
6341 unsigned int id = vi->id;
6343 insert_vi_for_tree (decl, vi);
6345 if (!VAR_P (decl))
6346 return id;
6348 /* Create initial constraints for globals. */
6349 for (; vi; vi = vi_next (vi))
6351 if (!vi->may_have_pointers
6352 || !vi->is_global_var)
6353 continue;
6355 /* Mark global restrict qualified pointers. */
6356 if ((POINTER_TYPE_P (TREE_TYPE (decl))
6357 && TYPE_RESTRICT (TREE_TYPE (decl)))
6358 || vi->only_restrict_pointers)
6360 varinfo_t rvi
6361 = make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT",
6362 true);
6363 /* ??? For now exclude reads from globals as restrict sources
6364 if those are not (indirectly) from incoming parameters. */
6365 rvi->is_restrict_var = false;
6366 continue;
6369 /* In non-IPA mode the initializer from nonlocal is all we need. */
6370 if (!in_ipa_mode
6371 || DECL_HARD_REGISTER (decl))
6372 make_copy_constraint (vi, nonlocal_id);
6374 /* In IPA mode parse the initializer and generate proper constraints
6375 for it. */
6376 else
6378 varpool_node *vnode = varpool_node::get (decl);
6380 /* For escaped variables initialize them from nonlocal. */
6381 if (!vnode->all_refs_explicit_p ())
6382 make_copy_constraint (vi, nonlocal_id);
6384 /* If this is a global variable with an initializer and we are in
6385 IPA mode generate constraints for it. */
6386 ipa_ref *ref;
6387 for (unsigned idx = 0; vnode->iterate_reference (idx, ref); ++idx)
6389 auto_vec<ce_s> rhsc;
6390 struct constraint_expr lhs, *rhsp;
6391 unsigned i;
6392 get_constraint_for_address_of (ref->referred->decl, &rhsc);
6393 lhs.var = vi->id;
6394 lhs.offset = 0;
6395 lhs.type = SCALAR;
6396 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
6397 process_constraint (new_constraint (lhs, *rhsp));
6398 /* If this is a variable that escapes from the unit
6399 the initializer escapes as well. */
6400 if (!vnode->all_refs_explicit_p ())
6402 lhs.var = escaped_id;
6403 lhs.offset = 0;
6404 lhs.type = SCALAR;
6405 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
6406 process_constraint (new_constraint (lhs, *rhsp));
6412 return id;
6415 /* Print out the points-to solution for VAR to FILE. */
6417 static void
6418 dump_solution_for_var (FILE *file, unsigned int var)
6420 varinfo_t vi = get_varinfo (var);
6421 unsigned int i;
6422 bitmap_iterator bi;
6424 /* Dump the solution for unified vars anyway, this avoids difficulties
6425 in scanning dumps in the testsuite. */
6426 fprintf (file, "%s = { ", vi->name);
6427 vi = get_varinfo (find (var));
6428 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
6429 fprintf (file, "%s ", get_varinfo (i)->name);
6430 fprintf (file, "}");
6432 /* But note when the variable was unified. */
6433 if (vi->id != var)
6434 fprintf (file, " same as %s", vi->name);
6436 fprintf (file, "\n");
6439 /* Print the points-to solution for VAR to stderr. */
6441 DEBUG_FUNCTION void
6442 debug_solution_for_var (unsigned int var)
6444 dump_solution_for_var (stderr, var);
6447 /* Register the constraints for function parameter related VI. */
6449 static void
6450 make_param_constraints (varinfo_t vi)
6452 for (; vi; vi = vi_next (vi))
6454 if (vi->only_restrict_pointers)
6456 else if (vi->may_have_pointers)
6457 make_constraint_from (vi, nonlocal_id);
6459 if (vi->is_full_var)
6460 break;
6464 /* Create varinfo structures for all of the variables in the
6465 function for intraprocedural mode. */
6467 static void
6468 intra_create_variable_infos (struct function *fn)
6470 tree t;
6471 bitmap handled_struct_type = NULL;
6472 bool this_parm_in_ctor = DECL_CXX_CONSTRUCTOR_P (fn->decl);
6474 /* For each incoming pointer argument arg, create the constraint ARG
6475 = NONLOCAL or a dummy variable if it is a restrict qualified
6476 passed-by-reference argument. */
6477 for (t = DECL_ARGUMENTS (fn->decl); t; t = DECL_CHAIN (t))
6479 if (handled_struct_type == NULL)
6480 handled_struct_type = BITMAP_ALLOC (NULL);
6482 varinfo_t p
6483 = create_variable_info_for_1 (t, alias_get_name (t), false, true,
6484 handled_struct_type, this_parm_in_ctor);
6485 insert_vi_for_tree (t, p);
6487 make_param_constraints (p);
6489 this_parm_in_ctor = false;
6492 if (handled_struct_type != NULL)
6493 BITMAP_FREE (handled_struct_type);
6495 /* Add a constraint for a result decl that is passed by reference. */
6496 if (DECL_RESULT (fn->decl)
6497 && DECL_BY_REFERENCE (DECL_RESULT (fn->decl)))
6499 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (fn->decl));
6501 for (p = result_vi; p; p = vi_next (p))
6502 make_constraint_from (p, nonlocal_id);
6505 /* Add a constraint for the incoming static chain parameter. */
6506 if (fn->static_chain_decl != NULL_TREE)
6508 varinfo_t p, chain_vi = get_vi_for_tree (fn->static_chain_decl);
6510 for (p = chain_vi; p; p = vi_next (p))
6511 make_constraint_from (p, nonlocal_id);
6515 /* Structure used to put solution bitmaps in a hashtable so they can
6516 be shared among variables with the same points-to set. */
6518 typedef struct shared_bitmap_info
6520 bitmap pt_vars;
6521 hashval_t hashcode;
6522 } *shared_bitmap_info_t;
6523 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
6525 /* Shared_bitmap hashtable helpers. */
6527 struct shared_bitmap_hasher : free_ptr_hash <shared_bitmap_info>
6529 static inline hashval_t hash (const shared_bitmap_info *);
6530 static inline bool equal (const shared_bitmap_info *,
6531 const shared_bitmap_info *);
6534 /* Hash function for a shared_bitmap_info_t */
6536 inline hashval_t
6537 shared_bitmap_hasher::hash (const shared_bitmap_info *bi)
6539 return bi->hashcode;
6542 /* Equality function for two shared_bitmap_info_t's. */
6544 inline bool
6545 shared_bitmap_hasher::equal (const shared_bitmap_info *sbi1,
6546 const shared_bitmap_info *sbi2)
6548 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
6551 /* Shared_bitmap hashtable. */
6553 static hash_table<shared_bitmap_hasher> *shared_bitmap_table;
6555 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
6556 existing instance if there is one, NULL otherwise. */
6558 static bitmap
6559 shared_bitmap_lookup (bitmap pt_vars)
6561 shared_bitmap_info **slot;
6562 struct shared_bitmap_info sbi;
6564 sbi.pt_vars = pt_vars;
6565 sbi.hashcode = bitmap_hash (pt_vars);
6567 slot = shared_bitmap_table->find_slot (&sbi, NO_INSERT);
6568 if (!slot)
6569 return NULL;
6570 else
6571 return (*slot)->pt_vars;
6575 /* Add a bitmap to the shared bitmap hashtable. */
6577 static void
6578 shared_bitmap_add (bitmap pt_vars)
6580 shared_bitmap_info **slot;
6581 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
6583 sbi->pt_vars = pt_vars;
6584 sbi->hashcode = bitmap_hash (pt_vars);
6586 slot = shared_bitmap_table->find_slot (sbi, INSERT);
6587 gcc_assert (!*slot);
6588 *slot = sbi;
6592 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
6594 static void
6595 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt,
6596 tree fndecl)
6598 unsigned int i;
6599 bitmap_iterator bi;
6600 varinfo_t escaped_vi = get_varinfo (find (escaped_id));
6601 bool everything_escaped
6602 = escaped_vi->solution && bitmap_bit_p (escaped_vi->solution, anything_id);
6604 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
6606 varinfo_t vi = get_varinfo (i);
6608 if (vi->is_artificial_var)
6609 continue;
6611 if (everything_escaped
6612 || (escaped_vi->solution
6613 && bitmap_bit_p (escaped_vi->solution, i)))
6615 pt->vars_contains_escaped = true;
6616 pt->vars_contains_escaped_heap |= vi->is_heap_var;
6619 if (vi->is_restrict_var)
6620 pt->vars_contains_restrict = true;
6622 if (VAR_P (vi->decl)
6623 || TREE_CODE (vi->decl) == PARM_DECL
6624 || TREE_CODE (vi->decl) == RESULT_DECL)
6626 /* If we are in IPA mode we will not recompute points-to
6627 sets after inlining so make sure they stay valid. */
6628 if (in_ipa_mode
6629 && !DECL_PT_UID_SET_P (vi->decl))
6630 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
6632 /* Add the decl to the points-to set. Note that the points-to
6633 set contains global variables. */
6634 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
6635 if (vi->is_global_var
6636 /* In IPA mode the escaped_heap trick doesn't work as
6637 ESCAPED is escaped from the unit but
6638 pt_solution_includes_global needs to answer true for
6639 all variables not automatic within a function.
6640 For the same reason is_global_var is not the
6641 correct flag to track - local variables from other
6642 functions also need to be considered global.
6643 Conveniently all HEAP vars are not put in function
6644 scope. */
6645 || (in_ipa_mode
6646 && fndecl
6647 && ! auto_var_in_fn_p (vi->decl, fndecl)))
6648 pt->vars_contains_nonlocal = true;
6650 /* If we have a variable that is interposable record that fact
6651 for pointer comparison simplification. */
6652 if (VAR_P (vi->decl)
6653 && (TREE_STATIC (vi->decl) || DECL_EXTERNAL (vi->decl))
6654 && ! decl_binds_to_current_def_p (vi->decl))
6655 pt->vars_contains_interposable = true;
6657 /* If this is a local variable we can have overlapping lifetime
6658 of different function invocations through recursion duplicate
6659 it with its shadow variable. */
6660 if (in_ipa_mode
6661 && vi->shadow_var_uid != 0)
6663 bitmap_set_bit (into, vi->shadow_var_uid);
6664 pt->vars_contains_nonlocal = true;
6668 else if (TREE_CODE (vi->decl) == FUNCTION_DECL
6669 || TREE_CODE (vi->decl) == LABEL_DECL)
6671 /* Nothing should read/write from/to code so we can
6672 save bits by not including them in the points-to bitmaps.
6673 Still mark the points-to set as containing global memory
6674 to make code-patching possible - see PR70128. */
6675 pt->vars_contains_nonlocal = true;
6681 /* Compute the points-to solution *PT for the variable VI. */
6683 static struct pt_solution
6684 find_what_var_points_to (tree fndecl, varinfo_t orig_vi)
6686 unsigned int i;
6687 bitmap_iterator bi;
6688 bitmap finished_solution;
6689 bitmap result;
6690 varinfo_t vi;
6691 struct pt_solution *pt;
6693 /* This variable may have been collapsed, let's get the real
6694 variable. */
6695 vi = get_varinfo (find (orig_vi->id));
6697 /* See if we have already computed the solution and return it. */
6698 pt_solution **slot = &final_solutions->get_or_insert (vi);
6699 if (*slot != NULL)
6700 return **slot;
6702 *slot = pt = XOBNEW (&final_solutions_obstack, struct pt_solution);
6703 memset (pt, 0, sizeof (struct pt_solution));
6705 /* Translate artificial variables into SSA_NAME_PTR_INFO
6706 attributes. */
6707 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
6709 varinfo_t vi = get_varinfo (i);
6711 if (vi->is_artificial_var)
6713 if (vi->id == nothing_id)
6714 pt->null = 1;
6715 else if (vi->id == escaped_id)
6717 if (in_ipa_mode)
6718 pt->ipa_escaped = 1;
6719 else
6720 pt->escaped = 1;
6721 /* Expand some special vars of ESCAPED in-place here. */
6722 varinfo_t evi = get_varinfo (find (escaped_id));
6723 if (bitmap_bit_p (evi->solution, nonlocal_id))
6724 pt->nonlocal = 1;
6726 else if (vi->id == nonlocal_id)
6727 pt->nonlocal = 1;
6728 else if (vi->id == string_id)
6729 /* Nobody cares - STRING_CSTs are read-only entities. */
6731 else if (vi->id == anything_id
6732 || vi->id == integer_id)
6733 pt->anything = 1;
6737 /* Instead of doing extra work, simply do not create
6738 elaborate points-to information for pt_anything pointers. */
6739 if (pt->anything)
6740 return *pt;
6742 /* Share the final set of variables when possible. */
6743 finished_solution = BITMAP_GGC_ALLOC ();
6744 stats.points_to_sets_created++;
6746 set_uids_in_ptset (finished_solution, vi->solution, pt, fndecl);
6747 result = shared_bitmap_lookup (finished_solution);
6748 if (!result)
6750 shared_bitmap_add (finished_solution);
6751 pt->vars = finished_solution;
6753 else
6755 pt->vars = result;
6756 bitmap_clear (finished_solution);
6759 return *pt;
6762 /* Given a pointer variable P, fill in its points-to set. */
6764 static void
6765 find_what_p_points_to (tree fndecl, tree p)
6767 struct ptr_info_def *pi;
6768 tree lookup_p = p;
6769 varinfo_t vi;
6770 value_range vr;
6771 get_range_query (DECL_STRUCT_FUNCTION (fndecl))->range_of_expr (vr, p);
6772 bool nonnull = vr.nonzero_p ();
6774 /* For parameters, get at the points-to set for the actual parm
6775 decl. */
6776 if (TREE_CODE (p) == SSA_NAME
6777 && SSA_NAME_IS_DEFAULT_DEF (p)
6778 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
6779 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL))
6780 lookup_p = SSA_NAME_VAR (p);
6782 vi = lookup_vi_for_tree (lookup_p);
6783 if (!vi)
6784 return;
6786 pi = get_ptr_info (p);
6787 pi->pt = find_what_var_points_to (fndecl, vi);
6788 /* Conservatively set to NULL from PTA (to true). */
6789 pi->pt.null = 1;
6790 /* Preserve pointer nonnull globally computed. */
6791 if (nonnull)
6792 set_ptr_nonnull (p);
6796 /* Query statistics for points-to solutions. */
6798 static struct {
6799 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
6800 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
6801 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
6802 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
6803 } pta_stats;
6805 void
6806 dump_pta_stats (FILE *s)
6808 fprintf (s, "\nPTA query stats:\n");
6809 fprintf (s, " pt_solution_includes: "
6810 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6811 HOST_WIDE_INT_PRINT_DEC" queries\n",
6812 pta_stats.pt_solution_includes_no_alias,
6813 pta_stats.pt_solution_includes_no_alias
6814 + pta_stats.pt_solution_includes_may_alias);
6815 fprintf (s, " pt_solutions_intersect: "
6816 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6817 HOST_WIDE_INT_PRINT_DEC" queries\n",
6818 pta_stats.pt_solutions_intersect_no_alias,
6819 pta_stats.pt_solutions_intersect_no_alias
6820 + pta_stats.pt_solutions_intersect_may_alias);
6824 /* Reset the points-to solution *PT to a conservative default
6825 (point to anything). */
6827 void
6828 pt_solution_reset (struct pt_solution *pt)
6830 memset (pt, 0, sizeof (struct pt_solution));
6831 pt->anything = true;
6832 pt->null = true;
6835 /* Set the points-to solution *PT to point only to the variables
6836 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6837 global variables and VARS_CONTAINS_RESTRICT specifies whether
6838 it contains restrict tag variables. */
6840 void
6841 pt_solution_set (struct pt_solution *pt, bitmap vars,
6842 bool vars_contains_nonlocal)
6844 memset (pt, 0, sizeof (struct pt_solution));
6845 pt->vars = vars;
6846 pt->vars_contains_nonlocal = vars_contains_nonlocal;
6847 pt->vars_contains_escaped
6848 = (cfun->gimple_df->escaped.anything
6849 || bitmap_intersect_p (cfun->gimple_df->escaped.vars, vars));
6852 /* Set the points-to solution *PT to point only to the variable VAR. */
6854 void
6855 pt_solution_set_var (struct pt_solution *pt, tree var)
6857 memset (pt, 0, sizeof (struct pt_solution));
6858 pt->vars = BITMAP_GGC_ALLOC ();
6859 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
6860 pt->vars_contains_nonlocal = is_global_var (var);
6861 pt->vars_contains_escaped
6862 = (cfun->gimple_df->escaped.anything
6863 || bitmap_bit_p (cfun->gimple_df->escaped.vars, DECL_PT_UID (var)));
6866 /* Computes the union of the points-to solutions *DEST and *SRC and
6867 stores the result in *DEST. This changes the points-to bitmap
6868 of *DEST and thus may not be used if that might be shared.
6869 The points-to bitmap of *SRC and *DEST will not be shared after
6870 this function if they were not before. */
6872 static void
6873 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6875 dest->anything |= src->anything;
6876 if (dest->anything)
6878 pt_solution_reset (dest);
6879 return;
6882 dest->nonlocal |= src->nonlocal;
6883 dest->escaped |= src->escaped;
6884 dest->ipa_escaped |= src->ipa_escaped;
6885 dest->null |= src->null;
6886 dest->vars_contains_nonlocal |= src->vars_contains_nonlocal;
6887 dest->vars_contains_escaped |= src->vars_contains_escaped;
6888 dest->vars_contains_escaped_heap |= src->vars_contains_escaped_heap;
6889 if (!src->vars)
6890 return;
6892 if (!dest->vars)
6893 dest->vars = BITMAP_GGC_ALLOC ();
6894 bitmap_ior_into (dest->vars, src->vars);
6897 /* Return true if the points-to solution *PT is empty. */
6899 bool
6900 pt_solution_empty_p (const pt_solution *pt)
6902 if (pt->anything
6903 || pt->nonlocal)
6904 return false;
6906 if (pt->vars
6907 && !bitmap_empty_p (pt->vars))
6908 return false;
6910 /* If the solution includes ESCAPED, check if that is empty. */
6911 if (pt->escaped
6912 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6913 return false;
6915 /* If the solution includes ESCAPED, check if that is empty. */
6916 if (pt->ipa_escaped
6917 && !pt_solution_empty_p (&ipa_escaped_pt))
6918 return false;
6920 return true;
6923 /* Return true if the points-to solution *PT only point to a single var, and
6924 return the var uid in *UID. */
6926 bool
6927 pt_solution_singleton_or_null_p (struct pt_solution *pt, unsigned *uid)
6929 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
6930 || pt->vars == NULL
6931 || !bitmap_single_bit_set_p (pt->vars))
6932 return false;
6934 *uid = bitmap_first_set_bit (pt->vars);
6935 return true;
6938 /* Return true if the points-to solution *PT includes global memory. */
6940 bool
6941 pt_solution_includes_global (struct pt_solution *pt)
6943 if (pt->anything
6944 || pt->nonlocal
6945 || pt->vars_contains_nonlocal
6946 /* The following is a hack to make the malloc escape hack work.
6947 In reality we'd need different sets for escaped-through-return
6948 and escaped-to-callees and passes would need to be updated. */
6949 || pt->vars_contains_escaped_heap)
6950 return true;
6952 /* 'escaped' is also a placeholder so we have to look into it. */
6953 if (pt->escaped)
6954 return pt_solution_includes_global (&cfun->gimple_df->escaped);
6956 if (pt->ipa_escaped)
6957 return pt_solution_includes_global (&ipa_escaped_pt);
6959 return false;
6962 /* Return true if the points-to solution *PT includes the variable
6963 declaration DECL. */
6965 static bool
6966 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6968 if (pt->anything)
6969 return true;
6971 if (pt->nonlocal
6972 && is_global_var (decl))
6973 return true;
6975 if (pt->vars
6976 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6977 return true;
6979 /* If the solution includes ESCAPED, check it. */
6980 if (pt->escaped
6981 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6982 return true;
6984 /* If the solution includes ESCAPED, check it. */
6985 if (pt->ipa_escaped
6986 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6987 return true;
6989 return false;
6992 bool
6993 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6995 bool res = pt_solution_includes_1 (pt, decl);
6996 if (res)
6997 ++pta_stats.pt_solution_includes_may_alias;
6998 else
6999 ++pta_stats.pt_solution_includes_no_alias;
7000 return res;
7003 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
7004 intersection. */
7006 static bool
7007 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
7009 if (pt1->anything || pt2->anything)
7010 return true;
7012 /* If either points to unknown global memory and the other points to
7013 any global memory they alias. */
7014 if ((pt1->nonlocal
7015 && (pt2->nonlocal
7016 || pt2->vars_contains_nonlocal))
7017 || (pt2->nonlocal
7018 && pt1->vars_contains_nonlocal))
7019 return true;
7021 /* If either points to all escaped memory and the other points to
7022 any escaped memory they alias. */
7023 if ((pt1->escaped
7024 && (pt2->escaped
7025 || pt2->vars_contains_escaped))
7026 || (pt2->escaped
7027 && pt1->vars_contains_escaped))
7028 return true;
7030 /* Check the escaped solution if required.
7031 ??? Do we need to check the local against the IPA escaped sets? */
7032 if ((pt1->ipa_escaped || pt2->ipa_escaped)
7033 && !pt_solution_empty_p (&ipa_escaped_pt))
7035 /* If both point to escaped memory and that solution
7036 is not empty they alias. */
7037 if (pt1->ipa_escaped && pt2->ipa_escaped)
7038 return true;
7040 /* If either points to escaped memory see if the escaped solution
7041 intersects with the other. */
7042 if ((pt1->ipa_escaped
7043 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
7044 || (pt2->ipa_escaped
7045 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
7046 return true;
7049 /* Now both pointers alias if their points-to solution intersects. */
7050 return (pt1->vars
7051 && pt2->vars
7052 && bitmap_intersect_p (pt1->vars, pt2->vars));
7055 bool
7056 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
7058 bool res = pt_solutions_intersect_1 (pt1, pt2);
7059 if (res)
7060 ++pta_stats.pt_solutions_intersect_may_alias;
7061 else
7062 ++pta_stats.pt_solutions_intersect_no_alias;
7063 return res;
7067 /* Dump points-to information to OUTFILE. */
7069 static void
7070 dump_sa_points_to_info (FILE *outfile)
7072 unsigned int i;
7074 fprintf (outfile, "\nPoints-to sets\n\n");
7076 if (dump_flags & TDF_STATS)
7078 fprintf (outfile, "Stats:\n");
7079 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
7080 fprintf (outfile, "Non-pointer vars: %d\n",
7081 stats.nonpointer_vars);
7082 fprintf (outfile, "Statically unified vars: %d\n",
7083 stats.unified_vars_static);
7084 fprintf (outfile, "Dynamically unified vars: %d\n",
7085 stats.unified_vars_dynamic);
7086 fprintf (outfile, "Iterations: %d\n", stats.iterations);
7087 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
7088 fprintf (outfile, "Number of implicit edges: %d\n",
7089 stats.num_implicit_edges);
7092 for (i = 1; i < varmap.length (); i++)
7094 varinfo_t vi = get_varinfo (i);
7095 if (!vi->may_have_pointers)
7096 continue;
7097 dump_solution_for_var (outfile, i);
7102 /* Debug points-to information to stderr. */
7104 DEBUG_FUNCTION void
7105 debug_sa_points_to_info (void)
7107 dump_sa_points_to_info (stderr);
7111 /* Initialize the always-existing constraint variables for NULL
7112 ANYTHING, READONLY, and INTEGER */
7114 static void
7115 init_base_vars (void)
7117 struct constraint_expr lhs, rhs;
7118 varinfo_t var_anything;
7119 varinfo_t var_nothing;
7120 varinfo_t var_string;
7121 varinfo_t var_escaped;
7122 varinfo_t var_nonlocal;
7123 varinfo_t var_storedanything;
7124 varinfo_t var_integer;
7126 /* Variable ID zero is reserved and should be NULL. */
7127 varmap.safe_push (NULL);
7129 /* Create the NULL variable, used to represent that a variable points
7130 to NULL. */
7131 var_nothing = new_var_info (NULL_TREE, "NULL", false);
7132 gcc_assert (var_nothing->id == nothing_id);
7133 var_nothing->is_artificial_var = 1;
7134 var_nothing->offset = 0;
7135 var_nothing->size = ~0;
7136 var_nothing->fullsize = ~0;
7137 var_nothing->is_special_var = 1;
7138 var_nothing->may_have_pointers = 0;
7139 var_nothing->is_global_var = 0;
7141 /* Create the ANYTHING variable, used to represent that a variable
7142 points to some unknown piece of memory. */
7143 var_anything = new_var_info (NULL_TREE, "ANYTHING", false);
7144 gcc_assert (var_anything->id == anything_id);
7145 var_anything->is_artificial_var = 1;
7146 var_anything->size = ~0;
7147 var_anything->offset = 0;
7148 var_anything->fullsize = ~0;
7149 var_anything->is_special_var = 1;
7151 /* Anything points to anything. This makes deref constraints just
7152 work in the presence of linked list and other p = *p type loops,
7153 by saying that *ANYTHING = ANYTHING. */
7154 lhs.type = SCALAR;
7155 lhs.var = anything_id;
7156 lhs.offset = 0;
7157 rhs.type = ADDRESSOF;
7158 rhs.var = anything_id;
7159 rhs.offset = 0;
7161 /* This specifically does not use process_constraint because
7162 process_constraint ignores all anything = anything constraints, since all
7163 but this one are redundant. */
7164 constraints.safe_push (new_constraint (lhs, rhs));
7166 /* Create the STRING variable, used to represent that a variable
7167 points to a string literal. String literals don't contain
7168 pointers so STRING doesn't point to anything. */
7169 var_string = new_var_info (NULL_TREE, "STRING", false);
7170 gcc_assert (var_string->id == string_id);
7171 var_string->is_artificial_var = 1;
7172 var_string->offset = 0;
7173 var_string->size = ~0;
7174 var_string->fullsize = ~0;
7175 var_string->is_special_var = 1;
7176 var_string->may_have_pointers = 0;
7178 /* Create the ESCAPED variable, used to represent the set of escaped
7179 memory. */
7180 var_escaped = new_var_info (NULL_TREE, "ESCAPED", false);
7181 gcc_assert (var_escaped->id == escaped_id);
7182 var_escaped->is_artificial_var = 1;
7183 var_escaped->offset = 0;
7184 var_escaped->size = ~0;
7185 var_escaped->fullsize = ~0;
7186 var_escaped->is_special_var = 0;
7188 /* Create the NONLOCAL variable, used to represent the set of nonlocal
7189 memory. */
7190 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL", false);
7191 gcc_assert (var_nonlocal->id == nonlocal_id);
7192 var_nonlocal->is_artificial_var = 1;
7193 var_nonlocal->offset = 0;
7194 var_nonlocal->size = ~0;
7195 var_nonlocal->fullsize = ~0;
7196 var_nonlocal->is_special_var = 1;
7198 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
7199 lhs.type = SCALAR;
7200 lhs.var = escaped_id;
7201 lhs.offset = 0;
7202 rhs.type = DEREF;
7203 rhs.var = escaped_id;
7204 rhs.offset = 0;
7205 process_constraint (new_constraint (lhs, rhs));
7207 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
7208 whole variable escapes. */
7209 lhs.type = SCALAR;
7210 lhs.var = escaped_id;
7211 lhs.offset = 0;
7212 rhs.type = SCALAR;
7213 rhs.var = escaped_id;
7214 rhs.offset = UNKNOWN_OFFSET;
7215 process_constraint (new_constraint (lhs, rhs));
7217 /* *ESCAPED = NONLOCAL. This is true because we have to assume
7218 everything pointed to by escaped points to what global memory can
7219 point to. */
7220 lhs.type = DEREF;
7221 lhs.var = escaped_id;
7222 lhs.offset = 0;
7223 rhs.type = SCALAR;
7224 rhs.var = nonlocal_id;
7225 rhs.offset = 0;
7226 process_constraint (new_constraint (lhs, rhs));
7228 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
7229 global memory may point to global memory and escaped memory. */
7230 lhs.type = SCALAR;
7231 lhs.var = nonlocal_id;
7232 lhs.offset = 0;
7233 rhs.type = ADDRESSOF;
7234 rhs.var = nonlocal_id;
7235 rhs.offset = 0;
7236 process_constraint (new_constraint (lhs, rhs));
7237 rhs.type = ADDRESSOF;
7238 rhs.var = escaped_id;
7239 rhs.offset = 0;
7240 process_constraint (new_constraint (lhs, rhs));
7242 /* Create the STOREDANYTHING variable, used to represent the set of
7243 variables stored to *ANYTHING. */
7244 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING", false);
7245 gcc_assert (var_storedanything->id == storedanything_id);
7246 var_storedanything->is_artificial_var = 1;
7247 var_storedanything->offset = 0;
7248 var_storedanything->size = ~0;
7249 var_storedanything->fullsize = ~0;
7250 var_storedanything->is_special_var = 0;
7252 /* Create the INTEGER variable, used to represent that a variable points
7253 to what an INTEGER "points to". */
7254 var_integer = new_var_info (NULL_TREE, "INTEGER", false);
7255 gcc_assert (var_integer->id == integer_id);
7256 var_integer->is_artificial_var = 1;
7257 var_integer->size = ~0;
7258 var_integer->fullsize = ~0;
7259 var_integer->offset = 0;
7260 var_integer->is_special_var = 1;
7262 /* INTEGER = ANYTHING, because we don't know where a dereference of
7263 a random integer will point to. */
7264 lhs.type = SCALAR;
7265 lhs.var = integer_id;
7266 lhs.offset = 0;
7267 rhs.type = ADDRESSOF;
7268 rhs.var = anything_id;
7269 rhs.offset = 0;
7270 process_constraint (new_constraint (lhs, rhs));
7273 /* Initialize things necessary to perform PTA */
7275 static void
7276 init_alias_vars (void)
7278 use_field_sensitive = (param_max_fields_for_field_sensitive > 1);
7280 bitmap_obstack_initialize (&pta_obstack);
7281 bitmap_obstack_initialize (&oldpta_obstack);
7282 bitmap_obstack_initialize (&predbitmap_obstack);
7284 constraints.create (8);
7285 varmap.create (8);
7286 vi_for_tree = new hash_map<tree, varinfo_t>;
7287 call_stmt_vars = new hash_map<gimple *, varinfo_t>;
7289 memset (&stats, 0, sizeof (stats));
7290 shared_bitmap_table = new hash_table<shared_bitmap_hasher> (511);
7291 init_base_vars ();
7293 gcc_obstack_init (&fake_var_decl_obstack);
7295 final_solutions = new hash_map<varinfo_t, pt_solution *>;
7296 gcc_obstack_init (&final_solutions_obstack);
7299 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
7300 predecessor edges. */
7302 static void
7303 remove_preds_and_fake_succs (constraint_graph_t graph)
7305 unsigned int i;
7307 /* Clear the implicit ref and address nodes from the successor
7308 lists. */
7309 for (i = 1; i < FIRST_REF_NODE; i++)
7311 if (graph->succs[i])
7312 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
7313 FIRST_REF_NODE * 2);
7316 /* Free the successor list for the non-ref nodes. */
7317 for (i = FIRST_REF_NODE + 1; i < graph->size; i++)
7319 if (graph->succs[i])
7320 BITMAP_FREE (graph->succs[i]);
7323 /* Now reallocate the size of the successor list as, and blow away
7324 the predecessor bitmaps. */
7325 graph->size = varmap.length ();
7326 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
7328 free (graph->implicit_preds);
7329 graph->implicit_preds = NULL;
7330 free (graph->preds);
7331 graph->preds = NULL;
7332 bitmap_obstack_release (&predbitmap_obstack);
7335 /* Solve the constraint set. */
7337 static void
7338 solve_constraints (void)
7340 class scc_info *si;
7342 /* Sort varinfos so that ones that cannot be pointed to are last.
7343 This makes bitmaps more efficient. */
7344 unsigned int *map = XNEWVEC (unsigned int, varmap.length ());
7345 for (unsigned i = 0; i < integer_id + 1; ++i)
7346 map[i] = i;
7347 /* Start with address-taken vars, followed by not address-taken vars
7348 to move vars never appearing in the points-to solution bitmaps last. */
7349 unsigned j = integer_id + 1;
7350 for (unsigned i = integer_id + 1; i < varmap.length (); ++i)
7351 if (varmap[varmap[i]->head]->address_taken)
7352 map[i] = j++;
7353 for (unsigned i = integer_id + 1; i < varmap.length (); ++i)
7354 if (! varmap[varmap[i]->head]->address_taken)
7355 map[i] = j++;
7356 /* Shuffle varmap according to map. */
7357 for (unsigned i = integer_id + 1; i < varmap.length (); ++i)
7359 while (map[varmap[i]->id] != i)
7360 std::swap (varmap[i], varmap[map[varmap[i]->id]]);
7361 gcc_assert (bitmap_empty_p (varmap[i]->solution));
7362 varmap[i]->id = i;
7363 varmap[i]->next = map[varmap[i]->next];
7364 varmap[i]->head = map[varmap[i]->head];
7366 /* Finally rewrite constraints. */
7367 for (unsigned i = 0; i < constraints.length (); ++i)
7369 constraints[i]->lhs.var = map[constraints[i]->lhs.var];
7370 constraints[i]->rhs.var = map[constraints[i]->rhs.var];
7372 free (map);
7374 if (dump_file)
7375 fprintf (dump_file,
7376 "\nCollapsing static cycles and doing variable "
7377 "substitution\n");
7379 init_graph (varmap.length () * 2);
7381 if (dump_file)
7382 fprintf (dump_file, "Building predecessor graph\n");
7383 build_pred_graph ();
7385 if (dump_file)
7386 fprintf (dump_file, "Detecting pointer and location "
7387 "equivalences\n");
7388 si = perform_var_substitution (graph);
7390 if (dump_file)
7391 fprintf (dump_file, "Rewriting constraints and unifying "
7392 "variables\n");
7393 rewrite_constraints (graph, si);
7395 build_succ_graph ();
7397 free_var_substitution_info (si);
7399 /* Attach complex constraints to graph nodes. */
7400 move_complex_constraints (graph);
7402 if (dump_file)
7403 fprintf (dump_file, "Uniting pointer but not location equivalent "
7404 "variables\n");
7405 unite_pointer_equivalences (graph);
7407 if (dump_file)
7408 fprintf (dump_file, "Finding indirect cycles\n");
7409 find_indirect_cycles (graph);
7411 /* Implicit nodes and predecessors are no longer necessary at this
7412 point. */
7413 remove_preds_and_fake_succs (graph);
7415 if (dump_file && (dump_flags & TDF_GRAPH))
7417 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
7418 "in dot format:\n");
7419 dump_constraint_graph (dump_file);
7420 fprintf (dump_file, "\n\n");
7423 if (dump_file)
7424 fprintf (dump_file, "Solving graph\n");
7426 solve_graph (graph);
7428 if (dump_file && (dump_flags & TDF_GRAPH))
7430 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
7431 "in dot format:\n");
7432 dump_constraint_graph (dump_file);
7433 fprintf (dump_file, "\n\n");
7437 /* Create points-to sets for the current function. See the comments
7438 at the start of the file for an algorithmic overview. */
7440 static void
7441 compute_points_to_sets (void)
7443 basic_block bb;
7444 varinfo_t vi;
7446 timevar_push (TV_TREE_PTA);
7448 init_alias_vars ();
7450 intra_create_variable_infos (cfun);
7452 /* Now walk all statements and build the constraint set. */
7453 FOR_EACH_BB_FN (bb, cfun)
7455 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7456 gsi_next (&gsi))
7458 gphi *phi = gsi.phi ();
7460 if (! virtual_operand_p (gimple_phi_result (phi)))
7461 find_func_aliases (cfun, phi);
7464 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
7465 gsi_next (&gsi))
7467 gimple *stmt = gsi_stmt (gsi);
7469 find_func_aliases (cfun, stmt);
7473 if (dump_file)
7475 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
7476 dump_constraints (dump_file, 0);
7479 /* From the constraints compute the points-to sets. */
7480 solve_constraints ();
7482 /* Post-process solutions for escapes through returns. */
7483 edge_iterator ei;
7484 edge e;
7485 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
7486 if (greturn *ret = safe_dyn_cast <greturn *> (last_stmt (e->src)))
7488 tree val = gimple_return_retval (ret);
7489 /* ??? Easy to handle simple indirections with some work.
7490 Arbitrary references like foo.bar.baz are more difficult
7491 (but conservatively easy enough with just looking at the base).
7492 Mind to fixup find_func_aliases as well. */
7493 if (!val || !SSA_VAR_P (val))
7494 continue;
7495 /* returns happen last in non-IPA so they only influence
7496 the ESCAPED solution and we can filter local variables. */
7497 varinfo_t escaped_vi = get_varinfo (find (escaped_id));
7498 varinfo_t vi = lookup_vi_for_tree (val);
7499 bitmap delta = BITMAP_ALLOC (&pta_obstack);
7500 bitmap_iterator bi;
7501 unsigned i;
7502 for (; vi; vi = vi_next (vi))
7504 varinfo_t part_vi = get_varinfo (find (vi->id));
7505 EXECUTE_IF_AND_COMPL_IN_BITMAP (part_vi->solution,
7506 escaped_vi->solution, 0, i, bi)
7508 varinfo_t pointed_to_vi = get_varinfo (i);
7509 if (pointed_to_vi->is_global_var
7510 /* We delay marking of heap memory as global. */
7511 || pointed_to_vi->is_heap_var)
7512 bitmap_set_bit (delta, i);
7516 /* Now compute the transitive closure. */
7517 bitmap_ior_into (escaped_vi->solution, delta);
7518 bitmap new_delta = BITMAP_ALLOC (&pta_obstack);
7519 while (!bitmap_empty_p (delta))
7521 EXECUTE_IF_SET_IN_BITMAP (delta, 0, i, bi)
7523 varinfo_t pointed_to_vi = get_varinfo (i);
7524 pointed_to_vi = get_varinfo (find (pointed_to_vi->id));
7525 unsigned j;
7526 bitmap_iterator bi2;
7527 EXECUTE_IF_AND_COMPL_IN_BITMAP (pointed_to_vi->solution,
7528 escaped_vi->solution,
7529 0, j, bi2)
7531 varinfo_t pointed_to_vi2 = get_varinfo (j);
7532 if (pointed_to_vi2->is_global_var
7533 /* We delay marking of heap memory as global. */
7534 || pointed_to_vi2->is_heap_var)
7535 bitmap_set_bit (new_delta, j);
7538 bitmap_ior_into (escaped_vi->solution, new_delta);
7539 bitmap_clear (delta);
7540 std::swap (delta, new_delta);
7542 BITMAP_FREE (delta);
7543 BITMAP_FREE (new_delta);
7546 if (dump_file)
7547 dump_sa_points_to_info (dump_file);
7549 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
7550 cfun->gimple_df->escaped = find_what_var_points_to (cfun->decl,
7551 get_varinfo (escaped_id));
7553 /* Make sure the ESCAPED solution (which is used as placeholder in
7554 other solutions) does not reference itself. This simplifies
7555 points-to solution queries. */
7556 cfun->gimple_df->escaped.escaped = 0;
7558 /* Compute the points-to sets for pointer SSA_NAMEs. */
7559 unsigned i;
7560 tree ptr;
7562 FOR_EACH_SSA_NAME (i, ptr, cfun)
7564 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
7565 find_what_p_points_to (cfun->decl, ptr);
7568 /* Compute the call-used/clobbered sets. */
7569 FOR_EACH_BB_FN (bb, cfun)
7571 gimple_stmt_iterator gsi;
7573 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7575 gcall *stmt;
7576 struct pt_solution *pt;
7578 stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
7579 if (!stmt)
7580 continue;
7582 pt = gimple_call_use_set (stmt);
7583 if (gimple_call_flags (stmt) & ECF_CONST)
7584 memset (pt, 0, sizeof (struct pt_solution));
7585 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7587 *pt = find_what_var_points_to (cfun->decl, vi);
7588 /* Escaped (and thus nonlocal) variables are always
7589 implicitly used by calls. */
7590 /* ??? ESCAPED can be empty even though NONLOCAL
7591 always escaped. */
7592 pt->nonlocal = 1;
7593 pt->escaped = 1;
7595 else
7597 /* If there is nothing special about this call then
7598 we have made everything that is used also escape. */
7599 *pt = cfun->gimple_df->escaped;
7600 pt->nonlocal = 1;
7603 pt = gimple_call_clobber_set (stmt);
7604 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7605 memset (pt, 0, sizeof (struct pt_solution));
7606 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7608 *pt = find_what_var_points_to (cfun->decl, vi);
7609 /* Escaped (and thus nonlocal) variables are always
7610 implicitly clobbered by calls. */
7611 /* ??? ESCAPED can be empty even though NONLOCAL
7612 always escaped. */
7613 pt->nonlocal = 1;
7614 pt->escaped = 1;
7616 else
7618 /* If there is nothing special about this call then
7619 we have made everything that is used also escape. */
7620 *pt = cfun->gimple_df->escaped;
7621 pt->nonlocal = 1;
7626 timevar_pop (TV_TREE_PTA);
7630 /* Delete created points-to sets. */
7632 static void
7633 delete_points_to_sets (void)
7635 unsigned int i;
7637 delete shared_bitmap_table;
7638 shared_bitmap_table = NULL;
7639 if (dump_file && (dump_flags & TDF_STATS))
7640 fprintf (dump_file, "Points to sets created:%d\n",
7641 stats.points_to_sets_created);
7643 delete vi_for_tree;
7644 delete call_stmt_vars;
7645 bitmap_obstack_release (&pta_obstack);
7646 constraints.release ();
7648 for (i = 0; i < graph->size; i++)
7649 graph->complex[i].release ();
7650 free (graph->complex);
7652 free (graph->rep);
7653 free (graph->succs);
7654 free (graph->pe);
7655 free (graph->pe_rep);
7656 free (graph->indirect_cycles);
7657 free (graph);
7659 varmap.release ();
7660 variable_info_pool.release ();
7661 constraint_pool.release ();
7663 obstack_free (&fake_var_decl_obstack, NULL);
7665 delete final_solutions;
7666 obstack_free (&final_solutions_obstack, NULL);
7669 struct vls_data
7671 unsigned short clique;
7672 bool escaped_p;
7673 bitmap rvars;
7676 /* Mark "other" loads and stores as belonging to CLIQUE and with
7677 base zero. */
7679 static bool
7680 visit_loadstore (gimple *, tree base, tree ref, void *data)
7682 unsigned short clique = ((vls_data *) data)->clique;
7683 bitmap rvars = ((vls_data *) data)->rvars;
7684 bool escaped_p = ((vls_data *) data)->escaped_p;
7685 if (TREE_CODE (base) == MEM_REF
7686 || TREE_CODE (base) == TARGET_MEM_REF)
7688 tree ptr = TREE_OPERAND (base, 0);
7689 if (TREE_CODE (ptr) == SSA_NAME)
7691 /* For parameters, get at the points-to set for the actual parm
7692 decl. */
7693 if (SSA_NAME_IS_DEFAULT_DEF (ptr)
7694 && (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
7695 || TREE_CODE (SSA_NAME_VAR (ptr)) == RESULT_DECL))
7696 ptr = SSA_NAME_VAR (ptr);
7698 /* We need to make sure 'ptr' doesn't include any of
7699 the restrict tags we added bases for in its points-to set. */
7700 varinfo_t vi = lookup_vi_for_tree (ptr);
7701 if (! vi)
7702 return false;
7704 vi = get_varinfo (find (vi->id));
7705 if (bitmap_intersect_p (rvars, vi->solution)
7706 || (escaped_p && bitmap_bit_p (vi->solution, escaped_id)))
7707 return false;
7710 /* Do not overwrite existing cliques (that includes clique, base
7711 pairs we just set). */
7712 if (MR_DEPENDENCE_CLIQUE (base) == 0)
7714 MR_DEPENDENCE_CLIQUE (base) = clique;
7715 MR_DEPENDENCE_BASE (base) = 0;
7719 /* For plain decl accesses see whether they are accesses to globals
7720 and rewrite them to MEM_REFs with { clique, 0 }. */
7721 if (VAR_P (base)
7722 && is_global_var (base)
7723 /* ??? We can't rewrite a plain decl with the walk_stmt_load_store
7724 ops callback. */
7725 && base != ref)
7727 tree *basep = &ref;
7728 while (handled_component_p (*basep))
7729 basep = &TREE_OPERAND (*basep, 0);
7730 gcc_assert (VAR_P (*basep));
7731 tree ptr = build_fold_addr_expr (*basep);
7732 tree zero = build_int_cst (TREE_TYPE (ptr), 0);
7733 *basep = build2 (MEM_REF, TREE_TYPE (*basep), ptr, zero);
7734 MR_DEPENDENCE_CLIQUE (*basep) = clique;
7735 MR_DEPENDENCE_BASE (*basep) = 0;
7738 return false;
7741 struct msdi_data {
7742 tree ptr;
7743 unsigned short *clique;
7744 unsigned short *last_ruid;
7745 varinfo_t restrict_var;
7748 /* If BASE is a MEM_REF then assign a clique, base pair to it, updating
7749 CLIQUE, *RESTRICT_VAR and LAST_RUID as passed via DATA.
7750 Return whether dependence info was assigned to BASE. */
7752 static bool
7753 maybe_set_dependence_info (gimple *, tree base, tree, void *data)
7755 tree ptr = ((msdi_data *)data)->ptr;
7756 unsigned short &clique = *((msdi_data *)data)->clique;
7757 unsigned short &last_ruid = *((msdi_data *)data)->last_ruid;
7758 varinfo_t restrict_var = ((msdi_data *)data)->restrict_var;
7759 if ((TREE_CODE (base) == MEM_REF
7760 || TREE_CODE (base) == TARGET_MEM_REF)
7761 && TREE_OPERAND (base, 0) == ptr)
7763 /* Do not overwrite existing cliques. This avoids overwriting dependence
7764 info inlined from a function with restrict parameters inlined
7765 into a function with restrict parameters. This usually means we
7766 prefer to be precise in innermost loops. */
7767 if (MR_DEPENDENCE_CLIQUE (base) == 0)
7769 if (clique == 0)
7771 if (cfun->last_clique == 0)
7772 cfun->last_clique = 1;
7773 clique = 1;
7775 if (restrict_var->ruid == 0)
7776 restrict_var->ruid = ++last_ruid;
7777 MR_DEPENDENCE_CLIQUE (base) = clique;
7778 MR_DEPENDENCE_BASE (base) = restrict_var->ruid;
7779 return true;
7782 return false;
7785 /* Clear dependence info for the clique DATA. */
7787 static bool
7788 clear_dependence_clique (gimple *, tree base, tree, void *data)
7790 unsigned short clique = (uintptr_t)data;
7791 if ((TREE_CODE (base) == MEM_REF
7792 || TREE_CODE (base) == TARGET_MEM_REF)
7793 && MR_DEPENDENCE_CLIQUE (base) == clique)
7795 MR_DEPENDENCE_CLIQUE (base) = 0;
7796 MR_DEPENDENCE_BASE (base) = 0;
7799 return false;
7802 /* Compute the set of independend memory references based on restrict
7803 tags and their conservative propagation to the points-to sets. */
7805 static void
7806 compute_dependence_clique (void)
7808 /* First clear the special "local" clique. */
7809 basic_block bb;
7810 if (cfun->last_clique != 0)
7811 FOR_EACH_BB_FN (bb, cfun)
7812 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
7813 !gsi_end_p (gsi); gsi_next (&gsi))
7815 gimple *stmt = gsi_stmt (gsi);
7816 walk_stmt_load_store_ops (stmt, (void *)(uintptr_t) 1,
7817 clear_dependence_clique,
7818 clear_dependence_clique);
7821 unsigned short clique = 0;
7822 unsigned short last_ruid = 0;
7823 bitmap rvars = BITMAP_ALLOC (NULL);
7824 bool escaped_p = false;
7825 for (unsigned i = 0; i < num_ssa_names; ++i)
7827 tree ptr = ssa_name (i);
7828 if (!ptr || !POINTER_TYPE_P (TREE_TYPE (ptr)))
7829 continue;
7831 /* Avoid all this when ptr is not dereferenced? */
7832 tree p = ptr;
7833 if (SSA_NAME_IS_DEFAULT_DEF (ptr)
7834 && (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
7835 || TREE_CODE (SSA_NAME_VAR (ptr)) == RESULT_DECL))
7836 p = SSA_NAME_VAR (ptr);
7837 varinfo_t vi = lookup_vi_for_tree (p);
7838 if (!vi)
7839 continue;
7840 vi = get_varinfo (find (vi->id));
7841 bitmap_iterator bi;
7842 unsigned j;
7843 varinfo_t restrict_var = NULL;
7844 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, j, bi)
7846 varinfo_t oi = get_varinfo (j);
7847 if (oi->head != j)
7848 oi = get_varinfo (oi->head);
7849 if (oi->is_restrict_var)
7851 if (restrict_var
7852 && restrict_var != oi)
7854 if (dump_file && (dump_flags & TDF_DETAILS))
7856 fprintf (dump_file, "found restrict pointed-to "
7857 "for ");
7858 print_generic_expr (dump_file, ptr);
7859 fprintf (dump_file, " but not exclusively\n");
7861 restrict_var = NULL;
7862 break;
7864 restrict_var = oi;
7866 /* NULL is the only other valid points-to entry. */
7867 else if (oi->id != nothing_id)
7869 restrict_var = NULL;
7870 break;
7873 /* Ok, found that ptr must(!) point to a single(!) restrict
7874 variable. */
7875 /* ??? PTA isn't really a proper propagation engine to compute
7876 this property.
7877 ??? We could handle merging of two restricts by unifying them. */
7878 if (restrict_var)
7880 /* Now look at possible dereferences of ptr. */
7881 imm_use_iterator ui;
7882 gimple *use_stmt;
7883 bool used = false;
7884 msdi_data data = { ptr, &clique, &last_ruid, restrict_var };
7885 FOR_EACH_IMM_USE_STMT (use_stmt, ui, ptr)
7886 used |= walk_stmt_load_store_ops (use_stmt, &data,
7887 maybe_set_dependence_info,
7888 maybe_set_dependence_info);
7889 if (used)
7891 /* Add all subvars to the set of restrict pointed-to set. */
7892 for (unsigned sv = restrict_var->head; sv != 0;
7893 sv = get_varinfo (sv)->next)
7894 bitmap_set_bit (rvars, sv);
7895 varinfo_t escaped = get_varinfo (find (escaped_id));
7896 if (bitmap_bit_p (escaped->solution, restrict_var->id))
7897 escaped_p = true;
7902 if (clique != 0)
7904 /* Assign the BASE id zero to all accesses not based on a restrict
7905 pointer. That way they get disambiguated against restrict
7906 accesses but not against each other. */
7907 /* ??? For restricts derived from globals (thus not incoming
7908 parameters) we can't restrict scoping properly thus the following
7909 is too aggressive there. For now we have excluded those globals from
7910 getting into the MR_DEPENDENCE machinery. */
7911 vls_data data = { clique, escaped_p, rvars };
7912 basic_block bb;
7913 FOR_EACH_BB_FN (bb, cfun)
7914 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
7915 !gsi_end_p (gsi); gsi_next (&gsi))
7917 gimple *stmt = gsi_stmt (gsi);
7918 walk_stmt_load_store_ops (stmt, &data,
7919 visit_loadstore, visit_loadstore);
7923 BITMAP_FREE (rvars);
7926 /* Compute points-to information for every SSA_NAME pointer in the
7927 current function and compute the transitive closure of escaped
7928 variables to re-initialize the call-clobber states of local variables. */
7930 unsigned int
7931 compute_may_aliases (void)
7933 if (cfun->gimple_df->ipa_pta)
7935 if (dump_file)
7937 fprintf (dump_file, "\nNot re-computing points-to information "
7938 "because IPA points-to information is available.\n\n");
7940 /* But still dump what we have remaining it. */
7941 dump_alias_info (dump_file);
7944 return 0;
7947 /* For each pointer P_i, determine the sets of variables that P_i may
7948 point-to. Compute the reachability set of escaped and call-used
7949 variables. */
7950 compute_points_to_sets ();
7952 /* Debugging dumps. */
7953 if (dump_file)
7954 dump_alias_info (dump_file);
7956 /* Compute restrict-based memory disambiguations. */
7957 compute_dependence_clique ();
7959 /* Deallocate memory used by aliasing data structures and the internal
7960 points-to solution. */
7961 delete_points_to_sets ();
7963 gcc_assert (!need_ssa_update_p (cfun));
7965 return 0;
7968 /* A dummy pass to cause points-to information to be computed via
7969 TODO_rebuild_alias. */
7971 namespace {
7973 const pass_data pass_data_build_alias =
7975 GIMPLE_PASS, /* type */
7976 "alias", /* name */
7977 OPTGROUP_NONE, /* optinfo_flags */
7978 TV_NONE, /* tv_id */
7979 ( PROP_cfg | PROP_ssa ), /* properties_required */
7980 0, /* properties_provided */
7981 0, /* properties_destroyed */
7982 0, /* todo_flags_start */
7983 TODO_rebuild_alias, /* todo_flags_finish */
7986 class pass_build_alias : public gimple_opt_pass
7988 public:
7989 pass_build_alias (gcc::context *ctxt)
7990 : gimple_opt_pass (pass_data_build_alias, ctxt)
7993 /* opt_pass methods: */
7994 virtual bool gate (function *) { return flag_tree_pta; }
7996 }; // class pass_build_alias
7998 } // anon namespace
8000 gimple_opt_pass *
8001 make_pass_build_alias (gcc::context *ctxt)
8003 return new pass_build_alias (ctxt);
8006 /* A dummy pass to cause points-to information to be computed via
8007 TODO_rebuild_alias. */
8009 namespace {
8011 const pass_data pass_data_build_ealias =
8013 GIMPLE_PASS, /* type */
8014 "ealias", /* name */
8015 OPTGROUP_NONE, /* optinfo_flags */
8016 TV_NONE, /* tv_id */
8017 ( PROP_cfg | PROP_ssa ), /* properties_required */
8018 0, /* properties_provided */
8019 0, /* properties_destroyed */
8020 0, /* todo_flags_start */
8021 TODO_rebuild_alias, /* todo_flags_finish */
8024 class pass_build_ealias : public gimple_opt_pass
8026 public:
8027 pass_build_ealias (gcc::context *ctxt)
8028 : gimple_opt_pass (pass_data_build_ealias, ctxt)
8031 /* opt_pass methods: */
8032 virtual bool gate (function *) { return flag_tree_pta; }
8034 }; // class pass_build_ealias
8036 } // anon namespace
8038 gimple_opt_pass *
8039 make_pass_build_ealias (gcc::context *ctxt)
8041 return new pass_build_ealias (ctxt);
8045 /* IPA PTA solutions for ESCAPED. */
8046 struct pt_solution ipa_escaped_pt
8047 = { true, false, false, false, false,
8048 false, false, false, false, false, NULL };
8050 /* Associate node with varinfo DATA. Worker for
8051 cgraph_for_symbol_thunks_and_aliases. */
8052 static bool
8053 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
8055 if ((node->alias
8056 || (node->thunk
8057 && ! node->inlined_to))
8058 && node->analyzed
8059 && !node->ifunc_resolver)
8060 insert_vi_for_tree (node->decl, (varinfo_t)data);
8061 return false;
8064 /* Dump varinfo VI to FILE. */
8066 static void
8067 dump_varinfo (FILE *file, varinfo_t vi)
8069 if (vi == NULL)
8070 return;
8072 fprintf (file, "%u: %s\n", vi->id, vi->name);
8074 const char *sep = " ";
8075 if (vi->is_artificial_var)
8076 fprintf (file, "%sartificial", sep);
8077 if (vi->is_special_var)
8078 fprintf (file, "%sspecial", sep);
8079 if (vi->is_unknown_size_var)
8080 fprintf (file, "%sunknown-size", sep);
8081 if (vi->is_full_var)
8082 fprintf (file, "%sfull", sep);
8083 if (vi->is_heap_var)
8084 fprintf (file, "%sheap", sep);
8085 if (vi->may_have_pointers)
8086 fprintf (file, "%smay-have-pointers", sep);
8087 if (vi->only_restrict_pointers)
8088 fprintf (file, "%sonly-restrict-pointers", sep);
8089 if (vi->is_restrict_var)
8090 fprintf (file, "%sis-restrict-var", sep);
8091 if (vi->is_global_var)
8092 fprintf (file, "%sglobal", sep);
8093 if (vi->is_ipa_escape_point)
8094 fprintf (file, "%sipa-escape-point", sep);
8095 if (vi->is_fn_info)
8096 fprintf (file, "%sfn-info", sep);
8097 if (vi->ruid)
8098 fprintf (file, "%srestrict-uid:%u", sep, vi->ruid);
8099 if (vi->next)
8100 fprintf (file, "%snext:%u", sep, vi->next);
8101 if (vi->head != vi->id)
8102 fprintf (file, "%shead:%u", sep, vi->head);
8103 if (vi->offset)
8104 fprintf (file, "%soffset:" HOST_WIDE_INT_PRINT_DEC, sep, vi->offset);
8105 if (vi->size != ~(unsigned HOST_WIDE_INT)0)
8106 fprintf (file, "%ssize:" HOST_WIDE_INT_PRINT_DEC, sep, vi->size);
8107 if (vi->fullsize != ~(unsigned HOST_WIDE_INT)0
8108 && vi->fullsize != vi->size)
8109 fprintf (file, "%sfullsize:" HOST_WIDE_INT_PRINT_DEC, sep,
8110 vi->fullsize);
8111 fprintf (file, "\n");
8113 if (vi->solution && !bitmap_empty_p (vi->solution))
8115 bitmap_iterator bi;
8116 unsigned i;
8117 fprintf (file, " solution: {");
8118 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
8119 fprintf (file, " %u", i);
8120 fprintf (file, " }\n");
8123 if (vi->oldsolution && !bitmap_empty_p (vi->oldsolution)
8124 && !bitmap_equal_p (vi->solution, vi->oldsolution))
8126 bitmap_iterator bi;
8127 unsigned i;
8128 fprintf (file, " oldsolution: {");
8129 EXECUTE_IF_SET_IN_BITMAP (vi->oldsolution, 0, i, bi)
8130 fprintf (file, " %u", i);
8131 fprintf (file, " }\n");
8135 /* Dump varinfo VI to stderr. */
8137 DEBUG_FUNCTION void
8138 debug_varinfo (varinfo_t vi)
8140 dump_varinfo (stderr, vi);
8143 /* Dump varmap to FILE. */
8145 static void
8146 dump_varmap (FILE *file)
8148 if (varmap.length () == 0)
8149 return;
8151 fprintf (file, "variables:\n");
8153 for (unsigned int i = 0; i < varmap.length (); ++i)
8155 varinfo_t vi = get_varinfo (i);
8156 dump_varinfo (file, vi);
8159 fprintf (file, "\n");
8162 /* Dump varmap to stderr. */
8164 DEBUG_FUNCTION void
8165 debug_varmap (void)
8167 dump_varmap (stderr);
8170 /* Compute whether node is refered to non-locally. Worker for
8171 cgraph_for_symbol_thunks_and_aliases. */
8172 static bool
8173 refered_from_nonlocal_fn (struct cgraph_node *node, void *data)
8175 bool *nonlocal_p = (bool *)data;
8176 *nonlocal_p |= (node->used_from_other_partition
8177 || DECL_EXTERNAL (node->decl)
8178 || TREE_PUBLIC (node->decl)
8179 || node->force_output
8180 || lookup_attribute ("noipa", DECL_ATTRIBUTES (node->decl)));
8181 return false;
8184 /* Same for varpool nodes. */
8185 static bool
8186 refered_from_nonlocal_var (struct varpool_node *node, void *data)
8188 bool *nonlocal_p = (bool *)data;
8189 *nonlocal_p |= (node->used_from_other_partition
8190 || DECL_EXTERNAL (node->decl)
8191 || TREE_PUBLIC (node->decl)
8192 || node->force_output);
8193 return false;
8196 /* Execute the driver for IPA PTA. */
8197 static unsigned int
8198 ipa_pta_execute (void)
8200 struct cgraph_node *node;
8201 varpool_node *var;
8202 unsigned int from = 0;
8204 in_ipa_mode = 1;
8206 init_alias_vars ();
8208 if (dump_file && (dump_flags & TDF_DETAILS))
8210 symtab->dump (dump_file);
8211 fprintf (dump_file, "\n");
8214 if (dump_file)
8216 fprintf (dump_file, "Generating generic constraints\n\n");
8217 dump_constraints (dump_file, from);
8218 fprintf (dump_file, "\n");
8219 from = constraints.length ();
8222 /* Build the constraints. */
8223 FOR_EACH_DEFINED_FUNCTION (node)
8225 varinfo_t vi;
8226 /* Nodes without a body in this partition are not interesting.
8227 Especially do not visit clones at this point for now - we
8228 get duplicate decls there for inline clones at least. */
8229 if (!node->has_gimple_body_p ()
8230 || node->in_other_partition
8231 || node->inlined_to)
8232 continue;
8233 node->get_body ();
8235 gcc_assert (!node->clone_of);
8237 /* For externally visible or attribute used annotated functions use
8238 local constraints for their arguments.
8239 For local functions we see all callers and thus do not need initial
8240 constraints for parameters. */
8241 bool nonlocal_p = (node->used_from_other_partition
8242 || DECL_EXTERNAL (node->decl)
8243 || TREE_PUBLIC (node->decl)
8244 || node->force_output
8245 || lookup_attribute ("noipa",
8246 DECL_ATTRIBUTES (node->decl)));
8247 node->call_for_symbol_thunks_and_aliases (refered_from_nonlocal_fn,
8248 &nonlocal_p, true);
8250 vi = create_function_info_for (node->decl,
8251 alias_get_name (node->decl), false,
8252 nonlocal_p);
8253 if (dump_file
8254 && from != constraints.length ())
8256 fprintf (dump_file,
8257 "Generating initial constraints for %s",
8258 node->dump_name ());
8259 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
8260 fprintf (dump_file, " (%s)",
8261 IDENTIFIER_POINTER
8262 (DECL_ASSEMBLER_NAME (node->decl)));
8263 fprintf (dump_file, "\n\n");
8264 dump_constraints (dump_file, from);
8265 fprintf (dump_file, "\n");
8267 from = constraints.length ();
8270 node->call_for_symbol_thunks_and_aliases
8271 (associate_varinfo_to_alias, vi, true);
8274 /* Create constraints for global variables and their initializers. */
8275 FOR_EACH_VARIABLE (var)
8277 if (var->alias && var->analyzed)
8278 continue;
8280 varinfo_t vi = get_vi_for_tree (var->decl);
8282 /* For the purpose of IPA PTA unit-local globals are not
8283 escape points. */
8284 bool nonlocal_p = (DECL_EXTERNAL (var->decl)
8285 || TREE_PUBLIC (var->decl)
8286 || var->used_from_other_partition
8287 || var->force_output);
8288 var->call_for_symbol_and_aliases (refered_from_nonlocal_var,
8289 &nonlocal_p, true);
8290 if (nonlocal_p)
8291 vi->is_ipa_escape_point = true;
8294 if (dump_file
8295 && from != constraints.length ())
8297 fprintf (dump_file,
8298 "Generating constraints for global initializers\n\n");
8299 dump_constraints (dump_file, from);
8300 fprintf (dump_file, "\n");
8301 from = constraints.length ();
8304 FOR_EACH_DEFINED_FUNCTION (node)
8306 struct function *func;
8307 basic_block bb;
8309 /* Nodes without a body in this partition are not interesting. */
8310 if (!node->has_gimple_body_p ()
8311 || node->in_other_partition
8312 || node->clone_of)
8313 continue;
8315 if (dump_file)
8317 fprintf (dump_file,
8318 "Generating constraints for %s", node->dump_name ());
8319 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
8320 fprintf (dump_file, " (%s)",
8321 IDENTIFIER_POINTER
8322 (DECL_ASSEMBLER_NAME (node->decl)));
8323 fprintf (dump_file, "\n");
8326 func = DECL_STRUCT_FUNCTION (node->decl);
8327 gcc_assert (cfun == NULL);
8329 /* Build constriants for the function body. */
8330 FOR_EACH_BB_FN (bb, func)
8332 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
8333 gsi_next (&gsi))
8335 gphi *phi = gsi.phi ();
8337 if (! virtual_operand_p (gimple_phi_result (phi)))
8338 find_func_aliases (func, phi);
8341 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
8342 gsi_next (&gsi))
8344 gimple *stmt = gsi_stmt (gsi);
8346 find_func_aliases (func, stmt);
8347 find_func_clobbers (func, stmt);
8351 if (dump_file)
8353 fprintf (dump_file, "\n");
8354 dump_constraints (dump_file, from);
8355 fprintf (dump_file, "\n");
8356 from = constraints.length ();
8360 /* From the constraints compute the points-to sets. */
8361 solve_constraints ();
8363 if (dump_file)
8364 dump_sa_points_to_info (dump_file);
8366 /* Now post-process solutions to handle locals from different
8367 runtime instantiations coming in through recursive invocations. */
8368 unsigned shadow_var_cnt = 0;
8369 for (unsigned i = 1; i < varmap.length (); ++i)
8371 varinfo_t fi = get_varinfo (i);
8372 if (fi->is_fn_info
8373 && fi->decl)
8374 /* Automatic variables pointed to by their containing functions
8375 parameters need this treatment. */
8376 for (varinfo_t ai = first_vi_for_offset (fi, fi_parm_base);
8377 ai; ai = vi_next (ai))
8379 varinfo_t vi = get_varinfo (find (ai->id));
8380 bitmap_iterator bi;
8381 unsigned j;
8382 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, j, bi)
8384 varinfo_t pt = get_varinfo (j);
8385 if (pt->shadow_var_uid == 0
8386 && pt->decl
8387 && auto_var_in_fn_p (pt->decl, fi->decl))
8389 pt->shadow_var_uid = allocate_decl_uid ();
8390 shadow_var_cnt++;
8394 /* As well as global variables which are another way of passing
8395 arguments to recursive invocations. */
8396 else if (fi->is_global_var)
8398 for (varinfo_t ai = fi; ai; ai = vi_next (ai))
8400 varinfo_t vi = get_varinfo (find (ai->id));
8401 bitmap_iterator bi;
8402 unsigned j;
8403 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, j, bi)
8405 varinfo_t pt = get_varinfo (j);
8406 if (pt->shadow_var_uid == 0
8407 && pt->decl
8408 && auto_var_p (pt->decl))
8410 pt->shadow_var_uid = allocate_decl_uid ();
8411 shadow_var_cnt++;
8417 if (shadow_var_cnt && dump_file && (dump_flags & TDF_DETAILS))
8418 fprintf (dump_file, "Allocated %u shadow variables for locals "
8419 "maybe leaking into recursive invocations of their containing "
8420 "functions\n", shadow_var_cnt);
8422 /* Compute the global points-to sets for ESCAPED.
8423 ??? Note that the computed escape set is not correct
8424 for the whole unit as we fail to consider graph edges to
8425 externally visible functions. */
8426 ipa_escaped_pt = find_what_var_points_to (NULL, get_varinfo (escaped_id));
8428 /* Make sure the ESCAPED solution (which is used as placeholder in
8429 other solutions) does not reference itself. This simplifies
8430 points-to solution queries. */
8431 ipa_escaped_pt.ipa_escaped = 0;
8433 /* Assign the points-to sets to the SSA names in the unit. */
8434 FOR_EACH_DEFINED_FUNCTION (node)
8436 tree ptr;
8437 struct function *fn;
8438 unsigned i;
8439 basic_block bb;
8441 /* Nodes without a body in this partition are not interesting. */
8442 if (!node->has_gimple_body_p ()
8443 || node->in_other_partition
8444 || node->clone_of)
8445 continue;
8447 fn = DECL_STRUCT_FUNCTION (node->decl);
8449 /* Compute the points-to sets for pointer SSA_NAMEs. */
8450 FOR_EACH_VEC_ELT (*fn->gimple_df->ssa_names, i, ptr)
8452 if (ptr
8453 && POINTER_TYPE_P (TREE_TYPE (ptr)))
8454 find_what_p_points_to (node->decl, ptr);
8457 /* Compute the call-use and call-clobber sets for indirect calls
8458 and calls to external functions. */
8459 FOR_EACH_BB_FN (bb, fn)
8461 gimple_stmt_iterator gsi;
8463 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
8465 gcall *stmt;
8466 struct pt_solution *pt;
8467 varinfo_t vi, fi;
8468 tree decl;
8470 stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
8471 if (!stmt)
8472 continue;
8474 /* Handle direct calls to functions with body. */
8475 decl = gimple_call_fndecl (stmt);
8478 tree called_decl = NULL_TREE;
8479 if (gimple_call_builtin_p (stmt, BUILT_IN_GOMP_PARALLEL))
8480 called_decl = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
8481 else if (gimple_call_builtin_p (stmt, BUILT_IN_GOACC_PARALLEL))
8482 called_decl = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
8484 if (called_decl != NULL_TREE
8485 && !fndecl_maybe_in_other_partition (called_decl))
8486 decl = called_decl;
8489 if (decl
8490 && (fi = lookup_vi_for_tree (decl))
8491 && fi->is_fn_info)
8493 *gimple_call_clobber_set (stmt)
8494 = find_what_var_points_to
8495 (node->decl, first_vi_for_offset (fi, fi_clobbers));
8496 *gimple_call_use_set (stmt)
8497 = find_what_var_points_to
8498 (node->decl, first_vi_for_offset (fi, fi_uses));
8500 /* Handle direct calls to external functions. */
8501 else if (decl && (!fi || fi->decl))
8503 pt = gimple_call_use_set (stmt);
8504 if (gimple_call_flags (stmt) & ECF_CONST)
8505 memset (pt, 0, sizeof (struct pt_solution));
8506 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
8508 *pt = find_what_var_points_to (node->decl, vi);
8509 /* Escaped (and thus nonlocal) variables are always
8510 implicitly used by calls. */
8511 /* ??? ESCAPED can be empty even though NONLOCAL
8512 always escaped. */
8513 pt->nonlocal = 1;
8514 pt->ipa_escaped = 1;
8516 else
8518 /* If there is nothing special about this call then
8519 we have made everything that is used also escape. */
8520 *pt = ipa_escaped_pt;
8521 pt->nonlocal = 1;
8524 pt = gimple_call_clobber_set (stmt);
8525 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
8526 memset (pt, 0, sizeof (struct pt_solution));
8527 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
8529 *pt = find_what_var_points_to (node->decl, vi);
8530 /* Escaped (and thus nonlocal) variables are always
8531 implicitly clobbered by calls. */
8532 /* ??? ESCAPED can be empty even though NONLOCAL
8533 always escaped. */
8534 pt->nonlocal = 1;
8535 pt->ipa_escaped = 1;
8537 else
8539 /* If there is nothing special about this call then
8540 we have made everything that is used also escape. */
8541 *pt = ipa_escaped_pt;
8542 pt->nonlocal = 1;
8545 /* Handle indirect calls. */
8546 else if ((fi = get_fi_for_callee (stmt)))
8548 /* We need to accumulate all clobbers/uses of all possible
8549 callees. */
8550 fi = get_varinfo (find (fi->id));
8551 /* If we cannot constrain the set of functions we'll end up
8552 calling we end up using/clobbering everything. */
8553 if (bitmap_bit_p (fi->solution, anything_id)
8554 || bitmap_bit_p (fi->solution, nonlocal_id)
8555 || bitmap_bit_p (fi->solution, escaped_id))
8557 pt_solution_reset (gimple_call_clobber_set (stmt));
8558 pt_solution_reset (gimple_call_use_set (stmt));
8560 else
8562 bitmap_iterator bi;
8563 unsigned i;
8564 struct pt_solution *uses, *clobbers;
8566 uses = gimple_call_use_set (stmt);
8567 clobbers = gimple_call_clobber_set (stmt);
8568 memset (uses, 0, sizeof (struct pt_solution));
8569 memset (clobbers, 0, sizeof (struct pt_solution));
8570 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
8572 struct pt_solution sol;
8574 vi = get_varinfo (i);
8575 if (!vi->is_fn_info)
8577 /* ??? We could be more precise here? */
8578 uses->nonlocal = 1;
8579 uses->ipa_escaped = 1;
8580 clobbers->nonlocal = 1;
8581 clobbers->ipa_escaped = 1;
8582 continue;
8585 if (!uses->anything)
8587 sol = find_what_var_points_to
8588 (node->decl,
8589 first_vi_for_offset (vi, fi_uses));
8590 pt_solution_ior_into (uses, &sol);
8592 if (!clobbers->anything)
8594 sol = find_what_var_points_to
8595 (node->decl,
8596 first_vi_for_offset (vi, fi_clobbers));
8597 pt_solution_ior_into (clobbers, &sol);
8602 else
8603 gcc_unreachable ();
8607 fn->gimple_df->ipa_pta = true;
8609 /* We have to re-set the final-solution cache after each function
8610 because what is a "global" is dependent on function context. */
8611 final_solutions->empty ();
8612 obstack_free (&final_solutions_obstack, NULL);
8613 gcc_obstack_init (&final_solutions_obstack);
8616 delete_points_to_sets ();
8618 in_ipa_mode = 0;
8620 return 0;
8623 namespace {
8625 const pass_data pass_data_ipa_pta =
8627 SIMPLE_IPA_PASS, /* type */
8628 "pta", /* name */
8629 OPTGROUP_NONE, /* optinfo_flags */
8630 TV_IPA_PTA, /* tv_id */
8631 0, /* properties_required */
8632 0, /* properties_provided */
8633 0, /* properties_destroyed */
8634 0, /* todo_flags_start */
8635 0, /* todo_flags_finish */
8638 class pass_ipa_pta : public simple_ipa_opt_pass
8640 public:
8641 pass_ipa_pta (gcc::context *ctxt)
8642 : simple_ipa_opt_pass (pass_data_ipa_pta, ctxt)
8645 /* opt_pass methods: */
8646 virtual bool gate (function *)
8648 return (optimize
8649 && flag_ipa_pta
8650 /* Don't bother doing anything if the program has errors. */
8651 && !seen_error ());
8654 opt_pass * clone () { return new pass_ipa_pta (m_ctxt); }
8656 virtual unsigned int execute (function *) { return ipa_pta_execute (); }
8658 }; // class pass_ipa_pta
8660 } // anon namespace
8662 simple_ipa_opt_pass *
8663 make_pass_ipa_pta (gcc::context *ctxt)
8665 return new pass_ipa_pta (ctxt);