Debian Codegraph @

gcc/tree-ssa-structalias.cc

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