2012-07-26 Kazu Hirata <kazu@codesourcery.com>
[official-gcc.git] / gcc / tree-dfa.c
blobf8ebfd2697299a5f993a4dc298b428ec8fcb272a
1 /* Data flow functions for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "hashtab.h"
27 #include "pointer-set.h"
28 #include "tree.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 "tree-flow.h"
38 #include "tree-inline.h"
39 #include "tree-pass.h"
40 #include "convert.h"
41 #include "params.h"
42 #include "cgraph.h"
44 /* Build and maintain data flow information for trees. */
46 /* Counters used to display DFA and SSA statistics. */
47 struct dfa_stats_d
49 long num_var_anns;
50 long num_defs;
51 long num_uses;
52 long num_phis;
53 long num_phi_args;
54 size_t max_num_phi_args;
55 long num_vdefs;
56 long num_vuses;
60 /* Local functions. */
61 static void collect_dfa_stats (struct dfa_stats_d *);
64 /*---------------------------------------------------------------------------
65 Dataflow analysis (DFA) routines
66 ---------------------------------------------------------------------------*/
67 /* Find all the variables referenced in the function. This function
68 builds the global arrays REFERENCED_VARS and CALL_CLOBBERED_VARS.
70 Note that this function does not look for statement operands, it simply
71 determines what variables are referenced in the program and detects
72 various attributes for each variable used by alias analysis and the
73 optimizer. */
75 static unsigned int
76 find_referenced_vars (void)
78 basic_block bb;
79 gimple_stmt_iterator si;
81 FOR_EACH_BB (bb)
83 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
85 gimple stmt = gsi_stmt (si);
86 if (is_gimple_debug (stmt))
87 continue;
88 find_referenced_vars_in (gsi_stmt (si));
91 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
92 find_referenced_vars_in (gsi_stmt (si));
95 return 0;
98 struct gimple_opt_pass pass_referenced_vars =
101 GIMPLE_PASS,
102 "*referenced_vars", /* name */
103 NULL, /* gate */
104 find_referenced_vars, /* execute */
105 NULL, /* sub */
106 NULL, /* next */
107 0, /* static_pass_number */
108 TV_FIND_REFERENCED_VARS, /* tv_id */
109 PROP_gimple_leh | PROP_cfg, /* properties_required */
110 PROP_referenced_vars, /* properties_provided */
111 0, /* properties_destroyed */
112 0, /* todo_flags_start */
113 0 /* todo_flags_finish */
118 /* Renumber all of the gimple stmt uids. */
120 void
121 renumber_gimple_stmt_uids (void)
123 basic_block bb;
125 set_gimple_stmt_max_uid (cfun, 0);
126 FOR_ALL_BB (bb)
128 gimple_stmt_iterator bsi;
129 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
131 gimple stmt = gsi_stmt (bsi);
132 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
134 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
136 gimple stmt = gsi_stmt (bsi);
137 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
142 /* Like renumber_gimple_stmt_uids, but only do work on the basic blocks
143 in BLOCKS, of which there are N_BLOCKS. Also renumbers PHIs. */
145 void
146 renumber_gimple_stmt_uids_in_blocks (basic_block *blocks, int n_blocks)
148 int i;
150 set_gimple_stmt_max_uid (cfun, 0);
151 for (i = 0; i < n_blocks; i++)
153 basic_block bb = blocks[i];
154 gimple_stmt_iterator bsi;
155 for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
157 gimple stmt = gsi_stmt (bsi);
158 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
160 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
162 gimple stmt = gsi_stmt (bsi);
163 gimple_set_uid (stmt, inc_gimple_stmt_max_uid (cfun));
168 /* Build a temporary. Make sure and register it to be renamed. */
170 tree
171 make_rename_temp (tree type, const char *prefix)
173 tree t = create_tmp_reg (type, prefix);
175 if (gimple_referenced_vars (cfun))
176 add_referenced_var (t);
177 if (gimple_in_ssa_p (cfun))
178 mark_sym_for_renaming (t);
180 return t;
185 /*---------------------------------------------------------------------------
186 Debugging functions
187 ---------------------------------------------------------------------------*/
188 /* Dump the list of all the referenced variables in the current function to
189 FILE. */
191 void
192 dump_referenced_vars (FILE *file)
194 tree var;
195 referenced_var_iterator rvi;
197 fprintf (file, "\nReferenced variables in %s: %u\n\n",
198 get_name (current_function_decl), (unsigned) num_referenced_vars);
200 FOR_EACH_REFERENCED_VAR (cfun, var, rvi)
202 fprintf (file, "Variable: ");
203 dump_variable (file, var);
206 fprintf (file, "\n");
210 /* Dump the list of all the referenced variables to stderr. */
212 DEBUG_FUNCTION void
213 debug_referenced_vars (void)
215 dump_referenced_vars (stderr);
219 /* Dump variable VAR and its may-aliases to FILE. */
221 void
222 dump_variable (FILE *file, tree var)
224 if (TREE_CODE (var) == SSA_NAME)
226 if (POINTER_TYPE_P (TREE_TYPE (var)))
227 dump_points_to_info_for (file, var);
228 var = SSA_NAME_VAR (var);
231 if (var == NULL_TREE)
233 fprintf (file, "<nil>");
234 return;
237 print_generic_expr (file, var, dump_flags);
239 fprintf (file, ", UID D.%u", (unsigned) DECL_UID (var));
240 if (DECL_PT_UID (var) != DECL_UID (var))
241 fprintf (file, ", PT-UID D.%u", (unsigned) DECL_PT_UID (var));
243 fprintf (file, ", ");
244 print_generic_expr (file, TREE_TYPE (var), dump_flags);
246 if (TREE_ADDRESSABLE (var))
247 fprintf (file, ", is addressable");
249 if (is_global_var (var))
250 fprintf (file, ", is global");
252 if (TREE_THIS_VOLATILE (var))
253 fprintf (file, ", is volatile");
255 if (cfun && gimple_default_def (cfun, var))
257 fprintf (file, ", default def: ");
258 print_generic_expr (file, gimple_default_def (cfun, var), dump_flags);
261 if (DECL_INITIAL (var))
263 fprintf (file, ", initial: ");
264 print_generic_expr (file, DECL_INITIAL (var), dump_flags);
267 fprintf (file, "\n");
271 /* Dump variable VAR and its may-aliases to stderr. */
273 DEBUG_FUNCTION void
274 debug_variable (tree var)
276 dump_variable (stderr, var);
280 /* Dump various DFA statistics to FILE. */
282 void
283 dump_dfa_stats (FILE *file)
285 struct dfa_stats_d dfa_stats;
287 unsigned long size, total = 0;
288 const char * const fmt_str = "%-30s%-13s%12s\n";
289 const char * const fmt_str_1 = "%-30s%13lu%11lu%c\n";
290 const char * const fmt_str_3 = "%-43s%11lu%c\n";
291 const char *funcname
292 = lang_hooks.decl_printable_name (current_function_decl, 2);
294 collect_dfa_stats (&dfa_stats);
296 fprintf (file, "\nDFA Statistics for %s\n\n", funcname);
298 fprintf (file, "---------------------------------------------------------\n");
299 fprintf (file, fmt_str, "", " Number of ", "Memory");
300 fprintf (file, fmt_str, "", " instances ", "used ");
301 fprintf (file, "---------------------------------------------------------\n");
303 size = num_referenced_vars * sizeof (tree);
304 total += size;
305 fprintf (file, fmt_str_1, "Referenced variables", (unsigned long)num_referenced_vars,
306 SCALE (size), LABEL (size));
308 size = dfa_stats.num_var_anns * sizeof (struct var_ann_d);
309 total += size;
310 fprintf (file, fmt_str_1, "Variables annotated", dfa_stats.num_var_anns,
311 SCALE (size), LABEL (size));
313 size = dfa_stats.num_uses * sizeof (tree *);
314 total += size;
315 fprintf (file, fmt_str_1, "USE operands", dfa_stats.num_uses,
316 SCALE (size), LABEL (size));
318 size = dfa_stats.num_defs * sizeof (tree *);
319 total += size;
320 fprintf (file, fmt_str_1, "DEF operands", dfa_stats.num_defs,
321 SCALE (size), LABEL (size));
323 size = dfa_stats.num_vuses * sizeof (tree *);
324 total += size;
325 fprintf (file, fmt_str_1, "VUSE operands", dfa_stats.num_vuses,
326 SCALE (size), LABEL (size));
328 size = dfa_stats.num_vdefs * sizeof (tree *);
329 total += size;
330 fprintf (file, fmt_str_1, "VDEF operands", dfa_stats.num_vdefs,
331 SCALE (size), LABEL (size));
333 size = dfa_stats.num_phis * sizeof (struct gimple_statement_phi);
334 total += size;
335 fprintf (file, fmt_str_1, "PHI nodes", dfa_stats.num_phis,
336 SCALE (size), LABEL (size));
338 size = dfa_stats.num_phi_args * sizeof (struct phi_arg_d);
339 total += size;
340 fprintf (file, fmt_str_1, "PHI arguments", dfa_stats.num_phi_args,
341 SCALE (size), LABEL (size));
343 fprintf (file, "---------------------------------------------------------\n");
344 fprintf (file, fmt_str_3, "Total memory used by DFA/SSA data", SCALE (total),
345 LABEL (total));
346 fprintf (file, "---------------------------------------------------------\n");
347 fprintf (file, "\n");
349 if (dfa_stats.num_phis)
350 fprintf (file, "Average number of arguments per PHI node: %.1f (max: %ld)\n",
351 (float) dfa_stats.num_phi_args / (float) dfa_stats.num_phis,
352 (long) dfa_stats.max_num_phi_args);
354 fprintf (file, "\n");
358 /* Dump DFA statistics on stderr. */
360 DEBUG_FUNCTION void
361 debug_dfa_stats (void)
363 dump_dfa_stats (stderr);
367 /* Collect DFA statistics and store them in the structure pointed to by
368 DFA_STATS_P. */
370 static void
371 collect_dfa_stats (struct dfa_stats_d *dfa_stats_p ATTRIBUTE_UNUSED)
373 basic_block bb;
375 gcc_assert (dfa_stats_p);
377 memset ((void *)dfa_stats_p, 0, sizeof (struct dfa_stats_d));
379 /* Count all the variable annotations. */
380 dfa_stats_p->num_var_anns = htab_elements (gimple_referenced_vars (cfun));
382 /* Walk all the statements in the function counting references. */
383 FOR_EACH_BB (bb)
385 gimple_stmt_iterator si;
387 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
389 gimple phi = gsi_stmt (si);
390 dfa_stats_p->num_phis++;
391 dfa_stats_p->num_phi_args += gimple_phi_num_args (phi);
392 if (gimple_phi_num_args (phi) > dfa_stats_p->max_num_phi_args)
393 dfa_stats_p->max_num_phi_args = gimple_phi_num_args (phi);
396 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
398 gimple stmt = gsi_stmt (si);
399 dfa_stats_p->num_defs += NUM_SSA_OPERANDS (stmt, SSA_OP_DEF);
400 dfa_stats_p->num_uses += NUM_SSA_OPERANDS (stmt, SSA_OP_USE);
401 dfa_stats_p->num_vdefs += gimple_vdef (stmt) ? 1 : 0;
402 dfa_stats_p->num_vuses += gimple_vuse (stmt) ? 1 : 0;
408 /*---------------------------------------------------------------------------
409 Miscellaneous helpers
410 ---------------------------------------------------------------------------*/
411 /* Callback for walk_tree. Used to collect variables referenced in
412 the function. */
414 static tree
415 find_vars_r (tree *tp, int *walk_subtrees, void *data)
417 struct function *fn = (struct function *) data;
419 /* If we are reading the lto info back in, we need to rescan the
420 referenced vars. */
421 if (TREE_CODE (*tp) == SSA_NAME)
422 add_referenced_var_1 (SSA_NAME_VAR (*tp), fn);
424 /* If T is a regular variable that the optimizers are interested
425 in, add it to the list of variables. */
426 else if ((TREE_CODE (*tp) == VAR_DECL
427 && !is_global_var (*tp))
428 || TREE_CODE (*tp) == PARM_DECL
429 || TREE_CODE (*tp) == RESULT_DECL)
430 add_referenced_var_1 (*tp, fn);
432 /* Type, _DECL and constant nodes have no interesting children.
433 Ignore them. */
434 else if (IS_TYPE_OR_DECL_P (*tp) || CONSTANT_CLASS_P (*tp))
435 *walk_subtrees = 0;
437 return NULL_TREE;
440 /* Find referenced variables in STMT. */
442 void
443 find_referenced_vars_in (gimple stmt)
445 size_t i;
447 if (gimple_code (stmt) != GIMPLE_PHI)
449 for (i = 0; i < gimple_num_ops (stmt); i++)
450 walk_tree (gimple_op_ptr (stmt, i), find_vars_r, cfun, NULL);
452 else
454 walk_tree (gimple_phi_result_ptr (stmt), find_vars_r, cfun, NULL);
456 for (i = 0; i < gimple_phi_num_args (stmt); i++)
458 tree arg = gimple_phi_arg_def (stmt, i);
459 walk_tree (&arg, find_vars_r, cfun, NULL);
465 /* Lookup UID in the referenced_vars hashtable and return the associated
466 variable. */
468 tree
469 referenced_var_lookup (struct function *fn, unsigned int uid)
471 tree h;
472 struct tree_decl_minimal in;
473 in.uid = uid;
474 h = (tree) htab_find_with_hash (gimple_referenced_vars (fn), &in, uid);
475 return h;
478 /* Check if TO is in the referenced_vars hash table and insert it if not.
479 Return true if it required insertion. */
481 static bool
482 referenced_var_check_and_insert (tree to, struct function *fn)
484 tree *loc;
485 struct tree_decl_minimal in;
486 unsigned int uid = DECL_UID (to);
488 in.uid = uid;
489 loc = (tree *) htab_find_slot_with_hash (gimple_referenced_vars (fn),
490 &in, uid, INSERT);
491 if (*loc)
493 /* DECL_UID has already been entered in the table. Verify that it is
494 the same entry as TO. See PR 27793. */
495 gcc_assert (*loc == to);
496 return false;
499 *loc = to;
500 return true;
503 /* Lookup VAR UID in the default_defs hashtable and return the associated
504 variable. */
506 tree
507 gimple_default_def (struct function *fn, tree var)
509 struct tree_decl_minimal ind;
510 struct tree_ssa_name in;
511 gcc_assert (SSA_VAR_P (var));
512 in.var = (tree)&ind;
513 ind.uid = DECL_UID (var);
514 return (tree) htab_find_with_hash (DEFAULT_DEFS (fn), &in, DECL_UID (var));
517 /* Insert the pair VAR's UID, DEF into the default_defs hashtable. */
519 void
520 set_default_def (tree var, tree def)
522 struct tree_decl_minimal ind;
523 struct tree_ssa_name in;
524 void **loc;
526 gcc_assert (SSA_VAR_P (var));
527 in.var = (tree)&ind;
528 ind.uid = DECL_UID (var);
529 if (!def)
531 loc = htab_find_slot_with_hash (DEFAULT_DEFS (cfun), &in,
532 DECL_UID (var), INSERT);
533 gcc_assert (*loc);
534 htab_remove_elt (DEFAULT_DEFS (cfun), *loc);
535 return;
537 gcc_assert (TREE_CODE (def) == SSA_NAME && SSA_NAME_VAR (def) == var);
538 loc = htab_find_slot_with_hash (DEFAULT_DEFS (cfun), &in,
539 DECL_UID (var), INSERT);
541 /* Default definition might be changed by tail call optimization. */
542 if (*loc)
543 SSA_NAME_IS_DEFAULT_DEF (*(tree *) loc) = false;
544 *(tree *) loc = def;
546 /* Mark DEF as the default definition for VAR. */
547 SSA_NAME_IS_DEFAULT_DEF (def) = true;
550 /* Add VAR to the list of referenced variables if it isn't already there. */
552 bool
553 add_referenced_var_1 (tree var, struct function *fn)
555 gcc_checking_assert (TREE_CODE (var) == VAR_DECL
556 || TREE_CODE (var) == PARM_DECL
557 || TREE_CODE (var) == RESULT_DECL);
559 gcc_checking_assert ((TREE_CODE (var) == VAR_DECL
560 && VAR_DECL_IS_VIRTUAL_OPERAND (var))
561 || !is_global_var (var));
563 /* Insert VAR into the referenced_vars hash table if it isn't present
564 and allocate its var-annotation. */
565 if (referenced_var_check_and_insert (var, fn))
567 gcc_checking_assert (!*DECL_VAR_ANN_PTR (var));
568 *DECL_VAR_ANN_PTR (var) = ggc_alloc_cleared_var_ann_d ();
569 return true;
572 return false;
575 /* Remove VAR from the list of referenced variables and clear its
576 var-annotation. */
578 void
579 remove_referenced_var (tree var)
581 var_ann_t v_ann;
582 struct tree_decl_minimal in;
583 void **loc;
584 unsigned int uid = DECL_UID (var);
586 gcc_checking_assert (TREE_CODE (var) == VAR_DECL
587 || TREE_CODE (var) == PARM_DECL
588 || TREE_CODE (var) == RESULT_DECL);
590 gcc_checking_assert (!is_global_var (var));
592 v_ann = var_ann (var);
593 ggc_free (v_ann);
594 *DECL_VAR_ANN_PTR (var) = NULL;
596 in.uid = uid;
597 loc = htab_find_slot_with_hash (gimple_referenced_vars (cfun), &in, uid,
598 NO_INSERT);
599 htab_clear_slot (gimple_referenced_vars (cfun), loc);
603 /* If EXP is a handled component reference for a structure, return the
604 base variable. The access range is delimited by bit positions *POFFSET and
605 *POFFSET + *PMAX_SIZE. The access size is *PSIZE bits. If either
606 *PSIZE or *PMAX_SIZE is -1, they could not be determined. If *PSIZE
607 and *PMAX_SIZE are equal, the access is non-variable. */
609 tree
610 get_ref_base_and_extent (tree exp, HOST_WIDE_INT *poffset,
611 HOST_WIDE_INT *psize,
612 HOST_WIDE_INT *pmax_size)
614 HOST_WIDE_INT bitsize = -1;
615 HOST_WIDE_INT maxsize = -1;
616 tree size_tree = NULL_TREE;
617 double_int bit_offset = double_int_zero;
618 HOST_WIDE_INT hbit_offset;
619 bool seen_variable_array_ref = false;
620 tree base_type;
622 /* First get the final access size from just the outermost expression. */
623 if (TREE_CODE (exp) == COMPONENT_REF)
624 size_tree = DECL_SIZE (TREE_OPERAND (exp, 1));
625 else if (TREE_CODE (exp) == BIT_FIELD_REF)
626 size_tree = TREE_OPERAND (exp, 1);
627 else if (!VOID_TYPE_P (TREE_TYPE (exp)))
629 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
630 if (mode == BLKmode)
631 size_tree = TYPE_SIZE (TREE_TYPE (exp));
632 else
633 bitsize = GET_MODE_BITSIZE (mode);
635 if (size_tree != NULL_TREE)
637 if (! host_integerp (size_tree, 1))
638 bitsize = -1;
639 else
640 bitsize = TREE_INT_CST_LOW (size_tree);
643 /* Initially, maxsize is the same as the accessed element size.
644 In the following it will only grow (or become -1). */
645 maxsize = bitsize;
647 /* Compute cumulative bit-offset for nested component-refs and array-refs,
648 and find the ultimate containing object. */
649 while (1)
651 base_type = TREE_TYPE (exp);
653 switch (TREE_CODE (exp))
655 case BIT_FIELD_REF:
656 bit_offset
657 = double_int_add (bit_offset,
658 tree_to_double_int (TREE_OPERAND (exp, 2)));
659 break;
661 case COMPONENT_REF:
663 tree field = TREE_OPERAND (exp, 1);
664 tree this_offset = component_ref_field_offset (exp);
666 if (this_offset && TREE_CODE (this_offset) == INTEGER_CST)
668 double_int doffset = tree_to_double_int (this_offset);
669 doffset = double_int_lshift (doffset,
670 BITS_PER_UNIT == 8
671 ? 3 : exact_log2 (BITS_PER_UNIT),
672 HOST_BITS_PER_DOUBLE_INT, true);
673 doffset = double_int_add (doffset,
674 tree_to_double_int
675 (DECL_FIELD_BIT_OFFSET (field)));
676 bit_offset = double_int_add (bit_offset, doffset);
678 /* If we had seen a variable array ref already and we just
679 referenced the last field of a struct or a union member
680 then we have to adjust maxsize by the padding at the end
681 of our field. */
682 if (seen_variable_array_ref && maxsize != -1)
684 tree stype = TREE_TYPE (TREE_OPERAND (exp, 0));
685 tree next = DECL_CHAIN (field);
686 while (next && TREE_CODE (next) != FIELD_DECL)
687 next = DECL_CHAIN (next);
688 if (!next
689 || TREE_CODE (stype) != RECORD_TYPE)
691 tree fsize = DECL_SIZE_UNIT (field);
692 tree ssize = TYPE_SIZE_UNIT (stype);
693 if (host_integerp (fsize, 0)
694 && host_integerp (ssize, 0)
695 && double_int_fits_in_shwi_p (doffset))
696 maxsize += ((TREE_INT_CST_LOW (ssize)
697 - TREE_INT_CST_LOW (fsize))
698 * BITS_PER_UNIT
699 - double_int_to_shwi (doffset));
700 else
701 maxsize = -1;
705 else
707 tree csize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
708 /* We need to adjust maxsize to the whole structure bitsize.
709 But we can subtract any constant offset seen so far,
710 because that would get us out of the structure otherwise. */
711 if (maxsize != -1
712 && csize
713 && host_integerp (csize, 1)
714 && double_int_fits_in_shwi_p (bit_offset))
715 maxsize = TREE_INT_CST_LOW (csize)
716 - double_int_to_shwi (bit_offset);
717 else
718 maxsize = -1;
721 break;
723 case ARRAY_REF:
724 case ARRAY_RANGE_REF:
726 tree index = TREE_OPERAND (exp, 1);
727 tree low_bound, unit_size;
729 /* If the resulting bit-offset is constant, track it. */
730 if (TREE_CODE (index) == INTEGER_CST
731 && (low_bound = array_ref_low_bound (exp),
732 TREE_CODE (low_bound) == INTEGER_CST)
733 && (unit_size = array_ref_element_size (exp),
734 TREE_CODE (unit_size) == INTEGER_CST))
736 double_int doffset
737 = double_int_sext
738 (double_int_sub (TREE_INT_CST (index),
739 TREE_INT_CST (low_bound)),
740 TYPE_PRECISION (TREE_TYPE (index)));
741 doffset = double_int_mul (doffset,
742 tree_to_double_int (unit_size));
743 doffset = double_int_lshift (doffset,
744 BITS_PER_UNIT == 8
745 ? 3 : exact_log2 (BITS_PER_UNIT),
746 HOST_BITS_PER_DOUBLE_INT, true);
747 bit_offset = double_int_add (bit_offset, doffset);
749 /* An array ref with a constant index up in the structure
750 hierarchy will constrain the size of any variable array ref
751 lower in the access hierarchy. */
752 seen_variable_array_ref = false;
754 else
756 tree asize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
757 /* We need to adjust maxsize to the whole array bitsize.
758 But we can subtract any constant offset seen so far,
759 because that would get us outside of the array otherwise. */
760 if (maxsize != -1
761 && asize
762 && host_integerp (asize, 1)
763 && double_int_fits_in_shwi_p (bit_offset))
764 maxsize = TREE_INT_CST_LOW (asize)
765 - double_int_to_shwi (bit_offset);
766 else
767 maxsize = -1;
769 /* Remember that we have seen an array ref with a variable
770 index. */
771 seen_variable_array_ref = true;
774 break;
776 case REALPART_EXPR:
777 break;
779 case IMAGPART_EXPR:
780 bit_offset
781 = double_int_add (bit_offset, uhwi_to_double_int (bitsize));
782 break;
784 case VIEW_CONVERT_EXPR:
785 break;
787 case MEM_REF:
788 /* Hand back the decl for MEM[&decl, off]. */
789 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
791 if (integer_zerop (TREE_OPERAND (exp, 1)))
792 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
793 else
795 double_int off = mem_ref_offset (exp);
796 off = double_int_lshift (off,
797 BITS_PER_UNIT == 8
798 ? 3 : exact_log2 (BITS_PER_UNIT),
799 HOST_BITS_PER_DOUBLE_INT, true);
800 off = double_int_add (off, bit_offset);
801 if (double_int_fits_in_shwi_p (off))
803 bit_offset = off;
804 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
808 goto done;
810 case TARGET_MEM_REF:
811 /* Hand back the decl for MEM[&decl, off]. */
812 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR)
814 /* Via the variable index or index2 we can reach the
815 whole object. */
816 if (TMR_INDEX (exp) || TMR_INDEX2 (exp))
818 exp = TREE_OPERAND (TMR_BASE (exp), 0);
819 bit_offset = double_int_zero;
820 maxsize = -1;
821 goto done;
823 if (integer_zerop (TMR_OFFSET (exp)))
824 exp = TREE_OPERAND (TMR_BASE (exp), 0);
825 else
827 double_int off = mem_ref_offset (exp);
828 off = double_int_lshift (off,
829 BITS_PER_UNIT == 8
830 ? 3 : exact_log2 (BITS_PER_UNIT),
831 HOST_BITS_PER_DOUBLE_INT, true);
832 off = double_int_add (off, bit_offset);
833 if (double_int_fits_in_shwi_p (off))
835 bit_offset = off;
836 exp = TREE_OPERAND (TMR_BASE (exp), 0);
840 goto done;
842 default:
843 goto done;
846 exp = TREE_OPERAND (exp, 0);
848 done:
850 if (!double_int_fits_in_shwi_p (bit_offset))
852 *poffset = 0;
853 *psize = bitsize;
854 *pmax_size = -1;
856 return exp;
859 hbit_offset = double_int_to_shwi (bit_offset);
861 /* We need to deal with variable arrays ending structures such as
862 struct { int length; int a[1]; } x; x.a[d]
863 struct { struct { int a; int b; } a[1]; } x; x.a[d].a
864 struct { struct { int a[1]; } a[1]; } x; x.a[0][d], x.a[d][0]
865 struct { int len; union { int a[1]; struct X x; } u; } x; x.u.a[d]
866 where we do not know maxsize for variable index accesses to
867 the array. The simplest way to conservatively deal with this
868 is to punt in the case that offset + maxsize reaches the
869 base type boundary. This needs to include possible trailing padding
870 that is there for alignment purposes. */
872 if (seen_variable_array_ref
873 && maxsize != -1
874 && (!host_integerp (TYPE_SIZE (base_type), 1)
875 || (hbit_offset + maxsize
876 == (signed) TREE_INT_CST_LOW (TYPE_SIZE (base_type)))))
877 maxsize = -1;
879 /* In case of a decl or constant base object we can do better. */
881 if (DECL_P (exp))
883 /* If maxsize is unknown adjust it according to the size of the
884 base decl. */
885 if (maxsize == -1
886 && host_integerp (DECL_SIZE (exp), 1))
887 maxsize = TREE_INT_CST_LOW (DECL_SIZE (exp)) - hbit_offset;
889 else if (CONSTANT_CLASS_P (exp))
891 /* If maxsize is unknown adjust it according to the size of the
892 base type constant. */
893 if (maxsize == -1
894 && host_integerp (TYPE_SIZE (TREE_TYPE (exp)), 1))
895 maxsize = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp))) - hbit_offset;
898 /* ??? Due to negative offsets in ARRAY_REF we can end up with
899 negative bit_offset here. We might want to store a zero offset
900 in this case. */
901 *poffset = hbit_offset;
902 *psize = bitsize;
903 *pmax_size = maxsize;
905 return exp;
908 /* Returns the base object and a constant BITS_PER_UNIT offset in *POFFSET that
909 denotes the starting address of the memory access EXP.
910 Returns NULL_TREE if the offset is not constant or any component
911 is not BITS_PER_UNIT-aligned. */
913 tree
914 get_addr_base_and_unit_offset (tree exp, HOST_WIDE_INT *poffset)
916 return get_addr_base_and_unit_offset_1 (exp, poffset, NULL);
919 /* Returns true if STMT references an SSA_NAME that has
920 SSA_NAME_OCCURS_IN_ABNORMAL_PHI set, otherwise false. */
922 bool
923 stmt_references_abnormal_ssa_name (gimple stmt)
925 ssa_op_iter oi;
926 use_operand_p use_p;
928 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE)
930 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (use_p)))
931 return true;
934 return false;
937 /* Pair of tree and a sorting index, for dump_enumerated_decls. */
938 struct GTY(()) numbered_tree_d
940 tree t;
941 int num;
943 typedef struct numbered_tree_d numbered_tree;
945 DEF_VEC_O (numbered_tree);
946 DEF_VEC_ALLOC_O (numbered_tree, heap);
948 /* Compare two declarations references by their DECL_UID / sequence number.
949 Called via qsort. */
951 static int
952 compare_decls_by_uid (const void *pa, const void *pb)
954 const numbered_tree *nt_a = ((const numbered_tree *)pa);
955 const numbered_tree *nt_b = ((const numbered_tree *)pb);
957 if (DECL_UID (nt_a->t) != DECL_UID (nt_b->t))
958 return DECL_UID (nt_a->t) - DECL_UID (nt_b->t);
959 return nt_a->num - nt_b->num;
962 /* Called via walk_gimple_stmt / walk_gimple_op by dump_enumerated_decls. */
963 static tree
964 dump_enumerated_decls_push (tree *tp, int *walk_subtrees, void *data)
966 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
967 VEC (numbered_tree, heap) **list = (VEC (numbered_tree, heap) **) &wi->info;
968 numbered_tree nt;
970 if (!DECL_P (*tp))
971 return NULL_TREE;
972 nt.t = *tp;
973 nt.num = VEC_length (numbered_tree, *list);
974 VEC_safe_push (numbered_tree, heap, *list, &nt);
975 *walk_subtrees = 0;
976 return NULL_TREE;
979 /* Find all the declarations used by the current function, sort them by uid,
980 and emit the sorted list. Each declaration is tagged with a sequence
981 number indicating when it was found during statement / tree walking,
982 so that TDF_NOUID comparisons of anonymous declarations are still
983 meaningful. Where a declaration was encountered more than once, we
984 emit only the sequence number of the first encounter.
985 FILE is the dump file where to output the list and FLAGS is as in
986 print_generic_expr. */
987 void
988 dump_enumerated_decls (FILE *file, int flags)
990 basic_block bb;
991 struct walk_stmt_info wi;
992 VEC (numbered_tree, heap) *decl_list = VEC_alloc (numbered_tree, heap, 40);
994 memset (&wi, '\0', sizeof (wi));
995 wi.info = (void*) decl_list;
996 FOR_EACH_BB (bb)
998 gimple_stmt_iterator gsi;
1000 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1001 if (!is_gimple_debug (gsi_stmt (gsi)))
1002 walk_gimple_stmt (&gsi, NULL, dump_enumerated_decls_push, &wi);
1004 decl_list = (VEC (numbered_tree, heap) *) wi.info;
1005 VEC_qsort (numbered_tree, decl_list, compare_decls_by_uid);
1006 if (VEC_length (numbered_tree, decl_list))
1008 unsigned ix;
1009 numbered_tree *ntp;
1010 tree last = NULL_TREE;
1012 fprintf (file, "Declarations used by %s, sorted by DECL_UID:\n",
1013 current_function_name ());
1014 FOR_EACH_VEC_ELT (numbered_tree, decl_list, ix, ntp)
1016 if (ntp->t == last)
1017 continue;
1018 fprintf (file, "%d: ", ntp->num);
1019 print_generic_decl (file, ntp->t, flags);
1020 fprintf (file, "\n");
1021 last = ntp->t;
1024 VEC_free (numbered_tree, heap, decl_list);