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"
29 #include "fold-const.h"
32 #include "hard-reg-set.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-expr.h"
41 #include "insn-config.h"
50 #include "gimple-iterator.h"
51 #include "gimple-ssa.h"
53 #include "stringpool.h"
55 #include "tree-pass.h"
56 #include "gimple-pretty-print.h"
59 #include "plugin-api.h"
62 #include "data-streamer.h"
63 #include "ipa-utils.h"
65 #include "tree-ssanames.h"
69 #include "ipa-icf-gimple.h"
72 namespace ipa_icf_gimple
{
74 /* Initialize internal structures for a given SOURCE_FUNC_DECL and
75 TARGET_FUNC_DECL. Strict polymorphic comparison is processed if
76 an option COMPARE_POLYMORPHIC is true. For special cases, one can
77 set IGNORE_LABELS to skip label comparison.
78 Similarly, IGNORE_SOURCE_DECLS and IGNORE_TARGET_DECLS are sets
79 of declarations that can be skipped. */
81 func_checker::func_checker (tree source_func_decl
, tree target_func_decl
,
82 bool compare_polymorphic
,
84 hash_set
<symtab_node
*> *ignored_source_nodes
,
85 hash_set
<symtab_node
*> *ignored_target_nodes
)
86 : m_source_func_decl (source_func_decl
), m_target_func_decl (target_func_decl
),
87 m_ignored_source_nodes (ignored_source_nodes
),
88 m_ignored_target_nodes (ignored_target_nodes
),
89 m_compare_polymorphic (compare_polymorphic
),
90 m_ignore_labels (ignore_labels
)
92 function
*source_func
= DECL_STRUCT_FUNCTION (source_func_decl
);
93 function
*target_func
= DECL_STRUCT_FUNCTION (target_func_decl
);
95 unsigned ssa_source
= SSANAMES (source_func
)->length ();
96 unsigned ssa_target
= SSANAMES (target_func
)->length ();
98 m_source_ssa_names
.create (ssa_source
);
99 m_target_ssa_names
.create (ssa_target
);
101 for (unsigned i
= 0; i
< ssa_source
; i
++)
102 m_source_ssa_names
.safe_push (-1);
104 for (unsigned i
= 0; i
< ssa_target
; i
++)
105 m_target_ssa_names
.safe_push (-1);
108 /* Memory release routine. */
110 func_checker::~func_checker ()
112 m_source_ssa_names
.release();
113 m_target_ssa_names
.release();
116 /* Verifies that trees T1 and T2 are equivalent from perspective of ICF. */
119 func_checker::compare_ssa_name (tree t1
, tree t2
)
121 gcc_assert (TREE_CODE (t1
) == SSA_NAME
);
122 gcc_assert (TREE_CODE (t2
) == SSA_NAME
);
124 unsigned i1
= SSA_NAME_VERSION (t1
);
125 unsigned i2
= SSA_NAME_VERSION (t2
);
127 if (m_source_ssa_names
[i1
] == -1)
128 m_source_ssa_names
[i1
] = i2
;
129 else if (m_source_ssa_names
[i1
] != (int) i2
)
132 if(m_target_ssa_names
[i2
] == -1)
133 m_target_ssa_names
[i2
] = i1
;
134 else if (m_target_ssa_names
[i2
] != (int) i1
)
137 if (SSA_NAME_IS_DEFAULT_DEF (t1
))
139 tree b1
= SSA_NAME_VAR (t1
);
140 tree b2
= SSA_NAME_VAR (t2
);
142 if (b1
== NULL
&& b2
== NULL
)
145 if (b1
== NULL
|| b2
== NULL
|| TREE_CODE (b1
) != TREE_CODE (b2
))
146 return return_false ();
148 return compare_cst_or_decl (b1
, b2
);
154 /* Verification function for edges E1 and E2. */
157 func_checker::compare_edge (edge e1
, edge e2
)
159 if (e1
->flags
!= e2
->flags
)
164 edge
&slot
= m_edge_map
.get_or_insert (e1
, &existed_p
);
166 return return_with_debug (slot
== e2
);
170 /* TODO: filter edge probabilities for profile feedback match. */
175 /* Verification function for declaration trees T1 and T2 that
176 come from functions FUNC1 and FUNC2. */
179 func_checker::compare_decl (tree t1
, tree t2
)
181 if (!auto_var_in_fn_p (t1
, m_source_func_decl
)
182 || !auto_var_in_fn_p (t2
, m_target_func_decl
))
183 return return_with_debug (t1
== t2
);
185 tree_code t
= TREE_CODE (t1
);
186 if ((t
== VAR_DECL
|| t
== PARM_DECL
|| t
== RESULT_DECL
)
187 && DECL_BY_REFERENCE (t1
) != DECL_BY_REFERENCE (t2
))
188 return return_false_with_msg ("DECL_BY_REFERENCE flags are different");
190 if (!compatible_types_p (TREE_TYPE (t1
), TREE_TYPE (t2
)))
191 return return_false ();
193 /* TODO: we are actually too strict here. We only need to compare if
194 T1 can be used in polymorphic call. */
195 if (TREE_ADDRESSABLE (t1
)
196 && m_compare_polymorphic
197 && !compatible_polymorphic_types_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
199 return return_false ();
201 if ((t
== VAR_DECL
|| t
== PARM_DECL
|| t
== RESULT_DECL
)
202 && DECL_BY_REFERENCE (t1
)
203 && m_compare_polymorphic
204 && !compatible_polymorphic_types_p (TREE_TYPE (t1
), TREE_TYPE (t2
),
206 return return_false ();
210 tree
&slot
= m_decl_map
.get_or_insert (t1
, &existed_p
);
212 return return_with_debug (slot
== t2
);
219 /* Return true if T1 and T2 are same for purposes of ipa-polymorphic-call
220 analysis. COMPARE_PTR indicates if types of pointers needs to be
224 func_checker::compatible_polymorphic_types_p (tree t1
, tree t2
,
227 gcc_assert (TREE_CODE (t1
) != FUNCTION_TYPE
&& TREE_CODE (t1
) != METHOD_TYPE
);
229 /* Pointer types generally give no information. */
230 if (POINTER_TYPE_P (t1
))
234 return func_checker::compatible_polymorphic_types_p (TREE_TYPE (t1
),
239 /* If types contain a polymorphic types, match them. */
240 bool c1
= contains_polymorphic_type_p (t1
);
241 bool c2
= contains_polymorphic_type_p (t2
);
245 return return_false_with_msg ("one type is not polymorphic");
246 if (!types_must_be_same_for_odr (t1
, t2
))
247 return return_false_with_msg ("types are not same for ODR");
251 /* Return true if types are compatible from perspective of ICF. */
253 func_checker::compatible_types_p (tree t1
, tree t2
)
255 if (TREE_CODE (t1
) != TREE_CODE (t2
))
256 return return_false_with_msg ("different tree types");
258 if (TYPE_RESTRICT (t1
) != TYPE_RESTRICT (t2
))
259 return return_false_with_msg ("restrict flags are different");
261 if (!types_compatible_p (t1
, t2
))
262 return return_false_with_msg ("types are not compatible");
264 if (get_alias_set (t1
) != get_alias_set (t2
))
265 return return_false_with_msg ("alias sets are different");
270 /* Function compare for equality given memory operands T1 and T2. */
273 func_checker::compare_memory_operand (tree t1
, tree t2
)
281 ao_ref_init (&r1
, t1
);
282 ao_ref_init (&r2
, t2
);
284 tree b1
= ao_ref_base (&r1
);
285 tree b2
= ao_ref_base (&r2
);
287 bool source_is_memop
= DECL_P (b1
) || INDIRECT_REF_P (b1
)
288 || TREE_CODE (b1
) == MEM_REF
289 || TREE_CODE (b1
) == TARGET_MEM_REF
;
291 bool target_is_memop
= DECL_P (b2
) || INDIRECT_REF_P (b2
)
292 || TREE_CODE (b2
) == MEM_REF
293 || TREE_CODE (b2
) == TARGET_MEM_REF
;
295 /* Compare alias sets for memory operands. */
296 if (source_is_memop
&& target_is_memop
)
298 if (TREE_THIS_VOLATILE (t1
) != TREE_THIS_VOLATILE (t2
))
299 return return_false_with_msg ("different operand volatility");
301 if (ao_ref_alias_set (&r1
) != ao_ref_alias_set (&r2
)
302 || ao_ref_base_alias_set (&r1
) != ao_ref_base_alias_set (&r2
))
303 return return_false_with_msg ("ao alias sets are different");
305 /* We can't simply use get_object_alignment_1 on the full
306 reference as for accesses with variable indexes this reports
307 too conservative alignment. We also can't use the ao_ref_base
308 base objects as ao_ref_base happily strips MEM_REFs around
309 decls even though that may carry alignment info. */
311 while (handled_component_p (b1
))
312 b1
= TREE_OPERAND (b1
, 0);
314 while (handled_component_p (b2
))
315 b2
= TREE_OPERAND (b2
, 0);
316 unsigned int align1
, align2
;
317 unsigned HOST_WIDE_INT tem
;
318 get_object_alignment_1 (b1
, &align1
, &tem
);
319 get_object_alignment_1 (b2
, &align2
, &tem
);
320 if (align1
!= align2
)
321 return return_false_with_msg ("different access alignment");
323 /* Similarly we have to compare dependence info where equality
324 tells us we are safe (even some unequal values would be safe
325 but then we have to maintain a map of bases and cliques). */
326 unsigned short clique1
= 0, base1
= 0, clique2
= 0, base2
= 0;
327 if (TREE_CODE (b1
) == MEM_REF
)
329 clique1
= MR_DEPENDENCE_CLIQUE (b1
);
330 base1
= MR_DEPENDENCE_BASE (b1
);
332 if (TREE_CODE (b2
) == MEM_REF
)
334 clique2
= MR_DEPENDENCE_CLIQUE (b2
);
335 base2
= MR_DEPENDENCE_BASE (b2
);
337 if (clique1
!= clique2
|| base1
!= base2
)
338 return return_false_with_msg ("different dependence info");
341 return compare_operand (t1
, t2
);
344 /* Function compare for equality given trees T1 and T2 which
345 can be either a constant or a declaration type. */
348 func_checker::compare_cst_or_decl (tree t1
, tree t2
)
352 switch (TREE_CODE (t1
))
360 ret
= compatible_types_p (TREE_TYPE (t1
), TREE_TYPE (t2
))
361 && operand_equal_p (t1
, t2
, OEP_ONLY_CONST
);
362 return return_with_debug (ret
);
365 /* All function decls are in the symbol table and known to match
366 before we start comparing bodies. */
369 return return_with_debug (compare_variable_decl (t1
, t2
));
372 tree offset1
= DECL_FIELD_OFFSET (t1
);
373 tree offset2
= DECL_FIELD_OFFSET (t2
);
375 tree bit_offset1
= DECL_FIELD_BIT_OFFSET (t1
);
376 tree bit_offset2
= DECL_FIELD_BIT_OFFSET (t2
);
378 ret
= compare_operand (offset1
, offset2
)
379 && compare_operand (bit_offset1
, bit_offset2
);
381 return return_with_debug (ret
);
385 int *bb1
= m_label_bb_map
.get (t1
);
386 int *bb2
= m_label_bb_map
.get (t2
);
388 return return_with_debug (*bb1
== *bb2
);
394 ret
= compare_decl (t1
, t2
);
395 return return_with_debug (ret
);
402 /* Function responsible for comparison of various operands T1 and T2.
403 If these components, from functions FUNC1 and FUNC2, are equal, true
407 func_checker::compare_operand (tree t1
, tree t2
)
409 tree x1
, x2
, y1
, y2
, z1
, z2
;
417 tree tt1
= TREE_TYPE (t1
);
418 tree tt2
= TREE_TYPE (t2
);
420 if (!func_checker::compatible_types_p (tt1
, tt2
))
423 if (TREE_CODE (t1
) != TREE_CODE (t2
))
424 return return_false ();
426 switch (TREE_CODE (t1
))
430 unsigned length1
= vec_safe_length (CONSTRUCTOR_ELTS (t1
));
431 unsigned length2
= vec_safe_length (CONSTRUCTOR_ELTS (t2
));
433 if (length1
!= length2
)
434 return return_false ();
436 for (unsigned i
= 0; i
< length1
; i
++)
437 if (!compare_operand (CONSTRUCTOR_ELT (t1
, i
)->value
,
438 CONSTRUCTOR_ELT (t2
, i
)->value
))
439 return return_false();
444 case ARRAY_RANGE_REF
:
445 /* First argument is the array, second is the index. */
446 x1
= TREE_OPERAND (t1
, 0);
447 x2
= TREE_OPERAND (t2
, 0);
448 y1
= TREE_OPERAND (t1
, 1);
449 y2
= TREE_OPERAND (t2
, 1);
451 if (!compare_operand (array_ref_low_bound (t1
),
452 array_ref_low_bound (t2
)))
453 return return_false_with_msg ("");
454 if (!compare_operand (array_ref_element_size (t1
),
455 array_ref_element_size (t2
)))
456 return return_false_with_msg ("");
458 if (!compare_operand (x1
, x2
))
459 return return_false_with_msg ("");
460 return compare_operand (y1
, y2
);
463 x1
= TREE_OPERAND (t1
, 0);
464 x2
= TREE_OPERAND (t2
, 0);
465 y1
= TREE_OPERAND (t1
, 1);
466 y2
= TREE_OPERAND (t2
, 1);
468 /* See if operand is an memory access (the test originate from
471 In this case the alias set of the function being replaced must
472 be subset of the alias set of the other function. At the moment
473 we seek for equivalency classes, so simply require inclussion in
476 if (!func_checker::compatible_types_p (TREE_TYPE (x1
), TREE_TYPE (x2
)))
477 return return_false ();
479 if (!compare_operand (x1
, x2
))
480 return return_false_with_msg ("");
482 /* Type of the offset on MEM_REF does not matter. */
483 return wi::to_offset (y1
) == wi::to_offset (y2
);
487 x1
= TREE_OPERAND (t1
, 0);
488 x2
= TREE_OPERAND (t2
, 0);
489 y1
= TREE_OPERAND (t1
, 1);
490 y2
= TREE_OPERAND (t2
, 1);
492 ret
= compare_operand (x1
, x2
)
493 && compare_cst_or_decl (y1
, y2
);
495 return return_with_debug (ret
);
497 /* Virtual table call. */
500 if (!compare_ssa_name (OBJ_TYPE_REF_EXPR (t1
), OBJ_TYPE_REF_EXPR (t2
)))
501 return return_false ();
502 if (opt_for_fn (m_source_func_decl
, flag_devirtualize
)
503 && virtual_method_call_p (t1
))
505 if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (t1
))
506 != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (t2
)))
507 return return_false_with_msg ("OBJ_TYPE_REF token mismatch");
508 if (!types_same_for_odr (obj_type_ref_class (t1
),
509 obj_type_ref_class (t2
)))
510 return return_false_with_msg ("OBJ_TYPE_REF OTR type mismatch");
511 if (!compare_operand (OBJ_TYPE_REF_OBJECT (t1
),
512 OBJ_TYPE_REF_OBJECT (t2
)))
513 return return_false_with_msg ("OBJ_TYPE_REF object mismatch");
516 return return_with_debug (true);
522 x1
= TREE_OPERAND (t1
, 0);
523 x2
= TREE_OPERAND (t2
, 0);
525 ret
= compare_operand (x1
, x2
);
526 return return_with_debug (ret
);
530 x1
= TREE_OPERAND (t1
, 0);
531 x2
= TREE_OPERAND (t2
, 0);
532 y1
= TREE_OPERAND (t1
, 1);
533 y2
= TREE_OPERAND (t2
, 1);
534 z1
= TREE_OPERAND (t1
, 2);
535 z2
= TREE_OPERAND (t2
, 2);
537 ret
= compare_operand (x1
, x2
)
538 && compare_cst_or_decl (y1
, y2
)
539 && compare_cst_or_decl (z1
, z2
);
541 return return_with_debug (ret
);
544 return compare_ssa_name (t1
, t2
);
557 return compare_cst_or_decl (t1
, t2
);
559 return return_false_with_msg ("Unknown TREE code reached");
563 /* Compares two tree list operands T1 and T2 and returns true if these
564 two trees are semantically equivalent. */
567 func_checker::compare_tree_list_operand (tree t1
, tree t2
)
569 gcc_assert (TREE_CODE (t1
) == TREE_LIST
);
570 gcc_assert (TREE_CODE (t2
) == TREE_LIST
);
572 for (; t1
; t1
= TREE_CHAIN (t1
))
577 if (!compare_operand (TREE_VALUE (t1
), TREE_VALUE (t2
)))
578 return return_false ();
580 t2
= TREE_CHAIN (t2
);
584 return return_false ();
589 /* Verifies that trees T1 and T2 do correspond. */
592 func_checker::compare_variable_decl (tree t1
, tree t2
)
599 if (DECL_ALIGN (t1
) != DECL_ALIGN (t2
))
600 return return_false_with_msg ("alignments are different");
602 if (DECL_HARD_REGISTER (t1
) != DECL_HARD_REGISTER (t2
))
603 return return_false_with_msg ("DECL_HARD_REGISTER are different");
605 if (DECL_HARD_REGISTER (t1
)
606 && DECL_ASSEMBLER_NAME (t1
) != DECL_ASSEMBLER_NAME (t2
))
607 return return_false_with_msg ("HARD REGISTERS are different");
609 /* Symbol table variables are known to match before we start comparing
611 if (decl_in_symtab_p (t1
))
612 return decl_in_symtab_p (t2
);
613 ret
= compare_decl (t1
, t2
);
615 return return_with_debug (ret
);
619 /* Function visits all gimple labels and creates corresponding
620 mapping between basic blocks and labels. */
623 func_checker::parse_labels (sem_bb
*bb
)
625 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
->bb
); !gsi_end_p (gsi
);
628 gimple stmt
= gsi_stmt (gsi
);
630 if (glabel
*label_stmt
= dyn_cast
<glabel
*> (stmt
))
632 tree t
= gimple_label_label (label_stmt
);
633 gcc_assert (TREE_CODE (t
) == LABEL_DECL
);
635 m_label_bb_map
.put (t
, bb
->bb
->index
);
640 /* Basic block equivalence comparison function that returns true if
641 basic blocks BB1 and BB2 (from functions FUNC1 and FUNC2) correspond.
643 In general, a collection of equivalence dictionaries is built for types
644 like SSA names, declarations (VAR_DECL, PARM_DECL, ..). This infrastructure
645 is utilized by every statement-by-statement comparison function. */
648 func_checker::compare_bb (sem_bb
*bb1
, sem_bb
*bb2
)
650 gimple_stmt_iterator gsi1
, gsi2
;
653 gsi1
= gsi_start_bb_nondebug (bb1
->bb
);
654 gsi2
= gsi_start_bb_nondebug (bb2
->bb
);
656 while (!gsi_end_p (gsi1
))
658 if (gsi_end_p (gsi2
))
659 return return_false ();
661 s1
= gsi_stmt (gsi1
);
662 s2
= gsi_stmt (gsi2
);
664 int eh1
= lookup_stmt_eh_lp_fn
665 (DECL_STRUCT_FUNCTION (m_source_func_decl
), s1
);
666 int eh2
= lookup_stmt_eh_lp_fn
667 (DECL_STRUCT_FUNCTION (m_target_func_decl
), s2
);
670 return return_false_with_msg ("EH regions are different");
672 if (gimple_code (s1
) != gimple_code (s2
))
673 return return_false_with_msg ("gimple codes are different");
675 switch (gimple_code (s1
))
678 if (!compare_gimple_call (as_a
<gcall
*> (s1
),
679 as_a
<gcall
*> (s2
)))
680 return return_different_stmts (s1
, s2
, "GIMPLE_CALL");
683 if (!compare_gimple_assign (s1
, s2
))
684 return return_different_stmts (s1
, s2
, "GIMPLE_ASSIGN");
687 if (!compare_gimple_cond (s1
, s2
))
688 return return_different_stmts (s1
, s2
, "GIMPLE_COND");
691 if (!compare_gimple_switch (as_a
<gswitch
*> (s1
),
692 as_a
<gswitch
*> (s2
)))
693 return return_different_stmts (s1
, s2
, "GIMPLE_SWITCH");
697 case GIMPLE_EH_DISPATCH
:
698 if (gimple_eh_dispatch_region (as_a
<geh_dispatch
*> (s1
))
699 != gimple_eh_dispatch_region (as_a
<geh_dispatch
*> (s2
)))
700 return return_different_stmts (s1
, s2
, "GIMPLE_EH_DISPATCH");
703 if (!compare_gimple_resx (as_a
<gresx
*> (s1
),
704 as_a
<gresx
*> (s2
)))
705 return return_different_stmts (s1
, s2
, "GIMPLE_RESX");
708 if (!compare_gimple_label (as_a
<glabel
*> (s1
),
709 as_a
<glabel
*> (s2
)))
710 return return_different_stmts (s1
, s2
, "GIMPLE_LABEL");
713 if (!compare_gimple_return (as_a
<greturn
*> (s1
),
714 as_a
<greturn
*> (s2
)))
715 return return_different_stmts (s1
, s2
, "GIMPLE_RETURN");
718 if (!compare_gimple_goto (s1
, s2
))
719 return return_different_stmts (s1
, s2
, "GIMPLE_GOTO");
722 if (!compare_gimple_asm (as_a
<gasm
*> (s1
),
724 return return_different_stmts (s1
, s2
, "GIMPLE_ASM");
730 return return_false_with_msg ("Unknown GIMPLE code reached");
733 gsi_next_nondebug (&gsi1
);
734 gsi_next_nondebug (&gsi2
);
737 if (!gsi_end_p (gsi2
))
738 return return_false ();
743 /* Verifies for given GIMPLEs S1 and S2 that
744 call statements are semantically equivalent. */
747 func_checker::compare_gimple_call (gcall
*s1
, gcall
*s2
)
752 if (gimple_call_num_args (s1
) != gimple_call_num_args (s2
))
755 t1
= gimple_call_fn (s1
);
756 t2
= gimple_call_fn (s2
);
757 if (!compare_operand (t1
, t2
))
758 return return_false ();
761 if (gimple_call_internal_p (s1
) != gimple_call_internal_p (s2
)
762 || gimple_call_ctrl_altering_p (s1
) != gimple_call_ctrl_altering_p (s2
)
763 || gimple_call_tail_p (s1
) != gimple_call_tail_p (s2
)
764 || gimple_call_return_slot_opt_p (s1
) != gimple_call_return_slot_opt_p (s2
)
765 || gimple_call_from_thunk_p (s1
) != gimple_call_from_thunk_p (s2
)
766 || gimple_call_va_arg_pack_p (s1
) != gimple_call_va_arg_pack_p (s2
)
767 || gimple_call_alloca_for_var_p (s1
) != gimple_call_alloca_for_var_p (s2
)
768 || gimple_call_with_bounds_p (s1
) != gimple_call_with_bounds_p (s2
))
771 if (gimple_call_internal_p (s1
)
772 && gimple_call_internal_fn (s1
) != gimple_call_internal_fn (s2
))
775 tree fntype1
= gimple_call_fntype (s1
);
776 tree fntype2
= gimple_call_fntype (s2
);
777 if ((fntype1
&& !fntype2
)
778 || (!fntype1
&& fntype2
)
779 || (fntype1
&& !types_compatible_p (fntype1
, fntype2
)))
780 return return_false_with_msg ("call function types are not compatible");
782 tree chain1
= gimple_call_chain (s1
);
783 tree chain2
= gimple_call_chain (s2
);
784 if ((chain1
&& !chain2
)
785 || (!chain1
&& chain2
)
786 || !compare_operand (chain1
, chain2
))
787 return return_false_with_msg ("static call chains are different");
789 /* Checking of argument. */
790 for (i
= 0; i
< gimple_call_num_args (s1
); ++i
)
792 t1
= gimple_call_arg (s1
, i
);
793 t2
= gimple_call_arg (s2
, i
);
795 if (!compare_memory_operand (t1
, t2
))
796 return return_false_with_msg ("memory operands are different");
799 /* Return value checking. */
800 t1
= gimple_get_lhs (s1
);
801 t2
= gimple_get_lhs (s2
);
803 return compare_memory_operand (t1
, t2
);
807 /* Verifies for given GIMPLEs S1 and S2 that
808 assignment statements are semantically equivalent. */
811 func_checker::compare_gimple_assign (gimple s1
, gimple s2
)
814 tree_code code1
, code2
;
817 code1
= gimple_expr_code (s1
);
818 code2
= gimple_expr_code (s2
);
823 code1
= gimple_assign_rhs_code (s1
);
824 code2
= gimple_assign_rhs_code (s2
);
829 for (i
= 0; i
< gimple_num_ops (s1
); i
++)
831 arg1
= gimple_op (s1
, i
);
832 arg2
= gimple_op (s2
, i
);
834 if (!compare_memory_operand (arg1
, arg2
))
835 return return_false_with_msg ("memory operands are different");
842 /* Verifies for given GIMPLEs S1 and S2 that
843 condition statements are semantically equivalent. */
846 func_checker::compare_gimple_cond (gimple s1
, gimple s2
)
849 tree_code code1
, code2
;
851 code1
= gimple_expr_code (s1
);
852 code2
= gimple_expr_code (s2
);
857 t1
= gimple_cond_lhs (s1
);
858 t2
= gimple_cond_lhs (s2
);
860 if (!compare_operand (t1
, t2
))
863 t1
= gimple_cond_rhs (s1
);
864 t2
= gimple_cond_rhs (s2
);
866 return compare_operand (t1
, t2
);
869 /* Verifies that tree labels T1 and T2 correspond in FUNC1 and FUNC2. */
872 func_checker::compare_tree_ssa_label (tree t1
, tree t2
)
874 return compare_operand (t1
, t2
);
877 /* Verifies for given GIMPLE_LABEL stmts S1 and S2 that
878 label statements are semantically equivalent. */
881 func_checker::compare_gimple_label (const glabel
*g1
, const glabel
*g2
)
886 tree t1
= gimple_label_label (g1
);
887 tree t2
= gimple_label_label (g2
);
889 if (FORCED_LABEL (t1
) || FORCED_LABEL (t2
))
890 return return_false_with_msg ("FORCED_LABEL");
892 /* As the pass build BB to label mapping, no further check is needed. */
896 /* Verifies for given GIMPLE_SWITCH stmts S1 and S2 that
897 switch statements are semantically equivalent. */
900 func_checker::compare_gimple_switch (const gswitch
*g1
, const gswitch
*g2
)
902 unsigned lsize1
, lsize2
, i
;
904 lsize1
= gimple_switch_num_labels (g1
);
905 lsize2
= gimple_switch_num_labels (g2
);
907 if (lsize1
!= lsize2
)
910 tree t1
= gimple_switch_index (g1
);
911 tree t2
= gimple_switch_index (g2
);
913 if (!compare_operand (t1
, t2
))
916 for (i
= 0; i
< lsize1
; i
++)
918 tree label1
= gimple_switch_label (g1
, i
);
919 tree label2
= gimple_switch_label (g2
, i
);
921 /* Label LOW and HIGH comparison. */
922 tree low1
= CASE_LOW (label1
);
923 tree low2
= CASE_LOW (label2
);
925 if (!tree_int_cst_equal (low1
, low2
))
926 return return_false_with_msg ("case low values are different");
928 tree high1
= CASE_HIGH (label1
);
929 tree high2
= CASE_HIGH (label2
);
931 if (!tree_int_cst_equal (high1
, high2
))
932 return return_false_with_msg ("case high values are different");
934 if (TREE_CODE (label1
) == CASE_LABEL_EXPR
935 && TREE_CODE (label2
) == CASE_LABEL_EXPR
)
937 label1
= CASE_LABEL (label1
);
938 label2
= CASE_LABEL (label2
);
940 if (!compare_operand (label1
, label2
))
941 return return_false_with_msg ("switch label_exprs are different");
943 else if (!tree_int_cst_equal (label1
, label2
))
944 return return_false_with_msg ("switch labels are different");
950 /* Verifies for given GIMPLE_RETURN stmts S1 and S2 that
951 return statements are semantically equivalent. */
954 func_checker::compare_gimple_return (const greturn
*g1
, const greturn
*g2
)
958 t1
= gimple_return_retval (g1
);
959 t2
= gimple_return_retval (g2
);
961 /* Void return type. */
962 if (t1
== NULL
&& t2
== NULL
)
965 return compare_operand (t1
, t2
);
968 /* Verifies for given GIMPLEs S1 and S2 that
969 goto statements are semantically equivalent. */
972 func_checker::compare_gimple_goto (gimple g1
, gimple g2
)
976 dest1
= gimple_goto_dest (g1
);
977 dest2
= gimple_goto_dest (g2
);
979 if (TREE_CODE (dest1
) != TREE_CODE (dest2
) || TREE_CODE (dest1
) != SSA_NAME
)
982 return compare_operand (dest1
, dest2
);
985 /* Verifies for given GIMPLE_RESX stmts S1 and S2 that
986 resx statements are semantically equivalent. */
989 func_checker::compare_gimple_resx (const gresx
*g1
, const gresx
*g2
)
991 return gimple_resx_region (g1
) == gimple_resx_region (g2
);
994 /* Verifies for given GIMPLEs S1 and S2 that ASM statements are equivalent.
995 For the beginning, the pass only supports equality for
996 '__asm__ __volatile__ ("", "", "", "memory")'. */
999 func_checker::compare_gimple_asm (const gasm
*g1
, const gasm
*g2
)
1001 if (gimple_asm_volatile_p (g1
) != gimple_asm_volatile_p (g2
))
1004 if (gimple_asm_ninputs (g1
) != gimple_asm_ninputs (g2
))
1007 if (gimple_asm_noutputs (g1
) != gimple_asm_noutputs (g2
))
1010 /* We do not suppport goto ASM statement comparison. */
1011 if (gimple_asm_nlabels (g1
) || gimple_asm_nlabels (g2
))
1014 if (gimple_asm_nclobbers (g1
) != gimple_asm_nclobbers (g2
))
1017 if (strcmp (gimple_asm_string (g1
), gimple_asm_string (g2
)) != 0)
1018 return return_false_with_msg ("ASM strings are different");
1020 for (unsigned i
= 0; i
< gimple_asm_ninputs (g1
); i
++)
1022 tree input1
= gimple_asm_input_op (g1
, i
);
1023 tree input2
= gimple_asm_input_op (g2
, i
);
1025 if (!compare_tree_list_operand (input1
, input2
))
1026 return return_false_with_msg ("ASM input is different");
1029 for (unsigned i
= 0; i
< gimple_asm_noutputs (g1
); i
++)
1031 tree output1
= gimple_asm_output_op (g1
, i
);
1032 tree output2
= gimple_asm_output_op (g2
, i
);
1034 if (!compare_tree_list_operand (output1
, output2
))
1035 return return_false_with_msg ("ASM output is different");
1038 for (unsigned i
= 0; i
< gimple_asm_nclobbers (g1
); i
++)
1040 tree clobber1
= gimple_asm_clobber_op (g1
, i
);
1041 tree clobber2
= gimple_asm_clobber_op (g2
, i
);
1043 if (!operand_equal_p (TREE_VALUE (clobber1
), TREE_VALUE (clobber2
),
1045 return return_false_with_msg ("ASM clobber is different");
1051 } // ipa_icf_gimple namespace