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 76553971 : renumber_gimple_stmt_uids_in_block (struct function *fun, basic_block bb)
67 : {
68 76553971 : gimple_stmt_iterator bsi;
69 103743800 : for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
70 : {
71 27189829 : gimple *stmt = gsi_stmt (bsi);
72 27189829 : gimple_set_uid (stmt, inc_gimple_stmt_max_uid (fun));
73 : }
74 645877260 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
75 : {
76 492769318 : gimple *stmt = gsi_stmt (bsi);
77 492769318 : gimple_set_uid (stmt, inc_gimple_stmt_max_uid (fun));
78 : }
79 76553971 : }
80 :
81 : /* Renumber all of the gimple stmt uids. */
82 :
83 : void
84 6729599 : renumber_gimple_stmt_uids (struct function *fun)
85 : {
86 6729599 : basic_block bb;
87 :
88 6729599 : set_gimple_stmt_max_uid (fun, 0);
89 78738471 : FOR_ALL_BB_FN (bb, fun)
90 72008872 : renumber_gimple_stmt_uids_in_block (fun, bb);
91 6729599 : }
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 652702 : renumber_gimple_stmt_uids_in_blocks (basic_block *blocks, int n_blocks)
98 : {
99 652702 : int i;
100 :
101 652702 : set_gimple_stmt_max_uid (cfun, 0);
102 4817687 : for (i = 0; i < n_blocks; i++)
103 4164985 : renumber_gimple_stmt_uids_in_block (cfun, blocks[i]);
104 652702 : }
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 13424 : for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
267 5315 : gsi_next (&si))
268 : {
269 5315 : gphi *phi = si.phi ();
270 5315 : dfa_stats_p->num_phis++;
271 5315 : dfa_stats_p->num_phi_args += gimple_phi_num_args (phi);
272 5315 : 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 30739 : for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
277 14521 : gsi_next (&si))
278 : {
279 14521 : gimple *stmt = gsi_stmt (si);
280 14521 : dfa_stats_p->num_defs += NUM_SSA_OPERANDS (stmt, SSA_OP_DEF);
281 14521 : dfa_stats_p->num_uses += NUM_SSA_OPERANDS (stmt, SSA_OP_USE);
282 27038 : dfa_stats_p->num_vdefs += gimple_vdef (stmt) ? 1 : 0;
283 27038 : 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 584104624 : ssa_default_def (struct function *fn, tree var)
298 : {
299 584104624 : struct tree_decl_minimal ind;
300 584104624 : struct tree_ssa_name in;
301 584104624 : 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 584104624 : if (!fn->gimple_df)
307 : return NULL_TREE;
308 :
309 584104624 : in.var = (tree)&ind;
310 584104624 : ind.uid = DECL_UID (var);
311 584104624 : 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 12789871 : set_ssa_default_def (struct function *fn, tree var, tree def)
319 : {
320 12789871 : struct tree_decl_minimal ind;
321 12789871 : struct tree_ssa_name in;
322 :
323 12789871 : gcc_assert (VAR_P (var)
324 : || TREE_CODE (var) == PARM_DECL
325 : || TREE_CODE (var) == RESULT_DECL);
326 12789871 : in.var = (tree)&ind;
327 12789871 : ind.uid = DECL_UID (var);
328 12789871 : if (!def)
329 : {
330 3629883 : tree *loc = DEFAULT_DEFS (fn)->find_slot_with_hash ((tree)&in,
331 1209961 : DECL_UID (var),
332 : NO_INSERT);
333 1209961 : if (loc)
334 : {
335 1209923 : SSA_NAME_IS_DEFAULT_DEF (*(tree *)loc) = false;
336 1209923 : DEFAULT_DEFS (fn)->clear_slot (loc);
337 : }
338 1209961 : return;
339 : }
340 11579910 : gcc_assert (TREE_CODE (def) == SSA_NAME && SSA_NAME_VAR (def) == var);
341 34739730 : tree *loc = DEFAULT_DEFS (fn)->find_slot_with_hash ((tree)&in,
342 11579910 : DECL_UID (var), INSERT);
343 :
344 : /* Default definition might be changed by tail call optimization. */
345 11579910 : if (*loc)
346 1660 : SSA_NAME_IS_DEFAULT_DEF (*loc) = false;
347 :
348 : /* Mark DEF as the default definition for VAR. */
349 11579910 : *loc = def;
350 11579910 : SSA_NAME_IS_DEFAULT_DEF (def) = true;
351 : }
352 :
353 : /* Retrieve or create a default definition for VAR. */
354 :
355 : tree
356 27817298 : get_or_create_ssa_default_def (struct function *fn, tree var)
357 : {
358 27817298 : tree ddef = ssa_default_def (fn, var);
359 27817298 : if (ddef == NULL_TREE)
360 : {
361 11045410 : ddef = make_ssa_name_fn (fn, var, gimple_build_nop ());
362 11045410 : set_ssa_default_def (fn, var, ddef);
363 : }
364 27817298 : 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 3336264123 : get_ref_base_and_extent (tree exp, poly_int64 *poffset,
377 : poly_int64 *psize,
378 : poly_int64 *pmax_size,
379 : bool *preverse)
380 : {
381 3336264123 : poly_offset_int bitsize = -1;
382 3336264123 : poly_offset_int maxsize;
383 3336264123 : tree size_tree = NULL_TREE;
384 3336264123 : poly_offset_int bit_offset = 0;
385 3336264123 : 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 3336264123 : if (TREE_CODE (exp) == COMPONENT_REF)
390 1637996907 : size_tree = DECL_SIZE (TREE_OPERAND (exp, 1));
391 1698267216 : else if (TREE_CODE (exp) == BIT_FIELD_REF)
392 11842242 : size_tree = TREE_OPERAND (exp, 1);
393 1686424974 : 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 1686424800 : else if (!VOID_TYPE_P (TREE_TYPE (exp)))
399 : {
400 1642523033 : machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
401 1642523033 : if (mode == BLKmode)
402 392366259 : size_tree = TYPE_SIZE (TREE_TYPE (exp));
403 : else
404 2500313548 : bitsize = GET_MODE_BITSIZE (mode);
405 : }
406 1250156774 : if (size_tree != NULL_TREE
407 2042205582 : && poly_int_tree_p (size_tree))
408 2041724147 : bitsize = wi::to_poly_offset (size_tree);
409 :
410 3336264123 : *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 3336264123 : maxsize = bitsize;
415 :
416 : /* Compute cumulative bit-offset for nested component-refs and array-refs,
417 : and find the ultimate containing object. */
418 9847120369 : while (1)
419 : {
420 6591692246 : switch (TREE_CODE (exp))
421 : {
422 11842242 : case BIT_FIELD_REF:
423 11842242 : bit_offset += wi::to_poly_offset (TREE_OPERAND (exp, 2));
424 11842242 : break;
425 :
426 2441710182 : case COMPONENT_REF:
427 2441710182 : {
428 2441710182 : tree field = TREE_OPERAND (exp, 1);
429 2441710182 : tree this_offset = component_ref_field_offset (exp);
430 :
431 2441710182 : if (this_offset && poly_int_tree_p (this_offset))
432 : {
433 2441707176 : poly_offset_int woffset = (wi::to_poly_offset (this_offset)
434 2441707176 : << LOG2_BITS_PER_UNIT);
435 2441707176 : woffset += wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
436 2441707176 : 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 2441707176 : if (seen_variable_array_ref)
443 : {
444 22710359 : tree stype = TREE_TYPE (TREE_OPERAND (exp, 0));
445 22710359 : tree next = DECL_CHAIN (field);
446 66333125 : while (next && TREE_CODE (next) != FIELD_DECL)
447 43622766 : next = DECL_CHAIN (next);
448 22710359 : if (!next
449 3204479 : || TREE_CODE (stype) != RECORD_TYPE)
450 : {
451 20276246 : tree fsize = DECL_SIZE (field);
452 20276246 : tree ssize = TYPE_SIZE (stype);
453 20276246 : if (fsize == NULL
454 12809672 : || !poly_int_tree_p (fsize)
455 12807549 : || ssize == NULL
456 33083795 : || !poly_int_tree_p (ssize))
457 7468697 : maxsize = -1;
458 12807549 : else if (known_size_p (maxsize))
459 : {
460 12807399 : poly_offset_int tem
461 12807399 : = (wi::to_poly_offset (ssize)
462 12807399 : - wi::to_poly_offset (fsize));
463 25614798 : tem -= woffset;
464 12807399 : 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 3006 : 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 3006 : if (known_size_p (maxsize)
481 1996 : && csize
482 5002 : && poly_int_tree_p (csize))
483 0 : maxsize = wi::to_poly_offset (csize) - bit_offset;
484 : else
485 3006 : maxsize = -1;
486 : }
487 : }
488 : break;
489 :
490 781878516 : case ARRAY_REF:
491 781878516 : case ARRAY_RANGE_REF:
492 781878516 : {
493 781878516 : tree index = TREE_OPERAND (exp, 1);
494 781878516 : tree low_bound, unit_size;
495 :
496 : /* If the resulting bit-offset is constant, track it. */
497 781878516 : if (poly_int_tree_p (index)
498 675739962 : && (low_bound = array_ref_low_bound (exp),
499 675739962 : poly_int_tree_p (low_bound))
500 1457609210 : && (unit_size = array_ref_element_size (exp),
501 675730694 : TREE_CODE (unit_size) == INTEGER_CST))
502 : {
503 675658499 : poly_offset_int woffset
504 675658499 : = wi::sext (wi::to_poly_offset (index)
505 1351316998 : - wi::to_poly_offset (low_bound),
506 675658499 : TYPE_PRECISION (sizetype));
507 675658499 : woffset *= wi::to_offset (unit_size);
508 675658499 : woffset <<= LOG2_BITS_PER_UNIT;
509 675658499 : 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 675658499 : seen_variable_array_ref = false;
515 : }
516 : else
517 : {
518 106220017 : 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 106220017 : if (known_size_p (maxsize)
523 106018675 : && asize
524 192776297 : && poly_int_tree_p (asize))
525 83490954 : maxsize = wi::to_poly_offset (asize) - bit_offset;
526 : else
527 22729063 : maxsize = -1;
528 :
529 : /* Remember that we have seen an array ref with a variable
530 : index. */
531 106220017 : 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 106220017 : range_query *query = get_range_query (cfun);
541 106220017 : range_op_handler minus_op (MINUS_EXPR);
542 106220017 : int_range_max vr_idx, vr_lb, vr;
543 :
544 106220017 : if (TREE_CODE (index) == SSA_NAME
545 105707231 : && (low_bound = array_ref_low_bound (exp),
546 105707231 : TREE_CODE (low_bound) == INTEGER_CST)
547 105706855 : && (unit_size = array_ref_element_size (exp),
548 105706855 : TREE_CODE (unit_size) == INTEGER_CST)
549 105536302 : && query->range_of_expr (vr, index)
550 105536302 : && (integer_zerop (low_bound)
551 496796 : || ((vr_idx = vr,
552 496796 : query->range_of_expr (vr_lb, low_bound))
553 46545028 : && minus_op.fold_range (vr, TREE_TYPE (index),
554 : vr_idx, vr_lb)))
555 105536302 : && !vr.varying_p ()
556 166396738 : && !vr.undefined_p ())
557 : {
558 60171785 : wide_int min = vr.lower_bound ();
559 60171785 : wide_int max = vr.upper_bound ();
560 : /* Try to constrain maxsize with range information. */
561 60171785 : offset_int omax
562 60171785 : = offset_int::from (max, TYPE_SIGN (TREE_TYPE (index)));
563 60171785 : if (wi::get_precision (max) <= ADDR_MAX_BITSIZE
564 60171785 : && omax >= 0)
565 : {
566 60061762 : offset_int rmaxsize
567 60061762 : = (omax + 1)
568 120123524 : * wi::to_offset (unit_size) << LOG2_BITS_PER_UNIT;
569 60061762 : if (!known_size_p (maxsize)
570 107510654 : || known_lt (rmaxsize, maxsize))
571 : {
572 : /* If we know an upper bound below the declared
573 : one this is no longer variable. */
574 22964824 : if (known_size_p (maxsize))
575 : seen_variable_array_ref = false;
576 22964824 : maxsize = rmaxsize;
577 : }
578 : }
579 : /* Try to adjust bit_offset with range information. */
580 60171785 : offset_int omin
581 60171785 : = offset_int::from (min, TYPE_SIGN (TREE_TYPE (index)));
582 60171785 : if (wi::get_precision (min) <= ADDR_MAX_BITSIZE
583 60171785 : && omin > 0)
584 : {
585 14996106 : offset_int woffset
586 14996106 : = wi::sext (omin, TYPE_PRECISION (sizetype));
587 14996106 : woffset *= wi::to_offset (unit_size);
588 14996106 : woffset <<= LOG2_BITS_PER_UNIT;
589 14996106 : bit_offset += woffset;
590 14996106 : if (known_size_p (maxsize))
591 14996106 : maxsize -= woffset;
592 : }
593 60171785 : }
594 106220017 : }
595 : }
596 : break;
597 :
598 : case REALPART_EXPR:
599 : break;
600 :
601 : case IMAGPART_EXPR:
602 3255428123 : bit_offset += bitsize;
603 : break;
604 :
605 : case VIEW_CONVERT_EXPR:
606 : break;
607 :
608 55869559 : 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 55869559 : if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
612 55869559 : && (TMR_INDEX (exp) || TMR_INDEX2 (exp)))
613 : {
614 7031116 : exp = TREE_OPERAND (TMR_BASE (exp), 0);
615 7031116 : bit_offset = 0;
616 7031116 : maxsize = -1;
617 7031116 : goto done;
618 : }
619 : /* Fallthru. */
620 1264948937 : 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 1264948937 : if (seen_variable_array_ref
632 32038013 : && known_size_p (maxsize)
633 1282781601 : && (TYPE_SIZE (TREE_TYPE (exp)) == NULL_TREE
634 11911985 : || !poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp)))
635 9959778 : || (maybe_eq
636 9959778 : (bit_offset + maxsize,
637 1274908715 : wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp)))))))
638 17832510 : maxsize = -1;
639 :
640 : /* Hand back the decl for MEM[&decl, off]. */
641 1264948937 : if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
642 : {
643 558808761 : if (integer_zerop (TREE_OPERAND (exp, 1)))
644 218348653 : exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
645 : else
646 : {
647 340460108 : poly_offset_int off = mem_ref_offset (exp);
648 340460108 : off <<= LOG2_BITS_PER_UNIT;
649 340460108 : off += bit_offset;
650 340460108 : poly_int64 off_hwi;
651 340460108 : if (off.to_shwi (&off_hwi))
652 : {
653 340458520 : bit_offset = off_hwi;
654 340458520 : exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
655 : }
656 : }
657 : }
658 1264948937 : goto done;
659 :
660 2064284070 : default:
661 2064284070 : goto done;
662 : }
663 :
664 3255428123 : exp = TREE_OPERAND (exp, 0);
665 3255428123 : }
666 :
667 3336264123 : done:
668 3336264123 : if (!bitsize.to_shwi (psize) || maybe_lt (*psize, 0))
669 : {
670 44383231 : *poffset = 0;
671 44383231 : *psize = -1;
672 44383231 : *pmax_size = -1;
673 :
674 44383231 : 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 3291880892 : if (!bit_offset.to_shwi (poffset))
681 : {
682 7346 : *poffset = 0;
683 7346 : *pmax_size = -1;
684 :
685 7346 : return exp;
686 : }
687 :
688 : /* In case of a decl or constant base object we can do better. */
689 :
690 3291873546 : if (DECL_P (exp))
691 : {
692 2555533553 : if (VAR_P (exp)
693 2555533553 : && ((flag_unconstrained_commons && DECL_COMMON (exp))
694 2493330917 : || (DECL_EXTERNAL (exp) && seen_variable_array_ref)))
695 : {
696 544540 : 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 544540 : if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
700 544540 : || (seen_variable_array_ref
701 35469 : && (sz_tree == NULL_TREE
702 35469 : || !poly_int_tree_p (sz_tree)
703 35469 : || maybe_eq (bit_offset + maxsize,
704 580009 : wi::to_poly_offset (sz_tree)))))
705 543824 : maxsize = -1;
706 : }
707 : /* If maxsize is unknown adjust it according to the size of the
708 : base decl. */
709 2554989013 : else if (!known_size_p (maxsize)
710 15754868 : && DECL_SIZE (exp)
711 2570738950 : && poly_int_tree_p (DECL_SIZE (exp)))
712 15749875 : maxsize = wi::to_poly_offset (DECL_SIZE (exp)) - bit_offset;
713 : }
714 736339993 : 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 7821455 : if (!known_size_p (maxsize)
719 35019 : && TYPE_SIZE (TREE_TYPE (exp))
720 7856474 : && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (exp))))
721 35019 : maxsize = (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (exp)))
722 35019 : - bit_offset);
723 : }
724 :
725 3291873546 : if (!maxsize.to_shwi (pmax_size)
726 3291873546 : || maybe_lt (*pmax_size, 0)
727 6559936199 : || !endpoint_representable_p (*poffset, *pmax_size))
728 23810930 : *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 6583747092 : 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 56090531 : get_ref_base_and_extent_hwi (tree exp, HOST_WIDE_INT *poffset,
750 : HOST_WIDE_INT *psize, bool *preverse)
751 : {
752 56090531 : poly_int64 offset, size, max_size;
753 56090531 : HOST_WIDE_INT const_offset, const_size;
754 56090531 : bool reverse;
755 56090531 : tree decl = get_ref_base_and_extent (exp, &offset, &size, &max_size,
756 : &reverse);
757 56090531 : if (!offset.is_constant (&const_offset)
758 56090531 : || !size.is_constant (&const_size)
759 56090531 : || const_offset < 0
760 56089742 : || !known_size_p (max_size)
761 55445335 : || maybe_ne (max_size, const_size))
762 : return NULL_TREE;
763 :
764 54315867 : *poffset = const_offset;
765 54315867 : *psize = const_size;
766 54315867 : *preverse = reverse;
767 54315867 : 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 234272079 : get_addr_base_and_unit_offset_1 (tree exp, poly_int64 *poffset,
779 : tree (*valueize) (tree))
780 : {
781 234272079 : 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 356761373 : while (1)
786 : {
787 295516726 : switch (TREE_CODE (exp))
788 : {
789 21596 : case BIT_FIELD_REF:
790 21596 : {
791 21596 : poly_int64 this_byte_offset;
792 21596 : poly_uint64 this_bit_offset;
793 21596 : if (!poly_int_tree_p (TREE_OPERAND (exp, 2), &this_bit_offset)
794 43192 : || !multiple_p (this_bit_offset, BITS_PER_UNIT,
795 : &this_byte_offset))
796 0 : return NULL_TREE;
797 21596 : byte_offset += this_byte_offset;
798 : }
799 21596 : break;
800 :
801 56369321 : case COMPONENT_REF:
802 56369321 : {
803 56369321 : tree field = TREE_OPERAND (exp, 1);
804 56369321 : tree this_offset = component_ref_field_offset (exp);
805 56369321 : poly_int64 hthis_offset;
806 :
807 56369321 : if (!this_offset
808 56369321 : || !poly_int_tree_p (this_offset, &hthis_offset)
809 112735845 : || (TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field))
810 : % BITS_PER_UNIT))
811 3792 : return NULL_TREE;
812 :
813 56365529 : hthis_offset += (TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (field))
814 56365529 : / BITS_PER_UNIT);
815 56365529 : byte_offset += hthis_offset;
816 : }
817 56365529 : break;
818 :
819 8719010 : case ARRAY_REF:
820 8719010 : case ARRAY_RANGE_REF:
821 8719010 : {
822 8719010 : tree index = TREE_OPERAND (exp, 1);
823 8719010 : tree low_bound, unit_size;
824 :
825 8719010 : if (valueize
826 1679829 : && TREE_CODE (index) == SSA_NAME)
827 1080978 : index = (*valueize) (index);
828 8719010 : if (!poly_int_tree_p (index))
829 4281336 : return NULL_TREE;
830 4453330 : low_bound = array_ref_low_bound (exp);
831 4453330 : if (valueize
832 873215 : && TREE_CODE (low_bound) == SSA_NAME)
833 4232 : low_bound = (*valueize) (low_bound);
834 4453330 : if (!poly_int_tree_p (low_bound))
835 : return NULL_TREE;
836 4453330 : unit_size = array_ref_element_size (exp);
837 4453330 : if (TREE_CODE (unit_size) != INTEGER_CST)
838 : return NULL_TREE;
839 :
840 : /* If the resulting bit-offset is constant, track it. */
841 4437674 : poly_offset_int woffset
842 4437674 : = wi::sext (wi::to_poly_offset (index)
843 8875348 : - wi::to_poly_offset (low_bound),
844 4437674 : TYPE_PRECISION (sizetype));
845 4437674 : woffset *= wi::to_offset (unit_size);
846 4437674 : byte_offset += woffset.force_shwi ();
847 : }
848 4437674 : break;
849 :
850 : case REALPART_EXPR:
851 : break;
852 :
853 26537 : case IMAGPART_EXPR:
854 26537 : byte_offset += TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (exp)));
855 26537 : break;
856 :
857 : case VIEW_CONVERT_EXPR:
858 : break;
859 :
860 35560055 : case MEM_REF:
861 35560055 : {
862 35560055 : tree base = TREE_OPERAND (exp, 0);
863 35560055 : if (valueize
864 6763907 : && TREE_CODE (base) == SSA_NAME)
865 6288640 : base = (*valueize) (base);
866 :
867 : /* Hand back the decl for MEM[&decl, off]. */
868 35560055 : if (TREE_CODE (base) == ADDR_EXPR)
869 : {
870 21099114 : if (!integer_zerop (TREE_OPERAND (exp, 1)))
871 : {
872 17685663 : poly_offset_int off = mem_ref_offset (exp);
873 17685663 : byte_offset += off.force_shwi ();
874 : }
875 21099114 : exp = TREE_OPERAND (base, 0);
876 : }
877 35560055 : goto done;
878 : }
879 :
880 738295 : case TARGET_MEM_REF:
881 738295 : {
882 738295 : tree base = TREE_OPERAND (exp, 0);
883 738295 : if (valueize
884 2570 : && TREE_CODE (base) == SSA_NAME)
885 1913 : base = (*valueize) (base);
886 :
887 : /* Hand back the decl for MEM[&decl, off]. */
888 738295 : if (TREE_CODE (base) == ADDR_EXPR)
889 : {
890 12096 : if (TMR_INDEX (exp) || TMR_INDEX2 (exp))
891 : return NULL_TREE;
892 33 : if (!integer_zerop (TMR_OFFSET (exp)))
893 : {
894 17 : poly_offset_int off = mem_ref_offset (exp);
895 17 : byte_offset += off.force_shwi ();
896 : }
897 33 : exp = TREE_OPERAND (base, 0);
898 : }
899 726232 : goto done;
900 : }
901 :
902 193688601 : default:
903 193688601 : goto done;
904 : }
905 :
906 61244647 : exp = TREE_OPERAND (exp, 0);
907 61244647 : }
908 229974888 : done:
909 :
910 229974888 : *poffset = byte_offset;
911 229974888 : 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 96190187 : get_addr_base_and_unit_offset (tree exp, poly_int64 *poffset)
921 : {
922 96190187 : 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 18251300 : stmt_references_abnormal_ssa_name (gimple *stmt)
930 : {
931 18251300 : ssa_op_iter oi;
932 18251300 : use_operand_p use_p;
933 :
934 40789490 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE)
935 : {
936 22538929 : 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 1896 : replace_abnormal_ssa_names (gimple *stmt)
948 : {
949 1896 : ssa_op_iter oi;
950 1896 : use_operand_p use_p;
951 :
952 3784 : FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE)
953 : {
954 1888 : tree op = USE_FROM_PTR (use_p);
955 1888 : 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 1896 : }
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 368195 : compare_decls_by_uid (const void *pa, const void *pb)
979 : {
980 368195 : const numbered_tree *nt_a = ((const numbered_tree *)pa);
981 368195 : const numbered_tree *nt_b = ((const numbered_tree *)pb);
982 :
983 368195 : if (DECL_UID (nt_a->t) != DECL_UID (nt_b->t))
984 300065 : return DECL_UID (nt_a->t) - DECL_UID (nt_b->t);
985 68130 : 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 114632 : dump_enumerated_decls_push (tree *tp, int *walk_subtrees, void *data)
991 : {
992 114632 : struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
993 114632 : vec<numbered_tree> *list = (vec<numbered_tree> *) wi->info;
994 114632 : numbered_tree nt;
995 :
996 114632 : if (!DECL_P (*tp))
997 : return NULL_TREE;
998 20809 : nt.t = *tp;
999 20809 : nt.num = list->length ();
1000 20809 : list->safe_push (nt);
1001 20809 : *walk_subtrees = 0;
1002 20809 : 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 3971 : dump_enumerated_decls (FILE *file, dump_flags_t flags)
1015 : {
1016 3971 : if (!cfun->cfg)
1017 4 : return;
1018 :
1019 3967 : basic_block bb;
1020 3967 : struct walk_stmt_info wi;
1021 3967 : auto_vec<numbered_tree, 40> decl_list;
1022 :
1023 3967 : memset (&wi, '\0', sizeof (wi));
1024 3967 : wi.info = (void *) &decl_list;
1025 15273 : FOR_EACH_BB_FN (bb, cfun)
1026 : {
1027 11306 : gimple_stmt_iterator gsi;
1028 :
1029 79463 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1030 56851 : if (!is_gimple_debug (gsi_stmt (gsi)))
1031 36259 : walk_gimple_stmt (&gsi, NULL, dump_enumerated_decls_push, &wi);
1032 : }
1033 3967 : decl_list.qsort (compare_decls_by_uid);
1034 3967 : if (decl_list.length ())
1035 : {
1036 3579 : unsigned ix;
1037 3579 : numbered_tree *ntp;
1038 3579 : tree last = NULL_TREE;
1039 :
1040 3579 : fprintf (file, "Declarations used by %s, sorted by DECL_UID:\n",
1041 : current_function_name ());
1042 31934 : FOR_EACH_VEC_ELT (decl_list, ix, ntp)
1043 : {
1044 20809 : if (ntp->t == last)
1045 7713 : continue;
1046 13096 : fprintf (file, "%d: ", ntp->num);
1047 13096 : print_generic_decl (file, ntp->t, flags);
1048 13096 : fprintf (file, "\n");
1049 13096 : last = ntp->t;
1050 : }
1051 : }
1052 3967 : }
|