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