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