* lto-partition.c (add_symbol_to_partition_1,
[official-gcc.git] / gcc / tree-dfa.c
blob6ae38daef2c161c7fef3b4c1759b261de33e96d5
1 /* Data flow functions for trees.
2 Copyright (C) 2001-2014 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 "tree.h"
27 #include "stor-layout.h"
28 #include "tm_p.h"
29 #include "basic-block.h"
30 #include "langhooks.h"
31 #include "flags.h"
32 #include "function.h"
33 #include "tree-pretty-print.h"
34 #include "tree-ssa-alias.h"
35 #include "internal-fn.h"
36 #include "gimple-expr.h"
37 #include "is-a.h"
38 #include "gimple.h"
39 #include "gimple-iterator.h"
40 #include "gimple-walk.h"
41 #include "gimple-ssa.h"
42 #include "tree-phinodes.h"
43 #include "ssa-iterators.h"
44 #include "stringpool.h"
45 #include "tree-ssanames.h"
46 #include "expr.h"
47 #include "tree-dfa.h"
48 #include "tree-inline.h"
49 #include "tree-pass.h"
50 #include "params.h"
52 /* Build and maintain data flow information for trees. */
54 /* Counters used to display DFA and SSA statistics. */
55 struct dfa_stats_d
57 long num_defs;
58 long num_uses;
59 long num_phis;
60 long num_phi_args;
61 size_t max_num_phi_args;
62 long num_vdefs;
63 long num_vuses;
67 /* Local functions. */
68 static void collect_dfa_stats (struct dfa_stats_d *);
71 /*---------------------------------------------------------------------------
72 Dataflow analysis (DFA) routines
73 ---------------------------------------------------------------------------*/
75 /* Renumber all of the gimple stmt uids. */
77 void
78 renumber_gimple_stmt_uids (void)
80 basic_block bb;
82 set_gimple_stmt_max_uid (cfun, 0);
83 FOR_ALL_BB_FN (bb, cfun)
85 gimple_stmt_iterator bsi;
86 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
88 gimple stmt = gsi_stmt (bsi);
89 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
91 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
93 gimple stmt = gsi_stmt (bsi);
94 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
99 /* Like renumber_gimple_stmt_uids, but only do work on the basic blocks
100 in BLOCKS, of which there are N_BLOCKS. Also renumbers PHIs. */
102 void
103 renumber_gimple_stmt_uids_in_blocks (basic_block *blocks, int n_blocks)
105 int i;
107 set_gimple_stmt_max_uid (cfun, 0);
108 for (i = 0; i < n_blocks; i++)
110 basic_block bb = blocks[i];
111 gimple_stmt_iterator bsi;
112 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
114 gimple stmt = gsi_stmt (bsi);
115 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
117 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
119 gimple stmt = gsi_stmt (bsi);
120 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
127 /*---------------------------------------------------------------------------
128 Debugging functions
129 ---------------------------------------------------------------------------*/
131 /* Dump variable VAR and its may-aliases to FILE. */
133 void
134 dump_variable (FILE *file, tree var)
136 if (TREE_CODE (var) == SSA_NAME)
138 if (POINTER_TYPE_P (TREE_TYPE (var)))
139 dump_points_to_info_for (file, var);
140 var = SSA_NAME_VAR (var);
143 if (var == NULL_TREE)
145 fprintf (file, "<nil>");
146 return;
149 print_generic_expr (file, var, dump_flags);
151 fprintf (file, ", UID D.%u", (unsigned) DECL_UID (var));
152 if (DECL_PT_UID (var) != DECL_UID (var))
153 fprintf (file, ", PT-UID D.%u", (unsigned) DECL_PT_UID (var));
155 fprintf (file, ", ");
156 print_generic_expr (file, TREE_TYPE (var), dump_flags);
158 if (TREE_ADDRESSABLE (var))
159 fprintf (file, ", is addressable");
161 if (is_global_var (var))
162 fprintf (file, ", is global");
164 if (TREE_THIS_VOLATILE (var))
165 fprintf (file, ", is volatile");
167 if (cfun && ssa_default_def (cfun, var))
169 fprintf (file, ", default def: ");
170 print_generic_expr (file, ssa_default_def (cfun, var), dump_flags);
173 if (DECL_INITIAL (var))
175 fprintf (file, ", initial: ");
176 print_generic_expr (file, DECL_INITIAL (var), dump_flags);
179 fprintf (file, "\n");
183 /* Dump variable VAR and its may-aliases to stderr. */
185 DEBUG_FUNCTION void
186 debug_variable (tree var)
188 dump_variable (stderr, var);
192 /* Dump various DFA statistics to FILE. */
194 void
195 dump_dfa_stats (FILE *file)
197 struct dfa_stats_d dfa_stats;
199 unsigned long size, total = 0;
200 const char * const fmt_str = "%-30s%-13s%12s\n";
201 const char * const fmt_str_1 = "%-30s%13lu%11lu%c\n";
202 const char * const fmt_str_3 = "%-43s%11lu%c\n";
203 const char *funcname
204 = lang_hooks.decl_printable_name (current_function_decl, 2);
206 collect_dfa_stats (&dfa_stats);
208 fprintf (file, "\nDFA Statistics for %s\n\n", funcname);
210 fprintf (file, "---------------------------------------------------------\n");
211 fprintf (file, fmt_str, "", " Number of ", "Memory");
212 fprintf (file, fmt_str, "", " instances ", "used ");
213 fprintf (file, "---------------------------------------------------------\n");
215 size = dfa_stats.num_uses * sizeof (tree *);
216 total += size;
217 fprintf (file, fmt_str_1, "USE operands", dfa_stats.num_uses,
218 SCALE (size), LABEL (size));
220 size = dfa_stats.num_defs * sizeof (tree *);
221 total += size;
222 fprintf (file, fmt_str_1, "DEF operands", dfa_stats.num_defs,
223 SCALE (size), LABEL (size));
225 size = dfa_stats.num_vuses * sizeof (tree *);
226 total += size;
227 fprintf (file, fmt_str_1, "VUSE operands", dfa_stats.num_vuses,
228 SCALE (size), LABEL (size));
230 size = dfa_stats.num_vdefs * sizeof (tree *);
231 total += size;
232 fprintf (file, fmt_str_1, "VDEF operands", dfa_stats.num_vdefs,
233 SCALE (size), LABEL (size));
235 size = dfa_stats.num_phis * sizeof (struct gimple_statement_phi);
236 total += size;
237 fprintf (file, fmt_str_1, "PHI nodes", dfa_stats.num_phis,
238 SCALE (size), LABEL (size));
240 size = dfa_stats.num_phi_args * sizeof (struct phi_arg_d);
241 total += size;
242 fprintf (file, fmt_str_1, "PHI arguments", dfa_stats.num_phi_args,
243 SCALE (size), LABEL (size));
245 fprintf (file, "---------------------------------------------------------\n");
246 fprintf (file, fmt_str_3, "Total memory used by DFA/SSA data", SCALE (total),
247 LABEL (total));
248 fprintf (file, "---------------------------------------------------------\n");
249 fprintf (file, "\n");
251 if (dfa_stats.num_phis)
252 fprintf (file, "Average number of arguments per PHI node: %.1f (max: %ld)\n",
253 (float) dfa_stats.num_phi_args / (float) dfa_stats.num_phis,
254 (long) dfa_stats.max_num_phi_args);
256 fprintf (file, "\n");
260 /* Dump DFA statistics on stderr. */
262 DEBUG_FUNCTION void
263 debug_dfa_stats (void)
265 dump_dfa_stats (stderr);
269 /* Collect DFA statistics and store them in the structure pointed to by
270 DFA_STATS_P. */
272 static void
273 collect_dfa_stats (struct dfa_stats_d *dfa_stats_p ATTRIBUTE_UNUSED)
275 basic_block bb;
277 gcc_assert (dfa_stats_p);
279 memset ((void *)dfa_stats_p, 0, sizeof (struct dfa_stats_d));
281 /* Walk all the statements in the function counting references. */
282 FOR_EACH_BB_FN (bb, cfun)
284 gimple_stmt_iterator si;
286 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
288 gimple phi = gsi_stmt (si);
289 dfa_stats_p->num_phis++;
290 dfa_stats_p->num_phi_args += gimple_phi_num_args (phi);
291 if (gimple_phi_num_args (phi) > dfa_stats_p->max_num_phi_args)
292 dfa_stats_p->max_num_phi_args = gimple_phi_num_args (phi);
295 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
297 gimple stmt = gsi_stmt (si);
298 dfa_stats_p->num_defs += NUM_SSA_OPERANDS (stmt, SSA_OP_DEF);
299 dfa_stats_p->num_uses += NUM_SSA_OPERANDS (stmt, SSA_OP_USE);
300 dfa_stats_p->num_vdefs += gimple_vdef (stmt) ? 1 : 0;
301 dfa_stats_p->num_vuses += gimple_vuse (stmt) ? 1 : 0;
307 /*---------------------------------------------------------------------------
308 Miscellaneous helpers
309 ---------------------------------------------------------------------------*/
311 /* Lookup VAR UID in the default_defs hashtable and return the associated
312 variable. */
314 tree
315 ssa_default_def (struct function *fn, tree var)
317 struct tree_decl_minimal ind;
318 struct tree_ssa_name in;
319 gcc_assert (TREE_CODE (var) == VAR_DECL
320 || TREE_CODE (var) == PARM_DECL
321 || TREE_CODE (var) == RESULT_DECL);
322 in.var = (tree)&ind;
323 ind.uid = DECL_UID (var);
324 return (tree) htab_find_with_hash (DEFAULT_DEFS (fn), &in, DECL_UID (var));
327 /* Insert the pair VAR's UID, DEF into the default_defs hashtable
328 of function FN. */
330 void
331 set_ssa_default_def (struct function *fn, tree var, tree def)
333 struct tree_decl_minimal ind;
334 struct tree_ssa_name in;
335 void **loc;
337 gcc_assert (TREE_CODE (var) == VAR_DECL
338 || TREE_CODE (var) == PARM_DECL
339 || TREE_CODE (var) == RESULT_DECL);
340 in.var = (tree)&ind;
341 ind.uid = DECL_UID (var);
342 if (!def)
344 loc = htab_find_slot_with_hash (DEFAULT_DEFS (fn), &in,
345 DECL_UID (var), NO_INSERT);
346 if (*loc)
348 SSA_NAME_IS_DEFAULT_DEF (*(tree *)loc) = false;
349 htab_clear_slot (DEFAULT_DEFS (fn), loc);
351 return;
353 gcc_assert (TREE_CODE (def) == SSA_NAME && SSA_NAME_VAR (def) == var);
354 loc = htab_find_slot_with_hash (DEFAULT_DEFS (fn), &in,
355 DECL_UID (var), INSERT);
357 /* Default definition might be changed by tail call optimization. */
358 if (*loc)
359 SSA_NAME_IS_DEFAULT_DEF (*(tree *) loc) = false;
361 /* Mark DEF as the default definition for VAR. */
362 *(tree *) loc = def;
363 SSA_NAME_IS_DEFAULT_DEF (def) = true;
366 /* Retrieve or create a default definition for VAR. */
368 tree
369 get_or_create_ssa_default_def (struct function *fn, tree var)
371 tree ddef = ssa_default_def (fn, var);
372 if (ddef == NULL_TREE)
374 ddef = make_ssa_name_fn (fn, var, gimple_build_nop ());
375 set_ssa_default_def (fn, var, ddef);
377 return ddef;
381 /* If EXP is a handled component reference for a structure, return the
382 base variable. The access range is delimited by bit positions *POFFSET and
383 *POFFSET + *PMAX_SIZE. The access size is *PSIZE bits. If either
384 *PSIZE or *PMAX_SIZE is -1, they could not be determined. If *PSIZE
385 and *PMAX_SIZE are equal, the access is non-variable. */
387 tree
388 get_ref_base_and_extent (tree exp, HOST_WIDE_INT *poffset,
389 HOST_WIDE_INT *psize,
390 HOST_WIDE_INT *pmax_size)
392 HOST_WIDE_INT bitsize = -1;
393 HOST_WIDE_INT maxsize = -1;
394 tree size_tree = NULL_TREE;
395 double_int bit_offset = double_int_zero;
396 HOST_WIDE_INT hbit_offset;
397 bool seen_variable_array_ref = false;
399 /* First get the final access size from just the outermost expression. */
400 if (TREE_CODE (exp) == COMPONENT_REF)
401 size_tree = DECL_SIZE (TREE_OPERAND (exp, 1));
402 else if (TREE_CODE (exp) == BIT_FIELD_REF)
403 size_tree = TREE_OPERAND (exp, 1);
404 else if (!VOID_TYPE_P (TREE_TYPE (exp)))
406 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
407 if (mode == BLKmode)
408 size_tree = TYPE_SIZE (TREE_TYPE (exp));
409 else
410 bitsize = GET_MODE_BITSIZE (mode);
412 if (size_tree != NULL_TREE)
414 if (! tree_fits_uhwi_p (size_tree))
415 bitsize = -1;
416 else
417 bitsize = tree_to_uhwi (size_tree);
420 /* Initially, maxsize is the same as the accessed element size.
421 In the following it will only grow (or become -1). */
422 maxsize = bitsize;
424 /* Compute cumulative bit-offset for nested component-refs and array-refs,
425 and find the ultimate containing object. */
426 while (1)
428 switch (TREE_CODE (exp))
430 case BIT_FIELD_REF:
431 bit_offset += tree_to_double_int (TREE_OPERAND (exp, 2));
432 break;
434 case COMPONENT_REF:
436 tree field = TREE_OPERAND (exp, 1);
437 tree this_offset = component_ref_field_offset (exp);
439 if (this_offset && TREE_CODE (this_offset) == INTEGER_CST)
441 double_int doffset = tree_to_double_int (this_offset);
442 doffset = doffset.lshift (BITS_PER_UNIT == 8
443 ? 3 : exact_log2 (BITS_PER_UNIT));
444 doffset += tree_to_double_int (DECL_FIELD_BIT_OFFSET (field));
445 bit_offset = bit_offset + doffset;
447 /* If we had seen a variable array ref already and we just
448 referenced the last field of a struct or a union member
449 then we have to adjust maxsize by the padding at the end
450 of our field. */
451 if (seen_variable_array_ref && maxsize != -1)
453 tree stype = TREE_TYPE (TREE_OPERAND (exp, 0));
454 tree next = DECL_CHAIN (field);
455 while (next && TREE_CODE (next) != FIELD_DECL)
456 next = DECL_CHAIN (next);
457 if (!next
458 || TREE_CODE (stype) != RECORD_TYPE)
460 tree fsize = DECL_SIZE_UNIT (field);
461 tree ssize = TYPE_SIZE_UNIT (stype);
462 if (tree_fits_shwi_p (fsize)
463 && tree_fits_shwi_p (ssize)
464 && doffset.fits_shwi ())
465 maxsize += ((tree_to_shwi (ssize)
466 - tree_to_shwi (fsize))
467 * BITS_PER_UNIT
468 - doffset.to_shwi ());
469 else
470 maxsize = -1;
474 else
476 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 if (maxsize != -1
481 && csize
482 && tree_fits_uhwi_p (csize)
483 && bit_offset.fits_shwi ())
484 maxsize = tree_to_uhwi (csize) - 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_to_uhwi (asize) - bit_offset.to_shwi ();
528 else
529 maxsize = -1;
531 /* Remember that we have seen an array ref with a variable
532 index. */
533 seen_variable_array_ref = true;
536 break;
538 case REALPART_EXPR:
539 break;
541 case IMAGPART_EXPR:
542 bit_offset += double_int::from_uhwi (bitsize);
543 break;
545 case VIEW_CONVERT_EXPR:
546 break;
548 case TARGET_MEM_REF:
549 /* Via the variable index or index2 we can reach the
550 whole object. Still hand back the decl here. */
551 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
552 && (TMR_INDEX (exp) || TMR_INDEX2 (exp)))
554 exp = TREE_OPERAND (TMR_BASE (exp), 0);
555 bit_offset = double_int_zero;
556 maxsize = -1;
557 goto done;
559 /* Fallthru. */
560 case MEM_REF:
561 /* We need to deal with variable arrays ending structures such as
562 struct { int length; int a[1]; } x; x.a[d]
563 struct { struct { int a; int b; } a[1]; } x; x.a[d].a
564 struct { struct { int a[1]; } a[1]; } x; x.a[0][d], x.a[d][0]
565 struct { int len; union { int a[1]; struct X x; } u; } x; x.u.a[d]
566 where we do not know maxsize for variable index accesses to
567 the array. The simplest way to conservatively deal with this
568 is to punt in the case that offset + maxsize reaches the
569 base type boundary. This needs to include possible trailing
570 padding that is there for alignment purposes. */
571 if (seen_variable_array_ref
572 && maxsize != -1
573 && (!bit_offset.fits_shwi ()
574 || !tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp)))
575 || (bit_offset.to_shwi () + maxsize
576 == (HOST_WIDE_INT) tree_to_uhwi
577 (TYPE_SIZE (TREE_TYPE (exp))))))
578 maxsize = -1;
580 /* Hand back the decl for MEM[&decl, off]. */
581 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
583 if (integer_zerop (TREE_OPERAND (exp, 1)))
584 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
585 else
587 double_int off = mem_ref_offset (exp);
588 off = off.lshift (BITS_PER_UNIT == 8
589 ? 3 : exact_log2 (BITS_PER_UNIT));
590 off += bit_offset;
591 if (off.fits_shwi ())
593 bit_offset = off;
594 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
598 goto done;
600 default:
601 goto done;
604 exp = TREE_OPERAND (exp, 0);
607 /* We need to deal with variable arrays ending structures. */
608 if (seen_variable_array_ref
609 && maxsize != -1
610 && (!bit_offset.fits_shwi ()
611 || !tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp)))
612 || (bit_offset.to_shwi () + maxsize
613 == (HOST_WIDE_INT) tree_to_uhwi
614 (TYPE_SIZE (TREE_TYPE (exp))))))
615 maxsize = -1;
617 done:
618 if (!bit_offset.fits_shwi ())
620 *poffset = 0;
621 *psize = bitsize;
622 *pmax_size = -1;
624 return exp;
627 hbit_offset = bit_offset.to_shwi ();
629 /* In case of a decl or constant base object we can do better. */
631 if (DECL_P (exp))
633 /* If maxsize is unknown adjust it according to the size of the
634 base decl. */
635 if (maxsize == -1
636 && tree_fits_uhwi_p (DECL_SIZE (exp)))
637 maxsize = tree_to_uhwi (DECL_SIZE (exp)) - hbit_offset;
639 else if (CONSTANT_CLASS_P (exp))
641 /* If maxsize is unknown adjust it according to the size of the
642 base type constant. */
643 if (maxsize == -1
644 && tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))))
645 maxsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp))) - hbit_offset;
648 /* ??? Due to negative offsets in ARRAY_REF we can end up with
649 negative bit_offset here. We might want to store a zero offset
650 in this case. */
651 *poffset = hbit_offset;
652 *psize = bitsize;
653 *pmax_size = maxsize;
655 return exp;
658 /* Returns the base object and a constant BITS_PER_UNIT offset in *POFFSET that
659 denotes the starting address of the memory access EXP.
660 Returns NULL_TREE if the offset is not constant or any component
661 is not BITS_PER_UNIT-aligned. */
663 tree
664 get_addr_base_and_unit_offset (tree exp, HOST_WIDE_INT *poffset)
666 return get_addr_base_and_unit_offset_1 (exp, poffset, NULL);
669 /* Returns true if STMT references an SSA_NAME that has
670 SSA_NAME_OCCURS_IN_ABNORMAL_PHI set, otherwise false. */
672 bool
673 stmt_references_abnormal_ssa_name (gimple stmt)
675 ssa_op_iter oi;
676 use_operand_p use_p;
678 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE)
680 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (use_p)))
681 return true;
684 return false;
687 /* Pair of tree and a sorting index, for dump_enumerated_decls. */
688 struct GTY(()) numbered_tree_d
690 tree t;
691 int num;
693 typedef struct numbered_tree_d numbered_tree;
696 /* Compare two declarations references by their DECL_UID / sequence number.
697 Called via qsort. */
699 static int
700 compare_decls_by_uid (const void *pa, const void *pb)
702 const numbered_tree *nt_a = ((const numbered_tree *)pa);
703 const numbered_tree *nt_b = ((const numbered_tree *)pb);
705 if (DECL_UID (nt_a->t) != DECL_UID (nt_b->t))
706 return DECL_UID (nt_a->t) - DECL_UID (nt_b->t);
707 return nt_a->num - nt_b->num;
710 /* Called via walk_gimple_stmt / walk_gimple_op by dump_enumerated_decls. */
711 static tree
712 dump_enumerated_decls_push (tree *tp, int *walk_subtrees, void *data)
714 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
715 vec<numbered_tree> *list = (vec<numbered_tree> *) wi->info;
716 numbered_tree nt;
718 if (!DECL_P (*tp))
719 return NULL_TREE;
720 nt.t = *tp;
721 nt.num = list->length ();
722 list->safe_push (nt);
723 *walk_subtrees = 0;
724 return NULL_TREE;
727 /* Find all the declarations used by the current function, sort them by uid,
728 and emit the sorted list. Each declaration is tagged with a sequence
729 number indicating when it was found during statement / tree walking,
730 so that TDF_NOUID comparisons of anonymous declarations are still
731 meaningful. Where a declaration was encountered more than once, we
732 emit only the sequence number of the first encounter.
733 FILE is the dump file where to output the list and FLAGS is as in
734 print_generic_expr. */
735 void
736 dump_enumerated_decls (FILE *file, int flags)
738 basic_block bb;
739 struct walk_stmt_info wi;
740 auto_vec<numbered_tree, 40> decl_list;
742 memset (&wi, '\0', sizeof (wi));
743 wi.info = (void *) &decl_list;
744 FOR_EACH_BB_FN (bb, cfun)
746 gimple_stmt_iterator gsi;
748 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
749 if (!is_gimple_debug (gsi_stmt (gsi)))
750 walk_gimple_stmt (&gsi, NULL, dump_enumerated_decls_push, &wi);
752 decl_list.qsort (compare_decls_by_uid);
753 if (decl_list.length ())
755 unsigned ix;
756 numbered_tree *ntp;
757 tree last = NULL_TREE;
759 fprintf (file, "Declarations used by %s, sorted by DECL_UID:\n",
760 current_function_name ());
761 FOR_EACH_VEC_ELT (decl_list, ix, ntp)
763 if (ntp->t == last)
764 continue;
765 fprintf (file, "%d: ", ntp->num);
766 print_generic_decl (file, ntp->t, flags);
767 fprintf (file, "\n");
768 last = ntp->t;