Line data Source code
1 : /* Data flow functions for trees.
2 : Copyright (C) 2001-2026 Free Software Foundation, Inc.
3 : Contributed by Diego Novillo <dnovillo@redhat.com>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
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.
16 :
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/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "rtl.h"
26 : #include "tree.h"
27 : #include "gimple.h"
28 : #include "tree-pass.h"
29 : #include "ssa.h"
30 : #include "tree-pretty-print.h"
31 : #include "fold-const.h"
32 : #include "stor-layout.h"
33 : #include "langhooks.h"
34 : #include "gimple-iterator.h"
35 : #include "gimple-walk.h"
36 : #include "tree-dfa.h"
37 : #include "gimple-range.h"
38 :
39 : /* Build and maintain data flow information for trees. */
40 :
41 : /* Counters used to display DFA and SSA statistics. */
42 : struct dfa_stats_d
43 : {
44 : long num_defs;
45 : long num_uses;
46 : long num_phis;
47 : long num_phi_args;
48 : size_t max_num_phi_args;
49 : long num_vdefs;
50 : long num_vuses;
51 : };
52 :
53 :
54 : /* Local functions. */
55 : static void collect_dfa_stats (struct dfa_stats_d *);
56 :
57 :
58 : /*---------------------------------------------------------------------------
59 : Dataflow analysis (DFA) routines
60 : ---------------------------------------------------------------------------*/
61 :
62 : /* Renumber the gimple stmt uids in one block. The caller is responsible
63 : of calling set_gimple_stmt_max_uid (fun, 0) at some point. */
64 :
65 : void
66 75754476 : renumber_gimple_stmt_uids_in_block (struct function *fun, basic_block bb)
67 : {
68 75754476 : gimple_stmt_iterator bsi;
69 102514182 : for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
70 : {
71 26759706 : gimple *stmt = gsi_stmt (bsi);
72 26759706 : gimple_set_uid (stmt, inc_gimple_stmt_max_uid (fun));
73 : }
74 646134616 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
75 : {
76 494625664 : gimple *stmt = gsi_stmt (bsi);
77 494625664 : gimple_set_uid (stmt, inc_gimple_stmt_max_uid (fun));
78 : }
79 75754476 : }
80 :
81 : /* Renumber all of the gimple stmt uids. */
82 :
83 : void
84 6723378 : renumber_gimple_stmt_uids (struct function *fun)
85 : {
86 6723378 : basic_block bb;
87 :
88 6723378 : set_gimple_stmt_max_uid (fun, 0);
89 78039980 : FOR_ALL_BB_FN (bb, fun)
90 71316602 : renumber_gimple_stmt_uids_in_block (fun, bb);
91 6723378 : }
92 :
93 : /* Like renumber_gimple_stmt_uids, but only do work on the basic blocks
94 : in BLOCKS, of which there are N_BLOCKS. Also renumbers PHIs. */
95 :
96 : void
97 637997 : renumber_gimple_stmt_uids_in_blocks (basic_block *blocks, int n_blocks)
98 : {
99 637997 : int i;
100 :
101 637997 : set_gimple_stmt_max_uid (cfun, 0);
102 4697880 : for (i = 0; i < n_blocks; i++)
103 4059883 : renumber_gimple_stmt_uids_in_block (cfun, blocks[i]);
104 637997 : }
105 :
106 :
107 :
108 : /*---------------------------------------------------------------------------
109 : Debugging functions
110 : ---------------------------------------------------------------------------*/
111 :
112 : /* Dump variable VAR and its may-aliases to FILE. */
113 :
114 : void
115 351 : dump_variable (FILE *file, tree var)
116 : {
117 351 : if (TREE_CODE (var) == SSA_NAME)
118 : {
119 0 : if (POINTER_TYPE_P (TREE_TYPE (var)))
120 0 : dump_points_to_info_for (file, var);
121 0 : var = SSA_NAME_VAR (var);
122 : }
123 :
124 : if (var == NULL_TREE)
125 : {
126 0 : fprintf (file, "<nil>");
127 0 : return;
128 : }
129 :
130 351 : print_generic_expr (file, var, dump_flags);
131 :
132 351 : fprintf (file, ", UID D.%u", (unsigned) DECL_UID (var));
133 351 : if (DECL_PT_UID (var) != DECL_UID (var))
134 0 : fprintf (file, ", PT-UID D.%u", (unsigned) DECL_PT_UID (var));
135 :
136 351 : fprintf (file, ", ");
137 351 : print_generic_expr (file, TREE_TYPE (var), dump_flags);
138 :
139 351 : if (TREE_ADDRESSABLE (var))
140 351 : fprintf (file, ", is addressable");
141 :
142 351 : if (is_global_var (var))
143 63 : fprintf (file, ", is global");
144 :
145 351 : if (TREE_THIS_VOLATILE (var))
146 0 : fprintf (file, ", is volatile");
147 :
148 351 : if (cfun && ssa_default_def (cfun, var))
149 : {
150 0 : fprintf (file, ", default def: ");
151 0 : print_generic_expr (file, ssa_default_def (cfun, var), dump_flags);
152 : }
153 :
154 351 : if (DECL_INITIAL (var))
155 : {
156 63 : fprintf (file, ", initial: ");
157 63 : print_generic_expr (file, DECL_INITIAL (var), dump_flags);
158 : }
159 :
160 351 : fprintf (file, "\n");
161 : }
162 :
163 :
164 : /* Dump variable VAR and its may-aliases to stderr. */
165 :
166 : DEBUG_FUNCTION void
167 0 : debug_variable (tree var)
168 : {
169 0 : dump_variable (stderr, var);
170 0 : }
171 :
172 :
173 : /* Dump various DFA statistics to FILE. */
174 :
175 : void
176 492 : dump_dfa_stats (FILE *file)
177 : {
178 492 : struct dfa_stats_d dfa_stats;
179 :
180 492 : unsigned long size, total = 0;
181 492 : const char * const fmt_str = "%-30s%-13s%12s\n";
182 492 : const char * const fmt_str_1 = "%-30s%13lu" PRsa (11) "\n";
183 492 : const char * const fmt_str_3 = "%-43s" PRsa (11) "\n";
184 492 : const char *funcname
185 492 : = lang_hooks.decl_printable_name (current_function_decl, 2);
186 :
187 492 : collect_dfa_stats (&dfa_stats);
188 :
189 492 : fprintf (file, "\nDFA Statistics for %s\n\n", funcname);
190 :
191 492 : fprintf (file, "---------------------------------------------------------\n");
192 492 : fprintf (file, fmt_str, "", " Number of ", "Memory");
193 492 : fprintf (file, fmt_str, "", " instances ", "used ");
194 492 : fprintf (file, "---------------------------------------------------------\n");
195 :
196 492 : size = dfa_stats.num_uses * sizeof (tree *);
197 492 : total += size;
198 492 : fprintf (file, fmt_str_1, "USE operands", dfa_stats.num_uses,
199 0 : SIZE_AMOUNT (size));
200 :
201 492 : size = dfa_stats.num_defs * sizeof (tree *);
202 492 : total += size;
203 492 : fprintf (file, fmt_str_1, "DEF operands", dfa_stats.num_defs,
204 0 : SIZE_AMOUNT (size));
205 :
206 492 : size = dfa_stats.num_vuses * sizeof (tree *);
207 492 : total += size;
208 492 : fprintf (file, fmt_str_1, "VUSE operands", dfa_stats.num_vuses,
209 0 : SIZE_AMOUNT (size));
210 :
211 492 : size = dfa_stats.num_vdefs * sizeof (tree *);
212 492 : total += size;
213 492 : fprintf (file, fmt_str_1, "VDEF operands", dfa_stats.num_vdefs,
214 0 : SIZE_AMOUNT (size));
215 :
216 492 : size = dfa_stats.num_phis * sizeof (struct gphi);
217 492 : total += size;
218 494 : fprintf (file, fmt_str_1, "PHI nodes", dfa_stats.num_phis,
219 4 : SIZE_AMOUNT (size));
220 :
221 492 : size = dfa_stats.num_phi_args * sizeof (struct phi_arg_d);
222 492 : total += size;
223 494 : fprintf (file, fmt_str_1, "PHI arguments", dfa_stats.num_phi_args,
224 4 : SIZE_AMOUNT (size));
225 :
226 492 : fprintf (file, "---------------------------------------------------------\n");
227 508 : fprintf (file, fmt_str_3, "Total memory used by DFA/SSA data",
228 32 : SIZE_AMOUNT (total));
229 492 : fprintf (file, "---------------------------------------------------------\n");
230 492 : fprintf (file, "\n");
231 :
232 492 : if (dfa_stats.num_phis)
233 485 : fprintf (file, "Average number of arguments per PHI node: %.1f (max: "
234 : HOST_SIZE_T_PRINT_DEC ")\n",
235 485 : (float) dfa_stats.num_phi_args / (float) dfa_stats.num_phis,
236 485 : (fmt_size_t) dfa_stats.max_num_phi_args);
237 :
238 492 : fprintf (file, "\n");
239 492 : }
240 :
241 :
242 : /* Dump DFA statistics on stderr. */
243 :
244 : DEBUG_FUNCTION void
245 0 : debug_dfa_stats (void)
246 : {
247 0 : dump_dfa_stats (stderr);
248 0 : }
249 :
250 :
251 : /* Collect DFA statistics and store them in the structure pointed to by
252 : DFA_STATS_P. */
253 :
254 : static void
255 492 : collect_dfa_stats (struct dfa_stats_d *dfa_stats_p ATTRIBUTE_UNUSED)
256 : {
257 492 : basic_block bb;
258 :
259 492 : gcc_assert (dfa_stats_p);
260 :
261 492 : memset ((void *)dfa_stats_p, 0, sizeof (struct dfa_stats_d));
262 :
263 : /* Walk all the statements in the function counting references. */
264 8601 : FOR_EACH_BB_FN (bb, cfun)
265 : {
266 13480 : for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
267 5371 : gsi_next (&si))
268 : {
269 5371 : gphi *phi = si.phi ();
270 5371 : dfa_stats_p->num_phis++;
271 5371 : dfa_stats_p->num_phi_args += gimple_phi_num_args (phi);
272 5371 : if (gimple_phi_num_args (phi) > dfa_stats_p->max_num_phi_args)
273 675 : dfa_stats_p->max_num_phi_args = gimple_phi_num_args (phi);
274 : }
275 :
276 30795 : for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
277 14577 : gsi_next (&si))
278 : {
279 14577 : gimple *stmt = gsi_stmt (si);
280 14577 : dfa_stats_p->num_defs += NUM_SSA_OPERANDS (stmt, SSA_OP_DEF);
281 14577 : dfa_stats_p->num_uses += NUM_SSA_OPERANDS (stmt, SSA_OP_USE);
282 27150 : dfa_stats_p->num_vdefs += gimple_vdef (stmt) ? 1 : 0;
283 27150 : dfa_stats_p->num_vuses += gimple_vuse (stmt) ? 1 : 0;
284 : }
285 : }
286 492 : }
287 :
288 :
289 : /*---------------------------------------------------------------------------
290 : Miscellaneous helpers
291 : ---------------------------------------------------------------------------*/
292 :
293 : /* Lookup VAR UID in the default_defs hashtable and return the associated
294 : variable. */
295 :
296 : tree
297 581210164 : ssa_default_def (struct function *fn, tree var)
298 : {
299 581210164 : struct tree_decl_minimal ind;
300 581210164 : struct tree_ssa_name in;
301 581210164 : gcc_assert (VAR_P (var)
302 : || TREE_CODE (var) == PARM_DECL
303 : || TREE_CODE (var) == RESULT_DECL);
304 :
305 : /* Always NULL_TREE for rtl function dumps. */
306 581210164 : if (!fn->gimple_df)
307 : return NULL_TREE;
308 :
309 581210164 : in.var = (tree)&ind;
310 581210164 : ind.uid = DECL_UID (var);
311 581210164 : return DEFAULT_DEFS (fn)->find_with_hash ((tree)&in, DECL_UID (var));
312 : }
313 :
314 : /* Insert the pair VAR's UID, DEF into the default_defs hashtable
315 : of function FN. */
316 :
317 : void
318 12784024 : set_ssa_default_def (struct function *fn, tree var, tree def)
319 : {
320 12784024 : struct tree_decl_minimal ind;
321 12784024 : struct tree_ssa_name in;
322 :
323 12784024 : gcc_assert (VAR_P (var)
324 : || TREE_CODE (var) == PARM_DECL
325 : || TREE_CODE (var) == RESULT_DECL);
326 12784024 : in.var = (tree)&ind;
327 12784024 : ind.uid = DECL_UID (var);
328 12784024 : if (!def)
329 : {
330 3614889 : tree *loc = DEFAULT_DEFS (fn)->find_slot_with_hash ((tree)&in,
331 1204963 : DECL_UID (var),
332 : NO_INSERT);
333 1204963 : if (loc)
334 : {
335 1204925 : SSA_NAME_IS_DEFAULT_DEF (*(tree *)loc) = false;
336 1204925 : DEFAULT_DEFS (fn)->clear_slot (loc);
337 : }
338 1204963 : return;
339 : }
340 11579061 : gcc_assert (TREE_CODE (def) == SSA_NAME && SSA_NAME_VAR (def) == var);
341 34737183 : tree *loc = DEFAULT_DEFS (fn)->find_slot_with_hash ((tree)&in,
342 11579061 : DECL_UID (var), INSERT);
343 :
344 : /* Default definition might be changed by tail call optimization. */
345 11579061 : if (*loc)
346 1332 : SSA_NAME_IS_DEFAULT_DEF (*loc) = false;
347 :
348 : /* Mark DEF as the default definition for VAR. */
349 11579061 : *loc = def;
350 11579061 : SSA_NAME_IS_DEFAULT_DEF (def) = true;
351 : }
352 :
353 : /* Retrieve or create a default definition for VAR. */
354 :
355 : tree
356 27760235 : get_or_create_ssa_default_def (struct function *fn, tree var)
357 : {
358 27760235 : tree ddef = ssa_default_def (fn, var);
359 27760235 : if (ddef == NULL_TREE)
360 : {
361 11044554 : ddef = make_ssa_name_fn (fn, var, gimple_build_nop ());
362 11044554 : set_ssa_default_def (fn, var, ddef);
363 : }
364 27760235 : return ddef;
365 : }
366 :
367 :
368 : /* If EXP is a handled component reference for a structure, return the
369 : base variable. The access range is delimited by bit positions *POFFSET and
370 : *POFFSET + *PMAX_SIZE. The access size is *PSIZE bits. If either
371 : *PSIZE or *PMAX_SIZE is -1, they could not be determined. If *PSIZE
372 : and *PMAX_SIZE are equal, the access is non-variable. If *PREVERSE is
373 : true, the storage order of the reference is reversed. */
374 :
375 : tree
376 3230683602 : get_ref_base_and_extent (tree exp, poly_int64 *poffset,
377 : poly_int64 *psize,
378 : poly_int64 *pmax_size,
379 : bool *preverse)
380 : {
381 3230683602 : poly_offset_int bitsize = -1;
382 3230683602 : poly_offset_int maxsize;
383 3230683602 : tree size_tree = NULL_TREE;
384 3230683602 : poly_offset_int bit_offset = 0;
385 3230683602 : bool seen_variable_array_ref = false;
386 :
387 : /* First get the final access size and the storage order from just the
388 : outermost expression. */
389 3230683602 : if (TREE_CODE (exp) == COMPONENT_REF)
390 1575759910 : size_tree = DECL_SIZE (TREE_OPERAND (exp, 1));
391 1654923692 : else if (TREE_CODE (exp) == BIT_FIELD_REF)
392 8760384 : size_tree = TREE_OPERAND (exp, 1);
393 1646163308 : else if (TREE_CODE (exp) == WITH_SIZE_EXPR)
394 : {
395 174 : size_tree = TREE_OPERAND (exp, 1);
396 174 : exp = TREE_OPERAND (exp, 0);
397 : }
398 1646163134 : else if (!VOID_TYPE_P (TREE_TYPE (exp)))
399 : {
400 1604090217 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
401 1604090217 : if (mode == BLKmode)
402 384821039 : size_tree = TYPE_SIZE (TREE_TYPE (exp));
403 : else
404 2438538356 : bitsize = GET_MODE_BITSIZE (mode);
405 : }
406 1219269178 : if (size_tree != NULL_TREE
407 1969341507 : && poly_int_tree_p (size_tree))
408 1968963864 : bitsize = wi::to_poly_offset (size_tree);
409 :
410 3230683602 : *preverse = reverse_storage_order_for_component_p (exp);
411 :
412 : /* Initially, maxsize is the same as the accessed element size.
413 : In the following it will only grow (or become -1). */
414 3230683602 : maxsize = bitsize;
415 :
416 : /* Compute cumulative bit-offset for nested component-refs and array-refs,
417 : and find the ultimate containing object. */
418 9455738006 : while (1)
419 : {
420 6343210804 : switch (TREE_CODE (exp))
421 : {
422 8760384 : case BIT_FIELD_REF:
423 8760384 : bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
424 8760384 : break;
425 :
426 2341571121 : case COMPONENT_REF:
427 2341571121 : {
428 2341571121 : tree field = TREE_OPERAND (exp, 1);
429 2341571121 : tree this_offset = component_ref_field_offset (exp);
430 :
431 2341571121 : if (this_offset && poly_int_tree_p (this_offset))
432 : {
433 2341568071 : poly_offset_int woffset = (wi::to_poly_offset (this_offset)
434 2341568071 : << LOG2_BITS_PER_UNIT);
435 2341568071 : woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
436 2341568071 : bit_offset += woffset;
437 :
438 : /* If we had seen a variable array ref already and we just
439 : referenced the last field of a struct or a union member
440 : then we have to adjust maxsize by the padding at the end
441 : of our field. */
442 2341568071 : if (seen_variable_array_ref)
443 : {
444 21117479 : tree stype = TREE_TYPE (TREE_OPERAND (exp, 0));
445 21117479 : tree next = DECL_CHAIN (field);
446 62593378 : while (next && TREE_CODE (next) != FIELD_DECL)
447 41475899 : next = DECL_CHAIN (next);
448 21117479 : if (!next
449 3115174 : || TREE_CODE (stype) != RECORD_TYPE)
450 : {
451 18736596 : tree fsize = DECL_SIZE (field);
452 18736596 : tree ssize = TYPE_SIZE (stype);
453 18736596 : if (fsize == NULL
454 11765166 : || !poly_int_tree_p (fsize)
455 11763020 : || ssize == NULL
456 30499616 : || !poly_int_tree_p (ssize))
457 6973576 : maxsize = -1;
458 11763020 : else if (known_size_p (maxsize))
459 : {
460 11762870 : poly_offset_int tem
461 11762870 : = (wi::to_poly_offset (ssize)
462 11762870 : - wi::to_poly_offset (fsize));
463 23525740 : tem -= woffset;
464 11762870 : maxsize += tem;
465 : }
466 : }
467 : /* An component ref with an adjacent field up in the
468 : structure hierarchy constrains the size of any variable
469 : array ref lower in the access hierarchy. */
470 : else
471 : seen_variable_array_ref = false;
472 : }
473 : }
474 : else
475 : {
476 3050 : tree csize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
477 : /* We need to adjust maxsize to the whole structure bitsize.
478 : But we can subtract any constant offset seen so far,
479 : because that would get us out of the structure otherwise. */
480 3050 : if (known_size_p (maxsize)
481 2041 : && csize
482 5091 : && poly_int_tree_p (csize))
483 0 : maxsize = wi::to_poly_offset (csize) - bit_offset;
484 : else
485 3050 : maxsize = -1;
486 : }
487 : }
488 : break;
489 :
490 745915260 : case ARRAY_REF:
491 745915260 : case ARRAY_RANGE_REF:
492 745915260 : {
493 745915260 : tree index = TREE_OPERAND (exp, 1);
494 745915260 : tree low_bound, unit_size;
495 :
496 : /* If the resulting bit-offset is constant, track it. */
497 745915260 : if (poly_int_tree_p (index)
498 643195963 : && (low_bound = array_ref_low_bound (exp),
499 643195963 : poly_int_tree_p (low_bound))
500 1389106171 : && (unit_size = array_ref_element_size (exp),
501 643190911 : TREE_CODE (unit_size) == INTEGER_CST))
502 : {
503 643119382 : poly_offset_int woffset
504 643119382 : = wi::sext (wi::to_poly_offset (index)
505 1286238764 : - wi::to_poly_offset (low_bound),
506 643119382 : TYPE_PRECISION (sizetype));
507 643119382 : woffset *= wi::to_offset (unit_size);
508 643119382 : woffset <<= LOG2_BITS_PER_UNIT;
509 643119382 : bit_offset += woffset;
510 :
511 : /* An array ref with a constant index up in the structure
512 : hierarchy will constrain the size of any variable array ref
513 : lower in the access hierarchy. */
514 643119382 : seen_variable_array_ref = false;
515 : }
516 : else
517 : {
518 102795878 : tree asize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
519 : /* We need to adjust maxsize to the whole array bitsize.
520 : But we can subtract any constant offset seen so far,
521 : because that would get us outside of the array otherwise. */
522 102795878 : if (known_size_p (maxsize)
523 102594394 : && asize
524 184418256 : && poly_int_tree_p (asize))
525 78805730 : maxsize = wi::to_poly_offset (asize) - bit_offset;
526 : else
527 23990148 : maxsize = -1;
528 :
529 : /* Remember that we have seen an array ref with a variable
530 : index. */
531 102795878 : seen_variable_array_ref = true;
532 :
533 : /* Try to constrain the access range by using the range of the
534 : index expression when we know it. Extra care must be taken
535 : when the low bound of the array is not zero, because it may
536 : be very large and the index expression may be unsigned and
537 : wrap around; in this case, the correct range is that of the
538 : difference between the index expression and the low bound,
539 : see get_inner_reference for the model computation. */
540 102795878 : range_query *query = get_range_query (cfun);
541 102795878 : range_op_handler minus_op (MINUS_EXPR);
542 102795878 : int_range_max vr_idx, vr_lb, vr;
543 :
544 102795878 : if (TREE_CODE (index) == SSA_NAME
545 102291071 : && (low_bound = array_ref_low_bound (exp),
546 102291071 : TREE_CODE (low_bound) == INTEGER_CST)
547 102290759 : && (unit_size = array_ref_element_size (exp),
548 102290759 : TREE_CODE (unit_size) == INTEGER_CST)
549 102119107 : && query->range_of_expr (vr, index)
550 102119107 : && (integer_zerop (low_bound)
551 477234 : || ((vr_idx = vr,
552 477234 : query->range_of_expr (vr_lb, low_bound))
553 42455510 : && minus_op.fold_range (vr, TREE_TYPE (index),
554 : vr_idx, vr_lb)))
555 102119107 : && !vr.varying_p ()
556 163618653 : && !vr.undefined_p ())
557 : {
558 60817602 : wide_int min = vr.lower_bound ();
559 60817602 : wide_int max = vr.upper_bound ();
560 : /* Try to constrain maxsize with range information. */
561 60817602 : offset_int omax
562 60817602 : = offset_int::from (max, TYPE_SIGN (TREE_TYPE (index)));
563 60817602 : if (wi::get_precision (max) <= ADDR_MAX_BITSIZE
564 60817602 : && omax >= 0)
565 : {
566 60710624 : offset_int rmaxsize
567 60710624 : = (omax + 1)
568 121421248 : * wi::to_offset (unit_size) << LOG2_BITS_PER_UNIT;
569 60710624 : if (!known_size_p (maxsize)
570 108016414 : || known_lt (rmaxsize, maxsize))
571 : {
572 : /* If we know an upper bound below the declared
573 : one this is no longer variable. */
574 24378328 : if (known_size_p (maxsize))
575 : seen_variable_array_ref = false;
576 24378328 : maxsize = rmaxsize;
577 : }
578 : }
579 : /* Try to adjust bit_offset with range information. */
580 60817602 : offset_int omin
581 60817602 : = offset_int::from (min, TYPE_SIGN (TREE_TYPE (index)));
582 60817602 : if (wi::get_precision (min) <= ADDR_MAX_BITSIZE
583 60817602 : && omin > 0)
584 : {
585 16010422 : offset_int woffset
586 16010422 : = wi::sext (omin, TYPE_PRECISION (sizetype));
587 16010422 : woffset *= wi::to_offset (unit_size);
588 16010422 : woffset <<= LOG2_BITS_PER_UNIT;
589 16010422 : bit_offset += woffset;
590 16010422 : if (known_size_p (maxsize))
591 16010422 : maxsize -= woffset;
592 : }
593 60817602 : }
594 102795878 : }
595 : }
596 : break;
597 :
598 : case REALPART_EXPR:
599 : break;
600 :
601 : case IMAGPART_EXPR:
602 3112527202 : bit_offset += bitsize;
603 : break;
604 :
605 : case VIEW_CONVERT_EXPR:
606 : break;
607 :
608 55783317 : case TARGET_MEM_REF:
609 : /* Via the variable index or index2 we can reach the
610 : whole object. Still hand back the decl here. */
611 55783317 : if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
612 55783317 : && (TMR_INDEX (exp) || TMR_INDEX2 (exp)))
613 : {
614 6889045 : exp = TREE_OPERAND (TMR_BASE (exp), 0);
615 6889045 : bit_offset = 0;
616 6889045 : maxsize = -1;
617 6889045 : goto done;
618 : }
619 : /* Fallthru. */
620 1241356197 : case MEM_REF:
621 : /* We need to deal with variable arrays ending structures such as
622 : struct { int length; int a[1]; } x; x.a[d]
623 : struct { struct { int a; int b; } a[1]; } x; x.a[d].a
624 : struct { struct { int a[1]; } a[1]; } x; x.a[0][d], x.a[d][0]
625 : struct { int len; union { int a[1]; struct X x; } u; } x; x.u.a[d]
626 : where we do not know maxsize for variable index accesses to
627 : the array. The simplest way to conservatively deal with this
628 : is to punt in the case that offset + maxsize reaches the
629 : base type boundary. This needs to include possible trailing
630 : padding that is there for alignment purposes. */
631 1241356197 : if (seen_variable_array_ref
632 32924099 : && known_size_p (maxsize)
633 1259796395 : && (TYPE_SIZE (TREE_TYPE (exp)) == NULL_TREE
634 11327057 : || !poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
635 9582924 : || (maybe_eq
636 9582924 : (bit_offset + maxsize,
637 1250939121 : wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp)))))))
638 18440196 : maxsize = -1;
639 :
640 : /* Hand back the decl for MEM[&decl, off]. */
641 1241356197 : if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
642 : {
643 542234891 : if (integer_zerop (TREE_OPERAND (exp, 1)))
644 211163101 : exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
645 : else
646 : {
647 331071790 : poly_offset_int off = mem_ref_offset (exp);
648 331071790 : off <<= LOG2_BITS_PER_UNIT;
649 331071790 : off += bit_offset;
650 331071790 : poly_int64 off_hwi;
651 331071790 : if (off.to_shwi (&off_hwi))
652 : {
653 331070190 : bit_offset = off_hwi;
654 331070190 : exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
655 : }
656 : }
657 : }
658 1241356197 : goto done;
659 :
660 1982438360 : default:
661 1982438360 : goto done;
662 : }
663 :
664 3112527202 : exp = TREE_OPERAND (exp, 0);
665 3112527202 : }
666 :
667 3230683602 : done:
668 3230683602 : if (!bitsize.to_shwi (psize) || maybe_lt (*psize, 0))
669 : {
670 42450589 : *poffset = 0;
671 42450589 : *psize = -1;
672 42450589 : *pmax_size = -1;
673 :
674 42450589 : return exp;
675 : }
676 :
677 : /* ??? Due to negative offsets in ARRAY_REF we can end up with
678 : negative bit_offset here. We might want to store a zero offset
679 : in this case. */
680 3188233013 : if (!bit_offset.to_shwi (poffset))
681 : {
682 7142 : *poffset = 0;
683 7142 : *pmax_size = -1;
684 :
685 7142 : return exp;
686 : }
687 :
688 : /* In case of a decl or constant base object we can do better. */
689 :
690 3188225871 : if (DECL_P (exp))
691 : {
692 2459361342 : if (VAR_P (exp)
693 2459361342 : && ((flag_unconstrained_commons && DECL_COMMON (exp))
694 2397386519 : || (DECL_EXTERNAL (exp) && seen_variable_array_ref)))
695 : {
696 528847 : tree sz_tree = TYPE_SIZE (TREE_TYPE (exp));
697 : /* If size is unknown, or we have read to the end, assume there
698 : may be more to the structure than we are told. */
699 528847 : if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
700 528847 : || (seen_variable_array_ref
701 35594 : && (sz_tree == NULL_TREE
702 35594 : || !poly_int_tree_p (sz_tree)
703 35594 : || maybe_eq (bit_offset + maxsize,
704 564441 : wi::to_poly_offset (sz_tree)))))
705 528149 : maxsize = -1;
706 : }
707 : /* If maxsize is unknown adjust it according to the size of the
708 : base decl. */
709 2458832495 : else if (!known_size_p (maxsize)
710 14966533 : && DECL_SIZE (exp)
711 2473794625 : && poly_int_tree_p (DECL_SIZE (exp)))
712 14962068 : maxsize = wi::to_poly_offset (DECL_SIZE (exp)) - bit_offset;
713 : }
714 728864529 : else if (CONSTANT_CLASS_P (exp))
715 : {
716 : /* If maxsize is unknown adjust it according to the size of the
717 : base type constant. */
718 7686234 : if (!known_size_p (maxsize)
719 35274 : && TYPE_SIZE (TREE_TYPE (exp))
720 7721508 : && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp))))
721 35274 : maxsize = (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp)))
722 35274 : - bit_offset);
723 : }
724 :
725 3188225871 : if (!maxsize.to_shwi (pmax_size)
726 3188225871 : || maybe_lt (*pmax_size, 0)
727 6351124494 : || !endpoint_representable_p (*poffset, *pmax_size))
728 25327285 : *pmax_size = -1;
729 :
730 : /* Punt if *POFFSET + *PSIZE overflows in HOST_WIDE_INT, the callers don't
731 : check for such overflows individually and assume it works. */
732 6376451742 : if (!endpoint_representable_p (*poffset, *psize))
733 : {
734 37 : *poffset = 0;
735 37 : *psize = -1;
736 37 : *pmax_size = -1;
737 :
738 37 : return exp;
739 : }
740 :
741 : return exp;
742 : }
743 :
744 : /* Like get_ref_base_and_extent, but for cases in which we only care
745 : about constant-width accesses at constant offsets. Return null
746 : if the access is anything else. */
747 :
748 : tree
749 49905065 : get_ref_base_and_extent_hwi (tree exp, HOST_WIDE_INT *poffset,
750 : HOST_WIDE_INT *psize, bool *preverse)
751 : {
752 49905065 : poly_int64 offset, size, max_size;
753 49905065 : HOST_WIDE_INT const_offset, const_size;
754 49905065 : bool reverse;
755 49905065 : tree decl = get_ref_base_and_extent (exp, &offset, &size, &max_size,
756 : &reverse);
757 49905065 : if (!offset.is_constant (&const_offset)
758 49905065 : || !size.is_constant (&const_size)
759 49905065 : || const_offset < 0
760 49904295 : || !known_size_p (max_size)
761 49374131 : || maybe_ne (max_size, const_size))
762 : return NULL_TREE;
763 :
764 48438609 : *poffset = const_offset;
765 48438609 : *psize = const_size;
766 48438609 : *preverse = reverse;
767 48438609 : return decl;
768 : }
769 :
770 : /* Returns the base object and a constant BITS_PER_UNIT offset in *POFFSET that
771 : denotes the starting address of the memory access EXP.
772 : Returns NULL_TREE if the offset is not constant or any component
773 : is not BITS_PER_UNIT-aligned.
774 : VALUEIZE if non-NULL is used to valueize SSA names. It should return
775 : its argument or a constant if the argument is known to be constant. */
776 :
777 : tree
778 222608952 : get_addr_base_and_unit_offset_1 (tree exp, poly_int64 *poffset,
779 : tree (*valueize) (tree))
780 : {
781 222608952 : poly_int64 byte_offset = 0;
782 :
783 : /* Compute cumulative byte-offset for nested component-refs and array-refs,
784 : and find the ultimate containing object. */
785 338425264 : while (1)
786 : {
787 280517108 : switch (TREE_CODE (exp))
788 : {
789 20725 : case BIT_FIELD_REF:
790 20725 : {
791 20725 : poly_int64 this_byte_offset;
792 20725 : poly_uint64 this_bit_offset;
793 20725 : if (!poly_int_tree_p (TREE_OPERAND (exp, 2), &this_bit_offset)
794 41450 : || !multiple_p (this_bit_offset, BITS_PER_UNIT,
795 : &this_byte_offset))
796 0 : return NULL_TREE;
797 20725 : byte_offset += this_byte_offset;
798 : }
799 20725 : break;
800 :
801 53347196 : case COMPONENT_REF:
802 53347196 : {
803 53347196 : tree field = TREE_OPERAND (exp, 1);
804 53347196 : tree this_offset = component_ref_field_offset (exp);
805 53347196 : poly_int64 hthis_offset;
806 :
807 53347196 : if (!this_offset
808 53347196 : || !poly_int_tree_p (this_offset, &hthis_offset)
809 106691595 : || (TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field))
810 : % BITS_PER_UNIT))
811 3792 : return NULL_TREE;
812 :
813 53343404 : hthis_offset += (TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field))
814 53343404 : / BITS_PER_UNIT);
815 53343404 : byte_offset += hthis_offset;
816 : }
817 53343404 : break;
818 :
819 8110348 : case ARRAY_REF:
820 8110348 : case ARRAY_RANGE_REF:
821 8110348 : {
822 8110348 : tree index = TREE_OPERAND (exp, 1);
823 8110348 : tree low_bound, unit_size;
824 :
825 8110348 : if (valueize
826 1653232 : && TREE_CODE (index) == SSA_NAME)
827 1065575 : index = (*valueize) (index);
828 8110348 : if (!poly_int_tree_p (index))
829 3970215 : return NULL_TREE;
830 4152701 : low_bound = array_ref_low_bound (exp);
831 4152701 : if (valueize
832 857340 : && TREE_CODE (low_bound) == SSA_NAME)
833 4061 : low_bound = (*valueize) (low_bound);
834 4152701 : if (!poly_int_tree_p (low_bound))
835 : return NULL_TREE;
836 4152701 : unit_size = array_ref_element_size (exp);
837 4152701 : if (TREE_CODE (unit_size) != INTEGER_CST)
838 : return NULL_TREE;
839 :
840 : /* If the resulting bit-offset is constant, track it. */
841 4140133 : poly_offset_int woffset
842 4140133 : = wi::sext (wi::to_poly_offset (index)
843 8280266 : - wi::to_poly_offset (low_bound),
844 4140133 : TYPE_PRECISION (sizetype));
845 4140133 : woffset *= wi::to_offset (unit_size);
846 4140133 : byte_offset += woffset.force_shwi ();
847 : }
848 4140133 : break;
849 :
850 : case REALPART_EXPR:
851 : break;
852 :
853 25727 : case IMAGPART_EXPR:
854 25727 : byte_offset += TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (exp)));
855 25727 : break;
856 :
857 : case VIEW_CONVERT_EXPR:
858 : break;
859 :
860 34596614 : case MEM_REF:
861 34596614 : {
862 34596614 : tree base = TREE_OPERAND (exp, 0);
863 34596614 : if (valueize
864 6834876 : && TREE_CODE (base) == SSA_NAME)
865 6378801 : base = (*valueize) (base);
866 :
867 : /* Hand back the decl for MEM[&decl, off]. */
868 34596614 : if (TREE_CODE (base) == ADDR_EXPR)
869 : {
870 20007967 : if (!integer_zerop (TREE_OPERAND (exp, 1)))
871 : {
872 16675220 : poly_offset_int off = mem_ref_offset (exp);
873 16675220 : byte_offset += off.force_shwi ();
874 : }
875 20007967 : exp = TREE_OPERAND (base, 0);
876 : }
877 34596614 : goto done;
878 : }
879 :
880 733328 : case TARGET_MEM_REF:
881 733328 : {
882 733328 : tree base = TREE_OPERAND (exp, 0);
883 733328 : if (valueize
884 2235 : && TREE_CODE (base) == SSA_NAME)
885 1611 : base = (*valueize) (base);
886 :
887 : /* Hand back the decl for MEM[&decl, off]. */
888 733328 : if (TREE_CODE (base) == ADDR_EXPR)
889 : {
890 12057 : if (TMR_INDEX (exp) || TMR_INDEX2 (exp))
891 : return NULL_TREE;
892 33 : if (!integer_zerop (TMR_OFFSET (exp)))
893 : {
894 16 : poly_offset_int off = mem_ref_offset (exp);
895 16 : byte_offset += off.force_shwi ();
896 : }
897 33 : exp = TREE_OPERAND (base, 0);
898 : }
899 721304 : goto done;
900 : }
901 :
902 183305003 : default:
903 183305003 : goto done;
904 : }
905 :
906 57908156 : exp = TREE_OPERAND (exp, 0);
907 57908156 : }
908 218622921 : done:
909 :
910 218622921 : *poffset = byte_offset;
911 218622921 : return exp;
912 : }
913 :
914 : /* Returns the base object and a constant BITS_PER_UNIT offset in *POFFSET that
915 : denotes the starting address of the memory access EXP.
916 : Returns NULL_TREE if the offset is not constant or any component
917 : is not BITS_PER_UNIT-aligned. */
918 :
919 : tree
920 85984538 : get_addr_base_and_unit_offset (tree exp, poly_int64 *poffset)
921 : {
922 85984538 : return get_addr_base_and_unit_offset_1 (exp, poffset, NULL);
923 : }
924 :
925 : /* Returns true if STMT references an SSA_NAME that has
926 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI set, otherwise false. */
927 :
928 : bool
929 18003532 : stmt_references_abnormal_ssa_name (gimple *stmt)
930 : {
931 18003532 : ssa_op_iter oi;
932 18003532 : use_operand_p use_p;
933 :
934 40216319 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE)
935 : {
936 22213522 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (use_p)))
937 : return true;
938 : }
939 :
940 : return false;
941 : }
942 :
943 : /* If STMT takes any abnormal PHI values as input, replace them with
944 : local copies. */
945 :
946 : void
947 1892 : replace_abnormal_ssa_names (gimple *stmt)
948 : {
949 1892 : ssa_op_iter oi;
950 1892 : use_operand_p use_p;
951 :
952 3776 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE)
953 : {
954 1884 : tree op = USE_FROM_PTR (use_p);
955 1884 : if (TREE_CODE (op) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
956 : {
957 20 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
958 20 : tree new_name = make_ssa_name (TREE_TYPE (op));
959 20 : gassign *assign = gimple_build_assign (new_name, op);
960 20 : gsi_insert_before (&gsi, assign, GSI_SAME_STMT);
961 20 : SET_USE (use_p, new_name);
962 : }
963 : }
964 1892 : }
965 :
966 : /* Pair of tree and a sorting index, for dump_enumerated_decls. */
967 : struct GTY(()) numbered_tree
968 : {
969 : tree t;
970 : int num;
971 : };
972 :
973 :
974 : /* Compare two declarations references by their DECL_UID / sequence number.
975 : Called via qsort. */
976 :
977 : static int
978 367723 : compare_decls_by_uid (const void *pa, const void *pb)
979 : {
980 367723 : const numbered_tree *nt_a = ((const numbered_tree *)pa);
981 367723 : const numbered_tree *nt_b = ((const numbered_tree *)pb);
982 :
983 367723 : if (DECL_UID (nt_a->t) != DECL_UID (nt_b->t))
984 299673 : return DECL_UID (nt_a->t) - DECL_UID (nt_b->t);
985 68050 : return nt_a->num - nt_b->num;
986 : }
987 :
988 : /* Called via walk_gimple_stmt / walk_gimple_op by dump_enumerated_decls. */
989 : static tree
990 114300 : dump_enumerated_decls_push (tree *tp, int *walk_subtrees, void *data)
991 : {
992 114300 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
993 114300 : vec<numbered_tree> *list = (vec<numbered_tree> *) wi->info;
994 114300 : numbered_tree nt;
995 :
996 114300 : if (!DECL_P (*tp))
997 : return NULL_TREE;
998 20785 : nt.t = *tp;
999 20785 : nt.num = list->length ();
1000 20785 : list->safe_push (nt);
1001 20785 : *walk_subtrees = 0;
1002 20785 : return NULL_TREE;
1003 : }
1004 :
1005 : /* Find all the declarations used by the current function, sort them by uid,
1006 : and emit the sorted list. Each declaration is tagged with a sequence
1007 : number indicating when it was found during statement / tree walking,
1008 : so that TDF_NOUID comparisons of anonymous declarations are still
1009 : meaningful. Where a declaration was encountered more than once, we
1010 : emit only the sequence number of the first encounter.
1011 : FILE is the dump file where to output the list and FLAGS is as in
1012 : print_generic_expr. */
1013 : void
1014 3965 : dump_enumerated_decls (FILE *file, dump_flags_t flags)
1015 : {
1016 3965 : if (!cfun->cfg)
1017 4 : return;
1018 :
1019 3961 : basic_block bb;
1020 3961 : struct walk_stmt_info wi;
1021 3961 : auto_vec<numbered_tree, 40> decl_list;
1022 :
1023 3961 : memset (&wi, '\0', sizeof (wi));
1024 3961 : wi.info = (void *) &decl_list;
1025 15241 : FOR_EACH_BB_FN (bb, cfun)
1026 : {
1027 11280 : gimple_stmt_iterator gsi;
1028 :
1029 79207 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1030 56647 : if (!is_gimple_debug (gsi_stmt (gsi)))
1031 36131 : walk_gimple_stmt (&gsi, NULL, dump_enumerated_decls_push, &wi);
1032 : }
1033 3961 : decl_list.qsort (compare_decls_by_uid);
1034 3961 : if (decl_list.length ())
1035 : {
1036 3577 : unsigned ix;
1037 3577 : numbered_tree *ntp;
1038 3577 : tree last = NULL_TREE;
1039 :
1040 3577 : fprintf (file, "Declarations used by %s, sorted by DECL_UID:\n",
1041 : current_function_name ());
1042 31900 : FOR_EACH_VEC_ELT (decl_list, ix, ntp)
1043 : {
1044 20785 : if (ntp->t == last)
1045 7701 : continue;
1046 13084 : fprintf (file, "%d: ", ntp->num);
1047 13084 : print_generic_decl (file, ntp->t, flags);
1048 13084 : fprintf (file, "\n");
1049 13084 : last = ntp->t;
1050 : }
1051 : }
1052 3961 : }
|