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