PR c++/53989
[official-gcc.git] / gcc / tree-dfa.c
blob7d20f6f1496acfaec84886a9bfbb166c9535b2d4
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;
374 referenced_var_iterator vi;
375 tree var;
377 gcc_assert (dfa_stats_p);
379 memset ((void *)dfa_stats_p, 0, sizeof (struct dfa_stats_d));
381 /* Count all the variable annotations. */
382 FOR_EACH_REFERENCED_VAR (cfun, var, vi)
383 if (var_ann (var))
384 dfa_stats_p->num_var_anns++;
386 /* Walk all the statements in the function counting references. */
387 FOR_EACH_BB (bb)
389 gimple_stmt_iterator si;
391 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
393 gimple phi = gsi_stmt (si);
394 dfa_stats_p->num_phis++;
395 dfa_stats_p->num_phi_args += gimple_phi_num_args (phi);
396 if (gimple_phi_num_args (phi) > dfa_stats_p->max_num_phi_args)
397 dfa_stats_p->max_num_phi_args = gimple_phi_num_args (phi);
400 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
402 gimple stmt = gsi_stmt (si);
403 dfa_stats_p->num_defs += NUM_SSA_OPERANDS (stmt, SSA_OP_DEF);
404 dfa_stats_p->num_uses += NUM_SSA_OPERANDS (stmt, SSA_OP_USE);
405 dfa_stats_p->num_vdefs += gimple_vdef (stmt) ? 1 : 0;
406 dfa_stats_p->num_vuses += gimple_vuse (stmt) ? 1 : 0;
412 /*---------------------------------------------------------------------------
413 Miscellaneous helpers
414 ---------------------------------------------------------------------------*/
415 /* Callback for walk_tree. Used to collect variables referenced in
416 the function. */
418 static tree
419 find_vars_r (tree *tp, int *walk_subtrees, void *data)
421 struct function *fn = (struct function *) data;
423 /* If we are reading the lto info back in, we need to rescan the
424 referenced vars. */
425 if (TREE_CODE (*tp) == SSA_NAME)
426 add_referenced_var_1 (SSA_NAME_VAR (*tp), fn);
428 /* If T is a regular variable that the optimizers are interested
429 in, add it to the list of variables. */
430 else if ((TREE_CODE (*tp) == VAR_DECL
431 && !is_global_var (*tp))
432 || TREE_CODE (*tp) == PARM_DECL
433 || TREE_CODE (*tp) == RESULT_DECL)
434 add_referenced_var_1 (*tp, fn);
436 /* Type, _DECL and constant nodes have no interesting children.
437 Ignore them. */
438 else if (IS_TYPE_OR_DECL_P (*tp) || CONSTANT_CLASS_P (*tp))
439 *walk_subtrees = 0;
441 return NULL_TREE;
444 /* Find referenced variables in STMT. */
446 void
447 find_referenced_vars_in (gimple stmt)
449 size_t i;
451 if (gimple_code (stmt) != GIMPLE_PHI)
453 for (i = 0; i < gimple_num_ops (stmt); i++)
454 walk_tree (gimple_op_ptr (stmt, i), find_vars_r, cfun, NULL);
456 else
458 walk_tree (gimple_phi_result_ptr (stmt), find_vars_r, cfun, NULL);
460 for (i = 0; i < gimple_phi_num_args (stmt); i++)
462 tree arg = gimple_phi_arg_def (stmt, i);
463 walk_tree (&arg, find_vars_r, cfun, NULL);
469 /* Lookup UID in the referenced_vars hashtable and return the associated
470 variable. */
472 tree
473 referenced_var_lookup (struct function *fn, unsigned int uid)
475 tree h;
476 struct tree_decl_minimal in;
477 in.uid = uid;
478 h = (tree) htab_find_with_hash (gimple_referenced_vars (fn), &in, uid);
479 return h;
482 /* Check if TO is in the referenced_vars hash table and insert it if not.
483 Return true if it required insertion. */
485 static bool
486 referenced_var_check_and_insert (tree to, struct function *fn)
488 tree *loc;
489 struct tree_decl_minimal in;
490 unsigned int uid = DECL_UID (to);
492 in.uid = uid;
493 loc = (tree *) htab_find_slot_with_hash (gimple_referenced_vars (fn),
494 &in, uid, INSERT);
495 if (*loc)
497 /* DECL_UID has already been entered in the table. Verify that it is
498 the same entry as TO. See PR 27793. */
499 gcc_assert (*loc == to);
500 return false;
503 *loc = to;
504 return true;
507 /* Lookup VAR UID in the default_defs hashtable and return the associated
508 variable. */
510 tree
511 gimple_default_def (struct function *fn, tree var)
513 struct tree_decl_minimal ind;
514 struct tree_ssa_name in;
515 gcc_assert (SSA_VAR_P (var));
516 in.var = (tree)&ind;
517 ind.uid = DECL_UID (var);
518 return (tree) htab_find_with_hash (DEFAULT_DEFS (fn), &in, DECL_UID (var));
521 /* Insert the pair VAR's UID, DEF into the default_defs hashtable. */
523 void
524 set_default_def (tree var, tree def)
526 struct tree_decl_minimal ind;
527 struct tree_ssa_name in;
528 void **loc;
530 gcc_assert (SSA_VAR_P (var));
531 in.var = (tree)&ind;
532 ind.uid = DECL_UID (var);
533 if (!def)
535 loc = htab_find_slot_with_hash (DEFAULT_DEFS (cfun), &in,
536 DECL_UID (var), INSERT);
537 gcc_assert (*loc);
538 htab_remove_elt (DEFAULT_DEFS (cfun), *loc);
539 return;
541 gcc_assert (TREE_CODE (def) == SSA_NAME && SSA_NAME_VAR (def) == var);
542 loc = htab_find_slot_with_hash (DEFAULT_DEFS (cfun), &in,
543 DECL_UID (var), INSERT);
545 /* Default definition might be changed by tail call optimization. */
546 if (*loc)
547 SSA_NAME_IS_DEFAULT_DEF (*(tree *) loc) = false;
548 *(tree *) loc = def;
550 /* Mark DEF as the default definition for VAR. */
551 SSA_NAME_IS_DEFAULT_DEF (def) = true;
554 /* Add VAR to the list of referenced variables if it isn't already there. */
556 bool
557 add_referenced_var_1 (tree var, struct function *fn)
559 gcc_checking_assert (TREE_CODE (var) == VAR_DECL
560 || TREE_CODE (var) == PARM_DECL
561 || TREE_CODE (var) == RESULT_DECL);
563 gcc_checking_assert ((TREE_CODE (var) == VAR_DECL
564 && VAR_DECL_IS_VIRTUAL_OPERAND (var))
565 || !is_global_var (var));
567 /* Insert VAR into the referenced_vars hash table if it isn't present
568 and allocate its var-annotation. */
569 if (referenced_var_check_and_insert (var, fn))
571 gcc_checking_assert (!*DECL_VAR_ANN_PTR (var));
572 *DECL_VAR_ANN_PTR (var) = ggc_alloc_cleared_var_ann_d ();
573 return true;
576 return false;
579 /* Remove VAR from the list of referenced variables and clear its
580 var-annotation. */
582 void
583 remove_referenced_var (tree var)
585 var_ann_t v_ann;
586 struct tree_decl_minimal in;
587 void **loc;
588 unsigned int uid = DECL_UID (var);
590 gcc_checking_assert (TREE_CODE (var) == VAR_DECL
591 || TREE_CODE (var) == PARM_DECL
592 || TREE_CODE (var) == RESULT_DECL);
594 gcc_checking_assert (!is_global_var (var));
596 v_ann = var_ann (var);
597 ggc_free (v_ann);
598 *DECL_VAR_ANN_PTR (var) = NULL;
600 in.uid = uid;
601 loc = htab_find_slot_with_hash (gimple_referenced_vars (cfun), &in, uid,
602 NO_INSERT);
603 htab_clear_slot (gimple_referenced_vars (cfun), loc);
607 /* If EXP is a handled component reference for a structure, return the
608 base variable. The access range is delimited by bit positions *POFFSET and
609 *POFFSET + *PMAX_SIZE. The access size is *PSIZE bits. If either
610 *PSIZE or *PMAX_SIZE is -1, they could not be determined. If *PSIZE
611 and *PMAX_SIZE are equal, the access is non-variable. */
613 tree
614 get_ref_base_and_extent (tree exp, HOST_WIDE_INT *poffset,
615 HOST_WIDE_INT *psize,
616 HOST_WIDE_INT *pmax_size)
618 HOST_WIDE_INT bitsize = -1;
619 HOST_WIDE_INT maxsize = -1;
620 tree size_tree = NULL_TREE;
621 double_int bit_offset = double_int_zero;
622 HOST_WIDE_INT hbit_offset;
623 bool seen_variable_array_ref = false;
624 tree base_type;
626 /* First get the final access size from just the outermost expression. */
627 if (TREE_CODE (exp) == COMPONENT_REF)
628 size_tree = DECL_SIZE (TREE_OPERAND (exp, 1));
629 else if (TREE_CODE (exp) == BIT_FIELD_REF)
630 size_tree = TREE_OPERAND (exp, 1);
631 else if (!VOID_TYPE_P (TREE_TYPE (exp)))
633 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
634 if (mode == BLKmode)
635 size_tree = TYPE_SIZE (TREE_TYPE (exp));
636 else
637 bitsize = GET_MODE_BITSIZE (mode);
639 if (size_tree != NULL_TREE)
641 if (! host_integerp (size_tree, 1))
642 bitsize = -1;
643 else
644 bitsize = TREE_INT_CST_LOW (size_tree);
647 /* Initially, maxsize is the same as the accessed element size.
648 In the following it will only grow (or become -1). */
649 maxsize = bitsize;
651 /* Compute cumulative bit-offset for nested component-refs and array-refs,
652 and find the ultimate containing object. */
653 while (1)
655 base_type = TREE_TYPE (exp);
657 switch (TREE_CODE (exp))
659 case BIT_FIELD_REF:
660 bit_offset
661 = double_int_add (bit_offset,
662 tree_to_double_int (TREE_OPERAND (exp, 2)));
663 break;
665 case COMPONENT_REF:
667 tree field = TREE_OPERAND (exp, 1);
668 tree this_offset = component_ref_field_offset (exp);
670 if (this_offset && TREE_CODE (this_offset) == INTEGER_CST)
672 double_int doffset = tree_to_double_int (this_offset);
673 doffset = double_int_lshift (doffset,
674 BITS_PER_UNIT == 8
675 ? 3 : exact_log2 (BITS_PER_UNIT),
676 HOST_BITS_PER_DOUBLE_INT, true);
677 doffset = double_int_add (doffset,
678 tree_to_double_int
679 (DECL_FIELD_BIT_OFFSET (field)));
680 bit_offset = double_int_add (bit_offset, doffset);
682 /* If we had seen a variable array ref already and we just
683 referenced the last field of a struct or a union member
684 then we have to adjust maxsize by the padding at the end
685 of our field. */
686 if (seen_variable_array_ref && maxsize != -1)
688 tree stype = TREE_TYPE (TREE_OPERAND (exp, 0));
689 tree next = DECL_CHAIN (field);
690 while (next && TREE_CODE (next) != FIELD_DECL)
691 next = DECL_CHAIN (next);
692 if (!next
693 || TREE_CODE (stype) != RECORD_TYPE)
695 tree fsize = DECL_SIZE_UNIT (field);
696 tree ssize = TYPE_SIZE_UNIT (stype);
697 if (host_integerp (fsize, 0)
698 && host_integerp (ssize, 0)
699 && double_int_fits_in_shwi_p (doffset))
700 maxsize += ((TREE_INT_CST_LOW (ssize)
701 - TREE_INT_CST_LOW (fsize))
702 * BITS_PER_UNIT
703 - double_int_to_shwi (doffset));
704 else
705 maxsize = -1;
709 else
711 tree csize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
712 /* We need to adjust maxsize to the whole structure bitsize.
713 But we can subtract any constant offset seen so far,
714 because that would get us out of the structure otherwise. */
715 if (maxsize != -1
716 && csize
717 && host_integerp (csize, 1)
718 && double_int_fits_in_shwi_p (bit_offset))
719 maxsize = TREE_INT_CST_LOW (csize)
720 - double_int_to_shwi (bit_offset);
721 else
722 maxsize = -1;
725 break;
727 case ARRAY_REF:
728 case ARRAY_RANGE_REF:
730 tree index = TREE_OPERAND (exp, 1);
731 tree low_bound, unit_size;
733 /* If the resulting bit-offset is constant, track it. */
734 if (TREE_CODE (index) == INTEGER_CST
735 && (low_bound = array_ref_low_bound (exp),
736 TREE_CODE (low_bound) == INTEGER_CST)
737 && (unit_size = array_ref_element_size (exp),
738 TREE_CODE (unit_size) == INTEGER_CST))
740 double_int doffset
741 = double_int_sext
742 (double_int_sub (TREE_INT_CST (index),
743 TREE_INT_CST (low_bound)),
744 TYPE_PRECISION (TREE_TYPE (index)));
745 doffset = double_int_mul (doffset,
746 tree_to_double_int (unit_size));
747 doffset = double_int_lshift (doffset,
748 BITS_PER_UNIT == 8
749 ? 3 : exact_log2 (BITS_PER_UNIT),
750 HOST_BITS_PER_DOUBLE_INT, true);
751 bit_offset = double_int_add (bit_offset, doffset);
753 /* An array ref with a constant index up in the structure
754 hierarchy will constrain the size of any variable array ref
755 lower in the access hierarchy. */
756 seen_variable_array_ref = false;
758 else
760 tree asize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
761 /* We need to adjust maxsize to the whole array bitsize.
762 But we can subtract any constant offset seen so far,
763 because that would get us outside of the array otherwise. */
764 if (maxsize != -1
765 && asize
766 && host_integerp (asize, 1)
767 && double_int_fits_in_shwi_p (bit_offset))
768 maxsize = TREE_INT_CST_LOW (asize)
769 - double_int_to_shwi (bit_offset);
770 else
771 maxsize = -1;
773 /* Remember that we have seen an array ref with a variable
774 index. */
775 seen_variable_array_ref = true;
778 break;
780 case REALPART_EXPR:
781 break;
783 case IMAGPART_EXPR:
784 bit_offset
785 = double_int_add (bit_offset, uhwi_to_double_int (bitsize));
786 break;
788 case VIEW_CONVERT_EXPR:
789 break;
791 case MEM_REF:
792 /* Hand back the decl for MEM[&decl, off]. */
793 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
795 if (integer_zerop (TREE_OPERAND (exp, 1)))
796 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
797 else
799 double_int off = mem_ref_offset (exp);
800 off = double_int_lshift (off,
801 BITS_PER_UNIT == 8
802 ? 3 : exact_log2 (BITS_PER_UNIT),
803 HOST_BITS_PER_DOUBLE_INT, true);
804 off = double_int_add (off, bit_offset);
805 if (double_int_fits_in_shwi_p (off))
807 bit_offset = off;
808 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
812 goto done;
814 case TARGET_MEM_REF:
815 /* Hand back the decl for MEM[&decl, off]. */
816 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR)
818 /* Via the variable index or index2 we can reach the
819 whole object. */
820 if (TMR_INDEX (exp) || TMR_INDEX2 (exp))
822 exp = TREE_OPERAND (TMR_BASE (exp), 0);
823 bit_offset = double_int_zero;
824 maxsize = -1;
825 goto done;
827 if (integer_zerop (TMR_OFFSET (exp)))
828 exp = TREE_OPERAND (TMR_BASE (exp), 0);
829 else
831 double_int off = mem_ref_offset (exp);
832 off = double_int_lshift (off,
833 BITS_PER_UNIT == 8
834 ? 3 : exact_log2 (BITS_PER_UNIT),
835 HOST_BITS_PER_DOUBLE_INT, true);
836 off = double_int_add (off, bit_offset);
837 if (double_int_fits_in_shwi_p (off))
839 bit_offset = off;
840 exp = TREE_OPERAND (TMR_BASE (exp), 0);
844 goto done;
846 default:
847 goto done;
850 exp = TREE_OPERAND (exp, 0);
852 done:
854 if (!double_int_fits_in_shwi_p (bit_offset))
856 *poffset = 0;
857 *psize = bitsize;
858 *pmax_size = -1;
860 return exp;
863 hbit_offset = double_int_to_shwi (bit_offset);
865 /* We need to deal with variable arrays ending structures such as
866 struct { int length; int a[1]; } x; x.a[d]
867 struct { struct { int a; int b; } a[1]; } x; x.a[d].a
868 struct { struct { int a[1]; } a[1]; } x; x.a[0][d], x.a[d][0]
869 struct { int len; union { int a[1]; struct X x; } u; } x; x.u.a[d]
870 where we do not know maxsize for variable index accesses to
871 the array. The simplest way to conservatively deal with this
872 is to punt in the case that offset + maxsize reaches the
873 base type boundary. This needs to include possible trailing padding
874 that is there for alignment purposes. */
876 if (seen_variable_array_ref
877 && maxsize != -1
878 && (!host_integerp (TYPE_SIZE (base_type), 1)
879 || (hbit_offset + maxsize
880 == (signed) TREE_INT_CST_LOW (TYPE_SIZE (base_type)))))
881 maxsize = -1;
883 /* In case of a decl or constant base object we can do better. */
885 if (DECL_P (exp))
887 /* If maxsize is unknown adjust it according to the size of the
888 base decl. */
889 if (maxsize == -1
890 && host_integerp (DECL_SIZE (exp), 1))
891 maxsize = TREE_INT_CST_LOW (DECL_SIZE (exp)) - hbit_offset;
893 else if (CONSTANT_CLASS_P (exp))
895 /* If maxsize is unknown adjust it according to the size of the
896 base type constant. */
897 if (maxsize == -1
898 && host_integerp (TYPE_SIZE (TREE_TYPE (exp)), 1))
899 maxsize = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp))) - hbit_offset;
902 /* ??? Due to negative offsets in ARRAY_REF we can end up with
903 negative bit_offset here. We might want to store a zero offset
904 in this case. */
905 *poffset = hbit_offset;
906 *psize = bitsize;
907 *pmax_size = maxsize;
909 return exp;
912 /* Returns the base object and a constant BITS_PER_UNIT offset in *POFFSET that
913 denotes the starting address of the memory access EXP.
914 Returns NULL_TREE if the offset is not constant or any component
915 is not BITS_PER_UNIT-aligned. */
917 tree
918 get_addr_base_and_unit_offset (tree exp, HOST_WIDE_INT *poffset)
920 return get_addr_base_and_unit_offset_1 (exp, poffset, NULL);
923 /* Returns true if STMT references an SSA_NAME that has
924 SSA_NAME_OCCURS_IN_ABNORMAL_PHI set, otherwise false. */
926 bool
927 stmt_references_abnormal_ssa_name (gimple stmt)
929 ssa_op_iter oi;
930 use_operand_p use_p;
932 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE)
934 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (use_p)))
935 return true;
938 return false;
941 /* Pair of tree and a sorting index, for dump_enumerated_decls. */
942 struct GTY(()) numbered_tree_d
944 tree t;
945 int num;
947 typedef struct numbered_tree_d numbered_tree;
949 DEF_VEC_O (numbered_tree);
950 DEF_VEC_ALLOC_O (numbered_tree, heap);
952 /* Compare two declarations references by their DECL_UID / sequence number.
953 Called via qsort. */
955 static int
956 compare_decls_by_uid (const void *pa, const void *pb)
958 const numbered_tree *nt_a = ((const numbered_tree *)pa);
959 const numbered_tree *nt_b = ((const numbered_tree *)pb);
961 if (DECL_UID (nt_a->t) != DECL_UID (nt_b->t))
962 return DECL_UID (nt_a->t) - DECL_UID (nt_b->t);
963 return nt_a->num - nt_b->num;
966 /* Called via walk_gimple_stmt / walk_gimple_op by dump_enumerated_decls. */
967 static tree
968 dump_enumerated_decls_push (tree *tp, int *walk_subtrees, void *data)
970 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
971 VEC (numbered_tree, heap) **list = (VEC (numbered_tree, heap) **) &wi->info;
972 numbered_tree nt;
974 if (!DECL_P (*tp))
975 return NULL_TREE;
976 nt.t = *tp;
977 nt.num = VEC_length (numbered_tree, *list);
978 VEC_safe_push (numbered_tree, heap, *list, &nt);
979 *walk_subtrees = 0;
980 return NULL_TREE;
983 /* Find all the declarations used by the current function, sort them by uid,
984 and emit the sorted list. Each declaration is tagged with a sequence
985 number indicating when it was found during statement / tree walking,
986 so that TDF_NOUID comparisons of anonymous declarations are still
987 meaningful. Where a declaration was encountered more than once, we
988 emit only the sequence number of the first encounter.
989 FILE is the dump file where to output the list and FLAGS is as in
990 print_generic_expr. */
991 void
992 dump_enumerated_decls (FILE *file, int flags)
994 basic_block bb;
995 struct walk_stmt_info wi;
996 VEC (numbered_tree, heap) *decl_list = VEC_alloc (numbered_tree, heap, 40);
998 memset (&wi, '\0', sizeof (wi));
999 wi.info = (void*) decl_list;
1000 FOR_EACH_BB (bb)
1002 gimple_stmt_iterator gsi;
1004 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1005 if (!is_gimple_debug (gsi_stmt (gsi)))
1006 walk_gimple_stmt (&gsi, NULL, dump_enumerated_decls_push, &wi);
1008 decl_list = (VEC (numbered_tree, heap) *) wi.info;
1009 VEC_qsort (numbered_tree, decl_list, compare_decls_by_uid);
1010 if (VEC_length (numbered_tree, decl_list))
1012 unsigned ix;
1013 numbered_tree *ntp;
1014 tree last = NULL_TREE;
1016 fprintf (file, "Declarations used by %s, sorted by DECL_UID:\n",
1017 current_function_name ());
1018 FOR_EACH_VEC_ELT (numbered_tree, decl_list, ix, ntp)
1020 if (ntp->t == last)
1021 continue;
1022 fprintf (file, "%d: ", ntp->num);
1023 print_generic_decl (file, ntp->t, flags);
1024 fprintf (file, "\n");
1025 last = ntp->t;
1028 VEC_free (numbered_tree, heap, decl_list);