Update copyright years in gcc/
[gcc.git] / gcc / tree-ssa-structalias.c
1 /* Tree based points-to analysis
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dberlin@dberlin.org>
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "obstack.h"
26 #include "bitmap.h"
27 #include "sbitmap.h"
28 #include "flags.h"
29 #include "basic-block.h"
30 #include "tree.h"
31 #include "stor-layout.h"
32 #include "stmt.h"
33 #include "pointer-set.h"
34 #include "hash-table.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-expr.h"
38 #include "is-a.h"
39 #include "gimple.h"
40 #include "gimple-iterator.h"
41 #include "gimple-ssa.h"
42 #include "cgraph.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
45 #include "tree-into-ssa.h"
46 #include "expr.h"
47 #include "tree-dfa.h"
48 #include "tree-inline.h"
49 #include "diagnostic-core.h"
50 #include "function.h"
51 #include "tree-pass.h"
52 #include "alloc-pool.h"
53 #include "splay-tree.h"
54 #include "params.h"
55 #include "alias.h"
56
57 /* The idea behind this analyzer is to generate set constraints from the
58 program, then solve the resulting constraints in order to generate the
59 points-to sets.
60
61 Set constraints are a way of modeling program analysis problems that
62 involve sets. They consist of an inclusion constraint language,
63 describing the variables (each variable is a set) and operations that
64 are involved on the variables, and a set of rules that derive facts
65 from these operations. To solve a system of set constraints, you derive
66 all possible facts under the rules, which gives you the correct sets
67 as a consequence.
68
69 See "Efficient Field-sensitive pointer analysis for C" by "David
70 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
71 http://citeseer.ist.psu.edu/pearce04efficient.html
72
73 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
74 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
75 http://citeseer.ist.psu.edu/heintze01ultrafast.html
76
77 There are three types of real constraint expressions, DEREF,
78 ADDRESSOF, and SCALAR. Each constraint expression consists
79 of a constraint type, a variable, and an offset.
80
81 SCALAR is a constraint expression type used to represent x, whether
82 it appears on the LHS or the RHS of a statement.
83 DEREF is a constraint expression type used to represent *x, whether
84 it appears on the LHS or the RHS of a statement.
85 ADDRESSOF is a constraint expression used to represent &x, whether
86 it appears on the LHS or the RHS of a statement.
87
88 Each pointer variable in the program is assigned an integer id, and
89 each field of a structure variable is assigned an integer id as well.
90
91 Structure variables are linked to their list of fields through a "next
92 field" in each variable that points to the next field in offset
93 order.
94 Each variable for a structure field has
95
96 1. "size", that tells the size in bits of that field.
97 2. "fullsize, that tells the size in bits of the entire structure.
98 3. "offset", that tells the offset in bits from the beginning of the
99 structure to this field.
100
101 Thus,
102 struct f
103 {
104 int a;
105 int b;
106 } foo;
107 int *bar;
108
109 looks like
110
111 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
112 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
113 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
114
115
116 In order to solve the system of set constraints, the following is
117 done:
118
119 1. Each constraint variable x has a solution set associated with it,
120 Sol(x).
121
122 2. Constraints are separated into direct, copy, and complex.
123 Direct constraints are ADDRESSOF constraints that require no extra
124 processing, such as P = &Q
125 Copy constraints are those of the form P = Q.
126 Complex constraints are all the constraints involving dereferences
127 and offsets (including offsetted copies).
128
129 3. All direct constraints of the form P = &Q are processed, such
130 that Q is added to Sol(P)
131
132 4. All complex constraints for a given constraint variable are stored in a
133 linked list attached to that variable's node.
134
135 5. A directed graph is built out of the copy constraints. Each
136 constraint variable is a node in the graph, and an edge from
137 Q to P is added for each copy constraint of the form P = Q
138
139 6. The graph is then walked, and solution sets are
140 propagated along the copy edges, such that an edge from Q to P
141 causes Sol(P) <- Sol(P) union Sol(Q).
142
143 7. As we visit each node, all complex constraints associated with
144 that node are processed by adding appropriate copy edges to the graph, or the
145 appropriate variables to the solution set.
146
147 8. The process of walking the graph is iterated until no solution
148 sets change.
149
150 Prior to walking the graph in steps 6 and 7, We perform static
151 cycle elimination on the constraint graph, as well
152 as off-line variable substitution.
153
154 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
155 on and turned into anything), but isn't. You can just see what offset
156 inside the pointed-to struct it's going to access.
157
158 TODO: Constant bounded arrays can be handled as if they were structs of the
159 same number of elements.
160
161 TODO: Modeling heap and incoming pointers becomes much better if we
162 add fields to them as we discover them, which we could do.
163
164 TODO: We could handle unions, but to be honest, it's probably not
165 worth the pain or slowdown. */
166
167 /* IPA-PTA optimizations possible.
168
169 When the indirect function called is ANYTHING we can add disambiguation
170 based on the function signatures (or simply the parameter count which
171 is the varinfo size). We also do not need to consider functions that
172 do not have their address taken.
173
174 The is_global_var bit which marks escape points is overly conservative
175 in IPA mode. Split it to is_escape_point and is_global_var - only
176 externally visible globals are escape points in IPA mode. This is
177 also needed to fix the pt_solution_includes_global predicate
178 (and thus ptr_deref_may_alias_global_p).
179
180 The way we introduce DECL_PT_UID to avoid fixing up all points-to
181 sets in the translation unit when we copy a DECL during inlining
182 pessimizes precision. The advantage is that the DECL_PT_UID keeps
183 compile-time and memory usage overhead low - the points-to sets
184 do not grow or get unshared as they would during a fixup phase.
185 An alternative solution is to delay IPA PTA until after all
186 inlining transformations have been applied.
187
188 The way we propagate clobber/use information isn't optimized.
189 It should use a new complex constraint that properly filters
190 out local variables of the callee (though that would make
191 the sets invalid after inlining). OTOH we might as well
192 admit defeat to WHOPR and simply do all the clobber/use analysis
193 and propagation after PTA finished but before we threw away
194 points-to information for memory variables. WHOPR and PTA
195 do not play along well anyway - the whole constraint solving
196 would need to be done in WPA phase and it will be very interesting
197 to apply the results to local SSA names during LTRANS phase.
198
199 We probably should compute a per-function unit-ESCAPE solution
200 propagating it simply like the clobber / uses solutions. The
201 solution can go alongside the non-IPA espaced solution and be
202 used to query which vars escape the unit through a function.
203
204 We never put function decls in points-to sets so we do not
205 keep the set of called functions for indirect calls.
206
207 And probably more. */
208
209 static bool use_field_sensitive = true;
210 static int in_ipa_mode = 0;
211
212 /* Used for predecessor bitmaps. */
213 static bitmap_obstack predbitmap_obstack;
214
215 /* Used for points-to sets. */
216 static bitmap_obstack pta_obstack;
217
218 /* Used for oldsolution members of variables. */
219 static bitmap_obstack oldpta_obstack;
220
221 /* Used for per-solver-iteration bitmaps. */
222 static bitmap_obstack iteration_obstack;
223
224 static unsigned int create_variable_info_for (tree, const char *);
225 typedef struct constraint_graph *constraint_graph_t;
226 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
227
228 struct constraint;
229 typedef struct constraint *constraint_t;
230
231
232 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
233 if (a) \
234 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
235
236 static struct constraint_stats
237 {
238 unsigned int total_vars;
239 unsigned int nonpointer_vars;
240 unsigned int unified_vars_static;
241 unsigned int unified_vars_dynamic;
242 unsigned int iterations;
243 unsigned int num_edges;
244 unsigned int num_implicit_edges;
245 unsigned int points_to_sets_created;
246 } stats;
247
248 struct variable_info
249 {
250 /* ID of this variable */
251 unsigned int id;
252
253 /* True if this is a variable created by the constraint analysis, such as
254 heap variables and constraints we had to break up. */
255 unsigned int is_artificial_var : 1;
256
257 /* True if this is a special variable whose solution set should not be
258 changed. */
259 unsigned int is_special_var : 1;
260
261 /* True for variables whose size is not known or variable. */
262 unsigned int is_unknown_size_var : 1;
263
264 /* True for (sub-)fields that represent a whole variable. */
265 unsigned int is_full_var : 1;
266
267 /* True if this is a heap variable. */
268 unsigned int is_heap_var : 1;
269
270 /* True if this field may contain pointers. */
271 unsigned int may_have_pointers : 1;
272
273 /* True if this field has only restrict qualified pointers. */
274 unsigned int only_restrict_pointers : 1;
275
276 /* True if this represents a global variable. */
277 unsigned int is_global_var : 1;
278
279 /* True if this represents a IPA function info. */
280 unsigned int is_fn_info : 1;
281
282 /* The ID of the variable for the next field in this structure
283 or zero for the last field in this structure. */
284 unsigned next;
285
286 /* The ID of the variable for the first field in this structure. */
287 unsigned head;
288
289 /* Offset of this variable, in bits, from the base variable */
290 unsigned HOST_WIDE_INT offset;
291
292 /* Size of the variable, in bits. */
293 unsigned HOST_WIDE_INT size;
294
295 /* Full size of the base variable, in bits. */
296 unsigned HOST_WIDE_INT fullsize;
297
298 /* Name of this variable */
299 const char *name;
300
301 /* Tree that this variable is associated with. */
302 tree decl;
303
304 /* Points-to set for this variable. */
305 bitmap solution;
306
307 /* Old points-to set for this variable. */
308 bitmap oldsolution;
309 };
310 typedef struct variable_info *varinfo_t;
311
312 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
313 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
314 unsigned HOST_WIDE_INT);
315 static varinfo_t lookup_vi_for_tree (tree);
316 static inline bool type_can_have_subvars (const_tree);
317
318 /* Pool of variable info structures. */
319 static alloc_pool variable_info_pool;
320
321 /* Map varinfo to final pt_solution. */
322 static pointer_map_t *final_solutions;
323 struct obstack final_solutions_obstack;
324
325 /* Table of variable info structures for constraint variables.
326 Indexed directly by variable info id. */
327 static vec<varinfo_t> varmap;
328
329 /* Return the varmap element N */
330
331 static inline varinfo_t
332 get_varinfo (unsigned int n)
333 {
334 return varmap[n];
335 }
336
337 /* Return the next variable in the list of sub-variables of VI
338 or NULL if VI is the last sub-variable. */
339
340 static inline varinfo_t
341 vi_next (varinfo_t vi)
342 {
343 return get_varinfo (vi->next);
344 }
345
346 /* Static IDs for the special variables. Variable ID zero is unused
347 and used as terminator for the sub-variable chain. */
348 enum { nothing_id = 1, anything_id = 2, readonly_id = 3,
349 escaped_id = 4, nonlocal_id = 5,
350 storedanything_id = 6, integer_id = 7 };
351
352 /* Return a new variable info structure consisting for a variable
353 named NAME, and using constraint graph node NODE. Append it
354 to the vector of variable info structures. */
355
356 static varinfo_t
357 new_var_info (tree t, const char *name)
358 {
359 unsigned index = varmap.length ();
360 varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
361
362 ret->id = index;
363 ret->name = name;
364 ret->decl = t;
365 /* Vars without decl are artificial and do not have sub-variables. */
366 ret->is_artificial_var = (t == NULL_TREE);
367 ret->is_special_var = false;
368 ret->is_unknown_size_var = false;
369 ret->is_full_var = (t == NULL_TREE);
370 ret->is_heap_var = false;
371 ret->may_have_pointers = true;
372 ret->only_restrict_pointers = false;
373 ret->is_global_var = (t == NULL_TREE);
374 ret->is_fn_info = false;
375 if (t && DECL_P (t))
376 ret->is_global_var = (is_global_var (t)
377 /* We have to treat even local register variables
378 as escape points. */
379 || (TREE_CODE (t) == VAR_DECL
380 && DECL_HARD_REGISTER (t)));
381 ret->solution = BITMAP_ALLOC (&pta_obstack);
382 ret->oldsolution = NULL;
383 ret->next = 0;
384 ret->head = ret->id;
385
386 stats.total_vars++;
387
388 varmap.safe_push (ret);
389
390 return ret;
391 }
392
393
394 /* A map mapping call statements to per-stmt variables for uses
395 and clobbers specific to the call. */
396 static struct pointer_map_t *call_stmt_vars;
397
398 /* Lookup or create the variable for the call statement CALL. */
399
400 static varinfo_t
401 get_call_vi (gimple call)
402 {
403 void **slot_p;
404 varinfo_t vi, vi2;
405
406 slot_p = pointer_map_insert (call_stmt_vars, call);
407 if (*slot_p)
408 return (varinfo_t) *slot_p;
409
410 vi = new_var_info (NULL_TREE, "CALLUSED");
411 vi->offset = 0;
412 vi->size = 1;
413 vi->fullsize = 2;
414 vi->is_full_var = true;
415
416 vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED");
417 vi2->offset = 1;
418 vi2->size = 1;
419 vi2->fullsize = 2;
420 vi2->is_full_var = true;
421
422 vi->next = vi2->id;
423
424 *slot_p = (void *) vi;
425 return vi;
426 }
427
428 /* Lookup the variable for the call statement CALL representing
429 the uses. Returns NULL if there is nothing special about this call. */
430
431 static varinfo_t
432 lookup_call_use_vi (gimple call)
433 {
434 void **slot_p;
435
436 slot_p = pointer_map_contains (call_stmt_vars, call);
437 if (slot_p)
438 return (varinfo_t) *slot_p;
439
440 return NULL;
441 }
442
443 /* Lookup the variable for the call statement CALL representing
444 the clobbers. Returns NULL if there is nothing special about this call. */
445
446 static varinfo_t
447 lookup_call_clobber_vi (gimple call)
448 {
449 varinfo_t uses = lookup_call_use_vi (call);
450 if (!uses)
451 return NULL;
452
453 return vi_next (uses);
454 }
455
456 /* Lookup or create the variable for the call statement CALL representing
457 the uses. */
458
459 static varinfo_t
460 get_call_use_vi (gimple call)
461 {
462 return get_call_vi (call);
463 }
464
465 /* Lookup or create the variable for the call statement CALL representing
466 the clobbers. */
467
468 static varinfo_t ATTRIBUTE_UNUSED
469 get_call_clobber_vi (gimple call)
470 {
471 return vi_next (get_call_vi (call));
472 }
473
474
475 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
476
477 /* An expression that appears in a constraint. */
478
479 struct constraint_expr
480 {
481 /* Constraint type. */
482 constraint_expr_type type;
483
484 /* Variable we are referring to in the constraint. */
485 unsigned int var;
486
487 /* Offset, in bits, of this constraint from the beginning of
488 variables it ends up referring to.
489
490 IOW, in a deref constraint, we would deref, get the result set,
491 then add OFFSET to each member. */
492 HOST_WIDE_INT offset;
493 };
494
495 /* Use 0x8000... as special unknown offset. */
496 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
497
498 typedef struct constraint_expr ce_s;
499 static void get_constraint_for_1 (tree, vec<ce_s> *, bool, bool);
500 static void get_constraint_for (tree, vec<ce_s> *);
501 static void get_constraint_for_rhs (tree, vec<ce_s> *);
502 static void do_deref (vec<ce_s> *);
503
504 /* Our set constraints are made up of two constraint expressions, one
505 LHS, and one RHS.
506
507 As described in the introduction, our set constraints each represent an
508 operation between set valued variables.
509 */
510 struct constraint
511 {
512 struct constraint_expr lhs;
513 struct constraint_expr rhs;
514 };
515
516 /* List of constraints that we use to build the constraint graph from. */
517
518 static vec<constraint_t> constraints;
519 static alloc_pool constraint_pool;
520
521 /* The constraint graph is represented as an array of bitmaps
522 containing successor nodes. */
523
524 struct constraint_graph
525 {
526 /* Size of this graph, which may be different than the number of
527 nodes in the variable map. */
528 unsigned int size;
529
530 /* Explicit successors of each node. */
531 bitmap *succs;
532
533 /* Implicit predecessors of each node (Used for variable
534 substitution). */
535 bitmap *implicit_preds;
536
537 /* Explicit predecessors of each node (Used for variable substitution). */
538 bitmap *preds;
539
540 /* Indirect cycle representatives, or -1 if the node has no indirect
541 cycles. */
542 int *indirect_cycles;
543
544 /* Representative node for a node. rep[a] == a unless the node has
545 been unified. */
546 unsigned int *rep;
547
548 /* Equivalence class representative for a label. This is used for
549 variable substitution. */
550 int *eq_rep;
551
552 /* Pointer equivalence label for a node. All nodes with the same
553 pointer equivalence label can be unified together at some point
554 (either during constraint optimization or after the constraint
555 graph is built). */
556 unsigned int *pe;
557
558 /* Pointer equivalence representative for a label. This is used to
559 handle nodes that are pointer equivalent but not location
560 equivalent. We can unite these once the addressof constraints
561 are transformed into initial points-to sets. */
562 int *pe_rep;
563
564 /* Pointer equivalence label for each node, used during variable
565 substitution. */
566 unsigned int *pointer_label;
567
568 /* Location equivalence label for each node, used during location
569 equivalence finding. */
570 unsigned int *loc_label;
571
572 /* Pointed-by set for each node, used during location equivalence
573 finding. This is pointed-by rather than pointed-to, because it
574 is constructed using the predecessor graph. */
575 bitmap *pointed_by;
576
577 /* Points to sets for pointer equivalence. This is *not* the actual
578 points-to sets for nodes. */
579 bitmap *points_to;
580
581 /* Bitmap of nodes where the bit is set if the node is a direct
582 node. Used for variable substitution. */
583 sbitmap direct_nodes;
584
585 /* Bitmap of nodes where the bit is set if the node is address
586 taken. Used for variable substitution. */
587 bitmap address_taken;
588
589 /* Vector of complex constraints for each graph node. Complex
590 constraints are those involving dereferences or offsets that are
591 not 0. */
592 vec<constraint_t> *complex;
593 };
594
595 static constraint_graph_t graph;
596
597 /* During variable substitution and the offline version of indirect
598 cycle finding, we create nodes to represent dereferences and
599 address taken constraints. These represent where these start and
600 end. */
601 #define FIRST_REF_NODE (varmap).length ()
602 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
603
604 /* Return the representative node for NODE, if NODE has been unioned
605 with another NODE.
606 This function performs path compression along the way to finding
607 the representative. */
608
609 static unsigned int
610 find (unsigned int node)
611 {
612 gcc_checking_assert (node < graph->size);
613 if (graph->rep[node] != node)
614 return graph->rep[node] = find (graph->rep[node]);
615 return node;
616 }
617
618 /* Union the TO and FROM nodes to the TO nodes.
619 Note that at some point in the future, we may want to do
620 union-by-rank, in which case we are going to have to return the
621 node we unified to. */
622
623 static bool
624 unite (unsigned int to, unsigned int from)
625 {
626 gcc_checking_assert (to < graph->size && from < graph->size);
627 if (to != from && graph->rep[from] != to)
628 {
629 graph->rep[from] = to;
630 return true;
631 }
632 return false;
633 }
634
635 /* Create a new constraint consisting of LHS and RHS expressions. */
636
637 static constraint_t
638 new_constraint (const struct constraint_expr lhs,
639 const struct constraint_expr rhs)
640 {
641 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
642 ret->lhs = lhs;
643 ret->rhs = rhs;
644 return ret;
645 }
646
647 /* Print out constraint C to FILE. */
648
649 static void
650 dump_constraint (FILE *file, constraint_t c)
651 {
652 if (c->lhs.type == ADDRESSOF)
653 fprintf (file, "&");
654 else if (c->lhs.type == DEREF)
655 fprintf (file, "*");
656 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
657 if (c->lhs.offset == UNKNOWN_OFFSET)
658 fprintf (file, " + UNKNOWN");
659 else if (c->lhs.offset != 0)
660 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
661 fprintf (file, " = ");
662 if (c->rhs.type == ADDRESSOF)
663 fprintf (file, "&");
664 else if (c->rhs.type == DEREF)
665 fprintf (file, "*");
666 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
667 if (c->rhs.offset == UNKNOWN_OFFSET)
668 fprintf (file, " + UNKNOWN");
669 else if (c->rhs.offset != 0)
670 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
671 }
672
673
674 void debug_constraint (constraint_t);
675 void debug_constraints (void);
676 void debug_constraint_graph (void);
677 void debug_solution_for_var (unsigned int);
678 void debug_sa_points_to_info (void);
679
680 /* Print out constraint C to stderr. */
681
682 DEBUG_FUNCTION void
683 debug_constraint (constraint_t c)
684 {
685 dump_constraint (stderr, c);
686 fprintf (stderr, "\n");
687 }
688
689 /* Print out all constraints to FILE */
690
691 static void
692 dump_constraints (FILE *file, int from)
693 {
694 int i;
695 constraint_t c;
696 for (i = from; constraints.iterate (i, &c); i++)
697 if (c)
698 {
699 dump_constraint (file, c);
700 fprintf (file, "\n");
701 }
702 }
703
704 /* Print out all constraints to stderr. */
705
706 DEBUG_FUNCTION void
707 debug_constraints (void)
708 {
709 dump_constraints (stderr, 0);
710 }
711
712 /* Print the constraint graph in dot format. */
713
714 static void
715 dump_constraint_graph (FILE *file)
716 {
717 unsigned int i;
718
719 /* Only print the graph if it has already been initialized: */
720 if (!graph)
721 return;
722
723 /* Prints the header of the dot file: */
724 fprintf (file, "strict digraph {\n");
725 fprintf (file, " node [\n shape = box\n ]\n");
726 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
727 fprintf (file, "\n // List of nodes and complex constraints in "
728 "the constraint graph:\n");
729
730 /* The next lines print the nodes in the graph together with the
731 complex constraints attached to them. */
732 for (i = 1; i < graph->size; i++)
733 {
734 if (i == FIRST_REF_NODE)
735 continue;
736 if (find (i) != i)
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 if (graph->complex[i].exists ())
743 {
744 unsigned j;
745 constraint_t c;
746 fprintf (file, " [label=\"\\N\\n");
747 for (j = 0; graph->complex[i].iterate (j, &c); ++j)
748 {
749 dump_constraint (file, c);
750 fprintf (file, "\\l");
751 }
752 fprintf (file, "\"]");
753 }
754 fprintf (file, ";\n");
755 }
756
757 /* Go over the edges. */
758 fprintf (file, "\n // Edges in the constraint graph:\n");
759 for (i = 1; i < graph->size; i++)
760 {
761 unsigned j;
762 bitmap_iterator bi;
763 if (find (i) != i)
764 continue;
765 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
766 {
767 unsigned to = find (j);
768 if (i == to)
769 continue;
770 if (i < FIRST_REF_NODE)
771 fprintf (file, "\"%s\"", get_varinfo (i)->name);
772 else
773 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
774 fprintf (file, " -> ");
775 if (to < FIRST_REF_NODE)
776 fprintf (file, "\"%s\"", get_varinfo (to)->name);
777 else
778 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name);
779 fprintf (file, ";\n");
780 }
781 }
782
783 /* Prints the tail of the dot file. */
784 fprintf (file, "}\n");
785 }
786
787 /* Print out the constraint graph to stderr. */
788
789 DEBUG_FUNCTION void
790 debug_constraint_graph (void)
791 {
792 dump_constraint_graph (stderr);
793 }
794
795 /* SOLVER FUNCTIONS
796
797 The solver is a simple worklist solver, that works on the following
798 algorithm:
799
800 sbitmap changed_nodes = all zeroes;
801 changed_count = 0;
802 For each node that is not already collapsed:
803 changed_count++;
804 set bit in changed nodes
805
806 while (changed_count > 0)
807 {
808 compute topological ordering for constraint graph
809
810 find and collapse cycles in the constraint graph (updating
811 changed if necessary)
812
813 for each node (n) in the graph in topological order:
814 changed_count--;
815
816 Process each complex constraint associated with the node,
817 updating changed if necessary.
818
819 For each outgoing edge from n, propagate the solution from n to
820 the destination of the edge, updating changed as necessary.
821
822 } */
823
824 /* Return true if two constraint expressions A and B are equal. */
825
826 static bool
827 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
828 {
829 return a.type == b.type && a.var == b.var && a.offset == b.offset;
830 }
831
832 /* Return true if constraint expression A is less than constraint expression
833 B. This is just arbitrary, but consistent, in order to give them an
834 ordering. */
835
836 static bool
837 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
838 {
839 if (a.type == b.type)
840 {
841 if (a.var == b.var)
842 return a.offset < b.offset;
843 else
844 return a.var < b.var;
845 }
846 else
847 return a.type < b.type;
848 }
849
850 /* Return true if constraint A is less than constraint B. This is just
851 arbitrary, but consistent, in order to give them an ordering. */
852
853 static bool
854 constraint_less (const constraint_t &a, const constraint_t &b)
855 {
856 if (constraint_expr_less (a->lhs, b->lhs))
857 return true;
858 else if (constraint_expr_less (b->lhs, a->lhs))
859 return false;
860 else
861 return constraint_expr_less (a->rhs, b->rhs);
862 }
863
864 /* Return true if two constraints A and B are equal. */
865
866 static bool
867 constraint_equal (struct constraint a, struct constraint b)
868 {
869 return constraint_expr_equal (a.lhs, b.lhs)
870 && constraint_expr_equal (a.rhs, b.rhs);
871 }
872
873
874 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
875
876 static constraint_t
877 constraint_vec_find (vec<constraint_t> vec,
878 struct constraint lookfor)
879 {
880 unsigned int place;
881 constraint_t found;
882
883 if (!vec.exists ())
884 return NULL;
885
886 place = vec.lower_bound (&lookfor, constraint_less);
887 if (place >= vec.length ())
888 return NULL;
889 found = vec[place];
890 if (!constraint_equal (*found, lookfor))
891 return NULL;
892 return found;
893 }
894
895 /* Union two constraint vectors, TO and FROM. Put the result in TO.
896 Returns true of TO set is changed. */
897
898 static bool
899 constraint_set_union (vec<constraint_t> *to,
900 vec<constraint_t> *from)
901 {
902 int i;
903 constraint_t c;
904 bool any_change = false;
905
906 FOR_EACH_VEC_ELT (*from, i, c)
907 {
908 if (constraint_vec_find (*to, *c) == NULL)
909 {
910 unsigned int place = to->lower_bound (c, constraint_less);
911 to->safe_insert (place, c);
912 any_change = true;
913 }
914 }
915 return any_change;
916 }
917
918 /* Expands the solution in SET to all sub-fields of variables included. */
919
920 static bitmap
921 solution_set_expand (bitmap set, bitmap *expanded)
922 {
923 bitmap_iterator bi;
924 unsigned j;
925
926 if (*expanded)
927 return *expanded;
928
929 *expanded = BITMAP_ALLOC (&iteration_obstack);
930
931 /* In a first pass expand to the head of the variables we need to
932 add all sub-fields off. This avoids quadratic behavior. */
933 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
934 {
935 varinfo_t v = get_varinfo (j);
936 if (v->is_artificial_var
937 || v->is_full_var)
938 continue;
939 bitmap_set_bit (*expanded, v->head);
940 }
941
942 /* In the second pass now expand all head variables with subfields. */
943 EXECUTE_IF_SET_IN_BITMAP (*expanded, 0, j, bi)
944 {
945 varinfo_t v = get_varinfo (j);
946 if (v->head != j)
947 continue;
948 for (v = vi_next (v); v != NULL; v = vi_next (v))
949 bitmap_set_bit (*expanded, v->id);
950 }
951
952 /* And finally set the rest of the bits from SET. */
953 bitmap_ior_into (*expanded, set);
954
955 return *expanded;
956 }
957
958 /* Union solution sets TO and DELTA, and add INC to each member of DELTA in the
959 process. */
960
961 static bool
962 set_union_with_increment (bitmap to, bitmap delta, HOST_WIDE_INT inc,
963 bitmap *expanded_delta)
964 {
965 bool changed = false;
966 bitmap_iterator bi;
967 unsigned int i;
968
969 /* If the solution of DELTA contains anything it is good enough to transfer
970 this to TO. */
971 if (bitmap_bit_p (delta, anything_id))
972 return bitmap_set_bit (to, anything_id);
973
974 /* If the offset is unknown we have to expand the solution to
975 all subfields. */
976 if (inc == UNKNOWN_OFFSET)
977 {
978 delta = solution_set_expand (delta, expanded_delta);
979 changed |= bitmap_ior_into (to, delta);
980 return changed;
981 }
982
983 /* For non-zero offset union the offsetted solution into the destination. */
984 EXECUTE_IF_SET_IN_BITMAP (delta, 0, i, bi)
985 {
986 varinfo_t vi = get_varinfo (i);
987
988 /* If this is a variable with just one field just set its bit
989 in the result. */
990 if (vi->is_artificial_var
991 || vi->is_unknown_size_var
992 || vi->is_full_var)
993 changed |= bitmap_set_bit (to, i);
994 else
995 {
996 unsigned HOST_WIDE_INT fieldoffset = vi->offset + inc;
997
998 /* If the offset makes the pointer point to before the
999 variable use offset zero for the field lookup. */
1000 if (inc < 0
1001 && fieldoffset > vi->offset)
1002 fieldoffset = 0;
1003
1004 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
1005
1006 changed |= bitmap_set_bit (to, vi->id);
1007 /* If the result is not exactly at fieldoffset include the next
1008 field as well. See get_constraint_for_ptr_offset for more
1009 rationale. */
1010 if (vi->offset != fieldoffset
1011 && vi->next != 0)
1012 changed |= bitmap_set_bit (to, vi->next);
1013 }
1014 }
1015
1016 return changed;
1017 }
1018
1019 /* Insert constraint C into the list of complex constraints for graph
1020 node VAR. */
1021
1022 static void
1023 insert_into_complex (constraint_graph_t graph,
1024 unsigned int var, constraint_t c)
1025 {
1026 vec<constraint_t> complex = graph->complex[var];
1027 unsigned int place = complex.lower_bound (c, constraint_less);
1028
1029 /* Only insert constraints that do not already exist. */
1030 if (place >= complex.length ()
1031 || !constraint_equal (*c, *complex[place]))
1032 graph->complex[var].safe_insert (place, c);
1033 }
1034
1035
1036 /* Condense two variable nodes into a single variable node, by moving
1037 all associated info from FROM to TO. Returns true if TO node's
1038 constraint set changes after the merge. */
1039
1040 static bool
1041 merge_node_constraints (constraint_graph_t graph, unsigned int to,
1042 unsigned int from)
1043 {
1044 unsigned int i;
1045 constraint_t c;
1046 bool any_change = false;
1047
1048 gcc_checking_assert (find (from) == to);
1049
1050 /* Move all complex constraints from src node into to node */
1051 FOR_EACH_VEC_ELT (graph->complex[from], i, c)
1052 {
1053 /* In complex constraints for node FROM, we may have either
1054 a = *FROM, and *FROM = a, or an offseted constraint which are
1055 always added to the rhs node's constraints. */
1056
1057 if (c->rhs.type == DEREF)
1058 c->rhs.var = to;
1059 else if (c->lhs.type == DEREF)
1060 c->lhs.var = to;
1061 else
1062 c->rhs.var = to;
1063
1064 }
1065 any_change = constraint_set_union (&graph->complex[to],
1066 &graph->complex[from]);
1067 graph->complex[from].release ();
1068 return any_change;
1069 }
1070
1071
1072 /* Remove edges involving NODE from GRAPH. */
1073
1074 static void
1075 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
1076 {
1077 if (graph->succs[node])
1078 BITMAP_FREE (graph->succs[node]);
1079 }
1080
1081 /* Merge GRAPH nodes FROM and TO into node TO. */
1082
1083 static void
1084 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1085 unsigned int from)
1086 {
1087 if (graph->indirect_cycles[from] != -1)
1088 {
1089 /* If we have indirect cycles with the from node, and we have
1090 none on the to node, the to node has indirect cycles from the
1091 from node now that they are unified.
1092 If indirect cycles exist on both, unify the nodes that they
1093 are in a cycle with, since we know they are in a cycle with
1094 each other. */
1095 if (graph->indirect_cycles[to] == -1)
1096 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1097 }
1098
1099 /* Merge all the successor edges. */
1100 if (graph->succs[from])
1101 {
1102 if (!graph->succs[to])
1103 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1104 bitmap_ior_into (graph->succs[to],
1105 graph->succs[from]);
1106 }
1107
1108 clear_edges_for_node (graph, from);
1109 }
1110
1111
1112 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1113 it doesn't exist in the graph already. */
1114
1115 static void
1116 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1117 unsigned int from)
1118 {
1119 if (to == from)
1120 return;
1121
1122 if (!graph->implicit_preds[to])
1123 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1124
1125 if (bitmap_set_bit (graph->implicit_preds[to], from))
1126 stats.num_implicit_edges++;
1127 }
1128
1129 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1130 it doesn't exist in the graph already.
1131 Return false if the edge already existed, true otherwise. */
1132
1133 static void
1134 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1135 unsigned int from)
1136 {
1137 if (!graph->preds[to])
1138 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1139 bitmap_set_bit (graph->preds[to], from);
1140 }
1141
1142 /* Add a graph edge to GRAPH, going from FROM to TO if
1143 it doesn't exist in the graph already.
1144 Return false if the edge already existed, true otherwise. */
1145
1146 static bool
1147 add_graph_edge (constraint_graph_t graph, unsigned int to,
1148 unsigned int from)
1149 {
1150 if (to == from)
1151 {
1152 return false;
1153 }
1154 else
1155 {
1156 bool r = false;
1157
1158 if (!graph->succs[from])
1159 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1160 if (bitmap_set_bit (graph->succs[from], to))
1161 {
1162 r = true;
1163 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1164 stats.num_edges++;
1165 }
1166 return r;
1167 }
1168 }
1169
1170
1171 /* Initialize the constraint graph structure to contain SIZE nodes. */
1172
1173 static void
1174 init_graph (unsigned int size)
1175 {
1176 unsigned int j;
1177
1178 graph = XCNEW (struct constraint_graph);
1179 graph->size = size;
1180 graph->succs = XCNEWVEC (bitmap, graph->size);
1181 graph->indirect_cycles = XNEWVEC (int, graph->size);
1182 graph->rep = XNEWVEC (unsigned int, graph->size);
1183 /* ??? Macros do not support template types with multiple arguments,
1184 so we use a typedef to work around it. */
1185 typedef vec<constraint_t> vec_constraint_t_heap;
1186 graph->complex = XCNEWVEC (vec_constraint_t_heap, size);
1187 graph->pe = XCNEWVEC (unsigned int, graph->size);
1188 graph->pe_rep = XNEWVEC (int, graph->size);
1189
1190 for (j = 0; j < graph->size; j++)
1191 {
1192 graph->rep[j] = j;
1193 graph->pe_rep[j] = -1;
1194 graph->indirect_cycles[j] = -1;
1195 }
1196 }
1197
1198 /* Build the constraint graph, adding only predecessor edges right now. */
1199
1200 static void
1201 build_pred_graph (void)
1202 {
1203 int i;
1204 constraint_t c;
1205 unsigned int j;
1206
1207 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1208 graph->preds = XCNEWVEC (bitmap, graph->size);
1209 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1210 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1211 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1212 graph->points_to = XCNEWVEC (bitmap, graph->size);
1213 graph->eq_rep = XNEWVEC (int, graph->size);
1214 graph->direct_nodes = sbitmap_alloc (graph->size);
1215 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1216 bitmap_clear (graph->direct_nodes);
1217
1218 for (j = 1; j < FIRST_REF_NODE; j++)
1219 {
1220 if (!get_varinfo (j)->is_special_var)
1221 bitmap_set_bit (graph->direct_nodes, j);
1222 }
1223
1224 for (j = 0; j < graph->size; j++)
1225 graph->eq_rep[j] = -1;
1226
1227 for (j = 0; j < varmap.length (); j++)
1228 graph->indirect_cycles[j] = -1;
1229
1230 FOR_EACH_VEC_ELT (constraints, i, c)
1231 {
1232 struct constraint_expr lhs = c->lhs;
1233 struct constraint_expr rhs = c->rhs;
1234 unsigned int lhsvar = lhs.var;
1235 unsigned int rhsvar = rhs.var;
1236
1237 if (lhs.type == DEREF)
1238 {
1239 /* *x = y. */
1240 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1241 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1242 }
1243 else if (rhs.type == DEREF)
1244 {
1245 /* x = *y */
1246 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1247 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1248 else
1249 bitmap_clear_bit (graph->direct_nodes, lhsvar);
1250 }
1251 else if (rhs.type == ADDRESSOF)
1252 {
1253 varinfo_t v;
1254
1255 /* x = &y */
1256 if (graph->points_to[lhsvar] == NULL)
1257 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1258 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1259
1260 if (graph->pointed_by[rhsvar] == NULL)
1261 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1262 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1263
1264 /* Implicitly, *x = y */
1265 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1266
1267 /* All related variables are no longer direct nodes. */
1268 bitmap_clear_bit (graph->direct_nodes, rhsvar);
1269 v = get_varinfo (rhsvar);
1270 if (!v->is_full_var)
1271 {
1272 v = get_varinfo (v->head);
1273 do
1274 {
1275 bitmap_clear_bit (graph->direct_nodes, v->id);
1276 v = vi_next (v);
1277 }
1278 while (v != NULL);
1279 }
1280 bitmap_set_bit (graph->address_taken, rhsvar);
1281 }
1282 else if (lhsvar > anything_id
1283 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1284 {
1285 /* x = y */
1286 add_pred_graph_edge (graph, lhsvar, rhsvar);
1287 /* Implicitly, *x = *y */
1288 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1289 FIRST_REF_NODE + rhsvar);
1290 }
1291 else if (lhs.offset != 0 || rhs.offset != 0)
1292 {
1293 if (rhs.offset != 0)
1294 bitmap_clear_bit (graph->direct_nodes, lhs.var);
1295 else if (lhs.offset != 0)
1296 bitmap_clear_bit (graph->direct_nodes, rhs.var);
1297 }
1298 }
1299 }
1300
1301 /* Build the constraint graph, adding successor edges. */
1302
1303 static void
1304 build_succ_graph (void)
1305 {
1306 unsigned i, t;
1307 constraint_t c;
1308
1309 FOR_EACH_VEC_ELT (constraints, i, c)
1310 {
1311 struct constraint_expr lhs;
1312 struct constraint_expr rhs;
1313 unsigned int lhsvar;
1314 unsigned int rhsvar;
1315
1316 if (!c)
1317 continue;
1318
1319 lhs = c->lhs;
1320 rhs = c->rhs;
1321 lhsvar = find (lhs.var);
1322 rhsvar = find (rhs.var);
1323
1324 if (lhs.type == DEREF)
1325 {
1326 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1327 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1328 }
1329 else if (rhs.type == DEREF)
1330 {
1331 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1332 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1333 }
1334 else if (rhs.type == ADDRESSOF)
1335 {
1336 /* x = &y */
1337 gcc_checking_assert (find (rhs.var) == rhs.var);
1338 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1339 }
1340 else if (lhsvar > anything_id
1341 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1342 {
1343 add_graph_edge (graph, lhsvar, rhsvar);
1344 }
1345 }
1346
1347 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1348 receive pointers. */
1349 t = find (storedanything_id);
1350 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1351 {
1352 if (!bitmap_bit_p (graph->direct_nodes, i)
1353 && get_varinfo (i)->may_have_pointers)
1354 add_graph_edge (graph, find (i), t);
1355 }
1356
1357 /* Everything stored to ANYTHING also potentially escapes. */
1358 add_graph_edge (graph, find (escaped_id), t);
1359 }
1360
1361
1362 /* Changed variables on the last iteration. */
1363 static bitmap changed;
1364
1365 /* Strongly Connected Component visitation info. */
1366
1367 struct scc_info
1368 {
1369 sbitmap visited;
1370 sbitmap deleted;
1371 unsigned int *dfs;
1372 unsigned int *node_mapping;
1373 int current_index;
1374 vec<unsigned> scc_stack;
1375 };
1376
1377
1378 /* Recursive routine to find strongly connected components in GRAPH.
1379 SI is the SCC info to store the information in, and N is the id of current
1380 graph node we are processing.
1381
1382 This is Tarjan's strongly connected component finding algorithm, as
1383 modified by Nuutila to keep only non-root nodes on the stack.
1384 The algorithm can be found in "On finding the strongly connected
1385 connected components in a directed graph" by Esko Nuutila and Eljas
1386 Soisalon-Soininen, in Information Processing Letters volume 49,
1387 number 1, pages 9-14. */
1388
1389 static void
1390 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1391 {
1392 unsigned int i;
1393 bitmap_iterator bi;
1394 unsigned int my_dfs;
1395
1396 bitmap_set_bit (si->visited, n);
1397 si->dfs[n] = si->current_index ++;
1398 my_dfs = si->dfs[n];
1399
1400 /* Visit all the successors. */
1401 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1402 {
1403 unsigned int w;
1404
1405 if (i > LAST_REF_NODE)
1406 break;
1407
1408 w = find (i);
1409 if (bitmap_bit_p (si->deleted, w))
1410 continue;
1411
1412 if (!bitmap_bit_p (si->visited, w))
1413 scc_visit (graph, si, w);
1414
1415 unsigned int t = find (w);
1416 gcc_checking_assert (find (n) == n);
1417 if (si->dfs[t] < si->dfs[n])
1418 si->dfs[n] = si->dfs[t];
1419 }
1420
1421 /* See if any components have been identified. */
1422 if (si->dfs[n] == my_dfs)
1423 {
1424 if (si->scc_stack.length () > 0
1425 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1426 {
1427 bitmap scc = BITMAP_ALLOC (NULL);
1428 unsigned int lowest_node;
1429 bitmap_iterator bi;
1430
1431 bitmap_set_bit (scc, n);
1432
1433 while (si->scc_stack.length () != 0
1434 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1435 {
1436 unsigned int w = si->scc_stack.pop ();
1437
1438 bitmap_set_bit (scc, w);
1439 }
1440
1441 lowest_node = bitmap_first_set_bit (scc);
1442 gcc_assert (lowest_node < FIRST_REF_NODE);
1443
1444 /* Collapse the SCC nodes into a single node, and mark the
1445 indirect cycles. */
1446 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1447 {
1448 if (i < FIRST_REF_NODE)
1449 {
1450 if (unite (lowest_node, i))
1451 unify_nodes (graph, lowest_node, i, false);
1452 }
1453 else
1454 {
1455 unite (lowest_node, i);
1456 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1457 }
1458 }
1459 }
1460 bitmap_set_bit (si->deleted, n);
1461 }
1462 else
1463 si->scc_stack.safe_push (n);
1464 }
1465
1466 /* Unify node FROM into node TO, updating the changed count if
1467 necessary when UPDATE_CHANGED is true. */
1468
1469 static void
1470 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1471 bool update_changed)
1472 {
1473 gcc_checking_assert (to != from && find (to) == to);
1474
1475 if (dump_file && (dump_flags & TDF_DETAILS))
1476 fprintf (dump_file, "Unifying %s to %s\n",
1477 get_varinfo (from)->name,
1478 get_varinfo (to)->name);
1479
1480 if (update_changed)
1481 stats.unified_vars_dynamic++;
1482 else
1483 stats.unified_vars_static++;
1484
1485 merge_graph_nodes (graph, to, from);
1486 if (merge_node_constraints (graph, to, from))
1487 {
1488 if (update_changed)
1489 bitmap_set_bit (changed, to);
1490 }
1491
1492 /* Mark TO as changed if FROM was changed. If TO was already marked
1493 as changed, decrease the changed count. */
1494
1495 if (update_changed
1496 && bitmap_clear_bit (changed, from))
1497 bitmap_set_bit (changed, to);
1498 varinfo_t fromvi = get_varinfo (from);
1499 if (fromvi->solution)
1500 {
1501 /* If the solution changes because of the merging, we need to mark
1502 the variable as changed. */
1503 varinfo_t tovi = get_varinfo (to);
1504 if (bitmap_ior_into (tovi->solution, fromvi->solution))
1505 {
1506 if (update_changed)
1507 bitmap_set_bit (changed, to);
1508 }
1509
1510 BITMAP_FREE (fromvi->solution);
1511 if (fromvi->oldsolution)
1512 BITMAP_FREE (fromvi->oldsolution);
1513
1514 if (stats.iterations > 0
1515 && tovi->oldsolution)
1516 BITMAP_FREE (tovi->oldsolution);
1517 }
1518 if (graph->succs[to])
1519 bitmap_clear_bit (graph->succs[to], to);
1520 }
1521
1522 /* Information needed to compute the topological ordering of a graph. */
1523
1524 struct topo_info
1525 {
1526 /* sbitmap of visited nodes. */
1527 sbitmap visited;
1528 /* Array that stores the topological order of the graph, *in
1529 reverse*. */
1530 vec<unsigned> topo_order;
1531 };
1532
1533
1534 /* Initialize and return a topological info structure. */
1535
1536 static struct topo_info *
1537 init_topo_info (void)
1538 {
1539 size_t size = graph->size;
1540 struct topo_info *ti = XNEW (struct topo_info);
1541 ti->visited = sbitmap_alloc (size);
1542 bitmap_clear (ti->visited);
1543 ti->topo_order.create (1);
1544 return ti;
1545 }
1546
1547
1548 /* Free the topological sort info pointed to by TI. */
1549
1550 static void
1551 free_topo_info (struct topo_info *ti)
1552 {
1553 sbitmap_free (ti->visited);
1554 ti->topo_order.release ();
1555 free (ti);
1556 }
1557
1558 /* Visit the graph in topological order, and store the order in the
1559 topo_info structure. */
1560
1561 static void
1562 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1563 unsigned int n)
1564 {
1565 bitmap_iterator bi;
1566 unsigned int j;
1567
1568 bitmap_set_bit (ti->visited, n);
1569
1570 if (graph->succs[n])
1571 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1572 {
1573 if (!bitmap_bit_p (ti->visited, j))
1574 topo_visit (graph, ti, j);
1575 }
1576
1577 ti->topo_order.safe_push (n);
1578 }
1579
1580 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1581 starting solution for y. */
1582
1583 static void
1584 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1585 bitmap delta, bitmap *expanded_delta)
1586 {
1587 unsigned int lhs = c->lhs.var;
1588 bool flag = false;
1589 bitmap sol = get_varinfo (lhs)->solution;
1590 unsigned int j;
1591 bitmap_iterator bi;
1592 HOST_WIDE_INT roffset = c->rhs.offset;
1593
1594 /* Our IL does not allow this. */
1595 gcc_checking_assert (c->lhs.offset == 0);
1596
1597 /* If the solution of Y contains anything it is good enough to transfer
1598 this to the LHS. */
1599 if (bitmap_bit_p (delta, anything_id))
1600 {
1601 flag |= bitmap_set_bit (sol, anything_id);
1602 goto done;
1603 }
1604
1605 /* If we do not know at with offset the rhs is dereferenced compute
1606 the reachability set of DELTA, conservatively assuming it is
1607 dereferenced at all valid offsets. */
1608 if (roffset == UNKNOWN_OFFSET)
1609 {
1610 delta = solution_set_expand (delta, expanded_delta);
1611 /* No further offset processing is necessary. */
1612 roffset = 0;
1613 }
1614
1615 /* For each variable j in delta (Sol(y)), add
1616 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1617 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1618 {
1619 varinfo_t v = get_varinfo (j);
1620 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1621 unsigned int t;
1622
1623 if (v->is_full_var)
1624 fieldoffset = v->offset;
1625 else if (roffset != 0)
1626 v = first_vi_for_offset (v, fieldoffset);
1627 /* If the access is outside of the variable we can ignore it. */
1628 if (!v)
1629 continue;
1630
1631 do
1632 {
1633 t = find (v->id);
1634
1635 /* Adding edges from the special vars is pointless.
1636 They don't have sets that can change. */
1637 if (get_varinfo (t)->is_special_var)
1638 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1639 /* Merging the solution from ESCAPED needlessly increases
1640 the set. Use ESCAPED as representative instead. */
1641 else if (v->id == escaped_id)
1642 flag |= bitmap_set_bit (sol, escaped_id);
1643 else if (v->may_have_pointers
1644 && add_graph_edge (graph, lhs, t))
1645 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1646
1647 /* If the variable is not exactly at the requested offset
1648 we have to include the next one. */
1649 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1650 || v->next == 0)
1651 break;
1652
1653 v = vi_next (v);
1654 fieldoffset = v->offset;
1655 }
1656 while (1);
1657 }
1658
1659 done:
1660 /* If the LHS solution changed, mark the var as changed. */
1661 if (flag)
1662 {
1663 get_varinfo (lhs)->solution = sol;
1664 bitmap_set_bit (changed, lhs);
1665 }
1666 }
1667
1668 /* Process a constraint C that represents *(x + off) = y using DELTA
1669 as the starting solution for x. */
1670
1671 static void
1672 do_ds_constraint (constraint_t c, bitmap delta, bitmap *expanded_delta)
1673 {
1674 unsigned int rhs = c->rhs.var;
1675 bitmap sol = get_varinfo (rhs)->solution;
1676 unsigned int j;
1677 bitmap_iterator bi;
1678 HOST_WIDE_INT loff = c->lhs.offset;
1679 bool escaped_p = false;
1680
1681 /* Our IL does not allow this. */
1682 gcc_checking_assert (c->rhs.offset == 0);
1683
1684 /* If the solution of y contains ANYTHING simply use the ANYTHING
1685 solution. This avoids needlessly increasing the points-to sets. */
1686 if (bitmap_bit_p (sol, anything_id))
1687 sol = get_varinfo (find (anything_id))->solution;
1688
1689 /* If the solution for x contains ANYTHING we have to merge the
1690 solution of y into all pointer variables which we do via
1691 STOREDANYTHING. */
1692 if (bitmap_bit_p (delta, anything_id))
1693 {
1694 unsigned t = find (storedanything_id);
1695 if (add_graph_edge (graph, t, rhs))
1696 {
1697 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1698 bitmap_set_bit (changed, t);
1699 }
1700 return;
1701 }
1702
1703 /* If we do not know at with offset the rhs is dereferenced compute
1704 the reachability set of DELTA, conservatively assuming it is
1705 dereferenced at all valid offsets. */
1706 if (loff == UNKNOWN_OFFSET)
1707 {
1708 delta = solution_set_expand (delta, expanded_delta);
1709 loff = 0;
1710 }
1711
1712 /* For each member j of delta (Sol(x)), add an edge from y to j and
1713 union Sol(y) into Sol(j) */
1714 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1715 {
1716 varinfo_t v = get_varinfo (j);
1717 unsigned int t;
1718 HOST_WIDE_INT fieldoffset = v->offset + loff;
1719
1720 if (v->is_full_var)
1721 fieldoffset = v->offset;
1722 else if (loff != 0)
1723 v = first_vi_for_offset (v, fieldoffset);
1724 /* If the access is outside of the variable we can ignore it. */
1725 if (!v)
1726 continue;
1727
1728 do
1729 {
1730 if (v->may_have_pointers)
1731 {
1732 /* If v is a global variable then this is an escape point. */
1733 if (v->is_global_var
1734 && !escaped_p)
1735 {
1736 t = find (escaped_id);
1737 if (add_graph_edge (graph, t, rhs)
1738 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1739 bitmap_set_bit (changed, t);
1740 /* Enough to let rhs escape once. */
1741 escaped_p = true;
1742 }
1743
1744 if (v->is_special_var)
1745 break;
1746
1747 t = find (v->id);
1748 if (add_graph_edge (graph, t, rhs)
1749 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1750 bitmap_set_bit (changed, t);
1751 }
1752
1753 /* If the variable is not exactly at the requested offset
1754 we have to include the next one. */
1755 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1756 || v->next == 0)
1757 break;
1758
1759 v = vi_next (v);
1760 fieldoffset = v->offset;
1761 }
1762 while (1);
1763 }
1764 }
1765
1766 /* Handle a non-simple (simple meaning requires no iteration),
1767 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1768
1769 static void
1770 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta,
1771 bitmap *expanded_delta)
1772 {
1773 if (c->lhs.type == DEREF)
1774 {
1775 if (c->rhs.type == ADDRESSOF)
1776 {
1777 gcc_unreachable ();
1778 }
1779 else
1780 {
1781 /* *x = y */
1782 do_ds_constraint (c, delta, expanded_delta);
1783 }
1784 }
1785 else if (c->rhs.type == DEREF)
1786 {
1787 /* x = *y */
1788 if (!(get_varinfo (c->lhs.var)->is_special_var))
1789 do_sd_constraint (graph, c, delta, expanded_delta);
1790 }
1791 else
1792 {
1793 bitmap tmp;
1794 bool flag = false;
1795
1796 gcc_checking_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR
1797 && c->rhs.offset != 0 && c->lhs.offset == 0);
1798 tmp = get_varinfo (c->lhs.var)->solution;
1799
1800 flag = set_union_with_increment (tmp, delta, c->rhs.offset,
1801 expanded_delta);
1802
1803 if (flag)
1804 bitmap_set_bit (changed, c->lhs.var);
1805 }
1806 }
1807
1808 /* Initialize and return a new SCC info structure. */
1809
1810 static struct scc_info *
1811 init_scc_info (size_t size)
1812 {
1813 struct scc_info *si = XNEW (struct scc_info);
1814 size_t i;
1815
1816 si->current_index = 0;
1817 si->visited = sbitmap_alloc (size);
1818 bitmap_clear (si->visited);
1819 si->deleted = sbitmap_alloc (size);
1820 bitmap_clear (si->deleted);
1821 si->node_mapping = XNEWVEC (unsigned int, size);
1822 si->dfs = XCNEWVEC (unsigned int, size);
1823
1824 for (i = 0; i < size; i++)
1825 si->node_mapping[i] = i;
1826
1827 si->scc_stack.create (1);
1828 return si;
1829 }
1830
1831 /* Free an SCC info structure pointed to by SI */
1832
1833 static void
1834 free_scc_info (struct scc_info *si)
1835 {
1836 sbitmap_free (si->visited);
1837 sbitmap_free (si->deleted);
1838 free (si->node_mapping);
1839 free (si->dfs);
1840 si->scc_stack.release ();
1841 free (si);
1842 }
1843
1844
1845 /* Find indirect cycles in GRAPH that occur, using strongly connected
1846 components, and note them in the indirect cycles map.
1847
1848 This technique comes from Ben Hardekopf and Calvin Lin,
1849 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1850 Lines of Code", submitted to PLDI 2007. */
1851
1852 static void
1853 find_indirect_cycles (constraint_graph_t graph)
1854 {
1855 unsigned int i;
1856 unsigned int size = graph->size;
1857 struct scc_info *si = init_scc_info (size);
1858
1859 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1860 if (!bitmap_bit_p (si->visited, i) && find (i) == i)
1861 scc_visit (graph, si, i);
1862
1863 free_scc_info (si);
1864 }
1865
1866 /* Compute a topological ordering for GRAPH, and store the result in the
1867 topo_info structure TI. */
1868
1869 static void
1870 compute_topo_order (constraint_graph_t graph,
1871 struct topo_info *ti)
1872 {
1873 unsigned int i;
1874 unsigned int size = graph->size;
1875
1876 for (i = 0; i != size; ++i)
1877 if (!bitmap_bit_p (ti->visited, i) && find (i) == i)
1878 topo_visit (graph, ti, i);
1879 }
1880
1881 /* Structure used to for hash value numbering of pointer equivalence
1882 classes. */
1883
1884 typedef struct equiv_class_label
1885 {
1886 hashval_t hashcode;
1887 unsigned int equivalence_class;
1888 bitmap labels;
1889 } *equiv_class_label_t;
1890 typedef const struct equiv_class_label *const_equiv_class_label_t;
1891
1892 /* Equiv_class_label hashtable helpers. */
1893
1894 struct equiv_class_hasher : typed_free_remove <equiv_class_label>
1895 {
1896 typedef equiv_class_label value_type;
1897 typedef equiv_class_label compare_type;
1898 static inline hashval_t hash (const value_type *);
1899 static inline bool equal (const value_type *, const compare_type *);
1900 };
1901
1902 /* Hash function for a equiv_class_label_t */
1903
1904 inline hashval_t
1905 equiv_class_hasher::hash (const value_type *ecl)
1906 {
1907 return ecl->hashcode;
1908 }
1909
1910 /* Equality function for two equiv_class_label_t's. */
1911
1912 inline bool
1913 equiv_class_hasher::equal (const value_type *eql1, const compare_type *eql2)
1914 {
1915 return (eql1->hashcode == eql2->hashcode
1916 && bitmap_equal_p (eql1->labels, eql2->labels));
1917 }
1918
1919 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1920 classes. */
1921 static hash_table <equiv_class_hasher> pointer_equiv_class_table;
1922
1923 /* A hashtable for mapping a bitmap of labels->location equivalence
1924 classes. */
1925 static hash_table <equiv_class_hasher> location_equiv_class_table;
1926
1927 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1928 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1929 is equivalent to. */
1930
1931 static equiv_class_label *
1932 equiv_class_lookup_or_add (hash_table <equiv_class_hasher> table, bitmap labels)
1933 {
1934 equiv_class_label **slot;
1935 equiv_class_label ecl;
1936
1937 ecl.labels = labels;
1938 ecl.hashcode = bitmap_hash (labels);
1939 slot = table.find_slot_with_hash (&ecl, ecl.hashcode, INSERT);
1940 if (!*slot)
1941 {
1942 *slot = XNEW (struct equiv_class_label);
1943 (*slot)->labels = labels;
1944 (*slot)->hashcode = ecl.hashcode;
1945 (*slot)->equivalence_class = 0;
1946 }
1947
1948 return *slot;
1949 }
1950
1951 /* Perform offline variable substitution.
1952
1953 This is a worst case quadratic time way of identifying variables
1954 that must have equivalent points-to sets, including those caused by
1955 static cycles, and single entry subgraphs, in the constraint graph.
1956
1957 The technique is described in "Exploiting Pointer and Location
1958 Equivalence to Optimize Pointer Analysis. In the 14th International
1959 Static Analysis Symposium (SAS), August 2007." It is known as the
1960 "HU" algorithm, and is equivalent to value numbering the collapsed
1961 constraint graph including evaluating unions.
1962
1963 The general method of finding equivalence classes is as follows:
1964 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1965 Initialize all non-REF nodes to be direct nodes.
1966 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1967 variable}
1968 For each constraint containing the dereference, we also do the same
1969 thing.
1970
1971 We then compute SCC's in the graph and unify nodes in the same SCC,
1972 including pts sets.
1973
1974 For each non-collapsed node x:
1975 Visit all unvisited explicit incoming edges.
1976 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1977 where y->x.
1978 Lookup the equivalence class for pts(x).
1979 If we found one, equivalence_class(x) = found class.
1980 Otherwise, equivalence_class(x) = new class, and new_class is
1981 added to the lookup table.
1982
1983 All direct nodes with the same equivalence class can be replaced
1984 with a single representative node.
1985 All unlabeled nodes (label == 0) are not pointers and all edges
1986 involving them can be eliminated.
1987 We perform these optimizations during rewrite_constraints
1988
1989 In addition to pointer equivalence class finding, we also perform
1990 location equivalence class finding. This is the set of variables
1991 that always appear together in points-to sets. We use this to
1992 compress the size of the points-to sets. */
1993
1994 /* Current maximum pointer equivalence class id. */
1995 static int pointer_equiv_class;
1996
1997 /* Current maximum location equivalence class id. */
1998 static int location_equiv_class;
1999
2000 /* Recursive routine to find strongly connected components in GRAPH,
2001 and label it's nodes with DFS numbers. */
2002
2003 static void
2004 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2005 {
2006 unsigned int i;
2007 bitmap_iterator bi;
2008 unsigned int my_dfs;
2009
2010 gcc_checking_assert (si->node_mapping[n] == n);
2011 bitmap_set_bit (si->visited, n);
2012 si->dfs[n] = si->current_index ++;
2013 my_dfs = si->dfs[n];
2014
2015 /* Visit all the successors. */
2016 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2017 {
2018 unsigned int w = si->node_mapping[i];
2019
2020 if (bitmap_bit_p (si->deleted, w))
2021 continue;
2022
2023 if (!bitmap_bit_p (si->visited, w))
2024 condense_visit (graph, si, w);
2025
2026 unsigned int t = si->node_mapping[w];
2027 gcc_checking_assert (si->node_mapping[n] == n);
2028 if (si->dfs[t] < si->dfs[n])
2029 si->dfs[n] = si->dfs[t];
2030 }
2031
2032 /* Visit all the implicit predecessors. */
2033 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2034 {
2035 unsigned int w = si->node_mapping[i];
2036
2037 if (bitmap_bit_p (si->deleted, w))
2038 continue;
2039
2040 if (!bitmap_bit_p (si->visited, w))
2041 condense_visit (graph, si, w);
2042
2043 unsigned int t = si->node_mapping[w];
2044 gcc_assert (si->node_mapping[n] == n);
2045 if (si->dfs[t] < si->dfs[n])
2046 si->dfs[n] = si->dfs[t];
2047 }
2048
2049 /* See if any components have been identified. */
2050 if (si->dfs[n] == my_dfs)
2051 {
2052 while (si->scc_stack.length () != 0
2053 && si->dfs[si->scc_stack.last ()] >= my_dfs)
2054 {
2055 unsigned int w = si->scc_stack.pop ();
2056 si->node_mapping[w] = n;
2057
2058 if (!bitmap_bit_p (graph->direct_nodes, w))
2059 bitmap_clear_bit (graph->direct_nodes, n);
2060
2061 /* Unify our nodes. */
2062 if (graph->preds[w])
2063 {
2064 if (!graph->preds[n])
2065 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2066 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2067 }
2068 if (graph->implicit_preds[w])
2069 {
2070 if (!graph->implicit_preds[n])
2071 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2072 bitmap_ior_into (graph->implicit_preds[n],
2073 graph->implicit_preds[w]);
2074 }
2075 if (graph->points_to[w])
2076 {
2077 if (!graph->points_to[n])
2078 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2079 bitmap_ior_into (graph->points_to[n],
2080 graph->points_to[w]);
2081 }
2082 }
2083 bitmap_set_bit (si->deleted, n);
2084 }
2085 else
2086 si->scc_stack.safe_push (n);
2087 }
2088
2089 /* Label pointer equivalences.
2090
2091 This performs a value numbering of the constraint graph to
2092 discover which variables will always have the same points-to sets
2093 under the current set of constraints.
2094
2095 The way it value numbers is to store the set of points-to bits
2096 generated by the constraints and graph edges. This is just used as a
2097 hash and equality comparison. The *actual set of points-to bits* is
2098 completely irrelevant, in that we don't care about being able to
2099 extract them later.
2100
2101 The equality values (currently bitmaps) just have to satisfy a few
2102 constraints, the main ones being:
2103 1. The combining operation must be order independent.
2104 2. The end result of a given set of operations must be unique iff the
2105 combination of input values is unique
2106 3. Hashable. */
2107
2108 static void
2109 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2110 {
2111 unsigned int i, first_pred;
2112 bitmap_iterator bi;
2113
2114 bitmap_set_bit (si->visited, n);
2115
2116 /* Label and union our incoming edges's points to sets. */
2117 first_pred = -1U;
2118 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2119 {
2120 unsigned int w = si->node_mapping[i];
2121 if (!bitmap_bit_p (si->visited, w))
2122 label_visit (graph, si, w);
2123
2124 /* Skip unused edges */
2125 if (w == n || graph->pointer_label[w] == 0)
2126 continue;
2127
2128 if (graph->points_to[w])
2129 {
2130 if (!graph->points_to[n])
2131 {
2132 if (first_pred == -1U)
2133 first_pred = w;
2134 else
2135 {
2136 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2137 bitmap_ior (graph->points_to[n],
2138 graph->points_to[first_pred],
2139 graph->points_to[w]);
2140 }
2141 }
2142 else
2143 bitmap_ior_into (graph->points_to[n], graph->points_to[w]);
2144 }
2145 }
2146
2147 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2148 if (!bitmap_bit_p (graph->direct_nodes, n))
2149 {
2150 if (!graph->points_to[n])
2151 {
2152 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2153 if (first_pred != -1U)
2154 bitmap_copy (graph->points_to[n], graph->points_to[first_pred]);
2155 }
2156 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2157 graph->pointer_label[n] = pointer_equiv_class++;
2158 equiv_class_label_t ecl;
2159 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2160 graph->points_to[n]);
2161 ecl->equivalence_class = graph->pointer_label[n];
2162 return;
2163 }
2164
2165 /* If there was only a single non-empty predecessor the pointer equiv
2166 class is the same. */
2167 if (!graph->points_to[n])
2168 {
2169 if (first_pred != -1U)
2170 {
2171 graph->pointer_label[n] = graph->pointer_label[first_pred];
2172 graph->points_to[n] = graph->points_to[first_pred];
2173 }
2174 return;
2175 }
2176
2177 if (!bitmap_empty_p (graph->points_to[n]))
2178 {
2179 equiv_class_label_t ecl;
2180 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2181 graph->points_to[n]);
2182 if (ecl->equivalence_class == 0)
2183 ecl->equivalence_class = pointer_equiv_class++;
2184 else
2185 {
2186 BITMAP_FREE (graph->points_to[n]);
2187 graph->points_to[n] = ecl->labels;
2188 }
2189 graph->pointer_label[n] = ecl->equivalence_class;
2190 }
2191 }
2192
2193 /* Print the pred graph in dot format. */
2194
2195 static void
2196 dump_pred_graph (struct scc_info *si, FILE *file)
2197 {
2198 unsigned int i;
2199
2200 /* Only print the graph if it has already been initialized: */
2201 if (!graph)
2202 return;
2203
2204 /* Prints the header of the dot file: */
2205 fprintf (file, "strict digraph {\n");
2206 fprintf (file, " node [\n shape = box\n ]\n");
2207 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
2208 fprintf (file, "\n // List of nodes and complex constraints in "
2209 "the constraint graph:\n");
2210
2211 /* The next lines print the nodes in the graph together with the
2212 complex constraints attached to them. */
2213 for (i = 1; i < graph->size; i++)
2214 {
2215 if (i == FIRST_REF_NODE)
2216 continue;
2217 if (si->node_mapping[i] != i)
2218 continue;
2219 if (i < FIRST_REF_NODE)
2220 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2221 else
2222 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2223 if (graph->points_to[i]
2224 && !bitmap_empty_p (graph->points_to[i]))
2225 {
2226 fprintf (file, "[label=\"%s = {", get_varinfo (i)->name);
2227 unsigned j;
2228 bitmap_iterator bi;
2229 EXECUTE_IF_SET_IN_BITMAP (graph->points_to[i], 0, j, bi)
2230 fprintf (file, " %d", j);
2231 fprintf (file, " }\"]");
2232 }
2233 fprintf (file, ";\n");
2234 }
2235
2236 /* Go over the edges. */
2237 fprintf (file, "\n // Edges in the constraint graph:\n");
2238 for (i = 1; i < graph->size; i++)
2239 {
2240 unsigned j;
2241 bitmap_iterator bi;
2242 if (si->node_mapping[i] != i)
2243 continue;
2244 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[i], 0, j, bi)
2245 {
2246 unsigned from = si->node_mapping[j];
2247 if (from < FIRST_REF_NODE)
2248 fprintf (file, "\"%s\"", get_varinfo (from)->name);
2249 else
2250 fprintf (file, "\"*%s\"", get_varinfo (from - FIRST_REF_NODE)->name);
2251 fprintf (file, " -> ");
2252 if (i < FIRST_REF_NODE)
2253 fprintf (file, "\"%s\"", get_varinfo (i)->name);
2254 else
2255 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
2256 fprintf (file, ";\n");
2257 }
2258 }
2259
2260 /* Prints the tail of the dot file. */
2261 fprintf (file, "}\n");
2262 }
2263
2264 /* Perform offline variable substitution, discovering equivalence
2265 classes, and eliminating non-pointer variables. */
2266
2267 static struct scc_info *
2268 perform_var_substitution (constraint_graph_t graph)
2269 {
2270 unsigned int i;
2271 unsigned int size = graph->size;
2272 struct scc_info *si = init_scc_info (size);
2273
2274 bitmap_obstack_initialize (&iteration_obstack);
2275 pointer_equiv_class_table.create (511);
2276 location_equiv_class_table.create (511);
2277 pointer_equiv_class = 1;
2278 location_equiv_class = 1;
2279
2280 /* Condense the nodes, which means to find SCC's, count incoming
2281 predecessors, and unite nodes in SCC's. */
2282 for (i = 1; i < FIRST_REF_NODE; i++)
2283 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2284 condense_visit (graph, si, si->node_mapping[i]);
2285
2286 if (dump_file && (dump_flags & TDF_GRAPH))
2287 {
2288 fprintf (dump_file, "\n\n// The constraint graph before var-substitution "
2289 "in dot format:\n");
2290 dump_pred_graph (si, dump_file);
2291 fprintf (dump_file, "\n\n");
2292 }
2293
2294 bitmap_clear (si->visited);
2295 /* Actually the label the nodes for pointer equivalences */
2296 for (i = 1; i < FIRST_REF_NODE; i++)
2297 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2298 label_visit (graph, si, si->node_mapping[i]);
2299
2300 /* Calculate location equivalence labels. */
2301 for (i = 1; i < FIRST_REF_NODE; i++)
2302 {
2303 bitmap pointed_by;
2304 bitmap_iterator bi;
2305 unsigned int j;
2306
2307 if (!graph->pointed_by[i])
2308 continue;
2309 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2310
2311 /* Translate the pointed-by mapping for pointer equivalence
2312 labels. */
2313 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2314 {
2315 bitmap_set_bit (pointed_by,
2316 graph->pointer_label[si->node_mapping[j]]);
2317 }
2318 /* The original pointed_by is now dead. */
2319 BITMAP_FREE (graph->pointed_by[i]);
2320
2321 /* Look up the location equivalence label if one exists, or make
2322 one otherwise. */
2323 equiv_class_label_t ecl;
2324 ecl = equiv_class_lookup_or_add (location_equiv_class_table, pointed_by);
2325 if (ecl->equivalence_class == 0)
2326 ecl->equivalence_class = location_equiv_class++;
2327 else
2328 {
2329 if (dump_file && (dump_flags & TDF_DETAILS))
2330 fprintf (dump_file, "Found location equivalence for node %s\n",
2331 get_varinfo (i)->name);
2332 BITMAP_FREE (pointed_by);
2333 }
2334 graph->loc_label[i] = ecl->equivalence_class;
2335
2336 }
2337
2338 if (dump_file && (dump_flags & TDF_DETAILS))
2339 for (i = 1; i < FIRST_REF_NODE; i++)
2340 {
2341 unsigned j = si->node_mapping[i];
2342 if (j != i)
2343 {
2344 fprintf (dump_file, "%s node id %d ",
2345 bitmap_bit_p (graph->direct_nodes, i)
2346 ? "Direct" : "Indirect", i);
2347 if (i < FIRST_REF_NODE)
2348 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2349 else
2350 fprintf (dump_file, "\"*%s\"",
2351 get_varinfo (i - FIRST_REF_NODE)->name);
2352 fprintf (dump_file, " mapped to SCC leader node id %d ", j);
2353 if (j < FIRST_REF_NODE)
2354 fprintf (dump_file, "\"%s\"\n", get_varinfo (j)->name);
2355 else
2356 fprintf (dump_file, "\"*%s\"\n",
2357 get_varinfo (j - FIRST_REF_NODE)->name);
2358 }
2359 else
2360 {
2361 fprintf (dump_file,
2362 "Equivalence classes for %s node id %d ",
2363 bitmap_bit_p (graph->direct_nodes, i)
2364 ? "direct" : "indirect", i);
2365 if (i < FIRST_REF_NODE)
2366 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2367 else
2368 fprintf (dump_file, "\"*%s\"",
2369 get_varinfo (i - FIRST_REF_NODE)->name);
2370 fprintf (dump_file,
2371 ": pointer %d, location %d\n",
2372 graph->pointer_label[i], graph->loc_label[i]);
2373 }
2374 }
2375
2376 /* Quickly eliminate our non-pointer variables. */
2377
2378 for (i = 1; i < FIRST_REF_NODE; i++)
2379 {
2380 unsigned int node = si->node_mapping[i];
2381
2382 if (graph->pointer_label[node] == 0)
2383 {
2384 if (dump_file && (dump_flags & TDF_DETAILS))
2385 fprintf (dump_file,
2386 "%s is a non-pointer variable, eliminating edges.\n",
2387 get_varinfo (node)->name);
2388 stats.nonpointer_vars++;
2389 clear_edges_for_node (graph, node);
2390 }
2391 }
2392
2393 return si;
2394 }
2395
2396 /* Free information that was only necessary for variable
2397 substitution. */
2398
2399 static void
2400 free_var_substitution_info (struct scc_info *si)
2401 {
2402 free_scc_info (si);
2403 free (graph->pointer_label);
2404 free (graph->loc_label);
2405 free (graph->pointed_by);
2406 free (graph->points_to);
2407 free (graph->eq_rep);
2408 sbitmap_free (graph->direct_nodes);
2409 pointer_equiv_class_table.dispose ();
2410 location_equiv_class_table.dispose ();
2411 bitmap_obstack_release (&iteration_obstack);
2412 }
2413
2414 /* Return an existing node that is equivalent to NODE, which has
2415 equivalence class LABEL, if one exists. Return NODE otherwise. */
2416
2417 static unsigned int
2418 find_equivalent_node (constraint_graph_t graph,
2419 unsigned int node, unsigned int label)
2420 {
2421 /* If the address version of this variable is unused, we can
2422 substitute it for anything else with the same label.
2423 Otherwise, we know the pointers are equivalent, but not the
2424 locations, and we can unite them later. */
2425
2426 if (!bitmap_bit_p (graph->address_taken, node))
2427 {
2428 gcc_checking_assert (label < graph->size);
2429
2430 if (graph->eq_rep[label] != -1)
2431 {
2432 /* Unify the two variables since we know they are equivalent. */
2433 if (unite (graph->eq_rep[label], node))
2434 unify_nodes (graph, graph->eq_rep[label], node, false);
2435 return graph->eq_rep[label];
2436 }
2437 else
2438 {
2439 graph->eq_rep[label] = node;
2440 graph->pe_rep[label] = node;
2441 }
2442 }
2443 else
2444 {
2445 gcc_checking_assert (label < graph->size);
2446 graph->pe[node] = label;
2447 if (graph->pe_rep[label] == -1)
2448 graph->pe_rep[label] = node;
2449 }
2450
2451 return node;
2452 }
2453
2454 /* Unite pointer equivalent but not location equivalent nodes in
2455 GRAPH. This may only be performed once variable substitution is
2456 finished. */
2457
2458 static void
2459 unite_pointer_equivalences (constraint_graph_t graph)
2460 {
2461 unsigned int i;
2462
2463 /* Go through the pointer equivalences and unite them to their
2464 representative, if they aren't already. */
2465 for (i = 1; i < FIRST_REF_NODE; i++)
2466 {
2467 unsigned int label = graph->pe[i];
2468 if (label)
2469 {
2470 int label_rep = graph->pe_rep[label];
2471
2472 if (label_rep == -1)
2473 continue;
2474
2475 label_rep = find (label_rep);
2476 if (label_rep >= 0 && unite (label_rep, find (i)))
2477 unify_nodes (graph, label_rep, i, false);
2478 }
2479 }
2480 }
2481
2482 /* Move complex constraints to the GRAPH nodes they belong to. */
2483
2484 static void
2485 move_complex_constraints (constraint_graph_t graph)
2486 {
2487 int i;
2488 constraint_t c;
2489
2490 FOR_EACH_VEC_ELT (constraints, i, c)
2491 {
2492 if (c)
2493 {
2494 struct constraint_expr lhs = c->lhs;
2495 struct constraint_expr rhs = c->rhs;
2496
2497 if (lhs.type == DEREF)
2498 {
2499 insert_into_complex (graph, lhs.var, c);
2500 }
2501 else if (rhs.type == DEREF)
2502 {
2503 if (!(get_varinfo (lhs.var)->is_special_var))
2504 insert_into_complex (graph, rhs.var, c);
2505 }
2506 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2507 && (lhs.offset != 0 || rhs.offset != 0))
2508 {
2509 insert_into_complex (graph, rhs.var, c);
2510 }
2511 }
2512 }
2513 }
2514
2515
2516 /* Optimize and rewrite complex constraints while performing
2517 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2518 result of perform_variable_substitution. */
2519
2520 static void
2521 rewrite_constraints (constraint_graph_t graph,
2522 struct scc_info *si)
2523 {
2524 int i;
2525 constraint_t c;
2526
2527 #ifdef ENABLE_CHECKING
2528 for (unsigned int j = 0; j < graph->size; j++)
2529 gcc_assert (find (j) == j);
2530 #endif
2531
2532 FOR_EACH_VEC_ELT (constraints, i, c)
2533 {
2534 struct constraint_expr lhs = c->lhs;
2535 struct constraint_expr rhs = c->rhs;
2536 unsigned int lhsvar = find (lhs.var);
2537 unsigned int rhsvar = find (rhs.var);
2538 unsigned int lhsnode, rhsnode;
2539 unsigned int lhslabel, rhslabel;
2540
2541 lhsnode = si->node_mapping[lhsvar];
2542 rhsnode = si->node_mapping[rhsvar];
2543 lhslabel = graph->pointer_label[lhsnode];
2544 rhslabel = graph->pointer_label[rhsnode];
2545
2546 /* See if it is really a non-pointer variable, and if so, ignore
2547 the constraint. */
2548 if (lhslabel == 0)
2549 {
2550 if (dump_file && (dump_flags & TDF_DETAILS))
2551 {
2552
2553 fprintf (dump_file, "%s is a non-pointer variable,"
2554 "ignoring constraint:",
2555 get_varinfo (lhs.var)->name);
2556 dump_constraint (dump_file, c);
2557 fprintf (dump_file, "\n");
2558 }
2559 constraints[i] = NULL;
2560 continue;
2561 }
2562
2563 if (rhslabel == 0)
2564 {
2565 if (dump_file && (dump_flags & TDF_DETAILS))
2566 {
2567
2568 fprintf (dump_file, "%s is a non-pointer variable,"
2569 "ignoring constraint:",
2570 get_varinfo (rhs.var)->name);
2571 dump_constraint (dump_file, c);
2572 fprintf (dump_file, "\n");
2573 }
2574 constraints[i] = NULL;
2575 continue;
2576 }
2577
2578 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2579 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2580 c->lhs.var = lhsvar;
2581 c->rhs.var = rhsvar;
2582 }
2583 }
2584
2585 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2586 part of an SCC, false otherwise. */
2587
2588 static bool
2589 eliminate_indirect_cycles (unsigned int node)
2590 {
2591 if (graph->indirect_cycles[node] != -1
2592 && !bitmap_empty_p (get_varinfo (node)->solution))
2593 {
2594 unsigned int i;
2595 auto_vec<unsigned> queue;
2596 int queuepos;
2597 unsigned int to = find (graph->indirect_cycles[node]);
2598 bitmap_iterator bi;
2599
2600 /* We can't touch the solution set and call unify_nodes
2601 at the same time, because unify_nodes is going to do
2602 bitmap unions into it. */
2603
2604 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2605 {
2606 if (find (i) == i && i != to)
2607 {
2608 if (unite (to, i))
2609 queue.safe_push (i);
2610 }
2611 }
2612
2613 for (queuepos = 0;
2614 queue.iterate (queuepos, &i);
2615 queuepos++)
2616 {
2617 unify_nodes (graph, to, i, true);
2618 }
2619 return true;
2620 }
2621 return false;
2622 }
2623
2624 /* Solve the constraint graph GRAPH using our worklist solver.
2625 This is based on the PW* family of solvers from the "Efficient Field
2626 Sensitive Pointer Analysis for C" paper.
2627 It works by iterating over all the graph nodes, processing the complex
2628 constraints and propagating the copy constraints, until everything stops
2629 changed. This corresponds to steps 6-8 in the solving list given above. */
2630
2631 static void
2632 solve_graph (constraint_graph_t graph)
2633 {
2634 unsigned int size = graph->size;
2635 unsigned int i;
2636 bitmap pts;
2637
2638 changed = BITMAP_ALLOC (NULL);
2639
2640 /* Mark all initial non-collapsed nodes as changed. */
2641 for (i = 1; i < size; i++)
2642 {
2643 varinfo_t ivi = get_varinfo (i);
2644 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2645 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2646 || graph->complex[i].length () > 0))
2647 bitmap_set_bit (changed, i);
2648 }
2649
2650 /* Allocate a bitmap to be used to store the changed bits. */
2651 pts = BITMAP_ALLOC (&pta_obstack);
2652
2653 while (!bitmap_empty_p (changed))
2654 {
2655 unsigned int i;
2656 struct topo_info *ti = init_topo_info ();
2657 stats.iterations++;
2658
2659 bitmap_obstack_initialize (&iteration_obstack);
2660
2661 compute_topo_order (graph, ti);
2662
2663 while (ti->topo_order.length () != 0)
2664 {
2665
2666 i = ti->topo_order.pop ();
2667
2668 /* If this variable is not a representative, skip it. */
2669 if (find (i) != i)
2670 continue;
2671
2672 /* In certain indirect cycle cases, we may merge this
2673 variable to another. */
2674 if (eliminate_indirect_cycles (i) && find (i) != i)
2675 continue;
2676
2677 /* If the node has changed, we need to process the
2678 complex constraints and outgoing edges again. */
2679 if (bitmap_clear_bit (changed, i))
2680 {
2681 unsigned int j;
2682 constraint_t c;
2683 bitmap solution;
2684 vec<constraint_t> complex = graph->complex[i];
2685 varinfo_t vi = get_varinfo (i);
2686 bool solution_empty;
2687
2688 /* Compute the changed set of solution bits. If anything
2689 is in the solution just propagate that. */
2690 if (bitmap_bit_p (vi->solution, anything_id))
2691 {
2692 /* If anything is also in the old solution there is
2693 nothing to do.
2694 ??? But we shouldn't ended up with "changed" set ... */
2695 if (vi->oldsolution
2696 && bitmap_bit_p (vi->oldsolution, anything_id))
2697 continue;
2698 bitmap_copy (pts, get_varinfo (find (anything_id))->solution);
2699 }
2700 else if (vi->oldsolution)
2701 bitmap_and_compl (pts, vi->solution, vi->oldsolution);
2702 else
2703 bitmap_copy (pts, vi->solution);
2704
2705 if (bitmap_empty_p (pts))
2706 continue;
2707
2708 if (vi->oldsolution)
2709 bitmap_ior_into (vi->oldsolution, pts);
2710 else
2711 {
2712 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
2713 bitmap_copy (vi->oldsolution, pts);
2714 }
2715
2716 solution = vi->solution;
2717 solution_empty = bitmap_empty_p (solution);
2718
2719 /* Process the complex constraints */
2720 bitmap expanded_pts = NULL;
2721 FOR_EACH_VEC_ELT (complex, j, c)
2722 {
2723 /* XXX: This is going to unsort the constraints in
2724 some cases, which will occasionally add duplicate
2725 constraints during unification. This does not
2726 affect correctness. */
2727 c->lhs.var = find (c->lhs.var);
2728 c->rhs.var = find (c->rhs.var);
2729
2730 /* The only complex constraint that can change our
2731 solution to non-empty, given an empty solution,
2732 is a constraint where the lhs side is receiving
2733 some set from elsewhere. */
2734 if (!solution_empty || c->lhs.type != DEREF)
2735 do_complex_constraint (graph, c, pts, &expanded_pts);
2736 }
2737 BITMAP_FREE (expanded_pts);
2738
2739 solution_empty = bitmap_empty_p (solution);
2740
2741 if (!solution_empty)
2742 {
2743 bitmap_iterator bi;
2744 unsigned eff_escaped_id = find (escaped_id);
2745
2746 /* Propagate solution to all successors. */
2747 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2748 0, j, bi)
2749 {
2750 bitmap tmp;
2751 bool flag;
2752
2753 unsigned int to = find (j);
2754 tmp = get_varinfo (to)->solution;
2755 flag = false;
2756
2757 /* Don't try to propagate to ourselves. */
2758 if (to == i)
2759 continue;
2760
2761 /* If we propagate from ESCAPED use ESCAPED as
2762 placeholder. */
2763 if (i == eff_escaped_id)
2764 flag = bitmap_set_bit (tmp, escaped_id);
2765 else
2766 flag = bitmap_ior_into (tmp, pts);
2767
2768 if (flag)
2769 bitmap_set_bit (changed, to);
2770 }
2771 }
2772 }
2773 }
2774 free_topo_info (ti);
2775 bitmap_obstack_release (&iteration_obstack);
2776 }
2777
2778 BITMAP_FREE (pts);
2779 BITMAP_FREE (changed);
2780 bitmap_obstack_release (&oldpta_obstack);
2781 }
2782
2783 /* Map from trees to variable infos. */
2784 static struct pointer_map_t *vi_for_tree;
2785
2786
2787 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2788
2789 static void
2790 insert_vi_for_tree (tree t, varinfo_t vi)
2791 {
2792 void **slot = pointer_map_insert (vi_for_tree, t);
2793 gcc_assert (vi);
2794 gcc_assert (*slot == NULL);
2795 *slot = vi;
2796 }
2797
2798 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2799 exist in the map, return NULL, otherwise, return the varinfo we found. */
2800
2801 static varinfo_t
2802 lookup_vi_for_tree (tree t)
2803 {
2804 void **slot = pointer_map_contains (vi_for_tree, t);
2805 if (slot == NULL)
2806 return NULL;
2807
2808 return (varinfo_t) *slot;
2809 }
2810
2811 /* Return a printable name for DECL */
2812
2813 static const char *
2814 alias_get_name (tree decl)
2815 {
2816 const char *res = NULL;
2817 char *temp;
2818 int num_printed = 0;
2819
2820 if (!dump_file)
2821 return "NULL";
2822
2823 if (TREE_CODE (decl) == SSA_NAME)
2824 {
2825 res = get_name (decl);
2826 if (res)
2827 num_printed = asprintf (&temp, "%s_%u", res, SSA_NAME_VERSION (decl));
2828 else
2829 num_printed = asprintf (&temp, "_%u", SSA_NAME_VERSION (decl));
2830 if (num_printed > 0)
2831 {
2832 res = ggc_strdup (temp);
2833 free (temp);
2834 }
2835 }
2836 else if (DECL_P (decl))
2837 {
2838 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2839 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2840 else
2841 {
2842 res = get_name (decl);
2843 if (!res)
2844 {
2845 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2846 if (num_printed > 0)
2847 {
2848 res = ggc_strdup (temp);
2849 free (temp);
2850 }
2851 }
2852 }
2853 }
2854 if (res != NULL)
2855 return res;
2856
2857 return "NULL";
2858 }
2859
2860 /* Find the variable id for tree T in the map.
2861 If T doesn't exist in the map, create an entry for it and return it. */
2862
2863 static varinfo_t
2864 get_vi_for_tree (tree t)
2865 {
2866 void **slot = pointer_map_contains (vi_for_tree, t);
2867 if (slot == NULL)
2868 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2869
2870 return (varinfo_t) *slot;
2871 }
2872
2873 /* Get a scalar constraint expression for a new temporary variable. */
2874
2875 static struct constraint_expr
2876 new_scalar_tmp_constraint_exp (const char *name)
2877 {
2878 struct constraint_expr tmp;
2879 varinfo_t vi;
2880
2881 vi = new_var_info (NULL_TREE, name);
2882 vi->offset = 0;
2883 vi->size = -1;
2884 vi->fullsize = -1;
2885 vi->is_full_var = 1;
2886
2887 tmp.var = vi->id;
2888 tmp.type = SCALAR;
2889 tmp.offset = 0;
2890
2891 return tmp;
2892 }
2893
2894 /* Get a constraint expression vector from an SSA_VAR_P node.
2895 If address_p is true, the result will be taken its address of. */
2896
2897 static void
2898 get_constraint_for_ssa_var (tree t, vec<ce_s> *results, bool address_p)
2899 {
2900 struct constraint_expr cexpr;
2901 varinfo_t vi;
2902
2903 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2904 gcc_assert (TREE_CODE (t) == SSA_NAME || DECL_P (t));
2905
2906 /* For parameters, get at the points-to set for the actual parm
2907 decl. */
2908 if (TREE_CODE (t) == SSA_NAME
2909 && SSA_NAME_IS_DEFAULT_DEF (t)
2910 && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2911 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL))
2912 {
2913 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2914 return;
2915 }
2916
2917 /* For global variables resort to the alias target. */
2918 if (TREE_CODE (t) == VAR_DECL
2919 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
2920 {
2921 varpool_node *node = varpool_get_node (t);
2922 if (node && node->alias && node->analyzed)
2923 {
2924 node = varpool_variable_node (node, NULL);
2925 t = node->decl;
2926 }
2927 }
2928
2929 vi = get_vi_for_tree (t);
2930 cexpr.var = vi->id;
2931 cexpr.type = SCALAR;
2932 cexpr.offset = 0;
2933 /* If we determine the result is "anything", and we know this is readonly,
2934 say it points to readonly memory instead. */
2935 if (cexpr.var == anything_id && TREE_READONLY (t))
2936 {
2937 gcc_unreachable ();
2938 cexpr.type = ADDRESSOF;
2939 cexpr.var = readonly_id;
2940 }
2941
2942 /* If we are not taking the address of the constraint expr, add all
2943 sub-fiels of the variable as well. */
2944 if (!address_p
2945 && !vi->is_full_var)
2946 {
2947 for (; vi; vi = vi_next (vi))
2948 {
2949 cexpr.var = vi->id;
2950 results->safe_push (cexpr);
2951 }
2952 return;
2953 }
2954
2955 results->safe_push (cexpr);
2956 }
2957
2958 /* Process constraint T, performing various simplifications and then
2959 adding it to our list of overall constraints. */
2960
2961 static void
2962 process_constraint (constraint_t t)
2963 {
2964 struct constraint_expr rhs = t->rhs;
2965 struct constraint_expr lhs = t->lhs;
2966
2967 gcc_assert (rhs.var < varmap.length ());
2968 gcc_assert (lhs.var < varmap.length ());
2969
2970 /* If we didn't get any useful constraint from the lhs we get
2971 &ANYTHING as fallback from get_constraint_for. Deal with
2972 it here by turning it into *ANYTHING. */
2973 if (lhs.type == ADDRESSOF
2974 && lhs.var == anything_id)
2975 lhs.type = DEREF;
2976
2977 /* ADDRESSOF on the lhs is invalid. */
2978 gcc_assert (lhs.type != ADDRESSOF);
2979
2980 /* We shouldn't add constraints from things that cannot have pointers.
2981 It's not completely trivial to avoid in the callers, so do it here. */
2982 if (rhs.type != ADDRESSOF
2983 && !get_varinfo (rhs.var)->may_have_pointers)
2984 return;
2985
2986 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2987 if (!get_varinfo (lhs.var)->may_have_pointers)
2988 return;
2989
2990 /* This can happen in our IR with things like n->a = *p */
2991 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
2992 {
2993 /* Split into tmp = *rhs, *lhs = tmp */
2994 struct constraint_expr tmplhs;
2995 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp");
2996 process_constraint (new_constraint (tmplhs, rhs));
2997 process_constraint (new_constraint (lhs, tmplhs));
2998 }
2999 else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
3000 {
3001 /* Split into tmp = &rhs, *lhs = tmp */
3002 struct constraint_expr tmplhs;
3003 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp");
3004 process_constraint (new_constraint (tmplhs, rhs));
3005 process_constraint (new_constraint (lhs, tmplhs));
3006 }
3007 else
3008 {
3009 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
3010 constraints.safe_push (t);
3011 }
3012 }
3013
3014
3015 /* Return the position, in bits, of FIELD_DECL from the beginning of its
3016 structure. */
3017
3018 static HOST_WIDE_INT
3019 bitpos_of_field (const tree fdecl)
3020 {
3021 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl))
3022 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl)))
3023 return -1;
3024
3025 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
3026 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl)));
3027 }
3028
3029
3030 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
3031 resulting constraint expressions in *RESULTS. */
3032
3033 static void
3034 get_constraint_for_ptr_offset (tree ptr, tree offset,
3035 vec<ce_s> *results)
3036 {
3037 struct constraint_expr c;
3038 unsigned int j, n;
3039 HOST_WIDE_INT rhsoffset;
3040
3041 /* If we do not do field-sensitive PTA adding offsets to pointers
3042 does not change the points-to solution. */
3043 if (!use_field_sensitive)
3044 {
3045 get_constraint_for_rhs (ptr, results);
3046 return;
3047 }
3048
3049 /* If the offset is not a non-negative integer constant that fits
3050 in a HOST_WIDE_INT, we have to fall back to a conservative
3051 solution which includes all sub-fields of all pointed-to
3052 variables of ptr. */
3053 if (offset == NULL_TREE
3054 || TREE_CODE (offset) != INTEGER_CST)
3055 rhsoffset = UNKNOWN_OFFSET;
3056 else
3057 {
3058 /* Sign-extend the offset. */
3059 double_int soffset = tree_to_double_int (offset)
3060 .sext (TYPE_PRECISION (TREE_TYPE (offset)));
3061 if (!soffset.fits_shwi ())
3062 rhsoffset = UNKNOWN_OFFSET;
3063 else
3064 {
3065 /* Make sure the bit-offset also fits. */
3066 HOST_WIDE_INT rhsunitoffset = soffset.low;
3067 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
3068 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
3069 rhsoffset = UNKNOWN_OFFSET;
3070 }
3071 }
3072
3073 get_constraint_for_rhs (ptr, results);
3074 if (rhsoffset == 0)
3075 return;
3076
3077 /* As we are eventually appending to the solution do not use
3078 vec::iterate here. */
3079 n = results->length ();
3080 for (j = 0; j < n; j++)
3081 {
3082 varinfo_t curr;
3083 c = (*results)[j];
3084 curr = get_varinfo (c.var);
3085
3086 if (c.type == ADDRESSOF
3087 /* If this varinfo represents a full variable just use it. */
3088 && curr->is_full_var)
3089 c.offset = 0;
3090 else if (c.type == ADDRESSOF
3091 /* If we do not know the offset add all subfields. */
3092 && rhsoffset == UNKNOWN_OFFSET)
3093 {
3094 varinfo_t temp = get_varinfo (curr->head);
3095 do
3096 {
3097 struct constraint_expr c2;
3098 c2.var = temp->id;
3099 c2.type = ADDRESSOF;
3100 c2.offset = 0;
3101 if (c2.var != c.var)
3102 results->safe_push (c2);
3103 temp = vi_next (temp);
3104 }
3105 while (temp);
3106 }
3107 else if (c.type == ADDRESSOF)
3108 {
3109 varinfo_t temp;
3110 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
3111
3112 /* Search the sub-field which overlaps with the
3113 pointed-to offset. If the result is outside of the variable
3114 we have to provide a conservative result, as the variable is
3115 still reachable from the resulting pointer (even though it
3116 technically cannot point to anything). The last and first
3117 sub-fields are such conservative results.
3118 ??? If we always had a sub-field for &object + 1 then
3119 we could represent this in a more precise way. */
3120 if (rhsoffset < 0
3121 && curr->offset < offset)
3122 offset = 0;
3123 temp = first_or_preceding_vi_for_offset (curr, offset);
3124
3125 /* If the found variable is not exactly at the pointed to
3126 result, we have to include the next variable in the
3127 solution as well. Otherwise two increments by offset / 2
3128 do not result in the same or a conservative superset
3129 solution. */
3130 if (temp->offset != offset
3131 && temp->next != 0)
3132 {
3133 struct constraint_expr c2;
3134 c2.var = temp->next;
3135 c2.type = ADDRESSOF;
3136 c2.offset = 0;
3137 results->safe_push (c2);
3138 }
3139 c.var = temp->id;
3140 c.offset = 0;
3141 }
3142 else
3143 c.offset = rhsoffset;
3144
3145 (*results)[j] = c;
3146 }
3147 }
3148
3149
3150 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3151 If address_p is true the result will be taken its address of.
3152 If lhs_p is true then the constraint expression is assumed to be used
3153 as the lhs. */
3154
3155 static void
3156 get_constraint_for_component_ref (tree t, vec<ce_s> *results,
3157 bool address_p, bool lhs_p)
3158 {
3159 tree orig_t = t;
3160 HOST_WIDE_INT bitsize = -1;
3161 HOST_WIDE_INT bitmaxsize = -1;
3162 HOST_WIDE_INT bitpos;
3163 tree forzero;
3164
3165 /* Some people like to do cute things like take the address of
3166 &0->a.b */
3167 forzero = t;
3168 while (handled_component_p (forzero)
3169 || INDIRECT_REF_P (forzero)
3170 || TREE_CODE (forzero) == MEM_REF)
3171 forzero = TREE_OPERAND (forzero, 0);
3172
3173 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
3174 {
3175 struct constraint_expr temp;
3176
3177 temp.offset = 0;
3178 temp.var = integer_id;
3179 temp.type = SCALAR;
3180 results->safe_push (temp);
3181 return;
3182 }
3183
3184 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
3185
3186 /* Pretend to take the address of the base, we'll take care of
3187 adding the required subset of sub-fields below. */
3188 get_constraint_for_1 (t, results, true, lhs_p);
3189 gcc_assert (results->length () == 1);
3190 struct constraint_expr &result = results->last ();
3191
3192 if (result.type == SCALAR
3193 && get_varinfo (result.var)->is_full_var)
3194 /* For single-field vars do not bother about the offset. */
3195 result.offset = 0;
3196 else if (result.type == SCALAR)
3197 {
3198 /* In languages like C, you can access one past the end of an
3199 array. You aren't allowed to dereference it, so we can
3200 ignore this constraint. When we handle pointer subtraction,
3201 we may have to do something cute here. */
3202
3203 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result.var)->fullsize
3204 && bitmaxsize != 0)
3205 {
3206 /* It's also not true that the constraint will actually start at the
3207 right offset, it may start in some padding. We only care about
3208 setting the constraint to the first actual field it touches, so
3209 walk to find it. */
3210 struct constraint_expr cexpr = result;
3211 varinfo_t curr;
3212 results->pop ();
3213 cexpr.offset = 0;
3214 for (curr = get_varinfo (cexpr.var); curr; curr = vi_next (curr))
3215 {
3216 if (ranges_overlap_p (curr->offset, curr->size,
3217 bitpos, bitmaxsize))
3218 {
3219 cexpr.var = curr->id;
3220 results->safe_push (cexpr);
3221 if (address_p)
3222 break;
3223 }
3224 }
3225 /* If we are going to take the address of this field then
3226 to be able to compute reachability correctly add at least
3227 the last field of the variable. */
3228 if (address_p && results->length () == 0)
3229 {
3230 curr = get_varinfo (cexpr.var);
3231 while (curr->next != 0)
3232 curr = vi_next (curr);
3233 cexpr.var = curr->id;
3234 results->safe_push (cexpr);
3235 }
3236 else if (results->length () == 0)
3237 /* Assert that we found *some* field there. The user couldn't be
3238 accessing *only* padding. */
3239 /* Still the user could access one past the end of an array
3240 embedded in a struct resulting in accessing *only* padding. */
3241 /* Or accessing only padding via type-punning to a type
3242 that has a filed just in padding space. */
3243 {
3244 cexpr.type = SCALAR;
3245 cexpr.var = anything_id;
3246 cexpr.offset = 0;
3247 results->safe_push (cexpr);
3248 }
3249 }
3250 else if (bitmaxsize == 0)
3251 {
3252 if (dump_file && (dump_flags & TDF_DETAILS))
3253 fprintf (dump_file, "Access to zero-sized part of variable,"
3254 "ignoring\n");
3255 }
3256 else
3257 if (dump_file && (dump_flags & TDF_DETAILS))
3258 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3259 }
3260 else if (result.type == DEREF)
3261 {
3262 /* If we do not know exactly where the access goes say so. Note
3263 that only for non-structure accesses we know that we access
3264 at most one subfiled of any variable. */
3265 if (bitpos == -1
3266 || bitsize != bitmaxsize
3267 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
3268 || result.offset == UNKNOWN_OFFSET)
3269 result.offset = UNKNOWN_OFFSET;
3270 else
3271 result.offset += bitpos;
3272 }
3273 else if (result.type == ADDRESSOF)
3274 {
3275 /* We can end up here for component references on a
3276 VIEW_CONVERT_EXPR <>(&foobar). */
3277 result.type = SCALAR;
3278 result.var = anything_id;
3279 result.offset = 0;
3280 }
3281 else
3282 gcc_unreachable ();
3283 }
3284
3285
3286 /* Dereference the constraint expression CONS, and return the result.
3287 DEREF (ADDRESSOF) = SCALAR
3288 DEREF (SCALAR) = DEREF
3289 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3290 This is needed so that we can handle dereferencing DEREF constraints. */
3291
3292 static void
3293 do_deref (vec<ce_s> *constraints)
3294 {
3295 struct constraint_expr *c;
3296 unsigned int i = 0;
3297
3298 FOR_EACH_VEC_ELT (*constraints, i, c)
3299 {
3300 if (c->type == SCALAR)
3301 c->type = DEREF;
3302 else if (c->type == ADDRESSOF)
3303 c->type = SCALAR;
3304 else if (c->type == DEREF)
3305 {
3306 struct constraint_expr tmplhs;
3307 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp");
3308 process_constraint (new_constraint (tmplhs, *c));
3309 c->var = tmplhs.var;
3310 }
3311 else
3312 gcc_unreachable ();
3313 }
3314 }
3315
3316 /* Given a tree T, return the constraint expression for taking the
3317 address of it. */
3318
3319 static void
3320 get_constraint_for_address_of (tree t, vec<ce_s> *results)
3321 {
3322 struct constraint_expr *c;
3323 unsigned int i;
3324
3325 get_constraint_for_1 (t, results, true, true);
3326
3327 FOR_EACH_VEC_ELT (*results, i, c)
3328 {
3329 if (c->type == DEREF)
3330 c->type = SCALAR;
3331 else
3332 c->type = ADDRESSOF;
3333 }
3334 }
3335
3336 /* Given a tree T, return the constraint expression for it. */
3337
3338 static void
3339 get_constraint_for_1 (tree t, vec<ce_s> *results, bool address_p,
3340 bool lhs_p)
3341 {
3342 struct constraint_expr temp;
3343
3344 /* x = integer is all glommed to a single variable, which doesn't
3345 point to anything by itself. That is, of course, unless it is an
3346 integer constant being treated as a pointer, in which case, we
3347 will return that this is really the addressof anything. This
3348 happens below, since it will fall into the default case. The only
3349 case we know something about an integer treated like a pointer is
3350 when it is the NULL pointer, and then we just say it points to
3351 NULL.
3352
3353 Do not do that if -fno-delete-null-pointer-checks though, because
3354 in that case *NULL does not fail, so it _should_ alias *anything.
3355 It is not worth adding a new option or renaming the existing one,
3356 since this case is relatively obscure. */
3357 if ((TREE_CODE (t) == INTEGER_CST
3358 && integer_zerop (t))
3359 /* The only valid CONSTRUCTORs in gimple with pointer typed
3360 elements are zero-initializer. But in IPA mode we also
3361 process global initializers, so verify at least. */
3362 || (TREE_CODE (t) == CONSTRUCTOR
3363 && CONSTRUCTOR_NELTS (t) == 0))
3364 {
3365 if (flag_delete_null_pointer_checks)
3366 temp.var = nothing_id;
3367 else
3368 temp.var = nonlocal_id;
3369 temp.type = ADDRESSOF;
3370 temp.offset = 0;
3371 results->safe_push (temp);
3372 return;
3373 }
3374
3375 /* String constants are read-only. */
3376 if (TREE_CODE (t) == STRING_CST)
3377 {
3378 temp.var = readonly_id;
3379 temp.type = SCALAR;
3380 temp.offset = 0;
3381 results->safe_push (temp);
3382 return;
3383 }
3384
3385 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3386 {
3387 case tcc_expression:
3388 {
3389 switch (TREE_CODE (t))
3390 {
3391 case ADDR_EXPR:
3392 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3393 return;
3394 default:;
3395 }
3396 break;
3397 }
3398 case tcc_reference:
3399 {
3400 switch (TREE_CODE (t))
3401 {
3402 case MEM_REF:
3403 {
3404 struct constraint_expr cs;
3405 varinfo_t vi, curr;
3406 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0),
3407 TREE_OPERAND (t, 1), results);
3408 do_deref (results);
3409
3410 /* If we are not taking the address then make sure to process
3411 all subvariables we might access. */
3412 if (address_p)
3413 return;
3414
3415 cs = results->last ();
3416 if (cs.type == DEREF
3417 && type_can_have_subvars (TREE_TYPE (t)))
3418 {
3419 /* For dereferences this means we have to defer it
3420 to solving time. */
3421 results->last ().offset = UNKNOWN_OFFSET;
3422 return;
3423 }
3424 if (cs.type != SCALAR)
3425 return;
3426
3427 vi = get_varinfo (cs.var);
3428 curr = vi_next (vi);
3429 if (!vi->is_full_var
3430 && curr)
3431 {
3432 unsigned HOST_WIDE_INT size;
3433 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t))))
3434 size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t)));
3435 else
3436 size = -1;
3437 for (; curr; curr = vi_next (curr))
3438 {
3439 if (curr->offset - vi->offset < size)
3440 {
3441 cs.var = curr->id;
3442 results->safe_push (cs);
3443 }
3444 else
3445 break;
3446 }
3447 }
3448 return;
3449 }
3450 case ARRAY_REF:
3451 case ARRAY_RANGE_REF:
3452 case COMPONENT_REF:
3453 get_constraint_for_component_ref (t, results, address_p, lhs_p);
3454 return;
3455 case VIEW_CONVERT_EXPR:
3456 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p,
3457 lhs_p);
3458 return;
3459 /* We are missing handling for TARGET_MEM_REF here. */
3460 default:;
3461 }
3462 break;
3463 }
3464 case tcc_exceptional:
3465 {
3466 switch (TREE_CODE (t))
3467 {
3468 case SSA_NAME:
3469 {
3470 get_constraint_for_ssa_var (t, results, address_p);
3471 return;
3472 }
3473 case CONSTRUCTOR:
3474 {
3475 unsigned int i;
3476 tree val;
3477 auto_vec<ce_s> tmp;
3478 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
3479 {
3480 struct constraint_expr *rhsp;
3481 unsigned j;
3482 get_constraint_for_1 (val, &tmp, address_p, lhs_p);
3483 FOR_EACH_VEC_ELT (tmp, j, rhsp)
3484 results->safe_push (*rhsp);
3485 tmp.truncate (0);
3486 }
3487 /* We do not know whether the constructor was complete,
3488 so technically we have to add &NOTHING or &ANYTHING
3489 like we do for an empty constructor as well. */
3490 return;
3491 }
3492 default:;
3493 }
3494 break;
3495 }
3496 case tcc_declaration:
3497 {
3498 get_constraint_for_ssa_var (t, results, address_p);
3499 return;
3500 }
3501 case tcc_constant:
3502 {
3503 /* We cannot refer to automatic variables through constants. */
3504 temp.type = ADDRESSOF;
3505 temp.var = nonlocal_id;
3506 temp.offset = 0;
3507 results->safe_push (temp);
3508 return;
3509 }
3510 default:;
3511 }
3512
3513 /* The default fallback is a constraint from anything. */
3514 temp.type = ADDRESSOF;
3515 temp.var = anything_id;
3516 temp.offset = 0;
3517 results->safe_push (temp);
3518 }
3519
3520 /* Given a gimple tree T, return the constraint expression vector for it. */
3521
3522 static void
3523 get_constraint_for (tree t, vec<ce_s> *results)
3524 {
3525 gcc_assert (results->length () == 0);
3526
3527 get_constraint_for_1 (t, results, false, true);
3528 }
3529
3530 /* Given a gimple tree T, return the constraint expression vector for it
3531 to be used as the rhs of a constraint. */
3532
3533 static void
3534 get_constraint_for_rhs (tree t, vec<ce_s> *results)
3535 {
3536 gcc_assert (results->length () == 0);
3537
3538 get_constraint_for_1 (t, results, false, false);
3539 }
3540
3541
3542 /* Efficiently generates constraints from all entries in *RHSC to all
3543 entries in *LHSC. */
3544
3545 static void
3546 process_all_all_constraints (vec<ce_s> lhsc,
3547 vec<ce_s> rhsc)
3548 {
3549 struct constraint_expr *lhsp, *rhsp;
3550 unsigned i, j;
3551
3552 if (lhsc.length () <= 1 || rhsc.length () <= 1)
3553 {
3554 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3555 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
3556 process_constraint (new_constraint (*lhsp, *rhsp));
3557 }
3558 else
3559 {
3560 struct constraint_expr tmp;
3561 tmp = new_scalar_tmp_constraint_exp ("allalltmp");
3562 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
3563 process_constraint (new_constraint (tmp, *rhsp));
3564 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3565 process_constraint (new_constraint (*lhsp, tmp));
3566 }
3567 }
3568
3569 /* Handle aggregate copies by expanding into copies of the respective
3570 fields of the structures. */
3571
3572 static void
3573 do_structure_copy (tree lhsop, tree rhsop)
3574 {
3575 struct constraint_expr *lhsp, *rhsp;
3576 auto_vec<ce_s> lhsc;
3577 auto_vec<ce_s> rhsc;
3578 unsigned j;
3579
3580 get_constraint_for (lhsop, &lhsc);
3581 get_constraint_for_rhs (rhsop, &rhsc);
3582 lhsp = &lhsc[0];
3583 rhsp = &rhsc[0];
3584 if (lhsp->type == DEREF
3585 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3586 || rhsp->type == DEREF)
3587 {
3588 if (lhsp->type == DEREF)
3589 {
3590 gcc_assert (lhsc.length () == 1);
3591 lhsp->offset = UNKNOWN_OFFSET;
3592 }
3593 if (rhsp->type == DEREF)
3594 {
3595 gcc_assert (rhsc.length () == 1);
3596 rhsp->offset = UNKNOWN_OFFSET;
3597 }
3598 process_all_all_constraints (lhsc, rhsc);
3599 }
3600 else if (lhsp->type == SCALAR
3601 && (rhsp->type == SCALAR
3602 || rhsp->type == ADDRESSOF))
3603 {
3604 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3605 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
3606 unsigned k = 0;
3607 get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize);
3608 get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize);
3609 for (j = 0; lhsc.iterate (j, &lhsp);)
3610 {
3611 varinfo_t lhsv, rhsv;
3612 rhsp = &rhsc[k];
3613 lhsv = get_varinfo (lhsp->var);
3614 rhsv = get_varinfo (rhsp->var);
3615 if (lhsv->may_have_pointers
3616 && (lhsv->is_full_var
3617 || rhsv->is_full_var
3618 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3619 rhsv->offset + lhsoffset, rhsv->size)))
3620 process_constraint (new_constraint (*lhsp, *rhsp));
3621 if (!rhsv->is_full_var
3622 && (lhsv->is_full_var
3623 || (lhsv->offset + rhsoffset + lhsv->size
3624 > rhsv->offset + lhsoffset + rhsv->size)))
3625 {
3626 ++k;
3627 if (k >= rhsc.length ())
3628 break;
3629 }
3630 else
3631 ++j;
3632 }
3633 }
3634 else
3635 gcc_unreachable ();
3636 }
3637
3638 /* Create constraints ID = { rhsc }. */
3639
3640 static void
3641 make_constraints_to (unsigned id, vec<ce_s> rhsc)
3642 {
3643 struct constraint_expr *c;
3644 struct constraint_expr includes;
3645 unsigned int j;
3646
3647 includes.var = id;
3648 includes.offset = 0;
3649 includes.type = SCALAR;
3650
3651 FOR_EACH_VEC_ELT (rhsc, j, c)
3652 process_constraint (new_constraint (includes, *c));
3653 }
3654
3655 /* Create a constraint ID = OP. */
3656
3657 static void
3658 make_constraint_to (unsigned id, tree op)
3659 {
3660 auto_vec<ce_s> rhsc;
3661 get_constraint_for_rhs (op, &rhsc);
3662 make_constraints_to (id, rhsc);
3663 }
3664
3665 /* Create a constraint ID = &FROM. */
3666
3667 static void
3668 make_constraint_from (varinfo_t vi, int from)
3669 {
3670 struct constraint_expr lhs, rhs;
3671
3672 lhs.var = vi->id;
3673 lhs.offset = 0;
3674 lhs.type = SCALAR;
3675
3676 rhs.var = from;
3677 rhs.offset = 0;
3678 rhs.type = ADDRESSOF;
3679 process_constraint (new_constraint (lhs, rhs));
3680 }
3681
3682 /* Create a constraint ID = FROM. */
3683
3684 static void
3685 make_copy_constraint (varinfo_t vi, int from)
3686 {
3687 struct constraint_expr lhs, rhs;
3688
3689 lhs.var = vi->id;
3690 lhs.offset = 0;
3691 lhs.type = SCALAR;
3692
3693 rhs.var = from;
3694 rhs.offset = 0;
3695 rhs.type = SCALAR;
3696 process_constraint (new_constraint (lhs, rhs));
3697 }
3698
3699 /* Make constraints necessary to make OP escape. */
3700
3701 static void
3702 make_escape_constraint (tree op)
3703 {
3704 make_constraint_to (escaped_id, op);
3705 }
3706
3707 /* Add constraints to that the solution of VI is transitively closed. */
3708
3709 static void
3710 make_transitive_closure_constraints (varinfo_t vi)
3711 {
3712 struct constraint_expr lhs, rhs;
3713
3714 /* VAR = *VAR; */
3715 lhs.type = SCALAR;
3716 lhs.var = vi->id;
3717 lhs.offset = 0;
3718 rhs.type = DEREF;
3719 rhs.var = vi->id;
3720 rhs.offset = UNKNOWN_OFFSET;
3721 process_constraint (new_constraint (lhs, rhs));
3722 }
3723
3724 /* Temporary storage for fake var decls. */
3725 struct obstack fake_var_decl_obstack;
3726
3727 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3728
3729 static tree
3730 build_fake_var_decl (tree type)
3731 {
3732 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl);
3733 memset (decl, 0, sizeof (struct tree_var_decl));
3734 TREE_SET_CODE (decl, VAR_DECL);
3735 TREE_TYPE (decl) = type;
3736 DECL_UID (decl) = allocate_decl_uid ();
3737 SET_DECL_PT_UID (decl, -1);
3738 layout_decl (decl, 0);
3739 return decl;
3740 }
3741
3742 /* Create a new artificial heap variable with NAME.
3743 Return the created variable. */
3744
3745 static varinfo_t
3746 make_heapvar (const char *name)
3747 {
3748 varinfo_t vi;
3749 tree heapvar;
3750
3751 heapvar = build_fake_var_decl (ptr_type_node);
3752 DECL_EXTERNAL (heapvar) = 1;
3753
3754 vi = new_var_info (heapvar, name);
3755 vi->is_artificial_var = true;
3756 vi->is_heap_var = true;
3757 vi->is_unknown_size_var = true;
3758 vi->offset = 0;
3759 vi->fullsize = ~0;
3760 vi->size = ~0;
3761 vi->is_full_var = true;
3762 insert_vi_for_tree (heapvar, vi);
3763
3764 return vi;
3765 }
3766
3767 /* Create a new artificial heap variable with NAME and make a
3768 constraint from it to LHS. Set flags according to a tag used
3769 for tracking restrict pointers. */
3770
3771 static varinfo_t
3772 make_constraint_from_restrict (varinfo_t lhs, const char *name)
3773 {
3774 varinfo_t vi = make_heapvar (name);
3775 vi->is_global_var = 1;
3776 vi->may_have_pointers = 1;
3777 make_constraint_from (lhs, vi->id);
3778 return vi;
3779 }
3780
3781 /* Create a new artificial heap variable with NAME and make a
3782 constraint from it to LHS. Set flags according to a tag used
3783 for tracking restrict pointers and make the artificial heap
3784 point to global memory. */
3785
3786 static varinfo_t
3787 make_constraint_from_global_restrict (varinfo_t lhs, const char *name)
3788 {
3789 varinfo_t vi = make_constraint_from_restrict (lhs, name);
3790 make_copy_constraint (vi, nonlocal_id);
3791 return vi;
3792 }
3793
3794 /* In IPA mode there are varinfos for different aspects of reach
3795 function designator. One for the points-to set of the return
3796 value, one for the variables that are clobbered by the function,
3797 one for its uses and one for each parameter (including a single
3798 glob for remaining variadic arguments). */
3799
3800 enum { fi_clobbers = 1, fi_uses = 2,
3801 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 };
3802
3803 /* Get a constraint for the requested part of a function designator FI
3804 when operating in IPA mode. */
3805
3806 static struct constraint_expr
3807 get_function_part_constraint (varinfo_t fi, unsigned part)
3808 {
3809 struct constraint_expr c;
3810
3811 gcc_assert (in_ipa_mode);
3812
3813 if (fi->id == anything_id)
3814 {
3815 /* ??? We probably should have a ANYFN special variable. */
3816 c.var = anything_id;
3817 c.offset = 0;
3818 c.type = SCALAR;
3819 }
3820 else if (TREE_CODE (fi->decl) == FUNCTION_DECL)
3821 {
3822 varinfo_t ai = first_vi_for_offset (fi, part);
3823 if (ai)
3824 c.var = ai->id;
3825 else
3826 c.var = anything_id;
3827 c.offset = 0;
3828 c.type = SCALAR;
3829 }
3830 else
3831 {
3832 c.var = fi->id;
3833 c.offset = part;
3834 c.type = DEREF;
3835 }
3836
3837 return c;
3838 }
3839
3840 /* For non-IPA mode, generate constraints necessary for a call on the
3841 RHS. */
3842
3843 static void
3844 handle_rhs_call (gimple stmt, vec<ce_s> *results)
3845 {
3846 struct constraint_expr rhsc;
3847 unsigned i;
3848 bool returns_uses = false;
3849
3850 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3851 {
3852 tree arg = gimple_call_arg (stmt, i);
3853 int flags = gimple_call_arg_flags (stmt, i);
3854
3855 /* If the argument is not used we can ignore it. */
3856 if (flags & EAF_UNUSED)
3857 continue;
3858
3859 /* As we compute ESCAPED context-insensitive we do not gain
3860 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3861 set. The argument would still get clobbered through the
3862 escape solution. */
3863 if ((flags & EAF_NOCLOBBER)
3864 && (flags & EAF_NOESCAPE))
3865 {
3866 varinfo_t uses = get_call_use_vi (stmt);
3867 if (!(flags & EAF_DIRECT))
3868 {
3869 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3870 make_constraint_to (tem->id, arg);
3871 make_transitive_closure_constraints (tem);
3872 make_copy_constraint (uses, tem->id);
3873 }
3874 else
3875 make_constraint_to (uses->id, arg);
3876 returns_uses = true;
3877 }
3878 else if (flags & EAF_NOESCAPE)
3879 {
3880 struct constraint_expr lhs, rhs;
3881 varinfo_t uses = get_call_use_vi (stmt);
3882 varinfo_t clobbers = get_call_clobber_vi (stmt);
3883 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3884 make_constraint_to (tem->id, arg);
3885 if (!(flags & EAF_DIRECT))
3886 make_transitive_closure_constraints (tem);
3887 make_copy_constraint (uses, tem->id);
3888 make_copy_constraint (clobbers, tem->id);
3889 /* Add *tem = nonlocal, do not add *tem = callused as
3890 EAF_NOESCAPE parameters do not escape to other parameters
3891 and all other uses appear in NONLOCAL as well. */
3892 lhs.type = DEREF;
3893 lhs.var = tem->id;
3894 lhs.offset = 0;
3895 rhs.type = SCALAR;
3896 rhs.var = nonlocal_id;
3897 rhs.offset = 0;
3898 process_constraint (new_constraint (lhs, rhs));
3899 returns_uses = true;
3900 }
3901 else
3902 make_escape_constraint (arg);
3903 }
3904
3905 /* If we added to the calls uses solution make sure we account for
3906 pointers to it to be returned. */
3907 if (returns_uses)
3908 {
3909 rhsc.var = get_call_use_vi (stmt)->id;
3910 rhsc.offset = 0;
3911 rhsc.type = SCALAR;
3912 results->safe_push (rhsc);
3913 }
3914
3915 /* The static chain escapes as well. */
3916 if (gimple_call_chain (stmt))
3917 make_escape_constraint (gimple_call_chain (stmt));
3918
3919 /* And if we applied NRV the address of the return slot escapes as well. */
3920 if (gimple_call_return_slot_opt_p (stmt)
3921 && gimple_call_lhs (stmt) != NULL_TREE
3922 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3923 {
3924 auto_vec<ce_s> tmpc;
3925 struct constraint_expr lhsc, *c;
3926 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3927 lhsc.var = escaped_id;
3928 lhsc.offset = 0;
3929 lhsc.type = SCALAR;
3930 FOR_EACH_VEC_ELT (tmpc, i, c)
3931 process_constraint (new_constraint (lhsc, *c));
3932 }
3933
3934 /* Regular functions return nonlocal memory. */
3935 rhsc.var = nonlocal_id;
3936 rhsc.offset = 0;
3937 rhsc.type = SCALAR;
3938 results->safe_push (rhsc);
3939 }
3940
3941 /* For non-IPA mode, generate constraints necessary for a call
3942 that returns a pointer and assigns it to LHS. This simply makes
3943 the LHS point to global and escaped variables. */
3944
3945 static void
3946 handle_lhs_call (gimple stmt, tree lhs, int flags, vec<ce_s> rhsc,
3947 tree fndecl)
3948 {
3949 auto_vec<ce_s> lhsc;
3950
3951 get_constraint_for (lhs, &lhsc);
3952 /* If the store is to a global decl make sure to
3953 add proper escape constraints. */
3954 lhs = get_base_address (lhs);
3955 if (lhs
3956 && DECL_P (lhs)
3957 && is_global_var (lhs))
3958 {
3959 struct constraint_expr tmpc;
3960 tmpc.var = escaped_id;
3961 tmpc.offset = 0;
3962 tmpc.type = SCALAR;
3963 lhsc.safe_push (tmpc);
3964 }
3965
3966 /* If the call returns an argument unmodified override the rhs
3967 constraints. */
3968 flags = gimple_call_return_flags (stmt);
3969 if (flags & ERF_RETURNS_ARG
3970 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
3971 {
3972 tree arg;
3973 rhsc.create (0);
3974 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
3975 get_constraint_for (arg, &rhsc);
3976 process_all_all_constraints (lhsc, rhsc);
3977 rhsc.release ();
3978 }
3979 else if (flags & ERF_NOALIAS)
3980 {
3981 varinfo_t vi;
3982 struct constraint_expr tmpc;
3983 rhsc.create (0);
3984 vi = make_heapvar ("HEAP");
3985 /* We marking allocated storage local, we deal with it becoming
3986 global by escaping and setting of vars_contains_escaped_heap. */
3987 DECL_EXTERNAL (vi->decl) = 0;
3988 vi->is_global_var = 0;
3989 /* If this is not a real malloc call assume the memory was
3990 initialized and thus may point to global memory. All
3991 builtin functions with the malloc attribute behave in a sane way. */
3992 if (!fndecl
3993 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3994 make_constraint_from (vi, nonlocal_id);
3995 tmpc.var = vi->id;
3996 tmpc.offset = 0;
3997 tmpc.type = ADDRESSOF;
3998 rhsc.safe_push (tmpc);
3999 process_all_all_constraints (lhsc, rhsc);
4000 rhsc.release ();
4001 }
4002 else
4003 process_all_all_constraints (lhsc, rhsc);
4004 }
4005
4006 /* For non-IPA mode, generate constraints necessary for a call of a
4007 const function that returns a pointer in the statement STMT. */
4008
4009 static void
4010 handle_const_call (gimple stmt, vec<ce_s> *results)
4011 {
4012 struct constraint_expr rhsc;
4013 unsigned int k;
4014
4015 /* Treat nested const functions the same as pure functions as far
4016 as the static chain is concerned. */
4017 if (gimple_call_chain (stmt))
4018 {
4019 varinfo_t uses = get_call_use_vi (stmt);
4020 make_transitive_closure_constraints (uses);
4021 make_constraint_to (uses->id, gimple_call_chain (stmt));
4022 rhsc.var = uses->id;
4023 rhsc.offset = 0;
4024 rhsc.type = SCALAR;
4025 results->safe_push (rhsc);
4026 }
4027
4028 /* May return arguments. */
4029 for (k = 0; k < gimple_call_num_args (stmt); ++k)
4030 {
4031 tree arg = gimple_call_arg (stmt, k);
4032 auto_vec<ce_s> argc;
4033 unsigned i;
4034 struct constraint_expr *argp;
4035 get_constraint_for_rhs (arg, &argc);
4036 FOR_EACH_VEC_ELT (argc, i, argp)
4037 results->safe_push (*argp);
4038 }
4039
4040 /* May return addresses of globals. */
4041 rhsc.var = nonlocal_id;
4042 rhsc.offset = 0;
4043 rhsc.type = ADDRESSOF;
4044 results->safe_push (rhsc);
4045 }
4046
4047 /* For non-IPA mode, generate constraints necessary for a call to a
4048 pure function in statement STMT. */
4049
4050 static void
4051 handle_pure_call (gimple stmt, vec<ce_s> *results)
4052 {
4053 struct constraint_expr rhsc;
4054 unsigned i;
4055 varinfo_t uses = NULL;
4056
4057 /* Memory reached from pointer arguments is call-used. */
4058 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4059 {
4060 tree arg = gimple_call_arg (stmt, i);
4061 if (!uses)
4062 {
4063 uses = get_call_use_vi (stmt);
4064 make_transitive_closure_constraints (uses);
4065 }
4066 make_constraint_to (uses->id, arg);
4067 }
4068
4069 /* The static chain is used as well. */
4070 if (gimple_call_chain (stmt))
4071 {
4072 if (!uses)
4073 {
4074 uses = get_call_use_vi (stmt);
4075 make_transitive_closure_constraints (uses);
4076 }
4077 make_constraint_to (uses->id, gimple_call_chain (stmt));
4078 }
4079
4080 /* Pure functions may return call-used and nonlocal memory. */
4081 if (uses)
4082 {
4083 rhsc.var = uses->id;
4084 rhsc.offset = 0;
4085 rhsc.type = SCALAR;
4086 results->safe_push (rhsc);
4087 }
4088 rhsc.var = nonlocal_id;
4089 rhsc.offset = 0;
4090 rhsc.type = SCALAR;
4091 results->safe_push (rhsc);
4092 }
4093
4094
4095 /* Return the varinfo for the callee of CALL. */
4096
4097 static varinfo_t
4098 get_fi_for_callee (gimple call)
4099 {
4100 tree decl, fn = gimple_call_fn (call);
4101
4102 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
4103 fn = OBJ_TYPE_REF_EXPR (fn);
4104
4105 /* If we can directly resolve the function being called, do so.
4106 Otherwise, it must be some sort of indirect expression that
4107 we should still be able to handle. */
4108 decl = gimple_call_addr_fndecl (fn);
4109 if (decl)
4110 return get_vi_for_tree (decl);
4111
4112 /* If the function is anything other than a SSA name pointer we have no
4113 clue and should be getting ANYFN (well, ANYTHING for now). */
4114 if (!fn || TREE_CODE (fn) != SSA_NAME)
4115 return get_varinfo (anything_id);
4116
4117 if (SSA_NAME_IS_DEFAULT_DEF (fn)
4118 && (TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
4119 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL))
4120 fn = SSA_NAME_VAR (fn);
4121
4122 return get_vi_for_tree (fn);
4123 }
4124
4125 /* Create constraints for the builtin call T. Return true if the call
4126 was handled, otherwise false. */
4127
4128 static bool
4129 find_func_aliases_for_builtin_call (gimple t)
4130 {
4131 tree fndecl = gimple_call_fndecl (t);
4132 vec<ce_s> lhsc = vNULL;
4133 vec<ce_s> rhsc = vNULL;
4134 varinfo_t fi;
4135
4136 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4137 /* ??? All builtins that are handled here need to be handled
4138 in the alias-oracle query functions explicitly! */
4139 switch (DECL_FUNCTION_CODE (fndecl))
4140 {
4141 /* All the following functions return a pointer to the same object
4142 as their first argument points to. The functions do not add
4143 to the ESCAPED solution. The functions make the first argument
4144 pointed to memory point to what the second argument pointed to
4145 memory points to. */
4146 case BUILT_IN_STRCPY:
4147 case BUILT_IN_STRNCPY:
4148 case BUILT_IN_BCOPY:
4149 case BUILT_IN_MEMCPY:
4150 case BUILT_IN_MEMMOVE:
4151 case BUILT_IN_MEMPCPY:
4152 case BUILT_IN_STPCPY:
4153 case BUILT_IN_STPNCPY:
4154 case BUILT_IN_STRCAT:
4155 case BUILT_IN_STRNCAT:
4156 case BUILT_IN_STRCPY_CHK:
4157 case BUILT_IN_STRNCPY_CHK:
4158 case BUILT_IN_MEMCPY_CHK:
4159 case BUILT_IN_MEMMOVE_CHK:
4160 case BUILT_IN_MEMPCPY_CHK:
4161 case BUILT_IN_STPCPY_CHK:
4162 case BUILT_IN_STPNCPY_CHK:
4163 case BUILT_IN_STRCAT_CHK:
4164 case BUILT_IN_STRNCAT_CHK:
4165 case BUILT_IN_TM_MEMCPY:
4166 case BUILT_IN_TM_MEMMOVE:
4167 {
4168 tree res = gimple_call_lhs (t);
4169 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4170 == BUILT_IN_BCOPY ? 1 : 0));
4171 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4172 == BUILT_IN_BCOPY ? 0 : 1));
4173 if (res != NULL_TREE)
4174 {
4175 get_constraint_for (res, &lhsc);
4176 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4177 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
4178 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4179 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
4180 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK
4181 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK)
4182 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4183 else
4184 get_constraint_for (dest, &rhsc);
4185 process_all_all_constraints (lhsc, rhsc);
4186 lhsc.release ();
4187 rhsc.release ();
4188 }
4189 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4190 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4191 do_deref (&lhsc);
4192 do_deref (&rhsc);
4193 process_all_all_constraints (lhsc, rhsc);
4194 lhsc.release ();
4195 rhsc.release ();
4196 return true;
4197 }
4198 case BUILT_IN_MEMSET:
4199 case BUILT_IN_MEMSET_CHK:
4200 case BUILT_IN_TM_MEMSET:
4201 {
4202 tree res = gimple_call_lhs (t);
4203 tree dest = gimple_call_arg (t, 0);
4204 unsigned i;
4205 ce_s *lhsp;
4206 struct constraint_expr ac;
4207 if (res != NULL_TREE)
4208 {
4209 get_constraint_for (res, &lhsc);
4210 get_constraint_for (dest, &rhsc);
4211 process_all_all_constraints (lhsc, rhsc);
4212 lhsc.release ();
4213 rhsc.release ();
4214 }
4215 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4216 do_deref (&lhsc);
4217 if (flag_delete_null_pointer_checks
4218 && integer_zerop (gimple_call_arg (t, 1)))
4219 {
4220 ac.type = ADDRESSOF;
4221 ac.var = nothing_id;
4222 }
4223 else
4224 {
4225 ac.type = SCALAR;
4226 ac.var = integer_id;
4227 }
4228 ac.offset = 0;
4229 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4230 process_constraint (new_constraint (*lhsp, ac));
4231 lhsc.release ();
4232 return true;
4233 }
4234 case BUILT_IN_ASSUME_ALIGNED:
4235 {
4236 tree res = gimple_call_lhs (t);
4237 tree dest = gimple_call_arg (t, 0);
4238 if (res != NULL_TREE)
4239 {
4240 get_constraint_for (res, &lhsc);
4241 get_constraint_for (dest, &rhsc);
4242 process_all_all_constraints (lhsc, rhsc);
4243 lhsc.release ();
4244 rhsc.release ();
4245 }
4246 return true;
4247 }
4248 /* All the following functions do not return pointers, do not
4249 modify the points-to sets of memory reachable from their
4250 arguments and do not add to the ESCAPED solution. */
4251 case BUILT_IN_SINCOS:
4252 case BUILT_IN_SINCOSF:
4253 case BUILT_IN_SINCOSL:
4254 case BUILT_IN_FREXP:
4255 case BUILT_IN_FREXPF:
4256 case BUILT_IN_FREXPL:
4257 case BUILT_IN_GAMMA_R:
4258 case BUILT_IN_GAMMAF_R:
4259 case BUILT_IN_GAMMAL_R:
4260 case BUILT_IN_LGAMMA_R:
4261 case BUILT_IN_LGAMMAF_R:
4262 case BUILT_IN_LGAMMAL_R:
4263 case BUILT_IN_MODF:
4264 case BUILT_IN_MODFF:
4265 case BUILT_IN_MODFL:
4266 case BUILT_IN_REMQUO:
4267 case BUILT_IN_REMQUOF:
4268 case BUILT_IN_REMQUOL:
4269 case BUILT_IN_FREE:
4270 return true;
4271 case BUILT_IN_STRDUP:
4272 case BUILT_IN_STRNDUP:
4273 if (gimple_call_lhs (t))
4274 {
4275 handle_lhs_call (t, gimple_call_lhs (t), gimple_call_flags (t),
4276 vNULL, fndecl);
4277 get_constraint_for_ptr_offset (gimple_call_lhs (t),
4278 NULL_TREE, &lhsc);
4279 get_constraint_for_ptr_offset (gimple_call_arg (t, 0),
4280 NULL_TREE, &rhsc);
4281 do_deref (&lhsc);
4282 do_deref (&rhsc);
4283 process_all_all_constraints (lhsc, rhsc);
4284 lhsc.release ();
4285 rhsc.release ();
4286 return true;
4287 }
4288 break;
4289 /* String / character search functions return a pointer into the
4290 source string or NULL. */
4291 case BUILT_IN_INDEX:
4292 case BUILT_IN_STRCHR:
4293 case BUILT_IN_STRRCHR:
4294 case BUILT_IN_MEMCHR:
4295 case BUILT_IN_STRSTR:
4296 case BUILT_IN_STRPBRK:
4297 if (gimple_call_lhs (t))
4298 {
4299 tree src = gimple_call_arg (t, 0);
4300 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4301 constraint_expr nul;
4302 nul.var = nothing_id;
4303 nul.offset = 0;
4304 nul.type = ADDRESSOF;
4305 rhsc.safe_push (nul);
4306 get_constraint_for (gimple_call_lhs (t), &lhsc);
4307 process_all_all_constraints (lhsc, rhsc);
4308 lhsc.release ();
4309 rhsc.release ();
4310 }
4311 return true;
4312 /* Trampolines are special - they set up passing the static
4313 frame. */
4314 case BUILT_IN_INIT_TRAMPOLINE:
4315 {
4316 tree tramp = gimple_call_arg (t, 0);
4317 tree nfunc = gimple_call_arg (t, 1);
4318 tree frame = gimple_call_arg (t, 2);
4319 unsigned i;
4320 struct constraint_expr lhs, *rhsp;
4321 if (in_ipa_mode)
4322 {
4323 varinfo_t nfi = NULL;
4324 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR);
4325 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0));
4326 if (nfi)
4327 {
4328 lhs = get_function_part_constraint (nfi, fi_static_chain);
4329 get_constraint_for (frame, &rhsc);
4330 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4331 process_constraint (new_constraint (lhs, *rhsp));
4332 rhsc.release ();
4333
4334 /* Make the frame point to the function for
4335 the trampoline adjustment call. */
4336 get_constraint_for (tramp, &lhsc);
4337 do_deref (&lhsc);
4338 get_constraint_for (nfunc, &rhsc);
4339 process_all_all_constraints (lhsc, rhsc);
4340 rhsc.release ();
4341 lhsc.release ();
4342
4343 return true;
4344 }
4345 }
4346 /* Else fallthru to generic handling which will let
4347 the frame escape. */
4348 break;
4349 }
4350 case BUILT_IN_ADJUST_TRAMPOLINE:
4351 {
4352 tree tramp = gimple_call_arg (t, 0);
4353 tree res = gimple_call_lhs (t);
4354 if (in_ipa_mode && res)
4355 {
4356 get_constraint_for (res, &lhsc);
4357 get_constraint_for (tramp, &rhsc);
4358 do_deref (&rhsc);
4359 process_all_all_constraints (lhsc, rhsc);
4360 rhsc.release ();
4361 lhsc.release ();
4362 }
4363 return true;
4364 }
4365 CASE_BUILT_IN_TM_STORE (1):
4366 CASE_BUILT_IN_TM_STORE (2):
4367 CASE_BUILT_IN_TM_STORE (4):
4368 CASE_BUILT_IN_TM_STORE (8):
4369 CASE_BUILT_IN_TM_STORE (FLOAT):
4370 CASE_BUILT_IN_TM_STORE (DOUBLE):
4371 CASE_BUILT_IN_TM_STORE (LDOUBLE):
4372 CASE_BUILT_IN_TM_STORE (M64):
4373 CASE_BUILT_IN_TM_STORE (M128):
4374 CASE_BUILT_IN_TM_STORE (M256):
4375 {
4376 tree addr = gimple_call_arg (t, 0);
4377 tree src = gimple_call_arg (t, 1);
4378
4379 get_constraint_for (addr, &lhsc);
4380 do_deref (&lhsc);
4381 get_constraint_for (src, &rhsc);
4382 process_all_all_constraints (lhsc, rhsc);
4383 lhsc.release ();
4384 rhsc.release ();
4385 return true;
4386 }
4387 CASE_BUILT_IN_TM_LOAD (1):
4388 CASE_BUILT_IN_TM_LOAD (2):
4389 CASE_BUILT_IN_TM_LOAD (4):
4390 CASE_BUILT_IN_TM_LOAD (8):
4391 CASE_BUILT_IN_TM_LOAD (FLOAT):
4392 CASE_BUILT_IN_TM_LOAD (DOUBLE):
4393 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
4394 CASE_BUILT_IN_TM_LOAD (M64):
4395 CASE_BUILT_IN_TM_LOAD (M128):
4396 CASE_BUILT_IN_TM_LOAD (M256):
4397 {
4398 tree dest = gimple_call_lhs (t);
4399 tree addr = gimple_call_arg (t, 0);
4400
4401 get_constraint_for (dest, &lhsc);
4402 get_constraint_for (addr, &rhsc);
4403 do_deref (&rhsc);
4404 process_all_all_constraints (lhsc, rhsc);
4405 lhsc.release ();
4406 rhsc.release ();
4407 return true;
4408 }
4409 /* Variadic argument handling needs to be handled in IPA
4410 mode as well. */
4411 case BUILT_IN_VA_START:
4412 {
4413 tree valist = gimple_call_arg (t, 0);
4414 struct constraint_expr rhs, *lhsp;
4415 unsigned i;
4416 get_constraint_for (valist, &lhsc);
4417 do_deref (&lhsc);
4418 /* The va_list gets access to pointers in variadic
4419 arguments. Which we know in the case of IPA analysis
4420 and otherwise are just all nonlocal variables. */
4421 if (in_ipa_mode)
4422 {
4423 fi = lookup_vi_for_tree (cfun->decl);
4424 rhs = get_function_part_constraint (fi, ~0);
4425 rhs.type = ADDRESSOF;
4426 }
4427 else
4428 {
4429 rhs.var = nonlocal_id;
4430 rhs.type = ADDRESSOF;
4431 rhs.offset = 0;
4432 }
4433 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4434 process_constraint (new_constraint (*lhsp, rhs));
4435 lhsc.release ();
4436 /* va_list is clobbered. */
4437 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4438 return true;
4439 }
4440 /* va_end doesn't have any effect that matters. */
4441 case BUILT_IN_VA_END:
4442 return true;
4443 /* Alternate return. Simply give up for now. */
4444 case BUILT_IN_RETURN:
4445 {
4446 fi = NULL;
4447 if (!in_ipa_mode
4448 || !(fi = get_vi_for_tree (cfun->decl)))
4449 make_constraint_from (get_varinfo (escaped_id), anything_id);
4450 else if (in_ipa_mode
4451 && fi != NULL)
4452 {
4453 struct constraint_expr lhs, rhs;
4454 lhs = get_function_part_constraint (fi, fi_result);
4455 rhs.var = anything_id;
4456 rhs.offset = 0;
4457 rhs.type = SCALAR;
4458 process_constraint (new_constraint (lhs, rhs));
4459 }
4460 return true;
4461 }
4462 /* printf-style functions may have hooks to set pointers to
4463 point to somewhere into the generated string. Leave them
4464 for a later exercise... */
4465 default:
4466 /* Fallthru to general call handling. */;
4467 }
4468
4469 return false;
4470 }
4471
4472 /* Create constraints for the call T. */
4473
4474 static void
4475 find_func_aliases_for_call (gimple t)
4476 {
4477 tree fndecl = gimple_call_fndecl (t);
4478 vec<ce_s> lhsc = vNULL;
4479 vec<ce_s> rhsc = vNULL;
4480 varinfo_t fi;
4481
4482 if (fndecl != NULL_TREE
4483 && DECL_BUILT_IN (fndecl)
4484 && find_func_aliases_for_builtin_call (t))
4485 return;
4486
4487 fi = get_fi_for_callee (t);
4488 if (!in_ipa_mode
4489 || (fndecl && !fi->is_fn_info))
4490 {
4491 vec<ce_s> rhsc = vNULL;
4492 int flags = gimple_call_flags (t);
4493
4494 /* Const functions can return their arguments and addresses
4495 of global memory but not of escaped memory. */
4496 if (flags & (ECF_CONST|ECF_NOVOPS))
4497 {
4498 if (gimple_call_lhs (t))
4499 handle_const_call (t, &rhsc);
4500 }
4501 /* Pure functions can return addresses in and of memory
4502 reachable from their arguments, but they are not an escape
4503 point for reachable memory of their arguments. */
4504 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4505 handle_pure_call (t, &rhsc);
4506 else
4507 handle_rhs_call (t, &rhsc);
4508 if (gimple_call_lhs (t))
4509 handle_lhs_call (t, gimple_call_lhs (t), flags, rhsc, fndecl);
4510 rhsc.release ();
4511 }
4512 else
4513 {
4514 tree lhsop;
4515 unsigned j;
4516
4517 /* Assign all the passed arguments to the appropriate incoming
4518 parameters of the function. */
4519 for (j = 0; j < gimple_call_num_args (t); j++)
4520 {
4521 struct constraint_expr lhs ;
4522 struct constraint_expr *rhsp;
4523 tree arg = gimple_call_arg (t, j);
4524
4525 get_constraint_for_rhs (arg, &rhsc);
4526 lhs = get_function_part_constraint (fi, fi_parm_base + j);
4527 while (rhsc.length () != 0)
4528 {
4529 rhsp = &rhsc.last ();
4530 process_constraint (new_constraint (lhs, *rhsp));
4531 rhsc.pop ();
4532 }
4533 }
4534
4535 /* If we are returning a value, assign it to the result. */
4536 lhsop = gimple_call_lhs (t);
4537 if (lhsop)
4538 {
4539 struct constraint_expr rhs;
4540 struct constraint_expr *lhsp;
4541
4542 get_constraint_for (lhsop, &lhsc);
4543 rhs = get_function_part_constraint (fi, fi_result);
4544 if (fndecl
4545 && DECL_RESULT (fndecl)
4546 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4547 {
4548 vec<ce_s> tem = vNULL;
4549 tem.safe_push (rhs);
4550 do_deref (&tem);
4551 rhs = tem[0];
4552 tem.release ();
4553 }
4554 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4555 process_constraint (new_constraint (*lhsp, rhs));
4556 }
4557
4558 /* If we pass the result decl by reference, honor that. */
4559 if (lhsop
4560 && fndecl
4561 && DECL_RESULT (fndecl)
4562 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4563 {
4564 struct constraint_expr lhs;
4565 struct constraint_expr *rhsp;
4566
4567 get_constraint_for_address_of (lhsop, &rhsc);
4568 lhs = get_function_part_constraint (fi, fi_result);
4569 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4570 process_constraint (new_constraint (lhs, *rhsp));
4571 rhsc.release ();
4572 }
4573
4574 /* If we use a static chain, pass it along. */
4575 if (gimple_call_chain (t))
4576 {
4577 struct constraint_expr lhs;
4578 struct constraint_expr *rhsp;
4579
4580 get_constraint_for (gimple_call_chain (t), &rhsc);
4581 lhs = get_function_part_constraint (fi, fi_static_chain);
4582 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4583 process_constraint (new_constraint (lhs, *rhsp));
4584 }
4585 }
4586 }
4587
4588 /* Walk statement T setting up aliasing constraints according to the
4589 references found in T. This function is the main part of the
4590 constraint builder. AI points to auxiliary alias information used
4591 when building alias sets and computing alias grouping heuristics. */
4592
4593 static void
4594 find_func_aliases (gimple origt)
4595 {
4596 gimple t = origt;
4597 vec<ce_s> lhsc = vNULL;
4598 vec<ce_s> rhsc = vNULL;
4599 struct constraint_expr *c;
4600 varinfo_t fi;
4601
4602 /* Now build constraints expressions. */
4603 if (gimple_code (t) == GIMPLE_PHI)
4604 {
4605 size_t i;
4606 unsigned int j;
4607
4608 /* For a phi node, assign all the arguments to
4609 the result. */
4610 get_constraint_for (gimple_phi_result (t), &lhsc);
4611 for (i = 0; i < gimple_phi_num_args (t); i++)
4612 {
4613 tree strippedrhs = PHI_ARG_DEF (t, i);
4614
4615 STRIP_NOPS (strippedrhs);
4616 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
4617
4618 FOR_EACH_VEC_ELT (lhsc, j, c)
4619 {
4620 struct constraint_expr *c2;
4621 while (rhsc.length () > 0)
4622 {
4623 c2 = &rhsc.last ();
4624 process_constraint (new_constraint (*c, *c2));
4625 rhsc.pop ();
4626 }
4627 }
4628 }
4629 }
4630 /* In IPA mode, we need to generate constraints to pass call
4631 arguments through their calls. There are two cases,
4632 either a GIMPLE_CALL returning a value, or just a plain
4633 GIMPLE_CALL when we are not.
4634
4635 In non-ipa mode, we need to generate constraints for each
4636 pointer passed by address. */
4637 else if (is_gimple_call (t))
4638 find_func_aliases_for_call (t);
4639
4640 /* Otherwise, just a regular assignment statement. Only care about
4641 operations with pointer result, others are dealt with as escape
4642 points if they have pointer operands. */
4643 else if (is_gimple_assign (t))
4644 {
4645 /* Otherwise, just a regular assignment statement. */
4646 tree lhsop = gimple_assign_lhs (t);
4647 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
4648
4649 if (rhsop && TREE_CLOBBER_P (rhsop))
4650 /* Ignore clobbers, they don't actually store anything into
4651 the LHS. */
4652 ;
4653 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
4654 do_structure_copy (lhsop, rhsop);
4655 else
4656 {
4657 enum tree_code code = gimple_assign_rhs_code (t);
4658
4659 get_constraint_for (lhsop, &lhsc);
4660
4661 if (FLOAT_TYPE_P (TREE_TYPE (lhsop)))
4662 /* If the operation produces a floating point result then
4663 assume the value is not produced to transfer a pointer. */
4664 ;
4665 else if (code == POINTER_PLUS_EXPR)
4666 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4667 gimple_assign_rhs2 (t), &rhsc);
4668 else if (code == BIT_AND_EXPR
4669 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
4670 {
4671 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4672 the pointer. Handle it by offsetting it by UNKNOWN. */
4673 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4674 NULL_TREE, &rhsc);
4675 }
4676 else if ((CONVERT_EXPR_CODE_P (code)
4677 && !(POINTER_TYPE_P (gimple_expr_type (t))
4678 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
4679 || gimple_assign_single_p (t))
4680 get_constraint_for_rhs (rhsop, &rhsc);
4681 else if (code == COND_EXPR)
4682 {
4683 /* The result is a merge of both COND_EXPR arms. */
4684 vec<ce_s> tmp = vNULL;
4685 struct constraint_expr *rhsp;
4686 unsigned i;
4687 get_constraint_for_rhs (gimple_assign_rhs2 (t), &rhsc);
4688 get_constraint_for_rhs (gimple_assign_rhs3 (t), &tmp);
4689 FOR_EACH_VEC_ELT (tmp, i, rhsp)
4690 rhsc.safe_push (*rhsp);
4691 tmp.release ();
4692 }
4693 else if (truth_value_p (code))
4694 /* Truth value results are not pointer (parts). Or at least
4695 very very unreasonable obfuscation of a part. */
4696 ;
4697 else
4698 {
4699 /* All other operations are merges. */
4700 vec<ce_s> tmp = vNULL;
4701 struct constraint_expr *rhsp;
4702 unsigned i, j;
4703 get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc);
4704 for (i = 2; i < gimple_num_ops (t); ++i)
4705 {
4706 get_constraint_for_rhs (gimple_op (t, i), &tmp);
4707 FOR_EACH_VEC_ELT (tmp, j, rhsp)
4708 rhsc.safe_push (*rhsp);
4709 tmp.truncate (0);
4710 }
4711 tmp.release ();
4712 }
4713 process_all_all_constraints (lhsc, rhsc);
4714 }
4715 /* If there is a store to a global variable the rhs escapes. */
4716 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
4717 && DECL_P (lhsop)
4718 && is_global_var (lhsop)
4719 && (!in_ipa_mode
4720 || DECL_EXTERNAL (lhsop) || TREE_PUBLIC (lhsop)))
4721 make_escape_constraint (rhsop);
4722 }
4723 /* Handle escapes through return. */
4724 else if (gimple_code (t) == GIMPLE_RETURN
4725 && gimple_return_retval (t) != NULL_TREE)
4726 {
4727 fi = NULL;
4728 if (!in_ipa_mode
4729 || !(fi = get_vi_for_tree (cfun->decl)))
4730 make_escape_constraint (gimple_return_retval (t));
4731 else if (in_ipa_mode
4732 && fi != NULL)
4733 {
4734 struct constraint_expr lhs ;
4735 struct constraint_expr *rhsp;
4736 unsigned i;
4737
4738 lhs = get_function_part_constraint (fi, fi_result);
4739 get_constraint_for_rhs (gimple_return_retval (t), &rhsc);
4740 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4741 process_constraint (new_constraint (lhs, *rhsp));
4742 }
4743 }
4744 /* Handle asms conservatively by adding escape constraints to everything. */
4745 else if (gimple_code (t) == GIMPLE_ASM)
4746 {
4747 unsigned i, noutputs;
4748 const char **oconstraints;
4749 const char *constraint;
4750 bool allows_mem, allows_reg, is_inout;
4751
4752 noutputs = gimple_asm_noutputs (t);
4753 oconstraints = XALLOCAVEC (const char *, noutputs);
4754
4755 for (i = 0; i < noutputs; ++i)
4756 {
4757 tree link = gimple_asm_output_op (t, i);
4758 tree op = TREE_VALUE (link);
4759
4760 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4761 oconstraints[i] = constraint;
4762 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4763 &allows_reg, &is_inout);
4764
4765 /* A memory constraint makes the address of the operand escape. */
4766 if (!allows_reg && allows_mem)
4767 make_escape_constraint (build_fold_addr_expr (op));
4768
4769 /* The asm may read global memory, so outputs may point to
4770 any global memory. */
4771 if (op)
4772 {
4773 vec<ce_s> lhsc = vNULL;
4774 struct constraint_expr rhsc, *lhsp;
4775 unsigned j;
4776 get_constraint_for (op, &lhsc);
4777 rhsc.var = nonlocal_id;
4778 rhsc.offset = 0;
4779 rhsc.type = SCALAR;
4780 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4781 process_constraint (new_constraint (*lhsp, rhsc));
4782 lhsc.release ();
4783 }
4784 }
4785 for (i = 0; i < gimple_asm_ninputs (t); ++i)
4786 {
4787 tree link = gimple_asm_input_op (t, i);
4788 tree op = TREE_VALUE (link);
4789
4790 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4791
4792 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4793 &allows_mem, &allows_reg);
4794
4795 /* A memory constraint makes the address of the operand escape. */
4796 if (!allows_reg && allows_mem)
4797 make_escape_constraint (build_fold_addr_expr (op));
4798 /* Strictly we'd only need the constraint to ESCAPED if
4799 the asm clobbers memory, otherwise using something
4800 along the lines of per-call clobbers/uses would be enough. */
4801 else if (op)
4802 make_escape_constraint (op);
4803 }
4804 }
4805
4806 rhsc.release ();
4807 lhsc.release ();
4808 }
4809
4810
4811 /* Create a constraint adding to the clobber set of FI the memory
4812 pointed to by PTR. */
4813
4814 static void
4815 process_ipa_clobber (varinfo_t fi, tree ptr)
4816 {
4817 vec<ce_s> ptrc = vNULL;
4818 struct constraint_expr *c, lhs;
4819 unsigned i;
4820 get_constraint_for_rhs (ptr, &ptrc);
4821 lhs = get_function_part_constraint (fi, fi_clobbers);
4822 FOR_EACH_VEC_ELT (ptrc, i, c)
4823 process_constraint (new_constraint (lhs, *c));
4824 ptrc.release ();
4825 }
4826
4827 /* Walk statement T setting up clobber and use constraints according to the
4828 references found in T. This function is a main part of the
4829 IPA constraint builder. */
4830
4831 static void
4832 find_func_clobbers (gimple origt)
4833 {
4834 gimple t = origt;
4835 vec<ce_s> lhsc = vNULL;
4836 auto_vec<ce_s> rhsc;
4837 varinfo_t fi;
4838
4839 /* Add constraints for clobbered/used in IPA mode.
4840 We are not interested in what automatic variables are clobbered
4841 or used as we only use the information in the caller to which
4842 they do not escape. */
4843 gcc_assert (in_ipa_mode);
4844
4845 /* If the stmt refers to memory in any way it better had a VUSE. */
4846 if (gimple_vuse (t) == NULL_TREE)
4847 return;
4848
4849 /* We'd better have function information for the current function. */
4850 fi = lookup_vi_for_tree (cfun->decl);
4851 gcc_assert (fi != NULL);
4852
4853 /* Account for stores in assignments and calls. */
4854 if (gimple_vdef (t) != NULL_TREE
4855 && gimple_has_lhs (t))
4856 {
4857 tree lhs = gimple_get_lhs (t);
4858 tree tem = lhs;
4859 while (handled_component_p (tem))
4860 tem = TREE_OPERAND (tem, 0);
4861 if ((DECL_P (tem)
4862 && !auto_var_in_fn_p (tem, cfun->decl))
4863 || INDIRECT_REF_P (tem)
4864 || (TREE_CODE (tem) == MEM_REF
4865 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4866 && auto_var_in_fn_p
4867 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4868 {
4869 struct constraint_expr lhsc, *rhsp;
4870 unsigned i;
4871 lhsc = get_function_part_constraint (fi, fi_clobbers);
4872 get_constraint_for_address_of (lhs, &rhsc);
4873 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4874 process_constraint (new_constraint (lhsc, *rhsp));
4875 rhsc.release ();
4876 }
4877 }
4878
4879 /* Account for uses in assigments and returns. */
4880 if (gimple_assign_single_p (t)
4881 || (gimple_code (t) == GIMPLE_RETURN
4882 && gimple_return_retval (t) != NULL_TREE))
4883 {
4884 tree rhs = (gimple_assign_single_p (t)
4885 ? gimple_assign_rhs1 (t) : gimple_return_retval (t));
4886 tree tem = rhs;
4887 while (handled_component_p (tem))
4888 tem = TREE_OPERAND (tem, 0);
4889 if ((DECL_P (tem)
4890 && !auto_var_in_fn_p (tem, cfun->decl))
4891 || INDIRECT_REF_P (tem)
4892 || (TREE_CODE (tem) == MEM_REF
4893 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4894 && auto_var_in_fn_p
4895 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4896 {
4897 struct constraint_expr lhs, *rhsp;
4898 unsigned i;
4899 lhs = get_function_part_constraint (fi, fi_uses);
4900 get_constraint_for_address_of (rhs, &rhsc);
4901 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4902 process_constraint (new_constraint (lhs, *rhsp));
4903 rhsc.release ();
4904 }
4905 }
4906
4907 if (is_gimple_call (t))
4908 {
4909 varinfo_t cfi = NULL;
4910 tree decl = gimple_call_fndecl (t);
4911 struct constraint_expr lhs, rhs;
4912 unsigned i, j;
4913
4914 /* For builtins we do not have separate function info. For those
4915 we do not generate escapes for we have to generate clobbers/uses. */
4916 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4917 switch (DECL_FUNCTION_CODE (decl))
4918 {
4919 /* The following functions use and clobber memory pointed to
4920 by their arguments. */
4921 case BUILT_IN_STRCPY:
4922 case BUILT_IN_STRNCPY:
4923 case BUILT_IN_BCOPY:
4924 case BUILT_IN_MEMCPY:
4925 case BUILT_IN_MEMMOVE:
4926 case BUILT_IN_MEMPCPY:
4927 case BUILT_IN_STPCPY:
4928 case BUILT_IN_STPNCPY:
4929 case BUILT_IN_STRCAT:
4930 case BUILT_IN_STRNCAT:
4931 case BUILT_IN_STRCPY_CHK:
4932 case BUILT_IN_STRNCPY_CHK:
4933 case BUILT_IN_MEMCPY_CHK:
4934 case BUILT_IN_MEMMOVE_CHK:
4935 case BUILT_IN_MEMPCPY_CHK:
4936 case BUILT_IN_STPCPY_CHK:
4937 case BUILT_IN_STPNCPY_CHK:
4938 case BUILT_IN_STRCAT_CHK:
4939 case BUILT_IN_STRNCAT_CHK:
4940 {
4941 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4942 == BUILT_IN_BCOPY ? 1 : 0));
4943 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4944 == BUILT_IN_BCOPY ? 0 : 1));
4945 unsigned i;
4946 struct constraint_expr *rhsp, *lhsp;
4947 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4948 lhs = get_function_part_constraint (fi, fi_clobbers);
4949 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4950 process_constraint (new_constraint (lhs, *lhsp));
4951 lhsc.release ();
4952 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4953 lhs = get_function_part_constraint (fi, fi_uses);
4954 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4955 process_constraint (new_constraint (lhs, *rhsp));
4956 rhsc.release ();
4957 return;
4958 }
4959 /* The following function clobbers memory pointed to by
4960 its argument. */
4961 case BUILT_IN_MEMSET:
4962 case BUILT_IN_MEMSET_CHK:
4963 {
4964 tree dest = gimple_call_arg (t, 0);
4965 unsigned i;
4966 ce_s *lhsp;
4967 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4968 lhs = get_function_part_constraint (fi, fi_clobbers);
4969 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4970 process_constraint (new_constraint (lhs, *lhsp));
4971 lhsc.release ();
4972 return;
4973 }
4974 /* The following functions clobber their second and third
4975 arguments. */
4976 case BUILT_IN_SINCOS:
4977 case BUILT_IN_SINCOSF:
4978 case BUILT_IN_SINCOSL:
4979 {
4980 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4981 process_ipa_clobber (fi, gimple_call_arg (t, 2));
4982 return;
4983 }
4984 /* The following functions clobber their second argument. */
4985 case BUILT_IN_FREXP:
4986 case BUILT_IN_FREXPF:
4987 case BUILT_IN_FREXPL:
4988 case BUILT_IN_LGAMMA_R:
4989 case BUILT_IN_LGAMMAF_R:
4990 case BUILT_IN_LGAMMAL_R:
4991 case BUILT_IN_GAMMA_R:
4992 case BUILT_IN_GAMMAF_R:
4993 case BUILT_IN_GAMMAL_R:
4994 case BUILT_IN_MODF:
4995 case BUILT_IN_MODFF:
4996 case BUILT_IN_MODFL:
4997 {
4998 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4999 return;
5000 }
5001 /* The following functions clobber their third argument. */
5002 case BUILT_IN_REMQUO:
5003 case BUILT_IN_REMQUOF:
5004 case BUILT_IN_REMQUOL:
5005 {
5006 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5007 return;
5008 }
5009 /* The following functions neither read nor clobber memory. */
5010 case BUILT_IN_ASSUME_ALIGNED:
5011 case BUILT_IN_FREE:
5012 return;
5013 /* Trampolines are of no interest to us. */
5014 case BUILT_IN_INIT_TRAMPOLINE:
5015 case BUILT_IN_ADJUST_TRAMPOLINE:
5016 return;
5017 case BUILT_IN_VA_START:
5018 case BUILT_IN_VA_END:
5019 return;
5020 /* printf-style functions may have hooks to set pointers to
5021 point to somewhere into the generated string. Leave them
5022 for a later exercise... */
5023 default:
5024 /* Fallthru to general call handling. */;
5025 }
5026
5027 /* Parameters passed by value are used. */
5028 lhs = get_function_part_constraint (fi, fi_uses);
5029 for (i = 0; i < gimple_call_num_args (t); i++)
5030 {
5031 struct constraint_expr *rhsp;
5032 tree arg = gimple_call_arg (t, i);
5033
5034 if (TREE_CODE (arg) == SSA_NAME
5035 || is_gimple_min_invariant (arg))
5036 continue;
5037
5038 get_constraint_for_address_of (arg, &rhsc);
5039 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5040 process_constraint (new_constraint (lhs, *rhsp));
5041 rhsc.release ();
5042 }
5043
5044 /* Build constraints for propagating clobbers/uses along the
5045 callgraph edges. */
5046 cfi = get_fi_for_callee (t);
5047 if (cfi->id == anything_id)
5048 {
5049 if (gimple_vdef (t))
5050 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5051 anything_id);
5052 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5053 anything_id);
5054 return;
5055 }
5056
5057 /* For callees without function info (that's external functions),
5058 ESCAPED is clobbered and used. */
5059 if (gimple_call_fndecl (t)
5060 && !cfi->is_fn_info)
5061 {
5062 varinfo_t vi;
5063
5064 if (gimple_vdef (t))
5065 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5066 escaped_id);
5067 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
5068
5069 /* Also honor the call statement use/clobber info. */
5070 if ((vi = lookup_call_clobber_vi (t)) != NULL)
5071 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5072 vi->id);
5073 if ((vi = lookup_call_use_vi (t)) != NULL)
5074 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
5075 vi->id);
5076 return;
5077 }
5078
5079 /* Otherwise the caller clobbers and uses what the callee does.
5080 ??? This should use a new complex constraint that filters
5081 local variables of the callee. */
5082 if (gimple_vdef (t))
5083 {
5084 lhs = get_function_part_constraint (fi, fi_clobbers);
5085 rhs = get_function_part_constraint (cfi, fi_clobbers);
5086 process_constraint (new_constraint (lhs, rhs));
5087 }
5088 lhs = get_function_part_constraint (fi, fi_uses);
5089 rhs = get_function_part_constraint (cfi, fi_uses);
5090 process_constraint (new_constraint (lhs, rhs));
5091 }
5092 else if (gimple_code (t) == GIMPLE_ASM)
5093 {
5094 /* ??? Ick. We can do better. */
5095 if (gimple_vdef (t))
5096 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5097 anything_id);
5098 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5099 anything_id);
5100 }
5101 }
5102
5103
5104 /* Find the first varinfo in the same variable as START that overlaps with
5105 OFFSET. Return NULL if we can't find one. */
5106
5107 static varinfo_t
5108 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
5109 {
5110 /* If the offset is outside of the variable, bail out. */
5111 if (offset >= start->fullsize)
5112 return NULL;
5113
5114 /* If we cannot reach offset from start, lookup the first field
5115 and start from there. */
5116 if (start->offset > offset)
5117 start = get_varinfo (start->head);
5118
5119 while (start)
5120 {
5121 /* We may not find a variable in the field list with the actual
5122 offset when when we have glommed a structure to a variable.
5123 In that case, however, offset should still be within the size
5124 of the variable. */
5125 if (offset >= start->offset
5126 && (offset - start->offset) < start->size)
5127 return start;
5128
5129 start = vi_next (start);
5130 }
5131
5132 return NULL;
5133 }
5134
5135 /* Find the first varinfo in the same variable as START that overlaps with
5136 OFFSET. If there is no such varinfo the varinfo directly preceding
5137 OFFSET is returned. */
5138
5139 static varinfo_t
5140 first_or_preceding_vi_for_offset (varinfo_t start,
5141 unsigned HOST_WIDE_INT offset)
5142 {
5143 /* If we cannot reach offset from start, lookup the first field
5144 and start from there. */
5145 if (start->offset > offset)
5146 start = get_varinfo (start->head);
5147
5148 /* We may not find a variable in the field list with the actual
5149 offset when when we have glommed a structure to a variable.
5150 In that case, however, offset should still be within the size
5151 of the variable.
5152 If we got beyond the offset we look for return the field
5153 directly preceding offset which may be the last field. */
5154 while (start->next
5155 && offset >= start->offset
5156 && !((offset - start->offset) < start->size))
5157 start = vi_next (start);
5158
5159 return start;
5160 }
5161
5162
5163 /* This structure is used during pushing fields onto the fieldstack
5164 to track the offset of the field, since bitpos_of_field gives it
5165 relative to its immediate containing type, and we want it relative
5166 to the ultimate containing object. */
5167
5168 struct fieldoff
5169 {
5170 /* Offset from the base of the base containing object to this field. */
5171 HOST_WIDE_INT offset;
5172
5173 /* Size, in bits, of the field. */
5174 unsigned HOST_WIDE_INT size;
5175
5176 unsigned has_unknown_size : 1;
5177
5178 unsigned must_have_pointers : 1;
5179
5180 unsigned may_have_pointers : 1;
5181
5182 unsigned only_restrict_pointers : 1;
5183 };
5184 typedef struct fieldoff fieldoff_s;
5185
5186
5187 /* qsort comparison function for two fieldoff's PA and PB */
5188
5189 static int
5190 fieldoff_compare (const void *pa, const void *pb)
5191 {
5192 const fieldoff_s *foa = (const fieldoff_s *)pa;
5193 const fieldoff_s *fob = (const fieldoff_s *)pb;
5194 unsigned HOST_WIDE_INT foasize, fobsize;
5195
5196 if (foa->offset < fob->offset)
5197 return -1;
5198 else if (foa->offset > fob->offset)
5199 return 1;
5200
5201 foasize = foa->size;
5202 fobsize = fob->size;
5203 if (foasize < fobsize)
5204 return -1;
5205 else if (foasize > fobsize)
5206 return 1;
5207 return 0;
5208 }
5209
5210 /* Sort a fieldstack according to the field offset and sizes. */
5211 static void
5212 sort_fieldstack (vec<fieldoff_s> fieldstack)
5213 {
5214 fieldstack.qsort (fieldoff_compare);
5215 }
5216
5217 /* Return true if T is a type that can have subvars. */
5218
5219 static inline bool
5220 type_can_have_subvars (const_tree t)
5221 {
5222 /* Aggregates without overlapping fields can have subvars. */
5223 return TREE_CODE (t) == RECORD_TYPE;
5224 }
5225
5226 /* Return true if V is a tree that we can have subvars for.
5227 Normally, this is any aggregate type. Also complex
5228 types which are not gimple registers can have subvars. */
5229
5230 static inline bool
5231 var_can_have_subvars (const_tree v)
5232 {
5233 /* Volatile variables should never have subvars. */
5234 if (TREE_THIS_VOLATILE (v))
5235 return false;
5236
5237 /* Non decls or memory tags can never have subvars. */
5238 if (!DECL_P (v))
5239 return false;
5240
5241 return type_can_have_subvars (TREE_TYPE (v));
5242 }
5243
5244 /* Return true if T is a type that does contain pointers. */
5245
5246 static bool
5247 type_must_have_pointers (tree type)
5248 {
5249 if (POINTER_TYPE_P (type))
5250 return true;
5251
5252 if (TREE_CODE (type) == ARRAY_TYPE)
5253 return type_must_have_pointers (TREE_TYPE (type));
5254
5255 /* A function or method can have pointers as arguments, so track
5256 those separately. */
5257 if (TREE_CODE (type) == FUNCTION_TYPE
5258 || TREE_CODE (type) == METHOD_TYPE)
5259 return true;
5260
5261 return false;
5262 }
5263
5264 static bool
5265 field_must_have_pointers (tree t)
5266 {
5267 return type_must_have_pointers (TREE_TYPE (t));
5268 }
5269
5270 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5271 the fields of TYPE onto fieldstack, recording their offsets along
5272 the way.
5273
5274 OFFSET is used to keep track of the offset in this entire
5275 structure, rather than just the immediately containing structure.
5276 Returns false if the caller is supposed to handle the field we
5277 recursed for. */
5278
5279 static bool
5280 push_fields_onto_fieldstack (tree type, vec<fieldoff_s> *fieldstack,
5281 HOST_WIDE_INT offset)
5282 {
5283 tree field;
5284 bool empty_p = true;
5285
5286 if (TREE_CODE (type) != RECORD_TYPE)
5287 return false;
5288
5289 /* If the vector of fields is growing too big, bail out early.
5290 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5291 sure this fails. */
5292 if (fieldstack->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5293 return false;
5294
5295 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5296 if (TREE_CODE (field) == FIELD_DECL)
5297 {
5298 bool push = false;
5299 HOST_WIDE_INT foff = bitpos_of_field (field);
5300
5301 if (!var_can_have_subvars (field)
5302 || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
5303 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5304 push = true;
5305 else if (!push_fields_onto_fieldstack
5306 (TREE_TYPE (field), fieldstack, offset + foff)
5307 && (DECL_SIZE (field)
5308 && !integer_zerop (DECL_SIZE (field))))
5309 /* Empty structures may have actual size, like in C++. So
5310 see if we didn't push any subfields and the size is
5311 nonzero, push the field onto the stack. */
5312 push = true;
5313
5314 if (push)
5315 {
5316 fieldoff_s *pair = NULL;
5317 bool has_unknown_size = false;
5318 bool must_have_pointers_p;
5319
5320 if (!fieldstack->is_empty ())
5321 pair = &fieldstack->last ();
5322
5323 /* If there isn't anything at offset zero, create sth. */
5324 if (!pair
5325 && offset + foff != 0)
5326 {
5327 fieldoff_s e = {0, offset + foff, false, false, false, false};
5328 pair = fieldstack->safe_push (e);
5329 }
5330
5331 if (!DECL_SIZE (field)
5332 || !tree_fits_uhwi_p (DECL_SIZE (field)))
5333 has_unknown_size = true;
5334
5335 /* If adjacent fields do not contain pointers merge them. */
5336 must_have_pointers_p = field_must_have_pointers (field);
5337 if (pair
5338 && !has_unknown_size
5339 && !must_have_pointers_p
5340 && !pair->must_have_pointers
5341 && !pair->has_unknown_size
5342 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5343 {
5344 pair->size += tree_to_uhwi (DECL_SIZE (field));
5345 }
5346 else
5347 {
5348 fieldoff_s e;
5349 e.offset = offset + foff;
5350 e.has_unknown_size = has_unknown_size;
5351 if (!has_unknown_size)
5352 e.size = tree_to_uhwi (DECL_SIZE (field));
5353 else
5354 e.size = -1;
5355 e.must_have_pointers = must_have_pointers_p;
5356 e.may_have_pointers = true;
5357 e.only_restrict_pointers
5358 = (!has_unknown_size
5359 && POINTER_TYPE_P (TREE_TYPE (field))
5360 && TYPE_RESTRICT (TREE_TYPE (field)));
5361 fieldstack->safe_push (e);
5362 }
5363 }
5364
5365 empty_p = false;
5366 }
5367
5368 return !empty_p;
5369 }
5370
5371 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5372 if it is a varargs function. */
5373
5374 static unsigned int
5375 count_num_arguments (tree decl, bool *is_varargs)
5376 {
5377 unsigned int num = 0;
5378 tree t;
5379
5380 /* Capture named arguments for K&R functions. They do not
5381 have a prototype and thus no TYPE_ARG_TYPES. */
5382 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5383 ++num;
5384
5385 /* Check if the function has variadic arguments. */
5386 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5387 if (TREE_VALUE (t) == void_type_node)
5388 break;
5389 if (!t)
5390 *is_varargs = true;
5391
5392 return num;
5393 }
5394
5395 /* Creation function node for DECL, using NAME, and return the index
5396 of the variable we've created for the function. */
5397
5398 static varinfo_t
5399 create_function_info_for (tree decl, const char *name)
5400 {
5401 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5402 varinfo_t vi, prev_vi;
5403 tree arg;
5404 unsigned int i;
5405 bool is_varargs = false;
5406 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5407
5408 /* Create the variable info. */
5409
5410 vi = new_var_info (decl, name);
5411 vi->offset = 0;
5412 vi->size = 1;
5413 vi->fullsize = fi_parm_base + num_args;
5414 vi->is_fn_info = 1;
5415 vi->may_have_pointers = false;
5416 if (is_varargs)
5417 vi->fullsize = ~0;
5418 insert_vi_for_tree (vi->decl, vi);
5419
5420 prev_vi = vi;
5421
5422 /* Create a variable for things the function clobbers and one for
5423 things the function uses. */
5424 {
5425 varinfo_t clobbervi, usevi;
5426 const char *newname;
5427 char *tempname;
5428
5429 asprintf (&tempname, "%s.clobber", name);
5430 newname = ggc_strdup (tempname);
5431 free (tempname);
5432
5433 clobbervi = new_var_info (NULL, newname);
5434 clobbervi->offset = fi_clobbers;
5435 clobbervi->size = 1;
5436 clobbervi->fullsize = vi->fullsize;
5437 clobbervi->is_full_var = true;
5438 clobbervi->is_global_var = false;
5439 gcc_assert (prev_vi->offset < clobbervi->offset);
5440 prev_vi->next = clobbervi->id;
5441 prev_vi = clobbervi;
5442
5443 asprintf (&tempname, "%s.use", name);
5444 newname = ggc_strdup (tempname);
5445 free (tempname);
5446
5447 usevi = new_var_info (NULL, newname);
5448 usevi->offset = fi_uses;
5449 usevi->size = 1;
5450 usevi->fullsize = vi->fullsize;
5451 usevi->is_full_var = true;
5452 usevi->is_global_var = false;
5453 gcc_assert (prev_vi->offset < usevi->offset);
5454 prev_vi->next = usevi->id;
5455 prev_vi = usevi;
5456 }
5457
5458 /* And one for the static chain. */
5459 if (fn->static_chain_decl != NULL_TREE)
5460 {
5461 varinfo_t chainvi;
5462 const char *newname;
5463 char *tempname;
5464
5465 asprintf (&tempname, "%s.chain", name);
5466 newname = ggc_strdup (tempname);
5467 free (tempname);
5468
5469 chainvi = new_var_info (fn->static_chain_decl, newname);
5470 chainvi->offset = fi_static_chain;
5471 chainvi->size = 1;
5472 chainvi->fullsize = vi->fullsize;
5473 chainvi->is_full_var = true;
5474 chainvi->is_global_var = false;
5475 gcc_assert (prev_vi->offset < chainvi->offset);
5476 prev_vi->next = chainvi->id;
5477 prev_vi = chainvi;
5478 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5479 }
5480
5481 /* Create a variable for the return var. */
5482 if (DECL_RESULT (decl) != NULL
5483 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5484 {
5485 varinfo_t resultvi;
5486 const char *newname;
5487 char *tempname;
5488 tree resultdecl = decl;
5489
5490 if (DECL_RESULT (decl))
5491 resultdecl = DECL_RESULT (decl);
5492
5493 asprintf (&tempname, "%s.result", name);
5494 newname = ggc_strdup (tempname);
5495 free (tempname);
5496
5497 resultvi = new_var_info (resultdecl, newname);
5498 resultvi->offset = fi_result;
5499 resultvi->size = 1;
5500 resultvi->fullsize = vi->fullsize;
5501 resultvi->is_full_var = true;
5502 if (DECL_RESULT (decl))
5503 resultvi->may_have_pointers = true;
5504 gcc_assert (prev_vi->offset < resultvi->offset);
5505 prev_vi->next = resultvi->id;
5506 prev_vi = resultvi;
5507 if (DECL_RESULT (decl))
5508 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
5509 }
5510
5511 /* Set up variables for each argument. */
5512 arg = DECL_ARGUMENTS (decl);
5513 for (i = 0; i < num_args; i++)
5514 {
5515 varinfo_t argvi;
5516 const char *newname;
5517 char *tempname;
5518 tree argdecl = decl;
5519
5520 if (arg)
5521 argdecl = arg;
5522
5523 asprintf (&tempname, "%s.arg%d", name, i);
5524 newname = ggc_strdup (tempname);
5525 free (tempname);
5526
5527 argvi = new_var_info (argdecl, newname);
5528 argvi->offset = fi_parm_base + i;
5529 argvi->size = 1;
5530 argvi->is_full_var = true;
5531 argvi->fullsize = vi->fullsize;
5532 if (arg)
5533 argvi->may_have_pointers = true;
5534 gcc_assert (prev_vi->offset < argvi->offset);
5535 prev_vi->next = argvi->id;
5536 prev_vi = argvi;
5537 if (arg)
5538 {
5539 insert_vi_for_tree (arg, argvi);
5540 arg = DECL_CHAIN (arg);
5541 }
5542 }
5543
5544 /* Add one representative for all further args. */
5545 if (is_varargs)
5546 {
5547 varinfo_t argvi;
5548 const char *newname;
5549 char *tempname;
5550 tree decl;
5551
5552 asprintf (&tempname, "%s.varargs", name);
5553 newname = ggc_strdup (tempname);
5554 free (tempname);
5555
5556 /* We need sth that can be pointed to for va_start. */
5557 decl = build_fake_var_decl (ptr_type_node);
5558
5559 argvi = new_var_info (decl, newname);
5560 argvi->offset = fi_parm_base + num_args;
5561 argvi->size = ~0;
5562 argvi->is_full_var = true;
5563 argvi->is_heap_var = true;
5564 argvi->fullsize = vi->fullsize;
5565 gcc_assert (prev_vi->offset < argvi->offset);
5566 prev_vi->next = argvi->id;
5567 prev_vi = argvi;
5568 }
5569
5570 return vi;
5571 }
5572
5573
5574 /* Return true if FIELDSTACK contains fields that overlap.
5575 FIELDSTACK is assumed to be sorted by offset. */
5576
5577 static bool
5578 check_for_overlaps (vec<fieldoff_s> fieldstack)
5579 {
5580 fieldoff_s *fo = NULL;
5581 unsigned int i;
5582 HOST_WIDE_INT lastoffset = -1;
5583
5584 FOR_EACH_VEC_ELT (fieldstack, i, fo)
5585 {
5586 if (fo->offset == lastoffset)
5587 return true;
5588 lastoffset = fo->offset;
5589 }
5590 return false;
5591 }
5592
5593 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5594 This will also create any varinfo structures necessary for fields
5595 of DECL. */
5596
5597 static varinfo_t
5598 create_variable_info_for_1 (tree decl, const char *name)
5599 {
5600 varinfo_t vi, newvi;
5601 tree decl_type = TREE_TYPE (decl);
5602 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
5603 auto_vec<fieldoff_s> fieldstack;
5604 fieldoff_s *fo;
5605 unsigned int i;
5606
5607 if (!declsize
5608 || !tree_fits_uhwi_p (declsize))
5609 {
5610 vi = new_var_info (decl, name);
5611 vi->offset = 0;
5612 vi->size = ~0;
5613 vi->fullsize = ~0;
5614 vi->is_unknown_size_var = true;
5615 vi->is_full_var = true;
5616 vi->may_have_pointers = true;
5617 return vi;
5618 }
5619
5620 /* Collect field information. */
5621 if (use_field_sensitive
5622 && var_can_have_subvars (decl)
5623 /* ??? Force us to not use subfields for global initializers
5624 in IPA mode. Else we'd have to parse arbitrary initializers. */
5625 && !(in_ipa_mode
5626 && is_global_var (decl)
5627 && DECL_INITIAL (decl)))
5628 {
5629 fieldoff_s *fo = NULL;
5630 bool notokay = false;
5631 unsigned int i;
5632
5633 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
5634
5635 for (i = 0; !notokay && fieldstack.iterate (i, &fo); i++)
5636 if (fo->has_unknown_size
5637 || fo->offset < 0)
5638 {
5639 notokay = true;
5640 break;
5641 }
5642
5643 /* We can't sort them if we have a field with a variable sized type,
5644 which will make notokay = true. In that case, we are going to return
5645 without creating varinfos for the fields anyway, so sorting them is a
5646 waste to boot. */
5647 if (!notokay)
5648 {
5649 sort_fieldstack (fieldstack);
5650 /* Due to some C++ FE issues, like PR 22488, we might end up
5651 what appear to be overlapping fields even though they,
5652 in reality, do not overlap. Until the C++ FE is fixed,
5653 we will simply disable field-sensitivity for these cases. */
5654 notokay = check_for_overlaps (fieldstack);
5655 }
5656
5657 if (notokay)
5658 fieldstack.release ();
5659 }
5660
5661 /* If we didn't end up collecting sub-variables create a full
5662 variable for the decl. */
5663 if (fieldstack.length () <= 1
5664 || fieldstack.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5665 {
5666 vi = new_var_info (decl, name);
5667 vi->offset = 0;
5668 vi->may_have_pointers = true;
5669 vi->fullsize = tree_to_uhwi (declsize);
5670 vi->size = vi->fullsize;
5671 vi->is_full_var = true;
5672 fieldstack.release ();
5673 return vi;
5674 }
5675
5676 vi = new_var_info (decl, name);
5677 vi->fullsize = tree_to_uhwi (declsize);
5678 for (i = 0, newvi = vi;
5679 fieldstack.iterate (i, &fo);
5680 ++i, newvi = vi_next (newvi))
5681 {
5682 const char *newname = "NULL";
5683 char *tempname;
5684
5685 if (dump_file)
5686 {
5687 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC
5688 "+" HOST_WIDE_INT_PRINT_DEC, name, fo->offset, fo->size);
5689 newname = ggc_strdup (tempname);
5690 free (tempname);
5691 }
5692 newvi->name = newname;
5693 newvi->offset = fo->offset;
5694 newvi->size = fo->size;
5695 newvi->fullsize = vi->fullsize;
5696 newvi->may_have_pointers = fo->may_have_pointers;
5697 newvi->only_restrict_pointers = fo->only_restrict_pointers;
5698 if (i + 1 < fieldstack.length ())
5699 {
5700 varinfo_t tem = new_var_info (decl, name);
5701 newvi->next = tem->id;
5702 tem->head = vi->id;
5703 }
5704 }
5705
5706 return vi;
5707 }
5708
5709 static unsigned int
5710 create_variable_info_for (tree decl, const char *name)
5711 {
5712 varinfo_t vi = create_variable_info_for_1 (decl, name);
5713 unsigned int id = vi->id;
5714
5715 insert_vi_for_tree (decl, vi);
5716
5717 if (TREE_CODE (decl) != VAR_DECL)
5718 return id;
5719
5720 /* Create initial constraints for globals. */
5721 for (; vi; vi = vi_next (vi))
5722 {
5723 if (!vi->may_have_pointers
5724 || !vi->is_global_var)
5725 continue;
5726
5727 /* Mark global restrict qualified pointers. */
5728 if ((POINTER_TYPE_P (TREE_TYPE (decl))
5729 && TYPE_RESTRICT (TREE_TYPE (decl)))
5730 || vi->only_restrict_pointers)
5731 {
5732 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5733 continue;
5734 }
5735
5736 /* In non-IPA mode the initializer from nonlocal is all we need. */
5737 if (!in_ipa_mode
5738 || DECL_HARD_REGISTER (decl))
5739 make_copy_constraint (vi, nonlocal_id);
5740
5741 /* In IPA mode parse the initializer and generate proper constraints
5742 for it. */
5743 else
5744 {
5745 varpool_node *vnode = varpool_get_node (decl);
5746
5747 /* For escaped variables initialize them from nonlocal. */
5748 if (!varpool_all_refs_explicit_p (vnode))
5749 make_copy_constraint (vi, nonlocal_id);
5750
5751 /* If this is a global variable with an initializer and we are in
5752 IPA mode generate constraints for it. */
5753 if (DECL_INITIAL (decl)
5754 && vnode->definition)
5755 {
5756 auto_vec<ce_s> rhsc;
5757 struct constraint_expr lhs, *rhsp;
5758 unsigned i;
5759 get_constraint_for_rhs (DECL_INITIAL (decl), &rhsc);
5760 lhs.var = vi->id;
5761 lhs.offset = 0;
5762 lhs.type = SCALAR;
5763 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5764 process_constraint (new_constraint (lhs, *rhsp));
5765 /* If this is a variable that escapes from the unit
5766 the initializer escapes as well. */
5767 if (!varpool_all_refs_explicit_p (vnode))
5768 {
5769 lhs.var = escaped_id;
5770 lhs.offset = 0;
5771 lhs.type = SCALAR;
5772 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5773 process_constraint (new_constraint (lhs, *rhsp));
5774 }
5775 }
5776 }
5777 }
5778
5779 return id;
5780 }
5781
5782 /* Print out the points-to solution for VAR to FILE. */
5783
5784 static void
5785 dump_solution_for_var (FILE *file, unsigned int var)
5786 {
5787 varinfo_t vi = get_varinfo (var);
5788 unsigned int i;
5789 bitmap_iterator bi;
5790
5791 /* Dump the solution for unified vars anyway, this avoids difficulties
5792 in scanning dumps in the testsuite. */
5793 fprintf (file, "%s = { ", vi->name);
5794 vi = get_varinfo (find (var));
5795 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5796 fprintf (file, "%s ", get_varinfo (i)->name);
5797 fprintf (file, "}");
5798
5799 /* But note when the variable was unified. */
5800 if (vi->id != var)
5801 fprintf (file, " same as %s", vi->name);
5802
5803 fprintf (file, "\n");
5804 }
5805
5806 /* Print the points-to solution for VAR to stdout. */
5807
5808 DEBUG_FUNCTION void
5809 debug_solution_for_var (unsigned int var)
5810 {
5811 dump_solution_for_var (stdout, var);
5812 }
5813
5814 /* Create varinfo structures for all of the variables in the
5815 function for intraprocedural mode. */
5816
5817 static void
5818 intra_create_variable_infos (void)
5819 {
5820 tree t;
5821
5822 /* For each incoming pointer argument arg, create the constraint ARG
5823 = NONLOCAL or a dummy variable if it is a restrict qualified
5824 passed-by-reference argument. */
5825 for (t = DECL_ARGUMENTS (current_function_decl); t; t = DECL_CHAIN (t))
5826 {
5827 varinfo_t p = get_vi_for_tree (t);
5828
5829 /* For restrict qualified pointers to objects passed by
5830 reference build a real representative for the pointed-to object.
5831 Treat restrict qualified references the same. */
5832 if (TYPE_RESTRICT (TREE_TYPE (t))
5833 && ((DECL_BY_REFERENCE (t) && POINTER_TYPE_P (TREE_TYPE (t)))
5834 || TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5835 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t))))
5836 {
5837 struct constraint_expr lhsc, rhsc;
5838 varinfo_t vi;
5839 tree heapvar = build_fake_var_decl (TREE_TYPE (TREE_TYPE (t)));
5840 DECL_EXTERNAL (heapvar) = 1;
5841 vi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS");
5842 insert_vi_for_tree (heapvar, vi);
5843 lhsc.var = p->id;
5844 lhsc.type = SCALAR;
5845 lhsc.offset = 0;
5846 rhsc.var = vi->id;
5847 rhsc.type = ADDRESSOF;
5848 rhsc.offset = 0;
5849 process_constraint (new_constraint (lhsc, rhsc));
5850 for (; vi; vi = vi_next (vi))
5851 if (vi->may_have_pointers)
5852 {
5853 if (vi->only_restrict_pointers)
5854 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5855 else
5856 make_copy_constraint (vi, nonlocal_id);
5857 }
5858 continue;
5859 }
5860
5861 if (POINTER_TYPE_P (TREE_TYPE (t))
5862 && TYPE_RESTRICT (TREE_TYPE (t)))
5863 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5864 else
5865 {
5866 for (; p; p = vi_next (p))
5867 {
5868 if (p->only_restrict_pointers)
5869 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5870 else if (p->may_have_pointers)
5871 make_constraint_from (p, nonlocal_id);
5872 }
5873 }
5874 }
5875
5876 /* Add a constraint for a result decl that is passed by reference. */
5877 if (DECL_RESULT (cfun->decl)
5878 && DECL_BY_REFERENCE (DECL_RESULT (cfun->decl)))
5879 {
5880 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (cfun->decl));
5881
5882 for (p = result_vi; p; p = vi_next (p))
5883 make_constraint_from (p, nonlocal_id);
5884 }
5885
5886 /* Add a constraint for the incoming static chain parameter. */
5887 if (cfun->static_chain_decl != NULL_TREE)
5888 {
5889 varinfo_t p, chain_vi = get_vi_for_tree (cfun->static_chain_decl);
5890
5891 for (p = chain_vi; p; p = vi_next (p))
5892 make_constraint_from (p, nonlocal_id);
5893 }
5894 }
5895
5896 /* Structure used to put solution bitmaps in a hashtable so they can
5897 be shared among variables with the same points-to set. */
5898
5899 typedef struct shared_bitmap_info
5900 {
5901 bitmap pt_vars;
5902 hashval_t hashcode;
5903 } *shared_bitmap_info_t;
5904 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
5905
5906 /* Shared_bitmap hashtable helpers. */
5907
5908 struct shared_bitmap_hasher : typed_free_remove <shared_bitmap_info>
5909 {
5910 typedef shared_bitmap_info value_type;
5911 typedef shared_bitmap_info compare_type;
5912 static inline hashval_t hash (const value_type *);
5913 static inline bool equal (const value_type *, const compare_type *);
5914 };
5915
5916 /* Hash function for a shared_bitmap_info_t */
5917
5918 inline hashval_t
5919 shared_bitmap_hasher::hash (const value_type *bi)
5920 {
5921 return bi->hashcode;
5922 }
5923
5924 /* Equality function for two shared_bitmap_info_t's. */
5925
5926 inline bool
5927 shared_bitmap_hasher::equal (const value_type *sbi1, const compare_type *sbi2)
5928 {
5929 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
5930 }
5931
5932 /* Shared_bitmap hashtable. */
5933
5934 static hash_table <shared_bitmap_hasher> shared_bitmap_table;
5935
5936 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5937 existing instance if there is one, NULL otherwise. */
5938
5939 static bitmap
5940 shared_bitmap_lookup (bitmap pt_vars)
5941 {
5942 shared_bitmap_info **slot;
5943 struct shared_bitmap_info sbi;
5944
5945 sbi.pt_vars = pt_vars;
5946 sbi.hashcode = bitmap_hash (pt_vars);
5947
5948 slot = shared_bitmap_table.find_slot_with_hash (&sbi, sbi.hashcode,
5949 NO_INSERT);
5950 if (!slot)
5951 return NULL;
5952 else
5953 return (*slot)->pt_vars;
5954 }
5955
5956
5957 /* Add a bitmap to the shared bitmap hashtable. */
5958
5959 static void
5960 shared_bitmap_add (bitmap pt_vars)
5961 {
5962 shared_bitmap_info **slot;
5963 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
5964
5965 sbi->pt_vars = pt_vars;
5966 sbi->hashcode = bitmap_hash (pt_vars);
5967
5968 slot = shared_bitmap_table.find_slot_with_hash (sbi, sbi->hashcode, INSERT);
5969 gcc_assert (!*slot);
5970 *slot = sbi;
5971 }
5972
5973
5974 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
5975
5976 static void
5977 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt)
5978 {
5979 unsigned int i;
5980 bitmap_iterator bi;
5981 varinfo_t escaped_vi = get_varinfo (find (escaped_id));
5982 bool everything_escaped
5983 = escaped_vi->solution && bitmap_bit_p (escaped_vi->solution, anything_id);
5984
5985 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
5986 {
5987 varinfo_t vi = get_varinfo (i);
5988
5989 /* The only artificial variables that are allowed in a may-alias
5990 set are heap variables. */
5991 if (vi->is_artificial_var && !vi->is_heap_var)
5992 continue;
5993
5994 if (everything_escaped
5995 || (escaped_vi->solution
5996 && bitmap_bit_p (escaped_vi->solution, i)))
5997 {
5998 pt->vars_contains_escaped = true;
5999 pt->vars_contains_escaped_heap = vi->is_heap_var;
6000 }
6001
6002 if (TREE_CODE (vi->decl) == VAR_DECL
6003 || TREE_CODE (vi->decl) == PARM_DECL
6004 || TREE_CODE (vi->decl) == RESULT_DECL)
6005 {
6006 /* If we are in IPA mode we will not recompute points-to
6007 sets after inlining so make sure they stay valid. */
6008 if (in_ipa_mode
6009 && !DECL_PT_UID_SET_P (vi->decl))
6010 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
6011
6012 /* Add the decl to the points-to set. Note that the points-to
6013 set contains global variables. */
6014 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
6015 if (vi->is_global_var)
6016 pt->vars_contains_nonlocal = true;
6017 }
6018 }
6019 }
6020
6021
6022 /* Compute the points-to solution *PT for the variable VI. */
6023
6024 static struct pt_solution
6025 find_what_var_points_to (varinfo_t orig_vi)
6026 {
6027 unsigned int i;
6028 bitmap_iterator bi;
6029 bitmap finished_solution;
6030 bitmap result;
6031 varinfo_t vi;
6032 void **slot;
6033 struct pt_solution *pt;
6034
6035 /* This variable may have been collapsed, let's get the real
6036 variable. */
6037 vi = get_varinfo (find (orig_vi->id));
6038
6039 /* See if we have already computed the solution and return it. */
6040 slot = pointer_map_insert (final_solutions, vi);
6041 if (*slot != NULL)
6042 return *(struct pt_solution *)*slot;
6043
6044 *slot = pt = XOBNEW (&final_solutions_obstack, struct pt_solution);
6045 memset (pt, 0, sizeof (struct pt_solution));
6046
6047 /* Translate artificial variables into SSA_NAME_PTR_INFO
6048 attributes. */
6049 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
6050 {
6051 varinfo_t vi = get_varinfo (i);
6052
6053 if (vi->is_artificial_var)
6054 {
6055 if (vi->id == nothing_id)
6056 pt->null = 1;
6057 else if (vi->id == escaped_id)
6058 {
6059 if (in_ipa_mode)
6060 pt->ipa_escaped = 1;
6061 else
6062 pt->escaped = 1;
6063 }
6064 else if (vi->id == nonlocal_id)
6065 pt->nonlocal = 1;
6066 else if (vi->is_heap_var)
6067 /* We represent heapvars in the points-to set properly. */
6068 ;
6069 else if (vi->id == readonly_id)
6070 /* Nobody cares. */
6071 ;
6072 else if (vi->id == anything_id
6073 || vi->id == integer_id)
6074 pt->anything = 1;
6075 }
6076 }
6077
6078 /* Instead of doing extra work, simply do not create
6079 elaborate points-to information for pt_anything pointers. */
6080 if (pt->anything)
6081 return *pt;
6082
6083 /* Share the final set of variables when possible. */
6084 finished_solution = BITMAP_GGC_ALLOC ();
6085 stats.points_to_sets_created++;
6086
6087 set_uids_in_ptset (finished_solution, vi->solution, pt);
6088 result = shared_bitmap_lookup (finished_solution);
6089 if (!result)
6090 {
6091 shared_bitmap_add (finished_solution);
6092 pt->vars = finished_solution;
6093 }
6094 else
6095 {
6096 pt->vars = result;
6097 bitmap_clear (finished_solution);
6098 }
6099
6100 return *pt;
6101 }
6102
6103 /* Given a pointer variable P, fill in its points-to set. */
6104
6105 static void
6106 find_what_p_points_to (tree p)
6107 {
6108 struct ptr_info_def *pi;
6109 tree lookup_p = p;
6110 varinfo_t vi;
6111
6112 /* For parameters, get at the points-to set for the actual parm
6113 decl. */
6114 if (TREE_CODE (p) == SSA_NAME
6115 && SSA_NAME_IS_DEFAULT_DEF (p)
6116 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
6117 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL))
6118 lookup_p = SSA_NAME_VAR (p);
6119
6120 vi = lookup_vi_for_tree (lookup_p);
6121 if (!vi)
6122 return;
6123
6124 pi = get_ptr_info (p);
6125 pi->pt = find_what_var_points_to (vi);
6126 }
6127
6128
6129 /* Query statistics for points-to solutions. */
6130
6131 static struct {
6132 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
6133 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
6134 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
6135 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
6136 } pta_stats;
6137
6138 void
6139 dump_pta_stats (FILE *s)
6140 {
6141 fprintf (s, "\nPTA query stats:\n");
6142 fprintf (s, " pt_solution_includes: "
6143 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6144 HOST_WIDE_INT_PRINT_DEC" queries\n",
6145 pta_stats.pt_solution_includes_no_alias,
6146 pta_stats.pt_solution_includes_no_alias
6147 + pta_stats.pt_solution_includes_may_alias);
6148 fprintf (s, " pt_solutions_intersect: "
6149 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6150 HOST_WIDE_INT_PRINT_DEC" queries\n",
6151 pta_stats.pt_solutions_intersect_no_alias,
6152 pta_stats.pt_solutions_intersect_no_alias
6153 + pta_stats.pt_solutions_intersect_may_alias);
6154 }
6155
6156
6157 /* Reset the points-to solution *PT to a conservative default
6158 (point to anything). */
6159
6160 void
6161 pt_solution_reset (struct pt_solution *pt)
6162 {
6163 memset (pt, 0, sizeof (struct pt_solution));
6164 pt->anything = true;
6165 }
6166
6167 /* Set the points-to solution *PT to point only to the variables
6168 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6169 global variables and VARS_CONTAINS_RESTRICT specifies whether
6170 it contains restrict tag variables. */
6171
6172 void
6173 pt_solution_set (struct pt_solution *pt, bitmap vars,
6174 bool vars_contains_nonlocal)
6175 {
6176 memset (pt, 0, sizeof (struct pt_solution));
6177 pt->vars = vars;
6178 pt->vars_contains_nonlocal = vars_contains_nonlocal;
6179 pt->vars_contains_escaped
6180 = (cfun->gimple_df->escaped.anything
6181 || bitmap_intersect_p (cfun->gimple_df->escaped.vars, vars));
6182 }
6183
6184 /* Set the points-to solution *PT to point only to the variable VAR. */
6185
6186 void
6187 pt_solution_set_var (struct pt_solution *pt, tree var)
6188 {
6189 memset (pt, 0, sizeof (struct pt_solution));
6190 pt->vars = BITMAP_GGC_ALLOC ();
6191 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
6192 pt->vars_contains_nonlocal = is_global_var (var);
6193 pt->vars_contains_escaped
6194 = (cfun->gimple_df->escaped.anything
6195 || bitmap_bit_p (cfun->gimple_df->escaped.vars, DECL_PT_UID (var)));
6196 }
6197
6198 /* Computes the union of the points-to solutions *DEST and *SRC and
6199 stores the result in *DEST. This changes the points-to bitmap
6200 of *DEST and thus may not be used if that might be shared.
6201 The points-to bitmap of *SRC and *DEST will not be shared after
6202 this function if they were not before. */
6203
6204 static void
6205 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6206 {
6207 dest->anything |= src->anything;
6208 if (dest->anything)
6209 {
6210 pt_solution_reset (dest);
6211 return;
6212 }
6213
6214 dest->nonlocal |= src->nonlocal;
6215 dest->escaped |= src->escaped;
6216 dest->ipa_escaped |= src->ipa_escaped;
6217 dest->null |= src->null;
6218 dest->vars_contains_nonlocal |= src->vars_contains_nonlocal;
6219 dest->vars_contains_escaped |= src->vars_contains_escaped;
6220 dest->vars_contains_escaped_heap |= src->vars_contains_escaped_heap;
6221 if (!src->vars)
6222 return;
6223
6224 if (!dest->vars)
6225 dest->vars = BITMAP_GGC_ALLOC ();
6226 bitmap_ior_into (dest->vars, src->vars);
6227 }
6228
6229 /* Return true if the points-to solution *PT is empty. */
6230
6231 bool
6232 pt_solution_empty_p (struct pt_solution *pt)
6233 {
6234 if (pt->anything
6235 || pt->nonlocal)
6236 return false;
6237
6238 if (pt->vars
6239 && !bitmap_empty_p (pt->vars))
6240 return false;
6241
6242 /* If the solution includes ESCAPED, check if that is empty. */
6243 if (pt->escaped
6244 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6245 return false;
6246
6247 /* If the solution includes ESCAPED, check if that is empty. */
6248 if (pt->ipa_escaped
6249 && !pt_solution_empty_p (&ipa_escaped_pt))
6250 return false;
6251
6252 return true;
6253 }
6254
6255 /* Return true if the points-to solution *PT only point to a single var, and
6256 return the var uid in *UID. */
6257
6258 bool
6259 pt_solution_singleton_p (struct pt_solution *pt, unsigned *uid)
6260 {
6261 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
6262 || pt->null || pt->vars == NULL
6263 || !bitmap_single_bit_set_p (pt->vars))
6264 return false;
6265
6266 *uid = bitmap_first_set_bit (pt->vars);
6267 return true;
6268 }
6269
6270 /* Return true if the points-to solution *PT includes global memory. */
6271
6272 bool
6273 pt_solution_includes_global (struct pt_solution *pt)
6274 {
6275 if (pt->anything
6276 || pt->nonlocal
6277 || pt->vars_contains_nonlocal
6278 /* The following is a hack to make the malloc escape hack work.
6279 In reality we'd need different sets for escaped-through-return
6280 and escaped-to-callees and passes would need to be updated. */
6281 || pt->vars_contains_escaped_heap)
6282 return true;
6283
6284 /* 'escaped' is also a placeholder so we have to look into it. */
6285 if (pt->escaped)
6286 return pt_solution_includes_global (&cfun->gimple_df->escaped);
6287
6288 if (pt->ipa_escaped)
6289 return pt_solution_includes_global (&ipa_escaped_pt);
6290
6291 /* ??? This predicate is not correct for the IPA-PTA solution
6292 as we do not properly distinguish between unit escape points
6293 and global variables. */
6294 if (cfun->gimple_df->ipa_pta)
6295 return true;
6296
6297 return false;
6298 }
6299
6300 /* Return true if the points-to solution *PT includes the variable
6301 declaration DECL. */
6302
6303 static bool
6304 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6305 {
6306 if (pt->anything)
6307 return true;
6308
6309 if (pt->nonlocal
6310 && is_global_var (decl))
6311 return true;
6312
6313 if (pt->vars
6314 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6315 return true;
6316
6317 /* If the solution includes ESCAPED, check it. */
6318 if (pt->escaped
6319 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6320 return true;
6321
6322 /* If the solution includes ESCAPED, check it. */
6323 if (pt->ipa_escaped
6324 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6325 return true;
6326
6327 return false;
6328 }
6329
6330 bool
6331 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6332 {
6333 bool res = pt_solution_includes_1 (pt, decl);
6334 if (res)
6335 ++pta_stats.pt_solution_includes_may_alias;
6336 else
6337 ++pta_stats.pt_solution_includes_no_alias;
6338 return res;
6339 }
6340
6341 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6342 intersection. */
6343
6344 static bool
6345 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
6346 {
6347 if (pt1->anything || pt2->anything)
6348 return true;
6349
6350 /* If either points to unknown global memory and the other points to
6351 any global memory they alias. */
6352 if ((pt1->nonlocal
6353 && (pt2->nonlocal
6354 || pt2->vars_contains_nonlocal))
6355 || (pt2->nonlocal
6356 && pt1->vars_contains_nonlocal))
6357 return true;
6358
6359 /* If either points to all escaped memory and the other points to
6360 any escaped memory they alias. */
6361 if ((pt1->escaped
6362 && (pt2->escaped
6363 || pt2->vars_contains_escaped))
6364 || (pt2->escaped
6365 && pt1->vars_contains_escaped))
6366 return true;
6367
6368 /* Check the escaped solution if required.
6369 ??? Do we need to check the local against the IPA escaped sets? */
6370 if ((pt1->ipa_escaped || pt2->ipa_escaped)
6371 && !pt_solution_empty_p (&ipa_escaped_pt))
6372 {
6373 /* If both point to escaped memory and that solution
6374 is not empty they alias. */
6375 if (pt1->ipa_escaped && pt2->ipa_escaped)
6376 return true;
6377
6378 /* If either points to escaped memory see if the escaped solution
6379 intersects with the other. */
6380 if ((pt1->ipa_escaped
6381 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
6382 || (pt2->ipa_escaped
6383 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
6384 return true;
6385 }
6386
6387 /* Now both pointers alias if their points-to solution intersects. */
6388 return (pt1->vars
6389 && pt2->vars
6390 && bitmap_intersect_p (pt1->vars, pt2->vars));
6391 }
6392
6393 bool
6394 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
6395 {
6396 bool res = pt_solutions_intersect_1 (pt1, pt2);
6397 if (res)
6398 ++pta_stats.pt_solutions_intersect_may_alias;
6399 else
6400 ++pta_stats.pt_solutions_intersect_no_alias;
6401 return res;
6402 }
6403
6404
6405 /* Dump points-to information to OUTFILE. */
6406
6407 static void
6408 dump_sa_points_to_info (FILE *outfile)
6409 {
6410 unsigned int i;
6411
6412 fprintf (outfile, "\nPoints-to sets\n\n");
6413
6414 if (dump_flags & TDF_STATS)
6415 {
6416 fprintf (outfile, "Stats:\n");
6417 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
6418 fprintf (outfile, "Non-pointer vars: %d\n",
6419 stats.nonpointer_vars);
6420 fprintf (outfile, "Statically unified vars: %d\n",
6421 stats.unified_vars_static);
6422 fprintf (outfile, "Dynamically unified vars: %d\n",
6423 stats.unified_vars_dynamic);
6424 fprintf (outfile, "Iterations: %d\n", stats.iterations);
6425 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
6426 fprintf (outfile, "Number of implicit edges: %d\n",
6427 stats.num_implicit_edges);
6428 }
6429
6430 for (i = 1; i < varmap.length (); i++)
6431 {
6432 varinfo_t vi = get_varinfo (i);
6433 if (!vi->may_have_pointers)
6434 continue;
6435 dump_solution_for_var (outfile, i);
6436 }
6437 }
6438
6439
6440 /* Debug points-to information to stderr. */
6441
6442 DEBUG_FUNCTION void
6443 debug_sa_points_to_info (void)
6444 {
6445 dump_sa_points_to_info (stderr);
6446 }
6447
6448
6449 /* Initialize the always-existing constraint variables for NULL
6450 ANYTHING, READONLY, and INTEGER */
6451
6452 static void
6453 init_base_vars (void)
6454 {
6455 struct constraint_expr lhs, rhs;
6456 varinfo_t var_anything;
6457 varinfo_t var_nothing;
6458 varinfo_t var_readonly;
6459 varinfo_t var_escaped;
6460 varinfo_t var_nonlocal;
6461 varinfo_t var_storedanything;
6462 varinfo_t var_integer;
6463
6464 /* Variable ID zero is reserved and should be NULL. */
6465 varmap.safe_push (NULL);
6466
6467 /* Create the NULL variable, used to represent that a variable points
6468 to NULL. */
6469 var_nothing = new_var_info (NULL_TREE, "NULL");
6470 gcc_assert (var_nothing->id == nothing_id);
6471 var_nothing->is_artificial_var = 1;
6472 var_nothing->offset = 0;
6473 var_nothing->size = ~0;
6474 var_nothing->fullsize = ~0;
6475 var_nothing->is_special_var = 1;
6476 var_nothing->may_have_pointers = 0;
6477 var_nothing->is_global_var = 0;
6478
6479 /* Create the ANYTHING variable, used to represent that a variable
6480 points to some unknown piece of memory. */
6481 var_anything = new_var_info (NULL_TREE, "ANYTHING");
6482 gcc_assert (var_anything->id == anything_id);
6483 var_anything->is_artificial_var = 1;
6484 var_anything->size = ~0;
6485 var_anything->offset = 0;
6486 var_anything->fullsize = ~0;
6487 var_anything->is_special_var = 1;
6488
6489 /* Anything points to anything. This makes deref constraints just
6490 work in the presence of linked list and other p = *p type loops,
6491 by saying that *ANYTHING = ANYTHING. */
6492 lhs.type = SCALAR;
6493 lhs.var = anything_id;
6494 lhs.offset = 0;
6495 rhs.type = ADDRESSOF;
6496 rhs.var = anything_id;
6497 rhs.offset = 0;
6498
6499 /* This specifically does not use process_constraint because
6500 process_constraint ignores all anything = anything constraints, since all
6501 but this one are redundant. */
6502 constraints.safe_push (new_constraint (lhs, rhs));
6503
6504 /* Create the READONLY variable, used to represent that a variable
6505 points to readonly memory. */
6506 var_readonly = new_var_info (NULL_TREE, "READONLY");
6507 gcc_assert (var_readonly->id == readonly_id);
6508 var_readonly->is_artificial_var = 1;
6509 var_readonly->offset = 0;
6510 var_readonly->size = ~0;
6511 var_readonly->fullsize = ~0;
6512 var_readonly->is_special_var = 1;
6513
6514 /* readonly memory points to anything, in order to make deref
6515 easier. In reality, it points to anything the particular
6516 readonly variable can point to, but we don't track this
6517 separately. */
6518 lhs.type = SCALAR;
6519 lhs.var = readonly_id;
6520 lhs.offset = 0;
6521 rhs.type = ADDRESSOF;
6522 rhs.var = readonly_id; /* FIXME */
6523 rhs.offset = 0;
6524 process_constraint (new_constraint (lhs, rhs));
6525
6526 /* Create the ESCAPED variable, used to represent the set of escaped
6527 memory. */
6528 var_escaped = new_var_info (NULL_TREE, "ESCAPED");
6529 gcc_assert (var_escaped->id == escaped_id);
6530 var_escaped->is_artificial_var = 1;
6531 var_escaped->offset = 0;
6532 var_escaped->size = ~0;
6533 var_escaped->fullsize = ~0;
6534 var_escaped->is_special_var = 0;
6535
6536 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6537 memory. */
6538 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL");
6539 gcc_assert (var_nonlocal->id == nonlocal_id);
6540 var_nonlocal->is_artificial_var = 1;
6541 var_nonlocal->offset = 0;
6542 var_nonlocal->size = ~0;
6543 var_nonlocal->fullsize = ~0;
6544 var_nonlocal->is_special_var = 1;
6545
6546 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6547 lhs.type = SCALAR;
6548 lhs.var = escaped_id;
6549 lhs.offset = 0;
6550 rhs.type = DEREF;
6551 rhs.var = escaped_id;
6552 rhs.offset = 0;
6553 process_constraint (new_constraint (lhs, rhs));
6554
6555 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6556 whole variable escapes. */
6557 lhs.type = SCALAR;
6558 lhs.var = escaped_id;
6559 lhs.offset = 0;
6560 rhs.type = SCALAR;
6561 rhs.var = escaped_id;
6562 rhs.offset = UNKNOWN_OFFSET;
6563 process_constraint (new_constraint (lhs, rhs));
6564
6565 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6566 everything pointed to by escaped points to what global memory can
6567 point to. */
6568 lhs.type = DEREF;
6569 lhs.var = escaped_id;
6570 lhs.offset = 0;
6571 rhs.type = SCALAR;
6572 rhs.var = nonlocal_id;
6573 rhs.offset = 0;
6574 process_constraint (new_constraint (lhs, rhs));
6575
6576 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6577 global memory may point to global memory and escaped memory. */
6578 lhs.type = SCALAR;
6579 lhs.var = nonlocal_id;
6580 lhs.offset = 0;
6581 rhs.type = ADDRESSOF;
6582 rhs.var = nonlocal_id;
6583 rhs.offset = 0;
6584 process_constraint (new_constraint (lhs, rhs));
6585 rhs.type = ADDRESSOF;
6586 rhs.var = escaped_id;
6587 rhs.offset = 0;
6588 process_constraint (new_constraint (lhs, rhs));
6589
6590 /* Create the STOREDANYTHING variable, used to represent the set of
6591 variables stored to *ANYTHING. */
6592 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING");
6593 gcc_assert (var_storedanything->id == storedanything_id);
6594 var_storedanything->is_artificial_var = 1;
6595 var_storedanything->offset = 0;
6596 var_storedanything->size = ~0;
6597 var_storedanything->fullsize = ~0;
6598 var_storedanything->is_special_var = 0;
6599
6600 /* Create the INTEGER variable, used to represent that a variable points
6601 to what an INTEGER "points to". */
6602 var_integer = new_var_info (NULL_TREE, "INTEGER");
6603 gcc_assert (var_integer->id == integer_id);
6604 var_integer->is_artificial_var = 1;
6605 var_integer->size = ~0;
6606 var_integer->fullsize = ~0;
6607 var_integer->offset = 0;
6608 var_integer->is_special_var = 1;
6609
6610 /* INTEGER = ANYTHING, because we don't know where a dereference of
6611 a random integer will point to. */
6612 lhs.type = SCALAR;
6613 lhs.var = integer_id;
6614 lhs.offset = 0;
6615 rhs.type = ADDRESSOF;
6616 rhs.var = anything_id;
6617 rhs.offset = 0;
6618 process_constraint (new_constraint (lhs, rhs));
6619 }
6620
6621 /* Initialize things necessary to perform PTA */
6622
6623 static void
6624 init_alias_vars (void)
6625 {
6626 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
6627
6628 bitmap_obstack_initialize (&pta_obstack);
6629 bitmap_obstack_initialize (&oldpta_obstack);
6630 bitmap_obstack_initialize (&predbitmap_obstack);
6631
6632 constraint_pool = create_alloc_pool ("Constraint pool",
6633 sizeof (struct constraint), 30);
6634 variable_info_pool = create_alloc_pool ("Variable info pool",
6635 sizeof (struct variable_info), 30);
6636 constraints.create (8);
6637 varmap.create (8);
6638 vi_for_tree = pointer_map_create ();
6639 call_stmt_vars = pointer_map_create ();
6640
6641 memset (&stats, 0, sizeof (stats));
6642 shared_bitmap_table.create (511);
6643 init_base_vars ();
6644
6645 gcc_obstack_init (&fake_var_decl_obstack);
6646
6647 final_solutions = pointer_map_create ();
6648 gcc_obstack_init (&final_solutions_obstack);
6649 }
6650
6651 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6652 predecessor edges. */
6653
6654 static void
6655 remove_preds_and_fake_succs (constraint_graph_t graph)
6656 {
6657 unsigned int i;
6658
6659 /* Clear the implicit ref and address nodes from the successor
6660 lists. */
6661 for (i = 1; i < FIRST_REF_NODE; i++)
6662 {
6663 if (graph->succs[i])
6664 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
6665 FIRST_REF_NODE * 2);
6666 }
6667
6668 /* Free the successor list for the non-ref nodes. */
6669 for (i = FIRST_REF_NODE + 1; i < graph->size; i++)
6670 {
6671 if (graph->succs[i])
6672 BITMAP_FREE (graph->succs[i]);
6673 }
6674
6675 /* Now reallocate the size of the successor list as, and blow away
6676 the predecessor bitmaps. */
6677 graph->size = varmap.length ();
6678 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
6679
6680 free (graph->implicit_preds);
6681 graph->implicit_preds = NULL;
6682 free (graph->preds);
6683 graph->preds = NULL;
6684 bitmap_obstack_release (&predbitmap_obstack);
6685 }
6686
6687 /* Solve the constraint set. */
6688
6689 static void
6690 solve_constraints (void)
6691 {
6692 struct scc_info *si;
6693
6694 if (dump_file)
6695 fprintf (dump_file,
6696 "\nCollapsing static cycles and doing variable "
6697 "substitution\n");
6698
6699 init_graph (varmap.length () * 2);
6700
6701 if (dump_file)
6702 fprintf (dump_file, "Building predecessor graph\n");
6703 build_pred_graph ();
6704
6705 if (dump_file)
6706 fprintf (dump_file, "Detecting pointer and location "
6707 "equivalences\n");
6708 si = perform_var_substitution (graph);
6709
6710 if (dump_file)
6711 fprintf (dump_file, "Rewriting constraints and unifying "
6712 "variables\n");
6713 rewrite_constraints (graph, si);
6714
6715 build_succ_graph ();
6716
6717 free_var_substitution_info (si);
6718
6719 /* Attach complex constraints to graph nodes. */
6720 move_complex_constraints (graph);
6721
6722 if (dump_file)
6723 fprintf (dump_file, "Uniting pointer but not location equivalent "
6724 "variables\n");
6725 unite_pointer_equivalences (graph);
6726
6727 if (dump_file)
6728 fprintf (dump_file, "Finding indirect cycles\n");
6729 find_indirect_cycles (graph);
6730
6731 /* Implicit nodes and predecessors are no longer necessary at this
6732 point. */
6733 remove_preds_and_fake_succs (graph);
6734
6735 if (dump_file && (dump_flags & TDF_GRAPH))
6736 {
6737 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
6738 "in dot format:\n");
6739 dump_constraint_graph (dump_file);
6740 fprintf (dump_file, "\n\n");
6741 }
6742
6743 if (dump_file)
6744 fprintf (dump_file, "Solving graph\n");
6745
6746 solve_graph (graph);
6747
6748 if (dump_file && (dump_flags & TDF_GRAPH))
6749 {
6750 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
6751 "in dot format:\n");
6752 dump_constraint_graph (dump_file);
6753 fprintf (dump_file, "\n\n");
6754 }
6755
6756 if (dump_file)
6757 dump_sa_points_to_info (dump_file);
6758 }
6759
6760 /* Create points-to sets for the current function. See the comments
6761 at the start of the file for an algorithmic overview. */
6762
6763 static void
6764 compute_points_to_sets (void)
6765 {
6766 basic_block bb;
6767 unsigned i;
6768 varinfo_t vi;
6769
6770 timevar_push (TV_TREE_PTA);
6771
6772 init_alias_vars ();
6773
6774 intra_create_variable_infos ();
6775
6776 /* Now walk all statements and build the constraint set. */
6777 FOR_EACH_BB_FN (bb, cfun)
6778 {
6779 gimple_stmt_iterator gsi;
6780
6781 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6782 {
6783 gimple phi = gsi_stmt (gsi);
6784
6785 if (! virtual_operand_p (gimple_phi_result (phi)))
6786 find_func_aliases (phi);
6787 }
6788
6789 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6790 {
6791 gimple stmt = gsi_stmt (gsi);
6792
6793 find_func_aliases (stmt);
6794 }
6795 }
6796
6797 if (dump_file)
6798 {
6799 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
6800 dump_constraints (dump_file, 0);
6801 }
6802
6803 /* From the constraints compute the points-to sets. */
6804 solve_constraints ();
6805
6806 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6807 cfun->gimple_df->escaped = find_what_var_points_to (get_varinfo (escaped_id));
6808
6809 /* Make sure the ESCAPED solution (which is used as placeholder in
6810 other solutions) does not reference itself. This simplifies
6811 points-to solution queries. */
6812 cfun->gimple_df->escaped.escaped = 0;
6813
6814 /* Compute the points-to sets for pointer SSA_NAMEs. */
6815 for (i = 0; i < num_ssa_names; ++i)
6816 {
6817 tree ptr = ssa_name (i);
6818 if (ptr
6819 && POINTER_TYPE_P (TREE_TYPE (ptr)))
6820 find_what_p_points_to (ptr);
6821 }
6822
6823 /* Compute the call-used/clobbered sets. */
6824 FOR_EACH_BB_FN (bb, cfun)
6825 {
6826 gimple_stmt_iterator gsi;
6827
6828 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6829 {
6830 gimple stmt = gsi_stmt (gsi);
6831 struct pt_solution *pt;
6832 if (!is_gimple_call (stmt))
6833 continue;
6834
6835 pt = gimple_call_use_set (stmt);
6836 if (gimple_call_flags (stmt) & ECF_CONST)
6837 memset (pt, 0, sizeof (struct pt_solution));
6838 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
6839 {
6840 *pt = find_what_var_points_to (vi);
6841 /* Escaped (and thus nonlocal) variables are always
6842 implicitly used by calls. */
6843 /* ??? ESCAPED can be empty even though NONLOCAL
6844 always escaped. */
6845 pt->nonlocal = 1;
6846 pt->escaped = 1;
6847 }
6848 else
6849 {
6850 /* If there is nothing special about this call then
6851 we have made everything that is used also escape. */
6852 *pt = cfun->gimple_df->escaped;
6853 pt->nonlocal = 1;
6854 }
6855
6856 pt = gimple_call_clobber_set (stmt);
6857 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
6858 memset (pt, 0, sizeof (struct pt_solution));
6859 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
6860 {
6861 *pt = find_what_var_points_to (vi);
6862 /* Escaped (and thus nonlocal) variables are always
6863 implicitly clobbered by calls. */
6864 /* ??? ESCAPED can be empty even though NONLOCAL
6865 always escaped. */
6866 pt->nonlocal = 1;
6867 pt->escaped = 1;
6868 }
6869 else
6870 {
6871 /* If there is nothing special about this call then
6872 we have made everything that is used also escape. */
6873 *pt = cfun->gimple_df->escaped;
6874 pt->nonlocal = 1;
6875 }
6876 }
6877 }
6878
6879 timevar_pop (TV_TREE_PTA);
6880 }
6881
6882
6883 /* Delete created points-to sets. */
6884
6885 static void
6886 delete_points_to_sets (void)
6887 {
6888 unsigned int i;
6889
6890 shared_bitmap_table.dispose ();
6891 if (dump_file && (dump_flags & TDF_STATS))
6892 fprintf (dump_file, "Points to sets created:%d\n",
6893 stats.points_to_sets_created);
6894
6895 pointer_map_destroy (vi_for_tree);
6896 pointer_map_destroy (call_stmt_vars);
6897 bitmap_obstack_release (&pta_obstack);
6898 constraints.release ();
6899
6900 for (i = 0; i < graph->size; i++)
6901 graph->complex[i].release ();
6902 free (graph->complex);
6903
6904 free (graph->rep);
6905 free (graph->succs);
6906 free (graph->pe);
6907 free (graph->pe_rep);
6908 free (graph->indirect_cycles);
6909 free (graph);
6910
6911 varmap.release ();
6912 free_alloc_pool (variable_info_pool);
6913 free_alloc_pool (constraint_pool);
6914
6915 obstack_free (&fake_var_decl_obstack, NULL);
6916
6917 pointer_map_destroy (final_solutions);
6918 obstack_free (&final_solutions_obstack, NULL);
6919 }
6920
6921
6922 /* Compute points-to information for every SSA_NAME pointer in the
6923 current function and compute the transitive closure of escaped
6924 variables to re-initialize the call-clobber states of local variables. */
6925
6926 unsigned int
6927 compute_may_aliases (void)
6928 {
6929 if (cfun->gimple_df->ipa_pta)
6930 {
6931 if (dump_file)
6932 {
6933 fprintf (dump_file, "\nNot re-computing points-to information "
6934 "because IPA points-to information is available.\n\n");
6935
6936 /* But still dump what we have remaining it. */
6937 dump_alias_info (dump_file);
6938 }
6939
6940 return 0;
6941 }
6942
6943 /* For each pointer P_i, determine the sets of variables that P_i may
6944 point-to. Compute the reachability set of escaped and call-used
6945 variables. */
6946 compute_points_to_sets ();
6947
6948 /* Debugging dumps. */
6949 if (dump_file)
6950 dump_alias_info (dump_file);
6951
6952 /* Deallocate memory used by aliasing data structures and the internal
6953 points-to solution. */
6954 delete_points_to_sets ();
6955
6956 gcc_assert (!need_ssa_update_p (cfun));
6957
6958 return 0;
6959 }
6960
6961 static bool
6962 gate_tree_pta (void)
6963 {
6964 return flag_tree_pta;
6965 }
6966
6967 /* A dummy pass to cause points-to information to be computed via
6968 TODO_rebuild_alias. */
6969
6970 namespace {
6971
6972 const pass_data pass_data_build_alias =
6973 {
6974 GIMPLE_PASS, /* type */
6975 "alias", /* name */
6976 OPTGROUP_NONE, /* optinfo_flags */
6977 true, /* has_gate */
6978 false, /* has_execute */
6979 TV_NONE, /* tv_id */
6980 ( PROP_cfg | PROP_ssa ), /* properties_required */
6981 0, /* properties_provided */
6982 0, /* properties_destroyed */
6983 0, /* todo_flags_start */
6984 TODO_rebuild_alias, /* todo_flags_finish */
6985 };
6986
6987 class pass_build_alias : public gimple_opt_pass
6988 {
6989 public:
6990 pass_build_alias (gcc::context *ctxt)
6991 : gimple_opt_pass (pass_data_build_alias, ctxt)
6992 {}
6993
6994 /* opt_pass methods: */
6995 bool gate () { return gate_tree_pta (); }
6996
6997 }; // class pass_build_alias
6998
6999 } // anon namespace
7000
7001 gimple_opt_pass *
7002 make_pass_build_alias (gcc::context *ctxt)
7003 {
7004 return new pass_build_alias (ctxt);
7005 }
7006
7007 /* A dummy pass to cause points-to information to be computed via
7008 TODO_rebuild_alias. */
7009
7010 namespace {
7011
7012 const pass_data pass_data_build_ealias =
7013 {
7014 GIMPLE_PASS, /* type */
7015 "ealias", /* name */
7016 OPTGROUP_NONE, /* optinfo_flags */
7017 true, /* has_gate */
7018 false, /* has_execute */
7019 TV_NONE, /* tv_id */
7020 ( PROP_cfg | PROP_ssa ), /* properties_required */
7021 0, /* properties_provided */
7022 0, /* properties_destroyed */
7023 0, /* todo_flags_start */
7024 TODO_rebuild_alias, /* todo_flags_finish */
7025 };
7026
7027 class pass_build_ealias : public gimple_opt_pass
7028 {
7029 public:
7030 pass_build_ealias (gcc::context *ctxt)
7031 : gimple_opt_pass (pass_data_build_ealias, ctxt)
7032 {}
7033
7034 /* opt_pass methods: */
7035 bool gate () { return gate_tree_pta (); }
7036
7037 }; // class pass_build_ealias
7038
7039 } // anon namespace
7040
7041 gimple_opt_pass *
7042 make_pass_build_ealias (gcc::context *ctxt)
7043 {
7044 return new pass_build_ealias (ctxt);
7045 }
7046
7047
7048 /* Return true if we should execute IPA PTA. */
7049 static bool
7050 gate_ipa_pta (void)
7051 {
7052 return (optimize
7053 && flag_ipa_pta
7054 /* Don't bother doing anything if the program has errors. */
7055 && !seen_error ());
7056 }
7057
7058 /* IPA PTA solutions for ESCAPED. */
7059 struct pt_solution ipa_escaped_pt
7060 = { true, false, false, false, false, false, false, false, NULL };
7061
7062 /* Associate node with varinfo DATA. Worker for
7063 cgraph_for_node_and_aliases. */
7064 static bool
7065 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
7066 {
7067 if ((node->alias || node->thunk.thunk_p)
7068 && node->analyzed)
7069 insert_vi_for_tree (node->decl, (varinfo_t)data);
7070 return false;
7071 }
7072
7073 /* Execute the driver for IPA PTA. */
7074 static unsigned int
7075 ipa_pta_execute (void)
7076 {
7077 struct cgraph_node *node;
7078 varpool_node *var;
7079 int from;
7080
7081 in_ipa_mode = 1;
7082
7083 init_alias_vars ();
7084
7085 if (dump_file && (dump_flags & TDF_DETAILS))
7086 {
7087 dump_symtab (dump_file);
7088 fprintf (dump_file, "\n");
7089 }
7090
7091 /* Build the constraints. */
7092 FOR_EACH_DEFINED_FUNCTION (node)
7093 {
7094 varinfo_t vi;
7095 /* Nodes without a body are not interesting. Especially do not
7096 visit clones at this point for now - we get duplicate decls
7097 there for inline clones at least. */
7098 if (!cgraph_function_with_gimple_body_p (node) || node->clone_of)
7099 continue;
7100 cgraph_get_body (node);
7101
7102 gcc_assert (!node->clone_of);
7103
7104 vi = create_function_info_for (node->decl,
7105 alias_get_name (node->decl));
7106 cgraph_for_node_and_aliases (node, associate_varinfo_to_alias, vi, true);
7107 }
7108
7109 /* Create constraints for global variables and their initializers. */
7110 FOR_EACH_VARIABLE (var)
7111 {
7112 if (var->alias && var->analyzed)
7113 continue;
7114
7115 get_vi_for_tree (var->decl);
7116 }
7117
7118 if (dump_file)
7119 {
7120 fprintf (dump_file,
7121 "Generating constraints for global initializers\n\n");
7122 dump_constraints (dump_file, 0);
7123 fprintf (dump_file, "\n");
7124 }
7125 from = constraints.length ();
7126
7127 FOR_EACH_DEFINED_FUNCTION (node)
7128 {
7129 struct function *func;
7130 basic_block bb;
7131
7132 /* Nodes without a body are not interesting. */
7133 if (!cgraph_function_with_gimple_body_p (node) || node->clone_of)
7134 continue;
7135
7136 if (dump_file)
7137 {
7138 fprintf (dump_file,
7139 "Generating constraints for %s", node->name ());
7140 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
7141 fprintf (dump_file, " (%s)",
7142 IDENTIFIER_POINTER
7143 (DECL_ASSEMBLER_NAME (node->decl)));
7144 fprintf (dump_file, "\n");
7145 }
7146
7147 func = DECL_STRUCT_FUNCTION (node->decl);
7148 push_cfun (func);
7149
7150 /* For externally visible or attribute used annotated functions use
7151 local constraints for their arguments.
7152 For local functions we see all callers and thus do not need initial
7153 constraints for parameters. */
7154 if (node->used_from_other_partition
7155 || node->externally_visible
7156 || node->force_output)
7157 {
7158 intra_create_variable_infos ();
7159
7160 /* We also need to make function return values escape. Nothing
7161 escapes by returning from main though. */
7162 if (!MAIN_NAME_P (DECL_NAME (node->decl)))
7163 {
7164 varinfo_t fi, rvi;
7165 fi = lookup_vi_for_tree (node->decl);
7166 rvi = first_vi_for_offset (fi, fi_result);
7167 if (rvi && rvi->offset == fi_result)
7168 {
7169 struct constraint_expr includes;
7170 struct constraint_expr var;
7171 includes.var = escaped_id;
7172 includes.offset = 0;
7173 includes.type = SCALAR;
7174 var.var = rvi->id;
7175 var.offset = 0;
7176 var.type = SCALAR;
7177 process_constraint (new_constraint (includes, var));
7178 }
7179 }
7180 }
7181
7182 /* Build constriants for the function body. */
7183 FOR_EACH_BB_FN (bb, func)
7184 {
7185 gimple_stmt_iterator gsi;
7186
7187 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7188 gsi_next (&gsi))
7189 {
7190 gimple phi = gsi_stmt (gsi);
7191
7192 if (! virtual_operand_p (gimple_phi_result (phi)))
7193 find_func_aliases (phi);
7194 }
7195
7196 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7197 {
7198 gimple stmt = gsi_stmt (gsi);
7199
7200 find_func_aliases (stmt);
7201 find_func_clobbers (stmt);
7202 }
7203 }
7204
7205 pop_cfun ();
7206
7207 if (dump_file)
7208 {
7209 fprintf (dump_file, "\n");
7210 dump_constraints (dump_file, from);
7211 fprintf (dump_file, "\n");
7212 }
7213 from = constraints.length ();
7214 }
7215
7216 /* From the constraints compute the points-to sets. */
7217 solve_constraints ();
7218
7219 /* Compute the global points-to sets for ESCAPED.
7220 ??? Note that the computed escape set is not correct
7221 for the whole unit as we fail to consider graph edges to
7222 externally visible functions. */
7223 ipa_escaped_pt = find_what_var_points_to (get_varinfo (escaped_id));
7224
7225 /* Make sure the ESCAPED solution (which is used as placeholder in
7226 other solutions) does not reference itself. This simplifies
7227 points-to solution queries. */
7228 ipa_escaped_pt.ipa_escaped = 0;
7229
7230 /* Assign the points-to sets to the SSA names in the unit. */
7231 FOR_EACH_DEFINED_FUNCTION (node)
7232 {
7233 tree ptr;
7234 struct function *fn;
7235 unsigned i;
7236 varinfo_t fi;
7237 basic_block bb;
7238 struct pt_solution uses, clobbers;
7239 struct cgraph_edge *e;
7240
7241 /* Nodes without a body are not interesting. */
7242 if (!cgraph_function_with_gimple_body_p (node) || node->clone_of)
7243 continue;
7244
7245 fn = DECL_STRUCT_FUNCTION (node->decl);
7246
7247 /* Compute the points-to sets for pointer SSA_NAMEs. */
7248 FOR_EACH_VEC_ELT (*fn->gimple_df->ssa_names, i, ptr)
7249 {
7250 if (ptr
7251 && POINTER_TYPE_P (TREE_TYPE (ptr)))
7252 find_what_p_points_to (ptr);
7253 }
7254
7255 /* Compute the call-use and call-clobber sets for all direct calls. */
7256 fi = lookup_vi_for_tree (node->decl);
7257 gcc_assert (fi->is_fn_info);
7258 clobbers
7259 = find_what_var_points_to (first_vi_for_offset (fi, fi_clobbers));
7260 uses = find_what_var_points_to (first_vi_for_offset (fi, fi_uses));
7261 for (e = node->callers; e; e = e->next_caller)
7262 {
7263 if (!e->call_stmt)
7264 continue;
7265
7266 *gimple_call_clobber_set (e->call_stmt) = clobbers;
7267 *gimple_call_use_set (e->call_stmt) = uses;
7268 }
7269
7270 /* Compute the call-use and call-clobber sets for indirect calls
7271 and calls to external functions. */
7272 FOR_EACH_BB_FN (bb, fn)
7273 {
7274 gimple_stmt_iterator gsi;
7275
7276 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7277 {
7278 gimple stmt = gsi_stmt (gsi);
7279 struct pt_solution *pt;
7280 varinfo_t vi;
7281 tree decl;
7282
7283 if (!is_gimple_call (stmt))
7284 continue;
7285
7286 /* Handle direct calls to external functions. */
7287 decl = gimple_call_fndecl (stmt);
7288 if (decl
7289 && (!(fi = lookup_vi_for_tree (decl))
7290 || !fi->is_fn_info))
7291 {
7292 pt = gimple_call_use_set (stmt);
7293 if (gimple_call_flags (stmt) & ECF_CONST)
7294 memset (pt, 0, sizeof (struct pt_solution));
7295 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7296 {
7297 *pt = find_what_var_points_to (vi);
7298 /* Escaped (and thus nonlocal) variables are always
7299 implicitly used by calls. */
7300 /* ??? ESCAPED can be empty even though NONLOCAL
7301 always escaped. */
7302 pt->nonlocal = 1;
7303 pt->ipa_escaped = 1;
7304 }
7305 else
7306 {
7307 /* If there is nothing special about this call then
7308 we have made everything that is used also escape. */
7309 *pt = ipa_escaped_pt;
7310 pt->nonlocal = 1;
7311 }
7312
7313 pt = gimple_call_clobber_set (stmt);
7314 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7315 memset (pt, 0, sizeof (struct pt_solution));
7316 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7317 {
7318 *pt = find_what_var_points_to (vi);
7319 /* Escaped (and thus nonlocal) variables are always
7320 implicitly clobbered by calls. */
7321 /* ??? ESCAPED can be empty even though NONLOCAL
7322 always escaped. */
7323 pt->nonlocal = 1;
7324 pt->ipa_escaped = 1;
7325 }
7326 else
7327 {
7328 /* If there is nothing special about this call then
7329 we have made everything that is used also escape. */
7330 *pt = ipa_escaped_pt;
7331 pt->nonlocal = 1;
7332 }
7333 }
7334
7335 /* Handle indirect calls. */
7336 if (!decl
7337 && (fi = get_fi_for_callee (stmt)))
7338 {
7339 /* We need to accumulate all clobbers/uses of all possible
7340 callees. */
7341 fi = get_varinfo (find (fi->id));
7342 /* If we cannot constrain the set of functions we'll end up
7343 calling we end up using/clobbering everything. */
7344 if (bitmap_bit_p (fi->solution, anything_id)
7345 || bitmap_bit_p (fi->solution, nonlocal_id)
7346 || bitmap_bit_p (fi->solution, escaped_id))
7347 {
7348 pt_solution_reset (gimple_call_clobber_set (stmt));
7349 pt_solution_reset (gimple_call_use_set (stmt));
7350 }
7351 else
7352 {
7353 bitmap_iterator bi;
7354 unsigned i;
7355 struct pt_solution *uses, *clobbers;
7356
7357 uses = gimple_call_use_set (stmt);
7358 clobbers = gimple_call_clobber_set (stmt);
7359 memset (uses, 0, sizeof (struct pt_solution));
7360 memset (clobbers, 0, sizeof (struct pt_solution));
7361 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
7362 {
7363 struct pt_solution sol;
7364
7365 vi = get_varinfo (i);
7366 if (!vi->is_fn_info)
7367 {
7368 /* ??? We could be more precise here? */
7369 uses->nonlocal = 1;
7370 uses->ipa_escaped = 1;
7371 clobbers->nonlocal = 1;
7372 clobbers->ipa_escaped = 1;
7373 continue;
7374 }
7375
7376 if (!uses->anything)
7377 {
7378 sol = find_what_var_points_to
7379 (first_vi_for_offset (vi, fi_uses));
7380 pt_solution_ior_into (uses, &sol);
7381 }
7382 if (!clobbers->anything)
7383 {
7384 sol = find_what_var_points_to
7385 (first_vi_for_offset (vi, fi_clobbers));
7386 pt_solution_ior_into (clobbers, &sol);
7387 }
7388 }
7389 }
7390 }
7391 }
7392 }
7393
7394 fn->gimple_df->ipa_pta = true;
7395 }
7396
7397 delete_points_to_sets ();
7398
7399 in_ipa_mode = 0;
7400
7401 return 0;
7402 }
7403
7404 namespace {
7405
7406 const pass_data pass_data_ipa_pta =
7407 {
7408 SIMPLE_IPA_PASS, /* type */
7409 "pta", /* name */
7410 OPTGROUP_NONE, /* optinfo_flags */
7411 true, /* has_gate */
7412 true, /* has_execute */
7413 TV_IPA_PTA, /* tv_id */
7414 0, /* properties_required */
7415 0, /* properties_provided */
7416 0, /* properties_destroyed */
7417 0, /* todo_flags_start */
7418 0, /* todo_flags_finish */
7419 };
7420
7421 class pass_ipa_pta : public simple_ipa_opt_pass
7422 {
7423 public:
7424 pass_ipa_pta (gcc::context *ctxt)
7425 : simple_ipa_opt_pass (pass_data_ipa_pta, ctxt)
7426 {}
7427
7428 /* opt_pass methods: */
7429 bool gate () { return gate_ipa_pta (); }
7430 unsigned int execute () { return ipa_pta_execute (); }
7431
7432 }; // class pass_ipa_pta
7433
7434 } // anon namespace
7435
7436 simple_ipa_opt_pass *
7437 make_pass_ipa_pta (gcc::context *ctxt)
7438 {
7439 return new pass_ipa_pta (ctxt);
7440 }