2012-08-01 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / tree-dfa.c
blob8989a5b869e8fa4c2c57ad2edf066ba856b4833f
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);
178 return t;
183 /*---------------------------------------------------------------------------
184 Debugging functions
185 ---------------------------------------------------------------------------*/
186 /* Dump the list of all the referenced variables in the current function to
187 FILE. */
189 void
190 dump_referenced_vars (FILE *file)
192 tree var;
193 referenced_var_iterator rvi;
195 fprintf (file, "\nReferenced variables in %s: %u\n\n",
196 get_name (current_function_decl), (unsigned) num_referenced_vars);
198 FOR_EACH_REFERENCED_VAR (cfun, var, rvi)
200 fprintf (file, "Variable: ");
201 dump_variable (file, var);
204 fprintf (file, "\n");
208 /* Dump the list of all the referenced variables to stderr. */
210 DEBUG_FUNCTION void
211 debug_referenced_vars (void)
213 dump_referenced_vars (stderr);
217 /* Dump variable VAR and its may-aliases to FILE. */
219 void
220 dump_variable (FILE *file, tree var)
222 if (TREE_CODE (var) == SSA_NAME)
224 if (POINTER_TYPE_P (TREE_TYPE (var)))
225 dump_points_to_info_for (file, var);
226 var = SSA_NAME_VAR (var);
229 if (var == NULL_TREE)
231 fprintf (file, "<nil>");
232 return;
235 print_generic_expr (file, var, dump_flags);
237 fprintf (file, ", UID D.%u", (unsigned) DECL_UID (var));
238 if (DECL_PT_UID (var) != DECL_UID (var))
239 fprintf (file, ", PT-UID D.%u", (unsigned) DECL_PT_UID (var));
241 fprintf (file, ", ");
242 print_generic_expr (file, TREE_TYPE (var), dump_flags);
244 if (TREE_ADDRESSABLE (var))
245 fprintf (file, ", is addressable");
247 if (is_global_var (var))
248 fprintf (file, ", is global");
250 if (TREE_THIS_VOLATILE (var))
251 fprintf (file, ", is volatile");
253 if (cfun && gimple_default_def (cfun, var))
255 fprintf (file, ", default def: ");
256 print_generic_expr (file, gimple_default_def (cfun, var), dump_flags);
259 if (DECL_INITIAL (var))
261 fprintf (file, ", initial: ");
262 print_generic_expr (file, DECL_INITIAL (var), dump_flags);
265 fprintf (file, "\n");
269 /* Dump variable VAR and its may-aliases to stderr. */
271 DEBUG_FUNCTION void
272 debug_variable (tree var)
274 dump_variable (stderr, var);
278 /* Dump various DFA statistics to FILE. */
280 void
281 dump_dfa_stats (FILE *file)
283 struct dfa_stats_d dfa_stats;
285 unsigned long size, total = 0;
286 const char * const fmt_str = "%-30s%-13s%12s\n";
287 const char * const fmt_str_1 = "%-30s%13lu%11lu%c\n";
288 const char * const fmt_str_3 = "%-43s%11lu%c\n";
289 const char *funcname
290 = lang_hooks.decl_printable_name (current_function_decl, 2);
292 collect_dfa_stats (&dfa_stats);
294 fprintf (file, "\nDFA Statistics for %s\n\n", funcname);
296 fprintf (file, "---------------------------------------------------------\n");
297 fprintf (file, fmt_str, "", " Number of ", "Memory");
298 fprintf (file, fmt_str, "", " instances ", "used ");
299 fprintf (file, "---------------------------------------------------------\n");
301 size = num_referenced_vars * sizeof (tree);
302 total += size;
303 fprintf (file, fmt_str_1, "Referenced variables", (unsigned long)num_referenced_vars,
304 SCALE (size), LABEL (size));
306 size = dfa_stats.num_var_anns * sizeof (struct var_ann_d);
307 total += size;
308 fprintf (file, fmt_str_1, "Variables annotated", dfa_stats.num_var_anns,
309 SCALE (size), LABEL (size));
311 size = dfa_stats.num_uses * sizeof (tree *);
312 total += size;
313 fprintf (file, fmt_str_1, "USE operands", dfa_stats.num_uses,
314 SCALE (size), LABEL (size));
316 size = dfa_stats.num_defs * sizeof (tree *);
317 total += size;
318 fprintf (file, fmt_str_1, "DEF operands", dfa_stats.num_defs,
319 SCALE (size), LABEL (size));
321 size = dfa_stats.num_vuses * sizeof (tree *);
322 total += size;
323 fprintf (file, fmt_str_1, "VUSE operands", dfa_stats.num_vuses,
324 SCALE (size), LABEL (size));
326 size = dfa_stats.num_vdefs * sizeof (tree *);
327 total += size;
328 fprintf (file, fmt_str_1, "VDEF operands", dfa_stats.num_vdefs,
329 SCALE (size), LABEL (size));
331 size = dfa_stats.num_phis * sizeof (struct gimple_statement_phi);
332 total += size;
333 fprintf (file, fmt_str_1, "PHI nodes", dfa_stats.num_phis,
334 SCALE (size), LABEL (size));
336 size = dfa_stats.num_phi_args * sizeof (struct phi_arg_d);
337 total += size;
338 fprintf (file, fmt_str_1, "PHI arguments", dfa_stats.num_phi_args,
339 SCALE (size), LABEL (size));
341 fprintf (file, "---------------------------------------------------------\n");
342 fprintf (file, fmt_str_3, "Total memory used by DFA/SSA data", SCALE (total),
343 LABEL (total));
344 fprintf (file, "---------------------------------------------------------\n");
345 fprintf (file, "\n");
347 if (dfa_stats.num_phis)
348 fprintf (file, "Average number of arguments per PHI node: %.1f (max: %ld)\n",
349 (float) dfa_stats.num_phi_args / (float) dfa_stats.num_phis,
350 (long) dfa_stats.max_num_phi_args);
352 fprintf (file, "\n");
356 /* Dump DFA statistics on stderr. */
358 DEBUG_FUNCTION void
359 debug_dfa_stats (void)
361 dump_dfa_stats (stderr);
365 /* Collect DFA statistics and store them in the structure pointed to by
366 DFA_STATS_P. */
368 static void
369 collect_dfa_stats (struct dfa_stats_d *dfa_stats_p ATTRIBUTE_UNUSED)
371 basic_block bb;
373 gcc_assert (dfa_stats_p);
375 memset ((void *)dfa_stats_p, 0, sizeof (struct dfa_stats_d));
377 /* Count all the variable annotations. */
378 dfa_stats_p->num_var_anns = htab_elements (gimple_referenced_vars (cfun));
380 /* Walk all the statements in the function counting references. */
381 FOR_EACH_BB (bb)
383 gimple_stmt_iterator si;
385 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
387 gimple phi = gsi_stmt (si);
388 dfa_stats_p->num_phis++;
389 dfa_stats_p->num_phi_args += gimple_phi_num_args (phi);
390 if (gimple_phi_num_args (phi) > dfa_stats_p->max_num_phi_args)
391 dfa_stats_p->max_num_phi_args = gimple_phi_num_args (phi);
394 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
396 gimple stmt = gsi_stmt (si);
397 dfa_stats_p->num_defs += NUM_SSA_OPERANDS (stmt, SSA_OP_DEF);
398 dfa_stats_p->num_uses += NUM_SSA_OPERANDS (stmt, SSA_OP_USE);
399 dfa_stats_p->num_vdefs += gimple_vdef (stmt) ? 1 : 0;
400 dfa_stats_p->num_vuses += gimple_vuse (stmt) ? 1 : 0;
406 /*---------------------------------------------------------------------------
407 Miscellaneous helpers
408 ---------------------------------------------------------------------------*/
409 /* Callback for walk_tree. Used to collect variables referenced in
410 the function. */
412 static tree
413 find_vars_r (tree *tp, int *walk_subtrees, void *data)
415 struct function *fn = (struct function *) data;
417 /* If we are reading the lto info back in, we need to rescan the
418 referenced vars. */
419 if (TREE_CODE (*tp) == SSA_NAME)
420 add_referenced_var_1 (SSA_NAME_VAR (*tp), fn);
422 /* If T is a regular variable that the optimizers are interested
423 in, add it to the list of variables. */
424 else if ((TREE_CODE (*tp) == VAR_DECL
425 && !is_global_var (*tp))
426 || TREE_CODE (*tp) == PARM_DECL
427 || TREE_CODE (*tp) == RESULT_DECL)
428 add_referenced_var_1 (*tp, fn);
430 /* Type, _DECL and constant nodes have no interesting children.
431 Ignore them. */
432 else if (IS_TYPE_OR_DECL_P (*tp) || CONSTANT_CLASS_P (*tp))
433 *walk_subtrees = 0;
435 return NULL_TREE;
438 /* Find referenced variables in STMT. */
440 void
441 find_referenced_vars_in (gimple stmt)
443 size_t i;
445 if (gimple_code (stmt) != GIMPLE_PHI)
447 for (i = 0; i < gimple_num_ops (stmt); i++)
448 walk_tree (gimple_op_ptr (stmt, i), find_vars_r, cfun, NULL);
450 else
452 walk_tree (gimple_phi_result_ptr (stmt), find_vars_r, cfun, NULL);
454 for (i = 0; i < gimple_phi_num_args (stmt); i++)
456 tree arg = gimple_phi_arg_def (stmt, i);
457 walk_tree (&arg, find_vars_r, cfun, NULL);
463 /* Lookup UID in the referenced_vars hashtable and return the associated
464 variable. */
466 tree
467 referenced_var_lookup (struct function *fn, unsigned int uid)
469 tree h;
470 struct tree_decl_minimal in;
471 in.uid = uid;
472 h = (tree) htab_find_with_hash (gimple_referenced_vars (fn), &in, uid);
473 return h;
476 /* Check if TO is in the referenced_vars hash table and insert it if not.
477 Return true if it required insertion. */
479 static bool
480 referenced_var_check_and_insert (tree to, struct function *fn)
482 tree *loc;
483 struct tree_decl_minimal in;
484 unsigned int uid = DECL_UID (to);
486 in.uid = uid;
487 loc = (tree *) htab_find_slot_with_hash (gimple_referenced_vars (fn),
488 &in, uid, INSERT);
489 if (*loc)
491 /* DECL_UID has already been entered in the table. Verify that it is
492 the same entry as TO. See PR 27793. */
493 gcc_assert (*loc == to);
494 return false;
497 *loc = to;
498 return true;
501 /* Lookup VAR UID in the default_defs hashtable and return the associated
502 variable. */
504 tree
505 gimple_default_def (struct function *fn, tree var)
507 struct tree_decl_minimal ind;
508 struct tree_ssa_name in;
509 gcc_assert (SSA_VAR_P (var));
510 in.var = (tree)&ind;
511 ind.uid = DECL_UID (var);
512 return (tree) htab_find_with_hash (DEFAULT_DEFS (fn), &in, DECL_UID (var));
515 /* Insert the pair VAR's UID, DEF into the default_defs hashtable. */
517 void
518 set_default_def (tree var, tree def)
520 struct tree_decl_minimal ind;
521 struct tree_ssa_name in;
522 void **loc;
524 gcc_assert (SSA_VAR_P (var));
525 in.var = (tree)&ind;
526 ind.uid = DECL_UID (var);
527 if (!def)
529 loc = htab_find_slot_with_hash (DEFAULT_DEFS (cfun), &in,
530 DECL_UID (var), INSERT);
531 gcc_assert (*loc);
532 htab_remove_elt (DEFAULT_DEFS (cfun), *loc);
533 return;
535 gcc_assert (TREE_CODE (def) == SSA_NAME && SSA_NAME_VAR (def) == var);
536 loc = htab_find_slot_with_hash (DEFAULT_DEFS (cfun), &in,
537 DECL_UID (var), INSERT);
539 /* Default definition might be changed by tail call optimization. */
540 if (*loc)
541 SSA_NAME_IS_DEFAULT_DEF (*(tree *) loc) = false;
542 *(tree *) loc = def;
544 /* Mark DEF as the default definition for VAR. */
545 SSA_NAME_IS_DEFAULT_DEF (def) = true;
548 /* Add VAR to the list of referenced variables if it isn't already there. */
550 bool
551 add_referenced_var_1 (tree var, struct function *fn)
553 gcc_checking_assert (TREE_CODE (var) == VAR_DECL
554 || TREE_CODE (var) == PARM_DECL
555 || TREE_CODE (var) == RESULT_DECL);
557 gcc_checking_assert ((TREE_CODE (var) == VAR_DECL
558 && VAR_DECL_IS_VIRTUAL_OPERAND (var))
559 || !is_global_var (var));
561 /* Insert VAR into the referenced_vars hash table if it isn't present
562 and allocate its var-annotation. */
563 if (referenced_var_check_and_insert (var, fn))
565 gcc_checking_assert (!*DECL_VAR_ANN_PTR (var));
566 *DECL_VAR_ANN_PTR (var) = ggc_alloc_cleared_var_ann_d ();
567 return true;
570 return false;
573 /* Remove VAR from the list of referenced variables and clear its
574 var-annotation. */
576 void
577 remove_referenced_var (tree var)
579 var_ann_t v_ann;
580 struct tree_decl_minimal in;
581 void **loc;
582 unsigned int uid = DECL_UID (var);
584 gcc_checking_assert (TREE_CODE (var) == VAR_DECL
585 || TREE_CODE (var) == PARM_DECL
586 || TREE_CODE (var) == RESULT_DECL);
588 gcc_checking_assert (!is_global_var (var));
590 v_ann = var_ann (var);
591 ggc_free (v_ann);
592 *DECL_VAR_ANN_PTR (var) = NULL;
594 in.uid = uid;
595 loc = htab_find_slot_with_hash (gimple_referenced_vars (cfun), &in, uid,
596 NO_INSERT);
597 htab_clear_slot (gimple_referenced_vars (cfun), loc);
601 /* If EXP is a handled component reference for a structure, return the
602 base variable. The access range is delimited by bit positions *POFFSET and
603 *POFFSET + *PMAX_SIZE. The access size is *PSIZE bits. If either
604 *PSIZE or *PMAX_SIZE is -1, they could not be determined. If *PSIZE
605 and *PMAX_SIZE are equal, the access is non-variable. */
607 tree
608 get_ref_base_and_extent (tree exp, HOST_WIDE_INT *poffset,
609 HOST_WIDE_INT *psize,
610 HOST_WIDE_INT *pmax_size)
612 HOST_WIDE_INT bitsize = -1;
613 HOST_WIDE_INT maxsize = -1;
614 tree size_tree = NULL_TREE;
615 double_int bit_offset = double_int_zero;
616 HOST_WIDE_INT hbit_offset;
617 bool seen_variable_array_ref = false;
618 tree base_type;
620 /* First get the final access size from just the outermost expression. */
621 if (TREE_CODE (exp) == COMPONENT_REF)
622 size_tree = DECL_SIZE (TREE_OPERAND (exp, 1));
623 else if (TREE_CODE (exp) == BIT_FIELD_REF)
624 size_tree = TREE_OPERAND (exp, 1);
625 else if (!VOID_TYPE_P (TREE_TYPE (exp)))
627 enum machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
628 if (mode == BLKmode)
629 size_tree = TYPE_SIZE (TREE_TYPE (exp));
630 else
631 bitsize = GET_MODE_BITSIZE (mode);
633 if (size_tree != NULL_TREE)
635 if (! host_integerp (size_tree, 1))
636 bitsize = -1;
637 else
638 bitsize = TREE_INT_CST_LOW (size_tree);
641 /* Initially, maxsize is the same as the accessed element size.
642 In the following it will only grow (or become -1). */
643 maxsize = bitsize;
645 /* Compute cumulative bit-offset for nested component-refs and array-refs,
646 and find the ultimate containing object. */
647 while (1)
649 base_type = TREE_TYPE (exp);
651 switch (TREE_CODE (exp))
653 case BIT_FIELD_REF:
654 bit_offset
655 = double_int_add (bit_offset,
656 tree_to_double_int (TREE_OPERAND (exp, 2)));
657 break;
659 case COMPONENT_REF:
661 tree field = TREE_OPERAND (exp, 1);
662 tree this_offset = component_ref_field_offset (exp);
664 if (this_offset && TREE_CODE (this_offset) == INTEGER_CST)
666 double_int doffset = tree_to_double_int (this_offset);
667 doffset = double_int_lshift (doffset,
668 BITS_PER_UNIT == 8
669 ? 3 : exact_log2 (BITS_PER_UNIT),
670 HOST_BITS_PER_DOUBLE_INT, true);
671 doffset = double_int_add (doffset,
672 tree_to_double_int
673 (DECL_FIELD_BIT_OFFSET (field)));
674 bit_offset = double_int_add (bit_offset, doffset);
676 /* If we had seen a variable array ref already and we just
677 referenced the last field of a struct or a union member
678 then we have to adjust maxsize by the padding at the end
679 of our field. */
680 if (seen_variable_array_ref && maxsize != -1)
682 tree stype = TREE_TYPE (TREE_OPERAND (exp, 0));
683 tree next = DECL_CHAIN (field);
684 while (next && TREE_CODE (next) != FIELD_DECL)
685 next = DECL_CHAIN (next);
686 if (!next
687 || TREE_CODE (stype) != RECORD_TYPE)
689 tree fsize = DECL_SIZE_UNIT (field);
690 tree ssize = TYPE_SIZE_UNIT (stype);
691 if (host_integerp (fsize, 0)
692 && host_integerp (ssize, 0)
693 && double_int_fits_in_shwi_p (doffset))
694 maxsize += ((TREE_INT_CST_LOW (ssize)
695 - TREE_INT_CST_LOW (fsize))
696 * BITS_PER_UNIT
697 - double_int_to_shwi (doffset));
698 else
699 maxsize = -1;
703 else
705 tree csize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
706 /* We need to adjust maxsize to the whole structure bitsize.
707 But we can subtract any constant offset seen so far,
708 because that would get us out of the structure otherwise. */
709 if (maxsize != -1
710 && csize
711 && host_integerp (csize, 1)
712 && double_int_fits_in_shwi_p (bit_offset))
713 maxsize = TREE_INT_CST_LOW (csize)
714 - double_int_to_shwi (bit_offset);
715 else
716 maxsize = -1;
719 break;
721 case ARRAY_REF:
722 case ARRAY_RANGE_REF:
724 tree index = TREE_OPERAND (exp, 1);
725 tree low_bound, unit_size;
727 /* If the resulting bit-offset is constant, track it. */
728 if (TREE_CODE (index) == INTEGER_CST
729 && (low_bound = array_ref_low_bound (exp),
730 TREE_CODE (low_bound) == INTEGER_CST)
731 && (unit_size = array_ref_element_size (exp),
732 TREE_CODE (unit_size) == INTEGER_CST))
734 double_int doffset
735 = double_int_sext
736 (double_int_sub (TREE_INT_CST (index),
737 TREE_INT_CST (low_bound)),
738 TYPE_PRECISION (TREE_TYPE (index)));
739 doffset = double_int_mul (doffset,
740 tree_to_double_int (unit_size));
741 doffset = double_int_lshift (doffset,
742 BITS_PER_UNIT == 8
743 ? 3 : exact_log2 (BITS_PER_UNIT),
744 HOST_BITS_PER_DOUBLE_INT, true);
745 bit_offset = double_int_add (bit_offset, doffset);
747 /* An array ref with a constant index up in the structure
748 hierarchy will constrain the size of any variable array ref
749 lower in the access hierarchy. */
750 seen_variable_array_ref = false;
752 else
754 tree asize = TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp, 0)));
755 /* We need to adjust maxsize to the whole array bitsize.
756 But we can subtract any constant offset seen so far,
757 because that would get us outside of the array otherwise. */
758 if (maxsize != -1
759 && asize
760 && host_integerp (asize, 1)
761 && double_int_fits_in_shwi_p (bit_offset))
762 maxsize = TREE_INT_CST_LOW (asize)
763 - double_int_to_shwi (bit_offset);
764 else
765 maxsize = -1;
767 /* Remember that we have seen an array ref with a variable
768 index. */
769 seen_variable_array_ref = true;
772 break;
774 case REALPART_EXPR:
775 break;
777 case IMAGPART_EXPR:
778 bit_offset
779 = double_int_add (bit_offset, uhwi_to_double_int (bitsize));
780 break;
782 case VIEW_CONVERT_EXPR:
783 break;
785 case MEM_REF:
786 /* Hand back the decl for MEM[&decl, off]. */
787 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
789 if (integer_zerop (TREE_OPERAND (exp, 1)))
790 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
791 else
793 double_int off = mem_ref_offset (exp);
794 off = double_int_lshift (off,
795 BITS_PER_UNIT == 8
796 ? 3 : exact_log2 (BITS_PER_UNIT),
797 HOST_BITS_PER_DOUBLE_INT, true);
798 off = double_int_add (off, bit_offset);
799 if (double_int_fits_in_shwi_p (off))
801 bit_offset = off;
802 exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
806 goto done;
808 case TARGET_MEM_REF:
809 /* Hand back the decl for MEM[&decl, off]. */
810 if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR)
812 /* Via the variable index or index2 we can reach the
813 whole object. */
814 if (TMR_INDEX (exp) || TMR_INDEX2 (exp))
816 exp = TREE_OPERAND (TMR_BASE (exp), 0);
817 bit_offset = double_int_zero;
818 maxsize = -1;
819 goto done;
821 if (integer_zerop (TMR_OFFSET (exp)))
822 exp = TREE_OPERAND (TMR_BASE (exp), 0);
823 else
825 double_int off = mem_ref_offset (exp);
826 off = double_int_lshift (off,
827 BITS_PER_UNIT == 8
828 ? 3 : exact_log2 (BITS_PER_UNIT),
829 HOST_BITS_PER_DOUBLE_INT, true);
830 off = double_int_add (off, bit_offset);
831 if (double_int_fits_in_shwi_p (off))
833 bit_offset = off;
834 exp = TREE_OPERAND (TMR_BASE (exp), 0);
838 goto done;
840 default:
841 goto done;
844 exp = TREE_OPERAND (exp, 0);
846 done:
848 if (!double_int_fits_in_shwi_p (bit_offset))
850 *poffset = 0;
851 *psize = bitsize;
852 *pmax_size = -1;
854 return exp;
857 hbit_offset = double_int_to_shwi (bit_offset);
859 /* We need to deal with variable arrays ending structures such as
860 struct { int length; int a[1]; } x; x.a[d]
861 struct { struct { int a; int b; } a[1]; } x; x.a[d].a
862 struct { struct { int a[1]; } a[1]; } x; x.a[0][d], x.a[d][0]
863 struct { int len; union { int a[1]; struct X x; } u; } x; x.u.a[d]
864 where we do not know maxsize for variable index accesses to
865 the array. The simplest way to conservatively deal with this
866 is to punt in the case that offset + maxsize reaches the
867 base type boundary. This needs to include possible trailing padding
868 that is there for alignment purposes. */
870 if (seen_variable_array_ref
871 && maxsize != -1
872 && (!host_integerp (TYPE_SIZE (base_type), 1)
873 || (hbit_offset + maxsize
874 == (signed) TREE_INT_CST_LOW (TYPE_SIZE (base_type)))))
875 maxsize = -1;
877 /* In case of a decl or constant base object we can do better. */
879 if (DECL_P (exp))
881 /* If maxsize is unknown adjust it according to the size of the
882 base decl. */
883 if (maxsize == -1
884 && host_integerp (DECL_SIZE (exp), 1))
885 maxsize = TREE_INT_CST_LOW (DECL_SIZE (exp)) - hbit_offset;
887 else if (CONSTANT_CLASS_P (exp))
889 /* If maxsize is unknown adjust it according to the size of the
890 base type constant. */
891 if (maxsize == -1
892 && host_integerp (TYPE_SIZE (TREE_TYPE (exp)), 1))
893 maxsize = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp))) - hbit_offset;
896 /* ??? Due to negative offsets in ARRAY_REF we can end up with
897 negative bit_offset here. We might want to store a zero offset
898 in this case. */
899 *poffset = hbit_offset;
900 *psize = bitsize;
901 *pmax_size = maxsize;
903 return exp;
906 /* Returns the base object and a constant BITS_PER_UNIT offset in *POFFSET that
907 denotes the starting address of the memory access EXP.
908 Returns NULL_TREE if the offset is not constant or any component
909 is not BITS_PER_UNIT-aligned. */
911 tree
912 get_addr_base_and_unit_offset (tree exp, HOST_WIDE_INT *poffset)
914 return get_addr_base_and_unit_offset_1 (exp, poffset, NULL);
917 /* Returns true if STMT references an SSA_NAME that has
918 SSA_NAME_OCCURS_IN_ABNORMAL_PHI set, otherwise false. */
920 bool
921 stmt_references_abnormal_ssa_name (gimple stmt)
923 ssa_op_iter oi;
924 use_operand_p use_p;
926 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE)
928 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (use_p)))
929 return true;
932 return false;
935 /* Pair of tree and a sorting index, for dump_enumerated_decls. */
936 struct GTY(()) numbered_tree_d
938 tree t;
939 int num;
941 typedef struct numbered_tree_d numbered_tree;
943 DEF_VEC_O (numbered_tree);
944 DEF_VEC_ALLOC_O (numbered_tree, heap);
946 /* Compare two declarations references by their DECL_UID / sequence number.
947 Called via qsort. */
949 static int
950 compare_decls_by_uid (const void *pa, const void *pb)
952 const numbered_tree *nt_a = ((const numbered_tree *)pa);
953 const numbered_tree *nt_b = ((const numbered_tree *)pb);
955 if (DECL_UID (nt_a->t) != DECL_UID (nt_b->t))
956 return DECL_UID (nt_a->t) - DECL_UID (nt_b->t);
957 return nt_a->num - nt_b->num;
960 /* Called via walk_gimple_stmt / walk_gimple_op by dump_enumerated_decls. */
961 static tree
962 dump_enumerated_decls_push (tree *tp, int *walk_subtrees, void *data)
964 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
965 VEC (numbered_tree, heap) **list = (VEC (numbered_tree, heap) **) &wi->info;
966 numbered_tree nt;
968 if (!DECL_P (*tp))
969 return NULL_TREE;
970 nt.t = *tp;
971 nt.num = VEC_length (numbered_tree, *list);
972 VEC_safe_push (numbered_tree, heap, *list, &nt);
973 *walk_subtrees = 0;
974 return NULL_TREE;
977 /* Find all the declarations used by the current function, sort them by uid,
978 and emit the sorted list. Each declaration is tagged with a sequence
979 number indicating when it was found during statement / tree walking,
980 so that TDF_NOUID comparisons of anonymous declarations are still
981 meaningful. Where a declaration was encountered more than once, we
982 emit only the sequence number of the first encounter.
983 FILE is the dump file where to output the list and FLAGS is as in
984 print_generic_expr. */
985 void
986 dump_enumerated_decls (FILE *file, int flags)
988 basic_block bb;
989 struct walk_stmt_info wi;
990 VEC (numbered_tree, heap) *decl_list = VEC_alloc (numbered_tree, heap, 40);
992 memset (&wi, '\0', sizeof (wi));
993 wi.info = (void*) decl_list;
994 FOR_EACH_BB (bb)
996 gimple_stmt_iterator gsi;
998 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
999 if (!is_gimple_debug (gsi_stmt (gsi)))
1000 walk_gimple_stmt (&gsi, NULL, dump_enumerated_decls_push, &wi);
1002 decl_list = (VEC (numbered_tree, heap) *) wi.info;
1003 VEC_qsort (numbered_tree, decl_list, compare_decls_by_uid);
1004 if (VEC_length (numbered_tree, decl_list))
1006 unsigned ix;
1007 numbered_tree *ntp;
1008 tree last = NULL_TREE;
1010 fprintf (file, "Declarations used by %s, sorted by DECL_UID:\n",
1011 current_function_name ());
1012 FOR_EACH_VEC_ELT (numbered_tree, decl_list, ix, ntp)
1014 if (ntp->t == last)
1015 continue;
1016 fprintf (file, "%d: ", ntp->num);
1017 print_generic_decl (file, ntp->t, flags);
1018 fprintf (file, "\n");
1019 last = ntp->t;
1022 VEC_free (numbered_tree, heap, decl_list);