comparison gcc/ipa-devirt.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
comparison
equal deleted inserted replaced
111:04ced10e8804 131:84e7813d76e9
1 /* Basic IPA utilities for type inheritance graph construction and 1 /* Basic IPA utilities for type inheritance graph construction and
2 devirtualization. 2 devirtualization.
3 Copyright (C) 2013-2017 Free Software Foundation, Inc. 3 Copyright (C) 2013-2018 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka 4 Contributed by Jan Hubicka
5 5
6 This file is part of GCC. 6 This file is part of GCC.
7 7
8 GCC is free software; you can redistribute it and/or modify it under 8 GCC is free software; you can redistribute it and/or modify it under
654 TYPE_BINFO (type) = binfo; 654 TYPE_BINFO (type) = binfo;
655 else 655 else
656 gcc_assert (!TYPE_BINFO (type)); 656 gcc_assert (!TYPE_BINFO (type));
657 } 657 }
658 658
659 /* Compare T2 and T2 based on name or structure. */ 659 /* Compare T1 and T2 based on name or structure. */
660 660
661 static bool 661 static bool
662 odr_subtypes_equivalent_p (tree t1, tree t2, 662 odr_subtypes_equivalent_p (tree t1, tree t2,
663 hash_set<type_pair> *visited, 663 hash_set<type_pair> *visited,
664 location_t loc1, location_t loc2) 664 location_t loc1, location_t loc2)
676 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1)) 676 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
677 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2))) 677 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
678 return false; 678 return false;
679 679
680 /* For ODR types be sure to compare their names. 680 /* For ODR types be sure to compare their names.
681 To support -wno-odr-type-merging we allow one type to be non-ODR 681 To support -Wno-odr-type-merging we allow one type to be non-ODR
682 and other ODR even though it is a violation. */ 682 and other ODR even though it is a violation. */
683 if (types_odr_comparable (t1, t2, true)) 683 if (types_odr_comparable (t1, t2, true))
684 { 684 {
685 if (!types_same_for_odr (t1, t2, true)) 685 if (!types_same_for_odr (t1, t2, true))
686 return false; 686 return false;
687 /* Limit recursion: If subtypes are ODR types and we know 687 /* Limit recursion: If subtypes are ODR types and we know
688 that they are same, be happy. */ 688 that they are same, be happy. */
689 if (!odr_type_p (t1) || !get_odr_type (t1, true)->odr_violated) 689 if (!odr_type_p (t1) || !get_odr_type (t1, true)->odr_violated)
690 return true; 690 return true;
691 } 691 }
692 692
693 /* Component types, builtins and possibly violating ODR types 693 /* Component types, builtins and possibly violating ODR types
747 { 747 {
748 varpool_node *tmp = prevailing; 748 varpool_node *tmp = prevailing;
749 prevailing = vtable; 749 prevailing = vtable;
750 vtable = tmp; 750 vtable = tmp;
751 } 751 }
752 auto_diagnostic_group d;
752 if (warning_at (DECL_SOURCE_LOCATION 753 if (warning_at (DECL_SOURCE_LOCATION
753 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 754 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
754 OPT_Wodr, 755 OPT_Wodr,
755 "virtual table of type %qD violates one definition rule", 756 "virtual table of type %qD violates one definition rule",
756 DECL_CONTEXT (vtable->decl))) 757 DECL_CONTEXT (vtable->decl)))
788 || (methods_equal_p (ref1->referred->decl, 789 || (methods_equal_p (ref1->referred->decl,
789 ref2->referred->decl) 790 ref2->referred->decl)
790 && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)) 791 && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
791 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL) 792 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
792 { 793 {
793 if (!class_type->rtti_broken 794 if (!class_type->rtti_broken)
794 && warning_at (DECL_SOURCE_LOCATION
795 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
796 OPT_Wodr,
797 "virtual table of type %qD contains RTTI "
798 "information",
799 DECL_CONTEXT (vtable->decl)))
800 { 795 {
801 inform (DECL_SOURCE_LOCATION 796 auto_diagnostic_group d;
802 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))), 797 if (warning_at (DECL_SOURCE_LOCATION
803 "but is prevailed by one without from other translation " 798 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
804 "unit"); 799 OPT_Wodr,
805 inform (DECL_SOURCE_LOCATION 800 "virtual table of type %qD contains RTTI "
806 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))), 801 "information",
807 "RTTI will not work on this type"); 802 DECL_CONTEXT (vtable->decl)))
808 class_type->rtti_broken = true; 803 {
804 inform (DECL_SOURCE_LOCATION
805 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
806 "but is prevailed by one without from other"
807 " translation unit");
808 inform (DECL_SOURCE_LOCATION
809 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
810 "RTTI will not work on this type");
811 class_type->rtti_broken = true;
812 }
809 } 813 }
810 n2++; 814 n2++;
811 end2 = !vtable->iterate_reference (n2, ref2); 815 end2 = !vtable->iterate_reference (n2, ref2);
812 } 816 }
813 while (!end1 817 while (!end1
829 Do this as very last check so the not very informative error 833 Do this as very last check so the not very informative error
830 is not output too often. */ 834 is not output too often. */
831 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl)) 835 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
832 { 836 {
833 class_type->odr_violated = true; 837 class_type->odr_violated = true;
838 auto_diagnostic_group d;
834 if (warning_at (DECL_SOURCE_LOCATION 839 if (warning_at (DECL_SOURCE_LOCATION
835 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 840 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
836 OPT_Wodr, 841 OPT_Wodr,
837 "virtual table of type %qD violates " 842 "virtual table of type %qD violates "
838 "one definition rule ", 843 "one definition rule ",
857 /* If the loops above stopped on non-virtual pointer, we have 862 /* If the loops above stopped on non-virtual pointer, we have
858 mismatch in RTTI information mangling. */ 863 mismatch in RTTI information mangling. */
859 if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL 864 if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
860 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL) 865 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
861 { 866 {
867 auto_diagnostic_group d;
862 if (warning_at (DECL_SOURCE_LOCATION 868 if (warning_at (DECL_SOURCE_LOCATION
863 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 869 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
864 OPT_Wodr, 870 OPT_Wodr,
865 "virtual table of type %qD violates " 871 "virtual table of type %qD violates "
866 "one definition rule ", 872 "one definition rule ",
898 varpool_node *tmp = prevailing; 904 varpool_node *tmp = prevailing;
899 prevailing = vtable; 905 prevailing = vtable;
900 vtable = tmp; 906 vtable = tmp;
901 ref1 = ref2; 907 ref1 = ref2;
902 } 908 }
909 auto_diagnostic_group d;
903 if (warning_at (DECL_SOURCE_LOCATION 910 if (warning_at (DECL_SOURCE_LOCATION
904 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 911 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
905 OPT_Wodr, 912 OPT_Wodr,
906 "virtual table of type %qD violates " 913 "virtual table of type %qD violates "
907 "one definition rule", 914 "one definition rule",
929 return; 936 return;
930 } 937 }
931 938
932 /* And in the last case we have either mistmatch in between two virtual 939 /* And in the last case we have either mistmatch in between two virtual
933 methods or two virtual table pointers. */ 940 methods or two virtual table pointers. */
941 auto_diagnostic_group d;
934 if (warning_at (DECL_SOURCE_LOCATION 942 if (warning_at (DECL_SOURCE_LOCATION
935 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr, 943 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
936 "virtual table of type %qD violates " 944 "virtual table of type %qD violates "
937 "one definition rule ", 945 "one definition rule ",
938 DECL_CONTEXT (vtable->decl))) 946 DECL_CONTEXT (vtable->decl)))
984 /* ODR warnings are output druing LTO streaming; we must apply location 992 /* ODR warnings are output druing LTO streaming; we must apply location
985 cache for potential warnings to be output correctly. */ 993 cache for potential warnings to be output correctly. */
986 if (lto_location_cache::current_cache) 994 if (lto_location_cache::current_cache)
987 lto_location_cache::current_cache->apply_location_cache (); 995 lto_location_cache::current_cache->apply_location_cache ();
988 996
997 auto_diagnostic_group d;
989 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr, 998 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr,
990 "type %qT violates the C++ One Definition Rule", 999 "type %qT violates the C++ One Definition Rule",
991 t1)) 1000 t1))
992 return; 1001 return;
993 if (!st1 && !st2) 1002 if (!st1 && !st2)
1578 warn_odr (t1, t2, f1, f2, warn, warned, 1587 warn_odr (t1, t2, f1, f2, warn, warned,
1579 G_("fields have different layout " 1588 G_("fields have different layout "
1580 "in another translation unit")); 1589 "in another translation unit"));
1581 return false; 1590 return false;
1582 } 1591 }
1583 gcc_assert (DECL_NONADDRESSABLE_P (f1) 1592 if (DECL_BIT_FIELD (f1) != DECL_BIT_FIELD (f2))
1584 == DECL_NONADDRESSABLE_P (f2)); 1593 {
1594 warn_odr (t1, t2, f1, f2, warn, warned,
1595 G_("one field is bitfield while other is not"));
1596 return false;
1597 }
1598 else
1599 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1600 == DECL_NONADDRESSABLE_P (f2));
1585 } 1601 }
1586 1602
1587 /* If one aggregate has more fields than the other, they 1603 /* If one aggregate has more fields than the other, they
1588 are not the same. */ 1604 are not the same. */
1589 if (f1 || f2) 1605 if (f1 || f2)
1842 putc ('\n',symtab->dump_file); 1858 putc ('\n',symtab->dump_file);
1843 } 1859 }
1844 } 1860 }
1845 } 1861 }
1846 1862
1847 /* Next compare memory layout. */ 1863 /* Next compare memory layout.
1864 The DECL_SOURCE_LOCATIONs in this invocation came from LTO streaming.
1865 We must apply the location cache to ensure that they are valid
1866 before we can pass them to odr_types_equivalent_p (PR lto/83121). */
1867 if (lto_location_cache::current_cache)
1868 lto_location_cache::current_cache->apply_location_cache ();
1848 if (!odr_types_equivalent_p (val->type, type, 1869 if (!odr_types_equivalent_p (val->type, type,
1849 !flag_ltrans && !val->odr_violated && !warned, 1870 !flag_ltrans && !val->odr_violated && !warned,
1850 &warned, &visited, 1871 &warned, &visited,
1851 DECL_SOURCE_LOCATION (TYPE_NAME (val->type)), 1872 DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
1852 DECL_SOURCE_LOCATION (TYPE_NAME (type)))) 1873 DECL_SOURCE_LOCATION (TYPE_NAME (type))))
2688 delete cached_polymorphic_call_targets; 2709 delete cached_polymorphic_call_targets;
2689 cached_polymorphic_call_targets = NULL; 2710 cached_polymorphic_call_targets = NULL;
2690 } 2711 }
2691 } 2712 }
2692 2713
2714 /* Force rebuilding type inheritance graph from scratch.
2715 This is use to make sure that we do not keep references to types
2716 which was not visible to free_lang_data. */
2717
2718 void
2719 rebuild_type_inheritance_graph ()
2720 {
2721 if (!odr_hash)
2722 return;
2723 delete odr_hash;
2724 if (in_lto_p)
2725 delete odr_vtable_hash;
2726 odr_hash = NULL;
2727 odr_vtable_hash = NULL;
2728 odr_types_ptr = NULL;
2729 free_polymorphic_call_targets_hash ();
2730 }
2731
2693 /* When virtual function is removed, we may need to flush the cache. */ 2732 /* When virtual function is removed, we may need to flush the cache. */
2694 2733
2695 static void 2734 static void
2696 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED) 2735 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
2697 { 2736 {
2899 2938
2900 /* Information about type and decl warnings. */ 2939 /* Information about type and decl warnings. */
2901 2940
2902 struct final_warning_record 2941 struct final_warning_record
2903 { 2942 {
2943 /* If needed grow type_warnings vector and initialize new decl_warn_count
2944 to have dyn_count set to profile_count::zero (). */
2945 void grow_type_warnings (unsigned newlen);
2946
2904 profile_count dyn_count; 2947 profile_count dyn_count;
2905 auto_vec<odr_type_warn_count> type_warnings; 2948 auto_vec<odr_type_warn_count> type_warnings;
2906 hash_map<tree, decl_warn_count> decl_warnings; 2949 hash_map<tree, decl_warn_count> decl_warnings;
2907 }; 2950 };
2951
2952 void
2953 final_warning_record::grow_type_warnings (unsigned newlen)
2954 {
2955 unsigned len = type_warnings.length ();
2956 if (newlen > len)
2957 {
2958 type_warnings.safe_grow_cleared (newlen);
2959 for (unsigned i = len; i < newlen; i++)
2960 type_warnings[i].dyn_count = profile_count::zero ();
2961 }
2962 }
2963
2908 struct final_warning_record *final_warning_records; 2964 struct final_warning_record *final_warning_records;
2909 2965
2910 /* Return vector containing possible targets of polymorphic call of type 2966 /* Return vector containing possible targets of polymorphic call of type
2911 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET. 2967 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2912 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing 2968 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
3174 { 3230 {
3175 if (complete 3231 if (complete
3176 && warn_suggest_final_types 3232 && warn_suggest_final_types
3177 && !outer_type->derived_types.length ()) 3233 && !outer_type->derived_types.length ())
3178 { 3234 {
3179 if (outer_type->id >= (int)final_warning_records->type_warnings.length ()) 3235 final_warning_records->grow_type_warnings
3180 final_warning_records->type_warnings.safe_grow_cleared 3236 (outer_type->id);
3181 (odr_types.length ());
3182 final_warning_records->type_warnings[outer_type->id].count++; 3237 final_warning_records->type_warnings[outer_type->id].count++;
3183 if (!final_warning_records->type_warnings 3238 if (!final_warning_records->type_warnings
3184 [outer_type->id].dyn_count.initialized_p ()) 3239 [outer_type->id].dyn_count.initialized_p ())
3185 final_warning_records->type_warnings 3240 final_warning_records->type_warnings
3186 [outer_type->id].dyn_count = profile_count::zero (); 3241 [outer_type->id].dyn_count = profile_count::zero ();
3543 entries. */ 3598 entries. */
3544 if (warn_suggest_final_methods || warn_suggest_final_types) 3599 if (warn_suggest_final_methods || warn_suggest_final_types)
3545 { 3600 {
3546 final_warning_records = new (final_warning_record); 3601 final_warning_records = new (final_warning_record);
3547 final_warning_records->dyn_count = profile_count::zero (); 3602 final_warning_records->dyn_count = profile_count::zero ();
3548 final_warning_records->type_warnings.safe_grow_cleared 3603 final_warning_records->grow_type_warnings (odr_types.length ());
3549 (odr_types.length ());
3550 free_polymorphic_call_targets_hash (); 3604 free_polymorphic_call_targets_hash ();
3551 } 3605 }
3552 3606
3553 FOR_EACH_DEFINED_FUNCTION (n) 3607 FOR_EACH_DEFINED_FUNCTION (n)
3554 { 3608 {
3564 struct cgraph_node *likely_target = NULL; 3618 struct cgraph_node *likely_target = NULL;
3565 void *cache_token; 3619 void *cache_token;
3566 bool final; 3620 bool final;
3567 3621
3568 if (final_warning_records) 3622 if (final_warning_records)
3569 final_warning_records->dyn_count = e->count; 3623 final_warning_records->dyn_count = e->count.ipa ();
3570 3624
3571 vec <cgraph_node *>targets 3625 vec <cgraph_node *>targets
3572 = possible_polymorphic_call_targets 3626 = possible_polymorphic_call_targets
3573 (e, &final, &cache_token, true); 3627 (e, &final, &cache_token, true);
3574 unsigned int i; 3628 unsigned int i;
3708 } 3762 }
3709 else if (dbg_cnt (devirt)) 3763 else if (dbg_cnt (devirt))
3710 { 3764 {
3711 if (dump_enabled_p ()) 3765 if (dump_enabled_p ())
3712 { 3766 {
3713 location_t locus = gimple_location_safe (e->call_stmt); 3767 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, e->call_stmt,
3714 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, locus,
3715 "speculatively devirtualizing call " 3768 "speculatively devirtualizing call "
3716 "in %s to %s\n", 3769 "in %s to %s\n",
3717 n->dump_name (), 3770 n->dump_name (),
3718 likely_target->dump_name ()); 3771 likely_target->dump_name ());
3719 } 3772 }
3725 likely_target = alias; 3778 likely_target = alias;
3726 } 3779 }
3727 nconverted++; 3780 nconverted++;
3728 update = true; 3781 update = true;
3729 e->make_speculative 3782 e->make_speculative
3730 (likely_target, e->count.apply_scale (8, 10), 3783 (likely_target, e->count.apply_scale (8, 10));
3731 e->frequency * 8 / 10);
3732 } 3784 }
3733 } 3785 }
3734 if (update) 3786 if (update)
3735 ipa_update_overall_fn_summary (n); 3787 ipa_update_overall_fn_summary (n);
3736 } 3788 }