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