1 /* Interprocedural Identical Code Folding pass
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <hubicka@ucw.cz> and Martin Liska <mliska@suse.cz>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "fold-const.h"
33 #include "hard-reg-set.h"
35 #include "basic-block.h"
36 #include "tree-ssa-alias.h"
37 #include "internal-fn.h"
38 #include "gimple-expr.h"
43 #include "insn-config.h"
52 #include "gimple-iterator.h"
53 #include "gimple-ssa.h"
55 #include "stringpool.h"
57 #include "tree-pass.h"
58 #include "gimple-pretty-print.h"
61 #include "plugin-api.h"
64 #include "data-streamer.h"
65 #include "ipa-utils.h"
67 #include "tree-ssanames.h"
71 #include "ipa-icf-gimple.h"
74 namespace ipa_icf_gimple
{
76 /* Initialize internal structures for a given SOURCE_FUNC_DECL and
77 TARGET_FUNC_DECL. Strict polymorphic comparison is processed if
78 an option COMPARE_POLYMORPHIC is true. For special cases, one can
79 set IGNORE_LABELS to skip label comparison.
80 Similarly, IGNORE_SOURCE_DECLS and IGNORE_TARGET_DECLS are sets
81 of declarations that can be skipped. */
83 func_checker::func_checker (tree source_func_decl
, tree target_func_decl
,
84 bool compare_polymorphic
,
86 hash_set
<symtab_node
*> *ignored_source_nodes
,
87 hash_set
<symtab_node
*> *ignored_target_nodes
)
88 : m_source_func_decl (source_func_decl
), m_target_func_decl (target_func_decl
),
89 m_ignored_source_nodes (ignored_source_nodes
),
90 m_ignored_target_nodes (ignored_target_nodes
),
91 m_compare_polymorphic (compare_polymorphic
),
92 m_ignore_labels (ignore_labels
)
94 function
*source_func
= DECL_STRUCT_FUNCTION (source_func_decl
);
95 function
*target_func
= DECL_STRUCT_FUNCTION (target_func_decl
);
97 unsigned ssa_source
= SSANAMES (source_func
)->length ();
98 unsigned ssa_target
= SSANAMES (target_func
)->length ();
100 m_source_ssa_names
.create (ssa_source
);
101 m_target_ssa_names
.create (ssa_target
);
103 for (unsigned i
= 0; i
< ssa_source
; i
++)
104 m_source_ssa_names
.safe_push (-1);
106 for (unsigned i
= 0; i
< ssa_target
; i
++)
107 m_target_ssa_names
.safe_push (-1);
110 /* Memory release routine. */
112 func_checker::~func_checker ()
114 m_source_ssa_names
.release();
115 m_target_ssa_names
.release();
118 /* Verifies that trees T1 and T2 are equivalent from perspective of ICF. */
121 func_checker::compare_ssa_name (tree t1
, tree t2
)
123 gcc_assert (TREE_CODE (t1
) == SSA_NAME
);
124 gcc_assert (TREE_CODE (t2
) == SSA_NAME
);
126 unsigned i1
= SSA_NAME_VERSION (t1
);
127 unsigned i2
= SSA_NAME_VERSION (t2
);
129 if (m_source_ssa_names
[i1
] == -1)
130 m_source_ssa_names
[i1
] = i2
;
131 else if (m_source_ssa_names
[i1
] != (int) i2
)
134 if(m_target_ssa_names
[i2
] == -1)
135 m_target_ssa_names
[i2
] = i1
;
136 else if (m_target_ssa_names
[i2
] != (int) i1
)
139 if (SSA_NAME_IS_DEFAULT_DEF (t1
))
141 tree b1
= SSA_NAME_VAR (t1
);
142 tree b2
= SSA_NAME_VAR (t2
);
144 if (b1
== NULL
&& b2
== NULL
)
147 if (b1
== NULL
|| b2
== NULL
|| TREE_CODE (b1
) != TREE_CODE (b2
))
148 return return_false ();
150 return compare_cst_or_decl (b1
, b2
);
156 /* Verification function for edges E1 and E2. */
159 func_checker::compare_edge (edge e1
, edge e2
)
161 if (e1
->flags
!= e2
->flags
)
166 edge
&slot
= m_edge_map
.get_or_insert (e1
, &existed_p
);
168 return return_with_debug (slot
== e2
);
172 /* TODO: filter edge probabilities for profile feedback match. */
177 /* Verification function for declaration trees T1 and T2 that
178 come from functions FUNC1 and FUNC2. */
181 func_checker::compare_decl (tree t1
, tree t2
)
183 if (!auto_var_in_fn_p (t1
, m_source_func_decl
)
184 || !auto_var_in_fn_p (t2
, m_target_func_decl
))
185 return return_with_debug (t1
== t2
);
187 tree_code t
= TREE_CODE (t1
);
188 if ((t
== VAR_DECL
|| t
== PARM_DECL
|| t
== RESULT_DECL
)
189 && DECL_BY_REFERENCE (t1
) != DECL_BY_REFERENCE (t2
))
190 return return_false_with_msg ("DECL_BY_REFERENCE flags are different");
192 if (!compatible_types_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
193 return return_false ();
195 /* TODO: we are actually too strict here. We only need to compare if
196 T1 can be used in polymorphic call. */
197 if (TREE_ADDRESSABLE (t1
)
198 && m_compare_polymorphic
199 && !compatible_polymorphic_types_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
201 return return_false ();
203 if ((t
== VAR_DECL
|| t
== PARM_DECL
|| t
== RESULT_DECL
)
204 && DECL_BY_REFERENCE (t1
)
205 && m_compare_polymorphic
206 && !compatible_polymorphic_types_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
208 return return_false ();
212 tree
&slot
= m_decl_map
.get_or_insert (t1
, &existed_p
);
214 return return_with_debug (slot
== t2
);
221 /* Return true if T1 and T2 are same for purposes of ipa-polymorphic-call
222 analysis. COMPARE_PTR indicates if types of pointers needs to be
226 func_checker::compatible_polymorphic_types_p (tree t1
, tree t2
,
229 gcc_assert (TREE_CODE (t1
) != FUNCTION_TYPE
&& TREE_CODE (t1
) != METHOD_TYPE
);
231 /* Pointer types generally give no information. */
232 if (POINTER_TYPE_P (t1
))
236 return func_checker::compatible_polymorphic_types_p (TREE_TYPE (t1
),
241 /* If types contain a polymorphic types, match them. */
242 bool c1
= contains_polymorphic_type_p (t1
);
243 bool c2
= contains_polymorphic_type_p (t2
);
247 return return_false_with_msg ("one type is not polymorphic");
248 if (!types_must_be_same_for_odr (t1
, t2
))
249 return return_false_with_msg ("types are not same for ODR");
253 /* Return true if types are compatible from perspective of ICF. */
255 func_checker::compatible_types_p (tree t1
, tree t2
)
257 if (TREE_CODE (t1
) != TREE_CODE (t2
))
258 return return_false_with_msg ("different tree types");
260 if (TYPE_RESTRICT (t1
) != TYPE_RESTRICT (t2
))
261 return return_false_with_msg ("restrict flags are different");
263 if (!types_compatible_p (t1
, t2
))
264 return return_false_with_msg ("types are not compatible");
266 if (get_alias_set (t1
) != get_alias_set (t2
))
267 return return_false_with_msg ("alias sets are different");
272 /* Function compare for equality given memory operands T1 and T2. */
275 func_checker::compare_memory_operand (tree t1
, tree t2
)
283 ao_ref_init (&r1
, t1
);
284 ao_ref_init (&r2
, t2
);
286 tree b1
= ao_ref_base (&r1
);
287 tree b2
= ao_ref_base (&r2
);
289 bool source_is_memop
= DECL_P (b1
) || INDIRECT_REF_P (b1
)
290 || TREE_CODE (b1
) == MEM_REF
291 || TREE_CODE (b1
) == TARGET_MEM_REF
;
293 bool target_is_memop
= DECL_P (b2
) || INDIRECT_REF_P (b2
)
294 || TREE_CODE (b2
) == MEM_REF
295 || TREE_CODE (b2
) == TARGET_MEM_REF
;
297 /* Compare alias sets for memory operands. */
298 if (source_is_memop
&& target_is_memop
)
300 if (TREE_THIS_VOLATILE (t1
) != TREE_THIS_VOLATILE (t2
))
301 return return_false_with_msg ("different operand volatility");
303 if (ao_ref_alias_set (&r1
) != ao_ref_alias_set (&r2
)
304 || ao_ref_base_alias_set (&r1
) != ao_ref_base_alias_set (&r2
))
305 return return_false_with_msg ("ao alias sets are different");
307 /* We can't simply use get_object_alignment_1 on the full
308 reference as for accesses with variable indexes this reports
309 too conservative alignment. We also can't use the ao_ref_base
310 base objects as ao_ref_base happily strips MEM_REFs around
311 decls even though that may carry alignment info. */
313 while (handled_component_p (b1
))
314 b1
= TREE_OPERAND (b1
, 0);
316 while (handled_component_p (b2
))
317 b2
= TREE_OPERAND (b2
, 0);
318 unsigned int align1
, align2
;
319 unsigned HOST_WIDE_INT tem
;
320 get_object_alignment_1 (b1
, &align1
, &tem
);
321 get_object_alignment_1 (b2
, &align2
, &tem
);
322 if (align1
!= align2
)
323 return return_false_with_msg ("different access alignment");
325 /* Similarly we have to compare dependence info where equality
326 tells us we are safe (even some unequal values would be safe
327 but then we have to maintain a map of bases and cliques). */
328 unsigned short clique1
= 0, base1
= 0, clique2
= 0, base2
= 0;
329 if (TREE_CODE (b1
) == MEM_REF
)
331 clique1
= MR_DEPENDENCE_CLIQUE (b1
);
332 base1
= MR_DEPENDENCE_BASE (b1
);
334 if (TREE_CODE (b2
) == MEM_REF
)
336 clique2
= MR_DEPENDENCE_CLIQUE (b2
);
337 base2
= MR_DEPENDENCE_BASE (b2
);
339 if (clique1
!= clique2
|| base1
!= base2
)
340 return return_false_with_msg ("different dependence info");
343 return compare_operand (t1
, t2
);
346 /* Function compare for equality given trees T1 and T2 which
347 can be either a constant or a declaration type. */
350 func_checker::compare_cst_or_decl (tree t1
, tree t2
)
354 switch (TREE_CODE (t1
))
362 ret
= compatible_types_p (TREE_TYPE (t1
), TREE_TYPE (t2
))
363 && operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
364 return return_with_debug (ret
);
367 /* All function decls are in the symbol table and known to match
368 before we start comparing bodies. */
371 return return_with_debug (compare_variable_decl (t1
, t2
));
374 tree offset1
= DECL_FIELD_OFFSET (t1
);
375 tree offset2
= DECL_FIELD_OFFSET (t2
);
377 tree bit_offset1
= DECL_FIELD_BIT_OFFSET (t1
);
378 tree bit_offset2
= DECL_FIELD_BIT_OFFSET (t2
);
380 ret
= compare_operand (offset1
, offset2
)
381 && compare_operand (bit_offset1
, bit_offset2
);
383 return return_with_debug (ret
);
387 int *bb1
= m_label_bb_map
.get (t1
);
388 int *bb2
= m_label_bb_map
.get (t2
);
390 return return_with_debug (*bb1
== *bb2
);
396 ret
= compare_decl (t1
, t2
);
397 return return_with_debug (ret
);
404 /* Function responsible for comparison of various operands T1 and T2.
405 If these components, from functions FUNC1 and FUNC2, are equal, true
409 func_checker::compare_operand (tree t1
, tree t2
)
411 tree x1
, x2
, y1
, y2
, z1
, z2
;
419 tree tt1
= TREE_TYPE (t1
);
420 tree tt2
= TREE_TYPE (t2
);
422 if (!func_checker::compatible_types_p (tt1
, tt2
))
425 if (TREE_CODE (t1
) != TREE_CODE (t2
))
426 return return_false ();
428 switch (TREE_CODE (t1
))
432 unsigned length1
= vec_safe_length (CONSTRUCTOR_ELTS (t1
));
433 unsigned length2
= vec_safe_length (CONSTRUCTOR_ELTS (t2
));
435 if (length1
!= length2
)
436 return return_false ();
438 for (unsigned i
= 0; i
< length1
; i
++)
439 if (!compare_operand (CONSTRUCTOR_ELT (t1
, i
)->value
,
440 CONSTRUCTOR_ELT (t2
, i
)->value
))
441 return return_false();
446 case ARRAY_RANGE_REF
:
447 /* First argument is the array, second is the index. */
448 x1
= TREE_OPERAND (t1
, 0);
449 x2
= TREE_OPERAND (t2
, 0);
450 y1
= TREE_OPERAND (t1
, 1);
451 y2
= TREE_OPERAND (t2
, 1);
453 if (!compare_operand (array_ref_low_bound (t1
),
454 array_ref_low_bound (t2
)))
455 return return_false_with_msg ("");
456 if (!compare_operand (array_ref_element_size (t1
),
457 array_ref_element_size (t2
)))
458 return return_false_with_msg ("");
460 if (!compare_operand (x1
, x2
))
461 return return_false_with_msg ("");
462 return compare_operand (y1
, y2
);
465 x1
= TREE_OPERAND (t1
, 0);
466 x2
= TREE_OPERAND (t2
, 0);
467 y1
= TREE_OPERAND (t1
, 1);
468 y2
= TREE_OPERAND (t2
, 1);
470 /* See if operand is an memory access (the test originate from
473 In this case the alias set of the function being replaced must
474 be subset of the alias set of the other function. At the moment
475 we seek for equivalency classes, so simply require inclussion in
478 if (!func_checker::compatible_types_p (TREE_TYPE (x1
), TREE_TYPE (x2
)))
479 return return_false ();
481 if (!compare_operand (x1
, x2
))
482 return return_false_with_msg ("");
484 /* Type of the offset on MEM_REF does not matter. */
485 return wi::to_offset (y1
) == wi::to_offset (y2
);
489 x1
= TREE_OPERAND (t1
, 0);
490 x2
= TREE_OPERAND (t2
, 0);
491 y1
= TREE_OPERAND (t1
, 1);
492 y2
= TREE_OPERAND (t2
, 1);
494 ret
= compare_operand (x1
, x2
)
495 && compare_cst_or_decl (y1
, y2
);
497 return return_with_debug (ret
);
499 /* Virtual table call. */
502 if (!compare_ssa_name (OBJ_TYPE_REF_EXPR (t1
), OBJ_TYPE_REF_EXPR (t2
)))
503 return return_false ();
504 if (opt_for_fn (m_source_func_decl
, flag_devirtualize
)
505 && virtual_method_call_p (t1
))
507 if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (t1
))
508 != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (t2
)))
509 return return_false_with_msg ("OBJ_TYPE_REF token mismatch");
510 if (!types_same_for_odr (obj_type_ref_class (t1
),
511 obj_type_ref_class (t2
)))
512 return return_false_with_msg ("OBJ_TYPE_REF OTR type mismatch");
513 if (!compare_operand (OBJ_TYPE_REF_OBJECT (t1
),
514 OBJ_TYPE_REF_OBJECT (t2
)))
515 return return_false_with_msg ("OBJ_TYPE_REF object mismatch");
518 return return_with_debug (true);
524 x1
= TREE_OPERAND (t1
, 0);
525 x2
= TREE_OPERAND (t2
, 0);
527 ret
= compare_operand (x1
, x2
);
528 return return_with_debug (ret
);
532 x1
= TREE_OPERAND (t1
, 0);
533 x2
= TREE_OPERAND (t2
, 0);
534 y1
= TREE_OPERAND (t1
, 1);
535 y2
= TREE_OPERAND (t2
, 1);
536 z1
= TREE_OPERAND (t1
, 2);
537 z2
= TREE_OPERAND (t2
, 2);
539 ret
= compare_operand (x1
, x2
)
540 && compare_cst_or_decl (y1
, y2
)
541 && compare_cst_or_decl (z1
, z2
);
543 return return_with_debug (ret
);
546 return compare_ssa_name (t1
, t2
);
559 return compare_cst_or_decl (t1
, t2
);
561 return return_false_with_msg ("Unknown TREE code reached");
565 /* Compares two tree list operands T1 and T2 and returns true if these
566 two trees are semantically equivalent. */
569 func_checker::compare_tree_list_operand (tree t1
, tree t2
)
571 gcc_assert (TREE_CODE (t1
) == TREE_LIST
);
572 gcc_assert (TREE_CODE (t2
) == TREE_LIST
);
574 for (; t1
; t1
= TREE_CHAIN (t1
))
579 if (!compare_operand (TREE_VALUE (t1
), TREE_VALUE (t2
)))
580 return return_false ();
582 t2
= TREE_CHAIN (t2
);
586 return return_false ();
591 /* Verifies that trees T1 and T2 do correspond. */
594 func_checker::compare_variable_decl (tree t1
, tree t2
)
601 if (DECL_ALIGN (t1
) != DECL_ALIGN (t2
))
602 return return_false_with_msg ("alignments are different");
604 if (DECL_HARD_REGISTER (t1
) != DECL_HARD_REGISTER (t2
))
605 return return_false_with_msg ("DECL_HARD_REGISTER are different");
607 if (DECL_HARD_REGISTER (t1
)
608 && DECL_ASSEMBLER_NAME (t1
) != DECL_ASSEMBLER_NAME (t2
))
609 return return_false_with_msg ("HARD REGISTERS are different");
611 /* Symbol table variables are known to match before we start comparing
613 if (decl_in_symtab_p (t1
))
614 return decl_in_symtab_p (t2
);
615 ret
= compare_decl (t1
, t2
);
617 return return_with_debug (ret
);
621 /* Function visits all gimple labels and creates corresponding
622 mapping between basic blocks and labels. */
625 func_checker::parse_labels (sem_bb
*bb
)
627 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
->bb
); !gsi_end_p (gsi
);
630 gimple stmt
= gsi_stmt (gsi
);
632 if (glabel
*label_stmt
= dyn_cast
<glabel
*> (stmt
))
634 tree t
= gimple_label_label (label_stmt
);
635 gcc_assert (TREE_CODE (t
) == LABEL_DECL
);
637 m_label_bb_map
.put (t
, bb
->bb
->index
);
642 /* Basic block equivalence comparison function that returns true if
643 basic blocks BB1 and BB2 (from functions FUNC1 and FUNC2) correspond.
645 In general, a collection of equivalence dictionaries is built for types
646 like SSA names, declarations (VAR_DECL, PARM_DECL, ..). This infrastructure
647 is utilized by every statement-by-statement comparison function. */
650 func_checker::compare_bb (sem_bb
*bb1
, sem_bb
*bb2
)
652 gimple_stmt_iterator gsi1
, gsi2
;
655 gsi1
= gsi_start_bb_nondebug (bb1
->bb
);
656 gsi2
= gsi_start_bb_nondebug (bb2
->bb
);
658 while (!gsi_end_p (gsi1
))
660 if (gsi_end_p (gsi2
))
661 return return_false ();
663 s1
= gsi_stmt (gsi1
);
664 s2
= gsi_stmt (gsi2
);
666 int eh1
= lookup_stmt_eh_lp_fn
667 (DECL_STRUCT_FUNCTION (m_source_func_decl
), s1
);
668 int eh2
= lookup_stmt_eh_lp_fn
669 (DECL_STRUCT_FUNCTION (m_target_func_decl
), s2
);
672 return return_false_with_msg ("EH regions are different");
674 if (gimple_code (s1
) != gimple_code (s2
))
675 return return_false_with_msg ("gimple codes are different");
677 switch (gimple_code (s1
))
680 if (!compare_gimple_call (as_a
<gcall
*> (s1
),
681 as_a
<gcall
*> (s2
)))
682 return return_different_stmts (s1
, s2
, "GIMPLE_CALL");
685 if (!compare_gimple_assign (s1
, s2
))
686 return return_different_stmts (s1
, s2
, "GIMPLE_ASSIGN");
689 if (!compare_gimple_cond (s1
, s2
))
690 return return_different_stmts (s1
, s2
, "GIMPLE_COND");
693 if (!compare_gimple_switch (as_a
<gswitch
*> (s1
),
694 as_a
<gswitch
*> (s2
)))
695 return return_different_stmts (s1
, s2
, "GIMPLE_SWITCH");
699 case GIMPLE_EH_DISPATCH
:
700 if (gimple_eh_dispatch_region (as_a
<geh_dispatch
*> (s1
))
701 != gimple_eh_dispatch_region (as_a
<geh_dispatch
*> (s2
)))
702 return return_different_stmts (s1
, s2
, "GIMPLE_EH_DISPATCH");
705 if (!compare_gimple_resx (as_a
<gresx
*> (s1
),
706 as_a
<gresx
*> (s2
)))
707 return return_different_stmts (s1
, s2
, "GIMPLE_RESX");
710 if (!compare_gimple_label (as_a
<glabel
*> (s1
),
711 as_a
<glabel
*> (s2
)))
712 return return_different_stmts (s1
, s2
, "GIMPLE_LABEL");
715 if (!compare_gimple_return (as_a
<greturn
*> (s1
),
716 as_a
<greturn
*> (s2
)))
717 return return_different_stmts (s1
, s2
, "GIMPLE_RETURN");
720 if (!compare_gimple_goto (s1
, s2
))
721 return return_different_stmts (s1
, s2
, "GIMPLE_GOTO");
724 if (!compare_gimple_asm (as_a
<gasm
*> (s1
),
726 return return_different_stmts (s1
, s2
, "GIMPLE_ASM");
732 return return_false_with_msg ("Unknown GIMPLE code reached");
735 gsi_next_nondebug (&gsi1
);
736 gsi_next_nondebug (&gsi2
);
739 if (!gsi_end_p (gsi2
))
740 return return_false ();
745 /* Verifies for given GIMPLEs S1 and S2 that
746 call statements are semantically equivalent. */
749 func_checker::compare_gimple_call (gcall
*s1
, gcall
*s2
)
754 if (gimple_call_num_args (s1
) != gimple_call_num_args (s2
))
757 t1
= gimple_call_fn (s1
);
758 t2
= gimple_call_fn (s2
);
759 if (!compare_operand (t1
, t2
))
760 return return_false ();
763 if (gimple_call_internal_p (s1
) != gimple_call_internal_p (s2
)
764 || gimple_call_ctrl_altering_p (s1
) != gimple_call_ctrl_altering_p (s2
)
765 || gimple_call_tail_p (s1
) != gimple_call_tail_p (s2
)
766 || gimple_call_return_slot_opt_p (s1
) != gimple_call_return_slot_opt_p (s2
)
767 || gimple_call_from_thunk_p (s1
) != gimple_call_from_thunk_p (s2
)
768 || gimple_call_va_arg_pack_p (s1
) != gimple_call_va_arg_pack_p (s2
)
769 || gimple_call_alloca_for_var_p (s1
) != gimple_call_alloca_for_var_p (s2
)
770 || gimple_call_with_bounds_p (s1
) != gimple_call_with_bounds_p (s2
))
773 if (gimple_call_internal_p (s1
)
774 && gimple_call_internal_fn (s1
) != gimple_call_internal_fn (s2
))
777 tree fntype1
= gimple_call_fntype (s1
);
778 tree fntype2
= gimple_call_fntype (s2
);
779 if ((fntype1
&& !fntype2
)
780 || (!fntype1
&& fntype2
)
781 || (fntype1
&& !types_compatible_p (fntype1
, fntype2
)))
782 return return_false_with_msg ("call function types are not compatible");
784 tree chain1
= gimple_call_chain (s1
);
785 tree chain2
= gimple_call_chain (s2
);
786 if ((chain1
&& !chain2
)
787 || (!chain1
&& chain2
)
788 || !compare_operand (chain1
, chain2
))
789 return return_false_with_msg ("static call chains are different");
791 /* Checking of argument. */
792 for (i
= 0; i
< gimple_call_num_args (s1
); ++i
)
794 t1
= gimple_call_arg (s1
, i
);
795 t2
= gimple_call_arg (s2
, i
);
797 if (!compare_memory_operand (t1
, t2
))
798 return return_false_with_msg ("memory operands are different");
801 /* Return value checking. */
802 t1
= gimple_get_lhs (s1
);
803 t2
= gimple_get_lhs (s2
);
805 return compare_memory_operand (t1
, t2
);
809 /* Verifies for given GIMPLEs S1 and S2 that
810 assignment statements are semantically equivalent. */
813 func_checker::compare_gimple_assign (gimple s1
, gimple s2
)
816 tree_code code1
, code2
;
819 code1
= gimple_expr_code (s1
);
820 code2
= gimple_expr_code (s2
);
825 code1
= gimple_assign_rhs_code (s1
);
826 code2
= gimple_assign_rhs_code (s2
);
831 for (i
= 0; i
< gimple_num_ops (s1
); i
++)
833 arg1
= gimple_op (s1
, i
);
834 arg2
= gimple_op (s2
, i
);
836 if (!compare_memory_operand (arg1
, arg2
))
837 return return_false_with_msg ("memory operands are different");
844 /* Verifies for given GIMPLEs S1 and S2 that
845 condition statements are semantically equivalent. */
848 func_checker::compare_gimple_cond (gimple s1
, gimple s2
)
851 tree_code code1
, code2
;
853 code1
= gimple_expr_code (s1
);
854 code2
= gimple_expr_code (s2
);
859 t1
= gimple_cond_lhs (s1
);
860 t2
= gimple_cond_lhs (s2
);
862 if (!compare_operand (t1
, t2
))
865 t1
= gimple_cond_rhs (s1
);
866 t2
= gimple_cond_rhs (s2
);
868 return compare_operand (t1
, t2
);
871 /* Verifies that tree labels T1 and T2 correspond in FUNC1 and FUNC2. */
874 func_checker::compare_tree_ssa_label (tree t1
, tree t2
)
876 return compare_operand (t1
, t2
);
879 /* Verifies for given GIMPLE_LABEL stmts S1 and S2 that
880 label statements are semantically equivalent. */
883 func_checker::compare_gimple_label (const glabel
*g1
, const glabel
*g2
)
888 tree t1
= gimple_label_label (g1
);
889 tree t2
= gimple_label_label (g2
);
891 if (FORCED_LABEL (t1
) || FORCED_LABEL (t2
))
892 return return_false_with_msg ("FORCED_LABEL");
894 /* As the pass build BB to label mapping, no further check is needed. */
898 /* Verifies for given GIMPLE_SWITCH stmts S1 and S2 that
899 switch statements are semantically equivalent. */
902 func_checker::compare_gimple_switch (const gswitch
*g1
, const gswitch
*g2
)
904 unsigned lsize1
, lsize2
, i
;
906 lsize1
= gimple_switch_num_labels (g1
);
907 lsize2
= gimple_switch_num_labels (g2
);
909 if (lsize1
!= lsize2
)
912 tree t1
= gimple_switch_index (g1
);
913 tree t2
= gimple_switch_index (g2
);
915 if (!compare_operand (t1
, t2
))
918 for (i
= 0; i
< lsize1
; i
++)
920 tree label1
= gimple_switch_label (g1
, i
);
921 tree label2
= gimple_switch_label (g2
, i
);
923 /* Label LOW and HIGH comparison. */
924 tree low1
= CASE_LOW (label1
);
925 tree low2
= CASE_LOW (label2
);
927 if (!tree_int_cst_equal (low1
, low2
))
928 return return_false_with_msg ("case low values are different");
930 tree high1
= CASE_HIGH (label1
);
931 tree high2
= CASE_HIGH (label2
);
933 if (!tree_int_cst_equal (high1
, high2
))
934 return return_false_with_msg ("case high values are different");
936 if (TREE_CODE (label1
) == CASE_LABEL_EXPR
937 && TREE_CODE (label2
) == CASE_LABEL_EXPR
)
939 label1
= CASE_LABEL (label1
);
940 label2
= CASE_LABEL (label2
);
942 if (!compare_operand (label1
, label2
))
943 return return_false_with_msg ("switch label_exprs are different");
945 else if (!tree_int_cst_equal (label1
, label2
))
946 return return_false_with_msg ("switch labels are different");
952 /* Verifies for given GIMPLE_RETURN stmts S1 and S2 that
953 return statements are semantically equivalent. */
956 func_checker::compare_gimple_return (const greturn
*g1
, const greturn
*g2
)
960 t1
= gimple_return_retval (g1
);
961 t2
= gimple_return_retval (g2
);
963 /* Void return type. */
964 if (t1
== NULL
&& t2
== NULL
)
967 return compare_operand (t1
, t2
);
970 /* Verifies for given GIMPLEs S1 and S2 that
971 goto statements are semantically equivalent. */
974 func_checker::compare_gimple_goto (gimple g1
, gimple g2
)
978 dest1
= gimple_goto_dest (g1
);
979 dest2
= gimple_goto_dest (g2
);
981 if (TREE_CODE (dest1
) != TREE_CODE (dest2
) || TREE_CODE (dest1
) != SSA_NAME
)
984 return compare_operand (dest1
, dest2
);
987 /* Verifies for given GIMPLE_RESX stmts S1 and S2 that
988 resx statements are semantically equivalent. */
991 func_checker::compare_gimple_resx (const gresx
*g1
, const gresx
*g2
)
993 return gimple_resx_region (g1
) == gimple_resx_region (g2
);
996 /* Verifies for given GIMPLEs S1 and S2 that ASM statements are equivalent.
997 For the beginning, the pass only supports equality for
998 '__asm__ __volatile__ ("", "", "", "memory")'. */
1001 func_checker::compare_gimple_asm (const gasm
*g1
, const gasm
*g2
)
1003 if (gimple_asm_volatile_p (g1
) != gimple_asm_volatile_p (g2
))
1006 if (gimple_asm_ninputs (g1
) != gimple_asm_ninputs (g2
))
1009 if (gimple_asm_noutputs (g1
) != gimple_asm_noutputs (g2
))
1012 /* We do not suppport goto ASM statement comparison. */
1013 if (gimple_asm_nlabels (g1
) || gimple_asm_nlabels (g2
))
1016 if (gimple_asm_nclobbers (g1
) != gimple_asm_nclobbers (g2
))
1019 if (strcmp (gimple_asm_string (g1
), gimple_asm_string (g2
)) != 0)
1020 return return_false_with_msg ("ASM strings are different");
1022 for (unsigned i
= 0; i
< gimple_asm_ninputs (g1
); i
++)
1024 tree input1
= gimple_asm_input_op (g1
, i
);
1025 tree input2
= gimple_asm_input_op (g2
, i
);
1027 if (!compare_tree_list_operand (input1
, input2
))
1028 return return_false_with_msg ("ASM input is different");
1031 for (unsigned i
= 0; i
< gimple_asm_noutputs (g1
); i
++)
1033 tree output1
= gimple_asm_output_op (g1
, i
);
1034 tree output2
= gimple_asm_output_op (g2
, i
);
1036 if (!compare_tree_list_operand (output1
, output2
))
1037 return return_false_with_msg ("ASM output is different");
1040 for (unsigned i
= 0; i
< gimple_asm_nclobbers (g1
); i
++)
1042 tree clobber1
= gimple_asm_clobber_op (g1
, i
);
1043 tree clobber2
= gimple_asm_clobber_op (g2
, i
);
1045 if (!operand_equal_p (TREE_VALUE (clobber1
), TREE_VALUE (clobber2
),
1047 return return_false_with_msg ("ASM clobber is different");
1053 } // ipa_icf_gimple namespace