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