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