1 /* Miscellaneous SSA utility functions.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
29 #include "langhooks.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
35 #include "diagnostic.h"
37 #include "pointer-set.h"
38 #include "tree-flow.h"
39 #include "tree-gimple.h"
40 #include "tree-inline.h"
44 #include "tree-dump.h"
45 #include "tree-pass.h"
48 /* Remove the corresponding arguments from the PHI nodes in E's
49 destination block and redirect it to DEST. Return redirected edge.
50 The list of removed arguments is stored in PENDING_STMT (e). */
53 ssa_redirect_edge (edge e
, basic_block dest
)
56 tree list
= NULL
, *last
= &list
;
59 /* Remove the appropriate PHI arguments in E's destination block. */
60 for (phi
= phi_nodes (e
->dest
); phi
; phi
= PHI_CHAIN (phi
))
62 if (PHI_ARG_DEF (phi
, e
->dest_idx
) == NULL_TREE
)
65 src
= PHI_ARG_DEF (phi
, e
->dest_idx
);
66 dst
= PHI_RESULT (phi
);
67 node
= build_tree_list (dst
, src
);
69 last
= &TREE_CHAIN (node
);
72 e
= redirect_edge_succ_nodup (e
, dest
);
73 PENDING_STMT (e
) = list
;
78 /* Add PHI arguments queued in PENDING_STMT list on edge E to edge
82 flush_pending_stmts (edge e
)
86 if (!PENDING_STMT (e
))
89 for (phi
= phi_nodes (e
->dest
), arg
= PENDING_STMT (e
);
91 phi
= PHI_CHAIN (phi
), arg
= TREE_CHAIN (arg
))
93 tree def
= TREE_VALUE (arg
);
94 add_phi_arg (phi
, def
, e
);
97 PENDING_STMT (e
) = NULL
;
100 /* Return true if SSA_NAME is malformed and mark it visited.
102 IS_VIRTUAL is true if this SSA_NAME was found inside a virtual
106 verify_ssa_name (tree ssa_name
, bool is_virtual
)
108 if (TREE_CODE (ssa_name
) != SSA_NAME
)
110 error ("expected an SSA_NAME object");
114 if (TREE_TYPE (ssa_name
) != TREE_TYPE (SSA_NAME_VAR (ssa_name
)))
116 error ("type mismatch between an SSA_NAME and its symbol");
120 if (SSA_NAME_IN_FREE_LIST (ssa_name
))
122 error ("found an SSA_NAME that had been released into the free pool");
126 if (is_virtual
&& is_gimple_reg (ssa_name
))
128 error ("found a virtual definition for a GIMPLE register");
132 if (!is_virtual
&& !is_gimple_reg (ssa_name
))
134 error ("found a real definition for a non-register");
138 if (is_virtual
&& var_ann (SSA_NAME_VAR (ssa_name
))
139 && get_subvars_for_var (SSA_NAME_VAR (ssa_name
)) != NULL
)
141 error ("found real variable when subvariables should have appeared");
145 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name
)
146 && !IS_EMPTY_STMT (SSA_NAME_DEF_STMT (ssa_name
)))
148 error ("found a default name with a non-empty defining statement");
156 /* Return true if the definition of SSA_NAME at block BB is malformed.
158 STMT is the statement where SSA_NAME is created.
160 DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME
161 version numbers. If DEFINITION_BLOCK[SSA_NAME_VERSION] is set,
162 it means that the block in that array slot contains the
163 definition of SSA_NAME.
165 IS_VIRTUAL is true if SSA_NAME is created by a VDEF. */
168 verify_def (basic_block bb
, basic_block
*definition_block
, tree ssa_name
,
169 tree stmt
, bool is_virtual
)
171 if (verify_ssa_name (ssa_name
, is_virtual
))
174 if (definition_block
[SSA_NAME_VERSION (ssa_name
)])
176 error ("SSA_NAME created in two different blocks %i and %i",
177 definition_block
[SSA_NAME_VERSION (ssa_name
)]->index
, bb
->index
);
181 definition_block
[SSA_NAME_VERSION (ssa_name
)] = bb
;
183 if (SSA_NAME_DEF_STMT (ssa_name
) != stmt
)
185 error ("SSA_NAME_DEF_STMT is wrong");
186 fprintf (stderr
, "Expected definition statement:\n");
187 print_generic_stmt (stderr
, SSA_NAME_DEF_STMT (ssa_name
), TDF_VOPS
);
188 fprintf (stderr
, "\nActual definition statement:\n");
189 print_generic_stmt (stderr
, stmt
, TDF_VOPS
);
196 fprintf (stderr
, "while verifying SSA_NAME ");
197 print_generic_expr (stderr
, ssa_name
, 0);
198 fprintf (stderr
, " in statement\n");
199 print_generic_stmt (stderr
, stmt
, TDF_VOPS
);
205 /* Return true if the use of SSA_NAME at statement STMT in block BB is
208 DEF_BB is the block where SSA_NAME was found to be created.
210 IDOM contains immediate dominator information for the flowgraph.
212 CHECK_ABNORMAL is true if the caller wants to check whether this use
213 is flowing through an abnormal edge (only used when checking PHI
216 If NAMES_DEFINED_IN_BB is not NULL, it contains a bitmap of ssa names
217 that are defined before STMT in basic block BB. */
220 verify_use (basic_block bb
, basic_block def_bb
, use_operand_p use_p
,
221 tree stmt
, bool check_abnormal
, bitmap names_defined_in_bb
)
224 tree ssa_name
= USE_FROM_PTR (use_p
);
226 if (!TREE_VISITED (ssa_name
))
227 if (verify_imm_links (stderr
, ssa_name
))
230 TREE_VISITED (ssa_name
) = 1;
232 if (IS_EMPTY_STMT (SSA_NAME_DEF_STMT (ssa_name
))
233 && SSA_NAME_IS_DEFAULT_DEF (ssa_name
))
234 ; /* Default definitions have empty statements. Nothing to do. */
237 error ("missing definition");
240 else if (bb
!= def_bb
241 && !dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
243 error ("definition in block %i does not dominate use in block %i",
244 def_bb
->index
, bb
->index
);
247 else if (bb
== def_bb
248 && names_defined_in_bb
!= NULL
249 && !bitmap_bit_p (names_defined_in_bb
, SSA_NAME_VERSION (ssa_name
)))
251 error ("definition in block %i follows the use", def_bb
->index
);
256 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
))
258 error ("SSA_NAME_OCCURS_IN_ABNORMAL_PHI should be set");
262 /* Make sure the use is in an appropriate list by checking the previous
263 element to make sure it's the same. */
264 if (use_p
->prev
== NULL
)
266 error ("no immediate_use list");
272 if (use_p
->prev
->use
== NULL
)
273 listvar
= use_p
->prev
->stmt
;
275 listvar
= USE_FROM_PTR (use_p
->prev
);
276 if (listvar
!= ssa_name
)
278 error ("wrong immediate use list");
285 fprintf (stderr
, "for SSA_NAME: ");
286 print_generic_expr (stderr
, ssa_name
, TDF_VOPS
);
287 fprintf (stderr
, " in statement:\n");
288 print_generic_stmt (stderr
, stmt
, TDF_VOPS
);
295 /* Return true if any of the arguments for PHI node PHI at block BB is
298 DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME
299 version numbers. If DEFINITION_BLOCK[SSA_NAME_VERSION] is set,
300 it means that the block in that array slot contains the
301 definition of SSA_NAME. */
304 verify_phi_args (tree phi
, basic_block bb
, basic_block
*definition_block
)
308 unsigned i
, phi_num_args
= PHI_NUM_ARGS (phi
);
310 if (EDGE_COUNT (bb
->preds
) != phi_num_args
)
312 error ("incoming edge count does not match number of PHI arguments");
317 for (i
= 0; i
< phi_num_args
; i
++)
319 use_operand_p op_p
= PHI_ARG_DEF_PTR (phi
, i
);
320 tree op
= USE_FROM_PTR (op_p
);
322 e
= EDGE_PRED (bb
, i
);
326 error ("PHI argument is missing for edge %d->%d",
333 if (TREE_CODE (op
) != SSA_NAME
&& !is_gimple_min_invariant (op
))
335 error ("PHI argument is not SSA_NAME, or invariant");
339 if (TREE_CODE (op
) == SSA_NAME
)
341 err
= verify_ssa_name (op
, !is_gimple_reg (PHI_RESULT (phi
)));
342 err
|= verify_use (e
->src
, definition_block
[SSA_NAME_VERSION (op
)],
343 op_p
, phi
, e
->flags
& EDGE_ABNORMAL
, NULL
);
348 error ("wrong edge %d->%d for PHI argument",
349 e
->src
->index
, e
->dest
->index
);
355 fprintf (stderr
, "PHI argument\n");
356 print_generic_stmt (stderr
, op
, TDF_VOPS
);
364 fprintf (stderr
, "for PHI node\n");
365 print_generic_stmt (stderr
, phi
, TDF_VOPS
|TDF_MEMSYMS
);
374 verify_flow_insensitive_alias_info (void)
377 referenced_var_iterator rvi
;
379 FOR_EACH_REFERENCED_VAR (var
, rvi
)
386 if (!MTAG_P (var
) || !MTAG_ALIASES (var
))
389 aliases
= MTAG_ALIASES (var
);
391 EXECUTE_IF_SET_IN_BITMAP (aliases
, 0, j
, bi
)
393 alias
= referenced_var (j
);
395 if (TREE_CODE (alias
) != MEMORY_PARTITION_TAG
396 && !may_be_aliased (alias
))
398 error ("non-addressable variable inside an alias set");
399 debug_variable (alias
);
408 debug_variable (var
);
409 internal_error ("verify_flow_insensitive_alias_info failed");
414 verify_flow_sensitive_alias_info (void)
419 for (i
= 1; i
< num_ssa_names
; i
++)
423 struct ptr_info_def
*pi
;
430 /* We only care for pointers that are actually referenced in the
432 if (!POINTER_TYPE_P (TREE_TYPE (ptr
)) || !TREE_VISITED (ptr
))
435 /* RESULT_DECL is special. If it's a GIMPLE register, then it
436 is only written-to only once in the return statement.
437 Otherwise, aggregate RESULT_DECLs may be written-to more than
438 once in virtual operands. */
439 var
= SSA_NAME_VAR (ptr
);
440 if (TREE_CODE (var
) == RESULT_DECL
441 && is_gimple_reg (ptr
))
444 pi
= SSA_NAME_PTR_INFO (ptr
);
449 if (pi
->is_dereferenced
&& !pi
->name_mem_tag
&& !ann
->symbol_mem_tag
)
451 error ("dereferenced pointers should have a name or a symbol tag");
456 && (pi
->pt_vars
== NULL
|| bitmap_empty_p (pi
->pt_vars
)))
458 error ("pointers with a memory tag, should have points-to sets");
462 if (pi
->value_escapes_p
&& pi
->name_mem_tag
)
464 tree t
= memory_partition (pi
->name_mem_tag
);
466 t
= pi
->name_mem_tag
;
468 if (!is_call_clobbered (t
))
470 error ("pointer escapes but its name tag is not call-clobbered");
479 debug_variable (ptr
);
480 internal_error ("verify_flow_sensitive_alias_info failed");
484 /* Verify the consistency of call clobbering information. */
487 verify_call_clobbering (void)
492 referenced_var_iterator rvi
;
494 /* At all times, the result of the call_clobbered flag should
495 match the result of the call_clobbered_vars bitmap. Verify both
496 that everything in call_clobbered_vars is marked
497 call_clobbered, and that everything marked
498 call_clobbered is in call_clobbered_vars. */
499 EXECUTE_IF_SET_IN_BITMAP (gimple_call_clobbered_vars (cfun
), 0, i
, bi
)
501 var
= referenced_var (i
);
503 if (memory_partition (var
))
504 var
= memory_partition (var
);
506 if (!MTAG_P (var
) && !var_ann (var
)->call_clobbered
)
508 error ("variable in call_clobbered_vars but not marked "
510 debug_variable (var
);
515 FOR_EACH_REFERENCED_VAR (var
, rvi
)
517 if (is_gimple_reg (var
))
520 if (memory_partition (var
))
521 var
= memory_partition (var
);
524 && var_ann (var
)->call_clobbered
525 && !bitmap_bit_p (gimple_call_clobbered_vars (cfun
), DECL_UID (var
)))
527 error ("variable marked call_clobbered but not in "
528 "call_clobbered_vars bitmap.");
529 debug_variable (var
);
537 internal_error ("verify_call_clobbering failed");
541 /* Verify invariants in memory partitions. */
544 verify_memory_partitions (void)
548 VEC(tree
,heap
) *mpt_table
= gimple_ssa_operands (cfun
)->mpt_table
;
549 struct pointer_set_t
*partitioned_syms
= pointer_set_create ();
551 for (i
= 0; VEC_iterate (tree
, mpt_table
, i
, mpt
); i
++)
556 if (MPT_SYMBOLS (mpt
) == NULL
)
558 error ("Memory partitions should have at least one symbol");
559 debug_variable (mpt
);
563 EXECUTE_IF_SET_IN_BITMAP (MPT_SYMBOLS (mpt
), 0, j
, bj
)
565 tree var
= referenced_var (j
);
566 if (pointer_set_insert (partitioned_syms
, var
))
568 error ("Partitioned symbols should belong to exactly one "
570 debug_variable (var
);
576 pointer_set_destroy (partitioned_syms
);
581 internal_error ("verify_memory_partitions failed");
585 /* Verify the consistency of aliasing information. */
588 verify_alias_info (void)
590 verify_flow_sensitive_alias_info ();
591 verify_call_clobbering ();
592 verify_flow_insensitive_alias_info ();
593 verify_memory_partitions ();
597 /* Verify common invariants in the SSA web.
598 TODO: verify the variable annotations. */
601 verify_ssa (bool check_modified_stmt
)
605 basic_block
*definition_block
= XCNEWVEC (basic_block
, num_ssa_names
);
608 enum dom_state orig_dom_state
= dom_info_state (CDI_DOMINATORS
);
609 bitmap names_defined_in_bb
= BITMAP_ALLOC (NULL
);
611 gcc_assert (!need_ssa_update_p ());
615 timevar_push (TV_TREE_SSA_VERIFY
);
617 /* Keep track of SSA names present in the IL. */
618 for (i
= 1; i
< num_ssa_names
; i
++)
620 tree name
= ssa_name (i
);
624 TREE_VISITED (name
) = 0;
626 stmt
= SSA_NAME_DEF_STMT (name
);
627 if (!IS_EMPTY_STMT (stmt
))
629 basic_block bb
= bb_for_stmt (stmt
);
630 verify_def (bb
, definition_block
,
631 name
, stmt
, !is_gimple_reg (name
));
637 calculate_dominance_info (CDI_DOMINATORS
);
639 /* Now verify all the uses and make sure they agree with the definitions
640 found in the previous pass. */
646 block_stmt_iterator bsi
;
648 /* Make sure that all edges have a clear 'aux' field. */
649 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
653 error ("AUX pointer initialized for edge %d->%d", e
->src
->index
,
659 /* Verify the arguments for every PHI node in the block. */
660 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
662 if (verify_phi_args (phi
, bb
, definition_block
))
665 bitmap_set_bit (names_defined_in_bb
,
666 SSA_NAME_VERSION (PHI_RESULT (phi
)));
669 /* Now verify all the uses and vuses in every statement of the block. */
670 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
672 tree stmt
= bsi_stmt (bsi
);
675 if (check_modified_stmt
&& stmt_modified_p (stmt
))
677 error ("stmt (%p) marked modified after optimization pass: ",
679 print_generic_stmt (stderr
, stmt
, TDF_VOPS
);
683 if (TREE_CODE (stmt
) == GIMPLE_MODIFY_STMT
684 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt
, 0)) != SSA_NAME
)
686 tree lhs
, base_address
;
688 lhs
= GIMPLE_STMT_OPERAND (stmt
, 0);
689 base_address
= get_base_address (lhs
);
692 && gimple_aliases_computed_p (cfun
)
693 && SSA_VAR_P (base_address
)
694 && !stmt_ann (stmt
)->has_volatile_ops
695 && ZERO_SSA_OPERANDS (stmt
, SSA_OP_VDEF
))
697 error ("statement makes a memory store, but has no VDEFS");
698 print_generic_stmt (stderr
, stmt
, TDF_VOPS
);
703 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_ALL_VIRTUALS
)
705 if (verify_ssa_name (op
, true))
707 error ("in statement");
708 print_generic_stmt (stderr
, stmt
, TDF_VOPS
|TDF_MEMSYMS
);
713 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
|SSA_OP_DEF
)
715 if (verify_ssa_name (op
, false))
717 error ("in statement");
718 print_generic_stmt (stderr
, stmt
, TDF_VOPS
|TDF_MEMSYMS
);
723 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
|SSA_OP_VUSE
)
725 op
= USE_FROM_PTR (use_p
);
726 if (verify_use (bb
, definition_block
[SSA_NAME_VERSION (op
)],
727 use_p
, stmt
, false, names_defined_in_bb
))
731 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_ALL_DEFS
)
732 bitmap_set_bit (names_defined_in_bb
, SSA_NAME_VERSION (op
));
735 bitmap_clear (names_defined_in_bb
);
738 /* Finally, verify alias information. */
739 if (gimple_aliases_computed_p (cfun
))
740 verify_alias_info ();
742 free (definition_block
);
744 /* Restore the dominance information to its prior known state, so
745 that we do not perturb the compiler's subsequent behavior. */
746 if (orig_dom_state
== DOM_NONE
)
747 free_dominance_info (CDI_DOMINATORS
);
749 set_dom_info_availability (CDI_DOMINATORS
, orig_dom_state
);
751 BITMAP_FREE (names_defined_in_bb
);
752 timevar_pop (TV_TREE_SSA_VERIFY
);
756 internal_error ("verify_ssa failed");
759 /* Return true if the uid in both int tree maps are equal. */
762 int_tree_map_eq (const void *va
, const void *vb
)
764 const struct int_tree_map
*a
= (const struct int_tree_map
*) va
;
765 const struct int_tree_map
*b
= (const struct int_tree_map
*) vb
;
766 return (a
->uid
== b
->uid
);
769 /* Hash a UID in a int_tree_map. */
772 int_tree_map_hash (const void *item
)
774 return ((const struct int_tree_map
*)item
)->uid
;
777 /* Return true if the uid in both int tree maps are equal. */
780 var_ann_eq (const void *va
, const void *vb
)
782 const struct static_var_ann_d
*a
= (const struct static_var_ann_d
*) va
;
783 const_tree
const b
= (const_tree
) vb
;
784 return (a
->uid
== DECL_UID (b
));
787 /* Hash a UID in a int_tree_map. */
790 var_ann_hash (const void *item
)
792 return ((const struct static_var_ann_d
*)item
)->uid
;
796 /* Initialize global DFA and SSA structures. */
801 cfun
->gimple_df
= GGC_CNEW (struct gimple_df
);
802 cfun
->gimple_df
->referenced_vars
= htab_create_ggc (20, int_tree_map_hash
,
803 int_tree_map_eq
, NULL
);
804 cfun
->gimple_df
->default_defs
= htab_create_ggc (20, int_tree_map_hash
,
805 int_tree_map_eq
, NULL
);
806 cfun
->gimple_df
->var_anns
= htab_create_ggc (20, var_ann_hash
,
808 cfun
->gimple_df
->call_clobbered_vars
= BITMAP_GGC_ALLOC ();
809 cfun
->gimple_df
->addressable_vars
= BITMAP_GGC_ALLOC ();
815 /* Deallocate memory associated with SSA data structures for FNDECL. */
818 delete_tree_ssa (void)
822 block_stmt_iterator bsi
;
823 referenced_var_iterator rvi
;
826 /* Release any ssa_names still in use. */
827 for (i
= 0; i
< num_ssa_names
; i
++)
829 tree var
= ssa_name (i
);
830 if (var
&& TREE_CODE (var
) == SSA_NAME
)
832 SSA_NAME_IMM_USE_NODE (var
).prev
= &(SSA_NAME_IMM_USE_NODE (var
));
833 SSA_NAME_IMM_USE_NODE (var
).next
= &(SSA_NAME_IMM_USE_NODE (var
));
835 release_ssa_name (var
);
838 /* Remove annotations from every tree in the function. */
841 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
843 tree stmt
= bsi_stmt (bsi
);
844 stmt_ann_t ann
= get_stmt_ann (stmt
);
846 free_ssa_operands (&ann
->operands
);
847 ann
->addresses_taken
= 0;
848 mark_stmt_modified (stmt
);
850 set_phi_nodes (bb
, NULL
);
853 /* Remove annotations from every referenced variable. */
854 FOR_EACH_REFERENCED_VAR (var
, rvi
)
857 ggc_free (var
->base
.ann
);
858 var
->base
.ann
= NULL
;
860 htab_delete (gimple_referenced_vars (cfun
));
861 cfun
->gimple_df
->referenced_vars
= NULL
;
865 /* we no longer maintain the SSA operand cache at this point. */
866 fini_ssa_operands ();
868 cfun
->gimple_df
->global_var
= NULL_TREE
;
870 htab_delete (cfun
->gimple_df
->default_defs
);
871 cfun
->gimple_df
->default_defs
= NULL
;
872 htab_delete (cfun
->gimple_df
->var_anns
);
873 cfun
->gimple_df
->var_anns
= NULL
;
874 cfun
->gimple_df
->call_clobbered_vars
= NULL
;
875 cfun
->gimple_df
->addressable_vars
= NULL
;
876 cfun
->gimple_df
->modified_noreturn_calls
= NULL
;
877 if (gimple_aliases_computed_p (cfun
))
879 delete_alias_heapvars ();
880 gcc_assert (!need_ssa_update_p ());
882 cfun
->gimple_df
->aliases_computed_p
= false;
883 delete_mem_ref_stats (cfun
);
885 cfun
->gimple_df
= NULL
;
888 /* Helper function for useless_type_conversion_p. */
891 useless_type_conversion_p_1 (tree outer_type
, tree inner_type
)
893 /* Qualifiers on value types do not matter. */
894 inner_type
= TYPE_MAIN_VARIANT (inner_type
);
895 outer_type
= TYPE_MAIN_VARIANT (outer_type
);
897 if (inner_type
== outer_type
)
900 /* If we know the canonical types, compare them. */
901 if (TYPE_CANONICAL (inner_type
)
902 && TYPE_CANONICAL (inner_type
) == TYPE_CANONICAL (outer_type
))
905 /* Changes in machine mode are never useless conversions. */
906 if (TYPE_MODE (inner_type
) != TYPE_MODE (outer_type
))
909 /* If both the inner and outer types are integral types, then the
910 conversion is not necessary if they have the same mode and
911 signedness and precision, and both or neither are boolean. */
912 if (INTEGRAL_TYPE_P (inner_type
)
913 && INTEGRAL_TYPE_P (outer_type
))
915 /* Preserve changes in signedness or precision. */
916 if (TYPE_UNSIGNED (inner_type
) != TYPE_UNSIGNED (outer_type
)
917 || TYPE_PRECISION (inner_type
) != TYPE_PRECISION (outer_type
))
920 /* Conversions from a non-base to a base type are not useless.
921 This way we preserve the invariant to do arithmetic in
923 if (TREE_TYPE (inner_type
)
924 && TREE_TYPE (inner_type
) != inner_type
925 && (TREE_TYPE (outer_type
) == outer_type
926 || TREE_TYPE (outer_type
) == NULL_TREE
))
929 /* We don't need to preserve changes in the types minimum or
930 maximum value in general as these do not generate code
931 unless the types precisions are different. */
936 /* Scalar floating point types with the same mode are compatible. */
937 else if (SCALAR_FLOAT_TYPE_P (inner_type
)
938 && SCALAR_FLOAT_TYPE_P (outer_type
))
941 /* We need to take special care recursing to pointed-to types. */
942 else if (POINTER_TYPE_P (inner_type
)
943 && POINTER_TYPE_P (outer_type
))
945 /* Don't lose casts between pointers to volatile and non-volatile
946 qualified types. Doing so would result in changing the semantics
947 of later accesses. */
948 if ((TYPE_VOLATILE (TREE_TYPE (outer_type
))
949 != TYPE_VOLATILE (TREE_TYPE (inner_type
)))
950 && TYPE_VOLATILE (TREE_TYPE (outer_type
)))
953 /* Do not lose casts between pointers with different
954 TYPE_REF_CAN_ALIAS_ALL setting or alias sets. */
955 if ((TYPE_REF_CAN_ALIAS_ALL (inner_type
)
956 != TYPE_REF_CAN_ALIAS_ALL (outer_type
))
957 || (get_alias_set (TREE_TYPE (inner_type
))
958 != get_alias_set (TREE_TYPE (outer_type
))))
961 /* Do not lose casts from const qualified to non-const
963 if ((TYPE_READONLY (TREE_TYPE (outer_type
))
964 != TYPE_READONLY (TREE_TYPE (inner_type
)))
965 && TYPE_READONLY (TREE_TYPE (inner_type
)))
968 /* Do not lose casts to restrict qualified pointers. */
969 if ((TYPE_RESTRICT (outer_type
)
970 != TYPE_RESTRICT (inner_type
))
971 && TYPE_RESTRICT (outer_type
))
974 /* Otherwise pointers/references are equivalent if their pointed
975 to types are effectively the same. We can strip qualifiers
976 on pointed-to types for further comparison, which is done in
978 return useless_type_conversion_p_1 (TREE_TYPE (outer_type
),
979 TREE_TYPE (inner_type
));
982 /* Recurse for complex types. */
983 else if (TREE_CODE (inner_type
) == COMPLEX_TYPE
984 && TREE_CODE (outer_type
) == COMPLEX_TYPE
)
985 return useless_type_conversion_p_1 (TREE_TYPE (outer_type
),
986 TREE_TYPE (inner_type
));
988 /* Recurse for vector types with the same number of subparts. */
989 else if (TREE_CODE (inner_type
) == VECTOR_TYPE
990 && TREE_CODE (outer_type
) == VECTOR_TYPE
991 && TYPE_PRECISION (inner_type
) == TYPE_PRECISION (outer_type
))
992 return useless_type_conversion_p_1 (TREE_TYPE (outer_type
),
993 TREE_TYPE (inner_type
));
995 /* For aggregates we may need to fall back to structural equality
997 else if (AGGREGATE_TYPE_P (inner_type
)
998 && AGGREGATE_TYPE_P (outer_type
))
1000 /* Different types of aggregates are incompatible. */
1001 if (TREE_CODE (inner_type
) != TREE_CODE (outer_type
))
1004 /* ??? Add structural equivalence check. */
1006 /* ??? This should eventually just return false. */
1007 return lang_hooks
.types_compatible_p (inner_type
, outer_type
);
1013 /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
1014 useless type conversion, otherwise return false.
1016 This function implicitly defines the middle-end type system. With
1017 the notion of 'a < b' meaning that useless_type_conversion_p (a, b)
1018 holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds,
1019 the following invariants shall be fulfilled:
1021 1) useless_type_conversion_p is transitive.
1022 If a < b and b < c then a < c.
1024 2) useless_type_conversion_p is not symmetric.
1025 From a < b does not follow a > b.
1027 3) Types define the available set of operations applicable to values.
1028 A type conversion is useless if the operations for the target type
1029 is a subset of the operations for the source type. For example
1030 casts to void* are useless, casts from void* are not (void* can't
1031 be dereferenced or offsetted, but copied, hence its set of operations
1032 is a strict subset of that of all other data pointer types). Casts
1033 to const T* are useless (can't be written to), casts from const T*
1037 useless_type_conversion_p (tree outer_type
, tree inner_type
)
1039 /* If the outer type is (void *), then the conversion is not
1040 necessary. We have to make sure to not apply this while
1041 recursing though. */
1042 if (POINTER_TYPE_P (inner_type
)
1043 && POINTER_TYPE_P (outer_type
)
1044 && TREE_CODE (TREE_TYPE (outer_type
)) == VOID_TYPE
)
1047 return useless_type_conversion_p_1 (outer_type
, inner_type
);
1050 /* Return true if a conversion from either type of TYPE1 and TYPE2
1051 to the other is not required. Otherwise return false. */
1054 types_compatible_p (tree type1
, tree type2
)
1056 return (type1
== type2
1057 || (useless_type_conversion_p (type1
, type2
)
1058 && useless_type_conversion_p (type2
, type1
)));
1061 /* Return true if EXPR is a useless type conversion, otherwise return
1065 tree_ssa_useless_type_conversion (tree expr
)
1067 /* If we have an assignment that merely uses a NOP_EXPR to change
1068 the top of the RHS to the type of the LHS and the type conversion
1069 is "safe", then strip away the type conversion so that we can
1070 enter LHS = RHS into the const_and_copies table. */
1071 if (TREE_CODE (expr
) == NOP_EXPR
|| TREE_CODE (expr
) == CONVERT_EXPR
1072 || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
1073 || TREE_CODE (expr
) == NON_LVALUE_EXPR
)
1074 /* FIXME: Use of GENERIC_TREE_TYPE here is a temporary measure to work
1075 around known bugs with GIMPLE_MODIFY_STMTs appearing in places
1076 they shouldn't. See PR 30391. */
1077 return useless_type_conversion_p
1079 GENERIC_TREE_TYPE (TREE_OPERAND (expr
, 0)));
1085 /* Internal helper for walk_use_def_chains. VAR, FN and DATA are as
1086 described in walk_use_def_chains.
1088 VISITED is a pointer set used to mark visited SSA_NAMEs to avoid
1089 infinite loops. We used to have a bitmap for this to just mark
1090 SSA versions we had visited. But non-sparse bitmaps are way too
1091 expensive, while sparse bitmaps may cause quadratic behavior.
1093 IS_DFS is true if the caller wants to perform a depth-first search
1094 when visiting PHI nodes. A DFS will visit each PHI argument and
1095 call FN after each one. Otherwise, all the arguments are
1096 visited first and then FN is called with each of the visited
1097 arguments in a separate pass. */
1100 walk_use_def_chains_1 (tree var
, walk_use_def_chains_fn fn
, void *data
,
1101 struct pointer_set_t
*visited
, bool is_dfs
)
1105 if (pointer_set_insert (visited
, var
))
1108 def_stmt
= SSA_NAME_DEF_STMT (var
);
1110 if (TREE_CODE (def_stmt
) != PHI_NODE
)
1112 /* If we reached the end of the use-def chain, call FN. */
1113 return fn (var
, def_stmt
, data
);
1119 /* When doing a breadth-first search, call FN before following the
1120 use-def links for each argument. */
1122 for (i
= 0; i
< PHI_NUM_ARGS (def_stmt
); i
++)
1123 if (fn (PHI_ARG_DEF (def_stmt
, i
), def_stmt
, data
))
1126 /* Follow use-def links out of each PHI argument. */
1127 for (i
= 0; i
< PHI_NUM_ARGS (def_stmt
); i
++)
1129 tree arg
= PHI_ARG_DEF (def_stmt
, i
);
1131 /* ARG may be NULL for newly introduced PHI nodes. */
1133 && TREE_CODE (arg
) == SSA_NAME
1134 && walk_use_def_chains_1 (arg
, fn
, data
, visited
, is_dfs
))
1138 /* When doing a depth-first search, call FN after following the
1139 use-def links for each argument. */
1141 for (i
= 0; i
< PHI_NUM_ARGS (def_stmt
); i
++)
1142 if (fn (PHI_ARG_DEF (def_stmt
, i
), def_stmt
, data
))
1151 /* Walk use-def chains starting at the SSA variable VAR. Call
1152 function FN at each reaching definition found. FN takes three
1153 arguments: VAR, its defining statement (DEF_STMT) and a generic
1154 pointer to whatever state information that FN may want to maintain
1155 (DATA). FN is able to stop the walk by returning true, otherwise
1156 in order to continue the walk, FN should return false.
1158 Note, that if DEF_STMT is a PHI node, the semantics are slightly
1159 different. The first argument to FN is no longer the original
1160 variable VAR, but the PHI argument currently being examined. If FN
1161 wants to get at VAR, it should call PHI_RESULT (PHI).
1163 If IS_DFS is true, this function will:
1165 1- walk the use-def chains for all the PHI arguments, and,
1166 2- call (*FN) (ARG, PHI, DATA) on all the PHI arguments.
1168 If IS_DFS is false, the two steps above are done in reverse order
1169 (i.e., a breadth-first search). */
1172 walk_use_def_chains (tree var
, walk_use_def_chains_fn fn
, void *data
,
1177 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
1179 def_stmt
= SSA_NAME_DEF_STMT (var
);
1181 /* We only need to recurse if the reaching definition comes from a PHI
1183 if (TREE_CODE (def_stmt
) != PHI_NODE
)
1184 (*fn
) (var
, def_stmt
, data
);
1187 struct pointer_set_t
*visited
= pointer_set_create ();
1188 walk_use_def_chains_1 (var
, fn
, data
, visited
, is_dfs
);
1189 pointer_set_destroy (visited
);
1194 /* Emit warnings for uninitialized variables. This is done in two passes.
1196 The first pass notices real uses of SSA names with default definitions.
1197 Such uses are unconditionally uninitialized, and we can be certain that
1198 such a use is a mistake. This pass is run before most optimizations,
1199 so that we catch as many as we can.
1201 The second pass follows PHI nodes to find uses that are potentially
1202 uninitialized. In this case we can't necessarily prove that the use
1203 is really uninitialized. This pass is run after most optimizations,
1204 so that we thread as many jumps and possible, and delete as much dead
1205 code as possible, in order to reduce false positives. We also look
1206 again for plain uninitialized variables, since optimization may have
1207 changed conditionally uninitialized to unconditionally uninitialized. */
1209 /* Emit a warning for T, an SSA_NAME, being uninitialized. The exact
1210 warning text is in MSGID and LOCUS may contain a location or be null. */
1213 warn_uninit (tree t
, const char *gmsgid
, void *data
)
1215 tree var
= SSA_NAME_VAR (t
);
1216 tree def
= SSA_NAME_DEF_STMT (t
);
1217 tree context
= (tree
) data
;
1219 expanded_location xloc
, floc
;
1221 /* Default uses (indicated by an empty definition statement),
1222 are uninitialized. */
1223 if (!IS_EMPTY_STMT (def
))
1226 /* Except for PARMs of course, which are always initialized. */
1227 if (TREE_CODE (var
) == PARM_DECL
)
1230 /* Hard register variables get their initial value from the ether. */
1231 if (TREE_CODE (var
) == VAR_DECL
&& DECL_HARD_REGISTER (var
))
1234 /* TREE_NO_WARNING either means we already warned, or the front end
1235 wishes to suppress the warning. */
1236 if (TREE_NO_WARNING (var
))
1239 locus
= (context
!= NULL
&& EXPR_HAS_LOCATION (context
)
1240 ? EXPR_LOCUS (context
)
1241 : &DECL_SOURCE_LOCATION (var
));
1242 warning (OPT_Wuninitialized
, gmsgid
, locus
, var
);
1243 xloc
= expand_location (*locus
);
1244 floc
= expand_location (DECL_SOURCE_LOCATION (cfun
->decl
));
1245 if (xloc
.file
!= floc
.file
1246 || xloc
.line
< floc
.line
1247 || xloc
.line
> LOCATION_LINE (cfun
->function_end_locus
))
1248 inform ("%J%qD was declared here", var
, var
);
1250 TREE_NO_WARNING (var
) = 1;
1253 /* Called via walk_tree, look for SSA_NAMEs that have empty definitions
1254 and warn about them. */
1257 warn_uninitialized_var (tree
*tp
, int *walk_subtrees
, void *data
)
1261 switch (TREE_CODE (t
))
1264 /* We only do data flow with SSA_NAMEs, so that's all we
1266 warn_uninit (t
, "%H%qD is used uninitialized in this function", data
);
1272 /* The total store transformation performed during gimplification
1273 creates uninitialized variable uses. If all is well, these will
1274 be optimized away, so don't warn now. */
1275 if (TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
)
1280 if (IS_TYPE_OR_DECL_P (t
))
1288 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
1289 and warn about them. */
1292 warn_uninitialized_phi (tree phi
)
1294 int i
, n
= PHI_NUM_ARGS (phi
);
1296 /* Don't look at memory tags. */
1297 if (!is_gimple_reg (PHI_RESULT (phi
)))
1300 for (i
= 0; i
< n
; ++i
)
1302 tree op
= PHI_ARG_DEF (phi
, i
);
1303 if (TREE_CODE (op
) == SSA_NAME
)
1304 warn_uninit (op
, "%H%qD may be used uninitialized in this function",
1310 execute_early_warn_uninitialized (void)
1312 block_stmt_iterator bsi
;
1316 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1318 tree context
= bsi_stmt (bsi
);
1319 walk_tree (bsi_stmt_ptr (bsi
), warn_uninitialized_var
,
1326 execute_late_warn_uninitialized (void)
1331 /* Re-do the plain uninitialized variable check, as optimization may have
1332 straightened control flow. Do this first so that we don't accidentally
1333 get a "may be" warning when we'd have seen an "is" warning later. */
1334 execute_early_warn_uninitialized ();
1337 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
1338 warn_uninitialized_phi (phi
);
1343 gate_warn_uninitialized (void)
1345 return warn_uninitialized
!= 0;
1348 struct tree_opt_pass pass_early_warn_uninitialized
=
1351 gate_warn_uninitialized
, /* gate */
1352 execute_early_warn_uninitialized
, /* execute */
1355 0, /* static_pass_number */
1357 PROP_ssa
, /* properties_required */
1358 0, /* properties_provided */
1359 0, /* properties_destroyed */
1360 0, /* todo_flags_start */
1361 0, /* todo_flags_finish */
1365 struct tree_opt_pass pass_late_warn_uninitialized
=
1368 gate_warn_uninitialized
, /* gate */
1369 execute_late_warn_uninitialized
, /* execute */
1372 0, /* static_pass_number */
1374 PROP_ssa
, /* properties_required */
1375 0, /* properties_provided */
1376 0, /* properties_destroyed */
1377 0, /* todo_flags_start */
1378 0, /* todo_flags_finish */
1382 /* Compute TREE_ADDRESSABLE for local variables. */
1385 execute_update_addresses_taken (void)
1388 referenced_var_iterator rvi
;
1389 block_stmt_iterator bsi
;
1391 bitmap addresses_taken
= BITMAP_ALLOC (NULL
);
1392 bitmap vars_updated
= BITMAP_ALLOC (NULL
);
1396 /* Collect into ADDRESSES_TAKEN all variables whose address is taken within
1397 the function body. */
1400 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1402 stmt_ann_t s_ann
= stmt_ann (bsi_stmt (bsi
));
1404 if (s_ann
->addresses_taken
)
1405 bitmap_ior_into (addresses_taken
, s_ann
->addresses_taken
);
1407 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
1409 unsigned i
, phi_num_args
= PHI_NUM_ARGS (phi
);
1410 for (i
= 0; i
< phi_num_args
; i
++)
1412 tree op
= PHI_ARG_DEF (phi
, i
), var
;
1413 if (TREE_CODE (op
) == ADDR_EXPR
1414 && (var
= get_base_address (TREE_OPERAND (op
, 0))) != NULL_TREE
1416 bitmap_set_bit (addresses_taken
, DECL_UID (var
));
1421 /* When possible, clear ADDRESSABLE bit and mark variable for conversion into
1423 FOR_EACH_REFERENCED_VAR (var
, rvi
)
1424 if (!is_global_var (var
)
1425 && TREE_CODE (var
) != RESULT_DECL
1426 && TREE_ADDRESSABLE (var
)
1427 && !bitmap_bit_p (addresses_taken
, DECL_UID (var
)))
1429 TREE_ADDRESSABLE (var
) = 0;
1430 if (is_gimple_reg (var
))
1431 mark_sym_for_renaming (var
);
1433 bitmap_set_bit (vars_updated
, DECL_UID (var
));
1436 fprintf (dump_file
, "No longer having address taken ");
1437 print_generic_expr (dump_file
, var
, 0);
1438 fprintf (dump_file
, "\n");
1442 /* Operand caches needs to be recomputed for operands referencing the updated
1446 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
1448 tree stmt
= bsi_stmt (bsi
);
1450 if ((LOADED_SYMS (stmt
)
1451 && bitmap_intersect_p (LOADED_SYMS (stmt
), vars_updated
))
1452 || (STORED_SYMS (stmt
)
1453 && bitmap_intersect_p (STORED_SYMS (stmt
), vars_updated
)))
1456 BITMAP_FREE (addresses_taken
);
1457 BITMAP_FREE (vars_updated
);
1461 struct tree_opt_pass pass_update_address_taken
=
1463 "addressables", /* name */
1465 execute_update_addresses_taken
, /* execute */
1468 0, /* static_pass_number */
1470 PROP_ssa
, /* properties_required */
1471 0, /* properties_provided */
1472 0, /* properties_destroyed */
1473 0, /* todo_flags_start */
1474 TODO_update_ssa
, /* todo_flags_finish */