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