]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-structalias.c
Remove trailing white spaces.
[thirdparty/gcc.git] / gcc / tree-ssa-structalias.c
CommitLineData
910fdc79 1/* Tree based points-to analysis
66647d44 2 Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
910fdc79
DB
3 Contributed by Daniel Berlin <dberlin@dberlin.org>
4
9dcd6f09 5 This file is part of GCC.
910fdc79 6
9dcd6f09
NC
7 GCC is free software; you can redistribute it and/or modify
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
910fdc79 11
9dcd6f09
NC
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
910fdc79 16
9dcd6f09
NC
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
910fdc79
DB
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
25#include "ggc.h"
26#include "obstack.h"
27#include "bitmap.h"
910fdc79
DB
28#include "flags.h"
29#include "rtl.h"
30#include "tm_p.h"
31#include "hard-reg-set.h"
32#include "basic-block.h"
33#include "output.h"
910fdc79 34#include "tree.h"
910fdc79
DB
35#include "tree-flow.h"
36#include "tree-inline.h"
37#include "varray.h"
ce1b6498
RG
38#include "diagnostic.h"
39#include "toplev.h"
726a989a 40#include "gimple.h"
910fdc79
DB
41#include "hashtab.h"
42#include "function.h"
43#include "cgraph.h"
44#include "tree-pass.h"
45#include "timevar.h"
46#include "alloc-pool.h"
47#include "splay-tree.h"
a916f21d 48#include "params.h"
4ee00913 49#include "cgraph.h"
c58936b6 50#include "alias.h"
38635499 51#include "pointer-set.h"
910fdc79
DB
52
53/* The idea behind this analyzer is to generate set constraints from the
54 program, then solve the resulting constraints in order to generate the
c58936b6 55 points-to sets.
910fdc79
DB
56
57 Set constraints are a way of modeling program analysis problems that
58 involve sets. They consist of an inclusion constraint language,
59 describing the variables (each variable is a set) and operations that
60 are involved on the variables, and a set of rules that derive facts
61 from these operations. To solve a system of set constraints, you derive
62 all possible facts under the rules, which gives you the correct sets
63 as a consequence.
64
65 See "Efficient Field-sensitive pointer analysis for C" by "David
66 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
67 http://citeseer.ist.psu.edu/pearce04efficient.html
68
69 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
70 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
c58936b6
DB
71 http://citeseer.ist.psu.edu/heintze01ultrafast.html
72
73 There are three types of real constraint expressions, DEREF,
3e5937d7 74 ADDRESSOF, and SCALAR. Each constraint expression consists
c58936b6 75 of a constraint type, a variable, and an offset.
910fdc79 76
910fdc79
DB
77 SCALAR is a constraint expression type used to represent x, whether
78 it appears on the LHS or the RHS of a statement.
79 DEREF is a constraint expression type used to represent *x, whether
c58936b6 80 it appears on the LHS or the RHS of a statement.
910fdc79 81 ADDRESSOF is a constraint expression used to represent &x, whether
607fb860 82 it appears on the LHS or the RHS of a statement.
c58936b6 83
910fdc79
DB
84 Each pointer variable in the program is assigned an integer id, and
85 each field of a structure variable is assigned an integer id as well.
c58936b6 86
910fdc79
DB
87 Structure variables are linked to their list of fields through a "next
88 field" in each variable that points to the next field in offset
c58936b6
DB
89 order.
90 Each variable for a structure field has
910fdc79
DB
91
92 1. "size", that tells the size in bits of that field.
93 2. "fullsize, that tells the size in bits of the entire structure.
94 3. "offset", that tells the offset in bits from the beginning of the
95 structure to this field.
96
c58936b6 97 Thus,
910fdc79
DB
98 struct f
99 {
100 int a;
101 int b;
102 } foo;
103 int *bar;
104
105 looks like
106
107 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
108 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
109 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
110
c58936b6 111
910fdc79
DB
112 In order to solve the system of set constraints, the following is
113 done:
114
115 1. Each constraint variable x has a solution set associated with it,
116 Sol(x).
c58936b6 117
910fdc79
DB
118 2. Constraints are separated into direct, copy, and complex.
119 Direct constraints are ADDRESSOF constraints that require no extra
120 processing, such as P = &Q
121 Copy constraints are those of the form P = Q.
2941f691
DB
122 Complex constraints are all the constraints involving dereferences
123 and offsets (including offsetted copies).
c58936b6 124
910fdc79 125 3. All direct constraints of the form P = &Q are processed, such
c58936b6 126 that Q is added to Sol(P)
910fdc79
DB
127
128 4. All complex constraints for a given constraint variable are stored in a
c58936b6 129 linked list attached to that variable's node.
910fdc79
DB
130
131 5. A directed graph is built out of the copy constraints. Each
c58936b6 132 constraint variable is a node in the graph, and an edge from
910fdc79 133 Q to P is added for each copy constraint of the form P = Q
c58936b6 134
910fdc79
DB
135 6. The graph is then walked, and solution sets are
136 propagated along the copy edges, such that an edge from Q to P
137 causes Sol(P) <- Sol(P) union Sol(Q).
c58936b6 138
910fdc79 139 7. As we visit each node, all complex constraints associated with
607fb860 140 that node are processed by adding appropriate copy edges to the graph, or the
c58936b6 141 appropriate variables to the solution set.
910fdc79
DB
142
143 8. The process of walking the graph is iterated until no solution
144 sets change.
145
146 Prior to walking the graph in steps 6 and 7, We perform static
c58936b6 147 cycle elimination on the constraint graph, as well
910fdc79 148 as off-line variable substitution.
c58936b6 149
910fdc79
DB
150 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
151 on and turned into anything), but isn't. You can just see what offset
152 inside the pointed-to struct it's going to access.
c58936b6 153
910fdc79 154 TODO: Constant bounded arrays can be handled as if they were structs of the
c58936b6 155 same number of elements.
910fdc79
DB
156
157 TODO: Modeling heap and incoming pointers becomes much better if we
158 add fields to them as we discover them, which we could do.
159
160 TODO: We could handle unions, but to be honest, it's probably not
161 worth the pain or slowdown. */
162
c58936b6 163static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
21392f19
DB
164htab_t heapvar_for_stmt;
165
910fdc79 166static bool use_field_sensitive = true;
4ee00913 167static int in_ipa_mode = 0;
3e5937d7
DB
168
169/* Used for predecessor bitmaps. */
4ee00913 170static bitmap_obstack predbitmap_obstack;
3e5937d7
DB
171
172/* Used for points-to sets. */
173static bitmap_obstack pta_obstack;
174
175/* Used for oldsolution members of variables. */
176static bitmap_obstack oldpta_obstack;
177
178/* Used for per-solver-iteration bitmaps. */
4ee00913
DB
179static bitmap_obstack iteration_obstack;
180
910fdc79 181static unsigned int create_variable_info_for (tree, const char *);
3e5937d7
DB
182typedef struct constraint_graph *constraint_graph_t;
183static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
910fdc79 184
5006671f
RG
185struct constraint;
186typedef struct constraint *constraint_t;
187
910fdc79 188DEF_VEC_P(constraint_t);
b5efa470 189DEF_VEC_ALLOC_P(constraint_t,heap);
910fdc79 190
4ee00913
DB
191#define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
192 if (a) \
193 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
194
910fdc79
DB
195static struct constraint_stats
196{
197 unsigned int total_vars;
3e5937d7 198 unsigned int nonpointer_vars;
910fdc79
DB
199 unsigned int unified_vars_static;
200 unsigned int unified_vars_dynamic;
201 unsigned int iterations;
4ee00913 202 unsigned int num_edges;
3e5937d7
DB
203 unsigned int num_implicit_edges;
204 unsigned int points_to_sets_created;
910fdc79
DB
205} stats;
206
207struct variable_info
208{
209 /* ID of this variable */
210 unsigned int id;
211
910fdc79
DB
212 /* True if this is a variable created by the constraint analysis, such as
213 heap variables and constraints we had to break up. */
74d27244 214 unsigned int is_artificial_var : 1;
c58936b6 215
13c2c08b
DB
216 /* True if this is a special variable whose solution set should not be
217 changed. */
74d27244 218 unsigned int is_special_var : 1;
910fdc79
DB
219
220 /* True for variables whose size is not known or variable. */
74d27244 221 unsigned int is_unknown_size_var : 1;
910fdc79 222
e5bae89b
RG
223 /* True for (sub-)fields that represent a whole variable. */
224 unsigned int is_full_var : 1;
225
e8ca4159 226 /* True if this is a heap variable. */
74d27244
RG
227 unsigned int is_heap_var : 1;
228
229 /* True if this is a variable tracking a restrict pointer source. */
230 unsigned int is_restrict_var : 1;
e8ca4159 231
9e39dba6
RG
232 /* True if this field may contain pointers. */
233 unsigned int may_have_pointers : 1;
234
0bbf2ffa
RG
235 /* True if this represents a global variable. */
236 unsigned int is_global_var : 1;
237
795a337a
RG
238 /* A link to the variable for the next field in this structure. */
239 struct variable_info *next;
240
241 /* Offset of this variable, in bits, from the base variable */
242 unsigned HOST_WIDE_INT offset;
243
244 /* Size of the variable, in bits. */
245 unsigned HOST_WIDE_INT size;
246
247 /* Full size of the base variable, in bits. */
248 unsigned HOST_WIDE_INT fullsize;
249
250 /* Name of this variable */
251 const char *name;
252
253 /* Tree that this variable is associated with. */
254 tree decl;
255
910fdc79
DB
256 /* Points-to set for this variable. */
257 bitmap solution;
258
3e5937d7
DB
259 /* Old points-to set for this variable. */
260 bitmap oldsolution;
910fdc79
DB
261};
262typedef struct variable_info *varinfo_t;
263
264static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
5006671f
RG
265static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
266 unsigned HOST_WIDE_INT);
0e1f4c6b 267static varinfo_t lookup_vi_for_tree (tree);
910fdc79
DB
268
269/* Pool of variable info structures. */
270static alloc_pool variable_info_pool;
271
272DEF_VEC_P(varinfo_t);
273
b5efa470 274DEF_VEC_ALLOC_P(varinfo_t, heap);
910fdc79 275
38635499
DN
276/* Table of variable info structures for constraint variables.
277 Indexed directly by variable info id. */
b5efa470 278static VEC(varinfo_t,heap) *varmap;
13c2c08b
DB
279
280/* Return the varmap element N */
281
282static inline varinfo_t
03190594 283get_varinfo (unsigned int n)
13c2c08b 284{
62e5bf5d 285 return VEC_index (varinfo_t, varmap, n);
13c2c08b 286}
910fdc79 287
b7091901
RG
288/* Static IDs for the special variables. */
289enum { nothing_id = 0, anything_id = 1, readonly_id = 2,
9e39dba6
RG
290 escaped_id = 3, nonlocal_id = 4, callused_id = 5,
291 storedanything_id = 6, integer_id = 7 };
b7091901 292
8bc88f25
RB
293struct GTY(()) heapvar_map {
294 struct tree_map map;
295 unsigned HOST_WIDE_INT offset;
296};
297
298static int
299heapvar_map_eq (const void *p1, const void *p2)
300{
301 const struct heapvar_map *h1 = (const struct heapvar_map *)p1;
302 const struct heapvar_map *h2 = (const struct heapvar_map *)p2;
303 return (h1->map.base.from == h2->map.base.from
304 && h1->offset == h2->offset);
305}
306
307static unsigned int
308heapvar_map_hash (struct heapvar_map *h)
309{
310 return iterative_hash_host_wide_int (h->offset,
311 htab_hash_pointer (h->map.base.from));
312}
313
f5d7990b 314/* Lookup a heap var for FROM, and return it if we find one. */
c900f6aa 315
c58936b6 316static tree
8bc88f25 317heapvar_lookup (tree from, unsigned HOST_WIDE_INT offset)
c900f6aa 318{
8bc88f25
RB
319 struct heapvar_map *h, in;
320 in.map.base.from = from;
321 in.offset = offset;
322 h = (struct heapvar_map *) htab_find_with_hash (heapvar_for_stmt, &in,
323 heapvar_map_hash (&in));
c900f6aa 324 if (h)
8bc88f25 325 return h->map.to;
c900f6aa
DB
326 return NULL_TREE;
327}
328
329/* Insert a mapping FROM->TO in the heap var for statement
330 hashtable. */
331
332static void
8bc88f25 333heapvar_insert (tree from, unsigned HOST_WIDE_INT offset, tree to)
c900f6aa 334{
8bc88f25 335 struct heapvar_map *h;
c900f6aa
DB
336 void **loc;
337
8bc88f25
RB
338 h = GGC_NEW (struct heapvar_map);
339 h->map.base.from = from;
340 h->offset = offset;
341 h->map.hash = heapvar_map_hash (h);
342 h->map.to = to;
343 loc = htab_find_slot_with_hash (heapvar_for_stmt, h, h->map.hash, INSERT);
344 gcc_assert (*loc == NULL);
345 *(struct heapvar_map **) loc = h;
21392f19 346}
c900f6aa 347
910fdc79 348/* Return a new variable info structure consisting for a variable
0bbf2ffa
RG
349 named NAME, and using constraint graph node NODE. Append it
350 to the vector of variable info structures. */
910fdc79
DB
351
352static varinfo_t
0bbf2ffa 353new_var_info (tree t, const char *name)
910fdc79 354{
0bbf2ffa 355 unsigned index = VEC_length (varinfo_t, varmap);
c22940cd 356 varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
910fdc79 357
0bbf2ffa 358 ret->id = index;
910fdc79
DB
359 ret->name = name;
360 ret->decl = t;
0bbf2ffa
RG
361 /* Vars without decl are artificial and do not have sub-variables. */
362 ret->is_artificial_var = (t == NULL_TREE);
13c2c08b 363 ret->is_special_var = false;
910fdc79 364 ret->is_unknown_size_var = false;
02583d3c
RG
365 ret->is_full_var = (t == NULL_TREE);
366 ret->is_heap_var = false;
367 ret->is_restrict_var = false;
9e39dba6 368 ret->may_have_pointers = true;
74d27244 369 ret->is_global_var = (t == NULL_TREE);
0bbf2ffa
RG
370 if (t && DECL_P (t))
371 ret->is_global_var = is_global_var (t);
3e5937d7
DB
372 ret->solution = BITMAP_ALLOC (&pta_obstack);
373 ret->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
910fdc79 374 ret->next = NULL;
0bbf2ffa
RG
375
376 VEC_safe_push (varinfo_t, heap, varmap, ret);
377
910fdc79
DB
378 return ret;
379}
380
3e5937d7 381typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
910fdc79
DB
382
383/* An expression that appears in a constraint. */
384
c58936b6 385struct constraint_expr
910fdc79
DB
386{
387 /* Constraint type. */
388 constraint_expr_type type;
389
390 /* Variable we are referring to in the constraint. */
391 unsigned int var;
392
393 /* Offset, in bits, of this constraint from the beginning of
394 variables it ends up referring to.
395
396 IOW, in a deref constraint, we would deref, get the result set,
397 then add OFFSET to each member. */
5006671f 398 HOST_WIDE_INT offset;
910fdc79
DB
399};
400
5006671f
RG
401/* Use 0x8000... as special unknown offset. */
402#define UNKNOWN_OFFSET ((HOST_WIDE_INT)-1 << (HOST_BITS_PER_WIDE_INT-1))
403
4ee00913
DB
404typedef struct constraint_expr ce_s;
405DEF_VEC_O(ce_s);
406DEF_VEC_ALLOC_O(ce_s, heap);
c0d459f0 407static void get_constraint_for_1 (tree, VEC(ce_s, heap) **, bool);
1d85354c 408static void get_constraint_for (tree, VEC(ce_s, heap) **);
4ee00913 409static void do_deref (VEC (ce_s, heap) **);
910fdc79
DB
410
411/* Our set constraints are made up of two constraint expressions, one
c58936b6 412 LHS, and one RHS.
910fdc79
DB
413
414 As described in the introduction, our set constraints each represent an
415 operation between set valued variables.
416*/
417struct constraint
418{
419 struct constraint_expr lhs;
420 struct constraint_expr rhs;
421};
422
423/* List of constraints that we use to build the constraint graph from. */
424
b5efa470 425static VEC(constraint_t,heap) *constraints;
910fdc79
DB
426static alloc_pool constraint_pool;
427
57250223
DB
428/* The constraint graph is represented as an array of bitmaps
429 containing successor nodes. */
910fdc79
DB
430
431struct constraint_graph
432{
3e5937d7
DB
433 /* Size of this graph, which may be different than the number of
434 nodes in the variable map. */
435 unsigned int size;
436
437 /* Explicit successors of each node. */
57250223 438 bitmap *succs;
3e5937d7
DB
439
440 /* Implicit predecessors of each node (Used for variable
441 substitution). */
442 bitmap *implicit_preds;
443
444 /* Explicit predecessors of each node (Used for variable substitution). */
57250223 445 bitmap *preds;
910fdc79 446
3e5937d7
DB
447 /* Indirect cycle representatives, or -1 if the node has no indirect
448 cycles. */
449 int *indirect_cycles;
450
451 /* Representative node for a node. rep[a] == a unless the node has
452 been unified. */
453 unsigned int *rep;
454
7b765bed 455 /* Equivalence class representative for a label. This is used for
3e5937d7
DB
456 variable substitution. */
457 int *eq_rep;
458
aa46c8a3
DB
459 /* Pointer equivalence label for a node. All nodes with the same
460 pointer equivalence label can be unified together at some point
461 (either during constraint optimization or after the constraint
462 graph is built). */
7b765bed
DB
463 unsigned int *pe;
464
465 /* Pointer equivalence representative for a label. This is used to
466 handle nodes that are pointer equivalent but not location
467 equivalent. We can unite these once the addressof constraints
468 are transformed into initial points-to sets. */
469 int *pe_rep;
470
471 /* Pointer equivalence label for each node, used during variable
472 substitution. */
473 unsigned int *pointer_label;
474
475 /* Location equivalence label for each node, used during location
476 equivalence finding. */
477 unsigned int *loc_label;
478
479 /* Pointed-by set for each node, used during location equivalence
480 finding. This is pointed-by rather than pointed-to, because it
481 is constructed using the predecessor graph. */
482 bitmap *pointed_by;
483
484 /* Points to sets for pointer equivalence. This is *not* the actual
485 points-to sets for nodes. */
486 bitmap *points_to;
3e5937d7
DB
487
488 /* Bitmap of nodes where the bit is set if the node is a direct
489 node. Used for variable substitution. */
490 sbitmap direct_nodes;
491
7b765bed
DB
492 /* Bitmap of nodes where the bit is set if the node is address
493 taken. Used for variable substitution. */
494 bitmap address_taken;
495
3e5937d7
DB
496 /* Vector of complex constraints for each graph node. Complex
497 constraints are those involving dereferences or offsets that are
498 not 0. */
499 VEC(constraint_t,heap) **complex;
500};
910fdc79
DB
501
502static constraint_graph_t graph;
503
3e5937d7
DB
504/* During variable substitution and the offline version of indirect
505 cycle finding, we create nodes to represent dereferences and
506 address taken constraints. These represent where these start and
507 end. */
508#define FIRST_REF_NODE (VEC_length (varinfo_t, varmap))
509#define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
3e5937d7
DB
510
511/* Return the representative node for NODE, if NODE has been unioned
512 with another NODE.
513 This function performs path compression along the way to finding
514 the representative. */
515
516static unsigned int
517find (unsigned int node)
518{
519 gcc_assert (node < graph->size);
520 if (graph->rep[node] != node)
521 return graph->rep[node] = find (graph->rep[node]);
522 return node;
523}
524
525/* Union the TO and FROM nodes to the TO nodes.
526 Note that at some point in the future, we may want to do
527 union-by-rank, in which case we are going to have to return the
528 node we unified to. */
529
530static bool
531unite (unsigned int to, unsigned int from)
532{
533 gcc_assert (to < graph->size && from < graph->size);
534 if (to != from && graph->rep[from] != to)
535 {
536 graph->rep[from] = to;
537 return true;
538 }
539 return false;
540}
541
910fdc79
DB
542/* Create a new constraint consisting of LHS and RHS expressions. */
543
c58936b6 544static constraint_t
910fdc79
DB
545new_constraint (const struct constraint_expr lhs,
546 const struct constraint_expr rhs)
547{
c22940cd 548 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
910fdc79
DB
549 ret->lhs = lhs;
550 ret->rhs = rhs;
551 return ret;
552}
553
554/* Print out constraint C to FILE. */
555
5006671f 556static void
910fdc79
DB
557dump_constraint (FILE *file, constraint_t c)
558{
559 if (c->lhs.type == ADDRESSOF)
560 fprintf (file, "&");
561 else if (c->lhs.type == DEREF)
c58936b6 562 fprintf (file, "*");
5006671f
RG
563 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
564 if (c->lhs.offset == UNKNOWN_OFFSET)
565 fprintf (file, " + UNKNOWN");
566 else if (c->lhs.offset != 0)
910fdc79
DB
567 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
568 fprintf (file, " = ");
569 if (c->rhs.type == ADDRESSOF)
570 fprintf (file, "&");
571 else if (c->rhs.type == DEREF)
572 fprintf (file, "*");
5006671f
RG
573 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
574 if (c->rhs.offset == UNKNOWN_OFFSET)
575 fprintf (file, " + UNKNOWN");
576 else if (c->rhs.offset != 0)
910fdc79
DB
577 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
578 fprintf (file, "\n");
579}
580
5006671f
RG
581
582void debug_constraint (constraint_t);
583void debug_constraints (void);
584void debug_constraint_graph (void);
585void debug_solution_for_var (unsigned int);
586void debug_sa_points_to_info (void);
587
910fdc79
DB
588/* Print out constraint C to stderr. */
589
590void
591debug_constraint (constraint_t c)
592{
593 dump_constraint (stderr, c);
594}
595
596/* Print out all constraints to FILE */
597
5006671f 598static void
910fdc79
DB
599dump_constraints (FILE *file)
600{
601 int i;
602 constraint_t c;
603 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
604 dump_constraint (file, c);
605}
606
607/* Print out all constraints to stderr. */
608
609void
610debug_constraints (void)
611{
612 dump_constraints (stderr);
613}
614
fc93bcb6
FP
615/* Print out to FILE the edge in the constraint graph that is created by
616 constraint c. The edge may have a label, depending on the type of
617 constraint that it represents. If complex1, e.g: a = *b, then the label
618 is "=*", if complex2, e.g: *a = b, then the label is "*=", if
619 complex with an offset, e.g: a = b + 8, then the label is "+".
620 Otherwise the edge has no label. */
621
5006671f 622static void
fc93bcb6
FP
623dump_constraint_edge (FILE *file, constraint_t c)
624{
625 if (c->rhs.type != ADDRESSOF)
626 {
5006671f
RG
627 const char *src = get_varinfo (c->rhs.var)->name;
628 const char *dst = get_varinfo (c->lhs.var)->name;
fc93bcb6
FP
629 fprintf (file, " \"%s\" -> \"%s\" ", src, dst);
630 /* Due to preprocessing of constraints, instructions like *a = *b are
631 illegal; thus, we do not have to handle such cases. */
632 if (c->lhs.type == DEREF)
633 fprintf (file, " [ label=\"*=\" ] ;\n");
634 else if (c->rhs.type == DEREF)
635 fprintf (file, " [ label=\"=*\" ] ;\n");
636 else
637 {
638 /* We must check the case where the constraint is an offset.
639 In this case, it is treated as a complex constraint. */
640 if (c->rhs.offset != c->lhs.offset)
641 fprintf (file, " [ label=\"+\" ] ;\n");
642 else
643 fprintf (file, " ;\n");
644 }
645 }
646}
647
648/* Print the constraint graph in dot format. */
649
5006671f 650static void
fc93bcb6
FP
651dump_constraint_graph (FILE *file)
652{
653 unsigned int i=0, size;
654 constraint_t c;
655
656 /* Only print the graph if it has already been initialized: */
657 if (!graph)
658 return;
659
660 /* Print the constraints used to produce the constraint graph. The
661 constraints will be printed as comments in the dot file: */
662 fprintf (file, "\n\n/* Constraints used in the constraint graph:\n");
663 dump_constraints (file);
664 fprintf (file, "*/\n");
665
666 /* Prints the header of the dot file: */
667 fprintf (file, "\n\n// The constraint graph in dot format:\n");
668 fprintf (file, "strict digraph {\n");
669 fprintf (file, " node [\n shape = box\n ]\n");
670 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
671 fprintf (file, "\n // List of nodes in the constraint graph:\n");
672
673 /* The next lines print the nodes in the graph. In order to get the
674 number of nodes in the graph, we must choose the minimum between the
675 vector VEC (varinfo_t, varmap) and graph->size. If the graph has not
676 yet been initialized, then graph->size == 0, otherwise we must only
677 read nodes that have an entry in VEC (varinfo_t, varmap). */
678 size = VEC_length (varinfo_t, varmap);
679 size = size < graph->size ? size : graph->size;
680 for (i = 0; i < size; i++)
681 {
5006671f 682 const char *name = get_varinfo (graph->rep[i])->name;
fc93bcb6
FP
683 fprintf (file, " \"%s\" ;\n", name);
684 }
685
686 /* Go over the list of constraints printing the edges in the constraint
687 graph. */
688 fprintf (file, "\n // The constraint edges:\n");
689 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
690 if (c)
691 dump_constraint_edge (file, c);
692
693 /* Prints the tail of the dot file. By now, only the closing bracket. */
694 fprintf (file, "}\n\n\n");
695}
696
697/* Print out the constraint graph to stderr. */
698
699void
700debug_constraint_graph (void)
701{
702 dump_constraint_graph (stderr);
703}
704
c58936b6 705/* SOLVER FUNCTIONS
910fdc79
DB
706
707 The solver is a simple worklist solver, that works on the following
708 algorithm:
c58936b6 709
3e5937d7
DB
710 sbitmap changed_nodes = all zeroes;
711 changed_count = 0;
712 For each node that is not already collapsed:
713 changed_count++;
714 set bit in changed nodes
910fdc79 715
910fdc79
DB
716 while (changed_count > 0)
717 {
718 compute topological ordering for constraint graph
c58936b6 719
910fdc79
DB
720 find and collapse cycles in the constraint graph (updating
721 changed if necessary)
c58936b6 722
910fdc79
DB
723 for each node (n) in the graph in topological order:
724 changed_count--;
725
726 Process each complex constraint associated with the node,
727 updating changed if necessary.
728
729 For each outgoing edge from n, propagate the solution from n to
730 the destination of the edge, updating changed as necessary.
731
732 } */
733
734/* Return true if two constraint expressions A and B are equal. */
735
736static bool
737constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
738{
4ee00913 739 return a.type == b.type && a.var == b.var && a.offset == b.offset;
910fdc79
DB
740}
741
742/* Return true if constraint expression A is less than constraint expression
743 B. This is just arbitrary, but consistent, in order to give them an
744 ordering. */
745
746static bool
747constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
748{
749 if (a.type == b.type)
750 {
751 if (a.var == b.var)
752 return a.offset < b.offset;
753 else
754 return a.var < b.var;
755 }
756 else
757 return a.type < b.type;
758}
759
760/* Return true if constraint A is less than constraint B. This is just
761 arbitrary, but consistent, in order to give them an ordering. */
762
763static bool
764constraint_less (const constraint_t a, const constraint_t b)
765{
766 if (constraint_expr_less (a->lhs, b->lhs))
767 return true;
768 else if (constraint_expr_less (b->lhs, a->lhs))
769 return false;
770 else
771 return constraint_expr_less (a->rhs, b->rhs);
772}
773
774/* Return true if two constraints A and B are equal. */
c58936b6 775
910fdc79
DB
776static bool
777constraint_equal (struct constraint a, struct constraint b)
778{
c58936b6 779 return constraint_expr_equal (a.lhs, b.lhs)
910fdc79
DB
780 && constraint_expr_equal (a.rhs, b.rhs);
781}
782
783
784/* Find a constraint LOOKFOR in the sorted constraint vector VEC */
785
786static constraint_t
b5efa470 787constraint_vec_find (VEC(constraint_t,heap) *vec,
910fdc79
DB
788 struct constraint lookfor)
789{
c58936b6 790 unsigned int place;
910fdc79
DB
791 constraint_t found;
792
793 if (vec == NULL)
794 return NULL;
795
796 place = VEC_lower_bound (constraint_t, vec, &lookfor, constraint_less);
797 if (place >= VEC_length (constraint_t, vec))
798 return NULL;
799 found = VEC_index (constraint_t, vec, place);
800 if (!constraint_equal (*found, lookfor))
801 return NULL;
802 return found;
803}
804
805/* Union two constraint vectors, TO and FROM. Put the result in TO. */
806
807static void
b5efa470
DB
808constraint_set_union (VEC(constraint_t,heap) **to,
809 VEC(constraint_t,heap) **from)
910fdc79
DB
810{
811 int i;
812 constraint_t c;
813
814 for (i = 0; VEC_iterate (constraint_t, *from, i, c); i++)
815 {
816 if (constraint_vec_find (*to, *c) == NULL)
817 {
818 unsigned int place = VEC_lower_bound (constraint_t, *to, c,
819 constraint_less);
b5efa470 820 VEC_safe_insert (constraint_t, heap, *to, place, c);
910fdc79
DB
821 }
822 }
823}
824
5006671f
RG
825/* Expands the solution in SET to all sub-fields of variables included.
826 Union the expanded result into RESULT. */
827
828static void
829solution_set_expand (bitmap result, bitmap set)
830{
831 bitmap_iterator bi;
832 bitmap vars = NULL;
833 unsigned j;
834
835 /* In a first pass record all variables we need to add all
836 sub-fields off. This avoids quadratic behavior. */
837 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
838 {
839 varinfo_t v = get_varinfo (j);
840 if (v->is_artificial_var
841 || v->is_full_var)
842 continue;
843 v = lookup_vi_for_tree (v->decl);
844 if (vars == NULL)
845 vars = BITMAP_ALLOC (NULL);
846 bitmap_set_bit (vars, v->id);
847 }
848
849 /* In the second pass now do the addition to the solution and
850 to speed up solving add it to the delta as well. */
851 if (vars != NULL)
852 {
853 EXECUTE_IF_SET_IN_BITMAP (vars, 0, j, bi)
854 {
855 varinfo_t v = get_varinfo (j);
856 for (; v != NULL; v = v->next)
857 bitmap_set_bit (result, v->id);
858 }
859 BITMAP_FREE (vars);
860 }
861}
862
910fdc79
DB
863/* Take a solution set SET, add OFFSET to each member of the set, and
864 overwrite SET with the result when done. */
865
866static void
5006671f 867solution_set_add (bitmap set, HOST_WIDE_INT offset)
910fdc79
DB
868{
869 bitmap result = BITMAP_ALLOC (&iteration_obstack);
870 unsigned int i;
871 bitmap_iterator bi;
872
5006671f
RG
873 /* If the offset is unknown we have to expand the solution to
874 all subfields. */
875 if (offset == UNKNOWN_OFFSET)
876 {
877 solution_set_expand (set, set);
878 return;
879 }
880
910fdc79
DB
881 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
882 {
e5bae89b 883 varinfo_t vi = get_varinfo (i);
c58936b6 884
e5bae89b
RG
885 /* If this is a variable with just one field just set its bit
886 in the result. */
887 if (vi->is_artificial_var
888 || vi->is_unknown_size_var
889 || vi->is_full_var)
890 bitmap_set_bit (result, i);
891 else
910fdc79 892 {
e5bae89b 893 unsigned HOST_WIDE_INT fieldoffset = vi->offset + offset;
5006671f
RG
894
895 /* If the offset makes the pointer point to before the
896 variable use offset zero for the field lookup. */
897 if (offset < 0
898 && fieldoffset > vi->offset)
899 fieldoffset = 0;
900
901 if (offset != 0)
902 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
903
904 bitmap_set_bit (result, vi->id);
e5bae89b
RG
905 /* If the result is not exactly at fieldoffset include the next
906 field as well. See get_constraint_for_ptr_offset for more
907 rationale. */
5006671f
RG
908 if (vi->offset != fieldoffset
909 && vi->next != NULL)
910 bitmap_set_bit (result, vi->next->id);
910fdc79
DB
911 }
912 }
c58936b6
DB
913
914 bitmap_copy (set, result);
910fdc79
DB
915 BITMAP_FREE (result);
916}
917
918/* Union solution sets TO and FROM, and add INC to each member of FROM in the
919 process. */
920
921static bool
5006671f 922set_union_with_increment (bitmap to, bitmap from, HOST_WIDE_INT inc)
910fdc79
DB
923{
924 if (inc == 0)
925 return bitmap_ior_into (to, from);
926 else
927 {
928 bitmap tmp;
929 bool res;
930
931 tmp = BITMAP_ALLOC (&iteration_obstack);
932 bitmap_copy (tmp, from);
933 solution_set_add (tmp, inc);
934 res = bitmap_ior_into (to, tmp);
935 BITMAP_FREE (tmp);
936 return res;
937 }
938}
939
3e5937d7
DB
940/* Insert constraint C into the list of complex constraints for graph
941 node VAR. */
910fdc79
DB
942
943static void
3e5937d7
DB
944insert_into_complex (constraint_graph_t graph,
945 unsigned int var, constraint_t c)
910fdc79 946{
3e5937d7
DB
947 VEC (constraint_t, heap) *complex = graph->complex[var];
948 unsigned int place = VEC_lower_bound (constraint_t, complex, c,
910fdc79 949 constraint_less);
3e5937d7
DB
950
951 /* Only insert constraints that do not already exist. */
952 if (place >= VEC_length (constraint_t, complex)
953 || !constraint_equal (*c, *VEC_index (constraint_t, complex, place)))
954 VEC_safe_insert (constraint_t, heap, graph->complex[var], place, c);
910fdc79
DB
955}
956
957
910fdc79
DB
958/* Condense two variable nodes into a single variable node, by moving
959 all associated info from SRC to TO. */
960
c58936b6 961static void
3e5937d7
DB
962merge_node_constraints (constraint_graph_t graph, unsigned int to,
963 unsigned int from)
910fdc79 964{
910fdc79
DB
965 unsigned int i;
966 constraint_t c;
c58936b6 967
3e5937d7 968 gcc_assert (find (from) == to);
c58936b6 969
910fdc79 970 /* Move all complex constraints from src node into to node */
3e5937d7 971 for (i = 0; VEC_iterate (constraint_t, graph->complex[from], i, c); i++)
910fdc79
DB
972 {
973 /* In complex constraints for node src, we may have either
3e5937d7
DB
974 a = *src, and *src = a, or an offseted constraint which are
975 always added to the rhs node's constraints. */
c58936b6 976
910fdc79
DB
977 if (c->rhs.type == DEREF)
978 c->rhs.var = to;
3e5937d7 979 else if (c->lhs.type == DEREF)
910fdc79 980 c->lhs.var = to;
3e5937d7
DB
981 else
982 c->rhs.var = to;
910fdc79 983 }
3e5937d7
DB
984 constraint_set_union (&graph->complex[to], &graph->complex[from]);
985 VEC_free (constraint_t, heap, graph->complex[from]);
986 graph->complex[from] = NULL;
910fdc79
DB
987}
988
910fdc79
DB
989
990/* Remove edges involving NODE from GRAPH. */
991
992static void
993clear_edges_for_node (constraint_graph_t graph, unsigned int node)
994{
57250223 995 if (graph->succs[node])
3e5937d7 996 BITMAP_FREE (graph->succs[node]);
f71ef09d
DB
997}
998
910fdc79
DB
999/* Merge GRAPH nodes FROM and TO into node TO. */
1000
1001static void
c58936b6 1002merge_graph_nodes (constraint_graph_t graph, unsigned int to,
910fdc79
DB
1003 unsigned int from)
1004{
3e5937d7 1005 if (graph->indirect_cycles[from] != -1)
4ee00913 1006 {
3e5937d7
DB
1007 /* If we have indirect cycles with the from node, and we have
1008 none on the to node, the to node has indirect cycles from the
1009 from node now that they are unified.
1010 If indirect cycles exist on both, unify the nodes that they
1011 are in a cycle with, since we know they are in a cycle with
1012 each other. */
1013 if (graph->indirect_cycles[to] == -1)
7b765bed 1014 graph->indirect_cycles[to] = graph->indirect_cycles[from];
4ee00913 1015 }
910fdc79 1016
57250223
DB
1017 /* Merge all the successor edges. */
1018 if (graph->succs[from])
4ee00913 1019 {
57250223 1020 if (!graph->succs[to])
3e5937d7 1021 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
c58936b6 1022 bitmap_ior_into (graph->succs[to],
57250223 1023 graph->succs[from]);
4ee00913 1024 }
4ee00913 1025
910fdc79
DB
1026 clear_edges_for_node (graph, from);
1027}
1028
3e5937d7
DB
1029
1030/* Add an indirect graph edge to GRAPH, going from TO to FROM if
1031 it doesn't exist in the graph already. */
1032
1033static void
1034add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1035 unsigned int from)
1036{
1037 if (to == from)
1038 return;
1039
1040 if (!graph->implicit_preds[to])
1041 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1042
5f0d975b
RG
1043 if (bitmap_set_bit (graph->implicit_preds[to], from))
1044 stats.num_implicit_edges++;
3e5937d7
DB
1045}
1046
1047/* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1048 it doesn't exist in the graph already.
1049 Return false if the edge already existed, true otherwise. */
1050
1051static void
1052add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1053 unsigned int from)
1054{
1055 if (!graph->preds[to])
1056 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
5f0d975b 1057 bitmap_set_bit (graph->preds[to], from);
3e5937d7
DB
1058}
1059
1060/* Add a graph edge to GRAPH, going from FROM to TO if
910fdc79
DB
1061 it doesn't exist in the graph already.
1062 Return false if the edge already existed, true otherwise. */
1063
1064static bool
57250223
DB
1065add_graph_edge (constraint_graph_t graph, unsigned int to,
1066 unsigned int from)
910fdc79 1067{
57250223 1068 if (to == from)
910fdc79
DB
1069 {
1070 return false;
1071 }
1072 else
1073 {
4ee00913 1074 bool r = false;
c58936b6 1075
57250223 1076 if (!graph->succs[from])
3e5937d7 1077 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
5f0d975b 1078 if (bitmap_set_bit (graph->succs[from], to))
f71ef09d 1079 {
57250223 1080 r = true;
3e5937d7
DB
1081 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1082 stats.num_edges++;
f71ef09d 1083 }
910fdc79
DB
1084 return r;
1085 }
1086}
1087
1088
4ee00913 1089/* Return true if {DEST.SRC} is an existing graph edge in GRAPH. */
910fdc79
DB
1090
1091static bool
c58936b6 1092valid_graph_edge (constraint_graph_t graph, unsigned int src,
4ee00913 1093 unsigned int dest)
910fdc79 1094{
c58936b6 1095 return (graph->succs[dest]
57250223 1096 && bitmap_bit_p (graph->succs[dest], src));
4ee00913
DB
1097}
1098
7b765bed
DB
1099/* Initialize the constraint graph structure to contain SIZE nodes. */
1100
1101static void
1102init_graph (unsigned int size)
1103{
1104 unsigned int j;
1105
1106 graph = XCNEW (struct constraint_graph);
1107 graph->size = size;
1108 graph->succs = XCNEWVEC (bitmap, graph->size);
1109 graph->indirect_cycles = XNEWVEC (int, graph->size);
1110 graph->rep = XNEWVEC (unsigned int, graph->size);
1111 graph->complex = XCNEWVEC (VEC(constraint_t, heap) *, size);
aa46c8a3 1112 graph->pe = XCNEWVEC (unsigned int, graph->size);
7b765bed
DB
1113 graph->pe_rep = XNEWVEC (int, graph->size);
1114
1115 for (j = 0; j < graph->size; j++)
1116 {
1117 graph->rep[j] = j;
7b765bed
DB
1118 graph->pe_rep[j] = -1;
1119 graph->indirect_cycles[j] = -1;
1120 }
1121}
1122
3e5937d7 1123/* Build the constraint graph, adding only predecessor edges right now. */
910fdc79
DB
1124
1125static void
3e5937d7 1126build_pred_graph (void)
910fdc79 1127{
3e5937d7 1128 int i;
910fdc79 1129 constraint_t c;
3e5937d7 1130 unsigned int j;
910fdc79 1131
3e5937d7
DB
1132 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1133 graph->preds = XCNEWVEC (bitmap, graph->size);
7b765bed
DB
1134 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1135 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1136 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1137 graph->points_to = XCNEWVEC (bitmap, graph->size);
3e5937d7 1138 graph->eq_rep = XNEWVEC (int, graph->size);
3e5937d7 1139 graph->direct_nodes = sbitmap_alloc (graph->size);
7b765bed 1140 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
3e5937d7
DB
1141 sbitmap_zero (graph->direct_nodes);
1142
1143 for (j = 0; j < FIRST_REF_NODE; j++)
1144 {
1145 if (!get_varinfo (j)->is_special_var)
1146 SET_BIT (graph->direct_nodes, j);
1147 }
1148
1149 for (j = 0; j < graph->size; j++)
7b765bed 1150 graph->eq_rep[j] = -1;
3e5937d7
DB
1151
1152 for (j = 0; j < VEC_length (varinfo_t, varmap); j++)
1153 graph->indirect_cycles[j] = -1;
e8ca4159 1154
910fdc79
DB
1155 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
1156 {
1157 struct constraint_expr lhs = c->lhs;
1158 struct constraint_expr rhs = c->rhs;
5006671f
RG
1159 unsigned int lhsvar = lhs.var;
1160 unsigned int rhsvar = rhs.var;
03190594 1161
910fdc79
DB
1162 if (lhs.type == DEREF)
1163 {
3e5937d7
DB
1164 /* *x = y. */
1165 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1166 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
910fdc79
DB
1167 }
1168 else if (rhs.type == DEREF)
1169 {
3e5937d7
DB
1170 /* x = *y */
1171 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1172 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1173 else
1174 RESET_BIT (graph->direct_nodes, lhsvar);
910fdc79 1175 }
3e5937d7 1176 else if (rhs.type == ADDRESSOF)
910fdc79 1177 {
10bd6c5c
RG
1178 varinfo_t v;
1179
910fdc79 1180 /* x = &y */
7b765bed
DB
1181 if (graph->points_to[lhsvar] == NULL)
1182 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1183 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1184
1185 if (graph->pointed_by[rhsvar] == NULL)
1186 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1187 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1188
3e5937d7
DB
1189 /* Implicitly, *x = y */
1190 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1191
10bd6c5c 1192 /* All related variables are no longer direct nodes. */
3e5937d7 1193 RESET_BIT (graph->direct_nodes, rhsvar);
5006671f
RG
1194 v = get_varinfo (rhsvar);
1195 if (!v->is_full_var)
1196 {
1197 v = lookup_vi_for_tree (v->decl);
1198 do
1199 {
1200 RESET_BIT (graph->direct_nodes, v->id);
1201 v = v->next;
1202 }
1203 while (v != NULL);
1204 }
7b765bed 1205 bitmap_set_bit (graph->address_taken, rhsvar);
910fdc79 1206 }
3e5937d7
DB
1207 else if (lhsvar > anything_id
1208 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
910fdc79 1209 {
3e5937d7
DB
1210 /* x = y */
1211 add_pred_graph_edge (graph, lhsvar, rhsvar);
1212 /* Implicitly, *x = *y */
1213 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1214 FIRST_REF_NODE + rhsvar);
1215 }
1216 else if (lhs.offset != 0 || rhs.offset != 0)
1217 {
1218 if (rhs.offset != 0)
1219 RESET_BIT (graph->direct_nodes, lhs.var);
7b765bed 1220 else if (lhs.offset != 0)
3e5937d7
DB
1221 RESET_BIT (graph->direct_nodes, rhs.var);
1222 }
1223 }
1224}
1225
1226/* Build the constraint graph, adding successor edges. */
1227
1228static void
1229build_succ_graph (void)
1230{
9e39dba6 1231 unsigned i, t;
3e5937d7
DB
1232 constraint_t c;
1233
1234 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
1235 {
1236 struct constraint_expr lhs;
1237 struct constraint_expr rhs;
1238 unsigned int lhsvar;
1239 unsigned int rhsvar;
1240
1241 if (!c)
1242 continue;
c58936b6 1243
3e5937d7
DB
1244 lhs = c->lhs;
1245 rhs = c->rhs;
5006671f
RG
1246 lhsvar = find (lhs.var);
1247 rhsvar = find (rhs.var);
3e5937d7
DB
1248
1249 if (lhs.type == DEREF)
1250 {
1251 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1252 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1253 }
1254 else if (rhs.type == DEREF)
1255 {
1256 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1257 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1258 }
1259 else if (rhs.type == ADDRESSOF)
1260 {
1261 /* x = &y */
5006671f 1262 gcc_assert (find (rhs.var) == rhs.var);
3e5937d7
DB
1263 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1264 }
1265 else if (lhsvar > anything_id
1266 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1267 {
1268 add_graph_edge (graph, lhsvar, rhsvar);
910fdc79
DB
1269 }
1270 }
9e39dba6 1271
de925a03
RG
1272 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1273 receive pointers. */
9e39dba6
RG
1274 t = find (storedanything_id);
1275 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1276 {
de925a03
RG
1277 if (!TEST_BIT (graph->direct_nodes, i)
1278 && get_varinfo (i)->may_have_pointers)
9e39dba6
RG
1279 add_graph_edge (graph, find (i), t);
1280 }
379c6f48
RG
1281
1282 /* Everything stored to ANYTHING also potentially escapes. */
1283 add_graph_edge (graph, find (escaped_id), t);
910fdc79 1284}
e8ca4159
DN
1285
1286
910fdc79
DB
1287/* Changed variables on the last iteration. */
1288static unsigned int changed_count;
1289static sbitmap changed;
1290
910fdc79
DB
1291/* Strongly Connected Component visitation info. */
1292
1293struct scc_info
1294{
1295 sbitmap visited;
7b765bed 1296 sbitmap deleted;
3e5937d7
DB
1297 unsigned int *dfs;
1298 unsigned int *node_mapping;
910fdc79 1299 int current_index;
740e80e8 1300 VEC(unsigned,heap) *scc_stack;
910fdc79
DB
1301};
1302
1303
1304/* Recursive routine to find strongly connected components in GRAPH.
1305 SI is the SCC info to store the information in, and N is the id of current
1306 graph node we are processing.
c58936b6 1307
910fdc79 1308 This is Tarjan's strongly connected component finding algorithm, as
c58936b6 1309 modified by Nuutila to keep only non-root nodes on the stack.
910fdc79
DB
1310 The algorithm can be found in "On finding the strongly connected
1311 connected components in a directed graph" by Esko Nuutila and Eljas
1312 Soisalon-Soininen, in Information Processing Letters volume 49,
1313 number 1, pages 9-14. */
1314
1315static void
1316scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1317{
4ee00913
DB
1318 unsigned int i;
1319 bitmap_iterator bi;
3e5937d7 1320 unsigned int my_dfs;
910fdc79 1321
910fdc79 1322 SET_BIT (si->visited, n);
3e5937d7
DB
1323 si->dfs[n] = si->current_index ++;
1324 my_dfs = si->dfs[n];
c58936b6 1325
910fdc79 1326 /* Visit all the successors. */
57250223 1327 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
910fdc79 1328 {
3e5937d7
DB
1329 unsigned int w;
1330
1331 if (i > LAST_REF_NODE)
1332 break;
1333
1334 w = find (i);
7b765bed 1335 if (TEST_BIT (si->deleted, w))
3e5937d7
DB
1336 continue;
1337
4ee00913
DB
1338 if (!TEST_BIT (si->visited, w))
1339 scc_visit (graph, si, w);
3e5937d7
DB
1340 {
1341 unsigned int t = find (w);
1342 unsigned int nnode = find (n);
62e5bf5d 1343 gcc_assert (nnode == n);
3e5937d7
DB
1344
1345 if (si->dfs[t] < si->dfs[nnode])
1346 si->dfs[n] = si->dfs[t];
1347 }
910fdc79 1348 }
c58936b6 1349
910fdc79 1350 /* See if any components have been identified. */
3e5937d7 1351 if (si->dfs[n] == my_dfs)
910fdc79 1352 {
3e5937d7
DB
1353 if (VEC_length (unsigned, si->scc_stack) > 0
1354 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
910fdc79 1355 {
3e5937d7
DB
1356 bitmap scc = BITMAP_ALLOC (NULL);
1357 bool have_ref_node = n >= FIRST_REF_NODE;
1358 unsigned int lowest_node;
1359 bitmap_iterator bi;
910fdc79 1360
3e5937d7 1361 bitmap_set_bit (scc, n);
910fdc79 1362
3e5937d7
DB
1363 while (VEC_length (unsigned, si->scc_stack) != 0
1364 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1365 {
1366 unsigned int w = VEC_pop (unsigned, si->scc_stack);
910fdc79 1367
3e5937d7
DB
1368 bitmap_set_bit (scc, w);
1369 if (w >= FIRST_REF_NODE)
1370 have_ref_node = true;
1371 }
4ee00913 1372
3e5937d7
DB
1373 lowest_node = bitmap_first_set_bit (scc);
1374 gcc_assert (lowest_node < FIRST_REF_NODE);
7b765bed
DB
1375
1376 /* Collapse the SCC nodes into a single node, and mark the
1377 indirect cycles. */
3e5937d7
DB
1378 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1379 {
1380 if (i < FIRST_REF_NODE)
1381 {
3e5937d7
DB
1382 if (unite (lowest_node, i))
1383 unify_nodes (graph, lowest_node, i, false);
1384 }
1385 else
1386 {
1387 unite (lowest_node, i);
1388 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1389 }
1390 }
4ee00913 1391 }
7b765bed 1392 SET_BIT (si->deleted, n);
910fdc79 1393 }
3e5937d7
DB
1394 else
1395 VEC_safe_push (unsigned, heap, si->scc_stack, n);
910fdc79
DB
1396}
1397
3e5937d7
DB
1398/* Unify node FROM into node TO, updating the changed count if
1399 necessary when UPDATE_CHANGED is true. */
910fdc79
DB
1400
1401static void
3e5937d7
DB
1402unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1403 bool update_changed)
910fdc79 1404{
910fdc79 1405
3e5937d7
DB
1406 gcc_assert (to != from && find (to) == to);
1407 if (dump_file && (dump_flags & TDF_DETAILS))
1408 fprintf (dump_file, "Unifying %s to %s\n",
1409 get_varinfo (from)->name,
1410 get_varinfo (to)->name);
910fdc79 1411
3e5937d7
DB
1412 if (update_changed)
1413 stats.unified_vars_dynamic++;
1414 else
1415 stats.unified_vars_static++;
910fdc79 1416
3e5937d7
DB
1417 merge_graph_nodes (graph, to, from);
1418 merge_node_constraints (graph, to, from);
c58936b6 1419
7b765bed
DB
1420 /* Mark TO as changed if FROM was changed. If TO was already marked
1421 as changed, decrease the changed count. */
1422
3e5937d7 1423 if (update_changed && TEST_BIT (changed, from))
910fdc79 1424 {
3e5937d7
DB
1425 RESET_BIT (changed, from);
1426 if (!TEST_BIT (changed, to))
1427 SET_BIT (changed, to);
910fdc79 1428 else
3e5937d7
DB
1429 {
1430 gcc_assert (changed_count > 0);
1431 changed_count--;
1432 }
1433 }
aa46c8a3 1434 if (get_varinfo (from)->solution)
3e5937d7 1435 {
aa46c8a3
DB
1436 /* If the solution changes because of the merging, we need to mark
1437 the variable as changed. */
1438 if (bitmap_ior_into (get_varinfo (to)->solution,
1439 get_varinfo (from)->solution))
910fdc79 1440 {
aa46c8a3
DB
1441 if (update_changed && !TEST_BIT (changed, to))
1442 {
1443 SET_BIT (changed, to);
1444 changed_count++;
1445 }
1446 }
b8698a0f 1447
aa46c8a3
DB
1448 BITMAP_FREE (get_varinfo (from)->solution);
1449 BITMAP_FREE (get_varinfo (from)->oldsolution);
b8698a0f 1450
aa46c8a3
DB
1451 if (stats.iterations > 0)
1452 {
1453 BITMAP_FREE (get_varinfo (to)->oldsolution);
1454 get_varinfo (to)->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
910fdc79 1455 }
3e5937d7 1456 }
3e5937d7
DB
1457 if (valid_graph_edge (graph, to, to))
1458 {
1459 if (graph->succs[to])
1460 bitmap_clear_bit (graph->succs[to], to);
910fdc79 1461 }
910fdc79
DB
1462}
1463
910fdc79
DB
1464/* Information needed to compute the topological ordering of a graph. */
1465
1466struct topo_info
1467{
1468 /* sbitmap of visited nodes. */
1469 sbitmap visited;
1470 /* Array that stores the topological order of the graph, *in
1471 reverse*. */
740e80e8 1472 VEC(unsigned,heap) *topo_order;
910fdc79
DB
1473};
1474
1475
1476/* Initialize and return a topological info structure. */
1477
1478static struct topo_info *
1479init_topo_info (void)
1480{
7b765bed 1481 size_t size = graph->size;
5ed6ace5 1482 struct topo_info *ti = XNEW (struct topo_info);
910fdc79
DB
1483 ti->visited = sbitmap_alloc (size);
1484 sbitmap_zero (ti->visited);
740e80e8 1485 ti->topo_order = VEC_alloc (unsigned, heap, 1);
910fdc79
DB
1486 return ti;
1487}
1488
1489
1490/* Free the topological sort info pointed to by TI. */
1491
1492static void
1493free_topo_info (struct topo_info *ti)
1494{
1495 sbitmap_free (ti->visited);
740e80e8 1496 VEC_free (unsigned, heap, ti->topo_order);
910fdc79
DB
1497 free (ti);
1498}
1499
1500/* Visit the graph in topological order, and store the order in the
1501 topo_info structure. */
1502
1503static void
1504topo_visit (constraint_graph_t graph, struct topo_info *ti,
1505 unsigned int n)
1506{
4ee00913 1507 bitmap_iterator bi;
4ee00913
DB
1508 unsigned int j;
1509
910fdc79 1510 SET_BIT (ti->visited, n);
4ee00913 1511
3e5937d7
DB
1512 if (graph->succs[n])
1513 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
4ee00913
DB
1514 {
1515 if (!TEST_BIT (ti->visited, j))
1516 topo_visit (graph, ti, j);
1517 }
3e5937d7 1518
740e80e8 1519 VEC_safe_push (unsigned, heap, ti->topo_order, n);
910fdc79
DB
1520}
1521
5006671f
RG
1522/* Process a constraint C that represents x = *(y + off), using DELTA as the
1523 starting solution for y. */
910fdc79
DB
1524
1525static void
1526do_sd_constraint (constraint_graph_t graph, constraint_t c,
1527 bitmap delta)
1528{
7b765bed 1529 unsigned int lhs = c->lhs.var;
910fdc79
DB
1530 bool flag = false;
1531 bitmap sol = get_varinfo (lhs)->solution;
1532 unsigned int j;
1533 bitmap_iterator bi;
5006671f 1534 HOST_WIDE_INT roffset = c->rhs.offset;
4ee00913 1535
5006671f
RG
1536 /* Our IL does not allow this. */
1537 gcc_assert (c->lhs.offset == 0);
0e1f4c6b 1538
5006671f
RG
1539 /* If the solution of Y contains anything it is good enough to transfer
1540 this to the LHS. */
14c28276
RG
1541 if (bitmap_bit_p (delta, anything_id))
1542 {
1543 flag |= bitmap_set_bit (sol, anything_id);
1544 goto done;
1545 }
1546
5006671f
RG
1547 /* If we do not know at with offset the rhs is dereferenced compute
1548 the reachability set of DELTA, conservatively assuming it is
1549 dereferenced at all valid offsets. */
1550 if (roffset == UNKNOWN_OFFSET)
1551 {
1552 solution_set_expand (delta, delta);
1553 /* No further offset processing is necessary. */
1554 roffset = 0;
1555 }
1556
c58936b6 1557 /* For each variable j in delta (Sol(y)), add
910fdc79
DB
1558 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1559 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1560 {
5006671f
RG
1561 varinfo_t v = get_varinfo (j);
1562 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1563 unsigned int t;
1564
1565 if (v->is_full_var)
1566 fieldoffset = v->offset;
1567 else if (roffset != 0)
1568 v = first_vi_for_offset (v, fieldoffset);
1569 /* If the access is outside of the variable we can ignore it. */
1570 if (!v)
1571 continue;
910fdc79 1572
5006671f
RG
1573 do
1574 {
3e5937d7 1575 t = find (v->id);
4ee00913
DB
1576
1577 /* Adding edges from the special vars is pointless.
1578 They don't have sets that can change. */
b7091901 1579 if (get_varinfo (t)->is_special_var)
4ee00913 1580 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
b7091901 1581 /* Merging the solution from ESCAPED needlessly increases
472c7fbd 1582 the set. Use ESCAPED as representative instead. */
5006671f 1583 else if (v->id == escaped_id)
6a66f28e 1584 flag |= bitmap_set_bit (sol, escaped_id);
57250223 1585 else if (add_graph_edge (graph, lhs, t))
4ee00913 1586 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
5006671f
RG
1587
1588 /* If the variable is not exactly at the requested offset
1589 we have to include the next one. */
1590 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1591 || v->next == NULL)
1592 break;
1593
1594 v = v->next;
1595 fieldoffset = v->offset;
910fdc79 1596 }
5006671f 1597 while (1);
910fdc79 1598 }
4cf4d6a3 1599
4ee00913 1600done:
910fdc79
DB
1601 /* If the LHS solution changed, mark the var as changed. */
1602 if (flag)
1603 {
1604 get_varinfo (lhs)->solution = sol;
1605 if (!TEST_BIT (changed, lhs))
1606 {
1607 SET_BIT (changed, lhs);
1608 changed_count++;
1609 }
c58936b6 1610 }
910fdc79
DB
1611}
1612
5006671f
RG
1613/* Process a constraint C that represents *(x + off) = y using DELTA
1614 as the starting solution for x. */
910fdc79
DB
1615
1616static void
57250223 1617do_ds_constraint (constraint_t c, bitmap delta)
910fdc79 1618{
7b765bed 1619 unsigned int rhs = c->rhs.var;
910fdc79
DB
1620 bitmap sol = get_varinfo (rhs)->solution;
1621 unsigned int j;
1622 bitmap_iterator bi;
5006671f 1623 HOST_WIDE_INT loff = c->lhs.offset;
910fdc79 1624
9e39dba6
RG
1625 /* Our IL does not allow this. */
1626 gcc_assert (c->rhs.offset == 0);
1627
1628 /* If the solution of y contains ANYTHING simply use the ANYTHING
1629 solution. This avoids needlessly increasing the points-to sets. */
1630 if (bitmap_bit_p (sol, anything_id))
1631 sol = get_varinfo (find (anything_id))->solution;
1632
1633 /* If the solution for x contains ANYTHING we have to merge the
1634 solution of y into all pointer variables which we do via
1635 STOREDANYTHING. */
1636 if (bitmap_bit_p (delta, anything_id))
1637 {
1638 unsigned t = find (storedanything_id);
1639 if (add_graph_edge (graph, t, rhs))
1640 {
1641 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1642 {
1643 if (!TEST_BIT (changed, t))
1644 {
1645 SET_BIT (changed, t);
1646 changed_count++;
1647 }
1648 }
1649 }
1650 return;
1651 }
4ee00913 1652
5006671f
RG
1653 /* If we do not know at with offset the rhs is dereferenced compute
1654 the reachability set of DELTA, conservatively assuming it is
1655 dereferenced at all valid offsets. */
1656 if (loff == UNKNOWN_OFFSET)
1657 {
1658 solution_set_expand (delta, delta);
1659 loff = 0;
1660 }
1661
910fdc79
DB
1662 /* For each member j of delta (Sol(x)), add an edge from y to j and
1663 union Sol(y) into Sol(j) */
1664 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1665 {
5006671f
RG
1666 varinfo_t v = get_varinfo (j);
1667 unsigned int t;
1668 HOST_WIDE_INT fieldoffset = v->offset + loff;
c58936b6 1669
46aaa417
RG
1670 /* If v is a global variable then this is an escape point. */
1671 if (v->is_global_var)
de70bb20
RG
1672 {
1673 t = find (escaped_id);
1674 if (add_graph_edge (graph, t, rhs)
1675 && bitmap_ior_into (get_varinfo (t)->solution, sol)
1676 && !TEST_BIT (changed, t))
1677 {
1678 SET_BIT (changed, t);
1679 changed_count++;
1680 }
1681 }
1682
5006671f
RG
1683 if (v->is_special_var)
1684 continue;
1685
1686 if (v->is_full_var)
1687 fieldoffset = v->offset;
1688 else if (loff != 0)
1689 v = first_vi_for_offset (v, fieldoffset);
1690 /* If the access is outside of the variable we can ignore it. */
1691 if (!v)
1692 continue;
57250223 1693
5006671f
RG
1694 do
1695 {
9e39dba6 1696 if (v->may_have_pointers)
910fdc79 1697 {
9e39dba6 1698 t = find (v->id);
de70bb20
RG
1699 if (add_graph_edge (graph, t, rhs)
1700 && bitmap_ior_into (get_varinfo (t)->solution, sol)
1701 && !TEST_BIT (changed, t))
910fdc79 1702 {
de70bb20
RG
1703 SET_BIT (changed, t);
1704 changed_count++;
1705 }
1706 }
5006671f
RG
1707
1708 /* If the variable is not exactly at the requested offset
1709 we have to include the next one. */
1710 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1711 || v->next == NULL)
1712 break;
1713
1714 v = v->next;
1715 fieldoffset = v->offset;
57250223 1716 }
5006671f 1717 while (1);
910fdc79
DB
1718 }
1719}
1720
3e5937d7
DB
1721/* Handle a non-simple (simple meaning requires no iteration),
1722 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
c58936b6 1723
910fdc79
DB
1724static void
1725do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta)
1726{
1727 if (c->lhs.type == DEREF)
1728 {
1729 if (c->rhs.type == ADDRESSOF)
1730 {
7b765bed 1731 gcc_unreachable();
910fdc79
DB
1732 }
1733 else
1734 {
1735 /* *x = y */
57250223 1736 do_ds_constraint (c, delta);
910fdc79
DB
1737 }
1738 }
57250223 1739 else if (c->rhs.type == DEREF)
910fdc79
DB
1740 {
1741 /* x = *y */
13c2c08b
DB
1742 if (!(get_varinfo (c->lhs.var)->is_special_var))
1743 do_sd_constraint (graph, c, delta);
910fdc79 1744 }
c58936b6 1745 else
57250223 1746 {
c58936b6 1747 bitmap tmp;
57250223
DB
1748 bitmap solution;
1749 bool flag = false;
57250223 1750
62e5bf5d 1751 gcc_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR);
7b765bed
DB
1752 solution = get_varinfo (c->rhs.var)->solution;
1753 tmp = get_varinfo (c->lhs.var)->solution;
57250223
DB
1754
1755 flag = set_union_with_increment (tmp, solution, c->rhs.offset);
c58936b6 1756
57250223
DB
1757 if (flag)
1758 {
7b765bed
DB
1759 get_varinfo (c->lhs.var)->solution = tmp;
1760 if (!TEST_BIT (changed, c->lhs.var))
57250223 1761 {
7b765bed 1762 SET_BIT (changed, c->lhs.var);
57250223
DB
1763 changed_count++;
1764 }
1765 }
1766 }
910fdc79
DB
1767}
1768
1769/* Initialize and return a new SCC info structure. */
1770
1771static struct scc_info *
3e5937d7 1772init_scc_info (size_t size)
910fdc79 1773{
5ed6ace5 1774 struct scc_info *si = XNEW (struct scc_info);
3e5937d7 1775 size_t i;
910fdc79
DB
1776
1777 si->current_index = 0;
1778 si->visited = sbitmap_alloc (size);
1779 sbitmap_zero (si->visited);
7b765bed
DB
1780 si->deleted = sbitmap_alloc (size);
1781 sbitmap_zero (si->deleted);
3e5937d7
DB
1782 si->node_mapping = XNEWVEC (unsigned int, size);
1783 si->dfs = XCNEWVEC (unsigned int, size);
1784
1785 for (i = 0; i < size; i++)
1786 si->node_mapping[i] = i;
1787
740e80e8 1788 si->scc_stack = VEC_alloc (unsigned, heap, 1);
910fdc79
DB
1789 return si;
1790}
1791
1792/* Free an SCC info structure pointed to by SI */
1793
1794static void
1795free_scc_info (struct scc_info *si)
c58936b6 1796{
910fdc79 1797 sbitmap_free (si->visited);
7b765bed 1798 sbitmap_free (si->deleted);
3e5937d7
DB
1799 free (si->node_mapping);
1800 free (si->dfs);
740e80e8 1801 VEC_free (unsigned, heap, si->scc_stack);
3e5937d7 1802 free (si);
910fdc79
DB
1803}
1804
1805
3e5937d7
DB
1806/* Find indirect cycles in GRAPH that occur, using strongly connected
1807 components, and note them in the indirect cycles map.
1808
1809 This technique comes from Ben Hardekopf and Calvin Lin,
1810 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1811 Lines of Code", submitted to PLDI 2007. */
910fdc79
DB
1812
1813static void
3e5937d7 1814find_indirect_cycles (constraint_graph_t graph)
910fdc79
DB
1815{
1816 unsigned int i;
3e5937d7
DB
1817 unsigned int size = graph->size;
1818 struct scc_info *si = init_scc_info (size);
910fdc79 1819
3e5937d7
DB
1820 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1821 if (!TEST_BIT (si->visited, i) && find (i) == i)
910fdc79 1822 scc_visit (graph, si, i);
c58936b6 1823
910fdc79
DB
1824 free_scc_info (si);
1825}
1826
1827/* Compute a topological ordering for GRAPH, and store the result in the
1828 topo_info structure TI. */
1829
c58936b6 1830static void
910fdc79
DB
1831compute_topo_order (constraint_graph_t graph,
1832 struct topo_info *ti)
1833{
1834 unsigned int i;
7b765bed 1835 unsigned int size = graph->size;
c58936b6 1836
910fdc79 1837 for (i = 0; i != size; ++i)
3e5937d7 1838 if (!TEST_BIT (ti->visited, i) && find (i) == i)
910fdc79
DB
1839 topo_visit (graph, ti, i);
1840}
1841
7b765bed
DB
1842/* Structure used to for hash value numbering of pointer equivalence
1843 classes. */
1844
1845typedef struct equiv_class_label
1846{
3691626c 1847 hashval_t hashcode;
7b765bed
DB
1848 unsigned int equivalence_class;
1849 bitmap labels;
7b765bed 1850} *equiv_class_label_t;
586de218 1851typedef const struct equiv_class_label *const_equiv_class_label_t;
7b765bed
DB
1852
1853/* A hashtable for mapping a bitmap of labels->pointer equivalence
1854 classes. */
1855static htab_t pointer_equiv_class_table;
1856
1857/* A hashtable for mapping a bitmap of labels->location equivalence
1858 classes. */
1859static htab_t location_equiv_class_table;
1860
1861/* Hash function for a equiv_class_label_t */
1862
1863static hashval_t
1864equiv_class_label_hash (const void *p)
1865{
586de218 1866 const_equiv_class_label_t const ecl = (const_equiv_class_label_t) p;
7b765bed
DB
1867 return ecl->hashcode;
1868}
1869
1870/* Equality function for two equiv_class_label_t's. */
1871
1872static int
1873equiv_class_label_eq (const void *p1, const void *p2)
1874{
586de218
KG
1875 const_equiv_class_label_t const eql1 = (const_equiv_class_label_t) p1;
1876 const_equiv_class_label_t const eql2 = (const_equiv_class_label_t) p2;
821bb7f8
RG
1877 return (eql1->hashcode == eql2->hashcode
1878 && bitmap_equal_p (eql1->labels, eql2->labels));
7b765bed
DB
1879}
1880
1881/* Lookup a equivalence class in TABLE by the bitmap of LABELS it
1882 contains. */
1883
1884static unsigned int
1885equiv_class_lookup (htab_t table, bitmap labels)
1886{
1887 void **slot;
1888 struct equiv_class_label ecl;
1889
1890 ecl.labels = labels;
1891 ecl.hashcode = bitmap_hash (labels);
c58936b6 1892
7b765bed
DB
1893 slot = htab_find_slot_with_hash (table, &ecl,
1894 ecl.hashcode, NO_INSERT);
1895 if (!slot)
1896 return 0;
1897 else
1898 return ((equiv_class_label_t) *slot)->equivalence_class;
1899}
1900
1901
1902/* Add an equivalence class named EQUIVALENCE_CLASS with labels LABELS
1903 to TABLE. */
1904
1905static void
1906equiv_class_add (htab_t table, unsigned int equivalence_class,
1907 bitmap labels)
1908{
1909 void **slot;
1910 equiv_class_label_t ecl = XNEW (struct equiv_class_label);
1911
1912 ecl->labels = labels;
1913 ecl->equivalence_class = equivalence_class;
1914 ecl->hashcode = bitmap_hash (labels);
1915
1916 slot = htab_find_slot_with_hash (table, ecl,
1917 ecl->hashcode, INSERT);
1918 gcc_assert (!*slot);
1919 *slot = (void *) ecl;
1920}
1921
1922/* Perform offline variable substitution.
910fdc79 1923
7b765bed
DB
1924 This is a worst case quadratic time way of identifying variables
1925 that must have equivalent points-to sets, including those caused by
1926 static cycles, and single entry subgraphs, in the constraint graph.
3e5937d7 1927
7b765bed
DB
1928 The technique is described in "Exploiting Pointer and Location
1929 Equivalence to Optimize Pointer Analysis. In the 14th International
1930 Static Analysis Symposium (SAS), August 2007." It is known as the
1931 "HU" algorithm, and is equivalent to value numbering the collapsed
1932 constraint graph including evaluating unions.
3e5937d7
DB
1933
1934 The general method of finding equivalence classes is as follows:
1935 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
7b765bed
DB
1936 Initialize all non-REF nodes to be direct nodes.
1937 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1938 variable}
1939 For each constraint containing the dereference, we also do the same
1940 thing.
1941
1942 We then compute SCC's in the graph and unify nodes in the same SCC,
1943 including pts sets.
1944
1945 For each non-collapsed node x:
1946 Visit all unvisited explicit incoming edges.
1947 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1948 where y->x.
1949 Lookup the equivalence class for pts(x).
1950 If we found one, equivalence_class(x) = found class.
1951 Otherwise, equivalence_class(x) = new class, and new_class is
1952 added to the lookup table.
3e5937d7
DB
1953
1954 All direct nodes with the same equivalence class can be replaced
1955 with a single representative node.
1956 All unlabeled nodes (label == 0) are not pointers and all edges
1957 involving them can be eliminated.
7b765bed
DB
1958 We perform these optimizations during rewrite_constraints
1959
1960 In addition to pointer equivalence class finding, we also perform
1961 location equivalence class finding. This is the set of variables
1962 that always appear together in points-to sets. We use this to
1963 compress the size of the points-to sets. */
1964
1965/* Current maximum pointer equivalence class id. */
1966static int pointer_equiv_class;
3e5937d7 1967
7b765bed
DB
1968/* Current maximum location equivalence class id. */
1969static int location_equiv_class;
3e5937d7
DB
1970
1971/* Recursive routine to find strongly connected components in GRAPH,
7b765bed 1972 and label it's nodes with DFS numbers. */
910fdc79
DB
1973
1974static void
7b765bed 1975condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
910fdc79 1976{
3e5937d7
DB
1977 unsigned int i;
1978 bitmap_iterator bi;
1979 unsigned int my_dfs;
c58936b6 1980
3e5937d7
DB
1981 gcc_assert (si->node_mapping[n] == n);
1982 SET_BIT (si->visited, n);
1983 si->dfs[n] = si->current_index ++;
1984 my_dfs = si->dfs[n];
c58936b6 1985
3e5937d7
DB
1986 /* Visit all the successors. */
1987 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
910fdc79 1988 {
3e5937d7 1989 unsigned int w = si->node_mapping[i];
910fdc79 1990
7b765bed 1991 if (TEST_BIT (si->deleted, w))
910fdc79
DB
1992 continue;
1993
3e5937d7 1994 if (!TEST_BIT (si->visited, w))
7b765bed 1995 condense_visit (graph, si, w);
3e5937d7
DB
1996 {
1997 unsigned int t = si->node_mapping[w];
1998 unsigned int nnode = si->node_mapping[n];
62e5bf5d 1999 gcc_assert (nnode == n);
910fdc79 2000
3e5937d7
DB
2001 if (si->dfs[t] < si->dfs[nnode])
2002 si->dfs[n] = si->dfs[t];
2003 }
2004 }
910fdc79 2005
3e5937d7
DB
2006 /* Visit all the implicit predecessors. */
2007 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2008 {
2009 unsigned int w = si->node_mapping[i];
2010
7b765bed 2011 if (TEST_BIT (si->deleted, w))
3e5937d7
DB
2012 continue;
2013
2014 if (!TEST_BIT (si->visited, w))
7b765bed 2015 condense_visit (graph, si, w);
3e5937d7
DB
2016 {
2017 unsigned int t = si->node_mapping[w];
2018 unsigned int nnode = si->node_mapping[n];
2019 gcc_assert (nnode == n);
2020
2021 if (si->dfs[t] < si->dfs[nnode])
2022 si->dfs[n] = si->dfs[t];
2023 }
2024 }
4ee00913 2025
3e5937d7
DB
2026 /* See if any components have been identified. */
2027 if (si->dfs[n] == my_dfs)
2028 {
2029 while (VEC_length (unsigned, si->scc_stack) != 0
2030 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
910fdc79 2031 {
3e5937d7
DB
2032 unsigned int w = VEC_pop (unsigned, si->scc_stack);
2033 si->node_mapping[w] = n;
2034
2035 if (!TEST_BIT (graph->direct_nodes, w))
2036 RESET_BIT (graph->direct_nodes, n);
3e5937d7 2037
7b765bed
DB
2038 /* Unify our nodes. */
2039 if (graph->preds[w])
2040 {
2041 if (!graph->preds[n])
2042 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2043 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2044 }
2045 if (graph->implicit_preds[w])
2046 {
2047 if (!graph->implicit_preds[n])
2048 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2049 bitmap_ior_into (graph->implicit_preds[n],
2050 graph->implicit_preds[w]);
2051 }
2052 if (graph->points_to[w])
2053 {
2054 if (!graph->points_to[n])
2055 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2056 bitmap_ior_into (graph->points_to[n],
2057 graph->points_to[w]);
2058 }
3e5937d7 2059 }
7b765bed 2060 SET_BIT (si->deleted, n);
3e5937d7
DB
2061 }
2062 else
2063 VEC_safe_push (unsigned, heap, si->scc_stack, n);
2064}
2065
7b765bed
DB
2066/* Label pointer equivalences. */
2067
2068static void
2069label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2070{
2071 unsigned int i;
2072 bitmap_iterator bi;
2073 SET_BIT (si->visited, n);
2074
2075 if (!graph->points_to[n])
2076 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2077
2078 /* Label and union our incoming edges's points to sets. */
2079 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2080 {
2081 unsigned int w = si->node_mapping[i];
2082 if (!TEST_BIT (si->visited, w))
2083 label_visit (graph, si, w);
2084
2085 /* Skip unused edges */
2086 if (w == n || graph->pointer_label[w] == 0)
3dc21182
DB
2087 continue;
2088
7b765bed
DB
2089 if (graph->points_to[w])
2090 bitmap_ior_into(graph->points_to[n], graph->points_to[w]);
7b765bed
DB
2091 }
2092 /* Indirect nodes get fresh variables. */
2093 if (!TEST_BIT (graph->direct_nodes, n))
2094 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2095
2096 if (!bitmap_empty_p (graph->points_to[n]))
2097 {
2098 unsigned int label = equiv_class_lookup (pointer_equiv_class_table,
2099 graph->points_to[n]);
2100 if (!label)
2101 {
7b765bed
DB
2102 label = pointer_equiv_class++;
2103 equiv_class_add (pointer_equiv_class_table,
2104 label, graph->points_to[n]);
2105 }
2106 graph->pointer_label[n] = label;
2107 }
2108}
2109
3e5937d7
DB
2110/* Perform offline variable substitution, discovering equivalence
2111 classes, and eliminating non-pointer variables. */
2112
2113static struct scc_info *
2114perform_var_substitution (constraint_graph_t graph)
2115{
2116 unsigned int i;
2117 unsigned int size = graph->size;
2118 struct scc_info *si = init_scc_info (size);
2119
2120 bitmap_obstack_initialize (&iteration_obstack);
7b765bed
DB
2121 pointer_equiv_class_table = htab_create (511, equiv_class_label_hash,
2122 equiv_class_label_eq, free);
2123 location_equiv_class_table = htab_create (511, equiv_class_label_hash,
2124 equiv_class_label_eq, free);
2125 pointer_equiv_class = 1;
2126 location_equiv_class = 1;
2127
2128 /* Condense the nodes, which means to find SCC's, count incoming
2129 predecessors, and unite nodes in SCC's. */
aa46c8a3 2130 for (i = 0; i < FIRST_REF_NODE; i++)
7b765bed
DB
2131 if (!TEST_BIT (si->visited, si->node_mapping[i]))
2132 condense_visit (graph, si, si->node_mapping[i]);
3e5937d7 2133
7b765bed
DB
2134 sbitmap_zero (si->visited);
2135 /* Actually the label the nodes for pointer equivalences */
aa46c8a3 2136 for (i = 0; i < FIRST_REF_NODE; i++)
3e5937d7
DB
2137 if (!TEST_BIT (si->visited, si->node_mapping[i]))
2138 label_visit (graph, si, si->node_mapping[i]);
2139
7b765bed
DB
2140 /* Calculate location equivalence labels. */
2141 for (i = 0; i < FIRST_REF_NODE; i++)
2142 {
2143 bitmap pointed_by;
2144 bitmap_iterator bi;
2145 unsigned int j;
2146 unsigned int label;
2147
2148 if (!graph->pointed_by[i])
2149 continue;
2150 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2151
2152 /* Translate the pointed-by mapping for pointer equivalence
2153 labels. */
2154 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2155 {
2156 bitmap_set_bit (pointed_by,
2157 graph->pointer_label[si->node_mapping[j]]);
2158 }
2159 /* The original pointed_by is now dead. */
2160 BITMAP_FREE (graph->pointed_by[i]);
2161
2162 /* Look up the location equivalence label if one exists, or make
2163 one otherwise. */
2164 label = equiv_class_lookup (location_equiv_class_table,
2165 pointed_by);
2166 if (label == 0)
2167 {
2168 label = location_equiv_class++;
2169 equiv_class_add (location_equiv_class_table,
2170 label, pointed_by);
2171 }
2172 else
2173 {
2174 if (dump_file && (dump_flags & TDF_DETAILS))
2175 fprintf (dump_file, "Found location equivalence for node %s\n",
2176 get_varinfo (i)->name);
2177 BITMAP_FREE (pointed_by);
2178 }
2179 graph->loc_label[i] = label;
2180
2181 }
2182
3e5937d7
DB
2183 if (dump_file && (dump_flags & TDF_DETAILS))
2184 for (i = 0; i < FIRST_REF_NODE; i++)
2185 {
2186 bool direct_node = TEST_BIT (graph->direct_nodes, i);
2187 fprintf (dump_file,
7b765bed
DB
2188 "Equivalence classes for %s node id %d:%s are pointer: %d"
2189 ", location:%d\n",
3e5937d7 2190 direct_node ? "Direct node" : "Indirect node", i,
62e5bf5d 2191 get_varinfo (i)->name,
7b765bed
DB
2192 graph->pointer_label[si->node_mapping[i]],
2193 graph->loc_label[si->node_mapping[i]]);
3e5937d7
DB
2194 }
2195
2196 /* Quickly eliminate our non-pointer variables. */
2197
2198 for (i = 0; i < FIRST_REF_NODE; i++)
2199 {
2200 unsigned int node = si->node_mapping[i];
2201
aa46c8a3 2202 if (graph->pointer_label[node] == 0)
3e5937d7 2203 {
23e73993 2204 if (dump_file && (dump_flags & TDF_DETAILS))
3e5937d7
DB
2205 fprintf (dump_file,
2206 "%s is a non-pointer variable, eliminating edges.\n",
2207 get_varinfo (node)->name);
2208 stats.nonpointer_vars++;
2209 clear_edges_for_node (graph, node);
910fdc79
DB
2210 }
2211 }
7b765bed 2212
3e5937d7
DB
2213 return si;
2214}
2215
2216/* Free information that was only necessary for variable
2217 substitution. */
910fdc79 2218
3e5937d7
DB
2219static void
2220free_var_substitution_info (struct scc_info *si)
2221{
2222 free_scc_info (si);
7b765bed
DB
2223 free (graph->pointer_label);
2224 free (graph->loc_label);
2225 free (graph->pointed_by);
2226 free (graph->points_to);
3e5937d7
DB
2227 free (graph->eq_rep);
2228 sbitmap_free (graph->direct_nodes);
7b765bed
DB
2229 htab_delete (pointer_equiv_class_table);
2230 htab_delete (location_equiv_class_table);
4ee00913 2231 bitmap_obstack_release (&iteration_obstack);
3e5937d7
DB
2232}
2233
2234/* Return an existing node that is equivalent to NODE, which has
2235 equivalence class LABEL, if one exists. Return NODE otherwise. */
2236
2237static unsigned int
2238find_equivalent_node (constraint_graph_t graph,
2239 unsigned int node, unsigned int label)
2240{
2241 /* If the address version of this variable is unused, we can
2242 substitute it for anything else with the same label.
2243 Otherwise, we know the pointers are equivalent, but not the
7b765bed 2244 locations, and we can unite them later. */
3e5937d7 2245
7b765bed 2246 if (!bitmap_bit_p (graph->address_taken, node))
3e5937d7
DB
2247 {
2248 gcc_assert (label < graph->size);
2249
2250 if (graph->eq_rep[label] != -1)
2251 {
2252 /* Unify the two variables since we know they are equivalent. */
2253 if (unite (graph->eq_rep[label], node))
2254 unify_nodes (graph, graph->eq_rep[label], node, false);
2255 return graph->eq_rep[label];
2256 }
2257 else
2258 {
2259 graph->eq_rep[label] = node;
7b765bed 2260 graph->pe_rep[label] = node;
3e5937d7
DB
2261 }
2262 }
7b765bed
DB
2263 else
2264 {
2265 gcc_assert (label < graph->size);
2266 graph->pe[node] = label;
2267 if (graph->pe_rep[label] == -1)
2268 graph->pe_rep[label] = node;
2269 }
2270
3e5937d7
DB
2271 return node;
2272}
2273
7b765bed
DB
2274/* Unite pointer equivalent but not location equivalent nodes in
2275 GRAPH. This may only be performed once variable substitution is
2276 finished. */
2277
2278static void
2279unite_pointer_equivalences (constraint_graph_t graph)
2280{
2281 unsigned int i;
2282
2283 /* Go through the pointer equivalences and unite them to their
2284 representative, if they aren't already. */
aa46c8a3 2285 for (i = 0; i < FIRST_REF_NODE; i++)
7b765bed
DB
2286 {
2287 unsigned int label = graph->pe[i];
aa46c8a3
DB
2288 if (label)
2289 {
2290 int label_rep = graph->pe_rep[label];
b8698a0f 2291
aa46c8a3
DB
2292 if (label_rep == -1)
2293 continue;
b8698a0f 2294
aa46c8a3
DB
2295 label_rep = find (label_rep);
2296 if (label_rep >= 0 && unite (label_rep, find (i)))
2297 unify_nodes (graph, label_rep, i, false);
2298 }
7b765bed
DB
2299 }
2300}
2301
2302/* Move complex constraints to the GRAPH nodes they belong to. */
3e5937d7
DB
2303
2304static void
7b765bed
DB
2305move_complex_constraints (constraint_graph_t graph)
2306{
2307 int i;
2308 constraint_t c;
2309
2310 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
2311 {
2312 if (c)
2313 {
2314 struct constraint_expr lhs = c->lhs;
2315 struct constraint_expr rhs = c->rhs;
2316
2317 if (lhs.type == DEREF)
2318 {
2319 insert_into_complex (graph, lhs.var, c);
2320 }
2321 else if (rhs.type == DEREF)
2322 {
2323 if (!(get_varinfo (lhs.var)->is_special_var))
2324 insert_into_complex (graph, rhs.var, c);
2325 }
2326 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2327 && (lhs.offset != 0 || rhs.offset != 0))
2328 {
2329 insert_into_complex (graph, rhs.var, c);
2330 }
2331 }
2332 }
2333}
2334
2335
2336/* Optimize and rewrite complex constraints while performing
2337 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2338 result of perform_variable_substitution. */
2339
2340static void
2341rewrite_constraints (constraint_graph_t graph,
2342 struct scc_info *si)
3e5937d7
DB
2343{
2344 int i;
2345 unsigned int j;
2346 constraint_t c;
2347
2348 for (j = 0; j < graph->size; j++)
2349 gcc_assert (find (j) == j);
2350
2351 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
2352 {
2353 struct constraint_expr lhs = c->lhs;
2354 struct constraint_expr rhs = c->rhs;
5006671f
RG
2355 unsigned int lhsvar = find (lhs.var);
2356 unsigned int rhsvar = find (rhs.var);
3e5937d7
DB
2357 unsigned int lhsnode, rhsnode;
2358 unsigned int lhslabel, rhslabel;
2359
2360 lhsnode = si->node_mapping[lhsvar];
2361 rhsnode = si->node_mapping[rhsvar];
7b765bed
DB
2362 lhslabel = graph->pointer_label[lhsnode];
2363 rhslabel = graph->pointer_label[rhsnode];
3e5937d7
DB
2364
2365 /* See if it is really a non-pointer variable, and if so, ignore
2366 the constraint. */
2367 if (lhslabel == 0)
2368 {
aa46c8a3 2369 if (dump_file && (dump_flags & TDF_DETAILS))
3e5937d7 2370 {
b8698a0f 2371
aa46c8a3
DB
2372 fprintf (dump_file, "%s is a non-pointer variable,"
2373 "ignoring constraint:",
2374 get_varinfo (lhs.var)->name);
2375 dump_constraint (dump_file, c);
3e5937d7 2376 }
aa46c8a3
DB
2377 VEC_replace (constraint_t, constraints, i, NULL);
2378 continue;
3e5937d7
DB
2379 }
2380
2381 if (rhslabel == 0)
2382 {
aa46c8a3 2383 if (dump_file && (dump_flags & TDF_DETAILS))
3e5937d7 2384 {
b8698a0f 2385
aa46c8a3
DB
2386 fprintf (dump_file, "%s is a non-pointer variable,"
2387 "ignoring constraint:",
2388 get_varinfo (rhs.var)->name);
2389 dump_constraint (dump_file, c);
3e5937d7 2390 }
aa46c8a3
DB
2391 VEC_replace (constraint_t, constraints, i, NULL);
2392 continue;
3e5937d7
DB
2393 }
2394
2395 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2396 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2397 c->lhs.var = lhsvar;
2398 c->rhs.var = rhsvar;
2399
3e5937d7
DB
2400 }
2401}
2402
2403/* Eliminate indirect cycles involving NODE. Return true if NODE was
2404 part of an SCC, false otherwise. */
2405
2406static bool
2407eliminate_indirect_cycles (unsigned int node)
2408{
2409 if (graph->indirect_cycles[node] != -1
2410 && !bitmap_empty_p (get_varinfo (node)->solution))
2411 {
2412 unsigned int i;
2413 VEC(unsigned,heap) *queue = NULL;
2414 int queuepos;
2415 unsigned int to = find (graph->indirect_cycles[node]);
2416 bitmap_iterator bi;
2417
2418 /* We can't touch the solution set and call unify_nodes
2419 at the same time, because unify_nodes is going to do
2420 bitmap unions into it. */
2421
2422 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2423 {
2424 if (find (i) == i && i != to)
2425 {
2426 if (unite (to, i))
2427 VEC_safe_push (unsigned, heap, queue, i);
2428 }
2429 }
2430
2431 for (queuepos = 0;
2432 VEC_iterate (unsigned, queue, queuepos, i);
2433 queuepos++)
2434 {
2435 unify_nodes (graph, to, i, true);
2436 }
2437 VEC_free (unsigned, heap, queue);
2438 return true;
2439 }
2440 return false;
910fdc79
DB
2441}
2442
910fdc79
DB
2443/* Solve the constraint graph GRAPH using our worklist solver.
2444 This is based on the PW* family of solvers from the "Efficient Field
2445 Sensitive Pointer Analysis for C" paper.
2446 It works by iterating over all the graph nodes, processing the complex
2447 constraints and propagating the copy constraints, until everything stops
2448 changed. This corresponds to steps 6-8 in the solving list given above. */
2449
2450static void
2451solve_graph (constraint_graph_t graph)
2452{
7b765bed 2453 unsigned int size = graph->size;
910fdc79 2454 unsigned int i;
3e5937d7 2455 bitmap pts;
910fdc79 2456
3e5937d7 2457 changed_count = 0;
910fdc79 2458 changed = sbitmap_alloc (size);
3e5937d7 2459 sbitmap_zero (changed);
c58936b6 2460
3e5937d7 2461 /* Mark all initial non-collapsed nodes as changed. */
910fdc79 2462 for (i = 0; i < size; i++)
3e5937d7
DB
2463 {
2464 varinfo_t ivi = get_varinfo (i);
2465 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2466 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2467 || VEC_length (constraint_t, graph->complex[i]) > 0))
2468 {
2469 SET_BIT (changed, i);
2470 changed_count++;
2471 }
2472 }
2473
2474 /* Allocate a bitmap to be used to store the changed bits. */
2475 pts = BITMAP_ALLOC (&pta_obstack);
c58936b6 2476
910fdc79
DB
2477 while (changed_count > 0)
2478 {
2479 unsigned int i;
2480 struct topo_info *ti = init_topo_info ();
2481 stats.iterations++;
4ee00913 2482
910fdc79 2483 bitmap_obstack_initialize (&iteration_obstack);
c58936b6 2484
910fdc79
DB
2485 compute_topo_order (graph, ti);
2486
740e80e8 2487 while (VEC_length (unsigned, ti->topo_order) != 0)
910fdc79 2488 {
3e5937d7 2489
740e80e8 2490 i = VEC_pop (unsigned, ti->topo_order);
3e5937d7
DB
2491
2492 /* If this variable is not a representative, skip it. */
2493 if (find (i) != i)
2494 continue;
2495
d3c36974
DB
2496 /* In certain indirect cycle cases, we may merge this
2497 variable to another. */
62e5bf5d 2498 if (eliminate_indirect_cycles (i) && find (i) != i)
d3c36974 2499 continue;
910fdc79
DB
2500
2501 /* If the node has changed, we need to process the
2502 complex constraints and outgoing edges again. */
2503 if (TEST_BIT (changed, i))
2504 {
2505 unsigned int j;
2506 constraint_t c;
910fdc79 2507 bitmap solution;
3e5937d7 2508 VEC(constraint_t,heap) *complex = graph->complex[i];
21392f19 2509 bool solution_empty;
48e540b0 2510
910fdc79
DB
2511 RESET_BIT (changed, i);
2512 changed_count--;
2513
3e5937d7
DB
2514 /* Compute the changed set of solution bits. */
2515 bitmap_and_compl (pts, get_varinfo (i)->solution,
2516 get_varinfo (i)->oldsolution);
2517
2518 if (bitmap_empty_p (pts))
2519 continue;
2520
2521 bitmap_ior_into (get_varinfo (i)->oldsolution, pts);
2522
910fdc79 2523 solution = get_varinfo (i)->solution;
21392f19
DB
2524 solution_empty = bitmap_empty_p (solution);
2525
2526 /* Process the complex constraints */
910fdc79 2527 for (j = 0; VEC_iterate (constraint_t, complex, j, c); j++)
21392f19 2528 {
7b765bed
DB
2529 /* XXX: This is going to unsort the constraints in
2530 some cases, which will occasionally add duplicate
2531 constraints during unification. This does not
2532 affect correctness. */
2533 c->lhs.var = find (c->lhs.var);
2534 c->rhs.var = find (c->rhs.var);
2535
21392f19
DB
2536 /* The only complex constraint that can change our
2537 solution to non-empty, given an empty solution,
2538 is a constraint where the lhs side is receiving
2539 some set from elsewhere. */
2540 if (!solution_empty || c->lhs.type != DEREF)
3e5937d7 2541 do_complex_constraint (graph, c, pts);
21392f19 2542 }
910fdc79 2543
21392f19
DB
2544 solution_empty = bitmap_empty_p (solution);
2545
5006671f 2546 if (!solution_empty)
4ee00913 2547 {
3e5937d7 2548 bitmap_iterator bi;
5006671f 2549 unsigned eff_escaped_id = find (escaped_id);
3e5937d7 2550
21392f19 2551 /* Propagate solution to all successors. */
c58936b6 2552 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
21392f19 2553 0, j, bi)
4ee00913 2554 {
3e5937d7
DB
2555 bitmap tmp;
2556 bool flag;
2557
2558 unsigned int to = find (j);
2559 tmp = get_varinfo (to)->solution;
2560 flag = false;
c58936b6 2561
3e5937d7
DB
2562 /* Don't try to propagate to ourselves. */
2563 if (to == i)
2564 continue;
c58936b6 2565
5006671f
RG
2566 /* If we propagate from ESCAPED use ESCAPED as
2567 placeholder. */
2568 if (i == eff_escaped_id)
2569 flag = bitmap_set_bit (tmp, escaped_id);
2570 else
2571 flag = set_union_with_increment (tmp, pts, 0);
c58936b6 2572
21392f19 2573 if (flag)
4ee00913 2574 {
3e5937d7
DB
2575 get_varinfo (to)->solution = tmp;
2576 if (!TEST_BIT (changed, to))
21392f19 2577 {
3e5937d7 2578 SET_BIT (changed, to);
21392f19
DB
2579 changed_count++;
2580 }
4ee00913
DB
2581 }
2582 }
910fdc79
DB
2583 }
2584 }
2585 }
2586 free_topo_info (ti);
2587 bitmap_obstack_release (&iteration_obstack);
2588 }
c58936b6 2589
3e5937d7 2590 BITMAP_FREE (pts);
910fdc79 2591 sbitmap_free (changed);
3e5937d7 2592 bitmap_obstack_release (&oldpta_obstack);
910fdc79
DB
2593}
2594
3e5937d7 2595/* Map from trees to variable infos. */
15814ba0 2596static struct pointer_map_t *vi_for_tree;
910fdc79 2597
910fdc79 2598
15814ba0 2599/* Insert ID as the variable id for tree T in the vi_for_tree map. */
910fdc79 2600
c58936b6 2601static void
3e5937d7 2602insert_vi_for_tree (tree t, varinfo_t vi)
910fdc79 2603{
15814ba0
PB
2604 void **slot = pointer_map_insert (vi_for_tree, t);
2605 gcc_assert (vi);
910fdc79 2606 gcc_assert (*slot == NULL);
15814ba0 2607 *slot = vi;
910fdc79
DB
2608}
2609
3e5937d7 2610/* Find the variable info for tree T in VI_FOR_TREE. If T does not
15814ba0 2611 exist in the map, return NULL, otherwise, return the varinfo we found. */
910fdc79 2612
15814ba0
PB
2613static varinfo_t
2614lookup_vi_for_tree (tree t)
910fdc79 2615{
15814ba0
PB
2616 void **slot = pointer_map_contains (vi_for_tree, t);
2617 if (slot == NULL)
2618 return NULL;
910fdc79 2619
15814ba0 2620 return (varinfo_t) *slot;
910fdc79
DB
2621}
2622
2623/* Return a printable name for DECL */
2624
2625static const char *
2626alias_get_name (tree decl)
2627{
2628 const char *res = get_name (decl);
2629 char *temp;
2630 int num_printed = 0;
2631
2632 if (res != NULL)
2633 return res;
2634
2635 res = "NULL";
4f6c9110
RG
2636 if (!dump_file)
2637 return res;
2638
910fdc79
DB
2639 if (TREE_CODE (decl) == SSA_NAME)
2640 {
c58936b6 2641 num_printed = asprintf (&temp, "%s_%u",
910fdc79
DB
2642 alias_get_name (SSA_NAME_VAR (decl)),
2643 SSA_NAME_VERSION (decl));
2644 }
2645 else if (DECL_P (decl))
2646 {
2647 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2648 }
2649 if (num_printed > 0)
2650 {
2651 res = ggc_strdup (temp);
2652 free (temp);
2653 }
2654 return res;
2655}
2656
15814ba0
PB
2657/* Find the variable id for tree T in the map.
2658 If T doesn't exist in the map, create an entry for it and return it. */
910fdc79 2659
3e5937d7
DB
2660static varinfo_t
2661get_vi_for_tree (tree t)
910fdc79 2662{
15814ba0
PB
2663 void **slot = pointer_map_contains (vi_for_tree, t);
2664 if (slot == NULL)
3e5937d7 2665 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
c58936b6 2666
15814ba0 2667 return (varinfo_t) *slot;
910fdc79
DB
2668}
2669
b14e9388 2670/* Get a scalar constraint expression for a new temporary variable. */
910fdc79
DB
2671
2672static struct constraint_expr
b14e9388 2673new_scalar_tmp_constraint_exp (const char *name)
910fdc79 2674{
b14e9388 2675 struct constraint_expr tmp;
b14e9388 2676 varinfo_t vi;
910fdc79 2677
0bbf2ffa 2678 vi = new_var_info (NULL_TREE, name);
b14e9388
RG
2679 vi->offset = 0;
2680 vi->size = -1;
2681 vi->fullsize = -1;
2682 vi->is_full_var = 1;
c0d459f0 2683
b14e9388
RG
2684 tmp.var = vi->id;
2685 tmp.type = SCALAR;
2686 tmp.offset = 0;
c0d459f0 2687
b14e9388 2688 return tmp;
c0d459f0
RG
2689}
2690
2691/* Get a constraint expression vector from an SSA_VAR_P node.
2692 If address_p is true, the result will be taken its address of. */
2693
2694static void
2695get_constraint_for_ssa_var (tree t, VEC(ce_s, heap) **results, bool address_p)
2696{
2697 struct constraint_expr cexpr;
2698 varinfo_t vi;
2699
2700 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
910fdc79
DB
2701 gcc_assert (SSA_VAR_P (t) || DECL_P (t));
2702
2703 /* For parameters, get at the points-to set for the actual parm
2704 decl. */
c58936b6
DB
2705 if (TREE_CODE (t) == SSA_NAME
2706 && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
38635499 2707 && SSA_NAME_IS_DEFAULT_DEF (t))
c0d459f0
RG
2708 {
2709 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2710 return;
2711 }
910fdc79 2712
c0d459f0
RG
2713 vi = get_vi_for_tree (t);
2714 cexpr.var = vi->id;
910fdc79 2715 cexpr.type = SCALAR;
c0d459f0 2716 cexpr.offset = 0;
47bcb538
DB
2717 /* If we determine the result is "anything", and we know this is readonly,
2718 say it points to readonly memory instead. */
2719 if (cexpr.var == anything_id && TREE_READONLY (t))
910fdc79 2720 {
c0d459f0 2721 gcc_unreachable ();
3e5937d7 2722 cexpr.type = ADDRESSOF;
910fdc79
DB
2723 cexpr.var = readonly_id;
2724 }
c58936b6 2725
c0d459f0
RG
2726 /* If we are not taking the address of the constraint expr, add all
2727 sub-fiels of the variable as well. */
de925a03
RG
2728 if (!address_p
2729 && !vi->is_full_var)
c0d459f0
RG
2730 {
2731 for (; vi; vi = vi->next)
2732 {
2733 cexpr.var = vi->id;
2734 VEC_safe_push (ce_s, heap, *results, &cexpr);
2735 }
2736 return;
2737 }
2738
2739 VEC_safe_push (ce_s, heap, *results, &cexpr);
910fdc79
DB
2740}
2741
faf2ecc5
RG
2742/* Process constraint T, performing various simplifications and then
2743 adding it to our list of overall constraints. */
910fdc79
DB
2744
2745static void
faf2ecc5 2746process_constraint (constraint_t t)
910fdc79
DB
2747{
2748 struct constraint_expr rhs = t->rhs;
2749 struct constraint_expr lhs = t->lhs;
c58936b6 2750
910fdc79
DB
2751 gcc_assert (rhs.var < VEC_length (varinfo_t, varmap));
2752 gcc_assert (lhs.var < VEC_length (varinfo_t, varmap));
2753
5006671f
RG
2754 /* If we didn't get any useful constraint from the lhs we get
2755 &ANYTHING as fallback from get_constraint_for. Deal with
2756 it here by turning it into *ANYTHING. */
2757 if (lhs.type == ADDRESSOF
2758 && lhs.var == anything_id)
2759 lhs.type = DEREF;
2760
2761 /* ADDRESSOF on the lhs is invalid. */
2762 gcc_assert (lhs.type != ADDRESSOF);
910fdc79 2763
910fdc79 2764 /* This can happen in our IR with things like n->a = *p */
5006671f 2765 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
910fdc79
DB
2766 {
2767 /* Split into tmp = *rhs, *lhs = tmp */
b14e9388
RG
2768 struct constraint_expr tmplhs;
2769 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp");
faf2ecc5
RG
2770 process_constraint (new_constraint (tmplhs, rhs));
2771 process_constraint (new_constraint (lhs, tmplhs));
7b765bed
DB
2772 }
2773 else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
2774 {
2775 /* Split into tmp = &rhs, *lhs = tmp */
b14e9388
RG
2776 struct constraint_expr tmplhs;
2777 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp");
faf2ecc5
RG
2778 process_constraint (new_constraint (tmplhs, rhs));
2779 process_constraint (new_constraint (lhs, tmplhs));
910fdc79 2780 }
910fdc79
DB
2781 else
2782 {
3e5937d7 2783 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
b5efa470 2784 VEC_safe_push (constraint_t, heap, constraints, t);
910fdc79
DB
2785 }
2786}
2787
9e39dba6
RG
2788/* Return true if T is a type that could contain pointers. */
2789
2790static bool
2791type_could_have_pointers (tree type)
2792{
2793 if (POINTER_TYPE_P (type))
2794 return true;
2795
2796 if (TREE_CODE (type) == ARRAY_TYPE)
2797 return type_could_have_pointers (TREE_TYPE (type));
2798
2799 return AGGREGATE_TYPE_P (type);
2800}
2801
21392f19
DB
2802/* Return true if T is a variable of a type that could contain
2803 pointers. */
2804
2805static bool
2806could_have_pointers (tree t)
2807{
9e39dba6 2808 return type_could_have_pointers (TREE_TYPE (t));
21392f19 2809}
910fdc79
DB
2810
2811/* Return the position, in bits, of FIELD_DECL from the beginning of its
2812 structure. */
2813
ee7d4b57 2814static HOST_WIDE_INT
910fdc79
DB
2815bitpos_of_field (const tree fdecl)
2816{
2817
ee7d4b57
RG
2818 if (!host_integerp (DECL_FIELD_OFFSET (fdecl), 0)
2819 || !host_integerp (DECL_FIELD_BIT_OFFSET (fdecl), 0))
910fdc79 2820 return -1;
c58936b6 2821
ee7d4b57
RG
2822 return (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (fdecl)) * 8
2823 + TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (fdecl)));
910fdc79
DB
2824}
2825
2826
e5bae89b
RG
2827/* Get constraint expressions for offsetting PTR by OFFSET. Stores the
2828 resulting constraint expressions in *RESULTS. */
2829
2830static void
2831get_constraint_for_ptr_offset (tree ptr, tree offset,
2832 VEC (ce_s, heap) **results)
2833{
bd02b3a0 2834 struct constraint_expr c;
e5bae89b 2835 unsigned int j, n;
5006671f 2836 HOST_WIDE_INT rhsunitoffset, rhsoffset;
e5bae89b
RG
2837
2838 /* If we do not do field-sensitive PTA adding offsets to pointers
2839 does not change the points-to solution. */
2840 if (!use_field_sensitive)
2841 {
2842 get_constraint_for (ptr, results);
2843 return;
2844 }
2845
2846 /* If the offset is not a non-negative integer constant that fits
2847 in a HOST_WIDE_INT, we have to fall back to a conservative
2848 solution which includes all sub-fields of all pointed-to
5006671f 2849 variables of ptr. */
779704e7
RG
2850 if (offset == NULL_TREE
2851 || !host_integerp (offset, 0))
5006671f
RG
2852 rhsoffset = UNKNOWN_OFFSET;
2853 else
e5bae89b 2854 {
5006671f
RG
2855 /* Make sure the bit-offset also fits. */
2856 rhsunitoffset = TREE_INT_CST_LOW (offset);
2857 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
2858 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
2859 rhsoffset = UNKNOWN_OFFSET;
e5bae89b
RG
2860 }
2861
2862 get_constraint_for (ptr, results);
2863 if (rhsoffset == 0)
2864 return;
2865
2866 /* As we are eventually appending to the solution do not use
2867 VEC_iterate here. */
2868 n = VEC_length (ce_s, *results);
2869 for (j = 0; j < n; j++)
2870 {
2871 varinfo_t curr;
bd02b3a0
RG
2872 c = *VEC_index (ce_s, *results, j);
2873 curr = get_varinfo (c.var);
e5bae89b 2874
bd02b3a0 2875 if (c.type == ADDRESSOF
5006671f
RG
2876 /* If this varinfo represents a full variable just use it. */
2877 && curr->is_full_var)
bd02b3a0
RG
2878 c.offset = 0;
2879 else if (c.type == ADDRESSOF
5006671f
RG
2880 /* If we do not know the offset add all subfields. */
2881 && rhsoffset == UNKNOWN_OFFSET)
2882 {
2883 varinfo_t temp = lookup_vi_for_tree (curr->decl);
2884 do
2885 {
2886 struct constraint_expr c2;
2887 c2.var = temp->id;
2888 c2.type = ADDRESSOF;
2889 c2.offset = 0;
bd02b3a0 2890 if (c2.var != c.var)
779704e7 2891 VEC_safe_push (ce_s, heap, *results, &c2);
5006671f
RG
2892 temp = temp->next;
2893 }
2894 while (temp);
2895 }
bd02b3a0 2896 else if (c.type == ADDRESSOF)
e5bae89b 2897 {
5006671f
RG
2898 varinfo_t temp;
2899 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
e5bae89b
RG
2900
2901 /* Search the sub-field which overlaps with the
5006671f
RG
2902 pointed-to offset. If the result is outside of the variable
2903 we have to provide a conservative result, as the variable is
2904 still reachable from the resulting pointer (even though it
2905 technically cannot point to anything). The last and first
2906 sub-fields are such conservative results.
e5bae89b
RG
2907 ??? If we always had a sub-field for &object + 1 then
2908 we could represent this in a more precise way. */
5006671f
RG
2909 if (rhsoffset < 0
2910 && curr->offset < offset)
2911 offset = 0;
2912 temp = first_or_preceding_vi_for_offset (curr, offset);
e5bae89b
RG
2913
2914 /* If the found variable is not exactly at the pointed to
2915 result, we have to include the next variable in the
2916 solution as well. Otherwise two increments by offset / 2
2917 do not result in the same or a conservative superset
2918 solution. */
5006671f 2919 if (temp->offset != offset
e5bae89b
RG
2920 && temp->next != NULL)
2921 {
2922 struct constraint_expr c2;
2923 c2.var = temp->next->id;
2924 c2.type = ADDRESSOF;
2925 c2.offset = 0;
2926 VEC_safe_push (ce_s, heap, *results, &c2);
2927 }
bd02b3a0
RG
2928 c.var = temp->id;
2929 c.offset = 0;
e5bae89b 2930 }
e5bae89b 2931 else
bd02b3a0
RG
2932 c.offset = rhsoffset;
2933
2934 VEC_replace (ce_s, *results, j, &c);
e5bae89b
RG
2935 }
2936}
2937
2938
c0d459f0
RG
2939/* Given a COMPONENT_REF T, return the constraint_expr vector for it.
2940 If address_p is true the result will be taken its address of. */
910fdc79 2941
4ee00913 2942static void
c0d459f0
RG
2943get_constraint_for_component_ref (tree t, VEC(ce_s, heap) **results,
2944 bool address_p)
910fdc79 2945{
4ee00913 2946 tree orig_t = t;
b1347638 2947 HOST_WIDE_INT bitsize = -1;
6bec9271 2948 HOST_WIDE_INT bitmaxsize = -1;
910fdc79 2949 HOST_WIDE_INT bitpos;
910fdc79 2950 tree forzero;
4ee00913 2951 struct constraint_expr *result;
910fdc79
DB
2952
2953 /* Some people like to do cute things like take the address of
2954 &0->a.b */
2955 forzero = t;
2956 while (!SSA_VAR_P (forzero) && !CONSTANT_CLASS_P (forzero))
4ee00913 2957 forzero = TREE_OPERAND (forzero, 0);
910fdc79 2958
c58936b6 2959 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
910fdc79 2960 {
4ee00913 2961 struct constraint_expr temp;
c58936b6 2962
4ee00913
DB
2963 temp.offset = 0;
2964 temp.var = integer_id;
2965 temp.type = SCALAR;
2966 VEC_safe_push (ce_s, heap, *results, &temp);
2967 return;
910fdc79 2968 }
c58936b6 2969
6bec9271 2970 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
21392f19 2971
c0d459f0
RG
2972 /* Pretend to take the address of the base, we'll take care of
2973 adding the required subset of sub-fields below. */
2974 get_constraint_for_1 (t, results, true);
c0d459f0 2975 gcc_assert (VEC_length (ce_s, *results) == 1);
e5bae89b 2976 result = VEC_last (ce_s, *results);
910fdc79 2977
e5bae89b
RG
2978 if (result->type == SCALAR
2979 && get_varinfo (result->var)->is_full_var)
2980 /* For single-field vars do not bother about the offset. */
2981 result->offset = 0;
2982 else if (result->type == SCALAR)
910fdc79
DB
2983 {
2984 /* In languages like C, you can access one past the end of an
2985 array. You aren't allowed to dereference it, so we can
2986 ignore this constraint. When we handle pointer subtraction,
2987 we may have to do something cute here. */
c58936b6 2988
c0d459f0 2989 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result->var)->fullsize
18455d17 2990 && bitmaxsize != 0)
dd68d988
DB
2991 {
2992 /* It's also not true that the constraint will actually start at the
2993 right offset, it may start in some padding. We only care about
2994 setting the constraint to the first actual field it touches, so
c58936b6 2995 walk to find it. */
c0d459f0 2996 struct constraint_expr cexpr = *result;
dd68d988 2997 varinfo_t curr;
c0d459f0
RG
2998 VEC_pop (ce_s, *results);
2999 cexpr.offset = 0;
3000 for (curr = get_varinfo (cexpr.var); curr; curr = curr->next)
dd68d988 3001 {
63d195d5 3002 if (ranges_overlap_p (curr->offset, curr->size,
c0d459f0 3003 bitpos, bitmaxsize))
dd68d988 3004 {
c0d459f0
RG
3005 cexpr.var = curr->id;
3006 VEC_safe_push (ce_s, heap, *results, &cexpr);
3007 if (address_p)
3008 break;
dd68d988
DB
3009 }
3010 }
e5bae89b
RG
3011 /* If we are going to take the address of this field then
3012 to be able to compute reachability correctly add at least
3013 the last field of the variable. */
3014 if (address_p
3015 && VEC_length (ce_s, *results) == 0)
3016 {
3017 curr = get_varinfo (cexpr.var);
3018 while (curr->next != NULL)
3019 curr = curr->next;
3020 cexpr.var = curr->id;
3021 VEC_safe_push (ce_s, heap, *results, &cexpr);
3022 }
3023 else
3024 /* Assert that we found *some* field there. The user couldn't be
3025 accessing *only* padding. */
3026 /* Still the user could access one past the end of an array
3027 embedded in a struct resulting in accessing *only* padding. */
3028 gcc_assert (VEC_length (ce_s, *results) >= 1
3029 || ref_contains_array_ref (orig_t));
dd68d988 3030 }
18455d17
RG
3031 else if (bitmaxsize == 0)
3032 {
3033 if (dump_file && (dump_flags & TDF_DETAILS))
3034 fprintf (dump_file, "Access to zero-sized part of variable,"
3035 "ignoring\n");
3036 }
910fdc79
DB
3037 else
3038 if (dump_file && (dump_flags & TDF_DETAILS))
3039 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
910fdc79 3040 }
5006671f 3041 else if (result->type == DEREF)
7b765bed 3042 {
5006671f
RG
3043 /* If we do not know exactly where the access goes say so. Note
3044 that only for non-structure accesses we know that we access
3045 at most one subfiled of any variable. */
3046 if (bitpos == -1
3047 || bitsize != bitmaxsize
3048 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t)))
3049 result->offset = UNKNOWN_OFFSET;
3050 else
3051 result->offset = bitpos;
7b765bed 3052 }
b51605c4
RG
3053 else if (result->type == ADDRESSOF)
3054 {
3055 /* We can end up here for component references on a
3056 VIEW_CONVERT_EXPR <>(&foobar). */
3057 result->type = SCALAR;
3058 result->var = anything_id;
3059 result->offset = 0;
3060 }
c0d459f0 3061 else
5006671f 3062 gcc_unreachable ();
910fdc79
DB
3063}
3064
3065
3066/* Dereference the constraint expression CONS, and return the result.
3067 DEREF (ADDRESSOF) = SCALAR
3068 DEREF (SCALAR) = DEREF
3069 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3070 This is needed so that we can handle dereferencing DEREF constraints. */
3071
4ee00913
DB
3072static void
3073do_deref (VEC (ce_s, heap) **constraints)
910fdc79 3074{
4ee00913
DB
3075 struct constraint_expr *c;
3076 unsigned int i = 0;
c58936b6 3077
4ee00913 3078 for (i = 0; VEC_iterate (ce_s, *constraints, i, c); i++)
910fdc79 3079 {
4ee00913
DB
3080 if (c->type == SCALAR)
3081 c->type = DEREF;
3082 else if (c->type == ADDRESSOF)
3083 c->type = SCALAR;
3084 else if (c->type == DEREF)
3085 {
b14e9388
RG
3086 struct constraint_expr tmplhs;
3087 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp");
4ee00913
DB
3088 process_constraint (new_constraint (tmplhs, *c));
3089 c->var = tmplhs.var;
3090 }
3091 else
3092 gcc_unreachable ();
910fdc79 3093 }
910fdc79
DB
3094}
3095
1d24fdd9
RG
3096static void get_constraint_for_1 (tree, VEC (ce_s, heap) **, bool);
3097
3098/* Given a tree T, return the constraint expression for taking the
3099 address of it. */
3100
3101static void
3102get_constraint_for_address_of (tree t, VEC (ce_s, heap) **results)
3103{
3104 struct constraint_expr *c;
3105 unsigned int i;
3106
3107 get_constraint_for_1 (t, results, true);
3108
3109 for (i = 0; VEC_iterate (ce_s, *results, i, c); i++)
3110 {
3111 if (c->type == DEREF)
3112 c->type = SCALAR;
3113 else
3114 c->type = ADDRESSOF;
3115 }
3116}
3117
910fdc79
DB
3118/* Given a tree T, return the constraint expression for it. */
3119
4ee00913 3120static void
c0d459f0 3121get_constraint_for_1 (tree t, VEC (ce_s, heap) **results, bool address_p)
910fdc79
DB
3122{
3123 struct constraint_expr temp;
3124
3125 /* x = integer is all glommed to a single variable, which doesn't
3126 point to anything by itself. That is, of course, unless it is an
3127 integer constant being treated as a pointer, in which case, we
3128 will return that this is really the addressof anything. This
3129 happens below, since it will fall into the default case. The only
3130 case we know something about an integer treated like a pointer is
3131 when it is the NULL pointer, and then we just say it points to
89ebafc6
PB
3132 NULL.
3133
3134 Do not do that if -fno-delete-null-pointer-checks though, because
3135 in that case *NULL does not fail, so it _should_ alias *anything.
3136 It is not worth adding a new option or renaming the existing one,
3137 since this case is relatively obscure. */
3138 if (flag_delete_null_pointer_checks
5006671f
RG
3139 && ((TREE_CODE (t) == INTEGER_CST
3140 && integer_zerop (t))
3141 /* The only valid CONSTRUCTORs in gimple with pointer typed
3142 elements are zero-initializer. */
3143 || TREE_CODE (t) == CONSTRUCTOR))
910fdc79
DB
3144 {
3145 temp.var = nothing_id;
3146 temp.type = ADDRESSOF;
3147 temp.offset = 0;
4ee00913
DB
3148 VEC_safe_push (ce_s, heap, *results, &temp);
3149 return;
910fdc79
DB
3150 }
3151
bd1f29d9
EB
3152 /* String constants are read-only. */
3153 if (TREE_CODE (t) == STRING_CST)
3154 {
3155 temp.var = readonly_id;
3156 temp.type = SCALAR;
3157 temp.offset = 0;
3158 VEC_safe_push (ce_s, heap, *results, &temp);
3159 return;
3160 }
3161
910fdc79
DB
3162 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3163 {
3164 case tcc_expression:
3165 {
3166 switch (TREE_CODE (t))
3167 {
3168 case ADDR_EXPR:
1d24fdd9
RG
3169 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3170 return;
e5bae89b 3171 default:;
910fdc79 3172 }
e5bae89b 3173 break;
910fdc79
DB
3174 }
3175 case tcc_reference:
3176 {
3177 switch (TREE_CODE (t))
3178 {
3179 case INDIRECT_REF:
3180 {
c0d459f0 3181 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p);
4ee00913
DB
3182 do_deref (results);
3183 return;
910fdc79
DB
3184 }
3185 case ARRAY_REF:
32961db5 3186 case ARRAY_RANGE_REF:
910fdc79 3187 case COMPONENT_REF:
c0d459f0 3188 get_constraint_for_component_ref (t, results, address_p);
4ee00913 3189 return;
5006671f
RG
3190 case VIEW_CONVERT_EXPR:
3191 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p);
3192 return;
3193 /* We are missing handling for TARGET_MEM_REF here. */
e5bae89b 3194 default:;
910fdc79 3195 }
e5bae89b 3196 break;
910fdc79 3197 }
910fdc79
DB
3198 case tcc_exceptional:
3199 {
3200 switch (TREE_CODE (t))
3201 {
910fdc79 3202 case SSA_NAME:
4ee00913 3203 {
c0d459f0 3204 get_constraint_for_ssa_var (t, results, address_p);
4ee00913
DB
3205 return;
3206 }
e5bae89b 3207 default:;
910fdc79 3208 }
e5bae89b 3209 break;
910fdc79
DB
3210 }
3211 case tcc_declaration:
4ee00913 3212 {
c0d459f0 3213 get_constraint_for_ssa_var (t, results, address_p);
4ee00913
DB
3214 return;
3215 }
e5bae89b 3216 default:;
910fdc79 3217 }
e5bae89b
RG
3218
3219 /* The default fallback is a constraint from anything. */
3220 temp.type = ADDRESSOF;
3221 temp.var = anything_id;
3222 temp.offset = 0;
3223 VEC_safe_push (ce_s, heap, *results, &temp);
910fdc79
DB
3224}
3225
c0d459f0
RG
3226/* Given a gimple tree T, return the constraint expression vector for it. */
3227
3228static void
3229get_constraint_for (tree t, VEC (ce_s, heap) **results)
3230{
3231 gcc_assert (VEC_length (ce_s, *results) == 0);
3232
3233 get_constraint_for_1 (t, results, false);
3234}
910fdc79 3235
779704e7
RG
3236
3237/* Efficiently generates constraints from all entries in *RHSC to all
3238 entries in *LHSC. */
3239
3240static void
3241process_all_all_constraints (VEC (ce_s, heap) *lhsc, VEC (ce_s, heap) *rhsc)
3242{
3243 struct constraint_expr *lhsp, *rhsp;
3244 unsigned i, j;
3245
3246 if (VEC_length (ce_s, lhsc) <= 1
3247 || VEC_length (ce_s, rhsc) <= 1)
3248 {
3249 for (i = 0; VEC_iterate (ce_s, lhsc, i, lhsp); ++i)
3250 for (j = 0; VEC_iterate (ce_s, rhsc, j, rhsp); ++j)
3251 process_constraint (new_constraint (*lhsp, *rhsp));
3252 }
3253 else
3254 {
3255 struct constraint_expr tmp;
b14e9388 3256 tmp = new_scalar_tmp_constraint_exp ("allalltmp");
779704e7
RG
3257 for (i = 0; VEC_iterate (ce_s, rhsc, i, rhsp); ++i)
3258 process_constraint (new_constraint (tmp, *rhsp));
3259 for (i = 0; VEC_iterate (ce_s, lhsc, i, lhsp); ++i)
3260 process_constraint (new_constraint (*lhsp, tmp));
3261 }
3262}
3263
910fdc79
DB
3264/* Handle aggregate copies by expanding into copies of the respective
3265 fields of the structures. */
3266
3267static void
3268do_structure_copy (tree lhsop, tree rhsop)
3269{
5006671f 3270 struct constraint_expr *lhsp, *rhsp;
4ee00913 3271 VEC (ce_s, heap) *lhsc = NULL, *rhsc = NULL;
5006671f
RG
3272 unsigned j;
3273
3274 get_constraint_for (lhsop, &lhsc);
3275 get_constraint_for (rhsop, &rhsc);
3276 lhsp = VEC_index (ce_s, lhsc, 0);
3277 rhsp = VEC_index (ce_s, rhsc, 0);
3278 if (lhsp->type == DEREF
3279 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3280 || rhsp->type == DEREF)
779704e7 3281 process_all_all_constraints (lhsc, rhsc);
5006671f
RG
3282 else if (lhsp->type == SCALAR
3283 && (rhsp->type == SCALAR
3284 || rhsp->type == ADDRESSOF))
910fdc79 3285 {
5006671f
RG
3286 tree lhsbase, rhsbase;
3287 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3288 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
3289 unsigned k = 0;
3290 lhsbase = get_ref_base_and_extent (lhsop, &lhsoffset,
3291 &lhssize, &lhsmaxsize);
3292 rhsbase = get_ref_base_and_extent (rhsop, &rhsoffset,
3293 &rhssize, &rhsmaxsize);
3294 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp);)
910fdc79 3295 {
5006671f
RG
3296 varinfo_t lhsv, rhsv;
3297 rhsp = VEC_index (ce_s, rhsc, k);
3298 lhsv = get_varinfo (lhsp->var);
3299 rhsv = get_varinfo (rhsp->var);
3300 if (lhsv->may_have_pointers
3301 && ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3302 rhsv->offset + lhsoffset, rhsv->size))
3303 process_constraint (new_constraint (*lhsp, *rhsp));
3304 if (lhsv->offset + rhsoffset + lhsv->size
3305 > rhsv->offset + lhsoffset + rhsv->size)
3306 {
3307 ++k;
3308 if (k >= VEC_length (ce_s, rhsc))
3309 break;
3310 }
910fdc79 3311 else
5006671f 3312 ++j;
910fdc79
DB
3313 }
3314 }
3315 else
5006671f 3316 gcc_unreachable ();
a5eadacc 3317
5006671f
RG
3318 VEC_free (ce_s, heap, lhsc);
3319 VEC_free (ce_s, heap, rhsc);
910fdc79
DB
3320}
3321
b7091901
RG
3322/* Create a constraint ID = OP. */
3323
3324static void
3325make_constraint_to (unsigned id, tree op)
3326{
3327 VEC(ce_s, heap) *rhsc = NULL;
3328 struct constraint_expr *c;
3329 struct constraint_expr includes;
3330 unsigned int j;
3331
3332 includes.var = id;
3333 includes.offset = 0;
3334 includes.type = SCALAR;
3335
3336 get_constraint_for (op, &rhsc);
3337 for (j = 0; VEC_iterate (ce_s, rhsc, j, c); j++)
faf2ecc5 3338 process_constraint (new_constraint (includes, *c));
b7091901
RG
3339 VEC_free (ce_s, heap, rhsc);
3340}
3341
74d27244
RG
3342/* Create a constraint ID = &FROM. */
3343
3344static void
3345make_constraint_from (varinfo_t vi, int from)
3346{
3347 struct constraint_expr lhs, rhs;
3348
3349 lhs.var = vi->id;
3350 lhs.offset = 0;
3351 lhs.type = SCALAR;
3352
3353 rhs.var = from;
3354 rhs.offset = 0;
3355 rhs.type = ADDRESSOF;
3356 process_constraint (new_constraint (lhs, rhs));
3357}
3358
3359/* Create a constraint ID = FROM. */
3360
3361static void
3362make_copy_constraint (varinfo_t vi, int from)
3363{
3364 struct constraint_expr lhs, rhs;
3365
3366 lhs.var = vi->id;
3367 lhs.offset = 0;
3368 lhs.type = SCALAR;
3369
3370 rhs.var = from;
3371 rhs.offset = 0;
3372 rhs.type = SCALAR;
3373 process_constraint (new_constraint (lhs, rhs));
3374}
3375
b7091901
RG
3376/* Make constraints necessary to make OP escape. */
3377
3378static void
3379make_escape_constraint (tree op)
3380{
3381 make_constraint_to (escaped_id, op);
3382}
3383
74d27244
RG
3384/* Create a new artificial heap variable with NAME and make a
3385 constraint from it to LHS. Return the created variable. */
3386
3387static varinfo_t
3388make_constraint_from_heapvar (varinfo_t lhs, const char *name)
3389{
3390 varinfo_t vi;
8bc88f25 3391 tree heapvar = heapvar_lookup (lhs->decl, lhs->offset);
74d27244
RG
3392
3393 if (heapvar == NULL_TREE)
3394 {
3395 var_ann_t ann;
3396 heapvar = create_tmp_var_raw (ptr_type_node, name);
3397 DECL_EXTERNAL (heapvar) = 1;
3398
8bc88f25 3399 heapvar_insert (lhs->decl, lhs->offset, heapvar);
74d27244
RG
3400
3401 ann = get_var_ann (heapvar);
3402 ann->is_heapvar = 1;
3403 }
3404
3405 /* For global vars we need to add a heapvar to the list of referenced
3406 vars of a different function than it was created for originally. */
3407 if (gimple_referenced_vars (cfun))
3408 add_referenced_var (heapvar);
3409
3410 vi = new_var_info (heapvar, name);
3411 vi->is_artificial_var = true;
3412 vi->is_heap_var = true;
3413 vi->is_unknown_size_var = true;
b41e33fe 3414 vi->offset = 0;
74d27244
RG
3415 vi->fullsize = ~0;
3416 vi->size = ~0;
3417 vi->is_full_var = true;
3418 insert_vi_for_tree (heapvar, vi);
3419
3420 make_constraint_from (lhs, vi->id);
3421
3422 return vi;
3423}
3424
3425/* Create a new artificial heap variable with NAME and make a
3426 constraint from it to LHS. Set flags according to a tag used
3427 for tracking restrict pointers. */
3428
3429static void
3430make_constraint_from_restrict (varinfo_t lhs, const char *name)
3431{
3432 varinfo_t vi;
3433 vi = make_constraint_from_heapvar (lhs, name);
3434 vi->is_restrict_var = 1;
3435 vi->is_global_var = 0;
3436 vi->is_special_var = 1;
3437 vi->may_have_pointers = 0;
3438}
3439
7b765bed
DB
3440/* For non-IPA mode, generate constraints necessary for a call on the
3441 RHS. */
3442
3443static void
472c7fbd 3444handle_rhs_call (gimple stmt, VEC(ce_s, heap) **results)
7b765bed 3445{
472c7fbd 3446 struct constraint_expr rhsc;
726a989a 3447 unsigned i;
7b765bed 3448
726a989a
RB
3449 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3450 {
3451 tree arg = gimple_call_arg (stmt, i);
3452
3453 /* Find those pointers being passed, and make sure they end up
3454 pointing to anything. */
3455 if (could_have_pointers (arg))
3456 make_escape_constraint (arg);
3457 }
b7091901
RG
3458
3459 /* The static chain escapes as well. */
726a989a
RB
3460 if (gimple_call_chain (stmt))
3461 make_escape_constraint (gimple_call_chain (stmt));
472c7fbd 3462
1d24fdd9
RG
3463 /* And if we applied NRV the address of the return slot escapes as well. */
3464 if (gimple_call_return_slot_opt_p (stmt)
3465 && gimple_call_lhs (stmt) != NULL_TREE
4d61856d 3466 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
1d24fdd9
RG
3467 {
3468 VEC(ce_s, heap) *tmpc = NULL;
3469 struct constraint_expr lhsc, *c;
3470 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3471 lhsc.var = escaped_id;
3472 lhsc.offset = 0;
3473 lhsc.type = SCALAR;
3474 for (i = 0; VEC_iterate (ce_s, tmpc, i, c); ++i)
3475 process_constraint (new_constraint (lhsc, *c));
3476 VEC_free(ce_s, heap, tmpc);
3477 }
3478
5006671f
RG
3479 /* Regular functions return nonlocal memory. */
3480 rhsc.var = nonlocal_id;
472c7fbd 3481 rhsc.offset = 0;
5006671f 3482 rhsc.type = SCALAR;
472c7fbd 3483 VEC_safe_push (ce_s, heap, *results, &rhsc);
7b765bed 3484}
e8ca4159 3485
af947da7
RG
3486/* For non-IPA mode, generate constraints necessary for a call
3487 that returns a pointer and assigns it to LHS. This simply makes
b7091901 3488 the LHS point to global and escaped variables. */
af947da7
RG
3489
3490static void
472c7fbd 3491handle_lhs_call (tree lhs, int flags, VEC(ce_s, heap) *rhsc)
af947da7
RG
3492{
3493 VEC(ce_s, heap) *lhsc = NULL;
af947da7 3494
b7091901 3495 get_constraint_for (lhs, &lhsc);
183ae595
RG
3496
3497 if (flags & ECF_MALLOC)
3498 {
183ae595 3499 varinfo_t vi;
74d27244 3500 vi = make_constraint_from_heapvar (get_vi_for_tree (lhs), "HEAP");
14c41b9b
RG
3501 /* We delay marking allocated storage global until we know if
3502 it escapes. */
91deb937 3503 DECL_EXTERNAL (vi->decl) = 0;
14c41b9b 3504 vi->is_global_var = 0;
183ae595 3505 }
472c7fbd 3506 else if (VEC_length (ce_s, rhsc) > 0)
183ae595 3507 {
472c7fbd
RG
3508 /* If the store is to a global decl make sure to
3509 add proper escape constraints. */
3510 lhs = get_base_address (lhs);
3511 if (lhs
3512 && DECL_P (lhs)
3513 && is_global_var (lhs))
3514 {
3515 struct constraint_expr tmpc;
3516 tmpc.var = escaped_id;
3517 tmpc.offset = 0;
3518 tmpc.type = SCALAR;
3519 VEC_safe_push (ce_s, heap, lhsc, &tmpc);
3520 }
779704e7 3521 process_all_all_constraints (lhsc, rhsc);
183ae595 3522 }
b7091901
RG
3523 VEC_free (ce_s, heap, lhsc);
3524}
3525
3526/* For non-IPA mode, generate constraints necessary for a call of a
3527 const function that returns a pointer in the statement STMT. */
3528
3529static void
472c7fbd 3530handle_const_call (gimple stmt, VEC(ce_s, heap) **results)
b7091901 3531{
b14e9388 3532 struct constraint_expr rhsc;
472c7fbd 3533 unsigned int k;
b7091901 3534
472c7fbd
RG
3535 /* Treat nested const functions the same as pure functions as far
3536 as the static chain is concerned. */
726a989a 3537 if (gimple_call_chain (stmt))
b7091901 3538 {
472c7fbd
RG
3539 make_constraint_to (callused_id, gimple_call_chain (stmt));
3540 rhsc.var = callused_id;
b7091901 3541 rhsc.offset = 0;
472c7fbd
RG
3542 rhsc.type = SCALAR;
3543 VEC_safe_push (ce_s, heap, *results, &rhsc);
b7091901
RG
3544 }
3545
b7091901 3546 /* May return arguments. */
726a989a
RB
3547 for (k = 0; k < gimple_call_num_args (stmt); ++k)
3548 {
3549 tree arg = gimple_call_arg (stmt, k);
3550
3551 if (could_have_pointers (arg))
3552 {
3553 VEC(ce_s, heap) *argc = NULL;
b14e9388 3554 unsigned i;
726a989a 3555 struct constraint_expr *argp;
726a989a 3556 get_constraint_for (arg, &argc);
b14e9388
RG
3557 for (i = 0; VEC_iterate (ce_s, argc, i, argp); ++i)
3558 VEC_safe_push (ce_s, heap, *results, argp);
3559 VEC_free(ce_s, heap, argc);
726a989a
RB
3560 }
3561 }
b7091901 3562
472c7fbd
RG
3563 /* May return addresses of globals. */
3564 rhsc.var = nonlocal_id;
3565 rhsc.offset = 0;
3566 rhsc.type = ADDRESSOF;
3567 VEC_safe_push (ce_s, heap, *results, &rhsc);
af947da7
RG
3568}
3569
15c15196
RG
3570/* For non-IPA mode, generate constraints necessary for a call to a
3571 pure function in statement STMT. */
3572
3573static void
472c7fbd 3574handle_pure_call (gimple stmt, VEC(ce_s, heap) **results)
15c15196 3575{
472c7fbd 3576 struct constraint_expr rhsc;
726a989a 3577 unsigned i;
472c7fbd 3578 bool need_callused = false;
15c15196
RG
3579
3580 /* Memory reached from pointer arguments is call-used. */
726a989a
RB
3581 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3582 {
3583 tree arg = gimple_call_arg (stmt, i);
3584
3585 if (could_have_pointers (arg))
472c7fbd
RG
3586 {
3587 make_constraint_to (callused_id, arg);
3588 need_callused = true;
3589 }
726a989a 3590 }
15c15196
RG
3591
3592 /* The static chain is used as well. */
726a989a 3593 if (gimple_call_chain (stmt))
15c15196 3594 {
472c7fbd
RG
3595 make_constraint_to (callused_id, gimple_call_chain (stmt));
3596 need_callused = true;
3597 }
15c15196 3598
5006671f 3599 /* Pure functions may return callused and nonlocal memory. */
472c7fbd
RG
3600 if (need_callused)
3601 {
15c15196
RG
3602 rhsc.var = callused_id;
3603 rhsc.offset = 0;
472c7fbd
RG
3604 rhsc.type = SCALAR;
3605 VEC_safe_push (ce_s, heap, *results, &rhsc);
15c15196 3606 }
5006671f 3607 rhsc.var = nonlocal_id;
472c7fbd 3608 rhsc.offset = 0;
5006671f 3609 rhsc.type = SCALAR;
472c7fbd 3610 VEC_safe_push (ce_s, heap, *results, &rhsc);
15c15196
RG
3611}
3612
e8ca4159
DN
3613/* Walk statement T setting up aliasing constraints according to the
3614 references found in T. This function is the main part of the
3615 constraint builder. AI points to auxiliary alias information used
3616 when building alias sets and computing alias grouping heuristics. */
910fdc79
DB
3617
3618static void
726a989a 3619find_func_aliases (gimple origt)
910fdc79 3620{
726a989a 3621 gimple t = origt;
4ee00913
DB
3622 VEC(ce_s, heap) *lhsc = NULL;
3623 VEC(ce_s, heap) *rhsc = NULL;
3624 struct constraint_expr *c;
910fdc79 3625
e8ca4159 3626 /* Now build constraints expressions. */
726a989a 3627 if (gimple_code (t) == GIMPLE_PHI)
e8ca4159 3628 {
726a989a 3629 gcc_assert (!AGGREGATE_TYPE_P (TREE_TYPE (gimple_phi_result (t))));
6df11ca1 3630
e8ca4159
DN
3631 /* Only care about pointers and structures containing
3632 pointers. */
726a989a 3633 if (could_have_pointers (gimple_phi_result (t)))
e8ca4159 3634 {
726a989a 3635 size_t i;
4ee00913 3636 unsigned int j;
c58936b6 3637
4ee00913
DB
3638 /* For a phi node, assign all the arguments to
3639 the result. */
726a989a
RB
3640 get_constraint_for (gimple_phi_result (t), &lhsc);
3641 for (i = 0; i < gimple_phi_num_args (t); i++)
c58936b6 3642 {
0a4288d9
RG
3643 tree rhstype;
3644 tree strippedrhs = PHI_ARG_DEF (t, i);
3645
3646 STRIP_NOPS (strippedrhs);
3647 rhstype = TREE_TYPE (strippedrhs);
726a989a 3648 get_constraint_for (gimple_phi_arg_def (t, i), &rhsc);
0a4288d9 3649
4ee00913
DB
3650 for (j = 0; VEC_iterate (ce_s, lhsc, j, c); j++)
3651 {
3652 struct constraint_expr *c2;
3653 while (VEC_length (ce_s, rhsc) > 0)
3654 {
3655 c2 = VEC_last (ce_s, rhsc);
3656 process_constraint (new_constraint (*c, *c2));
3657 VEC_pop (ce_s, rhsc);
3658 }
3659 }
c58936b6 3660 }
4ee00913
DB
3661 }
3662 }
3663 /* In IPA mode, we need to generate constraints to pass call
726a989a
RB
3664 arguments through their calls. There are two cases,
3665 either a GIMPLE_CALL returning a value, or just a plain
3666 GIMPLE_CALL when we are not.
7b765bed
DB
3667
3668 In non-ipa mode, we need to generate constraints for each
3669 pointer passed by address. */
726a989a 3670 else if (is_gimple_call (t))
4ee00913 3671 {
5c245b95
RG
3672 tree fndecl = gimple_call_fndecl (t);
3673 if (fndecl != NULL_TREE
779704e7
RG
3674 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
3675 /* ??? All builtins that are handled here need to be handled
3676 in the alias-oracle query functions explicitly! */
3677 switch (DECL_FUNCTION_CODE (fndecl))
3678 {
3679 /* All the following functions return a pointer to the same object
3680 as their first argument points to. The functions do not add
3681 to the ESCAPED solution. The functions make the first argument
3682 pointed to memory point to what the second argument pointed to
3683 memory points to. */
3684 case BUILT_IN_STRCPY:
3685 case BUILT_IN_STRNCPY:
3686 case BUILT_IN_BCOPY:
3687 case BUILT_IN_MEMCPY:
3688 case BUILT_IN_MEMMOVE:
3689 case BUILT_IN_MEMPCPY:
3690 case BUILT_IN_STPCPY:
3691 case BUILT_IN_STPNCPY:
3692 case BUILT_IN_STRCAT:
3693 case BUILT_IN_STRNCAT:
3694 {
3695 tree res = gimple_call_lhs (t);
1307c758
RG
3696 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
3697 == BUILT_IN_BCOPY ? 1 : 0));
3698 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
3699 == BUILT_IN_BCOPY ? 0 : 1));
779704e7
RG
3700 if (res != NULL_TREE)
3701 {
3702 get_constraint_for (res, &lhsc);
3703 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
3704 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
3705 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY)
3706 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
3707 else
3708 get_constraint_for (dest, &rhsc);
3709 process_all_all_constraints (lhsc, rhsc);
3710 VEC_free (ce_s, heap, lhsc);
3711 VEC_free (ce_s, heap, rhsc);
3712 }
3713 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
3714 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
3715 do_deref (&lhsc);
3716 do_deref (&rhsc);
3717 process_all_all_constraints (lhsc, rhsc);
3718 VEC_free (ce_s, heap, lhsc);
3719 VEC_free (ce_s, heap, rhsc);
3720 return;
3721 }
3722 case BUILT_IN_MEMSET:
3723 {
3724 tree res = gimple_call_lhs (t);
3725 tree dest = gimple_call_arg (t, 0);
3726 unsigned i;
3727 ce_s *lhsp;
3728 struct constraint_expr ac;
3729 if (res != NULL_TREE)
3730 {
3731 get_constraint_for (res, &lhsc);
3732 get_constraint_for (dest, &rhsc);
3733 process_all_all_constraints (lhsc, rhsc);
3734 VEC_free (ce_s, heap, lhsc);
3735 VEC_free (ce_s, heap, rhsc);
3736 }
3737 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
3738 do_deref (&lhsc);
825be69e
RG
3739 if (flag_delete_null_pointer_checks
3740 && integer_zerop (gimple_call_arg (t, 1)))
3741 {
3742 ac.type = ADDRESSOF;
3743 ac.var = nothing_id;
3744 }
3745 else
3746 {
3747 ac.type = SCALAR;
3748 ac.var = integer_id;
3749 }
779704e7
RG
3750 ac.offset = 0;
3751 for (i = 0; VEC_iterate (ce_s, lhsc, i, lhsp); ++i)
3752 process_constraint (new_constraint (*lhsp, ac));
3753 VEC_free (ce_s, heap, lhsc);
3754 return;
3755 }
3756 /* All the following functions do not return pointers, do not
3757 modify the points-to sets of memory reachable from their
3758 arguments and do not add to the ESCAPED solution. */
3759 case BUILT_IN_SINCOS:
3760 case BUILT_IN_SINCOSF:
3761 case BUILT_IN_SINCOSL:
3762 case BUILT_IN_FREXP:
3763 case BUILT_IN_FREXPF:
3764 case BUILT_IN_FREXPL:
3765 case BUILT_IN_GAMMA_R:
3766 case BUILT_IN_GAMMAF_R:
3767 case BUILT_IN_GAMMAL_R:
3768 case BUILT_IN_LGAMMA_R:
3769 case BUILT_IN_LGAMMAF_R:
3770 case BUILT_IN_LGAMMAL_R:
3771 case BUILT_IN_MODF:
3772 case BUILT_IN_MODFF:
3773 case BUILT_IN_MODFL:
3774 case BUILT_IN_REMQUO:
3775 case BUILT_IN_REMQUOF:
3776 case BUILT_IN_REMQUOL:
3777 case BUILT_IN_FREE:
3778 return;
3779 /* printf-style functions may have hooks to set pointers to
3780 point to somewhere into the generated string. Leave them
3781 for a later excercise... */
3782 default:
3783 /* Fallthru to general call handling. */;
3784 }
5c245b95
RG
3785 if (!in_ipa_mode
3786 || (fndecl
3787 && !lookup_vi_for_tree (fndecl)))
4ee00913 3788 {
472c7fbd 3789 VEC(ce_s, heap) *rhsc = NULL;
726a989a
RB
3790 int flags = gimple_call_flags (t);
3791
b7091901
RG
3792 /* Const functions can return their arguments and addresses
3793 of global memory but not of escaped memory. */
472c7fbd 3794 if (flags & (ECF_CONST|ECF_NOVOPS))
b7091901 3795 {
726a989a
RB
3796 if (gimple_call_lhs (t)
3797 && could_have_pointers (gimple_call_lhs (t)))
472c7fbd 3798 handle_const_call (t, &rhsc);
b7091901 3799 }
726a989a
RB
3800 /* Pure functions can return addresses in and of memory
3801 reachable from their arguments, but they are not an escape
3802 point for reachable memory of their arguments. */
472c7fbd
RG
3803 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
3804 handle_pure_call (t, &rhsc);
726a989a 3805 else
472c7fbd
RG
3806 handle_rhs_call (t, &rhsc);
3807 if (gimple_call_lhs (t)
3808 && could_have_pointers (gimple_call_lhs (t)))
3809 handle_lhs_call (gimple_call_lhs (t), flags, rhsc);
3810 VEC_free (ce_s, heap, rhsc);
4ee00913
DB
3811 }
3812 else
3813 {
7b765bed 3814 tree lhsop;
7b765bed
DB
3815 varinfo_t fi;
3816 int i = 1;
726a989a 3817 size_t j;
7b765bed 3818 tree decl;
726a989a
RB
3819
3820 lhsop = gimple_call_lhs (t);
3821 decl = gimple_call_fndecl (t);
7b765bed
DB
3822
3823 /* If we can directly resolve the function being called, do so.
3824 Otherwise, it must be some sort of indirect expression that
3825 we should still be able to handle. */
3826 if (decl)
726a989a 3827 fi = get_vi_for_tree (decl);
7b765bed
DB
3828 else
3829 {
726a989a 3830 decl = gimple_call_fn (t);
7b765bed 3831 fi = get_vi_for_tree (decl);
4ee00913 3832 }
6e7e772d 3833
7b765bed
DB
3834 /* Assign all the passed arguments to the appropriate incoming
3835 parameters of the function. */
726a989a 3836 for (j = 0; j < gimple_call_num_args (t); j++)
4ee00913 3837 {
7b765bed
DB
3838 struct constraint_expr lhs ;
3839 struct constraint_expr *rhsp;
726a989a 3840 tree arg = gimple_call_arg (t, j);
7b765bed
DB
3841
3842 get_constraint_for (arg, &rhsc);
3843 if (TREE_CODE (decl) != FUNCTION_DECL)
3844 {
3845 lhs.type = DEREF;
3846 lhs.var = fi->id;
3847 lhs.offset = i;
3848 }
3849 else
3850 {
3851 lhs.type = SCALAR;
3852 lhs.var = first_vi_for_offset (fi, i)->id;
3853 lhs.offset = 0;
3854 }
3855 while (VEC_length (ce_s, rhsc) != 0)
3856 {
3857 rhsp = VEC_last (ce_s, rhsc);
3858 process_constraint (new_constraint (lhs, *rhsp));
3859 VEC_pop (ce_s, rhsc);
3860 }
3861 i++;
4ee00913 3862 }
7b765bed
DB
3863
3864 /* If we are returning a value, assign it to the result. */
3865 if (lhsop)
4ee00913 3866 {
7b765bed
DB
3867 struct constraint_expr rhs;
3868 struct constraint_expr *lhsp;
3869 unsigned int j = 0;
3870
3871 get_constraint_for (lhsop, &lhsc);
3872 if (TREE_CODE (decl) != FUNCTION_DECL)
3873 {
3874 rhs.type = DEREF;
3875 rhs.var = fi->id;
3876 rhs.offset = i;
3877 }
3878 else
3879 {
3880 rhs.type = SCALAR;
3881 rhs.var = first_vi_for_offset (fi, i)->id;
3882 rhs.offset = 0;
3883 }
3884 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp); j++)
3885 process_constraint (new_constraint (*lhsp, rhs));
4ee00913 3886 }
c58936b6 3887 }
e8ca4159 3888 }
e5bae89b
RG
3889 /* Otherwise, just a regular assignment statement. Only care about
3890 operations with pointer result, others are dealt with as escape
3891 points if they have pointer operands. */
726a989a
RB
3892 else if (is_gimple_assign (t)
3893 && could_have_pointers (gimple_assign_lhs (t)))
e8ca4159 3894 {
726a989a
RB
3895 /* Otherwise, just a regular assignment statement. */
3896 tree lhsop = gimple_assign_lhs (t);
3897 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
e8ca4159 3898
726a989a 3899 if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
e5bae89b 3900 do_structure_copy (lhsop, rhsop);
e8ca4159
DN
3901 else
3902 {
726a989a 3903 struct constraint_expr temp;
e5bae89b 3904 get_constraint_for (lhsop, &lhsc);
726a989a
RB
3905
3906 if (gimple_assign_rhs_code (t) == POINTER_PLUS_EXPR)
3907 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
3908 gimple_assign_rhs2 (t), &rhsc);
1a87cf0c 3909 else if ((CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (t))
1961418e
RG
3910 && !(POINTER_TYPE_P (gimple_expr_type (t))
3911 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
3912 || gimple_assign_single_p (t))
726a989a
RB
3913 get_constraint_for (rhsop, &rhsc);
3914 else
3915 {
3916 temp.type = ADDRESSOF;
3917 temp.var = anything_id;
3918 temp.offset = 0;
3919 VEC_safe_push (ce_s, heap, rhsc, &temp);
3920 }
779704e7 3921 process_all_all_constraints (lhsc, rhsc);
e8ca4159 3922 }
de70bb20
RG
3923 /* If there is a store to a global variable the rhs escapes. */
3924 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
3925 && DECL_P (lhsop)
3926 && is_global_var (lhsop))
3927 make_escape_constraint (rhsop);
74d27244
RG
3928 /* If this is a conversion of a non-restrict pointer to a
3929 restrict pointer track it with a new heapvar. */
3930 else if (gimple_assign_cast_p (t)
3931 && POINTER_TYPE_P (TREE_TYPE (rhsop))
3932 && POINTER_TYPE_P (TREE_TYPE (lhsop))
3933 && !TYPE_RESTRICT (TREE_TYPE (rhsop))
3934 && TYPE_RESTRICT (TREE_TYPE (lhsop)))
3935 make_constraint_from_restrict (get_vi_for_tree (lhsop),
3936 "CAST_RESTRICT");
910fdc79 3937 }
2e407842
RG
3938 /* For conversions of pointers to non-pointers the pointer escapes. */
3939 else if (gimple_assign_cast_p (t)
3940 && POINTER_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (t)))
3941 && !POINTER_TYPE_P (TREE_TYPE (gimple_assign_lhs (t))))
b7091901 3942 {
726a989a 3943 make_escape_constraint (gimple_assign_rhs1 (t));
b7091901 3944 }
14c41b9b
RG
3945 /* Handle escapes through return. */
3946 else if (gimple_code (t) == GIMPLE_RETURN
3947 && gimple_return_retval (t) != NULL_TREE
3948 && could_have_pointers (gimple_return_retval (t)))
3949 {
3950 make_escape_constraint (gimple_return_retval (t));
3951 }
2e407842
RG
3952 /* Handle asms conservatively by adding escape constraints to everything. */
3953 else if (gimple_code (t) == GIMPLE_ASM)
b7091901 3954 {
5006671f
RG
3955 unsigned i, noutputs;
3956 const char **oconstraints;
3957 const char *constraint;
3958 bool allows_mem, allows_reg, is_inout;
3959
3960 noutputs = gimple_asm_noutputs (t);
3961 oconstraints = XALLOCAVEC (const char *, noutputs);
3962
3963 for (i = 0; i < noutputs; ++i)
b7091901 3964 {
5006671f
RG
3965 tree link = gimple_asm_output_op (t, i);
3966 tree op = TREE_VALUE (link);
3967
3968 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3969 oconstraints[i] = constraint;
3970 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
3971 &allows_reg, &is_inout);
3972
3973 /* A memory constraint makes the address of the operand escape. */
3974 if (!allows_reg && allows_mem)
3975 make_escape_constraint (build_fold_addr_expr (op));
3976
3977 /* The asm may read global memory, so outputs may point to
3978 any global memory. */
b7091901 3979 if (op && could_have_pointers (op))
5006671f
RG
3980 {
3981 VEC(ce_s, heap) *lhsc = NULL;
3982 struct constraint_expr rhsc, *lhsp;
3983 unsigned j;
3984 get_constraint_for (op, &lhsc);
3985 rhsc.var = nonlocal_id;
3986 rhsc.offset = 0;
3987 rhsc.type = SCALAR;
3988 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp); j++)
3989 process_constraint (new_constraint (*lhsp, rhsc));
3990 VEC_free (ce_s, heap, lhsc);
3991 }
b7091901 3992 }
726a989a 3993 for (i = 0; i < gimple_asm_ninputs (t); ++i)
b7091901 3994 {
5006671f
RG
3995 tree link = gimple_asm_input_op (t, i);
3996 tree op = TREE_VALUE (link);
3997
3998 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3999
4000 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4001 &allows_mem, &allows_reg);
4002
4003 /* A memory constraint makes the address of the operand escape. */
4004 if (!allows_reg && allows_mem)
4005 make_escape_constraint (build_fold_addr_expr (op));
4006 /* Strictly we'd only need the constraint to ESCAPED if
4007 the asm clobbers memory, otherwise using CALLUSED
4008 would be enough. */
4009 else if (op && could_have_pointers (op))
b7091901
RG
4010 make_escape_constraint (op);
4011 }
4012 }
4013
4ee00913
DB
4014 VEC_free (ce_s, heap, rhsc);
4015 VEC_free (ce_s, heap, lhsc);
910fdc79
DB
4016}
4017
4018
4019/* Find the first varinfo in the same variable as START that overlaps with
5006671f 4020 OFFSET. Return NULL if we can't find one. */
910fdc79 4021
c58936b6 4022static varinfo_t
910fdc79
DB
4023first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
4024{
5006671f
RG
4025 /* If the offset is outside of the variable, bail out. */
4026 if (offset >= start->fullsize)
4027 return NULL;
4028
4029 /* If we cannot reach offset from start, lookup the first field
4030 and start from there. */
4031 if (start->offset > offset)
4032 start = lookup_vi_for_tree (start->decl);
4033
4034 while (start)
910fdc79
DB
4035 {
4036 /* We may not find a variable in the field list with the actual
4037 offset when when we have glommed a structure to a variable.
4038 In that case, however, offset should still be within the size
4039 of the variable. */
5006671f 4040 if (offset >= start->offset
de925a03 4041 && (offset - start->offset) < start->size)
5006671f
RG
4042 return start;
4043
4044 start= start->next;
910fdc79 4045 }
5006671f 4046
8971094d 4047 return NULL;
910fdc79
DB
4048}
4049
5006671f
RG
4050/* Find the first varinfo in the same variable as START that overlaps with
4051 OFFSET. If there is no such varinfo the varinfo directly preceding
4052 OFFSET is returned. */
4053
4054static varinfo_t
4055first_or_preceding_vi_for_offset (varinfo_t start,
4056 unsigned HOST_WIDE_INT offset)
4057{
4058 /* If we cannot reach offset from start, lookup the first field
4059 and start from there. */
4060 if (start->offset > offset)
4061 start = lookup_vi_for_tree (start->decl);
4062
4063 /* We may not find a variable in the field list with the actual
4064 offset when when we have glommed a structure to a variable.
4065 In that case, however, offset should still be within the size
4066 of the variable.
4067 If we got beyond the offset we look for return the field
4068 directly preceding offset which may be the last field. */
4069 while (start->next
4070 && offset >= start->offset
de925a03 4071 && !((offset - start->offset) < start->size))
5006671f
RG
4072 start = start->next;
4073
4074 return start;
4075}
4076
910fdc79 4077
4cf4d6a3
DB
4078/* Insert the varinfo FIELD into the field list for BASE, at the front
4079 of the list. */
4080
4081static void
4082insert_into_field_list (varinfo_t base, varinfo_t field)
4083{
4084 varinfo_t prev = base;
4085 varinfo_t curr = base->next;
c58936b6 4086
4cf4d6a3
DB
4087 field->next = curr;
4088 prev->next = field;
4089}
4090
910fdc79
DB
4091/* Insert the varinfo FIELD into the field list for BASE, ordered by
4092 offset. */
4093
4094static void
4cf4d6a3 4095insert_into_field_list_sorted (varinfo_t base, varinfo_t field)
910fdc79
DB
4096{
4097 varinfo_t prev = base;
4098 varinfo_t curr = base->next;
c58936b6 4099
910fdc79
DB
4100 if (curr == NULL)
4101 {
4102 prev->next = field;
4103 field->next = NULL;
4104 }
4105 else
4106 {
4107 while (curr)
4108 {
4109 if (field->offset <= curr->offset)
4110 break;
4111 prev = curr;
4112 curr = curr->next;
4113 }
4114 field->next = prev->next;
4115 prev->next = field;
4116 }
4117}
4118
31de5b77
RG
4119/* This structure is used during pushing fields onto the fieldstack
4120 to track the offset of the field, since bitpos_of_field gives it
4121 relative to its immediate containing type, and we want it relative
4122 to the ultimate containing object. */
4123
4124struct fieldoff
4125{
ee7d4b57
RG
4126 /* Offset from the base of the base containing object to this field. */
4127 HOST_WIDE_INT offset;
31de5b77
RG
4128
4129 /* Size, in bits, of the field. */
ee7d4b57 4130 unsigned HOST_WIDE_INT size;
31de5b77 4131
ee7d4b57 4132 unsigned has_unknown_size : 1;
31de5b77 4133
ee7d4b57 4134 unsigned may_have_pointers : 1;
74d27244
RG
4135
4136 unsigned only_restrict_pointers : 1;
31de5b77
RG
4137};
4138typedef struct fieldoff fieldoff_s;
4139
4140DEF_VEC_O(fieldoff_s);
4141DEF_VEC_ALLOC_O(fieldoff_s,heap);
4142
910fdc79
DB
4143/* qsort comparison function for two fieldoff's PA and PB */
4144
c58936b6 4145static int
910fdc79
DB
4146fieldoff_compare (const void *pa, const void *pb)
4147{
4148 const fieldoff_s *foa = (const fieldoff_s *)pa;
4149 const fieldoff_s *fob = (const fieldoff_s *)pb;
185ab3b6 4150 unsigned HOST_WIDE_INT foasize, fobsize;
c58936b6 4151
185ab3b6
RG
4152 if (foa->offset < fob->offset)
4153 return -1;
4154 else if (foa->offset > fob->offset)
4155 return 1;
910fdc79 4156
ee7d4b57
RG
4157 foasize = foa->size;
4158 fobsize = fob->size;
185ab3b6 4159 if (foasize < fobsize)
ee7d4b57 4160 return -1;
185ab3b6
RG
4161 else if (foasize > fobsize)
4162 return 1;
4163 return 0;
910fdc79
DB
4164}
4165
4166/* Sort a fieldstack according to the field offset and sizes. */
31de5b77 4167static void
83f676b3 4168sort_fieldstack (VEC(fieldoff_s,heap) *fieldstack)
910fdc79 4169{
c58936b6
DB
4170 qsort (VEC_address (fieldoff_s, fieldstack),
4171 VEC_length (fieldoff_s, fieldstack),
910fdc79
DB
4172 sizeof (fieldoff_s),
4173 fieldoff_compare);
4174}
4175
31de5b77
RG
4176/* Return true if V is a tree that we can have subvars for.
4177 Normally, this is any aggregate type. Also complex
4178 types which are not gimple registers can have subvars. */
4179
4180static inline bool
4181var_can_have_subvars (const_tree v)
4182{
4183 /* Volatile variables should never have subvars. */
4184 if (TREE_THIS_VOLATILE (v))
4185 return false;
4186
4187 /* Non decls or memory tags can never have subvars. */
5006671f 4188 if (!DECL_P (v))
31de5b77
RG
4189 return false;
4190
4191 /* Aggregates without overlapping fields can have subvars. */
4192 if (TREE_CODE (TREE_TYPE (v)) == RECORD_TYPE)
4193 return true;
4194
4195 return false;
4196}
4197
d7705551
DN
4198/* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
4199 the fields of TYPE onto fieldstack, recording their offsets along
4200 the way.
4201
4202 OFFSET is used to keep track of the offset in this entire
4203 structure, rather than just the immediately containing structure.
ee7d4b57 4204 Returns the number of fields pushed. */
910fdc79 4205
31de5b77 4206static int
c58936b6 4207push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
ee7d4b57 4208 HOST_WIDE_INT offset)
910fdc79
DB
4209{
4210 tree field;
4211 int count = 0;
31de5b77
RG
4212
4213 if (TREE_CODE (type) != RECORD_TYPE)
4214 return 0;
3fe2f42a
RG
4215
4216 /* If the vector of fields is growing too big, bail out early.
4217 Callers check for VEC_length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
4218 sure this fails. */
31de5b77 4219 if (VEC_length (fieldoff_s, *fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE)
3fe2f42a 4220 return 0;
c58936b6 4221
31de5b77
RG
4222 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4223 if (TREE_CODE (field) == FIELD_DECL)
4224 {
4225 bool push = false;
4226 int pushed = 0;
ee7d4b57 4227 HOST_WIDE_INT foff = bitpos_of_field (field);
31de5b77 4228
ee7d4b57
RG
4229 if (!var_can_have_subvars (field)
4230 || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
4231 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
31de5b77
RG
4232 push = true;
4233 else if (!(pushed = push_fields_onto_fieldstack
ee7d4b57 4234 (TREE_TYPE (field), fieldstack, offset + foff))
31de5b77
RG
4235 && (DECL_SIZE (field)
4236 && !integer_zerop (DECL_SIZE (field))))
4237 /* Empty structures may have actual size, like in C++. So
4238 see if we didn't push any subfields and the size is
4239 nonzero, push the field onto the stack. */
4240 push = true;
4241
4242 if (push)
910fdc79 4243 {
ee7d4b57
RG
4244 fieldoff_s *pair = NULL;
4245 bool has_unknown_size = false;
4246
4247 if (!VEC_empty (fieldoff_s, *fieldstack))
4248 pair = VEC_last (fieldoff_s, *fieldstack);
4249
4250 if (!DECL_SIZE (field)
4251 || !host_integerp (DECL_SIZE (field), 1))
4252 has_unknown_size = true;
4253
4254 /* If adjacent fields do not contain pointers merge them. */
4255 if (pair
4256 && !pair->may_have_pointers
4257 && !could_have_pointers (field)
4258 && !pair->has_unknown_size
4259 && !has_unknown_size
4260 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
4261 {
4262 pair = VEC_last (fieldoff_s, *fieldstack);
4263 pair->size += TREE_INT_CST_LOW (DECL_SIZE (field));
4264 }
4265 else
4266 {
4267 pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
4268 pair->offset = offset + foff;
4269 pair->has_unknown_size = has_unknown_size;
4270 if (!has_unknown_size)
4271 pair->size = TREE_INT_CST_LOW (DECL_SIZE (field));
4272 else
4273 pair->size = -1;
4274 pair->may_have_pointers = could_have_pointers (field);
74d27244
RG
4275 pair->only_restrict_pointers
4276 = (!has_unknown_size
4277 && POINTER_TYPE_P (TREE_TYPE (field))
4278 && TYPE_RESTRICT (TREE_TYPE (field)));
ee7d4b57
RG
4279 count++;
4280 }
31de5b77
RG
4281 }
4282 else
4283 count += pushed;
4284 }
910fdc79
DB
4285
4286 return count;
4287}
4288
5006671f
RG
4289/* Count the number of arguments DECL has, and set IS_VARARGS to true
4290 if it is a varargs function. */
4291
4292static unsigned int
4293count_num_arguments (tree decl, bool *is_varargs)
4294{
de925a03 4295 unsigned int num = 0;
5006671f
RG
4296 tree t;
4297
de925a03
RG
4298 /* Capture named arguments for K&R functions. They do not
4299 have a prototype and thus no TYPE_ARG_TYPES. */
4300 for (t = DECL_ARGUMENTS (decl); t; t = TREE_CHAIN (t))
4301 ++num;
c58936b6 4302
de925a03
RG
4303 /* Check if the function has variadic arguments. */
4304 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
4305 if (TREE_VALUE (t) == void_type_node)
4306 break;
4ee00913
DB
4307 if (!t)
4308 *is_varargs = true;
de925a03
RG
4309
4310 return num;
4ee00913
DB
4311}
4312
4313/* Creation function node for DECL, using NAME, and return the index
4314 of the variable we've created for the function. */
4315
4316static unsigned int
4317create_function_info_for (tree decl, const char *name)
4318{
4ee00913 4319 varinfo_t vi;
c58936b6 4320 tree arg;
4ee00913
DB
4321 unsigned int i;
4322 bool is_varargs = false;
4323
4324 /* Create the variable info. */
4325
0bbf2ffa 4326 vi = new_var_info (decl, name);
4ee00913 4327 vi->offset = 0;
4ee00913
DB
4328 vi->size = 1;
4329 vi->fullsize = count_num_arguments (decl, &is_varargs) + 1;
3e5937d7 4330 insert_vi_for_tree (vi->decl, vi);
4ee00913
DB
4331
4332 stats.total_vars++;
4333
4334 /* If it's varargs, we don't know how many arguments it has, so we
e5bae89b 4335 can't do much. */
4ee00913
DB
4336 if (is_varargs)
4337 {
4338 vi->fullsize = ~0;
4339 vi->size = ~0;
4340 vi->is_unknown_size_var = true;
0bbf2ffa 4341 return vi->id;
4ee00913
DB
4342 }
4343
4ee00913
DB
4344 arg = DECL_ARGUMENTS (decl);
4345
6416ae7f 4346 /* Set up variables for each argument. */
4ee00913 4347 for (i = 1; i < vi->fullsize; i++)
c58936b6 4348 {
4ee00913
DB
4349 varinfo_t argvi;
4350 const char *newname;
4351 char *tempname;
4ee00913
DB
4352 tree argdecl = decl;
4353
4354 if (arg)
4355 argdecl = arg;
c58936b6 4356
4ee00913
DB
4357 asprintf (&tempname, "%s.arg%d", name, i-1);
4358 newname = ggc_strdup (tempname);
4359 free (tempname);
4360
0bbf2ffa 4361 argvi = new_var_info (argdecl, newname);
4ee00913
DB
4362 argvi->offset = i;
4363 argvi->size = 1;
e5bae89b 4364 argvi->is_full_var = true;
4ee00913 4365 argvi->fullsize = vi->fullsize;
4cf4d6a3 4366 insert_into_field_list_sorted (vi, argvi);
4ee00913
DB
4367 stats.total_vars ++;
4368 if (arg)
4369 {
3e5937d7 4370 insert_vi_for_tree (arg, argvi);
4ee00913
DB
4371 arg = TREE_CHAIN (arg);
4372 }
4373 }
4cf4d6a3 4374
4ee00913
DB
4375 /* Create a variable for the return var. */
4376 if (DECL_RESULT (decl) != NULL
4377 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4378 {
4379 varinfo_t resultvi;
4380 const char *newname;
4381 char *tempname;
4ee00913
DB
4382 tree resultdecl = decl;
4383
4384 vi->fullsize ++;
4385
4ee00913
DB
4386 if (DECL_RESULT (decl))
4387 resultdecl = DECL_RESULT (decl);
c58936b6 4388
4ee00913
DB
4389 asprintf (&tempname, "%s.result", name);
4390 newname = ggc_strdup (tempname);
4391 free (tempname);
4392
0bbf2ffa 4393 resultvi = new_var_info (resultdecl, newname);
4ee00913
DB
4394 resultvi->offset = i;
4395 resultvi->size = 1;
4396 resultvi->fullsize = vi->fullsize;
e5bae89b 4397 resultvi->is_full_var = true;
4cf4d6a3 4398 insert_into_field_list_sorted (vi, resultvi);
4ee00913
DB
4399 stats.total_vars ++;
4400 if (DECL_RESULT (decl))
3e5937d7 4401 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
4ee00913 4402 }
0bbf2ffa
RG
4403
4404 return vi->id;
c58936b6 4405}
4ee00913 4406
6c11790d 4407
c58936b6 4408/* Return true if FIELDSTACK contains fields that overlap.
6c11790d
DB
4409 FIELDSTACK is assumed to be sorted by offset. */
4410
4411static bool
4412check_for_overlaps (VEC (fieldoff_s,heap) *fieldstack)
4413{
4414 fieldoff_s *fo = NULL;
4415 unsigned int i;
30d2662c 4416 HOST_WIDE_INT lastoffset = -1;
6c11790d
DB
4417
4418 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
4419 {
4420 if (fo->offset == lastoffset)
4421 return true;
4422 lastoffset = fo->offset;
4423 }
4424 return false;
4425}
21392f19 4426
910fdc79
DB
4427/* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
4428 This will also create any varinfo structures necessary for fields
4429 of DECL. */
4430
4431static unsigned int
4432create_variable_info_for (tree decl, const char *name)
4433{
910fdc79 4434 varinfo_t vi;
82d6e6fc
KG
4435 tree decl_type = TREE_TYPE (decl);
4436 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
910fdc79 4437 VEC (fieldoff_s,heap) *fieldstack = NULL;
c58936b6 4438
74d27244 4439 if (var_can_have_subvars (decl) && use_field_sensitive)
82d6e6fc 4440 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
c58936b6 4441
910fdc79
DB
4442 /* If the variable doesn't have subvars, we may end up needing to
4443 sort the field list and create fake variables for all the
4444 fields. */
0bbf2ffa 4445 vi = new_var_info (decl, name);
910fdc79 4446 vi->offset = 0;
9e39dba6 4447 vi->may_have_pointers = could_have_pointers (decl);
4ee00913 4448 if (!declsize
ee7d4b57 4449 || !host_integerp (declsize, 1))
910fdc79
DB
4450 {
4451 vi->is_unknown_size_var = true;
4452 vi->fullsize = ~0;
4453 vi->size = ~0;
4454 }
4455 else
4456 {
4ee00913 4457 vi->fullsize = TREE_INT_CST_LOW (declsize);
910fdc79
DB
4458 vi->size = vi->fullsize;
4459 }
c58936b6 4460
3e5937d7 4461 insert_vi_for_tree (vi->decl, vi);
74d27244
RG
4462 if (vi->is_global_var
4463 && (!flag_whole_program || !in_ipa_mode)
9e39dba6 4464 && vi->may_have_pointers)
13c6bff4 4465 {
74d27244
RG
4466 if (POINTER_TYPE_P (TREE_TYPE (decl))
4467 && TYPE_RESTRICT (TREE_TYPE (decl)))
4468 make_constraint_from_restrict (vi, "GLOBAL_RESTRICT");
4469 make_copy_constraint (vi, nonlocal_id);
13c6bff4 4470 }
910fdc79
DB
4471
4472 stats.total_vars++;
c58936b6 4473 if (use_field_sensitive
c58936b6 4474 && !vi->is_unknown_size_var
98035a75 4475 && var_can_have_subvars (decl)
11948f6b 4476 && VEC_length (fieldoff_s, fieldstack) > 1
98035a75 4477 && VEC_length (fieldoff_s, fieldstack) <= MAX_FIELDS_FOR_FIELD_SENSITIVE)
910fdc79 4478 {
910fdc79 4479 fieldoff_s *fo = NULL;
ee7d4b57 4480 bool notokay = false;
910fdc79 4481 unsigned int i;
910fdc79
DB
4482
4483 for (i = 0; !notokay && VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
4484 {
ee7d4b57 4485 if (fo->has_unknown_size
910fdc79
DB
4486 || fo->offset < 0)
4487 {
4488 notokay = true;
4489 break;
4490 }
4491 }
58b82d2b
DB
4492
4493 /* We can't sort them if we have a field with a variable sized type,
4494 which will make notokay = true. In that case, we are going to return
4495 without creating varinfos for the fields anyway, so sorting them is a
4496 waste to boot. */
6c11790d 4497 if (!notokay)
c58936b6 4498 {
6c11790d
DB
4499 sort_fieldstack (fieldstack);
4500 /* Due to some C++ FE issues, like PR 22488, we might end up
4501 what appear to be overlapping fields even though they,
4502 in reality, do not overlap. Until the C++ FE is fixed,
4503 we will simply disable field-sensitivity for these cases. */
4504 notokay = check_for_overlaps (fieldstack);
4505 }
c58936b6
DB
4506
4507
910fdc79
DB
4508 if (VEC_length (fieldoff_s, fieldstack) != 0)
4509 fo = VEC_index (fieldoff_s, fieldstack, 0);
4510
4511 if (fo == NULL || notokay)
4512 {
4513 vi->is_unknown_size_var = 1;
4514 vi->fullsize = ~0;
4515 vi->size = ~0;
e5bae89b 4516 vi->is_full_var = true;
910fdc79 4517 VEC_free (fieldoff_s, heap, fieldstack);
0bbf2ffa 4518 return vi->id;
910fdc79 4519 }
c58936b6 4520
ee7d4b57 4521 vi->size = fo->size;
046a69e0 4522 vi->offset = fo->offset;
9e39dba6 4523 vi->may_have_pointers = fo->may_have_pointers;
10174ddf
MM
4524 if (vi->is_global_var
4525 && (!flag_whole_program || !in_ipa_mode)
4526 && vi->may_have_pointers)
4527 {
4528 if (fo->only_restrict_pointers)
4529 make_constraint_from_restrict (vi, "GLOBAL_RESTRICT");
4530 }
c58936b6
DB
4531 for (i = VEC_length (fieldoff_s, fieldstack) - 1;
4532 i >= 1 && VEC_iterate (fieldoff_s, fieldstack, i, fo);
4cf4d6a3 4533 i--)
910fdc79
DB
4534 {
4535 varinfo_t newvi;
4f6c9110 4536 const char *newname = "NULL";
910fdc79
DB
4537 char *tempname;
4538
4f6c9110
RG
4539 if (dump_file)
4540 {
ee7d4b57
RG
4541 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC
4542 "+" HOST_WIDE_INT_PRINT_DEC,
4543 vi->name, fo->offset, fo->size);
4f6c9110
RG
4544 newname = ggc_strdup (tempname);
4545 free (tempname);
4546 }
0bbf2ffa 4547 newvi = new_var_info (decl, newname);
910fdc79 4548 newvi->offset = fo->offset;
ee7d4b57 4549 newvi->size = fo->size;
910fdc79 4550 newvi->fullsize = vi->fullsize;
9e39dba6 4551 newvi->may_have_pointers = fo->may_have_pointers;
910fdc79 4552 insert_into_field_list (vi, newvi);
bacd3fb6 4553 if ((newvi->is_global_var || TREE_CODE (decl) == PARM_DECL)
9e39dba6 4554 && newvi->may_have_pointers)
74d27244
RG
4555 {
4556 if (fo->only_restrict_pointers)
4557 make_constraint_from_restrict (newvi, "GLOBAL_RESTRICT");
bacd3fb6
RG
4558 if (newvi->is_global_var && !in_ipa_mode)
4559 make_copy_constraint (newvi, nonlocal_id);
74d27244 4560 }
c58936b6 4561
4ee00913 4562 stats.total_vars++;
910fdc79 4563 }
910fdc79 4564 }
e5bae89b
RG
4565 else
4566 vi->is_full_var = true;
efe9e829
RG
4567
4568 VEC_free (fieldoff_s, heap, fieldstack);
4569
0bbf2ffa 4570 return vi->id;
910fdc79
DB
4571}
4572
4573/* Print out the points-to solution for VAR to FILE. */
4574
5006671f 4575static void
910fdc79
DB
4576dump_solution_for_var (FILE *file, unsigned int var)
4577{
4578 varinfo_t vi = get_varinfo (var);
4579 unsigned int i;
c58936b6
DB
4580 bitmap_iterator bi;
4581
3e5937d7 4582 if (find (var) != var)
910fdc79 4583 {
3e5937d7 4584 varinfo_t vipt = get_varinfo (find (var));
c58936b6
DB
4585 fprintf (file, "%s = same as %s\n", vi->name, vipt->name);
4586 }
4587 else
4588 {
4589 fprintf (file, "%s = { ", vi->name);
3e5937d7 4590 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
c58936b6
DB
4591 {
4592 fprintf (file, "%s ", get_varinfo (i)->name);
4593 }
4d7a65ea 4594 fprintf (file, "}\n");
910fdc79 4595 }
910fdc79
DB
4596}
4597
4598/* Print the points-to solution for VAR to stdout. */
4599
4600void
4601debug_solution_for_var (unsigned int var)
4602{
4603 dump_solution_for_var (stdout, var);
4604}
4605
910fdc79
DB
4606/* Create varinfo structures for all of the variables in the
4607 function for intraprocedural mode. */
4608
4609static void
4610intra_create_variable_infos (void)
4611{
4612 tree t;
b23987ec 4613
6e7e772d 4614 /* For each incoming pointer argument arg, create the constraint ARG
b7091901 4615 = NONLOCAL or a dummy variable if flag_argument_noalias is set. */
910fdc79
DB
4616 for (t = DECL_ARGUMENTS (current_function_decl); t; t = TREE_CHAIN (t))
4617 {
910fdc79 4618 varinfo_t p;
c58936b6 4619
21392f19
DB
4620 if (!could_have_pointers (t))
4621 continue;
c58936b6 4622
bacd3fb6
RG
4623 /* For restrict qualified pointers to objects passed by
4624 reference build a real representative for the pointed-to object. */
4625 if (DECL_BY_REFERENCE (t)
4626 && POINTER_TYPE_P (TREE_TYPE (t))
4627 && TYPE_RESTRICT (TREE_TYPE (t)))
4628 {
4629 struct constraint_expr lhsc, rhsc;
4630 varinfo_t vi;
4631 tree heapvar = heapvar_lookup (t, 0);
4632 if (heapvar == NULL_TREE)
4633 {
4634 var_ann_t ann;
4635 heapvar = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (t)),
4636 "PARM_NOALIAS");
4637 DECL_EXTERNAL (heapvar) = 1;
4638 heapvar_insert (t, 0, heapvar);
4639 ann = get_var_ann (heapvar);
4640 ann->is_heapvar = 1;
4641 }
4642 if (gimple_referenced_vars (cfun))
4643 add_referenced_var (heapvar);
4644 lhsc.var = get_vi_for_tree (t)->id;
4645 lhsc.type = SCALAR;
4646 lhsc.offset = 0;
4647 rhsc.var = (vi = get_vi_for_tree (heapvar))->id;
4648 rhsc.type = ADDRESSOF;
4649 rhsc.offset = 0;
4650 process_constraint (new_constraint (lhsc, rhsc));
4651 vi->is_restrict_var = 1;
4652 continue;
4653 }
4654
10174ddf 4655 for (p = get_vi_for_tree (t); p; p = p->next)
bacd3fb6
RG
4656 if (p->may_have_pointers)
4657 make_constraint_from (p, nonlocal_id);
74d27244
RG
4658 if (POINTER_TYPE_P (TREE_TYPE (t))
4659 && TYPE_RESTRICT (TREE_TYPE (t)))
4660 make_constraint_from_restrict (get_vi_for_tree (t), "PARM_RESTRICT");
21392f19 4661 }
75af9746 4662
10bd6c5c
RG
4663 /* Add a constraint for a result decl that is passed by reference. */
4664 if (DECL_RESULT (cfun->decl)
4665 && DECL_BY_REFERENCE (DECL_RESULT (cfun->decl)))
4666 {
4667 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (cfun->decl));
4668
4669 for (p = result_vi; p; p = p->next)
5006671f 4670 make_constraint_from (p, nonlocal_id);
10bd6c5c
RG
4671 }
4672
75af9746
RG
4673 /* Add a constraint for the incoming static chain parameter. */
4674 if (cfun->static_chain_decl != NULL_TREE)
4675 {
4676 varinfo_t p, chain_vi = get_vi_for_tree (cfun->static_chain_decl);
4677
4678 for (p = chain_vi; p; p = p->next)
4679 make_constraint_from (p, nonlocal_id);
4680 }
910fdc79
DB
4681}
4682
1296c31f
DB
4683/* Structure used to put solution bitmaps in a hashtable so they can
4684 be shared among variables with the same points-to set. */
4685
4686typedef struct shared_bitmap_info
4687{
4688 bitmap pt_vars;
4689 hashval_t hashcode;
4690} *shared_bitmap_info_t;
e5cfc29f 4691typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
1296c31f
DB
4692
4693static htab_t shared_bitmap_table;
4694
4695/* Hash function for a shared_bitmap_info_t */
4696
4697static hashval_t
4698shared_bitmap_hash (const void *p)
4699{
e5cfc29f 4700 const_shared_bitmap_info_t const bi = (const_shared_bitmap_info_t) p;
1296c31f
DB
4701 return bi->hashcode;
4702}
4703
4704/* Equality function for two shared_bitmap_info_t's. */
4705
4706static int
4707shared_bitmap_eq (const void *p1, const void *p2)
4708{
e5cfc29f
KG
4709 const_shared_bitmap_info_t const sbi1 = (const_shared_bitmap_info_t) p1;
4710 const_shared_bitmap_info_t const sbi2 = (const_shared_bitmap_info_t) p2;
1296c31f
DB
4711 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
4712}
4713
4714/* Lookup a bitmap in the shared bitmap hashtable, and return an already
4715 existing instance if there is one, NULL otherwise. */
4716
4717static bitmap
4718shared_bitmap_lookup (bitmap pt_vars)
4719{
4720 void **slot;
4721 struct shared_bitmap_info sbi;
4722
4723 sbi.pt_vars = pt_vars;
4724 sbi.hashcode = bitmap_hash (pt_vars);
7b765bed 4725
1296c31f
DB
4726 slot = htab_find_slot_with_hash (shared_bitmap_table, &sbi,
4727 sbi.hashcode, NO_INSERT);
4728 if (!slot)
4729 return NULL;
4730 else
4731 return ((shared_bitmap_info_t) *slot)->pt_vars;
4732}
4733
4734
4735/* Add a bitmap to the shared bitmap hashtable. */
4736
4737static void
4738shared_bitmap_add (bitmap pt_vars)
4739{
4740 void **slot;
4741 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
7b765bed 4742
1296c31f
DB
4743 sbi->pt_vars = pt_vars;
4744 sbi->hashcode = bitmap_hash (pt_vars);
7b765bed 4745
1296c31f
DB
4746 slot = htab_find_slot_with_hash (shared_bitmap_table, sbi,
4747 sbi->hashcode, INSERT);
4748 gcc_assert (!*slot);
4749 *slot = (void *) sbi;
4750}
4751
4752
4d7a65ea 4753/* Set bits in INTO corresponding to the variable uids in solution set FROM. */
910fdc79 4754
b8698a0f 4755static void
4d7a65ea 4756set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt)
910fdc79
DB
4757{
4758 unsigned int i;
4759 bitmap_iterator bi;
f83ca251 4760
910fdc79
DB
4761 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
4762 {
4763 varinfo_t vi = get_varinfo (i);
c58936b6 4764
e8ca4159
DN
4765 /* The only artificial variables that are allowed in a may-alias
4766 set are heap variables. */
4767 if (vi->is_artificial_var && !vi->is_heap_var)
4768 continue;
c58936b6 4769
5611cf0b
RG
4770 if (TREE_CODE (vi->decl) == VAR_DECL
4771 || TREE_CODE (vi->decl) == PARM_DECL
4772 || TREE_CODE (vi->decl) == RESULT_DECL)
58b82d2b 4773 {
5006671f
RG
4774 /* Add the decl to the points-to set. Note that the points-to
4775 set contains global variables. */
4776 bitmap_set_bit (into, DECL_UID (vi->decl));
74d27244 4777 if (vi->is_global_var)
5006671f 4778 pt->vars_contains_global = true;
e8ca4159 4779 }
910fdc79
DB
4780 }
4781}
e8ca4159
DN
4782
4783
4d7a65ea 4784/* Compute the points-to solution *PT for the variable VI. */
ce1b6498
RG
4785
4786static void
4d7a65ea 4787find_what_var_points_to (varinfo_t vi, struct pt_solution *pt)
ce1b6498 4788{
4d7a65ea 4789 unsigned int i;
5006671f
RG
4790 bitmap_iterator bi;
4791 bitmap finished_solution;
4792 bitmap result;
5006671f
RG
4793
4794 memset (pt, 0, sizeof (struct pt_solution));
4795
4796 /* This variable may have been collapsed, let's get the real
4797 variable. */
4798 vi = get_varinfo (find (vi->id));
4799
4800 /* Translate artificial variables into SSA_NAME_PTR_INFO
4801 attributes. */
4802 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
4803 {
4804 varinfo_t vi = get_varinfo (i);
4805
4806 if (vi->is_artificial_var)
4807 {
4808 if (vi->id == nothing_id)
4809 pt->null = 1;
4810 else if (vi->id == escaped_id)
4811 pt->escaped = 1;
4812 else if (vi->id == callused_id)
4813 gcc_unreachable ();
4814 else if (vi->id == nonlocal_id)
4815 pt->nonlocal = 1;
4816 else if (vi->is_heap_var)
4817 /* We represent heapvars in the points-to set properly. */
4818 ;
91deb937
RG
4819 else if (vi->id == readonly_id)
4820 /* Nobody cares. */
4821 ;
5006671f 4822 else if (vi->id == anything_id
5006671f
RG
4823 || vi->id == integer_id)
4824 pt->anything = 1;
4825 }
74d27244
RG
4826 if (vi->is_restrict_var)
4827 pt->vars_contains_restrict = true;
5006671f
RG
4828 }
4829
4830 /* Instead of doing extra work, simply do not create
4831 elaborate points-to information for pt_anything pointers. */
74d27244
RG
4832 if (pt->anything
4833 && (vi->is_artificial_var
4834 || !pt->vars_contains_restrict))
4d7a65ea 4835 return;
5006671f
RG
4836
4837 /* Share the final set of variables when possible. */
4838 finished_solution = BITMAP_GGC_ALLOC ();
4839 stats.points_to_sets_created++;
4840
4d7a65ea 4841 set_uids_in_ptset (finished_solution, vi->solution, pt);
5006671f
RG
4842 result = shared_bitmap_lookup (finished_solution);
4843 if (!result)
4844 {
4845 shared_bitmap_add (finished_solution);
4846 pt->vars = finished_solution;
4847 }
4848 else
4849 {
4850 pt->vars = result;
4851 bitmap_clear (finished_solution);
4852 }
5006671f
RG
4853}
4854
4d7a65ea 4855/* Given a pointer variable P, fill in its points-to set. */
5006671f
RG
4856
4857static void
4d7a65ea 4858find_what_p_points_to (tree p)
5006671f
RG
4859{
4860 struct ptr_info_def *pi;
7cc92f92 4861 tree lookup_p = p;
3e5937d7 4862 varinfo_t vi;
e8ca4159 4863
7cc92f92
RG
4864 /* For parameters, get at the points-to set for the actual parm
4865 decl. */
c58936b6
DB
4866 if (TREE_CODE (p) == SSA_NAME
4867 && TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
38635499 4868 && SSA_NAME_IS_DEFAULT_DEF (p))
7cc92f92
RG
4869 lookup_p = SSA_NAME_VAR (p);
4870
15814ba0 4871 vi = lookup_vi_for_tree (lookup_p);
5006671f
RG
4872 if (!vi)
4873 return;
4874
4875 pi = get_ptr_info (p);
4d7a65ea 4876 find_what_var_points_to (vi, &pi->pt);
5006671f 4877}
7b765bed 4878
910fdc79 4879
5006671f 4880/* Query statistics for points-to solutions. */
c58936b6 4881
5006671f
RG
4882static struct {
4883 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
4884 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
4885 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
4886 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
4887} pta_stats;
e8ca4159 4888
5006671f
RG
4889void
4890dump_pta_stats (FILE *s)
4891{
4892 fprintf (s, "\nPTA query stats:\n");
4893 fprintf (s, " pt_solution_includes: "
4894 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
4895 HOST_WIDE_INT_PRINT_DEC" queries\n",
4896 pta_stats.pt_solution_includes_no_alias,
4897 pta_stats.pt_solution_includes_no_alias
4898 + pta_stats.pt_solution_includes_may_alias);
4899 fprintf (s, " pt_solutions_intersect: "
4900 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
4901 HOST_WIDE_INT_PRINT_DEC" queries\n",
4902 pta_stats.pt_solutions_intersect_no_alias,
4903 pta_stats.pt_solutions_intersect_no_alias
4904 + pta_stats.pt_solutions_intersect_may_alias);
4905}
e8ca4159 4906
9f09b13f 4907
5006671f
RG
4908/* Reset the points-to solution *PT to a conservative default
4909 (point to anything). */
7b765bed 4910
5006671f
RG
4911void
4912pt_solution_reset (struct pt_solution *pt)
4913{
4914 memset (pt, 0, sizeof (struct pt_solution));
4915 pt->anything = true;
4916}
1296c31f 4917
55b34b5f
RG
4918/* Set the points-to solution *PT to point only to the variables
4919 in VARS. */
4920
4921void
4922pt_solution_set (struct pt_solution *pt, bitmap vars)
4923{
4924 bitmap_iterator bi;
4925 unsigned i;
4926
4927 memset (pt, 0, sizeof (struct pt_solution));
4928 pt->vars = vars;
4929 EXECUTE_IF_SET_IN_BITMAP (vars, 0, i, bi)
4930 {
4931 tree var = referenced_var_lookup (i);
4932 if (is_global_var (var))
4933 {
4934 pt->vars_contains_global = true;
4935 break;
4936 }
4937 }
4938}
4939
5006671f 4940/* Return true if the points-to solution *PT is empty. */
e8ca4159 4941
5006671f
RG
4942static bool
4943pt_solution_empty_p (struct pt_solution *pt)
4944{
4945 if (pt->anything
4946 || pt->nonlocal)
4947 return false;
e8ca4159 4948
5006671f
RG
4949 if (pt->vars
4950 && !bitmap_empty_p (pt->vars))
4951 return false;
e8ca4159 4952
5006671f
RG
4953 /* If the solution includes ESCAPED, check if that is empty. */
4954 if (pt->escaped
4955 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
4956 return false;
4957
4958 return true;
910fdc79
DB
4959}
4960
5006671f 4961/* Return true if the points-to solution *PT includes global memory. */
63a4ef6f 4962
2f571334 4963bool
5006671f 4964pt_solution_includes_global (struct pt_solution *pt)
2f571334 4965{
5006671f
RG
4966 if (pt->anything
4967 || pt->nonlocal
4968 || pt->vars_contains_global)
4969 return true;
2f571334 4970
5006671f
RG
4971 if (pt->escaped)
4972 return pt_solution_includes_global (&cfun->gimple_df->escaped);
2f571334 4973
5006671f
RG
4974 return false;
4975}
2f571334 4976
5006671f
RG
4977/* Return true if the points-to solution *PT includes the variable
4978 declaration DECL. */
15c15196 4979
5006671f
RG
4980static bool
4981pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
4982{
4983 if (pt->anything)
4984 return true;
2f571334 4985
5006671f
RG
4986 if (pt->nonlocal
4987 && is_global_var (decl))
4988 return true;
2f571334 4989
5006671f
RG
4990 if (pt->vars
4991 && bitmap_bit_p (pt->vars, DECL_UID (decl)))
4992 return true;
2f571334 4993
5006671f
RG
4994 /* If the solution includes ESCAPED, check it. */
4995 if (pt->escaped
4996 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
4997 return true;
2f571334 4998
5006671f 4999 return false;
2f571334 5000}
910fdc79 5001
5006671f
RG
5002bool
5003pt_solution_includes (struct pt_solution *pt, const_tree decl)
15c15196 5004{
5006671f
RG
5005 bool res = pt_solution_includes_1 (pt, decl);
5006 if (res)
5007 ++pta_stats.pt_solution_includes_may_alias;
5008 else
5009 ++pta_stats.pt_solution_includes_no_alias;
5010 return res;
5011}
15c15196 5012
5006671f
RG
5013/* Return true if both points-to solutions PT1 and PT2 have a non-empty
5014 intersection. */
15c15196 5015
5006671f
RG
5016static bool
5017pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
5018{
5019 if (pt1->anything || pt2->anything)
5020 return true;
15c15196 5021
5006671f
RG
5022 /* If either points to unknown global memory and the other points to
5023 any global memory they alias. */
5024 if ((pt1->nonlocal
5025 && (pt2->nonlocal
5026 || pt2->vars_contains_global))
5027 || (pt2->nonlocal
5028 && pt1->vars_contains_global))
5029 return true;
15c15196 5030
5006671f
RG
5031 /* Check the escaped solution if required. */
5032 if ((pt1->escaped || pt2->escaped)
5033 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
5034 {
5035 /* If both point to escaped memory and that solution
5036 is not empty they alias. */
5037 if (pt1->escaped && pt2->escaped)
5038 return true;
15c15196 5039
5006671f
RG
5040 /* If either points to escaped memory see if the escaped solution
5041 intersects with the other. */
5042 if ((pt1->escaped
5043 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt2))
5044 || (pt2->escaped
5045 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt1)))
5046 return true;
15c15196
RG
5047 }
5048
5006671f
RG
5049 /* Now both pointers alias if their points-to solution intersects. */
5050 return (pt1->vars
5051 && pt2->vars
5052 && bitmap_intersect_p (pt1->vars, pt2->vars));
5053}
5054
5055bool
5056pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
5057{
5058 bool res = pt_solutions_intersect_1 (pt1, pt2);
5059 if (res)
5060 ++pta_stats.pt_solutions_intersect_may_alias;
5061 else
5062 ++pta_stats.pt_solutions_intersect_no_alias;
5063 return res;
15c15196
RG
5064}
5065
74d27244
RG
5066/* Return true if both points-to solutions PT1 and PT2 for two restrict
5067 qualified pointers are possibly based on the same pointer. */
5068
5069bool
5070pt_solutions_same_restrict_base (struct pt_solution *pt1,
5071 struct pt_solution *pt2)
5072{
5073 /* If we deal with points-to solutions of two restrict qualified
5074 pointers solely rely on the pointed-to variable bitmap intersection.
5075 For two pointers that are based on each other the bitmaps will
5076 intersect. */
5077 if (pt1->vars_contains_restrict
5078 && pt2->vars_contains_restrict)
5079 {
5080 gcc_assert (pt1->vars && pt2->vars);
5081 return bitmap_intersect_p (pt1->vars, pt2->vars);
5082 }
5083
5084 return true;
5085}
5086
b7091901 5087
63a4ef6f
DN
5088/* Dump points-to information to OUTFILE. */
5089
5006671f 5090static void
910fdc79
DB
5091dump_sa_points_to_info (FILE *outfile)
5092{
910fdc79 5093 unsigned int i;
63a4ef6f 5094
e8ca4159 5095 fprintf (outfile, "\nPoints-to sets\n\n");
63a4ef6f 5096
910fdc79
DB
5097 if (dump_flags & TDF_STATS)
5098 {
5099 fprintf (outfile, "Stats:\n");
63a4ef6f 5100 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
3e5937d7
DB
5101 fprintf (outfile, "Non-pointer vars: %d\n",
5102 stats.nonpointer_vars);
63a4ef6f
DN
5103 fprintf (outfile, "Statically unified vars: %d\n",
5104 stats.unified_vars_static);
63a4ef6f
DN
5105 fprintf (outfile, "Dynamically unified vars: %d\n",
5106 stats.unified_vars_dynamic);
5107 fprintf (outfile, "Iterations: %d\n", stats.iterations);
4ee00913 5108 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
3e5937d7
DB
5109 fprintf (outfile, "Number of implicit edges: %d\n",
5110 stats.num_implicit_edges);
910fdc79 5111 }
63a4ef6f 5112
910fdc79
DB
5113 for (i = 0; i < VEC_length (varinfo_t, varmap); i++)
5114 dump_solution_for_var (outfile, i);
5115}
5116
5117
63a4ef6f
DN
5118/* Debug points-to information to stderr. */
5119
5120void
5121debug_sa_points_to_info (void)
5122{
5123 dump_sa_points_to_info (stderr);
5124}
5125
5126
910fdc79
DB
5127/* Initialize the always-existing constraint variables for NULL
5128 ANYTHING, READONLY, and INTEGER */
5129
5130static void
5131init_base_vars (void)
5132{
5133 struct constraint_expr lhs, rhs;
0bbf2ffa
RG
5134 varinfo_t var_anything;
5135 varinfo_t var_nothing;
5136 varinfo_t var_readonly;
5137 varinfo_t var_escaped;
5138 varinfo_t var_nonlocal;
5139 varinfo_t var_callused;
5140 varinfo_t var_storedanything;
5141 varinfo_t var_integer;
910fdc79
DB
5142
5143 /* Create the NULL variable, used to represent that a variable points
5144 to NULL. */
0bbf2ffa
RG
5145 var_nothing = new_var_info (NULL_TREE, "NULL");
5146 gcc_assert (var_nothing->id == nothing_id);
910fdc79
DB
5147 var_nothing->is_artificial_var = 1;
5148 var_nothing->offset = 0;
5149 var_nothing->size = ~0;
5150 var_nothing->fullsize = ~0;
13c2c08b 5151 var_nothing->is_special_var = 1;
910fdc79
DB
5152
5153 /* Create the ANYTHING variable, used to represent that a variable
5154 points to some unknown piece of memory. */
0bbf2ffa
RG
5155 var_anything = new_var_info (NULL_TREE, "ANYTHING");
5156 gcc_assert (var_anything->id == anything_id);
910fdc79
DB
5157 var_anything->is_artificial_var = 1;
5158 var_anything->size = ~0;
5159 var_anything->offset = 0;
5160 var_anything->next = NULL;
5161 var_anything->fullsize = ~0;
13c2c08b 5162 var_anything->is_special_var = 1;
910fdc79
DB
5163
5164 /* Anything points to anything. This makes deref constraints just
c58936b6 5165 work in the presence of linked list and other p = *p type loops,
910fdc79 5166 by saying that *ANYTHING = ANYTHING. */
910fdc79
DB
5167 lhs.type = SCALAR;
5168 lhs.var = anything_id;
5169 lhs.offset = 0;
3e5937d7 5170 rhs.type = ADDRESSOF;
910fdc79
DB
5171 rhs.var = anything_id;
5172 rhs.offset = 0;
e8ca4159 5173
a5eadacc
DB
5174 /* This specifically does not use process_constraint because
5175 process_constraint ignores all anything = anything constraints, since all
5176 but this one are redundant. */
b5efa470 5177 VEC_safe_push (constraint_t, heap, constraints, new_constraint (lhs, rhs));
c58936b6 5178
910fdc79
DB
5179 /* Create the READONLY variable, used to represent that a variable
5180 points to readonly memory. */
0bbf2ffa
RG
5181 var_readonly = new_var_info (NULL_TREE, "READONLY");
5182 gcc_assert (var_readonly->id == readonly_id);
910fdc79
DB
5183 var_readonly->is_artificial_var = 1;
5184 var_readonly->offset = 0;
5185 var_readonly->size = ~0;
5186 var_readonly->fullsize = ~0;
5187 var_readonly->next = NULL;
13c2c08b 5188 var_readonly->is_special_var = 1;
910fdc79
DB
5189
5190 /* readonly memory points to anything, in order to make deref
5191 easier. In reality, it points to anything the particular
5192 readonly variable can point to, but we don't track this
607fb860 5193 separately. */
910fdc79
DB
5194 lhs.type = SCALAR;
5195 lhs.var = readonly_id;
5196 lhs.offset = 0;
3e5937d7 5197 rhs.type = ADDRESSOF;
b7091901 5198 rhs.var = readonly_id; /* FIXME */
910fdc79 5199 rhs.offset = 0;
b7091901 5200 process_constraint (new_constraint (lhs, rhs));
c58936b6 5201
b7091901
RG
5202 /* Create the ESCAPED variable, used to represent the set of escaped
5203 memory. */
0bbf2ffa
RG
5204 var_escaped = new_var_info (NULL_TREE, "ESCAPED");
5205 gcc_assert (var_escaped->id == escaped_id);
b7091901
RG
5206 var_escaped->is_artificial_var = 1;
5207 var_escaped->offset = 0;
5208 var_escaped->size = ~0;
5209 var_escaped->fullsize = ~0;
5210 var_escaped->is_special_var = 0;
b7091901 5211
b7091901
RG
5212 /* Create the NONLOCAL variable, used to represent the set of nonlocal
5213 memory. */
0bbf2ffa
RG
5214 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL");
5215 gcc_assert (var_nonlocal->id == nonlocal_id);
b7091901
RG
5216 var_nonlocal->is_artificial_var = 1;
5217 var_nonlocal->offset = 0;
5218 var_nonlocal->size = ~0;
5219 var_nonlocal->fullsize = ~0;
5220 var_nonlocal->is_special_var = 1;
b7091901 5221
5006671f
RG
5222 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
5223 lhs.type = SCALAR;
5224 lhs.var = escaped_id;
5225 lhs.offset = 0;
5226 rhs.type = DEREF;
5227 rhs.var = escaped_id;
5228 rhs.offset = 0;
5229 process_constraint (new_constraint (lhs, rhs));
5230
5231 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
5232 whole variable escapes. */
5233 lhs.type = SCALAR;
5234 lhs.var = escaped_id;
5235 lhs.offset = 0;
5236 rhs.type = SCALAR;
5237 rhs.var = escaped_id;
5238 rhs.offset = UNKNOWN_OFFSET;
5239 process_constraint (new_constraint (lhs, rhs));
5240
5241 /* *ESCAPED = NONLOCAL. This is true because we have to assume
5242 everything pointed to by escaped points to what global memory can
5243 point to. */
5244 lhs.type = DEREF;
5245 lhs.var = escaped_id;
5246 lhs.offset = 0;
5247 rhs.type = SCALAR;
5248 rhs.var = nonlocal_id;
5249 rhs.offset = 0;
5250 process_constraint (new_constraint (lhs, rhs));
5251
5252 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
5253 global memory may point to global memory and escaped memory. */
b7091901
RG
5254 lhs.type = SCALAR;
5255 lhs.var = nonlocal_id;
5256 lhs.offset = 0;
5257 rhs.type = ADDRESSOF;
5006671f
RG
5258 rhs.var = nonlocal_id;
5259 rhs.offset = 0;
5260 process_constraint (new_constraint (lhs, rhs));
5261 rhs.type = ADDRESSOF;
b7091901
RG
5262 rhs.var = escaped_id;
5263 rhs.offset = 0;
910fdc79 5264 process_constraint (new_constraint (lhs, rhs));
c58936b6 5265
15c15196
RG
5266 /* Create the CALLUSED variable, used to represent the set of call-used
5267 memory. */
0bbf2ffa
RG
5268 var_callused = new_var_info (NULL_TREE, "CALLUSED");
5269 gcc_assert (var_callused->id == callused_id);
15c15196
RG
5270 var_callused->is_artificial_var = 1;
5271 var_callused->offset = 0;
5272 var_callused->size = ~0;
5273 var_callused->fullsize = ~0;
5274 var_callused->is_special_var = 0;
15c15196
RG
5275
5276 /* CALLUSED = *CALLUSED, because call-used is may-deref'd at calls, etc. */
5277 lhs.type = SCALAR;
5278 lhs.var = callused_id;
5279 lhs.offset = 0;
5280 rhs.type = DEREF;
5281 rhs.var = callused_id;
5282 rhs.offset = 0;
faf2ecc5 5283 process_constraint (new_constraint (lhs, rhs));
15c15196 5284
5006671f
RG
5285 /* CALLUSED = CALLUSED + UNKNOWN, because if a sub-field is call-used the
5286 whole variable is call-used. */
5287 lhs.type = SCALAR;
5288 lhs.var = callused_id;
5289 lhs.offset = 0;
5290 rhs.type = SCALAR;
5291 rhs.var = callused_id;
5292 rhs.offset = UNKNOWN_OFFSET;
5293 process_constraint (new_constraint (lhs, rhs));
5294
9e39dba6
RG
5295 /* Create the STOREDANYTHING variable, used to represent the set of
5296 variables stored to *ANYTHING. */
0bbf2ffa
RG
5297 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING");
5298 gcc_assert (var_storedanything->id == storedanything_id);
9e39dba6
RG
5299 var_storedanything->is_artificial_var = 1;
5300 var_storedanything->offset = 0;
5301 var_storedanything->size = ~0;
5302 var_storedanything->fullsize = ~0;
5303 var_storedanything->is_special_var = 0;
9e39dba6 5304
910fdc79 5305 /* Create the INTEGER variable, used to represent that a variable points
5006671f 5306 to what an INTEGER "points to". */
0bbf2ffa
RG
5307 var_integer = new_var_info (NULL_TREE, "INTEGER");
5308 gcc_assert (var_integer->id == integer_id);
910fdc79
DB
5309 var_integer->is_artificial_var = 1;
5310 var_integer->size = ~0;
5311 var_integer->fullsize = ~0;
5312 var_integer->offset = 0;
5313 var_integer->next = NULL;
13c2c08b 5314 var_integer->is_special_var = 1;
a5eadacc 5315
21392f19
DB
5316 /* INTEGER = ANYTHING, because we don't know where a dereference of
5317 a random integer will point to. */
a5eadacc
DB
5318 lhs.type = SCALAR;
5319 lhs.var = integer_id;
5320 lhs.offset = 0;
3e5937d7 5321 rhs.type = ADDRESSOF;
a5eadacc
DB
5322 rhs.var = anything_id;
5323 rhs.offset = 0;
5324 process_constraint (new_constraint (lhs, rhs));
c58936b6 5325}
910fdc79 5326
4ee00913 5327/* Initialize things necessary to perform PTA */
910fdc79 5328
4ee00913
DB
5329static void
5330init_alias_vars (void)
910fdc79 5331{
e5bae89b
RG
5332 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
5333
3e5937d7
DB
5334 bitmap_obstack_initialize (&pta_obstack);
5335 bitmap_obstack_initialize (&oldpta_obstack);
4ee00913 5336 bitmap_obstack_initialize (&predbitmap_obstack);
910fdc79 5337
c58936b6 5338 constraint_pool = create_alloc_pool ("Constraint pool",
910fdc79
DB
5339 sizeof (struct constraint), 30);
5340 variable_info_pool = create_alloc_pool ("Variable info pool",
5341 sizeof (struct variable_info), 30);
b5efa470
DB
5342 constraints = VEC_alloc (constraint_t, heap, 8);
5343 varmap = VEC_alloc (varinfo_t, heap, 8);
15814ba0 5344 vi_for_tree = pointer_map_create ();
3e5937d7 5345
910fdc79 5346 memset (&stats, 0, sizeof (stats));
1296c31f
DB
5347 shared_bitmap_table = htab_create (511, shared_bitmap_hash,
5348 shared_bitmap_eq, free);
910fdc79 5349 init_base_vars ();
4ee00913
DB
5350}
5351
3e5937d7
DB
5352/* Remove the REF and ADDRESS edges from GRAPH, as well as all the
5353 predecessor edges. */
5354
5355static void
5356remove_preds_and_fake_succs (constraint_graph_t graph)
5357{
5358 unsigned int i;
5359
5360 /* Clear the implicit ref and address nodes from the successor
5361 lists. */
5362 for (i = 0; i < FIRST_REF_NODE; i++)
5363 {
5364 if (graph->succs[i])
5365 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
5366 FIRST_REF_NODE * 2);
5367 }
5368
5369 /* Free the successor list for the non-ref nodes. */
5370 for (i = FIRST_REF_NODE; i < graph->size; i++)
5371 {
5372 if (graph->succs[i])
5373 BITMAP_FREE (graph->succs[i]);
5374 }
5375
5376 /* Now reallocate the size of the successor list as, and blow away
5377 the predecessor bitmaps. */
5378 graph->size = VEC_length (varinfo_t, varmap);
c22940cd 5379 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
3e5937d7
DB
5380
5381 free (graph->implicit_preds);
5382 graph->implicit_preds = NULL;
5383 free (graph->preds);
5384 graph->preds = NULL;
5385 bitmap_obstack_release (&predbitmap_obstack);
5386}
5387
5006671f
RG
5388/* Initialize the heapvar for statement mapping. */
5389
5390static void
5391init_alias_heapvars (void)
5392{
5393 if (!heapvar_for_stmt)
8bc88f25 5394 heapvar_for_stmt = htab_create_ggc (11, tree_map_hash, heapvar_map_eq,
5006671f
RG
5395 NULL);
5396}
5397
5398/* Delete the heapvar for statement mapping. */
5399
5400void
5401delete_alias_heapvars (void)
5402{
5403 if (heapvar_for_stmt)
5404 htab_delete (heapvar_for_stmt);
5405 heapvar_for_stmt = NULL;
5406}
5407
5c245b95 5408/* Solve the constraint set. */
4ee00913 5409
5006671f 5410static void
5c245b95 5411solve_constraints (void)
4ee00913 5412{
3e5937d7 5413 struct scc_info *si;
910fdc79 5414
910fdc79
DB
5415 if (dump_file)
5416 {
e8ca4159 5417 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
910fdc79
DB
5418 dump_constraints (dump_file);
5419 }
c58936b6 5420
21392f19
DB
5421 if (dump_file)
5422 fprintf (dump_file,
5423 "\nCollapsing static cycles and doing variable "
7b765bed
DB
5424 "substitution\n");
5425
5426 init_graph (VEC_length (varinfo_t, varmap) * 2);
b8698a0f 5427
7b765bed
DB
5428 if (dump_file)
5429 fprintf (dump_file, "Building predecessor graph\n");
3e5937d7 5430 build_pred_graph ();
b8698a0f 5431
7b765bed
DB
5432 if (dump_file)
5433 fprintf (dump_file, "Detecting pointer and location "
5434 "equivalences\n");
3e5937d7 5435 si = perform_var_substitution (graph);
b8698a0f 5436
7b765bed
DB
5437 if (dump_file)
5438 fprintf (dump_file, "Rewriting constraints and unifying "
5439 "variables\n");
5440 rewrite_constraints (graph, si);
3e5937d7
DB
5441
5442 build_succ_graph ();
9e39dba6 5443 free_var_substitution_info (si);
fc93bcb6
FP
5444
5445 if (dump_file && (dump_flags & TDF_GRAPH))
5446 dump_constraint_graph (dump_file);
5447
7b765bed
DB
5448 move_complex_constraints (graph);
5449
5450 if (dump_file)
5451 fprintf (dump_file, "Uniting pointer but not location equivalent "
5452 "variables\n");
5453 unite_pointer_equivalences (graph);
5454
5455 if (dump_file)
5456 fprintf (dump_file, "Finding indirect cycles\n");
3e5937d7 5457 find_indirect_cycles (graph);
c58936b6 5458
3e5937d7
DB
5459 /* Implicit nodes and predecessors are no longer necessary at this
5460 point. */
5461 remove_preds_and_fake_succs (graph);
c58936b6 5462
21392f19 5463 if (dump_file)
7b765bed 5464 fprintf (dump_file, "Solving graph\n");
c58936b6 5465
21392f19 5466 solve_graph (graph);
c58936b6 5467
910fdc79
DB
5468 if (dump_file)
5469 dump_sa_points_to_info (dump_file);
5c245b95
RG
5470}
5471
5472/* Create points-to sets for the current function. See the comments
5473 at the start of the file for an algorithmic overview. */
5474
5475static void
5476compute_points_to_sets (void)
5477{
5478 basic_block bb;
5479 unsigned i;
5480 varinfo_t vi;
5481
5482 timevar_push (TV_TREE_PTA);
5483
5484 init_alias_vars ();
5485 init_alias_heapvars ();
5486
5487 intra_create_variable_infos ();
5488
5489 /* Now walk all statements and derive aliases. */
5490 FOR_EACH_BB (bb)
5491 {
5492 gimple_stmt_iterator gsi;
5493
5494 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5495 {
5496 gimple phi = gsi_stmt (gsi);
5497
5498 if (is_gimple_reg (gimple_phi_result (phi)))
5499 find_func_aliases (phi);
5500 }
5501
5502 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5503 {
5504 gimple stmt = gsi_stmt (gsi);
5505
5506 find_func_aliases (stmt);
5507 }
5508 }
5509
5510 /* From the constraints compute the points-to sets. */
5511 solve_constraints ();
c58936b6 5512
5006671f
RG
5513 /* Compute the points-to sets for ESCAPED and CALLUSED used for
5514 call-clobber analysis. */
0bbf2ffa
RG
5515 find_what_var_points_to (get_varinfo (escaped_id),
5516 &cfun->gimple_df->escaped);
5517 find_what_var_points_to (get_varinfo (callused_id),
5518 &cfun->gimple_df->callused);
5006671f
RG
5519
5520 /* Make sure the ESCAPED solution (which is used as placeholder in
5521 other solutions) does not reference itself. This simplifies
5522 points-to solution queries. */
5523 cfun->gimple_df->escaped.escaped = 0;
5524
14c41b9b
RG
5525 /* Mark escaped HEAP variables as global. */
5526 for (i = 0; VEC_iterate (varinfo_t, varmap, i, vi); ++i)
5527 if (vi->is_heap_var
91deb937 5528 && !vi->is_restrict_var
14c41b9b 5529 && !vi->is_global_var)
91deb937
RG
5530 DECL_EXTERNAL (vi->decl) = vi->is_global_var
5531 = pt_solution_includes (&cfun->gimple_df->escaped, vi->decl);
14c41b9b 5532
5006671f
RG
5533 /* Compute the points-to sets for pointer SSA_NAMEs. */
5534 for (i = 0; i < num_ssa_names; ++i)
5535 {
5536 tree ptr = ssa_name (i);
5537 if (ptr
5538 && POINTER_TYPE_P (TREE_TYPE (ptr)))
4d7a65ea 5539 find_what_p_points_to (ptr);
5006671f 5540 }
e8ca4159
DN
5541
5542 timevar_pop (TV_TREE_PTA);
910fdc79
DB
5543}
5544
910fdc79
DB
5545
5546/* Delete created points-to sets. */
5547
5006671f 5548static void
e8ca4159 5549delete_points_to_sets (void)
910fdc79 5550{
7b765bed 5551 unsigned int i;
c58936b6 5552
1296c31f 5553 htab_delete (shared_bitmap_table);
3e5937d7
DB
5554 if (dump_file && (dump_flags & TDF_STATS))
5555 fprintf (dump_file, "Points to sets created:%d\n",
5556 stats.points_to_sets_created);
5557
15814ba0 5558 pointer_map_destroy (vi_for_tree);
3e5937d7 5559 bitmap_obstack_release (&pta_obstack);
b5efa470 5560 VEC_free (constraint_t, heap, constraints);
c58936b6 5561
7b765bed 5562 for (i = 0; i < graph->size; i++)
3e5937d7 5563 VEC_free (constraint_t, heap, graph->complex[i]);
285463b5 5564 free (graph->complex);
21392f19 5565
3e5937d7 5566 free (graph->rep);
57250223 5567 free (graph->succs);
7b765bed
DB
5568 free (graph->pe);
5569 free (graph->pe_rep);
3e5937d7 5570 free (graph->indirect_cycles);
b5efa470
DB
5571 free (graph);
5572
5573 VEC_free (varinfo_t, heap, varmap);
910fdc79 5574 free_alloc_pool (variable_info_pool);
c58936b6 5575 free_alloc_pool (constraint_pool);
910fdc79 5576}
973162ec 5577
5006671f
RG
5578
5579/* Compute points-to information for every SSA_NAME pointer in the
5580 current function and compute the transitive closure of escaped
5581 variables to re-initialize the call-clobber states of local variables. */
5582
5583unsigned int
5584compute_may_aliases (void)
5585{
5586 /* For each pointer P_i, determine the sets of variables that P_i may
5587 point-to. Compute the reachability set of escaped and call-used
5588 variables. */
5589 compute_points_to_sets ();
5590
5591 /* Debugging dumps. */
5592 if (dump_file)
5593 {
5594 dump_alias_info (dump_file);
5595
5596 if (dump_flags & TDF_DETAILS)
5597 dump_referenced_vars (dump_file);
5598 }
5599
5600 /* Deallocate memory used by aliasing data structures and the internal
5601 points-to solution. */
5602 delete_points_to_sets ();
5603
5604 gcc_assert (!need_ssa_update_p (cfun));
5605
5606 return 0;
5607}
5608
248fc9f3
RG
5609static bool
5610gate_tree_pta (void)
5611{
5612 return flag_tree_pta;
5613}
5006671f
RG
5614
5615/* A dummy pass to cause points-to information to be computed via
5616 TODO_rebuild_alias. */
5617
5618struct gimple_opt_pass pass_build_alias =
5619{
5620 {
5621 GIMPLE_PASS,
5622 "alias", /* name */
248fc9f3 5623 gate_tree_pta, /* gate */
5006671f
RG
5624 NULL, /* execute */
5625 NULL, /* sub */
5626 NULL, /* next */
5627 0, /* static_pass_number */
7072a650 5628 TV_NONE, /* tv_id */
5006671f 5629 PROP_cfg | PROP_ssa, /* properties_required */
4effdf02 5630 0, /* properties_provided */
5006671f
RG
5631 0, /* properties_destroyed */
5632 0, /* todo_flags_start */
5633 TODO_rebuild_alias | TODO_dump_func /* todo_flags_finish */
5634 }
5635};
5636
6b8ed145
RG
5637/* A dummy pass to cause points-to information to be computed via
5638 TODO_rebuild_alias. */
5639
5640struct gimple_opt_pass pass_build_ealias =
5641{
5642 {
5643 GIMPLE_PASS,
5644 "ealias", /* name */
5645 gate_tree_pta, /* gate */
5646 NULL, /* execute */
5647 NULL, /* sub */
5648 NULL, /* next */
5649 0, /* static_pass_number */
5650 TV_NONE, /* tv_id */
5651 PROP_cfg | PROP_ssa, /* properties_required */
5652 0, /* properties_provided */
5653 0, /* properties_destroyed */
5654 0, /* todo_flags_start */
5655 TODO_rebuild_alias | TODO_dump_func /* todo_flags_finish */
5656 }
5657};
5658
5006671f 5659
4ee00913
DB
5660/* Return true if we should execute IPA PTA. */
5661static bool
5662gate_ipa_pta (void)
5663{
de925a03
RG
5664 return (optimize
5665 && flag_ipa_pta
4ee00913
DB
5666 /* Don't bother doing anything if the program has errors. */
5667 && !(errorcount || sorrycount));
5668}
5669
5670/* Execute the driver for IPA PTA. */
c2924966 5671static unsigned int
4ee00913
DB
5672ipa_pta_execute (void)
5673{
5674 struct cgraph_node *node;
3e5937d7 5675
4ee00913 5676 in_ipa_mode = 1;
5c245b95 5677
4cf4d6a3 5678 init_alias_heapvars ();
4ee00913 5679 init_alias_vars ();
c58936b6 5680
5c245b95 5681 /* Build the constraints. */
4ee00913
DB
5682 for (node = cgraph_nodes; node; node = node->next)
5683 {
1de812a6 5684 unsigned int varid;
c58936b6 5685
5c245b95
RG
5686 /* Nodes without a body are not interesting. Especially do not
5687 visit clones at this point for now - we get duplicate decls
5688 there for inline clones at least. */
5689 if (!gimple_has_body_p (node->decl)
5690 || node->clone_of)
5691 continue;
5692
5693 /* It does not make sense to have graph edges into or out of
5694 externally visible functions. There is no extra information
5695 we can gather from them. */
5696 if (node->local.externally_visible)
5697 continue;
5698
1de812a6
JH
5699 varid = create_function_info_for (node->decl,
5700 cgraph_node_name (node));
4ee00913 5701 }
5c245b95 5702
4ee00913
DB
5703 for (node = cgraph_nodes; node; node = node->next)
5704 {
5c245b95
RG
5705 struct function *func;
5706 basic_block bb;
5707 tree old_func_decl;
4ee00913 5708
5c245b95
RG
5709 /* Nodes without a body are not interesting. */
5710 if (!gimple_has_body_p (node->decl)
5711 || node->clone_of)
5712 continue;
c58936b6 5713
5c245b95
RG
5714 if (dump_file)
5715 fprintf (dump_file,
5716 "Generating constraints for %s\n",
5717 cgraph_node_name (node));
c58936b6 5718
5c245b95
RG
5719 func = DECL_STRUCT_FUNCTION (node->decl);
5720 old_func_decl = current_function_decl;
5721 push_cfun (func);
5722 current_function_decl = node->decl;
726a989a 5723
5c245b95
RG
5724 /* For externally visible functions use local constraints for
5725 their arguments. For local functions we see all callers
5726 and thus do not need initial constraints for parameters. */
5727 if (node->local.externally_visible)
5728 intra_create_variable_infos ();
4ee00913 5729
5c245b95
RG
5730 /* Build constriants for the function body. */
5731 FOR_EACH_BB_FN (bb, func)
5732 {
5733 gimple_stmt_iterator gsi;
c58936b6 5734
5c245b95
RG
5735 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
5736 gsi_next (&gsi))
5737 {
5738 gimple phi = gsi_stmt (gsi);
c58936b6 5739
5c245b95
RG
5740 if (is_gimple_reg (gimple_phi_result (phi)))
5741 find_func_aliases (phi);
5742 }
3e5937d7 5743
5c245b95
RG
5744 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5745 {
5746 gimple stmt = gsi_stmt (gsi);
3e5937d7 5747
5c245b95
RG
5748 find_func_aliases (stmt);
5749 }
5750 }
c58936b6 5751
5c245b95
RG
5752 current_function_decl = old_func_decl;
5753 pop_cfun ();
5754 }
c58936b6 5755
5c245b95
RG
5756 /* From the constraints compute the points-to sets. */
5757 solve_constraints ();
c58936b6 5758
5c245b95 5759 delete_points_to_sets ();
c58936b6 5760
4ee00913 5761 in_ipa_mode = 0;
5c245b95 5762
c2924966 5763 return 0;
4ee00913 5764}
c58936b6 5765
8ddbbcae 5766struct simple_ipa_opt_pass pass_ipa_pta =
4ee00913 5767{
8ddbbcae
JH
5768 {
5769 SIMPLE_IPA_PASS,
4ee00913
DB
5770 "pta", /* name */
5771 gate_ipa_pta, /* gate */
5772 ipa_pta_execute, /* execute */
5773 NULL, /* sub */
5774 NULL, /* next */
5775 0, /* static_pass_number */
5776 TV_IPA_PTA, /* tv_id */
5777 0, /* properties_required */
5778 0, /* properties_provided */
5779 0, /* properties_destroyed */
5780 0, /* todo_flags_start */
8ddbbcae
JH
5781 TODO_update_ssa /* todo_flags_finish */
5782 }
4ee00913
DB
5783};
5784
c900f6aa 5785
c900f6aa 5786#include "gt-tree-ssa-structalias.h"