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