* testsuite/17_intro/static.cc: Ignore AIX TOC reload warnings.
[official-gcc.git] / gcc / tree-dfa.c
blob0d1eb999ccbb24812e5df9a54cb8f1f38b7b275b
1 /* Data flow functions for trees.
2 Copyright (C) 2001-2013 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "hashtab.h"
26 #include "pointer-set.h"
27 #include "tree.h"
28 #include "stor-layout.h"
29 #include "tm_p.h"
30 #include "basic-block.h"
31 #include "ggc.h"
32 #include "langhooks.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "tree-pretty-print.h"
36 #include "gimple.h"
37 #include "gimple-iterator.h"
38 #include "gimple-walk.h"
39 #include "gimple-ssa.h"
40 #include "tree-phinodes.h"
41 #include "ssa-iterators.h"
42 #include "stringpool.h"
43 #include "tree-ssanames.h"
44 #include "expr.h"
45 #include "tree-dfa.h"
46 #include "tree-inline.h"
47 #include "tree-pass.h"
48 #include "convert.h"
49 #include "params.h"
51 /* Build and maintain data flow information for trees. */
53 /* Counters used to display DFA and SSA statistics. */
54 struct dfa_stats_d
56 long num_defs;
57 long num_uses;
58 long num_phis;
59 long num_phi_args;
60 size_t max_num_phi_args;
61 long num_vdefs;
62 long num_vuses;
66 /* Local functions. */
67 static void collect_dfa_stats (struct dfa_stats_d *);
70 /*---------------------------------------------------------------------------
71 Dataflow analysis (DFA) routines
72 ---------------------------------------------------------------------------*/
74 /* Renumber all of the gimple stmt uids. */
76 void
77 renumber_gimple_stmt_uids (void)
79 basic_block bb;
81 set_gimple_stmt_max_uid (cfun, 0);
82 FOR_ALL_BB (bb)
84 gimple_stmt_iterator bsi;
85 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
87 gimple stmt = gsi_stmt (bsi);
88 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
90 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
92 gimple stmt = gsi_stmt (bsi);
93 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
98 /* Like renumber_gimple_stmt_uids, but only do work on the basic blocks
99 in BLOCKS, of which there are N_BLOCKS. Also renumbers PHIs. */
101 void
102 renumber_gimple_stmt_uids_in_blocks (basic_block *blocks, int n_blocks)
104 int i;
106 set_gimple_stmt_max_uid (cfun, 0);
107 for (i = 0; i < n_blocks; i++)
109 basic_block bb = blocks[i];
110 gimple_stmt_iterator bsi;
111 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
113 gimple stmt = gsi_stmt (bsi);
114 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
116 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
118 gimple stmt = gsi_stmt (bsi);
119 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
126 /*---------------------------------------------------------------------------
127 Debugging functions
128 ---------------------------------------------------------------------------*/
130 /* Dump variable VAR and its may-aliases to FILE. */
132 void
133 dump_variable (FILE *file, tree var)
135 if (TREE_CODE (var) == SSA_NAME)
137 if (POINTER_TYPE_P (TREE_TYPE (var)))
138 dump_points_to_info_for (file, var);
139 var = SSA_NAME_VAR (var);
142 if (var == NULL_TREE)
144 fprintf (file, "<nil>");
145 return;
148 print_generic_expr (file, var, dump_flags);
150 fprintf (file, ", UID D.%u", (unsigned) DECL_UID (var));
151 if (DECL_PT_UID (var) != DECL_UID (var))
152 fprintf (file, ", PT-UID D.%u", (unsigned) DECL_PT_UID (var));
154 fprintf (file, ", ");
155 print_generic_expr (file, TREE_TYPE (var), dump_flags);
157 if (TREE_ADDRESSABLE (var))
158 fprintf (file, ", is addressable");
160 if (is_global_var (var))
161 fprintf (file, ", is global");
163 if (TREE_THIS_VOLATILE (var))
164 fprintf (file, ", is volatile");
166 if (cfun && ssa_default_def (cfun, var))
168 fprintf (file, ", default def: ");
169 print_generic_expr (file, ssa_default_def (cfun, var), dump_flags);
172 if (DECL_INITIAL (var))
174 fprintf (file, ", initial: ");
175 print_generic_expr (file, DECL_INITIAL (var), dump_flags);
178 fprintf (file, "\n");
182 /* Dump variable VAR and its may-aliases to stderr. */
184 DEBUG_FUNCTION void
185 debug_variable (tree var)
187 dump_variable (stderr, var);
191 /* Dump various DFA statistics to FILE. */
193 void
194 dump_dfa_stats (FILE *file)
196 struct dfa_stats_d dfa_stats;
198 unsigned long size, total = 0;
199 const char * const fmt_str = "%-30s%-13s%12s\n";
200 const char * const fmt_str_1 = "%-30s%13lu%11lu%c\n";
201 const char * const fmt_str_3 = "%-43s%11lu%c\n";
202 const char *funcname
203 = lang_hooks.decl_printable_name (current_function_decl, 2);
205 collect_dfa_stats (&dfa_stats);
207 fprintf (file, "\nDFA Statistics for %s\n\n", funcname);
209 fprintf (file, "---------------------------------------------------------\n");
210 fprintf (file, fmt_str, "", " Number of ", "Memory");
211 fprintf (file, fmt_str, "", " instances ", "used ");
212 fprintf (file, "---------------------------------------------------------\n");
214 size = dfa_stats.num_uses * sizeof (tree *);
215 total += size;
216 fprintf (file, fmt_str_1, "USE operands", dfa_stats.num_uses,
217 SCALE (size), LABEL (size));
219 size = dfa_stats.num_defs * sizeof (tree *);
220 total += size;
221 fprintf (file, fmt_str_1, "DEF operands", dfa_stats.num_defs,
222 SCALE (size), LABEL (size));
224 size = dfa_stats.num_vuses * sizeof (tree *);
225 total += size;
226 fprintf (file, fmt_str_1, "VUSE operands", dfa_stats.num_vuses,
227 SCALE (size), LABEL (size));
229 size = dfa_stats.num_vdefs * sizeof (tree *);
230 total += size;
231 fprintf (file, fmt_str_1, "VDEF operands", dfa_stats.num_vdefs,
232 SCALE (size), LABEL (size));
234 size = dfa_stats.num_phis * sizeof (struct gimple_statement_phi);
235 total += size;
236 fprintf (file, fmt_str_1, "PHI nodes", dfa_stats.num_phis,
237 SCALE (size), LABEL (size));
239 size = dfa_stats.num_phi_args * sizeof (struct phi_arg_d);
240 total += size;
241 fprintf (file, fmt_str_1, "PHI arguments", dfa_stats.num_phi_args,
242 SCALE (size), LABEL (size));
244 fprintf (file, "---------------------------------------------------------\n");
245 fprintf (file, fmt_str_3, "Total memory used by DFA/SSA data", SCALE (total),
246 LABEL (total));
247 fprintf (file, "---------------------------------------------------------\n");
248 fprintf (file, "\n");
250 if (dfa_stats.num_phis)
251 fprintf (file, "Average number of arguments per PHI node: %.1f (max: %ld)\n",
252 (float) dfa_stats.num_phi_args / (float) dfa_stats.num_phis,
253 (long) dfa_stats.max_num_phi_args);
255 fprintf (file, "\n");
259 /* Dump DFA statistics on stderr. */
261 DEBUG_FUNCTION void
262 debug_dfa_stats (void)
264 dump_dfa_stats (stderr);
268 /* Collect DFA statistics and store them in the structure pointed to by
269 DFA_STATS_P. */
271 static void
272 collect_dfa_stats (struct dfa_stats_d *dfa_stats_p ATTRIBUTE_UNUSED)
274 basic_block bb;
276 gcc_assert (dfa_stats_p);
278 memset ((void *)dfa_stats_p, 0, sizeof (struct dfa_stats_d));
280 /* Walk all the statements in the function counting references. */
281 FOR_EACH_BB (bb)
283 gimple_stmt_iterator si;
285 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
287 gimple phi = gsi_stmt (si);
288 dfa_stats_p->num_phis++;
289 dfa_stats_p->num_phi_args += gimple_phi_num_args (phi);
290 if (gimple_phi_num_args (phi) > dfa_stats_p->max_num_phi_args)
291 dfa_stats_p->max_num_phi_args = gimple_phi_num_args (phi);
294 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
296 gimple stmt = gsi_stmt (si);
297 dfa_stats_p->num_defs += NUM_SSA_OPERANDS (stmt, SSA_OP_DEF);
298 dfa_stats_p->num_uses += NUM_SSA_OPERANDS (stmt, SSA_OP_USE);
299 dfa_stats_p->num_vdefs += gimple_vdef (stmt) ? 1 : 0;
300 dfa_stats_p->num_vuses += gimple_vuse (stmt) ? 1 : 0;
306 /*---------------------------------------------------------------------------
307 Miscellaneous helpers
308 ---------------------------------------------------------------------------*/
310 /* Lookup VAR UID in the default_defs hashtable and return the associated
311 variable. */
313 tree
314 ssa_default_def (struct function *fn, tree var)
316 struct tree_decl_minimal ind;
317 struct tree_ssa_name in;
318 gcc_assert (TREE_CODE (var) == VAR_DECL
319 || TREE_CODE (var) == PARM_DECL
320 || TREE_CODE (var) == RESULT_DECL);
321 in.var = (tree)&ind;
322 ind.uid = DECL_UID (var);
323 return (tree) htab_find_with_hash (DEFAULT_DEFS (fn), &in, DECL_UID (var));
326 /* Insert the pair VAR's UID, DEF into the default_defs hashtable
327 of function FN. */
329 void
330 set_ssa_default_def (struct function *fn, tree var, tree def)
332 struct tree_decl_minimal ind;
333 struct tree_ssa_name in;
334 void **loc;
336 gcc_assert (TREE_CODE (var) == VAR_DECL
337 || TREE_CODE (var) == PARM_DECL
338 || TREE_CODE (var) == RESULT_DECL);
339 in.var = (tree)&ind;
340 ind.uid = DECL_UID (var);
341 if (!def)
343 loc = htab_find_slot_with_hash (DEFAULT_DEFS (fn), &in,
344 DECL_UID (var), NO_INSERT);
345 if (*loc)
347 SSA_NAME_IS_DEFAULT_DEF (*(tree *)loc) = false;
348 htab_clear_slot (DEFAULT_DEFS (fn), loc);
350 return;
352 gcc_assert (TREE_CODE (def) == SSA_NAME && SSA_NAME_VAR (def) == var);
353 loc = htab_find_slot_with_hash (DEFAULT_DEFS (fn), &in,
354 DECL_UID (var), INSERT);
356 /* Default definition might be changed by tail call optimization. */
357 if (*loc)
358 SSA_NAME_IS_DEFAULT_DEF (*(tree *) loc) = false;
360 /* Mark DEF as the default definition for VAR. */
361 *(tree *) loc = def;
362 SSA_NAME_IS_DEFAULT_DEF (def) = true;
365 /* Retrieve or create a default definition for VAR. */
367 tree
368 get_or_create_ssa_default_def (struct function *fn, tree var)
370 tree ddef = ssa_default_def (fn, var);
371 if (ddef == NULL_TREE)
373 ddef = make_ssa_name_fn (fn, var, gimple_build_nop ());
374 set_ssa_default_def (fn, var, ddef);
376 return ddef;
380 /* If EXP is a handled component reference for a structure, return the
381 base variable. The access range is delimited by bit positions *POFFSET and
382 *POFFSET + *PMAX_SIZE. The access size is *PSIZE bits. If either
383 *PSIZE or *PMAX_SIZE is -1, they could not be determined. If *PSIZE
384 and *PMAX_SIZE are equal, the access is non-variable. */
386 tree
387 get_ref_base_and_extent (tree exp, HOST_WIDE_INT *poffset,
388 HOST_WIDE_INT *psize,
389 HOST_WIDE_INT *pmax_size)
391 HOST_WIDE_INT bitsize = -1;
392 HOST_WIDE_INT maxsize = -1;
393 tree size_tree = NULL_TREE;
394 double_int bit_offset = double_int_zero;
395 HOST_WIDE_INT hbit_offset;
396 bool seen_variable_array_ref = false;
398 /* First get the final access size from just the outermost expression. */
399 if (TREE_CODE (exp) == COMPONENT_REF)
400 size_tree = DECL_SIZE (TREE_OPERAND (exp, 1));
401 else if (TREE_CODE (exp) == BIT_FIELD_REF)
402 size_tree = TREE_OPERAND (exp, 1);
403 else if (!VOID_TYPE_P (TREE_TYPE (exp)))
405 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
406 if (mode == BLKmode)
407 size_tree = TYPE_SIZE (TREE_TYPE (exp));
408 else
409 bitsize = GET_MODE_BITSIZE (mode);
411 if (size_tree != NULL_TREE)
413 if (! tree_fits_uhwi_p (size_tree))
414 bitsize = -1;
415 else
416 bitsize = TREE_INT_CST_LOW (size_tree);
419 /* Initially, maxsize is the same as the accessed element size.
420 In the following it will only grow (or become -1). */
421 maxsize = bitsize;
423 /* Compute cumulative bit-offset for nested component-refs and array-refs,
424 and find the ultimate containing object. */
425 while (1)
427 switch (TREE_CODE (exp))
429 case BIT_FIELD_REF:
430 bit_offset += tree_to_double_int (TREE_OPERAND (exp, 2));
431 break;
433 case COMPONENT_REF:
435 tree field = TREE_OPERAND (exp, 1);
436 tree this_offset = component_ref_field_offset (exp);
438 if (this_offset && TREE_CODE (this_offset) == INTEGER_CST)
440 double_int doffset = tree_to_double_int (this_offset);
441 doffset = doffset.lshift (BITS_PER_UNIT == 8
442 ? 3 : exact_log2 (BITS_PER_UNIT));
443 doffset += tree_to_double_int (DECL_FIELD_BIT_OFFSET (field));
444 bit_offset = bit_offset + doffset;
446 /* If we had seen a variable array ref already and we just
447 referenced the last field of a struct or a union member
448 then we have to adjust maxsize by the padding at the end
449 of our field. */
450 if (seen_variable_array_ref && maxsize != -1)
452 tree stype = TREE_TYPE (TREE_OPERAND (exp, 0));
453 tree next = DECL_CHAIN (field);
454 while (next && TREE_CODE (next) != FIELD_DECL)
455 next = DECL_CHAIN (next);
456 if (!next
457 || TREE_CODE (stype) != RECORD_TYPE)
459 tree fsize = DECL_SIZE_UNIT (field);
460 tree ssize = TYPE_SIZE_UNIT (stype);
461 if (tree_fits_shwi_p (fsize)
462 && tree_fits_shwi_p (ssize)
463 && doffset.fits_shwi ())
464 maxsize += ((TREE_INT_CST_LOW (ssize)
465 - TREE_INT_CST_LOW (fsize))
466 * BITS_PER_UNIT
467 - doffset.to_shwi ());
468 else
469 maxsize = -1;
473 else
475 tree csize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
476 /* We need to adjust maxsize to the whole structure bitsize.
477 But we can subtract any constant offset seen so far,
478 because that would get us out of the structure otherwise. */
479 if (maxsize != -1
480 && csize
481 && tree_fits_uhwi_p (csize)
482 && bit_offset.fits_shwi ())
483 maxsize = TREE_INT_CST_LOW (csize)
484 - bit_offset.to_shwi ();
485 else
486 maxsize = -1;
489 break;
491 case ARRAY_REF:
492 case ARRAY_RANGE_REF:
494 tree index = TREE_OPERAND (exp, 1);
495 tree low_bound, unit_size;
497 /* If the resulting bit-offset is constant, track it. */
498 if (TREE_CODE (index) == INTEGER_CST
499 && (low_bound = array_ref_low_bound (exp),
500 TREE_CODE (low_bound) == INTEGER_CST)
501 && (unit_size = array_ref_element_size (exp),
502 TREE_CODE (unit_size) == INTEGER_CST))
504 double_int doffset
505 = (TREE_INT_CST (index) - TREE_INT_CST (low_bound))
506 .sext (TYPE_PRECISION (TREE_TYPE (index)));
507 doffset *= tree_to_double_int (unit_size);
508 doffset = doffset.lshift (BITS_PER_UNIT == 8
509 ? 3 : exact_log2 (BITS_PER_UNIT));
510 bit_offset = bit_offset + doffset;
512 /* An array ref with a constant index up in the structure
513 hierarchy will constrain the size of any variable array ref
514 lower in the access hierarchy. */
515 seen_variable_array_ref = false;
517 else
519 tree asize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
520 /* We need to adjust maxsize to the whole array bitsize.
521 But we can subtract any constant offset seen so far,
522 because that would get us outside of the array otherwise. */
523 if (maxsize != -1
524 && asize
525 && tree_fits_uhwi_p (asize)
526 && bit_offset.fits_shwi ())
527 maxsize = TREE_INT_CST_LOW (asize)
528 - bit_offset.to_shwi ();
529 else
530 maxsize = -1;
532 /* Remember that we have seen an array ref with a variable
533 index. */
534 seen_variable_array_ref = true;
537 break;
539 case REALPART_EXPR:
540 break;
542 case IMAGPART_EXPR:
543 bit_offset += double_int::from_uhwi (bitsize);
544 break;
546 case VIEW_CONVERT_EXPR:
547 break;
549 case TARGET_MEM_REF:
550 /* Via the variable index or index2 we can reach the
551 whole object. Still hand back the decl here. */
552 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
553 && (TMR_INDEX (exp) || TMR_INDEX2 (exp)))
555 exp = TREE_OPERAND (TMR_BASE (exp), 0);
556 bit_offset = double_int_zero;
557 maxsize = -1;
558 goto done;
560 /* Fallthru. */
561 case MEM_REF:
562 /* We need to deal with variable arrays ending structures such as
563 struct { int length; int a[1]; } x; x.a[d]
564 struct { struct { int a; int b; } a[1]; } x; x.a[d].a
565 struct { struct { int a[1]; } a[1]; } x; x.a[0][d], x.a[d][0]
566 struct { int len; union { int a[1]; struct X x; } u; } x; x.u.a[d]
567 where we do not know maxsize for variable index accesses to
568 the array. The simplest way to conservatively deal with this
569 is to punt in the case that offset + maxsize reaches the
570 base type boundary. This needs to include possible trailing
571 padding that is there for alignment purposes. */
572 if (seen_variable_array_ref
573 && maxsize != -1
574 && (!bit_offset.fits_shwi ()
575 || !tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp)))
576 || (bit_offset.to_shwi () + maxsize
577 == (HOST_WIDE_INT) TREE_INT_CST_LOW
578 (TYPE_SIZE (TREE_TYPE (exp))))))
579 maxsize = -1;
581 /* Hand back the decl for MEM[&decl, off]. */
582 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
584 if (integer_zerop (TREE_OPERAND (exp, 1)))
585 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
586 else
588 double_int off = mem_ref_offset (exp);
589 off = off.lshift (BITS_PER_UNIT == 8
590 ? 3 : exact_log2 (BITS_PER_UNIT));
591 off += bit_offset;
592 if (off.fits_shwi ())
594 bit_offset = off;
595 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
599 goto done;
601 default:
602 goto done;
605 exp = TREE_OPERAND (exp, 0);
608 /* We need to deal with variable arrays ending structures. */
609 if (seen_variable_array_ref
610 && maxsize != -1
611 && (!bit_offset.fits_shwi ()
612 || !tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp)))
613 || (bit_offset.to_shwi () + maxsize
614 == (HOST_WIDE_INT) TREE_INT_CST_LOW
615 (TYPE_SIZE (TREE_TYPE (exp))))))
616 maxsize = -1;
618 done:
619 if (!bit_offset.fits_shwi ())
621 *poffset = 0;
622 *psize = bitsize;
623 *pmax_size = -1;
625 return exp;
628 hbit_offset = bit_offset.to_shwi ();
630 /* In case of a decl or constant base object we can do better. */
632 if (DECL_P (exp))
634 /* If maxsize is unknown adjust it according to the size of the
635 base decl. */
636 if (maxsize == -1
637 && tree_fits_uhwi_p (DECL_SIZE (exp)))
638 maxsize = TREE_INT_CST_LOW (DECL_SIZE (exp)) - hbit_offset;
640 else if (CONSTANT_CLASS_P (exp))
642 /* If maxsize is unknown adjust it according to the size of the
643 base type constant. */
644 if (maxsize == -1
645 && tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))))
646 maxsize = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp))) - hbit_offset;
649 /* ??? Due to negative offsets in ARRAY_REF we can end up with
650 negative bit_offset here. We might want to store a zero offset
651 in this case. */
652 *poffset = hbit_offset;
653 *psize = bitsize;
654 *pmax_size = maxsize;
656 return exp;
659 /* Returns the base object and a constant BITS_PER_UNIT offset in *POFFSET that
660 denotes the starting address of the memory access EXP.
661 Returns NULL_TREE if the offset is not constant or any component
662 is not BITS_PER_UNIT-aligned. */
664 tree
665 get_addr_base_and_unit_offset (tree exp, HOST_WIDE_INT *poffset)
667 return get_addr_base_and_unit_offset_1 (exp, poffset, NULL);
670 /* Returns true if STMT references an SSA_NAME that has
671 SSA_NAME_OCCURS_IN_ABNORMAL_PHI set, otherwise false. */
673 bool
674 stmt_references_abnormal_ssa_name (gimple stmt)
676 ssa_op_iter oi;
677 use_operand_p use_p;
679 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE)
681 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (use_p)))
682 return true;
685 return false;
688 /* Pair of tree and a sorting index, for dump_enumerated_decls. */
689 struct GTY(()) numbered_tree_d
691 tree t;
692 int num;
694 typedef struct numbered_tree_d numbered_tree;
697 /* Compare two declarations references by their DECL_UID / sequence number.
698 Called via qsort. */
700 static int
701 compare_decls_by_uid (const void *pa, const void *pb)
703 const numbered_tree *nt_a = ((const numbered_tree *)pa);
704 const numbered_tree *nt_b = ((const numbered_tree *)pb);
706 if (DECL_UID (nt_a->t) != DECL_UID (nt_b->t))
707 return DECL_UID (nt_a->t) - DECL_UID (nt_b->t);
708 return nt_a->num - nt_b->num;
711 /* Called via walk_gimple_stmt / walk_gimple_op by dump_enumerated_decls. */
712 static tree
713 dump_enumerated_decls_push (tree *tp, int *walk_subtrees, void *data)
715 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
716 vec<numbered_tree> *list = (vec<numbered_tree> *) wi->info;
717 numbered_tree nt;
719 if (!DECL_P (*tp))
720 return NULL_TREE;
721 nt.t = *tp;
722 nt.num = list->length ();
723 list->safe_push (nt);
724 *walk_subtrees = 0;
725 return NULL_TREE;
728 /* Find all the declarations used by the current function, sort them by uid,
729 and emit the sorted list. Each declaration is tagged with a sequence
730 number indicating when it was found during statement / tree walking,
731 so that TDF_NOUID comparisons of anonymous declarations are still
732 meaningful. Where a declaration was encountered more than once, we
733 emit only the sequence number of the first encounter.
734 FILE is the dump file where to output the list and FLAGS is as in
735 print_generic_expr. */
736 void
737 dump_enumerated_decls (FILE *file, int flags)
739 basic_block bb;
740 struct walk_stmt_info wi;
741 stack_vec<numbered_tree, 40> decl_list;
743 memset (&wi, '\0', sizeof (wi));
744 wi.info = (void *) &decl_list;
745 FOR_EACH_BB (bb)
747 gimple_stmt_iterator gsi;
749 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
750 if (!is_gimple_debug (gsi_stmt (gsi)))
751 walk_gimple_stmt (&gsi, NULL, dump_enumerated_decls_push, &wi);
753 decl_list.qsort (compare_decls_by_uid);
754 if (decl_list.length ())
756 unsigned ix;
757 numbered_tree *ntp;
758 tree last = NULL_TREE;
760 fprintf (file, "Declarations used by %s, sorted by DECL_UID:\n",
761 current_function_name ());
762 FOR_EACH_VEC_ELT (decl_list, ix, ntp)
764 if (ntp->t == last)
765 continue;
766 fprintf (file, "%d: ", ntp->num);
767 print_generic_decl (file, ntp->t, flags);
768 fprintf (file, "\n");
769 last = ntp->t;