]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-structalias.c
Daily bump.
[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 2829 char *temp;
910fdc79 2830
4f6c9110 2831 if (!dump_file)
70b5e7dc 2832 return "NULL";
4f6c9110 2833
910fdc79
DB
2834 if (TREE_CODE (decl) == SSA_NAME)
2835 {
70b5e7dc
RG
2836 res = get_name (decl);
2837 if (res)
d172f538 2838 temp = xasprintf ("%s_%u", res, SSA_NAME_VERSION (decl));
70b5e7dc 2839 else
d172f538
GJL
2840 temp = xasprintf ("_%u", SSA_NAME_VERSION (decl));
2841 res = ggc_strdup (temp);
2842 free (temp);
910fdc79
DB
2843 }
2844 else if (DECL_P (decl))
2845 {
70b5e7dc
RG
2846 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2847 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2848 else
2849 {
2850 res = get_name (decl);
2851 if (!res)
2852 {
d172f538
GJL
2853 temp = xasprintf ("D.%u", DECL_UID (decl));
2854 res = ggc_strdup (temp);
2855 free (temp);
70b5e7dc
RG
2856 }
2857 }
910fdc79 2858 }
70b5e7dc
RG
2859 if (res != NULL)
2860 return res;
2861
2862 return "NULL";
910fdc79
DB
2863}
2864
15814ba0
PB
2865/* Find the variable id for tree T in the map.
2866 If T doesn't exist in the map, create an entry for it and return it. */
910fdc79 2867
3e5937d7
DB
2868static varinfo_t
2869get_vi_for_tree (tree t)
910fdc79 2870{
b787e7a2 2871 varinfo_t *slot = vi_for_tree->get (t);
15814ba0 2872 if (slot == NULL)
3781ab4b
TV
2873 {
2874 unsigned int id = create_variable_info_for (t, alias_get_name (t), false);
2875 return get_varinfo (id);
2876 }
c58936b6 2877
b787e7a2 2878 return *slot;
910fdc79
DB
2879}
2880
b14e9388 2881/* Get a scalar constraint expression for a new temporary variable. */
910fdc79
DB
2882
2883static struct constraint_expr
3781ab4b 2884new_scalar_tmp_constraint_exp (const char *name, bool add_id)
910fdc79 2885{
b14e9388 2886 struct constraint_expr tmp;
b14e9388 2887 varinfo_t vi;
910fdc79 2888
3781ab4b 2889 vi = new_var_info (NULL_TREE, name, add_id);
b14e9388
RG
2890 vi->offset = 0;
2891 vi->size = -1;
2892 vi->fullsize = -1;
2893 vi->is_full_var = 1;
c0d459f0 2894
b14e9388
RG
2895 tmp.var = vi->id;
2896 tmp.type = SCALAR;
2897 tmp.offset = 0;
c0d459f0 2898
b14e9388 2899 return tmp;
c0d459f0
RG
2900}
2901
2902/* Get a constraint expression vector from an SSA_VAR_P node.
2903 If address_p is true, the result will be taken its address of. */
2904
2905static void
9771b263 2906get_constraint_for_ssa_var (tree t, vec<ce_s> *results, bool address_p)
c0d459f0
RG
2907{
2908 struct constraint_expr cexpr;
2909 varinfo_t vi;
2910
2911 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
b2ec94d4 2912 gcc_assert (TREE_CODE (t) == SSA_NAME || DECL_P (t));
910fdc79
DB
2913
2914 /* For parameters, get at the points-to set for the actual parm
2915 decl. */
c58936b6 2916 if (TREE_CODE (t) == SSA_NAME
b2ec94d4 2917 && SSA_NAME_IS_DEFAULT_DEF (t)
6938f93f 2918 && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
b2ec94d4 2919 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL))
c0d459f0
RG
2920 {
2921 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2922 return;
2923 }
910fdc79 2924
9c7c9f10 2925 /* For global variables resort to the alias target. */
8813a647 2926 if (VAR_P (t) && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
9c7c9f10 2927 {
9041d2e6 2928 varpool_node *node = varpool_node::get (t);
67348ccc 2929 if (node && node->alias && node->analyzed)
9c7c9f10 2930 {
9041d2e6 2931 node = node->ultimate_alias_target ();
5aed7e2c
RB
2932 /* Canonicalize the PT uid of all aliases to the ultimate target.
2933 ??? Hopefully the set of aliases can't change in a way that
2934 changes the ultimate alias target. */
2935 gcc_assert ((! DECL_PT_UID_SET_P (node->decl)
2936 || DECL_PT_UID (node->decl) == DECL_UID (node->decl))
2937 && (! DECL_PT_UID_SET_P (t)
2938 || DECL_PT_UID (t) == DECL_UID (node->decl)));
2939 DECL_PT_UID (t) = DECL_UID (node->decl);
67348ccc 2940 t = node->decl;
9c7c9f10 2941 }
40b8428e
RB
2942
2943 /* If this is decl may bind to NULL note that. */
2944 if (address_p
2945 && (! node || ! node->nonzero_address ()))
2946 {
2947 cexpr.var = nothing_id;
2948 cexpr.type = SCALAR;
2949 cexpr.offset = 0;
2950 results->safe_push (cexpr);
2951 }
9c7c9f10
RG
2952 }
2953
c0d459f0
RG
2954 vi = get_vi_for_tree (t);
2955 cexpr.var = vi->id;
910fdc79 2956 cexpr.type = SCALAR;
c0d459f0 2957 cexpr.offset = 0;
c58936b6 2958
c0d459f0
RG
2959 /* If we are not taking the address of the constraint expr, add all
2960 sub-fiels of the variable as well. */
de925a03
RG
2961 if (!address_p
2962 && !vi->is_full_var)
c0d459f0 2963 {
d6d305fe 2964 for (; vi; vi = vi_next (vi))
c0d459f0
RG
2965 {
2966 cexpr.var = vi->id;
9771b263 2967 results->safe_push (cexpr);
c0d459f0
RG
2968 }
2969 return;
2970 }
2971
9771b263 2972 results->safe_push (cexpr);
910fdc79
DB
2973}
2974
faf2ecc5
RG
2975/* Process constraint T, performing various simplifications and then
2976 adding it to our list of overall constraints. */
910fdc79
DB
2977
2978static void
faf2ecc5 2979process_constraint (constraint_t t)
910fdc79
DB
2980{
2981 struct constraint_expr rhs = t->rhs;
2982 struct constraint_expr lhs = t->lhs;
c58936b6 2983
9771b263
DN
2984 gcc_assert (rhs.var < varmap.length ());
2985 gcc_assert (lhs.var < varmap.length ());
910fdc79 2986
5006671f
RG
2987 /* If we didn't get any useful constraint from the lhs we get
2988 &ANYTHING as fallback from get_constraint_for. Deal with
2989 it here by turning it into *ANYTHING. */
2990 if (lhs.type == ADDRESSOF
2991 && lhs.var == anything_id)
2992 lhs.type = DEREF;
2993
2994 /* ADDRESSOF on the lhs is invalid. */
2995 gcc_assert (lhs.type != ADDRESSOF);
910fdc79 2996
3c323b52
RG
2997 /* We shouldn't add constraints from things that cannot have pointers.
2998 It's not completely trivial to avoid in the callers, so do it here. */
2999 if (rhs.type != ADDRESSOF
3000 && !get_varinfo (rhs.var)->may_have_pointers)
3001 return;
3002
3003 /* Likewise adding to the solution of a non-pointer var isn't useful. */
3004 if (!get_varinfo (lhs.var)->may_have_pointers)
3005 return;
3006
910fdc79 3007 /* This can happen in our IR with things like n->a = *p */
5006671f 3008 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
910fdc79
DB
3009 {
3010 /* Split into tmp = *rhs, *lhs = tmp */
b14e9388 3011 struct constraint_expr tmplhs;
3781ab4b 3012 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp", true);
faf2ecc5
RG
3013 process_constraint (new_constraint (tmplhs, rhs));
3014 process_constraint (new_constraint (lhs, tmplhs));
7b765bed 3015 }
6811ea79 3016 else if ((rhs.type != SCALAR || rhs.offset != 0) && lhs.type == DEREF)
7b765bed
DB
3017 {
3018 /* Split into tmp = &rhs, *lhs = tmp */
b14e9388 3019 struct constraint_expr tmplhs;
3781ab4b 3020 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp", true);
faf2ecc5
RG
3021 process_constraint (new_constraint (tmplhs, rhs));
3022 process_constraint (new_constraint (lhs, tmplhs));
910fdc79 3023 }
910fdc79
DB
3024 else
3025 {
3e5937d7 3026 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
9771b263 3027 constraints.safe_push (t);
910fdc79
DB
3028 }
3029}
3030
3031
3032/* Return the position, in bits, of FIELD_DECL from the beginning of its
3033 structure. */
3034
ee7d4b57 3035static HOST_WIDE_INT
910fdc79
DB
3036bitpos_of_field (const tree fdecl)
3037{
9541ffee
RS
3038 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl))
3039 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl)))
910fdc79 3040 return -1;
c58936b6 3041
eb1ce453
KZ
3042 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
3043 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl)));
910fdc79
DB
3044}
3045
3046
e5bae89b
RG
3047/* Get constraint expressions for offsetting PTR by OFFSET. Stores the
3048 resulting constraint expressions in *RESULTS. */
3049
3050static void
3051get_constraint_for_ptr_offset (tree ptr, tree offset,
9771b263 3052 vec<ce_s> *results)
e5bae89b 3053{
bd02b3a0 3054 struct constraint_expr c;
e5bae89b 3055 unsigned int j, n;
97919ae7 3056 HOST_WIDE_INT rhsoffset;
e5bae89b
RG
3057
3058 /* If we do not do field-sensitive PTA adding offsets to pointers
3059 does not change the points-to solution. */
3060 if (!use_field_sensitive)
3061 {
ed6c4831 3062 get_constraint_for_rhs (ptr, results);
e5bae89b
RG
3063 return;
3064 }
3065
3066 /* If the offset is not a non-negative integer constant that fits
3067 in a HOST_WIDE_INT, we have to fall back to a conservative
3068 solution which includes all sub-fields of all pointed-to
5006671f 3069 variables of ptr. */
779704e7 3070 if (offset == NULL_TREE
97919ae7 3071 || TREE_CODE (offset) != INTEGER_CST)
5006671f
RG
3072 rhsoffset = UNKNOWN_OFFSET;
3073 else
e5bae89b 3074 {
97919ae7 3075 /* Sign-extend the offset. */
807e902e
KZ
3076 offset_int soffset = offset_int::from (offset, SIGNED);
3077 if (!wi::fits_shwi_p (soffset))
5006671f 3078 rhsoffset = UNKNOWN_OFFSET;
97919ae7
RG
3079 else
3080 {
3081 /* Make sure the bit-offset also fits. */
807e902e 3082 HOST_WIDE_INT rhsunitoffset = soffset.to_shwi ();
c9f8853f 3083 rhsoffset = rhsunitoffset * (unsigned HOST_WIDE_INT) BITS_PER_UNIT;
97919ae7
RG
3084 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
3085 rhsoffset = UNKNOWN_OFFSET;
3086 }
e5bae89b
RG
3087 }
3088
ed6c4831 3089 get_constraint_for_rhs (ptr, results);
e5bae89b
RG
3090 if (rhsoffset == 0)
3091 return;
3092
3093 /* As we are eventually appending to the solution do not use
9771b263
DN
3094 vec::iterate here. */
3095 n = results->length ();
e5bae89b
RG
3096 for (j = 0; j < n; j++)
3097 {
3098 varinfo_t curr;
9771b263 3099 c = (*results)[j];
bd02b3a0 3100 curr = get_varinfo (c.var);
e5bae89b 3101
bd02b3a0 3102 if (c.type == ADDRESSOF
5006671f
RG
3103 /* If this varinfo represents a full variable just use it. */
3104 && curr->is_full_var)
dfd7d2d6 3105 ;
bd02b3a0 3106 else if (c.type == ADDRESSOF
5006671f
RG
3107 /* If we do not know the offset add all subfields. */
3108 && rhsoffset == UNKNOWN_OFFSET)
3109 {
d6d305fe 3110 varinfo_t temp = get_varinfo (curr->head);
5006671f
RG
3111 do
3112 {
3113 struct constraint_expr c2;
3114 c2.var = temp->id;
3115 c2.type = ADDRESSOF;
3116 c2.offset = 0;
bd02b3a0 3117 if (c2.var != c.var)
9771b263 3118 results->safe_push (c2);
d6d305fe 3119 temp = vi_next (temp);
5006671f
RG
3120 }
3121 while (temp);
3122 }
bd02b3a0 3123 else if (c.type == ADDRESSOF)
e5bae89b 3124 {
5006671f
RG
3125 varinfo_t temp;
3126 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
e5bae89b 3127
af1ab449 3128 /* If curr->offset + rhsoffset is less than zero adjust it. */
5006671f
RG
3129 if (rhsoffset < 0
3130 && curr->offset < offset)
3131 offset = 0;
e5bae89b 3132
af1ab449
RB
3133 /* We have to include all fields that overlap the current
3134 field shifted by rhsoffset. And we include at least
3135 the last or the first field of the variable to represent
3136 reachability of off-bound addresses, in particular &object + 1,
3137 conservatively correct. */
3138 temp = first_or_preceding_vi_for_offset (curr, offset);
3139 c.var = temp->id;
3140 c.offset = 0;
3141 temp = vi_next (temp);
3142 while (temp
3143 && temp->offset < offset + curr->size)
e5bae89b
RG
3144 {
3145 struct constraint_expr c2;
af1ab449 3146 c2.var = temp->id;
e5bae89b
RG
3147 c2.type = ADDRESSOF;
3148 c2.offset = 0;
9771b263 3149 results->safe_push (c2);
af1ab449 3150 temp = vi_next (temp);
e5bae89b 3151 }
e5bae89b 3152 }
dfd7d2d6
RB
3153 else if (c.type == SCALAR)
3154 {
3155 gcc_assert (c.offset == 0);
3156 c.offset = rhsoffset;
3157 }
e5bae89b 3158 else
dfd7d2d6
RB
3159 /* We shouldn't get any DEREFs here. */
3160 gcc_unreachable ();
bd02b3a0 3161
9771b263 3162 (*results)[j] = c;
e5bae89b
RG
3163 }
3164}
3165
3166
c0d459f0 3167/* Given a COMPONENT_REF T, return the constraint_expr vector for it.
ed6c4831
RG
3168 If address_p is true the result will be taken its address of.
3169 If lhs_p is true then the constraint expression is assumed to be used
3170 as the lhs. */
910fdc79 3171
4ee00913 3172static void
9771b263 3173get_constraint_for_component_ref (tree t, vec<ce_s> *results,
ed6c4831 3174 bool address_p, bool lhs_p)
910fdc79 3175{
4ee00913 3176 tree orig_t = t;
b1347638 3177 HOST_WIDE_INT bitsize = -1;
6bec9271 3178 HOST_WIDE_INT bitmaxsize = -1;
910fdc79 3179 HOST_WIDE_INT bitpos;
ee45a32d 3180 bool reverse;
910fdc79 3181 tree forzero;
910fdc79
DB
3182
3183 /* Some people like to do cute things like take the address of
3184 &0->a.b */
3185 forzero = t;
2ea9dc64 3186 while (handled_component_p (forzero)
70f34814
RG
3187 || INDIRECT_REF_P (forzero)
3188 || TREE_CODE (forzero) == MEM_REF)
4ee00913 3189 forzero = TREE_OPERAND (forzero, 0);
910fdc79 3190
c58936b6 3191 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
910fdc79 3192 {
4ee00913 3193 struct constraint_expr temp;
c58936b6 3194
4ee00913
DB
3195 temp.offset = 0;
3196 temp.var = integer_id;
3197 temp.type = SCALAR;
9771b263 3198 results->safe_push (temp);
4ee00913 3199 return;
910fdc79 3200 }
c58936b6 3201
ee45a32d 3202 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize, &reverse);
21392f19 3203
9a4cb973
RB
3204 /* We can end up here for component references on a
3205 VIEW_CONVERT_EXPR <>(&foobar) or things like a
3206 BIT_FIELD_REF <&MEM[(void *)&b + 4B], ...>. So for
3207 symbolic constants simply give up. */
3208 if (TREE_CODE (t) == ADDR_EXPR)
3209 {
3210 constraint_expr result;
3211 result.type = SCALAR;
3212 result.var = anything_id;
3213 result.offset = 0;
3214 results->safe_push (result);
3215 return;
3216 }
3217
c0d459f0
RG
3218 /* Pretend to take the address of the base, we'll take care of
3219 adding the required subset of sub-fields below. */
ed6c4831 3220 get_constraint_for_1 (t, results, true, lhs_p);
40b8428e
RB
3221 /* Strip off nothing_id. */
3222 if (results->length () == 2)
3223 {
3224 gcc_assert ((*results)[0].var == nothing_id);
3225 results->unordered_remove (0);
3226 }
9771b263
DN
3227 gcc_assert (results->length () == 1);
3228 struct constraint_expr &result = results->last ();
910fdc79 3229
9771b263
DN
3230 if (result.type == SCALAR
3231 && get_varinfo (result.var)->is_full_var)
e5bae89b 3232 /* For single-field vars do not bother about the offset. */
9771b263
DN
3233 result.offset = 0;
3234 else if (result.type == SCALAR)
910fdc79
DB
3235 {
3236 /* In languages like C, you can access one past the end of an
3237 array. You aren't allowed to dereference it, so we can
3238 ignore this constraint. When we handle pointer subtraction,
3239 we may have to do something cute here. */
c58936b6 3240
9771b263 3241 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result.var)->fullsize
18455d17 3242 && bitmaxsize != 0)
dd68d988
DB
3243 {
3244 /* It's also not true that the constraint will actually start at the
3245 right offset, it may start in some padding. We only care about
3246 setting the constraint to the first actual field it touches, so
c58936b6 3247 walk to find it. */
9771b263 3248 struct constraint_expr cexpr = result;
dd68d988 3249 varinfo_t curr;
9771b263 3250 results->pop ();
c0d459f0 3251 cexpr.offset = 0;
d6d305fe 3252 for (curr = get_varinfo (cexpr.var); curr; curr = vi_next (curr))
dd68d988 3253 {
63d195d5 3254 if (ranges_overlap_p (curr->offset, curr->size,
c0d459f0 3255 bitpos, bitmaxsize))
dd68d988 3256 {
c0d459f0 3257 cexpr.var = curr->id;
9771b263 3258 results->safe_push (cexpr);
c0d459f0
RG
3259 if (address_p)
3260 break;
dd68d988
DB
3261 }
3262 }
e5bae89b
RG
3263 /* If we are going to take the address of this field then
3264 to be able to compute reachability correctly add at least
3265 the last field of the variable. */
9771b263 3266 if (address_p && results->length () == 0)
e5bae89b
RG
3267 {
3268 curr = get_varinfo (cexpr.var);
d6d305fe
RB
3269 while (curr->next != 0)
3270 curr = vi_next (curr);
e5bae89b 3271 cexpr.var = curr->id;
9771b263 3272 results->safe_push (cexpr);
e5bae89b 3273 }
9771b263 3274 else if (results->length () == 0)
e5bae89b
RG
3275 /* Assert that we found *some* field there. The user couldn't be
3276 accessing *only* padding. */
3277 /* Still the user could access one past the end of an array
3278 embedded in a struct resulting in accessing *only* padding. */
0ba0772b
RB
3279 /* Or accessing only padding via type-punning to a type
3280 that has a filed just in padding space. */
3281 {
3282 cexpr.type = SCALAR;
3283 cexpr.var = anything_id;
3284 cexpr.offset = 0;
9771b263 3285 results->safe_push (cexpr);
0ba0772b 3286 }
dd68d988 3287 }
18455d17
RG
3288 else if (bitmaxsize == 0)
3289 {
3290 if (dump_file && (dump_flags & TDF_DETAILS))
aa326bfb 3291 fprintf (dump_file, "Access to zero-sized part of variable, "
18455d17
RG
3292 "ignoring\n");
3293 }
910fdc79
DB
3294 else
3295 if (dump_file && (dump_flags & TDF_DETAILS))
3296 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
910fdc79 3297 }
9771b263 3298 else if (result.type == DEREF)
7b765bed 3299 {
5006671f
RG
3300 /* If we do not know exactly where the access goes say so. Note
3301 that only for non-structure accesses we know that we access
3302 at most one subfiled of any variable. */
3303 if (bitpos == -1
3304 || bitsize != bitmaxsize
1c09321c 3305 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
9771b263
DN
3306 || result.offset == UNKNOWN_OFFSET)
3307 result.offset = UNKNOWN_OFFSET;
5006671f 3308 else
9771b263 3309 result.offset += bitpos;
7b765bed 3310 }
9771b263 3311 else if (result.type == ADDRESSOF)
b51605c4 3312 {
9a4cb973
RB
3313 /* We can end up here for component references on constants like
3314 VIEW_CONVERT_EXPR <>({ 0, 1, 2, 3 })[i]. */
9771b263
DN
3315 result.type = SCALAR;
3316 result.var = anything_id;
3317 result.offset = 0;
b51605c4 3318 }
c0d459f0 3319 else
5006671f 3320 gcc_unreachable ();
910fdc79
DB
3321}
3322
3323
3324/* Dereference the constraint expression CONS, and return the result.
3325 DEREF (ADDRESSOF) = SCALAR
3326 DEREF (SCALAR) = DEREF
3327 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3328 This is needed so that we can handle dereferencing DEREF constraints. */
3329
4ee00913 3330static void
9771b263 3331do_deref (vec<ce_s> *constraints)
910fdc79 3332{
4ee00913
DB
3333 struct constraint_expr *c;
3334 unsigned int i = 0;
c58936b6 3335
9771b263 3336 FOR_EACH_VEC_ELT (*constraints, i, c)
910fdc79 3337 {
4ee00913
DB
3338 if (c->type == SCALAR)
3339 c->type = DEREF;
3340 else if (c->type == ADDRESSOF)
3341 c->type = SCALAR;
3342 else if (c->type == DEREF)
3343 {
b14e9388 3344 struct constraint_expr tmplhs;
3781ab4b 3345 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp", true);
4ee00913
DB
3346 process_constraint (new_constraint (tmplhs, *c));
3347 c->var = tmplhs.var;
3348 }
3349 else
3350 gcc_unreachable ();
910fdc79 3351 }
910fdc79
DB
3352}
3353
1d24fdd9
RG
3354/* Given a tree T, return the constraint expression for taking the
3355 address of it. */
3356
3357static void
9771b263 3358get_constraint_for_address_of (tree t, vec<ce_s> *results)
1d24fdd9
RG
3359{
3360 struct constraint_expr *c;
3361 unsigned int i;
3362
ed6c4831 3363 get_constraint_for_1 (t, results, true, true);
1d24fdd9 3364
9771b263 3365 FOR_EACH_VEC_ELT (*results, i, c)
1d24fdd9
RG
3366 {
3367 if (c->type == DEREF)
3368 c->type = SCALAR;
3369 else
3370 c->type = ADDRESSOF;
3371 }
3372}
3373
910fdc79
DB
3374/* Given a tree T, return the constraint expression for it. */
3375
4ee00913 3376static void
9771b263 3377get_constraint_for_1 (tree t, vec<ce_s> *results, bool address_p,
ed6c4831 3378 bool lhs_p)
910fdc79
DB
3379{
3380 struct constraint_expr temp;
3381
3382 /* x = integer is all glommed to a single variable, which doesn't
3383 point to anything by itself. That is, of course, unless it is an
3384 integer constant being treated as a pointer, in which case, we
3385 will return that this is really the addressof anything. This
3386 happens below, since it will fall into the default case. The only
3387 case we know something about an integer treated like a pointer is
3388 when it is the NULL pointer, and then we just say it points to
89ebafc6
PB
3389 NULL.
3390
3391 Do not do that if -fno-delete-null-pointer-checks though, because
3392 in that case *NULL does not fail, so it _should_ alias *anything.
3393 It is not worth adding a new option or renaming the existing one,
3394 since this case is relatively obscure. */
8eb7bc3c
RG
3395 if ((TREE_CODE (t) == INTEGER_CST
3396 && integer_zerop (t))
3397 /* The only valid CONSTRUCTORs in gimple with pointer typed
3398 elements are zero-initializer. But in IPA mode we also
3399 process global initializers, so verify at least. */
3400 || (TREE_CODE (t) == CONSTRUCTOR
3401 && CONSTRUCTOR_NELTS (t) == 0))
3402 {
3403 if (flag_delete_null_pointer_checks)
3404 temp.var = nothing_id;
3405 else
1f181fde 3406 temp.var = nonlocal_id;
910fdc79
DB
3407 temp.type = ADDRESSOF;
3408 temp.offset = 0;
9771b263 3409 results->safe_push (temp);
4ee00913 3410 return;
910fdc79
DB
3411 }
3412
ebd7d910
RB
3413 /* String constants are read-only, ideally we'd have a CONST_DECL
3414 for those. */
bd1f29d9
EB
3415 if (TREE_CODE (t) == STRING_CST)
3416 {
ebd7d910 3417 temp.var = string_id;
bd1f29d9
EB
3418 temp.type = SCALAR;
3419 temp.offset = 0;
9771b263 3420 results->safe_push (temp);
bd1f29d9
EB
3421 return;
3422 }
3423
910fdc79
DB
3424 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3425 {
3426 case tcc_expression:
3427 {
3428 switch (TREE_CODE (t))
3429 {
3430 case ADDR_EXPR:
1d24fdd9
RG
3431 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3432 return;
e5bae89b 3433 default:;
910fdc79 3434 }
e5bae89b 3435 break;
910fdc79
DB
3436 }
3437 case tcc_reference:
3438 {
3439 switch (TREE_CODE (t))
3440 {
70f34814 3441 case MEM_REF:
910fdc79 3442 {
de2184c0 3443 struct constraint_expr cs;
343b2efc 3444 varinfo_t vi, curr;
97919ae7
RG
3445 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0),
3446 TREE_OPERAND (t, 1), results);
4ee00913 3447 do_deref (results);
343b2efc
RG
3448
3449 /* If we are not taking the address then make sure to process
3450 all subvariables we might access. */
1a5d20a4
RG
3451 if (address_p)
3452 return;
3453
9771b263 3454 cs = results->last ();
b4cf8c9d
RG
3455 if (cs.type == DEREF
3456 && type_can_have_subvars (TREE_TYPE (t)))
1a5d20a4
RG
3457 {
3458 /* For dereferences this means we have to defer it
3459 to solving time. */
9771b263 3460 results->last ().offset = UNKNOWN_OFFSET;
1a5d20a4
RG
3461 return;
3462 }
3463 if (cs.type != SCALAR)
343b2efc
RG
3464 return;
3465
de2184c0 3466 vi = get_varinfo (cs.var);
d6d305fe 3467 curr = vi_next (vi);
343b2efc
RG
3468 if (!vi->is_full_var
3469 && curr)
3470 {
3471 unsigned HOST_WIDE_INT size;
cc269bb6 3472 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t))))
eb1ce453 3473 size = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t)));
343b2efc
RG
3474 else
3475 size = -1;
d6d305fe 3476 for (; curr; curr = vi_next (curr))
343b2efc
RG
3477 {
3478 if (curr->offset - vi->offset < size)
3479 {
343b2efc 3480 cs.var = curr->id;
9771b263 3481 results->safe_push (cs);
343b2efc
RG
3482 }
3483 else
3484 break;
3485 }
3486 }
4ee00913 3487 return;
910fdc79
DB
3488 }
3489 case ARRAY_REF:
32961db5 3490 case ARRAY_RANGE_REF:
910fdc79 3491 case COMPONENT_REF:
7b909872
RB
3492 case IMAGPART_EXPR:
3493 case REALPART_EXPR:
3494 case BIT_FIELD_REF:
ed6c4831 3495 get_constraint_for_component_ref (t, results, address_p, lhs_p);
4ee00913 3496 return;
5006671f 3497 case VIEW_CONVERT_EXPR:
ed6c4831
RG
3498 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p,
3499 lhs_p);
5006671f
RG
3500 return;
3501 /* We are missing handling for TARGET_MEM_REF here. */
e5bae89b 3502 default:;
910fdc79 3503 }
e5bae89b 3504 break;
910fdc79 3505 }
910fdc79
DB
3506 case tcc_exceptional:
3507 {
3508 switch (TREE_CODE (t))
3509 {
910fdc79 3510 case SSA_NAME:
4ee00913 3511 {
c0d459f0 3512 get_constraint_for_ssa_var (t, results, address_p);
4ee00913
DB
3513 return;
3514 }
47d8a903
RG
3515 case CONSTRUCTOR:
3516 {
3517 unsigned int i;
3518 tree val;
ef062b13 3519 auto_vec<ce_s> tmp;
47d8a903
RG
3520 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
3521 {
3522 struct constraint_expr *rhsp;
3523 unsigned j;
ed6c4831 3524 get_constraint_for_1 (val, &tmp, address_p, lhs_p);
9771b263
DN
3525 FOR_EACH_VEC_ELT (tmp, j, rhsp)
3526 results->safe_push (*rhsp);
3527 tmp.truncate (0);
47d8a903 3528 }
47d8a903
RG
3529 /* We do not know whether the constructor was complete,
3530 so technically we have to add &NOTHING or &ANYTHING
3531 like we do for an empty constructor as well. */
3532 return;
3533 }
e5bae89b 3534 default:;
910fdc79 3535 }
e5bae89b 3536 break;
910fdc79
DB
3537 }
3538 case tcc_declaration:
4ee00913 3539 {
c0d459f0 3540 get_constraint_for_ssa_var (t, results, address_p);
4ee00913
DB
3541 return;
3542 }
1f181fde
RG
3543 case tcc_constant:
3544 {
3545 /* We cannot refer to automatic variables through constants. */
3546 temp.type = ADDRESSOF;
3547 temp.var = nonlocal_id;
3548 temp.offset = 0;
9771b263 3549 results->safe_push (temp);
1f181fde
RG
3550 return;
3551 }
e5bae89b 3552 default:;
910fdc79 3553 }
e5bae89b
RG
3554
3555 /* The default fallback is a constraint from anything. */
3556 temp.type = ADDRESSOF;
3557 temp.var = anything_id;
3558 temp.offset = 0;
9771b263 3559 results->safe_push (temp);
910fdc79
DB
3560}
3561
c0d459f0
RG
3562/* Given a gimple tree T, return the constraint expression vector for it. */
3563
3564static void
9771b263 3565get_constraint_for (tree t, vec<ce_s> *results)
c0d459f0 3566{
9771b263 3567 gcc_assert (results->length () == 0);
c0d459f0 3568
ed6c4831
RG
3569 get_constraint_for_1 (t, results, false, true);
3570}
3571
3572/* Given a gimple tree T, return the constraint expression vector for it
3573 to be used as the rhs of a constraint. */
3574
3575static void
9771b263 3576get_constraint_for_rhs (tree t, vec<ce_s> *results)
ed6c4831 3577{
9771b263 3578 gcc_assert (results->length () == 0);
ed6c4831
RG
3579
3580 get_constraint_for_1 (t, results, false, false);
c0d459f0 3581}
910fdc79 3582
779704e7
RG
3583
3584/* Efficiently generates constraints from all entries in *RHSC to all
3585 entries in *LHSC. */
3586
3587static void
9771b263
DN
3588process_all_all_constraints (vec<ce_s> lhsc,
3589 vec<ce_s> rhsc)
779704e7
RG
3590{
3591 struct constraint_expr *lhsp, *rhsp;
3592 unsigned i, j;
3593
9771b263 3594 if (lhsc.length () <= 1 || rhsc.length () <= 1)
779704e7 3595 {
9771b263
DN
3596 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3597 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
779704e7
RG
3598 process_constraint (new_constraint (*lhsp, *rhsp));
3599 }
3600 else
3601 {
3602 struct constraint_expr tmp;
3781ab4b 3603 tmp = new_scalar_tmp_constraint_exp ("allalltmp", true);
9771b263 3604 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
779704e7 3605 process_constraint (new_constraint (tmp, *rhsp));
9771b263 3606 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
779704e7
RG
3607 process_constraint (new_constraint (*lhsp, tmp));
3608 }
3609}
3610
910fdc79
DB
3611/* Handle aggregate copies by expanding into copies of the respective
3612 fields of the structures. */
3613
3614static void
3615do_structure_copy (tree lhsop, tree rhsop)
3616{
5006671f 3617 struct constraint_expr *lhsp, *rhsp;
ef062b13
TS
3618 auto_vec<ce_s> lhsc;
3619 auto_vec<ce_s> rhsc;
5006671f
RG
3620 unsigned j;
3621
3622 get_constraint_for (lhsop, &lhsc);
ed6c4831 3623 get_constraint_for_rhs (rhsop, &rhsc);
9771b263
DN
3624 lhsp = &lhsc[0];
3625 rhsp = &rhsc[0];
5006671f
RG
3626 if (lhsp->type == DEREF
3627 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3628 || rhsp->type == DEREF)
b28ae58f
RG
3629 {
3630 if (lhsp->type == DEREF)
3631 {
9771b263 3632 gcc_assert (lhsc.length () == 1);
b28ae58f
RG
3633 lhsp->offset = UNKNOWN_OFFSET;
3634 }
3635 if (rhsp->type == DEREF)
3636 {
9771b263 3637 gcc_assert (rhsc.length () == 1);
b28ae58f
RG
3638 rhsp->offset = UNKNOWN_OFFSET;
3639 }
3640 process_all_all_constraints (lhsc, rhsc);
3641 }
5006671f
RG
3642 else if (lhsp->type == SCALAR
3643 && (rhsp->type == SCALAR
3644 || rhsp->type == ADDRESSOF))
910fdc79 3645 {
5006671f
RG
3646 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3647 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
ee45a32d 3648 bool reverse;
5006671f 3649 unsigned k = 0;
ee45a32d
EB
3650 get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize,
3651 &reverse);
3652 get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize,
3653 &reverse);
9771b263 3654 for (j = 0; lhsc.iterate (j, &lhsp);)
910fdc79 3655 {
5006671f 3656 varinfo_t lhsv, rhsv;
9771b263 3657 rhsp = &rhsc[k];
5006671f
RG
3658 lhsv = get_varinfo (lhsp->var);
3659 rhsv = get_varinfo (rhsp->var);
3660 if (lhsv->may_have_pointers
c636a4fb
RG
3661 && (lhsv->is_full_var
3662 || rhsv->is_full_var
3663 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3664 rhsv->offset + lhsoffset, rhsv->size)))
5006671f 3665 process_constraint (new_constraint (*lhsp, *rhsp));
c636a4fb
RG
3666 if (!rhsv->is_full_var
3667 && (lhsv->is_full_var
3668 || (lhsv->offset + rhsoffset + lhsv->size
3669 > rhsv->offset + lhsoffset + rhsv->size)))
5006671f
RG
3670 {
3671 ++k;
9771b263 3672 if (k >= rhsc.length ())
5006671f
RG
3673 break;
3674 }
910fdc79 3675 else
5006671f 3676 ++j;
910fdc79
DB
3677 }
3678 }
3679 else
5006671f 3680 gcc_unreachable ();
910fdc79
DB
3681}
3682
cb89b4b0 3683/* Create constraints ID = { rhsc }. */
b7091901
RG
3684
3685static void
9771b263 3686make_constraints_to (unsigned id, vec<ce_s> rhsc)
b7091901 3687{
b7091901
RG
3688 struct constraint_expr *c;
3689 struct constraint_expr includes;
3690 unsigned int j;
3691
3692 includes.var = id;
3693 includes.offset = 0;
3694 includes.type = SCALAR;
3695
9771b263 3696 FOR_EACH_VEC_ELT (rhsc, j, c)
faf2ecc5 3697 process_constraint (new_constraint (includes, *c));
cb89b4b0
RG
3698}
3699
3700/* Create a constraint ID = OP. */
3701
3702static void
3703make_constraint_to (unsigned id, tree op)
3704{
ef062b13 3705 auto_vec<ce_s> rhsc;
cb89b4b0
RG
3706 get_constraint_for_rhs (op, &rhsc);
3707 make_constraints_to (id, rhsc);
b7091901
RG
3708}
3709
74d27244
RG
3710/* Create a constraint ID = &FROM. */
3711
3712static void
3713make_constraint_from (varinfo_t vi, int from)
3714{
3715 struct constraint_expr lhs, rhs;
3716
3717 lhs.var = vi->id;
3718 lhs.offset = 0;
3719 lhs.type = SCALAR;
3720
3721 rhs.var = from;
3722 rhs.offset = 0;
3723 rhs.type = ADDRESSOF;
3724 process_constraint (new_constraint (lhs, rhs));
3725}
3726
3727/* Create a constraint ID = FROM. */
3728
3729static void
3730make_copy_constraint (varinfo_t vi, int from)
3731{
3732 struct constraint_expr lhs, rhs;
3733
3734 lhs.var = vi->id;
3735 lhs.offset = 0;
3736 lhs.type = SCALAR;
3737
3738 rhs.var = from;
3739 rhs.offset = 0;
3740 rhs.type = SCALAR;
3741 process_constraint (new_constraint (lhs, rhs));
3742}
3743
b7091901
RG
3744/* Make constraints necessary to make OP escape. */
3745
3746static void
3747make_escape_constraint (tree op)
3748{
3749 make_constraint_to (escaped_id, op);
3750}
3751
3e8542ca
RG
3752/* Add constraints to that the solution of VI is transitively closed. */
3753
3754static void
3755make_transitive_closure_constraints (varinfo_t vi)
3756{
3757 struct constraint_expr lhs, rhs;
3758
6811ea79 3759 /* VAR = *(VAR + UNKNOWN); */
3e8542ca
RG
3760 lhs.type = SCALAR;
3761 lhs.var = vi->id;
3762 lhs.offset = 0;
3763 rhs.type = DEREF;
3764 rhs.var = vi->id;
3e8542ca
RG
3765 rhs.offset = UNKNOWN_OFFSET;
3766 process_constraint (new_constraint (lhs, rhs));
3767}
3768
6811ea79
RB
3769/* Add constraints to that the solution of VI has all subvariables added. */
3770
3771static void
3772make_any_offset_constraints (varinfo_t vi)
3773{
3774 struct constraint_expr lhs, rhs;
3775
3776 /* VAR = VAR + UNKNOWN; */
3777 lhs.type = SCALAR;
3778 lhs.var = vi->id;
3779 lhs.offset = 0;
3780 rhs.type = SCALAR;
3781 rhs.var = vi->id;
3782 rhs.offset = UNKNOWN_OFFSET;
3783 process_constraint (new_constraint (lhs, rhs));
3784}
3785
7d6e2521
RG
3786/* Temporary storage for fake var decls. */
3787struct obstack fake_var_decl_obstack;
3788
3789/* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3790
3791static tree
3792build_fake_var_decl (tree type)
3793{
3794 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl);
3795 memset (decl, 0, sizeof (struct tree_var_decl));
3796 TREE_SET_CODE (decl, VAR_DECL);
3797 TREE_TYPE (decl) = type;
3798 DECL_UID (decl) = allocate_decl_uid ();
3799 SET_DECL_PT_UID (decl, -1);
3800 layout_decl (decl, 0);
3801 return decl;
3802}
3803
0b7b376d
RG
3804/* Create a new artificial heap variable with NAME.
3805 Return the created variable. */
74d27244
RG
3806
3807static varinfo_t
3781ab4b 3808make_heapvar (const char *name, bool add_id)
74d27244
RG
3809{
3810 varinfo_t vi;
7d6e2521
RG
3811 tree heapvar;
3812
3813 heapvar = build_fake_var_decl (ptr_type_node);
3814 DECL_EXTERNAL (heapvar) = 1;
74d27244 3815
3781ab4b 3816 vi = new_var_info (heapvar, name, add_id);
74d27244
RG
3817 vi->is_artificial_var = true;
3818 vi->is_heap_var = true;
3819 vi->is_unknown_size_var = true;
b41e33fe 3820 vi->offset = 0;
74d27244
RG
3821 vi->fullsize = ~0;
3822 vi->size = ~0;
3823 vi->is_full_var = true;
3824 insert_vi_for_tree (heapvar, vi);
3825
0b7b376d
RG
3826 return vi;
3827}
3828
3829/* Create a new artificial heap variable with NAME and make a
d3553615
RG
3830 constraint from it to LHS. Set flags according to a tag used
3831 for tracking restrict pointers. */
0b7b376d
RG
3832
3833static varinfo_t
3781ab4b 3834make_constraint_from_restrict (varinfo_t lhs, const char *name, bool add_id)
0b7b376d 3835{
3781ab4b 3836 varinfo_t vi = make_heapvar (name, add_id);
aa098165 3837 vi->is_restrict_var = 1;
d3553615
RG
3838 vi->is_global_var = 1;
3839 vi->may_have_pointers = 1;
74d27244 3840 make_constraint_from (lhs, vi->id);
74d27244
RG
3841 return vi;
3842}
3843
3844/* Create a new artificial heap variable with NAME and make a
3845 constraint from it to LHS. Set flags according to a tag used
d3553615
RG
3846 for tracking restrict pointers and make the artificial heap
3847 point to global memory. */
74d27244 3848
d3553615 3849static varinfo_t
3781ab4b
TV
3850make_constraint_from_global_restrict (varinfo_t lhs, const char *name,
3851 bool add_id)
74d27244 3852{
3781ab4b 3853 varinfo_t vi = make_constraint_from_restrict (lhs, name, add_id);
d3553615
RG
3854 make_copy_constraint (vi, nonlocal_id);
3855 return vi;
74d27244
RG
3856}
3857
25a6a873
RG
3858/* In IPA mode there are varinfos for different aspects of reach
3859 function designator. One for the points-to set of the return
3860 value, one for the variables that are clobbered by the function,
3861 one for its uses and one for each parameter (including a single
3862 glob for remaining variadic arguments). */
3863
3864enum { fi_clobbers = 1, fi_uses = 2,
3865 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 };
3866
3867/* Get a constraint for the requested part of a function designator FI
3868 when operating in IPA mode. */
3869
3870static struct constraint_expr
3871get_function_part_constraint (varinfo_t fi, unsigned part)
3872{
3873 struct constraint_expr c;
3874
3875 gcc_assert (in_ipa_mode);
3876
3877 if (fi->id == anything_id)
3878 {
3879 /* ??? We probably should have a ANYFN special variable. */
3880 c.var = anything_id;
3881 c.offset = 0;
3882 c.type = SCALAR;
3883 }
3884 else if (TREE_CODE (fi->decl) == FUNCTION_DECL)
3885 {
3886 varinfo_t ai = first_vi_for_offset (fi, part);
18abb35e
RG
3887 if (ai)
3888 c.var = ai->id;
3889 else
3890 c.var = anything_id;
25a6a873
RG
3891 c.offset = 0;
3892 c.type = SCALAR;
3893 }
3894 else
3895 {
3896 c.var = fi->id;
3897 c.offset = part;
3898 c.type = DEREF;
3899 }
3900
3901 return c;
3902}
3903
7b765bed
DB
3904/* For non-IPA mode, generate constraints necessary for a call on the
3905 RHS. */
3906
3907static void
538dd0b7 3908handle_rhs_call (gcall *stmt, vec<ce_s> *results)
7b765bed 3909{
472c7fbd 3910 struct constraint_expr rhsc;
726a989a 3911 unsigned i;
0b7b376d 3912 bool returns_uses = false;
7b765bed 3913
726a989a
RB
3914 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3915 {
3916 tree arg = gimple_call_arg (stmt, i);
0b7b376d 3917 int flags = gimple_call_arg_flags (stmt, i);
726a989a 3918
0f8d6231
RG
3919 /* If the argument is not used we can ignore it. */
3920 if (flags & EAF_UNUSED)
0b7b376d
RG
3921 continue;
3922
3923 /* As we compute ESCAPED context-insensitive we do not gain
3924 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3925 set. The argument would still get clobbered through the
81ab7312 3926 escape solution. */
0b7b376d
RG
3927 if ((flags & EAF_NOCLOBBER)
3928 && (flags & EAF_NOESCAPE))
3929 {
3930 varinfo_t uses = get_call_use_vi (stmt);
6811ea79
RB
3931 varinfo_t tem = new_var_info (NULL_TREE, "callarg", true);
3932 make_constraint_to (tem->id, arg);
3933 make_any_offset_constraints (tem);
0b7b376d 3934 if (!(flags & EAF_DIRECT))
6811ea79
RB
3935 make_transitive_closure_constraints (tem);
3936 make_copy_constraint (uses, tem->id);
0b7b376d
RG
3937 returns_uses = true;
3938 }
3939 else if (flags & EAF_NOESCAPE)
3940 {
81ab7312 3941 struct constraint_expr lhs, rhs;
0b7b376d
RG
3942 varinfo_t uses = get_call_use_vi (stmt);
3943 varinfo_t clobbers = get_call_clobber_vi (stmt);
3781ab4b 3944 varinfo_t tem = new_var_info (NULL_TREE, "callarg", true);
81ab7312 3945 make_constraint_to (tem->id, arg);
6811ea79 3946 make_any_offset_constraints (tem);
0b7b376d 3947 if (!(flags & EAF_DIRECT))
81ab7312
RG
3948 make_transitive_closure_constraints (tem);
3949 make_copy_constraint (uses, tem->id);
3950 make_copy_constraint (clobbers, tem->id);
3951 /* Add *tem = nonlocal, do not add *tem = callused as
3952 EAF_NOESCAPE parameters do not escape to other parameters
3953 and all other uses appear in NONLOCAL as well. */
3954 lhs.type = DEREF;
3955 lhs.var = tem->id;
3956 lhs.offset = 0;
3957 rhs.type = SCALAR;
3958 rhs.var = nonlocal_id;
3959 rhs.offset = 0;
3960 process_constraint (new_constraint (lhs, rhs));
0b7b376d
RG
3961 returns_uses = true;
3962 }
3963 else
726a989a
RB
3964 make_escape_constraint (arg);
3965 }
b7091901 3966
0b7b376d
RG
3967 /* If we added to the calls uses solution make sure we account for
3968 pointers to it to be returned. */
3969 if (returns_uses)
3970 {
3971 rhsc.var = get_call_use_vi (stmt)->id;
6811ea79 3972 rhsc.offset = UNKNOWN_OFFSET;
0b7b376d 3973 rhsc.type = SCALAR;
9771b263 3974 results->safe_push (rhsc);
0b7b376d
RG
3975 }
3976
b7091901 3977 /* The static chain escapes as well. */
726a989a
RB
3978 if (gimple_call_chain (stmt))
3979 make_escape_constraint (gimple_call_chain (stmt));
472c7fbd 3980
1d24fdd9
RG
3981 /* And if we applied NRV the address of the return slot escapes as well. */
3982 if (gimple_call_return_slot_opt_p (stmt)
3983 && gimple_call_lhs (stmt) != NULL_TREE
4d61856d 3984 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
1d24fdd9 3985 {
ef062b13 3986 auto_vec<ce_s> tmpc;
1d24fdd9
RG
3987 struct constraint_expr lhsc, *c;
3988 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3989 lhsc.var = escaped_id;
3990 lhsc.offset = 0;
3991 lhsc.type = SCALAR;
9771b263 3992 FOR_EACH_VEC_ELT (tmpc, i, c)
1d24fdd9 3993 process_constraint (new_constraint (lhsc, *c));
1d24fdd9
RG
3994 }
3995
5006671f
RG
3996 /* Regular functions return nonlocal memory. */
3997 rhsc.var = nonlocal_id;
472c7fbd 3998 rhsc.offset = 0;
5006671f 3999 rhsc.type = SCALAR;
9771b263 4000 results->safe_push (rhsc);
7b765bed 4001}
e8ca4159 4002
af947da7
RG
4003/* For non-IPA mode, generate constraints necessary for a call
4004 that returns a pointer and assigns it to LHS. This simply makes
b7091901 4005 the LHS point to global and escaped variables. */
af947da7
RG
4006
4007static void
538dd0b7 4008handle_lhs_call (gcall *stmt, tree lhs, int flags, vec<ce_s> rhsc,
0b7b376d 4009 tree fndecl)
af947da7 4010{
ef062b13 4011 auto_vec<ce_s> lhsc;
af947da7 4012
b7091901 4013 get_constraint_for (lhs, &lhsc);
0b7b376d
RG
4014 /* If the store is to a global decl make sure to
4015 add proper escape constraints. */
4016 lhs = get_base_address (lhs);
4017 if (lhs
4018 && DECL_P (lhs)
4019 && is_global_var (lhs))
4020 {
4021 struct constraint_expr tmpc;
4022 tmpc.var = escaped_id;
4023 tmpc.offset = 0;
4024 tmpc.type = SCALAR;
9771b263 4025 lhsc.safe_push (tmpc);
0b7b376d 4026 }
183ae595 4027
0b7b376d
RG
4028 /* If the call returns an argument unmodified override the rhs
4029 constraints. */
0b7b376d
RG
4030 if (flags & ERF_RETURNS_ARG
4031 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
4032 {
4033 tree arg;
9771b263 4034 rhsc.create (0);
0b7b376d
RG
4035 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
4036 get_constraint_for (arg, &rhsc);
4037 process_all_all_constraints (lhsc, rhsc);
9771b263 4038 rhsc.release ();
0b7b376d
RG
4039 }
4040 else if (flags & ERF_NOALIAS)
183ae595 4041 {
183ae595 4042 varinfo_t vi;
0b7b376d 4043 struct constraint_expr tmpc;
9771b263 4044 rhsc.create (0);
3781ab4b 4045 vi = make_heapvar ("HEAP", true);
32cab212 4046 /* We are marking allocated storage local, we deal with it becoming
11924f8b 4047 global by escaping and setting of vars_contains_escaped_heap. */
91deb937 4048 DECL_EXTERNAL (vi->decl) = 0;
14c41b9b 4049 vi->is_global_var = 0;
72d182d3 4050 /* If this is not a real malloc call assume the memory was
0b7b376d 4051 initialized and thus may point to global memory. All
72d182d3
RG
4052 builtin functions with the malloc attribute behave in a sane way. */
4053 if (!fndecl
4054 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
4055 make_constraint_from (vi, nonlocal_id);
0b7b376d
RG
4056 tmpc.var = vi->id;
4057 tmpc.offset = 0;
4058 tmpc.type = ADDRESSOF;
9771b263 4059 rhsc.safe_push (tmpc);
f5843d08 4060 process_all_all_constraints (lhsc, rhsc);
9771b263 4061 rhsc.release ();
183ae595 4062 }
f5843d08
RG
4063 else
4064 process_all_all_constraints (lhsc, rhsc);
b7091901
RG
4065}
4066
4067/* For non-IPA mode, generate constraints necessary for a call of a
4068 const function that returns a pointer in the statement STMT. */
4069
4070static void
538dd0b7 4071handle_const_call (gcall *stmt, vec<ce_s> *results)
b7091901 4072{
b14e9388 4073 struct constraint_expr rhsc;
472c7fbd 4074 unsigned int k;
4023bc56 4075 bool need_uses = false;
b7091901 4076
472c7fbd
RG
4077 /* Treat nested const functions the same as pure functions as far
4078 as the static chain is concerned. */
726a989a 4079 if (gimple_call_chain (stmt))
4023bc56
RB
4080 {
4081 varinfo_t uses = get_call_use_vi (stmt);
4082 make_constraint_to (uses->id, gimple_call_chain (stmt));
4083 need_uses = true;
4084 }
4085
4086 /* And if we applied NRV the address of the return slot escapes as well. */
4087 if (gimple_call_return_slot_opt_p (stmt)
4088 && gimple_call_lhs (stmt) != NULL_TREE
4089 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
4090 {
4091 varinfo_t uses = get_call_use_vi (stmt);
4092 auto_vec<ce_s> tmpc;
4093 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
4094 make_constraints_to (uses->id, tmpc);
4095 need_uses = true;
4096 }
4097
4098 if (need_uses)
b7091901 4099 {
3e8542ca 4100 varinfo_t uses = get_call_use_vi (stmt);
6811ea79 4101 make_any_offset_constraints (uses);
3e8542ca 4102 make_transitive_closure_constraints (uses);
3e8542ca 4103 rhsc.var = uses->id;
b7091901 4104 rhsc.offset = 0;
472c7fbd 4105 rhsc.type = SCALAR;
9771b263 4106 results->safe_push (rhsc);
b7091901
RG
4107 }
4108
6811ea79
RB
4109 /* May return offsetted arguments. */
4110 varinfo_t tem = NULL;
4111 if (gimple_call_num_args (stmt) != 0)
4112 tem = new_var_info (NULL_TREE, "callarg", true);
726a989a
RB
4113 for (k = 0; k < gimple_call_num_args (stmt); ++k)
4114 {
4115 tree arg = gimple_call_arg (stmt, k);
ef062b13 4116 auto_vec<ce_s> argc;
0f8d6231 4117 get_constraint_for_rhs (arg, &argc);
6811ea79
RB
4118 make_constraints_to (tem->id, argc);
4119 }
4120 if (tem)
4121 {
4122 ce_s ce;
4123 ce.type = SCALAR;
4124 ce.var = tem->id;
4125 ce.offset = UNKNOWN_OFFSET;
4126 results->safe_push (ce);
726a989a 4127 }
b7091901 4128
472c7fbd
RG
4129 /* May return addresses of globals. */
4130 rhsc.var = nonlocal_id;
4131 rhsc.offset = 0;
4132 rhsc.type = ADDRESSOF;
9771b263 4133 results->safe_push (rhsc);
af947da7
RG
4134}
4135
15c15196
RG
4136/* For non-IPA mode, generate constraints necessary for a call to a
4137 pure function in statement STMT. */
4138
4139static void
538dd0b7 4140handle_pure_call (gcall *stmt, vec<ce_s> *results)
15c15196 4141{
472c7fbd 4142 struct constraint_expr rhsc;
726a989a 4143 unsigned i;
3e8542ca 4144 varinfo_t uses = NULL;
15c15196
RG
4145
4146 /* Memory reached from pointer arguments is call-used. */
726a989a
RB
4147 for (i = 0; i < gimple_call_num_args (stmt); ++i)
4148 {
4149 tree arg = gimple_call_arg (stmt, i);
0f8d6231 4150 if (!uses)
472c7fbd 4151 {
0f8d6231 4152 uses = get_call_use_vi (stmt);
6811ea79 4153 make_any_offset_constraints (uses);
0f8d6231 4154 make_transitive_closure_constraints (uses);
472c7fbd 4155 }
0f8d6231 4156 make_constraint_to (uses->id, arg);
726a989a 4157 }
15c15196
RG
4158
4159 /* The static chain is used as well. */
726a989a 4160 if (gimple_call_chain (stmt))
15c15196 4161 {
3e8542ca
RG
4162 if (!uses)
4163 {
4164 uses = get_call_use_vi (stmt);
6811ea79 4165 make_any_offset_constraints (uses);
3e8542ca
RG
4166 make_transitive_closure_constraints (uses);
4167 }
4168 make_constraint_to (uses->id, gimple_call_chain (stmt));
472c7fbd 4169 }
15c15196 4170
4023bc56
RB
4171 /* And if we applied NRV the address of the return slot. */
4172 if (gimple_call_return_slot_opt_p (stmt)
4173 && gimple_call_lhs (stmt) != NULL_TREE
4174 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
4175 {
4176 if (!uses)
4177 {
4178 uses = get_call_use_vi (stmt);
4179 make_any_offset_constraints (uses);
4180 make_transitive_closure_constraints (uses);
4181 }
4182 auto_vec<ce_s> tmpc;
4183 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
4184 make_constraints_to (uses->id, tmpc);
4185 }
4186
3e8542ca
RG
4187 /* Pure functions may return call-used and nonlocal memory. */
4188 if (uses)
472c7fbd 4189 {
3e8542ca 4190 rhsc.var = uses->id;
15c15196 4191 rhsc.offset = 0;
472c7fbd 4192 rhsc.type = SCALAR;
9771b263 4193 results->safe_push (rhsc);
15c15196 4194 }
5006671f 4195 rhsc.var = nonlocal_id;
472c7fbd 4196 rhsc.offset = 0;
5006671f 4197 rhsc.type = SCALAR;
9771b263 4198 results->safe_push (rhsc);
15c15196
RG
4199}
4200
25a6a873
RG
4201
4202/* Return the varinfo for the callee of CALL. */
4203
4204static varinfo_t
538dd0b7 4205get_fi_for_callee (gcall *call)
25a6a873 4206{
5c04e9f4 4207 tree decl, fn = gimple_call_fn (call);
25a6a873 4208
5c04e9f4
RG
4209 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
4210 fn = OBJ_TYPE_REF_EXPR (fn);
25583c4f 4211
25a6a873
RG
4212 /* If we can directly resolve the function being called, do so.
4213 Otherwise, it must be some sort of indirect expression that
4214 we should still be able to handle. */
5c04e9f4 4215 decl = gimple_call_addr_fndecl (fn);
25a6a873
RG
4216 if (decl)
4217 return get_vi_for_tree (decl);
4218
5c04e9f4 4219 /* If the function is anything other than a SSA name pointer we have no
25a6a873 4220 clue and should be getting ANYFN (well, ANYTHING for now). */
5c04e9f4 4221 if (!fn || TREE_CODE (fn) != SSA_NAME)
25a6a873 4222 return get_varinfo (anything_id);
5c04e9f4 4223
67386041
RG
4224 if (SSA_NAME_IS_DEFAULT_DEF (fn)
4225 && (TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
4226 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL))
5c04e9f4
RG
4227 fn = SSA_NAME_VAR (fn);
4228
4229 return get_vi_for_tree (fn);
25a6a873
RG
4230}
4231
e0ca27c5
TV
4232/* Create constraints for assigning call argument ARG to the incoming parameter
4233 INDEX of function FI. */
4234
4235static void
4236find_func_aliases_for_call_arg (varinfo_t fi, unsigned index, tree arg)
4237{
4238 struct constraint_expr lhs;
4239 lhs = get_function_part_constraint (fi, fi_parm_base + index);
4240
4241 auto_vec<ce_s, 2> rhsc;
4242 get_constraint_for_rhs (arg, &rhsc);
4243
4244 unsigned j;
4245 struct constraint_expr *rhsp;
4246 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4247 process_constraint (new_constraint (lhs, *rhsp));
4248}
4249
17b6426c
TV
4250/* Return true if FNDECL may be part of another lto partition. */
4251
4252static bool
4253fndecl_maybe_in_other_partition (tree fndecl)
4254{
4255 cgraph_node *fn_node = cgraph_node::get (fndecl);
4256 if (fn_node == NULL)
4257 return true;
4258
4259 return fn_node->in_other_partition;
4260}
4261
e38811ce
RG
4262/* Create constraints for the builtin call T. Return true if the call
4263 was handled, otherwise false. */
910fdc79 4264
e38811ce 4265static bool
538dd0b7 4266find_func_aliases_for_builtin_call (struct function *fn, gcall *t)
910fdc79 4267{
e38811ce 4268 tree fndecl = gimple_call_fndecl (t);
a0791dcc
RB
4269 auto_vec<ce_s, 2> lhsc;
4270 auto_vec<ce_s, 4> rhsc;
25a6a873 4271 varinfo_t fi;
910fdc79 4272
3626621a 4273 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
e38811ce
RG
4274 /* ??? All builtins that are handled here need to be handled
4275 in the alias-oracle query functions explicitly! */
4276 switch (DECL_FUNCTION_CODE (fndecl))
4277 {
4278 /* All the following functions return a pointer to the same object
4279 as their first argument points to. The functions do not add
4280 to the ESCAPED solution. The functions make the first argument
4281 pointed to memory point to what the second argument pointed to
4282 memory points to. */
4283 case BUILT_IN_STRCPY:
4284 case BUILT_IN_STRNCPY:
4285 case BUILT_IN_BCOPY:
4286 case BUILT_IN_MEMCPY:
4287 case BUILT_IN_MEMMOVE:
4288 case BUILT_IN_MEMPCPY:
4289 case BUILT_IN_STPCPY:
4290 case BUILT_IN_STPNCPY:
4291 case BUILT_IN_STRCAT:
4292 case BUILT_IN_STRNCAT:
36dc1a88
JJ
4293 case BUILT_IN_STRCPY_CHK:
4294 case BUILT_IN_STRNCPY_CHK:
4295 case BUILT_IN_MEMCPY_CHK:
4296 case BUILT_IN_MEMMOVE_CHK:
4297 case BUILT_IN_MEMPCPY_CHK:
4298 case BUILT_IN_STPCPY_CHK:
f3fc9b80 4299 case BUILT_IN_STPNCPY_CHK:
36dc1a88
JJ
4300 case BUILT_IN_STRCAT_CHK:
4301 case BUILT_IN_STRNCAT_CHK:
0a35513e
AH
4302 case BUILT_IN_TM_MEMCPY:
4303 case BUILT_IN_TM_MEMMOVE:
e8ca4159 4304 {
e38811ce
RG
4305 tree res = gimple_call_lhs (t);
4306 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4307 == BUILT_IN_BCOPY ? 1 : 0));
4308 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4309 == BUILT_IN_BCOPY ? 0 : 1));
4310 if (res != NULL_TREE)
0f8d6231 4311 {
e38811ce
RG
4312 get_constraint_for (res, &lhsc);
4313 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4314 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
ce7e54ff
JJ
4315 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4316 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
f3fc9b80
RG
4317 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK
4318 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK)
e38811ce
RG
4319 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4320 else
4321 get_constraint_for (dest, &rhsc);
4322 process_all_all_constraints (lhsc, rhsc);
a0791dcc
RB
4323 lhsc.truncate (0);
4324 rhsc.truncate (0);
c58936b6 4325 }
e38811ce
RG
4326 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4327 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4328 do_deref (&lhsc);
4329 do_deref (&rhsc);
4330 process_all_all_constraints (lhsc, rhsc);
e38811ce 4331 return true;
4ee00913 4332 }
e38811ce 4333 case BUILT_IN_MEMSET:
36dc1a88 4334 case BUILT_IN_MEMSET_CHK:
0a35513e 4335 case BUILT_IN_TM_MEMSET:
e38811ce
RG
4336 {
4337 tree res = gimple_call_lhs (t);
4338 tree dest = gimple_call_arg (t, 0);
4339 unsigned i;
4340 ce_s *lhsp;
4341 struct constraint_expr ac;
4342 if (res != NULL_TREE)
779704e7 4343 {
e38811ce
RG
4344 get_constraint_for (res, &lhsc);
4345 get_constraint_for (dest, &rhsc);
779704e7 4346 process_all_all_constraints (lhsc, rhsc);
a0791dcc 4347 lhsc.truncate (0);
779704e7 4348 }
e38811ce
RG
4349 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4350 do_deref (&lhsc);
4351 if (flag_delete_null_pointer_checks
4352 && integer_zerop (gimple_call_arg (t, 1)))
779704e7 4353 {
e38811ce
RG
4354 ac.type = ADDRESSOF;
4355 ac.var = nothing_id;
779704e7 4356 }
e38811ce 4357 else
25a6a873 4358 {
e38811ce
RG
4359 ac.type = SCALAR;
4360 ac.var = integer_id;
25a6a873 4361 }
e38811ce 4362 ac.offset = 0;
9771b263 4363 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
e38811ce 4364 process_constraint (new_constraint (*lhsp, ac));
e38811ce
RG
4365 return true;
4366 }
32cab212
RB
4367 case BUILT_IN_POSIX_MEMALIGN:
4368 {
4369 tree ptrptr = gimple_call_arg (t, 0);
4370 get_constraint_for (ptrptr, &lhsc);
4371 do_deref (&lhsc);
3781ab4b 4372 varinfo_t vi = make_heapvar ("HEAP", true);
32cab212
RB
4373 /* We are marking allocated storage local, we deal with it becoming
4374 global by escaping and setting of vars_contains_escaped_heap. */
4375 DECL_EXTERNAL (vi->decl) = 0;
4376 vi->is_global_var = 0;
4377 struct constraint_expr tmpc;
4378 tmpc.var = vi->id;
4379 tmpc.offset = 0;
4380 tmpc.type = ADDRESSOF;
4381 rhsc.safe_push (tmpc);
4382 process_all_all_constraints (lhsc, rhsc);
32cab212
RB
4383 return true;
4384 }
d9048d16
JJ
4385 case BUILT_IN_ASSUME_ALIGNED:
4386 {
4387 tree res = gimple_call_lhs (t);
4388 tree dest = gimple_call_arg (t, 0);
4389 if (res != NULL_TREE)
4390 {
4391 get_constraint_for (res, &lhsc);
4392 get_constraint_for (dest, &rhsc);
4393 process_all_all_constraints (lhsc, rhsc);
d9048d16
JJ
4394 }
4395 return true;
4396 }
e38811ce
RG
4397 /* All the following functions do not return pointers, do not
4398 modify the points-to sets of memory reachable from their
4399 arguments and do not add to the ESCAPED solution. */
4400 case BUILT_IN_SINCOS:
4401 case BUILT_IN_SINCOSF:
4402 case BUILT_IN_SINCOSL:
4403 case BUILT_IN_FREXP:
4404 case BUILT_IN_FREXPF:
4405 case BUILT_IN_FREXPL:
4406 case BUILT_IN_GAMMA_R:
4407 case BUILT_IN_GAMMAF_R:
4408 case BUILT_IN_GAMMAL_R:
4409 case BUILT_IN_LGAMMA_R:
4410 case BUILT_IN_LGAMMAF_R:
4411 case BUILT_IN_LGAMMAL_R:
4412 case BUILT_IN_MODF:
4413 case BUILT_IN_MODFF:
4414 case BUILT_IN_MODFL:
4415 case BUILT_IN_REMQUO:
4416 case BUILT_IN_REMQUOF:
4417 case BUILT_IN_REMQUOL:
4418 case BUILT_IN_FREE:
4419 return true;
915afed6
JJ
4420 case BUILT_IN_STRDUP:
4421 case BUILT_IN_STRNDUP:
c8f49949 4422 case BUILT_IN_REALLOC:
915afed6
JJ
4423 if (gimple_call_lhs (t))
4424 {
c8f49949
RB
4425 handle_lhs_call (t, gimple_call_lhs (t),
4426 gimple_call_return_flags (t) | ERF_NOALIAS,
6e1aa848 4427 vNULL, fndecl);
915afed6
JJ
4428 get_constraint_for_ptr_offset (gimple_call_lhs (t),
4429 NULL_TREE, &lhsc);
4430 get_constraint_for_ptr_offset (gimple_call_arg (t, 0),
4431 NULL_TREE, &rhsc);
4432 do_deref (&lhsc);
4433 do_deref (&rhsc);
4434 process_all_all_constraints (lhsc, rhsc);
a0791dcc
RB
4435 lhsc.truncate (0);
4436 rhsc.truncate (0);
c8f49949
RB
4437 /* For realloc the resulting pointer can be equal to the
4438 argument as well. But only doing this wouldn't be
4439 correct because with ptr == 0 realloc behaves like malloc. */
4440 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_REALLOC)
4441 {
4442 get_constraint_for (gimple_call_lhs (t), &lhsc);
4443 get_constraint_for (gimple_call_arg (t, 0), &rhsc);
4444 process_all_all_constraints (lhsc, rhsc);
c8f49949 4445 }
915afed6
JJ
4446 return true;
4447 }
4448 break;
92608d0e
RB
4449 /* String / character search functions return a pointer into the
4450 source string or NULL. */
4451 case BUILT_IN_INDEX:
4452 case BUILT_IN_STRCHR:
4453 case BUILT_IN_STRRCHR:
4454 case BUILT_IN_MEMCHR:
4455 case BUILT_IN_STRSTR:
4456 case BUILT_IN_STRPBRK:
4457 if (gimple_call_lhs (t))
4458 {
4459 tree src = gimple_call_arg (t, 0);
4460 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4461 constraint_expr nul;
4462 nul.var = nothing_id;
4463 nul.offset = 0;
4464 nul.type = ADDRESSOF;
4465 rhsc.safe_push (nul);
4466 get_constraint_for (gimple_call_lhs (t), &lhsc);
4467 process_all_all_constraints (lhsc, rhsc);
92608d0e
RB
4468 }
4469 return true;
2fd30fbe
RB
4470 /* Pure functions that return something not based on any object and
4471 that use the memory pointed to by their arguments (but not
4472 transitively). */
4473 case BUILT_IN_STRCMP:
4474 case BUILT_IN_STRNCMP:
4475 case BUILT_IN_STRCASECMP:
4476 case BUILT_IN_STRNCASECMP:
4477 case BUILT_IN_MEMCMP:
4478 case BUILT_IN_BCMP:
4479 case BUILT_IN_STRSPN:
4480 case BUILT_IN_STRCSPN:
4481 {
4482 varinfo_t uses = get_call_use_vi (t);
4483 make_any_offset_constraints (uses);
4484 make_constraint_to (uses->id, gimple_call_arg (t, 0));
4485 make_constraint_to (uses->id, gimple_call_arg (t, 1));
4486 /* No constraints are necessary for the return value. */
4487 return true;
4488 }
4489 case BUILT_IN_STRLEN:
4490 {
4491 varinfo_t uses = get_call_use_vi (t);
4492 make_any_offset_constraints (uses);
4493 make_constraint_to (uses->id, gimple_call_arg (t, 0));
4494 /* No constraints are necessary for the return value. */
4495 return true;
4496 }
4497 case BUILT_IN_OBJECT_SIZE:
4498 case BUILT_IN_CONSTANT_P:
4499 {
4500 /* No constraints are necessary for the return value or the
4501 arguments. */
4502 return true;
4503 }
e38811ce
RG
4504 /* Trampolines are special - they set up passing the static
4505 frame. */
4506 case BUILT_IN_INIT_TRAMPOLINE:
4507 {
4508 tree tramp = gimple_call_arg (t, 0);
4509 tree nfunc = gimple_call_arg (t, 1);
4510 tree frame = gimple_call_arg (t, 2);
4511 unsigned i;
4512 struct constraint_expr lhs, *rhsp;
4513 if (in_ipa_mode)
25a6a873 4514 {
e38811ce
RG
4515 varinfo_t nfi = NULL;
4516 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR);
4517 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0));
4518 if (nfi)
25a6a873 4519 {
e38811ce
RG
4520 lhs = get_function_part_constraint (nfi, fi_static_chain);
4521 get_constraint_for (frame, &rhsc);
9771b263 4522 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
a0791dcc
RB
4523 process_constraint (new_constraint (lhs, *rhsp));
4524 rhsc.truncate (0);
e38811ce
RG
4525
4526 /* Make the frame point to the function for
4527 the trampoline adjustment call. */
4528 get_constraint_for (tramp, &lhsc);
4529 do_deref (&lhsc);
4530 get_constraint_for (nfunc, &rhsc);
25a6a873 4531 process_all_all_constraints (lhsc, rhsc);
e38811ce
RG
4532
4533 return true;
25a6a873 4534 }
25a6a873 4535 }
e38811ce
RG
4536 /* Else fallthru to generic handling which will let
4537 the frame escape. */
4538 break;
4539 }
4540 case BUILT_IN_ADJUST_TRAMPOLINE:
4541 {
4542 tree tramp = gimple_call_arg (t, 0);
4543 tree res = gimple_call_lhs (t);
4544 if (in_ipa_mode && res)
25a6a873 4545 {
e38811ce
RG
4546 get_constraint_for (res, &lhsc);
4547 get_constraint_for (tramp, &rhsc);
4548 do_deref (&rhsc);
4549 process_all_all_constraints (lhsc, rhsc);
25a6a873 4550 }
e38811ce
RG
4551 return true;
4552 }
0a35513e
AH
4553 CASE_BUILT_IN_TM_STORE (1):
4554 CASE_BUILT_IN_TM_STORE (2):
4555 CASE_BUILT_IN_TM_STORE (4):
4556 CASE_BUILT_IN_TM_STORE (8):
4557 CASE_BUILT_IN_TM_STORE (FLOAT):
4558 CASE_BUILT_IN_TM_STORE (DOUBLE):
4559 CASE_BUILT_IN_TM_STORE (LDOUBLE):
4560 CASE_BUILT_IN_TM_STORE (M64):
4561 CASE_BUILT_IN_TM_STORE (M128):
4562 CASE_BUILT_IN_TM_STORE (M256):
4563 {
4564 tree addr = gimple_call_arg (t, 0);
4565 tree src = gimple_call_arg (t, 1);
4566
4567 get_constraint_for (addr, &lhsc);
4568 do_deref (&lhsc);
4569 get_constraint_for (src, &rhsc);
4570 process_all_all_constraints (lhsc, rhsc);
0a35513e
AH
4571 return true;
4572 }
4573 CASE_BUILT_IN_TM_LOAD (1):
4574 CASE_BUILT_IN_TM_LOAD (2):
4575 CASE_BUILT_IN_TM_LOAD (4):
4576 CASE_BUILT_IN_TM_LOAD (8):
4577 CASE_BUILT_IN_TM_LOAD (FLOAT):
4578 CASE_BUILT_IN_TM_LOAD (DOUBLE):
4579 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
4580 CASE_BUILT_IN_TM_LOAD (M64):
4581 CASE_BUILT_IN_TM_LOAD (M128):
4582 CASE_BUILT_IN_TM_LOAD (M256):
4583 {
4584 tree dest = gimple_call_lhs (t);
4585 tree addr = gimple_call_arg (t, 0);
4586
4587 get_constraint_for (dest, &lhsc);
4588 get_constraint_for (addr, &rhsc);
4589 do_deref (&rhsc);
4590 process_all_all_constraints (lhsc, rhsc);
0a35513e
AH
4591 return true;
4592 }
e38811ce
RG
4593 /* Variadic argument handling needs to be handled in IPA
4594 mode as well. */
4595 case BUILT_IN_VA_START:
4596 {
df2f6100
RG
4597 tree valist = gimple_call_arg (t, 0);
4598 struct constraint_expr rhs, *lhsp;
4599 unsigned i;
ab4ccf20 4600 get_constraint_for_ptr_offset (valist, NULL_TREE, &lhsc);
df2f6100
RG
4601 do_deref (&lhsc);
4602 /* The va_list gets access to pointers in variadic
4603 arguments. Which we know in the case of IPA analysis
4604 and otherwise are just all nonlocal variables. */
e38811ce 4605 if (in_ipa_mode)
a4c9bc15 4606 {
628169e0 4607 fi = lookup_vi_for_tree (fn->decl);
e38811ce
RG
4608 rhs = get_function_part_constraint (fi, ~0);
4609 rhs.type = ADDRESSOF;
a4c9bc15 4610 }
df2f6100
RG
4611 else
4612 {
4613 rhs.var = nonlocal_id;
4614 rhs.type = ADDRESSOF;
4615 rhs.offset = 0;
4616 }
9771b263 4617 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
df2f6100 4618 process_constraint (new_constraint (*lhsp, rhs));
df2f6100
RG
4619 /* va_list is clobbered. */
4620 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4621 return true;
e38811ce
RG
4622 }
4623 /* va_end doesn't have any effect that matters. */
4624 case BUILT_IN_VA_END:
4625 return true;
4626 /* Alternate return. Simply give up for now. */
4627 case BUILT_IN_RETURN:
4ee00913 4628 {
e38811ce
RG
4629 fi = NULL;
4630 if (!in_ipa_mode
628169e0 4631 || !(fi = get_vi_for_tree (fn->decl)))
e38811ce
RG
4632 make_constraint_from (get_varinfo (escaped_id), anything_id);
4633 else if (in_ipa_mode
4634 && fi != NULL)
b7091901 4635 {
e38811ce
RG
4636 struct constraint_expr lhs, rhs;
4637 lhs = get_function_part_constraint (fi, fi_result);
4638 rhs.var = anything_id;
4639 rhs.offset = 0;
4640 rhs.type = SCALAR;
4641 process_constraint (new_constraint (lhs, rhs));
b7091901 4642 }
e38811ce
RG
4643 return true;
4644 }
e0ca27c5 4645 case BUILT_IN_GOMP_PARALLEL:
694e5e4b 4646 case BUILT_IN_GOACC_PARALLEL:
e0ca27c5 4647 {
e0ca27c5
TV
4648 if (in_ipa_mode)
4649 {
694e5e4b
TV
4650 unsigned int fnpos, argpos;
4651 switch (DECL_FUNCTION_CODE (fndecl))
4652 {
4653 case BUILT_IN_GOMP_PARALLEL:
4654 /* __builtin_GOMP_parallel (fn, data, num_threads, flags). */
4655 fnpos = 0;
4656 argpos = 1;
4657 break;
4658 case BUILT_IN_GOACC_PARALLEL:
4659 /* __builtin_GOACC_parallel (device, fn, mapnum, hostaddrs,
4660 sizes, kinds, ...). */
4661 fnpos = 1;
4662 argpos = 3;
4663 break;
4664 default:
4665 gcc_unreachable ();
4666 }
4667
4668 tree fnarg = gimple_call_arg (t, fnpos);
e0ca27c5
TV
4669 gcc_assert (TREE_CODE (fnarg) == ADDR_EXPR);
4670 tree fndecl = TREE_OPERAND (fnarg, 0);
17b6426c
TV
4671 if (fndecl_maybe_in_other_partition (fndecl))
4672 /* Fallthru to general call handling. */
4673 break;
4674
694e5e4b 4675 tree arg = gimple_call_arg (t, argpos);
e0ca27c5
TV
4676
4677 varinfo_t fi = get_vi_for_tree (fndecl);
4678 find_func_aliases_for_call_arg (fi, 0, arg);
4679 return true;
4680 }
4681 /* Else fallthru to generic call handling. */
4682 break;
4683 }
e38811ce
RG
4684 /* printf-style functions may have hooks to set pointers to
4685 point to somewhere into the generated string. Leave them
c0d18c6c 4686 for a later exercise... */
e38811ce
RG
4687 default:
4688 /* Fallthru to general call handling. */;
4689 }
4690
4691 return false;
4692}
4693
4694/* Create constraints for the call T. */
4695
4696static void
538dd0b7 4697find_func_aliases_for_call (struct function *fn, gcall *t)
e38811ce
RG
4698{
4699 tree fndecl = gimple_call_fndecl (t);
e38811ce
RG
4700 varinfo_t fi;
4701
4702 if (fndecl != NULL_TREE
4703 && DECL_BUILT_IN (fndecl)
628169e0 4704 && find_func_aliases_for_builtin_call (fn, t))
e38811ce
RG
4705 return;
4706
5c04e9f4 4707 fi = get_fi_for_callee (t);
e38811ce 4708 if (!in_ipa_mode
5c04e9f4 4709 || (fndecl && !fi->is_fn_info))
e38811ce 4710 {
a0791dcc 4711 auto_vec<ce_s, 16> rhsc;
e38811ce
RG
4712 int flags = gimple_call_flags (t);
4713
4714 /* Const functions can return their arguments and addresses
4715 of global memory but not of escaped memory. */
4716 if (flags & (ECF_CONST|ECF_NOVOPS))
4717 {
cb89b4b0 4718 if (gimple_call_lhs (t))
e38811ce 4719 handle_const_call (t, &rhsc);
4ee00913 4720 }
e38811ce
RG
4721 /* Pure functions can return addresses in and of memory
4722 reachable from their arguments, but they are not an escape
4723 point for reachable memory of their arguments. */
4724 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4725 handle_pure_call (t, &rhsc);
4ee00913 4726 else
e38811ce
RG
4727 handle_rhs_call (t, &rhsc);
4728 if (gimple_call_lhs (t))
c8f49949
RB
4729 handle_lhs_call (t, gimple_call_lhs (t),
4730 gimple_call_return_flags (t), rhsc, fndecl);
e38811ce
RG
4731 }
4732 else
4733 {
a0791dcc 4734 auto_vec<ce_s, 2> rhsc;
e38811ce
RG
4735 tree lhsop;
4736 unsigned j;
6e7e772d 4737
e38811ce
RG
4738 /* Assign all the passed arguments to the appropriate incoming
4739 parameters of the function. */
4740 for (j = 0; j < gimple_call_num_args (t); j++)
4741 {
e38811ce 4742 tree arg = gimple_call_arg (t, j);
e0ca27c5 4743 find_func_aliases_for_call_arg (fi, j, arg);
e38811ce
RG
4744 }
4745
4746 /* If we are returning a value, assign it to the result. */
4747 lhsop = gimple_call_lhs (t);
4748 if (lhsop)
4749 {
a0791dcc 4750 auto_vec<ce_s, 2> lhsc;
e38811ce
RG
4751 struct constraint_expr rhs;
4752 struct constraint_expr *lhsp;
72ed2b9c 4753 bool aggr_p = aggregate_value_p (lhsop, gimple_call_fntype (t));
25a6a873 4754
e38811ce
RG
4755 get_constraint_for (lhsop, &lhsc);
4756 rhs = get_function_part_constraint (fi, fi_result);
72ed2b9c 4757 if (aggr_p)
25a6a873 4758 {
a0791dcc
RB
4759 auto_vec<ce_s, 2> tem;
4760 tem.quick_push (rhs);
e38811ce 4761 do_deref (&tem);
a0791dcc 4762 gcc_checking_assert (tem.length () == 1);
9771b263 4763 rhs = tem[0];
25a6a873 4764 }
9771b263 4765 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
5c04e9f4 4766 process_constraint (new_constraint (*lhsp, rhs));
25a6a873 4767
72ed2b9c
RB
4768 /* If we pass the result decl by reference, honor that. */
4769 if (aggr_p)
4770 {
4771 struct constraint_expr lhs;
4772 struct constraint_expr *rhsp;
e38811ce 4773
72ed2b9c
RB
4774 get_constraint_for_address_of (lhsop, &rhsc);
4775 lhs = get_function_part_constraint (fi, fi_result);
4776 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4777 process_constraint (new_constraint (lhs, *rhsp));
4778 rhsc.truncate (0);
4779 }
e38811ce 4780 }
25a6a873 4781
e38811ce
RG
4782 /* If we use a static chain, pass it along. */
4783 if (gimple_call_chain (t))
4784 {
4785 struct constraint_expr lhs;
4786 struct constraint_expr *rhsp;
4787
4788 get_constraint_for (gimple_call_chain (t), &rhsc);
4789 lhs = get_function_part_constraint (fi, fi_static_chain);
9771b263 4790 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5c04e9f4 4791 process_constraint (new_constraint (lhs, *rhsp));
e38811ce
RG
4792 }
4793 }
4794}
4795
4796/* Walk statement T setting up aliasing constraints according to the
4797 references found in T. This function is the main part of the
4798 constraint builder. AI points to auxiliary alias information used
4799 when building alias sets and computing alias grouping heuristics. */
4800
4801static void
355fe088 4802find_func_aliases (struct function *fn, gimple *origt)
e38811ce 4803{
355fe088 4804 gimple *t = origt;
a0791dcc
RB
4805 auto_vec<ce_s, 16> lhsc;
4806 auto_vec<ce_s, 16> rhsc;
e38811ce
RG
4807 struct constraint_expr *c;
4808 varinfo_t fi;
4809
4810 /* Now build constraints expressions. */
4811 if (gimple_code (t) == GIMPLE_PHI)
4812 {
4813 size_t i;
4814 unsigned int j;
4815
4816 /* For a phi node, assign all the arguments to
4817 the result. */
4818 get_constraint_for (gimple_phi_result (t), &lhsc);
4819 for (i = 0; i < gimple_phi_num_args (t); i++)
4820 {
4821 tree strippedrhs = PHI_ARG_DEF (t, i);
4822
4823 STRIP_NOPS (strippedrhs);
4824 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
4825
9771b263 4826 FOR_EACH_VEC_ELT (lhsc, j, c)
e38811ce
RG
4827 {
4828 struct constraint_expr *c2;
9771b263 4829 while (rhsc.length () > 0)
e38811ce 4830 {
9771b263 4831 c2 = &rhsc.last ();
e38811ce 4832 process_constraint (new_constraint (*c, *c2));
9771b263 4833 rhsc.pop ();
e38811ce 4834 }
25a6a873 4835 }
c58936b6 4836 }
e8ca4159 4837 }
e38811ce
RG
4838 /* In IPA mode, we need to generate constraints to pass call
4839 arguments through their calls. There are two cases,
4840 either a GIMPLE_CALL returning a value, or just a plain
4841 GIMPLE_CALL when we are not.
4842
4843 In non-ipa mode, we need to generate constraints for each
4844 pointer passed by address. */
4845 else if (is_gimple_call (t))
538dd0b7 4846 find_func_aliases_for_call (fn, as_a <gcall *> (t));
e38811ce 4847
e5bae89b
RG
4848 /* Otherwise, just a regular assignment statement. Only care about
4849 operations with pointer result, others are dealt with as escape
4850 points if they have pointer operands. */
0f8d6231 4851 else if (is_gimple_assign (t))
e8ca4159 4852 {
726a989a
RB
4853 /* Otherwise, just a regular assignment statement. */
4854 tree lhsop = gimple_assign_lhs (t);
4855 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
e8ca4159 4856
47598145
MM
4857 if (rhsop && TREE_CLOBBER_P (rhsop))
4858 /* Ignore clobbers, they don't actually store anything into
4859 the LHS. */
4860 ;
4861 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
e5bae89b 4862 do_structure_copy (lhsop, rhsop);
e8ca4159
DN
4863 else
4864 {
194313e2
RG
4865 enum tree_code code = gimple_assign_rhs_code (t);
4866
e5bae89b 4867 get_constraint_for (lhsop, &lhsc);
726a989a 4868
7b909872 4869 if (code == POINTER_PLUS_EXPR)
726a989a
RB
4870 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4871 gimple_assign_rhs2 (t), &rhsc);
194313e2 4872 else if (code == BIT_AND_EXPR
fca821b5
RG
4873 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
4874 {
4875 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4876 the pointer. Handle it by offsetting it by UNKNOWN. */
4877 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4878 NULL_TREE, &rhsc);
4879 }
194313e2 4880 else if ((CONVERT_EXPR_CODE_P (code)
1961418e
RG
4881 && !(POINTER_TYPE_P (gimple_expr_type (t))
4882 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
4883 || gimple_assign_single_p (t))
ed6c4831 4884 get_constraint_for_rhs (rhsop, &rhsc);
6cadda8b
RB
4885 else if (code == COND_EXPR)
4886 {
4887 /* The result is a merge of both COND_EXPR arms. */
a0791dcc 4888 auto_vec<ce_s, 2> tmp;
6cadda8b
RB
4889 struct constraint_expr *rhsp;
4890 unsigned i;
4891 get_constraint_for_rhs (gimple_assign_rhs2 (t), &rhsc);
4892 get_constraint_for_rhs (gimple_assign_rhs3 (t), &tmp);
9771b263
DN
4893 FOR_EACH_VEC_ELT (tmp, i, rhsp)
4894 rhsc.safe_push (*rhsp);
6cadda8b 4895 }
194313e2
RG
4896 else if (truth_value_p (code))
4897 /* Truth value results are not pointer (parts). Or at least
026c3cfd 4898 very unreasonable obfuscation of a part. */
194313e2 4899 ;
726a989a
RB
4900 else
4901 {
0f8d6231 4902 /* All other operations are merges. */
a0791dcc 4903 auto_vec<ce_s, 4> tmp;
0f8d6231
RG
4904 struct constraint_expr *rhsp;
4905 unsigned i, j;
4906 get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc);
4907 for (i = 2; i < gimple_num_ops (t); ++i)
4908 {
4909 get_constraint_for_rhs (gimple_op (t, i), &tmp);
9771b263
DN
4910 FOR_EACH_VEC_ELT (tmp, j, rhsp)
4911 rhsc.safe_push (*rhsp);
4912 tmp.truncate (0);
0f8d6231 4913 }
726a989a 4914 }
779704e7 4915 process_all_all_constraints (lhsc, rhsc);
e8ca4159 4916 }
de70bb20
RG
4917 /* If there is a store to a global variable the rhs escapes. */
4918 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
47e5754e
RB
4919 && DECL_P (lhsop))
4920 {
4921 varinfo_t vi = get_vi_for_tree (lhsop);
4922 if ((! in_ipa_mode && vi->is_global_var)
4923 || vi->is_ipa_escape_point)
4924 make_escape_constraint (rhsop);
4925 }
910fdc79 4926 }
14c41b9b
RG
4927 /* Handle escapes through return. */
4928 else if (gimple_code (t) == GIMPLE_RETURN
538dd0b7 4929 && gimple_return_retval (as_a <greturn *> (t)) != NULL_TREE)
14c41b9b 4930 {
538dd0b7 4931 greturn *return_stmt = as_a <greturn *> (t);
25a6a873
RG
4932 fi = NULL;
4933 if (!in_ipa_mode
628169e0 4934 || !(fi = get_vi_for_tree (fn->decl)))
538dd0b7 4935 make_escape_constraint (gimple_return_retval (return_stmt));
47e5754e 4936 else if (in_ipa_mode)
25a6a873
RG
4937 {
4938 struct constraint_expr lhs ;
4939 struct constraint_expr *rhsp;
4940 unsigned i;
4941
4942 lhs = get_function_part_constraint (fi, fi_result);
538dd0b7 4943 get_constraint_for_rhs (gimple_return_retval (return_stmt), &rhsc);
9771b263 4944 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
25a6a873
RG
4945 process_constraint (new_constraint (lhs, *rhsp));
4946 }
14c41b9b 4947 }
2e407842 4948 /* Handle asms conservatively by adding escape constraints to everything. */
538dd0b7 4949 else if (gasm *asm_stmt = dyn_cast <gasm *> (t))
b7091901 4950 {
5006671f
RG
4951 unsigned i, noutputs;
4952 const char **oconstraints;
4953 const char *constraint;
4954 bool allows_mem, allows_reg, is_inout;
4955
538dd0b7 4956 noutputs = gimple_asm_noutputs (asm_stmt);
5006671f
RG
4957 oconstraints = XALLOCAVEC (const char *, noutputs);
4958
4959 for (i = 0; i < noutputs; ++i)
b7091901 4960 {
538dd0b7 4961 tree link = gimple_asm_output_op (asm_stmt, i);
5006671f
RG
4962 tree op = TREE_VALUE (link);
4963
4964 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4965 oconstraints[i] = constraint;
4966 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4967 &allows_reg, &is_inout);
4968
4969 /* A memory constraint makes the address of the operand escape. */
4970 if (!allows_reg && allows_mem)
4971 make_escape_constraint (build_fold_addr_expr (op));
4972
4973 /* The asm may read global memory, so outputs may point to
aee63acc 4974 any global memory. */
0f8d6231 4975 if (op)
5006671f 4976 {
a0791dcc 4977 auto_vec<ce_s, 2> lhsc;
5006671f
RG
4978 struct constraint_expr rhsc, *lhsp;
4979 unsigned j;
4980 get_constraint_for (op, &lhsc);
aee63acc 4981 rhsc.var = nonlocal_id;
5006671f
RG
4982 rhsc.offset = 0;
4983 rhsc.type = SCALAR;
9771b263 4984 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
5006671f 4985 process_constraint (new_constraint (*lhsp, rhsc));
5006671f 4986 }
b7091901 4987 }
538dd0b7 4988 for (i = 0; i < gimple_asm_ninputs (asm_stmt); ++i)
b7091901 4989 {
538dd0b7 4990 tree link = gimple_asm_input_op (asm_stmt, i);
5006671f
RG
4991 tree op = TREE_VALUE (link);
4992
4993 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4994
4995 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4996 &allows_mem, &allows_reg);
4997
4998 /* A memory constraint makes the address of the operand escape. */
4999 if (!allows_reg && allows_mem)
5000 make_escape_constraint (build_fold_addr_expr (op));
5001 /* Strictly we'd only need the constraint to ESCAPED if
3e8542ca
RG
5002 the asm clobbers memory, otherwise using something
5003 along the lines of per-call clobbers/uses would be enough. */
0f8d6231 5004 else if (op)
b7091901
RG
5005 make_escape_constraint (op);
5006 }
5007 }
910fdc79
DB
5008}
5009
5010
25a6a873
RG
5011/* Create a constraint adding to the clobber set of FI the memory
5012 pointed to by PTR. */
5013
5014static void
5015process_ipa_clobber (varinfo_t fi, tree ptr)
5016{
6e1aa848 5017 vec<ce_s> ptrc = vNULL;
25a6a873
RG
5018 struct constraint_expr *c, lhs;
5019 unsigned i;
ed6c4831 5020 get_constraint_for_rhs (ptr, &ptrc);
25a6a873 5021 lhs = get_function_part_constraint (fi, fi_clobbers);
9771b263 5022 FOR_EACH_VEC_ELT (ptrc, i, c)
25a6a873 5023 process_constraint (new_constraint (lhs, *c));
9771b263 5024 ptrc.release ();
25a6a873
RG
5025}
5026
5027/* Walk statement T setting up clobber and use constraints according to the
5028 references found in T. This function is a main part of the
5029 IPA constraint builder. */
5030
5031static void
355fe088 5032find_func_clobbers (struct function *fn, gimple *origt)
25a6a873 5033{
355fe088 5034 gimple *t = origt;
a0791dcc
RB
5035 auto_vec<ce_s, 16> lhsc;
5036 auto_vec<ce_s, 16> rhsc;
25a6a873
RG
5037 varinfo_t fi;
5038
5039 /* Add constraints for clobbered/used in IPA mode.
5040 We are not interested in what automatic variables are clobbered
5041 or used as we only use the information in the caller to which
5042 they do not escape. */
5043 gcc_assert (in_ipa_mode);
5044
5045 /* If the stmt refers to memory in any way it better had a VUSE. */
5046 if (gimple_vuse (t) == NULL_TREE)
5047 return;
5048
5049 /* We'd better have function information for the current function. */
628169e0 5050 fi = lookup_vi_for_tree (fn->decl);
25a6a873
RG
5051 gcc_assert (fi != NULL);
5052
5053 /* Account for stores in assignments and calls. */
5054 if (gimple_vdef (t) != NULL_TREE
5055 && gimple_has_lhs (t))
5056 {
5057 tree lhs = gimple_get_lhs (t);
5058 tree tem = lhs;
5059 while (handled_component_p (tem))
5060 tem = TREE_OPERAND (tem, 0);
5061 if ((DECL_P (tem)
628169e0 5062 && !auto_var_in_fn_p (tem, fn->decl))
70f34814
RG
5063 || INDIRECT_REF_P (tem)
5064 || (TREE_CODE (tem) == MEM_REF
5065 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
5066 && auto_var_in_fn_p
628169e0 5067 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl))))
25a6a873
RG
5068 {
5069 struct constraint_expr lhsc, *rhsp;
5070 unsigned i;
5071 lhsc = get_function_part_constraint (fi, fi_clobbers);
5072 get_constraint_for_address_of (lhs, &rhsc);
9771b263 5073 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
25a6a873 5074 process_constraint (new_constraint (lhsc, *rhsp));
a0791dcc 5075 rhsc.truncate (0);
25a6a873
RG
5076 }
5077 }
5078
5079 /* Account for uses in assigments and returns. */
5080 if (gimple_assign_single_p (t)
5081 || (gimple_code (t) == GIMPLE_RETURN
538dd0b7 5082 && gimple_return_retval (as_a <greturn *> (t)) != NULL_TREE))
25a6a873
RG
5083 {
5084 tree rhs = (gimple_assign_single_p (t)
538dd0b7
DM
5085 ? gimple_assign_rhs1 (t)
5086 : gimple_return_retval (as_a <greturn *> (t)));
25a6a873
RG
5087 tree tem = rhs;
5088 while (handled_component_p (tem))
5089 tem = TREE_OPERAND (tem, 0);
5090 if ((DECL_P (tem)
628169e0 5091 && !auto_var_in_fn_p (tem, fn->decl))
70f34814
RG
5092 || INDIRECT_REF_P (tem)
5093 || (TREE_CODE (tem) == MEM_REF
5094 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
5095 && auto_var_in_fn_p
628169e0 5096 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), fn->decl))))
25a6a873
RG
5097 {
5098 struct constraint_expr lhs, *rhsp;
5099 unsigned i;
5100 lhs = get_function_part_constraint (fi, fi_uses);
5101 get_constraint_for_address_of (rhs, &rhsc);
9771b263 5102 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
25a6a873 5103 process_constraint (new_constraint (lhs, *rhsp));
a0791dcc 5104 rhsc.truncate (0);
25a6a873
RG
5105 }
5106 }
5107
538dd0b7 5108 if (gcall *call_stmt = dyn_cast <gcall *> (t))
25a6a873
RG
5109 {
5110 varinfo_t cfi = NULL;
5111 tree decl = gimple_call_fndecl (t);
5112 struct constraint_expr lhs, rhs;
5113 unsigned i, j;
5114
5115 /* For builtins we do not have separate function info. For those
5116 we do not generate escapes for we have to generate clobbers/uses. */
3626621a 5117 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
25a6a873
RG
5118 switch (DECL_FUNCTION_CODE (decl))
5119 {
5120 /* The following functions use and clobber memory pointed to
5121 by their arguments. */
5122 case BUILT_IN_STRCPY:
5123 case BUILT_IN_STRNCPY:
5124 case BUILT_IN_BCOPY:
5125 case BUILT_IN_MEMCPY:
5126 case BUILT_IN_MEMMOVE:
5127 case BUILT_IN_MEMPCPY:
5128 case BUILT_IN_STPCPY:
5129 case BUILT_IN_STPNCPY:
5130 case BUILT_IN_STRCAT:
5131 case BUILT_IN_STRNCAT:
36dc1a88
JJ
5132 case BUILT_IN_STRCPY_CHK:
5133 case BUILT_IN_STRNCPY_CHK:
5134 case BUILT_IN_MEMCPY_CHK:
5135 case BUILT_IN_MEMMOVE_CHK:
5136 case BUILT_IN_MEMPCPY_CHK:
5137 case BUILT_IN_STPCPY_CHK:
f3fc9b80 5138 case BUILT_IN_STPNCPY_CHK:
36dc1a88
JJ
5139 case BUILT_IN_STRCAT_CHK:
5140 case BUILT_IN_STRNCAT_CHK:
25a6a873
RG
5141 {
5142 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
5143 == BUILT_IN_BCOPY ? 1 : 0));
5144 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
5145 == BUILT_IN_BCOPY ? 0 : 1));
5146 unsigned i;
5147 struct constraint_expr *rhsp, *lhsp;
5148 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
5149 lhs = get_function_part_constraint (fi, fi_clobbers);
9771b263 5150 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
25a6a873 5151 process_constraint (new_constraint (lhs, *lhsp));
25a6a873
RG
5152 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
5153 lhs = get_function_part_constraint (fi, fi_uses);
9771b263 5154 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
25a6a873 5155 process_constraint (new_constraint (lhs, *rhsp));
25a6a873
RG
5156 return;
5157 }
5158 /* The following function clobbers memory pointed to by
5159 its argument. */
5160 case BUILT_IN_MEMSET:
36dc1a88 5161 case BUILT_IN_MEMSET_CHK:
32cab212 5162 case BUILT_IN_POSIX_MEMALIGN:
25a6a873
RG
5163 {
5164 tree dest = gimple_call_arg (t, 0);
5165 unsigned i;
5166 ce_s *lhsp;
5167 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
5168 lhs = get_function_part_constraint (fi, fi_clobbers);
9771b263 5169 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
25a6a873 5170 process_constraint (new_constraint (lhs, *lhsp));
25a6a873
RG
5171 return;
5172 }
5173 /* The following functions clobber their second and third
5174 arguments. */
5175 case BUILT_IN_SINCOS:
5176 case BUILT_IN_SINCOSF:
5177 case BUILT_IN_SINCOSL:
5178 {
5179 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5180 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5181 return;
5182 }
5183 /* The following functions clobber their second argument. */
5184 case BUILT_IN_FREXP:
5185 case BUILT_IN_FREXPF:
5186 case BUILT_IN_FREXPL:
5187 case BUILT_IN_LGAMMA_R:
5188 case BUILT_IN_LGAMMAF_R:
5189 case BUILT_IN_LGAMMAL_R:
5190 case BUILT_IN_GAMMA_R:
5191 case BUILT_IN_GAMMAF_R:
5192 case BUILT_IN_GAMMAL_R:
5193 case BUILT_IN_MODF:
5194 case BUILT_IN_MODFF:
5195 case BUILT_IN_MODFL:
5196 {
5197 process_ipa_clobber (fi, gimple_call_arg (t, 1));
5198 return;
5199 }
5200 /* The following functions clobber their third argument. */
5201 case BUILT_IN_REMQUO:
5202 case BUILT_IN_REMQUOF:
5203 case BUILT_IN_REMQUOL:
5204 {
5205 process_ipa_clobber (fi, gimple_call_arg (t, 2));
5206 return;
5207 }
5208 /* The following functions neither read nor clobber memory. */
45d439ac 5209 case BUILT_IN_ASSUME_ALIGNED:
25a6a873
RG
5210 case BUILT_IN_FREE:
5211 return;
5212 /* Trampolines are of no interest to us. */
5213 case BUILT_IN_INIT_TRAMPOLINE:
5214 case BUILT_IN_ADJUST_TRAMPOLINE:
5215 return;
5216 case BUILT_IN_VA_START:
5217 case BUILT_IN_VA_END:
5218 return;
e0ca27c5 5219 case BUILT_IN_GOMP_PARALLEL:
694e5e4b 5220 case BUILT_IN_GOACC_PARALLEL:
76680678
TV
5221 {
5222 unsigned int fnpos, argpos;
40e2341e
TV
5223 unsigned int implicit_use_args[2];
5224 unsigned int num_implicit_use_args = 0;
76680678
TV
5225 switch (DECL_FUNCTION_CODE (decl))
5226 {
5227 case BUILT_IN_GOMP_PARALLEL:
5228 /* __builtin_GOMP_parallel (fn, data, num_threads, flags). */
5229 fnpos = 0;
5230 argpos = 1;
5231 break;
5232 case BUILT_IN_GOACC_PARALLEL:
5233 /* __builtin_GOACC_parallel (device, fn, mapnum, hostaddrs,
5234 sizes, kinds, ...). */
5235 fnpos = 1;
5236 argpos = 3;
40e2341e
TV
5237 implicit_use_args[num_implicit_use_args++] = 4;
5238 implicit_use_args[num_implicit_use_args++] = 5;
76680678
TV
5239 break;
5240 default:
5241 gcc_unreachable ();
5242 }
5243
5244 tree fnarg = gimple_call_arg (t, fnpos);
5245 gcc_assert (TREE_CODE (fnarg) == ADDR_EXPR);
5246 tree fndecl = TREE_OPERAND (fnarg, 0);
17b6426c
TV
5247 if (fndecl_maybe_in_other_partition (fndecl))
5248 /* Fallthru to general call handling. */
5249 break;
5250
76680678
TV
5251 varinfo_t cfi = get_vi_for_tree (fndecl);
5252
5253 tree arg = gimple_call_arg (t, argpos);
5254
5255 /* Parameter passed by value is used. */
5256 lhs = get_function_part_constraint (fi, fi_uses);
5257 struct constraint_expr *rhsp;
5258 get_constraint_for (arg, &rhsc);
5259 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5260 process_constraint (new_constraint (lhs, *rhsp));
5261 rhsc.truncate (0);
5262
40e2341e
TV
5263 /* Handle parameters used by the call, but not used in cfi, as
5264 implicitly used by cfi. */
5265 lhs = get_function_part_constraint (cfi, fi_uses);
5266 for (unsigned i = 0; i < num_implicit_use_args; ++i)
5267 {
5268 tree arg = gimple_call_arg (t, implicit_use_args[i]);
5269 get_constraint_for (arg, &rhsc);
5270 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
5271 process_constraint (new_constraint (lhs, *rhsp));
5272 rhsc.truncate (0);
5273 }
5274
76680678
TV
5275 /* The caller clobbers what the callee does. */
5276 lhs = get_function_part_constraint (fi, fi_clobbers);
5277 rhs = get_function_part_constraint (cfi, fi_clobbers);
5278 process_constraint (new_constraint (lhs, rhs));
5279
5280 /* The caller uses what the callee does. */
5281 lhs = get_function_part_constraint (fi, fi_uses);
5282 rhs = get_function_part_constraint (cfi, fi_uses);
5283 process_constraint (new_constraint (lhs, rhs));
5284
5285 return;
5286 }
25a6a873
RG
5287 /* printf-style functions may have hooks to set pointers to
5288 point to somewhere into the generated string. Leave them
c0d18c6c 5289 for a later exercise... */
25a6a873
RG
5290 default:
5291 /* Fallthru to general call handling. */;
5292 }
5293
5294 /* Parameters passed by value are used. */
5295 lhs = get_function_part_constraint (fi, fi_uses);
5296 for (i = 0; i < gimple_call_num_args (t); i++)
5297 {
5298 struct constraint_expr *rhsp;
5299 tree arg = gimple_call_arg (t, i);
5300
5301 if (TREE_CODE (arg) == SSA_NAME
5302 || is_gimple_min_invariant (arg))
5303 continue;
5304
5305 get_constraint_for_address_of (arg, &rhsc);
9771b263 5306 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
25a6a873 5307 process_constraint (new_constraint (lhs, *rhsp));
3f734f66 5308 rhsc.truncate (0);
25a6a873
RG
5309 }
5310
5311 /* Build constraints for propagating clobbers/uses along the
5312 callgraph edges. */
538dd0b7 5313 cfi = get_fi_for_callee (call_stmt);
25a6a873
RG
5314 if (cfi->id == anything_id)
5315 {
5316 if (gimple_vdef (t))
5317 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5318 anything_id);
5319 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5320 anything_id);
5321 return;
5322 }
5323
5324 /* For callees without function info (that's external functions),
5325 ESCAPED is clobbered and used. */
5326 if (gimple_call_fndecl (t)
5327 && !cfi->is_fn_info)
5328 {
5329 varinfo_t vi;
5330
5331 if (gimple_vdef (t))
5332 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5333 escaped_id);
5334 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
5335
5336 /* Also honor the call statement use/clobber info. */
538dd0b7 5337 if ((vi = lookup_call_clobber_vi (call_stmt)) != NULL)
25a6a873
RG
5338 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
5339 vi->id);
538dd0b7 5340 if ((vi = lookup_call_use_vi (call_stmt)) != NULL)
25a6a873
RG
5341 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
5342 vi->id);
5343 return;
5344 }
5345
5346 /* Otherwise the caller clobbers and uses what the callee does.
5347 ??? This should use a new complex constraint that filters
5348 local variables of the callee. */
5349 if (gimple_vdef (t))
5350 {
5351 lhs = get_function_part_constraint (fi, fi_clobbers);
5352 rhs = get_function_part_constraint (cfi, fi_clobbers);
5353 process_constraint (new_constraint (lhs, rhs));
5354 }
5355 lhs = get_function_part_constraint (fi, fi_uses);
5356 rhs = get_function_part_constraint (cfi, fi_uses);
5357 process_constraint (new_constraint (lhs, rhs));
5358 }
5359 else if (gimple_code (t) == GIMPLE_ASM)
5360 {
5361 /* ??? Ick. We can do better. */
5362 if (gimple_vdef (t))
5363 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
5364 anything_id);
5365 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5366 anything_id);
5367 }
25a6a873
RG
5368}
5369
5370
910fdc79 5371/* Find the first varinfo in the same variable as START that overlaps with
5006671f 5372 OFFSET. Return NULL if we can't find one. */
910fdc79 5373
c58936b6 5374static varinfo_t
910fdc79
DB
5375first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
5376{
5006671f
RG
5377 /* If the offset is outside of the variable, bail out. */
5378 if (offset >= start->fullsize)
5379 return NULL;
5380
5381 /* If we cannot reach offset from start, lookup the first field
5382 and start from there. */
5383 if (start->offset > offset)
d6d305fe 5384 start = get_varinfo (start->head);
5006671f
RG
5385
5386 while (start)
910fdc79
DB
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.
910fdc79
DB
5390 In that case, however, offset should still be within the size
5391 of the variable. */
5006671f 5392 if (offset >= start->offset
de925a03 5393 && (offset - start->offset) < start->size)
5006671f
RG
5394 return start;
5395
d6d305fe 5396 start = vi_next (start);
910fdc79 5397 }
5006671f 5398
8971094d 5399 return NULL;
910fdc79
DB
5400}
5401
5006671f
RG
5402/* Find the first varinfo in the same variable as START that overlaps with
5403 OFFSET. If there is no such varinfo the varinfo directly preceding
5404 OFFSET is returned. */
5405
5406static varinfo_t
5407first_or_preceding_vi_for_offset (varinfo_t start,
5408 unsigned HOST_WIDE_INT offset)
5409{
5410 /* If we cannot reach offset from start, lookup the first field
5411 and start from there. */
5412 if (start->offset > offset)
d6d305fe 5413 start = get_varinfo (start->head);
5006671f
RG
5414
5415 /* We may not find a variable in the field list with the actual
026c3cfd 5416 offset when we have glommed a structure to a variable.
5006671f
RG
5417 In that case, however, offset should still be within the size
5418 of the variable.
5419 If we got beyond the offset we look for return the field
5420 directly preceding offset which may be the last field. */
5421 while (start->next
5422 && offset >= start->offset
de925a03 5423 && !((offset - start->offset) < start->size))
d6d305fe 5424 start = vi_next (start);
5006671f
RG
5425
5426 return start;
5427}
5428
910fdc79 5429
31de5b77
RG
5430/* This structure is used during pushing fields onto the fieldstack
5431 to track the offset of the field, since bitpos_of_field gives it
5432 relative to its immediate containing type, and we want it relative
5433 to the ultimate containing object. */
5434
5435struct fieldoff
5436{
ee7d4b57
RG
5437 /* Offset from the base of the base containing object to this field. */
5438 HOST_WIDE_INT offset;
31de5b77
RG
5439
5440 /* Size, in bits, of the field. */
ee7d4b57 5441 unsigned HOST_WIDE_INT size;
31de5b77 5442
ee7d4b57 5443 unsigned has_unknown_size : 1;
31de5b77 5444
0f8d6231
RG
5445 unsigned must_have_pointers : 1;
5446
ee7d4b57 5447 unsigned may_have_pointers : 1;
74d27244
RG
5448
5449 unsigned only_restrict_pointers : 1;
7da5eaa0
TV
5450
5451 tree restrict_pointed_type;
31de5b77
RG
5452};
5453typedef struct fieldoff fieldoff_s;
5454
31de5b77 5455
910fdc79
DB
5456/* qsort comparison function for two fieldoff's PA and PB */
5457
c58936b6 5458static int
910fdc79
DB
5459fieldoff_compare (const void *pa, const void *pb)
5460{
5461 const fieldoff_s *foa = (const fieldoff_s *)pa;
5462 const fieldoff_s *fob = (const fieldoff_s *)pb;
185ab3b6 5463 unsigned HOST_WIDE_INT foasize, fobsize;
c58936b6 5464
185ab3b6
RG
5465 if (foa->offset < fob->offset)
5466 return -1;
5467 else if (foa->offset > fob->offset)
5468 return 1;
910fdc79 5469
ee7d4b57
RG
5470 foasize = foa->size;
5471 fobsize = fob->size;
185ab3b6 5472 if (foasize < fobsize)
ee7d4b57 5473 return -1;
185ab3b6
RG
5474 else if (foasize > fobsize)
5475 return 1;
5476 return 0;
910fdc79
DB
5477}
5478
5479/* Sort a fieldstack according to the field offset and sizes. */
31de5b77 5480static void
9771b263 5481sort_fieldstack (vec<fieldoff_s> fieldstack)
910fdc79 5482{
9771b263 5483 fieldstack.qsort (fieldoff_compare);
910fdc79
DB
5484}
5485
b4cf8c9d
RG
5486/* Return true if T is a type that can have subvars. */
5487
5488static inline bool
5489type_can_have_subvars (const_tree t)
5490{
5491 /* Aggregates without overlapping fields can have subvars. */
5492 return TREE_CODE (t) == RECORD_TYPE;
5493}
5494
31de5b77
RG
5495/* Return true if V is a tree that we can have subvars for.
5496 Normally, this is any aggregate type. Also complex
5497 types which are not gimple registers can have subvars. */
5498
5499static inline bool
5500var_can_have_subvars (const_tree v)
5501{
5502 /* Volatile variables should never have subvars. */
5503 if (TREE_THIS_VOLATILE (v))
5504 return false;
5505
5506 /* Non decls or memory tags can never have subvars. */
5006671f 5507 if (!DECL_P (v))
31de5b77
RG
5508 return false;
5509
b4cf8c9d 5510 return type_can_have_subvars (TREE_TYPE (v));
31de5b77
RG
5511}
5512
0f8d6231
RG
5513/* Return true if T is a type that does contain pointers. */
5514
5515static bool
5516type_must_have_pointers (tree type)
5517{
5518 if (POINTER_TYPE_P (type))
5519 return true;
5520
5521 if (TREE_CODE (type) == ARRAY_TYPE)
5522 return type_must_have_pointers (TREE_TYPE (type));
5523
5524 /* A function or method can have pointers as arguments, so track
5525 those separately. */
5526 if (TREE_CODE (type) == FUNCTION_TYPE
5527 || TREE_CODE (type) == METHOD_TYPE)
5528 return true;
5529
5530 return false;
5531}
5532
5533static bool
5534field_must_have_pointers (tree t)
5535{
5536 return type_must_have_pointers (TREE_TYPE (t));
5537}
5538
d7705551
DN
5539/* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5540 the fields of TYPE onto fieldstack, recording their offsets along
5541 the way.
5542
5543 OFFSET is used to keep track of the offset in this entire
5544 structure, rather than just the immediately containing structure.
18abb35e
RG
5545 Returns false if the caller is supposed to handle the field we
5546 recursed for. */
910fdc79 5547
18abb35e 5548static bool
9771b263 5549push_fields_onto_fieldstack (tree type, vec<fieldoff_s> *fieldstack,
0f8d6231 5550 HOST_WIDE_INT offset)
910fdc79
DB
5551{
5552 tree field;
18abb35e 5553 bool empty_p = true;
31de5b77
RG
5554
5555 if (TREE_CODE (type) != RECORD_TYPE)
18abb35e 5556 return false;
3fe2f42a
RG
5557
5558 /* If the vector of fields is growing too big, bail out early.
9771b263 5559 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
3fe2f42a 5560 sure this fails. */
9771b263 5561 if (fieldstack->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
18abb35e 5562 return false;
c58936b6 5563
910ad8de 5564 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
31de5b77
RG
5565 if (TREE_CODE (field) == FIELD_DECL)
5566 {
5567 bool push = false;
ee7d4b57 5568 HOST_WIDE_INT foff = bitpos_of_field (field);
5acdb61b 5569 tree field_type = TREE_TYPE (field);
31de5b77 5570
ee7d4b57 5571 if (!var_can_have_subvars (field)
5acdb61b
TV
5572 || TREE_CODE (field_type) == QUAL_UNION_TYPE
5573 || TREE_CODE (field_type) == UNION_TYPE)
31de5b77 5574 push = true;
18abb35e 5575 else if (!push_fields_onto_fieldstack
5acdb61b 5576 (field_type, fieldstack, offset + foff)
31de5b77
RG
5577 && (DECL_SIZE (field)
5578 && !integer_zerop (DECL_SIZE (field))))
5579 /* Empty structures may have actual size, like in C++. So
5580 see if we didn't push any subfields and the size is
5581 nonzero, push the field onto the stack. */
5582 push = true;
5583
5584 if (push)
910fdc79 5585 {
ee7d4b57
RG
5586 fieldoff_s *pair = NULL;
5587 bool has_unknown_size = false;
0f8d6231 5588 bool must_have_pointers_p;
ee7d4b57 5589
9771b263
DN
5590 if (!fieldstack->is_empty ())
5591 pair = &fieldstack->last ();
ee7d4b57 5592
3fd3b156
RG
5593 /* If there isn't anything at offset zero, create sth. */
5594 if (!pair
5595 && offset + foff != 0)
5596 {
7da5eaa0 5597 fieldoff_s e
3cea049a 5598 = {0, offset + foff, false, false, true, false, NULL_TREE};
9771b263 5599 pair = fieldstack->safe_push (e);
3fd3b156
RG
5600 }
5601
ee7d4b57 5602 if (!DECL_SIZE (field)
cc269bb6 5603 || !tree_fits_uhwi_p (DECL_SIZE (field)))
ee7d4b57
RG
5604 has_unknown_size = true;
5605
5606 /* If adjacent fields do not contain pointers merge them. */
0f8d6231 5607 must_have_pointers_p = field_must_have_pointers (field);
ee7d4b57 5608 if (pair
ee7d4b57 5609 && !has_unknown_size
a81b065a 5610 && !must_have_pointers_p
0f8d6231
RG
5611 && !pair->must_have_pointers
5612 && !pair->has_unknown_size
5613 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
ee7d4b57 5614 {
eb1ce453 5615 pair->size += tree_to_uhwi (DECL_SIZE (field));
ee7d4b57
RG
5616 }
5617 else
5618 {
f32682ca
DN
5619 fieldoff_s e;
5620 e.offset = offset + foff;
5621 e.has_unknown_size = has_unknown_size;
ee7d4b57 5622 if (!has_unknown_size)
eb1ce453 5623 e.size = tree_to_uhwi (DECL_SIZE (field));
ee7d4b57 5624 else
f32682ca
DN
5625 e.size = -1;
5626 e.must_have_pointers = must_have_pointers_p;
5627 e.may_have_pointers = true;
5628 e.only_restrict_pointers
74d27244 5629 = (!has_unknown_size
5acdb61b
TV
5630 && POINTER_TYPE_P (field_type)
5631 && TYPE_RESTRICT (field_type));
7da5eaa0
TV
5632 if (e.only_restrict_pointers)
5633 e.restrict_pointed_type = TREE_TYPE (field_type);
9771b263 5634 fieldstack->safe_push (e);
ee7d4b57 5635 }
31de5b77 5636 }
18abb35e
RG
5637
5638 empty_p = false;
31de5b77 5639 }
910fdc79 5640
18abb35e 5641 return !empty_p;
910fdc79
DB
5642}
5643
5006671f
RG
5644/* Count the number of arguments DECL has, and set IS_VARARGS to true
5645 if it is a varargs function. */
5646
5647static unsigned int
5648count_num_arguments (tree decl, bool *is_varargs)
5649{
de925a03 5650 unsigned int num = 0;
5006671f
RG
5651 tree t;
5652
de925a03
RG
5653 /* Capture named arguments for K&R functions. They do not
5654 have a prototype and thus no TYPE_ARG_TYPES. */
910ad8de 5655 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
de925a03 5656 ++num;
c58936b6 5657
de925a03
RG
5658 /* Check if the function has variadic arguments. */
5659 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5660 if (TREE_VALUE (t) == void_type_node)
5661 break;
4ee00913
DB
5662 if (!t)
5663 *is_varargs = true;
de925a03
RG
5664
5665 return num;
4ee00913
DB
5666}
5667
5668/* Creation function node for DECL, using NAME, and return the index
d0c9ca44
TV
5669 of the variable we've created for the function. If NONLOCAL_p, create
5670 initial constraints. */
4ee00913 5671
27c2cfa6 5672static varinfo_t
d0c9ca44
TV
5673create_function_info_for (tree decl, const char *name, bool add_id,
5674 bool nonlocal_p)
4ee00913 5675{
25a6a873
RG
5676 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5677 varinfo_t vi, prev_vi;
c58936b6 5678 tree arg;
4ee00913
DB
5679 unsigned int i;
5680 bool is_varargs = false;
25a6a873 5681 unsigned int num_args = count_num_arguments (decl, &is_varargs);
4ee00913
DB
5682
5683 /* Create the variable info. */
5684
3781ab4b 5685 vi = new_var_info (decl, name, add_id);
4ee00913 5686 vi->offset = 0;
4ee00913 5687 vi->size = 1;
25a6a873
RG
5688 vi->fullsize = fi_parm_base + num_args;
5689 vi->is_fn_info = 1;
5690 vi->may_have_pointers = false;
5691 if (is_varargs)
5692 vi->fullsize = ~0;
3e5937d7 5693 insert_vi_for_tree (vi->decl, vi);
4ee00913 5694
25a6a873
RG
5695 prev_vi = vi;
5696
5697 /* Create a variable for things the function clobbers and one for
5698 things the function uses. */
4ee00913 5699 {
25a6a873
RG
5700 varinfo_t clobbervi, usevi;
5701 const char *newname;
5702 char *tempname;
5703
582f770b 5704 tempname = xasprintf ("%s.clobber", name);
25a6a873
RG
5705 newname = ggc_strdup (tempname);
5706 free (tempname);
5707
3781ab4b 5708 clobbervi = new_var_info (NULL, newname, false);
25a6a873
RG
5709 clobbervi->offset = fi_clobbers;
5710 clobbervi->size = 1;
5711 clobbervi->fullsize = vi->fullsize;
5712 clobbervi->is_full_var = true;
5713 clobbervi->is_global_var = false;
2ce4413c 5714
25a6a873 5715 gcc_assert (prev_vi->offset < clobbervi->offset);
d6d305fe 5716 prev_vi->next = clobbervi->id;
25a6a873 5717 prev_vi = clobbervi;
25a6a873 5718
582f770b 5719 tempname = xasprintf ("%s.use", name);
25a6a873
RG
5720 newname = ggc_strdup (tempname);
5721 free (tempname);
5722
3781ab4b 5723 usevi = new_var_info (NULL, newname, false);
25a6a873
RG
5724 usevi->offset = fi_uses;
5725 usevi->size = 1;
5726 usevi->fullsize = vi->fullsize;
5727 usevi->is_full_var = true;
5728 usevi->is_global_var = false;
2ce4413c 5729
25a6a873 5730 gcc_assert (prev_vi->offset < usevi->offset);
d6d305fe 5731 prev_vi->next = usevi->id;
25a6a873 5732 prev_vi = usevi;
4ee00913
DB
5733 }
5734
25a6a873
RG
5735 /* And one for the static chain. */
5736 if (fn->static_chain_decl != NULL_TREE)
5737 {
5738 varinfo_t chainvi;
5739 const char *newname;
5740 char *tempname;
5741
582f770b 5742 tempname = xasprintf ("%s.chain", name);
25a6a873
RG
5743 newname = ggc_strdup (tempname);
5744 free (tempname);
5745
3781ab4b 5746 chainvi = new_var_info (fn->static_chain_decl, newname, false);
25a6a873
RG
5747 chainvi->offset = fi_static_chain;
5748 chainvi->size = 1;
5749 chainvi->fullsize = vi->fullsize;
5750 chainvi->is_full_var = true;
5751 chainvi->is_global_var = false;
2ce4413c
TV
5752
5753 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5754
d0c9ca44
TV
5755 if (nonlocal_p
5756 && chainvi->may_have_pointers)
5757 make_constraint_from (chainvi, nonlocal_id);
5758
25a6a873 5759 gcc_assert (prev_vi->offset < chainvi->offset);
d6d305fe 5760 prev_vi->next = chainvi->id;
25a6a873 5761 prev_vi = chainvi;
25a6a873
RG
5762 }
5763
5764 /* Create a variable for the return var. */
5765 if (DECL_RESULT (decl) != NULL
5766 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5767 {
5768 varinfo_t resultvi;
5769 const char *newname;
5770 char *tempname;
5771 tree resultdecl = decl;
5772
5773 if (DECL_RESULT (decl))
5774 resultdecl = DECL_RESULT (decl);
5775
582f770b 5776 tempname = xasprintf ("%s.result", name);
25a6a873
RG
5777 newname = ggc_strdup (tempname);
5778 free (tempname);
5779
3781ab4b 5780 resultvi = new_var_info (resultdecl, newname, false);
25a6a873
RG
5781 resultvi->offset = fi_result;
5782 resultvi->size = 1;
5783 resultvi->fullsize = vi->fullsize;
5784 resultvi->is_full_var = true;
5785 if (DECL_RESULT (decl))
0f8d6231 5786 resultvi->may_have_pointers = true;
2ce4413c
TV
5787
5788 if (DECL_RESULT (decl))
5789 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
5790
580a4eed
RB
5791 if (nonlocal_p
5792 && DECL_RESULT (decl)
5793 && DECL_BY_REFERENCE (DECL_RESULT (decl)))
5794 make_constraint_from (resultvi, nonlocal_id);
5795
25a6a873 5796 gcc_assert (prev_vi->offset < resultvi->offset);
d6d305fe 5797 prev_vi->next = resultvi->id;
25a6a873 5798 prev_vi = resultvi;
25a6a873 5799 }
4ee00913 5800
d0c9ca44
TV
5801 /* We also need to make function return values escape. Nothing
5802 escapes by returning from main though. */
5803 if (nonlocal_p
5804 && !MAIN_NAME_P (DECL_NAME (decl)))
5805 {
5806 varinfo_t fi, rvi;
5807 fi = lookup_vi_for_tree (decl);
5808 rvi = first_vi_for_offset (fi, fi_result);
5809 if (rvi && rvi->offset == fi_result)
5810 make_copy_constraint (get_varinfo (escaped_id), rvi->id);
5811 }
5812
6416ae7f 5813 /* Set up variables for each argument. */
25a6a873
RG
5814 arg = DECL_ARGUMENTS (decl);
5815 for (i = 0; i < num_args; i++)
c58936b6 5816 {
4ee00913
DB
5817 varinfo_t argvi;
5818 const char *newname;
5819 char *tempname;
4ee00913
DB
5820 tree argdecl = decl;
5821
5822 if (arg)
5823 argdecl = arg;
c58936b6 5824
582f770b 5825 tempname = xasprintf ("%s.arg%d", name, i);
4ee00913
DB
5826 newname = ggc_strdup (tempname);
5827 free (tempname);
5828
3781ab4b 5829 argvi = new_var_info (argdecl, newname, false);
25a6a873 5830 argvi->offset = fi_parm_base + i;
4ee00913 5831 argvi->size = 1;
e5bae89b 5832 argvi->is_full_var = true;
4ee00913 5833 argvi->fullsize = vi->fullsize;
25a6a873 5834 if (arg)
0f8d6231 5835 argvi->may_have_pointers = true;
2ce4413c
TV
5836
5837 if (arg)
5838 insert_vi_for_tree (arg, argvi);
5839
d0c9ca44
TV
5840 if (nonlocal_p
5841 && argvi->may_have_pointers)
5842 make_constraint_from (argvi, nonlocal_id);
5843
25a6a873 5844 gcc_assert (prev_vi->offset < argvi->offset);
d6d305fe 5845 prev_vi->next = argvi->id;
25a6a873 5846 prev_vi = argvi;
4ee00913 5847 if (arg)
2ce4413c 5848 arg = DECL_CHAIN (arg);
4ee00913 5849 }
4cf4d6a3 5850
25a6a873
RG
5851 /* Add one representative for all further args. */
5852 if (is_varargs)
4ee00913 5853 {
25a6a873 5854 varinfo_t argvi;
4ee00913
DB
5855 const char *newname;
5856 char *tempname;
25a6a873 5857 tree decl;
c58936b6 5858
582f770b 5859 tempname = xasprintf ("%s.varargs", name);
4ee00913
DB
5860 newname = ggc_strdup (tempname);
5861 free (tempname);
5862
25a6a873 5863 /* We need sth that can be pointed to for va_start. */
7d6e2521 5864 decl = build_fake_var_decl (ptr_type_node);
25a6a873 5865
3781ab4b 5866 argvi = new_var_info (decl, newname, false);
25a6a873
RG
5867 argvi->offset = fi_parm_base + num_args;
5868 argvi->size = ~0;
5869 argvi->is_full_var = true;
5870 argvi->is_heap_var = true;
5871 argvi->fullsize = vi->fullsize;
2ce4413c 5872
d0c9ca44
TV
5873 if (nonlocal_p
5874 && argvi->may_have_pointers)
5875 make_constraint_from (argvi, nonlocal_id);
5876
25a6a873 5877 gcc_assert (prev_vi->offset < argvi->offset);
d6d305fe 5878 prev_vi->next = argvi->id;
25a6a873 5879 prev_vi = argvi;
4ee00913 5880 }
0bbf2ffa 5881
27c2cfa6 5882 return vi;
c58936b6 5883}
4ee00913 5884
6c11790d 5885
c58936b6 5886/* Return true if FIELDSTACK contains fields that overlap.
6c11790d
DB
5887 FIELDSTACK is assumed to be sorted by offset. */
5888
5889static bool
9771b263 5890check_for_overlaps (vec<fieldoff_s> fieldstack)
6c11790d
DB
5891{
5892 fieldoff_s *fo = NULL;
5893 unsigned int i;
30d2662c 5894 HOST_WIDE_INT lastoffset = -1;
6c11790d 5895
9771b263 5896 FOR_EACH_VEC_ELT (fieldstack, i, fo)
6c11790d
DB
5897 {
5898 if (fo->offset == lastoffset)
5899 return true;
5900 lastoffset = fo->offset;
5901 }
5902 return false;
5903}
21392f19 5904
910fdc79
DB
5905/* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5906 This will also create any varinfo structures necessary for fields
0b4b6ef2
TV
5907 of DECL. DECL is a function parameter if HANDLE_PARAM is set.
5908 HANDLED_STRUCT_TYPE is used to register struct types reached by following
5909 restrict pointers. This is needed to prevent infinite recursion. */
910fdc79 5910
18abb35e 5911static varinfo_t
7da5eaa0 5912create_variable_info_for_1 (tree decl, const char *name, bool add_id,
0b4b6ef2 5913 bool handle_param, bitmap handled_struct_type)
910fdc79 5914{
18abb35e 5915 varinfo_t vi, newvi;
82d6e6fc
KG
5916 tree decl_type = TREE_TYPE (decl);
5917 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
ef062b13 5918 auto_vec<fieldoff_s> fieldstack;
18abb35e
RG
5919 fieldoff_s *fo;
5920 unsigned int i;
c58936b6 5921
4ee00913 5922 if (!declsize
cc269bb6 5923 || !tree_fits_uhwi_p (declsize))
910fdc79 5924 {
3781ab4b 5925 vi = new_var_info (decl, name, add_id);
18abb35e 5926 vi->offset = 0;
910fdc79 5927 vi->size = ~0;
18abb35e
RG
5928 vi->fullsize = ~0;
5929 vi->is_unknown_size_var = true;
5930 vi->is_full_var = true;
0f8d6231 5931 vi->may_have_pointers = true;
18abb35e 5932 return vi;
910fdc79 5933 }
18abb35e
RG
5934
5935 /* Collect field information. */
5936 if (use_field_sensitive
5937 && var_can_have_subvars (decl)
27e2bd9f
RB
5938 /* ??? Force us to not use subfields for globals in IPA mode.
5939 Else we'd have to parse arbitrary initializers. */
18abb35e 5940 && !(in_ipa_mode
27e2bd9f 5941 && is_global_var (decl)))
910fdc79 5942 {
18abb35e
RG
5943 fieldoff_s *fo = NULL;
5944 bool notokay = false;
5945 unsigned int i;
5946
0f8d6231 5947 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
18abb35e 5948
9771b263 5949 for (i = 0; !notokay && fieldstack.iterate (i, &fo); i++)
18abb35e
RG
5950 if (fo->has_unknown_size
5951 || fo->offset < 0)
5952 {
5953 notokay = true;
5954 break;
5955 }
5956
5957 /* We can't sort them if we have a field with a variable sized type,
5958 which will make notokay = true. In that case, we are going to return
5959 without creating varinfos for the fields anyway, so sorting them is a
5960 waste to boot. */
5961 if (!notokay)
5962 {
5963 sort_fieldstack (fieldstack);
5964 /* Due to some C++ FE issues, like PR 22488, we might end up
5965 what appear to be overlapping fields even though they,
5966 in reality, do not overlap. Until the C++ FE is fixed,
5967 we will simply disable field-sensitivity for these cases. */
5968 notokay = check_for_overlaps (fieldstack);
5969 }
5970
5971 if (notokay)
9771b263 5972 fieldstack.release ();
18abb35e
RG
5973 }
5974
5975 /* If we didn't end up collecting sub-variables create a full
5976 variable for the decl. */
50b4b446 5977 if (fieldstack.length () == 0
9771b263 5978 || fieldstack.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
18abb35e 5979 {
3781ab4b 5980 vi = new_var_info (decl, name, add_id);
18abb35e 5981 vi->offset = 0;
0f8d6231 5982 vi->may_have_pointers = true;
eb1ce453 5983 vi->fullsize = tree_to_uhwi (declsize);
910fdc79 5984 vi->size = vi->fullsize;
18abb35e 5985 vi->is_full_var = true;
7b0a0ee2
TV
5986 if (POINTER_TYPE_P (decl_type)
5987 && TYPE_RESTRICT (decl_type))
8013b816 5988 vi->only_restrict_pointers = 1;
7da5eaa0
TV
5989 if (vi->only_restrict_pointers
5990 && !type_contains_placeholder_p (TREE_TYPE (decl_type))
0b4b6ef2
TV
5991 && handle_param
5992 && !bitmap_bit_p (handled_struct_type,
5993 TYPE_UID (TREE_TYPE (decl_type))))
7da5eaa0
TV
5994 {
5995 varinfo_t rvi;
5996 tree heapvar = build_fake_var_decl (TREE_TYPE (decl_type));
5997 DECL_EXTERNAL (heapvar) = 1;
0b4b6ef2
TV
5998 if (var_can_have_subvars (heapvar))
5999 bitmap_set_bit (handled_struct_type,
6000 TYPE_UID (TREE_TYPE (decl_type)));
7da5eaa0 6001 rvi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS", true,
0b4b6ef2
TV
6002 true, handled_struct_type);
6003 if (var_can_have_subvars (heapvar))
6004 bitmap_clear_bit (handled_struct_type,
6005 TYPE_UID (TREE_TYPE (decl_type)));
7da5eaa0
TV
6006 rvi->is_restrict_var = 1;
6007 insert_vi_for_tree (heapvar, rvi);
6008 make_constraint_from (vi, rvi->id);
6009 make_param_constraints (rvi);
6010 }
9771b263 6011 fieldstack.release ();
18abb35e 6012 return vi;
910fdc79 6013 }
c58936b6 6014
3781ab4b 6015 vi = new_var_info (decl, name, add_id);
eb1ce453 6016 vi->fullsize = tree_to_uhwi (declsize);
498dbe0a
TV
6017 if (fieldstack.length () == 1)
6018 vi->is_full_var = true;
18abb35e 6019 for (i = 0, newvi = vi;
9771b263 6020 fieldstack.iterate (i, &fo);
d6d305fe 6021 ++i, newvi = vi_next (newvi))
18abb35e 6022 {
50b4b446 6023 const char *newname = NULL;
18abb35e
RG
6024 char *tempname;
6025
6026 if (dump_file)
6027 {
50b4b446
TV
6028 if (fieldstack.length () != 1)
6029 {
6030 tempname
6031 = xasprintf ("%s." HOST_WIDE_INT_PRINT_DEC
6032 "+" HOST_WIDE_INT_PRINT_DEC, name,
6033 fo->offset, fo->size);
6034 newname = ggc_strdup (tempname);
6035 free (tempname);
6036 }
18abb35e 6037 }
50b4b446
TV
6038 else
6039 newname = "NULL";
6040
6041 if (newname)
6042 newvi->name = newname;
18abb35e
RG
6043 newvi->offset = fo->offset;
6044 newvi->size = fo->size;
6045 newvi->fullsize = vi->fullsize;
6046 newvi->may_have_pointers = fo->may_have_pointers;
6047 newvi->only_restrict_pointers = fo->only_restrict_pointers;
7da5eaa0
TV
6048 if (handle_param
6049 && newvi->only_restrict_pointers
0b4b6ef2
TV
6050 && !type_contains_placeholder_p (fo->restrict_pointed_type)
6051 && !bitmap_bit_p (handled_struct_type,
6052 TYPE_UID (fo->restrict_pointed_type)))
7da5eaa0
TV
6053 {
6054 varinfo_t rvi;
6055 tree heapvar = build_fake_var_decl (fo->restrict_pointed_type);
6056 DECL_EXTERNAL (heapvar) = 1;
0b4b6ef2
TV
6057 if (var_can_have_subvars (heapvar))
6058 bitmap_set_bit (handled_struct_type,
6059 TYPE_UID (fo->restrict_pointed_type));
7da5eaa0 6060 rvi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS", true,
0b4b6ef2
TV
6061 true, handled_struct_type);
6062 if (var_can_have_subvars (heapvar))
6063 bitmap_clear_bit (handled_struct_type,
6064 TYPE_UID (fo->restrict_pointed_type));
7da5eaa0
TV
6065 rvi->is_restrict_var = 1;
6066 insert_vi_for_tree (heapvar, rvi);
6067 make_constraint_from (newvi, rvi->id);
6068 make_param_constraints (rvi);
6069 }
9771b263 6070 if (i + 1 < fieldstack.length ())
d6d305fe 6071 {
3781ab4b 6072 varinfo_t tem = new_var_info (decl, name, false);
d6d305fe
RB
6073 newvi->next = tem->id;
6074 tem->head = vi->id;
6075 }
18abb35e
RG
6076 }
6077
18abb35e
RG
6078 return vi;
6079}
6080
6081static unsigned int
3781ab4b 6082create_variable_info_for (tree decl, const char *name, bool add_id)
18abb35e 6083{
0b4b6ef2 6084 varinfo_t vi = create_variable_info_for_1 (decl, name, add_id, false, NULL);
18abb35e
RG
6085 unsigned int id = vi->id;
6086
6087 insert_vi_for_tree (decl, vi);
6088
8813a647 6089 if (!VAR_P (decl))
1565af08
RG
6090 return id;
6091
18abb35e 6092 /* Create initial constraints for globals. */
d6d305fe 6093 for (; vi; vi = vi_next (vi))
13c6bff4 6094 {
18abb35e
RG
6095 if (!vi->may_have_pointers
6096 || !vi->is_global_var)
6097 continue;
6098
25a6a873 6099 /* Mark global restrict qualified pointers. */
18abb35e
RG
6100 if ((POINTER_TYPE_P (TREE_TYPE (decl))
6101 && TYPE_RESTRICT (TREE_TYPE (decl)))
6102 || vi->only_restrict_pointers)
d3553615 6103 {
aa098165 6104 varinfo_t rvi
3781ab4b
TV
6105 = make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT",
6106 true);
aa098165
RB
6107 /* ??? For now exclude reads from globals as restrict sources
6108 if those are not (indirectly) from incoming parameters. */
6109 rvi->is_restrict_var = false;
d3553615
RG
6110 continue;
6111 }
25a6a873 6112
1565af08 6113 /* In non-IPA mode the initializer from nonlocal is all we need. */
25a6a873 6114 if (!in_ipa_mode
1565af08 6115 || DECL_HARD_REGISTER (decl))
25a6a873
RG
6116 make_copy_constraint (vi, nonlocal_id);
6117
d3553615
RG
6118 /* In IPA mode parse the initializer and generate proper constraints
6119 for it. */
1565af08 6120 else
25a6a873 6121 {
9041d2e6 6122 varpool_node *vnode = varpool_node::get (decl);
1565af08
RG
6123
6124 /* For escaped variables initialize them from nonlocal. */
9041d2e6 6125 if (!vnode->all_refs_explicit_p ())
1565af08
RG
6126 make_copy_constraint (vi, nonlocal_id);
6127
6128 /* If this is a global variable with an initializer and we are in
6129 IPA mode generate constraints for it. */
27e2bd9f
RB
6130 ipa_ref *ref;
6131 for (unsigned idx = 0; vnode->iterate_reference (idx, ref); ++idx)
25a6a873 6132 {
ef062b13 6133 auto_vec<ce_s> rhsc;
1565af08
RG
6134 struct constraint_expr lhs, *rhsp;
6135 unsigned i;
27e2bd9f 6136 get_constraint_for_address_of (ref->referred->decl, &rhsc);
1565af08 6137 lhs.var = vi->id;
25a6a873
RG
6138 lhs.offset = 0;
6139 lhs.type = SCALAR;
9771b263 6140 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
25a6a873 6141 process_constraint (new_constraint (lhs, *rhsp));
1565af08
RG
6142 /* If this is a variable that escapes from the unit
6143 the initializer escapes as well. */
9041d2e6 6144 if (!vnode->all_refs_explicit_p ())
1565af08
RG
6145 {
6146 lhs.var = escaped_id;
6147 lhs.offset = 0;
6148 lhs.type = SCALAR;
9771b263 6149 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
1565af08
RG
6150 process_constraint (new_constraint (lhs, *rhsp));
6151 }
25a6a873 6152 }
25a6a873 6153 }
13c6bff4 6154 }
910fdc79 6155
18abb35e 6156 return id;
910fdc79
DB
6157}
6158
6159/* Print out the points-to solution for VAR to FILE. */
6160
5006671f 6161static void
910fdc79
DB
6162dump_solution_for_var (FILE *file, unsigned int var)
6163{
6164 varinfo_t vi = get_varinfo (var);
6165 unsigned int i;
c58936b6
DB
6166 bitmap_iterator bi;
6167
25a6a873
RG
6168 /* Dump the solution for unified vars anyway, this avoids difficulties
6169 in scanning dumps in the testsuite. */
6170 fprintf (file, "%s = { ", vi->name);
6171 vi = get_varinfo (find (var));
6172 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
6173 fprintf (file, "%s ", get_varinfo (i)->name);
6174 fprintf (file, "}");
6175
6176 /* But note when the variable was unified. */
6177 if (vi->id != var)
6178 fprintf (file, " same as %s", vi->name);
6179
6180 fprintf (file, "\n");
910fdc79
DB
6181}
6182
3d224d46 6183/* Print the points-to solution for VAR to stderr. */
910fdc79 6184
24e47c76 6185DEBUG_FUNCTION void
910fdc79
DB
6186debug_solution_for_var (unsigned int var)
6187{
3d224d46 6188 dump_solution_for_var (stderr, var);
910fdc79
DB
6189}
6190
7da5eaa0 6191/* Register the constraints for function parameter related VI. */
930e85a3
TV
6192
6193static void
7da5eaa0 6194make_param_constraints (varinfo_t vi)
930e85a3
TV
6195{
6196 for (; vi; vi = vi_next (vi))
58b2d87e
TV
6197 {
6198 if (vi->only_restrict_pointers)
7da5eaa0 6199 ;
58b2d87e
TV
6200 else if (vi->may_have_pointers)
6201 make_constraint_from (vi, nonlocal_id);
6202
6203 if (vi->is_full_var)
6204 break;
6205 }
930e85a3
TV
6206}
6207
910fdc79
DB
6208/* Create varinfo structures for all of the variables in the
6209 function for intraprocedural mode. */
6210
6211static void
628169e0 6212intra_create_variable_infos (struct function *fn)
910fdc79
DB
6213{
6214 tree t;
0b4b6ef2 6215 bitmap handled_struct_type = NULL;
b23987ec 6216
6e7e772d 6217 /* For each incoming pointer argument arg, create the constraint ARG
0d3c82d6
RG
6218 = NONLOCAL or a dummy variable if it is a restrict qualified
6219 passed-by-reference argument. */
628169e0 6220 for (t = DECL_ARGUMENTS (fn->decl); t; t = DECL_CHAIN (t))
910fdc79 6221 {
0b4b6ef2
TV
6222 if (handled_struct_type == NULL)
6223 handled_struct_type = BITMAP_ALLOC (NULL);
6224
7da5eaa0 6225 varinfo_t p
0b4b6ef2
TV
6226 = create_variable_info_for_1 (t, alias_get_name (t), false, true,
6227 handled_struct_type);
82d2c270 6228 insert_vi_for_tree (t, p);
c58936b6 6229
7da5eaa0 6230 make_param_constraints (p);
21392f19 6231 }
75af9746 6232
0b4b6ef2
TV
6233 if (handled_struct_type != NULL)
6234 BITMAP_FREE (handled_struct_type);
6235
10bd6c5c 6236 /* Add a constraint for a result decl that is passed by reference. */
628169e0
RB
6237 if (DECL_RESULT (fn->decl)
6238 && DECL_BY_REFERENCE (DECL_RESULT (fn->decl)))
10bd6c5c 6239 {
628169e0 6240 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (fn->decl));
10bd6c5c 6241
d6d305fe 6242 for (p = result_vi; p; p = vi_next (p))
5006671f 6243 make_constraint_from (p, nonlocal_id);
10bd6c5c
RG
6244 }
6245
75af9746 6246 /* Add a constraint for the incoming static chain parameter. */
628169e0 6247 if (fn->static_chain_decl != NULL_TREE)
75af9746 6248 {
628169e0 6249 varinfo_t p, chain_vi = get_vi_for_tree (fn->static_chain_decl);
75af9746 6250
d6d305fe 6251 for (p = chain_vi; p; p = vi_next (p))
75af9746
RG
6252 make_constraint_from (p, nonlocal_id);
6253 }
910fdc79
DB
6254}
6255
1296c31f
DB
6256/* Structure used to put solution bitmaps in a hashtable so they can
6257 be shared among variables with the same points-to set. */
6258
6259typedef struct shared_bitmap_info
6260{
6261 bitmap pt_vars;
6262 hashval_t hashcode;
6263} *shared_bitmap_info_t;
e5cfc29f 6264typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
1296c31f 6265
bf190e8d
LC
6266/* Shared_bitmap hashtable helpers. */
6267
95fbe13e 6268struct shared_bitmap_hasher : free_ptr_hash <shared_bitmap_info>
bf190e8d 6269{
67f58944
TS
6270 static inline hashval_t hash (const shared_bitmap_info *);
6271 static inline bool equal (const shared_bitmap_info *,
6272 const shared_bitmap_info *);
bf190e8d 6273};
1296c31f
DB
6274
6275/* Hash function for a shared_bitmap_info_t */
6276
bf190e8d 6277inline hashval_t
67f58944 6278shared_bitmap_hasher::hash (const shared_bitmap_info *bi)
1296c31f 6279{
1296c31f
DB
6280 return bi->hashcode;
6281}
6282
6283/* Equality function for two shared_bitmap_info_t's. */
6284
bf190e8d 6285inline bool
67f58944
TS
6286shared_bitmap_hasher::equal (const shared_bitmap_info *sbi1,
6287 const shared_bitmap_info *sbi2)
1296c31f 6288{
1296c31f
DB
6289 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
6290}
6291
bf190e8d
LC
6292/* Shared_bitmap hashtable. */
6293
c203e8a7 6294static hash_table<shared_bitmap_hasher> *shared_bitmap_table;
bf190e8d 6295
1296c31f
DB
6296/* Lookup a bitmap in the shared bitmap hashtable, and return an already
6297 existing instance if there is one, NULL otherwise. */
6298
6299static bitmap
6300shared_bitmap_lookup (bitmap pt_vars)
6301{
bf190e8d 6302 shared_bitmap_info **slot;
1296c31f
DB
6303 struct shared_bitmap_info sbi;
6304
6305 sbi.pt_vars = pt_vars;
6306 sbi.hashcode = bitmap_hash (pt_vars);
7b765bed 6307
c203e8a7 6308 slot = shared_bitmap_table->find_slot (&sbi, NO_INSERT);
1296c31f
DB
6309 if (!slot)
6310 return NULL;
6311 else
bf190e8d 6312 return (*slot)->pt_vars;
1296c31f
DB
6313}
6314
6315
6316/* Add a bitmap to the shared bitmap hashtable. */
6317
6318static void
6319shared_bitmap_add (bitmap pt_vars)
6320{
bf190e8d 6321 shared_bitmap_info **slot;
1296c31f 6322 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
7b765bed 6323
1296c31f
DB
6324 sbi->pt_vars = pt_vars;
6325 sbi->hashcode = bitmap_hash (pt_vars);
7b765bed 6326
c203e8a7 6327 slot = shared_bitmap_table->find_slot (sbi, INSERT);
1296c31f 6328 gcc_assert (!*slot);
bf190e8d 6329 *slot = sbi;
1296c31f
DB
6330}
6331
6332
4d7a65ea 6333/* Set bits in INTO corresponding to the variable uids in solution set FROM. */
910fdc79 6334
b8698a0f 6335static void
ee7d29b4
RB
6336set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt,
6337 tree fndecl)
910fdc79
DB
6338{
6339 unsigned int i;
6340 bitmap_iterator bi;
11924f8b
RB
6341 varinfo_t escaped_vi = get_varinfo (find (escaped_id));
6342 bool everything_escaped
6343 = escaped_vi->solution && bitmap_bit_p (escaped_vi->solution, anything_id);
f83ca251 6344
910fdc79
DB
6345 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
6346 {
6347 varinfo_t vi = get_varinfo (i);
c58936b6 6348
e8ca4159
DN
6349 /* The only artificial variables that are allowed in a may-alias
6350 set are heap variables. */
6351 if (vi->is_artificial_var && !vi->is_heap_var)
6352 continue;
c58936b6 6353
11924f8b
RB
6354 if (everything_escaped
6355 || (escaped_vi->solution
6356 && bitmap_bit_p (escaped_vi->solution, i)))
6357 {
6358 pt->vars_contains_escaped = true;
6359 pt->vars_contains_escaped_heap = vi->is_heap_var;
6360 }
6361
763baff6
RB
6362 if (vi->is_restrict_var)
6363 pt->vars_contains_restrict = true;
6364
8813a647 6365 if (VAR_P (vi->decl)
5611cf0b
RG
6366 || TREE_CODE (vi->decl) == PARM_DECL
6367 || TREE_CODE (vi->decl) == RESULT_DECL)
58b82d2b 6368 {
25a6a873
RG
6369 /* If we are in IPA mode we will not recompute points-to
6370 sets after inlining so make sure they stay valid. */
6371 if (in_ipa_mode
6372 && !DECL_PT_UID_SET_P (vi->decl))
6373 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
6374
5006671f
RG
6375 /* Add the decl to the points-to set. Note that the points-to
6376 set contains global variables. */
25a6a873 6377 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
ee7d29b4
RB
6378 if (vi->is_global_var
6379 /* In IPA mode the escaped_heap trick doesn't work as
6380 ESCAPED is escaped from the unit but
6381 pt_solution_includes_global needs to answer true for
6382 all variables not automatic within a function.
6383 For the same reason is_global_var is not the
6384 correct flag to track - local variables from other
6385 functions also need to be considered global.
6386 Conveniently all HEAP vars are not put in function
6387 scope. */
6388 || (in_ipa_mode
6389 && fndecl
6390 && ! auto_var_in_fn_p (vi->decl, fndecl)))
11924f8b 6391 pt->vars_contains_nonlocal = true;
da42ac7b
RB
6392
6393 /* If we have a variable that is interposable record that fact
6394 for pointer comparison simplification. */
6395 if (VAR_P (vi->decl)
6396 && (TREE_STATIC (vi->decl) || DECL_EXTERNAL (vi->decl))
6397 && ! decl_binds_to_current_def_p (vi->decl))
6398 pt->vars_contains_interposable = true;
e8ca4159 6399 }
3a81a594
RB
6400
6401 else if (TREE_CODE (vi->decl) == FUNCTION_DECL
6402 || TREE_CODE (vi->decl) == LABEL_DECL)
6403 {
6404 /* Nothing should read/write from/to code so we can
6405 save bits by not including them in the points-to bitmaps.
6406 Still mark the points-to set as containing global memory
6407 to make code-patching possible - see PR70128. */
6408 pt->vars_contains_nonlocal = true;
6409 }
910fdc79
DB
6410 }
6411}
e8ca4159
DN
6412
6413
4d7a65ea 6414/* Compute the points-to solution *PT for the variable VI. */
ce1b6498 6415
d394a308 6416static struct pt_solution
ee7d29b4 6417find_what_var_points_to (tree fndecl, varinfo_t orig_vi)
ce1b6498 6418{
4d7a65ea 6419 unsigned int i;
5006671f
RG
6420 bitmap_iterator bi;
6421 bitmap finished_solution;
6422 bitmap result;
1cfd38be 6423 varinfo_t vi;
d394a308 6424 struct pt_solution *pt;
5006671f
RG
6425
6426 /* This variable may have been collapsed, let's get the real
6427 variable. */
1cfd38be 6428 vi = get_varinfo (find (orig_vi->id));
5006671f 6429
d394a308 6430 /* See if we have already computed the solution and return it. */
b787e7a2 6431 pt_solution **slot = &final_solutions->get_or_insert (vi);
d394a308 6432 if (*slot != NULL)
b787e7a2 6433 return **slot;
d394a308
RB
6434
6435 *slot = pt = XOBNEW (&final_solutions_obstack, struct pt_solution);
6436 memset (pt, 0, sizeof (struct pt_solution));
6437
5006671f
RG
6438 /* Translate artificial variables into SSA_NAME_PTR_INFO
6439 attributes. */
6440 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
6441 {
6442 varinfo_t vi = get_varinfo (i);
6443
6444 if (vi->is_artificial_var)
6445 {
6446 if (vi->id == nothing_id)
6447 pt->null = 1;
6448 else if (vi->id == escaped_id)
25a6a873
RG
6449 {
6450 if (in_ipa_mode)
6451 pt->ipa_escaped = 1;
6452 else
6453 pt->escaped = 1;
e8e938e0
RB
6454 /* Expand some special vars of ESCAPED in-place here. */
6455 varinfo_t evi = get_varinfo (find (escaped_id));
6456 if (bitmap_bit_p (evi->solution, nonlocal_id))
6457 pt->nonlocal = 1;
25a6a873 6458 }
5006671f
RG
6459 else if (vi->id == nonlocal_id)
6460 pt->nonlocal = 1;
6461 else if (vi->is_heap_var)
6462 /* We represent heapvars in the points-to set properly. */
6463 ;
ebd7d910
RB
6464 else if (vi->id == string_id)
6465 /* Nobody cares - STRING_CSTs are read-only entities. */
91deb937 6466 ;
5006671f 6467 else if (vi->id == anything_id
5006671f
RG
6468 || vi->id == integer_id)
6469 pt->anything = 1;
6470 }
6471 }
6472
6473 /* Instead of doing extra work, simply do not create
6474 elaborate points-to information for pt_anything pointers. */
d3553615 6475 if (pt->anything)
d394a308 6476 return *pt;
5006671f
RG
6477
6478 /* Share the final set of variables when possible. */
6479 finished_solution = BITMAP_GGC_ALLOC ();
6480 stats.points_to_sets_created++;
6481
ee7d29b4 6482 set_uids_in_ptset (finished_solution, vi->solution, pt, fndecl);
5006671f
RG
6483 result = shared_bitmap_lookup (finished_solution);
6484 if (!result)
6485 {
6486 shared_bitmap_add (finished_solution);
6487 pt->vars = finished_solution;
6488 }
6489 else
6490 {
6491 pt->vars = result;
6492 bitmap_clear (finished_solution);
6493 }
d394a308
RB
6494
6495 return *pt;
5006671f
RG
6496}
6497
4d7a65ea 6498/* Given a pointer variable P, fill in its points-to set. */
5006671f
RG
6499
6500static void
ee7d29b4 6501find_what_p_points_to (tree fndecl, tree p)
5006671f
RG
6502{
6503 struct ptr_info_def *pi;
7cc92f92 6504 tree lookup_p = p;
3e5937d7 6505 varinfo_t vi;
735b8f9f 6506 bool nonnull = get_ptr_nonnull (p);
e8ca4159 6507
7cc92f92
RG
6508 /* For parameters, get at the points-to set for the actual parm
6509 decl. */
c58936b6 6510 if (TREE_CODE (p) == SSA_NAME
67386041 6511 && SSA_NAME_IS_DEFAULT_DEF (p)
6938f93f 6512 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
67386041 6513 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL))
7cc92f92
RG
6514 lookup_p = SSA_NAME_VAR (p);
6515
15814ba0 6516 vi = lookup_vi_for_tree (lookup_p);
5006671f
RG
6517 if (!vi)
6518 return;
6519
6520 pi = get_ptr_info (p);
ee7d29b4 6521 pi->pt = find_what_var_points_to (fndecl, vi);
735b8f9f
KV
6522 /* Conservatively set to NULL from PTA (to true). */
6523 pi->pt.null = 1;
6524 /* Preserve pointer nonnull computed by VRP. See get_ptr_nonnull
6525 in gcc/tree-ssaname.c for more information. */
6526 if (nonnull)
6527 set_ptr_nonnull (p);
5006671f 6528}
7b765bed 6529
910fdc79 6530
5006671f 6531/* Query statistics for points-to solutions. */
c58936b6 6532
5006671f
RG
6533static struct {
6534 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
6535 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
6536 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
6537 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
6538} pta_stats;
e8ca4159 6539
5006671f
RG
6540void
6541dump_pta_stats (FILE *s)
6542{
6543 fprintf (s, "\nPTA query stats:\n");
6544 fprintf (s, " pt_solution_includes: "
6545 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6546 HOST_WIDE_INT_PRINT_DEC" queries\n",
6547 pta_stats.pt_solution_includes_no_alias,
6548 pta_stats.pt_solution_includes_no_alias
6549 + pta_stats.pt_solution_includes_may_alias);
6550 fprintf (s, " pt_solutions_intersect: "
6551 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6552 HOST_WIDE_INT_PRINT_DEC" queries\n",
6553 pta_stats.pt_solutions_intersect_no_alias,
6554 pta_stats.pt_solutions_intersect_no_alias
6555 + pta_stats.pt_solutions_intersect_may_alias);
6556}
e8ca4159 6557
9f09b13f 6558
5006671f
RG
6559/* Reset the points-to solution *PT to a conservative default
6560 (point to anything). */
7b765bed 6561
5006671f
RG
6562void
6563pt_solution_reset (struct pt_solution *pt)
6564{
6565 memset (pt, 0, sizeof (struct pt_solution));
6566 pt->anything = true;
735b8f9f 6567 pt->null = true;
5006671f 6568}
1296c31f 6569
55b34b5f 6570/* Set the points-to solution *PT to point only to the variables
25a6a873
RG
6571 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6572 global variables and VARS_CONTAINS_RESTRICT specifies whether
6573 it contains restrict tag variables. */
55b34b5f
RG
6574
6575void
11924f8b
RB
6576pt_solution_set (struct pt_solution *pt, bitmap vars,
6577 bool vars_contains_nonlocal)
55b34b5f 6578{
55b34b5f
RG
6579 memset (pt, 0, sizeof (struct pt_solution));
6580 pt->vars = vars;
11924f8b
RB
6581 pt->vars_contains_nonlocal = vars_contains_nonlocal;
6582 pt->vars_contains_escaped
6583 = (cfun->gimple_df->escaped.anything
6584 || bitmap_intersect_p (cfun->gimple_df->escaped.vars, vars));
25a6a873
RG
6585}
6586
90fa9e17
RG
6587/* Set the points-to solution *PT to point only to the variable VAR. */
6588
6589void
6590pt_solution_set_var (struct pt_solution *pt, tree var)
6591{
6592 memset (pt, 0, sizeof (struct pt_solution));
6593 pt->vars = BITMAP_GGC_ALLOC ();
1b2bb171 6594 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
11924f8b
RB
6595 pt->vars_contains_nonlocal = is_global_var (var);
6596 pt->vars_contains_escaped
6597 = (cfun->gimple_df->escaped.anything
6598 || bitmap_bit_p (cfun->gimple_df->escaped.vars, DECL_PT_UID (var)));
90fa9e17
RG
6599}
6600
25a6a873
RG
6601/* Computes the union of the points-to solutions *DEST and *SRC and
6602 stores the result in *DEST. This changes the points-to bitmap
6603 of *DEST and thus may not be used if that might be shared.
6604 The points-to bitmap of *SRC and *DEST will not be shared after
6605 this function if they were not before. */
6606
6607static void
6608pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6609{
6610 dest->anything |= src->anything;
6611 if (dest->anything)
55b34b5f 6612 {
25a6a873
RG
6613 pt_solution_reset (dest);
6614 return;
55b34b5f 6615 }
25a6a873
RG
6616
6617 dest->nonlocal |= src->nonlocal;
6618 dest->escaped |= src->escaped;
6619 dest->ipa_escaped |= src->ipa_escaped;
6620 dest->null |= src->null;
11924f8b
RB
6621 dest->vars_contains_nonlocal |= src->vars_contains_nonlocal;
6622 dest->vars_contains_escaped |= src->vars_contains_escaped;
6623 dest->vars_contains_escaped_heap |= src->vars_contains_escaped_heap;
25a6a873
RG
6624 if (!src->vars)
6625 return;
6626
6627 if (!dest->vars)
6628 dest->vars = BITMAP_GGC_ALLOC ();
6629 bitmap_ior_into (dest->vars, src->vars);
55b34b5f
RG
6630}
6631
5006671f 6632/* Return true if the points-to solution *PT is empty. */
e8ca4159 6633
25a6a873 6634bool
5006671f
RG
6635pt_solution_empty_p (struct pt_solution *pt)
6636{
6637 if (pt->anything
6638 || pt->nonlocal)
6639 return false;
e8ca4159 6640
5006671f
RG
6641 if (pt->vars
6642 && !bitmap_empty_p (pt->vars))
6643 return false;
e8ca4159 6644
5006671f
RG
6645 /* If the solution includes ESCAPED, check if that is empty. */
6646 if (pt->escaped
6647 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6648 return false;
6649
25a6a873
RG
6650 /* If the solution includes ESCAPED, check if that is empty. */
6651 if (pt->ipa_escaped
6652 && !pt_solution_empty_p (&ipa_escaped_pt))
6653 return false;
6654
5006671f 6655 return true;
910fdc79
DB
6656}
6657
703ffc30
TV
6658/* Return true if the points-to solution *PT only point to a single var, and
6659 return the var uid in *UID. */
6660
6661bool
735b8f9f 6662pt_solution_singleton_or_null_p (struct pt_solution *pt, unsigned *uid)
703ffc30
TV
6663{
6664 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
735b8f9f 6665 || pt->vars == NULL
703ffc30
TV
6666 || !bitmap_single_bit_set_p (pt->vars))
6667 return false;
6668
6669 *uid = bitmap_first_set_bit (pt->vars);
6670 return true;
6671}
6672
5006671f 6673/* Return true if the points-to solution *PT includes global memory. */
63a4ef6f 6674
2f571334 6675bool
5006671f 6676pt_solution_includes_global (struct pt_solution *pt)
2f571334 6677{
5006671f
RG
6678 if (pt->anything
6679 || pt->nonlocal
11924f8b
RB
6680 || pt->vars_contains_nonlocal
6681 /* The following is a hack to make the malloc escape hack work.
6682 In reality we'd need different sets for escaped-through-return
6683 and escaped-to-callees and passes would need to be updated. */
6684 || pt->vars_contains_escaped_heap)
5006671f 6685 return true;
2f571334 6686
11924f8b 6687 /* 'escaped' is also a placeholder so we have to look into it. */
5006671f
RG
6688 if (pt->escaped)
6689 return pt_solution_includes_global (&cfun->gimple_df->escaped);
2f571334 6690
25a6a873
RG
6691 if (pt->ipa_escaped)
6692 return pt_solution_includes_global (&ipa_escaped_pt);
6693
5006671f
RG
6694 return false;
6695}
2f571334 6696
5006671f
RG
6697/* Return true if the points-to solution *PT includes the variable
6698 declaration DECL. */
15c15196 6699
5006671f
RG
6700static bool
6701pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6702{
6703 if (pt->anything)
6704 return true;
2f571334 6705
5006671f
RG
6706 if (pt->nonlocal
6707 && is_global_var (decl))
6708 return true;
2f571334 6709
5006671f 6710 if (pt->vars
25a6a873 6711 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
5006671f 6712 return true;
2f571334 6713
5006671f
RG
6714 /* If the solution includes ESCAPED, check it. */
6715 if (pt->escaped
6716 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6717 return true;
2f571334 6718
25a6a873
RG
6719 /* If the solution includes ESCAPED, check it. */
6720 if (pt->ipa_escaped
6721 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6722 return true;
6723
5006671f 6724 return false;
2f571334 6725}
910fdc79 6726
5006671f
RG
6727bool
6728pt_solution_includes (struct pt_solution *pt, const_tree decl)
15c15196 6729{
5006671f
RG
6730 bool res = pt_solution_includes_1 (pt, decl);
6731 if (res)
6732 ++pta_stats.pt_solution_includes_may_alias;
6733 else
6734 ++pta_stats.pt_solution_includes_no_alias;
6735 return res;
6736}
15c15196 6737
5006671f
RG
6738/* Return true if both points-to solutions PT1 and PT2 have a non-empty
6739 intersection. */
15c15196 6740
5006671f
RG
6741static bool
6742pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
6743{
6744 if (pt1->anything || pt2->anything)
6745 return true;
15c15196 6746
5006671f
RG
6747 /* If either points to unknown global memory and the other points to
6748 any global memory they alias. */
6749 if ((pt1->nonlocal
6750 && (pt2->nonlocal
11924f8b 6751 || pt2->vars_contains_nonlocal))
5006671f 6752 || (pt2->nonlocal
11924f8b 6753 && pt1->vars_contains_nonlocal))
5006671f 6754 return true;
15c15196 6755
11924f8b
RB
6756 /* If either points to all escaped memory and the other points to
6757 any escaped memory they alias. */
6758 if ((pt1->escaped
6759 && (pt2->escaped
6760 || pt2->vars_contains_escaped))
6761 || (pt2->escaped
6762 && pt1->vars_contains_escaped))
6763 return true;
15c15196 6764
25a6a873
RG
6765 /* Check the escaped solution if required.
6766 ??? Do we need to check the local against the IPA escaped sets? */
6767 if ((pt1->ipa_escaped || pt2->ipa_escaped)
6768 && !pt_solution_empty_p (&ipa_escaped_pt))
6769 {
6770 /* If both point to escaped memory and that solution
6771 is not empty they alias. */
6772 if (pt1->ipa_escaped && pt2->ipa_escaped)
6773 return true;
6774
6775 /* If either points to escaped memory see if the escaped solution
6776 intersects with the other. */
6777 if ((pt1->ipa_escaped
6778 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
6779 || (pt2->ipa_escaped
6780 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
6781 return true;
6782 }
6783
5006671f
RG
6784 /* Now both pointers alias if their points-to solution intersects. */
6785 return (pt1->vars
6786 && pt2->vars
6787 && bitmap_intersect_p (pt1->vars, pt2->vars));
6788}
6789
6790bool
6791pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
6792{
6793 bool res = pt_solutions_intersect_1 (pt1, pt2);
6794 if (res)
6795 ++pta_stats.pt_solutions_intersect_may_alias;
6796 else
6797 ++pta_stats.pt_solutions_intersect_no_alias;
6798 return res;
15c15196
RG
6799}
6800
b7091901 6801
63a4ef6f
DN
6802/* Dump points-to information to OUTFILE. */
6803
5006671f 6804static void
910fdc79
DB
6805dump_sa_points_to_info (FILE *outfile)
6806{
910fdc79 6807 unsigned int i;
63a4ef6f 6808
e8ca4159 6809 fprintf (outfile, "\nPoints-to sets\n\n");
63a4ef6f 6810
910fdc79
DB
6811 if (dump_flags & TDF_STATS)
6812 {
6813 fprintf (outfile, "Stats:\n");
63a4ef6f 6814 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
3e5937d7
DB
6815 fprintf (outfile, "Non-pointer vars: %d\n",
6816 stats.nonpointer_vars);
63a4ef6f
DN
6817 fprintf (outfile, "Statically unified vars: %d\n",
6818 stats.unified_vars_static);
63a4ef6f
DN
6819 fprintf (outfile, "Dynamically unified vars: %d\n",
6820 stats.unified_vars_dynamic);
6821 fprintf (outfile, "Iterations: %d\n", stats.iterations);
4ee00913 6822 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
3e5937d7
DB
6823 fprintf (outfile, "Number of implicit edges: %d\n",
6824 stats.num_implicit_edges);
910fdc79 6825 }
63a4ef6f 6826
d6d305fe 6827 for (i = 1; i < varmap.length (); i++)
25a6a873
RG
6828 {
6829 varinfo_t vi = get_varinfo (i);
6830 if (!vi->may_have_pointers)
b28ae58f 6831 continue;
25a6a873
RG
6832 dump_solution_for_var (outfile, i);
6833 }
910fdc79
DB
6834}
6835
6836
63a4ef6f
DN
6837/* Debug points-to information to stderr. */
6838
24e47c76 6839DEBUG_FUNCTION void
63a4ef6f
DN
6840debug_sa_points_to_info (void)
6841{
6842 dump_sa_points_to_info (stderr);
6843}
6844
6845
910fdc79
DB
6846/* Initialize the always-existing constraint variables for NULL
6847 ANYTHING, READONLY, and INTEGER */
6848
6849static void
6850init_base_vars (void)
6851{
6852 struct constraint_expr lhs, rhs;
0bbf2ffa
RG
6853 varinfo_t var_anything;
6854 varinfo_t var_nothing;
ebd7d910 6855 varinfo_t var_string;
0bbf2ffa
RG
6856 varinfo_t var_escaped;
6857 varinfo_t var_nonlocal;
0bbf2ffa
RG
6858 varinfo_t var_storedanything;
6859 varinfo_t var_integer;
910fdc79 6860
d6d305fe
RB
6861 /* Variable ID zero is reserved and should be NULL. */
6862 varmap.safe_push (NULL);
6863
910fdc79
DB
6864 /* Create the NULL variable, used to represent that a variable points
6865 to NULL. */
3781ab4b 6866 var_nothing = new_var_info (NULL_TREE, "NULL", false);
0bbf2ffa 6867 gcc_assert (var_nothing->id == nothing_id);
910fdc79
DB
6868 var_nothing->is_artificial_var = 1;
6869 var_nothing->offset = 0;
6870 var_nothing->size = ~0;
6871 var_nothing->fullsize = ~0;
13c2c08b 6872 var_nothing->is_special_var = 1;
b28ae58f
RG
6873 var_nothing->may_have_pointers = 0;
6874 var_nothing->is_global_var = 0;
910fdc79
DB
6875
6876 /* Create the ANYTHING variable, used to represent that a variable
6877 points to some unknown piece of memory. */
3781ab4b 6878 var_anything = new_var_info (NULL_TREE, "ANYTHING", false);
0bbf2ffa 6879 gcc_assert (var_anything->id == anything_id);
910fdc79
DB
6880 var_anything->is_artificial_var = 1;
6881 var_anything->size = ~0;
6882 var_anything->offset = 0;
910fdc79 6883 var_anything->fullsize = ~0;
13c2c08b 6884 var_anything->is_special_var = 1;
910fdc79
DB
6885
6886 /* Anything points to anything. This makes deref constraints just
c58936b6 6887 work in the presence of linked list and other p = *p type loops,
910fdc79 6888 by saying that *ANYTHING = ANYTHING. */
910fdc79
DB
6889 lhs.type = SCALAR;
6890 lhs.var = anything_id;
6891 lhs.offset = 0;
3e5937d7 6892 rhs.type = ADDRESSOF;
910fdc79
DB
6893 rhs.var = anything_id;
6894 rhs.offset = 0;
e8ca4159 6895
a5eadacc
DB
6896 /* This specifically does not use process_constraint because
6897 process_constraint ignores all anything = anything constraints, since all
6898 but this one are redundant. */
9771b263 6899 constraints.safe_push (new_constraint (lhs, rhs));
c58936b6 6900
ebd7d910
RB
6901 /* Create the STRING variable, used to represent that a variable
6902 points to a string literal. String literals don't contain
6903 pointers so STRING doesn't point to anything. */
3781ab4b 6904 var_string = new_var_info (NULL_TREE, "STRING", false);
ebd7d910
RB
6905 gcc_assert (var_string->id == string_id);
6906 var_string->is_artificial_var = 1;
6907 var_string->offset = 0;
6908 var_string->size = ~0;
6909 var_string->fullsize = ~0;
6910 var_string->is_special_var = 1;
6911 var_string->may_have_pointers = 0;
c58936b6 6912
b7091901
RG
6913 /* Create the ESCAPED variable, used to represent the set of escaped
6914 memory. */
3781ab4b 6915 var_escaped = new_var_info (NULL_TREE, "ESCAPED", false);
0bbf2ffa 6916 gcc_assert (var_escaped->id == escaped_id);
b7091901
RG
6917 var_escaped->is_artificial_var = 1;
6918 var_escaped->offset = 0;
6919 var_escaped->size = ~0;
6920 var_escaped->fullsize = ~0;
6921 var_escaped->is_special_var = 0;
b7091901 6922
b7091901
RG
6923 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6924 memory. */
3781ab4b 6925 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL", false);
0bbf2ffa 6926 gcc_assert (var_nonlocal->id == nonlocal_id);
b7091901
RG
6927 var_nonlocal->is_artificial_var = 1;
6928 var_nonlocal->offset = 0;
6929 var_nonlocal->size = ~0;
6930 var_nonlocal->fullsize = ~0;
6931 var_nonlocal->is_special_var = 1;
b7091901 6932
5006671f
RG
6933 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6934 lhs.type = SCALAR;
6935 lhs.var = escaped_id;
6936 lhs.offset = 0;
6937 rhs.type = DEREF;
6938 rhs.var = escaped_id;
6939 rhs.offset = 0;
6940 process_constraint (new_constraint (lhs, rhs));
6941
6942 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6943 whole variable escapes. */
6944 lhs.type = SCALAR;
6945 lhs.var = escaped_id;
6946 lhs.offset = 0;
6947 rhs.type = SCALAR;
6948 rhs.var = escaped_id;
6949 rhs.offset = UNKNOWN_OFFSET;
6950 process_constraint (new_constraint (lhs, rhs));
6951
6952 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6953 everything pointed to by escaped points to what global memory can
6954 point to. */
6955 lhs.type = DEREF;
6956 lhs.var = escaped_id;
6957 lhs.offset = 0;
6958 rhs.type = SCALAR;
6959 rhs.var = nonlocal_id;
6960 rhs.offset = 0;
6961 process_constraint (new_constraint (lhs, rhs));
6962
6963 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6964 global memory may point to global memory and escaped memory. */
b7091901
RG
6965 lhs.type = SCALAR;
6966 lhs.var = nonlocal_id;
6967 lhs.offset = 0;
6968 rhs.type = ADDRESSOF;
5006671f
RG
6969 rhs.var = nonlocal_id;
6970 rhs.offset = 0;
6971 process_constraint (new_constraint (lhs, rhs));
6972 rhs.type = ADDRESSOF;
b7091901
RG
6973 rhs.var = escaped_id;
6974 rhs.offset = 0;
910fdc79 6975 process_constraint (new_constraint (lhs, rhs));
c58936b6 6976
9e39dba6
RG
6977 /* Create the STOREDANYTHING variable, used to represent the set of
6978 variables stored to *ANYTHING. */
3781ab4b 6979 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING", false);
0bbf2ffa 6980 gcc_assert (var_storedanything->id == storedanything_id);
9e39dba6
RG
6981 var_storedanything->is_artificial_var = 1;
6982 var_storedanything->offset = 0;
6983 var_storedanything->size = ~0;
6984 var_storedanything->fullsize = ~0;
6985 var_storedanything->is_special_var = 0;
9e39dba6 6986
910fdc79 6987 /* Create the INTEGER variable, used to represent that a variable points
5006671f 6988 to what an INTEGER "points to". */
3781ab4b 6989 var_integer = new_var_info (NULL_TREE, "INTEGER", false);
0bbf2ffa 6990 gcc_assert (var_integer->id == integer_id);
910fdc79
DB
6991 var_integer->is_artificial_var = 1;
6992 var_integer->size = ~0;
6993 var_integer->fullsize = ~0;
6994 var_integer->offset = 0;
13c2c08b 6995 var_integer->is_special_var = 1;
a5eadacc 6996
21392f19
DB
6997 /* INTEGER = ANYTHING, because we don't know where a dereference of
6998 a random integer will point to. */
a5eadacc
DB
6999 lhs.type = SCALAR;
7000 lhs.var = integer_id;
7001 lhs.offset = 0;
3e5937d7 7002 rhs.type = ADDRESSOF;
a5eadacc
DB
7003 rhs.var = anything_id;
7004 rhs.offset = 0;
7005 process_constraint (new_constraint (lhs, rhs));
c58936b6 7006}
910fdc79 7007
4ee00913 7008/* Initialize things necessary to perform PTA */
910fdc79 7009
4ee00913
DB
7010static void
7011init_alias_vars (void)
910fdc79 7012{
e5bae89b
RG
7013 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
7014
3e5937d7
DB
7015 bitmap_obstack_initialize (&pta_obstack);
7016 bitmap_obstack_initialize (&oldpta_obstack);
4ee00913 7017 bitmap_obstack_initialize (&predbitmap_obstack);
910fdc79 7018
9771b263
DN
7019 constraints.create (8);
7020 varmap.create (8);
b787e7a2 7021 vi_for_tree = new hash_map<tree, varinfo_t>;
355fe088 7022 call_stmt_vars = new hash_map<gimple *, varinfo_t>;
3e5937d7 7023
910fdc79 7024 memset (&stats, 0, sizeof (stats));
c203e8a7 7025 shared_bitmap_table = new hash_table<shared_bitmap_hasher> (511);
910fdc79 7026 init_base_vars ();
7d6e2521
RG
7027
7028 gcc_obstack_init (&fake_var_decl_obstack);
d394a308 7029
b787e7a2 7030 final_solutions = new hash_map<varinfo_t, pt_solution *>;
d394a308 7031 gcc_obstack_init (&final_solutions_obstack);
4ee00913
DB
7032}
7033
3e5937d7
DB
7034/* Remove the REF and ADDRESS edges from GRAPH, as well as all the
7035 predecessor edges. */
7036
7037static void
7038remove_preds_and_fake_succs (constraint_graph_t graph)
7039{
7040 unsigned int i;
7041
7042 /* Clear the implicit ref and address nodes from the successor
7043 lists. */
d6d305fe 7044 for (i = 1; i < FIRST_REF_NODE; i++)
3e5937d7
DB
7045 {
7046 if (graph->succs[i])
7047 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
7048 FIRST_REF_NODE * 2);
7049 }
7050
7051 /* Free the successor list for the non-ref nodes. */
d6d305fe 7052 for (i = FIRST_REF_NODE + 1; i < graph->size; i++)
3e5937d7
DB
7053 {
7054 if (graph->succs[i])
7055 BITMAP_FREE (graph->succs[i]);
7056 }
7057
7058 /* Now reallocate the size of the successor list as, and blow away
7059 the predecessor bitmaps. */
9771b263 7060 graph->size = varmap.length ();
c22940cd 7061 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
3e5937d7
DB
7062
7063 free (graph->implicit_preds);
7064 graph->implicit_preds = NULL;
7065 free (graph->preds);
7066 graph->preds = NULL;
7067 bitmap_obstack_release (&predbitmap_obstack);
7068}
7069
5c245b95 7070/* Solve the constraint set. */
4ee00913 7071
5006671f 7072static void
5c245b95 7073solve_constraints (void)
4ee00913 7074{
3e5937d7 7075 struct scc_info *si;
910fdc79 7076
21392f19
DB
7077 if (dump_file)
7078 fprintf (dump_file,
7079 "\nCollapsing static cycles and doing variable "
7b765bed
DB
7080 "substitution\n");
7081
9771b263 7082 init_graph (varmap.length () * 2);
b8698a0f 7083
7b765bed
DB
7084 if (dump_file)
7085 fprintf (dump_file, "Building predecessor graph\n");
3e5937d7 7086 build_pred_graph ();
b8698a0f 7087
7b765bed
DB
7088 if (dump_file)
7089 fprintf (dump_file, "Detecting pointer and location "
7090 "equivalences\n");
3e5937d7 7091 si = perform_var_substitution (graph);
b8698a0f 7092
7b765bed
DB
7093 if (dump_file)
7094 fprintf (dump_file, "Rewriting constraints and unifying "
7095 "variables\n");
7096 rewrite_constraints (graph, si);
fc93bcb6 7097
8576f20a 7098 build_succ_graph ();
fc93bcb6 7099
8d3e3924
RG
7100 free_var_substitution_info (si);
7101
8576f20a 7102 /* Attach complex constraints to graph nodes. */
7b765bed
DB
7103 move_complex_constraints (graph);
7104
7105 if (dump_file)
7106 fprintf (dump_file, "Uniting pointer but not location equivalent "
7107 "variables\n");
7108 unite_pointer_equivalences (graph);
7109
7110 if (dump_file)
7111 fprintf (dump_file, "Finding indirect cycles\n");
3e5937d7 7112 find_indirect_cycles (graph);
c58936b6 7113
3e5937d7
DB
7114 /* Implicit nodes and predecessors are no longer necessary at this
7115 point. */
7116 remove_preds_and_fake_succs (graph);
c58936b6 7117
8576f20a
RG
7118 if (dump_file && (dump_flags & TDF_GRAPH))
7119 {
7120 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
7121 "in dot format:\n");
7122 dump_constraint_graph (dump_file);
7123 fprintf (dump_file, "\n\n");
7124 }
7125
21392f19 7126 if (dump_file)
7b765bed 7127 fprintf (dump_file, "Solving graph\n");
c58936b6 7128
21392f19 7129 solve_graph (graph);
c58936b6 7130
8576f20a
RG
7131 if (dump_file && (dump_flags & TDF_GRAPH))
7132 {
7133 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
7134 "in dot format:\n");
7135 dump_constraint_graph (dump_file);
7136 fprintf (dump_file, "\n\n");
7137 }
7138
910fdc79
DB
7139 if (dump_file)
7140 dump_sa_points_to_info (dump_file);
5c245b95
RG
7141}
7142
7143/* Create points-to sets for the current function. See the comments
7144 at the start of the file for an algorithmic overview. */
7145
7146static void
7147compute_points_to_sets (void)
7148{
7149 basic_block bb;
5c245b95
RG
7150 varinfo_t vi;
7151
7152 timevar_push (TV_TREE_PTA);
7153
7154 init_alias_vars ();
5c245b95 7155
628169e0 7156 intra_create_variable_infos (cfun);
5c245b95 7157
25a6a873 7158 /* Now walk all statements and build the constraint set. */
11cd3bed 7159 FOR_EACH_BB_FN (bb, cfun)
5c245b95 7160 {
538dd0b7
DM
7161 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7162 gsi_next (&gsi))
5c245b95 7163 {
538dd0b7 7164 gphi *phi = gsi.phi ();
5c245b95 7165
ea057359 7166 if (! virtual_operand_p (gimple_phi_result (phi)))
628169e0 7167 find_func_aliases (cfun, phi);
5c245b95
RG
7168 }
7169
538dd0b7
DM
7170 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
7171 gsi_next (&gsi))
5c245b95 7172 {
355fe088 7173 gimple *stmt = gsi_stmt (gsi);
5c245b95 7174
628169e0 7175 find_func_aliases (cfun, stmt);
5c245b95
RG
7176 }
7177 }
7178
25a6a873
RG
7179 if (dump_file)
7180 {
7181 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
7182 dump_constraints (dump_file, 0);
7183 }
7184
5c245b95
RG
7185 /* From the constraints compute the points-to sets. */
7186 solve_constraints ();
c58936b6 7187
3e8542ca 7188 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
ee7d29b4
RB
7189 cfun->gimple_df->escaped = find_what_var_points_to (cfun->decl,
7190 get_varinfo (escaped_id));
5006671f
RG
7191
7192 /* Make sure the ESCAPED solution (which is used as placeholder in
7193 other solutions) does not reference itself. This simplifies
7194 points-to solution queries. */
7195 cfun->gimple_df->escaped.escaped = 0;
7196
7197 /* Compute the points-to sets for pointer SSA_NAMEs. */
46aa019a
KV
7198 unsigned i;
7199 tree ptr;
7200
7201 FOR_EACH_SSA_NAME (i, ptr, cfun)
5006671f 7202 {
46aa019a 7203 if (POINTER_TYPE_P (TREE_TYPE (ptr)))
ee7d29b4 7204 find_what_p_points_to (cfun->decl, ptr);
5006671f 7205 }
e8ca4159 7206
d086d311 7207 /* Compute the call-used/clobbered sets. */
11cd3bed 7208 FOR_EACH_BB_FN (bb, cfun)
d086d311
RG
7209 {
7210 gimple_stmt_iterator gsi;
7211
7212 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7213 {
538dd0b7 7214 gcall *stmt;
d086d311 7215 struct pt_solution *pt;
538dd0b7
DM
7216
7217 stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
7218 if (!stmt)
d086d311
RG
7219 continue;
7220
7221 pt = gimple_call_use_set (stmt);
7222 if (gimple_call_flags (stmt) & ECF_CONST)
7223 memset (pt, 0, sizeof (struct pt_solution));
3e8542ca 7224 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
d086d311 7225 {
ee7d29b4 7226 *pt = find_what_var_points_to (cfun->decl, vi);
3e8542ca
RG
7227 /* Escaped (and thus nonlocal) variables are always
7228 implicitly used by calls. */
d086d311
RG
7229 /* ??? ESCAPED can be empty even though NONLOCAL
7230 always escaped. */
7231 pt->nonlocal = 1;
7232 pt->escaped = 1;
7233 }
7234 else
7235 {
3e8542ca
RG
7236 /* If there is nothing special about this call then
7237 we have made everything that is used also escape. */
d086d311
RG
7238 *pt = cfun->gimple_df->escaped;
7239 pt->nonlocal = 1;
7240 }
7241
7242 pt = gimple_call_clobber_set (stmt);
7243 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7244 memset (pt, 0, sizeof (struct pt_solution));
3e8542ca
RG
7245 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7246 {
ee7d29b4 7247 *pt = find_what_var_points_to (cfun->decl, vi);
3e8542ca
RG
7248 /* Escaped (and thus nonlocal) variables are always
7249 implicitly clobbered by calls. */
7250 /* ??? ESCAPED can be empty even though NONLOCAL
7251 always escaped. */
7252 pt->nonlocal = 1;
7253 pt->escaped = 1;
7254 }
d086d311
RG
7255 else
7256 {
3e8542ca
RG
7257 /* If there is nothing special about this call then
7258 we have made everything that is used also escape. */
d086d311
RG
7259 *pt = cfun->gimple_df->escaped;
7260 pt->nonlocal = 1;
7261 }
7262 }
7263 }
7264
e8ca4159 7265 timevar_pop (TV_TREE_PTA);
910fdc79
DB
7266}
7267
910fdc79
DB
7268
7269/* Delete created points-to sets. */
7270
5006671f 7271static void
e8ca4159 7272delete_points_to_sets (void)
910fdc79 7273{
7b765bed 7274 unsigned int i;
c58936b6 7275
c203e8a7
TS
7276 delete shared_bitmap_table;
7277 shared_bitmap_table = NULL;
3e5937d7
DB
7278 if (dump_file && (dump_flags & TDF_STATS))
7279 fprintf (dump_file, "Points to sets created:%d\n",
7280 stats.points_to_sets_created);
7281
b787e7a2
TS
7282 delete vi_for_tree;
7283 delete call_stmt_vars;
3e5937d7 7284 bitmap_obstack_release (&pta_obstack);
9771b263 7285 constraints.release ();
c58936b6 7286
7b765bed 7287 for (i = 0; i < graph->size; i++)
9771b263 7288 graph->complex[i].release ();
285463b5 7289 free (graph->complex);
21392f19 7290
3e5937d7 7291 free (graph->rep);
57250223 7292 free (graph->succs);
7b765bed
DB
7293 free (graph->pe);
7294 free (graph->pe_rep);
3e5937d7 7295 free (graph->indirect_cycles);
b5efa470
DB
7296 free (graph);
7297
9771b263 7298 varmap.release ();
dc5667a3
ML
7299 variable_info_pool.release ();
7300 constraint_pool.release ();
7d6e2521
RG
7301
7302 obstack_free (&fake_var_decl_obstack, NULL);
d394a308 7303
b787e7a2 7304 delete final_solutions;
d394a308 7305 obstack_free (&final_solutions_obstack, NULL);
910fdc79 7306}
973162ec 7307
8270b82d
RB
7308struct vls_data
7309{
7310 unsigned short clique;
7311 bitmap rvars;
7312};
7313
aa098165
RB
7314/* Mark "other" loads and stores as belonging to CLIQUE and with
7315 base zero. */
7316
7317static bool
8270b82d 7318visit_loadstore (gimple *, tree base, tree ref, void *data)
aa098165 7319{
8270b82d
RB
7320 unsigned short clique = ((vls_data *) data)->clique;
7321 bitmap rvars = ((vls_data *) data)->rvars;
aa098165
RB
7322 if (TREE_CODE (base) == MEM_REF
7323 || TREE_CODE (base) == TARGET_MEM_REF)
7324 {
7325 tree ptr = TREE_OPERAND (base, 0);
1dc5814c 7326 if (TREE_CODE (ptr) == SSA_NAME)
aa098165 7327 {
1dc5814c
RB
7328 /* For parameters, get at the points-to set for the actual parm
7329 decl. */
7330 if (SSA_NAME_IS_DEFAULT_DEF (ptr)
7331 && (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
7332 || TREE_CODE (SSA_NAME_VAR (ptr)) == RESULT_DECL))
7333 ptr = SSA_NAME_VAR (ptr);
7334
8270b82d 7335 /* We need to make sure 'ptr' doesn't include any of
0a28fdca 7336 the restrict tags we added bases for in its points-to set. */
8270b82d
RB
7337 varinfo_t vi = lookup_vi_for_tree (ptr);
7338 if (! vi)
7339 return false;
aa098165 7340
8270b82d
RB
7341 vi = get_varinfo (find (vi->id));
7342 if (bitmap_intersect_p (rvars, vi->solution))
7343 return false;
7344 }
aa098165
RB
7345
7346 /* Do not overwrite existing cliques (that includes clique, base
7347 pairs we just set). */
7348 if (MR_DEPENDENCE_CLIQUE (base) == 0)
7349 {
7350 MR_DEPENDENCE_CLIQUE (base) = clique;
7351 MR_DEPENDENCE_BASE (base) = 0;
7352 }
7353 }
7354
7355 /* For plain decl accesses see whether they are accesses to globals
7356 and rewrite them to MEM_REFs with { clique, 0 }. */
8813a647 7357 if (VAR_P (base)
aa098165
RB
7358 && is_global_var (base)
7359 /* ??? We can't rewrite a plain decl with the walk_stmt_load_store
7360 ops callback. */
7361 && base != ref)
7362 {
7363 tree *basep = &ref;
7364 while (handled_component_p (*basep))
7365 basep = &TREE_OPERAND (*basep, 0);
8813a647 7366 gcc_assert (VAR_P (*basep));
aa098165
RB
7367 tree ptr = build_fold_addr_expr (*basep);
7368 tree zero = build_int_cst (TREE_TYPE (ptr), 0);
7369 *basep = build2 (MEM_REF, TREE_TYPE (*basep), ptr, zero);
7370 MR_DEPENDENCE_CLIQUE (*basep) = clique;
7371 MR_DEPENDENCE_BASE (*basep) = 0;
7372 }
7373
7374 return false;
7375}
7376
7377/* If REF is a MEM_REF then assign a clique, base pair to it, updating
7378 CLIQUE, *RESTRICT_VAR and LAST_RUID. Return whether dependence info
7379 was assigned to REF. */
7380
7381static bool
7382maybe_set_dependence_info (tree ref, tree ptr,
7383 unsigned short &clique, varinfo_t restrict_var,
7384 unsigned short &last_ruid)
7385{
7386 while (handled_component_p (ref))
7387 ref = TREE_OPERAND (ref, 0);
7388 if ((TREE_CODE (ref) == MEM_REF
7389 || TREE_CODE (ref) == TARGET_MEM_REF)
7390 && TREE_OPERAND (ref, 0) == ptr)
7391 {
7392 /* Do not overwrite existing cliques. This avoids overwriting dependence
7393 info inlined from a function with restrict parameters inlined
7394 into a function with restrict parameters. This usually means we
7395 prefer to be precise in innermost loops. */
7396 if (MR_DEPENDENCE_CLIQUE (ref) == 0)
7397 {
7398 if (clique == 0)
7399 clique = ++cfun->last_clique;
7400 if (restrict_var->ruid == 0)
7401 restrict_var->ruid = ++last_ruid;
7402 MR_DEPENDENCE_CLIQUE (ref) = clique;
7403 MR_DEPENDENCE_BASE (ref) = restrict_var->ruid;
7404 return true;
7405 }
7406 }
7407 return false;
7408}
7409
7410/* Compute the set of independend memory references based on restrict
7411 tags and their conservative propagation to the points-to sets. */
7412
7413static void
7414compute_dependence_clique (void)
7415{
7416 unsigned short clique = 0;
7417 unsigned short last_ruid = 0;
8270b82d 7418 bitmap rvars = BITMAP_ALLOC (NULL);
aa098165
RB
7419 for (unsigned i = 0; i < num_ssa_names; ++i)
7420 {
7421 tree ptr = ssa_name (i);
7422 if (!ptr || !POINTER_TYPE_P (TREE_TYPE (ptr)))
7423 continue;
7424
7425 /* Avoid all this when ptr is not dereferenced? */
7426 tree p = ptr;
7427 if (SSA_NAME_IS_DEFAULT_DEF (ptr)
7428 && (TREE_CODE (SSA_NAME_VAR (ptr)) == PARM_DECL
7429 || TREE_CODE (SSA_NAME_VAR (ptr)) == RESULT_DECL))
7430 p = SSA_NAME_VAR (ptr);
7431 varinfo_t vi = lookup_vi_for_tree (p);
7432 if (!vi)
7433 continue;
7434 vi = get_varinfo (find (vi->id));
7435 bitmap_iterator bi;
7436 unsigned j;
7437 varinfo_t restrict_var = NULL;
7438 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, j, bi)
7439 {
7440 varinfo_t oi = get_varinfo (j);
7441 if (oi->is_restrict_var)
7442 {
7443 if (restrict_var)
7444 {
7445 if (dump_file && (dump_flags & TDF_DETAILS))
7446 {
7447 fprintf (dump_file, "found restrict pointed-to "
7448 "for ");
ef6cb4c7 7449 print_generic_expr (dump_file, ptr);
aa098165
RB
7450 fprintf (dump_file, " but not exclusively\n");
7451 }
7452 restrict_var = NULL;
7453 break;
7454 }
7455 restrict_var = oi;
7456 }
7457 /* NULL is the only other valid points-to entry. */
7458 else if (oi->id != nothing_id)
7459 {
7460 restrict_var = NULL;
7461 break;
7462 }
7463 }
7464 /* Ok, found that ptr must(!) point to a single(!) restrict
7465 variable. */
7466 /* ??? PTA isn't really a proper propagation engine to compute
7467 this property.
7468 ??? We could handle merging of two restricts by unifying them. */
7469 if (restrict_var)
7470 {
7471 /* Now look at possible dereferences of ptr. */
7472 imm_use_iterator ui;
355fe088 7473 gimple *use_stmt;
8270b82d 7474 bool used = false;
aa098165
RB
7475 FOR_EACH_IMM_USE_STMT (use_stmt, ui, ptr)
7476 {
7477 /* ??? Calls and asms. */
7478 if (!gimple_assign_single_p (use_stmt))
7479 continue;
8270b82d
RB
7480 used |= maybe_set_dependence_info (gimple_assign_lhs (use_stmt),
7481 ptr, clique, restrict_var,
7482 last_ruid);
7483 used |= maybe_set_dependence_info (gimple_assign_rhs1 (use_stmt),
7484 ptr, clique, restrict_var,
7485 last_ruid);
aa098165 7486 }
8270b82d
RB
7487 if (used)
7488 bitmap_set_bit (rvars, restrict_var->id);
aa098165
RB
7489 }
7490 }
7491
8270b82d
RB
7492 if (clique != 0)
7493 {
7494 /* Assign the BASE id zero to all accesses not based on a restrict
7495 pointer. That way they get disambiguated against restrict
7496 accesses but not against each other. */
7497 /* ??? For restricts derived from globals (thus not incoming
7498 parameters) we can't restrict scoping properly thus the following
7499 is too aggressive there. For now we have excluded those globals from
7500 getting into the MR_DEPENDENCE machinery. */
7501 vls_data data = { clique, rvars };
7502 basic_block bb;
7503 FOR_EACH_BB_FN (bb, cfun)
7504 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
7505 !gsi_end_p (gsi); gsi_next (&gsi))
7506 {
7507 gimple *stmt = gsi_stmt (gsi);
7508 walk_stmt_load_store_ops (stmt, &data,
7509 visit_loadstore, visit_loadstore);
7510 }
7511 }
aa098165 7512
8270b82d 7513 BITMAP_FREE (rvars);
aa098165 7514}
5006671f
RG
7515
7516/* Compute points-to information for every SSA_NAME pointer in the
7517 current function and compute the transitive closure of escaped
7518 variables to re-initialize the call-clobber states of local variables. */
7519
7520unsigned int
7521compute_may_aliases (void)
7522{
25a6a873
RG
7523 if (cfun->gimple_df->ipa_pta)
7524 {
7525 if (dump_file)
7526 {
7527 fprintf (dump_file, "\nNot re-computing points-to information "
7528 "because IPA points-to information is available.\n\n");
7529
7530 /* But still dump what we have remaining it. */
7531 dump_alias_info (dump_file);
25a6a873
RG
7532 }
7533
7534 return 0;
7535 }
7536
5006671f
RG
7537 /* For each pointer P_i, determine the sets of variables that P_i may
7538 point-to. Compute the reachability set of escaped and call-used
7539 variables. */
7540 compute_points_to_sets ();
7541
7542 /* Debugging dumps. */
7543 if (dump_file)
824f71b9 7544 dump_alias_info (dump_file);
5006671f 7545
aa098165
RB
7546 /* Compute restrict-based memory disambiguations. */
7547 compute_dependence_clique ();
7548
5006671f
RG
7549 /* Deallocate memory used by aliasing data structures and the internal
7550 points-to solution. */
7551 delete_points_to_sets ();
7552
7553 gcc_assert (!need_ssa_update_p (cfun));
7554
7555 return 0;
7556}
7557
5006671f
RG
7558/* A dummy pass to cause points-to information to be computed via
7559 TODO_rebuild_alias. */
7560
27a4cd48
DM
7561namespace {
7562
7563const pass_data pass_data_build_alias =
7564{
7565 GIMPLE_PASS, /* type */
7566 "alias", /* name */
7567 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
7568 TV_NONE, /* tv_id */
7569 ( PROP_cfg | PROP_ssa ), /* properties_required */
7570 0, /* properties_provided */
7571 0, /* properties_destroyed */
7572 0, /* todo_flags_start */
7573 TODO_rebuild_alias, /* todo_flags_finish */
5006671f
RG
7574};
7575
27a4cd48
DM
7576class pass_build_alias : public gimple_opt_pass
7577{
7578public:
c3284718
RS
7579 pass_build_alias (gcc::context *ctxt)
7580 : gimple_opt_pass (pass_data_build_alias, ctxt)
27a4cd48
DM
7581 {}
7582
7583 /* opt_pass methods: */
1a3d085c 7584 virtual bool gate (function *) { return flag_tree_pta; }
27a4cd48
DM
7585
7586}; // class pass_build_alias
7587
7588} // anon namespace
7589
7590gimple_opt_pass *
7591make_pass_build_alias (gcc::context *ctxt)
7592{
7593 return new pass_build_alias (ctxt);
7594}
7595
6b8ed145
RG
7596/* A dummy pass to cause points-to information to be computed via
7597 TODO_rebuild_alias. */
7598
27a4cd48
DM
7599namespace {
7600
7601const pass_data pass_data_build_ealias =
7602{
7603 GIMPLE_PASS, /* type */
7604 "ealias", /* name */
7605 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
7606 TV_NONE, /* tv_id */
7607 ( PROP_cfg | PROP_ssa ), /* properties_required */
7608 0, /* properties_provided */
7609 0, /* properties_destroyed */
7610 0, /* todo_flags_start */
7611 TODO_rebuild_alias, /* todo_flags_finish */
6b8ed145
RG
7612};
7613
27a4cd48
DM
7614class pass_build_ealias : public gimple_opt_pass
7615{
7616public:
c3284718
RS
7617 pass_build_ealias (gcc::context *ctxt)
7618 : gimple_opt_pass (pass_data_build_ealias, ctxt)
27a4cd48
DM
7619 {}
7620
7621 /* opt_pass methods: */
1a3d085c 7622 virtual bool gate (function *) { return flag_tree_pta; }
27a4cd48
DM
7623
7624}; // class pass_build_ealias
7625
7626} // anon namespace
7627
7628gimple_opt_pass *
7629make_pass_build_ealias (gcc::context *ctxt)
7630{
7631 return new pass_build_ealias (ctxt);
7632}
7633
5006671f 7634
25a6a873
RG
7635/* IPA PTA solutions for ESCAPED. */
7636struct pt_solution ipa_escaped_pt
da42ac7b
RB
7637 = { true, false, false, false, false,
7638 false, false, false, false, false, NULL };
25a6a873 7639
39e2db00 7640/* Associate node with varinfo DATA. Worker for
2b78f221 7641 cgraph_for_symbol_thunks_and_aliases. */
39e2db00
JH
7642static bool
7643associate_varinfo_to_alias (struct cgraph_node *node, void *data)
7644{
c5e5f5f6
RB
7645 if ((node->alias
7646 || (node->thunk.thunk_p
7647 && ! node->global.inlined_to))
67348ccc
DM
7648 && node->analyzed)
7649 insert_vi_for_tree (node->decl, (varinfo_t)data);
39e2db00
JH
7650 return false;
7651}
7652
c5e2d17d
TV
7653/* Dump varinfo VI to FILE. */
7654
7655static void
7656dump_varinfo (FILE *file, varinfo_t vi)
7657{
7658 if (vi == NULL)
7659 return;
7660
7661 fprintf (file, "%u: %s\n", vi->id, vi->name);
7662
7663 const char *sep = " ";
7664 if (vi->is_artificial_var)
7665 fprintf (file, "%sartificial", sep);
7666 if (vi->is_special_var)
7667 fprintf (file, "%sspecial", sep);
7668 if (vi->is_unknown_size_var)
7669 fprintf (file, "%sunknown-size", sep);
7670 if (vi->is_full_var)
7671 fprintf (file, "%sfull", sep);
7672 if (vi->is_heap_var)
7673 fprintf (file, "%sheap", sep);
7674 if (vi->may_have_pointers)
7675 fprintf (file, "%smay-have-pointers", sep);
7676 if (vi->only_restrict_pointers)
7677 fprintf (file, "%sonly-restrict-pointers", sep);
7678 if (vi->is_restrict_var)
7679 fprintf (file, "%sis-restrict-var", sep);
7680 if (vi->is_global_var)
7681 fprintf (file, "%sglobal", sep);
7682 if (vi->is_ipa_escape_point)
7683 fprintf (file, "%sipa-escape-point", sep);
7684 if (vi->is_fn_info)
7685 fprintf (file, "%sfn-info", sep);
7686 if (vi->ruid)
7687 fprintf (file, "%srestrict-uid:%u", sep, vi->ruid);
7688 if (vi->next)
7689 fprintf (file, "%snext:%u", sep, vi->next);
7690 if (vi->head != vi->id)
7691 fprintf (file, "%shead:%u", sep, vi->head);
7692 if (vi->offset)
7693 fprintf (file, "%soffset:" HOST_WIDE_INT_PRINT_DEC, sep, vi->offset);
7694 if (vi->size != ~(unsigned HOST_WIDE_INT)0)
7695 fprintf (file, "%ssize:" HOST_WIDE_INT_PRINT_DEC, sep, vi->size);
7696 if (vi->fullsize != ~(unsigned HOST_WIDE_INT)0
7697 && vi->fullsize != vi->size)
7698 fprintf (file, "%sfullsize:" HOST_WIDE_INT_PRINT_DEC, sep,
7699 vi->fullsize);
7700 fprintf (file, "\n");
7701
7702 if (vi->solution && !bitmap_empty_p (vi->solution))
7703 {
7704 bitmap_iterator bi;
7705 unsigned i;
7706 fprintf (file, " solution: {");
7707 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
7708 fprintf (file, " %u", i);
7709 fprintf (file, " }\n");
7710 }
7711
7712 if (vi->oldsolution && !bitmap_empty_p (vi->oldsolution)
7713 && !bitmap_equal_p (vi->solution, vi->oldsolution))
7714 {
7715 bitmap_iterator bi;
7716 unsigned i;
7717 fprintf (file, " oldsolution: {");
7718 EXECUTE_IF_SET_IN_BITMAP (vi->oldsolution, 0, i, bi)
7719 fprintf (file, " %u", i);
7720 fprintf (file, " }\n");
7721 }
7722}
7723
7724/* Dump varinfo VI to stderr. */
7725
7726DEBUG_FUNCTION void
7727debug_varinfo (varinfo_t vi)
7728{
7729 dump_varinfo (stderr, vi);
7730}
7731
7732/* Dump varmap to FILE. */
7733
7734static void
7735dump_varmap (FILE *file)
7736{
7737 if (varmap.length () == 0)
7738 return;
7739
7740 fprintf (file, "variables:\n");
7741
7742 for (unsigned int i = 0; i < varmap.length (); ++i)
7743 {
7744 varinfo_t vi = get_varinfo (i);
7745 dump_varinfo (file, vi);
7746 }
7747
7748 fprintf (file, "\n");
7749}
7750
7751/* Dump varmap to stderr. */
7752
7753DEBUG_FUNCTION void
7754debug_varmap (void)
7755{
7756 dump_varmap (stderr);
7757}
7758
2b78f221
RB
7759/* Compute whether node is refered to non-locally. Worker for
7760 cgraph_for_symbol_thunks_and_aliases. */
7761static bool
7762refered_from_nonlocal_fn (struct cgraph_node *node, void *data)
7763{
7764 bool *nonlocal_p = (bool *)data;
7765 *nonlocal_p |= (node->used_from_other_partition
7766 || node->externally_visible
7767 || node->force_output);
7768 return false;
7769}
7770
7771/* Same for varpool nodes. */
7772static bool
7773refered_from_nonlocal_var (struct varpool_node *node, void *data)
7774{
7775 bool *nonlocal_p = (bool *)data;
7776 *nonlocal_p |= (node->used_from_other_partition
7777 || node->externally_visible
7778 || node->force_output);
7779 return false;
7780}
7781
4ee00913 7782/* Execute the driver for IPA PTA. */
c2924966 7783static unsigned int
4ee00913
DB
7784ipa_pta_execute (void)
7785{
7786 struct cgraph_node *node;
2c8326a5 7787 varpool_node *var;
bf749042 7788 unsigned int from = 0;
3e5937d7 7789
4ee00913 7790 in_ipa_mode = 1;
5c245b95 7791
4ee00913 7792 init_alias_vars ();
c58936b6 7793
1565af08
RG
7794 if (dump_file && (dump_flags & TDF_DETAILS))
7795 {
6c52831d 7796 symtab->dump (dump_file);
1565af08
RG
7797 fprintf (dump_file, "\n");
7798 }
7799
bf749042
TV
7800 if (dump_file)
7801 {
7802 fprintf (dump_file, "Generating generic constraints\n\n");
7803 dump_constraints (dump_file, from);
7804 fprintf (dump_file, "\n");
7805 from = constraints.length ();
7806 }
7807
5c245b95 7808 /* Build the constraints. */
65c70e6b 7809 FOR_EACH_DEFINED_FUNCTION (node)
4ee00913 7810 {
27c2cfa6 7811 varinfo_t vi;
5c245b95
RG
7812 /* Nodes without a body are not interesting. Especially do not
7813 visit clones at this point for now - we get duplicate decls
7814 there for inline clones at least. */
70486010 7815 if (!node->has_gimple_body_p () || node->global.inlined_to)
5c245b95 7816 continue;
d52f5295 7817 node->get_body ();
5c245b95 7818
1565af08
RG
7819 gcc_assert (!node->clone_of);
7820
d0c9ca44
TV
7821 /* For externally visible or attribute used annotated functions use
7822 local constraints for their arguments.
7823 For local functions we see all callers and thus do not need initial
7824 constraints for parameters. */
7825 bool nonlocal_p = (node->used_from_other_partition
7826 || node->externally_visible
72ed2b9c 7827 || node->force_output);
2b78f221
RB
7828 node->call_for_symbol_thunks_and_aliases (refered_from_nonlocal_fn,
7829 &nonlocal_p, true);
d0c9ca44 7830
67348ccc 7831 vi = create_function_info_for (node->decl,
d0c9ca44
TV
7832 alias_get_name (node->decl), false,
7833 nonlocal_p);
7834 if (dump_file
7835 && from != constraints.length ())
7836 {
7837 fprintf (dump_file,
7838 "Generating intial constraints for %s", node->name ());
7839 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
7840 fprintf (dump_file, " (%s)",
7841 IDENTIFIER_POINTER
7842 (DECL_ASSEMBLER_NAME (node->decl)));
7843 fprintf (dump_file, "\n\n");
7844 dump_constraints (dump_file, from);
7845 fprintf (dump_file, "\n");
7846
7847 from = constraints.length ();
7848 }
7849
d52f5295
ML
7850 node->call_for_symbol_thunks_and_aliases
7851 (associate_varinfo_to_alias, vi, true);
4ee00913 7852 }
5c245b95 7853
25a6a873 7854 /* Create constraints for global variables and their initializers. */
65c70e6b 7855 FOR_EACH_VARIABLE (var)
27c2cfa6 7856 {
67348ccc 7857 if (var->alias && var->analyzed)
cd35bcf7 7858 continue;
27c2cfa6 7859
47e5754e
RB
7860 varinfo_t vi = get_vi_for_tree (var->decl);
7861
7862 /* For the purpose of IPA PTA unit-local globals are not
7863 escape points. */
7864 bool nonlocal_p = (var->used_from_other_partition
7865 || var->externally_visible
7866 || var->force_output);
2b78f221
RB
7867 var->call_for_symbol_and_aliases (refered_from_nonlocal_var,
7868 &nonlocal_p, true);
47e5754e
RB
7869 if (nonlocal_p)
7870 vi->is_ipa_escape_point = true;
27c2cfa6 7871 }
25a6a873 7872
bf749042
TV
7873 if (dump_file
7874 && from != constraints.length ())
25a6a873
RG
7875 {
7876 fprintf (dump_file,
7877 "Generating constraints for global initializers\n\n");
bf749042 7878 dump_constraints (dump_file, from);
25a6a873 7879 fprintf (dump_file, "\n");
bf749042 7880 from = constraints.length ();
25a6a873 7881 }
25a6a873 7882
65c70e6b 7883 FOR_EACH_DEFINED_FUNCTION (node)
4ee00913 7884 {
5c245b95
RG
7885 struct function *func;
7886 basic_block bb;
4ee00913 7887
5c245b95 7888 /* Nodes without a body are not interesting. */
d52f5295 7889 if (!node->has_gimple_body_p () || node->clone_of)
5c245b95 7890 continue;
c58936b6 7891
5c245b95 7892 if (dump_file)
27c2cfa6
RG
7893 {
7894 fprintf (dump_file,
fec39fa6 7895 "Generating constraints for %s", node->name ());
67348ccc 7896 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
27c2cfa6 7897 fprintf (dump_file, " (%s)",
960bfb69 7898 IDENTIFIER_POINTER
67348ccc 7899 (DECL_ASSEMBLER_NAME (node->decl)));
27c2cfa6
RG
7900 fprintf (dump_file, "\n");
7901 }
c58936b6 7902
67348ccc 7903 func = DECL_STRUCT_FUNCTION (node->decl);
628169e0 7904 gcc_assert (cfun == NULL);
726a989a 7905
5c245b95
RG
7906 /* Build constriants for the function body. */
7907 FOR_EACH_BB_FN (bb, func)
7908 {
538dd0b7 7909 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
5c245b95
RG
7910 gsi_next (&gsi))
7911 {
538dd0b7 7912 gphi *phi = gsi.phi ();
c58936b6 7913
ea057359 7914 if (! virtual_operand_p (gimple_phi_result (phi)))
628169e0 7915 find_func_aliases (func, phi);
5c245b95 7916 }
3e5937d7 7917
538dd0b7
DM
7918 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
7919 gsi_next (&gsi))
5c245b95 7920 {
355fe088 7921 gimple *stmt = gsi_stmt (gsi);
3e5937d7 7922
628169e0
RB
7923 find_func_aliases (func, stmt);
7924 find_func_clobbers (func, stmt);
5c245b95
RG
7925 }
7926 }
c58936b6 7927
25a6a873
RG
7928 if (dump_file)
7929 {
7930 fprintf (dump_file, "\n");
7931 dump_constraints (dump_file, from);
7932 fprintf (dump_file, "\n");
bf749042 7933 from = constraints.length ();
25a6a873 7934 }
5c245b95 7935 }
c58936b6 7936
5c245b95
RG
7937 /* From the constraints compute the points-to sets. */
7938 solve_constraints ();
c58936b6 7939
25a6a873
RG
7940 /* Compute the global points-to sets for ESCAPED.
7941 ??? Note that the computed escape set is not correct
7942 for the whole unit as we fail to consider graph edges to
7943 externally visible functions. */
ee7d29b4 7944 ipa_escaped_pt = find_what_var_points_to (NULL, get_varinfo (escaped_id));
25a6a873
RG
7945
7946 /* Make sure the ESCAPED solution (which is used as placeholder in
7947 other solutions) does not reference itself. This simplifies
7948 points-to solution queries. */
7949 ipa_escaped_pt.ipa_escaped = 0;
7950
7951 /* Assign the points-to sets to the SSA names in the unit. */
65c70e6b 7952 FOR_EACH_DEFINED_FUNCTION (node)
25a6a873
RG
7953 {
7954 tree ptr;
7955 struct function *fn;
7956 unsigned i;
25a6a873 7957 basic_block bb;
25a6a873
RG
7958
7959 /* Nodes without a body are not interesting. */
d52f5295 7960 if (!node->has_gimple_body_p () || node->clone_of)
25a6a873
RG
7961 continue;
7962
67348ccc 7963 fn = DECL_STRUCT_FUNCTION (node->decl);
25a6a873
RG
7964
7965 /* Compute the points-to sets for pointer SSA_NAMEs. */
9771b263 7966 FOR_EACH_VEC_ELT (*fn->gimple_df->ssa_names, i, ptr)
25a6a873
RG
7967 {
7968 if (ptr
7969 && POINTER_TYPE_P (TREE_TYPE (ptr)))
ee7d29b4 7970 find_what_p_points_to (node->decl, ptr);
25a6a873
RG
7971 }
7972
25a6a873
RG
7973 /* Compute the call-use and call-clobber sets for indirect calls
7974 and calls to external functions. */
7975 FOR_EACH_BB_FN (bb, fn)
7976 {
7977 gimple_stmt_iterator gsi;
7978
7979 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7980 {
538dd0b7 7981 gcall *stmt;
25a6a873 7982 struct pt_solution *pt;
92b3326b 7983 varinfo_t vi, fi;
25a6a873
RG
7984 tree decl;
7985
538dd0b7
DM
7986 stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
7987 if (!stmt)
25a6a873
RG
7988 continue;
7989
92b3326b 7990 /* Handle direct calls to functions with body. */
17b6426c
TV
7991 decl = gimple_call_fndecl (stmt);
7992
7993 {
7994 tree called_decl = NULL_TREE;
7995 if (gimple_call_builtin_p (stmt, BUILT_IN_GOMP_PARALLEL))
7996 called_decl = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
7997 else if (gimple_call_builtin_p (stmt, BUILT_IN_GOACC_PARALLEL))
7998 called_decl = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
7999
8000 if (called_decl != NULL_TREE
8001 && !fndecl_maybe_in_other_partition (called_decl))
8002 decl = called_decl;
8003 }
e0ca27c5 8004
25a6a873 8005 if (decl
92b3326b
RB
8006 && (fi = lookup_vi_for_tree (decl))
8007 && fi->is_fn_info)
8008 {
8009 *gimple_call_clobber_set (stmt)
8010 = find_what_var_points_to
ee7d29b4 8011 (node->decl, first_vi_for_offset (fi, fi_clobbers));
92b3326b
RB
8012 *gimple_call_use_set (stmt)
8013 = find_what_var_points_to
ee7d29b4 8014 (node->decl, first_vi_for_offset (fi, fi_uses));
92b3326b
RB
8015 }
8016 /* Handle direct calls to external functions. */
8017 else if (decl)
25a6a873
RG
8018 {
8019 pt = gimple_call_use_set (stmt);
8020 if (gimple_call_flags (stmt) & ECF_CONST)
8021 memset (pt, 0, sizeof (struct pt_solution));
8022 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
8023 {
ee7d29b4 8024 *pt = find_what_var_points_to (node->decl, vi);
25a6a873
RG
8025 /* Escaped (and thus nonlocal) variables are always
8026 implicitly used by calls. */
8027 /* ??? ESCAPED can be empty even though NONLOCAL
8028 always escaped. */
8029 pt->nonlocal = 1;
8030 pt->ipa_escaped = 1;
8031 }
8032 else
8033 {
8034 /* If there is nothing special about this call then
8035 we have made everything that is used also escape. */
8036 *pt = ipa_escaped_pt;
8037 pt->nonlocal = 1;
8038 }
8039
8040 pt = gimple_call_clobber_set (stmt);
8041 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
8042 memset (pt, 0, sizeof (struct pt_solution));
8043 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
8044 {
ee7d29b4 8045 *pt = find_what_var_points_to (node->decl, vi);
25a6a873
RG
8046 /* Escaped (and thus nonlocal) variables are always
8047 implicitly clobbered by calls. */
8048 /* ??? ESCAPED can be empty even though NONLOCAL
8049 always escaped. */
8050 pt->nonlocal = 1;
8051 pt->ipa_escaped = 1;
8052 }
8053 else
8054 {
8055 /* If there is nothing special about this call then
8056 we have made everything that is used also escape. */
8057 *pt = ipa_escaped_pt;
8058 pt->nonlocal = 1;
8059 }
8060 }
25a6a873 8061 /* Handle indirect calls. */
92b3326b
RB
8062 else if (!decl
8063 && (fi = get_fi_for_callee (stmt)))
25a6a873
RG
8064 {
8065 /* We need to accumulate all clobbers/uses of all possible
8066 callees. */
8067 fi = get_varinfo (find (fi->id));
8068 /* If we cannot constrain the set of functions we'll end up
8069 calling we end up using/clobbering everything. */
8070 if (bitmap_bit_p (fi->solution, anything_id)
8071 || bitmap_bit_p (fi->solution, nonlocal_id)
8072 || bitmap_bit_p (fi->solution, escaped_id))
8073 {
8074 pt_solution_reset (gimple_call_clobber_set (stmt));
8075 pt_solution_reset (gimple_call_use_set (stmt));
8076 }
8077 else
8078 {
8079 bitmap_iterator bi;
8080 unsigned i;
8081 struct pt_solution *uses, *clobbers;
8082
8083 uses = gimple_call_use_set (stmt);
8084 clobbers = gimple_call_clobber_set (stmt);
8085 memset (uses, 0, sizeof (struct pt_solution));
8086 memset (clobbers, 0, sizeof (struct pt_solution));
8087 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
8088 {
8089 struct pt_solution sol;
8090
8091 vi = get_varinfo (i);
8092 if (!vi->is_fn_info)
8093 {
8094 /* ??? We could be more precise here? */
8095 uses->nonlocal = 1;
8096 uses->ipa_escaped = 1;
8097 clobbers->nonlocal = 1;
8098 clobbers->ipa_escaped = 1;
8099 continue;
8100 }
8101
8102 if (!uses->anything)
8103 {
d394a308 8104 sol = find_what_var_points_to
ee7d29b4
RB
8105 (node->decl,
8106 first_vi_for_offset (vi, fi_uses));
25a6a873
RG
8107 pt_solution_ior_into (uses, &sol);
8108 }
8109 if (!clobbers->anything)
8110 {
d394a308 8111 sol = find_what_var_points_to
ee7d29b4
RB
8112 (node->decl,
8113 first_vi_for_offset (vi, fi_clobbers));
25a6a873
RG
8114 pt_solution_ior_into (clobbers, &sol);
8115 }
8116 }
8117 }
8118 }
8119 }
8120 }
8121
8122 fn->gimple_df->ipa_pta = true;
ee7d29b4
RB
8123
8124 /* We have to re-set the final-solution cache after each function
8125 because what is a "global" is dependent on function context. */
8126 final_solutions->empty ();
8127 obstack_free (&final_solutions_obstack, NULL);
8128 gcc_obstack_init (&final_solutions_obstack);
25a6a873
RG
8129 }
8130
5c245b95 8131 delete_points_to_sets ();
c58936b6 8132
4ee00913 8133 in_ipa_mode = 0;
5c245b95 8134
c2924966 8135 return 0;
4ee00913 8136}
c58936b6 8137
27a4cd48
DM
8138namespace {
8139
8140const pass_data pass_data_ipa_pta =
8141{
8142 SIMPLE_IPA_PASS, /* type */
8143 "pta", /* name */
8144 OPTGROUP_NONE, /* optinfo_flags */
27a4cd48
DM
8145 TV_IPA_PTA, /* tv_id */
8146 0, /* properties_required */
8147 0, /* properties_provided */
8148 0, /* properties_destroyed */
8149 0, /* todo_flags_start */
11924f8b 8150 0, /* todo_flags_finish */
4ee00913 8151};
27a4cd48
DM
8152
8153class pass_ipa_pta : public simple_ipa_opt_pass
8154{
8155public:
c3284718
RS
8156 pass_ipa_pta (gcc::context *ctxt)
8157 : simple_ipa_opt_pass (pass_data_ipa_pta, ctxt)
27a4cd48
DM
8158 {}
8159
8160 /* opt_pass methods: */
1a3d085c
TS
8161 virtual bool gate (function *)
8162 {
8163 return (optimize
8164 && flag_ipa_pta
8165 /* Don't bother doing anything if the program has errors. */
8166 && !seen_error ());
8167 }
8168
597a8ab9
TV
8169 opt_pass * clone () { return new pass_ipa_pta (m_ctxt); }
8170
be55bfe6 8171 virtual unsigned int execute (function *) { return ipa_pta_execute (); }
27a4cd48
DM
8172
8173}; // class pass_ipa_pta
8174
8175} // anon namespace
8176
8177simple_ipa_opt_pass *
8178make_pass_ipa_pta (gcc::context *ctxt)
8179{
8180 return new pass_ipa_pta (ctxt);
8181}