1/* Basic IPA utilities for type inheritance graph construction and
2 devirtualization.
3 Copyright (C) 2013-2023 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 3, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22/* Brief vocabulary:
23 ODR = One Definition Rule
24 In short, the ODR states that:
25 1 In any translation unit, a template, type, function, or object can
26 have no more than one definition. Some of these can have any number
27 of declarations. A definition provides an instance.
28 2 In the entire program, an object or non-inline function cannot have
29 more than one definition; if an object or function is used, it must
30 have exactly one definition. You can declare an object or function
31 that is never used, in which case you don't have to provide
32 a definition. In no event can there be more than one definition.
33 3 Some things, like types, templates, and extern inline functions, can
34 be defined in more than one translation unit. For a given entity,
35 each definition must be the same. Non-extern objects and functions
36 in different translation units are different entities, even if their
37 names and types are the same.
38
39 OTR = OBJ_TYPE_REF
40 This is the Gimple representation of type information of a polymorphic call.
41 It contains two parameters:
42 otr_type is a type of class whose method is called.
43 otr_token is the index into virtual table where address is taken.
44
45 BINFO
46 This is the type inheritance information attached to each tree
47 RECORD_TYPE by the C++ frontend. It provides information about base
48 types and virtual tables.
49
50 BINFO is linked to the RECORD_TYPE by TYPE_BINFO.
51 BINFO also links to its type by BINFO_TYPE and to the virtual table by
52 BINFO_VTABLE.
53
54 Base types of a given type are enumerated by BINFO_BASE_BINFO
55 vector. Members of this vectors are not BINFOs associated
56 with a base type. Rather they are new copies of BINFOs
57 (base BINFOs). Their virtual tables may differ from
58 virtual table of the base type. Also BINFO_OFFSET specifies
59 offset of the base within the type.
60
61 In the case of single inheritance, the virtual table is shared
62 and BINFO_VTABLE of base BINFO is NULL. In the case of multiple
63 inheritance the individual virtual tables are pointer to by
64 BINFO_VTABLE of base binfos (that differs of BINFO_VTABLE of
65 binfo associated to the base type).
66
67 BINFO lookup for a given base type and offset can be done by
68 get_binfo_at_offset. It returns proper BINFO whose virtual table
69 can be used for lookup of virtual methods associated with the
70 base type.
71
72 token
73 This is an index of virtual method in virtual table associated
74 to the type defining it. Token can be looked up from OBJ_TYPE_REF
75 or from DECL_VINDEX of a given virtual table.
76
77 polymorphic (indirect) call
78 This is callgraph representation of virtual method call. Every
79 polymorphic call contains otr_type and otr_token taken from
80 original OBJ_TYPE_REF at callgraph construction time.
81
82 What we do here:
83
84 build_type_inheritance_graph triggers a construction of the type inheritance
85 graph.
86
87 We reconstruct it based on types of methods we see in the unit.
88 This means that the graph is not complete. Types with no methods are not
89 inserted into the graph. Also types without virtual methods are not
90 represented at all, though it may be easy to add this.
91
92 The inheritance graph is represented as follows:
93
94 Vertices are structures odr_type. Every odr_type may correspond
95 to one or more tree type nodes that are equivalent by ODR rule.
96 (the multiple type nodes appear only with linktime optimization)
97
98 Edges are represented by odr_type->base and odr_type->derived_types.
99 At the moment we do not track offsets of types for multiple inheritance.
100 Adding this is easy.
101
102 possible_polymorphic_call_targets returns, given an parameters found in
103 indirect polymorphic edge all possible polymorphic call targets of the call.
104
105 pass_ipa_devirt performs simple speculative devirtualization.
106*/
107
108#include "config.h"
109#include "system.h"
110#include "coretypes.h"
111#include "backend.h"
112#include "rtl.h"
113#include "tree.h"
114#include "gimple.h"
115#include "alloc-pool.h"
116#include "tree-pass.h"
117#include "cgraph.h"
118#include "lto-streamer.h"
119#include "fold-const.h"
120#include "print-tree.h"
121#include "calls.h"
122#include "ipa-utils.h"
123#include "gimple-iterator.h"
124#include "gimple-fold.h"
125#include "symbol-summary.h"
126#include "tree-vrp.h"
127#include "ipa-prop.h"
128#include "ipa-fnsummary.h"
129#include "demangle.h"
130#include "dbgcnt.h"
131#include "gimple-pretty-print.h"
132#include "intl.h"
133#include "stringpool.h"
134#include "attribs.h"
135#include "data-streamer.h"
136#include "lto-streamer.h"
137#include "streamer-hooks.h"
138
139/* Hash based set of pairs of types. */
140struct type_pair
141{
142 tree first;
143 tree second;
144};
145
146template <>
147struct default_hash_traits <type_pair>
148 : typed_noop_remove <type_pair>
149{
150 GTY((skip)) typedef type_pair value_type;
151 GTY((skip)) typedef type_pair compare_type;
152 static hashval_t
153 hash (type_pair p)
154 {
155 return TYPE_UID (p.first) ^ TYPE_UID (p.second);
156 }
157 static const bool empty_zero_p = true;
158 static bool
159 is_empty (type_pair p)
160 {
161 return p.first == NULL;
162 }
163 static bool
164 is_deleted (type_pair p ATTRIBUTE_UNUSED)
165 {
166 return false;
167 }
168 static bool
169 equal (const type_pair &a, const type_pair &b)
170 {
171 return a.first==b.first && a.second == b.second;
172 }
173 static void
174 mark_empty (type_pair &e)
175 {
176 e.first = NULL;
177 }
178};
179
180/* HACK alert: this is used to communicate with ipa-inline-transform that
181 thunk is being expanded and there is no need to clear the polymorphic
182 call target cache. */
183bool thunk_expansion;
184
185static bool odr_types_equivalent_p (tree, tree, bool, bool *,
186 hash_set<type_pair> *,
187 location_t, location_t);
188static void warn_odr (tree t1, tree t2, tree st1, tree st2,
189 bool warn, bool *warned, const char *reason);
190
191static bool odr_violation_reported = false;
192
193
194/* Pointer set of all call targets appearing in the cache. */
195static hash_set<cgraph_node *> *cached_polymorphic_call_targets;
196
197/* The node of type inheritance graph. For each type unique in
198 One Definition Rule (ODR) sense, we produce one node linking all
199 main variants of types equivalent to it, bases and derived types. */
200
201struct GTY(()) odr_type_d
202{
203 /* leader type. */
204 tree type;
205 /* All bases; built only for main variants of types. */
206 vec<odr_type> GTY((skip)) bases;
207 /* All derived types with virtual methods seen in unit;
208 built only for main variants of types. */
209 vec<odr_type> GTY((skip)) derived_types;
210
211 /* All equivalent types, if more than one. */
212 vec<tree, va_gc> *types;
213 /* Set of all equivalent types, if NON-NULL. */
214 hash_set<tree> * GTY((skip)) types_set;
215
216 /* Unique ID indexing the type in odr_types array. */
217 int id;
218 /* Is it in anonymous namespace? */
219 bool anonymous_namespace;
220 /* Do we know about all derivations of given type? */
221 bool all_derivations_known;
222 /* Did we report ODR violation here? */
223 bool odr_violated;
224 /* Set when virtual table without RTTI prevailed table with. */
225 bool rtti_broken;
226 /* Set when the canonical type is determined using the type name. */
227 bool tbaa_enabled;
228};
229
230/* Return TRUE if all derived types of T are known and thus
231 we may consider the walk of derived type complete.
232
233 This is typically true only for final anonymous namespace types and types
234 defined within functions (that may be COMDAT and thus shared across units,
235 but with the same set of derived types). */
236
237bool
238type_all_derivations_known_p (const_tree t)
239{
240 if (TYPE_FINAL_P (t))
241 return true;
242 if (flag_ltrans)
243 return false;
244 /* Non-C++ types may have IDENTIFIER_NODE here, do not crash. */
245 if (!TYPE_NAME (t) || TREE_CODE (TYPE_NAME (t)) != TYPE_DECL)
246 return true;
247 if (type_in_anonymous_namespace_p (t))
248 return true;
249 return (decl_function_context (TYPE_NAME (t)) != NULL);
250}
251
252/* Return TRUE if type's constructors are all visible. */
253
254static bool
255type_all_ctors_visible_p (tree t)
256{
257 return !flag_ltrans
258 && symtab->state >= CONSTRUCTION
259 /* We cannot always use type_all_derivations_known_p.
260 For function local types we must assume case where
261 the function is COMDAT and shared in between units.
262
263 TODO: These cases are quite easy to get, but we need
264 to keep track of C++ privatizing via -Wno-weak
265 as well as the IPA privatizing. */
266 && type_in_anonymous_namespace_p (t);
267}
268
269/* Return TRUE if type may have instance. */
270
271static bool
272type_possibly_instantiated_p (tree t)
273{
274 tree vtable;
275 varpool_node *vnode;
276
277 /* TODO: Add abstract types here. */
278 if (!type_all_ctors_visible_p (t))
279 return true;
280
281 vtable = BINFO_VTABLE (TYPE_BINFO (t));
282 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
283 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
284 vnode = varpool_node::get (decl: vtable);
285 return vnode && vnode->definition;
286}
287
288/* Return true if T or type derived from T may have instance. */
289
290static bool
291type_or_derived_type_possibly_instantiated_p (odr_type t)
292{
293 if (type_possibly_instantiated_p (t: t->type))
294 return true;
295 for (auto derived : t->derived_types)
296 if (type_or_derived_type_possibly_instantiated_p (t: derived))
297 return true;
298 return false;
299}
300
301/* Hash used to unify ODR types based on their mangled name and for anonymous
302 namespace types. */
303
304struct odr_name_hasher : pointer_hash <odr_type_d>
305{
306 typedef union tree_node *compare_type;
307 static inline hashval_t hash (const odr_type_d *);
308 static inline bool equal (const odr_type_d *, const tree_node *);
309 static inline void remove (odr_type_d *);
310};
311
312static bool
313can_be_name_hashed_p (tree t)
314{
315 return (!in_lto_p || odr_type_p (t));
316}
317
318/* Hash type by its ODR name. */
319
320static hashval_t
321hash_odr_name (const_tree t)
322{
323 gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
324
325 /* If not in LTO, all main variants are unique, so we can do
326 pointer hash. */
327 if (!in_lto_p)
328 return htab_hash_pointer (t);
329
330 /* Anonymous types are unique. */
331 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
332 return htab_hash_pointer (t);
333
334 gcc_checking_assert (TYPE_NAME (t)
335 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t)));
336 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (TYPE_NAME (t)));
337}
338
339/* Return the computed hashcode for ODR_TYPE. */
340
341inline hashval_t
342odr_name_hasher::hash (const odr_type_d *odr_type)
343{
344 return hash_odr_name (t: odr_type->type);
345}
346
347/* For languages with One Definition Rule, work out if
348 types are the same based on their name.
349
350 This is non-trivial for LTO where minor differences in
351 the type representation may have prevented type merging
352 to merge two copies of otherwise equivalent type.
353
354 Until we start streaming mangled type names, this function works
355 only for polymorphic types.
356*/
357
358bool
359types_same_for_odr (const_tree type1, const_tree type2)
360{
361 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
362
363 type1 = TYPE_MAIN_VARIANT (type1);
364 type2 = TYPE_MAIN_VARIANT (type2);
365
366 if (type1 == type2)
367 return true;
368
369 if (!in_lto_p)
370 return false;
371
372 /* Anonymous namespace types are never duplicated. */
373 if ((type_with_linkage_p (t: type1) && type_in_anonymous_namespace_p (t: type1))
374 || (type_with_linkage_p (t: type2) && type_in_anonymous_namespace_p (t: type2)))
375 return false;
376
377 /* If both type has mangled defined check if they are same.
378 Watch for anonymous types which are all mangled as "<anon">. */
379 if (!type_with_linkage_p (t: type1) || !type_with_linkage_p (t: type2))
380 return false;
381 if (type_in_anonymous_namespace_p (t: type1)
382 || type_in_anonymous_namespace_p (t: type2))
383 return false;
384 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
385 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
386}
387
388/* Return true if we can decide on ODR equivalency.
389
390 In non-LTO it is always decide, in LTO however it depends in the type has
391 ODR info attached. */
392
393bool
394types_odr_comparable (tree t1, tree t2)
395{
396 return (!in_lto_p
397 || TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)
398 || (odr_type_p (TYPE_MAIN_VARIANT (t1))
399 && odr_type_p (TYPE_MAIN_VARIANT (t2))));
400}
401
402/* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
403 known, be conservative and return false. */
404
405bool
406types_must_be_same_for_odr (tree t1, tree t2)
407{
408 if (types_odr_comparable (t1, t2))
409 return types_same_for_odr (type1: t1, type2: t2);
410 else
411 return TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2);
412}
413
414/* If T is compound type, return type it is based on. */
415
416static tree
417compound_type_base (const_tree t)
418{
419 if (TREE_CODE (t) == ARRAY_TYPE
420 || POINTER_TYPE_P (t)
421 || TREE_CODE (t) == COMPLEX_TYPE
422 || VECTOR_TYPE_P (t))
423 return TREE_TYPE (t);
424 if (TREE_CODE (t) == METHOD_TYPE)
425 return TYPE_METHOD_BASETYPE (t);
426 if (TREE_CODE (t) == OFFSET_TYPE)
427 return TYPE_OFFSET_BASETYPE (t);
428 return NULL_TREE;
429}
430
431/* Return true if T is either ODR type or compound type based from it.
432 If the function return true, we know that T is a type originating from C++
433 source even at link-time. */
434
435bool
436odr_or_derived_type_p (const_tree t)
437{
438 do
439 {
440 if (odr_type_p (TYPE_MAIN_VARIANT (t)))
441 return true;
442 /* Function type is a tricky one. Basically we can consider it
443 ODR derived if return type or any of the parameters is.
444 We need to check all parameters because LTO streaming merges
445 common types (such as void) and they are not considered ODR then. */
446 if (TREE_CODE (t) == FUNCTION_TYPE)
447 {
448 if (TYPE_METHOD_BASETYPE (t))
449 t = TYPE_METHOD_BASETYPE (t);
450 else
451 {
452 if (TREE_TYPE (t) && odr_or_derived_type_p (TREE_TYPE (t)))
453 return true;
454 for (t = TYPE_ARG_TYPES (t); t; t = TREE_CHAIN (t))
455 if (odr_or_derived_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (t))))
456 return true;
457 return false;
458 }
459 }
460 else
461 t = compound_type_base (t);
462 }
463 while (t);
464 return t;
465}
466
467/* Compare types T1 and T2 and return true if they are
468 equivalent. */
469
470inline bool
471odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2)
472{
473 tree t1 = o1->type;
474
475 gcc_checking_assert (TYPE_MAIN_VARIANT (t2) == t2);
476 gcc_checking_assert (TYPE_MAIN_VARIANT (t1) == t1);
477 if (t1 == t2)
478 return true;
479 if (!in_lto_p)
480 return false;
481 /* Check for anonymous namespaces. */
482 if ((type_with_linkage_p (t: t1) && type_in_anonymous_namespace_p (t: t1))
483 || (type_with_linkage_p (t: t2) && type_in_anonymous_namespace_p (t: t2)))
484 return false;
485 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
486 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
487 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
488 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
489}
490
491/* Free ODR type V. */
492
493inline void
494odr_name_hasher::remove (odr_type_d *v)
495{
496 v->bases.release ();
497 v->derived_types.release ();
498 if (v->types_set)
499 delete v->types_set;
500 ggc_free (v);
501}
502
503/* ODR type hash used to look up ODR type based on tree type node. */
504
505typedef hash_table<odr_name_hasher> odr_hash_type;
506static odr_hash_type *odr_hash;
507
508/* ODR types are also stored into ODR_TYPE vector to allow consistent
509 walking. Bases appear before derived types. Vector is garbage collected
510 so we won't end up visiting empty types. */
511
512static GTY(()) vec <odr_type, va_gc> *odr_types_ptr;
513#define odr_types (*odr_types_ptr)
514
515/* All enums defined and accessible for the unit. */
516static GTY(()) vec <tree, va_gc> *odr_enums;
517
518/* Information we hold about value defined by an enum type. */
519struct odr_enum_val
520{
521 const char *name;
522 wide_int val;
523 location_t locus;
524};
525
526/* Information about enum values. */
527struct odr_enum
528{
529 location_t locus;
530 auto_vec<odr_enum_val, 0> vals;
531 bool warned;
532};
533
534/* A table of all ODR enum definitions. */
535static hash_map <nofree_string_hash, odr_enum> *odr_enum_map = NULL;
536static struct obstack odr_enum_obstack;
537
538/* Set TYPE_BINFO of TYPE and its variants to BINFO. */
539void
540set_type_binfo (tree type, tree binfo)
541{
542 for (; type; type = TYPE_NEXT_VARIANT (type))
543 if (COMPLETE_TYPE_P (type))
544 TYPE_BINFO (type) = binfo;
545 else
546 gcc_assert (!TYPE_BINFO (type));
547}
548
549/* Return true if type variants match.
550 This assumes that we already verified that T1 and T2 are variants of the
551 same type. */
552
553static bool
554type_variants_equivalent_p (tree t1, tree t2)
555{
556 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
557 return false;
558
559 if (comp_type_attributes (t1, t2) != 1)
560 return false;
561
562 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
563 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
564 return false;
565
566 return true;
567}
568
569/* Compare T1 and T2 based on name or structure. */
570
571static bool
572odr_subtypes_equivalent_p (tree t1, tree t2,
573 hash_set<type_pair> *visited,
574 location_t loc1, location_t loc2)
575{
576
577 /* This can happen in incomplete types that should be handled earlier. */
578 gcc_assert (t1 && t2);
579
580 if (t1 == t2)
581 return true;
582
583 /* Anonymous namespace types must match exactly. */
584 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
585 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
586 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
587 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
588 return false;
589
590 /* For ODR types be sure to compare their names.
591 To support -Wno-odr-type-merging we allow one type to be non-ODR
592 and other ODR even though it is a violation. */
593 if (types_odr_comparable (t1, t2))
594 {
595 if (t1 != t2
596 && odr_type_p (TYPE_MAIN_VARIANT (t1))
597 && get_odr_type (TYPE_MAIN_VARIANT (t1), insert: true)->odr_violated)
598 return false;
599 if (!types_same_for_odr (type1: t1, type2: t2))
600 return false;
601 if (!type_variants_equivalent_p (t1, t2))
602 return false;
603 /* Limit recursion: If subtypes are ODR types and we know
604 that they are same, be happy. */
605 if (odr_type_p (TYPE_MAIN_VARIANT (t1)))
606 return true;
607 }
608
609 /* Component types, builtins and possibly violating ODR types
610 have to be compared structurally. */
611 if (TREE_CODE (t1) != TREE_CODE (t2))
612 return false;
613 if (AGGREGATE_TYPE_P (t1)
614 && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
615 return false;
616
617 type_pair pair={TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2)};
618 if (TYPE_UID (TYPE_MAIN_VARIANT (t1)) > TYPE_UID (TYPE_MAIN_VARIANT (t2)))
619 {
620 pair.first = TYPE_MAIN_VARIANT (t2);
621 pair.second = TYPE_MAIN_VARIANT (t1);
622 }
623 if (visited->add (k: pair))
624 return true;
625 if (!odr_types_equivalent_p (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2),
626 false, NULL, visited, loc1, loc2))
627 return false;
628 if (!type_variants_equivalent_p (t1, t2))
629 return false;
630 return true;
631}
632
633/* Return true if DECL1 and DECL2 are identical methods. Consider
634 name equivalent to name.localalias.xyz. */
635
636static bool
637methods_equal_p (tree decl1, tree decl2)
638{
639 if (DECL_ASSEMBLER_NAME (decl1) == DECL_ASSEMBLER_NAME (decl2))
640 return true;
641 const char sep = symbol_table::symbol_suffix_separator ();
642
643 const char *name1 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl1));
644 const char *ptr1 = strchr (s: name1, c: sep);
645 int len1 = ptr1 ? ptr1 - name1 : strlen (s: name1);
646
647 const char *name2 = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl2));
648 const char *ptr2 = strchr (s: name2, c: sep);
649 int len2 = ptr2 ? ptr2 - name2 : strlen (s: name2);
650
651 if (len1 != len2)
652 return false;
653 return !strncmp (s1: name1, s2: name2, n: len1);
654}
655
656/* Compare two virtual tables, PREVAILING and VTABLE and output ODR
657 violation warnings. */
658
659void
660compare_virtual_tables (varpool_node *prevailing, varpool_node *vtable)
661{
662 int n1, n2;
663
664 if (DECL_VIRTUAL_P (prevailing->decl) != DECL_VIRTUAL_P (vtable->decl))
665 {
666 odr_violation_reported = true;
667 if (DECL_VIRTUAL_P (prevailing->decl))
668 {
669 varpool_node *tmp = prevailing;
670 prevailing = vtable;
671 vtable = tmp;
672 }
673 auto_diagnostic_group d;
674 if (warning_at (DECL_SOURCE_LOCATION
675 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
676 OPT_Wodr,
677 "virtual table of type %qD violates one definition rule",
678 DECL_CONTEXT (vtable->decl)))
679 inform (DECL_SOURCE_LOCATION (prevailing->decl),
680 "variable of same assembler name as the virtual table is "
681 "defined in another translation unit");
682 return;
683 }
684 if (!prevailing->definition || !vtable->definition)
685 return;
686
687 /* If we do not stream ODR type info, do not bother to do useful compare. */
688 if (!TYPE_BINFO (DECL_CONTEXT (vtable->decl))
689 || !polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (vtable->decl))))
690 return;
691
692 odr_type class_type = get_odr_type (DECL_CONTEXT (vtable->decl), insert: true);
693
694 if (class_type->odr_violated)
695 return;
696
697 for (n1 = 0, n2 = 0; true; n1++, n2++)
698 {
699 struct ipa_ref *ref1, *ref2;
700 bool end1, end2;
701
702 end1 = !prevailing->iterate_reference (i: n1, ref&: ref1);
703 end2 = !vtable->iterate_reference (i: n2, ref&: ref2);
704
705 /* !DECL_VIRTUAL_P means RTTI entry;
706 We warn when RTTI is lost because non-RTTI prevails; we silently
707 accept the other case. */
708 while (!end2
709 && (end1
710 || (methods_equal_p (decl1: ref1->referred->decl,
711 decl2: ref2->referred->decl)
712 && TREE_CODE (ref1->referred->decl) == FUNCTION_DECL))
713 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
714 {
715 if (!class_type->rtti_broken)
716 {
717 auto_diagnostic_group d;
718 if (warning_at (DECL_SOURCE_LOCATION
719 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
720 OPT_Wodr,
721 "virtual table of type %qD contains RTTI "
722 "information",
723 DECL_CONTEXT (vtable->decl)))
724 {
725 inform (DECL_SOURCE_LOCATION
726 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
727 "but is prevailed by one without from other"
728 " translation unit");
729 inform (DECL_SOURCE_LOCATION
730 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
731 "RTTI will not work on this type");
732 class_type->rtti_broken = true;
733 }
734 }
735 n2++;
736 end2 = !vtable->iterate_reference (i: n2, ref&: ref2);
737 }
738 while (!end1
739 && (end2
740 || (methods_equal_p (decl1: ref2->referred->decl, decl2: ref1->referred->decl)
741 && TREE_CODE (ref2->referred->decl) == FUNCTION_DECL))
742 && TREE_CODE (ref1->referred->decl) != FUNCTION_DECL)
743 {
744 n1++;
745 end1 = !prevailing->iterate_reference (i: n1, ref&: ref1);
746 }
747
748 /* Finished? */
749 if (end1 && end2)
750 {
751 /* Extra paranoia; compare the sizes. We do not have information
752 about virtual inheritance offsets, so just be sure that these
753 match.
754 Do this as very last check so the not very informative error
755 is not output too often. */
756 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
757 {
758 class_type->odr_violated = true;
759 auto_diagnostic_group d;
760 tree ctx = TYPE_NAME (DECL_CONTEXT (vtable->decl));
761 if (warning_at (DECL_SOURCE_LOCATION (ctx), OPT_Wodr,
762 "virtual table of type %qD violates "
763 "one definition rule",
764 DECL_CONTEXT (vtable->decl)))
765 {
766 ctx = TYPE_NAME (DECL_CONTEXT (prevailing->decl));
767 inform (DECL_SOURCE_LOCATION (ctx),
768 "the conflicting type defined in another translation"
769 " unit has virtual table of different size");
770 }
771 }
772 return;
773 }
774
775 if (!end1 && !end2)
776 {
777 if (methods_equal_p (decl1: ref1->referred->decl, decl2: ref2->referred->decl))
778 continue;
779
780 class_type->odr_violated = true;
781
782 /* If the loops above stopped on non-virtual pointer, we have
783 mismatch in RTTI information mangling. */
784 if (TREE_CODE (ref1->referred->decl) != FUNCTION_DECL
785 && TREE_CODE (ref2->referred->decl) != FUNCTION_DECL)
786 {
787 auto_diagnostic_group d;
788 if (warning_at (DECL_SOURCE_LOCATION
789 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
790 OPT_Wodr,
791 "virtual table of type %qD violates "
792 "one definition rule",
793 DECL_CONTEXT (vtable->decl)))
794 {
795 inform (DECL_SOURCE_LOCATION
796 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
797 "the conflicting type defined in another translation "
798 "unit with different RTTI information");
799 }
800 return;
801 }
802 /* At this point both REF1 and REF2 points either to virtual table
803 or virtual method. If one points to virtual table and other to
804 method we can complain the same way as if one table was shorter
805 than other pointing out the extra method. */
806 if (TREE_CODE (ref1->referred->decl)
807 != TREE_CODE (ref2->referred->decl))
808 {
809 if (VAR_P (ref1->referred->decl))
810 end1 = true;
811 else if (VAR_P (ref2->referred->decl))
812 end2 = true;
813 }
814 }
815
816 class_type->odr_violated = true;
817
818 /* Complain about size mismatch. Either we have too many virtual
819 functions or too many virtual table pointers. */
820 if (end1 || end2)
821 {
822 if (end1)
823 {
824 varpool_node *tmp = prevailing;
825 prevailing = vtable;
826 vtable = tmp;
827 ref1 = ref2;
828 }
829 auto_diagnostic_group d;
830 if (warning_at (DECL_SOURCE_LOCATION
831 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
832 OPT_Wodr,
833 "virtual table of type %qD violates "
834 "one definition rule",
835 DECL_CONTEXT (vtable->decl)))
836 {
837 if (TREE_CODE (ref1->referring->decl) == FUNCTION_DECL)
838 {
839 inform (DECL_SOURCE_LOCATION
840 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
841 "the conflicting type defined in another translation "
842 "unit");
843 inform (DECL_SOURCE_LOCATION
844 (TYPE_NAME (DECL_CONTEXT (ref1->referring->decl))),
845 "contains additional virtual method %qD",
846 ref1->referred->decl);
847 }
848 else
849 {
850 inform (DECL_SOURCE_LOCATION
851 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
852 "the conflicting type defined in another translation "
853 "unit has virtual table with more entries");
854 }
855 }
856 return;
857 }
858
859 /* And in the last case we have either mismatch in between two virtual
860 methods or two virtual table pointers. */
861 auto_diagnostic_group d;
862 if (warning_at (DECL_SOURCE_LOCATION
863 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
864 "virtual table of type %qD violates "
865 "one definition rule",
866 DECL_CONTEXT (vtable->decl)))
867 {
868 if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
869 {
870 inform (DECL_SOURCE_LOCATION
871 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
872 "the conflicting type defined in another translation "
873 "unit");
874 gcc_assert (TREE_CODE (ref2->referred->decl)
875 == FUNCTION_DECL);
876 inform (DECL_SOURCE_LOCATION
877 (ref1->referred->ultimate_alias_target ()->decl),
878 "virtual method %qD",
879 ref1->referred->ultimate_alias_target ()->decl);
880 inform (DECL_SOURCE_LOCATION
881 (ref2->referred->ultimate_alias_target ()->decl),
882 "ought to match virtual method %qD but does not",
883 ref2->referred->ultimate_alias_target ()->decl);
884 }
885 else
886 inform (DECL_SOURCE_LOCATION
887 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
888 "the conflicting type defined in another translation "
889 "unit has virtual table with different contents");
890 return;
891 }
892 }
893}
894
895/* Output ODR violation warning about T1 and T2 with REASON.
896 Display location of ST1 and ST2 if REASON speaks about field or
897 method of the type.
898 If WARN is false, do nothing. Set WARNED if warning was indeed
899 output. */
900
901static void
902warn_odr (tree t1, tree t2, tree st1, tree st2,
903 bool warn, bool *warned, const char *reason)
904{
905 tree decl2 = TYPE_NAME (TYPE_MAIN_VARIANT (t2));
906 if (warned)
907 *warned = false;
908
909 if (!warn || !TYPE_NAME(TYPE_MAIN_VARIANT (t1)))
910 return;
911
912 /* ODR warnings are output during LTO streaming; we must apply location
913 cache for potential warnings to be output correctly. */
914 if (lto_location_cache::current_cache)
915 lto_location_cache::current_cache->apply_location_cache ();
916
917 auto_diagnostic_group d;
918 if (t1 != TYPE_MAIN_VARIANT (t1)
919 && TYPE_NAME (t1) != TYPE_NAME (TYPE_MAIN_VARIANT (t1)))
920 {
921 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1))),
922 OPT_Wodr, "type %qT (typedef of %qT) violates the "
923 "C++ One Definition Rule",
924 t1, TYPE_MAIN_VARIANT (t1)))
925 return;
926 }
927 else
928 {
929 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1))),
930 OPT_Wodr, "type %qT violates the C++ One Definition Rule",
931 t1))
932 return;
933 }
934 if (!st1 && !st2)
935 ;
936 /* For FIELD_DECL support also case where one of fields is
937 NULL - this is used when the structures have mismatching number of
938 elements. */
939 else if (!st1 || TREE_CODE (st1) == FIELD_DECL)
940 {
941 inform (DECL_SOURCE_LOCATION (decl2),
942 "a different type is defined in another translation unit");
943 if (!st1)
944 {
945 st1 = st2;
946 st2 = NULL;
947 }
948 inform (DECL_SOURCE_LOCATION (st1),
949 "the first difference of corresponding definitions is field %qD",
950 st1);
951 if (st2)
952 decl2 = st2;
953 }
954 else if (TREE_CODE (st1) == FUNCTION_DECL)
955 {
956 inform (DECL_SOURCE_LOCATION (decl2),
957 "a different type is defined in another translation unit");
958 inform (DECL_SOURCE_LOCATION (st1),
959 "the first difference of corresponding definitions is method %qD",
960 st1);
961 decl2 = st2;
962 }
963 else
964 return;
965 inform (DECL_SOURCE_LOCATION (decl2), reason);
966
967 if (warned)
968 *warned = true;
969}
970
971/* Return true if T1 and T2 are incompatible and we want to recursively
972 dive into them from warn_type_mismatch to give sensible answer. */
973
974static bool
975type_mismatch_p (tree t1, tree t2)
976{
977 if (odr_or_derived_type_p (t: t1) && odr_or_derived_type_p (t: t2)
978 && !odr_types_equivalent_p (type1: t1, type2: t2))
979 return true;
980 return !types_compatible_p (type1: t1, type2: t2);
981}
982
983
984/* Types T1 and T2 was found to be incompatible in a context they can't
985 (either used to declare a symbol of same assembler name or unified by
986 ODR rule). We already output warning about this, but if possible, output
987 extra information on how the types mismatch.
988
989 This is hard to do in general. We basically handle the common cases.
990
991 If LOC1 and LOC2 are meaningful locations, use it in the case the types
992 themselves do not have one. */
993
994void
995warn_types_mismatch (tree t1, tree t2, location_t loc1, location_t loc2)
996{
997 /* Location of type is known only if it has TYPE_NAME and the name is
998 TYPE_DECL. */
999 location_t loc_t1 = TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1000 ? DECL_SOURCE_LOCATION (TYPE_NAME (t1))
1001 : UNKNOWN_LOCATION;
1002 location_t loc_t2 = TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1003 ? DECL_SOURCE_LOCATION (TYPE_NAME (t2))
1004 : UNKNOWN_LOCATION;
1005 bool loc_t2_useful = false;
1006
1007 /* With LTO it is a common case that the location of both types match.
1008 See if T2 has a location that is different from T1. If so, we will
1009 inform user about the location.
1010 Do not consider the location passed to us in LOC1/LOC2 as those are
1011 already output. */
1012 if (loc_t2 > BUILTINS_LOCATION && loc_t2 != loc_t1)
1013 {
1014 if (loc_t1 <= BUILTINS_LOCATION)
1015 loc_t2_useful = true;
1016 else
1017 {
1018 expanded_location xloc1 = expand_location (loc_t1);
1019 expanded_location xloc2 = expand_location (loc_t2);
1020
1021 if (strcmp (s1: xloc1.file, s2: xloc2.file)
1022 || xloc1.line != xloc2.line
1023 || xloc1.column != xloc2.column)
1024 loc_t2_useful = true;
1025 }
1026 }
1027
1028 if (loc_t1 <= BUILTINS_LOCATION)
1029 loc_t1 = loc1;
1030 if (loc_t2 <= BUILTINS_LOCATION)
1031 loc_t2 = loc2;
1032
1033 location_t loc = loc_t1 <= BUILTINS_LOCATION ? loc_t2 : loc_t1;
1034
1035 /* It is a quite common bug to reference anonymous namespace type in
1036 non-anonymous namespace class. */
1037 tree mt1 = TYPE_MAIN_VARIANT (t1);
1038 tree mt2 = TYPE_MAIN_VARIANT (t2);
1039 if ((type_with_linkage_p (t: mt1)
1040 && type_in_anonymous_namespace_p (t: mt1))
1041 || (type_with_linkage_p (t: mt2)
1042 && type_in_anonymous_namespace_p (t: mt2)))
1043 {
1044 if (!type_with_linkage_p (t: mt1)
1045 || !type_in_anonymous_namespace_p (t: mt1))
1046 {
1047 std::swap (a&: t1, b&: t2);
1048 std::swap (a&: mt1, b&: mt2);
1049 std::swap (a&: loc_t1, b&: loc_t2);
1050 }
1051 gcc_assert (TYPE_NAME (mt1)
1052 && TREE_CODE (TYPE_NAME (mt1)) == TYPE_DECL);
1053 tree n1 = TYPE_NAME (mt1);
1054 tree n2 = TYPE_NAME (mt2) ? TYPE_NAME (mt2) : NULL;
1055
1056 if (TREE_CODE (n1) == TYPE_DECL)
1057 n1 = DECL_NAME (n1);
1058 if (n2 && TREE_CODE (n2) == TYPE_DECL)
1059 n2 = DECL_NAME (n2);
1060 /* Most of the time, the type names will match, do not be unnecessarily
1061 verbose. */
1062 if (n1 != n2)
1063 inform (loc_t1,
1064 "type %qT defined in anonymous namespace cannot match "
1065 "type %qT across the translation unit boundary",
1066 t1, t2);
1067 else
1068 inform (loc_t1,
1069 "type %qT defined in anonymous namespace cannot match "
1070 "across the translation unit boundary",
1071 t1);
1072 if (loc_t2_useful)
1073 inform (loc_t2,
1074 "the incompatible type defined in another translation unit");
1075 return;
1076 }
1077 /* If types have mangled ODR names and they are different, it is most
1078 informative to output those.
1079 This also covers types defined in different namespaces. */
1080 const char *odr1 = get_odr_name_for_type (type: mt1);
1081 const char *odr2 = get_odr_name_for_type (type: mt2);
1082 if (odr1 != NULL && odr2 != NULL && odr1 != odr2)
1083 {
1084 const int opts = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
1085 char *name1 = xstrdup (cplus_demangle (mangled: odr1, options: opts));
1086 char *name2 = cplus_demangle (mangled: odr2, options: opts);
1087 if (name1 && name2 && strcmp (s1: name1, s2: name2))
1088 {
1089 inform (loc_t1,
1090 "type name %qs should match type name %qs",
1091 name1, name2);
1092 if (loc_t2_useful)
1093 inform (loc_t2,
1094 "the incompatible type is defined here");
1095 free (ptr: name1);
1096 return;
1097 }
1098 free (ptr: name1);
1099 }
1100 /* A tricky case are compound types. Often they appear the same in source
1101 code and the mismatch is dragged in by type they are build from.
1102 Look for those differences in subtypes and try to be informative. In other
1103 cases just output nothing because the source code is probably different
1104 and in this case we already output a all necessary info. */
1105 if (!TYPE_NAME (t1) || !TYPE_NAME (t2))
1106 {
1107 if (TREE_CODE (t1) == TREE_CODE (t2))
1108 {
1109 if (TREE_CODE (t1) == ARRAY_TYPE
1110 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1111 {
1112 tree i1 = TYPE_DOMAIN (t1);
1113 tree i2 = TYPE_DOMAIN (t2);
1114
1115 if (i1 && i2
1116 && TYPE_MAX_VALUE (i1)
1117 && TYPE_MAX_VALUE (i2)
1118 && !operand_equal_p (TYPE_MAX_VALUE (i1),
1119 TYPE_MAX_VALUE (i2), flags: 0))
1120 {
1121 inform (loc,
1122 "array types have different bounds");
1123 return;
1124 }
1125 }
1126 if ((POINTER_TYPE_P (t1) || TREE_CODE (t1) == ARRAY_TYPE)
1127 && type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1128 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1: loc_t1, loc2: loc_t2);
1129 else if (TREE_CODE (t1) == METHOD_TYPE
1130 || TREE_CODE (t1) == FUNCTION_TYPE)
1131 {
1132 tree parms1 = NULL, parms2 = NULL;
1133 int count = 1;
1134
1135 if (type_mismatch_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1136 {
1137 inform (loc, "return value type mismatch");
1138 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1: loc_t1,
1139 loc2: loc_t2);
1140 return;
1141 }
1142 if (prototype_p (t1) && prototype_p (t2))
1143 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1144 parms1 && parms2;
1145 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2),
1146 count++)
1147 {
1148 if (type_mismatch_p (TREE_VALUE (parms1), TREE_VALUE (parms2)))
1149 {
1150 if (count == 1 && TREE_CODE (t1) == METHOD_TYPE)
1151 inform (loc,
1152 "implicit this pointer type mismatch");
1153 else
1154 inform (loc,
1155 "type mismatch in parameter %i",
1156 count - (TREE_CODE (t1) == METHOD_TYPE));
1157 warn_types_mismatch (TREE_VALUE (parms1),
1158 TREE_VALUE (parms2),
1159 loc1: loc_t1, loc2: loc_t2);
1160 return;
1161 }
1162 }
1163 if (parms1 || parms2)
1164 {
1165 inform (loc,
1166 "types have different parameter counts");
1167 return;
1168 }
1169 }
1170 }
1171 return;
1172 }
1173
1174 if (types_odr_comparable (t1, t2)
1175 /* We make assign integers mangled names to be able to handle
1176 signed/unsigned chars. Accepting them here would however lead to
1177 confusing message like
1178 "type ‘const int’ itself violates the C++ One Definition Rule" */
1179 && TREE_CODE (t1) != INTEGER_TYPE
1180 && types_same_for_odr (type1: t1, type2: t2))
1181 inform (loc_t1,
1182 "type %qT itself violates the C++ One Definition Rule", t1);
1183 /* Prevent pointless warnings like "struct aa" should match "struct aa". */
1184 else if (TYPE_NAME (t1) == TYPE_NAME (t2)
1185 && TREE_CODE (t1) == TREE_CODE (t2) && !loc_t2_useful)
1186 return;
1187 else
1188 inform (loc_t1, "type %qT should match type %qT",
1189 t1, t2);
1190 if (loc_t2_useful)
1191 inform (loc_t2, "the incompatible type is defined here");
1192}
1193
1194/* Return true if T should be ignored in TYPE_FIELDS for ODR comparison. */
1195
1196static bool
1197skip_in_fields_list_p (tree t)
1198{
1199 if (TREE_CODE (t) != FIELD_DECL)
1200 return true;
1201 /* C++ FE introduces zero sized fields depending on -std setting, see
1202 PR89358. */
1203 if (DECL_SIZE (t)
1204 && integer_zerop (DECL_SIZE (t))
1205 && DECL_ARTIFICIAL (t)
1206 && DECL_IGNORED_P (t)
1207 && !DECL_NAME (t))
1208 return true;
1209 return false;
1210}
1211
1212/* Compare T1 and T2, report ODR violations if WARN is true and set
1213 WARNED to true if anything is reported. Return true if types match.
1214 If true is returned, the types are also compatible in the sense of
1215 gimple_canonical_types_compatible_p.
1216 If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
1217 about the type if the type itself do not have location. */
1218
1219static bool
1220odr_types_equivalent_p (tree t1, tree t2, bool warn, bool *warned,
1221 hash_set<type_pair> *visited,
1222 location_t loc1, location_t loc2)
1223{
1224 /* If we are asked to warn, we need warned to keep track if warning was
1225 output. */
1226 gcc_assert (!warn || warned);
1227 /* Check first for the obvious case of pointer identity. */
1228 if (t1 == t2)
1229 return true;
1230
1231 /* Can't be the same type if the types don't have the same code. */
1232 if (TREE_CODE (t1) != TREE_CODE (t2))
1233 {
1234 warn_odr (t1, t2, NULL, NULL, warn, warned,
1235 G_("a different type is defined in another translation unit"));
1236 return false;
1237 }
1238
1239 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
1240 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
1241 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
1242 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
1243 {
1244 /* We cannot trip this when comparing ODR types, only when trying to
1245 match different ODR derivations from different declarations.
1246 So WARN should be always false. */
1247 gcc_assert (!warn);
1248 return false;
1249 }
1250
1251 /* Non-aggregate types can be handled cheaply. */
1252 if (INTEGRAL_TYPE_P (t1)
1253 || SCALAR_FLOAT_TYPE_P (t1)
1254 || FIXED_POINT_TYPE_P (t1)
1255 || VECTOR_TYPE_P (t1)
1256 || TREE_CODE (t1) == COMPLEX_TYPE
1257 || TREE_CODE (t1) == OFFSET_TYPE
1258 || POINTER_TYPE_P (t1))
1259 {
1260 if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
1261 {
1262 warn_odr (t1, t2, NULL, NULL, warn, warned,
1263 G_("a type with different precision is defined "
1264 "in another translation unit"));
1265 return false;
1266 }
1267 if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
1268 {
1269 warn_odr (t1, t2, NULL, NULL, warn, warned,
1270 G_("a type with different signedness is defined "
1271 "in another translation unit"));
1272 return false;
1273 }
1274
1275 if (TREE_CODE (t1) == INTEGER_TYPE
1276 && TYPE_STRING_FLAG (t1) != TYPE_STRING_FLAG (t2))
1277 {
1278 /* char WRT uint_8? */
1279 warn_odr (t1, t2, NULL, NULL, warn, warned,
1280 G_("a different type is defined in another "
1281 "translation unit"));
1282 return false;
1283 }
1284
1285 /* For canonical type comparisons we do not want to build SCCs
1286 so we cannot compare pointed-to types. But we can, for now,
1287 require the same pointed-to type kind and match what
1288 useless_type_conversion_p would do. */
1289 if (POINTER_TYPE_P (t1))
1290 {
1291 if (TYPE_ADDR_SPACE (TREE_TYPE (t1))
1292 != TYPE_ADDR_SPACE (TREE_TYPE (t2)))
1293 {
1294 warn_odr (t1, t2, NULL, NULL, warn, warned,
1295 G_("it is defined as a pointer in different address "
1296 "space in another translation unit"));
1297 return false;
1298 }
1299
1300 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1301 visited, loc1, loc2))
1302 {
1303 warn_odr (t1, t2, NULL, NULL, warn, warned,
1304 G_("it is defined as a pointer to different type "
1305 "in another translation unit"));
1306 if (warn && *warned)
1307 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2),
1308 loc1, loc2);
1309 return false;
1310 }
1311 }
1312
1313 if ((VECTOR_TYPE_P (t1) || TREE_CODE (t1) == COMPLEX_TYPE)
1314 && !odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1315 visited, loc1, loc2))
1316 {
1317 /* Probably specific enough. */
1318 warn_odr (t1, t2, NULL, NULL, warn, warned,
1319 G_("a different type is defined "
1320 "in another translation unit"));
1321 if (warn && *warned)
1322 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1323 return false;
1324 }
1325 }
1326 /* Do type-specific comparisons. */
1327 else switch (TREE_CODE (t1))
1328 {
1329 case ARRAY_TYPE:
1330 {
1331 /* Array types are the same if the element types are the same and
1332 the number of elements are the same. */
1333 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1334 visited, loc1, loc2))
1335 {
1336 warn_odr (t1, t2, NULL, NULL, warn, warned,
1337 G_("a different type is defined in another "
1338 "translation unit"));
1339 if (warn && *warned)
1340 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1341 }
1342 gcc_assert (TYPE_STRING_FLAG (t1) == TYPE_STRING_FLAG (t2));
1343 gcc_assert (TYPE_NONALIASED_COMPONENT (t1)
1344 == TYPE_NONALIASED_COMPONENT (t2));
1345
1346 tree i1 = TYPE_DOMAIN (t1);
1347 tree i2 = TYPE_DOMAIN (t2);
1348
1349 /* For an incomplete external array, the type domain can be
1350 NULL_TREE. Check this condition also. */
1351 if (i1 == NULL_TREE || i2 == NULL_TREE)
1352 return type_variants_equivalent_p (t1, t2);
1353
1354 tree min1 = TYPE_MIN_VALUE (i1);
1355 tree min2 = TYPE_MIN_VALUE (i2);
1356 tree max1 = TYPE_MAX_VALUE (i1);
1357 tree max2 = TYPE_MAX_VALUE (i2);
1358
1359 /* In C++, minimums should be always 0. */
1360 gcc_assert (min1 == min2);
1361 if (!operand_equal_p (max1, max2, flags: 0))
1362 {
1363 warn_odr (t1, t2, NULL, NULL, warn, warned,
1364 G_("an array of different size is defined "
1365 "in another translation unit"));
1366 return false;
1367 }
1368 }
1369 break;
1370
1371 case METHOD_TYPE:
1372 case FUNCTION_TYPE:
1373 /* Function types are the same if the return type and arguments types
1374 are the same. */
1375 if (!odr_subtypes_equivalent_p (TREE_TYPE (t1), TREE_TYPE (t2),
1376 visited, loc1, loc2))
1377 {
1378 warn_odr (t1, t2, NULL, NULL, warn, warned,
1379 G_("has different return value "
1380 "in another translation unit"));
1381 if (warn && *warned)
1382 warn_types_mismatch (TREE_TYPE (t1), TREE_TYPE (t2), loc1, loc2);
1383 return false;
1384 }
1385
1386 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
1387 || !prototype_p (t1) || !prototype_p (t2))
1388 return type_variants_equivalent_p (t1, t2);
1389 else
1390 {
1391 tree parms1, parms2;
1392
1393 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1394 parms1 && parms2;
1395 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
1396 {
1397 if (!odr_subtypes_equivalent_p
1398 (TREE_VALUE (parms1), TREE_VALUE (parms2),
1399 visited, loc1, loc2))
1400 {
1401 warn_odr (t1, t2, NULL, NULL, warn, warned,
1402 G_("has different parameters in another "
1403 "translation unit"));
1404 if (warn && *warned)
1405 warn_types_mismatch (TREE_VALUE (parms1),
1406 TREE_VALUE (parms2), loc1, loc2);
1407 return false;
1408 }
1409 }
1410
1411 if (parms1 || parms2)
1412 {
1413 warn_odr (t1, t2, NULL, NULL, warn, warned,
1414 G_("has different parameters "
1415 "in another translation unit"));
1416 return false;
1417 }
1418
1419 return type_variants_equivalent_p (t1, t2);
1420 }
1421
1422 case RECORD_TYPE:
1423 case UNION_TYPE:
1424 case QUAL_UNION_TYPE:
1425 {
1426 tree f1, f2;
1427
1428 /* For aggregate types, all the fields must be the same. */
1429 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1430 {
1431 if (TYPE_BINFO (t1) && TYPE_BINFO (t2)
1432 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
1433 != polymorphic_type_binfo_p (TYPE_BINFO (t2)))
1434 {
1435 if (polymorphic_type_binfo_p (TYPE_BINFO (t1)))
1436 warn_odr (t1, t2, NULL, NULL, warn, warned,
1437 G_("a type defined in another translation unit "
1438 "is not polymorphic"));
1439 else
1440 warn_odr (t1, t2, NULL, NULL, warn, warned,
1441 G_("a type defined in another translation unit "
1442 "is polymorphic"));
1443 return false;
1444 }
1445 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1446 f1 || f2;
1447 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1448 {
1449 /* Skip non-fields. */
1450 while (f1 && skip_in_fields_list_p (t: f1))
1451 f1 = TREE_CHAIN (f1);
1452 while (f2 && skip_in_fields_list_p (t: f2))
1453 f2 = TREE_CHAIN (f2);
1454 if (!f1 || !f2)
1455 break;
1456 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1457 {
1458 warn_odr (t1, t2, NULL, NULL, warn, warned,
1459 G_("a type with different virtual table pointers"
1460 " is defined in another translation unit"));
1461 return false;
1462 }
1463 if (DECL_ARTIFICIAL (f1) != DECL_ARTIFICIAL (f2))
1464 {
1465 warn_odr (t1, t2, NULL, NULL, warn, warned,
1466 G_("a type with different bases is defined "
1467 "in another translation unit"));
1468 return false;
1469 }
1470 if (DECL_NAME (f1) != DECL_NAME (f2)
1471 && !DECL_ARTIFICIAL (f1))
1472 {
1473 warn_odr (t1, t2, st1: f1, st2: f2, warn, warned,
1474 G_("a field with different name is defined "
1475 "in another translation unit"));
1476 return false;
1477 }
1478 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
1479 TREE_TYPE (f2),
1480 visited, loc1, loc2))
1481 {
1482 /* Do not warn about artificial fields and just go into
1483 generic field mismatch warning. */
1484 if (DECL_ARTIFICIAL (f1))
1485 break;
1486
1487 warn_odr (t1, t2, st1: f1, st2: f2, warn, warned,
1488 G_("a field of same name but different type "
1489 "is defined in another translation unit"));
1490 if (warn && *warned)
1491 warn_types_mismatch (TREE_TYPE (f1), TREE_TYPE (f2), loc1, loc2);
1492 return false;
1493 }
1494 if (!gimple_compare_field_offset (f1, f2))
1495 {
1496 /* Do not warn about artificial fields and just go into
1497 generic field mismatch warning. */
1498 if (DECL_ARTIFICIAL (f1))
1499 break;
1500 warn_odr (t1, t2, st1: f1, st2: f2, warn, warned,
1501 G_("fields have different layout "
1502 "in another translation unit"));
1503 return false;
1504 }
1505 if (DECL_BIT_FIELD (f1) != DECL_BIT_FIELD (f2))
1506 {
1507 warn_odr (t1, t2, st1: f1, st2: f2, warn, warned,
1508 G_("one field is a bitfield while the other "
1509 "is not"));
1510 return false;
1511 }
1512 else
1513 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1514 == DECL_NONADDRESSABLE_P (f2));
1515 }
1516
1517 /* If one aggregate has more fields than the other, they
1518 are not the same. */
1519 if (f1 || f2)
1520 {
1521 if ((f1 && DECL_VIRTUAL_P (f1)) || (f2 && DECL_VIRTUAL_P (f2)))
1522 warn_odr (t1, t2, NULL, NULL, warn, warned,
1523 G_("a type with different virtual table pointers"
1524 " is defined in another translation unit"));
1525 else if ((f1 && DECL_ARTIFICIAL (f1))
1526 || (f2 && DECL_ARTIFICIAL (f2)))
1527 warn_odr (t1, t2, NULL, NULL, warn, warned,
1528 G_("a type with different bases is defined "
1529 "in another translation unit"));
1530 else
1531 warn_odr (t1, t2, st1: f1, st2: f2, warn, warned,
1532 G_("a type with different number of fields "
1533 "is defined in another translation unit"));
1534
1535 return false;
1536 }
1537 }
1538 break;
1539 }
1540 case VOID_TYPE:
1541 case OPAQUE_TYPE:
1542 case NULLPTR_TYPE:
1543 break;
1544
1545 default:
1546 debug_tree (t1);
1547 gcc_unreachable ();
1548 }
1549
1550 /* Those are better to come last as they are utterly uninformative. */
1551 if (TYPE_SIZE (t1) && TYPE_SIZE (t2)
1552 && !operand_equal_p (TYPE_SIZE (t1), TYPE_SIZE (t2), flags: 0))
1553 {
1554 warn_odr (t1, t2, NULL, NULL, warn, warned,
1555 G_("a type with different size "
1556 "is defined in another translation unit"));
1557 return false;
1558 }
1559
1560 if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2)
1561 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1562 {
1563 warn_odr (t1, t2, NULL, NULL, warn, warned,
1564 G_("one type needs to be constructed while the other does not"));
1565 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (t1));
1566 return false;
1567 }
1568 /* There is no really good user facing warning for this.
1569 Either the original reason for modes being different is lost during
1570 streaming or we should catch earlier warnings. We however must detect
1571 the mismatch to avoid type verifier from cmplaining on mismatched
1572 types between type and canonical type. See PR91576. */
1573 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1574 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1575 {
1576 warn_odr (t1, t2, NULL, NULL, warn, warned,
1577 G_("memory layout mismatch"));
1578 return false;
1579 }
1580
1581 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
1582 || operand_equal_p (TYPE_SIZE_UNIT (t1),
1583 TYPE_SIZE_UNIT (t2), 0));
1584 return type_variants_equivalent_p (t1, t2);
1585}
1586
1587/* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */
1588
1589bool
1590odr_types_equivalent_p (tree type1, tree type2)
1591{
1592 gcc_checking_assert (odr_or_derived_type_p (type1)
1593 && odr_or_derived_type_p (type2));
1594
1595 hash_set<type_pair> visited;
1596 return odr_types_equivalent_p (t1: type1, t2: type2, warn: false, NULL,
1597 visited: &visited, UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1598}
1599
1600/* TYPE is equivalent to VAL by ODR, but its tree representation differs
1601 from VAL->type. This may happen in LTO where tree merging did not merge
1602 all variants of the same type or due to ODR violation.
1603
1604 Analyze and report ODR violations and add type to duplicate list.
1605 If TYPE is more specified than VAL->type, prevail VAL->type. Also if
1606 this is first time we see definition of a class return true so the
1607 base types are analyzed. */
1608
1609static bool
1610add_type_duplicate (odr_type val, tree type)
1611{
1612 bool build_bases = false;
1613 bool prevail = false;
1614 bool odr_must_violate = false;
1615
1616 if (!val->types_set)
1617 val->types_set = new hash_set<tree>;
1618
1619 /* Chose polymorphic type as leader (this happens only in case of ODR
1620 violations. */
1621 if ((TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1622 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
1623 && (TREE_CODE (val->type) != RECORD_TYPE || !TYPE_BINFO (val->type)
1624 || !polymorphic_type_binfo_p (TYPE_BINFO (val->type))))
1625 {
1626 prevail = true;
1627 build_bases = true;
1628 }
1629 /* Always prefer complete type to be the leader. */
1630 else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
1631 {
1632 prevail = true;
1633 if (TREE_CODE (type) == RECORD_TYPE)
1634 build_bases = TYPE_BINFO (type);
1635 }
1636 else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
1637 ;
1638 else if (TREE_CODE (val->type) == RECORD_TYPE
1639 && TREE_CODE (type) == RECORD_TYPE
1640 && TYPE_BINFO (type) && !TYPE_BINFO (val->type))
1641 {
1642 gcc_assert (!val->bases.length ());
1643 build_bases = true;
1644 prevail = true;
1645 }
1646
1647 if (prevail)
1648 std::swap (a&: val->type, b&: type);
1649
1650 val->types_set->add (k: type);
1651
1652 if (!odr_hash)
1653 return false;
1654
1655 gcc_checking_assert (can_be_name_hashed_p (type)
1656 && can_be_name_hashed_p (val->type));
1657
1658 bool merge = true;
1659 bool base_mismatch = false;
1660 unsigned int i;
1661 bool warned = false;
1662 hash_set<type_pair> visited;
1663
1664 gcc_assert (in_lto_p);
1665 vec_safe_push (v&: val->types, obj: type);
1666
1667 /* If both are class types, compare the bases. */
1668 if (COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1669 && TREE_CODE (val->type) == RECORD_TYPE
1670 && TREE_CODE (type) == RECORD_TYPE
1671 && TYPE_BINFO (val->type) && TYPE_BINFO (type))
1672 {
1673 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1674 != BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1675 {
1676 if (!flag_ltrans && !warned && !val->odr_violated)
1677 {
1678 tree extra_base;
1679 warn_odr (t1: type, t2: val->type, NULL, NULL, warn: !warned, warned: &warned,
1680 reason: "a type with the same name but different "
1681 "number of polymorphic bases is "
1682 "defined in another translation unit");
1683 if (warned)
1684 {
1685 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (type))
1686 > BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)))
1687 extra_base = BINFO_BASE_BINFO
1688 (TYPE_BINFO (type),
1689 BINFO_N_BASE_BINFOS (TYPE_BINFO (val->type)));
1690 else
1691 extra_base = BINFO_BASE_BINFO
1692 (TYPE_BINFO (val->type),
1693 BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1694 tree extra_base_type = BINFO_TYPE (extra_base);
1695 inform (DECL_SOURCE_LOCATION (TYPE_NAME (extra_base_type)),
1696 "the extra base is defined here");
1697 }
1698 }
1699 base_mismatch = true;
1700 }
1701 else
1702 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1703 {
1704 tree base1 = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1705 tree base2 = BINFO_BASE_BINFO (TYPE_BINFO (val->type), i);
1706 tree type1 = BINFO_TYPE (base1);
1707 tree type2 = BINFO_TYPE (base2);
1708
1709 if (types_odr_comparable (t1: type1, t2: type2))
1710 {
1711 if (!types_same_for_odr (type1, type2))
1712 base_mismatch = true;
1713 }
1714 else
1715 if (!odr_types_equivalent_p (type1, type2))
1716 base_mismatch = true;
1717 if (base_mismatch)
1718 {
1719 if (!warned && !val->odr_violated)
1720 {
1721 warn_odr (t1: type, t2: val->type, NULL, NULL,
1722 warn: !warned, warned: &warned,
1723 reason: "a type with the same name but different base "
1724 "type is defined in another translation unit");
1725 if (warned)
1726 warn_types_mismatch (t1: type1, t2: type2,
1727 UNKNOWN_LOCATION, UNKNOWN_LOCATION);
1728 }
1729 break;
1730 }
1731 if (BINFO_OFFSET (base1) != BINFO_OFFSET (base2))
1732 {
1733 base_mismatch = true;
1734 if (!warned && !val->odr_violated)
1735 warn_odr (t1: type, t2: val->type, NULL, NULL,
1736 warn: !warned, warned: &warned,
1737 reason: "a type with the same name but different base "
1738 "layout is defined in another translation unit");
1739 break;
1740 }
1741 /* One of bases is not of complete type. */
1742 if (!TYPE_BINFO (type1) != !TYPE_BINFO (type2))
1743 {
1744 /* If we have a polymorphic type info specified for TYPE1
1745 but not for TYPE2 we possibly missed a base when recording
1746 VAL->type earlier.
1747 Be sure this does not happen. */
1748 if (TYPE_BINFO (type1)
1749 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1750 && !build_bases)
1751 odr_must_violate = true;
1752 break;
1753 }
1754 /* One base is polymorphic and the other not.
1755 This ought to be diagnosed earlier, but do not ICE in the
1756 checking bellow. */
1757 else if (TYPE_BINFO (type1)
1758 && polymorphic_type_binfo_p (TYPE_BINFO (type1))
1759 != polymorphic_type_binfo_p (TYPE_BINFO (type2)))
1760 {
1761 if (!warned && !val->odr_violated)
1762 warn_odr (t1: type, t2: val->type, NULL, NULL,
1763 warn: !warned, warned: &warned,
1764 reason: "a base of the type is polymorphic only in one "
1765 "translation unit");
1766 base_mismatch = true;
1767 break;
1768 }
1769 }
1770 if (base_mismatch)
1771 {
1772 merge = false;
1773 odr_violation_reported = true;
1774 val->odr_violated = true;
1775
1776 if (symtab->dump_file)
1777 {
1778 fprintf (stream: symtab->dump_file, format: "ODR base violation\n");
1779
1780 print_node (symtab->dump_file, "", val->type, 0);
1781 putc (c: '\n',stream: symtab->dump_file);
1782 print_node (symtab->dump_file, "", type, 0);
1783 putc (c: '\n',stream: symtab->dump_file);
1784 }
1785 }
1786 }
1787
1788 /* Next compare memory layout.
1789 The DECL_SOURCE_LOCATIONs in this invocation came from LTO streaming.
1790 We must apply the location cache to ensure that they are valid
1791 before we can pass them to odr_types_equivalent_p (PR lto/83121). */
1792 if (lto_location_cache::current_cache)
1793 lto_location_cache::current_cache->apply_location_cache ();
1794 /* As a special case we stream mangles names of integer types so we can see
1795 if they are believed to be same even though they have different
1796 representation. Avoid bogus warning on mismatches in these. */
1797 if (TREE_CODE (type) != INTEGER_TYPE
1798 && TREE_CODE (val->type) != INTEGER_TYPE
1799 && !odr_types_equivalent_p (t1: val->type, t2: type,
1800 warn: !flag_ltrans && !val->odr_violated && !warned,
1801 warned: &warned, visited: &visited,
1802 DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
1803 DECL_SOURCE_LOCATION (TYPE_NAME (type))))
1804 {
1805 merge = false;
1806 odr_violation_reported = true;
1807 val->odr_violated = true;
1808 }
1809 gcc_assert (val->odr_violated || !odr_must_violate);
1810 /* Sanity check that all bases will be build same way again. */
1811 if (flag_checking
1812 && COMPLETE_TYPE_P (type) && COMPLETE_TYPE_P (val->type)
1813 && TREE_CODE (val->type) == RECORD_TYPE
1814 && TREE_CODE (type) == RECORD_TYPE
1815 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1816 && !val->odr_violated
1817 && !base_mismatch && val->bases.length ())
1818 {
1819 unsigned int num_poly_bases = 0;
1820 unsigned int j;
1821
1822 for (i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
1823 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1824 (TYPE_BINFO (type), i)))
1825 num_poly_bases++;
1826 gcc_assert (num_poly_bases == val->bases.length ());
1827 for (j = 0, i = 0; i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type));
1828 i++)
1829 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO
1830 (TYPE_BINFO (type), i)))
1831 {
1832 odr_type base = get_odr_type
1833 (BINFO_TYPE
1834 (BINFO_BASE_BINFO (TYPE_BINFO (type),
1835 i)),
1836 insert: true);
1837 gcc_assert (val->bases[j] == base);
1838 j++;
1839 }
1840 }
1841
1842
1843 /* Regularize things a little. During LTO same types may come with
1844 different BINFOs. Either because their virtual table was
1845 not merged by tree merging and only later at decl merging or
1846 because one type comes with external vtable, while other
1847 with internal. We want to merge equivalent binfos to conserve
1848 memory and streaming overhead.
1849
1850 The external vtables are more harmful: they contain references
1851 to external declarations of methods that may be defined in the
1852 merged LTO unit. For this reason we absolutely need to remove
1853 them and replace by internal variants. Not doing so will lead
1854 to incomplete answers from possible_polymorphic_call_targets.
1855
1856 FIXME: disable for now; because ODR types are now build during
1857 streaming in, the variants do not need to be linked to the type,
1858 yet. We need to do the merging in cleanup pass to be implemented
1859 soon. */
1860 if (!flag_ltrans && merge
1861 && 0
1862 && TREE_CODE (val->type) == RECORD_TYPE
1863 && TREE_CODE (type) == RECORD_TYPE
1864 && TYPE_BINFO (val->type) && TYPE_BINFO (type)
1865 && TYPE_MAIN_VARIANT (type) == type
1866 && TYPE_MAIN_VARIANT (val->type) == val->type
1867 && BINFO_VTABLE (TYPE_BINFO (val->type))
1868 && BINFO_VTABLE (TYPE_BINFO (type)))
1869 {
1870 tree master_binfo = TYPE_BINFO (val->type);
1871 tree v1 = BINFO_VTABLE (master_binfo);
1872 tree v2 = BINFO_VTABLE (TYPE_BINFO (type));
1873
1874 if (TREE_CODE (v1) == POINTER_PLUS_EXPR)
1875 {
1876 gcc_assert (TREE_CODE (v2) == POINTER_PLUS_EXPR
1877 && operand_equal_p (TREE_OPERAND (v1, 1),
1878 TREE_OPERAND (v2, 1), 0));
1879 v1 = TREE_OPERAND (TREE_OPERAND (v1, 0), 0);
1880 v2 = TREE_OPERAND (TREE_OPERAND (v2, 0), 0);
1881 }
1882 gcc_assert (DECL_ASSEMBLER_NAME (v1)
1883 == DECL_ASSEMBLER_NAME (v2));
1884
1885 if (DECL_EXTERNAL (v1) && !DECL_EXTERNAL (v2))
1886 {
1887 unsigned int i;
1888
1889 set_type_binfo (type: val->type, TYPE_BINFO (type));
1890 for (i = 0; i < val->types->length (); i++)
1891 {
1892 if (TYPE_BINFO ((*val->types)[i])
1893 == master_binfo)
1894 set_type_binfo (type: (*val->types)[i], TYPE_BINFO (type));
1895 }
1896 BINFO_TYPE (TYPE_BINFO (type)) = val->type;
1897 }
1898 else
1899 set_type_binfo (type, binfo: master_binfo);
1900 }
1901 return build_bases;
1902}
1903
1904/* REF is OBJ_TYPE_REF, return the class the ref corresponds to.
1905 FOR_DUMP_P is true when being called from the dump routines. */
1906
1907tree
1908obj_type_ref_class (const_tree ref, bool for_dump_p)
1909{
1910 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
1911 ref = TREE_TYPE (ref);
1912 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
1913 ref = TREE_TYPE (ref);
1914 /* We look for type THIS points to. ObjC also builds
1915 OBJ_TYPE_REF with non-method calls, Their first parameter
1916 ID however also corresponds to class type. */
1917 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
1918 || TREE_CODE (ref) == FUNCTION_TYPE);
1919 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
1920 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
1921 tree ret = TREE_TYPE (ref);
1922 if (!in_lto_p && !TYPE_STRUCTURAL_EQUALITY_P (ret))
1923 ret = TYPE_CANONICAL (ret);
1924 else if (odr_type ot = get_odr_type (ret, insert: !for_dump_p))
1925 ret = ot->type;
1926 else
1927 gcc_assert (for_dump_p);
1928 return ret;
1929}
1930
1931/* Get ODR type hash entry for TYPE. If INSERT is true, create
1932 possibly new entry. */
1933
1934odr_type
1935get_odr_type (tree type, bool insert)
1936{
1937 odr_type_d **slot = NULL;
1938 odr_type val = NULL;
1939 hashval_t hash;
1940 bool build_bases = false;
1941 bool insert_to_odr_array = false;
1942 int base_id = -1;
1943
1944 type = TYPE_MAIN_VARIANT (type);
1945 if (!in_lto_p && !TYPE_STRUCTURAL_EQUALITY_P (type))
1946 type = TYPE_CANONICAL (type);
1947
1948 gcc_checking_assert (can_be_name_hashed_p (type));
1949
1950 hash = hash_odr_name (t: type);
1951 slot = odr_hash->find_slot_with_hash (comparable: type, hash,
1952 insert: insert ? INSERT : NO_INSERT);
1953
1954 if (!slot)
1955 return NULL;
1956
1957 /* See if we already have entry for type. */
1958 if (*slot)
1959 {
1960 val = *slot;
1961
1962 if (val->type != type && insert
1963 && (!val->types_set || !val->types_set->add (k: type)))
1964 build_bases = add_type_duplicate (val, type);
1965 }
1966 else
1967 {
1968 val = ggc_cleared_alloc<odr_type_d> ();
1969 val->type = type;
1970 val->bases = vNULL;
1971 val->derived_types = vNULL;
1972 if (type_with_linkage_p (t: type))
1973 val->anonymous_namespace = type_in_anonymous_namespace_p (t: type);
1974 else
1975 val->anonymous_namespace = 0;
1976 build_bases = COMPLETE_TYPE_P (val->type);
1977 insert_to_odr_array = true;
1978 *slot = val;
1979 }
1980
1981 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
1982 && type_with_linkage_p (t: type)
1983 && type == TYPE_MAIN_VARIANT (type))
1984 {
1985 tree binfo = TYPE_BINFO (type);
1986 unsigned int i;
1987
1988 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
1989
1990 val->all_derivations_known = type_all_derivations_known_p (t: type);
1991 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
1992 /* For now record only polymorphic types. other are
1993 pointless for devirtualization and we cannot precisely
1994 determine ODR equivalency of these during LTO. */
1995 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
1996 {
1997 tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
1998 odr_type base = get_odr_type (type: base_type, insert: true);
1999 gcc_assert (TYPE_MAIN_VARIANT (base_type) == base_type);
2000 base->derived_types.safe_push (obj: val);
2001 val->bases.safe_push (obj: base);
2002 if (base->id > base_id)
2003 base_id = base->id;
2004 }
2005 }
2006 /* Ensure that type always appears after bases. */
2007 if (insert_to_odr_array)
2008 {
2009 if (odr_types_ptr)
2010 val->id = odr_types.length ();
2011 vec_safe_push (v&: odr_types_ptr, obj: val);
2012 }
2013 else if (base_id > val->id)
2014 {
2015 odr_types[val->id] = 0;
2016 /* Be sure we did not recorded any derived types; these may need
2017 renumbering too. */
2018 gcc_assert (val->derived_types.length() == 0);
2019 val->id = odr_types.length ();
2020 vec_safe_push (v&: odr_types_ptr, obj: val);
2021 }
2022 return val;
2023}
2024
2025/* Return type that in ODR type hash prevailed TYPE. Be careful and punt
2026 on ODR violations. */
2027
2028tree
2029prevailing_odr_type (tree type)
2030{
2031 odr_type t = get_odr_type (type, insert: false);
2032 if (!t || t->odr_violated)
2033 return type;
2034 return t->type;
2035}
2036
2037/* Set tbaa_enabled flag for TYPE. */
2038
2039void
2040enable_odr_based_tbaa (tree type)
2041{
2042 odr_type t = get_odr_type (type, insert: true);
2043 t->tbaa_enabled = true;
2044}
2045
2046/* True if canonical type of TYPE is determined using ODR name. */
2047
2048bool
2049odr_based_tbaa_p (const_tree type)
2050{
2051 if (!RECORD_OR_UNION_TYPE_P (type))
2052 return false;
2053 if (!odr_hash)
2054 return false;
2055 odr_type t = get_odr_type (type: const_cast <tree> (type), insert: false);
2056 if (!t || !t->tbaa_enabled)
2057 return false;
2058 return true;
2059}
2060
2061/* Set TYPE_CANONICAL of type and all its variants and duplicates
2062 to CANONICAL. */
2063
2064void
2065set_type_canonical_for_odr_type (tree type, tree canonical)
2066{
2067 odr_type t = get_odr_type (type, insert: false);
2068 unsigned int i;
2069 tree tt;
2070
2071 for (tree t2 = t->type; t2; t2 = TYPE_NEXT_VARIANT (t2))
2072 TYPE_CANONICAL (t2) = canonical;
2073 if (t->types)
2074 FOR_EACH_VEC_ELT (*t->types, i, tt)
2075 for (tree t2 = tt; t2; t2 = TYPE_NEXT_VARIANT (t2))
2076 TYPE_CANONICAL (t2) = canonical;
2077}
2078
2079/* Return true if we reported some ODR violation on TYPE. */
2080
2081bool
2082odr_type_violation_reported_p (tree type)
2083{
2084 return get_odr_type (type, insert: false)->odr_violated;
2085}
2086
2087/* Add TYPE of ODR type hash. */
2088
2089void
2090register_odr_type (tree type)
2091{
2092 if (!odr_hash)
2093 odr_hash = new odr_hash_type (23);
2094 if (type == TYPE_MAIN_VARIANT (type))
2095 {
2096 /* To get ODR warnings right, first register all sub-types. */
2097 if (RECORD_OR_UNION_TYPE_P (type)
2098 && COMPLETE_TYPE_P (type))
2099 {
2100 /* Limit recursion on types which are already registered. */
2101 odr_type ot = get_odr_type (type, insert: false);
2102 if (ot
2103 && (ot->type == type
2104 || (ot->types_set
2105 && ot->types_set->contains (k: type))))
2106 return;
2107 for (tree f = TYPE_FIELDS (type); f; f = TREE_CHAIN (f))
2108 if (TREE_CODE (f) == FIELD_DECL)
2109 {
2110 tree subtype = TREE_TYPE (f);
2111
2112 while (TREE_CODE (subtype) == ARRAY_TYPE)
2113 subtype = TREE_TYPE (subtype);
2114 if (type_with_linkage_p (TYPE_MAIN_VARIANT (subtype)))
2115 register_odr_type (TYPE_MAIN_VARIANT (subtype));
2116 }
2117 if (TYPE_BINFO (type))
2118 for (unsigned int i = 0;
2119 i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
2120 register_odr_type (BINFO_TYPE (BINFO_BASE_BINFO
2121 (TYPE_BINFO (type), i)));
2122 }
2123 get_odr_type (type, insert: true);
2124 }
2125}
2126
2127/* Return true if type is known to have no derivations. */
2128
2129bool
2130type_known_to_have_no_derivations_p (tree t)
2131{
2132 return (type_all_derivations_known_p (t)
2133 && (TYPE_FINAL_P (t)
2134 || (odr_hash
2135 && !get_odr_type (type: t, insert: true)->derived_types.length())));
2136}
2137
2138/* Dump ODR type T and all its derived types. INDENT specifies indentation for
2139 recursive printing. */
2140
2141static void
2142dump_odr_type (FILE *f, odr_type t, int indent=0)
2143{
2144 unsigned int i;
2145 fprintf (stream: f, format: "%*s type %i: ", indent * 2, "", t->id);
2146 print_generic_expr (f, t->type, TDF_SLIM);
2147 fprintf (stream: f, format: "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
2148 fprintf (stream: f, format: "%s\n", t->all_derivations_known ? " (derivations known)":"");
2149 if (TYPE_NAME (t->type))
2150 {
2151 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
2152 fprintf (stream: f, format: "%*s mangled name: %s\n", indent * 2, "",
2153 IDENTIFIER_POINTER
2154 (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type))));
2155 }
2156 if (t->bases.length ())
2157 {
2158 fprintf (stream: f, format: "%*s base odr type ids: ", indent * 2, "");
2159 for (i = 0; i < t->bases.length (); i++)
2160 fprintf (stream: f, format: " %i", t->bases[i]->id);
2161 fprintf (stream: f, format: "\n");
2162 }
2163 if (t->derived_types.length ())
2164 {
2165 fprintf (stream: f, format: "%*s derived types:\n", indent * 2, "");
2166 for (i = 0; i < t->derived_types.length (); i++)
2167 dump_odr_type (f, t: t->derived_types[i], indent: indent + 1);
2168 }
2169 fprintf (stream: f, format: "\n");
2170}
2171
2172/* Dump the type inheritance graph. */
2173
2174static void
2175dump_type_inheritance_graph (FILE *f)
2176{
2177 unsigned int i;
2178 unsigned int num_all_types = 0, num_types = 0, num_duplicates = 0;
2179 if (!odr_types_ptr)
2180 return;
2181 fprintf (stream: f, format: "\n\nType inheritance graph:\n");
2182 for (i = 0; i < odr_types.length (); i++)
2183 {
2184 if (odr_types[i] && odr_types[i]->bases.length () == 0)
2185 dump_odr_type (f, odr_types[i]);
2186 }
2187 for (i = 0; i < odr_types.length (); i++)
2188 {
2189 if (!odr_types[i])
2190 continue;
2191
2192 num_all_types++;
2193 if (!odr_types[i]->types || !odr_types[i]->types->length ())
2194 continue;
2195
2196 /* To aid ODR warnings we also mangle integer constants but do
2197 not consider duplicates there. */
2198 if (TREE_CODE (odr_types[i]->type) == INTEGER_TYPE)
2199 continue;
2200
2201 /* It is normal to have one duplicate and one normal variant. */
2202 if (odr_types[i]->types->length () == 1
2203 && COMPLETE_TYPE_P (odr_types[i]->type)
2204 && !COMPLETE_TYPE_P ((*odr_types[i]->types)[0]))
2205 continue;
2206
2207 num_types ++;
2208
2209 unsigned int j;
2210 fprintf (stream: f, format: "Duplicate tree types for odr type %i\n", i);
2211 print_node (f, "", odr_types[i]->type, 0);
2212 print_node (f, "", TYPE_NAME (odr_types[i]->type), 0);
2213 putc (c: '\n',stream: f);
2214 for (j = 0; j < odr_types[i]->types->length (); j++)
2215 {
2216 tree t;
2217 num_duplicates ++;
2218 fprintf (stream: f, format: "duplicate #%i\n", j);
2219 print_node (f, "", (*odr_types[i]->types)[j], 0);
2220 t = (*odr_types[i]->types)[j];
2221 while (TYPE_P (t) && TYPE_CONTEXT (t))
2222 {
2223 t = TYPE_CONTEXT (t);
2224 print_node (f, "", t, 0);
2225 }
2226 print_node (f, "", TYPE_NAME ((*odr_types[i]->types)[j]), 0);
2227 putc (c: '\n',stream: f);
2228 }
2229 }
2230 fprintf (stream: f, format: "Out of %i types there are %i types with duplicates; "
2231 "%i duplicates overall\n", num_all_types, num_types, num_duplicates);
2232}
2233
2234/* Save some WPA->ltrans streaming by freeing stuff needed only for good
2235 ODR warnings.
2236 We make TYPE_DECLs to not point back
2237 to the type (which is needed to keep them in the same SCC and preserve
2238 location information to output warnings) and subsequently we make all
2239 TYPE_DECLS of same assembler name equivalent. */
2240
2241static void
2242free_odr_warning_data ()
2243{
2244 static bool odr_data_freed = false;
2245
2246 if (odr_data_freed || !flag_wpa || !odr_types_ptr)
2247 return;
2248
2249 odr_data_freed = true;
2250
2251 for (unsigned int i = 0; i < odr_types.length (); i++)
2252 if (odr_types[i])
2253 {
2254 tree t = odr_types[i]->type;
2255
2256 TREE_TYPE (TYPE_NAME (t)) = void_type_node;
2257
2258 if (odr_types[i]->types)
2259 for (unsigned int j = 0; j < odr_types[i]->types->length (); j++)
2260 {
2261 tree td = (*odr_types[i]->types)[j];
2262
2263 TYPE_NAME (td) = TYPE_NAME (t);
2264 }
2265 }
2266 odr_data_freed = true;
2267}
2268
2269/* Initialize IPA devirt and build inheritance tree graph. */
2270
2271void
2272build_type_inheritance_graph (void)
2273{
2274 struct symtab_node *n;
2275 FILE *inheritance_dump_file;
2276 dump_flags_t flags;
2277
2278 if (odr_hash)
2279 {
2280 free_odr_warning_data ();
2281 return;
2282 }
2283 timevar_push (tv: TV_IPA_INHERITANCE);
2284 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
2285 odr_hash = new odr_hash_type (23);
2286
2287 /* We reconstruct the graph starting of types of all methods seen in the
2288 unit. */
2289 FOR_EACH_SYMBOL (n)
2290 if (is_a <cgraph_node *> (p: n)
2291 && DECL_VIRTUAL_P (n->decl)
2292 && n->real_symbol_p ())
2293 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), insert: true);
2294
2295 /* Look also for virtual tables of types that do not define any methods.
2296
2297 We need it in a case where class B has virtual base of class A
2298 re-defining its virtual method and there is class C with no virtual
2299 methods with B as virtual base.
2300
2301 Here we output B's virtual method in two variant - for non-virtual
2302 and virtual inheritance. B's virtual table has non-virtual version,
2303 while C's has virtual.
2304
2305 For this reason we need to know about C in order to include both
2306 variants of B. More correctly, record_target_from_binfo should
2307 add both variants of the method when walking B, but we have no
2308 link in between them.
2309
2310 We rely on fact that either the method is exported and thus we
2311 assume it is called externally or C is in anonymous namespace and
2312 thus we will see the vtable. */
2313
2314 else if (is_a <varpool_node *> (p: n)
2315 && DECL_VIRTUAL_P (n->decl)
2316 && TREE_CODE (DECL_CONTEXT (n->decl)) == RECORD_TYPE
2317 && TYPE_BINFO (DECL_CONTEXT (n->decl))
2318 && polymorphic_type_binfo_p (TYPE_BINFO (DECL_CONTEXT (n->decl))))
2319 get_odr_type (TYPE_MAIN_VARIANT (DECL_CONTEXT (n->decl)), insert: true);
2320 if (inheritance_dump_file)
2321 {
2322 dump_type_inheritance_graph (f: inheritance_dump_file);
2323 dump_end (TDI_inheritance, inheritance_dump_file);
2324 }
2325 free_odr_warning_data ();
2326 timevar_pop (tv: TV_IPA_INHERITANCE);
2327}
2328
2329/* Return true if N has reference from live virtual table
2330 (and thus can be a destination of polymorphic call).
2331 Be conservatively correct when callgraph is not built or
2332 if the method may be referred externally. */
2333
2334static bool
2335referenced_from_vtable_p (struct cgraph_node *node)
2336{
2337 int i;
2338 struct ipa_ref *ref;
2339 bool found = false;
2340
2341 if (node->externally_visible
2342 || DECL_EXTERNAL (node->decl)
2343 || node->used_from_other_partition)
2344 return true;
2345
2346 /* Keep this test constant time.
2347 It is unlikely this can happen except for the case where speculative
2348 devirtualization introduced many speculative edges to this node.
2349 In this case the target is very likely alive anyway. */
2350 if (node->ref_list.referring.length () > 100)
2351 return true;
2352
2353 /* We need references built. */
2354 if (symtab->state <= CONSTRUCTION)
2355 return true;
2356
2357 for (i = 0; node->iterate_referring (i, ref); i++)
2358 if ((ref->use == IPA_REF_ALIAS
2359 && referenced_from_vtable_p (node: dyn_cast<cgraph_node *> (p: ref->referring)))
2360 || (ref->use == IPA_REF_ADDR
2361 && VAR_P (ref->referring->decl)
2362 && DECL_VIRTUAL_P (ref->referring->decl)))
2363 {
2364 found = true;
2365 break;
2366 }
2367 return found;
2368}
2369
2370/* Return if TARGET is cxa_pure_virtual. */
2371
2372static bool
2373is_cxa_pure_virtual_p (tree target)
2374{
2375 return target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE
2376 && DECL_NAME (target)
2377 && id_equal (DECL_NAME (target),
2378 str: "__cxa_pure_virtual");
2379}
2380
2381/* If TARGET has associated node, record it in the NODES array.
2382 CAN_REFER specify if program can refer to the target directly.
2383 if TARGET is unknown (NULL) or it cannot be inserted (for example because
2384 its body was already removed and there is no way to refer to it), clear
2385 COMPLETEP. */
2386
2387static void
2388maybe_record_node (vec <cgraph_node *> &nodes,
2389 tree target, hash_set<tree> *inserted,
2390 bool can_refer,
2391 bool *completep)
2392{
2393 struct cgraph_node *target_node, *alias_target;
2394 enum availability avail;
2395 bool pure_virtual = is_cxa_pure_virtual_p (target);
2396
2397 /* __builtin_unreachable do not need to be added into
2398 list of targets; the runtime effect of calling them is undefined.
2399 Only "real" virtual methods should be accounted. */
2400 if (target && TREE_CODE (TREE_TYPE (target)) != METHOD_TYPE && !pure_virtual)
2401 return;
2402
2403 if (!can_refer)
2404 {
2405 /* The only case when method of anonymous namespace becomes unreferable
2406 is when we completely optimized it out. */
2407 if (flag_ltrans
2408 || !target
2409 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2410 *completep = false;
2411 return;
2412 }
2413
2414 if (!target)
2415 return;
2416
2417 target_node = cgraph_node::get (decl: target);
2418
2419 /* Prefer alias target over aliases, so we do not get confused by
2420 fake duplicates. */
2421 if (target_node)
2422 {
2423 alias_target = target_node->ultimate_alias_target (availability: &avail);
2424 if (target_node != alias_target
2425 && avail >= AVAIL_AVAILABLE
2426 && target_node->get_availability ())
2427 target_node = alias_target;
2428 }
2429
2430 /* Method can only be called by polymorphic call if any
2431 of vtables referring to it are alive.
2432
2433 While this holds for non-anonymous functions, too, there are
2434 cases where we want to keep them in the list; for example
2435 inline functions with -fno-weak are static, but we still
2436 may devirtualize them when instance comes from other unit.
2437 The same holds for LTO.
2438
2439 Currently we ignore these functions in speculative devirtualization.
2440 ??? Maybe it would make sense to be more aggressive for LTO even
2441 elsewhere. */
2442 if (!flag_ltrans
2443 && !pure_virtual
2444 && type_in_anonymous_namespace_p (DECL_CONTEXT (target))
2445 && (!target_node
2446 || !referenced_from_vtable_p (node: target_node)))
2447 ;
2448 /* See if TARGET is useful function we can deal with. */
2449 else if (target_node != NULL
2450 && (TREE_PUBLIC (target)
2451 || DECL_EXTERNAL (target)
2452 || target_node->definition)
2453 && target_node->real_symbol_p ())
2454 {
2455 gcc_assert (!target_node->inlined_to);
2456 gcc_assert (target_node->real_symbol_p ());
2457 /* When sanitizing, do not assume that __cxa_pure_virtual is not called
2458 by valid program. */
2459 if (flag_sanitize & SANITIZE_UNREACHABLE)
2460 ;
2461 /* Only add pure virtual if it is the only possible target. This way
2462 we will preserve the diagnostics about pure virtual called in many
2463 cases without disabling optimization in other. */
2464 else if (pure_virtual)
2465 {
2466 if (nodes.length ())
2467 return;
2468 }
2469 /* If we found a real target, take away cxa_pure_virtual. */
2470 else if (!pure_virtual && nodes.length () == 1
2471 && is_cxa_pure_virtual_p (target: nodes[0]->decl))
2472 nodes.pop ();
2473 if (pure_virtual && nodes.length ())
2474 return;
2475 if (!inserted->add (k: target))
2476 {
2477 cached_polymorphic_call_targets->add (k: target_node);
2478 nodes.safe_push (obj: target_node);
2479 }
2480 }
2481 else if (!completep)
2482 ;
2483 /* We have definition of __cxa_pure_virtual that is not accessible (it is
2484 optimized out or partitioned to other unit) so we cannot add it. When
2485 not sanitizing, there is nothing to do.
2486 Otherwise declare the list incomplete. */
2487 else if (pure_virtual)
2488 {
2489 if (flag_sanitize & SANITIZE_UNREACHABLE)
2490 *completep = false;
2491 }
2492 else if (flag_ltrans
2493 || !type_in_anonymous_namespace_p (DECL_CONTEXT (target)))
2494 *completep = false;
2495}
2496
2497/* See if BINFO's type matches OUTER_TYPE. If so, look up
2498 BINFO of subtype of OTR_TYPE at OFFSET and in that BINFO find
2499 method in vtable and insert method to NODES array
2500 or BASES_TO_CONSIDER if this array is non-NULL.
2501 Otherwise recurse to base BINFOs.
2502 This matches what get_binfo_at_offset does, but with offset
2503 being unknown.
2504
2505 TYPE_BINFOS is a stack of BINFOS of types with defined
2506 virtual table seen on way from class type to BINFO.
2507
2508 MATCHED_VTABLES tracks virtual tables we already did lookup
2509 for virtual function in. INSERTED tracks nodes we already
2510 inserted.
2511
2512 ANONYMOUS is true if BINFO is part of anonymous namespace.
2513
2514 Clear COMPLETEP when we hit unreferable target.
2515 */
2516
2517static void
2518record_target_from_binfo (vec <cgraph_node *> &nodes,
2519 vec <tree> *bases_to_consider,
2520 tree binfo,
2521 tree otr_type,
2522 vec <tree> &type_binfos,
2523 HOST_WIDE_INT otr_token,
2524 tree outer_type,
2525 HOST_WIDE_INT offset,
2526 hash_set<tree> *inserted,
2527 hash_set<tree> *matched_vtables,
2528 bool anonymous,
2529 bool *completep)
2530{
2531 tree type = BINFO_TYPE (binfo);
2532 int i;
2533 tree base_binfo;
2534
2535
2536 if (BINFO_VTABLE (binfo))
2537 type_binfos.safe_push (obj: binfo);
2538 if (types_same_for_odr (type1: type, type2: outer_type))
2539 {
2540 int i;
2541 tree type_binfo = NULL;
2542
2543 /* Look up BINFO with virtual table. For normal types it is always last
2544 binfo on stack. */
2545 for (i = type_binfos.length () - 1; i >= 0; i--)
2546 if (BINFO_OFFSET (type_binfos[i]) == BINFO_OFFSET (binfo))
2547 {
2548 type_binfo = type_binfos[i];
2549 break;
2550 }
2551 if (BINFO_VTABLE (binfo))
2552 type_binfos.pop ();
2553 /* If this is duplicated BINFO for base shared by virtual inheritance,
2554 we may not have its associated vtable. This is not a problem, since
2555 we will walk it on the other path. */
2556 if (!type_binfo)
2557 return;
2558 tree inner_binfo = get_binfo_at_offset (type_binfo,
2559 offset, otr_type);
2560 if (!inner_binfo)
2561 {
2562 gcc_assert (odr_violation_reported);
2563 return;
2564 }
2565 /* For types in anonymous namespace first check if the respective vtable
2566 is alive. If not, we know the type can't be called. */
2567 if (!flag_ltrans && anonymous)
2568 {
2569 tree vtable = BINFO_VTABLE (inner_binfo);
2570 varpool_node *vnode;
2571
2572 if (TREE_CODE (vtable) == POINTER_PLUS_EXPR)
2573 vtable = TREE_OPERAND (TREE_OPERAND (vtable, 0), 0);
2574 vnode = varpool_node::get (decl: vtable);
2575 if (!vnode || !vnode->definition)
2576 return;
2577 }
2578 gcc_assert (inner_binfo);
2579 if (bases_to_consider
2580 ? !matched_vtables->contains (BINFO_VTABLE (inner_binfo))
2581 : !matched_vtables->add (BINFO_VTABLE (inner_binfo)))
2582 {
2583 bool can_refer;
2584 tree target = gimple_get_virt_method_for_binfo (otr_token,
2585 inner_binfo,
2586 can_refer: &can_refer);
2587 if (!bases_to_consider)
2588 maybe_record_node (nodes, target, inserted, can_refer, completep);
2589 /* Destructors are never called via construction vtables. */
2590 else if (!target || !DECL_CXX_DESTRUCTOR_P (target))
2591 bases_to_consider->safe_push (obj: target);
2592 }
2593 return;
2594 }
2595
2596 /* Walk bases. */
2597 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2598 /* Walking bases that have no virtual method is pointless exercise. */
2599 if (polymorphic_type_binfo_p (binfo: base_binfo))
2600 record_target_from_binfo (nodes, bases_to_consider, binfo: base_binfo, otr_type,
2601 type_binfos,
2602 otr_token, outer_type, offset, inserted,
2603 matched_vtables, anonymous, completep);
2604 if (BINFO_VTABLE (binfo))
2605 type_binfos.pop ();
2606}
2607
2608/* Look up virtual methods matching OTR_TYPE (with OFFSET and OTR_TOKEN)
2609 of TYPE, insert them to NODES, recurse into derived nodes.
2610 INSERTED is used to avoid duplicate insertions of methods into NODES.
2611 MATCHED_VTABLES are used to avoid duplicate walking vtables.
2612 Clear COMPLETEP if unreferable target is found.
2613
2614 If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
2615 all cases where BASE_SKIPPED is true (because the base is abstract
2616 class). */
2617
2618static void
2619possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
2620 hash_set<tree> *inserted,
2621 hash_set<tree> *matched_vtables,
2622 tree otr_type,
2623 odr_type type,
2624 HOST_WIDE_INT otr_token,
2625 tree outer_type,
2626 HOST_WIDE_INT offset,
2627 bool *completep,
2628 vec <tree> &bases_to_consider,
2629 bool consider_construction)
2630{
2631 tree binfo = TYPE_BINFO (type->type);
2632 unsigned int i;
2633 auto_vec <tree, 8> type_binfos;
2634 bool possibly_instantiated = type_possibly_instantiated_p (t: type->type);
2635
2636 /* We may need to consider types w/o instances because of possible derived
2637 types using their methods either directly or via construction vtables.
2638 We are safe to skip them when all derivations are known, since we will
2639 handle them later.
2640 This is done by recording them to BASES_TO_CONSIDER array. */
2641 if (possibly_instantiated || consider_construction)
2642 {
2643 record_target_from_binfo (nodes,
2644 bases_to_consider: (!possibly_instantiated
2645 && type_all_derivations_known_p (t: type->type))
2646 ? &bases_to_consider : NULL,
2647 binfo, otr_type, type_binfos, otr_token,
2648 outer_type, offset,
2649 inserted, matched_vtables,
2650 anonymous: type->anonymous_namespace, completep);
2651 }
2652 for (i = 0; i < type->derived_types.length (); i++)
2653 possible_polymorphic_call_targets_1 (nodes, inserted,
2654 matched_vtables,
2655 otr_type,
2656 type: type->derived_types[i],
2657 otr_token, outer_type, offset, completep,
2658 bases_to_consider, consider_construction);
2659}
2660
2661/* Cache of queries for polymorphic call targets.
2662
2663 Enumerating all call targets may get expensive when there are many
2664 polymorphic calls in the program, so we memoize all the previous
2665 queries and avoid duplicated work. */
2666
2667class polymorphic_call_target_d
2668{
2669public:
2670 HOST_WIDE_INT otr_token;
2671 ipa_polymorphic_call_context context;
2672 odr_type type;
2673 vec <cgraph_node *> targets;
2674 tree decl_warning;
2675 int type_warning;
2676 unsigned int n_odr_types;
2677 bool complete;
2678 bool speculative;
2679};
2680
2681/* Polymorphic call target cache helpers. */
2682
2683struct polymorphic_call_target_hasher
2684 : pointer_hash <polymorphic_call_target_d>
2685{
2686 static inline hashval_t hash (const polymorphic_call_target_d *);
2687 static inline bool equal (const polymorphic_call_target_d *,
2688 const polymorphic_call_target_d *);
2689 static inline void remove (polymorphic_call_target_d *);
2690};
2691
2692/* Return the computed hashcode for ODR_QUERY. */
2693
2694inline hashval_t
2695polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query)
2696{
2697 inchash::hash hstate (odr_query->otr_token);
2698
2699 hstate.add_hwi (v: odr_query->type->id);
2700 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
2701 hstate.add_hwi (v: odr_query->context.offset);
2702 hstate.add_hwi (v: odr_query->n_odr_types);
2703
2704 if (odr_query->context.speculative_outer_type)
2705 {
2706 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
2707 hstate.add_hwi (v: odr_query->context.speculative_offset);
2708 }
2709 hstate.add_flag (flag: odr_query->speculative);
2710 hstate.add_flag (flag: odr_query->context.maybe_in_construction);
2711 hstate.add_flag (flag: odr_query->context.maybe_derived_type);
2712 hstate.add_flag (flag: odr_query->context.speculative_maybe_derived_type);
2713 hstate.commit_flag ();
2714 return hstate.end ();
2715}
2716
2717/* Compare cache entries T1 and T2. */
2718
2719inline bool
2720polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
2721 const polymorphic_call_target_d *t2)
2722{
2723 return (t1->type == t2->type && t1->otr_token == t2->otr_token
2724 && t1->speculative == t2->speculative
2725 && t1->context.offset == t2->context.offset
2726 && t1->context.speculative_offset == t2->context.speculative_offset
2727 && t1->context.outer_type == t2->context.outer_type
2728 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
2729 && t1->context.maybe_in_construction
2730 == t2->context.maybe_in_construction
2731 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
2732 && (t1->context.speculative_maybe_derived_type
2733 == t2->context.speculative_maybe_derived_type)
2734 /* Adding new type may affect outcome of target search. */
2735 && t1->n_odr_types == t2->n_odr_types);
2736}
2737
2738/* Remove entry in polymorphic call target cache hash. */
2739
2740inline void
2741polymorphic_call_target_hasher::remove (polymorphic_call_target_d *v)
2742{
2743 v->targets.release ();
2744 free (ptr: v);
2745}
2746
2747/* Polymorphic call target query cache. */
2748
2749typedef hash_table<polymorphic_call_target_hasher>
2750 polymorphic_call_target_hash_type;
2751static polymorphic_call_target_hash_type *polymorphic_call_target_hash;
2752
2753/* Destroy polymorphic call target query cache. */
2754
2755static void
2756free_polymorphic_call_targets_hash ()
2757{
2758 if (cached_polymorphic_call_targets)
2759 {
2760 delete polymorphic_call_target_hash;
2761 polymorphic_call_target_hash = NULL;
2762 delete cached_polymorphic_call_targets;
2763 cached_polymorphic_call_targets = NULL;
2764 }
2765}
2766
2767/* Force rebuilding type inheritance graph from scratch.
2768 This is use to make sure that we do not keep references to types
2769 which was not visible to free_lang_data. */
2770
2771void
2772rebuild_type_inheritance_graph ()
2773{
2774 if (!odr_hash)
2775 return;
2776 delete odr_hash;
2777 odr_hash = NULL;
2778 odr_types_ptr = NULL;
2779 free_polymorphic_call_targets_hash ();
2780}
2781
2782/* When virtual function is removed, we may need to flush the cache. */
2783
2784static void
2785devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
2786{
2787 if (cached_polymorphic_call_targets
2788 && !thunk_expansion
2789 && cached_polymorphic_call_targets->contains (k: n))
2790 free_polymorphic_call_targets_hash ();
2791}
2792
2793/* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2794
2795tree
2796subbinfo_with_vtable_at_offset (tree binfo, unsigned HOST_WIDE_INT offset,
2797 tree vtable)
2798{
2799 tree v = BINFO_VTABLE (binfo);
2800 int i;
2801 tree base_binfo;
2802 unsigned HOST_WIDE_INT this_offset;
2803
2804 if (v)
2805 {
2806 if (!vtable_pointer_value_to_vtable (v, &v, &this_offset))
2807 gcc_unreachable ();
2808
2809 if (offset == this_offset
2810 && DECL_ASSEMBLER_NAME (v) == DECL_ASSEMBLER_NAME (vtable))
2811 return binfo;
2812 }
2813
2814 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2815 if (polymorphic_type_binfo_p (binfo: base_binfo))
2816 {
2817 base_binfo = subbinfo_with_vtable_at_offset (binfo: base_binfo, offset, vtable);
2818 if (base_binfo)
2819 return base_binfo;
2820 }
2821 return NULL;
2822}
2823
2824/* T is known constant value of virtual table pointer.
2825 Store virtual table to V and its offset to OFFSET.
2826 Return false if T does not look like virtual table reference. */
2827
2828bool
2829vtable_pointer_value_to_vtable (const_tree t, tree *v,
2830 unsigned HOST_WIDE_INT *offset)
2831{
2832 /* We expect &MEM[(void *)&virtual_table + 16B].
2833 We obtain object's BINFO from the context of the virtual table.
2834 This one contains pointer to virtual table represented via
2835 POINTER_PLUS_EXPR. Verify that this pointer matches what
2836 we propagated through.
2837
2838 In the case of virtual inheritance, the virtual tables may
2839 be nested, i.e. the offset may be different from 16 and we may
2840 need to dive into the type representation. */
2841 if (TREE_CODE (t) == ADDR_EXPR
2842 && TREE_CODE (TREE_OPERAND (t, 0)) == MEM_REF
2843 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == ADDR_EXPR
2844 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST
2845 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0))
2846 == VAR_DECL)
2847 && DECL_VIRTUAL_P (TREE_OPERAND (TREE_OPERAND
2848 (TREE_OPERAND (t, 0), 0), 0)))
2849 {
2850 *v = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 0);
2851 *offset = tree_to_uhwi (TREE_OPERAND (TREE_OPERAND (t, 0), 1));
2852 return true;
2853 }
2854
2855 /* Alternative representation, used by C++ frontend is POINTER_PLUS_EXPR.
2856 We need to handle it when T comes from static variable initializer or
2857 BINFO. */
2858 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
2859 {
2860 *offset = tree_to_uhwi (TREE_OPERAND (t, 1));
2861 t = TREE_OPERAND (t, 0);
2862 }
2863 else
2864 *offset = 0;
2865
2866 if (TREE_CODE (t) != ADDR_EXPR)
2867 return false;
2868 *v = TREE_OPERAND (t, 0);
2869 return true;
2870}
2871
2872/* T is known constant value of virtual table pointer. Return BINFO of the
2873 instance type. */
2874
2875tree
2876vtable_pointer_value_to_binfo (const_tree t)
2877{
2878 tree vtable;
2879 unsigned HOST_WIDE_INT offset;
2880
2881 if (!vtable_pointer_value_to_vtable (t, v: &vtable, offset: &offset))
2882 return NULL_TREE;
2883
2884 /* FIXME: for stores of construction vtables we return NULL,
2885 because we do not have BINFO for those. Eventually we should fix
2886 our representation to allow this case to be handled, too.
2887 In the case we see store of BINFO we however may assume
2888 that standard folding will be able to cope with it. */
2889 return subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
2890 offset, vtable);
2891}
2892
2893/* Walk bases of OUTER_TYPE that contain OTR_TYPE at OFFSET.
2894 Look up their respective virtual methods for OTR_TOKEN and OTR_TYPE
2895 and insert them in NODES.
2896
2897 MATCHED_VTABLES and INSERTED is used to avoid duplicated work. */
2898
2899static void
2900record_targets_from_bases (tree otr_type,
2901 HOST_WIDE_INT otr_token,
2902 tree outer_type,
2903 HOST_WIDE_INT offset,
2904 vec <cgraph_node *> &nodes,
2905 hash_set<tree> *inserted,
2906 hash_set<tree> *matched_vtables,
2907 bool *completep)
2908{
2909 while (true)
2910 {
2911 HOST_WIDE_INT pos, size;
2912 tree base_binfo;
2913 tree fld;
2914
2915 if (types_same_for_odr (type1: outer_type, type2: otr_type))
2916 return;
2917
2918 for (fld = TYPE_FIELDS (outer_type); fld; fld = DECL_CHAIN (fld))
2919 {
2920 if (TREE_CODE (fld) != FIELD_DECL)
2921 continue;
2922
2923 pos = int_bit_position (field: fld);
2924 size = tree_to_shwi (DECL_SIZE (fld));
2925 if (pos <= offset && (pos + size) > offset
2926 /* Do not get confused by zero sized bases. */
2927 && polymorphic_type_binfo_p (TYPE_BINFO (TREE_TYPE (fld))))
2928 break;
2929 }
2930 /* Within a class type we should always find corresponding fields. */
2931 gcc_assert (fld && TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE);
2932
2933 /* Nonbase types should have been stripped by outer_class_type. */
2934 gcc_assert (DECL_ARTIFICIAL (fld));
2935
2936 outer_type = TREE_TYPE (fld);
2937 offset -= pos;
2938
2939 base_binfo = get_binfo_at_offset (TYPE_BINFO (outer_type),
2940 offset, otr_type);
2941 if (!base_binfo)
2942 {
2943 gcc_assert (odr_violation_reported);
2944 return;
2945 }
2946 gcc_assert (base_binfo);
2947 if (!matched_vtables->add (BINFO_VTABLE (base_binfo)))
2948 {
2949 bool can_refer;
2950 tree target = gimple_get_virt_method_for_binfo (otr_token,
2951 base_binfo,
2952 can_refer: &can_refer);
2953 if (!target || ! DECL_CXX_DESTRUCTOR_P (target))
2954 maybe_record_node (nodes, target, inserted, can_refer, completep);
2955 matched_vtables->add (BINFO_VTABLE (base_binfo));
2956 }
2957 }
2958}
2959
2960/* When virtual table is removed, we may need to flush the cache. */
2961
2962static void
2963devirt_variable_node_removal_hook (varpool_node *n,
2964 void *d ATTRIBUTE_UNUSED)
2965{
2966 if (cached_polymorphic_call_targets
2967 && DECL_VIRTUAL_P (n->decl)
2968 && type_in_anonymous_namespace_p (DECL_CONTEXT (n->decl)))
2969 free_polymorphic_call_targets_hash ();
2970}
2971
2972/* Record about how many calls would benefit from given type to be final. */
2973
2974struct odr_type_warn_count
2975{
2976 tree type;
2977 int count;
2978 profile_count dyn_count;
2979};
2980
2981/* Record about how many calls would benefit from given method to be final. */
2982
2983struct decl_warn_count
2984{
2985 tree decl;
2986 int count;
2987 profile_count dyn_count;
2988};
2989
2990/* Information about type and decl warnings. */
2991
2992class final_warning_record
2993{
2994public:
2995 /* If needed grow type_warnings vector and initialize new decl_warn_count
2996 to have dyn_count set to profile_count::zero (). */
2997 void grow_type_warnings (unsigned newlen);
2998
2999 profile_count dyn_count;
3000 auto_vec<odr_type_warn_count> type_warnings;
3001 hash_map<tree, decl_warn_count> decl_warnings;
3002};
3003
3004void
3005final_warning_record::grow_type_warnings (unsigned newlen)
3006{
3007 unsigned len = type_warnings.length ();
3008 if (newlen > len)
3009 {
3010 type_warnings.safe_grow_cleared (len: newlen, exact: true);
3011 for (unsigned i = len; i < newlen; i++)
3012 type_warnings[i].dyn_count = profile_count::zero ();
3013 }
3014}
3015
3016class final_warning_record *final_warning_records;
3017
3018/* Return vector containing possible targets of polymorphic call of type
3019 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
3020 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
3021 OTR_TYPE and include their virtual method. This is useful for types
3022 possibly in construction or destruction where the virtual table may
3023 temporarily change to one of base types. INCLUDE_DERIVED_TYPES make
3024 us to walk the inheritance graph for all derivations.
3025
3026 If COMPLETEP is non-NULL, store true if the list is complete.
3027 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
3028 in the target cache. If user needs to visit every target list
3029 just once, it can memoize them.
3030
3031 If SPECULATIVE is set, the list will not contain targets that
3032 are not speculatively taken.
3033
3034 Returned vector is placed into cache. It is NOT caller's responsibility
3035 to free it. The vector can be freed on cgraph_remove_node call if
3036 the particular node is a virtual function present in the cache. */
3037
3038vec <cgraph_node *>
3039possible_polymorphic_call_targets (tree otr_type,
3040 HOST_WIDE_INT otr_token,
3041 ipa_polymorphic_call_context context,
3042 bool *completep,
3043 void **cache_token,
3044 bool speculative)
3045{
3046 static struct cgraph_node_hook_list *node_removal_hook_holder;
3047 vec <cgraph_node *> nodes = vNULL;
3048 auto_vec <tree, 8> bases_to_consider;
3049 odr_type type, outer_type;
3050 polymorphic_call_target_d key;
3051 polymorphic_call_target_d **slot;
3052 unsigned int i;
3053 tree binfo, target;
3054 bool complete;
3055 bool can_refer = false;
3056 bool skipped = false;
3057
3058 otr_type = TYPE_MAIN_VARIANT (otr_type);
3059
3060 /* If ODR is not initialized or the context is invalid, return empty
3061 incomplete list. */
3062 if (!odr_hash || context.invalid || !TYPE_BINFO (otr_type))
3063 {
3064 if (completep)
3065 *completep = context.invalid;
3066 if (cache_token)
3067 *cache_token = NULL;
3068 return nodes;
3069 }
3070
3071 /* Do not bother to compute speculative info when user do not asks for it. */
3072 if (!speculative || !context.speculative_outer_type)
3073 context.clear_speculation ();
3074
3075 type = get_odr_type (type: otr_type, insert: true);
3076
3077 /* Recording type variants would waste results cache. */
3078 gcc_assert (!context.outer_type
3079 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3080
3081 /* Look up the outer class type we want to walk.
3082 If we fail to do so, the context is invalid. */
3083 if ((context.outer_type || context.speculative_outer_type)
3084 && !context.restrict_to_inner_class (otr_type))
3085 {
3086 if (completep)
3087 *completep = true;
3088 if (cache_token)
3089 *cache_token = NULL;
3090 return nodes;
3091 }
3092 gcc_assert (!context.invalid);
3093
3094 /* Check that restrict_to_inner_class kept the main variant. */
3095 gcc_assert (!context.outer_type
3096 || TYPE_MAIN_VARIANT (context.outer_type) == context.outer_type);
3097
3098 /* We canonicalize our query, so we do not need extra hashtable entries. */
3099
3100 /* Without outer type, we have no use for offset. Just do the
3101 basic search from inner type. */
3102 if (!context.outer_type)
3103 context.clear_outer_type (otr_type);
3104 /* We need to update our hierarchy if the type does not exist. */
3105 outer_type = get_odr_type (type: context.outer_type, insert: true);
3106 /* If the type is complete, there are no derivations. */
3107 if (TYPE_FINAL_P (outer_type->type))
3108 context.maybe_derived_type = false;
3109
3110 /* Initialize query cache. */
3111 if (!cached_polymorphic_call_targets)
3112 {
3113 cached_polymorphic_call_targets = new hash_set<cgraph_node *>;
3114 polymorphic_call_target_hash
3115 = new polymorphic_call_target_hash_type (23);
3116 if (!node_removal_hook_holder)
3117 {
3118 node_removal_hook_holder =
3119 symtab->add_cgraph_removal_hook (hook: &devirt_node_removal_hook, NULL);
3120 symtab->add_varpool_removal_hook (hook: &devirt_variable_node_removal_hook,
3121 NULL);
3122 }
3123 }
3124
3125 if (in_lto_p)
3126 {
3127 if (context.outer_type != otr_type)
3128 context.outer_type
3129 = get_odr_type (type: context.outer_type, insert: true)->type;
3130 if (context.speculative_outer_type)
3131 context.speculative_outer_type
3132 = get_odr_type (type: context.speculative_outer_type, insert: true)->type;
3133 }
3134
3135 /* Look up cached answer. */
3136 key.type = type;
3137 key.otr_token = otr_token;
3138 key.speculative = speculative;
3139 key.context = context;
3140 key.n_odr_types = odr_types.length ();
3141 slot = polymorphic_call_target_hash->find_slot (value: &key, insert: INSERT);
3142 if (cache_token)
3143 *cache_token = (void *)*slot;
3144 if (*slot)
3145 {
3146 if (completep)
3147 *completep = (*slot)->complete;
3148 if ((*slot)->type_warning && final_warning_records)
3149 {
3150 final_warning_records->type_warnings[(*slot)->type_warning - 1].count++;
3151 if (!final_warning_records->type_warnings
3152 [(*slot)->type_warning - 1].dyn_count.initialized_p ())
3153 final_warning_records->type_warnings
3154 [(*slot)->type_warning - 1].dyn_count = profile_count::zero ();
3155 if (final_warning_records->dyn_count > 0)
3156 final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3157 = final_warning_records->type_warnings[(*slot)->type_warning - 1].dyn_count
3158 + final_warning_records->dyn_count;
3159 }
3160 if (!speculative && (*slot)->decl_warning && final_warning_records)
3161 {
3162 struct decl_warn_count *c =
3163 final_warning_records->decl_warnings.get (k: (*slot)->decl_warning);
3164 c->count++;
3165 if (final_warning_records->dyn_count > 0)
3166 c->dyn_count += final_warning_records->dyn_count;
3167 }
3168 return (*slot)->targets;
3169 }
3170
3171 complete = true;
3172
3173 /* Do actual search. */
3174 timevar_push (tv: TV_IPA_VIRTUAL_CALL);
3175 *slot = XCNEW (polymorphic_call_target_d);
3176 if (cache_token)
3177 *cache_token = (void *)*slot;
3178 (*slot)->type = type;
3179 (*slot)->otr_token = otr_token;
3180 (*slot)->context = context;
3181 (*slot)->speculative = speculative;
3182
3183 hash_set<tree> inserted;
3184 hash_set<tree> matched_vtables;
3185
3186 /* First insert targets we speculatively identified as likely. */
3187 if (context.speculative_outer_type)
3188 {
3189 odr_type speculative_outer_type;
3190 bool speculation_complete = true;
3191 bool check_derived_types = false;
3192
3193 /* First insert target from type itself and check if it may have
3194 derived types. */
3195 speculative_outer_type = get_odr_type (type: context.speculative_outer_type, insert: true);
3196 if (TYPE_FINAL_P (speculative_outer_type->type))
3197 context.speculative_maybe_derived_type = false;
3198 binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
3199 context.speculative_offset, otr_type);
3200 if (binfo)
3201 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3202 can_refer: &can_refer);
3203 else
3204 target = NULL;
3205
3206 /* In the case we get complete method, we don't need
3207 to walk derivations. */
3208 if (target && DECL_FINAL_P (target))
3209 context.speculative_maybe_derived_type = false;
3210 if (check_derived_types
3211 ? type_or_derived_type_possibly_instantiated_p
3212 (t: speculative_outer_type)
3213 : type_possibly_instantiated_p (t: speculative_outer_type->type))
3214 maybe_record_node (nodes, target, inserted: &inserted, can_refer,
3215 completep: &speculation_complete);
3216 if (binfo)
3217 matched_vtables.add (BINFO_VTABLE (binfo));
3218
3219
3220 /* Next walk recursively all derived types. */
3221 if (context.speculative_maybe_derived_type)
3222 for (i = 0; i < speculative_outer_type->derived_types.length(); i++)
3223 possible_polymorphic_call_targets_1 (nodes, inserted: &inserted,
3224 matched_vtables: &matched_vtables,
3225 otr_type,
3226 type: speculative_outer_type->derived_types[i],
3227 otr_token, outer_type: speculative_outer_type->type,
3228 offset: context.speculative_offset,
3229 completep: &speculation_complete,
3230 bases_to_consider,
3231 consider_construction: false);
3232 }
3233
3234 if (!speculative || !nodes.length ())
3235 {
3236 bool check_derived_types = false;
3237 /* First see virtual method of type itself. */
3238 binfo = get_binfo_at_offset (TYPE_BINFO (outer_type->type),
3239 context.offset, otr_type);
3240 if (binfo)
3241 target = gimple_get_virt_method_for_binfo (otr_token, binfo,
3242 can_refer: &can_refer);
3243 else
3244 {
3245 gcc_assert (odr_violation_reported);
3246 target = NULL;
3247 }
3248
3249 /* Destructors are never called through construction virtual tables,
3250 because the type is always known. */
3251 if (target && DECL_CXX_DESTRUCTOR_P (target))
3252 context.maybe_in_construction = false;
3253
3254 /* In the case we get complete method, we don't need
3255 to walk derivations. */
3256 if (target && DECL_FINAL_P (target))
3257 {
3258 check_derived_types = true;
3259 context.maybe_derived_type = false;
3260 }
3261
3262 /* If OUTER_TYPE is abstract, we know we are not seeing its instance. */
3263 if (check_derived_types
3264 ? type_or_derived_type_possibly_instantiated_p (t: outer_type)
3265 : type_possibly_instantiated_p (t: outer_type->type))
3266 maybe_record_node (nodes, target, inserted: &inserted, can_refer, completep: &complete);
3267 else
3268 skipped = true;
3269
3270 if (binfo)
3271 matched_vtables.add (BINFO_VTABLE (binfo));
3272
3273 /* Next walk recursively all derived types. */
3274 if (context.maybe_derived_type)
3275 {
3276 for (i = 0; i < outer_type->derived_types.length(); i++)
3277 possible_polymorphic_call_targets_1 (nodes, inserted: &inserted,
3278 matched_vtables: &matched_vtables,
3279 otr_type,
3280 type: outer_type->derived_types[i],
3281 otr_token, outer_type: outer_type->type,
3282 offset: context.offset, completep: &complete,
3283 bases_to_consider,
3284 consider_construction: context.maybe_in_construction);
3285
3286 if (!outer_type->all_derivations_known)
3287 {
3288 if (!speculative && final_warning_records
3289 && nodes.length () == 1
3290 && TREE_CODE (TREE_TYPE (nodes[0]->decl)) == METHOD_TYPE)
3291 {
3292 if (complete
3293 && warn_suggest_final_types
3294 && !outer_type->derived_types.length ())
3295 {
3296 final_warning_records->grow_type_warnings
3297 (newlen: outer_type->id);
3298 final_warning_records->type_warnings[outer_type->id].count++;
3299 if (!final_warning_records->type_warnings
3300 [outer_type->id].dyn_count.initialized_p ())
3301 final_warning_records->type_warnings
3302 [outer_type->id].dyn_count = profile_count::zero ();
3303 final_warning_records->type_warnings[outer_type->id].dyn_count
3304 += final_warning_records->dyn_count;
3305 final_warning_records->type_warnings[outer_type->id].type
3306 = outer_type->type;
3307 (*slot)->type_warning = outer_type->id + 1;
3308 }
3309 if (complete
3310 && warn_suggest_final_methods
3311 && types_same_for_odr (DECL_CONTEXT (nodes[0]->decl),
3312 type2: outer_type->type))
3313 {
3314 bool existed;
3315 struct decl_warn_count &c =
3316 final_warning_records->decl_warnings.get_or_insert
3317 (k: nodes[0]->decl, existed: &existed);
3318
3319 if (existed)
3320 {
3321 c.count++;
3322 c.dyn_count += final_warning_records->dyn_count;
3323 }
3324 else
3325 {
3326 c.count = 1;
3327 c.dyn_count = final_warning_records->dyn_count;
3328 c.decl = nodes[0]->decl;
3329 }
3330 (*slot)->decl_warning = nodes[0]->decl;
3331 }
3332 }
3333 complete = false;
3334 }
3335 }
3336
3337 if (!speculative)
3338 {
3339 /* Destructors are never called through construction virtual tables,
3340 because the type is always known. One of entries may be
3341 cxa_pure_virtual so look to at least two of them. */
3342 if (context.maybe_in_construction)
3343 for (i =0 ; i < MIN (nodes.length (), 2); i++)
3344 if (DECL_CXX_DESTRUCTOR_P (nodes[i]->decl))
3345 context.maybe_in_construction = false;
3346 if (context.maybe_in_construction)
3347 {
3348 if (type != outer_type
3349 && (!skipped
3350 || (context.maybe_derived_type
3351 && !type_all_derivations_known_p (t: outer_type->type))))
3352 record_targets_from_bases (otr_type, otr_token, outer_type: outer_type->type,
3353 offset: context.offset, nodes, inserted: &inserted,
3354 matched_vtables: &matched_vtables, completep: &complete);
3355 if (skipped)
3356 maybe_record_node (nodes, target, inserted: &inserted, can_refer, completep: &complete);
3357 for (i = 0; i < bases_to_consider.length(); i++)
3358 maybe_record_node (nodes, target: bases_to_consider[i], inserted: &inserted, can_refer, completep: &complete);
3359 }
3360 }
3361 }
3362
3363 (*slot)->targets = nodes;
3364 (*slot)->complete = complete;
3365 (*slot)->n_odr_types = odr_types.length ();
3366 if (completep)
3367 *completep = complete;
3368
3369 timevar_pop (tv: TV_IPA_VIRTUAL_CALL);
3370 return nodes;
3371}
3372
3373bool
3374add_decl_warning (const tree &key ATTRIBUTE_UNUSED, const decl_warn_count &value,
3375 vec<const decl_warn_count*> *vec)
3376{
3377 vec->safe_push (obj: &value);
3378 return true;
3379}
3380
3381/* Dump target list TARGETS into FILE. */
3382
3383static void
3384dump_targets (FILE *f, vec <cgraph_node *> targets, bool verbose)
3385{
3386 unsigned int i;
3387
3388 for (i = 0; i < targets.length (); i++)
3389 {
3390 char *name = NULL;
3391 if (in_lto_p)
3392 name = cplus_demangle_v3 (mangled: targets[i]->asm_name (), options: 0);
3393 fprintf (stream: f, format: " %s", name ? name : targets[i]->dump_name ());
3394 if (in_lto_p)
3395 free (ptr: name);
3396 if (!targets[i]->definition)
3397 fprintf (stream: f, format: " (no definition%s)",
3398 DECL_DECLARED_INLINE_P (targets[i]->decl)
3399 ? " inline" : "");
3400 /* With many targets for every call polymorphic dumps are going to
3401 be quadratic in size. */
3402 if (i > 10 && !verbose)
3403 {
3404 fprintf (stream: f, format: " ... and %i more targets\n", targets.length () - i);
3405 return;
3406 }
3407 }
3408 fprintf (stream: f, format: "\n");
3409}
3410
3411/* Dump all possible targets of a polymorphic call. */
3412
3413void
3414dump_possible_polymorphic_call_targets (FILE *f,
3415 tree otr_type,
3416 HOST_WIDE_INT otr_token,
3417 const ipa_polymorphic_call_context &ctx,
3418 bool verbose)
3419{
3420 vec <cgraph_node *> targets;
3421 bool final;
3422 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), insert: false);
3423 unsigned int len;
3424
3425 if (!type)
3426 return;
3427 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3428 context: ctx,
3429 completep: &final, NULL, speculative: false);
3430 fprintf (stream: f, format: " Targets of polymorphic call of type %i:", type->id);
3431 print_generic_expr (f, type->type, TDF_SLIM);
3432 fprintf (stream: f, format: " token %i\n", (int)otr_token);
3433
3434 ctx.dump (f);
3435
3436 fprintf (stream: f, format: " %s%s%s%s\n ",
3437 final ? "This is a complete list." :
3438 "This is partial list; extra targets may be defined in other units.",
3439 ctx.maybe_in_construction ? " (base types included)" : "",
3440 ctx.maybe_derived_type ? " (derived types included)" : "",
3441 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
3442 len = targets.length ();
3443 dump_targets (f, targets, verbose);
3444
3445 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3446 context: ctx,
3447 completep: &final, NULL, speculative: true);
3448 if (targets.length () != len)
3449 {
3450 fprintf (stream: f, format: " Speculative targets:");
3451 dump_targets (f, targets, verbose);
3452 }
3453 /* Ugly: during callgraph construction the target cache may get populated
3454 before all targets are found. While this is harmless (because all local
3455 types are discovered and only in those case we devirtualize fully and we
3456 don't do speculative devirtualization before IPA stage) it triggers
3457 assert here when dumping at that stage also populates the case with
3458 speculative targets. Quietly ignore this. */
3459 gcc_assert (symtab->state < IPA_SSA || targets.length () <= len);
3460 fprintf (stream: f, format: "\n");
3461}
3462
3463
3464/* Return true if N can be possibly target of a polymorphic call of
3465 OTR_TYPE/OTR_TOKEN. */
3466
3467bool
3468possible_polymorphic_call_target_p (tree otr_type,
3469 HOST_WIDE_INT otr_token,
3470 const ipa_polymorphic_call_context &ctx,
3471 struct cgraph_node *n)
3472{
3473 vec <cgraph_node *> targets;
3474 unsigned int i;
3475 bool final;
3476
3477 if (fndecl_built_in_p (node: n->decl, klass: BUILT_IN_NORMAL)
3478 && (DECL_FUNCTION_CODE (decl: n->decl) == BUILT_IN_UNREACHABLE
3479 || DECL_FUNCTION_CODE (decl: n->decl) == BUILT_IN_TRAP
3480 || DECL_FUNCTION_CODE (decl: n->decl) == BUILT_IN_UNREACHABLE_TRAP))
3481 return true;
3482
3483 if (is_cxa_pure_virtual_p (target: n->decl))
3484 return true;
3485
3486 if (!odr_hash)
3487 return true;
3488 targets = possible_polymorphic_call_targets (otr_type, otr_token, context: ctx, completep: &final);
3489 for (i = 0; i < targets.length (); i++)
3490 if (n->semantically_equivalent_p (target: targets[i]))
3491 return true;
3492
3493 /* At a moment we allow middle end to dig out new external declarations
3494 as a targets of polymorphic calls. */
3495 if (!final && !n->definition)
3496 return true;
3497 return false;
3498}
3499
3500
3501
3502/* Return true if N can be possibly target of a polymorphic call of
3503 OBJ_TYPE_REF expression REF in STMT. */
3504
3505bool
3506possible_polymorphic_call_target_p (tree ref,
3507 gimple *stmt,
3508 struct cgraph_node *n)
3509{
3510 ipa_polymorphic_call_context context (current_function_decl, ref, stmt);
3511 tree call_fn = gimple_call_fn (gs: stmt);
3512
3513 return possible_polymorphic_call_target_p (otr_type: obj_type_ref_class (ref: call_fn),
3514 otr_token: tree_to_uhwi
3515 (OBJ_TYPE_REF_TOKEN (call_fn)),
3516 ctx: context,
3517 n);
3518}
3519
3520
3521/* After callgraph construction new external nodes may appear.
3522 Add them into the graph. */
3523
3524void
3525update_type_inheritance_graph (void)
3526{
3527 struct cgraph_node *n;
3528
3529 if (!odr_hash)
3530 return;
3531 free_polymorphic_call_targets_hash ();
3532 timevar_push (tv: TV_IPA_INHERITANCE);
3533 /* We reconstruct the graph starting from types of all methods seen in the
3534 unit. */
3535 FOR_EACH_FUNCTION (n)
3536 if (DECL_VIRTUAL_P (n->decl)
3537 && !n->definition
3538 && n->real_symbol_p ())
3539 get_odr_type (TYPE_METHOD_BASETYPE (TREE_TYPE (n->decl)), insert: true);
3540 timevar_pop (tv: TV_IPA_INHERITANCE);
3541}
3542
3543
3544/* Return true if N looks like likely target of a polymorphic call.
3545 Rule out cxa_pure_virtual, noreturns, function declared cold and
3546 other obvious cases. */
3547
3548bool
3549likely_target_p (struct cgraph_node *n)
3550{
3551 int flags;
3552 /* cxa_pure_virtual and similar things are not likely. */
3553 if (TREE_CODE (TREE_TYPE (n->decl)) != METHOD_TYPE)
3554 return false;
3555 flags = flags_from_decl_or_type (n->decl);
3556 if (flags & ECF_NORETURN)
3557 return false;
3558 if (lookup_attribute (attr_name: "cold",
3559 DECL_ATTRIBUTES (n->decl)))
3560 return false;
3561 if (n->frequency < NODE_FREQUENCY_NORMAL)
3562 return false;
3563 /* If there are no live virtual tables referring the target,
3564 the only way the target can be called is an instance coming from other
3565 compilation unit; speculative devirtualization is built around an
3566 assumption that won't happen. */
3567 if (!referenced_from_vtable_p (node: n))
3568 return false;
3569 return true;
3570}
3571
3572/* Compare type warning records P1 and P2 and choose one with larger count;
3573 helper for qsort. */
3574
3575static int
3576type_warning_cmp (const void *p1, const void *p2)
3577{
3578 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
3579 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
3580
3581 if (t1->dyn_count < t2->dyn_count)
3582 return 1;
3583 if (t1->dyn_count > t2->dyn_count)
3584 return -1;
3585 return t2->count - t1->count;
3586}
3587
3588/* Compare decl warning records P1 and P2 and choose one with larger count;
3589 helper for qsort. */
3590
3591static int
3592decl_warning_cmp (const void *p1, const void *p2)
3593{
3594 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
3595 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
3596
3597 if (t1->dyn_count < t2->dyn_count)
3598 return 1;
3599 if (t1->dyn_count > t2->dyn_count)
3600 return -1;
3601 return t2->count - t1->count;
3602}
3603
3604
3605/* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
3606 context CTX. */
3607
3608struct cgraph_node *
3609try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
3610 ipa_polymorphic_call_context ctx)
3611{
3612 vec <cgraph_node *>targets
3613 = possible_polymorphic_call_targets
3614 (otr_type, otr_token, context: ctx, NULL, NULL, speculative: true);
3615 unsigned int i;
3616 struct cgraph_node *likely_target = NULL;
3617
3618 for (i = 0; i < targets.length (); i++)
3619 if (likely_target_p (n: targets[i]))
3620 {
3621 if (likely_target)
3622 return NULL;
3623 likely_target = targets[i];
3624 }
3625 if (!likely_target
3626 ||!likely_target->definition
3627 || DECL_EXTERNAL (likely_target->decl))
3628 return NULL;
3629
3630 /* Don't use an implicitly-declared destructor (c++/58678). */
3631 struct cgraph_node *non_thunk_target
3632 = likely_target->function_symbol ();
3633 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3634 return NULL;
3635 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3636 && likely_target->can_be_discarded_p ())
3637 return NULL;
3638 return likely_target;
3639}
3640
3641/* The ipa-devirt pass.
3642 When polymorphic call has only one likely target in the unit,
3643 turn it into a speculative call. */
3644
3645static unsigned int
3646ipa_devirt (void)
3647{
3648 struct cgraph_node *n;
3649 hash_set<void *> bad_call_targets;
3650 struct cgraph_edge *e;
3651
3652 int npolymorphic = 0, nspeculated = 0, nconverted = 0, ncold = 0;
3653 int nmultiple = 0, noverwritable = 0, ndevirtualized = 0, nnotdefined = 0;
3654 int nwrong = 0, nok = 0, nexternal = 0, nartificial = 0;
3655 int ndropped = 0;
3656
3657 if (!odr_types_ptr)
3658 return 0;
3659
3660 if (dump_file)
3661 dump_type_inheritance_graph (f: dump_file);
3662
3663 /* We can output -Wsuggest-final-methods and -Wsuggest-final-types warnings.
3664 This is implemented by setting up final_warning_records that are updated
3665 by get_polymorphic_call_targets.
3666 We need to clear cache in this case to trigger recomputation of all
3667 entries. */
3668 if (warn_suggest_final_methods || warn_suggest_final_types)
3669 {
3670 final_warning_records = new (final_warning_record);
3671 final_warning_records->dyn_count = profile_count::zero ();
3672 final_warning_records->grow_type_warnings (odr_types.length ());
3673 free_polymorphic_call_targets_hash ();
3674 }
3675
3676 FOR_EACH_DEFINED_FUNCTION (n)
3677 {
3678 bool update = false;
3679 if (!opt_for_fn (n->decl, flag_devirtualize))
3680 continue;
3681 if (dump_file && n->indirect_calls)
3682 fprintf (stream: dump_file, format: "\n\nProcesing function %s\n",
3683 n->dump_name ());
3684 for (e = n->indirect_calls; e; e = e->next_callee)
3685 if (e->indirect_info->polymorphic)
3686 {
3687 struct cgraph_node *likely_target = NULL;
3688 void *cache_token;
3689 bool final;
3690
3691 if (final_warning_records)
3692 final_warning_records->dyn_count = e->count.ipa ();
3693
3694 vec <cgraph_node *>targets
3695 = possible_polymorphic_call_targets
3696 (e, completep: &final, cache_token: &cache_token, speculative: true);
3697 unsigned int i;
3698
3699 /* Trigger warnings by calculating non-speculative targets. */
3700 if (warn_suggest_final_methods || warn_suggest_final_types)
3701 possible_polymorphic_call_targets (e);
3702
3703 if (dump_file)
3704 dump_possible_polymorphic_call_targets
3705 (f: dump_file, e, verbose: (dump_flags & TDF_DETAILS));
3706
3707 npolymorphic++;
3708
3709 /* See if the call can be devirtualized by means of ipa-prop's
3710 polymorphic call context propagation. If not, we can just
3711 forget about this call being polymorphic and avoid some heavy
3712 lifting in remove_unreachable_nodes that will otherwise try to
3713 keep all possible targets alive until inlining and in the inliner
3714 itself.
3715
3716 This may need to be revisited once we add further ways to use
3717 the may edges, but it is a reasonable thing to do right now. */
3718
3719 if ((e->indirect_info->param_index == -1
3720 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
3721 && e->indirect_info->vptr_changed))
3722 && !flag_ltrans_devirtualize)
3723 {
3724 e->indirect_info->polymorphic = false;
3725 ndropped++;
3726 if (dump_file)
3727 fprintf (stream: dump_file, format: "Dropping polymorphic call info;"
3728 " it cannot be used by ipa-prop\n");
3729 }
3730
3731 if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
3732 continue;
3733
3734 if (!e->maybe_hot_p ())
3735 {
3736 if (dump_file)
3737 fprintf (stream: dump_file, format: "Call is cold\n\n");
3738 ncold++;
3739 continue;
3740 }
3741 if (e->speculative)
3742 {
3743 if (dump_file)
3744 fprintf (stream: dump_file, format: "Call is already speculated\n\n");
3745 nspeculated++;
3746
3747 /* When dumping see if we agree with speculation. */
3748 if (!dump_file)
3749 continue;
3750 }
3751 if (bad_call_targets.contains (k: cache_token))
3752 {
3753 if (dump_file)
3754 fprintf (stream: dump_file, format: "Target list is known to be useless\n\n");
3755 nmultiple++;
3756 continue;
3757 }
3758 for (i = 0; i < targets.length (); i++)
3759 if (likely_target_p (n: targets[i]))
3760 {
3761 if (likely_target)
3762 {
3763 likely_target = NULL;
3764 if (dump_file)
3765 fprintf (stream: dump_file, format: "More than one likely target\n\n");
3766 nmultiple++;
3767 break;
3768 }
3769 likely_target = targets[i];
3770 }
3771 if (!likely_target)
3772 {
3773 bad_call_targets.add (k: cache_token);
3774 continue;
3775 }
3776 /* This is reached only when dumping; check if we agree or disagree
3777 with the speculation. */
3778 if (e->speculative)
3779 {
3780 bool found = e->speculative_call_for_target (likely_target);
3781 if (found)
3782 {
3783 fprintf (stream: dump_file, format: "We agree with speculation\n\n");
3784 nok++;
3785 }
3786 else
3787 {
3788 fprintf (stream: dump_file, format: "We disagree with speculation\n\n");
3789 nwrong++;
3790 }
3791 continue;
3792 }
3793 if (!likely_target->definition)
3794 {
3795 if (dump_file)
3796 fprintf (stream: dump_file, format: "Target is not a definition\n\n");
3797 nnotdefined++;
3798 continue;
3799 }
3800 /* Do not introduce new references to external symbols. While we
3801 can handle these just well, it is common for programs to
3802 incorrectly with headers defining methods they are linked
3803 with. */
3804 if (DECL_EXTERNAL (likely_target->decl))
3805 {
3806 if (dump_file)
3807 fprintf (stream: dump_file, format: "Target is external\n\n");
3808 nexternal++;
3809 continue;
3810 }
3811 /* Don't use an implicitly-declared destructor (c++/58678). */
3812 struct cgraph_node *non_thunk_target
3813 = likely_target->function_symbol ();
3814 if (DECL_ARTIFICIAL (non_thunk_target->decl))
3815 {
3816 if (dump_file)
3817 fprintf (stream: dump_file, format: "Target is artificial\n\n");
3818 nartificial++;
3819 continue;
3820 }
3821 if (likely_target->get_availability () <= AVAIL_INTERPOSABLE
3822 && likely_target->can_be_discarded_p ())
3823 {
3824 if (dump_file)
3825 fprintf (stream: dump_file, format: "Target is overwritable\n\n");
3826 noverwritable++;
3827 continue;
3828 }
3829 else if (dbg_cnt (index: devirt))
3830 {
3831 if (dump_enabled_p ())
3832 {
3833 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, e->call_stmt,
3834 "speculatively devirtualizing call "
3835 "in %s to %s\n",
3836 n->dump_name (),
3837 likely_target->dump_name ());
3838 }
3839 if (!likely_target->can_be_discarded_p ())
3840 {
3841 cgraph_node *alias;
3842 alias = dyn_cast<cgraph_node *> (p: likely_target->noninterposable_alias ());
3843 if (alias)
3844 likely_target = alias;
3845 }
3846 nconverted++;
3847 update = true;
3848 e->make_speculative
3849 (n2: likely_target, direct_count: e->count.apply_scale (num: 8, den: 10));
3850 }
3851 }
3852 if (update)
3853 ipa_update_overall_fn_summary (node: n);
3854 }
3855 if (warn_suggest_final_methods || warn_suggest_final_types)
3856 {
3857 if (warn_suggest_final_types)
3858 {
3859 final_warning_records->type_warnings.qsort (type_warning_cmp);
3860 for (unsigned int i = 0;
3861 i < final_warning_records->type_warnings.length (); i++)
3862 if (final_warning_records->type_warnings[i].count)
3863 {
3864 tree type = final_warning_records->type_warnings[i].type;
3865 int count = final_warning_records->type_warnings[i].count;
3866 profile_count dyn_count
3867 = final_warning_records->type_warnings[i].dyn_count;
3868
3869 if (!(dyn_count > 0))
3870 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3871 OPT_Wsuggest_final_types, count,
3872 "Declaring type %qD final "
3873 "would enable devirtualization of %i call",
3874 "Declaring type %qD final "
3875 "would enable devirtualization of %i calls",
3876 type,
3877 count);
3878 else
3879 warning_n (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
3880 OPT_Wsuggest_final_types, count,
3881 "Declaring type %qD final "
3882 "would enable devirtualization of %i call "
3883 "executed %lli times",
3884 "Declaring type %qD final "
3885 "would enable devirtualization of %i calls "
3886 "executed %lli times",
3887 type,
3888 count,
3889 (long long) dyn_count.to_gcov_type ());
3890 }
3891 }
3892
3893 if (warn_suggest_final_methods)
3894 {
3895 auto_vec<const decl_warn_count*> decl_warnings_vec;
3896
3897 final_warning_records->decl_warnings.traverse
3898 <vec<const decl_warn_count *> *, add_decl_warning> (a: &decl_warnings_vec);
3899 decl_warnings_vec.qsort (decl_warning_cmp);
3900 for (unsigned int i = 0; i < decl_warnings_vec.length (); i++)
3901 {
3902 tree decl = decl_warnings_vec[i]->decl;
3903 int count = decl_warnings_vec[i]->count;
3904 profile_count dyn_count
3905 = decl_warnings_vec[i]->dyn_count;
3906
3907 if (!(dyn_count > 0))
3908 if (DECL_CXX_DESTRUCTOR_P (decl))
3909 warning_n (DECL_SOURCE_LOCATION (decl),
3910 OPT_Wsuggest_final_methods, count,
3911 "Declaring virtual destructor of %qD final "
3912 "would enable devirtualization of %i call",
3913 "Declaring virtual destructor of %qD final "
3914 "would enable devirtualization of %i calls",
3915 DECL_CONTEXT (decl), count);
3916 else
3917 warning_n (DECL_SOURCE_LOCATION (decl),
3918 OPT_Wsuggest_final_methods, count,
3919 "Declaring method %qD final "
3920 "would enable devirtualization of %i call",
3921 "Declaring method %qD final "
3922 "would enable devirtualization of %i calls",
3923 decl, count);
3924 else if (DECL_CXX_DESTRUCTOR_P (decl))
3925 warning_n (DECL_SOURCE_LOCATION (decl),
3926 OPT_Wsuggest_final_methods, count,
3927 "Declaring virtual destructor of %qD final "
3928 "would enable devirtualization of %i call "
3929 "executed %lli times",
3930 "Declaring virtual destructor of %qD final "
3931 "would enable devirtualization of %i calls "
3932 "executed %lli times",
3933 DECL_CONTEXT (decl), count,
3934 (long long)dyn_count.to_gcov_type ());
3935 else
3936 warning_n (DECL_SOURCE_LOCATION (decl),
3937 OPT_Wsuggest_final_methods, count,
3938 "Declaring method %qD final "
3939 "would enable devirtualization of %i call "
3940 "executed %lli times",
3941 "Declaring method %qD final "
3942 "would enable devirtualization of %i calls "
3943 "executed %lli times",
3944 decl, count,
3945 (long long)dyn_count.to_gcov_type ());
3946 }
3947 }
3948
3949 delete (final_warning_records);
3950 final_warning_records = 0;
3951 }
3952
3953 if (dump_file)
3954 fprintf (stream: dump_file,
3955 format: "%i polymorphic calls, %i devirtualized,"
3956 " %i speculatively devirtualized, %i cold\n"
3957 "%i have multiple targets, %i overwritable,"
3958 " %i already speculated (%i agree, %i disagree),"
3959 " %i external, %i not defined, %i artificial, %i infos dropped\n",
3960 npolymorphic, ndevirtualized, nconverted, ncold,
3961 nmultiple, noverwritable, nspeculated, nok, nwrong,
3962 nexternal, nnotdefined, nartificial, ndropped);
3963 return ndevirtualized || ndropped ? TODO_remove_functions : 0;
3964}
3965
3966namespace {
3967
3968const pass_data pass_data_ipa_devirt =
3969{
3970 .type: IPA_PASS, /* type */
3971 .name: "devirt", /* name */
3972 .optinfo_flags: OPTGROUP_NONE, /* optinfo_flags */
3973 .tv_id: TV_IPA_DEVIRT, /* tv_id */
3974 .properties_required: 0, /* properties_required */
3975 .properties_provided: 0, /* properties_provided */
3976 .properties_destroyed: 0, /* properties_destroyed */
3977 .todo_flags_start: 0, /* todo_flags_start */
3978 .todo_flags_finish: ( TODO_dump_symtab ), /* todo_flags_finish */
3979};
3980
3981class pass_ipa_devirt : public ipa_opt_pass_d
3982{
3983public:
3984 pass_ipa_devirt (gcc::context *ctxt)
3985 : ipa_opt_pass_d (pass_data_ipa_devirt, ctxt,
3986 NULL, /* generate_summary */
3987 NULL, /* write_summary */
3988 NULL, /* read_summary */
3989 NULL, /* write_optimization_summary */
3990 NULL, /* read_optimization_summary */
3991 NULL, /* stmt_fixup */
3992 0, /* function_transform_todo_flags_start */
3993 NULL, /* function_transform */
3994 NULL) /* variable_transform */
3995 {}
3996
3997 /* opt_pass methods: */
3998 bool gate (function *) final override
3999 {
4000 /* In LTO, always run the IPA passes and decide on function basis if the
4001 pass is enabled. */
4002 if (in_lto_p)
4003 return true;
4004 return (flag_devirtualize
4005 && (flag_devirtualize_speculatively
4006 || (warn_suggest_final_methods
4007 || warn_suggest_final_types))
4008 && optimize);
4009 }
4010
4011 unsigned int execute (function *) final override { return ipa_devirt (); }
4012
4013}; // class pass_ipa_devirt
4014
4015} // anon namespace
4016
4017ipa_opt_pass_d *
4018make_pass_ipa_devirt (gcc::context *ctxt)
4019{
4020 return new pass_ipa_devirt (ctxt);
4021}
4022
4023/* Print ODR name of a TYPE if available.
4024 Use demangler when option DEMANGLE is used. */
4025
4026DEBUG_FUNCTION void
4027debug_tree_odr_name (tree type, bool demangle)
4028{
4029 const char *odr = get_odr_name_for_type (type);
4030 if (demangle)
4031 {
4032 const int opts = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
4033 odr = cplus_demangle (mangled: odr, options: opts);
4034 }
4035
4036 fprintf (stderr, format: "%s\n", odr);
4037}
4038
4039/* Register ODR enum so we later stream record about its values. */
4040
4041void
4042register_odr_enum (tree t)
4043{
4044 if (flag_lto)
4045 vec_safe_push (v&: odr_enums, obj: t);
4046}
4047
4048/* Write ODR enums to LTO stream file. */
4049
4050static void
4051ipa_odr_summary_write (void)
4052{
4053 if (!odr_enums && !odr_enum_map)
4054 return;
4055 struct output_block *ob = create_output_block (LTO_section_odr_types);
4056 unsigned int i;
4057 tree t;
4058
4059 if (odr_enums)
4060 {
4061 streamer_write_uhwi (ob, odr_enums->length ());
4062
4063 /* For every ODR enum stream out
4064 - its ODR name
4065 - number of values,
4066 - value names and constant their represent
4067 - bitpack of locations so we can do good diagnostics. */
4068 FOR_EACH_VEC_ELT (*odr_enums, i, t)
4069 {
4070 streamer_write_string (ob, ob->main_stream,
4071 IDENTIFIER_POINTER
4072 (DECL_ASSEMBLER_NAME (TYPE_NAME (t))),
4073 true);
4074
4075 int n = 0;
4076 for (tree e = TYPE_VALUES (t); e; e = TREE_CHAIN (e))
4077 n++;
4078 streamer_write_uhwi (ob, n);
4079 for (tree e = TYPE_VALUES (t); e; e = TREE_CHAIN (e))
4080 {
4081 streamer_write_string (ob, ob->main_stream,
4082 IDENTIFIER_POINTER (TREE_PURPOSE (e)),
4083 true);
4084 streamer_write_wide_int (ob,
4085 wi::to_wide (DECL_INITIAL
4086 (TREE_VALUE (e))));
4087 }
4088
4089 bitpack_d bp = bitpack_create (s: ob->main_stream);
4090 lto_output_location (ob, &bp, DECL_SOURCE_LOCATION (TYPE_NAME (t)));
4091 for (tree e = TYPE_VALUES (t); e; e = TREE_CHAIN (e))
4092 lto_output_location (ob, &bp,
4093 DECL_SOURCE_LOCATION (TREE_VALUE (e)));
4094 streamer_write_bitpack (bp: &bp);
4095 }
4096 vec_free (v&: odr_enums);
4097 odr_enums = NULL;
4098 }
4099 /* During LTO incremental linking we already have streamed in types. */
4100 else if (odr_enum_map)
4101 {
4102 gcc_checking_assert (!odr_enums);
4103 streamer_write_uhwi (ob, odr_enum_map->elements ());
4104
4105 hash_map<nofree_string_hash, odr_enum>::iterator iter
4106 = odr_enum_map->begin ();
4107 for (; iter != odr_enum_map->end (); ++iter)
4108 {
4109 odr_enum &this_enum = (*iter).second;
4110 streamer_write_string (ob, ob->main_stream, (*iter).first, true);
4111
4112 streamer_write_uhwi (ob, this_enum.vals.length ());
4113 for (unsigned j = 0; j < this_enum.vals.length (); j++)
4114 {
4115 streamer_write_string (ob, ob->main_stream,
4116 this_enum.vals[j].name, true);
4117 streamer_write_wide_int (ob, this_enum.vals[j].val);
4118 }
4119
4120 bitpack_d bp = bitpack_create (s: ob->main_stream);
4121 lto_output_location (ob, &bp, this_enum.locus);
4122 for (unsigned j = 0; j < this_enum.vals.length (); j++)
4123 lto_output_location (ob, &bp, this_enum.vals[j].locus);
4124 streamer_write_bitpack (bp: &bp);
4125 }
4126
4127 delete odr_enum_map;
4128 obstack_free (&odr_enum_obstack, NULL);
4129 odr_enum_map = NULL;
4130 }
4131
4132 produce_asm (ob, NULL);
4133 destroy_output_block (ob);
4134}
4135
4136/* Write ODR enums from LTO stream file and warn on mismatches. */
4137
4138static void
4139ipa_odr_read_section (struct lto_file_decl_data *file_data, const char *data,
4140 size_t len)
4141{
4142 const struct lto_function_header *header
4143 = (const struct lto_function_header *) data;
4144 const int cfg_offset = sizeof (struct lto_function_header);
4145 const int main_offset = cfg_offset + header->cfg_size;
4146 const int string_offset = main_offset + header->main_size;
4147 class data_in *data_in;
4148
4149 lto_input_block ib ((const char *) data + main_offset, header->main_size,
4150 file_data);
4151
4152 data_in
4153 = lto_data_in_create (file_data, (const char *) data + string_offset,
4154 header->string_size, vNULL);
4155 unsigned int n = streamer_read_uhwi (&ib);
4156
4157 if (!odr_enum_map)
4158 {
4159 gcc_obstack_init (&odr_enum_obstack);
4160 odr_enum_map = new (hash_map <nofree_string_hash, odr_enum>);
4161 }
4162
4163 for (unsigned i = 0; i < n; i++)
4164 {
4165 const char *rname = streamer_read_string (data_in, &ib);
4166 unsigned int nvals = streamer_read_uhwi (&ib);
4167 char *name;
4168
4169 obstack_grow (&odr_enum_obstack, rname, strlen (rname) + 1);
4170 name = XOBFINISH (&odr_enum_obstack, char *);
4171
4172 bool existed_p;
4173 class odr_enum &this_enum
4174 = odr_enum_map->get_or_insert (k: xstrdup (name), existed: &existed_p);
4175
4176 /* If this is first time we see the enum, remember its definition. */
4177 if (!existed_p)
4178 {
4179 this_enum.vals.safe_grow_cleared (len: nvals, exact: true);
4180 this_enum.warned = false;
4181 if (dump_file)
4182 fprintf (stream: dump_file, format: "enum %s\n{\n", name);
4183 for (unsigned j = 0; j < nvals; j++)
4184 {
4185 const char *val_name = streamer_read_string (data_in, &ib);
4186 obstack_grow (&odr_enum_obstack, val_name, strlen (val_name) + 1);
4187 this_enum.vals[j].name = XOBFINISH (&odr_enum_obstack, char *);
4188 this_enum.vals[j].val = streamer_read_wide_int (&ib);
4189 if (dump_file)
4190 fprintf (stream: dump_file, format: " %s = " HOST_WIDE_INT_PRINT_DEC ",\n",
4191 val_name, wi::fits_shwi_p (x: this_enum.vals[j].val)
4192 ? this_enum.vals[j].val.to_shwi () : -1);
4193 }
4194 bitpack_d bp = streamer_read_bitpack (ib: &ib);
4195 stream_input_location (&this_enum.locus, &bp, data_in);
4196 for (unsigned j = 0; j < nvals; j++)
4197 stream_input_location (&this_enum.vals[j].locus, &bp, data_in);
4198 data_in->location_cache.apply_location_cache ();
4199 if (dump_file)
4200 fprintf (stream: dump_file, format: "}\n");
4201 }
4202 /* If we already have definition, compare it with new one and output
4203 warnings if they differs. */
4204 else
4205 {
4206 int do_warning = -1;
4207 char *warn_name = NULL;
4208 wide_int warn_value = wi::zero (precision: 1);
4209
4210 if (dump_file)
4211 fprintf (stream: dump_file, format: "Comparing enum %s\n", name);
4212
4213 /* Look for differences which we will warn about later once locations
4214 are streamed. */
4215 for (unsigned j = 0; j < nvals; j++)
4216 {
4217 const char *id = streamer_read_string (data_in, &ib);
4218 wide_int val = streamer_read_wide_int (&ib);
4219
4220 if (do_warning != -1 || j >= this_enum.vals.length ())
4221 continue;
4222 if (strcmp (s1: id, s2: this_enum.vals[j].name)
4223 || (val.get_precision() !=
4224 this_enum.vals[j].val.get_precision())
4225 || val != this_enum.vals[j].val)
4226 {
4227 warn_name = xstrdup (id);
4228 warn_value = val;
4229 do_warning = j;
4230 if (dump_file)
4231 fprintf (stream: dump_file, format: " Different on entry %i\n", j);
4232 }
4233 }
4234
4235 /* Stream in locations, but do not apply them unless we are going
4236 to warn. */
4237 bitpack_d bp = streamer_read_bitpack (ib: &ib);
4238 location_t locus;
4239
4240 stream_input_location (&locus, &bp, data_in);
4241
4242 /* Did we find a difference? */
4243 if (do_warning != -1 || nvals != this_enum.vals.length ())
4244 {
4245 data_in->location_cache.apply_location_cache ();
4246
4247 const int opts = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
4248 char *dmgname = cplus_demangle (mangled: name, options: opts);
4249 if (this_enum.warned
4250 || !warning_at (this_enum.locus,
4251 OPT_Wodr, "type %qs violates the "
4252 "C++ One Definition Rule",
4253 dmgname))
4254 do_warning = -1;
4255 else
4256 {
4257 this_enum.warned = true;
4258 if (do_warning == -1)
4259 inform (locus,
4260 "an enum with different number of values is defined"
4261 " in another translation unit");
4262 else if (warn_name)
4263 inform (locus,
4264 "an enum with different value name"
4265 " is defined in another translation unit");
4266 else
4267 inform (locus,
4268 "an enum with different values"
4269 " is defined in another translation unit");
4270 }
4271 }
4272 else
4273 data_in->location_cache.revert_location_cache ();
4274
4275 /* Finally look up for location of the actual value that diverged. */
4276 for (unsigned j = 0; j < nvals; j++)
4277 {
4278 location_t id_locus;
4279
4280 data_in->location_cache.revert_location_cache ();
4281 stream_input_location (&id_locus, &bp, data_in);
4282
4283 if ((int) j == do_warning)
4284 {
4285 data_in->location_cache.apply_location_cache ();
4286
4287 if (strcmp (s1: warn_name, s2: this_enum.vals[j].name))
4288 inform (this_enum.vals[j].locus,
4289 "name %qs differs from name %qs defined"
4290 " in another translation unit",
4291 this_enum.vals[j].name, warn_name);
4292 else if (this_enum.vals[j].val.get_precision() !=
4293 warn_value.get_precision())
4294 inform (this_enum.vals[j].locus,
4295 "name %qs is defined as %u-bit while another "
4296 "translation unit defines it as %u-bit",
4297 warn_name, this_enum.vals[j].val.get_precision(),
4298 warn_value.get_precision());
4299 /* FIXME: In case there is easy way to print wide_ints,
4300 perhaps we could do it here instead of overflow check. */
4301 else if (wi::fits_shwi_p (x: this_enum.vals[j].val)
4302 && wi::fits_shwi_p (x: warn_value))
4303 inform (this_enum.vals[j].locus,
4304 "name %qs is defined to %wd while another "
4305 "translation unit defines it as %wd",
4306 warn_name, this_enum.vals[j].val.to_shwi (),
4307 warn_value.to_shwi ());
4308 else
4309 inform (this_enum.vals[j].locus,
4310 "name %qs is defined to different value "
4311 "in another translation unit",
4312 warn_name);
4313
4314 inform (id_locus,
4315 "mismatching definition");
4316 }
4317 else
4318 data_in->location_cache.revert_location_cache ();
4319 }
4320 if (warn_name)
4321 free (ptr: warn_name);
4322 obstack_free (&odr_enum_obstack, name);
4323 }
4324 }
4325 lto_free_section_data (file_data, LTO_section_ipa_fn_summary, NULL, data,
4326 len);
4327 lto_data_in_delete (data_in);
4328}
4329
4330/* Read all ODR type sections. */
4331
4332static void
4333ipa_odr_summary_read (void)
4334{
4335 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
4336 struct lto_file_decl_data *file_data;
4337 unsigned int j = 0;
4338
4339 while ((file_data = file_data_vec[j++]))
4340 {
4341 size_t len;
4342 const char *data
4343 = lto_get_summary_section_data (file_data, LTO_section_odr_types,
4344 &len);
4345 if (data)
4346 ipa_odr_read_section (file_data, data, len);
4347 }
4348 /* Enum info is used only to produce warnings. Only case we will need it
4349 again is streaming for incremental LTO. */
4350 if (flag_incremental_link != INCREMENTAL_LINK_LTO)
4351 {
4352 delete odr_enum_map;
4353 obstack_free (&odr_enum_obstack, NULL);
4354 odr_enum_map = NULL;
4355 }
4356}
4357
4358namespace {
4359
4360const pass_data pass_data_ipa_odr =
4361{
4362 .type: IPA_PASS, /* type */
4363 .name: "odr", /* name */
4364 .optinfo_flags: OPTGROUP_NONE, /* optinfo_flags */
4365 .tv_id: TV_IPA_ODR, /* tv_id */
4366 .properties_required: 0, /* properties_required */
4367 .properties_provided: 0, /* properties_provided */
4368 .properties_destroyed: 0, /* properties_destroyed */
4369 .todo_flags_start: 0, /* todo_flags_start */
4370 .todo_flags_finish: 0, /* todo_flags_finish */
4371};
4372
4373class pass_ipa_odr : public ipa_opt_pass_d
4374{
4375public:
4376 pass_ipa_odr (gcc::context *ctxt)
4377 : ipa_opt_pass_d (pass_data_ipa_odr, ctxt,
4378 NULL, /* generate_summary */
4379 ipa_odr_summary_write, /* write_summary */
4380 ipa_odr_summary_read, /* read_summary */
4381 NULL, /* write_optimization_summary */
4382 NULL, /* read_optimization_summary */
4383 NULL, /* stmt_fixup */
4384 0, /* function_transform_todo_flags_start */
4385 NULL, /* function_transform */
4386 NULL) /* variable_transform */
4387 {}
4388
4389 /* opt_pass methods: */
4390 bool gate (function *) final override
4391 {
4392 return (in_lto_p || flag_lto);
4393 }
4394
4395 unsigned int execute (function *) final override
4396 {
4397 return 0;
4398 }
4399
4400}; // class pass_ipa_odr
4401
4402} // anon namespace
4403
4404ipa_opt_pass_d *
4405make_pass_ipa_odr (gcc::context *ctxt)
4406{
4407 return new pass_ipa_odr (ctxt);
4408}
4409
4410
4411#include "gt-ipa-devirt.h"
4412

source code of gcc/ipa-devirt.cc