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