1 /* Miscellaneous SSA utility functions.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
3 Free Software Foundation, Inc.
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"
30 #include "langhooks.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
36 #include "diagnostic.h"
38 #include "pointer-set.h"
39 #include "tree-flow.h"
41 #include "tree-inline.h"
45 #include "tree-dump.h"
46 #include "tree-pass.h"
49 /* Pointer map of variable mappings, keyed by edge. */
50 static struct pointer_map_t
*edge_var_maps
;
53 /* Add a mapping with PHI RESULT and PHI DEF associated with edge E. */
56 redirect_edge_var_map_add (edge e
, tree result
, tree def
)
59 edge_var_map_vector old_head
, head
;
60 edge_var_map new_node
;
62 if (edge_var_maps
== NULL
)
63 edge_var_maps
= pointer_map_create ();
65 slot
= pointer_map_insert (edge_var_maps
, e
);
66 old_head
= head
= (edge_var_map_vector
) *slot
;
69 head
= VEC_alloc (edge_var_map
, heap
, 5);
73 new_node
.result
= result
;
75 VEC_safe_push (edge_var_map
, heap
, head
, &new_node
);
78 /* The push did some reallocation. Update the pointer map. */
84 /* Clear the var mappings in edge E. */
87 redirect_edge_var_map_clear (edge e
)
90 edge_var_map_vector head
;
95 slot
= pointer_map_contains (edge_var_maps
, e
);
99 head
= (edge_var_map_vector
) *slot
;
100 VEC_free (edge_var_map
, heap
, head
);
106 /* Duplicate the redirected var mappings in OLDE in NEWE.
108 Since we can't remove a mapping, let's just duplicate it. This assumes a
109 pointer_map can have multiple edges mapping to the same var_map (many to
110 one mapping), since we don't remove the previous mappings. */
113 redirect_edge_var_map_dup (edge newe
, edge olde
)
115 void **new_slot
, **old_slot
;
116 edge_var_map_vector head
;
121 new_slot
= pointer_map_insert (edge_var_maps
, newe
);
122 old_slot
= pointer_map_contains (edge_var_maps
, olde
);
125 head
= (edge_var_map_vector
) *old_slot
;
128 *new_slot
= VEC_copy (edge_var_map
, heap
, head
);
130 *new_slot
= VEC_alloc (edge_var_map
, heap
, 5);
134 /* Return the variable mappings for a given edge. If there is none, return
138 redirect_edge_var_map_vector (edge e
)
142 /* Hey, what kind of idiot would... you'd be surprised. */
146 slot
= pointer_map_contains (edge_var_maps
, e
);
150 return (edge_var_map_vector
) *slot
;
153 /* Used by redirect_edge_var_map_destroy to free all memory. */
156 free_var_map_entry (const void *key ATTRIBUTE_UNUSED
,
158 void *data ATTRIBUTE_UNUSED
)
160 edge_var_map_vector head
= (edge_var_map_vector
) *value
;
161 VEC_free (edge_var_map
, heap
, head
);
165 /* Clear the edge variable mappings. */
168 redirect_edge_var_map_destroy (void)
172 pointer_map_traverse (edge_var_maps
, free_var_map_entry
, NULL
);
173 pointer_map_destroy (edge_var_maps
);
174 edge_var_maps
= NULL
;
179 /* Remove the corresponding arguments from the PHI nodes in E's
180 destination block and redirect it to DEST. Return redirected edge.
181 The list of removed arguments is stored in a vector accessed
182 through edge_var_maps. */
185 ssa_redirect_edge (edge e
, basic_block dest
)
187 gimple_stmt_iterator gsi
;
190 redirect_edge_var_map_clear (e
);
192 /* Remove the appropriate PHI arguments in E's destination block. */
193 for (gsi
= gsi_start_phis (e
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
197 phi
= gsi_stmt (gsi
);
198 def
= gimple_phi_arg_def (phi
, e
->dest_idx
);
200 if (def
== NULL_TREE
)
203 redirect_edge_var_map_add (e
, gimple_phi_result (phi
), def
);
206 e
= redirect_edge_succ_nodup (e
, dest
);
212 /* Add PHI arguments queued in PENDING_STMT list on edge E to edge
216 flush_pending_stmts (edge e
)
219 edge_var_map_vector v
;
222 gimple_stmt_iterator gsi
;
224 v
= redirect_edge_var_map_vector (e
);
228 for (gsi
= gsi_start_phis (e
->dest
), i
= 0;
229 !gsi_end_p (gsi
) && VEC_iterate (edge_var_map
, v
, i
, vm
);
230 gsi_next (&gsi
), i
++)
234 phi
= gsi_stmt (gsi
);
235 def
= redirect_edge_var_map_def (vm
);
236 add_phi_arg (phi
, def
, e
);
239 redirect_edge_var_map_clear (e
);
242 /* Return true if SSA_NAME is malformed and mark it visited.
244 IS_VIRTUAL is true if this SSA_NAME was found inside a virtual
248 verify_ssa_name (tree ssa_name
, bool is_virtual
)
250 if (TREE_CODE (ssa_name
) != SSA_NAME
)
252 error ("expected an SSA_NAME object");
256 if (TREE_TYPE (ssa_name
) != TREE_TYPE (SSA_NAME_VAR (ssa_name
)))
258 error ("type mismatch between an SSA_NAME and its symbol");
262 if (SSA_NAME_IN_FREE_LIST (ssa_name
))
264 error ("found an SSA_NAME that had been released into the free pool");
268 if (is_virtual
&& is_gimple_reg (ssa_name
))
270 error ("found a virtual definition for a GIMPLE register");
274 if (is_virtual
&& SSA_NAME_VAR (ssa_name
) != gimple_vop (cfun
))
276 error ("virtual SSA name for non-VOP decl");
280 if (!is_virtual
&& !is_gimple_reg (ssa_name
))
282 error ("found a real definition for a non-register");
286 if (SSA_NAME_IS_DEFAULT_DEF (ssa_name
)
287 && !gimple_nop_p (SSA_NAME_DEF_STMT (ssa_name
)))
289 error ("found a default name with a non-empty defining statement");
297 /* Return true if the definition of SSA_NAME at block BB is malformed.
299 STMT is the statement where SSA_NAME is created.
301 DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME
302 version numbers. If DEFINITION_BLOCK[SSA_NAME_VERSION] is set,
303 it means that the block in that array slot contains the
304 definition of SSA_NAME.
306 IS_VIRTUAL is true if SSA_NAME is created by a VDEF. */
309 verify_def (basic_block bb
, basic_block
*definition_block
, tree ssa_name
,
310 gimple stmt
, bool is_virtual
)
312 if (verify_ssa_name (ssa_name
, is_virtual
))
315 if (definition_block
[SSA_NAME_VERSION (ssa_name
)])
317 error ("SSA_NAME created in two different blocks %i and %i",
318 definition_block
[SSA_NAME_VERSION (ssa_name
)]->index
, bb
->index
);
322 definition_block
[SSA_NAME_VERSION (ssa_name
)] = bb
;
324 if (SSA_NAME_DEF_STMT (ssa_name
) != stmt
)
326 error ("SSA_NAME_DEF_STMT is wrong");
327 fprintf (stderr
, "Expected definition statement:\n");
328 print_gimple_stmt (stderr
, SSA_NAME_DEF_STMT (ssa_name
), 4, TDF_VOPS
);
329 fprintf (stderr
, "\nActual definition statement:\n");
330 print_gimple_stmt (stderr
, stmt
, 4, TDF_VOPS
);
337 fprintf (stderr
, "while verifying SSA_NAME ");
338 print_generic_expr (stderr
, ssa_name
, 0);
339 fprintf (stderr
, " in statement\n");
340 print_gimple_stmt (stderr
, stmt
, 4, TDF_VOPS
);
346 /* Return true if the use of SSA_NAME at statement STMT in block BB is
349 DEF_BB is the block where SSA_NAME was found to be created.
351 IDOM contains immediate dominator information for the flowgraph.
353 CHECK_ABNORMAL is true if the caller wants to check whether this use
354 is flowing through an abnormal edge (only used when checking PHI
357 If NAMES_DEFINED_IN_BB is not NULL, it contains a bitmap of ssa names
358 that are defined before STMT in basic block BB. */
361 verify_use (basic_block bb
, basic_block def_bb
, use_operand_p use_p
,
362 gimple stmt
, bool check_abnormal
, bitmap names_defined_in_bb
)
365 tree ssa_name
= USE_FROM_PTR (use_p
);
367 if (!TREE_VISITED (ssa_name
))
368 if (verify_imm_links (stderr
, ssa_name
))
371 TREE_VISITED (ssa_name
) = 1;
373 if (gimple_nop_p (SSA_NAME_DEF_STMT (ssa_name
))
374 && SSA_NAME_IS_DEFAULT_DEF (ssa_name
))
375 ; /* Default definitions have empty statements. Nothing to do. */
378 error ("missing definition");
381 else if (bb
!= def_bb
382 && !dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
384 error ("definition in block %i does not dominate use in block %i",
385 def_bb
->index
, bb
->index
);
388 else if (bb
== def_bb
389 && names_defined_in_bb
!= NULL
390 && !bitmap_bit_p (names_defined_in_bb
, SSA_NAME_VERSION (ssa_name
)))
392 error ("definition in block %i follows the use", def_bb
->index
);
397 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssa_name
))
399 error ("SSA_NAME_OCCURS_IN_ABNORMAL_PHI should be set");
403 /* Make sure the use is in an appropriate list by checking the previous
404 element to make sure it's the same. */
405 if (use_p
->prev
== NULL
)
407 error ("no immediate_use list");
413 if (use_p
->prev
->use
== NULL
)
414 listvar
= use_p
->prev
->loc
.ssa_name
;
416 listvar
= USE_FROM_PTR (use_p
->prev
);
417 if (listvar
!= ssa_name
)
419 error ("wrong immediate use list");
426 fprintf (stderr
, "for SSA_NAME: ");
427 print_generic_expr (stderr
, ssa_name
, TDF_VOPS
);
428 fprintf (stderr
, " in statement:\n");
429 print_gimple_stmt (stderr
, stmt
, 0, TDF_VOPS
);
436 /* Return true if any of the arguments for PHI node PHI at block BB is
439 DEFINITION_BLOCK is an array of basic blocks indexed by SSA_NAME
440 version numbers. If DEFINITION_BLOCK[SSA_NAME_VERSION] is set,
441 it means that the block in that array slot contains the
442 definition of SSA_NAME. */
445 verify_phi_args (gimple phi
, basic_block bb
, basic_block
*definition_block
)
449 size_t i
, phi_num_args
= gimple_phi_num_args (phi
);
451 if (EDGE_COUNT (bb
->preds
) != phi_num_args
)
453 error ("incoming edge count does not match number of PHI arguments");
458 for (i
= 0; i
< phi_num_args
; i
++)
460 use_operand_p op_p
= gimple_phi_arg_imm_use_ptr (phi
, i
);
461 tree op
= USE_FROM_PTR (op_p
);
463 e
= EDGE_PRED (bb
, i
);
467 error ("PHI argument is missing for edge %d->%d",
474 if (TREE_CODE (op
) != SSA_NAME
&& !is_gimple_min_invariant (op
))
476 error ("PHI argument is not SSA_NAME, or invariant");
480 if (TREE_CODE (op
) == SSA_NAME
)
482 err
= verify_ssa_name (op
, !is_gimple_reg (gimple_phi_result (phi
)));
483 err
|= verify_use (e
->src
, definition_block
[SSA_NAME_VERSION (op
)],
484 op_p
, phi
, e
->flags
& EDGE_ABNORMAL
, NULL
);
487 if (TREE_CODE (op
) == ADDR_EXPR
)
489 tree base
= TREE_OPERAND (op
, 0);
490 while (handled_component_p (base
))
491 base
= TREE_OPERAND (base
, 0);
492 if ((TREE_CODE (base
) == VAR_DECL
493 || TREE_CODE (base
) == PARM_DECL
494 || TREE_CODE (base
) == RESULT_DECL
)
495 && !TREE_ADDRESSABLE (base
))
497 error ("address taken, but ADDRESSABLE bit not set");
504 error ("wrong edge %d->%d for PHI argument",
505 e
->src
->index
, e
->dest
->index
);
511 fprintf (stderr
, "PHI argument\n");
512 print_generic_stmt (stderr
, op
, TDF_VOPS
);
520 fprintf (stderr
, "for PHI node\n");
521 print_gimple_stmt (stderr
, phi
, 0, TDF_VOPS
|TDF_MEMSYMS
);
529 /* Verify common invariants in the SSA web.
530 TODO: verify the variable annotations. */
533 verify_ssa (bool check_modified_stmt
)
537 basic_block
*definition_block
= XCNEWVEC (basic_block
, num_ssa_names
);
540 enum dom_state orig_dom_state
= dom_info_state (CDI_DOMINATORS
);
541 bitmap names_defined_in_bb
= BITMAP_ALLOC (NULL
);
543 gcc_assert (!need_ssa_update_p (cfun
));
547 timevar_push (TV_TREE_SSA_VERIFY
);
549 /* Keep track of SSA names present in the IL. */
550 for (i
= 1; i
< num_ssa_names
; i
++)
552 tree name
= ssa_name (i
);
556 TREE_VISITED (name
) = 0;
558 stmt
= SSA_NAME_DEF_STMT (name
);
559 if (!gimple_nop_p (stmt
))
561 basic_block bb
= gimple_bb (stmt
);
562 verify_def (bb
, definition_block
,
563 name
, stmt
, !is_gimple_reg (name
));
569 calculate_dominance_info (CDI_DOMINATORS
);
571 /* Now verify all the uses and make sure they agree with the definitions
572 found in the previous pass. */
578 gimple_stmt_iterator gsi
;
580 /* Make sure that all edges have a clear 'aux' field. */
581 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
585 error ("AUX pointer initialized for edge %d->%d", e
->src
->index
,
591 /* Verify the arguments for every PHI node in the block. */
592 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
594 phi
= gsi_stmt (gsi
);
595 if (verify_phi_args (phi
, bb
, definition_block
))
598 bitmap_set_bit (names_defined_in_bb
,
599 SSA_NAME_VERSION (gimple_phi_result (phi
)));
602 /* Now verify all the uses and vuses in every statement of the block. */
603 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
605 gimple stmt
= gsi_stmt (gsi
);
609 if (check_modified_stmt
&& gimple_modified_p (stmt
))
611 error ("stmt (%p) marked modified after optimization pass: ",
613 print_gimple_stmt (stderr
, stmt
, 0, TDF_VOPS
);
617 if (is_gimple_assign (stmt
)
618 && TREE_CODE (gimple_assign_lhs (stmt
)) != SSA_NAME
)
620 tree lhs
, base_address
;
622 lhs
= gimple_assign_lhs (stmt
);
623 base_address
= get_base_address (lhs
);
626 && SSA_VAR_P (base_address
)
627 && !gimple_vdef (stmt
)
630 error ("statement makes a memory store, but has no VDEFS");
631 print_gimple_stmt (stderr
, stmt
, 0, TDF_VOPS
);
636 /* Verify the single virtual operand and its constraints. */
638 if (gimple_vdef (stmt
))
640 if (gimple_vdef_op (stmt
) == NULL_DEF_OPERAND_P
)
642 error ("statement has VDEF operand not in defs list");
645 if (!gimple_vuse (stmt
))
647 error ("statement has VDEF but no VUSE operand");
650 else if (SSA_NAME_VAR (gimple_vdef (stmt
))
651 != SSA_NAME_VAR (gimple_vuse (stmt
)))
653 error ("VDEF and VUSE do not use the same symbol");
656 has_err
|= verify_ssa_name (gimple_vdef (stmt
), true);
658 if (gimple_vuse (stmt
))
660 if (gimple_vuse_op (stmt
) == NULL_USE_OPERAND_P
)
662 error ("statement has VUSE operand not in uses list");
665 has_err
|= verify_ssa_name (gimple_vuse (stmt
), true);
669 error ("in statement");
670 print_gimple_stmt (stderr
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
);
674 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
|SSA_OP_DEF
)
676 if (verify_ssa_name (op
, false))
678 error ("in statement");
679 print_gimple_stmt (stderr
, stmt
, 0, TDF_VOPS
|TDF_MEMSYMS
);
684 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
|SSA_OP_VUSE
)
686 op
= USE_FROM_PTR (use_p
);
687 if (verify_use (bb
, definition_block
[SSA_NAME_VERSION (op
)],
688 use_p
, stmt
, false, names_defined_in_bb
))
692 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_ALL_DEFS
)
694 if (SSA_NAME_DEF_STMT (op
) != stmt
)
696 error ("SSA_NAME_DEF_STMT is wrong");
697 fprintf (stderr
, "Expected definition statement:\n");
698 print_gimple_stmt (stderr
, stmt
, 4, TDF_VOPS
);
699 fprintf (stderr
, "\nActual definition statement:\n");
700 print_gimple_stmt (stderr
, SSA_NAME_DEF_STMT (op
),
704 bitmap_set_bit (names_defined_in_bb
, SSA_NAME_VERSION (op
));
708 bitmap_clear (names_defined_in_bb
);
711 free (definition_block
);
713 /* Restore the dominance information to its prior known state, so
714 that we do not perturb the compiler's subsequent behavior. */
715 if (orig_dom_state
== DOM_NONE
)
716 free_dominance_info (CDI_DOMINATORS
);
718 set_dom_info_availability (CDI_DOMINATORS
, orig_dom_state
);
720 BITMAP_FREE (names_defined_in_bb
);
721 timevar_pop (TV_TREE_SSA_VERIFY
);
725 internal_error ("verify_ssa failed");
728 /* Return true if the uid in both int tree maps are equal. */
731 int_tree_map_eq (const void *va
, const void *vb
)
733 const struct int_tree_map
*a
= (const struct int_tree_map
*) va
;
734 const struct int_tree_map
*b
= (const struct int_tree_map
*) vb
;
735 return (a
->uid
== b
->uid
);
738 /* Hash a UID in a int_tree_map. */
741 int_tree_map_hash (const void *item
)
743 return ((const struct int_tree_map
*)item
)->uid
;
746 /* Return true if the DECL_UID in both trees are equal. */
749 uid_decl_map_eq (const void *va
, const void *vb
)
751 const_tree a
= (const_tree
) va
;
752 const_tree b
= (const_tree
) vb
;
753 return (a
->decl_minimal
.uid
== b
->decl_minimal
.uid
);
756 /* Hash a tree in a uid_decl_map. */
759 uid_decl_map_hash (const void *item
)
761 return ((const_tree
)item
)->decl_minimal
.uid
;
764 /* Return true if the DECL_UID in both trees are equal. */
767 uid_ssaname_map_eq (const void *va
, const void *vb
)
769 const_tree a
= (const_tree
) va
;
770 const_tree b
= (const_tree
) vb
;
771 return (a
->ssa_name
.var
->decl_minimal
.uid
== b
->ssa_name
.var
->decl_minimal
.uid
);
774 /* Hash a tree in a uid_decl_map. */
777 uid_ssaname_map_hash (const void *item
)
779 return ((const_tree
)item
)->ssa_name
.var
->decl_minimal
.uid
;
783 /* Initialize global DFA and SSA structures. */
786 init_tree_ssa (struct function
*fn
)
788 fn
->gimple_df
= GGC_CNEW (struct gimple_df
);
789 fn
->gimple_df
->referenced_vars
= htab_create_ggc (20, uid_decl_map_hash
,
790 uid_decl_map_eq
, NULL
);
791 fn
->gimple_df
->default_defs
= htab_create_ggc (20, uid_ssaname_map_hash
,
792 uid_ssaname_map_eq
, NULL
);
793 pt_solution_reset (&fn
->gimple_df
->escaped
);
794 pt_solution_reset (&fn
->gimple_df
->callused
);
795 init_ssanames (fn
, 0);
800 /* Deallocate memory associated with SSA data structures for FNDECL. */
803 delete_tree_ssa (void)
807 gimple_stmt_iterator gsi
;
808 referenced_var_iterator rvi
;
811 /* Release any ssa_names still in use. */
812 for (i
= 0; i
< num_ssa_names
; i
++)
814 tree var
= ssa_name (i
);
815 if (var
&& TREE_CODE (var
) == SSA_NAME
)
817 SSA_NAME_IMM_USE_NODE (var
).prev
= &(SSA_NAME_IMM_USE_NODE (var
));
818 SSA_NAME_IMM_USE_NODE (var
).next
= &(SSA_NAME_IMM_USE_NODE (var
));
820 release_ssa_name (var
);
823 /* FIXME. This may not be necessary. We will release all this
824 memory en masse in free_ssa_operands. This clearing used to be
825 necessary to avoid problems with the inliner, but it may not be
829 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
831 gimple stmt
= gsi_stmt (gsi
);
833 if (gimple_has_ops (stmt
))
835 gimple_set_def_ops (stmt
, NULL
);
836 gimple_set_use_ops (stmt
, NULL
);
839 if (gimple_has_mem_ops (stmt
))
841 gimple_set_vdef (stmt
, NULL_TREE
);
842 gimple_set_vuse (stmt
, NULL_TREE
);
845 gimple_set_modified (stmt
, true);
847 if (!(bb
->flags
& BB_RTL
))
848 set_phi_nodes (bb
, NULL
);
851 /* Remove annotations from every referenced local variable. */
852 FOR_EACH_REFERENCED_VAR (var
, rvi
)
854 if (is_global_var (var
))
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
;
866 /* We no longer maintain the SSA operand cache at this point. */
867 if (ssa_operands_active ())
868 fini_ssa_operands ();
870 delete_alias_heapvars ();
872 htab_delete (cfun
->gimple_df
->default_defs
);
873 cfun
->gimple_df
->default_defs
= NULL
;
874 pt_solution_reset (&cfun
->gimple_df
->escaped
);
875 pt_solution_reset (&cfun
->gimple_df
->callused
);
876 cfun
->gimple_df
->modified_noreturn_calls
= NULL
;
877 cfun
->gimple_df
= NULL
;
879 /* We no longer need the edge variable maps. */
880 redirect_edge_var_map_destroy ();
883 /* Helper function for useless_type_conversion_p. */
886 useless_type_conversion_p_1 (tree outer_type
, tree inner_type
)
888 /* Do the following before stripping toplevel qualifiers. */
889 if (POINTER_TYPE_P (inner_type
)
890 && POINTER_TYPE_P (outer_type
))
892 /* Do not lose casts to restrict qualified pointers. */
893 if ((TYPE_RESTRICT (outer_type
)
894 != TYPE_RESTRICT (inner_type
))
895 && TYPE_RESTRICT (outer_type
))
899 /* From now on qualifiers on value types do not matter. */
900 inner_type
= TYPE_MAIN_VARIANT (inner_type
);
901 outer_type
= TYPE_MAIN_VARIANT (outer_type
);
903 if (inner_type
== outer_type
)
906 /* If we know the canonical types, compare them. */
907 if (TYPE_CANONICAL (inner_type
)
908 && TYPE_CANONICAL (inner_type
) == TYPE_CANONICAL (outer_type
))
911 /* Changes in machine mode are never useless conversions. */
912 if (TYPE_MODE (inner_type
) != TYPE_MODE (outer_type
))
915 /* If both the inner and outer types are integral types, then the
916 conversion is not necessary if they have the same mode and
917 signedness and precision, and both or neither are boolean. */
918 if (INTEGRAL_TYPE_P (inner_type
)
919 && INTEGRAL_TYPE_P (outer_type
))
921 /* Preserve changes in signedness or precision. */
922 if (TYPE_UNSIGNED (inner_type
) != TYPE_UNSIGNED (outer_type
)
923 || TYPE_PRECISION (inner_type
) != TYPE_PRECISION (outer_type
))
926 /* We don't need to preserve changes in the types minimum or
927 maximum value in general as these do not generate code
928 unless the types precisions are different. */
932 /* Scalar floating point types with the same mode are compatible. */
933 else if (SCALAR_FLOAT_TYPE_P (inner_type
)
934 && SCALAR_FLOAT_TYPE_P (outer_type
))
937 /* We need to take special care recursing to pointed-to types. */
938 else if (POINTER_TYPE_P (inner_type
)
939 && POINTER_TYPE_P (outer_type
))
941 /* Don't lose casts between pointers to volatile and non-volatile
942 qualified types. Doing so would result in changing the semantics
943 of later accesses. For function types the volatile qualifier
944 is used to indicate noreturn functions. */
945 if (TREE_CODE (TREE_TYPE (outer_type
)) != FUNCTION_TYPE
946 && TREE_CODE (TREE_TYPE (outer_type
)) != METHOD_TYPE
947 && TREE_CODE (TREE_TYPE (inner_type
)) != FUNCTION_TYPE
948 && TREE_CODE (TREE_TYPE (inner_type
)) != METHOD_TYPE
949 && (TYPE_VOLATILE (TREE_TYPE (outer_type
))
950 != TYPE_VOLATILE (TREE_TYPE (inner_type
)))
951 && TYPE_VOLATILE (TREE_TYPE (outer_type
)))
954 /* Do not lose casts between pointers that when dereferenced access
955 memory with different alias sets. */
956 if (get_deref_alias_set (inner_type
) != get_deref_alias_set (outer_type
))
959 /* We do not care for const qualification of the pointed-to types
960 as const qualification has no semantic value to the middle-end. */
962 /* Otherwise pointers/references are equivalent if their pointed
963 to types are effectively the same. We can strip qualifiers
964 on pointed-to types for further comparison, which is done in
966 return useless_type_conversion_p_1 (TREE_TYPE (outer_type
),
967 TREE_TYPE (inner_type
));
970 /* Recurse for complex types. */
971 else if (TREE_CODE (inner_type
) == COMPLEX_TYPE
972 && TREE_CODE (outer_type
) == COMPLEX_TYPE
)
973 return useless_type_conversion_p (TREE_TYPE (outer_type
),
974 TREE_TYPE (inner_type
));
976 /* Recurse for vector types with the same number of subparts. */
977 else if (TREE_CODE (inner_type
) == VECTOR_TYPE
978 && TREE_CODE (outer_type
) == VECTOR_TYPE
979 && TYPE_PRECISION (inner_type
) == TYPE_PRECISION (outer_type
))
980 return useless_type_conversion_p (TREE_TYPE (outer_type
),
981 TREE_TYPE (inner_type
));
983 /* For aggregates we may need to fall back to structural equality
985 else if (AGGREGATE_TYPE_P (inner_type
)
986 && AGGREGATE_TYPE_P (outer_type
))
988 /* Different types of aggregates are incompatible. */
989 if (TREE_CODE (inner_type
) != TREE_CODE (outer_type
))
992 /* Conversions from array types with unknown extent to
993 array types with known extent are not useless. */
994 if (TREE_CODE (inner_type
) == ARRAY_TYPE
995 && !TYPE_DOMAIN (inner_type
)
996 && TYPE_DOMAIN (outer_type
))
999 /* ??? This seems to be necessary even for aggregates that don't
1000 have TYPE_STRUCTURAL_EQUALITY_P set. */
1002 /* ??? This should eventually just return false. */
1003 return lang_hooks
.types_compatible_p (inner_type
, outer_type
);
1005 /* Also for functions and possibly other types with
1006 TYPE_STRUCTURAL_EQUALITY_P set. */
1007 else if (TYPE_STRUCTURAL_EQUALITY_P (inner_type
)
1008 && TYPE_STRUCTURAL_EQUALITY_P (outer_type
))
1009 return lang_hooks
.types_compatible_p (inner_type
, outer_type
);
1014 /* Return true if the conversion from INNER_TYPE to OUTER_TYPE is a
1015 useless type conversion, otherwise return false.
1017 This function implicitly defines the middle-end type system. With
1018 the notion of 'a < b' meaning that useless_type_conversion_p (a, b)
1019 holds and 'a > b' meaning that useless_type_conversion_p (b, a) holds,
1020 the following invariants shall be fulfilled:
1022 1) useless_type_conversion_p is transitive.
1023 If a < b and b < c then a < c.
1025 2) useless_type_conversion_p is not symmetric.
1026 From a < b does not follow a > b.
1028 3) Types define the available set of operations applicable to values.
1029 A type conversion is useless if the operations for the target type
1030 is a subset of the operations for the source type. For example
1031 casts to void* are useless, casts from void* are not (void* can't
1032 be dereferenced or offsetted, but copied, hence its set of operations
1033 is a strict subset of that of all other data pointer types). Casts
1034 to const T* are useless (can't be written to), casts from const T*
1038 useless_type_conversion_p (tree outer_type
, tree inner_type
)
1040 /* If the outer type is (void *), then the conversion is not
1041 necessary. We have to make sure to not apply this while
1042 recursing though. */
1043 if (POINTER_TYPE_P (inner_type
)
1044 && POINTER_TYPE_P (outer_type
)
1045 && TREE_CODE (TREE_TYPE (outer_type
)) == VOID_TYPE
)
1048 return useless_type_conversion_p_1 (outer_type
, inner_type
);
1051 /* Return true if a conversion from either type of TYPE1 and TYPE2
1052 to the other is not required. Otherwise return false. */
1055 types_compatible_p (tree type1
, tree type2
)
1057 return (type1
== type2
1058 || (useless_type_conversion_p (type1
, type2
)
1059 && useless_type_conversion_p (type2
, type1
)));
1062 /* Return true if EXPR is a useless type conversion, otherwise return
1066 tree_ssa_useless_type_conversion (tree expr
)
1068 /* If we have an assignment that merely uses a NOP_EXPR to change
1069 the top of the RHS to the type of the LHS and the type conversion
1070 is "safe", then strip away the type conversion so that we can
1071 enter LHS = RHS into the const_and_copies table. */
1072 if (CONVERT_EXPR_P (expr
)
1073 || TREE_CODE (expr
) == VIEW_CONVERT_EXPR
1074 || TREE_CODE (expr
) == NON_LVALUE_EXPR
)
1075 return useless_type_conversion_p
1077 TREE_TYPE (TREE_OPERAND (expr
, 0)));
1082 /* Strip conversions from EXP according to
1083 tree_ssa_useless_type_conversion and return the resulting
1087 tree_ssa_strip_useless_type_conversions (tree exp
)
1089 while (tree_ssa_useless_type_conversion (exp
))
1090 exp
= TREE_OPERAND (exp
, 0);
1095 /* Internal helper for walk_use_def_chains. VAR, FN and DATA are as
1096 described in walk_use_def_chains.
1098 VISITED is a pointer set used to mark visited SSA_NAMEs to avoid
1099 infinite loops. We used to have a bitmap for this to just mark
1100 SSA versions we had visited. But non-sparse bitmaps are way too
1101 expensive, while sparse bitmaps may cause quadratic behavior.
1103 IS_DFS is true if the caller wants to perform a depth-first search
1104 when visiting PHI nodes. A DFS will visit each PHI argument and
1105 call FN after each one. Otherwise, all the arguments are
1106 visited first and then FN is called with each of the visited
1107 arguments in a separate pass. */
1110 walk_use_def_chains_1 (tree var
, walk_use_def_chains_fn fn
, void *data
,
1111 struct pointer_set_t
*visited
, bool is_dfs
)
1115 if (pointer_set_insert (visited
, var
))
1118 def_stmt
= SSA_NAME_DEF_STMT (var
);
1120 if (gimple_code (def_stmt
) != GIMPLE_PHI
)
1122 /* If we reached the end of the use-def chain, call FN. */
1123 return fn (var
, def_stmt
, data
);
1129 /* When doing a breadth-first search, call FN before following the
1130 use-def links for each argument. */
1132 for (i
= 0; i
< gimple_phi_num_args (def_stmt
); i
++)
1133 if (fn (gimple_phi_arg_def (def_stmt
, i
), def_stmt
, data
))
1136 /* Follow use-def links out of each PHI argument. */
1137 for (i
= 0; i
< gimple_phi_num_args (def_stmt
); i
++)
1139 tree arg
= gimple_phi_arg_def (def_stmt
, i
);
1141 /* ARG may be NULL for newly introduced PHI nodes. */
1143 && TREE_CODE (arg
) == SSA_NAME
1144 && walk_use_def_chains_1 (arg
, fn
, data
, visited
, is_dfs
))
1148 /* When doing a depth-first search, call FN after following the
1149 use-def links for each argument. */
1151 for (i
= 0; i
< gimple_phi_num_args (def_stmt
); i
++)
1152 if (fn (gimple_phi_arg_def (def_stmt
, i
), def_stmt
, data
))
1161 /* Walk use-def chains starting at the SSA variable VAR. Call
1162 function FN at each reaching definition found. FN takes three
1163 arguments: VAR, its defining statement (DEF_STMT) and a generic
1164 pointer to whatever state information that FN may want to maintain
1165 (DATA). FN is able to stop the walk by returning true, otherwise
1166 in order to continue the walk, FN should return false.
1168 Note, that if DEF_STMT is a PHI node, the semantics are slightly
1169 different. The first argument to FN is no longer the original
1170 variable VAR, but the PHI argument currently being examined. If FN
1171 wants to get at VAR, it should call PHI_RESULT (PHI).
1173 If IS_DFS is true, this function will:
1175 1- walk the use-def chains for all the PHI arguments, and,
1176 2- call (*FN) (ARG, PHI, DATA) on all the PHI arguments.
1178 If IS_DFS is false, the two steps above are done in reverse order
1179 (i.e., a breadth-first search). */
1182 walk_use_def_chains (tree var
, walk_use_def_chains_fn fn
, void *data
,
1187 gcc_assert (TREE_CODE (var
) == SSA_NAME
);
1189 def_stmt
= SSA_NAME_DEF_STMT (var
);
1191 /* We only need to recurse if the reaching definition comes from a PHI
1193 if (gimple_code (def_stmt
) != GIMPLE_PHI
)
1194 (*fn
) (var
, def_stmt
, data
);
1197 struct pointer_set_t
*visited
= pointer_set_create ();
1198 walk_use_def_chains_1 (var
, fn
, data
, visited
, is_dfs
);
1199 pointer_set_destroy (visited
);
1204 /* Return true if T, an SSA_NAME, has an undefined value. */
1207 ssa_undefined_value_p (tree t
)
1209 tree var
= SSA_NAME_VAR (t
);
1211 /* Parameters get their initial value from the function entry. */
1212 if (TREE_CODE (var
) == PARM_DECL
)
1215 /* Hard register variables get their initial value from the ether. */
1216 if (TREE_CODE (var
) == VAR_DECL
&& DECL_HARD_REGISTER (var
))
1219 /* The value is undefined iff its definition statement is empty. */
1220 return gimple_nop_p (SSA_NAME_DEF_STMT (t
));
1223 /* Emit warnings for uninitialized variables. This is done in two passes.
1225 The first pass notices real uses of SSA names with undefined values.
1226 Such uses are unconditionally uninitialized, and we can be certain that
1227 such a use is a mistake. This pass is run before most optimizations,
1228 so that we catch as many as we can.
1230 The second pass follows PHI nodes to find uses that are potentially
1231 uninitialized. In this case we can't necessarily prove that the use
1232 is really uninitialized. This pass is run after most optimizations,
1233 so that we thread as many jumps and possible, and delete as much dead
1234 code as possible, in order to reduce false positives. We also look
1235 again for plain uninitialized variables, since optimization may have
1236 changed conditionally uninitialized to unconditionally uninitialized. */
1238 /* Emit a warning for T, an SSA_NAME, being uninitialized. The exact
1239 warning text is in MSGID and LOCUS may contain a location or be null. */
1242 warn_uninit (tree t
, const char *gmsgid
, void *data
)
1244 tree var
= SSA_NAME_VAR (t
);
1245 gimple context
= (gimple
) data
;
1246 location_t location
;
1247 expanded_location xloc
, floc
;
1249 if (!ssa_undefined_value_p (t
))
1252 /* TREE_NO_WARNING either means we already warned, or the front end
1253 wishes to suppress the warning. */
1254 if (TREE_NO_WARNING (var
))
1257 /* Do not warn if it can be initialized outside this module. */
1258 if (is_global_var (var
))
1261 location
= (context
!= NULL
&& gimple_has_location (context
))
1262 ? gimple_location (context
)
1263 : DECL_SOURCE_LOCATION (var
);
1264 xloc
= expand_location (location
);
1265 floc
= expand_location (DECL_SOURCE_LOCATION (cfun
->decl
));
1266 if (warning_at (location
, OPT_Wuninitialized
, gmsgid
, var
))
1268 TREE_NO_WARNING (var
) = 1;
1270 if (xloc
.file
!= floc
.file
1271 || xloc
.line
< floc
.line
1272 || xloc
.line
> LOCATION_LINE (cfun
->function_end_locus
))
1273 inform (location
, "%J%qD was declared here", var
, var
);
1279 bool always_executed
;
1280 bool warn_possibly_uninitialized
;
1283 /* Called via walk_tree, look for SSA_NAMEs that have empty definitions
1284 and warn about them. */
1287 warn_uninitialized_var (tree
*tp
, int *walk_subtrees
, void *data_
)
1289 struct walk_stmt_info
*wi
= (struct walk_stmt_info
*) data_
;
1290 struct walk_data
*data
= (struct walk_data
*) wi
->info
;
1293 /* We do not care about LHS. */
1297 switch (TREE_CODE (t
))
1300 /* Taking the address of an uninitialized variable does not
1301 count as using it. */
1307 /* A VAR_DECL in the RHS of a gimple statement may mean that
1308 this variable is loaded from memory. */
1312 /* If there is not gimple stmt,
1313 or alias information has not been computed,
1314 then we cannot check VUSE ops. */
1315 if (data
->stmt
== NULL
)
1318 /* If the load happens as part of a call do not warn about it. */
1319 if (is_gimple_call (data
->stmt
))
1322 vuse
= gimple_vuse_op (data
->stmt
);
1323 if (vuse
== NULL_USE_OPERAND_P
)
1326 op
= USE_FROM_PTR (vuse
);
1327 if (t
!= SSA_NAME_VAR (op
)
1328 || !SSA_NAME_IS_DEFAULT_DEF (op
))
1330 /* If this is a VUSE of t and it is the default definition,
1331 then warn about op. */
1333 /* Fall through into SSA_NAME. */
1337 /* We only do data flow with SSA_NAMEs, so that's all we
1339 if (data
->always_executed
)
1340 warn_uninit (t
, "%qD is used uninitialized in this function",
1342 else if (data
->warn_possibly_uninitialized
)
1343 warn_uninit (t
, "%qD may be used uninitialized in this function",
1350 /* The total store transformation performed during gimplification
1351 creates uninitialized variable uses. If all is well, these will
1352 be optimized away, so don't warn now. */
1353 if (TREE_CODE (TREE_OPERAND (t
, 0)) == SSA_NAME
)
1358 if (IS_TYPE_OR_DECL_P (t
))
1366 /* Look for inputs to PHI that are SSA_NAMEs that have empty definitions
1367 and warn about them. */
1370 warn_uninitialized_phi (gimple phi
)
1372 size_t i
, n
= gimple_phi_num_args (phi
);
1374 /* Don't look at memory tags. */
1375 if (!is_gimple_reg (gimple_phi_result (phi
)))
1378 for (i
= 0; i
< n
; ++i
)
1380 tree op
= gimple_phi_arg_def (phi
, i
);
1381 if (TREE_CODE (op
) == SSA_NAME
)
1382 warn_uninit (op
, "%qD may be used uninitialized in this function",
1388 warn_uninitialized_vars (bool warn_possibly_uninitialized
)
1390 gimple_stmt_iterator gsi
;
1392 struct walk_data data
;
1394 data
.warn_possibly_uninitialized
= warn_possibly_uninitialized
;
1396 calculate_dominance_info (CDI_POST_DOMINATORS
);
1400 data
.always_executed
= dominated_by_p (CDI_POST_DOMINATORS
,
1401 single_succ (ENTRY_BLOCK_PTR
), bb
);
1402 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1404 struct walk_stmt_info wi
;
1405 data
.stmt
= gsi_stmt (gsi
);
1406 memset (&wi
, 0, sizeof (wi
));
1408 walk_gimple_op (gsi_stmt (gsi
), warn_uninitialized_var
, &wi
);
1412 /* Post-dominator information can not be reliably updated. Free it
1415 free_dominance_info (CDI_POST_DOMINATORS
);
1420 execute_early_warn_uninitialized (void)
1422 /* Currently, this pass runs always but
1423 execute_late_warn_uninitialized only runs with optimization. With
1424 optimization we want to warn about possible uninitialized as late
1425 as possible, thus don't do it here. However, without
1426 optimization we need to warn here about "may be uninitialized".
1428 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/!optimize
);
1433 execute_late_warn_uninitialized (void)
1436 gimple_stmt_iterator gsi
;
1438 /* Re-do the plain uninitialized variable check, as optimization may have
1439 straightened control flow. Do this first so that we don't accidentally
1440 get a "may be" warning when we'd have seen an "is" warning later. */
1441 warn_uninitialized_vars (/*warn_possibly_uninitialized=*/1);
1444 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1445 warn_uninitialized_phi (gsi_stmt (gsi
));
1451 gate_warn_uninitialized (void)
1453 return warn_uninitialized
!= 0;
1456 struct gimple_opt_pass pass_early_warn_uninitialized
=
1461 gate_warn_uninitialized
, /* gate */
1462 execute_early_warn_uninitialized
, /* execute */
1465 0, /* static_pass_number */
1466 TV_NONE
, /* tv_id */
1467 PROP_ssa
, /* properties_required */
1468 0, /* properties_provided */
1469 0, /* properties_destroyed */
1470 0, /* todo_flags_start */
1471 0 /* todo_flags_finish */
1475 struct gimple_opt_pass pass_late_warn_uninitialized
=
1480 gate_warn_uninitialized
, /* gate */
1481 execute_late_warn_uninitialized
, /* execute */
1484 0, /* static_pass_number */
1485 TV_NONE
, /* tv_id */
1486 PROP_ssa
, /* properties_required */
1487 0, /* properties_provided */
1488 0, /* properties_destroyed */
1489 0, /* todo_flags_start */
1490 0 /* todo_flags_finish */
1494 /* Compute TREE_ADDRESSABLE and DECL_GIMPLE_REG_P for local variables. */
1497 execute_update_addresses_taken (bool do_optimize
)
1500 referenced_var_iterator rvi
;
1501 gimple_stmt_iterator gsi
;
1503 bitmap addresses_taken
= BITMAP_ALLOC (NULL
);
1504 bitmap not_reg_needs
= BITMAP_ALLOC (NULL
);
1505 bool update_vops
= false;
1507 /* Collect into ADDRESSES_TAKEN all variables whose address is taken within
1508 the function body. */
1511 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1513 gimple stmt
= gsi_stmt (gsi
);
1514 enum gimple_code code
= gimple_code (stmt
);
1516 /* Note all addresses taken by the stmt. */
1517 gimple_ior_addresses_taken (addresses_taken
, stmt
);
1519 /* If we have a call or an assignment, see if the lhs contains
1520 a local decl that requires not to be a gimple register. */
1521 if (code
== GIMPLE_ASSIGN
|| code
== GIMPLE_CALL
)
1523 tree lhs
= gimple_get_lhs (stmt
);
1525 /* We may not rewrite TMR_SYMBOL to SSA. */
1526 if (lhs
&& TREE_CODE (lhs
) == TARGET_MEM_REF
1527 && TMR_SYMBOL (lhs
))
1528 bitmap_set_bit (not_reg_needs
, DECL_UID (TMR_SYMBOL (lhs
)));
1530 /* A plain decl does not need it set. */
1531 else if (lhs
&& handled_component_p (lhs
))
1533 var
= get_base_address (lhs
);
1535 bitmap_set_bit (not_reg_needs
, DECL_UID (var
));
1540 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1543 gimple phi
= gsi_stmt (gsi
);
1545 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
1547 tree op
= PHI_ARG_DEF (phi
, i
), var
;
1548 if (TREE_CODE (op
) == ADDR_EXPR
1549 && (var
= get_base_address (TREE_OPERAND (op
, 0))) != NULL
1551 bitmap_set_bit (addresses_taken
, DECL_UID (var
));
1556 /* When possible, clear ADDRESSABLE bit or set the REGISTER bit
1557 and mark variable for conversion into SSA. */
1558 if (optimize
&& do_optimize
)
1559 FOR_EACH_REFERENCED_VAR (var
, rvi
)
1561 /* Global Variables, result decls cannot be changed. */
1562 if (is_global_var (var
)
1563 || TREE_CODE (var
) == RESULT_DECL
1564 || bitmap_bit_p (addresses_taken
, DECL_UID (var
)))
1567 if (TREE_ADDRESSABLE (var
)
1568 /* Do not change TREE_ADDRESSABLE if we need to preserve var as
1569 a non-register. Otherwise we are confused and forget to
1570 add virtual operands for it. */
1571 && (!is_gimple_reg_type (TREE_TYPE (var
))
1572 || !bitmap_bit_p (not_reg_needs
, DECL_UID (var
))))
1574 TREE_ADDRESSABLE (var
) = 0;
1575 if (is_gimple_reg (var
))
1576 mark_sym_for_renaming (var
);
1580 fprintf (dump_file
, "No longer having address taken ");
1581 print_generic_expr (dump_file
, var
, 0);
1582 fprintf (dump_file
, "\n");
1585 if (!DECL_GIMPLE_REG_P (var
)
1586 && !bitmap_bit_p (not_reg_needs
, DECL_UID (var
))
1587 && (TREE_CODE (TREE_TYPE (var
)) == COMPLEX_TYPE
1588 || TREE_CODE (TREE_TYPE (var
)) == VECTOR_TYPE
)
1589 && !TREE_THIS_VOLATILE (var
)
1590 && (TREE_CODE (var
) != VAR_DECL
|| !DECL_HARD_REGISTER (var
)))
1592 DECL_GIMPLE_REG_P (var
) = 1;
1593 mark_sym_for_renaming (var
);
1597 fprintf (dump_file
, "Decl is now a gimple register ");
1598 print_generic_expr (dump_file
, var
, 0);
1599 fprintf (dump_file
, "\n");
1604 /* Operand caches needs to be recomputed for operands referencing the updated
1609 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1611 gimple stmt
= gsi_stmt (gsi
);
1613 if (gimple_references_memory_p (stmt
))
1617 /* Update SSA form here, we are called as non-pass as well. */
1618 update_ssa (TODO_update_ssa
);
1621 BITMAP_FREE (not_reg_needs
);
1622 BITMAP_FREE (addresses_taken
);
1625 struct gimple_opt_pass pass_update_address_taken
=
1629 "addressables", /* name */
1634 0, /* static_pass_number */
1635 TV_NONE
, /* tv_id */
1636 PROP_ssa
, /* properties_required */
1637 0, /* properties_provided */
1638 0, /* properties_destroyed */
1639 0, /* todo_flags_start */
1640 TODO_update_address_taken
1641 | TODO_dump_func
/* todo_flags_finish */