* config/rs6000/rs6000.c (rs6000_output_symbol_ref): Move storage
[official-gcc.git] / gcc / tree-ssa-structalias.c
blob78f45334785071e9ffdf973eb5177c7732677533
1 /* Tree based points-to analysis
2 Copyright (C) 2005-2016 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 "params.h"
41 #include "gimple-walk.h"
43 /* The idea behind this analyzer is to generate set constraints from the
44 program, then solve the resulting constraints in order to generate the
45 points-to sets.
47 Set constraints are a way of modeling program analysis problems that
48 involve sets. They consist of an inclusion constraint language,
49 describing the variables (each variable is a set) and operations that
50 are involved on the variables, and a set of rules that derive facts
51 from these operations. To solve a system of set constraints, you derive
52 all possible facts under the rules, which gives you the correct sets
53 as a consequence.
55 See "Efficient Field-sensitive pointer analysis for C" by "David
56 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
57 http://citeseer.ist.psu.edu/pearce04efficient.html
59 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
60 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
61 http://citeseer.ist.psu.edu/heintze01ultrafast.html
63 There are three types of real constraint expressions, DEREF,
64 ADDRESSOF, and SCALAR. Each constraint expression consists
65 of a constraint type, a variable, and an offset.
67 SCALAR is a constraint expression type used to represent x, whether
68 it appears on the LHS or the RHS of a statement.
69 DEREF is a constraint expression type used to represent *x, whether
70 it appears on the LHS or the RHS of a statement.
71 ADDRESSOF is a constraint expression used to represent &x, whether
72 it appears on the LHS or the RHS of a statement.
74 Each pointer variable in the program is assigned an integer id, and
75 each field of a structure variable is assigned an integer id as well.
77 Structure variables are linked to their list of fields through a "next
78 field" in each variable that points to the next field in offset
79 order.
80 Each variable for a structure field has
82 1. "size", that tells the size in bits of that field.
83 2. "fullsize, that tells the size in bits of the entire structure.
84 3. "offset", that tells the offset in bits from the beginning of the
85 structure to this field.
87 Thus,
88 struct f
90 int a;
91 int b;
92 } foo;
93 int *bar;
95 looks like
97 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
98 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
99 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
102 In order to solve the system of set constraints, the following is
103 done:
105 1. Each constraint variable x has a solution set associated with it,
106 Sol(x).
108 2. Constraints are separated into direct, copy, and complex.
109 Direct constraints are ADDRESSOF constraints that require no extra
110 processing, such as P = &Q
111 Copy constraints are those of the form P = Q.
112 Complex constraints are all the constraints involving dereferences
113 and offsets (including offsetted copies).
115 3. All direct constraints of the form P = &Q are processed, such
116 that Q is added to Sol(P)
118 4. All complex constraints for a given constraint variable are stored in a
119 linked list attached to that variable's node.
121 5. A directed graph is built out of the copy constraints. Each
122 constraint variable is a node in the graph, and an edge from
123 Q to P is added for each copy constraint of the form P = Q
125 6. The graph is then walked, and solution sets are
126 propagated along the copy edges, such that an edge from Q to P
127 causes Sol(P) <- Sol(P) union Sol(Q).
129 7. As we visit each node, all complex constraints associated with
130 that node are processed by adding appropriate copy edges to the graph, or the
131 appropriate variables to the solution set.
133 8. The process of walking the graph is iterated until no solution
134 sets change.
136 Prior to walking the graph in steps 6 and 7, We perform static
137 cycle elimination on the constraint graph, as well
138 as off-line variable substitution.
140 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
141 on and turned into anything), but isn't. You can just see what offset
142 inside the pointed-to struct it's going to access.
144 TODO: Constant bounded arrays can be handled as if they were structs of the
145 same number of elements.
147 TODO: Modeling heap and incoming pointers becomes much better if we
148 add fields to them as we discover them, which we could do.
150 TODO: We could handle unions, but to be honest, it's probably not
151 worth the pain or slowdown. */
153 /* IPA-PTA optimizations possible.
155 When the indirect function called is ANYTHING we can add disambiguation
156 based on the function signatures (or simply the parameter count which
157 is the varinfo size). We also do not need to consider functions that
158 do not have their address taken.
160 The is_global_var bit which marks escape points is overly conservative
161 in IPA mode. Split it to is_escape_point and is_global_var - only
162 externally visible globals are escape points in IPA mode.
163 There is now is_ipa_escape_point but this is only used in a few
164 selected places.
166 The way we introduce DECL_PT_UID to avoid fixing up all points-to
167 sets in the translation unit when we copy a DECL during inlining
168 pessimizes precision. The advantage is that the DECL_PT_UID keeps
169 compile-time and memory usage overhead low - the points-to sets
170 do not grow or get unshared as they would during a fixup phase.
171 An alternative solution is to delay IPA PTA until after all
172 inlining transformations have been applied.
174 The way we propagate clobber/use information isn't optimized.
175 It should use a new complex constraint that properly filters
176 out local variables of the callee (though that would make
177 the sets invalid after inlining). OTOH we might as well
178 admit defeat to WHOPR and simply do all the clobber/use analysis
179 and propagation after PTA finished but before we threw away
180 points-to information for memory variables. WHOPR and PTA
181 do not play along well anyway - the whole constraint solving
182 would need to be done in WPA phase and it will be very interesting
183 to apply the results to local SSA names during LTRANS phase.
185 We probably should compute a per-function unit-ESCAPE solution
186 propagating it simply like the clobber / uses solutions. The
187 solution can go alongside the non-IPA espaced solution and be
188 used to query which vars escape the unit through a function.
189 This is also required to make the escaped-HEAP trick work in IPA mode.
191 We never put function decls in points-to sets so we do not
192 keep the set of called functions for indirect calls.
194 And probably more. */
196 static bool use_field_sensitive = true;
197 static int in_ipa_mode = 0;
199 /* Used for predecessor bitmaps. */
200 static bitmap_obstack predbitmap_obstack;
202 /* Used for points-to sets. */
203 static bitmap_obstack pta_obstack;
205 /* Used for oldsolution members of variables. */
206 static bitmap_obstack oldpta_obstack;
208 /* Used for per-solver-iteration bitmaps. */
209 static bitmap_obstack iteration_obstack;
211 static unsigned int create_variable_info_for (tree, const char *, bool);
212 typedef struct constraint_graph *constraint_graph_t;
213 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
215 struct constraint;
216 typedef struct constraint *constraint_t;
219 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
220 if (a) \
221 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
223 static struct constraint_stats
225 unsigned int total_vars;
226 unsigned int nonpointer_vars;
227 unsigned int unified_vars_static;
228 unsigned int unified_vars_dynamic;
229 unsigned int iterations;
230 unsigned int num_edges;
231 unsigned int num_implicit_edges;
232 unsigned int points_to_sets_created;
233 } stats;
235 struct variable_info
237 /* ID of this variable */
238 unsigned int id;
240 /* True if this is a variable created by the constraint analysis, such as
241 heap variables and constraints we had to break up. */
242 unsigned int is_artificial_var : 1;
244 /* True if this is a special variable whose solution set should not be
245 changed. */
246 unsigned int is_special_var : 1;
248 /* True for variables whose size is not known or variable. */
249 unsigned int is_unknown_size_var : 1;
251 /* True for (sub-)fields that represent a whole variable. */
252 unsigned int is_full_var : 1;
254 /* True if this is a heap variable. */
255 unsigned int is_heap_var : 1;
257 /* True if this field may contain pointers. */
258 unsigned int may_have_pointers : 1;
260 /* True if this field has only restrict qualified pointers. */
261 unsigned int only_restrict_pointers : 1;
263 /* True if this represents a heap var created for a restrict qualified
264 pointer. */
265 unsigned int is_restrict_var : 1;
267 /* True if this represents a global variable. */
268 unsigned int is_global_var : 1;
270 /* True if this represents a module escape point for IPA analysis. */
271 unsigned int is_ipa_escape_point : 1;
273 /* True if this represents a IPA function info. */
274 unsigned int is_fn_info : 1;
276 /* ??? Store somewhere better. */
277 unsigned short ruid;
279 /* The ID of the variable for the next field in this structure
280 or zero for the last field in this structure. */
281 unsigned next;
283 /* The ID of the variable for the first field in this structure. */
284 unsigned head;
286 /* Offset of this variable, in bits, from the base variable */
287 unsigned HOST_WIDE_INT offset;
289 /* Size of the variable, in bits. */
290 unsigned HOST_WIDE_INT size;
292 /* Full size of the base variable, in bits. */
293 unsigned HOST_WIDE_INT fullsize;
295 /* Name of this variable */
296 const char *name;
298 /* Tree that this variable is associated with. */
299 tree decl;
301 /* Points-to set for this variable. */
302 bitmap solution;
304 /* Old points-to set for this variable. */
305 bitmap oldsolution;
307 typedef struct variable_info *varinfo_t;
309 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
310 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
311 unsigned HOST_WIDE_INT);
312 static varinfo_t lookup_vi_for_tree (tree);
313 static inline bool type_can_have_subvars (const_tree);
314 static void make_param_constraints (varinfo_t);
316 /* Pool of variable info structures. */
317 static object_allocator<variable_info> variable_info_pool
318 ("Variable info pool");
320 /* Map varinfo to final pt_solution. */
321 static hash_map<varinfo_t, pt_solution *> *final_solutions;
322 struct obstack final_solutions_obstack;
324 /* Table of variable info structures for constraint variables.
325 Indexed directly by variable info id. */
326 static vec<varinfo_t> varmap;
328 /* Return the varmap element N */
330 static inline varinfo_t
331 get_varinfo (unsigned int n)
333 return varmap[n];
336 /* Return the next variable in the list of sub-variables of VI
337 or NULL if VI is the last sub-variable. */
339 static inline varinfo_t
340 vi_next (varinfo_t vi)
342 return get_varinfo (vi->next);
345 /* Static IDs for the special variables. Variable ID zero is unused
346 and used as terminator for the sub-variable chain. */
347 enum { nothing_id = 1, anything_id = 2, string_id = 3,
348 escaped_id = 4, nonlocal_id = 5,
349 storedanything_id = 6, integer_id = 7 };
351 /* Return a new variable info structure consisting for a variable
352 named NAME, and using constraint graph node NODE. Append it
353 to the vector of variable info structures. */
355 static varinfo_t
356 new_var_info (tree t, const char *name, bool add_id)
358 unsigned index = varmap.length ();
359 varinfo_t ret = variable_info_pool.allocate ();
361 if (dump_file && add_id)
363 char *tempname = xasprintf ("%s(%d)", name, index);
364 name = ggc_strdup (tempname);
365 free (tempname);
368 ret->id = index;
369 ret->name = name;
370 ret->decl = t;
371 /* Vars without decl are artificial and do not have sub-variables. */
372 ret->is_artificial_var = (t == NULL_TREE);
373 ret->is_special_var = false;
374 ret->is_unknown_size_var = false;
375 ret->is_full_var = (t == NULL_TREE);
376 ret->is_heap_var = false;
377 ret->may_have_pointers = true;
378 ret->only_restrict_pointers = false;
379 ret->is_restrict_var = false;
380 ret->ruid = 0;
381 ret->is_global_var = (t == NULL_TREE);
382 ret->is_ipa_escape_point = false;
383 ret->is_fn_info = false;
384 if (t && DECL_P (t))
385 ret->is_global_var = (is_global_var (t)
386 /* We have to treat even local register variables
387 as escape points. */
388 || (VAR_P (t) && DECL_HARD_REGISTER (t)));
389 ret->solution = BITMAP_ALLOC (&pta_obstack);
390 ret->oldsolution = NULL;
391 ret->next = 0;
392 ret->head = ret->id;
394 stats.total_vars++;
396 varmap.safe_push (ret);
398 return ret;
401 /* A map mapping call statements to per-stmt variables for uses
402 and clobbers specific to the call. */
403 static hash_map<gimple *, varinfo_t> *call_stmt_vars;
405 /* Lookup or create the variable for the call statement CALL. */
407 static varinfo_t
408 get_call_vi (gcall *call)
410 varinfo_t vi, vi2;
412 bool existed;
413 varinfo_t *slot_p = &call_stmt_vars->get_or_insert (call, &existed);
414 if (existed)
415 return *slot_p;
417 vi = new_var_info (NULL_TREE, "CALLUSED", true);
418 vi->offset = 0;
419 vi->size = 1;
420 vi->fullsize = 2;
421 vi->is_full_var = true;
423 vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED", true);
424 vi2->offset = 1;
425 vi2->size = 1;
426 vi2->fullsize = 2;
427 vi2->is_full_var = true;
429 vi->next = vi2->id;
431 *slot_p = vi;
432 return vi;
435 /* Lookup the variable for the call statement CALL representing
436 the uses. Returns NULL if there is nothing special about this call. */
438 static varinfo_t
439 lookup_call_use_vi (gcall *call)
441 varinfo_t *slot_p = call_stmt_vars->get (call);
442 if (slot_p)
443 return *slot_p;
445 return NULL;
448 /* Lookup the variable for the call statement CALL representing
449 the clobbers. Returns NULL if there is nothing special about this call. */
451 static varinfo_t
452 lookup_call_clobber_vi (gcall *call)
454 varinfo_t uses = lookup_call_use_vi (call);
455 if (!uses)
456 return NULL;
458 return vi_next (uses);
461 /* Lookup or create the variable for the call statement CALL representing
462 the uses. */
464 static varinfo_t
465 get_call_use_vi (gcall *call)
467 return get_call_vi (call);
470 /* Lookup or create the variable for the call statement CALL representing
471 the clobbers. */
473 static varinfo_t ATTRIBUTE_UNUSED
474 get_call_clobber_vi (gcall *call)
476 return vi_next (get_call_vi (call));
480 enum constraint_expr_type {SCALAR, DEREF, ADDRESSOF};
482 /* An expression that appears in a constraint. */
484 struct constraint_expr
486 /* Constraint type. */
487 constraint_expr_type type;
489 /* Variable we are referring to in the constraint. */
490 unsigned int var;
492 /* Offset, in bits, of this constraint from the beginning of
493 variables it ends up referring to.
495 IOW, in a deref constraint, we would deref, get the result set,
496 then add OFFSET to each member. */
497 HOST_WIDE_INT offset;
500 /* Use 0x8000... as special unknown offset. */
501 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
503 typedef struct constraint_expr ce_s;
504 static void get_constraint_for_1 (tree, vec<ce_s> *, bool, bool);
505 static void get_constraint_for (tree, vec<ce_s> *);
506 static void get_constraint_for_rhs (tree, vec<ce_s> *);
507 static void do_deref (vec<ce_s> *);
509 /* Our set constraints are made up of two constraint expressions, one
510 LHS, and one RHS.
512 As described in the introduction, our set constraints each represent an
513 operation between set valued variables.
515 struct constraint
517 struct constraint_expr lhs;
518 struct constraint_expr rhs;
521 /* List of constraints that we use to build the constraint graph from. */
523 static vec<constraint_t> constraints;
524 static object_allocator<constraint> constraint_pool ("Constraint pool");
526 /* The constraint graph is represented as an array of bitmaps
527 containing successor nodes. */
529 struct constraint_graph
531 /* Size of this graph, which may be different than the number of
532 nodes in the variable map. */
533 unsigned int size;
535 /* Explicit successors of each node. */
536 bitmap *succs;
538 /* Implicit predecessors of each node (Used for variable
539 substitution). */
540 bitmap *implicit_preds;
542 /* Explicit predecessors of each node (Used for variable substitution). */
543 bitmap *preds;
545 /* Indirect cycle representatives, or -1 if the node has no indirect
546 cycles. */
547 int *indirect_cycles;
549 /* Representative node for a node. rep[a] == a unless the node has
550 been unified. */
551 unsigned int *rep;
553 /* Equivalence class representative for a label. This is used for
554 variable substitution. */
555 int *eq_rep;
557 /* Pointer equivalence label for a node. All nodes with the same
558 pointer equivalence label can be unified together at some point
559 (either during constraint optimization or after the constraint
560 graph is built). */
561 unsigned int *pe;
563 /* Pointer equivalence representative for a label. This is used to
564 handle nodes that are pointer equivalent but not location
565 equivalent. We can unite these once the addressof constraints
566 are transformed into initial points-to sets. */
567 int *pe_rep;
569 /* Pointer equivalence label for each node, used during variable
570 substitution. */
571 unsigned int *pointer_label;
573 /* Location equivalence label for each node, used during location
574 equivalence finding. */
575 unsigned int *loc_label;
577 /* Pointed-by set for each node, used during location equivalence
578 finding. This is pointed-by rather than pointed-to, because it
579 is constructed using the predecessor graph. */
580 bitmap *pointed_by;
582 /* Points to sets for pointer equivalence. This is *not* the actual
583 points-to sets for nodes. */
584 bitmap *points_to;
586 /* Bitmap of nodes where the bit is set if the node is a direct
587 node. Used for variable substitution. */
588 sbitmap direct_nodes;
590 /* Bitmap of nodes where the bit is set if the node is address
591 taken. Used for variable substitution. */
592 bitmap address_taken;
594 /* Vector of complex constraints for each graph node. Complex
595 constraints are those involving dereferences or offsets that are
596 not 0. */
597 vec<constraint_t> *complex;
600 static constraint_graph_t graph;
602 /* During variable substitution and the offline version of indirect
603 cycle finding, we create nodes to represent dereferences and
604 address taken constraints. These represent where these start and
605 end. */
606 #define FIRST_REF_NODE (varmap).length ()
607 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
609 /* Return the representative node for NODE, if NODE has been unioned
610 with another NODE.
611 This function performs path compression along the way to finding
612 the representative. */
614 static unsigned int
615 find (unsigned int node)
617 gcc_checking_assert (node < graph->size);
618 if (graph->rep[node] != node)
619 return graph->rep[node] = find (graph->rep[node]);
620 return node;
623 /* Union the TO and FROM nodes to the TO nodes.
624 Note that at some point in the future, we may want to do
625 union-by-rank, in which case we are going to have to return the
626 node we unified to. */
628 static bool
629 unite (unsigned int to, unsigned int from)
631 gcc_checking_assert (to < graph->size && from < graph->size);
632 if (to != from && graph->rep[from] != to)
634 graph->rep[from] = to;
635 return true;
637 return false;
640 /* Create a new constraint consisting of LHS and RHS expressions. */
642 static constraint_t
643 new_constraint (const struct constraint_expr lhs,
644 const struct constraint_expr rhs)
646 constraint_t ret = constraint_pool.allocate ();
647 ret->lhs = lhs;
648 ret->rhs = rhs;
649 return ret;
652 /* Print out constraint C to FILE. */
654 static void
655 dump_constraint (FILE *file, constraint_t c)
657 if (c->lhs.type == ADDRESSOF)
658 fprintf (file, "&");
659 else if (c->lhs.type == DEREF)
660 fprintf (file, "*");
661 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
662 if (c->lhs.offset == UNKNOWN_OFFSET)
663 fprintf (file, " + UNKNOWN");
664 else if (c->lhs.offset != 0)
665 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
666 fprintf (file, " = ");
667 if (c->rhs.type == ADDRESSOF)
668 fprintf (file, "&");
669 else if (c->rhs.type == DEREF)
670 fprintf (file, "*");
671 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
672 if (c->rhs.offset == UNKNOWN_OFFSET)
673 fprintf (file, " + UNKNOWN");
674 else if (c->rhs.offset != 0)
675 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
679 void debug_constraint (constraint_t);
680 void debug_constraints (void);
681 void debug_constraint_graph (void);
682 void debug_solution_for_var (unsigned int);
683 void debug_sa_points_to_info (void);
684 void debug_varinfo (varinfo_t);
685 void debug_varmap (void);
687 /* Print out constraint C to stderr. */
689 DEBUG_FUNCTION void
690 debug_constraint (constraint_t c)
692 dump_constraint (stderr, c);
693 fprintf (stderr, "\n");
696 /* Print out all constraints to FILE */
698 static void
699 dump_constraints (FILE *file, int from)
701 int i;
702 constraint_t c;
703 for (i = from; constraints.iterate (i, &c); i++)
704 if (c)
706 dump_constraint (file, c);
707 fprintf (file, "\n");
711 /* Print out all constraints to stderr. */
713 DEBUG_FUNCTION void
714 debug_constraints (void)
716 dump_constraints (stderr, 0);
719 /* Print the constraint graph in dot format. */
721 static void
722 dump_constraint_graph (FILE *file)
724 unsigned int i;
726 /* Only print the graph if it has already been initialized: */
727 if (!graph)
728 return;
730 /* Prints the header of the dot file: */
731 fprintf (file, "strict digraph {\n");
732 fprintf (file, " node [\n shape = box\n ]\n");
733 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
734 fprintf (file, "\n // List of nodes and complex constraints in "
735 "the constraint graph:\n");
737 /* The next lines print the nodes in the graph together with the
738 complex constraints attached to them. */
739 for (i = 1; i < graph->size; i++)
741 if (i == FIRST_REF_NODE)
742 continue;
743 if (find (i) != i)
744 continue;
745 if (i < FIRST_REF_NODE)
746 fprintf (file, "\"%s\"", get_varinfo (i)->name);
747 else
748 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
749 if (graph->complex[i].exists ())
751 unsigned j;
752 constraint_t c;
753 fprintf (file, " [label=\"\\N\\n");
754 for (j = 0; graph->complex[i].iterate (j, &c); ++j)
756 dump_constraint (file, c);
757 fprintf (file, "\\l");
759 fprintf (file, "\"]");
761 fprintf (file, ";\n");
764 /* Go over the edges. */
765 fprintf (file, "\n // Edges in the constraint graph:\n");
766 for (i = 1; i < graph->size; i++)
768 unsigned j;
769 bitmap_iterator bi;
770 if (find (i) != i)
771 continue;
772 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
774 unsigned to = find (j);
775 if (i == to)
776 continue;
777 if (i < FIRST_REF_NODE)
778 fprintf (file, "\"%s\"", get_varinfo (i)->name);
779 else
780 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
781 fprintf (file, " -> ");
782 if (to < FIRST_REF_NODE)
783 fprintf (file, "\"%s\"", get_varinfo (to)->name);
784 else
785 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name);
786 fprintf (file, ";\n");
790 /* Prints the tail of the dot file. */
791 fprintf (file, "}\n");
794 /* Print out the constraint graph to stderr. */
796 DEBUG_FUNCTION void
797 debug_constraint_graph (void)
799 dump_constraint_graph (stderr);
802 /* SOLVER FUNCTIONS
804 The solver is a simple worklist solver, that works on the following
805 algorithm:
807 sbitmap changed_nodes = all zeroes;
808 changed_count = 0;
809 For each node that is not already collapsed:
810 changed_count++;
811 set bit in changed nodes
813 while (changed_count > 0)
815 compute topological ordering for constraint graph
817 find and collapse cycles in the constraint graph (updating
818 changed if necessary)
820 for each node (n) in the graph in topological order:
821 changed_count--;
823 Process each complex constraint associated with the node,
824 updating changed if necessary.
826 For each outgoing edge from n, propagate the solution from n to
827 the destination of the edge, updating changed as necessary.
829 } */
831 /* Return true if two constraint expressions A and B are equal. */
833 static bool
834 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
836 return a.type == b.type && a.var == b.var && a.offset == b.offset;
839 /* Return true if constraint expression A is less than constraint expression
840 B. This is just arbitrary, but consistent, in order to give them an
841 ordering. */
843 static bool
844 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
846 if (a.type == b.type)
848 if (a.var == b.var)
849 return a.offset < b.offset;
850 else
851 return a.var < b.var;
853 else
854 return a.type < b.type;
857 /* Return true if constraint A is less than constraint B. This is just
858 arbitrary, but consistent, in order to give them an ordering. */
860 static bool
861 constraint_less (const constraint_t &a, const constraint_t &b)
863 if (constraint_expr_less (a->lhs, b->lhs))
864 return true;
865 else if (constraint_expr_less (b->lhs, a->lhs))
866 return false;
867 else
868 return constraint_expr_less (a->rhs, b->rhs);
871 /* Return true if two constraints A and B are equal. */
873 static bool
874 constraint_equal (struct constraint a, struct constraint b)
876 return constraint_expr_equal (a.lhs, b.lhs)
877 && constraint_expr_equal (a.rhs, b.rhs);
881 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
883 static constraint_t
884 constraint_vec_find (vec<constraint_t> vec,
885 struct constraint lookfor)
887 unsigned int place;
888 constraint_t found;
890 if (!vec.exists ())
891 return NULL;
893 place = vec.lower_bound (&lookfor, constraint_less);
894 if (place >= vec.length ())
895 return NULL;
896 found = vec[place];
897 if (!constraint_equal (*found, lookfor))
898 return NULL;
899 return found;
902 /* Union two constraint vectors, TO and FROM. Put the result in TO.
903 Returns true of TO set is changed. */
905 static bool
906 constraint_set_union (vec<constraint_t> *to,
907 vec<constraint_t> *from)
909 int i;
910 constraint_t c;
911 bool any_change = false;
913 FOR_EACH_VEC_ELT (*from, i, c)
915 if (constraint_vec_find (*to, *c) == NULL)
917 unsigned int place = to->lower_bound (c, constraint_less);
918 to->safe_insert (place, c);
919 any_change = true;
922 return any_change;
925 /* Expands the solution in SET to all sub-fields of variables included. */
927 static bitmap
928 solution_set_expand (bitmap set, bitmap *expanded)
930 bitmap_iterator bi;
931 unsigned j;
933 if (*expanded)
934 return *expanded;
936 *expanded = BITMAP_ALLOC (&iteration_obstack);
938 /* In a first pass expand to the head of the variables we need to
939 add all sub-fields off. This avoids quadratic behavior. */
940 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
942 varinfo_t v = get_varinfo (j);
943 if (v->is_artificial_var
944 || v->is_full_var)
945 continue;
946 bitmap_set_bit (*expanded, v->head);
949 /* In the second pass now expand all head variables with subfields. */
950 EXECUTE_IF_SET_IN_BITMAP (*expanded, 0, j, bi)
952 varinfo_t v = get_varinfo (j);
953 if (v->head != j)
954 continue;
955 for (v = vi_next (v); v != NULL; v = vi_next (v))
956 bitmap_set_bit (*expanded, v->id);
959 /* And finally set the rest of the bits from SET. */
960 bitmap_ior_into (*expanded, set);
962 return *expanded;
965 /* Union solution sets TO and DELTA, and add INC to each member of DELTA in the
966 process. */
968 static bool
969 set_union_with_increment (bitmap to, bitmap delta, HOST_WIDE_INT inc,
970 bitmap *expanded_delta)
972 bool changed = false;
973 bitmap_iterator bi;
974 unsigned int i;
976 /* If the solution of DELTA contains anything it is good enough to transfer
977 this to TO. */
978 if (bitmap_bit_p (delta, anything_id))
979 return bitmap_set_bit (to, anything_id);
981 /* If the offset is unknown we have to expand the solution to
982 all subfields. */
983 if (inc == UNKNOWN_OFFSET)
985 delta = solution_set_expand (delta, expanded_delta);
986 changed |= bitmap_ior_into (to, delta);
987 return changed;
990 /* For non-zero offset union the offsetted solution into the destination. */
991 EXECUTE_IF_SET_IN_BITMAP (delta, 0, i, bi)
993 varinfo_t vi = get_varinfo (i);
995 /* If this is a variable with just one field just set its bit
996 in the result. */
997 if (vi->is_artificial_var
998 || vi->is_unknown_size_var
999 || vi->is_full_var)
1000 changed |= bitmap_set_bit (to, i);
1001 else
1003 HOST_WIDE_INT fieldoffset = vi->offset + inc;
1004 unsigned HOST_WIDE_INT size = vi->size;
1006 /* If the offset makes the pointer point to before the
1007 variable use offset zero for the field lookup. */
1008 if (fieldoffset < 0)
1009 vi = get_varinfo (vi->head);
1010 else
1011 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
1015 changed |= bitmap_set_bit (to, vi->id);
1016 if (vi->is_full_var
1017 || vi->next == 0)
1018 break;
1020 /* We have to include all fields that overlap the current field
1021 shifted by inc. */
1022 vi = vi_next (vi);
1024 while (vi->offset < fieldoffset + size);
1028 return changed;
1031 /* Insert constraint C into the list of complex constraints for graph
1032 node VAR. */
1034 static void
1035 insert_into_complex (constraint_graph_t graph,
1036 unsigned int var, constraint_t c)
1038 vec<constraint_t> complex = graph->complex[var];
1039 unsigned int place = complex.lower_bound (c, constraint_less);
1041 /* Only insert constraints that do not already exist. */
1042 if (place >= complex.length ()
1043 || !constraint_equal (*c, *complex[place]))
1044 graph->complex[var].safe_insert (place, c);
1048 /* Condense two variable nodes into a single variable node, by moving
1049 all associated info from FROM to TO. Returns true if TO node's
1050 constraint set changes after the merge. */
1052 static bool
1053 merge_node_constraints (constraint_graph_t graph, unsigned int to,
1054 unsigned int from)
1056 unsigned int i;
1057 constraint_t c;
1058 bool any_change = false;
1060 gcc_checking_assert (find (from) == to);
1062 /* Move all complex constraints from src node into to node */
1063 FOR_EACH_VEC_ELT (graph->complex[from], i, c)
1065 /* In complex constraints for node FROM, we may have either
1066 a = *FROM, and *FROM = a, or an offseted constraint which are
1067 always added to the rhs node's constraints. */
1069 if (c->rhs.type == DEREF)
1070 c->rhs.var = to;
1071 else if (c->lhs.type == DEREF)
1072 c->lhs.var = to;
1073 else
1074 c->rhs.var = to;
1077 any_change = constraint_set_union (&graph->complex[to],
1078 &graph->complex[from]);
1079 graph->complex[from].release ();
1080 return any_change;
1084 /* Remove edges involving NODE from GRAPH. */
1086 static void
1087 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
1089 if (graph->succs[node])
1090 BITMAP_FREE (graph->succs[node]);
1093 /* Merge GRAPH nodes FROM and TO into node TO. */
1095 static void
1096 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1097 unsigned int from)
1099 if (graph->indirect_cycles[from] != -1)
1101 /* If we have indirect cycles with the from node, and we have
1102 none on the to node, the to node has indirect cycles from the
1103 from node now that they are unified.
1104 If indirect cycles exist on both, unify the nodes that they
1105 are in a cycle with, since we know they are in a cycle with
1106 each other. */
1107 if (graph->indirect_cycles[to] == -1)
1108 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1111 /* Merge all the successor edges. */
1112 if (graph->succs[from])
1114 if (!graph->succs[to])
1115 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1116 bitmap_ior_into (graph->succs[to],
1117 graph->succs[from]);
1120 clear_edges_for_node (graph, from);
1124 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1125 it doesn't exist in the graph already. */
1127 static void
1128 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1129 unsigned int from)
1131 if (to == from)
1132 return;
1134 if (!graph->implicit_preds[to])
1135 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1137 if (bitmap_set_bit (graph->implicit_preds[to], from))
1138 stats.num_implicit_edges++;
1141 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1142 it doesn't exist in the graph already.
1143 Return false if the edge already existed, true otherwise. */
1145 static void
1146 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1147 unsigned int from)
1149 if (!graph->preds[to])
1150 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1151 bitmap_set_bit (graph->preds[to], from);
1154 /* Add a graph edge to GRAPH, going from FROM to TO if
1155 it doesn't exist in the graph already.
1156 Return false if the edge already existed, true otherwise. */
1158 static bool
1159 add_graph_edge (constraint_graph_t graph, unsigned int to,
1160 unsigned int from)
1162 if (to == from)
1164 return false;
1166 else
1168 bool r = false;
1170 if (!graph->succs[from])
1171 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1172 if (bitmap_set_bit (graph->succs[from], to))
1174 r = true;
1175 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1176 stats.num_edges++;
1178 return r;
1183 /* Initialize the constraint graph structure to contain SIZE nodes. */
1185 static void
1186 init_graph (unsigned int size)
1188 unsigned int j;
1190 graph = XCNEW (struct constraint_graph);
1191 graph->size = size;
1192 graph->succs = XCNEWVEC (bitmap, graph->size);
1193 graph->indirect_cycles = XNEWVEC (int, graph->size);
1194 graph->rep = XNEWVEC (unsigned int, graph->size);
1195 /* ??? Macros do not support template types with multiple arguments,
1196 so we use a typedef to work around it. */
1197 typedef vec<constraint_t> vec_constraint_t_heap;
1198 graph->complex = XCNEWVEC (vec_constraint_t_heap, size);
1199 graph->pe = XCNEWVEC (unsigned int, graph->size);
1200 graph->pe_rep = XNEWVEC (int, graph->size);
1202 for (j = 0; j < graph->size; j++)
1204 graph->rep[j] = j;
1205 graph->pe_rep[j] = -1;
1206 graph->indirect_cycles[j] = -1;
1210 /* Build the constraint graph, adding only predecessor edges right now. */
1212 static void
1213 build_pred_graph (void)
1215 int i;
1216 constraint_t c;
1217 unsigned int j;
1219 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1220 graph->preds = XCNEWVEC (bitmap, graph->size);
1221 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1222 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1223 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1224 graph->points_to = XCNEWVEC (bitmap, graph->size);
1225 graph->eq_rep = XNEWVEC (int, graph->size);
1226 graph->direct_nodes = sbitmap_alloc (graph->size);
1227 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1228 bitmap_clear (graph->direct_nodes);
1230 for (j = 1; j < FIRST_REF_NODE; j++)
1232 if (!get_varinfo (j)->is_special_var)
1233 bitmap_set_bit (graph->direct_nodes, j);
1236 for (j = 0; j < graph->size; j++)
1237 graph->eq_rep[j] = -1;
1239 for (j = 0; j < varmap.length (); j++)
1240 graph->indirect_cycles[j] = -1;
1242 FOR_EACH_VEC_ELT (constraints, i, c)
1244 struct constraint_expr lhs = c->lhs;
1245 struct constraint_expr rhs = c->rhs;
1246 unsigned int lhsvar = lhs.var;
1247 unsigned int rhsvar = rhs.var;
1249 if (lhs.type == DEREF)
1251 /* *x = y. */
1252 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1253 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1255 else if (rhs.type == DEREF)
1257 /* x = *y */
1258 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1259 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1260 else
1261 bitmap_clear_bit (graph->direct_nodes, lhsvar);
1263 else if (rhs.type == ADDRESSOF)
1265 varinfo_t v;
1267 /* x = &y */
1268 if (graph->points_to[lhsvar] == NULL)
1269 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1270 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1272 if (graph->pointed_by[rhsvar] == NULL)
1273 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1274 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1276 /* Implicitly, *x = y */
1277 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1279 /* All related variables are no longer direct nodes. */
1280 bitmap_clear_bit (graph->direct_nodes, rhsvar);
1281 v = get_varinfo (rhsvar);
1282 if (!v->is_full_var)
1284 v = get_varinfo (v->head);
1287 bitmap_clear_bit (graph->direct_nodes, v->id);
1288 v = vi_next (v);
1290 while (v != NULL);
1292 bitmap_set_bit (graph->address_taken, rhsvar);
1294 else if (lhsvar > anything_id
1295 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1297 /* x = y */
1298 add_pred_graph_edge (graph, lhsvar, rhsvar);
1299 /* Implicitly, *x = *y */
1300 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1301 FIRST_REF_NODE + rhsvar);
1303 else if (lhs.offset != 0 || rhs.offset != 0)
1305 if (rhs.offset != 0)
1306 bitmap_clear_bit (graph->direct_nodes, lhs.var);
1307 else if (lhs.offset != 0)
1308 bitmap_clear_bit (graph->direct_nodes, rhs.var);
1313 /* Build the constraint graph, adding successor edges. */
1315 static void
1316 build_succ_graph (void)
1318 unsigned i, t;
1319 constraint_t c;
1321 FOR_EACH_VEC_ELT (constraints, i, c)
1323 struct constraint_expr lhs;
1324 struct constraint_expr rhs;
1325 unsigned int lhsvar;
1326 unsigned int rhsvar;
1328 if (!c)
1329 continue;
1331 lhs = c->lhs;
1332 rhs = c->rhs;
1333 lhsvar = find (lhs.var);
1334 rhsvar = find (rhs.var);
1336 if (lhs.type == DEREF)
1338 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1339 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1341 else if (rhs.type == DEREF)
1343 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1344 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1346 else if (rhs.type == ADDRESSOF)
1348 /* x = &y */
1349 gcc_checking_assert (find (rhs.var) == rhs.var);
1350 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1352 else if (lhsvar > anything_id
1353 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1355 add_graph_edge (graph, lhsvar, rhsvar);
1359 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1360 receive pointers. */
1361 t = find (storedanything_id);
1362 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1364 if (!bitmap_bit_p (graph->direct_nodes, i)
1365 && get_varinfo (i)->may_have_pointers)
1366 add_graph_edge (graph, find (i), t);
1369 /* Everything stored to ANYTHING also potentially escapes. */
1370 add_graph_edge (graph, find (escaped_id), t);
1374 /* Changed variables on the last iteration. */
1375 static bitmap changed;
1377 /* Strongly Connected Component visitation info. */
1379 struct scc_info
1381 scc_info (size_t size);
1382 ~scc_info ();
1384 auto_sbitmap visited;
1385 auto_sbitmap deleted;
1386 unsigned int *dfs;
1387 unsigned int *node_mapping;
1388 int current_index;
1389 auto_vec<unsigned> scc_stack;
1393 /* Recursive routine to find strongly connected components in GRAPH.
1394 SI is the SCC info to store the information in, and N is the id of current
1395 graph node we are processing.
1397 This is Tarjan's strongly connected component finding algorithm, as
1398 modified by Nuutila to keep only non-root nodes on the stack.
1399 The algorithm can be found in "On finding the strongly connected
1400 connected components in a directed graph" by Esko Nuutila and Eljas
1401 Soisalon-Soininen, in Information Processing Letters volume 49,
1402 number 1, pages 9-14. */
1404 static void
1405 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1407 unsigned int i;
1408 bitmap_iterator bi;
1409 unsigned int my_dfs;
1411 bitmap_set_bit (si->visited, n);
1412 si->dfs[n] = si->current_index ++;
1413 my_dfs = si->dfs[n];
1415 /* Visit all the successors. */
1416 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1418 unsigned int w;
1420 if (i > LAST_REF_NODE)
1421 break;
1423 w = find (i);
1424 if (bitmap_bit_p (si->deleted, w))
1425 continue;
1427 if (!bitmap_bit_p (si->visited, w))
1428 scc_visit (graph, si, w);
1430 unsigned int t = find (w);
1431 gcc_checking_assert (find (n) == n);
1432 if (si->dfs[t] < si->dfs[n])
1433 si->dfs[n] = si->dfs[t];
1436 /* See if any components have been identified. */
1437 if (si->dfs[n] == my_dfs)
1439 if (si->scc_stack.length () > 0
1440 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1442 bitmap scc = BITMAP_ALLOC (NULL);
1443 unsigned int lowest_node;
1444 bitmap_iterator bi;
1446 bitmap_set_bit (scc, n);
1448 while (si->scc_stack.length () != 0
1449 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1451 unsigned int w = si->scc_stack.pop ();
1453 bitmap_set_bit (scc, w);
1456 lowest_node = bitmap_first_set_bit (scc);
1457 gcc_assert (lowest_node < FIRST_REF_NODE);
1459 /* Collapse the SCC nodes into a single node, and mark the
1460 indirect cycles. */
1461 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1463 if (i < FIRST_REF_NODE)
1465 if (unite (lowest_node, i))
1466 unify_nodes (graph, lowest_node, i, false);
1468 else
1470 unite (lowest_node, i);
1471 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1475 bitmap_set_bit (si->deleted, n);
1477 else
1478 si->scc_stack.safe_push (n);
1481 /* Unify node FROM into node TO, updating the changed count if
1482 necessary when UPDATE_CHANGED is true. */
1484 static void
1485 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1486 bool update_changed)
1488 gcc_checking_assert (to != from && find (to) == to);
1490 if (dump_file && (dump_flags & TDF_DETAILS))
1491 fprintf (dump_file, "Unifying %s to %s\n",
1492 get_varinfo (from)->name,
1493 get_varinfo (to)->name);
1495 if (update_changed)
1496 stats.unified_vars_dynamic++;
1497 else
1498 stats.unified_vars_static++;
1500 merge_graph_nodes (graph, to, from);
1501 if (merge_node_constraints (graph, to, from))
1503 if (update_changed)
1504 bitmap_set_bit (changed, to);
1507 /* Mark TO as changed if FROM was changed. If TO was already marked
1508 as changed, decrease the changed count. */
1510 if (update_changed
1511 && bitmap_clear_bit (changed, from))
1512 bitmap_set_bit (changed, to);
1513 varinfo_t fromvi = get_varinfo (from);
1514 if (fromvi->solution)
1516 /* If the solution changes because of the merging, we need to mark
1517 the variable as changed. */
1518 varinfo_t tovi = get_varinfo (to);
1519 if (bitmap_ior_into (tovi->solution, fromvi->solution))
1521 if (update_changed)
1522 bitmap_set_bit (changed, to);
1525 BITMAP_FREE (fromvi->solution);
1526 if (fromvi->oldsolution)
1527 BITMAP_FREE (fromvi->oldsolution);
1529 if (stats.iterations > 0
1530 && tovi->oldsolution)
1531 BITMAP_FREE (tovi->oldsolution);
1533 if (graph->succs[to])
1534 bitmap_clear_bit (graph->succs[to], to);
1537 /* Information needed to compute the topological ordering of a graph. */
1539 struct topo_info
1541 /* sbitmap of visited nodes. */
1542 sbitmap visited;
1543 /* Array that stores the topological order of the graph, *in
1544 reverse*. */
1545 vec<unsigned> topo_order;
1549 /* Initialize and return a topological info structure. */
1551 static struct topo_info *
1552 init_topo_info (void)
1554 size_t size = graph->size;
1555 struct topo_info *ti = XNEW (struct topo_info);
1556 ti->visited = sbitmap_alloc (size);
1557 bitmap_clear (ti->visited);
1558 ti->topo_order.create (1);
1559 return ti;
1563 /* Free the topological sort info pointed to by TI. */
1565 static void
1566 free_topo_info (struct topo_info *ti)
1568 sbitmap_free (ti->visited);
1569 ti->topo_order.release ();
1570 free (ti);
1573 /* Visit the graph in topological order, and store the order in the
1574 topo_info structure. */
1576 static void
1577 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1578 unsigned int n)
1580 bitmap_iterator bi;
1581 unsigned int j;
1583 bitmap_set_bit (ti->visited, n);
1585 if (graph->succs[n])
1586 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1588 if (!bitmap_bit_p (ti->visited, j))
1589 topo_visit (graph, ti, j);
1592 ti->topo_order.safe_push (n);
1595 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1596 starting solution for y. */
1598 static void
1599 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1600 bitmap delta, bitmap *expanded_delta)
1602 unsigned int lhs = c->lhs.var;
1603 bool flag = false;
1604 bitmap sol = get_varinfo (lhs)->solution;
1605 unsigned int j;
1606 bitmap_iterator bi;
1607 HOST_WIDE_INT roffset = c->rhs.offset;
1609 /* Our IL does not allow this. */
1610 gcc_checking_assert (c->lhs.offset == 0);
1612 /* If the solution of Y contains anything it is good enough to transfer
1613 this to the LHS. */
1614 if (bitmap_bit_p (delta, anything_id))
1616 flag |= bitmap_set_bit (sol, anything_id);
1617 goto done;
1620 /* If we do not know at with offset the rhs is dereferenced compute
1621 the reachability set of DELTA, conservatively assuming it is
1622 dereferenced at all valid offsets. */
1623 if (roffset == UNKNOWN_OFFSET)
1625 delta = solution_set_expand (delta, expanded_delta);
1626 /* No further offset processing is necessary. */
1627 roffset = 0;
1630 /* For each variable j in delta (Sol(y)), add
1631 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1632 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1634 varinfo_t v = get_varinfo (j);
1635 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1636 unsigned HOST_WIDE_INT size = v->size;
1637 unsigned int t;
1639 if (v->is_full_var)
1641 else if (roffset != 0)
1643 if (fieldoffset < 0)
1644 v = get_varinfo (v->head);
1645 else
1646 v = first_or_preceding_vi_for_offset (v, fieldoffset);
1649 /* We have to include all fields that overlap the current field
1650 shifted by roffset. */
1653 t = find (v->id);
1655 /* Adding edges from the special vars is pointless.
1656 They don't have sets that can change. */
1657 if (get_varinfo (t)->is_special_var)
1658 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1659 /* Merging the solution from ESCAPED needlessly increases
1660 the set. Use ESCAPED as representative instead. */
1661 else if (v->id == escaped_id)
1662 flag |= bitmap_set_bit (sol, escaped_id);
1663 else if (v->may_have_pointers
1664 && add_graph_edge (graph, lhs, t))
1665 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1667 if (v->is_full_var
1668 || v->next == 0)
1669 break;
1671 v = vi_next (v);
1673 while (v->offset < fieldoffset + size);
1676 done:
1677 /* If the LHS solution changed, mark the var as changed. */
1678 if (flag)
1680 get_varinfo (lhs)->solution = sol;
1681 bitmap_set_bit (changed, lhs);
1685 /* Process a constraint C that represents *(x + off) = y using DELTA
1686 as the starting solution for x. */
1688 static void
1689 do_ds_constraint (constraint_t c, bitmap delta, bitmap *expanded_delta)
1691 unsigned int rhs = c->rhs.var;
1692 bitmap sol = get_varinfo (rhs)->solution;
1693 unsigned int j;
1694 bitmap_iterator bi;
1695 HOST_WIDE_INT loff = c->lhs.offset;
1696 bool escaped_p = false;
1698 /* Our IL does not allow this. */
1699 gcc_checking_assert (c->rhs.offset == 0);
1701 /* If the solution of y contains ANYTHING simply use the ANYTHING
1702 solution. This avoids needlessly increasing the points-to sets. */
1703 if (bitmap_bit_p (sol, anything_id))
1704 sol = get_varinfo (find (anything_id))->solution;
1706 /* If the solution for x contains ANYTHING we have to merge the
1707 solution of y into all pointer variables which we do via
1708 STOREDANYTHING. */
1709 if (bitmap_bit_p (delta, anything_id))
1711 unsigned t = find (storedanything_id);
1712 if (add_graph_edge (graph, t, rhs))
1714 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1715 bitmap_set_bit (changed, t);
1717 return;
1720 /* If we do not know at with offset the rhs is dereferenced compute
1721 the reachability set of DELTA, conservatively assuming it is
1722 dereferenced at all valid offsets. */
1723 if (loff == UNKNOWN_OFFSET)
1725 delta = solution_set_expand (delta, expanded_delta);
1726 loff = 0;
1729 /* For each member j of delta (Sol(x)), add an edge from y to j and
1730 union Sol(y) into Sol(j) */
1731 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1733 varinfo_t v = get_varinfo (j);
1734 unsigned int t;
1735 HOST_WIDE_INT fieldoffset = v->offset + loff;
1736 unsigned HOST_WIDE_INT size = v->size;
1738 if (v->is_full_var)
1740 else if (loff != 0)
1742 if (fieldoffset < 0)
1743 v = get_varinfo (v->head);
1744 else
1745 v = first_or_preceding_vi_for_offset (v, fieldoffset);
1748 /* We have to include all fields that overlap the current field
1749 shifted by loff. */
1752 if (v->may_have_pointers)
1754 /* If v is a global variable then this is an escape point. */
1755 if (v->is_global_var
1756 && !escaped_p)
1758 t = find (escaped_id);
1759 if (add_graph_edge (graph, t, rhs)
1760 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1761 bitmap_set_bit (changed, t);
1762 /* Enough to let rhs escape once. */
1763 escaped_p = true;
1766 if (v->is_special_var)
1767 break;
1769 t = find (v->id);
1770 if (add_graph_edge (graph, t, rhs)
1771 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1772 bitmap_set_bit (changed, t);
1775 if (v->is_full_var
1776 || v->next == 0)
1777 break;
1779 v = vi_next (v);
1781 while (v->offset < fieldoffset + size);
1785 /* Handle a non-simple (simple meaning requires no iteration),
1786 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1788 static void
1789 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta,
1790 bitmap *expanded_delta)
1792 if (c->lhs.type == DEREF)
1794 if (c->rhs.type == ADDRESSOF)
1796 gcc_unreachable ();
1798 else
1800 /* *x = y */
1801 do_ds_constraint (c, delta, expanded_delta);
1804 else if (c->rhs.type == DEREF)
1806 /* x = *y */
1807 if (!(get_varinfo (c->lhs.var)->is_special_var))
1808 do_sd_constraint (graph, c, delta, expanded_delta);
1810 else
1812 bitmap tmp;
1813 bool flag = false;
1815 gcc_checking_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR
1816 && c->rhs.offset != 0 && c->lhs.offset == 0);
1817 tmp = get_varinfo (c->lhs.var)->solution;
1819 flag = set_union_with_increment (tmp, delta, c->rhs.offset,
1820 expanded_delta);
1822 if (flag)
1823 bitmap_set_bit (changed, c->lhs.var);
1827 /* Initialize and return a new SCC info structure. */
1829 scc_info::scc_info (size_t size) :
1830 visited (size), deleted (size), current_index (0), scc_stack (1)
1832 bitmap_clear (visited);
1833 bitmap_clear (deleted);
1834 node_mapping = XNEWVEC (unsigned int, size);
1835 dfs = XCNEWVEC (unsigned int, size);
1837 for (size_t i = 0; i < size; i++)
1838 node_mapping[i] = i;
1841 /* Free an SCC info structure pointed to by SI */
1843 scc_info::~scc_info ()
1845 free (node_mapping);
1846 free (dfs);
1850 /* Find indirect cycles in GRAPH that occur, using strongly connected
1851 components, and note them in the indirect cycles map.
1853 This technique comes from Ben Hardekopf and Calvin Lin,
1854 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1855 Lines of Code", submitted to PLDI 2007. */
1857 static void
1858 find_indirect_cycles (constraint_graph_t graph)
1860 unsigned int i;
1861 unsigned int size = graph->size;
1862 scc_info si (size);
1864 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1865 if (!bitmap_bit_p (si.visited, i) && find (i) == i)
1866 scc_visit (graph, &si, i);
1869 /* Compute a topological ordering for GRAPH, and store the result in the
1870 topo_info structure TI. */
1872 static void
1873 compute_topo_order (constraint_graph_t graph,
1874 struct topo_info *ti)
1876 unsigned int i;
1877 unsigned int size = graph->size;
1879 for (i = 0; i != size; ++i)
1880 if (!bitmap_bit_p (ti->visited, i) && find (i) == i)
1881 topo_visit (graph, ti, i);
1884 /* Structure used to for hash value numbering of pointer equivalence
1885 classes. */
1887 typedef struct equiv_class_label
1889 hashval_t hashcode;
1890 unsigned int equivalence_class;
1891 bitmap labels;
1892 } *equiv_class_label_t;
1893 typedef const struct equiv_class_label *const_equiv_class_label_t;
1895 /* Equiv_class_label hashtable helpers. */
1897 struct equiv_class_hasher : free_ptr_hash <equiv_class_label>
1899 static inline hashval_t hash (const equiv_class_label *);
1900 static inline bool equal (const equiv_class_label *,
1901 const equiv_class_label *);
1904 /* Hash function for a equiv_class_label_t */
1906 inline hashval_t
1907 equiv_class_hasher::hash (const equiv_class_label *ecl)
1909 return ecl->hashcode;
1912 /* Equality function for two equiv_class_label_t's. */
1914 inline bool
1915 equiv_class_hasher::equal (const equiv_class_label *eql1,
1916 const equiv_class_label *eql2)
1918 return (eql1->hashcode == eql2->hashcode
1919 && bitmap_equal_p (eql1->labels, eql2->labels));
1922 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1923 classes. */
1924 static hash_table<equiv_class_hasher> *pointer_equiv_class_table;
1926 /* A hashtable for mapping a bitmap of labels->location equivalence
1927 classes. */
1928 static hash_table<equiv_class_hasher> *location_equiv_class_table;
1930 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1931 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1932 is equivalent to. */
1934 static equiv_class_label *
1935 equiv_class_lookup_or_add (hash_table<equiv_class_hasher> *table,
1936 bitmap labels)
1938 equiv_class_label **slot;
1939 equiv_class_label ecl;
1941 ecl.labels = labels;
1942 ecl.hashcode = bitmap_hash (labels);
1943 slot = table->find_slot (&ecl, INSERT);
1944 if (!*slot)
1946 *slot = XNEW (struct equiv_class_label);
1947 (*slot)->labels = labels;
1948 (*slot)->hashcode = ecl.hashcode;
1949 (*slot)->equivalence_class = 0;
1952 return *slot;
1955 /* Perform offline variable substitution.
1957 This is a worst case quadratic time way of identifying variables
1958 that must have equivalent points-to sets, including those caused by
1959 static cycles, and single entry subgraphs, in the constraint graph.
1961 The technique is described in "Exploiting Pointer and Location
1962 Equivalence to Optimize Pointer Analysis. In the 14th International
1963 Static Analysis Symposium (SAS), August 2007." It is known as the
1964 "HU" algorithm, and is equivalent to value numbering the collapsed
1965 constraint graph including evaluating unions.
1967 The general method of finding equivalence classes is as follows:
1968 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1969 Initialize all non-REF nodes to be direct nodes.
1970 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1971 variable}
1972 For each constraint containing the dereference, we also do the same
1973 thing.
1975 We then compute SCC's in the graph and unify nodes in the same SCC,
1976 including pts sets.
1978 For each non-collapsed node x:
1979 Visit all unvisited explicit incoming edges.
1980 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1981 where y->x.
1982 Lookup the equivalence class for pts(x).
1983 If we found one, equivalence_class(x) = found class.
1984 Otherwise, equivalence_class(x) = new class, and new_class is
1985 added to the lookup table.
1987 All direct nodes with the same equivalence class can be replaced
1988 with a single representative node.
1989 All unlabeled nodes (label == 0) are not pointers and all edges
1990 involving them can be eliminated.
1991 We perform these optimizations during rewrite_constraints
1993 In addition to pointer equivalence class finding, we also perform
1994 location equivalence class finding. This is the set of variables
1995 that always appear together in points-to sets. We use this to
1996 compress the size of the points-to sets. */
1998 /* Current maximum pointer equivalence class id. */
1999 static int pointer_equiv_class;
2001 /* Current maximum location equivalence class id. */
2002 static int location_equiv_class;
2004 /* Recursive routine to find strongly connected components in GRAPH,
2005 and label it's nodes with DFS numbers. */
2007 static void
2008 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2010 unsigned int i;
2011 bitmap_iterator bi;
2012 unsigned int my_dfs;
2014 gcc_checking_assert (si->node_mapping[n] == n);
2015 bitmap_set_bit (si->visited, n);
2016 si->dfs[n] = si->current_index ++;
2017 my_dfs = si->dfs[n];
2019 /* Visit all the successors. */
2020 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2022 unsigned int w = si->node_mapping[i];
2024 if (bitmap_bit_p (si->deleted, w))
2025 continue;
2027 if (!bitmap_bit_p (si->visited, w))
2028 condense_visit (graph, si, w);
2030 unsigned int t = si->node_mapping[w];
2031 gcc_checking_assert (si->node_mapping[n] == n);
2032 if (si->dfs[t] < si->dfs[n])
2033 si->dfs[n] = si->dfs[t];
2036 /* Visit all the implicit predecessors. */
2037 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2039 unsigned int w = si->node_mapping[i];
2041 if (bitmap_bit_p (si->deleted, w))
2042 continue;
2044 if (!bitmap_bit_p (si->visited, w))
2045 condense_visit (graph, si, w);
2047 unsigned int t = si->node_mapping[w];
2048 gcc_assert (si->node_mapping[n] == n);
2049 if (si->dfs[t] < si->dfs[n])
2050 si->dfs[n] = si->dfs[t];
2053 /* See if any components have been identified. */
2054 if (si->dfs[n] == my_dfs)
2056 while (si->scc_stack.length () != 0
2057 && si->dfs[si->scc_stack.last ()] >= my_dfs)
2059 unsigned int w = si->scc_stack.pop ();
2060 si->node_mapping[w] = n;
2062 if (!bitmap_bit_p (graph->direct_nodes, w))
2063 bitmap_clear_bit (graph->direct_nodes, n);
2065 /* Unify our nodes. */
2066 if (graph->preds[w])
2068 if (!graph->preds[n])
2069 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2070 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2072 if (graph->implicit_preds[w])
2074 if (!graph->implicit_preds[n])
2075 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2076 bitmap_ior_into (graph->implicit_preds[n],
2077 graph->implicit_preds[w]);
2079 if (graph->points_to[w])
2081 if (!graph->points_to[n])
2082 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2083 bitmap_ior_into (graph->points_to[n],
2084 graph->points_to[w]);
2087 bitmap_set_bit (si->deleted, n);
2089 else
2090 si->scc_stack.safe_push (n);
2093 /* Label pointer equivalences.
2095 This performs a value numbering of the constraint graph to
2096 discover which variables will always have the same points-to sets
2097 under the current set of constraints.
2099 The way it value numbers is to store the set of points-to bits
2100 generated by the constraints and graph edges. This is just used as a
2101 hash and equality comparison. The *actual set of points-to bits* is
2102 completely irrelevant, in that we don't care about being able to
2103 extract them later.
2105 The equality values (currently bitmaps) just have to satisfy a few
2106 constraints, the main ones being:
2107 1. The combining operation must be order independent.
2108 2. The end result of a given set of operations must be unique iff the
2109 combination of input values is unique
2110 3. Hashable. */
2112 static void
2113 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2115 unsigned int i, first_pred;
2116 bitmap_iterator bi;
2118 bitmap_set_bit (si->visited, n);
2120 /* Label and union our incoming edges's points to sets. */
2121 first_pred = -1U;
2122 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2124 unsigned int w = si->node_mapping[i];
2125 if (!bitmap_bit_p (si->visited, w))
2126 label_visit (graph, si, w);
2128 /* Skip unused edges */
2129 if (w == n || graph->pointer_label[w] == 0)
2130 continue;
2132 if (graph->points_to[w])
2134 if (!graph->points_to[n])
2136 if (first_pred == -1U)
2137 first_pred = w;
2138 else
2140 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2141 bitmap_ior (graph->points_to[n],
2142 graph->points_to[first_pred],
2143 graph->points_to[w]);
2146 else
2147 bitmap_ior_into (graph->points_to[n], graph->points_to[w]);
2151 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2152 if (!bitmap_bit_p (graph->direct_nodes, n))
2154 if (!graph->points_to[n])
2156 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2157 if (first_pred != -1U)
2158 bitmap_copy (graph->points_to[n], graph->points_to[first_pred]);
2160 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2161 graph->pointer_label[n] = pointer_equiv_class++;
2162 equiv_class_label_t ecl;
2163 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2164 graph->points_to[n]);
2165 ecl->equivalence_class = graph->pointer_label[n];
2166 return;
2169 /* If there was only a single non-empty predecessor the pointer equiv
2170 class is the same. */
2171 if (!graph->points_to[n])
2173 if (first_pred != -1U)
2175 graph->pointer_label[n] = graph->pointer_label[first_pred];
2176 graph->points_to[n] = graph->points_to[first_pred];
2178 return;
2181 if (!bitmap_empty_p (graph->points_to[n]))
2183 equiv_class_label_t ecl;
2184 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2185 graph->points_to[n]);
2186 if (ecl->equivalence_class == 0)
2187 ecl->equivalence_class = pointer_equiv_class++;
2188 else
2190 BITMAP_FREE (graph->points_to[n]);
2191 graph->points_to[n] = ecl->labels;
2193 graph->pointer_label[n] = ecl->equivalence_class;
2197 /* Print the pred graph in dot format. */
2199 static void
2200 dump_pred_graph (struct scc_info *si, FILE *file)
2202 unsigned int i;
2204 /* Only print the graph if it has already been initialized: */
2205 if (!graph)
2206 return;
2208 /* Prints the header of the dot file: */
2209 fprintf (file, "strict digraph {\n");
2210 fprintf (file, " node [\n shape = box\n ]\n");
2211 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
2212 fprintf (file, "\n // List of nodes and complex constraints in "
2213 "the constraint graph:\n");
2215 /* The next lines print the nodes in the graph together with the
2216 complex constraints attached to them. */
2217 for (i = 1; i < graph->size; i++)
2219 if (i == FIRST_REF_NODE)
2220 continue;
2221 if (si->node_mapping[i] != i)
2222 continue;
2223 if (i < FIRST_REF_NODE)
2224 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2225 else
2226 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2227 if (graph->points_to[i]
2228 && !bitmap_empty_p (graph->points_to[i]))
2230 if (i < FIRST_REF_NODE)
2231 fprintf (file, "[label=\"%s = {", get_varinfo (i)->name);
2232 else
2233 fprintf (file, "[label=\"*%s = {",
2234 get_varinfo (i - FIRST_REF_NODE)->name);
2235 unsigned j;
2236 bitmap_iterator bi;
2237 EXECUTE_IF_SET_IN_BITMAP (graph->points_to[i], 0, j, bi)
2238 fprintf (file, " %d", j);
2239 fprintf (file, " }\"]");
2241 fprintf (file, ";\n");
2244 /* Go over the edges. */
2245 fprintf (file, "\n // Edges in the constraint graph:\n");
2246 for (i = 1; i < graph->size; i++)
2248 unsigned j;
2249 bitmap_iterator bi;
2250 if (si->node_mapping[i] != i)
2251 continue;
2252 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[i], 0, j, bi)
2254 unsigned from = si->node_mapping[j];
2255 if (from < FIRST_REF_NODE)
2256 fprintf (file, "\"%s\"", get_varinfo (from)->name);
2257 else
2258 fprintf (file, "\"*%s\"", get_varinfo (from - FIRST_REF_NODE)->name);
2259 fprintf (file, " -> ");
2260 if (i < FIRST_REF_NODE)
2261 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2262 else
2263 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2264 fprintf (file, ";\n");
2268 /* Prints the tail of the dot file. */
2269 fprintf (file, "}\n");
2272 /* Perform offline variable substitution, discovering equivalence
2273 classes, and eliminating non-pointer variables. */
2275 static struct scc_info *
2276 perform_var_substitution (constraint_graph_t graph)
2278 unsigned int i;
2279 unsigned int size = graph->size;
2280 scc_info *si = new scc_info (size);
2282 bitmap_obstack_initialize (&iteration_obstack);
2283 pointer_equiv_class_table = new hash_table<equiv_class_hasher> (511);
2284 location_equiv_class_table
2285 = new hash_table<equiv_class_hasher> (511);
2286 pointer_equiv_class = 1;
2287 location_equiv_class = 1;
2289 /* Condense the nodes, which means to find SCC's, count incoming
2290 predecessors, and unite nodes in SCC's. */
2291 for (i = 1; i < FIRST_REF_NODE; i++)
2292 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2293 condense_visit (graph, si, si->node_mapping[i]);
2295 if (dump_file && (dump_flags & TDF_GRAPH))
2297 fprintf (dump_file, "\n\n// The constraint graph before var-substitution "
2298 "in dot format:\n");
2299 dump_pred_graph (si, dump_file);
2300 fprintf (dump_file, "\n\n");
2303 bitmap_clear (si->visited);
2304 /* Actually the label the nodes for pointer equivalences */
2305 for (i = 1; i < FIRST_REF_NODE; i++)
2306 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2307 label_visit (graph, si, si->node_mapping[i]);
2309 /* Calculate location equivalence labels. */
2310 for (i = 1; i < FIRST_REF_NODE; i++)
2312 bitmap pointed_by;
2313 bitmap_iterator bi;
2314 unsigned int j;
2316 if (!graph->pointed_by[i])
2317 continue;
2318 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2320 /* Translate the pointed-by mapping for pointer equivalence
2321 labels. */
2322 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2324 bitmap_set_bit (pointed_by,
2325 graph->pointer_label[si->node_mapping[j]]);
2327 /* The original pointed_by is now dead. */
2328 BITMAP_FREE (graph->pointed_by[i]);
2330 /* Look up the location equivalence label if one exists, or make
2331 one otherwise. */
2332 equiv_class_label_t ecl;
2333 ecl = equiv_class_lookup_or_add (location_equiv_class_table, pointed_by);
2334 if (ecl->equivalence_class == 0)
2335 ecl->equivalence_class = location_equiv_class++;
2336 else
2338 if (dump_file && (dump_flags & TDF_DETAILS))
2339 fprintf (dump_file, "Found location equivalence for node %s\n",
2340 get_varinfo (i)->name);
2341 BITMAP_FREE (pointed_by);
2343 graph->loc_label[i] = ecl->equivalence_class;
2347 if (dump_file && (dump_flags & TDF_DETAILS))
2348 for (i = 1; i < FIRST_REF_NODE; i++)
2350 unsigned j = si->node_mapping[i];
2351 if (j != i)
2353 fprintf (dump_file, "%s node id %d ",
2354 bitmap_bit_p (graph->direct_nodes, i)
2355 ? "Direct" : "Indirect", i);
2356 if (i < FIRST_REF_NODE)
2357 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2358 else
2359 fprintf (dump_file, "\"*%s\"",
2360 get_varinfo (i - FIRST_REF_NODE)->name);
2361 fprintf (dump_file, " mapped to SCC leader node id %d ", j);
2362 if (j < FIRST_REF_NODE)
2363 fprintf (dump_file, "\"%s\"\n", get_varinfo (j)->name);
2364 else
2365 fprintf (dump_file, "\"*%s\"\n",
2366 get_varinfo (j - FIRST_REF_NODE)->name);
2368 else
2370 fprintf (dump_file,
2371 "Equivalence classes for %s node id %d ",
2372 bitmap_bit_p (graph->direct_nodes, i)
2373 ? "direct" : "indirect", i);
2374 if (i < FIRST_REF_NODE)
2375 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2376 else
2377 fprintf (dump_file, "\"*%s\"",
2378 get_varinfo (i - FIRST_REF_NODE)->name);
2379 fprintf (dump_file,
2380 ": pointer %d, location %d\n",
2381 graph->pointer_label[i], graph->loc_label[i]);
2385 /* Quickly eliminate our non-pointer variables. */
2387 for (i = 1; i < FIRST_REF_NODE; i++)
2389 unsigned int node = si->node_mapping[i];
2391 if (graph->pointer_label[node] == 0)
2393 if (dump_file && (dump_flags & TDF_DETAILS))
2394 fprintf (dump_file,
2395 "%s is a non-pointer variable, eliminating edges.\n",
2396 get_varinfo (node)->name);
2397 stats.nonpointer_vars++;
2398 clear_edges_for_node (graph, node);
2402 return si;
2405 /* Free information that was only necessary for variable
2406 substitution. */
2408 static void
2409 free_var_substitution_info (struct scc_info *si)
2411 delete si;
2412 free (graph->pointer_label);
2413 free (graph->loc_label);
2414 free (graph->pointed_by);
2415 free (graph->points_to);
2416 free (graph->eq_rep);
2417 sbitmap_free (graph->direct_nodes);
2418 delete pointer_equiv_class_table;
2419 pointer_equiv_class_table = NULL;
2420 delete location_equiv_class_table;
2421 location_equiv_class_table = NULL;
2422 bitmap_obstack_release (&iteration_obstack);
2425 /* Return an existing node that is equivalent to NODE, which has
2426 equivalence class LABEL, if one exists. Return NODE otherwise. */
2428 static unsigned int
2429 find_equivalent_node (constraint_graph_t graph,
2430 unsigned int node, unsigned int label)
2432 /* If the address version of this variable is unused, we can
2433 substitute it for anything else with the same label.
2434 Otherwise, we know the pointers are equivalent, but not the
2435 locations, and we can unite them later. */
2437 if (!bitmap_bit_p (graph->address_taken, node))
2439 gcc_checking_assert (label < graph->size);
2441 if (graph->eq_rep[label] != -1)
2443 /* Unify the two variables since we know they are equivalent. */
2444 if (unite (graph->eq_rep[label], node))
2445 unify_nodes (graph, graph->eq_rep[label], node, false);
2446 return graph->eq_rep[label];
2448 else
2450 graph->eq_rep[label] = node;
2451 graph->pe_rep[label] = node;
2454 else
2456 gcc_checking_assert (label < graph->size);
2457 graph->pe[node] = label;
2458 if (graph->pe_rep[label] == -1)
2459 graph->pe_rep[label] = node;
2462 return node;
2465 /* Unite pointer equivalent but not location equivalent nodes in
2466 GRAPH. This may only be performed once variable substitution is
2467 finished. */
2469 static void
2470 unite_pointer_equivalences (constraint_graph_t graph)
2472 unsigned int i;
2474 /* Go through the pointer equivalences and unite them to their
2475 representative, if they aren't already. */
2476 for (i = 1; i < FIRST_REF_NODE; i++)
2478 unsigned int label = graph->pe[i];
2479 if (label)
2481 int label_rep = graph->pe_rep[label];
2483 if (label_rep == -1)
2484 continue;
2486 label_rep = find (label_rep);
2487 if (label_rep >= 0 && unite (label_rep, find (i)))
2488 unify_nodes (graph, label_rep, i, false);
2493 /* Move complex constraints to the GRAPH nodes they belong to. */
2495 static void
2496 move_complex_constraints (constraint_graph_t graph)
2498 int i;
2499 constraint_t c;
2501 FOR_EACH_VEC_ELT (constraints, i, c)
2503 if (c)
2505 struct constraint_expr lhs = c->lhs;
2506 struct constraint_expr rhs = c->rhs;
2508 if (lhs.type == DEREF)
2510 insert_into_complex (graph, lhs.var, c);
2512 else if (rhs.type == DEREF)
2514 if (!(get_varinfo (lhs.var)->is_special_var))
2515 insert_into_complex (graph, rhs.var, c);
2517 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2518 && (lhs.offset != 0 || rhs.offset != 0))
2520 insert_into_complex (graph, rhs.var, c);
2527 /* Optimize and rewrite complex constraints while performing
2528 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2529 result of perform_variable_substitution. */
2531 static void
2532 rewrite_constraints (constraint_graph_t graph,
2533 struct scc_info *si)
2535 int i;
2536 constraint_t c;
2538 if (flag_checking)
2540 for (unsigned int j = 0; j < graph->size; j++)
2541 gcc_assert (find (j) == j);
2544 FOR_EACH_VEC_ELT (constraints, i, c)
2546 struct constraint_expr lhs = c->lhs;
2547 struct constraint_expr rhs = c->rhs;
2548 unsigned int lhsvar = find (lhs.var);
2549 unsigned int rhsvar = find (rhs.var);
2550 unsigned int lhsnode, rhsnode;
2551 unsigned int lhslabel, rhslabel;
2553 lhsnode = si->node_mapping[lhsvar];
2554 rhsnode = si->node_mapping[rhsvar];
2555 lhslabel = graph->pointer_label[lhsnode];
2556 rhslabel = graph->pointer_label[rhsnode];
2558 /* See if it is really a non-pointer variable, and if so, ignore
2559 the constraint. */
2560 if (lhslabel == 0)
2562 if (dump_file && (dump_flags & TDF_DETAILS))
2565 fprintf (dump_file, "%s is a non-pointer variable,"
2566 "ignoring constraint:",
2567 get_varinfo (lhs.var)->name);
2568 dump_constraint (dump_file, c);
2569 fprintf (dump_file, "\n");
2571 constraints[i] = NULL;
2572 continue;
2575 if (rhslabel == 0)
2577 if (dump_file && (dump_flags & TDF_DETAILS))
2580 fprintf (dump_file, "%s is a non-pointer variable,"
2581 "ignoring constraint:",
2582 get_varinfo (rhs.var)->name);
2583 dump_constraint (dump_file, c);
2584 fprintf (dump_file, "\n");
2586 constraints[i] = NULL;
2587 continue;
2590 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2591 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2592 c->lhs.var = lhsvar;
2593 c->rhs.var = rhsvar;
2597 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2598 part of an SCC, false otherwise. */
2600 static bool
2601 eliminate_indirect_cycles (unsigned int node)
2603 if (graph->indirect_cycles[node] != -1
2604 && !bitmap_empty_p (get_varinfo (node)->solution))
2606 unsigned int i;
2607 auto_vec<unsigned> queue;
2608 int queuepos;
2609 unsigned int to = find (graph->indirect_cycles[node]);
2610 bitmap_iterator bi;
2612 /* We can't touch the solution set and call unify_nodes
2613 at the same time, because unify_nodes is going to do
2614 bitmap unions into it. */
2616 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2618 if (find (i) == i && i != to)
2620 if (unite (to, i))
2621 queue.safe_push (i);
2625 for (queuepos = 0;
2626 queue.iterate (queuepos, &i);
2627 queuepos++)
2629 unify_nodes (graph, to, i, true);
2631 return true;
2633 return false;
2636 /* Solve the constraint graph GRAPH using our worklist solver.
2637 This is based on the PW* family of solvers from the "Efficient Field
2638 Sensitive Pointer Analysis for C" paper.
2639 It works by iterating over all the graph nodes, processing the complex
2640 constraints and propagating the copy constraints, until everything stops
2641 changed. This corresponds to steps 6-8 in the solving list given above. */
2643 static void
2644 solve_graph (constraint_graph_t graph)
2646 unsigned int size = graph->size;
2647 unsigned int i;
2648 bitmap pts;
2650 changed = BITMAP_ALLOC (NULL);
2652 /* Mark all initial non-collapsed nodes as changed. */
2653 for (i = 1; i < size; i++)
2655 varinfo_t ivi = get_varinfo (i);
2656 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2657 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2658 || graph->complex[i].length () > 0))
2659 bitmap_set_bit (changed, i);
2662 /* Allocate a bitmap to be used to store the changed bits. */
2663 pts = BITMAP_ALLOC (&pta_obstack);
2665 while (!bitmap_empty_p (changed))
2667 unsigned int i;
2668 struct topo_info *ti = init_topo_info ();
2669 stats.iterations++;
2671 bitmap_obstack_initialize (&iteration_obstack);
2673 compute_topo_order (graph, ti);
2675 while (ti->topo_order.length () != 0)
2678 i = ti->topo_order.pop ();
2680 /* If this variable is not a representative, skip it. */
2681 if (find (i) != i)
2682 continue;
2684 /* In certain indirect cycle cases, we may merge this
2685 variable to another. */
2686 if (eliminate_indirect_cycles (i) && find (i) != i)
2687 continue;
2689 /* If the node has changed, we need to process the
2690 complex constraints and outgoing edges again. */
2691 if (bitmap_clear_bit (changed, i))
2693 unsigned int j;
2694 constraint_t c;
2695 bitmap solution;
2696 vec<constraint_t> complex = graph->complex[i];
2697 varinfo_t vi = get_varinfo (i);
2698 bool solution_empty;
2700 /* Compute the changed set of solution bits. If anything
2701 is in the solution just propagate that. */
2702 if (bitmap_bit_p (vi->solution, anything_id))
2704 /* If anything is also in the old solution there is
2705 nothing to do.
2706 ??? But we shouldn't ended up with "changed" set ... */
2707 if (vi->oldsolution
2708 && bitmap_bit_p (vi->oldsolution, anything_id))
2709 continue;
2710 bitmap_copy (pts, get_varinfo (find (anything_id))->solution);
2712 else if (vi->oldsolution)
2713 bitmap_and_compl (pts, vi->solution, vi->oldsolution);
2714 else
2715 bitmap_copy (pts, vi->solution);
2717 if (bitmap_empty_p (pts))
2718 continue;
2720 if (vi->oldsolution)
2721 bitmap_ior_into (vi->oldsolution, pts);
2722 else
2724 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
2725 bitmap_copy (vi->oldsolution, pts);
2728 solution = vi->solution;
2729 solution_empty = bitmap_empty_p (solution);
2731 /* Process the complex constraints */
2732 bitmap expanded_pts = NULL;
2733 FOR_EACH_VEC_ELT (complex, j, c)
2735 /* XXX: This is going to unsort the constraints in
2736 some cases, which will occasionally add duplicate
2737 constraints during unification. This does not
2738 affect correctness. */
2739 c->lhs.var = find (c->lhs.var);
2740 c->rhs.var = find (c->rhs.var);
2742 /* The only complex constraint that can change our
2743 solution to non-empty, given an empty solution,
2744 is a constraint where the lhs side is receiving
2745 some set from elsewhere. */
2746 if (!solution_empty || c->lhs.type != DEREF)
2747 do_complex_constraint (graph, c, pts, &expanded_pts);
2749 BITMAP_FREE (expanded_pts);
2751 solution_empty = bitmap_empty_p (solution);
2753 if (!solution_empty)
2755 bitmap_iterator bi;
2756 unsigned eff_escaped_id = find (escaped_id);
2758 /* Propagate solution to all successors. */
2759 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2760 0, j, bi)
2762 bitmap tmp;
2763 bool flag;
2765 unsigned int to = find (j);
2766 tmp = get_varinfo (to)->solution;
2767 flag = false;
2769 /* Don't try to propagate to ourselves. */
2770 if (to == i)
2771 continue;
2773 /* If we propagate from ESCAPED use ESCAPED as
2774 placeholder. */
2775 if (i == eff_escaped_id)
2776 flag = bitmap_set_bit (tmp, escaped_id);
2777 else
2778 flag = bitmap_ior_into (tmp, pts);
2780 if (flag)
2781 bitmap_set_bit (changed, to);
2786 free_topo_info (ti);
2787 bitmap_obstack_release (&iteration_obstack);
2790 BITMAP_FREE (pts);
2791 BITMAP_FREE (changed);
2792 bitmap_obstack_release (&oldpta_obstack);
2795 /* Map from trees to variable infos. */
2796 static hash_map<tree, varinfo_t> *vi_for_tree;
2799 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2801 static void
2802 insert_vi_for_tree (tree t, varinfo_t vi)
2804 gcc_assert (vi);
2805 gcc_assert (!vi_for_tree->put (t, vi));
2808 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2809 exist in the map, return NULL, otherwise, return the varinfo we found. */
2811 static varinfo_t
2812 lookup_vi_for_tree (tree t)
2814 varinfo_t *slot = vi_for_tree->get (t);
2815 if (slot == NULL)
2816 return NULL;
2818 return *slot;
2821 /* Return a printable name for DECL */
2823 static const char *
2824 alias_get_name (tree decl)
2826 const char *res = NULL;
2827 char *temp;
2828 int num_printed = 0;
2830 if (!dump_file)
2831 return "NULL";
2833 if (TREE_CODE (decl) == SSA_NAME)
2835 res = get_name (decl);
2836 if (res)
2837 num_printed = asprintf (&temp, "%s_%u", res, SSA_NAME_VERSION (decl));
2838 else
2839 num_printed = asprintf (&temp, "_%u", SSA_NAME_VERSION (decl));
2840 if (num_printed > 0)
2842 res = ggc_strdup (temp);
2843 free (temp);
2846 else if (DECL_P (decl))
2848 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2849 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2850 else
2852 res = get_name (decl);
2853 if (!res)
2855 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2856 if (num_printed > 0)
2858 res = ggc_strdup (temp);
2859 free (temp);
2864 if (res != NULL)
2865 return res;
2867 return "NULL";
2870 /* Find the variable id for tree T in the map.
2871 If T doesn't exist in the map, create an entry for it and return it. */
2873 static varinfo_t
2874 get_vi_for_tree (tree t)
2876 varinfo_t *slot = vi_for_tree->get (t);
2877 if (slot == NULL)
2879 unsigned int id = create_variable_info_for (t, alias_get_name (t), false);
2880 return get_varinfo (id);
2883 return *slot;
2886 /* Get a scalar constraint expression for a new temporary variable. */
2888 static struct constraint_expr
2889 new_scalar_tmp_constraint_exp (const char *name, bool add_id)
2891 struct constraint_expr tmp;
2892 varinfo_t vi;
2894 vi = new_var_info (NULL_TREE, name, add_id);
2895 vi->offset = 0;
2896 vi->size = -1;
2897 vi->fullsize = -1;
2898 vi->is_full_var = 1;
2900 tmp.var = vi->id;
2901 tmp.type = SCALAR;
2902 tmp.offset = 0;
2904 return tmp;
2907 /* Get a constraint expression vector from an SSA_VAR_P node.
2908 If address_p is true, the result will be taken its address of. */
2910 static void
2911 get_constraint_for_ssa_var (tree t, vec<ce_s> *results, bool address_p)
2913 struct constraint_expr cexpr;
2914 varinfo_t vi;
2916 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2917 gcc_assert (TREE_CODE (t) == SSA_NAME || DECL_P (t));
2919 /* For parameters, get at the points-to set for the actual parm
2920 decl. */
2921 if (TREE_CODE (t) == SSA_NAME
2922 && SSA_NAME_IS_DEFAULT_DEF (t)
2923 && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2924 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL))
2926 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2927 return;
2930 /* For global variables resort to the alias target. */
2931 if (VAR_P (t) && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
2933 varpool_node *node = varpool_node::get (t);
2934 if (node && node->alias && node->analyzed)
2936 node = node->ultimate_alias_target ();
2937 /* Canonicalize the PT uid of all aliases to the ultimate target.
2938 ??? Hopefully the set of aliases can't change in a way that
2939 changes the ultimate alias target. */
2940 gcc_assert ((! DECL_PT_UID_SET_P (node->decl)
2941 || DECL_PT_UID (node->decl) == DECL_UID (node->decl))
2942 && (! DECL_PT_UID_SET_P (t)
2943 || DECL_PT_UID (t) == DECL_UID (node->decl)));
2944 DECL_PT_UID (t) = DECL_UID (node->decl);
2945 t = node->decl;
2949 vi = get_vi_for_tree (t);
2950 cexpr.var = vi->id;
2951 cexpr.type = SCALAR;
2952 cexpr.offset = 0;
2954 /* If we are not taking the address of the constraint expr, add all
2955 sub-fiels of the variable as well. */
2956 if (!address_p
2957 && !vi->is_full_var)
2959 for (; vi; vi = vi_next (vi))
2961 cexpr.var = vi->id;
2962 results->safe_push (cexpr);
2964 return;
2967 results->safe_push (cexpr);
2970 /* Process constraint T, performing various simplifications and then
2971 adding it to our list of overall constraints. */
2973 static void
2974 process_constraint (constraint_t t)
2976 struct constraint_expr rhs = t->rhs;
2977 struct constraint_expr lhs = t->lhs;
2979 gcc_assert (rhs.var < varmap.length ());
2980 gcc_assert (lhs.var < varmap.length ());
2982 /* If we didn't get any useful constraint from the lhs we get
2983 &ANYTHING as fallback from get_constraint_for. Deal with
2984 it here by turning it into *ANYTHING. */
2985 if (lhs.type == ADDRESSOF
2986 && lhs.var == anything_id)
2987 lhs.type = DEREF;
2989 /* ADDRESSOF on the lhs is invalid. */
2990 gcc_assert (lhs.type != ADDRESSOF);
2992 /* We shouldn't add constraints from things that cannot have pointers.
2993 It's not completely trivial to avoid in the callers, so do it here. */
2994 if (rhs.type != ADDRESSOF
2995 && !get_varinfo (rhs.var)->may_have_pointers)
2996 return;
2998 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2999 if (!get_varinfo (lhs.var)->may_have_pointers)
3000 return;
3002 /* This can happen in our IR with things like n->a = *p */
3003 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
3005 /* Split into tmp = *rhs, *lhs = tmp */
3006 struct constraint_expr tmplhs;
3007 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp", true);
3008 process_constraint (new_constraint (tmplhs, rhs));
3009 process_constraint (new_constraint (lhs, tmplhs));
3011 else if ((rhs.type != SCALAR || rhs.offset != 0) && lhs.type == DEREF)
3013 /* Split into tmp = &rhs, *lhs = tmp */
3014 struct constraint_expr tmplhs;
3015 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp", true);
3016 process_constraint (new_constraint (tmplhs, rhs));
3017 process_constraint (new_constraint (lhs, tmplhs));
3019 else
3021 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
3022 constraints.safe_push (t);
3027 /* Return the position, in bits, of FIELD_DECL from the beginning of its
3028 structure. */
3030 static HOST_WIDE_INT
3031 bitpos_of_field (const tree fdecl)
3033 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl))
3034 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl)))
3035 return -1;
3037 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
3038 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl)));
3042 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
3043 resulting constraint expressions in *RESULTS. */
3045 static void
3046 get_constraint_for_ptr_offset (tree ptr, tree offset,
3047 vec<ce_s> *results)
3049 struct constraint_expr c;
3050 unsigned int j, n;
3051 HOST_WIDE_INT rhsoffset;
3053 /* If we do not do field-sensitive PTA adding offsets to pointers
3054 does not change the points-to solution. */
3055 if (!use_field_sensitive)
3057 get_constraint_for_rhs (ptr, results);
3058 return;
3061 /* If the offset is not a non-negative integer constant that fits
3062 in a HOST_WIDE_INT, we have to fall back to a conservative
3063 solution which includes all sub-fields of all pointed-to
3064 variables of ptr. */
3065 if (offset == NULL_TREE
3066 || TREE_CODE (offset) != INTEGER_CST)
3067 rhsoffset = UNKNOWN_OFFSET;
3068 else
3070 /* Sign-extend the offset. */
3071 offset_int soffset = offset_int::from (offset, SIGNED);
3072 if (!wi::fits_shwi_p (soffset))
3073 rhsoffset = UNKNOWN_OFFSET;
3074 else
3076 /* Make sure the bit-offset also fits. */
3077 HOST_WIDE_INT rhsunitoffset = soffset.to_shwi ();
3078 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
3079 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
3080 rhsoffset = UNKNOWN_OFFSET;
3084 get_constraint_for_rhs (ptr, results);
3085 if (rhsoffset == 0)
3086 return;
3088 /* As we are eventually appending to the solution do not use
3089 vec::iterate here. */
3090 n = results->length ();
3091 for (j = 0; j < n; j++)
3093 varinfo_t curr;
3094 c = (*results)[j];
3095 curr = get_varinfo (c.var);
3097 if (c.type == ADDRESSOF
3098 /* If this varinfo represents a full variable just use it. */
3099 && curr->is_full_var)
3101 else if (c.type == ADDRESSOF
3102 /* If we do not know the offset add all subfields. */
3103 && rhsoffset == UNKNOWN_OFFSET)
3105 varinfo_t temp = get_varinfo (curr->head);
3108 struct constraint_expr c2;
3109 c2.var = temp->id;
3110 c2.type = ADDRESSOF;
3111 c2.offset = 0;
3112 if (c2.var != c.var)
3113 results->safe_push (c2);
3114 temp = vi_next (temp);
3116 while (temp);
3118 else if (c.type == ADDRESSOF)
3120 varinfo_t temp;
3121 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
3123 /* If curr->offset + rhsoffset is less than zero adjust it. */
3124 if (rhsoffset < 0
3125 && curr->offset < offset)
3126 offset = 0;
3128 /* We have to include all fields that overlap the current
3129 field shifted by rhsoffset. And we include at least
3130 the last or the first field of the variable to represent
3131 reachability of off-bound addresses, in particular &object + 1,
3132 conservatively correct. */
3133 temp = first_or_preceding_vi_for_offset (curr, offset);
3134 c.var = temp->id;
3135 c.offset = 0;
3136 temp = vi_next (temp);
3137 while (temp
3138 && temp->offset < offset + curr->size)
3140 struct constraint_expr c2;
3141 c2.var = temp->id;
3142 c2.type = ADDRESSOF;
3143 c2.offset = 0;
3144 results->safe_push (c2);
3145 temp = vi_next (temp);
3148 else if (c.type == SCALAR)
3150 gcc_assert (c.offset == 0);
3151 c.offset = rhsoffset;
3153 else
3154 /* We shouldn't get any DEREFs here. */
3155 gcc_unreachable ();
3157 (*results)[j] = c;
3162 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3163 If address_p is true the result will be taken its address of.
3164 If lhs_p is true then the constraint expression is assumed to be used
3165 as the lhs. */
3167 static void
3168 get_constraint_for_component_ref (tree t, vec<ce_s> *results,
3169 bool address_p, bool lhs_p)
3171 tree orig_t = t;
3172 HOST_WIDE_INT bitsize = -1;
3173 HOST_WIDE_INT bitmaxsize = -1;
3174 HOST_WIDE_INT bitpos;
3175 bool reverse;
3176 tree forzero;
3178 /* Some people like to do cute things like take the address of
3179 &0->a.b */
3180 forzero = t;
3181 while (handled_component_p (forzero)
3182 || INDIRECT_REF_P (forzero)
3183 || TREE_CODE (forzero) == MEM_REF)
3184 forzero = TREE_OPERAND (forzero, 0);
3186 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
3188 struct constraint_expr temp;
3190 temp.offset = 0;
3191 temp.var = integer_id;
3192 temp.type = SCALAR;
3193 results->safe_push (temp);
3194 return;
3197 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize, &reverse);
3199 /* We can end up here for component references on a
3200 VIEW_CONVERT_EXPR <>(&foobar) or things like a
3201 BIT_FIELD_REF <&MEM[(void *)&b + 4B], ...>. So for
3202 symbolic constants simply give up. */
3203 if (TREE_CODE (t) == ADDR_EXPR)
3205 constraint_expr result;
3206 result.type = SCALAR;
3207 result.var = anything_id;
3208 result.offset = 0;
3209 results->safe_push (result);
3210 return;
3213 /* Pretend to take the address of the base, we'll take care of
3214 adding the required subset of sub-fields below. */
3215 get_constraint_for_1 (t, results, true, lhs_p);
3216 gcc_assert (results->length () == 1);
3217 struct constraint_expr &result = results->last ();
3219 if (result.type == SCALAR
3220 && get_varinfo (result.var)->is_full_var)
3221 /* For single-field vars do not bother about the offset. */
3222 result.offset = 0;
3223 else if (result.type == SCALAR)
3225 /* In languages like C, you can access one past the end of an
3226 array. You aren't allowed to dereference it, so we can
3227 ignore this constraint. When we handle pointer subtraction,
3228 we may have to do something cute here. */
3230 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result.var)->fullsize
3231 && bitmaxsize != 0)
3233 /* It's also not true that the constraint will actually start at the
3234 right offset, it may start in some padding. We only care about
3235 setting the constraint to the first actual field it touches, so
3236 walk to find it. */
3237 struct constraint_expr cexpr = result;
3238 varinfo_t curr;
3239 results->pop ();
3240 cexpr.offset = 0;
3241 for (curr = get_varinfo (cexpr.var); curr; curr = vi_next (curr))
3243 if (ranges_overlap_p (curr->offset, curr->size,
3244 bitpos, bitmaxsize))
3246 cexpr.var = curr->id;
3247 results->safe_push (cexpr);
3248 if (address_p)
3249 break;
3252 /* If we are going to take the address of this field then
3253 to be able to compute reachability correctly add at least
3254 the last field of the variable. */
3255 if (address_p && results->length () == 0)
3257 curr = get_varinfo (cexpr.var);
3258 while (curr->next != 0)
3259 curr = vi_next (curr);
3260 cexpr.var = curr->id;
3261 results->safe_push (cexpr);
3263 else if (results->length () == 0)
3264 /* Assert that we found *some* field there. The user couldn't be
3265 accessing *only* padding. */
3266 /* Still the user could access one past the end of an array
3267 embedded in a struct resulting in accessing *only* padding. */
3268 /* Or accessing only padding via type-punning to a type
3269 that has a filed just in padding space. */
3271 cexpr.type = SCALAR;
3272 cexpr.var = anything_id;
3273 cexpr.offset = 0;
3274 results->safe_push (cexpr);
3277 else if (bitmaxsize == 0)
3279 if (dump_file && (dump_flags & TDF_DETAILS))
3280 fprintf (dump_file, "Access to zero-sized part of variable,"
3281 "ignoring\n");
3283 else
3284 if (dump_file && (dump_flags & TDF_DETAILS))
3285 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3287 else if (result.type == DEREF)
3289 /* If we do not know exactly where the access goes say so. Note
3290 that only for non-structure accesses we know that we access
3291 at most one subfiled of any variable. */
3292 if (bitpos == -1
3293 || bitsize != bitmaxsize
3294 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
3295 || result.offset == UNKNOWN_OFFSET)
3296 result.offset = UNKNOWN_OFFSET;
3297 else
3298 result.offset += bitpos;
3300 else if (result.type == ADDRESSOF)
3302 /* We can end up here for component references on constants like
3303 VIEW_CONVERT_EXPR <>({ 0, 1, 2, 3 })[i]. */
3304 result.type = SCALAR;
3305 result.var = anything_id;
3306 result.offset = 0;
3308 else
3309 gcc_unreachable ();
3313 /* Dereference the constraint expression CONS, and return the result.
3314 DEREF (ADDRESSOF) = SCALAR
3315 DEREF (SCALAR) = DEREF
3316 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3317 This is needed so that we can handle dereferencing DEREF constraints. */
3319 static void
3320 do_deref (vec<ce_s> *constraints)
3322 struct constraint_expr *c;
3323 unsigned int i = 0;
3325 FOR_EACH_VEC_ELT (*constraints, i, c)
3327 if (c->type == SCALAR)
3328 c->type = DEREF;
3329 else if (c->type == ADDRESSOF)
3330 c->type = SCALAR;
3331 else if (c->type == DEREF)
3333 struct constraint_expr tmplhs;
3334 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp", true);
3335 process_constraint (new_constraint (tmplhs, *c));
3336 c->var = tmplhs.var;
3338 else
3339 gcc_unreachable ();
3343 /* Given a tree T, return the constraint expression for taking the
3344 address of it. */
3346 static void
3347 get_constraint_for_address_of (tree t, vec<ce_s> *results)
3349 struct constraint_expr *c;
3350 unsigned int i;
3352 get_constraint_for_1 (t, results, true, true);
3354 FOR_EACH_VEC_ELT (*results, i, c)
3356 if (c->type == DEREF)
3357 c->type = SCALAR;
3358 else
3359 c->type = ADDRESSOF;
3363 /* Given a tree T, return the constraint expression for it. */
3365 static void
3366 get_constraint_for_1 (tree t, vec<ce_s> *results, bool address_p,
3367 bool lhs_p)
3369 struct constraint_expr temp;
3371 /* x = integer is all glommed to a single variable, which doesn't
3372 point to anything by itself. That is, of course, unless it is an
3373 integer constant being treated as a pointer, in which case, we
3374 will return that this is really the addressof anything. This
3375 happens below, since it will fall into the default case. The only
3376 case we know something about an integer treated like a pointer is
3377 when it is the NULL pointer, and then we just say it points to
3378 NULL.
3380 Do not do that if -fno-delete-null-pointer-checks though, because
3381 in that case *NULL does not fail, so it _should_ alias *anything.
3382 It is not worth adding a new option or renaming the existing one,
3383 since this case is relatively obscure. */
3384 if ((TREE_CODE (t) == INTEGER_CST
3385 && integer_zerop (t))
3386 /* The only valid CONSTRUCTORs in gimple with pointer typed
3387 elements are zero-initializer. But in IPA mode we also
3388 process global initializers, so verify at least. */
3389 || (TREE_CODE (t) == CONSTRUCTOR
3390 && CONSTRUCTOR_NELTS (t) == 0))
3392 if (flag_delete_null_pointer_checks)
3393 temp.var = nothing_id;
3394 else
3395 temp.var = nonlocal_id;
3396 temp.type = ADDRESSOF;
3397 temp.offset = 0;
3398 results->safe_push (temp);
3399 return;
3402 /* String constants are read-only, ideally we'd have a CONST_DECL
3403 for those. */
3404 if (TREE_CODE (t) == STRING_CST)
3406 temp.var = string_id;
3407 temp.type = SCALAR;
3408 temp.offset = 0;
3409 results->safe_push (temp);
3410 return;
3413 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3415 case tcc_expression:
3417 switch (TREE_CODE (t))
3419 case ADDR_EXPR:
3420 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3421 return;
3422 default:;
3424 break;
3426 case tcc_reference:
3428 switch (TREE_CODE (t))
3430 case MEM_REF:
3432 struct constraint_expr cs;
3433 varinfo_t vi, curr;
3434 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0),
3435 TREE_OPERAND (t, 1), results);
3436 do_deref (results);
3438 /* If we are not taking the address then make sure to process
3439 all subvariables we might access. */
3440 if (address_p)
3441 return;
3443 cs = results->last ();
3444 if (cs.type == DEREF
3445 && type_can_have_subvars (TREE_TYPE (t)))
3447 /* For dereferences this means we have to defer it
3448 to solving time. */
3449 results->last ().offset = UNKNOWN_OFFSET;
3450 return;
3452 if (cs.type != SCALAR)
3453 return;
3455 vi = get_varinfo (cs.var);
3456 curr = vi_next (vi);
3457 if (!vi->is_full_var
3458 && curr)
3460 unsigned HOST_WIDE_INT size;
3461 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t))))
3462 size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t)));
3463 else
3464 size = -1;
3465 for (; curr; curr = vi_next (curr))
3467 if (curr->offset - vi->offset < size)
3469 cs.var = curr->id;
3470 results->safe_push (cs);
3472 else
3473 break;
3476 return;
3478 case ARRAY_REF:
3479 case ARRAY_RANGE_REF:
3480 case COMPONENT_REF:
3481 case IMAGPART_EXPR:
3482 case REALPART_EXPR:
3483 case BIT_FIELD_REF:
3484 get_constraint_for_component_ref (t, results, address_p, lhs_p);
3485 return;
3486 case VIEW_CONVERT_EXPR:
3487 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p,
3488 lhs_p);
3489 return;
3490 /* We are missing handling for TARGET_MEM_REF here. */
3491 default:;
3493 break;
3495 case tcc_exceptional:
3497 switch (TREE_CODE (t))
3499 case SSA_NAME:
3501 get_constraint_for_ssa_var (t, results, address_p);
3502 return;
3504 case CONSTRUCTOR:
3506 unsigned int i;
3507 tree val;
3508 auto_vec<ce_s> tmp;
3509 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
3511 struct constraint_expr *rhsp;
3512 unsigned j;
3513 get_constraint_for_1 (val, &tmp, address_p, lhs_p);
3514 FOR_EACH_VEC_ELT (tmp, j, rhsp)
3515 results->safe_push (*rhsp);
3516 tmp.truncate (0);
3518 /* We do not know whether the constructor was complete,
3519 so technically we have to add &NOTHING or &ANYTHING
3520 like we do for an empty constructor as well. */
3521 return;
3523 default:;
3525 break;
3527 case tcc_declaration:
3529 get_constraint_for_ssa_var (t, results, address_p);
3530 return;
3532 case tcc_constant:
3534 /* We cannot refer to automatic variables through constants. */
3535 temp.type = ADDRESSOF;
3536 temp.var = nonlocal_id;
3537 temp.offset = 0;
3538 results->safe_push (temp);
3539 return;
3541 default:;
3544 /* The default fallback is a constraint from anything. */
3545 temp.type = ADDRESSOF;
3546 temp.var = anything_id;
3547 temp.offset = 0;
3548 results->safe_push (temp);
3551 /* Given a gimple tree T, return the constraint expression vector for it. */
3553 static void
3554 get_constraint_for (tree t, vec<ce_s> *results)
3556 gcc_assert (results->length () == 0);
3558 get_constraint_for_1 (t, results, false, true);
3561 /* Given a gimple tree T, return the constraint expression vector for it
3562 to be used as the rhs of a constraint. */
3564 static void
3565 get_constraint_for_rhs (tree t, vec<ce_s> *results)
3567 gcc_assert (results->length () == 0);
3569 get_constraint_for_1 (t, results, false, false);
3573 /* Efficiently generates constraints from all entries in *RHSC to all
3574 entries in *LHSC. */
3576 static void
3577 process_all_all_constraints (vec<ce_s> lhsc,
3578 vec<ce_s> rhsc)
3580 struct constraint_expr *lhsp, *rhsp;
3581 unsigned i, j;
3583 if (lhsc.length () <= 1 || rhsc.length () <= 1)
3585 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3586 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
3587 process_constraint (new_constraint (*lhsp, *rhsp));
3589 else
3591 struct constraint_expr tmp;
3592 tmp = new_scalar_tmp_constraint_exp ("allalltmp", true);
3593 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
3594 process_constraint (new_constraint (tmp, *rhsp));
3595 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3596 process_constraint (new_constraint (*lhsp, tmp));
3600 /* Handle aggregate copies by expanding into copies of the respective
3601 fields of the structures. */
3603 static void
3604 do_structure_copy (tree lhsop, tree rhsop)
3606 struct constraint_expr *lhsp, *rhsp;
3607 auto_vec<ce_s> lhsc;
3608 auto_vec<ce_s> rhsc;
3609 unsigned j;
3611 get_constraint_for (lhsop, &lhsc);
3612 get_constraint_for_rhs (rhsop, &rhsc);
3613 lhsp = &lhsc[0];
3614 rhsp = &rhsc[0];
3615 if (lhsp->type == DEREF
3616 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3617 || rhsp->type == DEREF)
3619 if (lhsp->type == DEREF)
3621 gcc_assert (lhsc.length () == 1);
3622 lhsp->offset = UNKNOWN_OFFSET;
3624 if (rhsp->type == DEREF)
3626 gcc_assert (rhsc.length () == 1);
3627 rhsp->offset = UNKNOWN_OFFSET;
3629 process_all_all_constraints (lhsc, rhsc);
3631 else if (lhsp->type == SCALAR
3632 && (rhsp->type == SCALAR
3633 || rhsp->type == ADDRESSOF))
3635 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3636 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
3637 bool reverse;
3638 unsigned k = 0;
3639 get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize,
3640 &reverse);
3641 get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize,
3642 &reverse);
3643 for (j = 0; lhsc.iterate (j, &lhsp);)
3645 varinfo_t lhsv, rhsv;
3646 rhsp = &rhsc[k];
3647 lhsv = get_varinfo (lhsp->var);
3648 rhsv = get_varinfo (rhsp->var);
3649 if (lhsv->may_have_pointers
3650 && (lhsv->is_full_var
3651 || rhsv->is_full_var
3652 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3653 rhsv->offset + lhsoffset, rhsv->size)))
3654 process_constraint (new_constraint (*lhsp, *rhsp));
3655 if (!rhsv->is_full_var
3656 && (lhsv->is_full_var
3657 || (lhsv->offset + rhsoffset + lhsv->size
3658 > rhsv->offset + lhsoffset + rhsv->size)))
3660 ++k;
3661 if (k >= rhsc.length ())
3662 break;
3664 else
3665 ++j;
3668 else
3669 gcc_unreachable ();
3672 /* Create constraints ID = { rhsc }. */
3674 static void
3675 make_constraints_to (unsigned id, vec<ce_s> rhsc)
3677 struct constraint_expr *c;
3678 struct constraint_expr includes;
3679 unsigned int j;
3681 includes.var = id;
3682 includes.offset = 0;
3683 includes.type = SCALAR;
3685 FOR_EACH_VEC_ELT (rhsc, j, c)
3686 process_constraint (new_constraint (includes, *c));
3689 /* Create a constraint ID = OP. */
3691 static void
3692 make_constraint_to (unsigned id, tree op)
3694 auto_vec<ce_s> rhsc;
3695 get_constraint_for_rhs (op, &rhsc);
3696 make_constraints_to (id, rhsc);
3699 /* Create a constraint ID = &FROM. */
3701 static void
3702 make_constraint_from (varinfo_t vi, int from)
3704 struct constraint_expr lhs, rhs;
3706 lhs.var = vi->id;
3707 lhs.offset = 0;
3708 lhs.type = SCALAR;
3710 rhs.var = from;
3711 rhs.offset = 0;
3712 rhs.type = ADDRESSOF;
3713 process_constraint (new_constraint (lhs, rhs));
3716 /* Create a constraint ID = FROM. */
3718 static void
3719 make_copy_constraint (varinfo_t vi, int from)
3721 struct constraint_expr lhs, rhs;
3723 lhs.var = vi->id;
3724 lhs.offset = 0;
3725 lhs.type = SCALAR;
3727 rhs.var = from;
3728 rhs.offset = 0;
3729 rhs.type = SCALAR;
3730 process_constraint (new_constraint (lhs, rhs));
3733 /* Make constraints necessary to make OP escape. */
3735 static void
3736 make_escape_constraint (tree op)
3738 make_constraint_to (escaped_id, op);
3741 /* Add constraints to that the solution of VI is transitively closed. */
3743 static void
3744 make_transitive_closure_constraints (varinfo_t vi)
3746 struct constraint_expr lhs, rhs;
3748 /* VAR = *(VAR + UNKNOWN); */
3749 lhs.type = SCALAR;
3750 lhs.var = vi->id;
3751 lhs.offset = 0;
3752 rhs.type = DEREF;
3753 rhs.var = vi->id;
3754 rhs.offset = UNKNOWN_OFFSET;
3755 process_constraint (new_constraint (lhs, rhs));
3758 /* Add constraints to that the solution of VI has all subvariables added. */
3760 static void
3761 make_any_offset_constraints (varinfo_t vi)
3763 struct constraint_expr lhs, rhs;
3765 /* VAR = VAR + UNKNOWN; */
3766 lhs.type = SCALAR;
3767 lhs.var = vi->id;
3768 lhs.offset = 0;
3769 rhs.type = SCALAR;
3770 rhs.var = vi->id;
3771 rhs.offset = UNKNOWN_OFFSET;
3772 process_constraint (new_constraint (lhs, rhs));
3775 /* Temporary storage for fake var decls. */
3776 struct obstack fake_var_decl_obstack;
3778 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3780 static tree
3781 build_fake_var_decl (tree type)
3783 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl);
3784 memset (decl, 0, sizeof (struct tree_var_decl));
3785 TREE_SET_CODE (decl, VAR_DECL);
3786 TREE_TYPE (decl) = type;
3787 DECL_UID (decl) = allocate_decl_uid ();
3788 SET_DECL_PT_UID (decl, -1);
3789 layout_decl (decl, 0);
3790 return decl;
3793 /* Create a new artificial heap variable with NAME.
3794 Return the created variable. */
3796 static varinfo_t
3797 make_heapvar (const char *name, bool add_id)
3799 varinfo_t vi;
3800 tree heapvar;
3802 heapvar = build_fake_var_decl (ptr_type_node);
3803 DECL_EXTERNAL (heapvar) = 1;
3805 vi = new_var_info (heapvar, name, add_id);
3806 vi->is_artificial_var = true;
3807 vi->is_heap_var = true;
3808 vi->is_unknown_size_var = true;
3809 vi->offset = 0;
3810 vi->fullsize = ~0;
3811 vi->size = ~0;
3812 vi->is_full_var = true;
3813 insert_vi_for_tree (heapvar, vi);
3815 return vi;
3818 /* Create a new artificial heap variable with NAME and make a
3819 constraint from it to LHS. Set flags according to a tag used
3820 for tracking restrict pointers. */
3822 static varinfo_t
3823 make_constraint_from_restrict (varinfo_t lhs, const char *name, bool add_id)
3825 varinfo_t vi = make_heapvar (name, add_id);
3826 vi->is_restrict_var = 1;
3827 vi->is_global_var = 1;
3828 vi->may_have_pointers = 1;
3829 make_constraint_from (lhs, vi->id);
3830 return vi;
3833 /* Create a new artificial heap variable with NAME and make a
3834 constraint from it to LHS. Set flags according to a tag used
3835 for tracking restrict pointers and make the artificial heap
3836 point to global memory. */
3838 static varinfo_t
3839 make_constraint_from_global_restrict (varinfo_t lhs, const char *name,
3840 bool add_id)
3842 varinfo_t vi = make_constraint_from_restrict (lhs, name, add_id);
3843 make_copy_constraint (vi, nonlocal_id);
3844 return vi;
3847 /* In IPA mode there are varinfos for different aspects of reach
3848 function designator. One for the points-to set of the return
3849 value, one for the variables that are clobbered by the function,
3850 one for its uses and one for each parameter (including a single
3851 glob for remaining variadic arguments). */
3853 enum { fi_clobbers = 1, fi_uses = 2,
3854 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 };
3856 /* Get a constraint for the requested part of a function designator FI
3857 when operating in IPA mode. */
3859 static struct constraint_expr
3860 get_function_part_constraint (varinfo_t fi, unsigned part)
3862 struct constraint_expr c;
3864 gcc_assert (in_ipa_mode);
3866 if (fi->id == anything_id)
3868 /* ??? We probably should have a ANYFN special variable. */
3869 c.var = anything_id;
3870 c.offset = 0;
3871 c.type = SCALAR;
3873 else if (TREE_CODE (fi->decl) == FUNCTION_DECL)
3875 varinfo_t ai = first_vi_for_offset (fi, part);
3876 if (ai)
3877 c.var = ai->id;
3878 else
3879 c.var = anything_id;
3880 c.offset = 0;
3881 c.type = SCALAR;
3883 else
3885 c.var = fi->id;
3886 c.offset = part;
3887 c.type = DEREF;
3890 return c;
3893 /* For non-IPA mode, generate constraints necessary for a call on the
3894 RHS. */
3896 static void
3897 handle_rhs_call (gcall *stmt, vec<ce_s> *results)
3899 struct constraint_expr rhsc;
3900 unsigned i;
3901 bool returns_uses = false;
3903 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3905 tree arg = gimple_call_arg (stmt, i);
3906 int flags = gimple_call_arg_flags (stmt, i);
3908 /* If the argument is not used we can ignore it. */
3909 if (flags & EAF_UNUSED)
3910 continue;
3912 /* As we compute ESCAPED context-insensitive we do not gain
3913 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3914 set. The argument would still get clobbered through the
3915 escape solution. */
3916 if ((flags & EAF_NOCLOBBER)
3917 && (flags & EAF_NOESCAPE))
3919 varinfo_t uses = get_call_use_vi (stmt);
3920 varinfo_t tem = new_var_info (NULL_TREE, "callarg", true);
3921 make_constraint_to (tem->id, arg);
3922 make_any_offset_constraints (tem);
3923 if (!(flags & EAF_DIRECT))
3924 make_transitive_closure_constraints (tem);
3925 make_copy_constraint (uses, tem->id);
3926 returns_uses = true;
3928 else if (flags & EAF_NOESCAPE)
3930 struct constraint_expr lhs, rhs;
3931 varinfo_t uses = get_call_use_vi (stmt);
3932 varinfo_t clobbers = get_call_clobber_vi (stmt);
3933 varinfo_t tem = new_var_info (NULL_TREE, "callarg", true);
3934 make_constraint_to (tem->id, arg);
3935 make_any_offset_constraints (tem);
3936 if (!(flags & EAF_DIRECT))
3937 make_transitive_closure_constraints (tem);
3938 make_copy_constraint (uses, tem->id);
3939 make_copy_constraint (clobbers, tem->id);
3940 /* Add *tem = nonlocal, do not add *tem = callused as
3941 EAF_NOESCAPE parameters do not escape to other parameters
3942 and all other uses appear in NONLOCAL as well. */
3943 lhs.type = DEREF;
3944 lhs.var = tem->id;
3945 lhs.offset = 0;
3946 rhs.type = SCALAR;
3947 rhs.var = nonlocal_id;
3948 rhs.offset = 0;
3949 process_constraint (new_constraint (lhs, rhs));
3950 returns_uses = true;
3952 else
3953 make_escape_constraint (arg);
3956 /* If we added to the calls uses solution make sure we account for
3957 pointers to it to be returned. */
3958 if (returns_uses)
3960 rhsc.var = get_call_use_vi (stmt)->id;
3961 rhsc.offset = UNKNOWN_OFFSET;
3962 rhsc.type = SCALAR;
3963 results->safe_push (rhsc);
3966 /* The static chain escapes as well. */
3967 if (gimple_call_chain (stmt))
3968 make_escape_constraint (gimple_call_chain (stmt));
3970 /* And if we applied NRV the address of the return slot escapes as well. */
3971 if (gimple_call_return_slot_opt_p (stmt)
3972 && gimple_call_lhs (stmt) != NULL_TREE
3973 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3975 auto_vec<ce_s> tmpc;
3976 struct constraint_expr lhsc, *c;
3977 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3978 lhsc.var = escaped_id;
3979 lhsc.offset = 0;
3980 lhsc.type = SCALAR;
3981 FOR_EACH_VEC_ELT (tmpc, i, c)
3982 process_constraint (new_constraint (lhsc, *c));
3985 /* Regular functions return nonlocal memory. */
3986 rhsc.var = nonlocal_id;
3987 rhsc.offset = 0;
3988 rhsc.type = SCALAR;
3989 results->safe_push (rhsc);
3992 /* For non-IPA mode, generate constraints necessary for a call
3993 that returns a pointer and assigns it to LHS. This simply makes
3994 the LHS point to global and escaped variables. */
3996 static void
3997 handle_lhs_call (gcall *stmt, tree lhs, int flags, vec<ce_s> rhsc,
3998 tree fndecl)
4000 auto_vec<ce_s> lhsc;
4002 get_constraint_for (lhs, &lhsc);
4003 /* If the store is to a global decl make sure to
4004 add proper escape constraints. */
4005 lhs = get_base_address (lhs);
4006 if (lhs
4007 && DECL_P (lhs)
4008 && is_global_var (lhs))
4010 struct constraint_expr tmpc;
4011 tmpc.var = escaped_id;
4012 tmpc.offset = 0;
4013 tmpc.type = SCALAR;
4014 lhsc.safe_push (tmpc);
4017 /* If the call returns an argument unmodified override the rhs
4018 constraints. */
4019 if (flags & ERF_RETURNS_ARG
4020 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
4022 tree arg;
4023 rhsc.create (0);
4024 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
4025 get_constraint_for (arg, &rhsc);
4026 process_all_all_constraints (lhsc, rhsc);
4027 rhsc.release ();
4029 else if (flags & ERF_NOALIAS)
4031 varinfo_t vi;
4032 struct constraint_expr tmpc;
4033 rhsc.create (0);
4034 vi = make_heapvar ("HEAP", true);
4035 /* We are marking allocated storage local, we deal with it becoming
4036 global by escaping and setting of vars_contains_escaped_heap. */
4037 DECL_EXTERNAL (vi->decl) = 0;
4038 vi->is_global_var = 0;
4039 /* If this is not a real malloc call assume the memory was
4040 initialized and thus may point to global memory. All
4041 builtin functions with the malloc attribute behave in a sane way. */
4042 if (!fndecl
4043 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
4044 make_constraint_from (vi, nonlocal_id);
4045 tmpc.var = vi->id;
4046 tmpc.offset = 0;
4047 tmpc.type = ADDRESSOF;
4048 rhsc.safe_push (tmpc);
4049 process_all_all_constraints (lhsc, rhsc);
4050 rhsc.release ();
4052 else
4053 process_all_all_constraints (lhsc, rhsc);
4056 /* For non-IPA mode, generate constraints necessary for a call of a
4057 const function that returns a pointer in the statement STMT. */
4059 static void
4060 handle_const_call (gcall *stmt, vec<ce_s> *results)
4062 struct constraint_expr rhsc;
4063 unsigned int k;
4064 bool need_uses = false;
4066 /* Treat nested const functions the same as pure functions as far
4067 as the static chain is concerned. */
4068 if (gimple_call_chain (stmt))
4070 varinfo_t uses = get_call_use_vi (stmt);
4071 make_constraint_to (uses->id, gimple_call_chain (stmt));
4072 need_uses = true;
4075 /* And if we applied NRV the address of the return slot escapes as well. */
4076 if (gimple_call_return_slot_opt_p (stmt)
4077 && gimple_call_lhs (stmt) != NULL_TREE
4078 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
4080 varinfo_t uses = get_call_use_vi (stmt);
4081 auto_vec<ce_s> tmpc;
4082 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
4083 make_constraints_to (uses->id, tmpc);
4084 need_uses = true;
4087 if (need_uses)
4089 varinfo_t uses = get_call_use_vi (stmt);
4090 make_any_offset_constraints (uses);
4091 make_transitive_closure_constraints (uses);
4092 rhsc.var = uses->id;
4093 rhsc.offset = 0;
4094 rhsc.type = SCALAR;
4095 results->safe_push (rhsc);
4098 /* May return offsetted arguments. */
4099 varinfo_t tem = NULL;
4100 if (gimple_call_num_args (stmt) != 0)
4101 tem = new_var_info (NULL_TREE, "callarg", true);
4102 for (k = 0; k < gimple_call_num_args (stmt); ++k)
4104 tree arg = gimple_call_arg (stmt, k);
4105 auto_vec<ce_s> argc;
4106 get_constraint_for_rhs (arg, &argc);
4107 make_constraints_to (tem->id, argc);
4109 if (tem)
4111 ce_s ce;
4112 ce.type = SCALAR;
4113 ce.var = tem->id;
4114 ce.offset = UNKNOWN_OFFSET;
4115 results->safe_push (ce);
4118 /* May return addresses of globals. */
4119 rhsc.var = nonlocal_id;
4120 rhsc.offset = 0;
4121 rhsc.type = ADDRESSOF;
4122 results->safe_push (rhsc);
4125 /* For non-IPA mode, generate constraints necessary for a call to a
4126 pure function in statement STMT. */
4128 static void
4129 handle_pure_call (gcall *stmt, vec<ce_s> *results)
4131 struct constraint_expr rhsc;
4132 unsigned i;
4133 varinfo_t uses = NULL;
4135 /* Memory reached from pointer arguments is call-used. */
4136 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4138 tree arg = gimple_call_arg (stmt, i);
4139 if (!uses)
4141 uses = get_call_use_vi (stmt);
4142 make_any_offset_constraints (uses);
4143 make_transitive_closure_constraints (uses);
4145 make_constraint_to (uses->id, arg);
4148 /* The static chain is used as well. */
4149 if (gimple_call_chain (stmt))
4151 if (!uses)
4153 uses = get_call_use_vi (stmt);
4154 make_any_offset_constraints (uses);
4155 make_transitive_closure_constraints (uses);
4157 make_constraint_to (uses->id, gimple_call_chain (stmt));
4160 /* And if we applied NRV the address of the return slot. */
4161 if (gimple_call_return_slot_opt_p (stmt)
4162 && gimple_call_lhs (stmt) != NULL_TREE
4163 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
4165 if (!uses)
4167 uses = get_call_use_vi (stmt);
4168 make_any_offset_constraints (uses);
4169 make_transitive_closure_constraints (uses);
4171 auto_vec<ce_s> tmpc;
4172 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
4173 make_constraints_to (uses->id, tmpc);
4176 /* Pure functions may return call-used and nonlocal memory. */
4177 if (uses)
4179 rhsc.var = uses->id;
4180 rhsc.offset = 0;
4181 rhsc.type = SCALAR;
4182 results->safe_push (rhsc);
4184 rhsc.var = nonlocal_id;
4185 rhsc.offset = 0;
4186 rhsc.type = SCALAR;
4187 results->safe_push (rhsc);
4191 /* Return the varinfo for the callee of CALL. */
4193 static varinfo_t
4194 get_fi_for_callee (gcall *call)
4196 tree decl, fn = gimple_call_fn (call);
4198 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
4199 fn = OBJ_TYPE_REF_EXPR (fn);
4201 /* If we can directly resolve the function being called, do so.
4202 Otherwise, it must be some sort of indirect expression that
4203 we should still be able to handle. */
4204 decl = gimple_call_addr_fndecl (fn);
4205 if (decl)
4206 return get_vi_for_tree (decl);
4208 /* If the function is anything other than a SSA name pointer we have no
4209 clue and should be getting ANYFN (well, ANYTHING for now). */
4210 if (!fn || TREE_CODE (fn) != SSA_NAME)
4211 return get_varinfo (anything_id);
4213 if (SSA_NAME_IS_DEFAULT_DEF (fn)
4214 && (TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
4215 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL))
4216 fn = SSA_NAME_VAR (fn);
4218 return get_vi_for_tree (fn);
4221 /* Create constraints for assigning call argument ARG to the incoming parameter
4222 INDEX of function FI. */
4224 static void
4225 find_func_aliases_for_call_arg (varinfo_t fi, unsigned index, tree arg)
4227 struct constraint_expr lhs;
4228 lhs = get_function_part_constraint (fi, fi_parm_base + index);
4230 auto_vec<ce_s, 2> rhsc;
4231 get_constraint_for_rhs (arg, &rhsc);
4233 unsigned j;
4234 struct constraint_expr *rhsp;
4235 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4236 process_constraint (new_constraint (lhs, *rhsp));
4239 /* Return true if FNDECL may be part of another lto partition. */
4241 static bool
4242 fndecl_maybe_in_other_partition (tree fndecl)
4244 cgraph_node *fn_node = cgraph_node::get (fndecl);
4245 if (fn_node == NULL)
4246 return true;
4248 return fn_node->in_other_partition;
4251 /* Create constraints for the builtin call T. Return true if the call
4252 was handled, otherwise false. */
4254 static bool
4255 find_func_aliases_for_builtin_call (struct function *fn, gcall *t)
4257 tree fndecl = gimple_call_fndecl (t);
4258 auto_vec<ce_s, 2> lhsc;
4259 auto_vec<ce_s, 4> rhsc;
4260 varinfo_t fi;
4262 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4263 /* ??? All builtins that are handled here need to be handled
4264 in the alias-oracle query functions explicitly! */
4265 switch (DECL_FUNCTION_CODE (fndecl))
4267 /* All the following functions return a pointer to the same object
4268 as their first argument points to. The functions do not add
4269 to the ESCAPED solution. The functions make the first argument
4270 pointed to memory point to what the second argument pointed to
4271 memory points to. */
4272 case BUILT_IN_STRCPY:
4273 case BUILT_IN_STRNCPY:
4274 case BUILT_IN_BCOPY:
4275 case BUILT_IN_MEMCPY:
4276 case BUILT_IN_MEMMOVE:
4277 case BUILT_IN_MEMPCPY:
4278 case BUILT_IN_STPCPY:
4279 case BUILT_IN_STPNCPY:
4280 case BUILT_IN_STRCAT:
4281 case BUILT_IN_STRNCAT:
4282 case BUILT_IN_STRCPY_CHK:
4283 case BUILT_IN_STRNCPY_CHK:
4284 case BUILT_IN_MEMCPY_CHK:
4285 case BUILT_IN_MEMMOVE_CHK:
4286 case BUILT_IN_MEMPCPY_CHK:
4287 case BUILT_IN_STPCPY_CHK:
4288 case BUILT_IN_STPNCPY_CHK:
4289 case BUILT_IN_STRCAT_CHK:
4290 case BUILT_IN_STRNCAT_CHK:
4291 case BUILT_IN_TM_MEMCPY:
4292 case BUILT_IN_TM_MEMMOVE:
4294 tree res = gimple_call_lhs (t);
4295 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4296 == BUILT_IN_BCOPY ? 1 : 0));
4297 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4298 == BUILT_IN_BCOPY ? 0 : 1));
4299 if (res != NULL_TREE)
4301 get_constraint_for (res, &lhsc);
4302 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4303 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
4304 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4305 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
4306 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK
4307 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK)
4308 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4309 else
4310 get_constraint_for (dest, &rhsc);
4311 process_all_all_constraints (lhsc, rhsc);
4312 lhsc.truncate (0);
4313 rhsc.truncate (0);
4315 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4316 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4317 do_deref (&lhsc);
4318 do_deref (&rhsc);
4319 process_all_all_constraints (lhsc, rhsc);
4320 return true;
4322 case BUILT_IN_MEMSET:
4323 case BUILT_IN_MEMSET_CHK:
4324 case BUILT_IN_TM_MEMSET:
4326 tree res = gimple_call_lhs (t);
4327 tree dest = gimple_call_arg (t, 0);
4328 unsigned i;
4329 ce_s *lhsp;
4330 struct constraint_expr ac;
4331 if (res != NULL_TREE)
4333 get_constraint_for (res, &lhsc);
4334 get_constraint_for (dest, &rhsc);
4335 process_all_all_constraints (lhsc, rhsc);
4336 lhsc.truncate (0);
4338 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4339 do_deref (&lhsc);
4340 if (flag_delete_null_pointer_checks
4341 && integer_zerop (gimple_call_arg (t, 1)))
4343 ac.type = ADDRESSOF;
4344 ac.var = nothing_id;
4346 else
4348 ac.type = SCALAR;
4349 ac.var = integer_id;
4351 ac.offset = 0;
4352 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4353 process_constraint (new_constraint (*lhsp, ac));
4354 return true;
4356 case BUILT_IN_POSIX_MEMALIGN:
4358 tree ptrptr = gimple_call_arg (t, 0);
4359 get_constraint_for (ptrptr, &lhsc);
4360 do_deref (&lhsc);
4361 varinfo_t vi = make_heapvar ("HEAP", true);
4362 /* We are marking allocated storage local, we deal with it becoming
4363 global by escaping and setting of vars_contains_escaped_heap. */
4364 DECL_EXTERNAL (vi->decl) = 0;
4365 vi->is_global_var = 0;
4366 struct constraint_expr tmpc;
4367 tmpc.var = vi->id;
4368 tmpc.offset = 0;
4369 tmpc.type = ADDRESSOF;
4370 rhsc.safe_push (tmpc);
4371 process_all_all_constraints (lhsc, rhsc);
4372 return true;
4374 case BUILT_IN_ASSUME_ALIGNED:
4376 tree res = gimple_call_lhs (t);
4377 tree dest = gimple_call_arg (t, 0);
4378 if (res != NULL_TREE)
4380 get_constraint_for (res, &lhsc);
4381 get_constraint_for (dest, &rhsc);
4382 process_all_all_constraints (lhsc, rhsc);
4384 return true;
4386 /* All the following functions do not return pointers, do not
4387 modify the points-to sets of memory reachable from their
4388 arguments and do not add to the ESCAPED solution. */
4389 case BUILT_IN_SINCOS:
4390 case BUILT_IN_SINCOSF:
4391 case BUILT_IN_SINCOSL:
4392 case BUILT_IN_FREXP:
4393 case BUILT_IN_FREXPF:
4394 case BUILT_IN_FREXPL:
4395 case BUILT_IN_GAMMA_R:
4396 case BUILT_IN_GAMMAF_R:
4397 case BUILT_IN_GAMMAL_R:
4398 case BUILT_IN_LGAMMA_R:
4399 case BUILT_IN_LGAMMAF_R:
4400 case BUILT_IN_LGAMMAL_R:
4401 case BUILT_IN_MODF:
4402 case BUILT_IN_MODFF:
4403 case BUILT_IN_MODFL:
4404 case BUILT_IN_REMQUO:
4405 case BUILT_IN_REMQUOF:
4406 case BUILT_IN_REMQUOL:
4407 case BUILT_IN_FREE:
4408 return true;
4409 case BUILT_IN_STRDUP:
4410 case BUILT_IN_STRNDUP:
4411 case BUILT_IN_REALLOC:
4412 if (gimple_call_lhs (t))
4414 handle_lhs_call (t, gimple_call_lhs (t),
4415 gimple_call_return_flags (t) | ERF_NOALIAS,
4416 vNULL, fndecl);
4417 get_constraint_for_ptr_offset (gimple_call_lhs (t),
4418 NULL_TREE, &lhsc);
4419 get_constraint_for_ptr_offset (gimple_call_arg (t, 0),
4420 NULL_TREE, &rhsc);
4421 do_deref (&lhsc);
4422 do_deref (&rhsc);
4423 process_all_all_constraints (lhsc, rhsc);
4424 lhsc.truncate (0);
4425 rhsc.truncate (0);
4426 /* For realloc the resulting pointer can be equal to the
4427 argument as well. But only doing this wouldn't be
4428 correct because with ptr == 0 realloc behaves like malloc. */
4429 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_REALLOC)
4431 get_constraint_for (gimple_call_lhs (t), &lhsc);
4432 get_constraint_for (gimple_call_arg (t, 0), &rhsc);
4433 process_all_all_constraints (lhsc, rhsc);
4435 return true;
4437 break;
4438 /* String / character search functions return a pointer into the
4439 source string or NULL. */
4440 case BUILT_IN_INDEX:
4441 case BUILT_IN_STRCHR:
4442 case BUILT_IN_STRRCHR:
4443 case BUILT_IN_MEMCHR:
4444 case BUILT_IN_STRSTR:
4445 case BUILT_IN_STRPBRK:
4446 if (gimple_call_lhs (t))
4448 tree src = gimple_call_arg (t, 0);
4449 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4450 constraint_expr nul;
4451 nul.var = nothing_id;
4452 nul.offset = 0;
4453 nul.type = ADDRESSOF;
4454 rhsc.safe_push (nul);
4455 get_constraint_for (gimple_call_lhs (t), &lhsc);
4456 process_all_all_constraints (lhsc, rhsc);
4458 return true;
4459 /* Trampolines are special - they set up passing the static
4460 frame. */
4461 case BUILT_IN_INIT_TRAMPOLINE:
4463 tree tramp = gimple_call_arg (t, 0);
4464 tree nfunc = gimple_call_arg (t, 1);
4465 tree frame = gimple_call_arg (t, 2);
4466 unsigned i;
4467 struct constraint_expr lhs, *rhsp;
4468 if (in_ipa_mode)
4470 varinfo_t nfi = NULL;
4471 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR);
4472 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0));
4473 if (nfi)
4475 lhs = get_function_part_constraint (nfi, fi_static_chain);
4476 get_constraint_for (frame, &rhsc);
4477 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4478 process_constraint (new_constraint (lhs, *rhsp));
4479 rhsc.truncate (0);
4481 /* Make the frame point to the function for
4482 the trampoline adjustment call. */
4483 get_constraint_for (tramp, &lhsc);
4484 do_deref (&lhsc);
4485 get_constraint_for (nfunc, &rhsc);
4486 process_all_all_constraints (lhsc, rhsc);
4488 return true;
4491 /* Else fallthru to generic handling which will let
4492 the frame escape. */
4493 break;
4495 case BUILT_IN_ADJUST_TRAMPOLINE:
4497 tree tramp = gimple_call_arg (t, 0);
4498 tree res = gimple_call_lhs (t);
4499 if (in_ipa_mode && res)
4501 get_constraint_for (res, &lhsc);
4502 get_constraint_for (tramp, &rhsc);
4503 do_deref (&rhsc);
4504 process_all_all_constraints (lhsc, rhsc);
4506 return true;
4508 CASE_BUILT_IN_TM_STORE (1):
4509 CASE_BUILT_IN_TM_STORE (2):
4510 CASE_BUILT_IN_TM_STORE (4):
4511 CASE_BUILT_IN_TM_STORE (8):
4512 CASE_BUILT_IN_TM_STORE (FLOAT):
4513 CASE_BUILT_IN_TM_STORE (DOUBLE):
4514 CASE_BUILT_IN_TM_STORE (LDOUBLE):
4515 CASE_BUILT_IN_TM_STORE (M64):
4516 CASE_BUILT_IN_TM_STORE (M128):
4517 CASE_BUILT_IN_TM_STORE (M256):
4519 tree addr = gimple_call_arg (t, 0);
4520 tree src = gimple_call_arg (t, 1);
4522 get_constraint_for (addr, &lhsc);
4523 do_deref (&lhsc);
4524 get_constraint_for (src, &rhsc);
4525 process_all_all_constraints (lhsc, rhsc);
4526 return true;
4528 CASE_BUILT_IN_TM_LOAD (1):
4529 CASE_BUILT_IN_TM_LOAD (2):
4530 CASE_BUILT_IN_TM_LOAD (4):
4531 CASE_BUILT_IN_TM_LOAD (8):
4532 CASE_BUILT_IN_TM_LOAD (FLOAT):
4533 CASE_BUILT_IN_TM_LOAD (DOUBLE):
4534 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
4535 CASE_BUILT_IN_TM_LOAD (M64):
4536 CASE_BUILT_IN_TM_LOAD (M128):
4537 CASE_BUILT_IN_TM_LOAD (M256):
4539 tree dest = gimple_call_lhs (t);
4540 tree addr = gimple_call_arg (t, 0);
4542 get_constraint_for (dest, &lhsc);
4543 get_constraint_for (addr, &rhsc);
4544 do_deref (&rhsc);
4545 process_all_all_constraints (lhsc, rhsc);
4546 return true;
4548 /* Variadic argument handling needs to be handled in IPA
4549 mode as well. */
4550 case BUILT_IN_VA_START:
4552 tree valist = gimple_call_arg (t, 0);
4553 struct constraint_expr rhs, *lhsp;
4554 unsigned i;
4555 get_constraint_for_ptr_offset (valist, NULL_TREE, &lhsc);
4556 do_deref (&lhsc);
4557 /* The va_list gets access to pointers in variadic
4558 arguments. Which we know in the case of IPA analysis
4559 and otherwise are just all nonlocal variables. */
4560 if (in_ipa_mode)
4562 fi = lookup_vi_for_tree (fn->decl);
4563 rhs = get_function_part_constraint (fi, ~0);
4564 rhs.type = ADDRESSOF;
4566 else
4568 rhs.var = nonlocal_id;
4569 rhs.type = ADDRESSOF;
4570 rhs.offset = 0;
4572 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4573 process_constraint (new_constraint (*lhsp, rhs));
4574 /* va_list is clobbered. */
4575 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4576 return true;
4578 /* va_end doesn't have any effect that matters. */
4579 case BUILT_IN_VA_END:
4580 return true;
4581 /* Alternate return. Simply give up for now. */
4582 case BUILT_IN_RETURN:
4584 fi = NULL;
4585 if (!in_ipa_mode
4586 || !(fi = get_vi_for_tree (fn->decl)))
4587 make_constraint_from (get_varinfo (escaped_id), anything_id);
4588 else if (in_ipa_mode
4589 && fi != NULL)
4591 struct constraint_expr lhs, rhs;
4592 lhs = get_function_part_constraint (fi, fi_result);
4593 rhs.var = anything_id;
4594 rhs.offset = 0;
4595 rhs.type = SCALAR;
4596 process_constraint (new_constraint (lhs, rhs));
4598 return true;
4600 case BUILT_IN_GOMP_PARALLEL:
4601 case BUILT_IN_GOACC_PARALLEL:
4603 if (in_ipa_mode)
4605 unsigned int fnpos, argpos;
4606 switch (DECL_FUNCTION_CODE (fndecl))
4608 case BUILT_IN_GOMP_PARALLEL:
4609 /* __builtin_GOMP_parallel (fn, data, num_threads, flags). */
4610 fnpos = 0;
4611 argpos = 1;
4612 break;
4613 case BUILT_IN_GOACC_PARALLEL:
4614 /* __builtin_GOACC_parallel (device, fn, mapnum, hostaddrs,
4615 sizes, kinds, ...). */
4616 fnpos = 1;
4617 argpos = 3;
4618 break;
4619 default:
4620 gcc_unreachable ();
4623 tree fnarg = gimple_call_arg (t, fnpos);
4624 gcc_assert (TREE_CODE (fnarg) == ADDR_EXPR);
4625 tree fndecl = TREE_OPERAND (fnarg, 0);
4626 if (fndecl_maybe_in_other_partition (fndecl))
4627 /* Fallthru to general call handling. */
4628 break;
4630 tree arg = gimple_call_arg (t, argpos);
4632 varinfo_t fi = get_vi_for_tree (fndecl);
4633 find_func_aliases_for_call_arg (fi, 0, arg);
4634 return true;
4636 /* Else fallthru to generic call handling. */
4637 break;
4639 /* printf-style functions may have hooks to set pointers to
4640 point to somewhere into the generated string. Leave them
4641 for a later exercise... */
4642 default:
4643 /* Fallthru to general call handling. */;
4646 return false;
4649 /* Create constraints for the call T. */
4651 static void
4652 find_func_aliases_for_call (struct function *fn, gcall *t)
4654 tree fndecl = gimple_call_fndecl (t);
4655 varinfo_t fi;
4657 if (fndecl != NULL_TREE
4658 && DECL_BUILT_IN (fndecl)
4659 && find_func_aliases_for_builtin_call (fn, t))
4660 return;
4662 fi = get_fi_for_callee (t);
4663 if (!in_ipa_mode
4664 || (fndecl && !fi->is_fn_info))
4666 auto_vec<ce_s, 16> rhsc;
4667 int flags = gimple_call_flags (t);
4669 /* Const functions can return their arguments and addresses
4670 of global memory but not of escaped memory. */
4671 if (flags & (ECF_CONST|ECF_NOVOPS))
4673 if (gimple_call_lhs (t))
4674 handle_const_call (t, &rhsc);
4676 /* Pure functions can return addresses in and of memory
4677 reachable from their arguments, but they are not an escape
4678 point for reachable memory of their arguments. */
4679 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4680 handle_pure_call (t, &rhsc);
4681 else
4682 handle_rhs_call (t, &rhsc);
4683 if (gimple_call_lhs (t))
4684 handle_lhs_call (t, gimple_call_lhs (t),
4685 gimple_call_return_flags (t), rhsc, fndecl);
4687 else
4689 auto_vec<ce_s, 2> rhsc;
4690 tree lhsop;
4691 unsigned j;
4693 /* Assign all the passed arguments to the appropriate incoming
4694 parameters of the function. */
4695 for (j = 0; j < gimple_call_num_args (t); j++)
4697 tree arg = gimple_call_arg (t, j);
4698 find_func_aliases_for_call_arg (fi, j, arg);
4701 /* If we are returning a value, assign it to the result. */
4702 lhsop = gimple_call_lhs (t);
4703 if (lhsop)
4705 auto_vec<ce_s, 2> lhsc;
4706 struct constraint_expr rhs;
4707 struct constraint_expr *lhsp;
4708 bool aggr_p = aggregate_value_p (lhsop, gimple_call_fntype (t));
4710 get_constraint_for (lhsop, &lhsc);
4711 rhs = get_function_part_constraint (fi, fi_result);
4712 if (aggr_p)
4714 auto_vec<ce_s, 2> tem;
4715 tem.quick_push (rhs);
4716 do_deref (&tem);
4717 gcc_checking_assert (tem.length () == 1);
4718 rhs = tem[0];
4720 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4721 process_constraint (new_constraint (*lhsp, rhs));
4723 /* If we pass the result decl by reference, honor that. */
4724 if (aggr_p)
4726 struct constraint_expr lhs;
4727 struct constraint_expr *rhsp;
4729 get_constraint_for_address_of (lhsop, &rhsc);
4730 lhs = get_function_part_constraint (fi, fi_result);
4731 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4732 process_constraint (new_constraint (lhs, *rhsp));
4733 rhsc.truncate (0);
4737 /* If we use a static chain, pass it along. */
4738 if (gimple_call_chain (t))
4740 struct constraint_expr lhs;
4741 struct constraint_expr *rhsp;
4743 get_constraint_for (gimple_call_chain (t), &rhsc);
4744 lhs = get_function_part_constraint (fi, fi_static_chain);
4745 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4746 process_constraint (new_constraint (lhs, *rhsp));
4751 /* Walk statement T setting up aliasing constraints according to the
4752 references found in T. This function is the main part of the
4753 constraint builder. AI points to auxiliary alias information used
4754 when building alias sets and computing alias grouping heuristics. */
4756 static void
4757 find_func_aliases (struct function *fn, gimple *origt)
4759 gimple *t = origt;
4760 auto_vec<ce_s, 16> lhsc;
4761 auto_vec<ce_s, 16> rhsc;
4762 struct constraint_expr *c;
4763 varinfo_t fi;
4765 /* Now build constraints expressions. */
4766 if (gimple_code (t) == GIMPLE_PHI)
4768 size_t i;
4769 unsigned int j;
4771 /* For a phi node, assign all the arguments to
4772 the result. */
4773 get_constraint_for (gimple_phi_result (t), &lhsc);
4774 for (i = 0; i < gimple_phi_num_args (t); i++)
4776 tree strippedrhs = PHI_ARG_DEF (t, i);
4778 STRIP_NOPS (strippedrhs);
4779 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
4781 FOR_EACH_VEC_ELT (lhsc, j, c)
4783 struct constraint_expr *c2;
4784 while (rhsc.length () > 0)
4786 c2 = &rhsc.last ();
4787 process_constraint (new_constraint (*c, *c2));
4788 rhsc.pop ();
4793 /* In IPA mode, we need to generate constraints to pass call
4794 arguments through their calls. There are two cases,
4795 either a GIMPLE_CALL returning a value, or just a plain
4796 GIMPLE_CALL when we are not.
4798 In non-ipa mode, we need to generate constraints for each
4799 pointer passed by address. */
4800 else if (is_gimple_call (t))
4801 find_func_aliases_for_call (fn, as_a <gcall *> (t));
4803 /* Otherwise, just a regular assignment statement. Only care about
4804 operations with pointer result, others are dealt with as escape
4805 points if they have pointer operands. */
4806 else if (is_gimple_assign (t))
4808 /* Otherwise, just a regular assignment statement. */
4809 tree lhsop = gimple_assign_lhs (t);
4810 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
4812 if (rhsop && TREE_CLOBBER_P (rhsop))
4813 /* Ignore clobbers, they don't actually store anything into
4814 the LHS. */
4816 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
4817 do_structure_copy (lhsop, rhsop);
4818 else
4820 enum tree_code code = gimple_assign_rhs_code (t);
4822 get_constraint_for (lhsop, &lhsc);
4824 if (code == POINTER_PLUS_EXPR)
4825 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4826 gimple_assign_rhs2 (t), &rhsc);
4827 else if (code == BIT_AND_EXPR
4828 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
4830 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4831 the pointer. Handle it by offsetting it by UNKNOWN. */
4832 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4833 NULL_TREE, &rhsc);
4835 else if ((CONVERT_EXPR_CODE_P (code)
4836 && !(POINTER_TYPE_P (gimple_expr_type (t))
4837 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
4838 || gimple_assign_single_p (t))
4839 get_constraint_for_rhs (rhsop, &rhsc);
4840 else if (code == COND_EXPR)
4842 /* The result is a merge of both COND_EXPR arms. */
4843 auto_vec<ce_s, 2> tmp;
4844 struct constraint_expr *rhsp;
4845 unsigned i;
4846 get_constraint_for_rhs (gimple_assign_rhs2 (t), &rhsc);
4847 get_constraint_for_rhs (gimple_assign_rhs3 (t), &tmp);
4848 FOR_EACH_VEC_ELT (tmp, i, rhsp)
4849 rhsc.safe_push (*rhsp);
4851 else if (truth_value_p (code))
4852 /* Truth value results are not pointer (parts). Or at least
4853 very unreasonable obfuscation of a part. */
4855 else
4857 /* All other operations are merges. */
4858 auto_vec<ce_s, 4> tmp;
4859 struct constraint_expr *rhsp;
4860 unsigned i, j;
4861 get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc);
4862 for (i = 2; i < gimple_num_ops (t); ++i)
4864 get_constraint_for_rhs (gimple_op (t, i), &tmp);
4865 FOR_EACH_VEC_ELT (tmp, j, rhsp)
4866 rhsc.safe_push (*rhsp);
4867 tmp.truncate (0);
4870 process_all_all_constraints (lhsc, rhsc);
4872 /* If there is a store to a global variable the rhs escapes. */
4873 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
4874 && DECL_P (lhsop))
4876 varinfo_t vi = get_vi_for_tree (lhsop);
4877 if ((! in_ipa_mode && vi->is_global_var)
4878 || vi->is_ipa_escape_point)
4879 make_escape_constraint (rhsop);
4882 /* Handle escapes through return. */
4883 else if (gimple_code (t) == GIMPLE_RETURN
4884 && gimple_return_retval (as_a <greturn *> (t)) != NULL_TREE)
4886 greturn *return_stmt = as_a <greturn *> (t);
4887 fi = NULL;
4888 if (!in_ipa_mode
4889 || !(fi = get_vi_for_tree (fn->decl)))
4890 make_escape_constraint (gimple_return_retval (return_stmt));
4891 else if (in_ipa_mode)
4893 struct constraint_expr lhs ;
4894 struct constraint_expr *rhsp;
4895 unsigned i;
4897 lhs = get_function_part_constraint (fi, fi_result);
4898 get_constraint_for_rhs (gimple_return_retval (return_stmt), &rhsc);
4899 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4900 process_constraint (new_constraint (lhs, *rhsp));
4903 /* Handle asms conservatively by adding escape constraints to everything. */
4904 else if (gasm *asm_stmt = dyn_cast <gasm *> (t))
4906 unsigned i, noutputs;
4907 const char **oconstraints;
4908 const char *constraint;
4909 bool allows_mem, allows_reg, is_inout;
4911 noutputs = gimple_asm_noutputs (asm_stmt);
4912 oconstraints = XALLOCAVEC (const char *, noutputs);
4914 for (i = 0; i < noutputs; ++i)
4916 tree link = gimple_asm_output_op (asm_stmt, i);
4917 tree op = TREE_VALUE (link);
4919 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4920 oconstraints[i] = constraint;
4921 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4922 &allows_reg, &is_inout);
4924 /* A memory constraint makes the address of the operand escape. */
4925 if (!allows_reg && allows_mem)
4926 make_escape_constraint (build_fold_addr_expr (op));
4928 /* The asm may read global memory, so outputs may point to
4929 any global memory. */
4930 if (op)
4932 auto_vec<ce_s, 2> lhsc;
4933 struct constraint_expr rhsc, *lhsp;
4934 unsigned j;
4935 get_constraint_for (op, &lhsc);
4936 rhsc.var = nonlocal_id;
4937 rhsc.offset = 0;
4938 rhsc.type = SCALAR;
4939 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4940 process_constraint (new_constraint (*lhsp, rhsc));
4943 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
4945 tree link = gimple_asm_input_op (asm_stmt, i);
4946 tree op = TREE_VALUE (link);
4948 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4950 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4951 &allows_mem, &allows_reg);
4953 /* A memory constraint makes the address of the operand escape. */
4954 if (!allows_reg && allows_mem)
4955 make_escape_constraint (build_fold_addr_expr (op));
4956 /* Strictly we'd only need the constraint to ESCAPED if
4957 the asm clobbers memory, otherwise using something
4958 along the lines of per-call clobbers/uses would be enough. */
4959 else if (op)
4960 make_escape_constraint (op);
4966 /* Create a constraint adding to the clobber set of FI the memory
4967 pointed to by PTR. */
4969 static void
4970 process_ipa_clobber (varinfo_t fi, tree ptr)
4972 vec<ce_s> ptrc = vNULL;
4973 struct constraint_expr *c, lhs;
4974 unsigned i;
4975 get_constraint_for_rhs (ptr, &ptrc);
4976 lhs = get_function_part_constraint (fi, fi_clobbers);
4977 FOR_EACH_VEC_ELT (ptrc, i, c)
4978 process_constraint (new_constraint (lhs, *c));
4979 ptrc.release ();
4982 /* Walk statement T setting up clobber and use constraints according to the
4983 references found in T. This function is a main part of the
4984 IPA constraint builder. */
4986 static void
4987 find_func_clobbers (struct function *fn, gimple *origt)
4989 gimple *t = origt;
4990 auto_vec<ce_s, 16> lhsc;
4991 auto_vec<ce_s, 16> rhsc;
4992 varinfo_t fi;
4994 /* Add constraints for clobbered/used in IPA mode.
4995 We are not interested in what automatic variables are clobbered
4996 or used as we only use the information in the caller to which
4997 they do not escape. */
4998 gcc_assert (in_ipa_mode);
5000 /* If the stmt refers to memory in any way it better had a VUSE. */
5001 if (gimple_vuse (t) == NULL_TREE)
5002 return;
5004 /* We'd better have function information for the current function. */
5005 fi = lookup_vi_for_tree (fn->decl);
5006 gcc_assert (fi != NULL);
5008 /* Account for stores in assignments and calls. */
5009 if (gimple_vdef (t) != NULL_TREE
5010 && gimple_has_lhs (t))
5012 tree lhs = gimple_get_lhs (t);
5013 tree tem = lhs;
5014 while (handled_component_p (tem))
5015 tem = TREE_OPERAND (tem, 0);
5016 if ((DECL_P (tem)
5017 && !auto_var_in_fn_p (tem, fn->decl))
5018 || INDIRECT_REF_P (tem)
5019 || (TREE_CODE (tem) == MEM_REF
5020 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
5021 && auto_var_in_fn_p
5022 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl))))
5024 struct constraint_expr lhsc, *rhsp;
5025 unsigned i;
5026 lhsc = get_function_part_constraint (fi, fi_clobbers);
5027 get_constraint_for_address_of (lhs, &rhsc);
5028 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5029 process_constraint (new_constraint (lhsc, *rhsp));
5030 rhsc.truncate (0);
5034 /* Account for uses in assigments and returns. */
5035 if (gimple_assign_single_p (t)
5036 || (gimple_code (t) == GIMPLE_RETURN
5037 && gimple_return_retval (as_a <greturn *> (t)) != NULL_TREE))
5039 tree rhs = (gimple_assign_single_p (t)
5040 ? gimple_assign_rhs1 (t)
5041 : gimple_return_retval (as_a <greturn *> (t)));
5042 tree tem = rhs;
5043 while (handled_component_p (tem))
5044 tem = TREE_OPERAND (tem, 0);
5045 if ((DECL_P (tem)
5046 && !auto_var_in_fn_p (tem, fn->decl))
5047 || INDIRECT_REF_P (tem)
5048 || (TREE_CODE (tem) == MEM_REF
5049 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
5050 && auto_var_in_fn_p
5051 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl))))
5053 struct constraint_expr lhs, *rhsp;
5054 unsigned i;
5055 lhs = get_function_part_constraint (fi, fi_uses);
5056 get_constraint_for_address_of (rhs, &rhsc);
5057 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5058 process_constraint (new_constraint (lhs, *rhsp));
5059 rhsc.truncate (0);
5063 if (gcall *call_stmt = dyn_cast <gcall *> (t))
5065 varinfo_t cfi = NULL;
5066 tree decl = gimple_call_fndecl (t);
5067 struct constraint_expr lhs, rhs;
5068 unsigned i, j;
5070 /* For builtins we do not have separate function info. For those
5071 we do not generate escapes for we have to generate clobbers/uses. */
5072 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
5073 switch (DECL_FUNCTION_CODE (decl))
5075 /* The following functions use and clobber memory pointed to
5076 by their arguments. */
5077 case BUILT_IN_STRCPY:
5078 case BUILT_IN_STRNCPY:
5079 case BUILT_IN_BCOPY:
5080 case BUILT_IN_MEMCPY:
5081 case BUILT_IN_MEMMOVE:
5082 case BUILT_IN_MEMPCPY:
5083 case BUILT_IN_STPCPY:
5084 case BUILT_IN_STPNCPY:
5085 case BUILT_IN_STRCAT:
5086 case BUILT_IN_STRNCAT:
5087 case BUILT_IN_STRCPY_CHK:
5088 case BUILT_IN_STRNCPY_CHK:
5089 case BUILT_IN_MEMCPY_CHK:
5090 case BUILT_IN_MEMMOVE_CHK:
5091 case BUILT_IN_MEMPCPY_CHK:
5092 case BUILT_IN_STPCPY_CHK:
5093 case BUILT_IN_STPNCPY_CHK:
5094 case BUILT_IN_STRCAT_CHK:
5095 case BUILT_IN_STRNCAT_CHK:
5097 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
5098 == BUILT_IN_BCOPY ? 1 : 0));
5099 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
5100 == BUILT_IN_BCOPY ? 0 : 1));
5101 unsigned i;
5102 struct constraint_expr *rhsp, *lhsp;
5103 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
5104 lhs = get_function_part_constraint (fi, fi_clobbers);
5105 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
5106 process_constraint (new_constraint (lhs, *lhsp));
5107 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
5108 lhs = get_function_part_constraint (fi, fi_uses);
5109 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5110 process_constraint (new_constraint (lhs, *rhsp));
5111 return;
5113 /* The following function clobbers memory pointed to by
5114 its argument. */
5115 case BUILT_IN_MEMSET:
5116 case BUILT_IN_MEMSET_CHK:
5117 case BUILT_IN_POSIX_MEMALIGN:
5119 tree dest = gimple_call_arg (t, 0);
5120 unsigned i;
5121 ce_s *lhsp;
5122 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
5123 lhs = get_function_part_constraint (fi, fi_clobbers);
5124 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
5125 process_constraint (new_constraint (lhs, *lhsp));
5126 return;
5128 /* The following functions clobber their second and third
5129 arguments. */
5130 case BUILT_IN_SINCOS:
5131 case BUILT_IN_SINCOSF:
5132 case BUILT_IN_SINCOSL:
5134 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5135 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5136 return;
5138 /* The following functions clobber their second argument. */
5139 case BUILT_IN_FREXP:
5140 case BUILT_IN_FREXPF:
5141 case BUILT_IN_FREXPL:
5142 case BUILT_IN_LGAMMA_R:
5143 case BUILT_IN_LGAMMAF_R:
5144 case BUILT_IN_LGAMMAL_R:
5145 case BUILT_IN_GAMMA_R:
5146 case BUILT_IN_GAMMAF_R:
5147 case BUILT_IN_GAMMAL_R:
5148 case BUILT_IN_MODF:
5149 case BUILT_IN_MODFF:
5150 case BUILT_IN_MODFL:
5152 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5153 return;
5155 /* The following functions clobber their third argument. */
5156 case BUILT_IN_REMQUO:
5157 case BUILT_IN_REMQUOF:
5158 case BUILT_IN_REMQUOL:
5160 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5161 return;
5163 /* The following functions neither read nor clobber memory. */
5164 case BUILT_IN_ASSUME_ALIGNED:
5165 case BUILT_IN_FREE:
5166 return;
5167 /* Trampolines are of no interest to us. */
5168 case BUILT_IN_INIT_TRAMPOLINE:
5169 case BUILT_IN_ADJUST_TRAMPOLINE:
5170 return;
5171 case BUILT_IN_VA_START:
5172 case BUILT_IN_VA_END:
5173 return;
5174 case BUILT_IN_GOMP_PARALLEL:
5175 case BUILT_IN_GOACC_PARALLEL:
5177 unsigned int fnpos, argpos;
5178 unsigned int implicit_use_args[2];
5179 unsigned int num_implicit_use_args = 0;
5180 switch (DECL_FUNCTION_CODE (decl))
5182 case BUILT_IN_GOMP_PARALLEL:
5183 /* __builtin_GOMP_parallel (fn, data, num_threads, flags). */
5184 fnpos = 0;
5185 argpos = 1;
5186 break;
5187 case BUILT_IN_GOACC_PARALLEL:
5188 /* __builtin_GOACC_parallel (device, fn, mapnum, hostaddrs,
5189 sizes, kinds, ...). */
5190 fnpos = 1;
5191 argpos = 3;
5192 implicit_use_args[num_implicit_use_args++] = 4;
5193 implicit_use_args[num_implicit_use_args++] = 5;
5194 break;
5195 default:
5196 gcc_unreachable ();
5199 tree fnarg = gimple_call_arg (t, fnpos);
5200 gcc_assert (TREE_CODE (fnarg) == ADDR_EXPR);
5201 tree fndecl = TREE_OPERAND (fnarg, 0);
5202 if (fndecl_maybe_in_other_partition (fndecl))
5203 /* Fallthru to general call handling. */
5204 break;
5206 varinfo_t cfi = get_vi_for_tree (fndecl);
5208 tree arg = gimple_call_arg (t, argpos);
5210 /* Parameter passed by value is used. */
5211 lhs = get_function_part_constraint (fi, fi_uses);
5212 struct constraint_expr *rhsp;
5213 get_constraint_for (arg, &rhsc);
5214 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5215 process_constraint (new_constraint (lhs, *rhsp));
5216 rhsc.truncate (0);
5218 /* Handle parameters used by the call, but not used in cfi, as
5219 implicitly used by cfi. */
5220 lhs = get_function_part_constraint (cfi, fi_uses);
5221 for (unsigned i = 0; i < num_implicit_use_args; ++i)
5223 tree arg = gimple_call_arg (t, implicit_use_args[i]);
5224 get_constraint_for (arg, &rhsc);
5225 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5226 process_constraint (new_constraint (lhs, *rhsp));
5227 rhsc.truncate (0);
5230 /* The caller clobbers what the callee does. */
5231 lhs = get_function_part_constraint (fi, fi_clobbers);
5232 rhs = get_function_part_constraint (cfi, fi_clobbers);
5233 process_constraint (new_constraint (lhs, rhs));
5235 /* The caller uses what the callee does. */
5236 lhs = get_function_part_constraint (fi, fi_uses);
5237 rhs = get_function_part_constraint (cfi, fi_uses);
5238 process_constraint (new_constraint (lhs, rhs));
5240 return;
5242 /* printf-style functions may have hooks to set pointers to
5243 point to somewhere into the generated string. Leave them
5244 for a later exercise... */
5245 default:
5246 /* Fallthru to general call handling. */;
5249 /* Parameters passed by value are used. */
5250 lhs = get_function_part_constraint (fi, fi_uses);
5251 for (i = 0; i < gimple_call_num_args (t); i++)
5253 struct constraint_expr *rhsp;
5254 tree arg = gimple_call_arg (t, i);
5256 if (TREE_CODE (arg) == SSA_NAME
5257 || is_gimple_min_invariant (arg))
5258 continue;
5260 get_constraint_for_address_of (arg, &rhsc);
5261 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5262 process_constraint (new_constraint (lhs, *rhsp));
5263 rhsc.truncate (0);
5266 /* Build constraints for propagating clobbers/uses along the
5267 callgraph edges. */
5268 cfi = get_fi_for_callee (call_stmt);
5269 if (cfi->id == anything_id)
5271 if (gimple_vdef (t))
5272 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5273 anything_id);
5274 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5275 anything_id);
5276 return;
5279 /* For callees without function info (that's external functions),
5280 ESCAPED is clobbered and used. */
5281 if (gimple_call_fndecl (t)
5282 && !cfi->is_fn_info)
5284 varinfo_t vi;
5286 if (gimple_vdef (t))
5287 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5288 escaped_id);
5289 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
5291 /* Also honor the call statement use/clobber info. */
5292 if ((vi = lookup_call_clobber_vi (call_stmt)) != NULL)
5293 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5294 vi->id);
5295 if ((vi = lookup_call_use_vi (call_stmt)) != NULL)
5296 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
5297 vi->id);
5298 return;
5301 /* Otherwise the caller clobbers and uses what the callee does.
5302 ??? This should use a new complex constraint that filters
5303 local variables of the callee. */
5304 if (gimple_vdef (t))
5306 lhs = get_function_part_constraint (fi, fi_clobbers);
5307 rhs = get_function_part_constraint (cfi, fi_clobbers);
5308 process_constraint (new_constraint (lhs, rhs));
5310 lhs = get_function_part_constraint (fi, fi_uses);
5311 rhs = get_function_part_constraint (cfi, fi_uses);
5312 process_constraint (new_constraint (lhs, rhs));
5314 else if (gimple_code (t) == GIMPLE_ASM)
5316 /* ??? Ick. We can do better. */
5317 if (gimple_vdef (t))
5318 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5319 anything_id);
5320 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5321 anything_id);
5326 /* Find the first varinfo in the same variable as START that overlaps with
5327 OFFSET. Return NULL if we can't find one. */
5329 static varinfo_t
5330 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
5332 /* If the offset is outside of the variable, bail out. */
5333 if (offset >= start->fullsize)
5334 return NULL;
5336 /* If we cannot reach offset from start, lookup the first field
5337 and start from there. */
5338 if (start->offset > offset)
5339 start = get_varinfo (start->head);
5341 while (start)
5343 /* We may not find a variable in the field list with the actual
5344 offset when we have glommed a structure to a variable.
5345 In that case, however, offset should still be within the size
5346 of the variable. */
5347 if (offset >= start->offset
5348 && (offset - start->offset) < start->size)
5349 return start;
5351 start = vi_next (start);
5354 return NULL;
5357 /* Find the first varinfo in the same variable as START that overlaps with
5358 OFFSET. If there is no such varinfo the varinfo directly preceding
5359 OFFSET is returned. */
5361 static varinfo_t
5362 first_or_preceding_vi_for_offset (varinfo_t start,
5363 unsigned HOST_WIDE_INT offset)
5365 /* If we cannot reach offset from start, lookup the first field
5366 and start from there. */
5367 if (start->offset > offset)
5368 start = get_varinfo (start->head);
5370 /* We may not find a variable in the field list with the actual
5371 offset when we have glommed a structure to a variable.
5372 In that case, however, offset should still be within the size
5373 of the variable.
5374 If we got beyond the offset we look for return the field
5375 directly preceding offset which may be the last field. */
5376 while (start->next
5377 && offset >= start->offset
5378 && !((offset - start->offset) < start->size))
5379 start = vi_next (start);
5381 return start;
5385 /* This structure is used during pushing fields onto the fieldstack
5386 to track the offset of the field, since bitpos_of_field gives it
5387 relative to its immediate containing type, and we want it relative
5388 to the ultimate containing object. */
5390 struct fieldoff
5392 /* Offset from the base of the base containing object to this field. */
5393 HOST_WIDE_INT offset;
5395 /* Size, in bits, of the field. */
5396 unsigned HOST_WIDE_INT size;
5398 unsigned has_unknown_size : 1;
5400 unsigned must_have_pointers : 1;
5402 unsigned may_have_pointers : 1;
5404 unsigned only_restrict_pointers : 1;
5406 tree restrict_pointed_type;
5408 typedef struct fieldoff fieldoff_s;
5411 /* qsort comparison function for two fieldoff's PA and PB */
5413 static int
5414 fieldoff_compare (const void *pa, const void *pb)
5416 const fieldoff_s *foa = (const fieldoff_s *)pa;
5417 const fieldoff_s *fob = (const fieldoff_s *)pb;
5418 unsigned HOST_WIDE_INT foasize, fobsize;
5420 if (foa->offset < fob->offset)
5421 return -1;
5422 else if (foa->offset > fob->offset)
5423 return 1;
5425 foasize = foa->size;
5426 fobsize = fob->size;
5427 if (foasize < fobsize)
5428 return -1;
5429 else if (foasize > fobsize)
5430 return 1;
5431 return 0;
5434 /* Sort a fieldstack according to the field offset and sizes. */
5435 static void
5436 sort_fieldstack (vec<fieldoff_s> fieldstack)
5438 fieldstack.qsort (fieldoff_compare);
5441 /* Return true if T is a type that can have subvars. */
5443 static inline bool
5444 type_can_have_subvars (const_tree t)
5446 /* Aggregates without overlapping fields can have subvars. */
5447 return TREE_CODE (t) == RECORD_TYPE;
5450 /* Return true if V is a tree that we can have subvars for.
5451 Normally, this is any aggregate type. Also complex
5452 types which are not gimple registers can have subvars. */
5454 static inline bool
5455 var_can_have_subvars (const_tree v)
5457 /* Volatile variables should never have subvars. */
5458 if (TREE_THIS_VOLATILE (v))
5459 return false;
5461 /* Non decls or memory tags can never have subvars. */
5462 if (!DECL_P (v))
5463 return false;
5465 return type_can_have_subvars (TREE_TYPE (v));
5468 /* Return true if T is a type that does contain pointers. */
5470 static bool
5471 type_must_have_pointers (tree type)
5473 if (POINTER_TYPE_P (type))
5474 return true;
5476 if (TREE_CODE (type) == ARRAY_TYPE)
5477 return type_must_have_pointers (TREE_TYPE (type));
5479 /* A function or method can have pointers as arguments, so track
5480 those separately. */
5481 if (TREE_CODE (type) == FUNCTION_TYPE
5482 || TREE_CODE (type) == METHOD_TYPE)
5483 return true;
5485 return false;
5488 static bool
5489 field_must_have_pointers (tree t)
5491 return type_must_have_pointers (TREE_TYPE (t));
5494 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5495 the fields of TYPE onto fieldstack, recording their offsets along
5496 the way.
5498 OFFSET is used to keep track of the offset in this entire
5499 structure, rather than just the immediately containing structure.
5500 Returns false if the caller is supposed to handle the field we
5501 recursed for. */
5503 static bool
5504 push_fields_onto_fieldstack (tree type, vec<fieldoff_s> *fieldstack,
5505 HOST_WIDE_INT offset)
5507 tree field;
5508 bool empty_p = true;
5510 if (TREE_CODE (type) != RECORD_TYPE)
5511 return false;
5513 /* If the vector of fields is growing too big, bail out early.
5514 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5515 sure this fails. */
5516 if (fieldstack->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5517 return false;
5519 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5520 if (TREE_CODE (field) == FIELD_DECL)
5522 bool push = false;
5523 HOST_WIDE_INT foff = bitpos_of_field (field);
5524 tree field_type = TREE_TYPE (field);
5526 if (!var_can_have_subvars (field)
5527 || TREE_CODE (field_type) == QUAL_UNION_TYPE
5528 || TREE_CODE (field_type) == UNION_TYPE)
5529 push = true;
5530 else if (!push_fields_onto_fieldstack
5531 (field_type, fieldstack, offset + foff)
5532 && (DECL_SIZE (field)
5533 && !integer_zerop (DECL_SIZE (field))))
5534 /* Empty structures may have actual size, like in C++. So
5535 see if we didn't push any subfields and the size is
5536 nonzero, push the field onto the stack. */
5537 push = true;
5539 if (push)
5541 fieldoff_s *pair = NULL;
5542 bool has_unknown_size = false;
5543 bool must_have_pointers_p;
5545 if (!fieldstack->is_empty ())
5546 pair = &fieldstack->last ();
5548 /* If there isn't anything at offset zero, create sth. */
5549 if (!pair
5550 && offset + foff != 0)
5552 fieldoff_s e
5553 = {0, offset + foff, false, false, false, false, NULL_TREE};
5554 pair = fieldstack->safe_push (e);
5557 if (!DECL_SIZE (field)
5558 || !tree_fits_uhwi_p (DECL_SIZE (field)))
5559 has_unknown_size = true;
5561 /* If adjacent fields do not contain pointers merge them. */
5562 must_have_pointers_p = field_must_have_pointers (field);
5563 if (pair
5564 && !has_unknown_size
5565 && !must_have_pointers_p
5566 && !pair->must_have_pointers
5567 && !pair->has_unknown_size
5568 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5570 pair->size += tree_to_uhwi (DECL_SIZE (field));
5572 else
5574 fieldoff_s e;
5575 e.offset = offset + foff;
5576 e.has_unknown_size = has_unknown_size;
5577 if (!has_unknown_size)
5578 e.size = tree_to_uhwi (DECL_SIZE (field));
5579 else
5580 e.size = -1;
5581 e.must_have_pointers = must_have_pointers_p;
5582 e.may_have_pointers = true;
5583 e.only_restrict_pointers
5584 = (!has_unknown_size
5585 && POINTER_TYPE_P (field_type)
5586 && TYPE_RESTRICT (field_type));
5587 if (e.only_restrict_pointers)
5588 e.restrict_pointed_type = TREE_TYPE (field_type);
5589 fieldstack->safe_push (e);
5593 empty_p = false;
5596 return !empty_p;
5599 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5600 if it is a varargs function. */
5602 static unsigned int
5603 count_num_arguments (tree decl, bool *is_varargs)
5605 unsigned int num = 0;
5606 tree t;
5608 /* Capture named arguments for K&R functions. They do not
5609 have a prototype and thus no TYPE_ARG_TYPES. */
5610 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5611 ++num;
5613 /* Check if the function has variadic arguments. */
5614 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5615 if (TREE_VALUE (t) == void_type_node)
5616 break;
5617 if (!t)
5618 *is_varargs = true;
5620 return num;
5623 /* Creation function node for DECL, using NAME, and return the index
5624 of the variable we've created for the function. If NONLOCAL_p, create
5625 initial constraints. */
5627 static varinfo_t
5628 create_function_info_for (tree decl, const char *name, bool add_id,
5629 bool nonlocal_p)
5631 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5632 varinfo_t vi, prev_vi;
5633 tree arg;
5634 unsigned int i;
5635 bool is_varargs = false;
5636 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5638 /* Create the variable info. */
5640 vi = new_var_info (decl, name, add_id);
5641 vi->offset = 0;
5642 vi->size = 1;
5643 vi->fullsize = fi_parm_base + num_args;
5644 vi->is_fn_info = 1;
5645 vi->may_have_pointers = false;
5646 if (is_varargs)
5647 vi->fullsize = ~0;
5648 insert_vi_for_tree (vi->decl, vi);
5650 prev_vi = vi;
5652 /* Create a variable for things the function clobbers and one for
5653 things the function uses. */
5655 varinfo_t clobbervi, usevi;
5656 const char *newname;
5657 char *tempname;
5659 tempname = xasprintf ("%s.clobber", name);
5660 newname = ggc_strdup (tempname);
5661 free (tempname);
5663 clobbervi = new_var_info (NULL, newname, false);
5664 clobbervi->offset = fi_clobbers;
5665 clobbervi->size = 1;
5666 clobbervi->fullsize = vi->fullsize;
5667 clobbervi->is_full_var = true;
5668 clobbervi->is_global_var = false;
5670 gcc_assert (prev_vi->offset < clobbervi->offset);
5671 prev_vi->next = clobbervi->id;
5672 prev_vi = clobbervi;
5674 tempname = xasprintf ("%s.use", name);
5675 newname = ggc_strdup (tempname);
5676 free (tempname);
5678 usevi = new_var_info (NULL, newname, false);
5679 usevi->offset = fi_uses;
5680 usevi->size = 1;
5681 usevi->fullsize = vi->fullsize;
5682 usevi->is_full_var = true;
5683 usevi->is_global_var = false;
5685 gcc_assert (prev_vi->offset < usevi->offset);
5686 prev_vi->next = usevi->id;
5687 prev_vi = usevi;
5690 /* And one for the static chain. */
5691 if (fn->static_chain_decl != NULL_TREE)
5693 varinfo_t chainvi;
5694 const char *newname;
5695 char *tempname;
5697 tempname = xasprintf ("%s.chain", name);
5698 newname = ggc_strdup (tempname);
5699 free (tempname);
5701 chainvi = new_var_info (fn->static_chain_decl, newname, false);
5702 chainvi->offset = fi_static_chain;
5703 chainvi->size = 1;
5704 chainvi->fullsize = vi->fullsize;
5705 chainvi->is_full_var = true;
5706 chainvi->is_global_var = false;
5708 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5710 if (nonlocal_p
5711 && chainvi->may_have_pointers)
5712 make_constraint_from (chainvi, nonlocal_id);
5714 gcc_assert (prev_vi->offset < chainvi->offset);
5715 prev_vi->next = chainvi->id;
5716 prev_vi = chainvi;
5719 /* Create a variable for the return var. */
5720 if (DECL_RESULT (decl) != NULL
5721 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5723 varinfo_t resultvi;
5724 const char *newname;
5725 char *tempname;
5726 tree resultdecl = decl;
5728 if (DECL_RESULT (decl))
5729 resultdecl = DECL_RESULT (decl);
5731 tempname = xasprintf ("%s.result", name);
5732 newname = ggc_strdup (tempname);
5733 free (tempname);
5735 resultvi = new_var_info (resultdecl, newname, false);
5736 resultvi->offset = fi_result;
5737 resultvi->size = 1;
5738 resultvi->fullsize = vi->fullsize;
5739 resultvi->is_full_var = true;
5740 if (DECL_RESULT (decl))
5741 resultvi->may_have_pointers = true;
5743 if (DECL_RESULT (decl))
5744 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
5746 if (nonlocal_p
5747 && DECL_RESULT (decl)
5748 && DECL_BY_REFERENCE (DECL_RESULT (decl)))
5749 make_constraint_from (resultvi, nonlocal_id);
5751 gcc_assert (prev_vi->offset < resultvi->offset);
5752 prev_vi->next = resultvi->id;
5753 prev_vi = resultvi;
5756 /* We also need to make function return values escape. Nothing
5757 escapes by returning from main though. */
5758 if (nonlocal_p
5759 && !MAIN_NAME_P (DECL_NAME (decl)))
5761 varinfo_t fi, rvi;
5762 fi = lookup_vi_for_tree (decl);
5763 rvi = first_vi_for_offset (fi, fi_result);
5764 if (rvi && rvi->offset == fi_result)
5765 make_copy_constraint (get_varinfo (escaped_id), rvi->id);
5768 /* Set up variables for each argument. */
5769 arg = DECL_ARGUMENTS (decl);
5770 for (i = 0; i < num_args; i++)
5772 varinfo_t argvi;
5773 const char *newname;
5774 char *tempname;
5775 tree argdecl = decl;
5777 if (arg)
5778 argdecl = arg;
5780 tempname = xasprintf ("%s.arg%d", name, i);
5781 newname = ggc_strdup (tempname);
5782 free (tempname);
5784 argvi = new_var_info (argdecl, newname, false);
5785 argvi->offset = fi_parm_base + i;
5786 argvi->size = 1;
5787 argvi->is_full_var = true;
5788 argvi->fullsize = vi->fullsize;
5789 if (arg)
5790 argvi->may_have_pointers = true;
5792 if (arg)
5793 insert_vi_for_tree (arg, argvi);
5795 if (nonlocal_p
5796 && argvi->may_have_pointers)
5797 make_constraint_from (argvi, nonlocal_id);
5799 gcc_assert (prev_vi->offset < argvi->offset);
5800 prev_vi->next = argvi->id;
5801 prev_vi = argvi;
5802 if (arg)
5803 arg = DECL_CHAIN (arg);
5806 /* Add one representative for all further args. */
5807 if (is_varargs)
5809 varinfo_t argvi;
5810 const char *newname;
5811 char *tempname;
5812 tree decl;
5814 tempname = xasprintf ("%s.varargs", name);
5815 newname = ggc_strdup (tempname);
5816 free (tempname);
5818 /* We need sth that can be pointed to for va_start. */
5819 decl = build_fake_var_decl (ptr_type_node);
5821 argvi = new_var_info (decl, newname, false);
5822 argvi->offset = fi_parm_base + num_args;
5823 argvi->size = ~0;
5824 argvi->is_full_var = true;
5825 argvi->is_heap_var = true;
5826 argvi->fullsize = vi->fullsize;
5828 if (nonlocal_p
5829 && argvi->may_have_pointers)
5830 make_constraint_from (argvi, nonlocal_id);
5832 gcc_assert (prev_vi->offset < argvi->offset);
5833 prev_vi->next = argvi->id;
5834 prev_vi = argvi;
5837 return vi;
5841 /* Return true if FIELDSTACK contains fields that overlap.
5842 FIELDSTACK is assumed to be sorted by offset. */
5844 static bool
5845 check_for_overlaps (vec<fieldoff_s> fieldstack)
5847 fieldoff_s *fo = NULL;
5848 unsigned int i;
5849 HOST_WIDE_INT lastoffset = -1;
5851 FOR_EACH_VEC_ELT (fieldstack, i, fo)
5853 if (fo->offset == lastoffset)
5854 return true;
5855 lastoffset = fo->offset;
5857 return false;
5860 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5861 This will also create any varinfo structures necessary for fields
5862 of DECL. DECL is a function parameter if HANDLE_PARAM is set.
5863 HANDLED_STRUCT_TYPE is used to register struct types reached by following
5864 restrict pointers. This is needed to prevent infinite recursion. */
5866 static varinfo_t
5867 create_variable_info_for_1 (tree decl, const char *name, bool add_id,
5868 bool handle_param, bitmap handled_struct_type)
5870 varinfo_t vi, newvi;
5871 tree decl_type = TREE_TYPE (decl);
5872 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
5873 auto_vec<fieldoff_s> fieldstack;
5874 fieldoff_s *fo;
5875 unsigned int i;
5877 if (!declsize
5878 || !tree_fits_uhwi_p (declsize))
5880 vi = new_var_info (decl, name, add_id);
5881 vi->offset = 0;
5882 vi->size = ~0;
5883 vi->fullsize = ~0;
5884 vi->is_unknown_size_var = true;
5885 vi->is_full_var = true;
5886 vi->may_have_pointers = true;
5887 return vi;
5890 /* Collect field information. */
5891 if (use_field_sensitive
5892 && var_can_have_subvars (decl)
5893 /* ??? Force us to not use subfields for globals in IPA mode.
5894 Else we'd have to parse arbitrary initializers. */
5895 && !(in_ipa_mode
5896 && is_global_var (decl)))
5898 fieldoff_s *fo = NULL;
5899 bool notokay = false;
5900 unsigned int i;
5902 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
5904 for (i = 0; !notokay && fieldstack.iterate (i, &fo); i++)
5905 if (fo->has_unknown_size
5906 || fo->offset < 0)
5908 notokay = true;
5909 break;
5912 /* We can't sort them if we have a field with a variable sized type,
5913 which will make notokay = true. In that case, we are going to return
5914 without creating varinfos for the fields anyway, so sorting them is a
5915 waste to boot. */
5916 if (!notokay)
5918 sort_fieldstack (fieldstack);
5919 /* Due to some C++ FE issues, like PR 22488, we might end up
5920 what appear to be overlapping fields even though they,
5921 in reality, do not overlap. Until the C++ FE is fixed,
5922 we will simply disable field-sensitivity for these cases. */
5923 notokay = check_for_overlaps (fieldstack);
5926 if (notokay)
5927 fieldstack.release ();
5930 /* If we didn't end up collecting sub-variables create a full
5931 variable for the decl. */
5932 if (fieldstack.length () == 0
5933 || fieldstack.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5935 vi = new_var_info (decl, name, add_id);
5936 vi->offset = 0;
5937 vi->may_have_pointers = true;
5938 vi->fullsize = tree_to_uhwi (declsize);
5939 vi->size = vi->fullsize;
5940 vi->is_full_var = true;
5941 if (POINTER_TYPE_P (decl_type)
5942 && TYPE_RESTRICT (decl_type))
5943 vi->only_restrict_pointers = 1;
5944 if (vi->only_restrict_pointers
5945 && !type_contains_placeholder_p (TREE_TYPE (decl_type))
5946 && handle_param
5947 && !bitmap_bit_p (handled_struct_type,
5948 TYPE_UID (TREE_TYPE (decl_type))))
5950 varinfo_t rvi;
5951 tree heapvar = build_fake_var_decl (TREE_TYPE (decl_type));
5952 DECL_EXTERNAL (heapvar) = 1;
5953 if (var_can_have_subvars (heapvar))
5954 bitmap_set_bit (handled_struct_type,
5955 TYPE_UID (TREE_TYPE (decl_type)));
5956 rvi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS", true,
5957 true, handled_struct_type);
5958 if (var_can_have_subvars (heapvar))
5959 bitmap_clear_bit (handled_struct_type,
5960 TYPE_UID (TREE_TYPE (decl_type)));
5961 rvi->is_restrict_var = 1;
5962 insert_vi_for_tree (heapvar, rvi);
5963 make_constraint_from (vi, rvi->id);
5964 make_param_constraints (rvi);
5966 fieldstack.release ();
5967 return vi;
5970 vi = new_var_info (decl, name, add_id);
5971 vi->fullsize = tree_to_uhwi (declsize);
5972 if (fieldstack.length () == 1)
5973 vi->is_full_var = true;
5974 for (i = 0, newvi = vi;
5975 fieldstack.iterate (i, &fo);
5976 ++i, newvi = vi_next (newvi))
5978 const char *newname = NULL;
5979 char *tempname;
5981 if (dump_file)
5983 if (fieldstack.length () != 1)
5985 tempname
5986 = xasprintf ("%s." HOST_WIDE_INT_PRINT_DEC
5987 "+" HOST_WIDE_INT_PRINT_DEC, name,
5988 fo->offset, fo->size);
5989 newname = ggc_strdup (tempname);
5990 free (tempname);
5993 else
5994 newname = "NULL";
5996 if (newname)
5997 newvi->name = newname;
5998 newvi->offset = fo->offset;
5999 newvi->size = fo->size;
6000 newvi->fullsize = vi->fullsize;
6001 newvi->may_have_pointers = fo->may_have_pointers;
6002 newvi->only_restrict_pointers = fo->only_restrict_pointers;
6003 if (handle_param
6004 && newvi->only_restrict_pointers
6005 && !type_contains_placeholder_p (fo->restrict_pointed_type)
6006 && !bitmap_bit_p (handled_struct_type,
6007 TYPE_UID (fo->restrict_pointed_type)))
6009 varinfo_t rvi;
6010 tree heapvar = build_fake_var_decl (fo->restrict_pointed_type);
6011 DECL_EXTERNAL (heapvar) = 1;
6012 if (var_can_have_subvars (heapvar))
6013 bitmap_set_bit (handled_struct_type,
6014 TYPE_UID (fo->restrict_pointed_type));
6015 rvi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS", true,
6016 true, handled_struct_type);
6017 if (var_can_have_subvars (heapvar))
6018 bitmap_clear_bit (handled_struct_type,
6019 TYPE_UID (fo->restrict_pointed_type));
6020 rvi->is_restrict_var = 1;
6021 insert_vi_for_tree (heapvar, rvi);
6022 make_constraint_from (newvi, rvi->id);
6023 make_param_constraints (rvi);
6025 if (i + 1 < fieldstack.length ())
6027 varinfo_t tem = new_var_info (decl, name, false);
6028 newvi->next = tem->id;
6029 tem->head = vi->id;
6033 return vi;
6036 static unsigned int
6037 create_variable_info_for (tree decl, const char *name, bool add_id)
6039 varinfo_t vi = create_variable_info_for_1 (decl, name, add_id, false, NULL);
6040 unsigned int id = vi->id;
6042 insert_vi_for_tree (decl, vi);
6044 if (!VAR_P (decl))
6045 return id;
6047 /* Create initial constraints for globals. */
6048 for (; vi; vi = vi_next (vi))
6050 if (!vi->may_have_pointers
6051 || !vi->is_global_var)
6052 continue;
6054 /* Mark global restrict qualified pointers. */
6055 if ((POINTER_TYPE_P (TREE_TYPE (decl))
6056 && TYPE_RESTRICT (TREE_TYPE (decl)))
6057 || vi->only_restrict_pointers)
6059 varinfo_t rvi
6060 = make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT",
6061 true);
6062 /* ??? For now exclude reads from globals as restrict sources
6063 if those are not (indirectly) from incoming parameters. */
6064 rvi->is_restrict_var = false;
6065 continue;
6068 /* In non-IPA mode the initializer from nonlocal is all we need. */
6069 if (!in_ipa_mode
6070 || DECL_HARD_REGISTER (decl))
6071 make_copy_constraint (vi, nonlocal_id);
6073 /* In IPA mode parse the initializer and generate proper constraints
6074 for it. */
6075 else
6077 varpool_node *vnode = varpool_node::get (decl);
6079 /* For escaped variables initialize them from nonlocal. */
6080 if (!vnode->all_refs_explicit_p ())
6081 make_copy_constraint (vi, nonlocal_id);
6083 /* If this is a global variable with an initializer and we are in
6084 IPA mode generate constraints for it. */
6085 ipa_ref *ref;
6086 for (unsigned idx = 0; vnode->iterate_reference (idx, ref); ++idx)
6088 auto_vec<ce_s> rhsc;
6089 struct constraint_expr lhs, *rhsp;
6090 unsigned i;
6091 get_constraint_for_address_of (ref->referred->decl, &rhsc);
6092 lhs.var = vi->id;
6093 lhs.offset = 0;
6094 lhs.type = SCALAR;
6095 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
6096 process_constraint (new_constraint (lhs, *rhsp));
6097 /* If this is a variable that escapes from the unit
6098 the initializer escapes as well. */
6099 if (!vnode->all_refs_explicit_p ())
6101 lhs.var = escaped_id;
6102 lhs.offset = 0;
6103 lhs.type = SCALAR;
6104 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
6105 process_constraint (new_constraint (lhs, *rhsp));
6111 return id;
6114 /* Print out the points-to solution for VAR to FILE. */
6116 static void
6117 dump_solution_for_var (FILE *file, unsigned int var)
6119 varinfo_t vi = get_varinfo (var);
6120 unsigned int i;
6121 bitmap_iterator bi;
6123 /* Dump the solution for unified vars anyway, this avoids difficulties
6124 in scanning dumps in the testsuite. */
6125 fprintf (file, "%s = { ", vi->name);
6126 vi = get_varinfo (find (var));
6127 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
6128 fprintf (file, "%s ", get_varinfo (i)->name);
6129 fprintf (file, "}");
6131 /* But note when the variable was unified. */
6132 if (vi->id != var)
6133 fprintf (file, " same as %s", vi->name);
6135 fprintf (file, "\n");
6138 /* Print the points-to solution for VAR to stderr. */
6140 DEBUG_FUNCTION void
6141 debug_solution_for_var (unsigned int var)
6143 dump_solution_for_var (stderr, var);
6146 /* Register the constraints for function parameter related VI. */
6148 static void
6149 make_param_constraints (varinfo_t vi)
6151 for (; vi; vi = vi_next (vi))
6153 if (vi->only_restrict_pointers)
6155 else if (vi->may_have_pointers)
6156 make_constraint_from (vi, nonlocal_id);
6158 if (vi->is_full_var)
6159 break;
6163 /* Create varinfo structures for all of the variables in the
6164 function for intraprocedural mode. */
6166 static void
6167 intra_create_variable_infos (struct function *fn)
6169 tree t;
6170 bitmap handled_struct_type = NULL;
6172 /* For each incoming pointer argument arg, create the constraint ARG
6173 = NONLOCAL or a dummy variable if it is a restrict qualified
6174 passed-by-reference argument. */
6175 for (t = DECL_ARGUMENTS (fn->decl); t; t = DECL_CHAIN (t))
6177 if (handled_struct_type == NULL)
6178 handled_struct_type = BITMAP_ALLOC (NULL);
6180 varinfo_t p
6181 = create_variable_info_for_1 (t, alias_get_name (t), false, true,
6182 handled_struct_type);
6183 insert_vi_for_tree (t, p);
6185 make_param_constraints (p);
6188 if (handled_struct_type != NULL)
6189 BITMAP_FREE (handled_struct_type);
6191 /* Add a constraint for a result decl that is passed by reference. */
6192 if (DECL_RESULT (fn->decl)
6193 && DECL_BY_REFERENCE (DECL_RESULT (fn->decl)))
6195 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (fn->decl));
6197 for (p = result_vi; p; p = vi_next (p))
6198 make_constraint_from (p, nonlocal_id);
6201 /* Add a constraint for the incoming static chain parameter. */
6202 if (fn->static_chain_decl != NULL_TREE)
6204 varinfo_t p, chain_vi = get_vi_for_tree (fn->static_chain_decl);
6206 for (p = chain_vi; p; p = vi_next (p))
6207 make_constraint_from (p, nonlocal_id);
6211 /* Structure used to put solution bitmaps in a hashtable so they can
6212 be shared among variables with the same points-to set. */
6214 typedef struct shared_bitmap_info
6216 bitmap pt_vars;
6217 hashval_t hashcode;
6218 } *shared_bitmap_info_t;
6219 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
6221 /* Shared_bitmap hashtable helpers. */
6223 struct shared_bitmap_hasher : free_ptr_hash <shared_bitmap_info>
6225 static inline hashval_t hash (const shared_bitmap_info *);
6226 static inline bool equal (const shared_bitmap_info *,
6227 const shared_bitmap_info *);
6230 /* Hash function for a shared_bitmap_info_t */
6232 inline hashval_t
6233 shared_bitmap_hasher::hash (const shared_bitmap_info *bi)
6235 return bi->hashcode;
6238 /* Equality function for two shared_bitmap_info_t's. */
6240 inline bool
6241 shared_bitmap_hasher::equal (const shared_bitmap_info *sbi1,
6242 const shared_bitmap_info *sbi2)
6244 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
6247 /* Shared_bitmap hashtable. */
6249 static hash_table<shared_bitmap_hasher> *shared_bitmap_table;
6251 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
6252 existing instance if there is one, NULL otherwise. */
6254 static bitmap
6255 shared_bitmap_lookup (bitmap pt_vars)
6257 shared_bitmap_info **slot;
6258 struct shared_bitmap_info sbi;
6260 sbi.pt_vars = pt_vars;
6261 sbi.hashcode = bitmap_hash (pt_vars);
6263 slot = shared_bitmap_table->find_slot (&sbi, NO_INSERT);
6264 if (!slot)
6265 return NULL;
6266 else
6267 return (*slot)->pt_vars;
6271 /* Add a bitmap to the shared bitmap hashtable. */
6273 static void
6274 shared_bitmap_add (bitmap pt_vars)
6276 shared_bitmap_info **slot;
6277 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
6279 sbi->pt_vars = pt_vars;
6280 sbi->hashcode = bitmap_hash (pt_vars);
6282 slot = shared_bitmap_table->find_slot (sbi, INSERT);
6283 gcc_assert (!*slot);
6284 *slot = sbi;
6288 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
6290 static void
6291 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt,
6292 tree fndecl)
6294 unsigned int i;
6295 bitmap_iterator bi;
6296 varinfo_t escaped_vi = get_varinfo (find (escaped_id));
6297 bool everything_escaped
6298 = escaped_vi->solution && bitmap_bit_p (escaped_vi->solution, anything_id);
6300 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
6302 varinfo_t vi = get_varinfo (i);
6304 /* The only artificial variables that are allowed in a may-alias
6305 set are heap variables. */
6306 if (vi->is_artificial_var && !vi->is_heap_var)
6307 continue;
6309 if (everything_escaped
6310 || (escaped_vi->solution
6311 && bitmap_bit_p (escaped_vi->solution, i)))
6313 pt->vars_contains_escaped = true;
6314 pt->vars_contains_escaped_heap = vi->is_heap_var;
6317 if (vi->is_restrict_var)
6318 pt->vars_contains_restrict = true;
6320 if (VAR_P (vi->decl)
6321 || TREE_CODE (vi->decl) == PARM_DECL
6322 || TREE_CODE (vi->decl) == RESULT_DECL)
6324 /* If we are in IPA mode we will not recompute points-to
6325 sets after inlining so make sure they stay valid. */
6326 if (in_ipa_mode
6327 && !DECL_PT_UID_SET_P (vi->decl))
6328 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
6330 /* Add the decl to the points-to set. Note that the points-to
6331 set contains global variables. */
6332 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
6333 if (vi->is_global_var
6334 /* In IPA mode the escaped_heap trick doesn't work as
6335 ESCAPED is escaped from the unit but
6336 pt_solution_includes_global needs to answer true for
6337 all variables not automatic within a function.
6338 For the same reason is_global_var is not the
6339 correct flag to track - local variables from other
6340 functions also need to be considered global.
6341 Conveniently all HEAP vars are not put in function
6342 scope. */
6343 || (in_ipa_mode
6344 && fndecl
6345 && ! auto_var_in_fn_p (vi->decl, fndecl)))
6346 pt->vars_contains_nonlocal = true;
6349 else if (TREE_CODE (vi->decl) == FUNCTION_DECL
6350 || TREE_CODE (vi->decl) == LABEL_DECL)
6352 /* Nothing should read/write from/to code so we can
6353 save bits by not including them in the points-to bitmaps.
6354 Still mark the points-to set as containing global memory
6355 to make code-patching possible - see PR70128. */
6356 pt->vars_contains_nonlocal = true;
6362 /* Compute the points-to solution *PT for the variable VI. */
6364 static struct pt_solution
6365 find_what_var_points_to (tree fndecl, varinfo_t orig_vi)
6367 unsigned int i;
6368 bitmap_iterator bi;
6369 bitmap finished_solution;
6370 bitmap result;
6371 varinfo_t vi;
6372 struct pt_solution *pt;
6374 /* This variable may have been collapsed, let's get the real
6375 variable. */
6376 vi = get_varinfo (find (orig_vi->id));
6378 /* See if we have already computed the solution and return it. */
6379 pt_solution **slot = &final_solutions->get_or_insert (vi);
6380 if (*slot != NULL)
6381 return **slot;
6383 *slot = pt = XOBNEW (&final_solutions_obstack, struct pt_solution);
6384 memset (pt, 0, sizeof (struct pt_solution));
6386 /* Translate artificial variables into SSA_NAME_PTR_INFO
6387 attributes. */
6388 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
6390 varinfo_t vi = get_varinfo (i);
6392 if (vi->is_artificial_var)
6394 if (vi->id == nothing_id)
6395 pt->null = 1;
6396 else if (vi->id == escaped_id)
6398 if (in_ipa_mode)
6399 pt->ipa_escaped = 1;
6400 else
6401 pt->escaped = 1;
6402 /* Expand some special vars of ESCAPED in-place here. */
6403 varinfo_t evi = get_varinfo (find (escaped_id));
6404 if (bitmap_bit_p (evi->solution, nonlocal_id))
6405 pt->nonlocal = 1;
6407 else if (vi->id == nonlocal_id)
6408 pt->nonlocal = 1;
6409 else if (vi->is_heap_var)
6410 /* We represent heapvars in the points-to set properly. */
6412 else if (vi->id == string_id)
6413 /* Nobody cares - STRING_CSTs are read-only entities. */
6415 else if (vi->id == anything_id
6416 || vi->id == integer_id)
6417 pt->anything = 1;
6421 /* Instead of doing extra work, simply do not create
6422 elaborate points-to information for pt_anything pointers. */
6423 if (pt->anything)
6424 return *pt;
6426 /* Share the final set of variables when possible. */
6427 finished_solution = BITMAP_GGC_ALLOC ();
6428 stats.points_to_sets_created++;
6430 set_uids_in_ptset (finished_solution, vi->solution, pt, fndecl);
6431 result = shared_bitmap_lookup (finished_solution);
6432 if (!result)
6434 shared_bitmap_add (finished_solution);
6435 pt->vars = finished_solution;
6437 else
6439 pt->vars = result;
6440 bitmap_clear (finished_solution);
6443 return *pt;
6446 /* Given a pointer variable P, fill in its points-to set. */
6448 static void
6449 find_what_p_points_to (tree fndecl, tree p)
6451 struct ptr_info_def *pi;
6452 tree lookup_p = p;
6453 varinfo_t vi;
6454 bool nonnull = get_ptr_nonnull (p);
6456 /* For parameters, get at the points-to set for the actual parm
6457 decl. */
6458 if (TREE_CODE (p) == SSA_NAME
6459 && SSA_NAME_IS_DEFAULT_DEF (p)
6460 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
6461 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL))
6462 lookup_p = SSA_NAME_VAR (p);
6464 vi = lookup_vi_for_tree (lookup_p);
6465 if (!vi)
6466 return;
6468 pi = get_ptr_info (p);
6469 pi->pt = find_what_var_points_to (fndecl, vi);
6470 /* Conservatively set to NULL from PTA (to true). */
6471 pi->pt.null = 1;
6472 /* Preserve pointer nonnull computed by VRP. See get_ptr_nonnull
6473 in gcc/tree-ssaname.c for more information. */
6474 if (nonnull)
6475 set_ptr_nonnull (p);
6479 /* Query statistics for points-to solutions. */
6481 static struct {
6482 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
6483 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
6484 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
6485 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
6486 } pta_stats;
6488 void
6489 dump_pta_stats (FILE *s)
6491 fprintf (s, "\nPTA query stats:\n");
6492 fprintf (s, " pt_solution_includes: "
6493 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6494 HOST_WIDE_INT_PRINT_DEC" queries\n",
6495 pta_stats.pt_solution_includes_no_alias,
6496 pta_stats.pt_solution_includes_no_alias
6497 + pta_stats.pt_solution_includes_may_alias);
6498 fprintf (s, " pt_solutions_intersect: "
6499 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6500 HOST_WIDE_INT_PRINT_DEC" queries\n",
6501 pta_stats.pt_solutions_intersect_no_alias,
6502 pta_stats.pt_solutions_intersect_no_alias
6503 + pta_stats.pt_solutions_intersect_may_alias);
6507 /* Reset the points-to solution *PT to a conservative default
6508 (point to anything). */
6510 void
6511 pt_solution_reset (struct pt_solution *pt)
6513 memset (pt, 0, sizeof (struct pt_solution));
6514 pt->anything = true;
6515 pt->null = true;
6518 /* Set the points-to solution *PT to point only to the variables
6519 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6520 global variables and VARS_CONTAINS_RESTRICT specifies whether
6521 it contains restrict tag variables. */
6523 void
6524 pt_solution_set (struct pt_solution *pt, bitmap vars,
6525 bool vars_contains_nonlocal)
6527 memset (pt, 0, sizeof (struct pt_solution));
6528 pt->vars = vars;
6529 pt->vars_contains_nonlocal = vars_contains_nonlocal;
6530 pt->vars_contains_escaped
6531 = (cfun->gimple_df->escaped.anything
6532 || bitmap_intersect_p (cfun->gimple_df->escaped.vars, vars));
6535 /* Set the points-to solution *PT to point only to the variable VAR. */
6537 void
6538 pt_solution_set_var (struct pt_solution *pt, tree var)
6540 memset (pt, 0, sizeof (struct pt_solution));
6541 pt->vars = BITMAP_GGC_ALLOC ();
6542 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
6543 pt->vars_contains_nonlocal = is_global_var (var);
6544 pt->vars_contains_escaped
6545 = (cfun->gimple_df->escaped.anything
6546 || bitmap_bit_p (cfun->gimple_df->escaped.vars, DECL_PT_UID (var)));
6549 /* Computes the union of the points-to solutions *DEST and *SRC and
6550 stores the result in *DEST. This changes the points-to bitmap
6551 of *DEST and thus may not be used if that might be shared.
6552 The points-to bitmap of *SRC and *DEST will not be shared after
6553 this function if they were not before. */
6555 static void
6556 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6558 dest->anything |= src->anything;
6559 if (dest->anything)
6561 pt_solution_reset (dest);
6562 return;
6565 dest->nonlocal |= src->nonlocal;
6566 dest->escaped |= src->escaped;
6567 dest->ipa_escaped |= src->ipa_escaped;
6568 dest->null |= src->null;
6569 dest->vars_contains_nonlocal |= src->vars_contains_nonlocal;
6570 dest->vars_contains_escaped |= src->vars_contains_escaped;
6571 dest->vars_contains_escaped_heap |= src->vars_contains_escaped_heap;
6572 if (!src->vars)
6573 return;
6575 if (!dest->vars)
6576 dest->vars = BITMAP_GGC_ALLOC ();
6577 bitmap_ior_into (dest->vars, src->vars);
6580 /* Return true if the points-to solution *PT is empty. */
6582 bool
6583 pt_solution_empty_p (struct pt_solution *pt)
6585 if (pt->anything
6586 || pt->nonlocal)
6587 return false;
6589 if (pt->vars
6590 && !bitmap_empty_p (pt->vars))
6591 return false;
6593 /* If the solution includes ESCAPED, check if that is empty. */
6594 if (pt->escaped
6595 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6596 return false;
6598 /* If the solution includes ESCAPED, check if that is empty. */
6599 if (pt->ipa_escaped
6600 && !pt_solution_empty_p (&ipa_escaped_pt))
6601 return false;
6603 return true;
6606 /* Return true if the points-to solution *PT only point to a single var, and
6607 return the var uid in *UID. */
6609 bool
6610 pt_solution_singleton_or_null_p (struct pt_solution *pt, unsigned *uid)
6612 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
6613 || pt->vars == NULL
6614 || !bitmap_single_bit_set_p (pt->vars))
6615 return false;
6617 *uid = bitmap_first_set_bit (pt->vars);
6618 return true;
6621 /* Return true if the points-to solution *PT includes global memory. */
6623 bool
6624 pt_solution_includes_global (struct pt_solution *pt)
6626 if (pt->anything
6627 || pt->nonlocal
6628 || pt->vars_contains_nonlocal
6629 /* The following is a hack to make the malloc escape hack work.
6630 In reality we'd need different sets for escaped-through-return
6631 and escaped-to-callees and passes would need to be updated. */
6632 || pt->vars_contains_escaped_heap)
6633 return true;
6635 /* 'escaped' is also a placeholder so we have to look into it. */
6636 if (pt->escaped)
6637 return pt_solution_includes_global (&cfun->gimple_df->escaped);
6639 if (pt->ipa_escaped)
6640 return pt_solution_includes_global (&ipa_escaped_pt);
6642 return false;
6645 /* Return true if the points-to solution *PT includes the variable
6646 declaration DECL. */
6648 static bool
6649 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6651 if (pt->anything)
6652 return true;
6654 if (pt->nonlocal
6655 && is_global_var (decl))
6656 return true;
6658 if (pt->vars
6659 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6660 return true;
6662 /* If the solution includes ESCAPED, check it. */
6663 if (pt->escaped
6664 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6665 return true;
6667 /* If the solution includes ESCAPED, check it. */
6668 if (pt->ipa_escaped
6669 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6670 return true;
6672 return false;
6675 bool
6676 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6678 bool res = pt_solution_includes_1 (pt, decl);
6679 if (res)
6680 ++pta_stats.pt_solution_includes_may_alias;
6681 else
6682 ++pta_stats.pt_solution_includes_no_alias;
6683 return res;
6686 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6687 intersection. */
6689 static bool
6690 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
6692 if (pt1->anything || pt2->anything)
6693 return true;
6695 /* If either points to unknown global memory and the other points to
6696 any global memory they alias. */
6697 if ((pt1->nonlocal
6698 && (pt2->nonlocal
6699 || pt2->vars_contains_nonlocal))
6700 || (pt2->nonlocal
6701 && pt1->vars_contains_nonlocal))
6702 return true;
6704 /* If either points to all escaped memory and the other points to
6705 any escaped memory they alias. */
6706 if ((pt1->escaped
6707 && (pt2->escaped
6708 || pt2->vars_contains_escaped))
6709 || (pt2->escaped
6710 && pt1->vars_contains_escaped))
6711 return true;
6713 /* Check the escaped solution if required.
6714 ??? Do we need to check the local against the IPA escaped sets? */
6715 if ((pt1->ipa_escaped || pt2->ipa_escaped)
6716 && !pt_solution_empty_p (&ipa_escaped_pt))
6718 /* If both point to escaped memory and that solution
6719 is not empty they alias. */
6720 if (pt1->ipa_escaped && pt2->ipa_escaped)
6721 return true;
6723 /* If either points to escaped memory see if the escaped solution
6724 intersects with the other. */
6725 if ((pt1->ipa_escaped
6726 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
6727 || (pt2->ipa_escaped
6728 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
6729 return true;
6732 /* Now both pointers alias if their points-to solution intersects. */
6733 return (pt1->vars
6734 && pt2->vars
6735 && bitmap_intersect_p (pt1->vars, pt2->vars));
6738 bool
6739 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
6741 bool res = pt_solutions_intersect_1 (pt1, pt2);
6742 if (res)
6743 ++pta_stats.pt_solutions_intersect_may_alias;
6744 else
6745 ++pta_stats.pt_solutions_intersect_no_alias;
6746 return res;
6750 /* Dump points-to information to OUTFILE. */
6752 static void
6753 dump_sa_points_to_info (FILE *outfile)
6755 unsigned int i;
6757 fprintf (outfile, "\nPoints-to sets\n\n");
6759 if (dump_flags & TDF_STATS)
6761 fprintf (outfile, "Stats:\n");
6762 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
6763 fprintf (outfile, "Non-pointer vars: %d\n",
6764 stats.nonpointer_vars);
6765 fprintf (outfile, "Statically unified vars: %d\n",
6766 stats.unified_vars_static);
6767 fprintf (outfile, "Dynamically unified vars: %d\n",
6768 stats.unified_vars_dynamic);
6769 fprintf (outfile, "Iterations: %d\n", stats.iterations);
6770 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
6771 fprintf (outfile, "Number of implicit edges: %d\n",
6772 stats.num_implicit_edges);
6775 for (i = 1; i < varmap.length (); i++)
6777 varinfo_t vi = get_varinfo (i);
6778 if (!vi->may_have_pointers)
6779 continue;
6780 dump_solution_for_var (outfile, i);
6785 /* Debug points-to information to stderr. */
6787 DEBUG_FUNCTION void
6788 debug_sa_points_to_info (void)
6790 dump_sa_points_to_info (stderr);
6794 /* Initialize the always-existing constraint variables for NULL
6795 ANYTHING, READONLY, and INTEGER */
6797 static void
6798 init_base_vars (void)
6800 struct constraint_expr lhs, rhs;
6801 varinfo_t var_anything;
6802 varinfo_t var_nothing;
6803 varinfo_t var_string;
6804 varinfo_t var_escaped;
6805 varinfo_t var_nonlocal;
6806 varinfo_t var_storedanything;
6807 varinfo_t var_integer;
6809 /* Variable ID zero is reserved and should be NULL. */
6810 varmap.safe_push (NULL);
6812 /* Create the NULL variable, used to represent that a variable points
6813 to NULL. */
6814 var_nothing = new_var_info (NULL_TREE, "NULL", false);
6815 gcc_assert (var_nothing->id == nothing_id);
6816 var_nothing->is_artificial_var = 1;
6817 var_nothing->offset = 0;
6818 var_nothing->size = ~0;
6819 var_nothing->fullsize = ~0;
6820 var_nothing->is_special_var = 1;
6821 var_nothing->may_have_pointers = 0;
6822 var_nothing->is_global_var = 0;
6824 /* Create the ANYTHING variable, used to represent that a variable
6825 points to some unknown piece of memory. */
6826 var_anything = new_var_info (NULL_TREE, "ANYTHING", false);
6827 gcc_assert (var_anything->id == anything_id);
6828 var_anything->is_artificial_var = 1;
6829 var_anything->size = ~0;
6830 var_anything->offset = 0;
6831 var_anything->fullsize = ~0;
6832 var_anything->is_special_var = 1;
6834 /* Anything points to anything. This makes deref constraints just
6835 work in the presence of linked list and other p = *p type loops,
6836 by saying that *ANYTHING = ANYTHING. */
6837 lhs.type = SCALAR;
6838 lhs.var = anything_id;
6839 lhs.offset = 0;
6840 rhs.type = ADDRESSOF;
6841 rhs.var = anything_id;
6842 rhs.offset = 0;
6844 /* This specifically does not use process_constraint because
6845 process_constraint ignores all anything = anything constraints, since all
6846 but this one are redundant. */
6847 constraints.safe_push (new_constraint (lhs, rhs));
6849 /* Create the STRING variable, used to represent that a variable
6850 points to a string literal. String literals don't contain
6851 pointers so STRING doesn't point to anything. */
6852 var_string = new_var_info (NULL_TREE, "STRING", false);
6853 gcc_assert (var_string->id == string_id);
6854 var_string->is_artificial_var = 1;
6855 var_string->offset = 0;
6856 var_string->size = ~0;
6857 var_string->fullsize = ~0;
6858 var_string->is_special_var = 1;
6859 var_string->may_have_pointers = 0;
6861 /* Create the ESCAPED variable, used to represent the set of escaped
6862 memory. */
6863 var_escaped = new_var_info (NULL_TREE, "ESCAPED", false);
6864 gcc_assert (var_escaped->id == escaped_id);
6865 var_escaped->is_artificial_var = 1;
6866 var_escaped->offset = 0;
6867 var_escaped->size = ~0;
6868 var_escaped->fullsize = ~0;
6869 var_escaped->is_special_var = 0;
6871 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6872 memory. */
6873 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL", false);
6874 gcc_assert (var_nonlocal->id == nonlocal_id);
6875 var_nonlocal->is_artificial_var = 1;
6876 var_nonlocal->offset = 0;
6877 var_nonlocal->size = ~0;
6878 var_nonlocal->fullsize = ~0;
6879 var_nonlocal->is_special_var = 1;
6881 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6882 lhs.type = SCALAR;
6883 lhs.var = escaped_id;
6884 lhs.offset = 0;
6885 rhs.type = DEREF;
6886 rhs.var = escaped_id;
6887 rhs.offset = 0;
6888 process_constraint (new_constraint (lhs, rhs));
6890 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6891 whole variable escapes. */
6892 lhs.type = SCALAR;
6893 lhs.var = escaped_id;
6894 lhs.offset = 0;
6895 rhs.type = SCALAR;
6896 rhs.var = escaped_id;
6897 rhs.offset = UNKNOWN_OFFSET;
6898 process_constraint (new_constraint (lhs, rhs));
6900 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6901 everything pointed to by escaped points to what global memory can
6902 point to. */
6903 lhs.type = DEREF;
6904 lhs.var = escaped_id;
6905 lhs.offset = 0;
6906 rhs.type = SCALAR;
6907 rhs.var = nonlocal_id;
6908 rhs.offset = 0;
6909 process_constraint (new_constraint (lhs, rhs));
6911 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6912 global memory may point to global memory and escaped memory. */
6913 lhs.type = SCALAR;
6914 lhs.var = nonlocal_id;
6915 lhs.offset = 0;
6916 rhs.type = ADDRESSOF;
6917 rhs.var = nonlocal_id;
6918 rhs.offset = 0;
6919 process_constraint (new_constraint (lhs, rhs));
6920 rhs.type = ADDRESSOF;
6921 rhs.var = escaped_id;
6922 rhs.offset = 0;
6923 process_constraint (new_constraint (lhs, rhs));
6925 /* Create the STOREDANYTHING variable, used to represent the set of
6926 variables stored to *ANYTHING. */
6927 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING", false);
6928 gcc_assert (var_storedanything->id == storedanything_id);
6929 var_storedanything->is_artificial_var = 1;
6930 var_storedanything->offset = 0;
6931 var_storedanything->size = ~0;
6932 var_storedanything->fullsize = ~0;
6933 var_storedanything->is_special_var = 0;
6935 /* Create the INTEGER variable, used to represent that a variable points
6936 to what an INTEGER "points to". */
6937 var_integer = new_var_info (NULL_TREE, "INTEGER", false);
6938 gcc_assert (var_integer->id == integer_id);
6939 var_integer->is_artificial_var = 1;
6940 var_integer->size = ~0;
6941 var_integer->fullsize = ~0;
6942 var_integer->offset = 0;
6943 var_integer->is_special_var = 1;
6945 /* INTEGER = ANYTHING, because we don't know where a dereference of
6946 a random integer will point to. */
6947 lhs.type = SCALAR;
6948 lhs.var = integer_id;
6949 lhs.offset = 0;
6950 rhs.type = ADDRESSOF;
6951 rhs.var = anything_id;
6952 rhs.offset = 0;
6953 process_constraint (new_constraint (lhs, rhs));
6956 /* Initialize things necessary to perform PTA */
6958 static void
6959 init_alias_vars (void)
6961 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
6963 bitmap_obstack_initialize (&pta_obstack);
6964 bitmap_obstack_initialize (&oldpta_obstack);
6965 bitmap_obstack_initialize (&predbitmap_obstack);
6967 constraints.create (8);
6968 varmap.create (8);
6969 vi_for_tree = new hash_map<tree, varinfo_t>;
6970 call_stmt_vars = new hash_map<gimple *, varinfo_t>;
6972 memset (&stats, 0, sizeof (stats));
6973 shared_bitmap_table = new hash_table<shared_bitmap_hasher> (511);
6974 init_base_vars ();
6976 gcc_obstack_init (&fake_var_decl_obstack);
6978 final_solutions = new hash_map<varinfo_t, pt_solution *>;
6979 gcc_obstack_init (&final_solutions_obstack);
6982 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6983 predecessor edges. */
6985 static void
6986 remove_preds_and_fake_succs (constraint_graph_t graph)
6988 unsigned int i;
6990 /* Clear the implicit ref and address nodes from the successor
6991 lists. */
6992 for (i = 1; i < FIRST_REF_NODE; i++)
6994 if (graph->succs[i])
6995 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
6996 FIRST_REF_NODE * 2);
6999 /* Free the successor list for the non-ref nodes. */
7000 for (i = FIRST_REF_NODE + 1; i < graph->size; i++)
7002 if (graph->succs[i])
7003 BITMAP_FREE (graph->succs[i]);
7006 /* Now reallocate the size of the successor list as, and blow away
7007 the predecessor bitmaps. */
7008 graph->size = varmap.length ();
7009 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
7011 free (graph->implicit_preds);
7012 graph->implicit_preds = NULL;
7013 free (graph->preds);
7014 graph->preds = NULL;
7015 bitmap_obstack_release (&predbitmap_obstack);
7018 /* Solve the constraint set. */
7020 static void
7021 solve_constraints (void)
7023 struct scc_info *si;
7025 if (dump_file)
7026 fprintf (dump_file,
7027 "\nCollapsing static cycles and doing variable "
7028 "substitution\n");
7030 init_graph (varmap.length () * 2);
7032 if (dump_file)
7033 fprintf (dump_file, "Building predecessor graph\n");
7034 build_pred_graph ();
7036 if (dump_file)
7037 fprintf (dump_file, "Detecting pointer and location "
7038 "equivalences\n");
7039 si = perform_var_substitution (graph);
7041 if (dump_file)
7042 fprintf (dump_file, "Rewriting constraints and unifying "
7043 "variables\n");
7044 rewrite_constraints (graph, si);
7046 build_succ_graph ();
7048 free_var_substitution_info (si);
7050 /* Attach complex constraints to graph nodes. */
7051 move_complex_constraints (graph);
7053 if (dump_file)
7054 fprintf (dump_file, "Uniting pointer but not location equivalent "
7055 "variables\n");
7056 unite_pointer_equivalences (graph);
7058 if (dump_file)
7059 fprintf (dump_file, "Finding indirect cycles\n");
7060 find_indirect_cycles (graph);
7062 /* Implicit nodes and predecessors are no longer necessary at this
7063 point. */
7064 remove_preds_and_fake_succs (graph);
7066 if (dump_file && (dump_flags & TDF_GRAPH))
7068 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
7069 "in dot format:\n");
7070 dump_constraint_graph (dump_file);
7071 fprintf (dump_file, "\n\n");
7074 if (dump_file)
7075 fprintf (dump_file, "Solving graph\n");
7077 solve_graph (graph);
7079 if (dump_file && (dump_flags & TDF_GRAPH))
7081 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
7082 "in dot format:\n");
7083 dump_constraint_graph (dump_file);
7084 fprintf (dump_file, "\n\n");
7087 if (dump_file)
7088 dump_sa_points_to_info (dump_file);
7091 /* Create points-to sets for the current function. See the comments
7092 at the start of the file for an algorithmic overview. */
7094 static void
7095 compute_points_to_sets (void)
7097 basic_block bb;
7098 varinfo_t vi;
7100 timevar_push (TV_TREE_PTA);
7102 init_alias_vars ();
7104 intra_create_variable_infos (cfun);
7106 /* Now walk all statements and build the constraint set. */
7107 FOR_EACH_BB_FN (bb, cfun)
7109 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7110 gsi_next (&gsi))
7112 gphi *phi = gsi.phi ();
7114 if (! virtual_operand_p (gimple_phi_result (phi)))
7115 find_func_aliases (cfun, phi);
7118 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
7119 gsi_next (&gsi))
7121 gimple *stmt = gsi_stmt (gsi);
7123 find_func_aliases (cfun, stmt);
7127 if (dump_file)
7129 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
7130 dump_constraints (dump_file, 0);
7133 /* From the constraints compute the points-to sets. */
7134 solve_constraints ();
7136 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
7137 cfun->gimple_df->escaped = find_what_var_points_to (cfun->decl,
7138 get_varinfo (escaped_id));
7140 /* Make sure the ESCAPED solution (which is used as placeholder in
7141 other solutions) does not reference itself. This simplifies
7142 points-to solution queries. */
7143 cfun->gimple_df->escaped.escaped = 0;
7145 /* Compute the points-to sets for pointer SSA_NAMEs. */
7146 unsigned i;
7147 tree ptr;
7149 FOR_EACH_SSA_NAME (i, ptr, cfun)
7151 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
7152 find_what_p_points_to (cfun->decl, ptr);
7155 /* Compute the call-used/clobbered sets. */
7156 FOR_EACH_BB_FN (bb, cfun)
7158 gimple_stmt_iterator gsi;
7160 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7162 gcall *stmt;
7163 struct pt_solution *pt;
7165 stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
7166 if (!stmt)
7167 continue;
7169 pt = gimple_call_use_set (stmt);
7170 if (gimple_call_flags (stmt) & ECF_CONST)
7171 memset (pt, 0, sizeof (struct pt_solution));
7172 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7174 *pt = find_what_var_points_to (cfun->decl, vi);
7175 /* Escaped (and thus nonlocal) variables are always
7176 implicitly used by calls. */
7177 /* ??? ESCAPED can be empty even though NONLOCAL
7178 always escaped. */
7179 pt->nonlocal = 1;
7180 pt->escaped = 1;
7182 else
7184 /* If there is nothing special about this call then
7185 we have made everything that is used also escape. */
7186 *pt = cfun->gimple_df->escaped;
7187 pt->nonlocal = 1;
7190 pt = gimple_call_clobber_set (stmt);
7191 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7192 memset (pt, 0, sizeof (struct pt_solution));
7193 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7195 *pt = find_what_var_points_to (cfun->decl, vi);
7196 /* Escaped (and thus nonlocal) variables are always
7197 implicitly clobbered by calls. */
7198 /* ??? ESCAPED can be empty even though NONLOCAL
7199 always escaped. */
7200 pt->nonlocal = 1;
7201 pt->escaped = 1;
7203 else
7205 /* If there is nothing special about this call then
7206 we have made everything that is used also escape. */
7207 *pt = cfun->gimple_df->escaped;
7208 pt->nonlocal = 1;
7213 timevar_pop (TV_TREE_PTA);
7217 /* Delete created points-to sets. */
7219 static void
7220 delete_points_to_sets (void)
7222 unsigned int i;
7224 delete shared_bitmap_table;
7225 shared_bitmap_table = NULL;
7226 if (dump_file && (dump_flags & TDF_STATS))
7227 fprintf (dump_file, "Points to sets created:%d\n",
7228 stats.points_to_sets_created);
7230 delete vi_for_tree;
7231 delete call_stmt_vars;
7232 bitmap_obstack_release (&pta_obstack);
7233 constraints.release ();
7235 for (i = 0; i < graph->size; i++)
7236 graph->complex[i].release ();
7237 free (graph->complex);
7239 free (graph->rep);
7240 free (graph->succs);
7241 free (graph->pe);
7242 free (graph->pe_rep);
7243 free (graph->indirect_cycles);
7244 free (graph);
7246 varmap.release ();
7247 variable_info_pool.release ();
7248 constraint_pool.release ();
7250 obstack_free (&fake_var_decl_obstack, NULL);
7252 delete final_solutions;
7253 obstack_free (&final_solutions_obstack, NULL);
7256 struct vls_data
7258 unsigned short clique;
7259 bitmap rvars;
7262 /* Mark "other" loads and stores as belonging to CLIQUE and with
7263 base zero. */
7265 static bool
7266 visit_loadstore (gimple *, tree base, tree ref, void *data)
7268 unsigned short clique = ((vls_data *) data)->clique;
7269 bitmap rvars = ((vls_data *) data)->rvars;
7270 if (TREE_CODE (base) == MEM_REF
7271 || TREE_CODE (base) == TARGET_MEM_REF)
7273 tree ptr = TREE_OPERAND (base, 0);
7274 if (TREE_CODE (ptr) == SSA_NAME
7275 && ! SSA_NAME_IS_DEFAULT_DEF (ptr))
7277 /* We need to make sure 'ptr' doesn't include any of
7278 the restrict tags we added bases for in its points-to set. */
7279 varinfo_t vi = lookup_vi_for_tree (ptr);
7280 if (! vi)
7281 return false;
7283 vi = get_varinfo (find (vi->id));
7284 if (bitmap_intersect_p (rvars, vi->solution))
7285 return false;
7288 /* Do not overwrite existing cliques (that includes clique, base
7289 pairs we just set). */
7290 if (MR_DEPENDENCE_CLIQUE (base) == 0)
7292 MR_DEPENDENCE_CLIQUE (base) = clique;
7293 MR_DEPENDENCE_BASE (base) = 0;
7297 /* For plain decl accesses see whether they are accesses to globals
7298 and rewrite them to MEM_REFs with { clique, 0 }. */
7299 if (VAR_P (base)
7300 && is_global_var (base)
7301 /* ??? We can't rewrite a plain decl with the walk_stmt_load_store
7302 ops callback. */
7303 && base != ref)
7305 tree *basep = &ref;
7306 while (handled_component_p (*basep))
7307 basep = &TREE_OPERAND (*basep, 0);
7308 gcc_assert (VAR_P (*basep));
7309 tree ptr = build_fold_addr_expr (*basep);
7310 tree zero = build_int_cst (TREE_TYPE (ptr), 0);
7311 *basep = build2 (MEM_REF, TREE_TYPE (*basep), ptr, zero);
7312 MR_DEPENDENCE_CLIQUE (*basep) = clique;
7313 MR_DEPENDENCE_BASE (*basep) = 0;
7316 return false;
7319 /* If REF is a MEM_REF then assign a clique, base pair to it, updating
7320 CLIQUE, *RESTRICT_VAR and LAST_RUID. Return whether dependence info
7321 was assigned to REF. */
7323 static bool
7324 maybe_set_dependence_info (tree ref, tree ptr,
7325 unsigned short &clique, varinfo_t restrict_var,
7326 unsigned short &last_ruid)
7328 while (handled_component_p (ref))
7329 ref = TREE_OPERAND (ref, 0);
7330 if ((TREE_CODE (ref) == MEM_REF
7331 || TREE_CODE (ref) == TARGET_MEM_REF)
7332 && TREE_OPERAND (ref, 0) == ptr)
7334 /* Do not overwrite existing cliques. This avoids overwriting dependence
7335 info inlined from a function with restrict parameters inlined
7336 into a function with restrict parameters. This usually means we
7337 prefer to be precise in innermost loops. */
7338 if (MR_DEPENDENCE_CLIQUE (ref) == 0)
7340 if (clique == 0)
7341 clique = ++cfun->last_clique;
7342 if (restrict_var->ruid == 0)
7343 restrict_var->ruid = ++last_ruid;
7344 MR_DEPENDENCE_CLIQUE (ref) = clique;
7345 MR_DEPENDENCE_BASE (ref) = restrict_var->ruid;
7346 return true;
7349 return false;
7352 /* Compute the set of independend memory references based on restrict
7353 tags and their conservative propagation to the points-to sets. */
7355 static void
7356 compute_dependence_clique (void)
7358 unsigned short clique = 0;
7359 unsigned short last_ruid = 0;
7360 bitmap rvars = BITMAP_ALLOC (NULL);
7361 for (unsigned i = 0; i < num_ssa_names; ++i)
7363 tree ptr = ssa_name (i);
7364 if (!ptr || !POINTER_TYPE_P (TREE_TYPE (ptr)))
7365 continue;
7367 /* Avoid all this when ptr is not dereferenced? */
7368 tree p = ptr;
7369 if (SSA_NAME_IS_DEFAULT_DEF (ptr)
7370 && (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
7371 || TREE_CODE (SSA_NAME_VAR (ptr)) == RESULT_DECL))
7372 p = SSA_NAME_VAR (ptr);
7373 varinfo_t vi = lookup_vi_for_tree (p);
7374 if (!vi)
7375 continue;
7376 vi = get_varinfo (find (vi->id));
7377 bitmap_iterator bi;
7378 unsigned j;
7379 varinfo_t restrict_var = NULL;
7380 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, j, bi)
7382 varinfo_t oi = get_varinfo (j);
7383 if (oi->is_restrict_var)
7385 if (restrict_var)
7387 if (dump_file && (dump_flags & TDF_DETAILS))
7389 fprintf (dump_file, "found restrict pointed-to "
7390 "for ");
7391 print_generic_expr (dump_file, ptr, 0);
7392 fprintf (dump_file, " but not exclusively\n");
7394 restrict_var = NULL;
7395 break;
7397 restrict_var = oi;
7399 /* NULL is the only other valid points-to entry. */
7400 else if (oi->id != nothing_id)
7402 restrict_var = NULL;
7403 break;
7406 /* Ok, found that ptr must(!) point to a single(!) restrict
7407 variable. */
7408 /* ??? PTA isn't really a proper propagation engine to compute
7409 this property.
7410 ??? We could handle merging of two restricts by unifying them. */
7411 if (restrict_var)
7413 /* Now look at possible dereferences of ptr. */
7414 imm_use_iterator ui;
7415 gimple *use_stmt;
7416 bool used = false;
7417 FOR_EACH_IMM_USE_STMT (use_stmt, ui, ptr)
7419 /* ??? Calls and asms. */
7420 if (!gimple_assign_single_p (use_stmt))
7421 continue;
7422 used |= maybe_set_dependence_info (gimple_assign_lhs (use_stmt),
7423 ptr, clique, restrict_var,
7424 last_ruid);
7425 used |= maybe_set_dependence_info (gimple_assign_rhs1 (use_stmt),
7426 ptr, clique, restrict_var,
7427 last_ruid);
7429 if (used)
7430 bitmap_set_bit (rvars, restrict_var->id);
7434 if (clique != 0)
7436 /* Assign the BASE id zero to all accesses not based on a restrict
7437 pointer. That way they get disambiguated against restrict
7438 accesses but not against each other. */
7439 /* ??? For restricts derived from globals (thus not incoming
7440 parameters) we can't restrict scoping properly thus the following
7441 is too aggressive there. For now we have excluded those globals from
7442 getting into the MR_DEPENDENCE machinery. */
7443 vls_data data = { clique, rvars };
7444 basic_block bb;
7445 FOR_EACH_BB_FN (bb, cfun)
7446 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
7447 !gsi_end_p (gsi); gsi_next (&gsi))
7449 gimple *stmt = gsi_stmt (gsi);
7450 walk_stmt_load_store_ops (stmt, &data,
7451 visit_loadstore, visit_loadstore);
7455 BITMAP_FREE (rvars);
7458 /* Compute points-to information for every SSA_NAME pointer in the
7459 current function and compute the transitive closure of escaped
7460 variables to re-initialize the call-clobber states of local variables. */
7462 unsigned int
7463 compute_may_aliases (void)
7465 if (cfun->gimple_df->ipa_pta)
7467 if (dump_file)
7469 fprintf (dump_file, "\nNot re-computing points-to information "
7470 "because IPA points-to information is available.\n\n");
7472 /* But still dump what we have remaining it. */
7473 dump_alias_info (dump_file);
7476 return 0;
7479 /* For each pointer P_i, determine the sets of variables that P_i may
7480 point-to. Compute the reachability set of escaped and call-used
7481 variables. */
7482 compute_points_to_sets ();
7484 /* Debugging dumps. */
7485 if (dump_file)
7486 dump_alias_info (dump_file);
7488 /* Compute restrict-based memory disambiguations. */
7489 compute_dependence_clique ();
7491 /* Deallocate memory used by aliasing data structures and the internal
7492 points-to solution. */
7493 delete_points_to_sets ();
7495 gcc_assert (!need_ssa_update_p (cfun));
7497 return 0;
7500 /* A dummy pass to cause points-to information to be computed via
7501 TODO_rebuild_alias. */
7503 namespace {
7505 const pass_data pass_data_build_alias =
7507 GIMPLE_PASS, /* type */
7508 "alias", /* name */
7509 OPTGROUP_NONE, /* optinfo_flags */
7510 TV_NONE, /* tv_id */
7511 ( PROP_cfg | PROP_ssa ), /* properties_required */
7512 0, /* properties_provided */
7513 0, /* properties_destroyed */
7514 0, /* todo_flags_start */
7515 TODO_rebuild_alias, /* todo_flags_finish */
7518 class pass_build_alias : public gimple_opt_pass
7520 public:
7521 pass_build_alias (gcc::context *ctxt)
7522 : gimple_opt_pass (pass_data_build_alias, ctxt)
7525 /* opt_pass methods: */
7526 virtual bool gate (function *) { return flag_tree_pta; }
7528 }; // class pass_build_alias
7530 } // anon namespace
7532 gimple_opt_pass *
7533 make_pass_build_alias (gcc::context *ctxt)
7535 return new pass_build_alias (ctxt);
7538 /* A dummy pass to cause points-to information to be computed via
7539 TODO_rebuild_alias. */
7541 namespace {
7543 const pass_data pass_data_build_ealias =
7545 GIMPLE_PASS, /* type */
7546 "ealias", /* name */
7547 OPTGROUP_NONE, /* optinfo_flags */
7548 TV_NONE, /* tv_id */
7549 ( PROP_cfg | PROP_ssa ), /* properties_required */
7550 0, /* properties_provided */
7551 0, /* properties_destroyed */
7552 0, /* todo_flags_start */
7553 TODO_rebuild_alias, /* todo_flags_finish */
7556 class pass_build_ealias : public gimple_opt_pass
7558 public:
7559 pass_build_ealias (gcc::context *ctxt)
7560 : gimple_opt_pass (pass_data_build_ealias, ctxt)
7563 /* opt_pass methods: */
7564 virtual bool gate (function *) { return flag_tree_pta; }
7566 }; // class pass_build_ealias
7568 } // anon namespace
7570 gimple_opt_pass *
7571 make_pass_build_ealias (gcc::context *ctxt)
7573 return new pass_build_ealias (ctxt);
7577 /* IPA PTA solutions for ESCAPED. */
7578 struct pt_solution ipa_escaped_pt
7579 = { true, false, false, false, false, false, false, false, false, NULL };
7581 /* Associate node with varinfo DATA. Worker for
7582 cgraph_for_symbol_thunks_and_aliases. */
7583 static bool
7584 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
7586 if ((node->alias || node->thunk.thunk_p)
7587 && node->analyzed)
7588 insert_vi_for_tree (node->decl, (varinfo_t)data);
7589 return false;
7592 /* Dump varinfo VI to FILE. */
7594 static void
7595 dump_varinfo (FILE *file, varinfo_t vi)
7597 if (vi == NULL)
7598 return;
7600 fprintf (file, "%u: %s\n", vi->id, vi->name);
7602 const char *sep = " ";
7603 if (vi->is_artificial_var)
7604 fprintf (file, "%sartificial", sep);
7605 if (vi->is_special_var)
7606 fprintf (file, "%sspecial", sep);
7607 if (vi->is_unknown_size_var)
7608 fprintf (file, "%sunknown-size", sep);
7609 if (vi->is_full_var)
7610 fprintf (file, "%sfull", sep);
7611 if (vi->is_heap_var)
7612 fprintf (file, "%sheap", sep);
7613 if (vi->may_have_pointers)
7614 fprintf (file, "%smay-have-pointers", sep);
7615 if (vi->only_restrict_pointers)
7616 fprintf (file, "%sonly-restrict-pointers", sep);
7617 if (vi->is_restrict_var)
7618 fprintf (file, "%sis-restrict-var", sep);
7619 if (vi->is_global_var)
7620 fprintf (file, "%sglobal", sep);
7621 if (vi->is_ipa_escape_point)
7622 fprintf (file, "%sipa-escape-point", sep);
7623 if (vi->is_fn_info)
7624 fprintf (file, "%sfn-info", sep);
7625 if (vi->ruid)
7626 fprintf (file, "%srestrict-uid:%u", sep, vi->ruid);
7627 if (vi->next)
7628 fprintf (file, "%snext:%u", sep, vi->next);
7629 if (vi->head != vi->id)
7630 fprintf (file, "%shead:%u", sep, vi->head);
7631 if (vi->offset)
7632 fprintf (file, "%soffset:" HOST_WIDE_INT_PRINT_DEC, sep, vi->offset);
7633 if (vi->size != ~(unsigned HOST_WIDE_INT)0)
7634 fprintf (file, "%ssize:" HOST_WIDE_INT_PRINT_DEC, sep, vi->size);
7635 if (vi->fullsize != ~(unsigned HOST_WIDE_INT)0
7636 && vi->fullsize != vi->size)
7637 fprintf (file, "%sfullsize:" HOST_WIDE_INT_PRINT_DEC, sep,
7638 vi->fullsize);
7639 fprintf (file, "\n");
7641 if (vi->solution && !bitmap_empty_p (vi->solution))
7643 bitmap_iterator bi;
7644 unsigned i;
7645 fprintf (file, " solution: {");
7646 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
7647 fprintf (file, " %u", i);
7648 fprintf (file, " }\n");
7651 if (vi->oldsolution && !bitmap_empty_p (vi->oldsolution)
7652 && !bitmap_equal_p (vi->solution, vi->oldsolution))
7654 bitmap_iterator bi;
7655 unsigned i;
7656 fprintf (file, " oldsolution: {");
7657 EXECUTE_IF_SET_IN_BITMAP (vi->oldsolution, 0, i, bi)
7658 fprintf (file, " %u", i);
7659 fprintf (file, " }\n");
7663 /* Dump varinfo VI to stderr. */
7665 DEBUG_FUNCTION void
7666 debug_varinfo (varinfo_t vi)
7668 dump_varinfo (stderr, vi);
7671 /* Dump varmap to FILE. */
7673 static void
7674 dump_varmap (FILE *file)
7676 if (varmap.length () == 0)
7677 return;
7679 fprintf (file, "variables:\n");
7681 for (unsigned int i = 0; i < varmap.length (); ++i)
7683 varinfo_t vi = get_varinfo (i);
7684 dump_varinfo (file, vi);
7687 fprintf (file, "\n");
7690 /* Dump varmap to stderr. */
7692 DEBUG_FUNCTION void
7693 debug_varmap (void)
7695 dump_varmap (stderr);
7698 /* Compute whether node is refered to non-locally. Worker for
7699 cgraph_for_symbol_thunks_and_aliases. */
7700 static bool
7701 refered_from_nonlocal_fn (struct cgraph_node *node, void *data)
7703 bool *nonlocal_p = (bool *)data;
7704 *nonlocal_p |= (node->used_from_other_partition
7705 || node->externally_visible
7706 || node->force_output);
7707 return false;
7710 /* Same for varpool nodes. */
7711 static bool
7712 refered_from_nonlocal_var (struct varpool_node *node, void *data)
7714 bool *nonlocal_p = (bool *)data;
7715 *nonlocal_p |= (node->used_from_other_partition
7716 || node->externally_visible
7717 || node->force_output);
7718 return false;
7721 /* Execute the driver for IPA PTA. */
7722 static unsigned int
7723 ipa_pta_execute (void)
7725 struct cgraph_node *node;
7726 varpool_node *var;
7727 unsigned int from = 0;
7729 in_ipa_mode = 1;
7731 init_alias_vars ();
7733 if (dump_file && (dump_flags & TDF_DETAILS))
7735 symtab_node::dump_table (dump_file);
7736 fprintf (dump_file, "\n");
7739 if (dump_file)
7741 fprintf (dump_file, "Generating generic constraints\n\n");
7742 dump_constraints (dump_file, from);
7743 fprintf (dump_file, "\n");
7744 from = constraints.length ();
7747 /* Build the constraints. */
7748 FOR_EACH_DEFINED_FUNCTION (node)
7750 varinfo_t vi;
7751 /* Nodes without a body are not interesting. Especially do not
7752 visit clones at this point for now - we get duplicate decls
7753 there for inline clones at least. */
7754 if (!node->has_gimple_body_p () || node->global.inlined_to)
7755 continue;
7756 node->get_body ();
7758 gcc_assert (!node->clone_of);
7760 /* For externally visible or attribute used annotated functions use
7761 local constraints for their arguments.
7762 For local functions we see all callers and thus do not need initial
7763 constraints for parameters. */
7764 bool nonlocal_p = (node->used_from_other_partition
7765 || node->externally_visible
7766 || node->force_output);
7767 node->call_for_symbol_thunks_and_aliases (refered_from_nonlocal_fn,
7768 &nonlocal_p, true);
7770 vi = create_function_info_for (node->decl,
7771 alias_get_name (node->decl), false,
7772 nonlocal_p);
7773 if (dump_file
7774 && from != constraints.length ())
7776 fprintf (dump_file,
7777 "Generating intial constraints for %s", node->name ());
7778 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
7779 fprintf (dump_file, " (%s)",
7780 IDENTIFIER_POINTER
7781 (DECL_ASSEMBLER_NAME (node->decl)));
7782 fprintf (dump_file, "\n\n");
7783 dump_constraints (dump_file, from);
7784 fprintf (dump_file, "\n");
7786 from = constraints.length ();
7789 node->call_for_symbol_thunks_and_aliases
7790 (associate_varinfo_to_alias, vi, true);
7793 /* Create constraints for global variables and their initializers. */
7794 FOR_EACH_VARIABLE (var)
7796 if (var->alias && var->analyzed)
7797 continue;
7799 varinfo_t vi = get_vi_for_tree (var->decl);
7801 /* For the purpose of IPA PTA unit-local globals are not
7802 escape points. */
7803 bool nonlocal_p = (var->used_from_other_partition
7804 || var->externally_visible
7805 || var->force_output);
7806 var->call_for_symbol_and_aliases (refered_from_nonlocal_var,
7807 &nonlocal_p, true);
7808 if (nonlocal_p)
7809 vi->is_ipa_escape_point = true;
7812 if (dump_file
7813 && from != constraints.length ())
7815 fprintf (dump_file,
7816 "Generating constraints for global initializers\n\n");
7817 dump_constraints (dump_file, from);
7818 fprintf (dump_file, "\n");
7819 from = constraints.length ();
7822 FOR_EACH_DEFINED_FUNCTION (node)
7824 struct function *func;
7825 basic_block bb;
7827 /* Nodes without a body are not interesting. */
7828 if (!node->has_gimple_body_p () || node->clone_of)
7829 continue;
7831 if (dump_file)
7833 fprintf (dump_file,
7834 "Generating constraints for %s", node->name ());
7835 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
7836 fprintf (dump_file, " (%s)",
7837 IDENTIFIER_POINTER
7838 (DECL_ASSEMBLER_NAME (node->decl)));
7839 fprintf (dump_file, "\n");
7842 func = DECL_STRUCT_FUNCTION (node->decl);
7843 gcc_assert (cfun == NULL);
7845 /* Build constriants for the function body. */
7846 FOR_EACH_BB_FN (bb, func)
7848 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7849 gsi_next (&gsi))
7851 gphi *phi = gsi.phi ();
7853 if (! virtual_operand_p (gimple_phi_result (phi)))
7854 find_func_aliases (func, phi);
7857 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
7858 gsi_next (&gsi))
7860 gimple *stmt = gsi_stmt (gsi);
7862 find_func_aliases (func, stmt);
7863 find_func_clobbers (func, stmt);
7867 if (dump_file)
7869 fprintf (dump_file, "\n");
7870 dump_constraints (dump_file, from);
7871 fprintf (dump_file, "\n");
7872 from = constraints.length ();
7876 /* From the constraints compute the points-to sets. */
7877 solve_constraints ();
7879 /* Compute the global points-to sets for ESCAPED.
7880 ??? Note that the computed escape set is not correct
7881 for the whole unit as we fail to consider graph edges to
7882 externally visible functions. */
7883 ipa_escaped_pt = find_what_var_points_to (NULL, get_varinfo (escaped_id));
7885 /* Make sure the ESCAPED solution (which is used as placeholder in
7886 other solutions) does not reference itself. This simplifies
7887 points-to solution queries. */
7888 ipa_escaped_pt.ipa_escaped = 0;
7890 /* Assign the points-to sets to the SSA names in the unit. */
7891 FOR_EACH_DEFINED_FUNCTION (node)
7893 tree ptr;
7894 struct function *fn;
7895 unsigned i;
7896 basic_block bb;
7898 /* Nodes without a body are not interesting. */
7899 if (!node->has_gimple_body_p () || node->clone_of)
7900 continue;
7902 fn = DECL_STRUCT_FUNCTION (node->decl);
7904 /* Compute the points-to sets for pointer SSA_NAMEs. */
7905 FOR_EACH_VEC_ELT (*fn->gimple_df->ssa_names, i, ptr)
7907 if (ptr
7908 && POINTER_TYPE_P (TREE_TYPE (ptr)))
7909 find_what_p_points_to (node->decl, ptr);
7912 /* Compute the call-use and call-clobber sets for indirect calls
7913 and calls to external functions. */
7914 FOR_EACH_BB_FN (bb, fn)
7916 gimple_stmt_iterator gsi;
7918 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7920 gcall *stmt;
7921 struct pt_solution *pt;
7922 varinfo_t vi, fi;
7923 tree decl;
7925 stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
7926 if (!stmt)
7927 continue;
7929 /* Handle direct calls to functions with body. */
7930 decl = gimple_call_fndecl (stmt);
7933 tree called_decl = NULL_TREE;
7934 if (gimple_call_builtin_p (stmt, BUILT_IN_GOMP_PARALLEL))
7935 called_decl = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
7936 else if (gimple_call_builtin_p (stmt, BUILT_IN_GOACC_PARALLEL))
7937 called_decl = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
7939 if (called_decl != NULL_TREE
7940 && !fndecl_maybe_in_other_partition (called_decl))
7941 decl = called_decl;
7944 if (decl
7945 && (fi = lookup_vi_for_tree (decl))
7946 && fi->is_fn_info)
7948 *gimple_call_clobber_set (stmt)
7949 = find_what_var_points_to
7950 (node->decl, first_vi_for_offset (fi, fi_clobbers));
7951 *gimple_call_use_set (stmt)
7952 = find_what_var_points_to
7953 (node->decl, first_vi_for_offset (fi, fi_uses));
7955 /* Handle direct calls to external functions. */
7956 else if (decl)
7958 pt = gimple_call_use_set (stmt);
7959 if (gimple_call_flags (stmt) & ECF_CONST)
7960 memset (pt, 0, sizeof (struct pt_solution));
7961 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7963 *pt = find_what_var_points_to (node->decl, vi);
7964 /* Escaped (and thus nonlocal) variables are always
7965 implicitly used by calls. */
7966 /* ??? ESCAPED can be empty even though NONLOCAL
7967 always escaped. */
7968 pt->nonlocal = 1;
7969 pt->ipa_escaped = 1;
7971 else
7973 /* If there is nothing special about this call then
7974 we have made everything that is used also escape. */
7975 *pt = ipa_escaped_pt;
7976 pt->nonlocal = 1;
7979 pt = gimple_call_clobber_set (stmt);
7980 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7981 memset (pt, 0, sizeof (struct pt_solution));
7982 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7984 *pt = find_what_var_points_to (node->decl, vi);
7985 /* Escaped (and thus nonlocal) variables are always
7986 implicitly clobbered by calls. */
7987 /* ??? ESCAPED can be empty even though NONLOCAL
7988 always escaped. */
7989 pt->nonlocal = 1;
7990 pt->ipa_escaped = 1;
7992 else
7994 /* If there is nothing special about this call then
7995 we have made everything that is used also escape. */
7996 *pt = ipa_escaped_pt;
7997 pt->nonlocal = 1;
8000 /* Handle indirect calls. */
8001 else if (!decl
8002 && (fi = get_fi_for_callee (stmt)))
8004 /* We need to accumulate all clobbers/uses of all possible
8005 callees. */
8006 fi = get_varinfo (find (fi->id));
8007 /* If we cannot constrain the set of functions we'll end up
8008 calling we end up using/clobbering everything. */
8009 if (bitmap_bit_p (fi->solution, anything_id)
8010 || bitmap_bit_p (fi->solution, nonlocal_id)
8011 || bitmap_bit_p (fi->solution, escaped_id))
8013 pt_solution_reset (gimple_call_clobber_set (stmt));
8014 pt_solution_reset (gimple_call_use_set (stmt));
8016 else
8018 bitmap_iterator bi;
8019 unsigned i;
8020 struct pt_solution *uses, *clobbers;
8022 uses = gimple_call_use_set (stmt);
8023 clobbers = gimple_call_clobber_set (stmt);
8024 memset (uses, 0, sizeof (struct pt_solution));
8025 memset (clobbers, 0, sizeof (struct pt_solution));
8026 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
8028 struct pt_solution sol;
8030 vi = get_varinfo (i);
8031 if (!vi->is_fn_info)
8033 /* ??? We could be more precise here? */
8034 uses->nonlocal = 1;
8035 uses->ipa_escaped = 1;
8036 clobbers->nonlocal = 1;
8037 clobbers->ipa_escaped = 1;
8038 continue;
8041 if (!uses->anything)
8043 sol = find_what_var_points_to
8044 (node->decl,
8045 first_vi_for_offset (vi, fi_uses));
8046 pt_solution_ior_into (uses, &sol);
8048 if (!clobbers->anything)
8050 sol = find_what_var_points_to
8051 (node->decl,
8052 first_vi_for_offset (vi, fi_clobbers));
8053 pt_solution_ior_into (clobbers, &sol);
8061 fn->gimple_df->ipa_pta = true;
8063 /* We have to re-set the final-solution cache after each function
8064 because what is a "global" is dependent on function context. */
8065 final_solutions->empty ();
8066 obstack_free (&final_solutions_obstack, NULL);
8067 gcc_obstack_init (&final_solutions_obstack);
8070 delete_points_to_sets ();
8072 in_ipa_mode = 0;
8074 return 0;
8077 namespace {
8079 const pass_data pass_data_ipa_pta =
8081 SIMPLE_IPA_PASS, /* type */
8082 "pta", /* name */
8083 OPTGROUP_NONE, /* optinfo_flags */
8084 TV_IPA_PTA, /* tv_id */
8085 0, /* properties_required */
8086 0, /* properties_provided */
8087 0, /* properties_destroyed */
8088 0, /* todo_flags_start */
8089 0, /* todo_flags_finish */
8092 class pass_ipa_pta : public simple_ipa_opt_pass
8094 public:
8095 pass_ipa_pta (gcc::context *ctxt)
8096 : simple_ipa_opt_pass (pass_data_ipa_pta, ctxt)
8099 /* opt_pass methods: */
8100 virtual bool gate (function *)
8102 return (optimize
8103 && flag_ipa_pta
8104 /* Don't bother doing anything if the program has errors. */
8105 && !seen_error ());
8108 opt_pass * clone () { return new pass_ipa_pta (m_ctxt); }
8110 virtual unsigned int execute (function *) { return ipa_pta_execute (); }
8112 }; // class pass_ipa_pta
8114 } // anon namespace
8116 simple_ipa_opt_pass *
8117 make_pass_ipa_pta (gcc::context *ctxt)
8119 return new pass_ipa_pta (ctxt);