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