jit: document union types
[official-gcc.git] / gcc / ipa-icf-gimple.c
blob8317dc1d5c860f34127eff5ded077c4f83820fdc
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 "cgraph.h"
60 #include "data-streamer.h"
61 #include "ipa-utils.h"
62 #include <list>
63 #include "tree-ssanames.h"
64 #include "tree-eh.h"
65 #include "builtins.h"
67 #include "ipa-icf-gimple.h"
68 #include "ipa-icf.h"
70 namespace ipa_icf_gimple {
72 /* Initialize internal structures for a given SOURCE_FUNC_DECL and
73 TARGET_FUNC_DECL. Strict polymorphic comparison is processed if
74 an option COMPARE_POLYMORPHIC is true. For special cases, one can
75 set IGNORE_LABELS to skip label comparison.
76 Similarly, IGNORE_SOURCE_DECLS and IGNORE_TARGET_DECLS are sets
77 of declarations that can be skipped. */
79 func_checker::func_checker (tree source_func_decl, tree target_func_decl,
80 bool compare_polymorphic,
81 bool ignore_labels,
82 hash_set<symtab_node *> *ignored_source_nodes,
83 hash_set<symtab_node *> *ignored_target_nodes)
84 : m_source_func_decl (source_func_decl), m_target_func_decl (target_func_decl),
85 m_ignored_source_nodes (ignored_source_nodes),
86 m_ignored_target_nodes (ignored_target_nodes),
87 m_compare_polymorphic (compare_polymorphic),
88 m_ignore_labels (ignore_labels)
90 function *source_func = DECL_STRUCT_FUNCTION (source_func_decl);
91 function *target_func = DECL_STRUCT_FUNCTION (target_func_decl);
93 unsigned ssa_source = SSANAMES (source_func)->length ();
94 unsigned ssa_target = SSANAMES (target_func)->length ();
96 m_source_ssa_names.create (ssa_source);
97 m_target_ssa_names.create (ssa_target);
99 for (unsigned i = 0; i < ssa_source; i++)
100 m_source_ssa_names.safe_push (-1);
102 for (unsigned i = 0; i < ssa_target; i++)
103 m_target_ssa_names.safe_push (-1);
106 /* Memory release routine. */
108 func_checker::~func_checker ()
110 m_source_ssa_names.release();
111 m_target_ssa_names.release();
114 /* Verifies that trees T1 and T2 are equivalent from perspective of ICF. */
116 bool
117 func_checker::compare_ssa_name (tree t1, tree t2)
119 gcc_assert (TREE_CODE (t1) == SSA_NAME);
120 gcc_assert (TREE_CODE (t2) == SSA_NAME);
122 unsigned i1 = SSA_NAME_VERSION (t1);
123 unsigned i2 = SSA_NAME_VERSION (t2);
125 if (m_source_ssa_names[i1] == -1)
126 m_source_ssa_names[i1] = i2;
127 else if (m_source_ssa_names[i1] != (int) i2)
128 return false;
130 if(m_target_ssa_names[i2] == -1)
131 m_target_ssa_names[i2] = i1;
132 else if (m_target_ssa_names[i2] != (int) i1)
133 return false;
135 if (SSA_NAME_IS_DEFAULT_DEF (t1))
137 tree b1 = SSA_NAME_VAR (t1);
138 tree b2 = SSA_NAME_VAR (t2);
140 if (b1 == NULL && b2 == NULL)
141 return true;
143 if (b1 == NULL || b2 == NULL || TREE_CODE (b1) != TREE_CODE (b2))
144 return return_false ();
146 return compare_cst_or_decl (b1, b2);
149 return true;
152 /* Verification function for edges E1 and E2. */
154 bool
155 func_checker::compare_edge (edge e1, edge e2)
157 if (e1->flags != e2->flags)
158 return false;
160 bool existed_p;
162 edge &slot = m_edge_map.get_or_insert (e1, &existed_p);
163 if (existed_p)
164 return return_with_debug (slot == e2);
165 else
166 slot = e2;
168 /* TODO: filter edge probabilities for profile feedback match. */
170 return true;
173 /* Verification function for declaration trees T1 and T2 that
174 come from functions FUNC1 and FUNC2. */
176 bool
177 func_checker::compare_decl (tree t1, tree t2)
179 if (!auto_var_in_fn_p (t1, m_source_func_decl)
180 || !auto_var_in_fn_p (t2, m_target_func_decl))
181 return return_with_debug (t1 == t2);
183 tree_code t = TREE_CODE (t1);
184 if ((t == VAR_DECL || t == PARM_DECL || t == RESULT_DECL)
185 && DECL_BY_REFERENCE (t1) != DECL_BY_REFERENCE (t2))
186 return return_false_with_msg ("DECL_BY_REFERENCE flags are different");
188 if (!compatible_types_p (TREE_TYPE (t1), TREE_TYPE (t2)))
189 return return_false ();
191 /* TODO: we are actually too strict here. We only need to compare if
192 T1 can be used in polymorphic call. */
193 if (TREE_ADDRESSABLE (t1)
194 && m_compare_polymorphic
195 && !compatible_polymorphic_types_p (TREE_TYPE (t1), TREE_TYPE (t2),
196 false))
197 return return_false ();
199 if ((t == VAR_DECL || t == PARM_DECL || t == RESULT_DECL)
200 && DECL_BY_REFERENCE (t1)
201 && m_compare_polymorphic
202 && !compatible_polymorphic_types_p (TREE_TYPE (t1), TREE_TYPE (t2),
203 true))
204 return return_false ();
206 bool existed_p;
208 tree &slot = m_decl_map.get_or_insert (t1, &existed_p);
209 if (existed_p)
210 return return_with_debug (slot == t2);
211 else
212 slot = t2;
214 return true;
217 /* Return true if T1 and T2 are same for purposes of ipa-polymorphic-call
218 analysis. COMPARE_PTR indicates if types of pointers needs to be
219 considered. */
221 bool
222 func_checker::compatible_polymorphic_types_p (tree t1, tree t2,
223 bool compare_ptr)
225 gcc_assert (TREE_CODE (t1) != FUNCTION_TYPE && TREE_CODE (t1) != METHOD_TYPE);
227 /* Pointer types generally give no information. */
228 if (POINTER_TYPE_P (t1))
230 if (!compare_ptr)
231 return true;
232 return func_checker::compatible_polymorphic_types_p (TREE_TYPE (t1),
233 TREE_TYPE (t2),
234 false);
237 /* If types contain a polymorphic types, match them. */
238 bool c1 = contains_polymorphic_type_p (t1);
239 bool c2 = contains_polymorphic_type_p (t2);
240 if (!c1 && !c2)
241 return true;
242 if (!c1 || !c2)
243 return return_false_with_msg ("one type is not polymorphic");
244 if (!types_must_be_same_for_odr (t1, t2))
245 return return_false_with_msg ("types are not same for ODR");
246 return true;
249 /* Return true if types are compatible from perspective of ICF. */
250 bool
251 func_checker::compatible_types_p (tree t1, tree t2)
253 if (TREE_CODE (t1) != TREE_CODE (t2))
254 return return_false_with_msg ("different tree types");
256 if (TYPE_RESTRICT (t1) != TYPE_RESTRICT (t2))
257 return return_false_with_msg ("restrict flags are different");
259 if (!types_compatible_p (t1, t2))
260 return return_false_with_msg ("types are not compatible");
262 if (get_alias_set (t1) != get_alias_set (t2))
263 return return_false_with_msg ("alias sets are different");
265 return true;
268 /* Function compare for equality given memory operands T1 and T2. */
270 bool
271 func_checker::compare_memory_operand (tree t1, tree t2)
273 if (!t1 && !t2)
274 return true;
275 else if (!t1 || !t2)
276 return false;
278 ao_ref r1, r2;
279 ao_ref_init (&r1, t1);
280 ao_ref_init (&r2, t2);
282 tree b1 = ao_ref_base (&r1);
283 tree b2 = ao_ref_base (&r2);
285 bool source_is_memop = DECL_P (b1) || INDIRECT_REF_P (b1)
286 || TREE_CODE (b1) == MEM_REF
287 || TREE_CODE (b1) == TARGET_MEM_REF;
289 bool target_is_memop = DECL_P (b2) || INDIRECT_REF_P (b2)
290 || TREE_CODE (b2) == MEM_REF
291 || TREE_CODE (b2) == TARGET_MEM_REF;
293 /* Compare alias sets for memory operands. */
294 if (source_is_memop && target_is_memop)
296 if (TREE_THIS_VOLATILE (t1) != TREE_THIS_VOLATILE (t2))
297 return return_false_with_msg ("different operand volatility");
299 if (ao_ref_alias_set (&r1) != ao_ref_alias_set (&r2)
300 || ao_ref_base_alias_set (&r1) != ao_ref_base_alias_set (&r2))
301 return return_false_with_msg ("ao alias sets are different");
303 /* We can't simply use get_object_alignment_1 on the full
304 reference as for accesses with variable indexes this reports
305 too conservative alignment. We also can't use the ao_ref_base
306 base objects as ao_ref_base happily strips MEM_REFs around
307 decls even though that may carry alignment info. */
308 b1 = t1;
309 while (handled_component_p (b1))
310 b1 = TREE_OPERAND (b1, 0);
311 b2 = t2;
312 while (handled_component_p (b2))
313 b2 = TREE_OPERAND (b2, 0);
314 unsigned int align1, align2;
315 unsigned HOST_WIDE_INT tem;
316 get_object_alignment_1 (b1, &align1, &tem);
317 get_object_alignment_1 (b2, &align2, &tem);
318 if (align1 != align2)
319 return return_false_with_msg ("different access alignment");
321 /* Similarly we have to compare dependence info where equality
322 tells us we are safe (even some unequal values would be safe
323 but then we have to maintain a map of bases and cliques). */
324 unsigned short clique1 = 0, base1 = 0, clique2 = 0, base2 = 0;
325 if (TREE_CODE (b1) == MEM_REF)
327 clique1 = MR_DEPENDENCE_CLIQUE (b1);
328 base1 = MR_DEPENDENCE_BASE (b1);
330 if (TREE_CODE (b2) == MEM_REF)
332 clique2 = MR_DEPENDENCE_CLIQUE (b2);
333 base2 = MR_DEPENDENCE_BASE (b2);
335 if (clique1 != clique2 || base1 != base2)
336 return return_false_with_msg ("different dependence info");
339 return compare_operand (t1, t2);
342 /* Function compare for equality given trees T1 and T2 which
343 can be either a constant or a declaration type. */
345 bool
346 func_checker::compare_cst_or_decl (tree t1, tree t2)
348 bool ret;
350 switch (TREE_CODE (t1))
352 case INTEGER_CST:
353 case COMPLEX_CST:
354 case VECTOR_CST:
355 case STRING_CST:
356 case REAL_CST:
358 ret = compatible_types_p (TREE_TYPE (t1), TREE_TYPE (t2))
359 && operand_equal_p (t1, t2, OEP_ONLY_CONST);
360 return return_with_debug (ret);
362 case FUNCTION_DECL:
363 /* All function decls are in the symbol table and known to match
364 before we start comparing bodies. */
365 return true;
366 case VAR_DECL:
367 return return_with_debug (compare_variable_decl (t1, t2));
368 case FIELD_DECL:
370 tree offset1 = DECL_FIELD_OFFSET (t1);
371 tree offset2 = DECL_FIELD_OFFSET (t2);
373 tree bit_offset1 = DECL_FIELD_BIT_OFFSET (t1);
374 tree bit_offset2 = DECL_FIELD_BIT_OFFSET (t2);
376 ret = compare_operand (offset1, offset2)
377 && compare_operand (bit_offset1, bit_offset2);
379 return return_with_debug (ret);
381 case LABEL_DECL:
383 int *bb1 = m_label_bb_map.get (t1);
384 int *bb2 = m_label_bb_map.get (t2);
386 return return_with_debug (*bb1 == *bb2);
388 case PARM_DECL:
389 case RESULT_DECL:
390 case CONST_DECL:
392 ret = compare_decl (t1, t2);
393 return return_with_debug (ret);
395 default:
396 gcc_unreachable ();
400 /* Function responsible for comparison of various operands T1 and T2.
401 If these components, from functions FUNC1 and FUNC2, are equal, true
402 is returned. */
404 bool
405 func_checker::compare_operand (tree t1, tree t2)
407 tree x1, x2, y1, y2, z1, z2;
408 bool ret;
410 if (!t1 && !t2)
411 return true;
412 else if (!t1 || !t2)
413 return false;
415 tree tt1 = TREE_TYPE (t1);
416 tree tt2 = TREE_TYPE (t2);
418 if (!func_checker::compatible_types_p (tt1, tt2))
419 return false;
421 if (TREE_CODE (t1) != TREE_CODE (t2))
422 return return_false ();
424 switch (TREE_CODE (t1))
426 case CONSTRUCTOR:
428 unsigned length1 = vec_safe_length (CONSTRUCTOR_ELTS (t1));
429 unsigned length2 = vec_safe_length (CONSTRUCTOR_ELTS (t2));
431 if (length1 != length2)
432 return return_false ();
434 for (unsigned i = 0; i < length1; i++)
435 if (!compare_operand (CONSTRUCTOR_ELT (t1, i)->value,
436 CONSTRUCTOR_ELT (t2, i)->value))
437 return return_false();
439 return true;
441 case ARRAY_REF:
442 case ARRAY_RANGE_REF:
443 /* First argument is the array, second is the index. */
444 x1 = TREE_OPERAND (t1, 0);
445 x2 = TREE_OPERAND (t2, 0);
446 y1 = TREE_OPERAND (t1, 1);
447 y2 = TREE_OPERAND (t2, 1);
449 if (!compare_operand (array_ref_low_bound (t1),
450 array_ref_low_bound (t2)))
451 return return_false_with_msg ("");
452 if (!compare_operand (array_ref_element_size (t1),
453 array_ref_element_size (t2)))
454 return return_false_with_msg ("");
456 if (!compare_operand (x1, x2))
457 return return_false_with_msg ("");
458 return compare_operand (y1, y2);
459 case MEM_REF:
461 x1 = TREE_OPERAND (t1, 0);
462 x2 = TREE_OPERAND (t2, 0);
463 y1 = TREE_OPERAND (t1, 1);
464 y2 = TREE_OPERAND (t2, 1);
466 /* See if operand is an memory access (the test originate from
467 gimple_load_p).
469 In this case the alias set of the function being replaced must
470 be subset of the alias set of the other function. At the moment
471 we seek for equivalency classes, so simply require inclussion in
472 both directions. */
474 if (!func_checker::compatible_types_p (TREE_TYPE (x1), TREE_TYPE (x2)))
475 return return_false ();
477 if (!compare_operand (x1, x2))
478 return return_false_with_msg ("");
480 /* Type of the offset on MEM_REF does not matter. */
481 return wi::to_offset (y1) == wi::to_offset (y2);
483 case COMPONENT_REF:
485 x1 = TREE_OPERAND (t1, 0);
486 x2 = TREE_OPERAND (t2, 0);
487 y1 = TREE_OPERAND (t1, 1);
488 y2 = TREE_OPERAND (t2, 1);
490 ret = compare_operand (x1, x2)
491 && compare_cst_or_decl (y1, y2);
493 return return_with_debug (ret);
495 /* Virtual table call. */
496 case OBJ_TYPE_REF:
498 if (!compare_ssa_name (OBJ_TYPE_REF_EXPR (t1), OBJ_TYPE_REF_EXPR (t2)))
499 return return_false ();
500 if (opt_for_fn (m_source_func_decl, flag_devirtualize)
501 && virtual_method_call_p (t1))
503 if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (t1))
504 != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (t2)))
505 return return_false_with_msg ("OBJ_TYPE_REF token mismatch");
506 if (!types_same_for_odr (obj_type_ref_class (t1),
507 obj_type_ref_class (t2)))
508 return return_false_with_msg ("OBJ_TYPE_REF OTR type mismatch");
509 if (!compare_operand (OBJ_TYPE_REF_OBJECT (t1),
510 OBJ_TYPE_REF_OBJECT (t2)))
511 return return_false_with_msg ("OBJ_TYPE_REF object mismatch");
514 return return_with_debug (true);
516 case IMAGPART_EXPR:
517 case REALPART_EXPR:
518 case ADDR_EXPR:
520 x1 = TREE_OPERAND (t1, 0);
521 x2 = TREE_OPERAND (t2, 0);
523 ret = compare_operand (x1, x2);
524 return return_with_debug (ret);
526 case BIT_FIELD_REF:
528 x1 = TREE_OPERAND (t1, 0);
529 x2 = TREE_OPERAND (t2, 0);
530 y1 = TREE_OPERAND (t1, 1);
531 y2 = TREE_OPERAND (t2, 1);
532 z1 = TREE_OPERAND (t1, 2);
533 z2 = TREE_OPERAND (t2, 2);
535 ret = compare_operand (x1, x2)
536 && compare_cst_or_decl (y1, y2)
537 && compare_cst_or_decl (z1, z2);
539 return return_with_debug (ret);
541 case SSA_NAME:
542 return compare_ssa_name (t1, t2);
543 case INTEGER_CST:
544 case COMPLEX_CST:
545 case VECTOR_CST:
546 case STRING_CST:
547 case REAL_CST:
548 case FUNCTION_DECL:
549 case VAR_DECL:
550 case FIELD_DECL:
551 case LABEL_DECL:
552 case PARM_DECL:
553 case RESULT_DECL:
554 case CONST_DECL:
555 return compare_cst_or_decl (t1, t2);
556 default:
557 return return_false_with_msg ("Unknown TREE code reached");
561 /* Compares two tree list operands T1 and T2 and returns true if these
562 two trees are semantically equivalent. */
564 bool
565 func_checker::compare_tree_list_operand (tree t1, tree t2)
567 gcc_assert (TREE_CODE (t1) == TREE_LIST);
568 gcc_assert (TREE_CODE (t2) == TREE_LIST);
570 for (; t1; t1 = TREE_CHAIN (t1))
572 if (!t2)
573 return false;
575 if (!compare_operand (TREE_VALUE (t1), TREE_VALUE (t2)))
576 return return_false ();
578 t2 = TREE_CHAIN (t2);
581 if (t2)
582 return return_false ();
584 return true;
587 /* Verifies that trees T1 and T2 do correspond. */
589 bool
590 func_checker::compare_variable_decl (tree t1, tree t2)
592 bool ret = false;
594 if (t1 == t2)
595 return true;
597 if (DECL_ALIGN (t1) != DECL_ALIGN (t2))
598 return return_false_with_msg ("alignments are different");
600 if (DECL_HARD_REGISTER (t1) != DECL_HARD_REGISTER (t2))
601 return return_false_with_msg ("DECL_HARD_REGISTER are different");
603 if (DECL_HARD_REGISTER (t1)
604 && DECL_ASSEMBLER_NAME (t1) != DECL_ASSEMBLER_NAME (t2))
605 return return_false_with_msg ("HARD REGISTERS are different");
607 /* Symbol table variables are known to match before we start comparing
608 bodies. */
609 if (decl_in_symtab_p (t1))
610 return decl_in_symtab_p (t2);
611 ret = compare_decl (t1, t2);
613 return return_with_debug (ret);
617 /* Function visits all gimple labels and creates corresponding
618 mapping between basic blocks and labels. */
620 void
621 func_checker::parse_labels (sem_bb *bb)
623 for (gimple_stmt_iterator gsi = gsi_start_bb (bb->bb); !gsi_end_p (gsi);
624 gsi_next (&gsi))
626 gimple stmt = gsi_stmt (gsi);
628 if (glabel *label_stmt = dyn_cast <glabel *> (stmt))
630 tree t = gimple_label_label (label_stmt);
631 gcc_assert (TREE_CODE (t) == LABEL_DECL);
633 m_label_bb_map.put (t, bb->bb->index);
638 /* Basic block equivalence comparison function that returns true if
639 basic blocks BB1 and BB2 (from functions FUNC1 and FUNC2) correspond.
641 In general, a collection of equivalence dictionaries is built for types
642 like SSA names, declarations (VAR_DECL, PARM_DECL, ..). This infrastructure
643 is utilized by every statement-by-statement comparison function. */
645 bool
646 func_checker::compare_bb (sem_bb *bb1, sem_bb *bb2)
648 gimple_stmt_iterator gsi1, gsi2;
649 gimple s1, s2;
651 gsi1 = gsi_start_bb_nondebug (bb1->bb);
652 gsi2 = gsi_start_bb_nondebug (bb2->bb);
654 while (!gsi_end_p (gsi1))
656 if (gsi_end_p (gsi2))
657 return return_false ();
659 s1 = gsi_stmt (gsi1);
660 s2 = gsi_stmt (gsi2);
662 int eh1 = lookup_stmt_eh_lp_fn
663 (DECL_STRUCT_FUNCTION (m_source_func_decl), s1);
664 int eh2 = lookup_stmt_eh_lp_fn
665 (DECL_STRUCT_FUNCTION (m_target_func_decl), s2);
667 if (eh1 != eh2)
668 return return_false_with_msg ("EH regions are different");
670 if (gimple_code (s1) != gimple_code (s2))
671 return return_false_with_msg ("gimple codes are different");
673 switch (gimple_code (s1))
675 case GIMPLE_CALL:
676 if (!compare_gimple_call (as_a <gcall *> (s1),
677 as_a <gcall *> (s2)))
678 return return_different_stmts (s1, s2, "GIMPLE_CALL");
679 break;
680 case GIMPLE_ASSIGN:
681 if (!compare_gimple_assign (s1, s2))
682 return return_different_stmts (s1, s2, "GIMPLE_ASSIGN");
683 break;
684 case GIMPLE_COND:
685 if (!compare_gimple_cond (s1, s2))
686 return return_different_stmts (s1, s2, "GIMPLE_COND");
687 break;
688 case GIMPLE_SWITCH:
689 if (!compare_gimple_switch (as_a <gswitch *> (s1),
690 as_a <gswitch *> (s2)))
691 return return_different_stmts (s1, s2, "GIMPLE_SWITCH");
692 break;
693 case GIMPLE_DEBUG:
694 break;
695 case GIMPLE_EH_DISPATCH:
696 if (gimple_eh_dispatch_region (as_a <geh_dispatch *> (s1))
697 != gimple_eh_dispatch_region (as_a <geh_dispatch *> (s2)))
698 return return_different_stmts (s1, s2, "GIMPLE_EH_DISPATCH");
699 break;
700 case GIMPLE_RESX:
701 if (!compare_gimple_resx (as_a <gresx *> (s1),
702 as_a <gresx *> (s2)))
703 return return_different_stmts (s1, s2, "GIMPLE_RESX");
704 break;
705 case GIMPLE_LABEL:
706 if (!compare_gimple_label (as_a <glabel *> (s1),
707 as_a <glabel *> (s2)))
708 return return_different_stmts (s1, s2, "GIMPLE_LABEL");
709 break;
710 case GIMPLE_RETURN:
711 if (!compare_gimple_return (as_a <greturn *> (s1),
712 as_a <greturn *> (s2)))
713 return return_different_stmts (s1, s2, "GIMPLE_RETURN");
714 break;
715 case GIMPLE_GOTO:
716 if (!compare_gimple_goto (s1, s2))
717 return return_different_stmts (s1, s2, "GIMPLE_GOTO");
718 break;
719 case GIMPLE_ASM:
720 if (!compare_gimple_asm (as_a <gasm *> (s1),
721 as_a <gasm *> (s2)))
722 return return_different_stmts (s1, s2, "GIMPLE_ASM");
723 break;
724 case GIMPLE_PREDICT:
725 case GIMPLE_NOP:
726 break;
727 default:
728 return return_false_with_msg ("Unknown GIMPLE code reached");
731 gsi_next_nondebug (&gsi1);
732 gsi_next_nondebug (&gsi2);
735 if (!gsi_end_p (gsi2))
736 return return_false ();
738 return true;
741 /* Verifies for given GIMPLEs S1 and S2 that
742 call statements are semantically equivalent. */
744 bool
745 func_checker::compare_gimple_call (gcall *s1, gcall *s2)
747 unsigned i;
748 tree t1, t2;
750 if (gimple_call_num_args (s1) != gimple_call_num_args (s2))
751 return false;
753 t1 = gimple_call_fn (s1);
754 t2 = gimple_call_fn (s2);
755 if (!compare_operand (t1, t2))
756 return return_false ();
758 /* Compare flags. */
759 if (gimple_call_internal_p (s1) != gimple_call_internal_p (s2)
760 || gimple_call_ctrl_altering_p (s1) != gimple_call_ctrl_altering_p (s2)
761 || gimple_call_tail_p (s1) != gimple_call_tail_p (s2)
762 || gimple_call_return_slot_opt_p (s1) != gimple_call_return_slot_opt_p (s2)
763 || gimple_call_from_thunk_p (s1) != gimple_call_from_thunk_p (s2)
764 || gimple_call_va_arg_pack_p (s1) != gimple_call_va_arg_pack_p (s2)
765 || gimple_call_alloca_for_var_p (s1) != gimple_call_alloca_for_var_p (s2)
766 || gimple_call_with_bounds_p (s1) != gimple_call_with_bounds_p (s2))
767 return false;
769 if (gimple_call_internal_p (s1)
770 && gimple_call_internal_fn (s1) != gimple_call_internal_fn (s2))
771 return false;
773 tree fntype1 = gimple_call_fntype (s1);
774 tree fntype2 = gimple_call_fntype (s2);
775 if ((fntype1 && !fntype2)
776 || (!fntype1 && fntype2)
777 || (fntype1 && !types_compatible_p (fntype1, fntype2)))
778 return return_false_with_msg ("call function types are not compatible");
780 tree chain1 = gimple_call_chain (s1);
781 tree chain2 = gimple_call_chain (s2);
782 if ((chain1 && !chain2)
783 || (!chain1 && chain2)
784 || !compare_operand (chain1, chain2))
785 return return_false_with_msg ("static call chains are different");
787 /* Checking of argument. */
788 for (i = 0; i < gimple_call_num_args (s1); ++i)
790 t1 = gimple_call_arg (s1, i);
791 t2 = gimple_call_arg (s2, i);
793 if (!compare_memory_operand (t1, t2))
794 return return_false_with_msg ("memory operands are different");
797 /* Return value checking. */
798 t1 = gimple_get_lhs (s1);
799 t2 = gimple_get_lhs (s2);
801 return compare_memory_operand (t1, t2);
805 /* Verifies for given GIMPLEs S1 and S2 that
806 assignment statements are semantically equivalent. */
808 bool
809 func_checker::compare_gimple_assign (gimple s1, gimple s2)
811 tree arg1, arg2;
812 tree_code code1, code2;
813 unsigned i;
815 code1 = gimple_expr_code (s1);
816 code2 = gimple_expr_code (s2);
818 if (code1 != code2)
819 return false;
821 code1 = gimple_assign_rhs_code (s1);
822 code2 = gimple_assign_rhs_code (s2);
824 if (code1 != code2)
825 return false;
827 for (i = 0; i < gimple_num_ops (s1); i++)
829 arg1 = gimple_op (s1, i);
830 arg2 = gimple_op (s2, i);
832 if (!compare_memory_operand (arg1, arg2))
833 return return_false_with_msg ("memory operands are different");
837 return true;
840 /* Verifies for given GIMPLEs S1 and S2 that
841 condition statements are semantically equivalent. */
843 bool
844 func_checker::compare_gimple_cond (gimple s1, gimple s2)
846 tree t1, t2;
847 tree_code code1, code2;
849 code1 = gimple_expr_code (s1);
850 code2 = gimple_expr_code (s2);
852 if (code1 != code2)
853 return false;
855 t1 = gimple_cond_lhs (s1);
856 t2 = gimple_cond_lhs (s2);
858 if (!compare_operand (t1, t2))
859 return false;
861 t1 = gimple_cond_rhs (s1);
862 t2 = gimple_cond_rhs (s2);
864 return compare_operand (t1, t2);
867 /* Verifies that tree labels T1 and T2 correspond in FUNC1 and FUNC2. */
869 bool
870 func_checker::compare_tree_ssa_label (tree t1, tree t2)
872 return compare_operand (t1, t2);
875 /* Verifies for given GIMPLE_LABEL stmts S1 and S2 that
876 label statements are semantically equivalent. */
878 bool
879 func_checker::compare_gimple_label (const glabel *g1, const glabel *g2)
881 if (m_ignore_labels)
882 return true;
884 tree t1 = gimple_label_label (g1);
885 tree t2 = gimple_label_label (g2);
887 if (FORCED_LABEL (t1) || FORCED_LABEL (t2))
888 return return_false_with_msg ("FORCED_LABEL");
890 /* As the pass build BB to label mapping, no further check is needed. */
891 return true;
894 /* Verifies for given GIMPLE_SWITCH stmts S1 and S2 that
895 switch statements are semantically equivalent. */
897 bool
898 func_checker::compare_gimple_switch (const gswitch *g1, const gswitch *g2)
900 unsigned lsize1, lsize2, i;
902 lsize1 = gimple_switch_num_labels (g1);
903 lsize2 = gimple_switch_num_labels (g2);
905 if (lsize1 != lsize2)
906 return false;
908 tree t1 = gimple_switch_index (g1);
909 tree t2 = gimple_switch_index (g2);
911 if (!compare_operand (t1, t2))
912 return false;
914 for (i = 0; i < lsize1; i++)
916 tree label1 = gimple_switch_label (g1, i);
917 tree label2 = gimple_switch_label (g2, i);
919 /* Label LOW and HIGH comparison. */
920 tree low1 = CASE_LOW (label1);
921 tree low2 = CASE_LOW (label2);
923 if (!tree_int_cst_equal (low1, low2))
924 return return_false_with_msg ("case low values are different");
926 tree high1 = CASE_HIGH (label1);
927 tree high2 = CASE_HIGH (label2);
929 if (!tree_int_cst_equal (high1, high2))
930 return return_false_with_msg ("case high values are different");
932 if (TREE_CODE (label1) == CASE_LABEL_EXPR
933 && TREE_CODE (label2) == CASE_LABEL_EXPR)
935 label1 = CASE_LABEL (label1);
936 label2 = CASE_LABEL (label2);
938 if (!compare_operand (label1, label2))
939 return return_false_with_msg ("switch label_exprs are different");
941 else if (!tree_int_cst_equal (label1, label2))
942 return return_false_with_msg ("switch labels are different");
945 return true;
948 /* Verifies for given GIMPLE_RETURN stmts S1 and S2 that
949 return statements are semantically equivalent. */
951 bool
952 func_checker::compare_gimple_return (const greturn *g1, const greturn *g2)
954 tree t1, t2;
956 t1 = gimple_return_retval (g1);
957 t2 = gimple_return_retval (g2);
959 /* Void return type. */
960 if (t1 == NULL && t2 == NULL)
961 return true;
962 else
963 return compare_operand (t1, t2);
966 /* Verifies for given GIMPLEs S1 and S2 that
967 goto statements are semantically equivalent. */
969 bool
970 func_checker::compare_gimple_goto (gimple g1, gimple g2)
972 tree dest1, dest2;
974 dest1 = gimple_goto_dest (g1);
975 dest2 = gimple_goto_dest (g2);
977 if (TREE_CODE (dest1) != TREE_CODE (dest2) || TREE_CODE (dest1) != SSA_NAME)
978 return false;
980 return compare_operand (dest1, dest2);
983 /* Verifies for given GIMPLE_RESX stmts S1 and S2 that
984 resx statements are semantically equivalent. */
986 bool
987 func_checker::compare_gimple_resx (const gresx *g1, const gresx *g2)
989 return gimple_resx_region (g1) == gimple_resx_region (g2);
992 /* Verifies for given GIMPLEs S1 and S2 that ASM statements are equivalent.
993 For the beginning, the pass only supports equality for
994 '__asm__ __volatile__ ("", "", "", "memory")'. */
996 bool
997 func_checker::compare_gimple_asm (const gasm *g1, const gasm *g2)
999 if (gimple_asm_volatile_p (g1) != gimple_asm_volatile_p (g2))
1000 return false;
1002 if (gimple_asm_ninputs (g1) != gimple_asm_ninputs (g2))
1003 return false;
1005 if (gimple_asm_noutputs (g1) != gimple_asm_noutputs (g2))
1006 return false;
1008 /* We do not suppport goto ASM statement comparison. */
1009 if (gimple_asm_nlabels (g1) || gimple_asm_nlabels (g2))
1010 return false;
1012 if (gimple_asm_nclobbers (g1) != gimple_asm_nclobbers (g2))
1013 return false;
1015 if (strcmp (gimple_asm_string (g1), gimple_asm_string (g2)) != 0)
1016 return return_false_with_msg ("ASM strings are different");
1018 for (unsigned i = 0; i < gimple_asm_ninputs (g1); i++)
1020 tree input1 = gimple_asm_input_op (g1, i);
1021 tree input2 = gimple_asm_input_op (g2, i);
1023 if (!compare_tree_list_operand (input1, input2))
1024 return return_false_with_msg ("ASM input is different");
1027 for (unsigned i = 0; i < gimple_asm_noutputs (g1); i++)
1029 tree output1 = gimple_asm_output_op (g1, i);
1030 tree output2 = gimple_asm_output_op (g2, i);
1032 if (!compare_tree_list_operand (output1, output2))
1033 return return_false_with_msg ("ASM output is different");
1036 for (unsigned i = 0; i < gimple_asm_nclobbers (g1); i++)
1038 tree clobber1 = gimple_asm_clobber_op (g1, i);
1039 tree clobber2 = gimple_asm_clobber_op (g2, i);
1041 if (!operand_equal_p (TREE_VALUE (clobber1), TREE_VALUE (clobber2),
1042 OEP_ONLY_CONST))
1043 return return_false_with_msg ("ASM clobber is different");
1046 return true;
1049 } // ipa_icf_gimple namespace