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