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