Debian Codegraph @

gcc/ipa-devirt.cc

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