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