2013-11-21 Edward Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / tree-dfa.c
blob0ecf7a3e1554c0d463a401a24602bb34999e9e2f
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_to_uhwi (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_to_shwi (ssize)
465 - tree_to_shwi (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_to_uhwi (csize) - bit_offset.to_shwi ();
484 else
485 maxsize = -1;
488 break;
490 case ARRAY_REF:
491 case ARRAY_RANGE_REF:
493 tree index = TREE_OPERAND (exp, 1);
494 tree low_bound, unit_size;
496 /* If the resulting bit-offset is constant, track it. */
497 if (TREE_CODE (index) == INTEGER_CST
498 && (low_bound = array_ref_low_bound (exp),
499 TREE_CODE (low_bound) == INTEGER_CST)
500 && (unit_size = array_ref_element_size (exp),
501 TREE_CODE (unit_size) == INTEGER_CST))
503 double_int doffset
504 = (TREE_INT_CST (index) - TREE_INT_CST (low_bound))
505 .sext (TYPE_PRECISION (TREE_TYPE (index)));
506 doffset *= tree_to_double_int (unit_size);
507 doffset = doffset.lshift (BITS_PER_UNIT == 8
508 ? 3 : exact_log2 (BITS_PER_UNIT));
509 bit_offset = bit_offset + doffset;
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 seen_variable_array_ref = false;
516 else
518 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 if (maxsize != -1
523 && asize
524 && tree_fits_uhwi_p (asize)
525 && bit_offset.fits_shwi ())
526 maxsize = tree_to_uhwi (asize) - bit_offset.to_shwi ();
527 else
528 maxsize = -1;
530 /* Remember that we have seen an array ref with a variable
531 index. */
532 seen_variable_array_ref = true;
535 break;
537 case REALPART_EXPR:
538 break;
540 case IMAGPART_EXPR:
541 bit_offset += double_int::from_uhwi (bitsize);
542 break;
544 case VIEW_CONVERT_EXPR:
545 break;
547 case TARGET_MEM_REF:
548 /* Via the variable index or index2 we can reach the
549 whole object. Still hand back the decl here. */
550 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
551 && (TMR_INDEX (exp) || TMR_INDEX2 (exp)))
553 exp = TREE_OPERAND (TMR_BASE (exp), 0);
554 bit_offset = double_int_zero;
555 maxsize = -1;
556 goto done;
558 /* Fallthru. */
559 case MEM_REF:
560 /* We need to deal with variable arrays ending structures such as
561 struct { int length; int a[1]; } x; x.a[d]
562 struct { struct { int a; int b; } a[1]; } x; x.a[d].a
563 struct { struct { int a[1]; } a[1]; } x; x.a[0][d], x.a[d][0]
564 struct { int len; union { int a[1]; struct X x; } u; } x; x.u.a[d]
565 where we do not know maxsize for variable index accesses to
566 the array. The simplest way to conservatively deal with this
567 is to punt in the case that offset + maxsize reaches the
568 base type boundary. This needs to include possible trailing
569 padding that is there for alignment purposes. */
570 if (seen_variable_array_ref
571 && maxsize != -1
572 && (!bit_offset.fits_shwi ()
573 || !tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp)))
574 || (bit_offset.to_shwi () + maxsize
575 == (HOST_WIDE_INT) tree_to_uhwi
576 (TYPE_SIZE (TREE_TYPE (exp))))))
577 maxsize = -1;
579 /* Hand back the decl for MEM[&decl, off]. */
580 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
582 if (integer_zerop (TREE_OPERAND (exp, 1)))
583 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
584 else
586 double_int off = mem_ref_offset (exp);
587 off = off.lshift (BITS_PER_UNIT == 8
588 ? 3 : exact_log2 (BITS_PER_UNIT));
589 off += bit_offset;
590 if (off.fits_shwi ())
592 bit_offset = off;
593 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
597 goto done;
599 default:
600 goto done;
603 exp = TREE_OPERAND (exp, 0);
606 /* We need to deal with variable arrays ending structures. */
607 if (seen_variable_array_ref
608 && maxsize != -1
609 && (!bit_offset.fits_shwi ()
610 || !tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp)))
611 || (bit_offset.to_shwi () + maxsize
612 == (HOST_WIDE_INT) tree_to_uhwi
613 (TYPE_SIZE (TREE_TYPE (exp))))))
614 maxsize = -1;
616 done:
617 if (!bit_offset.fits_shwi ())
619 *poffset = 0;
620 *psize = bitsize;
621 *pmax_size = -1;
623 return exp;
626 hbit_offset = bit_offset.to_shwi ();
628 /* In case of a decl or constant base object we can do better. */
630 if (DECL_P (exp))
632 /* If maxsize is unknown adjust it according to the size of the
633 base decl. */
634 if (maxsize == -1
635 && tree_fits_uhwi_p (DECL_SIZE (exp)))
636 maxsize = tree_to_uhwi (DECL_SIZE (exp)) - hbit_offset;
638 else if (CONSTANT_CLASS_P (exp))
640 /* If maxsize is unknown adjust it according to the size of the
641 base type constant. */
642 if (maxsize == -1
643 && tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (exp))))
644 maxsize = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (exp))) - hbit_offset;
647 /* ??? Due to negative offsets in ARRAY_REF we can end up with
648 negative bit_offset here. We might want to store a zero offset
649 in this case. */
650 *poffset = hbit_offset;
651 *psize = bitsize;
652 *pmax_size = maxsize;
654 return exp;
657 /* Returns the base object and a constant BITS_PER_UNIT offset in *POFFSET that
658 denotes the starting address of the memory access EXP.
659 Returns NULL_TREE if the offset is not constant or any component
660 is not BITS_PER_UNIT-aligned. */
662 tree
663 get_addr_base_and_unit_offset (tree exp, HOST_WIDE_INT *poffset)
665 return get_addr_base_and_unit_offset_1 (exp, poffset, NULL);
668 /* Returns true if STMT references an SSA_NAME that has
669 SSA_NAME_OCCURS_IN_ABNORMAL_PHI set, otherwise false. */
671 bool
672 stmt_references_abnormal_ssa_name (gimple stmt)
674 ssa_op_iter oi;
675 use_operand_p use_p;
677 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE)
679 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (use_p)))
680 return true;
683 return false;
686 /* Pair of tree and a sorting index, for dump_enumerated_decls. */
687 struct GTY(()) numbered_tree_d
689 tree t;
690 int num;
692 typedef struct numbered_tree_d numbered_tree;
695 /* Compare two declarations references by their DECL_UID / sequence number.
696 Called via qsort. */
698 static int
699 compare_decls_by_uid (const void *pa, const void *pb)
701 const numbered_tree *nt_a = ((const numbered_tree *)pa);
702 const numbered_tree *nt_b = ((const numbered_tree *)pb);
704 if (DECL_UID (nt_a->t) != DECL_UID (nt_b->t))
705 return DECL_UID (nt_a->t) - DECL_UID (nt_b->t);
706 return nt_a->num - nt_b->num;
709 /* Called via walk_gimple_stmt / walk_gimple_op by dump_enumerated_decls. */
710 static tree
711 dump_enumerated_decls_push (tree *tp, int *walk_subtrees, void *data)
713 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
714 vec<numbered_tree> *list = (vec<numbered_tree> *) wi->info;
715 numbered_tree nt;
717 if (!DECL_P (*tp))
718 return NULL_TREE;
719 nt.t = *tp;
720 nt.num = list->length ();
721 list->safe_push (nt);
722 *walk_subtrees = 0;
723 return NULL_TREE;
726 /* Find all the declarations used by the current function, sort them by uid,
727 and emit the sorted list. Each declaration is tagged with a sequence
728 number indicating when it was found during statement / tree walking,
729 so that TDF_NOUID comparisons of anonymous declarations are still
730 meaningful. Where a declaration was encountered more than once, we
731 emit only the sequence number of the first encounter.
732 FILE is the dump file where to output the list and FLAGS is as in
733 print_generic_expr. */
734 void
735 dump_enumerated_decls (FILE *file, int flags)
737 basic_block bb;
738 struct walk_stmt_info wi;
739 stack_vec<numbered_tree, 40> decl_list;
741 memset (&wi, '\0', sizeof (wi));
742 wi.info = (void *) &decl_list;
743 FOR_EACH_BB (bb)
745 gimple_stmt_iterator gsi;
747 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
748 if (!is_gimple_debug (gsi_stmt (gsi)))
749 walk_gimple_stmt (&gsi, NULL, dump_enumerated_decls_push, &wi);
751 decl_list.qsort (compare_decls_by_uid);
752 if (decl_list.length ())
754 unsigned ix;
755 numbered_tree *ntp;
756 tree last = NULL_TREE;
758 fprintf (file, "Declarations used by %s, sorted by DECL_UID:\n",
759 current_function_name ());
760 FOR_EACH_VEC_ELT (decl_list, ix, ntp)
762 if (ntp->t == last)
763 continue;
764 fprintf (file, "%d: ", ntp->num);
765 print_generic_decl (file, ntp->t, flags);
766 fprintf (file, "\n");
767 last = ntp->t;