2013-01-12 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / tree-ssa-structalias.c
blob4ad956f66491ebd80602e8d920e83bab04b2f1c6
1 /* Tree based points-to analysis
2 Copyright (C) 2005-2013 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dberlin@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "obstack.h"
27 #include "bitmap.h"
28 #include "flags.h"
29 #include "basic-block.h"
30 #include "tree.h"
31 #include "tree-flow.h"
32 #include "tree-inline.h"
33 #include "diagnostic-core.h"
34 #include "gimple.h"
35 #include "hashtab.h"
36 #include "function.h"
37 #include "cgraph.h"
38 #include "tree-pass.h"
39 #include "alloc-pool.h"
40 #include "splay-tree.h"
41 #include "params.h"
42 #include "cgraph.h"
43 #include "alias.h"
44 #include "pointer-set.h"
46 /* The idea behind this analyzer is to generate set constraints from the
47 program, then solve the resulting constraints in order to generate the
48 points-to sets.
50 Set constraints are a way of modeling program analysis problems that
51 involve sets. They consist of an inclusion constraint language,
52 describing the variables (each variable is a set) and operations that
53 are involved on the variables, and a set of rules that derive facts
54 from these operations. To solve a system of set constraints, you derive
55 all possible facts under the rules, which gives you the correct sets
56 as a consequence.
58 See "Efficient Field-sensitive pointer analysis for C" by "David
59 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
60 http://citeseer.ist.psu.edu/pearce04efficient.html
62 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
63 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
64 http://citeseer.ist.psu.edu/heintze01ultrafast.html
66 There are three types of real constraint expressions, DEREF,
67 ADDRESSOF, and SCALAR. Each constraint expression consists
68 of a constraint type, a variable, and an offset.
70 SCALAR is a constraint expression type used to represent x, whether
71 it appears on the LHS or the RHS of a statement.
72 DEREF is a constraint expression type used to represent *x, whether
73 it appears on the LHS or the RHS of a statement.
74 ADDRESSOF is a constraint expression used to represent &x, whether
75 it appears on the LHS or the RHS of a statement.
77 Each pointer variable in the program is assigned an integer id, and
78 each field of a structure variable is assigned an integer id as well.
80 Structure variables are linked to their list of fields through a "next
81 field" in each variable that points to the next field in offset
82 order.
83 Each variable for a structure field has
85 1. "size", that tells the size in bits of that field.
86 2. "fullsize, that tells the size in bits of the entire structure.
87 3. "offset", that tells the offset in bits from the beginning of the
88 structure to this field.
90 Thus,
91 struct f
93 int a;
94 int b;
95 } foo;
96 int *bar;
98 looks like
100 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
101 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
102 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
105 In order to solve the system of set constraints, the following is
106 done:
108 1. Each constraint variable x has a solution set associated with it,
109 Sol(x).
111 2. Constraints are separated into direct, copy, and complex.
112 Direct constraints are ADDRESSOF constraints that require no extra
113 processing, such as P = &Q
114 Copy constraints are those of the form P = Q.
115 Complex constraints are all the constraints involving dereferences
116 and offsets (including offsetted copies).
118 3. All direct constraints of the form P = &Q are processed, such
119 that Q is added to Sol(P)
121 4. All complex constraints for a given constraint variable are stored in a
122 linked list attached to that variable's node.
124 5. A directed graph is built out of the copy constraints. Each
125 constraint variable is a node in the graph, and an edge from
126 Q to P is added for each copy constraint of the form P = Q
128 6. The graph is then walked, and solution sets are
129 propagated along the copy edges, such that an edge from Q to P
130 causes Sol(P) <- Sol(P) union Sol(Q).
132 7. As we visit each node, all complex constraints associated with
133 that node are processed by adding appropriate copy edges to the graph, or the
134 appropriate variables to the solution set.
136 8. The process of walking the graph is iterated until no solution
137 sets change.
139 Prior to walking the graph in steps 6 and 7, We perform static
140 cycle elimination on the constraint graph, as well
141 as off-line variable substitution.
143 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
144 on and turned into anything), but isn't. You can just see what offset
145 inside the pointed-to struct it's going to access.
147 TODO: Constant bounded arrays can be handled as if they were structs of the
148 same number of elements.
150 TODO: Modeling heap and incoming pointers becomes much better if we
151 add fields to them as we discover them, which we could do.
153 TODO: We could handle unions, but to be honest, it's probably not
154 worth the pain or slowdown. */
156 /* IPA-PTA optimizations possible.
158 When the indirect function called is ANYTHING we can add disambiguation
159 based on the function signatures (or simply the parameter count which
160 is the varinfo size). We also do not need to consider functions that
161 do not have their address taken.
163 The is_global_var bit which marks escape points is overly conservative
164 in IPA mode. Split it to is_escape_point and is_global_var - only
165 externally visible globals are escape points in IPA mode. This is
166 also needed to fix the pt_solution_includes_global predicate
167 (and thus ptr_deref_may_alias_global_p).
169 The way we introduce DECL_PT_UID to avoid fixing up all points-to
170 sets in the translation unit when we copy a DECL during inlining
171 pessimizes precision. The advantage is that the DECL_PT_UID keeps
172 compile-time and memory usage overhead low - the points-to sets
173 do not grow or get unshared as they would during a fixup phase.
174 An alternative solution is to delay IPA PTA until after all
175 inlining transformations have been applied.
177 The way we propagate clobber/use information isn't optimized.
178 It should use a new complex constraint that properly filters
179 out local variables of the callee (though that would make
180 the sets invalid after inlining). OTOH we might as well
181 admit defeat to WHOPR and simply do all the clobber/use analysis
182 and propagation after PTA finished but before we threw away
183 points-to information for memory variables. WHOPR and PTA
184 do not play along well anyway - the whole constraint solving
185 would need to be done in WPA phase and it will be very interesting
186 to apply the results to local SSA names during LTRANS phase.
188 We probably should compute a per-function unit-ESCAPE solution
189 propagating it simply like the clobber / uses solutions. The
190 solution can go alongside the non-IPA espaced solution and be
191 used to query which vars escape the unit through a function.
193 We never put function decls in points-to sets so we do not
194 keep the set of called functions for indirect calls.
196 And probably more. */
198 static bool use_field_sensitive = true;
199 static int in_ipa_mode = 0;
201 /* Used for predecessor bitmaps. */
202 static bitmap_obstack predbitmap_obstack;
204 /* Used for points-to sets. */
205 static bitmap_obstack pta_obstack;
207 /* Used for oldsolution members of variables. */
208 static bitmap_obstack oldpta_obstack;
210 /* Used for per-solver-iteration bitmaps. */
211 static bitmap_obstack iteration_obstack;
213 static unsigned int create_variable_info_for (tree, const char *);
214 typedef struct constraint_graph *constraint_graph_t;
215 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
217 struct constraint;
218 typedef struct constraint *constraint_t;
221 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
222 if (a) \
223 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
225 static struct constraint_stats
227 unsigned int total_vars;
228 unsigned int nonpointer_vars;
229 unsigned int unified_vars_static;
230 unsigned int unified_vars_dynamic;
231 unsigned int iterations;
232 unsigned int num_edges;
233 unsigned int num_implicit_edges;
234 unsigned int points_to_sets_created;
235 } stats;
237 struct variable_info
239 /* ID of this variable */
240 unsigned int id;
242 /* True if this is a variable created by the constraint analysis, such as
243 heap variables and constraints we had to break up. */
244 unsigned int is_artificial_var : 1;
246 /* True if this is a special variable whose solution set should not be
247 changed. */
248 unsigned int is_special_var : 1;
250 /* True for variables whose size is not known or variable. */
251 unsigned int is_unknown_size_var : 1;
253 /* True for (sub-)fields that represent a whole variable. */
254 unsigned int is_full_var : 1;
256 /* True if this is a heap variable. */
257 unsigned int is_heap_var : 1;
259 /* True if this field may contain pointers. */
260 unsigned int may_have_pointers : 1;
262 /* True if this field has only restrict qualified pointers. */
263 unsigned int only_restrict_pointers : 1;
265 /* True if this represents a global variable. */
266 unsigned int is_global_var : 1;
268 /* True if this represents a IPA function info. */
269 unsigned int is_fn_info : 1;
271 /* A link to the variable for the next field in this structure. */
272 struct variable_info *next;
274 /* Offset of this variable, in bits, from the base variable */
275 unsigned HOST_WIDE_INT offset;
277 /* Size of the variable, in bits. */
278 unsigned HOST_WIDE_INT size;
280 /* Full size of the base variable, in bits. */
281 unsigned HOST_WIDE_INT fullsize;
283 /* Name of this variable */
284 const char *name;
286 /* Tree that this variable is associated with. */
287 tree decl;
289 /* Points-to set for this variable. */
290 bitmap solution;
292 /* Old points-to set for this variable. */
293 bitmap oldsolution;
295 typedef struct variable_info *varinfo_t;
297 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
298 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
299 unsigned HOST_WIDE_INT);
300 static varinfo_t lookup_vi_for_tree (tree);
301 static inline bool type_can_have_subvars (const_tree);
303 /* Pool of variable info structures. */
304 static alloc_pool variable_info_pool;
308 /* Table of variable info structures for constraint variables.
309 Indexed directly by variable info id. */
310 static vec<varinfo_t> varmap;
312 /* Return the varmap element N */
314 static inline varinfo_t
315 get_varinfo (unsigned int n)
317 return varmap[n];
320 /* Static IDs for the special variables. */
321 enum { nothing_id = 0, anything_id = 1, readonly_id = 2,
322 escaped_id = 3, nonlocal_id = 4,
323 storedanything_id = 5, integer_id = 6 };
325 /* Return a new variable info structure consisting for a variable
326 named NAME, and using constraint graph node NODE. Append it
327 to the vector of variable info structures. */
329 static varinfo_t
330 new_var_info (tree t, const char *name)
332 unsigned index = varmap.length ();
333 varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
335 ret->id = index;
336 ret->name = name;
337 ret->decl = t;
338 /* Vars without decl are artificial and do not have sub-variables. */
339 ret->is_artificial_var = (t == NULL_TREE);
340 ret->is_special_var = false;
341 ret->is_unknown_size_var = false;
342 ret->is_full_var = (t == NULL_TREE);
343 ret->is_heap_var = false;
344 ret->may_have_pointers = true;
345 ret->only_restrict_pointers = false;
346 ret->is_global_var = (t == NULL_TREE);
347 ret->is_fn_info = false;
348 if (t && DECL_P (t))
349 ret->is_global_var = (is_global_var (t)
350 /* We have to treat even local register variables
351 as escape points. */
352 || (TREE_CODE (t) == VAR_DECL
353 && DECL_HARD_REGISTER (t)));
354 ret->solution = BITMAP_ALLOC (&pta_obstack);
355 ret->oldsolution = NULL;
356 ret->next = NULL;
358 stats.total_vars++;
360 varmap.safe_push (ret);
362 return ret;
366 /* A map mapping call statements to per-stmt variables for uses
367 and clobbers specific to the call. */
368 struct pointer_map_t *call_stmt_vars;
370 /* Lookup or create the variable for the call statement CALL. */
372 static varinfo_t
373 get_call_vi (gimple call)
375 void **slot_p;
376 varinfo_t vi, vi2;
378 slot_p = pointer_map_insert (call_stmt_vars, call);
379 if (*slot_p)
380 return (varinfo_t) *slot_p;
382 vi = new_var_info (NULL_TREE, "CALLUSED");
383 vi->offset = 0;
384 vi->size = 1;
385 vi->fullsize = 2;
386 vi->is_full_var = true;
388 vi->next = vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED");
389 vi2->offset = 1;
390 vi2->size = 1;
391 vi2->fullsize = 2;
392 vi2->is_full_var = true;
394 *slot_p = (void *) vi;
395 return vi;
398 /* Lookup the variable for the call statement CALL representing
399 the uses. Returns NULL if there is nothing special about this call. */
401 static varinfo_t
402 lookup_call_use_vi (gimple call)
404 void **slot_p;
406 slot_p = pointer_map_contains (call_stmt_vars, call);
407 if (slot_p)
408 return (varinfo_t) *slot_p;
410 return NULL;
413 /* Lookup the variable for the call statement CALL representing
414 the clobbers. Returns NULL if there is nothing special about this call. */
416 static varinfo_t
417 lookup_call_clobber_vi (gimple call)
419 varinfo_t uses = lookup_call_use_vi (call);
420 if (!uses)
421 return NULL;
423 return uses->next;
426 /* Lookup or create the variable for the call statement CALL representing
427 the uses. */
429 static varinfo_t
430 get_call_use_vi (gimple call)
432 return get_call_vi (call);
435 /* Lookup or create the variable for the call statement CALL representing
436 the clobbers. */
438 static varinfo_t ATTRIBUTE_UNUSED
439 get_call_clobber_vi (gimple call)
441 return get_call_vi (call)->next;
445 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
447 /* An expression that appears in a constraint. */
449 struct constraint_expr
451 /* Constraint type. */
452 constraint_expr_type type;
454 /* Variable we are referring to in the constraint. */
455 unsigned int var;
457 /* Offset, in bits, of this constraint from the beginning of
458 variables it ends up referring to.
460 IOW, in a deref constraint, we would deref, get the result set,
461 then add OFFSET to each member. */
462 HOST_WIDE_INT offset;
465 /* Use 0x8000... as special unknown offset. */
466 #define UNKNOWN_OFFSET ((HOST_WIDE_INT)-1 << (HOST_BITS_PER_WIDE_INT-1))
468 typedef struct constraint_expr ce_s;
469 static void get_constraint_for_1 (tree, vec<ce_s> *, bool, bool);
470 static void get_constraint_for (tree, vec<ce_s> *);
471 static void get_constraint_for_rhs (tree, vec<ce_s> *);
472 static void do_deref (vec<ce_s> *);
474 /* Our set constraints are made up of two constraint expressions, one
475 LHS, and one RHS.
477 As described in the introduction, our set constraints each represent an
478 operation between set valued variables.
480 struct constraint
482 struct constraint_expr lhs;
483 struct constraint_expr rhs;
486 /* List of constraints that we use to build the constraint graph from. */
488 static vec<constraint_t> constraints;
489 static alloc_pool constraint_pool;
491 /* The constraint graph is represented as an array of bitmaps
492 containing successor nodes. */
494 struct constraint_graph
496 /* Size of this graph, which may be different than the number of
497 nodes in the variable map. */
498 unsigned int size;
500 /* Explicit successors of each node. */
501 bitmap *succs;
503 /* Implicit predecessors of each node (Used for variable
504 substitution). */
505 bitmap *implicit_preds;
507 /* Explicit predecessors of each node (Used for variable substitution). */
508 bitmap *preds;
510 /* Indirect cycle representatives, or -1 if the node has no indirect
511 cycles. */
512 int *indirect_cycles;
514 /* Representative node for a node. rep[a] == a unless the node has
515 been unified. */
516 unsigned int *rep;
518 /* Equivalence class representative for a label. This is used for
519 variable substitution. */
520 int *eq_rep;
522 /* Pointer equivalence label for a node. All nodes with the same
523 pointer equivalence label can be unified together at some point
524 (either during constraint optimization or after the constraint
525 graph is built). */
526 unsigned int *pe;
528 /* Pointer equivalence representative for a label. This is used to
529 handle nodes that are pointer equivalent but not location
530 equivalent. We can unite these once the addressof constraints
531 are transformed into initial points-to sets. */
532 int *pe_rep;
534 /* Pointer equivalence label for each node, used during variable
535 substitution. */
536 unsigned int *pointer_label;
538 /* Location equivalence label for each node, used during location
539 equivalence finding. */
540 unsigned int *loc_label;
542 /* Pointed-by set for each node, used during location equivalence
543 finding. This is pointed-by rather than pointed-to, because it
544 is constructed using the predecessor graph. */
545 bitmap *pointed_by;
547 /* Points to sets for pointer equivalence. This is *not* the actual
548 points-to sets for nodes. */
549 bitmap *points_to;
551 /* Bitmap of nodes where the bit is set if the node is a direct
552 node. Used for variable substitution. */
553 sbitmap direct_nodes;
555 /* Bitmap of nodes where the bit is set if the node is address
556 taken. Used for variable substitution. */
557 bitmap address_taken;
559 /* Vector of complex constraints for each graph node. Complex
560 constraints are those involving dereferences or offsets that are
561 not 0. */
562 vec<constraint_t> *complex;
565 static constraint_graph_t graph;
567 /* During variable substitution and the offline version of indirect
568 cycle finding, we create nodes to represent dereferences and
569 address taken constraints. These represent where these start and
570 end. */
571 #define FIRST_REF_NODE (varmap).length ()
572 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
574 /* Return the representative node for NODE, if NODE has been unioned
575 with another NODE.
576 This function performs path compression along the way to finding
577 the representative. */
579 static unsigned int
580 find (unsigned int node)
582 gcc_assert (node < graph->size);
583 if (graph->rep[node] != node)
584 return graph->rep[node] = find (graph->rep[node]);
585 return node;
588 /* Union the TO and FROM nodes to the TO nodes.
589 Note that at some point in the future, we may want to do
590 union-by-rank, in which case we are going to have to return the
591 node we unified to. */
593 static bool
594 unite (unsigned int to, unsigned int from)
596 gcc_assert (to < graph->size && from < graph->size);
597 if (to != from && graph->rep[from] != to)
599 graph->rep[from] = to;
600 return true;
602 return false;
605 /* Create a new constraint consisting of LHS and RHS expressions. */
607 static constraint_t
608 new_constraint (const struct constraint_expr lhs,
609 const struct constraint_expr rhs)
611 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
612 ret->lhs = lhs;
613 ret->rhs = rhs;
614 return ret;
617 /* Print out constraint C to FILE. */
619 static void
620 dump_constraint (FILE *file, constraint_t c)
622 if (c->lhs.type == ADDRESSOF)
623 fprintf (file, "&");
624 else if (c->lhs.type == DEREF)
625 fprintf (file, "*");
626 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
627 if (c->lhs.offset == UNKNOWN_OFFSET)
628 fprintf (file, " + UNKNOWN");
629 else if (c->lhs.offset != 0)
630 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
631 fprintf (file, " = ");
632 if (c->rhs.type == ADDRESSOF)
633 fprintf (file, "&");
634 else if (c->rhs.type == DEREF)
635 fprintf (file, "*");
636 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
637 if (c->rhs.offset == UNKNOWN_OFFSET)
638 fprintf (file, " + UNKNOWN");
639 else if (c->rhs.offset != 0)
640 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
644 void debug_constraint (constraint_t);
645 void debug_constraints (void);
646 void debug_constraint_graph (void);
647 void debug_solution_for_var (unsigned int);
648 void debug_sa_points_to_info (void);
650 /* Print out constraint C to stderr. */
652 DEBUG_FUNCTION void
653 debug_constraint (constraint_t c)
655 dump_constraint (stderr, c);
656 fprintf (stderr, "\n");
659 /* Print out all constraints to FILE */
661 static void
662 dump_constraints (FILE *file, int from)
664 int i;
665 constraint_t c;
666 for (i = from; constraints.iterate (i, &c); i++)
667 if (c)
669 dump_constraint (file, c);
670 fprintf (file, "\n");
674 /* Print out all constraints to stderr. */
676 DEBUG_FUNCTION void
677 debug_constraints (void)
679 dump_constraints (stderr, 0);
682 /* Print the constraint graph in dot format. */
684 static void
685 dump_constraint_graph (FILE *file)
687 unsigned int i;
689 /* Only print the graph if it has already been initialized: */
690 if (!graph)
691 return;
693 /* Prints the header of the dot file: */
694 fprintf (file, "strict digraph {\n");
695 fprintf (file, " node [\n shape = box\n ]\n");
696 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
697 fprintf (file, "\n // List of nodes and complex constraints in "
698 "the constraint graph:\n");
700 /* The next lines print the nodes in the graph together with the
701 complex constraints attached to them. */
702 for (i = 0; i < graph->size; i++)
704 if (find (i) != i)
705 continue;
706 if (i < FIRST_REF_NODE)
707 fprintf (file, "\"%s\"", get_varinfo (i)->name);
708 else
709 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
710 if (graph->complex[i].exists ())
712 unsigned j;
713 constraint_t c;
714 fprintf (file, " [label=\"\\N\\n");
715 for (j = 0; graph->complex[i].iterate (j, &c); ++j)
717 dump_constraint (file, c);
718 fprintf (file, "\\l");
720 fprintf (file, "\"]");
722 fprintf (file, ";\n");
725 /* Go over the edges. */
726 fprintf (file, "\n // Edges in the constraint graph:\n");
727 for (i = 0; i < graph->size; i++)
729 unsigned j;
730 bitmap_iterator bi;
731 if (find (i) != i)
732 continue;
733 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
735 unsigned to = find (j);
736 if (i == to)
737 continue;
738 if (i < FIRST_REF_NODE)
739 fprintf (file, "\"%s\"", get_varinfo (i)->name);
740 else
741 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
742 fprintf (file, " -> ");
743 if (to < FIRST_REF_NODE)
744 fprintf (file, "\"%s\"", get_varinfo (to)->name);
745 else
746 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name);
747 fprintf (file, ";\n");
751 /* Prints the tail of the dot file. */
752 fprintf (file, "}\n");
755 /* Print out the constraint graph to stderr. */
757 DEBUG_FUNCTION void
758 debug_constraint_graph (void)
760 dump_constraint_graph (stderr);
763 /* SOLVER FUNCTIONS
765 The solver is a simple worklist solver, that works on the following
766 algorithm:
768 sbitmap changed_nodes = all zeroes;
769 changed_count = 0;
770 For each node that is not already collapsed:
771 changed_count++;
772 set bit in changed nodes
774 while (changed_count > 0)
776 compute topological ordering for constraint graph
778 find and collapse cycles in the constraint graph (updating
779 changed if necessary)
781 for each node (n) in the graph in topological order:
782 changed_count--;
784 Process each complex constraint associated with the node,
785 updating changed if necessary.
787 For each outgoing edge from n, propagate the solution from n to
788 the destination of the edge, updating changed as necessary.
790 } */
792 /* Return true if two constraint expressions A and B are equal. */
794 static bool
795 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
797 return a.type == b.type && a.var == b.var && a.offset == b.offset;
800 /* Return true if constraint expression A is less than constraint expression
801 B. This is just arbitrary, but consistent, in order to give them an
802 ordering. */
804 static bool
805 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
807 if (a.type == b.type)
809 if (a.var == b.var)
810 return a.offset < b.offset;
811 else
812 return a.var < b.var;
814 else
815 return a.type < b.type;
818 /* Return true if constraint A is less than constraint B. This is just
819 arbitrary, but consistent, in order to give them an ordering. */
821 static bool
822 constraint_less (const constraint_t &a, const constraint_t &b)
824 if (constraint_expr_less (a->lhs, b->lhs))
825 return true;
826 else if (constraint_expr_less (b->lhs, a->lhs))
827 return false;
828 else
829 return constraint_expr_less (a->rhs, b->rhs);
832 /* Return true if two constraints A and B are equal. */
834 static bool
835 constraint_equal (struct constraint a, struct constraint b)
837 return constraint_expr_equal (a.lhs, b.lhs)
838 && constraint_expr_equal (a.rhs, b.rhs);
842 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
844 static constraint_t
845 constraint_vec_find (vec<constraint_t> vec,
846 struct constraint lookfor)
848 unsigned int place;
849 constraint_t found;
851 if (!vec.exists ())
852 return NULL;
854 place = vec.lower_bound (&lookfor, constraint_less);
855 if (place >= vec.length ())
856 return NULL;
857 found = vec[place];
858 if (!constraint_equal (*found, lookfor))
859 return NULL;
860 return found;
863 /* Union two constraint vectors, TO and FROM. Put the result in TO. */
865 static void
866 constraint_set_union (vec<constraint_t> *to,
867 vec<constraint_t> *from)
869 int i;
870 constraint_t c;
872 FOR_EACH_VEC_ELT (*from, i, c)
874 if (constraint_vec_find (*to, *c) == NULL)
876 unsigned int place = to->lower_bound (c, constraint_less);
877 to->safe_insert (place, c);
882 /* Expands the solution in SET to all sub-fields of variables included.
883 Union the expanded result into RESULT. */
885 static void
886 solution_set_expand (bitmap result, bitmap set)
888 bitmap_iterator bi;
889 bitmap vars = NULL;
890 unsigned j;
892 /* In a first pass record all variables we need to add all
893 sub-fields off. This avoids quadratic behavior. */
894 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
896 varinfo_t v = get_varinfo (j);
897 if (v->is_artificial_var
898 || v->is_full_var)
899 continue;
900 v = lookup_vi_for_tree (v->decl);
901 if (vars == NULL)
902 vars = BITMAP_ALLOC (NULL);
903 bitmap_set_bit (vars, v->id);
906 /* In the second pass now do the addition to the solution and
907 to speed up solving add it to the delta as well. */
908 if (vars != NULL)
910 EXECUTE_IF_SET_IN_BITMAP (vars, 0, j, bi)
912 varinfo_t v = get_varinfo (j);
913 for (; v != NULL; v = v->next)
914 bitmap_set_bit (result, v->id);
916 BITMAP_FREE (vars);
920 /* Take a solution set SET, add OFFSET to each member of the set, and
921 overwrite SET with the result when done. */
923 static void
924 solution_set_add (bitmap set, HOST_WIDE_INT offset)
926 bitmap result = BITMAP_ALLOC (&iteration_obstack);
927 unsigned int i;
928 bitmap_iterator bi;
930 /* If the offset is unknown we have to expand the solution to
931 all subfields. */
932 if (offset == UNKNOWN_OFFSET)
934 solution_set_expand (set, set);
935 return;
938 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
940 varinfo_t vi = get_varinfo (i);
942 /* If this is a variable with just one field just set its bit
943 in the result. */
944 if (vi->is_artificial_var
945 || vi->is_unknown_size_var
946 || vi->is_full_var)
947 bitmap_set_bit (result, i);
948 else
950 unsigned HOST_WIDE_INT fieldoffset = vi->offset + offset;
952 /* If the offset makes the pointer point to before the
953 variable use offset zero for the field lookup. */
954 if (offset < 0
955 && fieldoffset > vi->offset)
956 fieldoffset = 0;
958 if (offset != 0)
959 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
961 bitmap_set_bit (result, vi->id);
962 /* If the result is not exactly at fieldoffset include the next
963 field as well. See get_constraint_for_ptr_offset for more
964 rationale. */
965 if (vi->offset != fieldoffset
966 && vi->next != NULL)
967 bitmap_set_bit (result, vi->next->id);
971 bitmap_copy (set, result);
972 BITMAP_FREE (result);
975 /* Union solution sets TO and FROM, and add INC to each member of FROM in the
976 process. */
978 static bool
979 set_union_with_increment (bitmap to, bitmap from, HOST_WIDE_INT inc)
981 if (inc == 0)
982 return bitmap_ior_into (to, from);
983 else
985 bitmap tmp;
986 bool res;
988 tmp = BITMAP_ALLOC (&iteration_obstack);
989 bitmap_copy (tmp, from);
990 solution_set_add (tmp, inc);
991 res = bitmap_ior_into (to, tmp);
992 BITMAP_FREE (tmp);
993 return res;
997 /* Insert constraint C into the list of complex constraints for graph
998 node VAR. */
1000 static void
1001 insert_into_complex (constraint_graph_t graph,
1002 unsigned int var, constraint_t c)
1004 vec<constraint_t> complex = graph->complex[var];
1005 unsigned int place = complex.lower_bound (c, constraint_less);
1007 /* Only insert constraints that do not already exist. */
1008 if (place >= complex.length ()
1009 || !constraint_equal (*c, *complex[place]))
1010 graph->complex[var].safe_insert (place, c);
1014 /* Condense two variable nodes into a single variable node, by moving
1015 all associated info from SRC to TO. */
1017 static void
1018 merge_node_constraints (constraint_graph_t graph, unsigned int to,
1019 unsigned int from)
1021 unsigned int i;
1022 constraint_t c;
1024 gcc_assert (find (from) == to);
1026 /* Move all complex constraints from src node into to node */
1027 FOR_EACH_VEC_ELT (graph->complex[from], i, c)
1029 /* In complex constraints for node src, we may have either
1030 a = *src, and *src = a, or an offseted constraint which are
1031 always added to the rhs node's constraints. */
1033 if (c->rhs.type == DEREF)
1034 c->rhs.var = to;
1035 else if (c->lhs.type == DEREF)
1036 c->lhs.var = to;
1037 else
1038 c->rhs.var = to;
1040 constraint_set_union (&graph->complex[to], &graph->complex[from]);
1041 graph->complex[from].release ();
1045 /* Remove edges involving NODE from GRAPH. */
1047 static void
1048 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
1050 if (graph->succs[node])
1051 BITMAP_FREE (graph->succs[node]);
1054 /* Merge GRAPH nodes FROM and TO into node TO. */
1056 static void
1057 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1058 unsigned int from)
1060 if (graph->indirect_cycles[from] != -1)
1062 /* If we have indirect cycles with the from node, and we have
1063 none on the to node, the to node has indirect cycles from the
1064 from node now that they are unified.
1065 If indirect cycles exist on both, unify the nodes that they
1066 are in a cycle with, since we know they are in a cycle with
1067 each other. */
1068 if (graph->indirect_cycles[to] == -1)
1069 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1072 /* Merge all the successor edges. */
1073 if (graph->succs[from])
1075 if (!graph->succs[to])
1076 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1077 bitmap_ior_into (graph->succs[to],
1078 graph->succs[from]);
1081 clear_edges_for_node (graph, from);
1085 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1086 it doesn't exist in the graph already. */
1088 static void
1089 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1090 unsigned int from)
1092 if (to == from)
1093 return;
1095 if (!graph->implicit_preds[to])
1096 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1098 if (bitmap_set_bit (graph->implicit_preds[to], from))
1099 stats.num_implicit_edges++;
1102 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1103 it doesn't exist in the graph already.
1104 Return false if the edge already existed, true otherwise. */
1106 static void
1107 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1108 unsigned int from)
1110 if (!graph->preds[to])
1111 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1112 bitmap_set_bit (graph->preds[to], from);
1115 /* Add a graph edge to GRAPH, going from FROM to TO if
1116 it doesn't exist in the graph already.
1117 Return false if the edge already existed, true otherwise. */
1119 static bool
1120 add_graph_edge (constraint_graph_t graph, unsigned int to,
1121 unsigned int from)
1123 if (to == from)
1125 return false;
1127 else
1129 bool r = false;
1131 if (!graph->succs[from])
1132 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1133 if (bitmap_set_bit (graph->succs[from], to))
1135 r = true;
1136 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1137 stats.num_edges++;
1139 return r;
1144 /* Return true if {DEST.SRC} is an existing graph edge in GRAPH. */
1146 static bool
1147 valid_graph_edge (constraint_graph_t graph, unsigned int src,
1148 unsigned int dest)
1150 return (graph->succs[dest]
1151 && bitmap_bit_p (graph->succs[dest], src));
1154 /* Initialize the constraint graph structure to contain SIZE nodes. */
1156 static void
1157 init_graph (unsigned int size)
1159 unsigned int j;
1161 graph = XCNEW (struct constraint_graph);
1162 graph->size = size;
1163 graph->succs = XCNEWVEC (bitmap, graph->size);
1164 graph->indirect_cycles = XNEWVEC (int, graph->size);
1165 graph->rep = XNEWVEC (unsigned int, graph->size);
1166 /* ??? Macros do not support template types with multiple arguments,
1167 so we use a typedef to work around it. */
1168 typedef vec<constraint_t> vec_constraint_t_heap;
1169 graph->complex = XCNEWVEC (vec_constraint_t_heap, size);
1170 graph->pe = XCNEWVEC (unsigned int, graph->size);
1171 graph->pe_rep = XNEWVEC (int, graph->size);
1173 for (j = 0; j < graph->size; j++)
1175 graph->rep[j] = j;
1176 graph->pe_rep[j] = -1;
1177 graph->indirect_cycles[j] = -1;
1181 /* Build the constraint graph, adding only predecessor edges right now. */
1183 static void
1184 build_pred_graph (void)
1186 int i;
1187 constraint_t c;
1188 unsigned int j;
1190 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1191 graph->preds = XCNEWVEC (bitmap, graph->size);
1192 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1193 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1194 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1195 graph->points_to = XCNEWVEC (bitmap, graph->size);
1196 graph->eq_rep = XNEWVEC (int, graph->size);
1197 graph->direct_nodes = sbitmap_alloc (graph->size);
1198 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1199 bitmap_clear (graph->direct_nodes);
1201 for (j = 0; j < FIRST_REF_NODE; j++)
1203 if (!get_varinfo (j)->is_special_var)
1204 bitmap_set_bit (graph->direct_nodes, j);
1207 for (j = 0; j < graph->size; j++)
1208 graph->eq_rep[j] = -1;
1210 for (j = 0; j < varmap.length (); j++)
1211 graph->indirect_cycles[j] = -1;
1213 FOR_EACH_VEC_ELT (constraints, i, c)
1215 struct constraint_expr lhs = c->lhs;
1216 struct constraint_expr rhs = c->rhs;
1217 unsigned int lhsvar = lhs.var;
1218 unsigned int rhsvar = rhs.var;
1220 if (lhs.type == DEREF)
1222 /* *x = y. */
1223 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1224 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1226 else if (rhs.type == DEREF)
1228 /* x = *y */
1229 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1230 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1231 else
1232 bitmap_clear_bit (graph->direct_nodes, lhsvar);
1234 else if (rhs.type == ADDRESSOF)
1236 varinfo_t v;
1238 /* x = &y */
1239 if (graph->points_to[lhsvar] == NULL)
1240 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1241 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1243 if (graph->pointed_by[rhsvar] == NULL)
1244 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1245 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1247 /* Implicitly, *x = y */
1248 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1250 /* All related variables are no longer direct nodes. */
1251 bitmap_clear_bit (graph->direct_nodes, rhsvar);
1252 v = get_varinfo (rhsvar);
1253 if (!v->is_full_var)
1255 v = lookup_vi_for_tree (v->decl);
1258 bitmap_clear_bit (graph->direct_nodes, v->id);
1259 v = v->next;
1261 while (v != NULL);
1263 bitmap_set_bit (graph->address_taken, rhsvar);
1265 else if (lhsvar > anything_id
1266 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1268 /* x = y */
1269 add_pred_graph_edge (graph, lhsvar, rhsvar);
1270 /* Implicitly, *x = *y */
1271 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1272 FIRST_REF_NODE + rhsvar);
1274 else if (lhs.offset != 0 || rhs.offset != 0)
1276 if (rhs.offset != 0)
1277 bitmap_clear_bit (graph->direct_nodes, lhs.var);
1278 else if (lhs.offset != 0)
1279 bitmap_clear_bit (graph->direct_nodes, rhs.var);
1284 /* Build the constraint graph, adding successor edges. */
1286 static void
1287 build_succ_graph (void)
1289 unsigned i, t;
1290 constraint_t c;
1292 FOR_EACH_VEC_ELT (constraints, i, c)
1294 struct constraint_expr lhs;
1295 struct constraint_expr rhs;
1296 unsigned int lhsvar;
1297 unsigned int rhsvar;
1299 if (!c)
1300 continue;
1302 lhs = c->lhs;
1303 rhs = c->rhs;
1304 lhsvar = find (lhs.var);
1305 rhsvar = find (rhs.var);
1307 if (lhs.type == DEREF)
1309 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1310 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1312 else if (rhs.type == DEREF)
1314 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1315 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1317 else if (rhs.type == ADDRESSOF)
1319 /* x = &y */
1320 gcc_assert (find (rhs.var) == rhs.var);
1321 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1323 else if (lhsvar > anything_id
1324 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1326 add_graph_edge (graph, lhsvar, rhsvar);
1330 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1331 receive pointers. */
1332 t = find (storedanything_id);
1333 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1335 if (!bitmap_bit_p (graph->direct_nodes, i)
1336 && get_varinfo (i)->may_have_pointers)
1337 add_graph_edge (graph, find (i), t);
1340 /* Everything stored to ANYTHING also potentially escapes. */
1341 add_graph_edge (graph, find (escaped_id), t);
1345 /* Changed variables on the last iteration. */
1346 static bitmap changed;
1348 /* Strongly Connected Component visitation info. */
1350 struct scc_info
1352 sbitmap visited;
1353 sbitmap deleted;
1354 unsigned int *dfs;
1355 unsigned int *node_mapping;
1356 int current_index;
1357 vec<unsigned> scc_stack;
1361 /* Recursive routine to find strongly connected components in GRAPH.
1362 SI is the SCC info to store the information in, and N is the id of current
1363 graph node we are processing.
1365 This is Tarjan's strongly connected component finding algorithm, as
1366 modified by Nuutila to keep only non-root nodes on the stack.
1367 The algorithm can be found in "On finding the strongly connected
1368 connected components in a directed graph" by Esko Nuutila and Eljas
1369 Soisalon-Soininen, in Information Processing Letters volume 49,
1370 number 1, pages 9-14. */
1372 static void
1373 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1375 unsigned int i;
1376 bitmap_iterator bi;
1377 unsigned int my_dfs;
1379 bitmap_set_bit (si->visited, n);
1380 si->dfs[n] = si->current_index ++;
1381 my_dfs = si->dfs[n];
1383 /* Visit all the successors. */
1384 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1386 unsigned int w;
1388 if (i > LAST_REF_NODE)
1389 break;
1391 w = find (i);
1392 if (bitmap_bit_p (si->deleted, w))
1393 continue;
1395 if (!bitmap_bit_p (si->visited, w))
1396 scc_visit (graph, si, w);
1398 unsigned int t = find (w);
1399 unsigned int nnode = find (n);
1400 gcc_assert (nnode == n);
1402 if (si->dfs[t] < si->dfs[nnode])
1403 si->dfs[n] = si->dfs[t];
1407 /* See if any components have been identified. */
1408 if (si->dfs[n] == my_dfs)
1410 if (si->scc_stack.length () > 0
1411 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1413 bitmap scc = BITMAP_ALLOC (NULL);
1414 unsigned int lowest_node;
1415 bitmap_iterator bi;
1417 bitmap_set_bit (scc, n);
1419 while (si->scc_stack.length () != 0
1420 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1422 unsigned int w = si->scc_stack.pop ();
1424 bitmap_set_bit (scc, w);
1427 lowest_node = bitmap_first_set_bit (scc);
1428 gcc_assert (lowest_node < FIRST_REF_NODE);
1430 /* Collapse the SCC nodes into a single node, and mark the
1431 indirect cycles. */
1432 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1434 if (i < FIRST_REF_NODE)
1436 if (unite (lowest_node, i))
1437 unify_nodes (graph, lowest_node, i, false);
1439 else
1441 unite (lowest_node, i);
1442 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1446 bitmap_set_bit (si->deleted, n);
1448 else
1449 si->scc_stack.safe_push (n);
1452 /* Unify node FROM into node TO, updating the changed count if
1453 necessary when UPDATE_CHANGED is true. */
1455 static void
1456 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1457 bool update_changed)
1460 gcc_assert (to != from && find (to) == to);
1461 if (dump_file && (dump_flags & TDF_DETAILS))
1462 fprintf (dump_file, "Unifying %s to %s\n",
1463 get_varinfo (from)->name,
1464 get_varinfo (to)->name);
1466 if (update_changed)
1467 stats.unified_vars_dynamic++;
1468 else
1469 stats.unified_vars_static++;
1471 merge_graph_nodes (graph, to, from);
1472 merge_node_constraints (graph, to, from);
1474 /* Mark TO as changed if FROM was changed. If TO was already marked
1475 as changed, decrease the changed count. */
1477 if (update_changed
1478 && bitmap_bit_p (changed, from))
1480 bitmap_clear_bit (changed, from);
1481 bitmap_set_bit (changed, to);
1483 if (get_varinfo (from)->solution)
1485 /* If the solution changes because of the merging, we need to mark
1486 the variable as changed. */
1487 if (bitmap_ior_into (get_varinfo (to)->solution,
1488 get_varinfo (from)->solution))
1490 if (update_changed)
1491 bitmap_set_bit (changed, to);
1494 BITMAP_FREE (get_varinfo (from)->solution);
1495 if (get_varinfo (from)->oldsolution)
1496 BITMAP_FREE (get_varinfo (from)->oldsolution);
1498 if (stats.iterations > 0
1499 && get_varinfo (to)->oldsolution)
1500 BITMAP_FREE (get_varinfo (to)->oldsolution);
1502 if (valid_graph_edge (graph, to, to))
1504 if (graph->succs[to])
1505 bitmap_clear_bit (graph->succs[to], to);
1509 /* Information needed to compute the topological ordering of a graph. */
1511 struct topo_info
1513 /* sbitmap of visited nodes. */
1514 sbitmap visited;
1515 /* Array that stores the topological order of the graph, *in
1516 reverse*. */
1517 vec<unsigned> topo_order;
1521 /* Initialize and return a topological info structure. */
1523 static struct topo_info *
1524 init_topo_info (void)
1526 size_t size = graph->size;
1527 struct topo_info *ti = XNEW (struct topo_info);
1528 ti->visited = sbitmap_alloc (size);
1529 bitmap_clear (ti->visited);
1530 ti->topo_order.create (1);
1531 return ti;
1535 /* Free the topological sort info pointed to by TI. */
1537 static void
1538 free_topo_info (struct topo_info *ti)
1540 sbitmap_free (ti->visited);
1541 ti->topo_order.release ();
1542 free (ti);
1545 /* Visit the graph in topological order, and store the order in the
1546 topo_info structure. */
1548 static void
1549 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1550 unsigned int n)
1552 bitmap_iterator bi;
1553 unsigned int j;
1555 bitmap_set_bit (ti->visited, n);
1557 if (graph->succs[n])
1558 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1560 if (!bitmap_bit_p (ti->visited, j))
1561 topo_visit (graph, ti, j);
1564 ti->topo_order.safe_push (n);
1567 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1568 starting solution for y. */
1570 static void
1571 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1572 bitmap delta)
1574 unsigned int lhs = c->lhs.var;
1575 bool flag = false;
1576 bitmap sol = get_varinfo (lhs)->solution;
1577 unsigned int j;
1578 bitmap_iterator bi;
1579 HOST_WIDE_INT roffset = c->rhs.offset;
1581 /* Our IL does not allow this. */
1582 gcc_assert (c->lhs.offset == 0);
1584 /* If the solution of Y contains anything it is good enough to transfer
1585 this to the LHS. */
1586 if (bitmap_bit_p (delta, anything_id))
1588 flag |= bitmap_set_bit (sol, anything_id);
1589 goto done;
1592 /* If we do not know at with offset the rhs is dereferenced compute
1593 the reachability set of DELTA, conservatively assuming it is
1594 dereferenced at all valid offsets. */
1595 if (roffset == UNKNOWN_OFFSET)
1597 solution_set_expand (delta, delta);
1598 /* No further offset processing is necessary. */
1599 roffset = 0;
1602 /* For each variable j in delta (Sol(y)), add
1603 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1604 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1606 varinfo_t v = get_varinfo (j);
1607 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1608 unsigned int t;
1610 if (v->is_full_var)
1611 fieldoffset = v->offset;
1612 else if (roffset != 0)
1613 v = first_vi_for_offset (v, fieldoffset);
1614 /* If the access is outside of the variable we can ignore it. */
1615 if (!v)
1616 continue;
1620 t = find (v->id);
1622 /* Adding edges from the special vars is pointless.
1623 They don't have sets that can change. */
1624 if (get_varinfo (t)->is_special_var)
1625 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1626 /* Merging the solution from ESCAPED needlessly increases
1627 the set. Use ESCAPED as representative instead. */
1628 else if (v->id == escaped_id)
1629 flag |= bitmap_set_bit (sol, escaped_id);
1630 else if (v->may_have_pointers
1631 && add_graph_edge (graph, lhs, t))
1632 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1634 /* If the variable is not exactly at the requested offset
1635 we have to include the next one. */
1636 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1637 || v->next == NULL)
1638 break;
1640 v = v->next;
1641 fieldoffset = v->offset;
1643 while (1);
1646 done:
1647 /* If the LHS solution changed, mark the var as changed. */
1648 if (flag)
1650 get_varinfo (lhs)->solution = sol;
1651 bitmap_set_bit (changed, lhs);
1655 /* Process a constraint C that represents *(x + off) = y using DELTA
1656 as the starting solution for x. */
1658 static void
1659 do_ds_constraint (constraint_t c, bitmap delta)
1661 unsigned int rhs = c->rhs.var;
1662 bitmap sol = get_varinfo (rhs)->solution;
1663 unsigned int j;
1664 bitmap_iterator bi;
1665 HOST_WIDE_INT loff = c->lhs.offset;
1666 bool escaped_p = false;
1668 /* Our IL does not allow this. */
1669 gcc_assert (c->rhs.offset == 0);
1671 /* If the solution of y contains ANYTHING simply use the ANYTHING
1672 solution. This avoids needlessly increasing the points-to sets. */
1673 if (bitmap_bit_p (sol, anything_id))
1674 sol = get_varinfo (find (anything_id))->solution;
1676 /* If the solution for x contains ANYTHING we have to merge the
1677 solution of y into all pointer variables which we do via
1678 STOREDANYTHING. */
1679 if (bitmap_bit_p (delta, anything_id))
1681 unsigned t = find (storedanything_id);
1682 if (add_graph_edge (graph, t, rhs))
1684 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1685 bitmap_set_bit (changed, t);
1687 return;
1690 /* If we do not know at with offset the rhs is dereferenced compute
1691 the reachability set of DELTA, conservatively assuming it is
1692 dereferenced at all valid offsets. */
1693 if (loff == UNKNOWN_OFFSET)
1695 solution_set_expand (delta, delta);
1696 loff = 0;
1699 /* For each member j of delta (Sol(x)), add an edge from y to j and
1700 union Sol(y) into Sol(j) */
1701 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1703 varinfo_t v = get_varinfo (j);
1704 unsigned int t;
1705 HOST_WIDE_INT fieldoffset = v->offset + loff;
1707 if (v->is_full_var)
1708 fieldoffset = v->offset;
1709 else if (loff != 0)
1710 v = first_vi_for_offset (v, fieldoffset);
1711 /* If the access is outside of the variable we can ignore it. */
1712 if (!v)
1713 continue;
1717 if (v->may_have_pointers)
1719 /* If v is a global variable then this is an escape point. */
1720 if (v->is_global_var
1721 && !escaped_p)
1723 t = find (escaped_id);
1724 if (add_graph_edge (graph, t, rhs)
1725 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1726 bitmap_set_bit (changed, t);
1727 /* Enough to let rhs escape once. */
1728 escaped_p = true;
1731 if (v->is_special_var)
1732 break;
1734 t = find (v->id);
1735 if (add_graph_edge (graph, t, rhs)
1736 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1737 bitmap_set_bit (changed, t);
1740 /* If the variable is not exactly at the requested offset
1741 we have to include the next one. */
1742 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1743 || v->next == NULL)
1744 break;
1746 v = v->next;
1747 fieldoffset = v->offset;
1749 while (1);
1753 /* Handle a non-simple (simple meaning requires no iteration),
1754 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1756 static void
1757 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta)
1759 if (c->lhs.type == DEREF)
1761 if (c->rhs.type == ADDRESSOF)
1763 gcc_unreachable();
1765 else
1767 /* *x = y */
1768 do_ds_constraint (c, delta);
1771 else if (c->rhs.type == DEREF)
1773 /* x = *y */
1774 if (!(get_varinfo (c->lhs.var)->is_special_var))
1775 do_sd_constraint (graph, c, delta);
1777 else
1779 bitmap tmp;
1780 bitmap solution;
1781 bool flag = false;
1783 gcc_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR);
1784 solution = get_varinfo (c->rhs.var)->solution;
1785 tmp = get_varinfo (c->lhs.var)->solution;
1787 flag = set_union_with_increment (tmp, solution, c->rhs.offset);
1789 if (flag)
1791 get_varinfo (c->lhs.var)->solution = tmp;
1792 bitmap_set_bit (changed, c->lhs.var);
1797 /* Initialize and return a new SCC info structure. */
1799 static struct scc_info *
1800 init_scc_info (size_t size)
1802 struct scc_info *si = XNEW (struct scc_info);
1803 size_t i;
1805 si->current_index = 0;
1806 si->visited = sbitmap_alloc (size);
1807 bitmap_clear (si->visited);
1808 si->deleted = sbitmap_alloc (size);
1809 bitmap_clear (si->deleted);
1810 si->node_mapping = XNEWVEC (unsigned int, size);
1811 si->dfs = XCNEWVEC (unsigned int, size);
1813 for (i = 0; i < size; i++)
1814 si->node_mapping[i] = i;
1816 si->scc_stack.create (1);
1817 return si;
1820 /* Free an SCC info structure pointed to by SI */
1822 static void
1823 free_scc_info (struct scc_info *si)
1825 sbitmap_free (si->visited);
1826 sbitmap_free (si->deleted);
1827 free (si->node_mapping);
1828 free (si->dfs);
1829 si->scc_stack.release ();
1830 free (si);
1834 /* Find indirect cycles in GRAPH that occur, using strongly connected
1835 components, and note them in the indirect cycles map.
1837 This technique comes from Ben Hardekopf and Calvin Lin,
1838 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1839 Lines of Code", submitted to PLDI 2007. */
1841 static void
1842 find_indirect_cycles (constraint_graph_t graph)
1844 unsigned int i;
1845 unsigned int size = graph->size;
1846 struct scc_info *si = init_scc_info (size);
1848 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1849 if (!bitmap_bit_p (si->visited, i) && find (i) == i)
1850 scc_visit (graph, si, i);
1852 free_scc_info (si);
1855 /* Compute a topological ordering for GRAPH, and store the result in the
1856 topo_info structure TI. */
1858 static void
1859 compute_topo_order (constraint_graph_t graph,
1860 struct topo_info *ti)
1862 unsigned int i;
1863 unsigned int size = graph->size;
1865 for (i = 0; i != size; ++i)
1866 if (!bitmap_bit_p (ti->visited, i) && find (i) == i)
1867 topo_visit (graph, ti, i);
1870 /* Structure used to for hash value numbering of pointer equivalence
1871 classes. */
1873 typedef struct equiv_class_label
1875 hashval_t hashcode;
1876 unsigned int equivalence_class;
1877 bitmap labels;
1878 } *equiv_class_label_t;
1879 typedef const struct equiv_class_label *const_equiv_class_label_t;
1881 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1882 classes. */
1883 static htab_t pointer_equiv_class_table;
1885 /* A hashtable for mapping a bitmap of labels->location equivalence
1886 classes. */
1887 static htab_t location_equiv_class_table;
1889 /* Hash function for a equiv_class_label_t */
1891 static hashval_t
1892 equiv_class_label_hash (const void *p)
1894 const_equiv_class_label_t const ecl = (const_equiv_class_label_t) p;
1895 return ecl->hashcode;
1898 /* Equality function for two equiv_class_label_t's. */
1900 static int
1901 equiv_class_label_eq (const void *p1, const void *p2)
1903 const_equiv_class_label_t const eql1 = (const_equiv_class_label_t) p1;
1904 const_equiv_class_label_t const eql2 = (const_equiv_class_label_t) p2;
1905 return (eql1->hashcode == eql2->hashcode
1906 && bitmap_equal_p (eql1->labels, eql2->labels));
1909 /* Lookup a equivalence class in TABLE by the bitmap of LABELS it
1910 contains. */
1912 static unsigned int
1913 equiv_class_lookup (htab_t table, bitmap labels)
1915 void **slot;
1916 struct equiv_class_label ecl;
1918 ecl.labels = labels;
1919 ecl.hashcode = bitmap_hash (labels);
1921 slot = htab_find_slot_with_hash (table, &ecl,
1922 ecl.hashcode, NO_INSERT);
1923 if (!slot)
1924 return 0;
1925 else
1926 return ((equiv_class_label_t) *slot)->equivalence_class;
1930 /* Add an equivalence class named EQUIVALENCE_CLASS with labels LABELS
1931 to TABLE. */
1933 static void
1934 equiv_class_add (htab_t table, unsigned int equivalence_class,
1935 bitmap labels)
1937 void **slot;
1938 equiv_class_label_t ecl = XNEW (struct equiv_class_label);
1940 ecl->labels = labels;
1941 ecl->equivalence_class = equivalence_class;
1942 ecl->hashcode = bitmap_hash (labels);
1944 slot = htab_find_slot_with_hash (table, ecl,
1945 ecl->hashcode, INSERT);
1946 gcc_assert (!*slot);
1947 *slot = (void *) ecl;
1950 /* Perform offline variable substitution.
1952 This is a worst case quadratic time way of identifying variables
1953 that must have equivalent points-to sets, including those caused by
1954 static cycles, and single entry subgraphs, in the constraint graph.
1956 The technique is described in "Exploiting Pointer and Location
1957 Equivalence to Optimize Pointer Analysis. In the 14th International
1958 Static Analysis Symposium (SAS), August 2007." It is known as the
1959 "HU" algorithm, and is equivalent to value numbering the collapsed
1960 constraint graph including evaluating unions.
1962 The general method of finding equivalence classes is as follows:
1963 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1964 Initialize all non-REF nodes to be direct nodes.
1965 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1966 variable}
1967 For each constraint containing the dereference, we also do the same
1968 thing.
1970 We then compute SCC's in the graph and unify nodes in the same SCC,
1971 including pts sets.
1973 For each non-collapsed node x:
1974 Visit all unvisited explicit incoming edges.
1975 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1976 where y->x.
1977 Lookup the equivalence class for pts(x).
1978 If we found one, equivalence_class(x) = found class.
1979 Otherwise, equivalence_class(x) = new class, and new_class is
1980 added to the lookup table.
1982 All direct nodes with the same equivalence class can be replaced
1983 with a single representative node.
1984 All unlabeled nodes (label == 0) are not pointers and all edges
1985 involving them can be eliminated.
1986 We perform these optimizations during rewrite_constraints
1988 In addition to pointer equivalence class finding, we also perform
1989 location equivalence class finding. This is the set of variables
1990 that always appear together in points-to sets. We use this to
1991 compress the size of the points-to sets. */
1993 /* Current maximum pointer equivalence class id. */
1994 static int pointer_equiv_class;
1996 /* Current maximum location equivalence class id. */
1997 static int location_equiv_class;
1999 /* Recursive routine to find strongly connected components in GRAPH,
2000 and label it's nodes with DFS numbers. */
2002 static void
2003 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2005 unsigned int i;
2006 bitmap_iterator bi;
2007 unsigned int my_dfs;
2009 gcc_assert (si->node_mapping[n] == n);
2010 bitmap_set_bit (si->visited, n);
2011 si->dfs[n] = si->current_index ++;
2012 my_dfs = si->dfs[n];
2014 /* Visit all the successors. */
2015 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2017 unsigned int w = si->node_mapping[i];
2019 if (bitmap_bit_p (si->deleted, w))
2020 continue;
2022 if (!bitmap_bit_p (si->visited, w))
2023 condense_visit (graph, si, w);
2025 unsigned int t = si->node_mapping[w];
2026 unsigned int nnode = si->node_mapping[n];
2027 gcc_assert (nnode == n);
2029 if (si->dfs[t] < si->dfs[nnode])
2030 si->dfs[n] = si->dfs[t];
2034 /* Visit all the implicit predecessors. */
2035 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2037 unsigned int w = si->node_mapping[i];
2039 if (bitmap_bit_p (si->deleted, w))
2040 continue;
2042 if (!bitmap_bit_p (si->visited, w))
2043 condense_visit (graph, si, w);
2045 unsigned int t = si->node_mapping[w];
2046 unsigned int nnode = si->node_mapping[n];
2047 gcc_assert (nnode == n);
2049 if (si->dfs[t] < si->dfs[nnode])
2050 si->dfs[n] = si->dfs[t];
2054 /* See if any components have been identified. */
2055 if (si->dfs[n] == my_dfs)
2057 while (si->scc_stack.length () != 0
2058 && si->dfs[si->scc_stack.last ()] >= my_dfs)
2060 unsigned int w = si->scc_stack.pop ();
2061 si->node_mapping[w] = n;
2063 if (!bitmap_bit_p (graph->direct_nodes, w))
2064 bitmap_clear_bit (graph->direct_nodes, n);
2066 /* Unify our nodes. */
2067 if (graph->preds[w])
2069 if (!graph->preds[n])
2070 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2071 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2073 if (graph->implicit_preds[w])
2075 if (!graph->implicit_preds[n])
2076 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2077 bitmap_ior_into (graph->implicit_preds[n],
2078 graph->implicit_preds[w]);
2080 if (graph->points_to[w])
2082 if (!graph->points_to[n])
2083 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2084 bitmap_ior_into (graph->points_to[n],
2085 graph->points_to[w]);
2088 bitmap_set_bit (si->deleted, n);
2090 else
2091 si->scc_stack.safe_push (n);
2094 /* Label pointer equivalences. */
2096 static void
2097 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2099 unsigned int i;
2100 bitmap_iterator bi;
2101 bitmap_set_bit (si->visited, n);
2103 if (!graph->points_to[n])
2104 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2106 /* Label and union our incoming edges's points to sets. */
2107 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2109 unsigned int w = si->node_mapping[i];
2110 if (!bitmap_bit_p (si->visited, w))
2111 label_visit (graph, si, w);
2113 /* Skip unused edges */
2114 if (w == n || graph->pointer_label[w] == 0)
2115 continue;
2117 if (graph->points_to[w])
2118 bitmap_ior_into(graph->points_to[n], graph->points_to[w]);
2120 /* Indirect nodes get fresh variables. */
2121 if (!bitmap_bit_p (graph->direct_nodes, n))
2122 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2124 if (!bitmap_empty_p (graph->points_to[n]))
2126 unsigned int label = equiv_class_lookup (pointer_equiv_class_table,
2127 graph->points_to[n]);
2128 if (!label)
2130 label = pointer_equiv_class++;
2131 equiv_class_add (pointer_equiv_class_table,
2132 label, graph->points_to[n]);
2134 graph->pointer_label[n] = label;
2138 /* Perform offline variable substitution, discovering equivalence
2139 classes, and eliminating non-pointer variables. */
2141 static struct scc_info *
2142 perform_var_substitution (constraint_graph_t graph)
2144 unsigned int i;
2145 unsigned int size = graph->size;
2146 struct scc_info *si = init_scc_info (size);
2148 bitmap_obstack_initialize (&iteration_obstack);
2149 pointer_equiv_class_table = htab_create (511, equiv_class_label_hash,
2150 equiv_class_label_eq, free);
2151 location_equiv_class_table = htab_create (511, equiv_class_label_hash,
2152 equiv_class_label_eq, free);
2153 pointer_equiv_class = 1;
2154 location_equiv_class = 1;
2156 /* Condense the nodes, which means to find SCC's, count incoming
2157 predecessors, and unite nodes in SCC's. */
2158 for (i = 0; i < FIRST_REF_NODE; i++)
2159 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2160 condense_visit (graph, si, si->node_mapping[i]);
2162 bitmap_clear (si->visited);
2163 /* Actually the label the nodes for pointer equivalences */
2164 for (i = 0; i < FIRST_REF_NODE; i++)
2165 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2166 label_visit (graph, si, si->node_mapping[i]);
2168 /* Calculate location equivalence labels. */
2169 for (i = 0; i < FIRST_REF_NODE; i++)
2171 bitmap pointed_by;
2172 bitmap_iterator bi;
2173 unsigned int j;
2174 unsigned int label;
2176 if (!graph->pointed_by[i])
2177 continue;
2178 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2180 /* Translate the pointed-by mapping for pointer equivalence
2181 labels. */
2182 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2184 bitmap_set_bit (pointed_by,
2185 graph->pointer_label[si->node_mapping[j]]);
2187 /* The original pointed_by is now dead. */
2188 BITMAP_FREE (graph->pointed_by[i]);
2190 /* Look up the location equivalence label if one exists, or make
2191 one otherwise. */
2192 label = equiv_class_lookup (location_equiv_class_table,
2193 pointed_by);
2194 if (label == 0)
2196 label = location_equiv_class++;
2197 equiv_class_add (location_equiv_class_table,
2198 label, pointed_by);
2200 else
2202 if (dump_file && (dump_flags & TDF_DETAILS))
2203 fprintf (dump_file, "Found location equivalence for node %s\n",
2204 get_varinfo (i)->name);
2205 BITMAP_FREE (pointed_by);
2207 graph->loc_label[i] = label;
2211 if (dump_file && (dump_flags & TDF_DETAILS))
2212 for (i = 0; i < FIRST_REF_NODE; i++)
2214 bool direct_node = bitmap_bit_p (graph->direct_nodes, i);
2215 fprintf (dump_file,
2216 "Equivalence classes for %s node id %d:%s are pointer: %d"
2217 ", location:%d\n",
2218 direct_node ? "Direct node" : "Indirect node", i,
2219 get_varinfo (i)->name,
2220 graph->pointer_label[si->node_mapping[i]],
2221 graph->loc_label[si->node_mapping[i]]);
2224 /* Quickly eliminate our non-pointer variables. */
2226 for (i = 0; i < FIRST_REF_NODE; i++)
2228 unsigned int node = si->node_mapping[i];
2230 if (graph->pointer_label[node] == 0)
2232 if (dump_file && (dump_flags & TDF_DETAILS))
2233 fprintf (dump_file,
2234 "%s is a non-pointer variable, eliminating edges.\n",
2235 get_varinfo (node)->name);
2236 stats.nonpointer_vars++;
2237 clear_edges_for_node (graph, node);
2241 return si;
2244 /* Free information that was only necessary for variable
2245 substitution. */
2247 static void
2248 free_var_substitution_info (struct scc_info *si)
2250 free_scc_info (si);
2251 free (graph->pointer_label);
2252 free (graph->loc_label);
2253 free (graph->pointed_by);
2254 free (graph->points_to);
2255 free (graph->eq_rep);
2256 sbitmap_free (graph->direct_nodes);
2257 htab_delete (pointer_equiv_class_table);
2258 htab_delete (location_equiv_class_table);
2259 bitmap_obstack_release (&iteration_obstack);
2262 /* Return an existing node that is equivalent to NODE, which has
2263 equivalence class LABEL, if one exists. Return NODE otherwise. */
2265 static unsigned int
2266 find_equivalent_node (constraint_graph_t graph,
2267 unsigned int node, unsigned int label)
2269 /* If the address version of this variable is unused, we can
2270 substitute it for anything else with the same label.
2271 Otherwise, we know the pointers are equivalent, but not the
2272 locations, and we can unite them later. */
2274 if (!bitmap_bit_p (graph->address_taken, node))
2276 gcc_assert (label < graph->size);
2278 if (graph->eq_rep[label] != -1)
2280 /* Unify the two variables since we know they are equivalent. */
2281 if (unite (graph->eq_rep[label], node))
2282 unify_nodes (graph, graph->eq_rep[label], node, false);
2283 return graph->eq_rep[label];
2285 else
2287 graph->eq_rep[label] = node;
2288 graph->pe_rep[label] = node;
2291 else
2293 gcc_assert (label < graph->size);
2294 graph->pe[node] = label;
2295 if (graph->pe_rep[label] == -1)
2296 graph->pe_rep[label] = node;
2299 return node;
2302 /* Unite pointer equivalent but not location equivalent nodes in
2303 GRAPH. This may only be performed once variable substitution is
2304 finished. */
2306 static void
2307 unite_pointer_equivalences (constraint_graph_t graph)
2309 unsigned int i;
2311 /* Go through the pointer equivalences and unite them to their
2312 representative, if they aren't already. */
2313 for (i = 0; i < FIRST_REF_NODE; i++)
2315 unsigned int label = graph->pe[i];
2316 if (label)
2318 int label_rep = graph->pe_rep[label];
2320 if (label_rep == -1)
2321 continue;
2323 label_rep = find (label_rep);
2324 if (label_rep >= 0 && unite (label_rep, find (i)))
2325 unify_nodes (graph, label_rep, i, false);
2330 /* Move complex constraints to the GRAPH nodes they belong to. */
2332 static void
2333 move_complex_constraints (constraint_graph_t graph)
2335 int i;
2336 constraint_t c;
2338 FOR_EACH_VEC_ELT (constraints, i, c)
2340 if (c)
2342 struct constraint_expr lhs = c->lhs;
2343 struct constraint_expr rhs = c->rhs;
2345 if (lhs.type == DEREF)
2347 insert_into_complex (graph, lhs.var, c);
2349 else if (rhs.type == DEREF)
2351 if (!(get_varinfo (lhs.var)->is_special_var))
2352 insert_into_complex (graph, rhs.var, c);
2354 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2355 && (lhs.offset != 0 || rhs.offset != 0))
2357 insert_into_complex (graph, rhs.var, c);
2364 /* Optimize and rewrite complex constraints while performing
2365 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2366 result of perform_variable_substitution. */
2368 static void
2369 rewrite_constraints (constraint_graph_t graph,
2370 struct scc_info *si)
2372 int i;
2373 unsigned int j;
2374 constraint_t c;
2376 for (j = 0; j < graph->size; j++)
2377 gcc_assert (find (j) == j);
2379 FOR_EACH_VEC_ELT (constraints, i, c)
2381 struct constraint_expr lhs = c->lhs;
2382 struct constraint_expr rhs = c->rhs;
2383 unsigned int lhsvar = find (lhs.var);
2384 unsigned int rhsvar = find (rhs.var);
2385 unsigned int lhsnode, rhsnode;
2386 unsigned int lhslabel, rhslabel;
2388 lhsnode = si->node_mapping[lhsvar];
2389 rhsnode = si->node_mapping[rhsvar];
2390 lhslabel = graph->pointer_label[lhsnode];
2391 rhslabel = graph->pointer_label[rhsnode];
2393 /* See if it is really a non-pointer variable, and if so, ignore
2394 the constraint. */
2395 if (lhslabel == 0)
2397 if (dump_file && (dump_flags & TDF_DETAILS))
2400 fprintf (dump_file, "%s is a non-pointer variable,"
2401 "ignoring constraint:",
2402 get_varinfo (lhs.var)->name);
2403 dump_constraint (dump_file, c);
2404 fprintf (dump_file, "\n");
2406 constraints[i] = NULL;
2407 continue;
2410 if (rhslabel == 0)
2412 if (dump_file && (dump_flags & TDF_DETAILS))
2415 fprintf (dump_file, "%s is a non-pointer variable,"
2416 "ignoring constraint:",
2417 get_varinfo (rhs.var)->name);
2418 dump_constraint (dump_file, c);
2419 fprintf (dump_file, "\n");
2421 constraints[i] = NULL;
2422 continue;
2425 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2426 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2427 c->lhs.var = lhsvar;
2428 c->rhs.var = rhsvar;
2433 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2434 part of an SCC, false otherwise. */
2436 static bool
2437 eliminate_indirect_cycles (unsigned int node)
2439 if (graph->indirect_cycles[node] != -1
2440 && !bitmap_empty_p (get_varinfo (node)->solution))
2442 unsigned int i;
2443 vec<unsigned> queue = vNULL;
2444 int queuepos;
2445 unsigned int to = find (graph->indirect_cycles[node]);
2446 bitmap_iterator bi;
2448 /* We can't touch the solution set and call unify_nodes
2449 at the same time, because unify_nodes is going to do
2450 bitmap unions into it. */
2452 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2454 if (find (i) == i && i != to)
2456 if (unite (to, i))
2457 queue.safe_push (i);
2461 for (queuepos = 0;
2462 queue.iterate (queuepos, &i);
2463 queuepos++)
2465 unify_nodes (graph, to, i, true);
2467 queue.release ();
2468 return true;
2470 return false;
2473 /* Solve the constraint graph GRAPH using our worklist solver.
2474 This is based on the PW* family of solvers from the "Efficient Field
2475 Sensitive Pointer Analysis for C" paper.
2476 It works by iterating over all the graph nodes, processing the complex
2477 constraints and propagating the copy constraints, until everything stops
2478 changed. This corresponds to steps 6-8 in the solving list given above. */
2480 static void
2481 solve_graph (constraint_graph_t graph)
2483 unsigned int size = graph->size;
2484 unsigned int i;
2485 bitmap pts;
2487 changed = BITMAP_ALLOC (NULL);
2489 /* Mark all initial non-collapsed nodes as changed. */
2490 for (i = 0; i < size; i++)
2492 varinfo_t ivi = get_varinfo (i);
2493 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2494 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2495 || graph->complex[i].length () > 0))
2496 bitmap_set_bit (changed, i);
2499 /* Allocate a bitmap to be used to store the changed bits. */
2500 pts = BITMAP_ALLOC (&pta_obstack);
2502 while (!bitmap_empty_p (changed))
2504 unsigned int i;
2505 struct topo_info *ti = init_topo_info ();
2506 stats.iterations++;
2508 bitmap_obstack_initialize (&iteration_obstack);
2510 compute_topo_order (graph, ti);
2512 while (ti->topo_order.length () != 0)
2515 i = ti->topo_order.pop ();
2517 /* If this variable is not a representative, skip it. */
2518 if (find (i) != i)
2519 continue;
2521 /* In certain indirect cycle cases, we may merge this
2522 variable to another. */
2523 if (eliminate_indirect_cycles (i) && find (i) != i)
2524 continue;
2526 /* If the node has changed, we need to process the
2527 complex constraints and outgoing edges again. */
2528 if (bitmap_clear_bit (changed, i))
2530 unsigned int j;
2531 constraint_t c;
2532 bitmap solution;
2533 vec<constraint_t> complex = graph->complex[i];
2534 varinfo_t vi = get_varinfo (i);
2535 bool solution_empty;
2537 /* Compute the changed set of solution bits. */
2538 if (vi->oldsolution)
2539 bitmap_and_compl (pts, vi->solution, vi->oldsolution);
2540 else
2541 bitmap_copy (pts, vi->solution);
2543 if (bitmap_empty_p (pts))
2544 continue;
2546 if (vi->oldsolution)
2547 bitmap_ior_into (vi->oldsolution, pts);
2548 else
2550 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
2551 bitmap_copy (vi->oldsolution, pts);
2554 solution = vi->solution;
2555 solution_empty = bitmap_empty_p (solution);
2557 /* Process the complex constraints */
2558 FOR_EACH_VEC_ELT (complex, j, c)
2560 /* XXX: This is going to unsort the constraints in
2561 some cases, which will occasionally add duplicate
2562 constraints during unification. This does not
2563 affect correctness. */
2564 c->lhs.var = find (c->lhs.var);
2565 c->rhs.var = find (c->rhs.var);
2567 /* The only complex constraint that can change our
2568 solution to non-empty, given an empty solution,
2569 is a constraint where the lhs side is receiving
2570 some set from elsewhere. */
2571 if (!solution_empty || c->lhs.type != DEREF)
2572 do_complex_constraint (graph, c, pts);
2575 solution_empty = bitmap_empty_p (solution);
2577 if (!solution_empty)
2579 bitmap_iterator bi;
2580 unsigned eff_escaped_id = find (escaped_id);
2582 /* Propagate solution to all successors. */
2583 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2584 0, j, bi)
2586 bitmap tmp;
2587 bool flag;
2589 unsigned int to = find (j);
2590 tmp = get_varinfo (to)->solution;
2591 flag = false;
2593 /* Don't try to propagate to ourselves. */
2594 if (to == i)
2595 continue;
2597 /* If we propagate from ESCAPED use ESCAPED as
2598 placeholder. */
2599 if (i == eff_escaped_id)
2600 flag = bitmap_set_bit (tmp, escaped_id);
2601 else
2602 flag = set_union_with_increment (tmp, pts, 0);
2604 if (flag)
2606 get_varinfo (to)->solution = tmp;
2607 bitmap_set_bit (changed, to);
2613 free_topo_info (ti);
2614 bitmap_obstack_release (&iteration_obstack);
2617 BITMAP_FREE (pts);
2618 BITMAP_FREE (changed);
2619 bitmap_obstack_release (&oldpta_obstack);
2622 /* Map from trees to variable infos. */
2623 static struct pointer_map_t *vi_for_tree;
2626 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2628 static void
2629 insert_vi_for_tree (tree t, varinfo_t vi)
2631 void **slot = pointer_map_insert (vi_for_tree, t);
2632 gcc_assert (vi);
2633 gcc_assert (*slot == NULL);
2634 *slot = vi;
2637 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2638 exist in the map, return NULL, otherwise, return the varinfo we found. */
2640 static varinfo_t
2641 lookup_vi_for_tree (tree t)
2643 void **slot = pointer_map_contains (vi_for_tree, t);
2644 if (slot == NULL)
2645 return NULL;
2647 return (varinfo_t) *slot;
2650 /* Return a printable name for DECL */
2652 static const char *
2653 alias_get_name (tree decl)
2655 const char *res = NULL;
2656 char *temp;
2657 int num_printed = 0;
2659 if (!dump_file)
2660 return "NULL";
2662 if (TREE_CODE (decl) == SSA_NAME)
2664 res = get_name (decl);
2665 if (res)
2666 num_printed = asprintf (&temp, "%s_%u", res, SSA_NAME_VERSION (decl));
2667 else
2668 num_printed = asprintf (&temp, "_%u", SSA_NAME_VERSION (decl));
2669 if (num_printed > 0)
2671 res = ggc_strdup (temp);
2672 free (temp);
2675 else if (DECL_P (decl))
2677 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2678 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2679 else
2681 res = get_name (decl);
2682 if (!res)
2684 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2685 if (num_printed > 0)
2687 res = ggc_strdup (temp);
2688 free (temp);
2693 if (res != NULL)
2694 return res;
2696 return "NULL";
2699 /* Find the variable id for tree T in the map.
2700 If T doesn't exist in the map, create an entry for it and return it. */
2702 static varinfo_t
2703 get_vi_for_tree (tree t)
2705 void **slot = pointer_map_contains (vi_for_tree, t);
2706 if (slot == NULL)
2707 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2709 return (varinfo_t) *slot;
2712 /* Get a scalar constraint expression for a new temporary variable. */
2714 static struct constraint_expr
2715 new_scalar_tmp_constraint_exp (const char *name)
2717 struct constraint_expr tmp;
2718 varinfo_t vi;
2720 vi = new_var_info (NULL_TREE, name);
2721 vi->offset = 0;
2722 vi->size = -1;
2723 vi->fullsize = -1;
2724 vi->is_full_var = 1;
2726 tmp.var = vi->id;
2727 tmp.type = SCALAR;
2728 tmp.offset = 0;
2730 return tmp;
2733 /* Get a constraint expression vector from an SSA_VAR_P node.
2734 If address_p is true, the result will be taken its address of. */
2736 static void
2737 get_constraint_for_ssa_var (tree t, vec<ce_s> *results, bool address_p)
2739 struct constraint_expr cexpr;
2740 varinfo_t vi;
2742 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2743 gcc_assert (TREE_CODE (t) == SSA_NAME || DECL_P (t));
2745 /* For parameters, get at the points-to set for the actual parm
2746 decl. */
2747 if (TREE_CODE (t) == SSA_NAME
2748 && SSA_NAME_IS_DEFAULT_DEF (t)
2749 && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2750 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL))
2752 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2753 return;
2756 /* For global variables resort to the alias target. */
2757 if (TREE_CODE (t) == VAR_DECL
2758 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
2760 struct varpool_node *node = varpool_get_node (t);
2761 if (node && node->alias)
2763 node = varpool_variable_node (node, NULL);
2764 t = node->symbol.decl;
2768 vi = get_vi_for_tree (t);
2769 cexpr.var = vi->id;
2770 cexpr.type = SCALAR;
2771 cexpr.offset = 0;
2772 /* If we determine the result is "anything", and we know this is readonly,
2773 say it points to readonly memory instead. */
2774 if (cexpr.var == anything_id && TREE_READONLY (t))
2776 gcc_unreachable ();
2777 cexpr.type = ADDRESSOF;
2778 cexpr.var = readonly_id;
2781 /* If we are not taking the address of the constraint expr, add all
2782 sub-fiels of the variable as well. */
2783 if (!address_p
2784 && !vi->is_full_var)
2786 for (; vi; vi = vi->next)
2788 cexpr.var = vi->id;
2789 results->safe_push (cexpr);
2791 return;
2794 results->safe_push (cexpr);
2797 /* Process constraint T, performing various simplifications and then
2798 adding it to our list of overall constraints. */
2800 static void
2801 process_constraint (constraint_t t)
2803 struct constraint_expr rhs = t->rhs;
2804 struct constraint_expr lhs = t->lhs;
2806 gcc_assert (rhs.var < varmap.length ());
2807 gcc_assert (lhs.var < varmap.length ());
2809 /* If we didn't get any useful constraint from the lhs we get
2810 &ANYTHING as fallback from get_constraint_for. Deal with
2811 it here by turning it into *ANYTHING. */
2812 if (lhs.type == ADDRESSOF
2813 && lhs.var == anything_id)
2814 lhs.type = DEREF;
2816 /* ADDRESSOF on the lhs is invalid. */
2817 gcc_assert (lhs.type != ADDRESSOF);
2819 /* We shouldn't add constraints from things that cannot have pointers.
2820 It's not completely trivial to avoid in the callers, so do it here. */
2821 if (rhs.type != ADDRESSOF
2822 && !get_varinfo (rhs.var)->may_have_pointers)
2823 return;
2825 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2826 if (!get_varinfo (lhs.var)->may_have_pointers)
2827 return;
2829 /* This can happen in our IR with things like n->a = *p */
2830 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
2832 /* Split into tmp = *rhs, *lhs = tmp */
2833 struct constraint_expr tmplhs;
2834 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp");
2835 process_constraint (new_constraint (tmplhs, rhs));
2836 process_constraint (new_constraint (lhs, tmplhs));
2838 else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
2840 /* Split into tmp = &rhs, *lhs = tmp */
2841 struct constraint_expr tmplhs;
2842 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp");
2843 process_constraint (new_constraint (tmplhs, rhs));
2844 process_constraint (new_constraint (lhs, tmplhs));
2846 else
2848 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
2849 constraints.safe_push (t);
2854 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2855 structure. */
2857 static HOST_WIDE_INT
2858 bitpos_of_field (const tree fdecl)
2860 if (!host_integerp (DECL_FIELD_OFFSET (fdecl), 0)
2861 || !host_integerp (DECL_FIELD_BIT_OFFSET (fdecl), 0))
2862 return -1;
2864 return (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
2865 + TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (fdecl)));
2869 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
2870 resulting constraint expressions in *RESULTS. */
2872 static void
2873 get_constraint_for_ptr_offset (tree ptr, tree offset,
2874 vec<ce_s> *results)
2876 struct constraint_expr c;
2877 unsigned int j, n;
2878 HOST_WIDE_INT rhsoffset;
2880 /* If we do not do field-sensitive PTA adding offsets to pointers
2881 does not change the points-to solution. */
2882 if (!use_field_sensitive)
2884 get_constraint_for_rhs (ptr, results);
2885 return;
2888 /* If the offset is not a non-negative integer constant that fits
2889 in a HOST_WIDE_INT, we have to fall back to a conservative
2890 solution which includes all sub-fields of all pointed-to
2891 variables of ptr. */
2892 if (offset == NULL_TREE
2893 || TREE_CODE (offset) != INTEGER_CST)
2894 rhsoffset = UNKNOWN_OFFSET;
2895 else
2897 /* Sign-extend the offset. */
2898 double_int soffset = tree_to_double_int (offset)
2899 .sext (TYPE_PRECISION (TREE_TYPE (offset)));
2900 if (!soffset.fits_shwi ())
2901 rhsoffset = UNKNOWN_OFFSET;
2902 else
2904 /* Make sure the bit-offset also fits. */
2905 HOST_WIDE_INT rhsunitoffset = soffset.low;
2906 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
2907 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
2908 rhsoffset = UNKNOWN_OFFSET;
2912 get_constraint_for_rhs (ptr, results);
2913 if (rhsoffset == 0)
2914 return;
2916 /* As we are eventually appending to the solution do not use
2917 vec::iterate here. */
2918 n = results->length ();
2919 for (j = 0; j < n; j++)
2921 varinfo_t curr;
2922 c = (*results)[j];
2923 curr = get_varinfo (c.var);
2925 if (c.type == ADDRESSOF
2926 /* If this varinfo represents a full variable just use it. */
2927 && curr->is_full_var)
2928 c.offset = 0;
2929 else if (c.type == ADDRESSOF
2930 /* If we do not know the offset add all subfields. */
2931 && rhsoffset == UNKNOWN_OFFSET)
2933 varinfo_t temp = lookup_vi_for_tree (curr->decl);
2936 struct constraint_expr c2;
2937 c2.var = temp->id;
2938 c2.type = ADDRESSOF;
2939 c2.offset = 0;
2940 if (c2.var != c.var)
2941 results->safe_push (c2);
2942 temp = temp->next;
2944 while (temp);
2946 else if (c.type == ADDRESSOF)
2948 varinfo_t temp;
2949 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
2951 /* Search the sub-field which overlaps with the
2952 pointed-to offset. If the result is outside of the variable
2953 we have to provide a conservative result, as the variable is
2954 still reachable from the resulting pointer (even though it
2955 technically cannot point to anything). The last and first
2956 sub-fields are such conservative results.
2957 ??? If we always had a sub-field for &object + 1 then
2958 we could represent this in a more precise way. */
2959 if (rhsoffset < 0
2960 && curr->offset < offset)
2961 offset = 0;
2962 temp = first_or_preceding_vi_for_offset (curr, offset);
2964 /* If the found variable is not exactly at the pointed to
2965 result, we have to include the next variable in the
2966 solution as well. Otherwise two increments by offset / 2
2967 do not result in the same or a conservative superset
2968 solution. */
2969 if (temp->offset != offset
2970 && temp->next != NULL)
2972 struct constraint_expr c2;
2973 c2.var = temp->next->id;
2974 c2.type = ADDRESSOF;
2975 c2.offset = 0;
2976 results->safe_push (c2);
2978 c.var = temp->id;
2979 c.offset = 0;
2981 else
2982 c.offset = rhsoffset;
2984 (*results)[j] = c;
2989 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
2990 If address_p is true the result will be taken its address of.
2991 If lhs_p is true then the constraint expression is assumed to be used
2992 as the lhs. */
2994 static void
2995 get_constraint_for_component_ref (tree t, vec<ce_s> *results,
2996 bool address_p, bool lhs_p)
2998 tree orig_t = t;
2999 HOST_WIDE_INT bitsize = -1;
3000 HOST_WIDE_INT bitmaxsize = -1;
3001 HOST_WIDE_INT bitpos;
3002 tree forzero;
3004 /* Some people like to do cute things like take the address of
3005 &0->a.b */
3006 forzero = t;
3007 while (handled_component_p (forzero)
3008 || INDIRECT_REF_P (forzero)
3009 || TREE_CODE (forzero) == MEM_REF)
3010 forzero = TREE_OPERAND (forzero, 0);
3012 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
3014 struct constraint_expr temp;
3016 temp.offset = 0;
3017 temp.var = integer_id;
3018 temp.type = SCALAR;
3019 results->safe_push (temp);
3020 return;
3023 /* Handle type-punning through unions. If we are extracting a pointer
3024 from a union via a possibly type-punning access that pointer
3025 points to anything, similar to a conversion of an integer to
3026 a pointer. */
3027 if (!lhs_p)
3029 tree u;
3030 for (u = t;
3031 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3032 u = TREE_OPERAND (u, 0))
3033 if (TREE_CODE (u) == COMPONENT_REF
3034 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3036 struct constraint_expr temp;
3038 temp.offset = 0;
3039 temp.var = anything_id;
3040 temp.type = ADDRESSOF;
3041 results->safe_push (temp);
3042 return;
3046 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
3048 /* Pretend to take the address of the base, we'll take care of
3049 adding the required subset of sub-fields below. */
3050 get_constraint_for_1 (t, results, true, lhs_p);
3051 gcc_assert (results->length () == 1);
3052 struct constraint_expr &result = results->last ();
3054 if (result.type == SCALAR
3055 && get_varinfo (result.var)->is_full_var)
3056 /* For single-field vars do not bother about the offset. */
3057 result.offset = 0;
3058 else if (result.type == SCALAR)
3060 /* In languages like C, you can access one past the end of an
3061 array. You aren't allowed to dereference it, so we can
3062 ignore this constraint. When we handle pointer subtraction,
3063 we may have to do something cute here. */
3065 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result.var)->fullsize
3066 && bitmaxsize != 0)
3068 /* It's also not true that the constraint will actually start at the
3069 right offset, it may start in some padding. We only care about
3070 setting the constraint to the first actual field it touches, so
3071 walk to find it. */
3072 struct constraint_expr cexpr = result;
3073 varinfo_t curr;
3074 results->pop ();
3075 cexpr.offset = 0;
3076 for (curr = get_varinfo (cexpr.var); curr; curr = curr->next)
3078 if (ranges_overlap_p (curr->offset, curr->size,
3079 bitpos, bitmaxsize))
3081 cexpr.var = curr->id;
3082 results->safe_push (cexpr);
3083 if (address_p)
3084 break;
3087 /* If we are going to take the address of this field then
3088 to be able to compute reachability correctly add at least
3089 the last field of the variable. */
3090 if (address_p && results->length () == 0)
3092 curr = get_varinfo (cexpr.var);
3093 while (curr->next != NULL)
3094 curr = curr->next;
3095 cexpr.var = curr->id;
3096 results->safe_push (cexpr);
3098 else if (results->length () == 0)
3099 /* Assert that we found *some* field there. The user couldn't be
3100 accessing *only* padding. */
3101 /* Still the user could access one past the end of an array
3102 embedded in a struct resulting in accessing *only* padding. */
3103 /* Or accessing only padding via type-punning to a type
3104 that has a filed just in padding space. */
3106 cexpr.type = SCALAR;
3107 cexpr.var = anything_id;
3108 cexpr.offset = 0;
3109 results->safe_push (cexpr);
3112 else if (bitmaxsize == 0)
3114 if (dump_file && (dump_flags & TDF_DETAILS))
3115 fprintf (dump_file, "Access to zero-sized part of variable,"
3116 "ignoring\n");
3118 else
3119 if (dump_file && (dump_flags & TDF_DETAILS))
3120 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3122 else if (result.type == DEREF)
3124 /* If we do not know exactly where the access goes say so. Note
3125 that only for non-structure accesses we know that we access
3126 at most one subfiled of any variable. */
3127 if (bitpos == -1
3128 || bitsize != bitmaxsize
3129 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
3130 || result.offset == UNKNOWN_OFFSET)
3131 result.offset = UNKNOWN_OFFSET;
3132 else
3133 result.offset += bitpos;
3135 else if (result.type == ADDRESSOF)
3137 /* We can end up here for component references on a
3138 VIEW_CONVERT_EXPR <>(&foobar). */
3139 result.type = SCALAR;
3140 result.var = anything_id;
3141 result.offset = 0;
3143 else
3144 gcc_unreachable ();
3148 /* Dereference the constraint expression CONS, and return the result.
3149 DEREF (ADDRESSOF) = SCALAR
3150 DEREF (SCALAR) = DEREF
3151 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3152 This is needed so that we can handle dereferencing DEREF constraints. */
3154 static void
3155 do_deref (vec<ce_s> *constraints)
3157 struct constraint_expr *c;
3158 unsigned int i = 0;
3160 FOR_EACH_VEC_ELT (*constraints, i, c)
3162 if (c->type == SCALAR)
3163 c->type = DEREF;
3164 else if (c->type == ADDRESSOF)
3165 c->type = SCALAR;
3166 else if (c->type == DEREF)
3168 struct constraint_expr tmplhs;
3169 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp");
3170 process_constraint (new_constraint (tmplhs, *c));
3171 c->var = tmplhs.var;
3173 else
3174 gcc_unreachable ();
3178 /* Given a tree T, return the constraint expression for taking the
3179 address of it. */
3181 static void
3182 get_constraint_for_address_of (tree t, vec<ce_s> *results)
3184 struct constraint_expr *c;
3185 unsigned int i;
3187 get_constraint_for_1 (t, results, true, true);
3189 FOR_EACH_VEC_ELT (*results, i, c)
3191 if (c->type == DEREF)
3192 c->type = SCALAR;
3193 else
3194 c->type = ADDRESSOF;
3198 /* Given a tree T, return the constraint expression for it. */
3200 static void
3201 get_constraint_for_1 (tree t, vec<ce_s> *results, bool address_p,
3202 bool lhs_p)
3204 struct constraint_expr temp;
3206 /* x = integer is all glommed to a single variable, which doesn't
3207 point to anything by itself. That is, of course, unless it is an
3208 integer constant being treated as a pointer, in which case, we
3209 will return that this is really the addressof anything. This
3210 happens below, since it will fall into the default case. The only
3211 case we know something about an integer treated like a pointer is
3212 when it is the NULL pointer, and then we just say it points to
3213 NULL.
3215 Do not do that if -fno-delete-null-pointer-checks though, because
3216 in that case *NULL does not fail, so it _should_ alias *anything.
3217 It is not worth adding a new option or renaming the existing one,
3218 since this case is relatively obscure. */
3219 if ((TREE_CODE (t) == INTEGER_CST
3220 && integer_zerop (t))
3221 /* The only valid CONSTRUCTORs in gimple with pointer typed
3222 elements are zero-initializer. But in IPA mode we also
3223 process global initializers, so verify at least. */
3224 || (TREE_CODE (t) == CONSTRUCTOR
3225 && CONSTRUCTOR_NELTS (t) == 0))
3227 if (flag_delete_null_pointer_checks)
3228 temp.var = nothing_id;
3229 else
3230 temp.var = nonlocal_id;
3231 temp.type = ADDRESSOF;
3232 temp.offset = 0;
3233 results->safe_push (temp);
3234 return;
3237 /* String constants are read-only. */
3238 if (TREE_CODE (t) == STRING_CST)
3240 temp.var = readonly_id;
3241 temp.type = SCALAR;
3242 temp.offset = 0;
3243 results->safe_push (temp);
3244 return;
3247 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3249 case tcc_expression:
3251 switch (TREE_CODE (t))
3253 case ADDR_EXPR:
3254 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3255 return;
3256 default:;
3258 break;
3260 case tcc_reference:
3262 switch (TREE_CODE (t))
3264 case MEM_REF:
3266 struct constraint_expr cs;
3267 varinfo_t vi, curr;
3268 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0),
3269 TREE_OPERAND (t, 1), results);
3270 do_deref (results);
3272 /* If we are not taking the address then make sure to process
3273 all subvariables we might access. */
3274 if (address_p)
3275 return;
3277 cs = results->last ();
3278 if (cs.type == DEREF
3279 && type_can_have_subvars (TREE_TYPE (t)))
3281 /* For dereferences this means we have to defer it
3282 to solving time. */
3283 results->last ().offset = UNKNOWN_OFFSET;
3284 return;
3286 if (cs.type != SCALAR)
3287 return;
3289 vi = get_varinfo (cs.var);
3290 curr = vi->next;
3291 if (!vi->is_full_var
3292 && curr)
3294 unsigned HOST_WIDE_INT size;
3295 if (host_integerp (TYPE_SIZE (TREE_TYPE (t)), 1))
3296 size = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (t)));
3297 else
3298 size = -1;
3299 for (; curr; curr = curr->next)
3301 if (curr->offset - vi->offset < size)
3303 cs.var = curr->id;
3304 results->safe_push (cs);
3306 else
3307 break;
3310 return;
3312 case ARRAY_REF:
3313 case ARRAY_RANGE_REF:
3314 case COMPONENT_REF:
3315 get_constraint_for_component_ref (t, results, address_p, lhs_p);
3316 return;
3317 case VIEW_CONVERT_EXPR:
3318 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p,
3319 lhs_p);
3320 return;
3321 /* We are missing handling for TARGET_MEM_REF here. */
3322 default:;
3324 break;
3326 case tcc_exceptional:
3328 switch (TREE_CODE (t))
3330 case SSA_NAME:
3332 get_constraint_for_ssa_var (t, results, address_p);
3333 return;
3335 case CONSTRUCTOR:
3337 unsigned int i;
3338 tree val;
3339 vec<ce_s> tmp = vNULL;
3340 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
3342 struct constraint_expr *rhsp;
3343 unsigned j;
3344 get_constraint_for_1 (val, &tmp, address_p, lhs_p);
3345 FOR_EACH_VEC_ELT (tmp, j, rhsp)
3346 results->safe_push (*rhsp);
3347 tmp.truncate (0);
3349 tmp.release ();
3350 /* We do not know whether the constructor was complete,
3351 so technically we have to add &NOTHING or &ANYTHING
3352 like we do for an empty constructor as well. */
3353 return;
3355 default:;
3357 break;
3359 case tcc_declaration:
3361 get_constraint_for_ssa_var (t, results, address_p);
3362 return;
3364 case tcc_constant:
3366 /* We cannot refer to automatic variables through constants. */
3367 temp.type = ADDRESSOF;
3368 temp.var = nonlocal_id;
3369 temp.offset = 0;
3370 results->safe_push (temp);
3371 return;
3373 default:;
3376 /* The default fallback is a constraint from anything. */
3377 temp.type = ADDRESSOF;
3378 temp.var = anything_id;
3379 temp.offset = 0;
3380 results->safe_push (temp);
3383 /* Given a gimple tree T, return the constraint expression vector for it. */
3385 static void
3386 get_constraint_for (tree t, vec<ce_s> *results)
3388 gcc_assert (results->length () == 0);
3390 get_constraint_for_1 (t, results, false, true);
3393 /* Given a gimple tree T, return the constraint expression vector for it
3394 to be used as the rhs of a constraint. */
3396 static void
3397 get_constraint_for_rhs (tree t, vec<ce_s> *results)
3399 gcc_assert (results->length () == 0);
3401 get_constraint_for_1 (t, results, false, false);
3405 /* Efficiently generates constraints from all entries in *RHSC to all
3406 entries in *LHSC. */
3408 static void
3409 process_all_all_constraints (vec<ce_s> lhsc,
3410 vec<ce_s> rhsc)
3412 struct constraint_expr *lhsp, *rhsp;
3413 unsigned i, j;
3415 if (lhsc.length () <= 1 || rhsc.length () <= 1)
3417 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3418 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
3419 process_constraint (new_constraint (*lhsp, *rhsp));
3421 else
3423 struct constraint_expr tmp;
3424 tmp = new_scalar_tmp_constraint_exp ("allalltmp");
3425 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
3426 process_constraint (new_constraint (tmp, *rhsp));
3427 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3428 process_constraint (new_constraint (*lhsp, tmp));
3432 /* Handle aggregate copies by expanding into copies of the respective
3433 fields of the structures. */
3435 static void
3436 do_structure_copy (tree lhsop, tree rhsop)
3438 struct constraint_expr *lhsp, *rhsp;
3439 vec<ce_s> lhsc = vNULL;
3440 vec<ce_s> rhsc = vNULL;
3441 unsigned j;
3443 get_constraint_for (lhsop, &lhsc);
3444 get_constraint_for_rhs (rhsop, &rhsc);
3445 lhsp = &lhsc[0];
3446 rhsp = &rhsc[0];
3447 if (lhsp->type == DEREF
3448 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3449 || rhsp->type == DEREF)
3451 if (lhsp->type == DEREF)
3453 gcc_assert (lhsc.length () == 1);
3454 lhsp->offset = UNKNOWN_OFFSET;
3456 if (rhsp->type == DEREF)
3458 gcc_assert (rhsc.length () == 1);
3459 rhsp->offset = UNKNOWN_OFFSET;
3461 process_all_all_constraints (lhsc, rhsc);
3463 else if (lhsp->type == SCALAR
3464 && (rhsp->type == SCALAR
3465 || rhsp->type == ADDRESSOF))
3467 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3468 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
3469 unsigned k = 0;
3470 get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize);
3471 get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize);
3472 for (j = 0; lhsc.iterate (j, &lhsp);)
3474 varinfo_t lhsv, rhsv;
3475 rhsp = &rhsc[k];
3476 lhsv = get_varinfo (lhsp->var);
3477 rhsv = get_varinfo (rhsp->var);
3478 if (lhsv->may_have_pointers
3479 && (lhsv->is_full_var
3480 || rhsv->is_full_var
3481 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3482 rhsv->offset + lhsoffset, rhsv->size)))
3483 process_constraint (new_constraint (*lhsp, *rhsp));
3484 if (!rhsv->is_full_var
3485 && (lhsv->is_full_var
3486 || (lhsv->offset + rhsoffset + lhsv->size
3487 > rhsv->offset + lhsoffset + rhsv->size)))
3489 ++k;
3490 if (k >= rhsc.length ())
3491 break;
3493 else
3494 ++j;
3497 else
3498 gcc_unreachable ();
3500 lhsc.release ();
3501 rhsc.release ();
3504 /* Create constraints ID = { rhsc }. */
3506 static void
3507 make_constraints_to (unsigned id, vec<ce_s> rhsc)
3509 struct constraint_expr *c;
3510 struct constraint_expr includes;
3511 unsigned int j;
3513 includes.var = id;
3514 includes.offset = 0;
3515 includes.type = SCALAR;
3517 FOR_EACH_VEC_ELT (rhsc, j, c)
3518 process_constraint (new_constraint (includes, *c));
3521 /* Create a constraint ID = OP. */
3523 static void
3524 make_constraint_to (unsigned id, tree op)
3526 vec<ce_s> rhsc = vNULL;
3527 get_constraint_for_rhs (op, &rhsc);
3528 make_constraints_to (id, rhsc);
3529 rhsc.release ();
3532 /* Create a constraint ID = &FROM. */
3534 static void
3535 make_constraint_from (varinfo_t vi, int from)
3537 struct constraint_expr lhs, rhs;
3539 lhs.var = vi->id;
3540 lhs.offset = 0;
3541 lhs.type = SCALAR;
3543 rhs.var = from;
3544 rhs.offset = 0;
3545 rhs.type = ADDRESSOF;
3546 process_constraint (new_constraint (lhs, rhs));
3549 /* Create a constraint ID = FROM. */
3551 static void
3552 make_copy_constraint (varinfo_t vi, int from)
3554 struct constraint_expr lhs, rhs;
3556 lhs.var = vi->id;
3557 lhs.offset = 0;
3558 lhs.type = SCALAR;
3560 rhs.var = from;
3561 rhs.offset = 0;
3562 rhs.type = SCALAR;
3563 process_constraint (new_constraint (lhs, rhs));
3566 /* Make constraints necessary to make OP escape. */
3568 static void
3569 make_escape_constraint (tree op)
3571 make_constraint_to (escaped_id, op);
3574 /* Add constraints to that the solution of VI is transitively closed. */
3576 static void
3577 make_transitive_closure_constraints (varinfo_t vi)
3579 struct constraint_expr lhs, rhs;
3581 /* VAR = *VAR; */
3582 lhs.type = SCALAR;
3583 lhs.var = vi->id;
3584 lhs.offset = 0;
3585 rhs.type = DEREF;
3586 rhs.var = vi->id;
3587 rhs.offset = 0;
3588 process_constraint (new_constraint (lhs, rhs));
3590 /* VAR = VAR + UNKNOWN; */
3591 lhs.type = SCALAR;
3592 lhs.var = vi->id;
3593 lhs.offset = 0;
3594 rhs.type = SCALAR;
3595 rhs.var = vi->id;
3596 rhs.offset = UNKNOWN_OFFSET;
3597 process_constraint (new_constraint (lhs, rhs));
3600 /* Temporary storage for fake var decls. */
3601 struct obstack fake_var_decl_obstack;
3603 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3605 static tree
3606 build_fake_var_decl (tree type)
3608 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl);
3609 memset (decl, 0, sizeof (struct tree_var_decl));
3610 TREE_SET_CODE (decl, VAR_DECL);
3611 TREE_TYPE (decl) = type;
3612 DECL_UID (decl) = allocate_decl_uid ();
3613 SET_DECL_PT_UID (decl, -1);
3614 layout_decl (decl, 0);
3615 return decl;
3618 /* Create a new artificial heap variable with NAME.
3619 Return the created variable. */
3621 static varinfo_t
3622 make_heapvar (const char *name)
3624 varinfo_t vi;
3625 tree heapvar;
3627 heapvar = build_fake_var_decl (ptr_type_node);
3628 DECL_EXTERNAL (heapvar) = 1;
3630 vi = new_var_info (heapvar, name);
3631 vi->is_artificial_var = true;
3632 vi->is_heap_var = true;
3633 vi->is_unknown_size_var = true;
3634 vi->offset = 0;
3635 vi->fullsize = ~0;
3636 vi->size = ~0;
3637 vi->is_full_var = true;
3638 insert_vi_for_tree (heapvar, vi);
3640 return vi;
3643 /* Create a new artificial heap variable with NAME and make a
3644 constraint from it to LHS. Set flags according to a tag used
3645 for tracking restrict pointers. */
3647 static varinfo_t
3648 make_constraint_from_restrict (varinfo_t lhs, const char *name)
3650 varinfo_t vi = make_heapvar (name);
3651 vi->is_global_var = 1;
3652 vi->may_have_pointers = 1;
3653 make_constraint_from (lhs, vi->id);
3654 return vi;
3657 /* Create a new artificial heap variable with NAME and make a
3658 constraint from it to LHS. Set flags according to a tag used
3659 for tracking restrict pointers and make the artificial heap
3660 point to global memory. */
3662 static varinfo_t
3663 make_constraint_from_global_restrict (varinfo_t lhs, const char *name)
3665 varinfo_t vi = make_constraint_from_restrict (lhs, name);
3666 make_copy_constraint (vi, nonlocal_id);
3667 return vi;
3670 /* In IPA mode there are varinfos for different aspects of reach
3671 function designator. One for the points-to set of the return
3672 value, one for the variables that are clobbered by the function,
3673 one for its uses and one for each parameter (including a single
3674 glob for remaining variadic arguments). */
3676 enum { fi_clobbers = 1, fi_uses = 2,
3677 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 };
3679 /* Get a constraint for the requested part of a function designator FI
3680 when operating in IPA mode. */
3682 static struct constraint_expr
3683 get_function_part_constraint (varinfo_t fi, unsigned part)
3685 struct constraint_expr c;
3687 gcc_assert (in_ipa_mode);
3689 if (fi->id == anything_id)
3691 /* ??? We probably should have a ANYFN special variable. */
3692 c.var = anything_id;
3693 c.offset = 0;
3694 c.type = SCALAR;
3696 else if (TREE_CODE (fi->decl) == FUNCTION_DECL)
3698 varinfo_t ai = first_vi_for_offset (fi, part);
3699 if (ai)
3700 c.var = ai->id;
3701 else
3702 c.var = anything_id;
3703 c.offset = 0;
3704 c.type = SCALAR;
3706 else
3708 c.var = fi->id;
3709 c.offset = part;
3710 c.type = DEREF;
3713 return c;
3716 /* For non-IPA mode, generate constraints necessary for a call on the
3717 RHS. */
3719 static void
3720 handle_rhs_call (gimple stmt, vec<ce_s> *results)
3722 struct constraint_expr rhsc;
3723 unsigned i;
3724 bool returns_uses = false;
3726 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3728 tree arg = gimple_call_arg (stmt, i);
3729 int flags = gimple_call_arg_flags (stmt, i);
3731 /* If the argument is not used we can ignore it. */
3732 if (flags & EAF_UNUSED)
3733 continue;
3735 /* As we compute ESCAPED context-insensitive we do not gain
3736 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3737 set. The argument would still get clobbered through the
3738 escape solution. */
3739 if ((flags & EAF_NOCLOBBER)
3740 && (flags & EAF_NOESCAPE))
3742 varinfo_t uses = get_call_use_vi (stmt);
3743 if (!(flags & EAF_DIRECT))
3745 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3746 make_constraint_to (tem->id, arg);
3747 make_transitive_closure_constraints (tem);
3748 make_copy_constraint (uses, tem->id);
3750 else
3751 make_constraint_to (uses->id, arg);
3752 returns_uses = true;
3754 else if (flags & EAF_NOESCAPE)
3756 struct constraint_expr lhs, rhs;
3757 varinfo_t uses = get_call_use_vi (stmt);
3758 varinfo_t clobbers = get_call_clobber_vi (stmt);
3759 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3760 make_constraint_to (tem->id, arg);
3761 if (!(flags & EAF_DIRECT))
3762 make_transitive_closure_constraints (tem);
3763 make_copy_constraint (uses, tem->id);
3764 make_copy_constraint (clobbers, tem->id);
3765 /* Add *tem = nonlocal, do not add *tem = callused as
3766 EAF_NOESCAPE parameters do not escape to other parameters
3767 and all other uses appear in NONLOCAL as well. */
3768 lhs.type = DEREF;
3769 lhs.var = tem->id;
3770 lhs.offset = 0;
3771 rhs.type = SCALAR;
3772 rhs.var = nonlocal_id;
3773 rhs.offset = 0;
3774 process_constraint (new_constraint (lhs, rhs));
3775 returns_uses = true;
3777 else
3778 make_escape_constraint (arg);
3781 /* If we added to the calls uses solution make sure we account for
3782 pointers to it to be returned. */
3783 if (returns_uses)
3785 rhsc.var = get_call_use_vi (stmt)->id;
3786 rhsc.offset = 0;
3787 rhsc.type = SCALAR;
3788 results->safe_push (rhsc);
3791 /* The static chain escapes as well. */
3792 if (gimple_call_chain (stmt))
3793 make_escape_constraint (gimple_call_chain (stmt));
3795 /* And if we applied NRV the address of the return slot escapes as well. */
3796 if (gimple_call_return_slot_opt_p (stmt)
3797 && gimple_call_lhs (stmt) != NULL_TREE
3798 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3800 vec<ce_s> tmpc = vNULL;
3801 struct constraint_expr lhsc, *c;
3802 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3803 lhsc.var = escaped_id;
3804 lhsc.offset = 0;
3805 lhsc.type = SCALAR;
3806 FOR_EACH_VEC_ELT (tmpc, i, c)
3807 process_constraint (new_constraint (lhsc, *c));
3808 tmpc.release ();
3811 /* Regular functions return nonlocal memory. */
3812 rhsc.var = nonlocal_id;
3813 rhsc.offset = 0;
3814 rhsc.type = SCALAR;
3815 results->safe_push (rhsc);
3818 /* For non-IPA mode, generate constraints necessary for a call
3819 that returns a pointer and assigns it to LHS. This simply makes
3820 the LHS point to global and escaped variables. */
3822 static void
3823 handle_lhs_call (gimple stmt, tree lhs, int flags, vec<ce_s> rhsc,
3824 tree fndecl)
3826 vec<ce_s> lhsc = vNULL;
3828 get_constraint_for (lhs, &lhsc);
3829 /* If the store is to a global decl make sure to
3830 add proper escape constraints. */
3831 lhs = get_base_address (lhs);
3832 if (lhs
3833 && DECL_P (lhs)
3834 && is_global_var (lhs))
3836 struct constraint_expr tmpc;
3837 tmpc.var = escaped_id;
3838 tmpc.offset = 0;
3839 tmpc.type = SCALAR;
3840 lhsc.safe_push (tmpc);
3843 /* If the call returns an argument unmodified override the rhs
3844 constraints. */
3845 flags = gimple_call_return_flags (stmt);
3846 if (flags & ERF_RETURNS_ARG
3847 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
3849 tree arg;
3850 rhsc.create (0);
3851 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
3852 get_constraint_for (arg, &rhsc);
3853 process_all_all_constraints (lhsc, rhsc);
3854 rhsc.release ();
3856 else if (flags & ERF_NOALIAS)
3858 varinfo_t vi;
3859 struct constraint_expr tmpc;
3860 rhsc.create (0);
3861 vi = make_heapvar ("HEAP");
3862 /* We delay marking allocated storage global until we know if
3863 it escapes. */
3864 DECL_EXTERNAL (vi->decl) = 0;
3865 vi->is_global_var = 0;
3866 /* If this is not a real malloc call assume the memory was
3867 initialized and thus may point to global memory. All
3868 builtin functions with the malloc attribute behave in a sane way. */
3869 if (!fndecl
3870 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3871 make_constraint_from (vi, nonlocal_id);
3872 tmpc.var = vi->id;
3873 tmpc.offset = 0;
3874 tmpc.type = ADDRESSOF;
3875 rhsc.safe_push (tmpc);
3876 process_all_all_constraints (lhsc, rhsc);
3877 rhsc.release ();
3879 else
3880 process_all_all_constraints (lhsc, rhsc);
3882 lhsc.release ();
3885 /* For non-IPA mode, generate constraints necessary for a call of a
3886 const function that returns a pointer in the statement STMT. */
3888 static void
3889 handle_const_call (gimple stmt, vec<ce_s> *results)
3891 struct constraint_expr rhsc;
3892 unsigned int k;
3894 /* Treat nested const functions the same as pure functions as far
3895 as the static chain is concerned. */
3896 if (gimple_call_chain (stmt))
3898 varinfo_t uses = get_call_use_vi (stmt);
3899 make_transitive_closure_constraints (uses);
3900 make_constraint_to (uses->id, gimple_call_chain (stmt));
3901 rhsc.var = uses->id;
3902 rhsc.offset = 0;
3903 rhsc.type = SCALAR;
3904 results->safe_push (rhsc);
3907 /* May return arguments. */
3908 for (k = 0; k < gimple_call_num_args (stmt); ++k)
3910 tree arg = gimple_call_arg (stmt, k);
3911 vec<ce_s> argc = vNULL;
3912 unsigned i;
3913 struct constraint_expr *argp;
3914 get_constraint_for_rhs (arg, &argc);
3915 FOR_EACH_VEC_ELT (argc, i, argp)
3916 results->safe_push (*argp);
3917 argc.release ();
3920 /* May return addresses of globals. */
3921 rhsc.var = nonlocal_id;
3922 rhsc.offset = 0;
3923 rhsc.type = ADDRESSOF;
3924 results->safe_push (rhsc);
3927 /* For non-IPA mode, generate constraints necessary for a call to a
3928 pure function in statement STMT. */
3930 static void
3931 handle_pure_call (gimple stmt, vec<ce_s> *results)
3933 struct constraint_expr rhsc;
3934 unsigned i;
3935 varinfo_t uses = NULL;
3937 /* Memory reached from pointer arguments is call-used. */
3938 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3940 tree arg = gimple_call_arg (stmt, i);
3941 if (!uses)
3943 uses = get_call_use_vi (stmt);
3944 make_transitive_closure_constraints (uses);
3946 make_constraint_to (uses->id, arg);
3949 /* The static chain is used as well. */
3950 if (gimple_call_chain (stmt))
3952 if (!uses)
3954 uses = get_call_use_vi (stmt);
3955 make_transitive_closure_constraints (uses);
3957 make_constraint_to (uses->id, gimple_call_chain (stmt));
3960 /* Pure functions may return call-used and nonlocal memory. */
3961 if (uses)
3963 rhsc.var = uses->id;
3964 rhsc.offset = 0;
3965 rhsc.type = SCALAR;
3966 results->safe_push (rhsc);
3968 rhsc.var = nonlocal_id;
3969 rhsc.offset = 0;
3970 rhsc.type = SCALAR;
3971 results->safe_push (rhsc);
3975 /* Return the varinfo for the callee of CALL. */
3977 static varinfo_t
3978 get_fi_for_callee (gimple call)
3980 tree decl, fn = gimple_call_fn (call);
3982 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
3983 fn = OBJ_TYPE_REF_EXPR (fn);
3985 /* If we can directly resolve the function being called, do so.
3986 Otherwise, it must be some sort of indirect expression that
3987 we should still be able to handle. */
3988 decl = gimple_call_addr_fndecl (fn);
3989 if (decl)
3990 return get_vi_for_tree (decl);
3992 /* If the function is anything other than a SSA name pointer we have no
3993 clue and should be getting ANYFN (well, ANYTHING for now). */
3994 if (!fn || TREE_CODE (fn) != SSA_NAME)
3995 return get_varinfo (anything_id);
3997 if (SSA_NAME_IS_DEFAULT_DEF (fn)
3998 && (TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
3999 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL))
4000 fn = SSA_NAME_VAR (fn);
4002 return get_vi_for_tree (fn);
4005 /* Create constraints for the builtin call T. Return true if the call
4006 was handled, otherwise false. */
4008 static bool
4009 find_func_aliases_for_builtin_call (gimple t)
4011 tree fndecl = gimple_call_fndecl (t);
4012 vec<ce_s> lhsc = vNULL;
4013 vec<ce_s> rhsc = vNULL;
4014 varinfo_t fi;
4016 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4017 /* ??? All builtins that are handled here need to be handled
4018 in the alias-oracle query functions explicitly! */
4019 switch (DECL_FUNCTION_CODE (fndecl))
4021 /* All the following functions return a pointer to the same object
4022 as their first argument points to. The functions do not add
4023 to the ESCAPED solution. The functions make the first argument
4024 pointed to memory point to what the second argument pointed to
4025 memory points to. */
4026 case BUILT_IN_STRCPY:
4027 case BUILT_IN_STRNCPY:
4028 case BUILT_IN_BCOPY:
4029 case BUILT_IN_MEMCPY:
4030 case BUILT_IN_MEMMOVE:
4031 case BUILT_IN_MEMPCPY:
4032 case BUILT_IN_STPCPY:
4033 case BUILT_IN_STPNCPY:
4034 case BUILT_IN_STRCAT:
4035 case BUILT_IN_STRNCAT:
4036 case BUILT_IN_STRCPY_CHK:
4037 case BUILT_IN_STRNCPY_CHK:
4038 case BUILT_IN_MEMCPY_CHK:
4039 case BUILT_IN_MEMMOVE_CHK:
4040 case BUILT_IN_MEMPCPY_CHK:
4041 case BUILT_IN_STPCPY_CHK:
4042 case BUILT_IN_STPNCPY_CHK:
4043 case BUILT_IN_STRCAT_CHK:
4044 case BUILT_IN_STRNCAT_CHK:
4045 case BUILT_IN_TM_MEMCPY:
4046 case BUILT_IN_TM_MEMMOVE:
4048 tree res = gimple_call_lhs (t);
4049 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4050 == BUILT_IN_BCOPY ? 1 : 0));
4051 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4052 == BUILT_IN_BCOPY ? 0 : 1));
4053 if (res != NULL_TREE)
4055 get_constraint_for (res, &lhsc);
4056 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4057 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
4058 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4059 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
4060 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK
4061 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK)
4062 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4063 else
4064 get_constraint_for (dest, &rhsc);
4065 process_all_all_constraints (lhsc, rhsc);
4066 lhsc.release ();
4067 rhsc.release ();
4069 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4070 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4071 do_deref (&lhsc);
4072 do_deref (&rhsc);
4073 process_all_all_constraints (lhsc, rhsc);
4074 lhsc.release ();
4075 rhsc.release ();
4076 return true;
4078 case BUILT_IN_MEMSET:
4079 case BUILT_IN_MEMSET_CHK:
4080 case BUILT_IN_TM_MEMSET:
4082 tree res = gimple_call_lhs (t);
4083 tree dest = gimple_call_arg (t, 0);
4084 unsigned i;
4085 ce_s *lhsp;
4086 struct constraint_expr ac;
4087 if (res != NULL_TREE)
4089 get_constraint_for (res, &lhsc);
4090 get_constraint_for (dest, &rhsc);
4091 process_all_all_constraints (lhsc, rhsc);
4092 lhsc.release ();
4093 rhsc.release ();
4095 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4096 do_deref (&lhsc);
4097 if (flag_delete_null_pointer_checks
4098 && integer_zerop (gimple_call_arg (t, 1)))
4100 ac.type = ADDRESSOF;
4101 ac.var = nothing_id;
4103 else
4105 ac.type = SCALAR;
4106 ac.var = integer_id;
4108 ac.offset = 0;
4109 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4110 process_constraint (new_constraint (*lhsp, ac));
4111 lhsc.release ();
4112 return true;
4114 case BUILT_IN_ASSUME_ALIGNED:
4116 tree res = gimple_call_lhs (t);
4117 tree dest = gimple_call_arg (t, 0);
4118 if (res != NULL_TREE)
4120 get_constraint_for (res, &lhsc);
4121 get_constraint_for (dest, &rhsc);
4122 process_all_all_constraints (lhsc, rhsc);
4123 lhsc.release ();
4124 rhsc.release ();
4126 return true;
4128 /* All the following functions do not return pointers, do not
4129 modify the points-to sets of memory reachable from their
4130 arguments and do not add to the ESCAPED solution. */
4131 case BUILT_IN_SINCOS:
4132 case BUILT_IN_SINCOSF:
4133 case BUILT_IN_SINCOSL:
4134 case BUILT_IN_FREXP:
4135 case BUILT_IN_FREXPF:
4136 case BUILT_IN_FREXPL:
4137 case BUILT_IN_GAMMA_R:
4138 case BUILT_IN_GAMMAF_R:
4139 case BUILT_IN_GAMMAL_R:
4140 case BUILT_IN_LGAMMA_R:
4141 case BUILT_IN_LGAMMAF_R:
4142 case BUILT_IN_LGAMMAL_R:
4143 case BUILT_IN_MODF:
4144 case BUILT_IN_MODFF:
4145 case BUILT_IN_MODFL:
4146 case BUILT_IN_REMQUO:
4147 case BUILT_IN_REMQUOF:
4148 case BUILT_IN_REMQUOL:
4149 case BUILT_IN_FREE:
4150 return true;
4151 case BUILT_IN_STRDUP:
4152 case BUILT_IN_STRNDUP:
4153 if (gimple_call_lhs (t))
4155 handle_lhs_call (t, gimple_call_lhs (t), gimple_call_flags (t),
4156 vNULL, fndecl);
4157 get_constraint_for_ptr_offset (gimple_call_lhs (t),
4158 NULL_TREE, &lhsc);
4159 get_constraint_for_ptr_offset (gimple_call_arg (t, 0),
4160 NULL_TREE, &rhsc);
4161 do_deref (&lhsc);
4162 do_deref (&rhsc);
4163 process_all_all_constraints (lhsc, rhsc);
4164 lhsc.release ();
4165 rhsc.release ();
4166 return true;
4168 break;
4169 /* Trampolines are special - they set up passing the static
4170 frame. */
4171 case BUILT_IN_INIT_TRAMPOLINE:
4173 tree tramp = gimple_call_arg (t, 0);
4174 tree nfunc = gimple_call_arg (t, 1);
4175 tree frame = gimple_call_arg (t, 2);
4176 unsigned i;
4177 struct constraint_expr lhs, *rhsp;
4178 if (in_ipa_mode)
4180 varinfo_t nfi = NULL;
4181 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR);
4182 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0));
4183 if (nfi)
4185 lhs = get_function_part_constraint (nfi, fi_static_chain);
4186 get_constraint_for (frame, &rhsc);
4187 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4188 process_constraint (new_constraint (lhs, *rhsp));
4189 rhsc.release ();
4191 /* Make the frame point to the function for
4192 the trampoline adjustment call. */
4193 get_constraint_for (tramp, &lhsc);
4194 do_deref (&lhsc);
4195 get_constraint_for (nfunc, &rhsc);
4196 process_all_all_constraints (lhsc, rhsc);
4197 rhsc.release ();
4198 lhsc.release ();
4200 return true;
4203 /* Else fallthru to generic handling which will let
4204 the frame escape. */
4205 break;
4207 case BUILT_IN_ADJUST_TRAMPOLINE:
4209 tree tramp = gimple_call_arg (t, 0);
4210 tree res = gimple_call_lhs (t);
4211 if (in_ipa_mode && res)
4213 get_constraint_for (res, &lhsc);
4214 get_constraint_for (tramp, &rhsc);
4215 do_deref (&rhsc);
4216 process_all_all_constraints (lhsc, rhsc);
4217 rhsc.release ();
4218 lhsc.release ();
4220 return true;
4222 CASE_BUILT_IN_TM_STORE (1):
4223 CASE_BUILT_IN_TM_STORE (2):
4224 CASE_BUILT_IN_TM_STORE (4):
4225 CASE_BUILT_IN_TM_STORE (8):
4226 CASE_BUILT_IN_TM_STORE (FLOAT):
4227 CASE_BUILT_IN_TM_STORE (DOUBLE):
4228 CASE_BUILT_IN_TM_STORE (LDOUBLE):
4229 CASE_BUILT_IN_TM_STORE (M64):
4230 CASE_BUILT_IN_TM_STORE (M128):
4231 CASE_BUILT_IN_TM_STORE (M256):
4233 tree addr = gimple_call_arg (t, 0);
4234 tree src = gimple_call_arg (t, 1);
4236 get_constraint_for (addr, &lhsc);
4237 do_deref (&lhsc);
4238 get_constraint_for (src, &rhsc);
4239 process_all_all_constraints (lhsc, rhsc);
4240 lhsc.release ();
4241 rhsc.release ();
4242 return true;
4244 CASE_BUILT_IN_TM_LOAD (1):
4245 CASE_BUILT_IN_TM_LOAD (2):
4246 CASE_BUILT_IN_TM_LOAD (4):
4247 CASE_BUILT_IN_TM_LOAD (8):
4248 CASE_BUILT_IN_TM_LOAD (FLOAT):
4249 CASE_BUILT_IN_TM_LOAD (DOUBLE):
4250 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
4251 CASE_BUILT_IN_TM_LOAD (M64):
4252 CASE_BUILT_IN_TM_LOAD (M128):
4253 CASE_BUILT_IN_TM_LOAD (M256):
4255 tree dest = gimple_call_lhs (t);
4256 tree addr = gimple_call_arg (t, 0);
4258 get_constraint_for (dest, &lhsc);
4259 get_constraint_for (addr, &rhsc);
4260 do_deref (&rhsc);
4261 process_all_all_constraints (lhsc, rhsc);
4262 lhsc.release ();
4263 rhsc.release ();
4264 return true;
4266 /* Variadic argument handling needs to be handled in IPA
4267 mode as well. */
4268 case BUILT_IN_VA_START:
4270 tree valist = gimple_call_arg (t, 0);
4271 struct constraint_expr rhs, *lhsp;
4272 unsigned i;
4273 get_constraint_for (valist, &lhsc);
4274 do_deref (&lhsc);
4275 /* The va_list gets access to pointers in variadic
4276 arguments. Which we know in the case of IPA analysis
4277 and otherwise are just all nonlocal variables. */
4278 if (in_ipa_mode)
4280 fi = lookup_vi_for_tree (cfun->decl);
4281 rhs = get_function_part_constraint (fi, ~0);
4282 rhs.type = ADDRESSOF;
4284 else
4286 rhs.var = nonlocal_id;
4287 rhs.type = ADDRESSOF;
4288 rhs.offset = 0;
4290 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4291 process_constraint (new_constraint (*lhsp, rhs));
4292 lhsc.release ();
4293 /* va_list is clobbered. */
4294 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4295 return true;
4297 /* va_end doesn't have any effect that matters. */
4298 case BUILT_IN_VA_END:
4299 return true;
4300 /* Alternate return. Simply give up for now. */
4301 case BUILT_IN_RETURN:
4303 fi = NULL;
4304 if (!in_ipa_mode
4305 || !(fi = get_vi_for_tree (cfun->decl)))
4306 make_constraint_from (get_varinfo (escaped_id), anything_id);
4307 else if (in_ipa_mode
4308 && fi != NULL)
4310 struct constraint_expr lhs, rhs;
4311 lhs = get_function_part_constraint (fi, fi_result);
4312 rhs.var = anything_id;
4313 rhs.offset = 0;
4314 rhs.type = SCALAR;
4315 process_constraint (new_constraint (lhs, rhs));
4317 return true;
4319 /* printf-style functions may have hooks to set pointers to
4320 point to somewhere into the generated string. Leave them
4321 for a later excercise... */
4322 default:
4323 /* Fallthru to general call handling. */;
4326 return false;
4329 /* Create constraints for the call T. */
4331 static void
4332 find_func_aliases_for_call (gimple t)
4334 tree fndecl = gimple_call_fndecl (t);
4335 vec<ce_s> lhsc = vNULL;
4336 vec<ce_s> rhsc = vNULL;
4337 varinfo_t fi;
4339 if (fndecl != NULL_TREE
4340 && DECL_BUILT_IN (fndecl)
4341 && find_func_aliases_for_builtin_call (t))
4342 return;
4344 fi = get_fi_for_callee (t);
4345 if (!in_ipa_mode
4346 || (fndecl && !fi->is_fn_info))
4348 vec<ce_s> rhsc = vNULL;
4349 int flags = gimple_call_flags (t);
4351 /* Const functions can return their arguments and addresses
4352 of global memory but not of escaped memory. */
4353 if (flags & (ECF_CONST|ECF_NOVOPS))
4355 if (gimple_call_lhs (t))
4356 handle_const_call (t, &rhsc);
4358 /* Pure functions can return addresses in and of memory
4359 reachable from their arguments, but they are not an escape
4360 point for reachable memory of their arguments. */
4361 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4362 handle_pure_call (t, &rhsc);
4363 else
4364 handle_rhs_call (t, &rhsc);
4365 if (gimple_call_lhs (t))
4366 handle_lhs_call (t, gimple_call_lhs (t), flags, rhsc, fndecl);
4367 rhsc.release ();
4369 else
4371 tree lhsop;
4372 unsigned j;
4374 /* Assign all the passed arguments to the appropriate incoming
4375 parameters of the function. */
4376 for (j = 0; j < gimple_call_num_args (t); j++)
4378 struct constraint_expr lhs ;
4379 struct constraint_expr *rhsp;
4380 tree arg = gimple_call_arg (t, j);
4382 get_constraint_for_rhs (arg, &rhsc);
4383 lhs = get_function_part_constraint (fi, fi_parm_base + j);
4384 while (rhsc.length () != 0)
4386 rhsp = &rhsc.last ();
4387 process_constraint (new_constraint (lhs, *rhsp));
4388 rhsc.pop ();
4392 /* If we are returning a value, assign it to the result. */
4393 lhsop = gimple_call_lhs (t);
4394 if (lhsop)
4396 struct constraint_expr rhs;
4397 struct constraint_expr *lhsp;
4399 get_constraint_for (lhsop, &lhsc);
4400 rhs = get_function_part_constraint (fi, fi_result);
4401 if (fndecl
4402 && DECL_RESULT (fndecl)
4403 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4405 vec<ce_s> tem = vNULL;
4406 tem.safe_push (rhs);
4407 do_deref (&tem);
4408 rhs = tem[0];
4409 tem.release ();
4411 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4412 process_constraint (new_constraint (*lhsp, rhs));
4415 /* If we pass the result decl by reference, honor that. */
4416 if (lhsop
4417 && fndecl
4418 && DECL_RESULT (fndecl)
4419 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4421 struct constraint_expr lhs;
4422 struct constraint_expr *rhsp;
4424 get_constraint_for_address_of (lhsop, &rhsc);
4425 lhs = get_function_part_constraint (fi, fi_result);
4426 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4427 process_constraint (new_constraint (lhs, *rhsp));
4428 rhsc.release ();
4431 /* If we use a static chain, pass it along. */
4432 if (gimple_call_chain (t))
4434 struct constraint_expr lhs;
4435 struct constraint_expr *rhsp;
4437 get_constraint_for (gimple_call_chain (t), &rhsc);
4438 lhs = get_function_part_constraint (fi, fi_static_chain);
4439 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4440 process_constraint (new_constraint (lhs, *rhsp));
4445 /* Walk statement T setting up aliasing constraints according to the
4446 references found in T. This function is the main part of the
4447 constraint builder. AI points to auxiliary alias information used
4448 when building alias sets and computing alias grouping heuristics. */
4450 static void
4451 find_func_aliases (gimple origt)
4453 gimple t = origt;
4454 vec<ce_s> lhsc = vNULL;
4455 vec<ce_s> rhsc = vNULL;
4456 struct constraint_expr *c;
4457 varinfo_t fi;
4459 /* Now build constraints expressions. */
4460 if (gimple_code (t) == GIMPLE_PHI)
4462 size_t i;
4463 unsigned int j;
4465 /* For a phi node, assign all the arguments to
4466 the result. */
4467 get_constraint_for (gimple_phi_result (t), &lhsc);
4468 for (i = 0; i < gimple_phi_num_args (t); i++)
4470 tree strippedrhs = PHI_ARG_DEF (t, i);
4472 STRIP_NOPS (strippedrhs);
4473 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
4475 FOR_EACH_VEC_ELT (lhsc, j, c)
4477 struct constraint_expr *c2;
4478 while (rhsc.length () > 0)
4480 c2 = &rhsc.last ();
4481 process_constraint (new_constraint (*c, *c2));
4482 rhsc.pop ();
4487 /* In IPA mode, we need to generate constraints to pass call
4488 arguments through their calls. There are two cases,
4489 either a GIMPLE_CALL returning a value, or just a plain
4490 GIMPLE_CALL when we are not.
4492 In non-ipa mode, we need to generate constraints for each
4493 pointer passed by address. */
4494 else if (is_gimple_call (t))
4495 find_func_aliases_for_call (t);
4497 /* Otherwise, just a regular assignment statement. Only care about
4498 operations with pointer result, others are dealt with as escape
4499 points if they have pointer operands. */
4500 else if (is_gimple_assign (t))
4502 /* Otherwise, just a regular assignment statement. */
4503 tree lhsop = gimple_assign_lhs (t);
4504 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
4506 if (rhsop && TREE_CLOBBER_P (rhsop))
4507 /* Ignore clobbers, they don't actually store anything into
4508 the LHS. */
4510 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
4511 do_structure_copy (lhsop, rhsop);
4512 else
4514 enum tree_code code = gimple_assign_rhs_code (t);
4516 get_constraint_for (lhsop, &lhsc);
4518 if (code == POINTER_PLUS_EXPR)
4519 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4520 gimple_assign_rhs2 (t), &rhsc);
4521 else if (code == BIT_AND_EXPR
4522 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
4524 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4525 the pointer. Handle it by offsetting it by UNKNOWN. */
4526 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4527 NULL_TREE, &rhsc);
4529 else if ((CONVERT_EXPR_CODE_P (code)
4530 && !(POINTER_TYPE_P (gimple_expr_type (t))
4531 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
4532 || gimple_assign_single_p (t))
4533 get_constraint_for_rhs (rhsop, &rhsc);
4534 else if (code == COND_EXPR)
4536 /* The result is a merge of both COND_EXPR arms. */
4537 vec<ce_s> tmp = vNULL;
4538 struct constraint_expr *rhsp;
4539 unsigned i;
4540 get_constraint_for_rhs (gimple_assign_rhs2 (t), &rhsc);
4541 get_constraint_for_rhs (gimple_assign_rhs3 (t), &tmp);
4542 FOR_EACH_VEC_ELT (tmp, i, rhsp)
4543 rhsc.safe_push (*rhsp);
4544 tmp.release ();
4546 else if (truth_value_p (code))
4547 /* Truth value results are not pointer (parts). Or at least
4548 very very unreasonable obfuscation of a part. */
4550 else
4552 /* All other operations are merges. */
4553 vec<ce_s> tmp = vNULL;
4554 struct constraint_expr *rhsp;
4555 unsigned i, j;
4556 get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc);
4557 for (i = 2; i < gimple_num_ops (t); ++i)
4559 get_constraint_for_rhs (gimple_op (t, i), &tmp);
4560 FOR_EACH_VEC_ELT (tmp, j, rhsp)
4561 rhsc.safe_push (*rhsp);
4562 tmp.truncate (0);
4564 tmp.release ();
4566 process_all_all_constraints (lhsc, rhsc);
4568 /* If there is a store to a global variable the rhs escapes. */
4569 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
4570 && DECL_P (lhsop)
4571 && is_global_var (lhsop)
4572 && (!in_ipa_mode
4573 || DECL_EXTERNAL (lhsop) || TREE_PUBLIC (lhsop)))
4574 make_escape_constraint (rhsop);
4576 /* Handle escapes through return. */
4577 else if (gimple_code (t) == GIMPLE_RETURN
4578 && gimple_return_retval (t) != NULL_TREE)
4580 fi = NULL;
4581 if (!in_ipa_mode
4582 || !(fi = get_vi_for_tree (cfun->decl)))
4583 make_escape_constraint (gimple_return_retval (t));
4584 else if (in_ipa_mode
4585 && fi != NULL)
4587 struct constraint_expr lhs ;
4588 struct constraint_expr *rhsp;
4589 unsigned i;
4591 lhs = get_function_part_constraint (fi, fi_result);
4592 get_constraint_for_rhs (gimple_return_retval (t), &rhsc);
4593 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4594 process_constraint (new_constraint (lhs, *rhsp));
4597 /* Handle asms conservatively by adding escape constraints to everything. */
4598 else if (gimple_code (t) == GIMPLE_ASM)
4600 unsigned i, noutputs;
4601 const char **oconstraints;
4602 const char *constraint;
4603 bool allows_mem, allows_reg, is_inout;
4605 noutputs = gimple_asm_noutputs (t);
4606 oconstraints = XALLOCAVEC (const char *, noutputs);
4608 for (i = 0; i < noutputs; ++i)
4610 tree link = gimple_asm_output_op (t, i);
4611 tree op = TREE_VALUE (link);
4613 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4614 oconstraints[i] = constraint;
4615 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4616 &allows_reg, &is_inout);
4618 /* A memory constraint makes the address of the operand escape. */
4619 if (!allows_reg && allows_mem)
4620 make_escape_constraint (build_fold_addr_expr (op));
4622 /* The asm may read global memory, so outputs may point to
4623 any global memory. */
4624 if (op)
4626 vec<ce_s> lhsc = vNULL;
4627 struct constraint_expr rhsc, *lhsp;
4628 unsigned j;
4629 get_constraint_for (op, &lhsc);
4630 rhsc.var = nonlocal_id;
4631 rhsc.offset = 0;
4632 rhsc.type = SCALAR;
4633 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4634 process_constraint (new_constraint (*lhsp, rhsc));
4635 lhsc.release ();
4638 for (i = 0; i < gimple_asm_ninputs (t); ++i)
4640 tree link = gimple_asm_input_op (t, i);
4641 tree op = TREE_VALUE (link);
4643 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4645 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4646 &allows_mem, &allows_reg);
4648 /* A memory constraint makes the address of the operand escape. */
4649 if (!allows_reg && allows_mem)
4650 make_escape_constraint (build_fold_addr_expr (op));
4651 /* Strictly we'd only need the constraint to ESCAPED if
4652 the asm clobbers memory, otherwise using something
4653 along the lines of per-call clobbers/uses would be enough. */
4654 else if (op)
4655 make_escape_constraint (op);
4659 rhsc.release ();
4660 lhsc.release ();
4664 /* Create a constraint adding to the clobber set of FI the memory
4665 pointed to by PTR. */
4667 static void
4668 process_ipa_clobber (varinfo_t fi, tree ptr)
4670 vec<ce_s> ptrc = vNULL;
4671 struct constraint_expr *c, lhs;
4672 unsigned i;
4673 get_constraint_for_rhs (ptr, &ptrc);
4674 lhs = get_function_part_constraint (fi, fi_clobbers);
4675 FOR_EACH_VEC_ELT (ptrc, i, c)
4676 process_constraint (new_constraint (lhs, *c));
4677 ptrc.release ();
4680 /* Walk statement T setting up clobber and use constraints according to the
4681 references found in T. This function is a main part of the
4682 IPA constraint builder. */
4684 static void
4685 find_func_clobbers (gimple origt)
4687 gimple t = origt;
4688 vec<ce_s> lhsc = vNULL;
4689 vec<ce_s> rhsc = vNULL;
4690 varinfo_t fi;
4692 /* Add constraints for clobbered/used in IPA mode.
4693 We are not interested in what automatic variables are clobbered
4694 or used as we only use the information in the caller to which
4695 they do not escape. */
4696 gcc_assert (in_ipa_mode);
4698 /* If the stmt refers to memory in any way it better had a VUSE. */
4699 if (gimple_vuse (t) == NULL_TREE)
4700 return;
4702 /* We'd better have function information for the current function. */
4703 fi = lookup_vi_for_tree (cfun->decl);
4704 gcc_assert (fi != NULL);
4706 /* Account for stores in assignments and calls. */
4707 if (gimple_vdef (t) != NULL_TREE
4708 && gimple_has_lhs (t))
4710 tree lhs = gimple_get_lhs (t);
4711 tree tem = lhs;
4712 while (handled_component_p (tem))
4713 tem = TREE_OPERAND (tem, 0);
4714 if ((DECL_P (tem)
4715 && !auto_var_in_fn_p (tem, cfun->decl))
4716 || INDIRECT_REF_P (tem)
4717 || (TREE_CODE (tem) == MEM_REF
4718 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4719 && auto_var_in_fn_p
4720 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4722 struct constraint_expr lhsc, *rhsp;
4723 unsigned i;
4724 lhsc = get_function_part_constraint (fi, fi_clobbers);
4725 get_constraint_for_address_of (lhs, &rhsc);
4726 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4727 process_constraint (new_constraint (lhsc, *rhsp));
4728 rhsc.release ();
4732 /* Account for uses in assigments and returns. */
4733 if (gimple_assign_single_p (t)
4734 || (gimple_code (t) == GIMPLE_RETURN
4735 && gimple_return_retval (t) != NULL_TREE))
4737 tree rhs = (gimple_assign_single_p (t)
4738 ? gimple_assign_rhs1 (t) : gimple_return_retval (t));
4739 tree tem = rhs;
4740 while (handled_component_p (tem))
4741 tem = TREE_OPERAND (tem, 0);
4742 if ((DECL_P (tem)
4743 && !auto_var_in_fn_p (tem, cfun->decl))
4744 || INDIRECT_REF_P (tem)
4745 || (TREE_CODE (tem) == MEM_REF
4746 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4747 && auto_var_in_fn_p
4748 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4750 struct constraint_expr lhs, *rhsp;
4751 unsigned i;
4752 lhs = get_function_part_constraint (fi, fi_uses);
4753 get_constraint_for_address_of (rhs, &rhsc);
4754 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4755 process_constraint (new_constraint (lhs, *rhsp));
4756 rhsc.release ();
4760 if (is_gimple_call (t))
4762 varinfo_t cfi = NULL;
4763 tree decl = gimple_call_fndecl (t);
4764 struct constraint_expr lhs, rhs;
4765 unsigned i, j;
4767 /* For builtins we do not have separate function info. For those
4768 we do not generate escapes for we have to generate clobbers/uses. */
4769 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4770 switch (DECL_FUNCTION_CODE (decl))
4772 /* The following functions use and clobber memory pointed to
4773 by their arguments. */
4774 case BUILT_IN_STRCPY:
4775 case BUILT_IN_STRNCPY:
4776 case BUILT_IN_BCOPY:
4777 case BUILT_IN_MEMCPY:
4778 case BUILT_IN_MEMMOVE:
4779 case BUILT_IN_MEMPCPY:
4780 case BUILT_IN_STPCPY:
4781 case BUILT_IN_STPNCPY:
4782 case BUILT_IN_STRCAT:
4783 case BUILT_IN_STRNCAT:
4784 case BUILT_IN_STRCPY_CHK:
4785 case BUILT_IN_STRNCPY_CHK:
4786 case BUILT_IN_MEMCPY_CHK:
4787 case BUILT_IN_MEMMOVE_CHK:
4788 case BUILT_IN_MEMPCPY_CHK:
4789 case BUILT_IN_STPCPY_CHK:
4790 case BUILT_IN_STPNCPY_CHK:
4791 case BUILT_IN_STRCAT_CHK:
4792 case BUILT_IN_STRNCAT_CHK:
4794 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4795 == BUILT_IN_BCOPY ? 1 : 0));
4796 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4797 == BUILT_IN_BCOPY ? 0 : 1));
4798 unsigned i;
4799 struct constraint_expr *rhsp, *lhsp;
4800 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4801 lhs = get_function_part_constraint (fi, fi_clobbers);
4802 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4803 process_constraint (new_constraint (lhs, *lhsp));
4804 lhsc.release ();
4805 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4806 lhs = get_function_part_constraint (fi, fi_uses);
4807 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4808 process_constraint (new_constraint (lhs, *rhsp));
4809 rhsc.release ();
4810 return;
4812 /* The following function clobbers memory pointed to by
4813 its argument. */
4814 case BUILT_IN_MEMSET:
4815 case BUILT_IN_MEMSET_CHK:
4817 tree dest = gimple_call_arg (t, 0);
4818 unsigned i;
4819 ce_s *lhsp;
4820 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4821 lhs = get_function_part_constraint (fi, fi_clobbers);
4822 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4823 process_constraint (new_constraint (lhs, *lhsp));
4824 lhsc.release ();
4825 return;
4827 /* The following functions clobber their second and third
4828 arguments. */
4829 case BUILT_IN_SINCOS:
4830 case BUILT_IN_SINCOSF:
4831 case BUILT_IN_SINCOSL:
4833 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4834 process_ipa_clobber (fi, gimple_call_arg (t, 2));
4835 return;
4837 /* The following functions clobber their second argument. */
4838 case BUILT_IN_FREXP:
4839 case BUILT_IN_FREXPF:
4840 case BUILT_IN_FREXPL:
4841 case BUILT_IN_LGAMMA_R:
4842 case BUILT_IN_LGAMMAF_R:
4843 case BUILT_IN_LGAMMAL_R:
4844 case BUILT_IN_GAMMA_R:
4845 case BUILT_IN_GAMMAF_R:
4846 case BUILT_IN_GAMMAL_R:
4847 case BUILT_IN_MODF:
4848 case BUILT_IN_MODFF:
4849 case BUILT_IN_MODFL:
4851 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4852 return;
4854 /* The following functions clobber their third argument. */
4855 case BUILT_IN_REMQUO:
4856 case BUILT_IN_REMQUOF:
4857 case BUILT_IN_REMQUOL:
4859 process_ipa_clobber (fi, gimple_call_arg (t, 2));
4860 return;
4862 /* The following functions neither read nor clobber memory. */
4863 case BUILT_IN_ASSUME_ALIGNED:
4864 case BUILT_IN_FREE:
4865 return;
4866 /* Trampolines are of no interest to us. */
4867 case BUILT_IN_INIT_TRAMPOLINE:
4868 case BUILT_IN_ADJUST_TRAMPOLINE:
4869 return;
4870 case BUILT_IN_VA_START:
4871 case BUILT_IN_VA_END:
4872 return;
4873 /* printf-style functions may have hooks to set pointers to
4874 point to somewhere into the generated string. Leave them
4875 for a later excercise... */
4876 default:
4877 /* Fallthru to general call handling. */;
4880 /* Parameters passed by value are used. */
4881 lhs = get_function_part_constraint (fi, fi_uses);
4882 for (i = 0; i < gimple_call_num_args (t); i++)
4884 struct constraint_expr *rhsp;
4885 tree arg = gimple_call_arg (t, i);
4887 if (TREE_CODE (arg) == SSA_NAME
4888 || is_gimple_min_invariant (arg))
4889 continue;
4891 get_constraint_for_address_of (arg, &rhsc);
4892 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4893 process_constraint (new_constraint (lhs, *rhsp));
4894 rhsc.release ();
4897 /* Build constraints for propagating clobbers/uses along the
4898 callgraph edges. */
4899 cfi = get_fi_for_callee (t);
4900 if (cfi->id == anything_id)
4902 if (gimple_vdef (t))
4903 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
4904 anything_id);
4905 make_constraint_from (first_vi_for_offset (fi, fi_uses),
4906 anything_id);
4907 return;
4910 /* For callees without function info (that's external functions),
4911 ESCAPED is clobbered and used. */
4912 if (gimple_call_fndecl (t)
4913 && !cfi->is_fn_info)
4915 varinfo_t vi;
4917 if (gimple_vdef (t))
4918 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
4919 escaped_id);
4920 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
4922 /* Also honor the call statement use/clobber info. */
4923 if ((vi = lookup_call_clobber_vi (t)) != NULL)
4924 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
4925 vi->id);
4926 if ((vi = lookup_call_use_vi (t)) != NULL)
4927 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
4928 vi->id);
4929 return;
4932 /* Otherwise the caller clobbers and uses what the callee does.
4933 ??? This should use a new complex constraint that filters
4934 local variables of the callee. */
4935 if (gimple_vdef (t))
4937 lhs = get_function_part_constraint (fi, fi_clobbers);
4938 rhs = get_function_part_constraint (cfi, fi_clobbers);
4939 process_constraint (new_constraint (lhs, rhs));
4941 lhs = get_function_part_constraint (fi, fi_uses);
4942 rhs = get_function_part_constraint (cfi, fi_uses);
4943 process_constraint (new_constraint (lhs, rhs));
4945 else if (gimple_code (t) == GIMPLE_ASM)
4947 /* ??? Ick. We can do better. */
4948 if (gimple_vdef (t))
4949 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
4950 anything_id);
4951 make_constraint_from (first_vi_for_offset (fi, fi_uses),
4952 anything_id);
4955 rhsc.release ();
4959 /* Find the first varinfo in the same variable as START that overlaps with
4960 OFFSET. Return NULL if we can't find one. */
4962 static varinfo_t
4963 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
4965 /* If the offset is outside of the variable, bail out. */
4966 if (offset >= start->fullsize)
4967 return NULL;
4969 /* If we cannot reach offset from start, lookup the first field
4970 and start from there. */
4971 if (start->offset > offset)
4972 start = lookup_vi_for_tree (start->decl);
4974 while (start)
4976 /* We may not find a variable in the field list with the actual
4977 offset when when we have glommed a structure to a variable.
4978 In that case, however, offset should still be within the size
4979 of the variable. */
4980 if (offset >= start->offset
4981 && (offset - start->offset) < start->size)
4982 return start;
4984 start= start->next;
4987 return NULL;
4990 /* Find the first varinfo in the same variable as START that overlaps with
4991 OFFSET. If there is no such varinfo the varinfo directly preceding
4992 OFFSET is returned. */
4994 static varinfo_t
4995 first_or_preceding_vi_for_offset (varinfo_t start,
4996 unsigned HOST_WIDE_INT offset)
4998 /* If we cannot reach offset from start, lookup the first field
4999 and start from there. */
5000 if (start->offset > offset)
5001 start = lookup_vi_for_tree (start->decl);
5003 /* We may not find a variable in the field list with the actual
5004 offset when when we have glommed a structure to a variable.
5005 In that case, however, offset should still be within the size
5006 of the variable.
5007 If we got beyond the offset we look for return the field
5008 directly preceding offset which may be the last field. */
5009 while (start->next
5010 && offset >= start->offset
5011 && !((offset - start->offset) < start->size))
5012 start = start->next;
5014 return start;
5018 /* This structure is used during pushing fields onto the fieldstack
5019 to track the offset of the field, since bitpos_of_field gives it
5020 relative to its immediate containing type, and we want it relative
5021 to the ultimate containing object. */
5023 struct fieldoff
5025 /* Offset from the base of the base containing object to this field. */
5026 HOST_WIDE_INT offset;
5028 /* Size, in bits, of the field. */
5029 unsigned HOST_WIDE_INT size;
5031 unsigned has_unknown_size : 1;
5033 unsigned must_have_pointers : 1;
5035 unsigned may_have_pointers : 1;
5037 unsigned only_restrict_pointers : 1;
5039 typedef struct fieldoff fieldoff_s;
5042 /* qsort comparison function for two fieldoff's PA and PB */
5044 static int
5045 fieldoff_compare (const void *pa, const void *pb)
5047 const fieldoff_s *foa = (const fieldoff_s *)pa;
5048 const fieldoff_s *fob = (const fieldoff_s *)pb;
5049 unsigned HOST_WIDE_INT foasize, fobsize;
5051 if (foa->offset < fob->offset)
5052 return -1;
5053 else if (foa->offset > fob->offset)
5054 return 1;
5056 foasize = foa->size;
5057 fobsize = fob->size;
5058 if (foasize < fobsize)
5059 return -1;
5060 else if (foasize > fobsize)
5061 return 1;
5062 return 0;
5065 /* Sort a fieldstack according to the field offset and sizes. */
5066 static void
5067 sort_fieldstack (vec<fieldoff_s> fieldstack)
5069 fieldstack.qsort (fieldoff_compare);
5072 /* Return true if T is a type that can have subvars. */
5074 static inline bool
5075 type_can_have_subvars (const_tree t)
5077 /* Aggregates without overlapping fields can have subvars. */
5078 return TREE_CODE (t) == RECORD_TYPE;
5081 /* Return true if V is a tree that we can have subvars for.
5082 Normally, this is any aggregate type. Also complex
5083 types which are not gimple registers can have subvars. */
5085 static inline bool
5086 var_can_have_subvars (const_tree v)
5088 /* Volatile variables should never have subvars. */
5089 if (TREE_THIS_VOLATILE (v))
5090 return false;
5092 /* Non decls or memory tags can never have subvars. */
5093 if (!DECL_P (v))
5094 return false;
5096 return type_can_have_subvars (TREE_TYPE (v));
5099 /* Return true if T is a type that does contain pointers. */
5101 static bool
5102 type_must_have_pointers (tree type)
5104 if (POINTER_TYPE_P (type))
5105 return true;
5107 if (TREE_CODE (type) == ARRAY_TYPE)
5108 return type_must_have_pointers (TREE_TYPE (type));
5110 /* A function or method can have pointers as arguments, so track
5111 those separately. */
5112 if (TREE_CODE (type) == FUNCTION_TYPE
5113 || TREE_CODE (type) == METHOD_TYPE)
5114 return true;
5116 return false;
5119 static bool
5120 field_must_have_pointers (tree t)
5122 return type_must_have_pointers (TREE_TYPE (t));
5125 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5126 the fields of TYPE onto fieldstack, recording their offsets along
5127 the way.
5129 OFFSET is used to keep track of the offset in this entire
5130 structure, rather than just the immediately containing structure.
5131 Returns false if the caller is supposed to handle the field we
5132 recursed for. */
5134 static bool
5135 push_fields_onto_fieldstack (tree type, vec<fieldoff_s> *fieldstack,
5136 HOST_WIDE_INT offset)
5138 tree field;
5139 bool empty_p = true;
5141 if (TREE_CODE (type) != RECORD_TYPE)
5142 return false;
5144 /* If the vector of fields is growing too big, bail out early.
5145 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5146 sure this fails. */
5147 if (fieldstack->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5148 return false;
5150 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5151 if (TREE_CODE (field) == FIELD_DECL)
5153 bool push = false;
5154 HOST_WIDE_INT foff = bitpos_of_field (field);
5156 if (!var_can_have_subvars (field)
5157 || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
5158 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5159 push = true;
5160 else if (!push_fields_onto_fieldstack
5161 (TREE_TYPE (field), fieldstack, offset + foff)
5162 && (DECL_SIZE (field)
5163 && !integer_zerop (DECL_SIZE (field))))
5164 /* Empty structures may have actual size, like in C++. So
5165 see if we didn't push any subfields and the size is
5166 nonzero, push the field onto the stack. */
5167 push = true;
5169 if (push)
5171 fieldoff_s *pair = NULL;
5172 bool has_unknown_size = false;
5173 bool must_have_pointers_p;
5175 if (!fieldstack->is_empty ())
5176 pair = &fieldstack->last ();
5178 /* If there isn't anything at offset zero, create sth. */
5179 if (!pair
5180 && offset + foff != 0)
5182 fieldoff_s e = {0, offset + foff, false, false, false, false};
5183 pair = fieldstack->safe_push (e);
5186 if (!DECL_SIZE (field)
5187 || !host_integerp (DECL_SIZE (field), 1))
5188 has_unknown_size = true;
5190 /* If adjacent fields do not contain pointers merge them. */
5191 must_have_pointers_p = field_must_have_pointers (field);
5192 if (pair
5193 && !has_unknown_size
5194 && !must_have_pointers_p
5195 && !pair->must_have_pointers
5196 && !pair->has_unknown_size
5197 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5199 pair->size += TREE_INT_CST_LOW (DECL_SIZE (field));
5201 else
5203 fieldoff_s e;
5204 e.offset = offset + foff;
5205 e.has_unknown_size = has_unknown_size;
5206 if (!has_unknown_size)
5207 e.size = TREE_INT_CST_LOW (DECL_SIZE (field));
5208 else
5209 e.size = -1;
5210 e.must_have_pointers = must_have_pointers_p;
5211 e.may_have_pointers = true;
5212 e.only_restrict_pointers
5213 = (!has_unknown_size
5214 && POINTER_TYPE_P (TREE_TYPE (field))
5215 && TYPE_RESTRICT (TREE_TYPE (field)));
5216 fieldstack->safe_push (e);
5220 empty_p = false;
5223 return !empty_p;
5226 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5227 if it is a varargs function. */
5229 static unsigned int
5230 count_num_arguments (tree decl, bool *is_varargs)
5232 unsigned int num = 0;
5233 tree t;
5235 /* Capture named arguments for K&R functions. They do not
5236 have a prototype and thus no TYPE_ARG_TYPES. */
5237 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5238 ++num;
5240 /* Check if the function has variadic arguments. */
5241 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5242 if (TREE_VALUE (t) == void_type_node)
5243 break;
5244 if (!t)
5245 *is_varargs = true;
5247 return num;
5250 /* Creation function node for DECL, using NAME, and return the index
5251 of the variable we've created for the function. */
5253 static varinfo_t
5254 create_function_info_for (tree decl, const char *name)
5256 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5257 varinfo_t vi, prev_vi;
5258 tree arg;
5259 unsigned int i;
5260 bool is_varargs = false;
5261 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5263 /* Create the variable info. */
5265 vi = new_var_info (decl, name);
5266 vi->offset = 0;
5267 vi->size = 1;
5268 vi->fullsize = fi_parm_base + num_args;
5269 vi->is_fn_info = 1;
5270 vi->may_have_pointers = false;
5271 if (is_varargs)
5272 vi->fullsize = ~0;
5273 insert_vi_for_tree (vi->decl, vi);
5275 prev_vi = vi;
5277 /* Create a variable for things the function clobbers and one for
5278 things the function uses. */
5280 varinfo_t clobbervi, usevi;
5281 const char *newname;
5282 char *tempname;
5284 asprintf (&tempname, "%s.clobber", name);
5285 newname = ggc_strdup (tempname);
5286 free (tempname);
5288 clobbervi = new_var_info (NULL, newname);
5289 clobbervi->offset = fi_clobbers;
5290 clobbervi->size = 1;
5291 clobbervi->fullsize = vi->fullsize;
5292 clobbervi->is_full_var = true;
5293 clobbervi->is_global_var = false;
5294 gcc_assert (prev_vi->offset < clobbervi->offset);
5295 prev_vi->next = clobbervi;
5296 prev_vi = clobbervi;
5298 asprintf (&tempname, "%s.use", name);
5299 newname = ggc_strdup (tempname);
5300 free (tempname);
5302 usevi = new_var_info (NULL, newname);
5303 usevi->offset = fi_uses;
5304 usevi->size = 1;
5305 usevi->fullsize = vi->fullsize;
5306 usevi->is_full_var = true;
5307 usevi->is_global_var = false;
5308 gcc_assert (prev_vi->offset < usevi->offset);
5309 prev_vi->next = usevi;
5310 prev_vi = usevi;
5313 /* And one for the static chain. */
5314 if (fn->static_chain_decl != NULL_TREE)
5316 varinfo_t chainvi;
5317 const char *newname;
5318 char *tempname;
5320 asprintf (&tempname, "%s.chain", name);
5321 newname = ggc_strdup (tempname);
5322 free (tempname);
5324 chainvi = new_var_info (fn->static_chain_decl, newname);
5325 chainvi->offset = fi_static_chain;
5326 chainvi->size = 1;
5327 chainvi->fullsize = vi->fullsize;
5328 chainvi->is_full_var = true;
5329 chainvi->is_global_var = false;
5330 gcc_assert (prev_vi->offset < chainvi->offset);
5331 prev_vi->next = chainvi;
5332 prev_vi = chainvi;
5333 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5336 /* Create a variable for the return var. */
5337 if (DECL_RESULT (decl) != NULL
5338 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5340 varinfo_t resultvi;
5341 const char *newname;
5342 char *tempname;
5343 tree resultdecl = decl;
5345 if (DECL_RESULT (decl))
5346 resultdecl = DECL_RESULT (decl);
5348 asprintf (&tempname, "%s.result", name);
5349 newname = ggc_strdup (tempname);
5350 free (tempname);
5352 resultvi = new_var_info (resultdecl, newname);
5353 resultvi->offset = fi_result;
5354 resultvi->size = 1;
5355 resultvi->fullsize = vi->fullsize;
5356 resultvi->is_full_var = true;
5357 if (DECL_RESULT (decl))
5358 resultvi->may_have_pointers = true;
5359 gcc_assert (prev_vi->offset < resultvi->offset);
5360 prev_vi->next = resultvi;
5361 prev_vi = resultvi;
5362 if (DECL_RESULT (decl))
5363 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
5366 /* Set up variables for each argument. */
5367 arg = DECL_ARGUMENTS (decl);
5368 for (i = 0; i < num_args; i++)
5370 varinfo_t argvi;
5371 const char *newname;
5372 char *tempname;
5373 tree argdecl = decl;
5375 if (arg)
5376 argdecl = arg;
5378 asprintf (&tempname, "%s.arg%d", name, i);
5379 newname = ggc_strdup (tempname);
5380 free (tempname);
5382 argvi = new_var_info (argdecl, newname);
5383 argvi->offset = fi_parm_base + i;
5384 argvi->size = 1;
5385 argvi->is_full_var = true;
5386 argvi->fullsize = vi->fullsize;
5387 if (arg)
5388 argvi->may_have_pointers = true;
5389 gcc_assert (prev_vi->offset < argvi->offset);
5390 prev_vi->next = argvi;
5391 prev_vi = argvi;
5392 if (arg)
5394 insert_vi_for_tree (arg, argvi);
5395 arg = DECL_CHAIN (arg);
5399 /* Add one representative for all further args. */
5400 if (is_varargs)
5402 varinfo_t argvi;
5403 const char *newname;
5404 char *tempname;
5405 tree decl;
5407 asprintf (&tempname, "%s.varargs", name);
5408 newname = ggc_strdup (tempname);
5409 free (tempname);
5411 /* We need sth that can be pointed to for va_start. */
5412 decl = build_fake_var_decl (ptr_type_node);
5414 argvi = new_var_info (decl, newname);
5415 argvi->offset = fi_parm_base + num_args;
5416 argvi->size = ~0;
5417 argvi->is_full_var = true;
5418 argvi->is_heap_var = true;
5419 argvi->fullsize = vi->fullsize;
5420 gcc_assert (prev_vi->offset < argvi->offset);
5421 prev_vi->next = argvi;
5422 prev_vi = argvi;
5425 return vi;
5429 /* Return true if FIELDSTACK contains fields that overlap.
5430 FIELDSTACK is assumed to be sorted by offset. */
5432 static bool
5433 check_for_overlaps (vec<fieldoff_s> fieldstack)
5435 fieldoff_s *fo = NULL;
5436 unsigned int i;
5437 HOST_WIDE_INT lastoffset = -1;
5439 FOR_EACH_VEC_ELT (fieldstack, i, fo)
5441 if (fo->offset == lastoffset)
5442 return true;
5443 lastoffset = fo->offset;
5445 return false;
5448 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5449 This will also create any varinfo structures necessary for fields
5450 of DECL. */
5452 static varinfo_t
5453 create_variable_info_for_1 (tree decl, const char *name)
5455 varinfo_t vi, newvi;
5456 tree decl_type = TREE_TYPE (decl);
5457 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
5458 vec<fieldoff_s> fieldstack = vNULL;
5459 fieldoff_s *fo;
5460 unsigned int i;
5462 if (!declsize
5463 || !host_integerp (declsize, 1))
5465 vi = new_var_info (decl, name);
5466 vi->offset = 0;
5467 vi->size = ~0;
5468 vi->fullsize = ~0;
5469 vi->is_unknown_size_var = true;
5470 vi->is_full_var = true;
5471 vi->may_have_pointers = true;
5472 return vi;
5475 /* Collect field information. */
5476 if (use_field_sensitive
5477 && var_can_have_subvars (decl)
5478 /* ??? Force us to not use subfields for global initializers
5479 in IPA mode. Else we'd have to parse arbitrary initializers. */
5480 && !(in_ipa_mode
5481 && is_global_var (decl)
5482 && DECL_INITIAL (decl)))
5484 fieldoff_s *fo = NULL;
5485 bool notokay = false;
5486 unsigned int i;
5488 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
5490 for (i = 0; !notokay && fieldstack.iterate (i, &fo); i++)
5491 if (fo->has_unknown_size
5492 || fo->offset < 0)
5494 notokay = true;
5495 break;
5498 /* We can't sort them if we have a field with a variable sized type,
5499 which will make notokay = true. In that case, we are going to return
5500 without creating varinfos for the fields anyway, so sorting them is a
5501 waste to boot. */
5502 if (!notokay)
5504 sort_fieldstack (fieldstack);
5505 /* Due to some C++ FE issues, like PR 22488, we might end up
5506 what appear to be overlapping fields even though they,
5507 in reality, do not overlap. Until the C++ FE is fixed,
5508 we will simply disable field-sensitivity for these cases. */
5509 notokay = check_for_overlaps (fieldstack);
5512 if (notokay)
5513 fieldstack.release ();
5516 /* If we didn't end up collecting sub-variables create a full
5517 variable for the decl. */
5518 if (fieldstack.length () <= 1
5519 || fieldstack.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5521 vi = new_var_info (decl, name);
5522 vi->offset = 0;
5523 vi->may_have_pointers = true;
5524 vi->fullsize = TREE_INT_CST_LOW (declsize);
5525 vi->size = vi->fullsize;
5526 vi->is_full_var = true;
5527 fieldstack.release ();
5528 return vi;
5531 vi = new_var_info (decl, name);
5532 vi->fullsize = TREE_INT_CST_LOW (declsize);
5533 for (i = 0, newvi = vi;
5534 fieldstack.iterate (i, &fo);
5535 ++i, newvi = newvi->next)
5537 const char *newname = "NULL";
5538 char *tempname;
5540 if (dump_file)
5542 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC
5543 "+" HOST_WIDE_INT_PRINT_DEC, name, fo->offset, fo->size);
5544 newname = ggc_strdup (tempname);
5545 free (tempname);
5547 newvi->name = newname;
5548 newvi->offset = fo->offset;
5549 newvi->size = fo->size;
5550 newvi->fullsize = vi->fullsize;
5551 newvi->may_have_pointers = fo->may_have_pointers;
5552 newvi->only_restrict_pointers = fo->only_restrict_pointers;
5553 if (i + 1 < fieldstack.length ())
5554 newvi->next = new_var_info (decl, name);
5557 fieldstack.release ();
5559 return vi;
5562 static unsigned int
5563 create_variable_info_for (tree decl, const char *name)
5565 varinfo_t vi = create_variable_info_for_1 (decl, name);
5566 unsigned int id = vi->id;
5568 insert_vi_for_tree (decl, vi);
5570 if (TREE_CODE (decl) != VAR_DECL)
5571 return id;
5573 /* Create initial constraints for globals. */
5574 for (; vi; vi = vi->next)
5576 if (!vi->may_have_pointers
5577 || !vi->is_global_var)
5578 continue;
5580 /* Mark global restrict qualified pointers. */
5581 if ((POINTER_TYPE_P (TREE_TYPE (decl))
5582 && TYPE_RESTRICT (TREE_TYPE (decl)))
5583 || vi->only_restrict_pointers)
5585 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5586 continue;
5589 /* In non-IPA mode the initializer from nonlocal is all we need. */
5590 if (!in_ipa_mode
5591 || DECL_HARD_REGISTER (decl))
5592 make_copy_constraint (vi, nonlocal_id);
5594 /* In IPA mode parse the initializer and generate proper constraints
5595 for it. */
5596 else
5598 struct varpool_node *vnode = varpool_get_node (decl);
5600 /* For escaped variables initialize them from nonlocal. */
5601 if (!varpool_all_refs_explicit_p (vnode))
5602 make_copy_constraint (vi, nonlocal_id);
5604 /* If this is a global variable with an initializer and we are in
5605 IPA mode generate constraints for it. */
5606 if (DECL_INITIAL (decl)
5607 && vnode->analyzed)
5609 vec<ce_s> rhsc = vNULL;
5610 struct constraint_expr lhs, *rhsp;
5611 unsigned i;
5612 get_constraint_for_rhs (DECL_INITIAL (decl), &rhsc);
5613 lhs.var = vi->id;
5614 lhs.offset = 0;
5615 lhs.type = SCALAR;
5616 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5617 process_constraint (new_constraint (lhs, *rhsp));
5618 /* If this is a variable that escapes from the unit
5619 the initializer escapes as well. */
5620 if (!varpool_all_refs_explicit_p (vnode))
5622 lhs.var = escaped_id;
5623 lhs.offset = 0;
5624 lhs.type = SCALAR;
5625 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5626 process_constraint (new_constraint (lhs, *rhsp));
5628 rhsc.release ();
5633 return id;
5636 /* Print out the points-to solution for VAR to FILE. */
5638 static void
5639 dump_solution_for_var (FILE *file, unsigned int var)
5641 varinfo_t vi = get_varinfo (var);
5642 unsigned int i;
5643 bitmap_iterator bi;
5645 /* Dump the solution for unified vars anyway, this avoids difficulties
5646 in scanning dumps in the testsuite. */
5647 fprintf (file, "%s = { ", vi->name);
5648 vi = get_varinfo (find (var));
5649 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5650 fprintf (file, "%s ", get_varinfo (i)->name);
5651 fprintf (file, "}");
5653 /* But note when the variable was unified. */
5654 if (vi->id != var)
5655 fprintf (file, " same as %s", vi->name);
5657 fprintf (file, "\n");
5660 /* Print the points-to solution for VAR to stdout. */
5662 DEBUG_FUNCTION void
5663 debug_solution_for_var (unsigned int var)
5665 dump_solution_for_var (stdout, var);
5668 /* Create varinfo structures for all of the variables in the
5669 function for intraprocedural mode. */
5671 static void
5672 intra_create_variable_infos (void)
5674 tree t;
5676 /* For each incoming pointer argument arg, create the constraint ARG
5677 = NONLOCAL or a dummy variable if it is a restrict qualified
5678 passed-by-reference argument. */
5679 for (t = DECL_ARGUMENTS (current_function_decl); t; t = DECL_CHAIN (t))
5681 varinfo_t p = get_vi_for_tree (t);
5683 /* For restrict qualified pointers to objects passed by
5684 reference build a real representative for the pointed-to object.
5685 Treat restrict qualified references the same. */
5686 if (TYPE_RESTRICT (TREE_TYPE (t))
5687 && ((DECL_BY_REFERENCE (t) && POINTER_TYPE_P (TREE_TYPE (t)))
5688 || TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5689 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t))))
5691 struct constraint_expr lhsc, rhsc;
5692 varinfo_t vi;
5693 tree heapvar = build_fake_var_decl (TREE_TYPE (TREE_TYPE (t)));
5694 DECL_EXTERNAL (heapvar) = 1;
5695 vi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS");
5696 insert_vi_for_tree (heapvar, vi);
5697 lhsc.var = p->id;
5698 lhsc.type = SCALAR;
5699 lhsc.offset = 0;
5700 rhsc.var = vi->id;
5701 rhsc.type = ADDRESSOF;
5702 rhsc.offset = 0;
5703 process_constraint (new_constraint (lhsc, rhsc));
5704 for (; vi; vi = vi->next)
5705 if (vi->may_have_pointers)
5707 if (vi->only_restrict_pointers)
5708 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5709 else
5710 make_copy_constraint (vi, nonlocal_id);
5712 continue;
5715 if (POINTER_TYPE_P (TREE_TYPE (t))
5716 && TYPE_RESTRICT (TREE_TYPE (t)))
5717 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5718 else
5720 for (; p; p = p->next)
5722 if (p->only_restrict_pointers)
5723 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5724 else if (p->may_have_pointers)
5725 make_constraint_from (p, nonlocal_id);
5730 /* Add a constraint for a result decl that is passed by reference. */
5731 if (DECL_RESULT (cfun->decl)
5732 && DECL_BY_REFERENCE (DECL_RESULT (cfun->decl)))
5734 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (cfun->decl));
5736 for (p = result_vi; p; p = p->next)
5737 make_constraint_from (p, nonlocal_id);
5740 /* Add a constraint for the incoming static chain parameter. */
5741 if (cfun->static_chain_decl != NULL_TREE)
5743 varinfo_t p, chain_vi = get_vi_for_tree (cfun->static_chain_decl);
5745 for (p = chain_vi; p; p = p->next)
5746 make_constraint_from (p, nonlocal_id);
5750 /* Structure used to put solution bitmaps in a hashtable so they can
5751 be shared among variables with the same points-to set. */
5753 typedef struct shared_bitmap_info
5755 bitmap pt_vars;
5756 hashval_t hashcode;
5757 } *shared_bitmap_info_t;
5758 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
5760 static htab_t shared_bitmap_table;
5762 /* Hash function for a shared_bitmap_info_t */
5764 static hashval_t
5765 shared_bitmap_hash (const void *p)
5767 const_shared_bitmap_info_t const bi = (const_shared_bitmap_info_t) p;
5768 return bi->hashcode;
5771 /* Equality function for two shared_bitmap_info_t's. */
5773 static int
5774 shared_bitmap_eq (const void *p1, const void *p2)
5776 const_shared_bitmap_info_t const sbi1 = (const_shared_bitmap_info_t) p1;
5777 const_shared_bitmap_info_t const sbi2 = (const_shared_bitmap_info_t) p2;
5778 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
5781 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5782 existing instance if there is one, NULL otherwise. */
5784 static bitmap
5785 shared_bitmap_lookup (bitmap pt_vars)
5787 void **slot;
5788 struct shared_bitmap_info sbi;
5790 sbi.pt_vars = pt_vars;
5791 sbi.hashcode = bitmap_hash (pt_vars);
5793 slot = htab_find_slot_with_hash (shared_bitmap_table, &sbi,
5794 sbi.hashcode, NO_INSERT);
5795 if (!slot)
5796 return NULL;
5797 else
5798 return ((shared_bitmap_info_t) *slot)->pt_vars;
5802 /* Add a bitmap to the shared bitmap hashtable. */
5804 static void
5805 shared_bitmap_add (bitmap pt_vars)
5807 void **slot;
5808 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
5810 sbi->pt_vars = pt_vars;
5811 sbi->hashcode = bitmap_hash (pt_vars);
5813 slot = htab_find_slot_with_hash (shared_bitmap_table, sbi,
5814 sbi->hashcode, INSERT);
5815 gcc_assert (!*slot);
5816 *slot = (void *) sbi;
5820 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
5822 static void
5823 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt)
5825 unsigned int i;
5826 bitmap_iterator bi;
5828 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
5830 varinfo_t vi = get_varinfo (i);
5832 /* The only artificial variables that are allowed in a may-alias
5833 set are heap variables. */
5834 if (vi->is_artificial_var && !vi->is_heap_var)
5835 continue;
5837 if (TREE_CODE (vi->decl) == VAR_DECL
5838 || TREE_CODE (vi->decl) == PARM_DECL
5839 || TREE_CODE (vi->decl) == RESULT_DECL)
5841 /* If we are in IPA mode we will not recompute points-to
5842 sets after inlining so make sure they stay valid. */
5843 if (in_ipa_mode
5844 && !DECL_PT_UID_SET_P (vi->decl))
5845 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
5847 /* Add the decl to the points-to set. Note that the points-to
5848 set contains global variables. */
5849 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
5850 if (vi->is_global_var)
5851 pt->vars_contains_global = true;
5857 /* Compute the points-to solution *PT for the variable VI. */
5859 static void
5860 find_what_var_points_to (varinfo_t orig_vi, struct pt_solution *pt)
5862 unsigned int i;
5863 bitmap_iterator bi;
5864 bitmap finished_solution;
5865 bitmap result;
5866 varinfo_t vi;
5868 memset (pt, 0, sizeof (struct pt_solution));
5870 /* This variable may have been collapsed, let's get the real
5871 variable. */
5872 vi = get_varinfo (find (orig_vi->id));
5874 /* Translate artificial variables into SSA_NAME_PTR_INFO
5875 attributes. */
5876 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5878 varinfo_t vi = get_varinfo (i);
5880 if (vi->is_artificial_var)
5882 if (vi->id == nothing_id)
5883 pt->null = 1;
5884 else if (vi->id == escaped_id)
5886 if (in_ipa_mode)
5887 pt->ipa_escaped = 1;
5888 else
5889 pt->escaped = 1;
5891 else if (vi->id == nonlocal_id)
5892 pt->nonlocal = 1;
5893 else if (vi->is_heap_var)
5894 /* We represent heapvars in the points-to set properly. */
5896 else if (vi->id == readonly_id)
5897 /* Nobody cares. */
5899 else if (vi->id == anything_id
5900 || vi->id == integer_id)
5901 pt->anything = 1;
5905 /* Instead of doing extra work, simply do not create
5906 elaborate points-to information for pt_anything pointers. */
5907 if (pt->anything)
5908 return;
5910 /* Share the final set of variables when possible. */
5911 finished_solution = BITMAP_GGC_ALLOC ();
5912 stats.points_to_sets_created++;
5914 set_uids_in_ptset (finished_solution, vi->solution, pt);
5915 result = shared_bitmap_lookup (finished_solution);
5916 if (!result)
5918 shared_bitmap_add (finished_solution);
5919 pt->vars = finished_solution;
5921 else
5923 pt->vars = result;
5924 bitmap_clear (finished_solution);
5928 /* Given a pointer variable P, fill in its points-to set. */
5930 static void
5931 find_what_p_points_to (tree p)
5933 struct ptr_info_def *pi;
5934 tree lookup_p = p;
5935 varinfo_t vi;
5937 /* For parameters, get at the points-to set for the actual parm
5938 decl. */
5939 if (TREE_CODE (p) == SSA_NAME
5940 && SSA_NAME_IS_DEFAULT_DEF (p)
5941 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
5942 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL))
5943 lookup_p = SSA_NAME_VAR (p);
5945 vi = lookup_vi_for_tree (lookup_p);
5946 if (!vi)
5947 return;
5949 pi = get_ptr_info (p);
5950 find_what_var_points_to (vi, &pi->pt);
5954 /* Query statistics for points-to solutions. */
5956 static struct {
5957 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
5958 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
5959 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
5960 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
5961 } pta_stats;
5963 void
5964 dump_pta_stats (FILE *s)
5966 fprintf (s, "\nPTA query stats:\n");
5967 fprintf (s, " pt_solution_includes: "
5968 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
5969 HOST_WIDE_INT_PRINT_DEC" queries\n",
5970 pta_stats.pt_solution_includes_no_alias,
5971 pta_stats.pt_solution_includes_no_alias
5972 + pta_stats.pt_solution_includes_may_alias);
5973 fprintf (s, " pt_solutions_intersect: "
5974 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
5975 HOST_WIDE_INT_PRINT_DEC" queries\n",
5976 pta_stats.pt_solutions_intersect_no_alias,
5977 pta_stats.pt_solutions_intersect_no_alias
5978 + pta_stats.pt_solutions_intersect_may_alias);
5982 /* Reset the points-to solution *PT to a conservative default
5983 (point to anything). */
5985 void
5986 pt_solution_reset (struct pt_solution *pt)
5988 memset (pt, 0, sizeof (struct pt_solution));
5989 pt->anything = true;
5992 /* Set the points-to solution *PT to point only to the variables
5993 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
5994 global variables and VARS_CONTAINS_RESTRICT specifies whether
5995 it contains restrict tag variables. */
5997 void
5998 pt_solution_set (struct pt_solution *pt, bitmap vars, bool vars_contains_global)
6000 memset (pt, 0, sizeof (struct pt_solution));
6001 pt->vars = vars;
6002 pt->vars_contains_global = vars_contains_global;
6005 /* Set the points-to solution *PT to point only to the variable VAR. */
6007 void
6008 pt_solution_set_var (struct pt_solution *pt, tree var)
6010 memset (pt, 0, sizeof (struct pt_solution));
6011 pt->vars = BITMAP_GGC_ALLOC ();
6012 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
6013 pt->vars_contains_global = is_global_var (var);
6016 /* Computes the union of the points-to solutions *DEST and *SRC and
6017 stores the result in *DEST. This changes the points-to bitmap
6018 of *DEST and thus may not be used if that might be shared.
6019 The points-to bitmap of *SRC and *DEST will not be shared after
6020 this function if they were not before. */
6022 static void
6023 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6025 dest->anything |= src->anything;
6026 if (dest->anything)
6028 pt_solution_reset (dest);
6029 return;
6032 dest->nonlocal |= src->nonlocal;
6033 dest->escaped |= src->escaped;
6034 dest->ipa_escaped |= src->ipa_escaped;
6035 dest->null |= src->null;
6036 dest->vars_contains_global |= src->vars_contains_global;
6037 if (!src->vars)
6038 return;
6040 if (!dest->vars)
6041 dest->vars = BITMAP_GGC_ALLOC ();
6042 bitmap_ior_into (dest->vars, src->vars);
6045 /* Return true if the points-to solution *PT is empty. */
6047 bool
6048 pt_solution_empty_p (struct pt_solution *pt)
6050 if (pt->anything
6051 || pt->nonlocal)
6052 return false;
6054 if (pt->vars
6055 && !bitmap_empty_p (pt->vars))
6056 return false;
6058 /* If the solution includes ESCAPED, check if that is empty. */
6059 if (pt->escaped
6060 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6061 return false;
6063 /* If the solution includes ESCAPED, check if that is empty. */
6064 if (pt->ipa_escaped
6065 && !pt_solution_empty_p (&ipa_escaped_pt))
6066 return false;
6068 return true;
6071 /* Return true if the points-to solution *PT only point to a single var, and
6072 return the var uid in *UID. */
6074 bool
6075 pt_solution_singleton_p (struct pt_solution *pt, unsigned *uid)
6077 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
6078 || pt->null || pt->vars == NULL
6079 || !bitmap_single_bit_set_p (pt->vars))
6080 return false;
6082 *uid = bitmap_first_set_bit (pt->vars);
6083 return true;
6086 /* Return true if the points-to solution *PT includes global memory. */
6088 bool
6089 pt_solution_includes_global (struct pt_solution *pt)
6091 if (pt->anything
6092 || pt->nonlocal
6093 || pt->vars_contains_global)
6094 return true;
6096 if (pt->escaped)
6097 return pt_solution_includes_global (&cfun->gimple_df->escaped);
6099 if (pt->ipa_escaped)
6100 return pt_solution_includes_global (&ipa_escaped_pt);
6102 /* ??? This predicate is not correct for the IPA-PTA solution
6103 as we do not properly distinguish between unit escape points
6104 and global variables. */
6105 if (cfun->gimple_df->ipa_pta)
6106 return true;
6108 return false;
6111 /* Return true if the points-to solution *PT includes the variable
6112 declaration DECL. */
6114 static bool
6115 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6117 if (pt->anything)
6118 return true;
6120 if (pt->nonlocal
6121 && is_global_var (decl))
6122 return true;
6124 if (pt->vars
6125 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6126 return true;
6128 /* If the solution includes ESCAPED, check it. */
6129 if (pt->escaped
6130 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6131 return true;
6133 /* If the solution includes ESCAPED, check it. */
6134 if (pt->ipa_escaped
6135 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6136 return true;
6138 return false;
6141 bool
6142 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6144 bool res = pt_solution_includes_1 (pt, decl);
6145 if (res)
6146 ++pta_stats.pt_solution_includes_may_alias;
6147 else
6148 ++pta_stats.pt_solution_includes_no_alias;
6149 return res;
6152 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6153 intersection. */
6155 static bool
6156 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
6158 if (pt1->anything || pt2->anything)
6159 return true;
6161 /* If either points to unknown global memory and the other points to
6162 any global memory they alias. */
6163 if ((pt1->nonlocal
6164 && (pt2->nonlocal
6165 || pt2->vars_contains_global))
6166 || (pt2->nonlocal
6167 && pt1->vars_contains_global))
6168 return true;
6170 /* Check the escaped solution if required. */
6171 if ((pt1->escaped || pt2->escaped)
6172 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6174 /* If both point to escaped memory and that solution
6175 is not empty they alias. */
6176 if (pt1->escaped && pt2->escaped)
6177 return true;
6179 /* If either points to escaped memory see if the escaped solution
6180 intersects with the other. */
6181 if ((pt1->escaped
6182 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt2))
6183 || (pt2->escaped
6184 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt1)))
6185 return true;
6188 /* Check the escaped solution if required.
6189 ??? Do we need to check the local against the IPA escaped sets? */
6190 if ((pt1->ipa_escaped || pt2->ipa_escaped)
6191 && !pt_solution_empty_p (&ipa_escaped_pt))
6193 /* If both point to escaped memory and that solution
6194 is not empty they alias. */
6195 if (pt1->ipa_escaped && pt2->ipa_escaped)
6196 return true;
6198 /* If either points to escaped memory see if the escaped solution
6199 intersects with the other. */
6200 if ((pt1->ipa_escaped
6201 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
6202 || (pt2->ipa_escaped
6203 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
6204 return true;
6207 /* Now both pointers alias if their points-to solution intersects. */
6208 return (pt1->vars
6209 && pt2->vars
6210 && bitmap_intersect_p (pt1->vars, pt2->vars));
6213 bool
6214 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
6216 bool res = pt_solutions_intersect_1 (pt1, pt2);
6217 if (res)
6218 ++pta_stats.pt_solutions_intersect_may_alias;
6219 else
6220 ++pta_stats.pt_solutions_intersect_no_alias;
6221 return res;
6225 /* Dump points-to information to OUTFILE. */
6227 static void
6228 dump_sa_points_to_info (FILE *outfile)
6230 unsigned int i;
6232 fprintf (outfile, "\nPoints-to sets\n\n");
6234 if (dump_flags & TDF_STATS)
6236 fprintf (outfile, "Stats:\n");
6237 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
6238 fprintf (outfile, "Non-pointer vars: %d\n",
6239 stats.nonpointer_vars);
6240 fprintf (outfile, "Statically unified vars: %d\n",
6241 stats.unified_vars_static);
6242 fprintf (outfile, "Dynamically unified vars: %d\n",
6243 stats.unified_vars_dynamic);
6244 fprintf (outfile, "Iterations: %d\n", stats.iterations);
6245 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
6246 fprintf (outfile, "Number of implicit edges: %d\n",
6247 stats.num_implicit_edges);
6250 for (i = 0; i < varmap.length (); i++)
6252 varinfo_t vi = get_varinfo (i);
6253 if (!vi->may_have_pointers)
6254 continue;
6255 dump_solution_for_var (outfile, i);
6260 /* Debug points-to information to stderr. */
6262 DEBUG_FUNCTION void
6263 debug_sa_points_to_info (void)
6265 dump_sa_points_to_info (stderr);
6269 /* Initialize the always-existing constraint variables for NULL
6270 ANYTHING, READONLY, and INTEGER */
6272 static void
6273 init_base_vars (void)
6275 struct constraint_expr lhs, rhs;
6276 varinfo_t var_anything;
6277 varinfo_t var_nothing;
6278 varinfo_t var_readonly;
6279 varinfo_t var_escaped;
6280 varinfo_t var_nonlocal;
6281 varinfo_t var_storedanything;
6282 varinfo_t var_integer;
6284 /* Create the NULL variable, used to represent that a variable points
6285 to NULL. */
6286 var_nothing = new_var_info (NULL_TREE, "NULL");
6287 gcc_assert (var_nothing->id == nothing_id);
6288 var_nothing->is_artificial_var = 1;
6289 var_nothing->offset = 0;
6290 var_nothing->size = ~0;
6291 var_nothing->fullsize = ~0;
6292 var_nothing->is_special_var = 1;
6293 var_nothing->may_have_pointers = 0;
6294 var_nothing->is_global_var = 0;
6296 /* Create the ANYTHING variable, used to represent that a variable
6297 points to some unknown piece of memory. */
6298 var_anything = new_var_info (NULL_TREE, "ANYTHING");
6299 gcc_assert (var_anything->id == anything_id);
6300 var_anything->is_artificial_var = 1;
6301 var_anything->size = ~0;
6302 var_anything->offset = 0;
6303 var_anything->next = NULL;
6304 var_anything->fullsize = ~0;
6305 var_anything->is_special_var = 1;
6307 /* Anything points to anything. This makes deref constraints just
6308 work in the presence of linked list and other p = *p type loops,
6309 by saying that *ANYTHING = ANYTHING. */
6310 lhs.type = SCALAR;
6311 lhs.var = anything_id;
6312 lhs.offset = 0;
6313 rhs.type = ADDRESSOF;
6314 rhs.var = anything_id;
6315 rhs.offset = 0;
6317 /* This specifically does not use process_constraint because
6318 process_constraint ignores all anything = anything constraints, since all
6319 but this one are redundant. */
6320 constraints.safe_push (new_constraint (lhs, rhs));
6322 /* Create the READONLY variable, used to represent that a variable
6323 points to readonly memory. */
6324 var_readonly = new_var_info (NULL_TREE, "READONLY");
6325 gcc_assert (var_readonly->id == readonly_id);
6326 var_readonly->is_artificial_var = 1;
6327 var_readonly->offset = 0;
6328 var_readonly->size = ~0;
6329 var_readonly->fullsize = ~0;
6330 var_readonly->next = NULL;
6331 var_readonly->is_special_var = 1;
6333 /* readonly memory points to anything, in order to make deref
6334 easier. In reality, it points to anything the particular
6335 readonly variable can point to, but we don't track this
6336 separately. */
6337 lhs.type = SCALAR;
6338 lhs.var = readonly_id;
6339 lhs.offset = 0;
6340 rhs.type = ADDRESSOF;
6341 rhs.var = readonly_id; /* FIXME */
6342 rhs.offset = 0;
6343 process_constraint (new_constraint (lhs, rhs));
6345 /* Create the ESCAPED variable, used to represent the set of escaped
6346 memory. */
6347 var_escaped = new_var_info (NULL_TREE, "ESCAPED");
6348 gcc_assert (var_escaped->id == escaped_id);
6349 var_escaped->is_artificial_var = 1;
6350 var_escaped->offset = 0;
6351 var_escaped->size = ~0;
6352 var_escaped->fullsize = ~0;
6353 var_escaped->is_special_var = 0;
6355 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6356 memory. */
6357 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL");
6358 gcc_assert (var_nonlocal->id == nonlocal_id);
6359 var_nonlocal->is_artificial_var = 1;
6360 var_nonlocal->offset = 0;
6361 var_nonlocal->size = ~0;
6362 var_nonlocal->fullsize = ~0;
6363 var_nonlocal->is_special_var = 1;
6365 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6366 lhs.type = SCALAR;
6367 lhs.var = escaped_id;
6368 lhs.offset = 0;
6369 rhs.type = DEREF;
6370 rhs.var = escaped_id;
6371 rhs.offset = 0;
6372 process_constraint (new_constraint (lhs, rhs));
6374 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6375 whole variable escapes. */
6376 lhs.type = SCALAR;
6377 lhs.var = escaped_id;
6378 lhs.offset = 0;
6379 rhs.type = SCALAR;
6380 rhs.var = escaped_id;
6381 rhs.offset = UNKNOWN_OFFSET;
6382 process_constraint (new_constraint (lhs, rhs));
6384 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6385 everything pointed to by escaped points to what global memory can
6386 point to. */
6387 lhs.type = DEREF;
6388 lhs.var = escaped_id;
6389 lhs.offset = 0;
6390 rhs.type = SCALAR;
6391 rhs.var = nonlocal_id;
6392 rhs.offset = 0;
6393 process_constraint (new_constraint (lhs, rhs));
6395 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6396 global memory may point to global memory and escaped memory. */
6397 lhs.type = SCALAR;
6398 lhs.var = nonlocal_id;
6399 lhs.offset = 0;
6400 rhs.type = ADDRESSOF;
6401 rhs.var = nonlocal_id;
6402 rhs.offset = 0;
6403 process_constraint (new_constraint (lhs, rhs));
6404 rhs.type = ADDRESSOF;
6405 rhs.var = escaped_id;
6406 rhs.offset = 0;
6407 process_constraint (new_constraint (lhs, rhs));
6409 /* Create the STOREDANYTHING variable, used to represent the set of
6410 variables stored to *ANYTHING. */
6411 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING");
6412 gcc_assert (var_storedanything->id == storedanything_id);
6413 var_storedanything->is_artificial_var = 1;
6414 var_storedanything->offset = 0;
6415 var_storedanything->size = ~0;
6416 var_storedanything->fullsize = ~0;
6417 var_storedanything->is_special_var = 0;
6419 /* Create the INTEGER variable, used to represent that a variable points
6420 to what an INTEGER "points to". */
6421 var_integer = new_var_info (NULL_TREE, "INTEGER");
6422 gcc_assert (var_integer->id == integer_id);
6423 var_integer->is_artificial_var = 1;
6424 var_integer->size = ~0;
6425 var_integer->fullsize = ~0;
6426 var_integer->offset = 0;
6427 var_integer->next = NULL;
6428 var_integer->is_special_var = 1;
6430 /* INTEGER = ANYTHING, because we don't know where a dereference of
6431 a random integer will point to. */
6432 lhs.type = SCALAR;
6433 lhs.var = integer_id;
6434 lhs.offset = 0;
6435 rhs.type = ADDRESSOF;
6436 rhs.var = anything_id;
6437 rhs.offset = 0;
6438 process_constraint (new_constraint (lhs, rhs));
6441 /* Initialize things necessary to perform PTA */
6443 static void
6444 init_alias_vars (void)
6446 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
6448 bitmap_obstack_initialize (&pta_obstack);
6449 bitmap_obstack_initialize (&oldpta_obstack);
6450 bitmap_obstack_initialize (&predbitmap_obstack);
6452 constraint_pool = create_alloc_pool ("Constraint pool",
6453 sizeof (struct constraint), 30);
6454 variable_info_pool = create_alloc_pool ("Variable info pool",
6455 sizeof (struct variable_info), 30);
6456 constraints.create (8);
6457 varmap.create (8);
6458 vi_for_tree = pointer_map_create ();
6459 call_stmt_vars = pointer_map_create ();
6461 memset (&stats, 0, sizeof (stats));
6462 shared_bitmap_table = htab_create (511, shared_bitmap_hash,
6463 shared_bitmap_eq, free);
6464 init_base_vars ();
6466 gcc_obstack_init (&fake_var_decl_obstack);
6469 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6470 predecessor edges. */
6472 static void
6473 remove_preds_and_fake_succs (constraint_graph_t graph)
6475 unsigned int i;
6477 /* Clear the implicit ref and address nodes from the successor
6478 lists. */
6479 for (i = 0; i < FIRST_REF_NODE; i++)
6481 if (graph->succs[i])
6482 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
6483 FIRST_REF_NODE * 2);
6486 /* Free the successor list for the non-ref nodes. */
6487 for (i = FIRST_REF_NODE; i < graph->size; i++)
6489 if (graph->succs[i])
6490 BITMAP_FREE (graph->succs[i]);
6493 /* Now reallocate the size of the successor list as, and blow away
6494 the predecessor bitmaps. */
6495 graph->size = varmap.length ();
6496 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
6498 free (graph->implicit_preds);
6499 graph->implicit_preds = NULL;
6500 free (graph->preds);
6501 graph->preds = NULL;
6502 bitmap_obstack_release (&predbitmap_obstack);
6505 /* Solve the constraint set. */
6507 static void
6508 solve_constraints (void)
6510 struct scc_info *si;
6512 if (dump_file)
6513 fprintf (dump_file,
6514 "\nCollapsing static cycles and doing variable "
6515 "substitution\n");
6517 init_graph (varmap.length () * 2);
6519 if (dump_file)
6520 fprintf (dump_file, "Building predecessor graph\n");
6521 build_pred_graph ();
6523 if (dump_file)
6524 fprintf (dump_file, "Detecting pointer and location "
6525 "equivalences\n");
6526 si = perform_var_substitution (graph);
6528 if (dump_file)
6529 fprintf (dump_file, "Rewriting constraints and unifying "
6530 "variables\n");
6531 rewrite_constraints (graph, si);
6533 build_succ_graph ();
6535 free_var_substitution_info (si);
6537 /* Attach complex constraints to graph nodes. */
6538 move_complex_constraints (graph);
6540 if (dump_file)
6541 fprintf (dump_file, "Uniting pointer but not location equivalent "
6542 "variables\n");
6543 unite_pointer_equivalences (graph);
6545 if (dump_file)
6546 fprintf (dump_file, "Finding indirect cycles\n");
6547 find_indirect_cycles (graph);
6549 /* Implicit nodes and predecessors are no longer necessary at this
6550 point. */
6551 remove_preds_and_fake_succs (graph);
6553 if (dump_file && (dump_flags & TDF_GRAPH))
6555 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
6556 "in dot format:\n");
6557 dump_constraint_graph (dump_file);
6558 fprintf (dump_file, "\n\n");
6561 if (dump_file)
6562 fprintf (dump_file, "Solving graph\n");
6564 solve_graph (graph);
6566 if (dump_file && (dump_flags & TDF_GRAPH))
6568 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
6569 "in dot format:\n");
6570 dump_constraint_graph (dump_file);
6571 fprintf (dump_file, "\n\n");
6574 if (dump_file)
6575 dump_sa_points_to_info (dump_file);
6578 /* Create points-to sets for the current function. See the comments
6579 at the start of the file for an algorithmic overview. */
6581 static void
6582 compute_points_to_sets (void)
6584 basic_block bb;
6585 unsigned i;
6586 varinfo_t vi;
6588 timevar_push (TV_TREE_PTA);
6590 init_alias_vars ();
6592 intra_create_variable_infos ();
6594 /* Now walk all statements and build the constraint set. */
6595 FOR_EACH_BB (bb)
6597 gimple_stmt_iterator gsi;
6599 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6601 gimple phi = gsi_stmt (gsi);
6603 if (! virtual_operand_p (gimple_phi_result (phi)))
6604 find_func_aliases (phi);
6607 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6609 gimple stmt = gsi_stmt (gsi);
6611 find_func_aliases (stmt);
6615 if (dump_file)
6617 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
6618 dump_constraints (dump_file, 0);
6621 /* From the constraints compute the points-to sets. */
6622 solve_constraints ();
6624 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6625 find_what_var_points_to (get_varinfo (escaped_id),
6626 &cfun->gimple_df->escaped);
6628 /* Make sure the ESCAPED solution (which is used as placeholder in
6629 other solutions) does not reference itself. This simplifies
6630 points-to solution queries. */
6631 cfun->gimple_df->escaped.escaped = 0;
6633 /* Mark escaped HEAP variables as global. */
6634 FOR_EACH_VEC_ELT (varmap, i, vi)
6635 if (vi->is_heap_var
6636 && !vi->is_global_var)
6637 DECL_EXTERNAL (vi->decl) = vi->is_global_var
6638 = pt_solution_includes (&cfun->gimple_df->escaped, vi->decl);
6640 /* Compute the points-to sets for pointer SSA_NAMEs. */
6641 for (i = 0; i < num_ssa_names; ++i)
6643 tree ptr = ssa_name (i);
6644 if (ptr
6645 && POINTER_TYPE_P (TREE_TYPE (ptr)))
6646 find_what_p_points_to (ptr);
6649 /* Compute the call-used/clobbered sets. */
6650 FOR_EACH_BB (bb)
6652 gimple_stmt_iterator gsi;
6654 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6656 gimple stmt = gsi_stmt (gsi);
6657 struct pt_solution *pt;
6658 if (!is_gimple_call (stmt))
6659 continue;
6661 pt = gimple_call_use_set (stmt);
6662 if (gimple_call_flags (stmt) & ECF_CONST)
6663 memset (pt, 0, sizeof (struct pt_solution));
6664 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
6666 find_what_var_points_to (vi, pt);
6667 /* Escaped (and thus nonlocal) variables are always
6668 implicitly used by calls. */
6669 /* ??? ESCAPED can be empty even though NONLOCAL
6670 always escaped. */
6671 pt->nonlocal = 1;
6672 pt->escaped = 1;
6674 else
6676 /* If there is nothing special about this call then
6677 we have made everything that is used also escape. */
6678 *pt = cfun->gimple_df->escaped;
6679 pt->nonlocal = 1;
6682 pt = gimple_call_clobber_set (stmt);
6683 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
6684 memset (pt, 0, sizeof (struct pt_solution));
6685 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
6687 find_what_var_points_to (vi, pt);
6688 /* Escaped (and thus nonlocal) variables are always
6689 implicitly clobbered by calls. */
6690 /* ??? ESCAPED can be empty even though NONLOCAL
6691 always escaped. */
6692 pt->nonlocal = 1;
6693 pt->escaped = 1;
6695 else
6697 /* If there is nothing special about this call then
6698 we have made everything that is used also escape. */
6699 *pt = cfun->gimple_df->escaped;
6700 pt->nonlocal = 1;
6705 timevar_pop (TV_TREE_PTA);
6709 /* Delete created points-to sets. */
6711 static void
6712 delete_points_to_sets (void)
6714 unsigned int i;
6716 htab_delete (shared_bitmap_table);
6717 if (dump_file && (dump_flags & TDF_STATS))
6718 fprintf (dump_file, "Points to sets created:%d\n",
6719 stats.points_to_sets_created);
6721 pointer_map_destroy (vi_for_tree);
6722 pointer_map_destroy (call_stmt_vars);
6723 bitmap_obstack_release (&pta_obstack);
6724 constraints.release ();
6726 for (i = 0; i < graph->size; i++)
6727 graph->complex[i].release ();
6728 free (graph->complex);
6730 free (graph->rep);
6731 free (graph->succs);
6732 free (graph->pe);
6733 free (graph->pe_rep);
6734 free (graph->indirect_cycles);
6735 free (graph);
6737 varmap.release ();
6738 free_alloc_pool (variable_info_pool);
6739 free_alloc_pool (constraint_pool);
6741 obstack_free (&fake_var_decl_obstack, NULL);
6745 /* Compute points-to information for every SSA_NAME pointer in the
6746 current function and compute the transitive closure of escaped
6747 variables to re-initialize the call-clobber states of local variables. */
6749 unsigned int
6750 compute_may_aliases (void)
6752 if (cfun->gimple_df->ipa_pta)
6754 if (dump_file)
6756 fprintf (dump_file, "\nNot re-computing points-to information "
6757 "because IPA points-to information is available.\n\n");
6759 /* But still dump what we have remaining it. */
6760 dump_alias_info (dump_file);
6763 return 0;
6766 /* For each pointer P_i, determine the sets of variables that P_i may
6767 point-to. Compute the reachability set of escaped and call-used
6768 variables. */
6769 compute_points_to_sets ();
6771 /* Debugging dumps. */
6772 if (dump_file)
6773 dump_alias_info (dump_file);
6775 /* Deallocate memory used by aliasing data structures and the internal
6776 points-to solution. */
6777 delete_points_to_sets ();
6779 gcc_assert (!need_ssa_update_p (cfun));
6781 return 0;
6784 static bool
6785 gate_tree_pta (void)
6787 return flag_tree_pta;
6790 /* A dummy pass to cause points-to information to be computed via
6791 TODO_rebuild_alias. */
6793 struct gimple_opt_pass pass_build_alias =
6796 GIMPLE_PASS,
6797 "alias", /* name */
6798 OPTGROUP_NONE, /* optinfo_flags */
6799 gate_tree_pta, /* gate */
6800 NULL, /* execute */
6801 NULL, /* sub */
6802 NULL, /* next */
6803 0, /* static_pass_number */
6804 TV_NONE, /* tv_id */
6805 PROP_cfg | PROP_ssa, /* properties_required */
6806 0, /* properties_provided */
6807 0, /* properties_destroyed */
6808 0, /* todo_flags_start */
6809 TODO_rebuild_alias /* todo_flags_finish */
6813 /* A dummy pass to cause points-to information to be computed via
6814 TODO_rebuild_alias. */
6816 struct gimple_opt_pass pass_build_ealias =
6819 GIMPLE_PASS,
6820 "ealias", /* name */
6821 OPTGROUP_NONE, /* optinfo_flags */
6822 gate_tree_pta, /* gate */
6823 NULL, /* execute */
6824 NULL, /* sub */
6825 NULL, /* next */
6826 0, /* static_pass_number */
6827 TV_NONE, /* tv_id */
6828 PROP_cfg | PROP_ssa, /* properties_required */
6829 0, /* properties_provided */
6830 0, /* properties_destroyed */
6831 0, /* todo_flags_start */
6832 TODO_rebuild_alias /* todo_flags_finish */
6837 /* Return true if we should execute IPA PTA. */
6838 static bool
6839 gate_ipa_pta (void)
6841 return (optimize
6842 && flag_ipa_pta
6843 /* Don't bother doing anything if the program has errors. */
6844 && !seen_error ());
6847 /* IPA PTA solutions for ESCAPED. */
6848 struct pt_solution ipa_escaped_pt
6849 = { true, false, false, false, false, false, NULL };
6851 /* Associate node with varinfo DATA. Worker for
6852 cgraph_for_node_and_aliases. */
6853 static bool
6854 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
6856 if (node->alias || node->thunk.thunk_p)
6857 insert_vi_for_tree (node->symbol.decl, (varinfo_t)data);
6858 return false;
6861 /* Execute the driver for IPA PTA. */
6862 static unsigned int
6863 ipa_pta_execute (void)
6865 struct cgraph_node *node;
6866 struct varpool_node *var;
6867 int from;
6869 in_ipa_mode = 1;
6871 init_alias_vars ();
6873 if (dump_file && (dump_flags & TDF_DETAILS))
6875 dump_symtab (dump_file);
6876 fprintf (dump_file, "\n");
6879 /* Build the constraints. */
6880 FOR_EACH_DEFINED_FUNCTION (node)
6882 varinfo_t vi;
6883 /* Nodes without a body are not interesting. Especially do not
6884 visit clones at this point for now - we get duplicate decls
6885 there for inline clones at least. */
6886 if (!cgraph_function_with_gimple_body_p (node))
6887 continue;
6889 gcc_assert (!node->clone_of);
6891 vi = create_function_info_for (node->symbol.decl,
6892 alias_get_name (node->symbol.decl));
6893 cgraph_for_node_and_aliases (node, associate_varinfo_to_alias, vi, true);
6896 /* Create constraints for global variables and their initializers. */
6897 FOR_EACH_VARIABLE (var)
6899 if (var->alias)
6900 continue;
6902 get_vi_for_tree (var->symbol.decl);
6905 if (dump_file)
6907 fprintf (dump_file,
6908 "Generating constraints for global initializers\n\n");
6909 dump_constraints (dump_file, 0);
6910 fprintf (dump_file, "\n");
6912 from = constraints.length ();
6914 FOR_EACH_DEFINED_FUNCTION (node)
6916 struct function *func;
6917 basic_block bb;
6919 /* Nodes without a body are not interesting. */
6920 if (!cgraph_function_with_gimple_body_p (node))
6921 continue;
6923 if (dump_file)
6925 fprintf (dump_file,
6926 "Generating constraints for %s", cgraph_node_name (node));
6927 if (DECL_ASSEMBLER_NAME_SET_P (node->symbol.decl))
6928 fprintf (dump_file, " (%s)",
6929 IDENTIFIER_POINTER
6930 (DECL_ASSEMBLER_NAME (node->symbol.decl)));
6931 fprintf (dump_file, "\n");
6934 func = DECL_STRUCT_FUNCTION (node->symbol.decl);
6935 push_cfun (func);
6937 /* For externally visible or attribute used annotated functions use
6938 local constraints for their arguments.
6939 For local functions we see all callers and thus do not need initial
6940 constraints for parameters. */
6941 if (node->symbol.used_from_other_partition
6942 || node->symbol.externally_visible
6943 || node->symbol.force_output)
6945 intra_create_variable_infos ();
6947 /* We also need to make function return values escape. Nothing
6948 escapes by returning from main though. */
6949 if (!MAIN_NAME_P (DECL_NAME (node->symbol.decl)))
6951 varinfo_t fi, rvi;
6952 fi = lookup_vi_for_tree (node->symbol.decl);
6953 rvi = first_vi_for_offset (fi, fi_result);
6954 if (rvi && rvi->offset == fi_result)
6956 struct constraint_expr includes;
6957 struct constraint_expr var;
6958 includes.var = escaped_id;
6959 includes.offset = 0;
6960 includes.type = SCALAR;
6961 var.var = rvi->id;
6962 var.offset = 0;
6963 var.type = SCALAR;
6964 process_constraint (new_constraint (includes, var));
6969 /* Build constriants for the function body. */
6970 FOR_EACH_BB_FN (bb, func)
6972 gimple_stmt_iterator gsi;
6974 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
6975 gsi_next (&gsi))
6977 gimple phi = gsi_stmt (gsi);
6979 if (! virtual_operand_p (gimple_phi_result (phi)))
6980 find_func_aliases (phi);
6983 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6985 gimple stmt = gsi_stmt (gsi);
6987 find_func_aliases (stmt);
6988 find_func_clobbers (stmt);
6992 pop_cfun ();
6994 if (dump_file)
6996 fprintf (dump_file, "\n");
6997 dump_constraints (dump_file, from);
6998 fprintf (dump_file, "\n");
7000 from = constraints.length ();
7003 /* From the constraints compute the points-to sets. */
7004 solve_constraints ();
7006 /* Compute the global points-to sets for ESCAPED.
7007 ??? Note that the computed escape set is not correct
7008 for the whole unit as we fail to consider graph edges to
7009 externally visible functions. */
7010 find_what_var_points_to (get_varinfo (escaped_id), &ipa_escaped_pt);
7012 /* Make sure the ESCAPED solution (which is used as placeholder in
7013 other solutions) does not reference itself. This simplifies
7014 points-to solution queries. */
7015 ipa_escaped_pt.ipa_escaped = 0;
7017 /* Assign the points-to sets to the SSA names in the unit. */
7018 FOR_EACH_DEFINED_FUNCTION (node)
7020 tree ptr;
7021 struct function *fn;
7022 unsigned i;
7023 varinfo_t fi;
7024 basic_block bb;
7025 struct pt_solution uses, clobbers;
7026 struct cgraph_edge *e;
7028 /* Nodes without a body are not interesting. */
7029 if (!cgraph_function_with_gimple_body_p (node))
7030 continue;
7032 fn = DECL_STRUCT_FUNCTION (node->symbol.decl);
7034 /* Compute the points-to sets for pointer SSA_NAMEs. */
7035 FOR_EACH_VEC_ELT (*fn->gimple_df->ssa_names, i, ptr)
7037 if (ptr
7038 && POINTER_TYPE_P (TREE_TYPE (ptr)))
7039 find_what_p_points_to (ptr);
7042 /* Compute the call-use and call-clobber sets for all direct calls. */
7043 fi = lookup_vi_for_tree (node->symbol.decl);
7044 gcc_assert (fi->is_fn_info);
7045 find_what_var_points_to (first_vi_for_offset (fi, fi_clobbers),
7046 &clobbers);
7047 find_what_var_points_to (first_vi_for_offset (fi, fi_uses), &uses);
7048 for (e = node->callers; e; e = e->next_caller)
7050 if (!e->call_stmt)
7051 continue;
7053 *gimple_call_clobber_set (e->call_stmt) = clobbers;
7054 *gimple_call_use_set (e->call_stmt) = uses;
7057 /* Compute the call-use and call-clobber sets for indirect calls
7058 and calls to external functions. */
7059 FOR_EACH_BB_FN (bb, fn)
7061 gimple_stmt_iterator gsi;
7063 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7065 gimple stmt = gsi_stmt (gsi);
7066 struct pt_solution *pt;
7067 varinfo_t vi;
7068 tree decl;
7070 if (!is_gimple_call (stmt))
7071 continue;
7073 /* Handle direct calls to external functions. */
7074 decl = gimple_call_fndecl (stmt);
7075 if (decl
7076 && (!(fi = lookup_vi_for_tree (decl))
7077 || !fi->is_fn_info))
7079 pt = gimple_call_use_set (stmt);
7080 if (gimple_call_flags (stmt) & ECF_CONST)
7081 memset (pt, 0, sizeof (struct pt_solution));
7082 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7084 find_what_var_points_to (vi, pt);
7085 /* Escaped (and thus nonlocal) variables are always
7086 implicitly used by calls. */
7087 /* ??? ESCAPED can be empty even though NONLOCAL
7088 always escaped. */
7089 pt->nonlocal = 1;
7090 pt->ipa_escaped = 1;
7092 else
7094 /* If there is nothing special about this call then
7095 we have made everything that is used also escape. */
7096 *pt = ipa_escaped_pt;
7097 pt->nonlocal = 1;
7100 pt = gimple_call_clobber_set (stmt);
7101 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7102 memset (pt, 0, sizeof (struct pt_solution));
7103 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7105 find_what_var_points_to (vi, pt);
7106 /* Escaped (and thus nonlocal) variables are always
7107 implicitly clobbered by calls. */
7108 /* ??? ESCAPED can be empty even though NONLOCAL
7109 always escaped. */
7110 pt->nonlocal = 1;
7111 pt->ipa_escaped = 1;
7113 else
7115 /* If there is nothing special about this call then
7116 we have made everything that is used also escape. */
7117 *pt = ipa_escaped_pt;
7118 pt->nonlocal = 1;
7122 /* Handle indirect calls. */
7123 if (!decl
7124 && (fi = get_fi_for_callee (stmt)))
7126 /* We need to accumulate all clobbers/uses of all possible
7127 callees. */
7128 fi = get_varinfo (find (fi->id));
7129 /* If we cannot constrain the set of functions we'll end up
7130 calling we end up using/clobbering everything. */
7131 if (bitmap_bit_p (fi->solution, anything_id)
7132 || bitmap_bit_p (fi->solution, nonlocal_id)
7133 || bitmap_bit_p (fi->solution, escaped_id))
7135 pt_solution_reset (gimple_call_clobber_set (stmt));
7136 pt_solution_reset (gimple_call_use_set (stmt));
7138 else
7140 bitmap_iterator bi;
7141 unsigned i;
7142 struct pt_solution *uses, *clobbers;
7144 uses = gimple_call_use_set (stmt);
7145 clobbers = gimple_call_clobber_set (stmt);
7146 memset (uses, 0, sizeof (struct pt_solution));
7147 memset (clobbers, 0, sizeof (struct pt_solution));
7148 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
7150 struct pt_solution sol;
7152 vi = get_varinfo (i);
7153 if (!vi->is_fn_info)
7155 /* ??? We could be more precise here? */
7156 uses->nonlocal = 1;
7157 uses->ipa_escaped = 1;
7158 clobbers->nonlocal = 1;
7159 clobbers->ipa_escaped = 1;
7160 continue;
7163 if (!uses->anything)
7165 find_what_var_points_to
7166 (first_vi_for_offset (vi, fi_uses), &sol);
7167 pt_solution_ior_into (uses, &sol);
7169 if (!clobbers->anything)
7171 find_what_var_points_to
7172 (first_vi_for_offset (vi, fi_clobbers), &sol);
7173 pt_solution_ior_into (clobbers, &sol);
7181 fn->gimple_df->ipa_pta = true;
7184 delete_points_to_sets ();
7186 in_ipa_mode = 0;
7188 return 0;
7191 struct simple_ipa_opt_pass pass_ipa_pta =
7194 SIMPLE_IPA_PASS,
7195 "pta", /* name */
7196 OPTGROUP_NONE, /* optinfo_flags */
7197 gate_ipa_pta, /* gate */
7198 ipa_pta_execute, /* execute */
7199 NULL, /* sub */
7200 NULL, /* next */
7201 0, /* static_pass_number */
7202 TV_IPA_PTA, /* tv_id */
7203 0, /* properties_required */
7204 0, /* properties_provided */
7205 0, /* properties_destroyed */
7206 0, /* todo_flags_start */
7207 TODO_update_ssa /* todo_flags_finish */