2015-06-23 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ipa-icf-gimple.c
blobc151cd1a36fbc793fea14c0103a45f55e1c59cc7
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
11 version.
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
16 for more details.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "options.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "predict.h"
31 #include "tm.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "basic-block.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-expr.h"
38 #include "gimple.h"
39 #include "rtl.h"
40 #include "flags.h"
41 #include "insn-config.h"
42 #include "expmed.h"
43 #include "dojump.h"
44 #include "explow.h"
45 #include "calls.h"
46 #include "emit-rtl.h"
47 #include "varasm.h"
48 #include "stmt.h"
49 #include "expr.h"
50 #include "gimple-iterator.h"
51 #include "gimple-ssa.h"
52 #include "tree-cfg.h"
53 #include "stringpool.h"
54 #include "tree-dfa.h"
55 #include "tree-pass.h"
56 #include "gimple-pretty-print.h"
57 #include "cfgloop.h"
58 #include "except.h"
59 #include "plugin-api.h"
60 #include "ipa-ref.h"
61 #include "cgraph.h"
62 #include "data-streamer.h"
63 #include "ipa-utils.h"
64 #include <list>
65 #include "tree-ssanames.h"
66 #include "tree-eh.h"
67 #include "builtins.h"
69 #include "ipa-icf-gimple.h"
70 #include "ipa-icf.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,
83 bool ignore_labels,
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. */
118 bool
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)
130 return false;
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)
135 return false;
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)
143 return true;
145 if (b1 == NULL || b2 == NULL || TREE_CODE (b1) != TREE_CODE (b2))
146 return return_false ();
148 return compare_cst_or_decl (b1, b2);
151 return true;
154 /* Verification function for edges E1 and E2. */
156 bool
157 func_checker::compare_edge (edge e1, edge e2)
159 if (e1->flags != e2->flags)
160 return false;
162 bool existed_p;
164 edge &slot = m_edge_map.get_or_insert (e1, &existed_p);
165 if (existed_p)
166 return return_with_debug (slot == e2);
167 else
168 slot = e2;
170 /* TODO: filter edge probabilities for profile feedback match. */
172 return true;
175 /* Verification function for declaration trees T1 and T2 that
176 come from functions FUNC1 and FUNC2. */
178 bool
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),
198 false))
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),
205 true))
206 return return_false ();
208 bool existed_p;
210 tree &slot = m_decl_map.get_or_insert (t1, &existed_p);
211 if (existed_p)
212 return return_with_debug (slot == t2);
213 else
214 slot = t2;
216 return true;
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
221 considered. */
223 bool
224 func_checker::compatible_polymorphic_types_p (tree t1, tree t2,
225 bool compare_ptr)
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))
232 if (!compare_ptr)
233 return true;
234 return func_checker::compatible_polymorphic_types_p (TREE_TYPE (t1),
235 TREE_TYPE (t2),
236 false);
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);
242 if (!c1 && !c2)
243 return true;
244 if (!c1 || !c2)
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");
248 return true;
251 /* Return true if types are compatible from perspective of ICF. */
252 bool
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");
267 return true;
270 /* Function compare for equality given memory operands T1 and T2. */
272 bool
273 func_checker::compare_memory_operand (tree t1, tree t2)
275 if (!t1 && !t2)
276 return true;
277 else if (!t1 || !t2)
278 return false;
280 ao_ref r1, r2;
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. */
310 b1 = t1;
311 while (handled_component_p (b1))
312 b1 = TREE_OPERAND (b1, 0);
313 b2 = t2;
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. */
347 bool
348 func_checker::compare_cst_or_decl (tree t1, tree t2)
350 bool ret;
352 switch (TREE_CODE (t1))
354 case INTEGER_CST:
355 case COMPLEX_CST:
356 case VECTOR_CST:
357 case STRING_CST:
358 case REAL_CST:
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);
364 case FUNCTION_DECL:
365 /* All function decls are in the symbol table and known to match
366 before we start comparing bodies. */
367 return true;
368 case VAR_DECL:
369 return return_with_debug (compare_variable_decl (t1, t2));
370 case FIELD_DECL:
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);
383 case LABEL_DECL:
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);
390 case PARM_DECL:
391 case RESULT_DECL:
392 case CONST_DECL:
394 ret = compare_decl (t1, t2);
395 return return_with_debug (ret);
397 default:
398 gcc_unreachable ();
402 /* Function responsible for comparison of various operands T1 and T2.
403 If these components, from functions FUNC1 and FUNC2, are equal, true
404 is returned. */
406 bool
407 func_checker::compare_operand (tree t1, tree t2)
409 tree x1, x2, y1, y2, z1, z2;
410 bool ret;
412 if (!t1 && !t2)
413 return true;
414 else if (!t1 || !t2)
415 return false;
417 tree tt1 = TREE_TYPE (t1);
418 tree tt2 = TREE_TYPE (t2);
420 if (!func_checker::compatible_types_p (tt1, tt2))
421 return false;
423 if (TREE_CODE (t1) != TREE_CODE (t2))
424 return return_false ();
426 switch (TREE_CODE (t1))
428 case CONSTRUCTOR:
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();
441 return true;
443 case ARRAY_REF:
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);
461 case MEM_REF:
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
469 gimple_load_p).
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
474 both directions. */
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);
485 case COMPONENT_REF:
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. */
498 case OBJ_TYPE_REF:
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);
518 case IMAGPART_EXPR:
519 case REALPART_EXPR:
520 case ADDR_EXPR:
522 x1 = TREE_OPERAND (t1, 0);
523 x2 = TREE_OPERAND (t2, 0);
525 ret = compare_operand (x1, x2);
526 return return_with_debug (ret);
528 case BIT_FIELD_REF:
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);
543 case SSA_NAME:
544 return compare_ssa_name (t1, t2);
545 case INTEGER_CST:
546 case COMPLEX_CST:
547 case VECTOR_CST:
548 case STRING_CST:
549 case REAL_CST:
550 case FUNCTION_DECL:
551 case VAR_DECL:
552 case FIELD_DECL:
553 case LABEL_DECL:
554 case PARM_DECL:
555 case RESULT_DECL:
556 case CONST_DECL:
557 return compare_cst_or_decl (t1, t2);
558 default:
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. */
566 bool
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))
574 if (!t2)
575 return false;
577 if (!compare_operand (TREE_VALUE (t1), TREE_VALUE (t2)))
578 return return_false ();
580 t2 = TREE_CHAIN (t2);
583 if (t2)
584 return return_false ();
586 return true;
589 /* Verifies that trees T1 and T2 do correspond. */
591 bool
592 func_checker::compare_variable_decl (tree t1, tree t2)
594 bool ret = false;
596 if (t1 == t2)
597 return true;
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
610 bodies. */
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. */
622 void
623 func_checker::parse_labels (sem_bb *bb)
625 for (gimple_stmt_iterator gsi = gsi_start_bb (bb->bb); !gsi_end_p (gsi);
626 gsi_next (&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. */
647 bool
648 func_checker::compare_bb (sem_bb *bb1, sem_bb *bb2)
650 gimple_stmt_iterator gsi1, gsi2;
651 gimple s1, s2;
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);
669 if (eh1 != eh2)
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))
677 case GIMPLE_CALL:
678 if (!compare_gimple_call (as_a <gcall *> (s1),
679 as_a <gcall *> (s2)))
680 return return_different_stmts (s1, s2, "GIMPLE_CALL");
681 break;
682 case GIMPLE_ASSIGN:
683 if (!compare_gimple_assign (s1, s2))
684 return return_different_stmts (s1, s2, "GIMPLE_ASSIGN");
685 break;
686 case GIMPLE_COND:
687 if (!compare_gimple_cond (s1, s2))
688 return return_different_stmts (s1, s2, "GIMPLE_COND");
689 break;
690 case GIMPLE_SWITCH:
691 if (!compare_gimple_switch (as_a <gswitch *> (s1),
692 as_a <gswitch *> (s2)))
693 return return_different_stmts (s1, s2, "GIMPLE_SWITCH");
694 break;
695 case GIMPLE_DEBUG:
696 break;
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");
701 break;
702 case GIMPLE_RESX:
703 if (!compare_gimple_resx (as_a <gresx *> (s1),
704 as_a <gresx *> (s2)))
705 return return_different_stmts (s1, s2, "GIMPLE_RESX");
706 break;
707 case GIMPLE_LABEL:
708 if (!compare_gimple_label (as_a <glabel *> (s1),
709 as_a <glabel *> (s2)))
710 return return_different_stmts (s1, s2, "GIMPLE_LABEL");
711 break;
712 case GIMPLE_RETURN:
713 if (!compare_gimple_return (as_a <greturn *> (s1),
714 as_a <greturn *> (s2)))
715 return return_different_stmts (s1, s2, "GIMPLE_RETURN");
716 break;
717 case GIMPLE_GOTO:
718 if (!compare_gimple_goto (s1, s2))
719 return return_different_stmts (s1, s2, "GIMPLE_GOTO");
720 break;
721 case GIMPLE_ASM:
722 if (!compare_gimple_asm (as_a <gasm *> (s1),
723 as_a <gasm *> (s2)))
724 return return_different_stmts (s1, s2, "GIMPLE_ASM");
725 break;
726 case GIMPLE_PREDICT:
727 case GIMPLE_NOP:
728 break;
729 default:
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 ();
740 return true;
743 /* Verifies for given GIMPLEs S1 and S2 that
744 call statements are semantically equivalent. */
746 bool
747 func_checker::compare_gimple_call (gcall *s1, gcall *s2)
749 unsigned i;
750 tree t1, t2;
752 if (gimple_call_num_args (s1) != gimple_call_num_args (s2))
753 return false;
755 t1 = gimple_call_fn (s1);
756 t2 = gimple_call_fn (s2);
757 if (!compare_operand (t1, t2))
758 return return_false ();
760 /* Compare flags. */
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))
769 return false;
771 if (gimple_call_internal_p (s1)
772 && gimple_call_internal_fn (s1) != gimple_call_internal_fn (s2))
773 return false;
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. */
810 bool
811 func_checker::compare_gimple_assign (gimple s1, gimple s2)
813 tree arg1, arg2;
814 tree_code code1, code2;
815 unsigned i;
817 code1 = gimple_expr_code (s1);
818 code2 = gimple_expr_code (s2);
820 if (code1 != code2)
821 return false;
823 code1 = gimple_assign_rhs_code (s1);
824 code2 = gimple_assign_rhs_code (s2);
826 if (code1 != code2)
827 return false;
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");
839 return true;
842 /* Verifies for given GIMPLEs S1 and S2 that
843 condition statements are semantically equivalent. */
845 bool
846 func_checker::compare_gimple_cond (gimple s1, gimple s2)
848 tree t1, t2;
849 tree_code code1, code2;
851 code1 = gimple_expr_code (s1);
852 code2 = gimple_expr_code (s2);
854 if (code1 != code2)
855 return false;
857 t1 = gimple_cond_lhs (s1);
858 t2 = gimple_cond_lhs (s2);
860 if (!compare_operand (t1, t2))
861 return false;
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. */
871 bool
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. */
880 bool
881 func_checker::compare_gimple_label (const glabel *g1, const glabel *g2)
883 if (m_ignore_labels)
884 return true;
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. */
893 return true;
896 /* Verifies for given GIMPLE_SWITCH stmts S1 and S2 that
897 switch statements are semantically equivalent. */
899 bool
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)
908 return false;
910 tree t1 = gimple_switch_index (g1);
911 tree t2 = gimple_switch_index (g2);
913 if (!compare_operand (t1, t2))
914 return false;
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");
947 return true;
950 /* Verifies for given GIMPLE_RETURN stmts S1 and S2 that
951 return statements are semantically equivalent. */
953 bool
954 func_checker::compare_gimple_return (const greturn *g1, const greturn *g2)
956 tree t1, t2;
958 t1 = gimple_return_retval (g1);
959 t2 = gimple_return_retval (g2);
961 /* Void return type. */
962 if (t1 == NULL && t2 == NULL)
963 return true;
964 else
965 return compare_operand (t1, t2);
968 /* Verifies for given GIMPLEs S1 and S2 that
969 goto statements are semantically equivalent. */
971 bool
972 func_checker::compare_gimple_goto (gimple g1, gimple g2)
974 tree dest1, dest2;
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)
980 return false;
982 return compare_operand (dest1, dest2);
985 /* Verifies for given GIMPLE_RESX stmts S1 and S2 that
986 resx statements are semantically equivalent. */
988 bool
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")'. */
998 bool
999 func_checker::compare_gimple_asm (const gasm *g1, const gasm *g2)
1001 if (gimple_asm_volatile_p (g1) != gimple_asm_volatile_p (g2))
1002 return false;
1004 if (gimple_asm_ninputs (g1) != gimple_asm_ninputs (g2))
1005 return false;
1007 if (gimple_asm_noutputs (g1) != gimple_asm_noutputs (g2))
1008 return false;
1010 /* We do not suppport goto ASM statement comparison. */
1011 if (gimple_asm_nlabels (g1) || gimple_asm_nlabels (g2))
1012 return false;
1014 if (gimple_asm_nclobbers (g1) != gimple_asm_nclobbers (g2))
1015 return false;
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),
1044 OEP_ONLY_CONST))
1045 return return_false_with_msg ("ASM clobber is different");
1048 return true;
1051 } // ipa_icf_gimple namespace