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