2015-03-06 Christophe Lyon <christophe.lyon@linaro.org>
[official-gcc.git] / gcc-4_9-branch / gcc / tree-dfa.c
bloba1f27587487dc2cc15c2410bc3f12e1a1c95d4e9
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 double_int bitsize = double_int_minus_one;
393 double_int maxsize;
394 tree size_tree = NULL_TREE;
395 double_int bit_offset = double_int_zero;
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 = double_int::from_uhwi (GET_MODE_BITSIZE (mode));
411 if (size_tree != NULL_TREE
412 && TREE_CODE (size_tree) == INTEGER_CST)
413 bitsize = tree_to_double_int (size_tree);
415 /* Initially, maxsize is the same as the accessed element size.
416 In the following it will only grow (or become -1). */
417 maxsize = bitsize;
419 /* Compute cumulative bit-offset for nested component-refs and array-refs,
420 and find the ultimate containing object. */
421 while (1)
423 switch (TREE_CODE (exp))
425 case BIT_FIELD_REF:
426 bit_offset += tree_to_double_int (TREE_OPERAND (exp, 2));
427 break;
429 case COMPONENT_REF:
431 tree field = TREE_OPERAND (exp, 1);
432 tree this_offset = component_ref_field_offset (exp);
434 if (this_offset && TREE_CODE (this_offset) == INTEGER_CST)
436 double_int doffset = tree_to_double_int (this_offset);
437 doffset = doffset.lshift (BITS_PER_UNIT == 8
438 ? 3 : exact_log2 (BITS_PER_UNIT));
439 doffset += tree_to_double_int (DECL_FIELD_BIT_OFFSET (field));
440 bit_offset = bit_offset + doffset;
442 /* If we had seen a variable array ref already and we just
443 referenced the last field of a struct or a union member
444 then we have to adjust maxsize by the padding at the end
445 of our field. */
446 if (seen_variable_array_ref && !maxsize.is_minus_one ())
448 tree stype = TREE_TYPE (TREE_OPERAND (exp, 0));
449 tree next = DECL_CHAIN (field);
450 while (next && TREE_CODE (next) != FIELD_DECL)
451 next = DECL_CHAIN (next);
452 if (!next
453 || TREE_CODE (stype) != RECORD_TYPE)
455 tree fsize = DECL_SIZE_UNIT (field);
456 tree ssize = TYPE_SIZE_UNIT (stype);
457 if (fsize == NULL
458 || TREE_CODE (fsize) != INTEGER_CST
459 || ssize == NULL
460 || TREE_CODE (ssize) != INTEGER_CST)
461 maxsize = double_int_minus_one;
462 else
464 double_int tem = tree_to_double_int (ssize)
465 - tree_to_double_int (fsize);
466 if (BITS_PER_UNIT == 8)
467 tem = tem.lshift (3);
468 else
469 tem *= double_int::from_uhwi (BITS_PER_UNIT);
470 tem -= doffset;
471 maxsize += tem;
476 else
478 tree csize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
479 /* We need to adjust maxsize to the whole structure bitsize.
480 But we can subtract any constant offset seen so far,
481 because that would get us out of the structure otherwise. */
482 if (!maxsize.is_minus_one ()
483 && csize
484 && TREE_CODE (csize) == INTEGER_CST)
485 maxsize = tree_to_double_int (csize) - bit_offset;
486 else
487 maxsize = double_int_minus_one;
490 break;
492 case ARRAY_REF:
493 case ARRAY_RANGE_REF:
495 tree index = TREE_OPERAND (exp, 1);
496 tree low_bound, unit_size;
498 /* If the resulting bit-offset is constant, track it. */
499 if (TREE_CODE (index) == INTEGER_CST
500 && (low_bound = array_ref_low_bound (exp),
501 TREE_CODE (low_bound) == INTEGER_CST)
502 && (unit_size = array_ref_element_size (exp),
503 TREE_CODE (unit_size) == INTEGER_CST))
505 double_int doffset
506 = (TREE_INT_CST (index) - TREE_INT_CST (low_bound))
507 .sext (TYPE_PRECISION (TREE_TYPE (index)));
508 doffset *= tree_to_double_int (unit_size);
509 doffset = doffset.lshift (BITS_PER_UNIT == 8
510 ? 3 : exact_log2 (BITS_PER_UNIT));
511 bit_offset = bit_offset + doffset;
513 /* An array ref with a constant index up in the structure
514 hierarchy will constrain the size of any variable array ref
515 lower in the access hierarchy. */
516 seen_variable_array_ref = false;
518 else
520 tree asize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
521 /* We need to adjust maxsize to the whole array bitsize.
522 But we can subtract any constant offset seen so far,
523 because that would get us outside of the array otherwise. */
524 if (!maxsize.is_minus_one ()
525 && asize
526 && TREE_CODE (asize) == INTEGER_CST)
527 maxsize = tree_to_double_int (asize) - bit_offset;
528 else
529 maxsize = double_int_minus_one;
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 += 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 = double_int_minus_one;
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.is_minus_one ()
573 && (TYPE_SIZE (TREE_TYPE (exp)) == NULL_TREE
574 || TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
575 || (bit_offset + maxsize
576 == tree_to_double_int (TYPE_SIZE (TREE_TYPE (exp))))))
577 maxsize = double_int_minus_one;
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.is_minus_one ()
609 && (TYPE_SIZE (TREE_TYPE (exp)) == NULL_TREE
610 || TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
611 || (bit_offset + maxsize
612 == tree_to_double_int (TYPE_SIZE (TREE_TYPE (exp))))))
613 maxsize = double_int_minus_one;
615 done:
616 if (!bitsize.fits_shwi () || bitsize.is_negative ())
618 *poffset = 0;
619 *psize = -1;
620 *pmax_size = -1;
622 return exp;
625 *psize = bitsize.to_shwi ();
627 if (!bit_offset.fits_shwi ())
629 *poffset = 0;
630 *pmax_size = -1;
632 return exp;
635 /* In case of a decl or constant base object we can do better. */
637 if (DECL_P (exp))
639 /* If maxsize is unknown adjust it according to the size of the
640 base decl. */
641 if (maxsize.is_minus_one ()
642 && DECL_SIZE (exp)
643 && TREE_CODE (DECL_SIZE (exp)) == INTEGER_CST)
644 maxsize = tree_to_double_int (DECL_SIZE (exp)) - bit_offset;
646 else if (CONSTANT_CLASS_P (exp))
648 /* If maxsize is unknown adjust it according to the size of the
649 base type constant. */
650 if (maxsize.is_minus_one ()
651 && TYPE_SIZE (TREE_TYPE (exp))
652 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST)
653 maxsize = tree_to_double_int (TYPE_SIZE (TREE_TYPE (exp)))
654 - bit_offset;
657 /* ??? Due to negative offsets in ARRAY_REF we can end up with
658 negative bit_offset here. We might want to store a zero offset
659 in this case. */
660 *poffset = bit_offset.to_shwi ();
661 if (!maxsize.fits_shwi () || maxsize.is_negative ())
662 *pmax_size = -1;
663 else
664 *pmax_size = maxsize.to_shwi ();
666 return exp;
669 /* Returns the base object and a constant BITS_PER_UNIT offset in *POFFSET that
670 denotes the starting address of the memory access EXP.
671 Returns NULL_TREE if the offset is not constant or any component
672 is not BITS_PER_UNIT-aligned. */
674 tree
675 get_addr_base_and_unit_offset (tree exp, HOST_WIDE_INT *poffset)
677 return get_addr_base_and_unit_offset_1 (exp, poffset, NULL);
680 /* Returns true if STMT references an SSA_NAME that has
681 SSA_NAME_OCCURS_IN_ABNORMAL_PHI set, otherwise false. */
683 bool
684 stmt_references_abnormal_ssa_name (gimple stmt)
686 ssa_op_iter oi;
687 use_operand_p use_p;
689 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE)
691 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (use_p)))
692 return true;
695 return false;
698 /* Pair of tree and a sorting index, for dump_enumerated_decls. */
699 struct GTY(()) numbered_tree_d
701 tree t;
702 int num;
704 typedef struct numbered_tree_d numbered_tree;
707 /* Compare two declarations references by their DECL_UID / sequence number.
708 Called via qsort. */
710 static int
711 compare_decls_by_uid (const void *pa, const void *pb)
713 const numbered_tree *nt_a = ((const numbered_tree *)pa);
714 const numbered_tree *nt_b = ((const numbered_tree *)pb);
716 if (DECL_UID (nt_a->t) != DECL_UID (nt_b->t))
717 return DECL_UID (nt_a->t) - DECL_UID (nt_b->t);
718 return nt_a->num - nt_b->num;
721 /* Called via walk_gimple_stmt / walk_gimple_op by dump_enumerated_decls. */
722 static tree
723 dump_enumerated_decls_push (tree *tp, int *walk_subtrees, void *data)
725 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
726 vec<numbered_tree> *list = (vec<numbered_tree> *) wi->info;
727 numbered_tree nt;
729 if (!DECL_P (*tp))
730 return NULL_TREE;
731 nt.t = *tp;
732 nt.num = list->length ();
733 list->safe_push (nt);
734 *walk_subtrees = 0;
735 return NULL_TREE;
738 /* Find all the declarations used by the current function, sort them by uid,
739 and emit the sorted list. Each declaration is tagged with a sequence
740 number indicating when it was found during statement / tree walking,
741 so that TDF_NOUID comparisons of anonymous declarations are still
742 meaningful. Where a declaration was encountered more than once, we
743 emit only the sequence number of the first encounter.
744 FILE is the dump file where to output the list and FLAGS is as in
745 print_generic_expr. */
746 void
747 dump_enumerated_decls (FILE *file, int flags)
749 basic_block bb;
750 struct walk_stmt_info wi;
751 auto_vec<numbered_tree, 40> decl_list;
753 memset (&wi, '\0', sizeof (wi));
754 wi.info = (void *) &decl_list;
755 FOR_EACH_BB_FN (bb, cfun)
757 gimple_stmt_iterator gsi;
759 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
760 if (!is_gimple_debug (gsi_stmt (gsi)))
761 walk_gimple_stmt (&gsi, NULL, dump_enumerated_decls_push, &wi);
763 decl_list.qsort (compare_decls_by_uid);
764 if (decl_list.length ())
766 unsigned ix;
767 numbered_tree *ntp;
768 tree last = NULL_TREE;
770 fprintf (file, "Declarations used by %s, sorted by DECL_UID:\n",
771 current_function_name ());
772 FOR_EACH_VEC_ELT (decl_list, ix, ntp)
774 if (ntp->t == last)
775 continue;
776 fprintf (file, "%d: ", ntp->num);
777 print_generic_decl (file, ntp->t, flags);
778 fprintf (file, "\n");
779 last = ntp->t;