1 /* Data flow functions for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007 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)
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/>. */
23 #include "coretypes.h"
26 #include "pointer-set.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
36 #include "langhooks.h"
39 #include "diagnostic.h"
40 #include "tree-dump.h"
41 #include "tree-gimple.h"
42 #include "tree-flow.h"
43 #include "tree-inline.h"
44 #include "tree-pass.h"
49 /* Build and maintain data flow information for trees. */
51 /* Counters used to display DFA and SSA statistics. */
66 /* Local functions. */
67 static void collect_dfa_stats (struct dfa_stats_d
*);
68 static tree
collect_dfa_stats_r (tree
*, int *, void *);
69 static tree
find_vars_r (tree
*, int *, void *);
72 /*---------------------------------------------------------------------------
73 Dataflow analysis (DFA) routines
74 ---------------------------------------------------------------------------*/
75 /* Find all the variables referenced in the function. This function
76 builds the global arrays REFERENCED_VARS and CALL_CLOBBERED_VARS.
78 Note that this function does not look for statement operands, it simply
79 determines what variables are referenced in the program and detects
80 various attributes for each variable used by alias analysis and the
84 find_referenced_vars (void)
87 block_stmt_iterator si
;
90 for (si
= bsi_start (bb
); !bsi_end_p (si
); bsi_next (&si
))
92 tree
*stmt_p
= bsi_stmt_ptr (si
);
93 walk_tree (stmt_p
, find_vars_r
, NULL
, NULL
);
99 struct gimple_opt_pass pass_referenced_vars
=
105 find_referenced_vars
, /* execute */
108 0, /* static_pass_number */
109 TV_FIND_REFERENCED_VARS
, /* tv_id */
110 PROP_gimple_leh
| PROP_cfg
, /* properties_required */
111 PROP_referenced_vars
, /* properties_provided */
112 0, /* properties_destroyed */
113 0, /* todo_flags_start */
114 0 /* todo_flags_finish */
119 /*---------------------------------------------------------------------------
121 ---------------------------------------------------------------------------*/
122 /* Create a new annotation for a _DECL node T. */
125 create_var_ann (tree t
)
128 struct static_var_ann_d
*sann
= NULL
;
131 gcc_assert (DECL_P (t
));
132 gcc_assert (!t
->base
.ann
|| t
->base
.ann
->common
.type
== VAR_ANN
);
134 if (!MTAG_P (t
) && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
136 sann
= GGC_CNEW (struct static_var_ann_d
);
140 ann
= GGC_CNEW (struct var_ann_d
);
142 ann
->common
.type
= VAR_ANN
;
144 if (!MTAG_P (t
) && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
147 sann
->uid
= DECL_UID (t
);
148 slot
= htab_find_slot_with_hash (gimple_var_anns (cfun
),
149 t
, DECL_UID (t
), INSERT
);
154 t
->base
.ann
= (tree_ann_t
) ann
;
159 /* Create a new annotation for a FUNCTION_DECL node T. */
162 create_function_ann (tree t
)
167 gcc_assert (TREE_CODE (t
) == FUNCTION_DECL
);
168 gcc_assert (!t
->base
.ann
|| t
->base
.ann
->common
.type
== FUNCTION_ANN
);
170 ann
= ggc_alloc (sizeof (*ann
));
171 memset ((void *) ann
, 0, sizeof (*ann
));
173 ann
->common
.type
= FUNCTION_ANN
;
175 t
->base
.ann
= (tree_ann_t
) ann
;
180 /* Create a new annotation for a statement node T. */
183 create_stmt_ann (tree t
)
187 gcc_assert (is_gimple_stmt (t
));
188 gcc_assert (!t
->base
.ann
|| t
->base
.ann
->common
.type
== STMT_ANN
);
190 ann
= GGC_CNEW (struct stmt_ann_d
);
192 ann
->common
.type
= STMT_ANN
;
194 /* Since we just created the annotation, mark the statement modified. */
195 ann
->modified
= true;
197 t
->base
.ann
= (tree_ann_t
) ann
;
202 /* Create a new annotation for a tree T. */
205 create_tree_common_ann (tree t
)
207 tree_ann_common_t ann
;
210 gcc_assert (!t
->base
.ann
|| t
->base
.ann
->common
.type
== TREE_ANN_COMMON
);
212 ann
= GGC_CNEW (struct tree_ann_common_d
);
214 ann
->type
= TREE_ANN_COMMON
;
215 t
->base
.ann
= (tree_ann_t
) ann
;
220 /* Build a temporary. Make sure and register it to be renamed. */
223 make_rename_temp (tree type
, const char *prefix
)
225 tree t
= create_tmp_var (type
, prefix
);
227 if (TREE_CODE (TREE_TYPE (t
)) == COMPLEX_TYPE
228 || TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
229 DECL_GIMPLE_REG_P (t
) = 1;
231 if (gimple_referenced_vars (cfun
))
233 add_referenced_var (t
);
234 mark_sym_for_renaming (t
);
242 /*---------------------------------------------------------------------------
244 ---------------------------------------------------------------------------*/
245 /* Dump the list of all the referenced variables in the current function to
249 dump_referenced_vars (FILE *file
)
252 referenced_var_iterator rvi
;
254 fprintf (file
, "\nReferenced variables in %s: %u\n\n",
255 get_name (current_function_decl
), (unsigned) num_referenced_vars
);
257 FOR_EACH_REFERENCED_VAR (var
, rvi
)
259 fprintf (file
, "Variable: ");
260 dump_variable (file
, var
);
261 fprintf (file
, "\n");
266 /* Dump the list of all the referenced variables to stderr. */
269 debug_referenced_vars (void)
271 dump_referenced_vars (stderr
);
275 /* Dump sub-variables for VAR to FILE. */
278 dump_subvars_for (FILE *file
, tree var
)
280 subvar_t sv
= get_subvars_for_var (var
);
287 fprintf (file
, "{ ");
289 for (i
= 0; VEC_iterate (tree
, sv
, i
, subvar
); ++i
)
291 print_generic_expr (file
, subvar
, dump_flags
);
292 fprintf (file
, "@" HOST_WIDE_INT_PRINT_UNSIGNED
, SFT_OFFSET (subvar
));
293 if (SFT_BASE_FOR_COMPONENTS_P (subvar
))
294 fprintf (file
, "[B]");
302 /* Dumb sub-variables for VAR to stderr. */
305 debug_subvars_for (tree var
)
307 dump_subvars_for (stderr
, var
);
311 /* Dump variable VAR and its may-aliases to FILE. */
314 dump_variable (FILE *file
, tree var
)
318 if (TREE_CODE (var
) == SSA_NAME
)
320 if (POINTER_TYPE_P (TREE_TYPE (var
)))
321 dump_points_to_info_for (file
, var
);
322 var
= SSA_NAME_VAR (var
);
325 if (var
== NULL_TREE
)
327 fprintf (file
, "<nil>");
331 print_generic_expr (file
, var
, dump_flags
);
335 fprintf (file
, ", UID D.%u", (unsigned) DECL_UID (var
));
337 fprintf (file
, ", ");
338 print_generic_expr (file
, TREE_TYPE (var
), dump_flags
);
340 if (ann
&& ann
->symbol_mem_tag
)
342 fprintf (file
, ", symbol memory tag: ");
343 print_generic_expr (file
, ann
->symbol_mem_tag
, dump_flags
);
346 if (TREE_ADDRESSABLE (var
))
347 fprintf (file
, ", is addressable");
349 if (is_global_var (var
))
350 fprintf (file
, ", is global");
352 if (TREE_THIS_VOLATILE (var
))
353 fprintf (file
, ", is volatile");
355 dump_mem_sym_stats_for_var (file
, var
);
357 if (is_call_clobbered (var
))
360 var_ann_t va
= var_ann (var
);
361 unsigned int escape_mask
= va
->escape_mask
;
363 fprintf (file
, ", call clobbered");
364 fprintf (file
, " (");
365 if (escape_mask
& ESCAPE_STORED_IN_GLOBAL
)
366 { fprintf (file
, "%sstored in global", s
); s
= ", "; }
367 if (escape_mask
& ESCAPE_TO_ASM
)
368 { fprintf (file
, "%sgoes through ASM", s
); s
= ", "; }
369 if (escape_mask
& ESCAPE_TO_CALL
)
370 { fprintf (file
, "%spassed to call", s
); s
= ", "; }
371 if (escape_mask
& ESCAPE_BAD_CAST
)
372 { fprintf (file
, "%sbad cast", s
); s
= ", "; }
373 if (escape_mask
& ESCAPE_TO_RETURN
)
374 { fprintf (file
, "%sreturned from func", s
); s
= ", "; }
375 if (escape_mask
& ESCAPE_TO_PURE_CONST
)
376 { fprintf (file
, "%spassed to pure/const", s
); s
= ", "; }
377 if (escape_mask
& ESCAPE_IS_GLOBAL
)
378 { fprintf (file
, "%sis global var", s
); s
= ", "; }
379 if (escape_mask
& ESCAPE_IS_PARM
)
380 { fprintf (file
, "%sis incoming pointer", s
); s
= ", "; }
381 if (escape_mask
& ESCAPE_UNKNOWN
)
382 { fprintf (file
, "%sunknown escape", s
); s
= ", "; }
386 if (ann
->noalias_state
== NO_ALIAS
)
387 fprintf (file
, ", NO_ALIAS (does not alias other NO_ALIAS symbols)");
388 else if (ann
->noalias_state
== NO_ALIAS_GLOBAL
)
389 fprintf (file
, ", NO_ALIAS_GLOBAL (does not alias other NO_ALIAS symbols"
390 " and global vars)");
391 else if (ann
->noalias_state
== NO_ALIAS_ANYTHING
)
392 fprintf (file
, ", NO_ALIAS_ANYTHING (does not alias any other symbols)");
394 if (gimple_default_def (cfun
, var
))
396 fprintf (file
, ", default def: ");
397 print_generic_expr (file
, gimple_default_def (cfun
, var
), dump_flags
);
400 if (MTAG_P (var
) && may_aliases (var
))
402 fprintf (file
, ", may aliases: ");
403 dump_may_aliases_for (file
, var
);
406 if (get_subvars_for_var (var
))
408 fprintf (file
, ", sub-vars: ");
409 dump_subvars_for (file
, var
);
412 if (!is_gimple_reg (var
))
414 if (memory_partition (var
))
416 fprintf (file
, ", belongs to partition: ");
417 print_generic_expr (file
, memory_partition (var
), dump_flags
);
420 if (TREE_CODE (var
) == MEMORY_PARTITION_TAG
)
422 fprintf (file
, ", partition symbols: ");
423 dump_decl_set (file
, MPT_SYMBOLS (var
));
426 if (TREE_CODE (var
) == STRUCT_FIELD_TAG
)
428 fprintf (file
, ", offset: " HOST_WIDE_INT_PRINT_UNSIGNED
,
430 fprintf (file
, ", base for components: %s",
431 SFT_BASE_FOR_COMPONENTS_P (var
) ? "NO" : "YES");
432 fprintf (file
, ", partitionable: %s",
433 SFT_UNPARTITIONABLE_P (var
) ? "NO" : "YES");
437 fprintf (file
, "\n");
441 /* Dump variable VAR and its may-aliases to stderr. */
444 debug_variable (tree var
)
446 dump_variable (stderr
, var
);
450 /* Dump various DFA statistics to FILE. */
453 dump_dfa_stats (FILE *file
)
455 struct dfa_stats_d dfa_stats
;
457 unsigned long size
, total
= 0;
458 const char * const fmt_str
= "%-30s%-13s%12s\n";
459 const char * const fmt_str_1
= "%-30s%13lu%11lu%c\n";
460 const char * const fmt_str_3
= "%-43s%11lu%c\n";
462 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
464 collect_dfa_stats (&dfa_stats
);
466 fprintf (file
, "\nDFA Statistics for %s\n\n", funcname
);
468 fprintf (file
, "---------------------------------------------------------\n");
469 fprintf (file
, fmt_str
, "", " Number of ", "Memory");
470 fprintf (file
, fmt_str
, "", " instances ", "used ");
471 fprintf (file
, "---------------------------------------------------------\n");
473 size
= num_referenced_vars
* sizeof (tree
);
475 fprintf (file
, fmt_str_1
, "Referenced variables", (unsigned long)num_referenced_vars
,
476 SCALE (size
), LABEL (size
));
478 size
= dfa_stats
.num_stmt_anns
* sizeof (struct stmt_ann_d
);
480 fprintf (file
, fmt_str_1
, "Statements annotated", dfa_stats
.num_stmt_anns
,
481 SCALE (size
), LABEL (size
));
483 size
= dfa_stats
.num_var_anns
* sizeof (struct var_ann_d
);
485 fprintf (file
, fmt_str_1
, "Variables annotated", dfa_stats
.num_var_anns
,
486 SCALE (size
), LABEL (size
));
488 size
= dfa_stats
.num_uses
* sizeof (tree
*);
490 fprintf (file
, fmt_str_1
, "USE operands", dfa_stats
.num_uses
,
491 SCALE (size
), LABEL (size
));
493 size
= dfa_stats
.num_defs
* sizeof (tree
*);
495 fprintf (file
, fmt_str_1
, "DEF operands", dfa_stats
.num_defs
,
496 SCALE (size
), LABEL (size
));
498 size
= dfa_stats
.num_vuses
* sizeof (tree
*);
500 fprintf (file
, fmt_str_1
, "VUSE operands", dfa_stats
.num_vuses
,
501 SCALE (size
), LABEL (size
));
503 size
= dfa_stats
.num_vdefs
* sizeof (tree
*);
505 fprintf (file
, fmt_str_1
, "VDEF operands", dfa_stats
.num_vdefs
,
506 SCALE (size
), LABEL (size
));
508 size
= dfa_stats
.num_phis
* sizeof (struct tree_phi_node
);
510 fprintf (file
, fmt_str_1
, "PHI nodes", dfa_stats
.num_phis
,
511 SCALE (size
), LABEL (size
));
513 size
= dfa_stats
.num_phi_args
* sizeof (struct phi_arg_d
);
515 fprintf (file
, fmt_str_1
, "PHI arguments", dfa_stats
.num_phi_args
,
516 SCALE (size
), LABEL (size
));
518 fprintf (file
, "---------------------------------------------------------\n");
519 fprintf (file
, fmt_str_3
, "Total memory used by DFA/SSA data", SCALE (total
),
521 fprintf (file
, "---------------------------------------------------------\n");
522 fprintf (file
, "\n");
524 if (dfa_stats
.num_phis
)
525 fprintf (file
, "Average number of arguments per PHI node: %.1f (max: %d)\n",
526 (float) dfa_stats
.num_phi_args
/ (float) dfa_stats
.num_phis
,
527 dfa_stats
.max_num_phi_args
);
529 fprintf (file
, "\n");
533 /* Dump DFA statistics on stderr. */
536 debug_dfa_stats (void)
538 dump_dfa_stats (stderr
);
542 /* Collect DFA statistics and store them in the structure pointed to by
546 collect_dfa_stats (struct dfa_stats_d
*dfa_stats_p
)
548 struct pointer_set_t
*pset
;
550 block_stmt_iterator i
;
552 gcc_assert (dfa_stats_p
);
554 memset ((void *)dfa_stats_p
, 0, sizeof (struct dfa_stats_d
));
556 /* Walk all the trees in the function counting references. Start at
557 basic block NUM_FIXED_BLOCKS, but don't stop at block boundaries. */
558 pset
= pointer_set_create ();
560 for (i
= bsi_start (BASIC_BLOCK (NUM_FIXED_BLOCKS
));
561 !bsi_end_p (i
); bsi_next (&i
))
562 walk_tree (bsi_stmt_ptr (i
), collect_dfa_stats_r
, (void *) dfa_stats_p
,
565 pointer_set_destroy (pset
);
570 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
572 dfa_stats_p
->num_phis
++;
573 dfa_stats_p
->num_phi_args
+= PHI_NUM_ARGS (phi
);
574 if (PHI_NUM_ARGS (phi
) > dfa_stats_p
->max_num_phi_args
)
575 dfa_stats_p
->max_num_phi_args
= PHI_NUM_ARGS (phi
);
581 /* Callback for walk_tree to collect DFA statistics for a tree and its
585 collect_dfa_stats_r (tree
*tp
, int *walk_subtrees ATTRIBUTE_UNUSED
,
589 struct dfa_stats_d
*dfa_stats_p
= (struct dfa_stats_d
*)data
;
593 switch (ann_type (t
->base
.ann
))
597 dfa_stats_p
->num_stmt_anns
++;
598 dfa_stats_p
->num_defs
+= NUM_SSA_OPERANDS (t
, SSA_OP_DEF
);
599 dfa_stats_p
->num_uses
+= NUM_SSA_OPERANDS (t
, SSA_OP_USE
);
600 dfa_stats_p
->num_vdefs
+= NUM_SSA_OPERANDS (t
, SSA_OP_VDEF
);
601 dfa_stats_p
->num_vuses
+= NUM_SSA_OPERANDS (t
, SSA_OP_VUSE
);
606 dfa_stats_p
->num_var_anns
++;
618 /*---------------------------------------------------------------------------
619 Miscellaneous helpers
620 ---------------------------------------------------------------------------*/
621 /* Callback for walk_tree. Used to collect variables referenced in
625 find_vars_r (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
627 /* If T is a regular variable that the optimizers are interested
628 in, add it to the list of variables. */
630 add_referenced_var (*tp
);
632 /* Type, _DECL and constant nodes have no interesting children.
634 else if (IS_TYPE_OR_DECL_P (*tp
) || CONSTANT_CLASS_P (*tp
))
640 /* Lookup UID in the referenced_vars hashtable and return the associated
644 referenced_var_lookup (unsigned int uid
)
647 struct tree_decl_minimal in
;
649 h
= (tree
) htab_find_with_hash (gimple_referenced_vars (cfun
), &in
, uid
);
650 gcc_assert (h
|| uid
== 0);
654 /* Check if TO is in the referenced_vars hash table and insert it if not.
655 Return true if it required insertion. */
658 referenced_var_check_and_insert (tree to
)
661 struct tree_decl_minimal in
;
662 unsigned int uid
= DECL_UID (to
);
665 h
= (tree
) htab_find_with_hash (gimple_referenced_vars (cfun
), &in
, uid
);
668 /* DECL_UID has already been entered in the table. Verify that it is
669 the same entry as TO. See PR 27793. */
670 gcc_assert (h
== to
);
674 loc
= (tree
*) htab_find_slot_with_hash (gimple_referenced_vars (cfun
),
680 /* Lookup VAR UID in the default_defs hashtable and return the associated
684 gimple_default_def (struct function
*fn
, tree var
)
686 struct tree_decl_minimal ind
;
687 struct tree_ssa_name in
;
688 gcc_assert (SSA_VAR_P (var
));
690 ind
.uid
= DECL_UID (var
);
691 return (tree
) htab_find_with_hash (DEFAULT_DEFS (fn
), &in
, DECL_UID (var
));
694 /* Insert the pair VAR's UID, DEF into the default_defs hashtable. */
697 set_default_def (tree var
, tree def
)
699 struct tree_decl_minimal ind
;
700 struct tree_ssa_name in
;
703 gcc_assert (SSA_VAR_P (var
));
705 ind
.uid
= DECL_UID (var
);
708 loc
= htab_find_slot_with_hash (DEFAULT_DEFS (cfun
), &in
,
709 DECL_UID (var
), INSERT
);
711 htab_remove_elt (DEFAULT_DEFS (cfun
), *loc
);
714 gcc_assert (TREE_CODE (def
) == SSA_NAME
&& SSA_NAME_VAR (def
) == var
);
715 loc
= htab_find_slot_with_hash (DEFAULT_DEFS (cfun
), &in
,
716 DECL_UID (var
), INSERT
);
718 /* Default definition might be changed by tail call optimization. */
720 SSA_NAME_IS_DEFAULT_DEF (*(tree
*) loc
) = false;
723 /* Mark DEF as the default definition for VAR. */
724 SSA_NAME_IS_DEFAULT_DEF (def
) = true;
727 /* Add VAR to the list of referenced variables if it isn't already there. */
730 add_referenced_var (tree var
)
734 v_ann
= get_var_ann (var
);
735 gcc_assert (DECL_P (var
));
737 /* Insert VAR into the referenced_vars has table if it isn't present. */
738 if (referenced_var_check_and_insert (var
))
740 /* This is the first time we found this variable, annotate it with
741 attributes that are intrinsic to the variable. */
743 /* Tag's don't have DECL_INITIAL. */
747 /* Scan DECL_INITIAL for pointer variables as they may contain
748 address arithmetic referencing the address of other
750 Even non-constant intializers need to be walked, because
751 IPA passes might prove that their are invariant later on. */
752 if (DECL_INITIAL (var
)
753 /* Initializers of external variables are not useful to the
755 && !DECL_EXTERNAL (var
))
756 walk_tree (&DECL_INITIAL (var
), find_vars_r
, NULL
, 0);
760 /* Remove VAR from the list. */
763 remove_referenced_var (tree var
)
766 struct tree_decl_minimal in
;
768 unsigned int uid
= DECL_UID (var
);
771 /* If we remove a var, we should also remove its subvars, as we kill
772 their parent var and its annotation. */
773 if (var_can_have_subvars (var
)
774 && (sv
= get_subvars_for_var (var
)))
778 for (i
= 0; VEC_iterate (tree
, sv
, i
, subvar
); ++i
)
779 remove_referenced_var (subvar
);
782 clear_call_clobbered (var
);
783 if ((v_ann
= var_ann (var
)))
785 var
->base
.ann
= NULL
;
786 gcc_assert (DECL_P (var
));
788 loc
= htab_find_slot_with_hash (gimple_referenced_vars (cfun
), &in
, uid
,
790 htab_clear_slot (gimple_referenced_vars (cfun
), loc
);
794 /* Return the virtual variable associated to the non-scalar variable VAR. */
797 get_virtual_var (tree var
)
801 if (TREE_CODE (var
) == SSA_NAME
)
802 var
= SSA_NAME_VAR (var
);
804 while (TREE_CODE (var
) == REALPART_EXPR
|| TREE_CODE (var
) == IMAGPART_EXPR
805 || handled_component_p (var
))
806 var
= TREE_OPERAND (var
, 0);
808 /* Treating GIMPLE registers as virtual variables makes no sense.
809 Also complain if we couldn't extract a _DECL out of the original
811 gcc_assert (SSA_VAR_P (var
));
812 gcc_assert (!is_gimple_reg (var
));
817 /* Mark all the naked symbols in STMT for SSA renaming.
819 NOTE: This function should only be used for brand new statements.
820 If the caller is modifying an existing statement, it should use the
821 combination push_stmt_changes/pop_stmt_changes. */
824 mark_symbols_for_renaming (tree stmt
)
831 /* Mark all the operands for renaming. */
832 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_ALL_OPERANDS
)
834 mark_sym_for_renaming (op
);
838 /* Find all variables within the gimplified statement that were not previously
839 visible to the function and add them to the referenced variables list. */
842 find_new_referenced_vars_1 (tree
*tp
, int *walk_subtrees
,
843 void *data ATTRIBUTE_UNUSED
)
847 if (TREE_CODE (t
) == VAR_DECL
&& !var_ann (t
))
849 add_referenced_var (t
);
850 mark_sym_for_renaming (t
);
853 if (IS_TYPE_OR_DECL_P (t
))
860 find_new_referenced_vars (tree
*stmt_p
)
862 walk_tree (stmt_p
, find_new_referenced_vars_1
, NULL
, NULL
);
866 /* If EXP is a handled component reference for a structure, return the
867 base variable. The access range is delimited by bit positions *POFFSET and
868 *POFFSET + *PMAX_SIZE. The access size is *PSIZE bits. If either
869 *PSIZE or *PMAX_SIZE is -1, they could not be determined. If *PSIZE
870 and *PMAX_SIZE are equal, the access is non-variable. */
873 get_ref_base_and_extent (tree exp
, HOST_WIDE_INT
*poffset
,
874 HOST_WIDE_INT
*psize
,
875 HOST_WIDE_INT
*pmax_size
)
877 HOST_WIDE_INT bitsize
= -1;
878 HOST_WIDE_INT maxsize
= -1;
879 tree size_tree
= NULL_TREE
;
880 HOST_WIDE_INT bit_offset
= 0;
881 bool seen_variable_array_ref
= false;
883 gcc_assert (!SSA_VAR_P (exp
));
885 /* First get the final access size from just the outermost expression. */
886 if (TREE_CODE (exp
) == COMPONENT_REF
)
887 size_tree
= DECL_SIZE (TREE_OPERAND (exp
, 1));
888 else if (TREE_CODE (exp
) == BIT_FIELD_REF
)
889 size_tree
= TREE_OPERAND (exp
, 1);
892 enum machine_mode mode
= TYPE_MODE (TREE_TYPE (exp
));
894 size_tree
= TYPE_SIZE (TREE_TYPE (exp
));
896 bitsize
= GET_MODE_BITSIZE (mode
);
898 if (size_tree
!= NULL_TREE
)
900 if (! host_integerp (size_tree
, 1))
903 bitsize
= TREE_INT_CST_LOW (size_tree
);
906 /* Initially, maxsize is the same as the accessed element size.
907 In the following it will only grow (or become -1). */
910 /* Compute cumulative bit-offset for nested component-refs and array-refs,
911 and find the ultimate containing object. */
914 switch (TREE_CODE (exp
))
917 bit_offset
+= tree_low_cst (TREE_OPERAND (exp
, 2), 0);
922 tree field
= TREE_OPERAND (exp
, 1);
923 tree this_offset
= component_ref_field_offset (exp
);
925 if (this_offset
&& TREE_CODE (this_offset
) == INTEGER_CST
)
927 HOST_WIDE_INT hthis_offset
= tree_low_cst (this_offset
, 0);
929 hthis_offset
*= BITS_PER_UNIT
;
930 bit_offset
+= hthis_offset
;
931 bit_offset
+= tree_low_cst (DECL_FIELD_BIT_OFFSET (field
), 0);
935 tree csize
= TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp
, 0)));
936 /* We need to adjust maxsize to the whole structure bitsize.
937 But we can subtract any constant offset seen sofar,
938 because that would get us out of the structure otherwise. */
939 if (maxsize
!= -1 && csize
&& host_integerp (csize
, 1))
940 maxsize
= TREE_INT_CST_LOW (csize
) - bit_offset
;
948 case ARRAY_RANGE_REF
:
950 tree index
= TREE_OPERAND (exp
, 1);
951 tree low_bound
= array_ref_low_bound (exp
);
952 tree unit_size
= array_ref_element_size (exp
);
954 /* If the resulting bit-offset is constant, track it. */
955 if (host_integerp (index
, 0)
956 && host_integerp (low_bound
, 0)
957 && host_integerp (unit_size
, 1))
959 HOST_WIDE_INT hindex
= tree_low_cst (index
, 0);
961 hindex
-= tree_low_cst (low_bound
, 0);
962 hindex
*= tree_low_cst (unit_size
, 1);
963 hindex
*= BITS_PER_UNIT
;
964 bit_offset
+= hindex
;
966 /* An array ref with a constant index up in the structure
967 hierarchy will constrain the size of any variable array ref
968 lower in the access hierarchy. */
969 seen_variable_array_ref
= false;
973 tree asize
= TYPE_SIZE (TREE_TYPE (TREE_OPERAND (exp
, 0)));
974 /* We need to adjust maxsize to the whole array bitsize.
975 But we can subtract any constant offset seen sofar,
976 because that would get us outside of the array otherwise. */
977 if (maxsize
!= -1 && asize
&& host_integerp (asize
, 1))
978 maxsize
= TREE_INT_CST_LOW (asize
) - bit_offset
;
982 /* Remember that we have seen an array ref with a variable
984 seen_variable_array_ref
= true;
993 bit_offset
+= bitsize
;
996 case VIEW_CONVERT_EXPR
:
997 /* ??? We probably should give up here and bail out. */
1004 exp
= TREE_OPERAND (exp
, 0);
1008 /* We need to deal with variable arrays ending structures such as
1009 struct { int length; int a[1]; } x; x.a[d]
1010 struct { struct { int a; int b; } a[1]; } x; x.a[d].a
1011 struct { struct { int a[1]; } a[1]; } x; x.a[0][d], x.a[d][0]
1012 where we do not know maxsize for variable index accesses to
1013 the array. The simplest way to conservatively deal with this
1014 is to punt in the case that offset + maxsize reaches the
1015 base type boundary. */
1016 if (seen_variable_array_ref
1018 && host_integerp (TYPE_SIZE (TREE_TYPE (exp
)), 1)
1019 && bit_offset
+ maxsize
1020 == (signed)TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp
))))
1023 /* ??? Due to negative offsets in ARRAY_REF we can end up with
1024 negative bit_offset here. We might want to store a zero offset
1026 *poffset
= bit_offset
;
1028 *pmax_size
= maxsize
;
1033 /* Returns true if STMT references an SSA_NAME that has
1034 SSA_NAME_OCCURS_IN_ABNORMAL_PHI set, otherwise false. */
1037 stmt_references_abnormal_ssa_name (tree stmt
)
1040 use_operand_p use_p
;
1042 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, oi
, SSA_OP_USE
)
1044 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (use_p
)))
1051 /* Return true, if the two memory references REF1 and REF2 may alias. */
1054 refs_may_alias_p (tree ref1
, tree ref2
)
1057 HOST_WIDE_INT offset1
= 0, offset2
= 0;
1058 HOST_WIDE_INT size1
= -1, size2
= -1;
1059 HOST_WIDE_INT max_size1
= -1, max_size2
= -1;
1061 gcc_assert ((SSA_VAR_P (ref1
)
1062 || handled_component_p (ref1
)
1063 || TREE_CODE (ref1
) == INDIRECT_REF
)
1064 && (SSA_VAR_P (ref2
)
1065 || handled_component_p (ref2
)
1066 || TREE_CODE (ref2
) == INDIRECT_REF
));
1068 /* Defer to TBAA if possible. */
1069 if (flag_strict_aliasing
1070 && !alias_sets_conflict_p (get_alias_set (ref1
), get_alias_set (ref2
)))
1073 /* Decompose the references into their base objects and the access. */
1075 if (handled_component_p (ref1
))
1076 base1
= get_ref_base_and_extent (ref1
, &offset1
, &size1
, &max_size1
);
1078 if (handled_component_p (ref2
))
1079 base2
= get_ref_base_and_extent (ref2
, &offset2
, &size2
, &max_size2
);
1081 /* If both references are based on different variables, they cannot alias.
1082 If both references are based on the same variable, they cannot alias if
1083 if the accesses do not overlap. */
1084 if (SSA_VAR_P (base1
)
1085 && SSA_VAR_P (base2
)
1086 && (!operand_equal_p (base1
, base2
, 0)
1087 || !ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
)))
1090 /* If both references are through pointers and both pointers are equal
1091 then they do not alias if the accesses do not overlap. */
1092 if (TREE_CODE (base1
) == INDIRECT_REF
1093 && TREE_CODE (base2
) == INDIRECT_REF
1094 && operand_equal_p (TREE_OPERAND (base1
, 0),
1095 TREE_OPERAND (base2
, 0), 0)
1096 && !ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
))
1102 /* Given a stmt STMT that references memory, return the single stmt
1103 that is reached by following the VUSE -> VDEF link. Returns
1104 NULL_TREE, if there is no single stmt that defines all VUSEs of
1106 Note that for a stmt with a single virtual operand this may return
1107 a PHI node as well. Note that if all VUSEs are default definitions
1108 this function will return an empty statement. */
1111 get_single_def_stmt (tree stmt
)
1113 tree def_stmt
= NULL_TREE
;
1117 FOR_EACH_SSA_TREE_OPERAND (use
, stmt
, iter
, SSA_OP_VIRTUAL_USES
)
1119 tree tmp
= SSA_NAME_DEF_STMT (use
);
1121 /* ??? This is too simplistic for multiple virtual operands
1122 reaching different PHI nodes of the same basic blocks or for
1123 reaching all default definitions. */
1126 && !(IS_EMPTY_STMT (def_stmt
)
1127 && IS_EMPTY_STMT (tmp
)))
1136 /* Given a PHI node of virtual operands, tries to eliminate cyclic
1137 reached definitions if they do not alias REF and returns the
1138 defining statement of the single virtual operand that flows in
1139 from a non-backedge. Returns NULL_TREE if such statement within
1140 the above conditions cannot be found. */
1143 get_single_def_stmt_from_phi (tree ref
, tree phi
)
1145 tree def_arg
= NULL_TREE
;
1148 /* Find the single PHI argument that is not flowing in from a
1149 back edge and verify that the loop-carried definitions do
1150 not alias the reference we look for. */
1151 for (i
= 0; i
< PHI_NUM_ARGS (phi
); ++i
)
1153 tree arg
= PHI_ARG_DEF (phi
, i
);
1156 if (!(PHI_ARG_EDGE (phi
, i
)->flags
& EDGE_DFS_BACK
))
1158 /* Multiple non-back edges? Do not try to handle this. */
1165 /* Follow the definitions back to the original PHI node. Bail
1166 out once a definition is found that may alias REF. */
1167 def_stmt
= SSA_NAME_DEF_STMT (arg
);
1170 if (TREE_CODE (def_stmt
) != GIMPLE_MODIFY_STMT
1171 || refs_may_alias_p (ref
, GIMPLE_STMT_OPERAND (def_stmt
, 0)))
1173 /* ??? This will only work, reaching the PHI node again if
1174 there is a single virtual operand on def_stmt. */
1175 def_stmt
= get_single_def_stmt (def_stmt
);
1179 while (def_stmt
!= phi
);
1182 return SSA_NAME_DEF_STMT (def_arg
);
1185 /* Return the single reference statement defining all virtual uses
1186 on STMT or NULL_TREE, if there are multiple defining statements.
1187 Take into account only definitions that alias REF if following
1188 back-edges when looking through a loop PHI node. */
1191 get_single_def_stmt_with_phi (tree ref
, tree stmt
)
1193 switch (NUM_SSA_OPERANDS (stmt
, SSA_OP_VIRTUAL_USES
))
1200 tree def_stmt
= SSA_NAME_DEF_STMT (SINGLE_SSA_TREE_OPERAND
1201 (stmt
, SSA_OP_VIRTUAL_USES
));
1202 /* We can handle lookups over PHI nodes only for a single
1204 if (TREE_CODE (def_stmt
) == PHI_NODE
)
1205 return get_single_def_stmt_from_phi (ref
, def_stmt
);
1210 return get_single_def_stmt (stmt
);