2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ipa-icf-gimple.c
blob91e18ae7fab3327c9d95ada53df9427bdc4fa6cb
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 "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "options.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "predict.h"
32 #include "tm.h"
33 #include "hard-reg-set.h"
34 #include "function.h"
35 #include "basic-block.h"
36 #include "tree-ssa-alias.h"
37 #include "internal-fn.h"
38 #include "gimple-expr.h"
39 #include "is-a.h"
40 #include "gimple.h"
41 #include "rtl.h"
42 #include "flags.h"
43 #include "insn-config.h"
44 #include "expmed.h"
45 #include "dojump.h"
46 #include "explow.h"
47 #include "calls.h"
48 #include "emit-rtl.h"
49 #include "varasm.h"
50 #include "stmt.h"
51 #include "expr.h"
52 #include "gimple-iterator.h"
53 #include "gimple-ssa.h"
54 #include "tree-cfg.h"
55 #include "stringpool.h"
56 #include "tree-dfa.h"
57 #include "tree-pass.h"
58 #include "gimple-pretty-print.h"
59 #include "cfgloop.h"
60 #include "except.h"
61 #include "plugin-api.h"
62 #include "ipa-ref.h"
63 #include "cgraph.h"
64 #include "data-streamer.h"
65 #include "ipa-utils.h"
66 #include <list>
67 #include "tree-ssanames.h"
68 #include "tree-eh.h"
69 #include "builtins.h"
71 #include "ipa-icf-gimple.h"
72 #include "ipa-icf.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,
85 bool ignore_labels,
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. */
120 bool
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)
132 return false;
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)
137 return false;
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)
145 return true;
147 if (b1 == NULL || b2 == NULL || TREE_CODE (b1) != TREE_CODE (b2))
148 return return_false ();
150 return compare_cst_or_decl (b1, b2);
153 return true;
156 /* Verification function for edges E1 and E2. */
158 bool
159 func_checker::compare_edge (edge e1, edge e2)
161 if (e1->flags != e2->flags)
162 return false;
164 bool existed_p;
166 edge &slot = m_edge_map.get_or_insert (e1, &existed_p);
167 if (existed_p)
168 return return_with_debug (slot == e2);
169 else
170 slot = e2;
172 /* TODO: filter edge probabilities for profile feedback match. */
174 return true;
177 /* Verification function for declaration trees T1 and T2 that
178 come from functions FUNC1 and FUNC2. */
180 bool
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),
200 false))
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),
207 true))
208 return return_false ();
210 bool existed_p;
212 tree &slot = m_decl_map.get_or_insert (t1, &existed_p);
213 if (existed_p)
214 return return_with_debug (slot == t2);
215 else
216 slot = t2;
218 return true;
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
223 considered. */
225 bool
226 func_checker::compatible_polymorphic_types_p (tree t1, tree t2,
227 bool compare_ptr)
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))
234 if (!compare_ptr)
235 return true;
236 return func_checker::compatible_polymorphic_types_p (TREE_TYPE (t1),
237 TREE_TYPE (t2),
238 false);
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);
244 if (!c1 && !c2)
245 return true;
246 if (!c1 || !c2)
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");
250 return true;
253 /* Return true if types are compatible from perspective of ICF. */
254 bool
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");
269 return true;
272 /* Function compare for equality given memory operands T1 and T2. */
274 bool
275 func_checker::compare_memory_operand (tree t1, tree t2)
277 if (!t1 && !t2)
278 return true;
279 else if (!t1 || !t2)
280 return false;
282 ao_ref r1, r2;
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. */
312 b1 = t1;
313 while (handled_component_p (b1))
314 b1 = TREE_OPERAND (b1, 0);
315 b2 = t2;
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. */
349 bool
350 func_checker::compare_cst_or_decl (tree t1, tree t2)
352 bool ret;
354 switch (TREE_CODE (t1))
356 case INTEGER_CST:
357 case COMPLEX_CST:
358 case VECTOR_CST:
359 case STRING_CST:
360 case REAL_CST:
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);
366 case FUNCTION_DECL:
367 /* All function decls are in the symbol table and known to match
368 before we start comparing bodies. */
369 return true;
370 case VAR_DECL:
371 return return_with_debug (compare_variable_decl (t1, t2));
372 case FIELD_DECL:
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);
385 case LABEL_DECL:
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);
392 case PARM_DECL:
393 case RESULT_DECL:
394 case CONST_DECL:
396 ret = compare_decl (t1, t2);
397 return return_with_debug (ret);
399 default:
400 gcc_unreachable ();
404 /* Function responsible for comparison of various operands T1 and T2.
405 If these components, from functions FUNC1 and FUNC2, are equal, true
406 is returned. */
408 bool
409 func_checker::compare_operand (tree t1, tree t2)
411 tree x1, x2, y1, y2, z1, z2;
412 bool ret;
414 if (!t1 && !t2)
415 return true;
416 else if (!t1 || !t2)
417 return false;
419 tree tt1 = TREE_TYPE (t1);
420 tree tt2 = TREE_TYPE (t2);
422 if (!func_checker::compatible_types_p (tt1, tt2))
423 return false;
425 if (TREE_CODE (t1) != TREE_CODE (t2))
426 return return_false ();
428 switch (TREE_CODE (t1))
430 case CONSTRUCTOR:
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();
443 return true;
445 case ARRAY_REF:
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);
463 case MEM_REF:
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
471 gimple_load_p).
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
476 both directions. */
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);
487 case COMPONENT_REF:
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. */
500 case OBJ_TYPE_REF:
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);
520 case IMAGPART_EXPR:
521 case REALPART_EXPR:
522 case ADDR_EXPR:
524 x1 = TREE_OPERAND (t1, 0);
525 x2 = TREE_OPERAND (t2, 0);
527 ret = compare_operand (x1, x2);
528 return return_with_debug (ret);
530 case BIT_FIELD_REF:
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);
545 case SSA_NAME:
546 return compare_ssa_name (t1, t2);
547 case INTEGER_CST:
548 case COMPLEX_CST:
549 case VECTOR_CST:
550 case STRING_CST:
551 case REAL_CST:
552 case FUNCTION_DECL:
553 case VAR_DECL:
554 case FIELD_DECL:
555 case LABEL_DECL:
556 case PARM_DECL:
557 case RESULT_DECL:
558 case CONST_DECL:
559 return compare_cst_or_decl (t1, t2);
560 default:
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. */
568 bool
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))
576 if (!t2)
577 return false;
579 if (!compare_operand (TREE_VALUE (t1), TREE_VALUE (t2)))
580 return return_false ();
582 t2 = TREE_CHAIN (t2);
585 if (t2)
586 return return_false ();
588 return true;
591 /* Verifies that trees T1 and T2 do correspond. */
593 bool
594 func_checker::compare_variable_decl (tree t1, tree t2)
596 bool ret = false;
598 if (t1 == t2)
599 return true;
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
612 bodies. */
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. */
624 void
625 func_checker::parse_labels (sem_bb *bb)
627 for (gimple_stmt_iterator gsi = gsi_start_bb (bb->bb); !gsi_end_p (gsi);
628 gsi_next (&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. */
649 bool
650 func_checker::compare_bb (sem_bb *bb1, sem_bb *bb2)
652 gimple_stmt_iterator gsi1, gsi2;
653 gimple s1, s2;
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);
671 if (eh1 != eh2)
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))
679 case GIMPLE_CALL:
680 if (!compare_gimple_call (as_a <gcall *> (s1),
681 as_a <gcall *> (s2)))
682 return return_different_stmts (s1, s2, "GIMPLE_CALL");
683 break;
684 case GIMPLE_ASSIGN:
685 if (!compare_gimple_assign (s1, s2))
686 return return_different_stmts (s1, s2, "GIMPLE_ASSIGN");
687 break;
688 case GIMPLE_COND:
689 if (!compare_gimple_cond (s1, s2))
690 return return_different_stmts (s1, s2, "GIMPLE_COND");
691 break;
692 case GIMPLE_SWITCH:
693 if (!compare_gimple_switch (as_a <gswitch *> (s1),
694 as_a <gswitch *> (s2)))
695 return return_different_stmts (s1, s2, "GIMPLE_SWITCH");
696 break;
697 case GIMPLE_DEBUG:
698 break;
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");
703 break;
704 case GIMPLE_RESX:
705 if (!compare_gimple_resx (as_a <gresx *> (s1),
706 as_a <gresx *> (s2)))
707 return return_different_stmts (s1, s2, "GIMPLE_RESX");
708 break;
709 case GIMPLE_LABEL:
710 if (!compare_gimple_label (as_a <glabel *> (s1),
711 as_a <glabel *> (s2)))
712 return return_different_stmts (s1, s2, "GIMPLE_LABEL");
713 break;
714 case GIMPLE_RETURN:
715 if (!compare_gimple_return (as_a <greturn *> (s1),
716 as_a <greturn *> (s2)))
717 return return_different_stmts (s1, s2, "GIMPLE_RETURN");
718 break;
719 case GIMPLE_GOTO:
720 if (!compare_gimple_goto (s1, s2))
721 return return_different_stmts (s1, s2, "GIMPLE_GOTO");
722 break;
723 case GIMPLE_ASM:
724 if (!compare_gimple_asm (as_a <gasm *> (s1),
725 as_a <gasm *> (s2)))
726 return return_different_stmts (s1, s2, "GIMPLE_ASM");
727 break;
728 case GIMPLE_PREDICT:
729 case GIMPLE_NOP:
730 break;
731 default:
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 ();
742 return true;
745 /* Verifies for given GIMPLEs S1 and S2 that
746 call statements are semantically equivalent. */
748 bool
749 func_checker::compare_gimple_call (gcall *s1, gcall *s2)
751 unsigned i;
752 tree t1, t2;
754 if (gimple_call_num_args (s1) != gimple_call_num_args (s2))
755 return false;
757 t1 = gimple_call_fn (s1);
758 t2 = gimple_call_fn (s2);
759 if (!compare_operand (t1, t2))
760 return return_false ();
762 /* Compare flags. */
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))
771 return false;
773 if (gimple_call_internal_p (s1)
774 && gimple_call_internal_fn (s1) != gimple_call_internal_fn (s2))
775 return false;
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. */
812 bool
813 func_checker::compare_gimple_assign (gimple s1, gimple s2)
815 tree arg1, arg2;
816 tree_code code1, code2;
817 unsigned i;
819 code1 = gimple_expr_code (s1);
820 code2 = gimple_expr_code (s2);
822 if (code1 != code2)
823 return false;
825 code1 = gimple_assign_rhs_code (s1);
826 code2 = gimple_assign_rhs_code (s2);
828 if (code1 != code2)
829 return false;
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");
841 return true;
844 /* Verifies for given GIMPLEs S1 and S2 that
845 condition statements are semantically equivalent. */
847 bool
848 func_checker::compare_gimple_cond (gimple s1, gimple s2)
850 tree t1, t2;
851 tree_code code1, code2;
853 code1 = gimple_expr_code (s1);
854 code2 = gimple_expr_code (s2);
856 if (code1 != code2)
857 return false;
859 t1 = gimple_cond_lhs (s1);
860 t2 = gimple_cond_lhs (s2);
862 if (!compare_operand (t1, t2))
863 return false;
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. */
873 bool
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. */
882 bool
883 func_checker::compare_gimple_label (const glabel *g1, const glabel *g2)
885 if (m_ignore_labels)
886 return true;
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. */
895 return true;
898 /* Verifies for given GIMPLE_SWITCH stmts S1 and S2 that
899 switch statements are semantically equivalent. */
901 bool
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)
910 return false;
912 tree t1 = gimple_switch_index (g1);
913 tree t2 = gimple_switch_index (g2);
915 if (!compare_operand (t1, t2))
916 return false;
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");
949 return true;
952 /* Verifies for given GIMPLE_RETURN stmts S1 and S2 that
953 return statements are semantically equivalent. */
955 bool
956 func_checker::compare_gimple_return (const greturn *g1, const greturn *g2)
958 tree t1, t2;
960 t1 = gimple_return_retval (g1);
961 t2 = gimple_return_retval (g2);
963 /* Void return type. */
964 if (t1 == NULL && t2 == NULL)
965 return true;
966 else
967 return compare_operand (t1, t2);
970 /* Verifies for given GIMPLEs S1 and S2 that
971 goto statements are semantically equivalent. */
973 bool
974 func_checker::compare_gimple_goto (gimple g1, gimple g2)
976 tree dest1, dest2;
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)
982 return false;
984 return compare_operand (dest1, dest2);
987 /* Verifies for given GIMPLE_RESX stmts S1 and S2 that
988 resx statements are semantically equivalent. */
990 bool
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")'. */
1000 bool
1001 func_checker::compare_gimple_asm (const gasm *g1, const gasm *g2)
1003 if (gimple_asm_volatile_p (g1) != gimple_asm_volatile_p (g2))
1004 return false;
1006 if (gimple_asm_ninputs (g1) != gimple_asm_ninputs (g2))
1007 return false;
1009 if (gimple_asm_noutputs (g1) != gimple_asm_noutputs (g2))
1010 return false;
1012 /* We do not suppport goto ASM statement comparison. */
1013 if (gimple_asm_nlabels (g1) || gimple_asm_nlabels (g2))
1014 return false;
1016 if (gimple_asm_nclobbers (g1) != gimple_asm_nclobbers (g2))
1017 return false;
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),
1046 OEP_ONLY_CONST))
1047 return return_false_with_msg ("ASM clobber is different");
1050 return true;
1053 } // ipa_icf_gimple namespace