comparison gcc/ipa-devirt.c @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
comparison
equal deleted inserted replaced
131:84e7813d76e9 145:1830386684a0
1 /* Basic IPA utilities for type inheritance graph construction and 1 /* Basic IPA utilities for type inheritance graph construction and
2 devirtualization. 2 devirtualization.
3 Copyright (C) 2013-2018 Free Software Foundation, Inc. 3 Copyright (C) 2013-2020 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka 4 Contributed by Jan Hubicka
5 5
6 This file is part of GCC. 6 This file is part of GCC.
7 7
8 GCC is free software; you can redistribute it and/or modify it under 8 GCC is free software; you can redistribute it and/or modify it under
148 static hashval_t 148 static hashval_t
149 hash (type_pair p) 149 hash (type_pair p)
150 { 150 {
151 return TYPE_UID (p.first) ^ TYPE_UID (p.second); 151 return TYPE_UID (p.first) ^ TYPE_UID (p.second);
152 } 152 }
153 static const bool empty_zero_p = true;
153 static bool 154 static bool
154 is_empty (type_pair p) 155 is_empty (type_pair p)
155 { 156 {
156 return p.first == NULL; 157 return p.first == NULL;
157 } 158 }
170 { 171 {
171 e.first = NULL; 172 e.first = NULL;
172 } 173 }
173 }; 174 };
174 175
176 /* HACK alert: this is used to communicate with ipa-inline-transform that
177 thunk is being expanded and there is no need to clear the polymorphic
178 call target cache. */
179 bool thunk_expansion;
180
175 static bool odr_types_equivalent_p (tree, tree, bool, bool *, 181 static bool odr_types_equivalent_p (tree, tree, bool, bool *,
176 hash_set<type_pair> *, 182 hash_set<type_pair> *,
177 location_t, location_t); 183 location_t, location_t);
184 static void warn_odr (tree t1, tree t2, tree st1, tree st2,
185 bool warn, bool *warned, const char *reason);
178 186
179 static bool odr_violation_reported = false; 187 static bool odr_violation_reported = false;
180 188
181 189
182 /* Pointer set of all call targets appearing in the cache. */ 190 /* Pointer set of all call targets appearing in the cache. */
207 bool anonymous_namespace; 215 bool anonymous_namespace;
208 /* Do we know about all derivations of given type? */ 216 /* Do we know about all derivations of given type? */
209 bool all_derivations_known; 217 bool all_derivations_known;
210 /* Did we report ODR violation here? */ 218 /* Did we report ODR violation here? */
211 bool odr_violated; 219 bool odr_violated;
212 /* Set when virtual table without RTTI previaled table with. */ 220 /* Set when virtual table without RTTI prevailed table with. */
213 bool rtti_broken; 221 bool rtti_broken;
222 /* Set when the canonical type is determined using the type name. */
223 bool tbaa_enabled;
214 }; 224 };
215 225
216 /* Return TRUE if all derived types of T are known and thus 226 /* Return TRUE if all derived types of T are known and thus
217 we may consider the walk of derived type complete. 227 we may consider the walk of derived type complete.
218 228
240 static bool 250 static bool
241 type_all_ctors_visible_p (tree t) 251 type_all_ctors_visible_p (tree t)
242 { 252 {
243 return !flag_ltrans 253 return !flag_ltrans
244 && symtab->state >= CONSTRUCTION 254 && symtab->state >= CONSTRUCTION
245 /* We can not always use type_all_derivations_known_p. 255 /* We cannot always use type_all_derivations_known_p.
246 For function local types we must assume case where 256 For function local types we must assume case where
247 the function is COMDAT and shared in between units. 257 the function is COMDAT and shared in between units.
248 258
249 TODO: These cases are quite easy to get, but we need 259 TODO: These cases are quite easy to get, but we need
250 to keep track of C++ privatizing via -Wno-weak 260 to keep track of C++ privatizing via -Wno-weak
280 static inline hashval_t hash (const odr_type_d *); 290 static inline hashval_t hash (const odr_type_d *);
281 static inline bool equal (const odr_type_d *, const tree_node *); 291 static inline bool equal (const odr_type_d *, const tree_node *);
282 static inline void remove (odr_type_d *); 292 static inline void remove (odr_type_d *);
283 }; 293 };
284 294
285 /* Has used to unify ODR types based on their associated virtual table.
286 This hash is needed to keep -fno-lto-odr-type-merging to work and contains
287 only polymorphic types. Types with mangled names are inserted to both. */
288
289 struct odr_vtable_hasher:odr_name_hasher
290 {
291 static inline hashval_t hash (const odr_type_d *);
292 static inline bool equal (const odr_type_d *, const tree_node *);
293 };
294
295 /* Return type that was declared with T's name so that T is an
296 qualified variant of it. */
297
298 static inline tree
299 main_odr_variant (const_tree t)
300 {
301 if (TYPE_NAME (t) && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
302 return TREE_TYPE (TYPE_NAME (t));
303 /* Unnamed types and non-C++ produced types can be compared by variants. */
304 else
305 return TYPE_MAIN_VARIANT (t);
306 }
307
308 static bool 295 static bool
309 can_be_name_hashed_p (tree t) 296 can_be_name_hashed_p (tree t)
310 { 297 {
311 return (!in_lto_p || odr_type_p (t)); 298 return (!in_lto_p || odr_type_p (t));
312 } 299 }
314 /* Hash type by its ODR name. */ 301 /* Hash type by its ODR name. */
315 302
316 static hashval_t 303 static hashval_t
317 hash_odr_name (const_tree t) 304 hash_odr_name (const_tree t)
318 { 305 {
319 gcc_checking_assert (main_odr_variant (t) == t); 306 gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
320 307
321 /* If not in LTO, all main variants are unique, so we can do 308 /* If not in LTO, all main variants are unique, so we can do
322 pointer hash. */ 309 pointer hash. */
323 if (!in_lto_p) 310 if (!in_lto_p)
324 return htab_hash_pointer (t); 311 return htab_hash_pointer (t);
338 odr_name_hasher::hash (const odr_type_d *odr_type) 325 odr_name_hasher::hash (const odr_type_d *odr_type)
339 { 326 {
340 return hash_odr_name (odr_type->type); 327 return hash_odr_name (odr_type->type);
341 } 328 }
342 329
343 static bool
344 can_be_vtable_hashed_p (tree t)
345 {
346 /* vtable hashing can distinguish only main variants. */
347 if (TYPE_MAIN_VARIANT (t) != t)
348 return false;
349 /* Anonymous namespace types are always handled by name hash. */
350 if (type_with_linkage_p (t) && type_in_anonymous_namespace_p (t))
351 return false;
352 return (TREE_CODE (t) == RECORD_TYPE
353 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
354 }
355
356 /* Hash type by assembler name of its vtable. */
357
358 static hashval_t
359 hash_odr_vtable (const_tree t)
360 {
361 tree v = BINFO_VTABLE (TYPE_BINFO (TYPE_MAIN_VARIANT (t)));
362 inchash::hash hstate;
363
364 gcc_checking_assert (in_lto_p);
365 gcc_checking_assert (!type_in_anonymous_namespace_p (t));
366 gcc_checking_assert (TREE_CODE (t) == RECORD_TYPE
367 && TYPE_BINFO (t) && BINFO_VTABLE (TYPE_BINFO (t)));
368 gcc_checking_assert (main_odr_variant (t) == t);
369
370 if (TREE_CODE (v) == POINTER_PLUS_EXPR)
371 {
372 add_expr (TREE_OPERAND (v, 1), hstate);
373 v = TREE_OPERAND (TREE_OPERAND (v, 0), 0);
374 }
375
376 hstate.add_hwi (IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME (v)));
377 return hstate.end ();
378 }
379
380 /* Return the computed hashcode for ODR_TYPE. */
381
382 inline hashval_t
383 odr_vtable_hasher::hash (const odr_type_d *odr_type)
384 {
385 return hash_odr_vtable (odr_type->type);
386 }
387
388 /* For languages with One Definition Rule, work out if 330 /* For languages with One Definition Rule, work out if
389 types are the same based on their name. 331 types are the same based on their name.
390 332
391 This is non-trivial for LTO where minor differences in 333 This is non-trivial for LTO where minor differences in
392 the type representation may have prevented type merging 334 the type representation may have prevented type merging
393 to merge two copies of otherwise equivalent type. 335 to merge two copies of otherwise equivalent type.
394 336
395 Until we start streaming mangled type names, this function works 337 Until we start streaming mangled type names, this function works
396 only for polymorphic types. 338 only for polymorphic types.
397
398 When STRICT is true, we compare types by their names for purposes of
399 ODR violation warnings. When strict is false, we consider variants
400 equivalent, because it is all that matters for devirtualization machinery.
401 */ 339 */
402 340
403 bool 341 bool
404 types_same_for_odr (const_tree type1, const_tree type2, bool strict) 342 types_same_for_odr (const_tree type1, const_tree type2)
405 { 343 {
406 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2)); 344 gcc_checking_assert (TYPE_P (type1) && TYPE_P (type2));
407 345
408 type1 = main_odr_variant (type1); 346 type1 = TYPE_MAIN_VARIANT (type1);
409 type2 = main_odr_variant (type2); 347 type2 = TYPE_MAIN_VARIANT (type2);
410 if (!strict)
411 {
412 type1 = TYPE_MAIN_VARIANT (type1);
413 type2 = TYPE_MAIN_VARIANT (type2);
414 }
415 348
416 if (type1 == type2) 349 if (type1 == type2)
417 return true; 350 return true;
418 351
419 if (!in_lto_p) 352 if (!in_lto_p)
420 return false; 353 return false;
421 354
422 /* Check for anonymous namespaces. Those have !TREE_PUBLIC 355 /* Anonymous namespace types are never duplicated. */
423 on the corresponding TYPE_STUB_DECL. */
424 if ((type_with_linkage_p (type1) && type_in_anonymous_namespace_p (type1)) 356 if ((type_with_linkage_p (type1) && type_in_anonymous_namespace_p (type1))
425 || (type_with_linkage_p (type2) && type_in_anonymous_namespace_p (type2))) 357 || (type_with_linkage_p (type2) && type_in_anonymous_namespace_p (type2)))
426 return false; 358 return false;
427 359
428 360 /* If both type has mangled defined check if they are same.
429 /* ODR name of the type is set in DECL_ASSEMBLER_NAME of its TYPE_NAME. 361 Watch for anonymous types which are all mangled as "<anon">. */
430 362 if (!type_with_linkage_p (type1) || !type_with_linkage_p (type2))
431 Ideally we should never need types without ODR names here. It can however 363 return false;
432 happen in two cases: 364 if (type_in_anonymous_namespace_p (type1)
433 365 || type_in_anonymous_namespace_p (type2))
434 1) for builtin types that are not streamed but rebuilt in lto/lto-lang.c 366 return false;
435 Here testing for equivalence is safe, since their MAIN_VARIANTs are
436 unique.
437 2) for units streamed with -fno-lto-odr-type-merging. Here we can't
438 establish precise ODR equivalency, but for correctness we care only
439 about equivalency on complete polymorphic types. For these we can
440 compare assembler names of their virtual tables. */
441 if ((!TYPE_NAME (type1) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type1)))
442 || (!TYPE_NAME (type2) || !DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (type2))))
443 {
444 /* See if types are obviously different (i.e. different codes
445 or polymorphic wrt non-polymorphic). This is not strictly correct
446 for ODR violating programs, but we can't do better without streaming
447 ODR names. */
448 if (TREE_CODE (type1) != TREE_CODE (type2))
449 return false;
450 if (TREE_CODE (type1) == RECORD_TYPE
451 && (TYPE_BINFO (type1) == NULL_TREE)
452 != (TYPE_BINFO (type2) == NULL_TREE))
453 return false;
454 if (TREE_CODE (type1) == RECORD_TYPE && TYPE_BINFO (type1)
455 && (BINFO_VTABLE (TYPE_BINFO (type1)) == NULL_TREE)
456 != (BINFO_VTABLE (TYPE_BINFO (type2)) == NULL_TREE))
457 return false;
458
459 /* At the moment we have no way to establish ODR equivalence at LTO
460 other than comparing virtual table pointers of polymorphic types.
461 Eventually we should start saving mangled names in TYPE_NAME.
462 Then this condition will become non-trivial. */
463
464 if (TREE_CODE (type1) == RECORD_TYPE
465 && TYPE_BINFO (type1) && TYPE_BINFO (type2)
466 && BINFO_VTABLE (TYPE_BINFO (type1))
467 && BINFO_VTABLE (TYPE_BINFO (type2)))
468 {
469 tree v1 = BINFO_VTABLE (TYPE_BINFO (type1));
470 tree v2 = BINFO_VTABLE (TYPE_BINFO (type2));
471 gcc_assert (TREE_CODE (v1) == POINTER_PLUS_EXPR
472 && TREE_CODE (v2) == POINTER_PLUS_EXPR);
473 return (operand_equal_p (TREE_OPERAND (v1, 1),
474 TREE_OPERAND (v2, 1), 0)
475 && DECL_ASSEMBLER_NAME
476 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
477 == DECL_ASSEMBLER_NAME
478 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
479 }
480 gcc_unreachable ();
481 }
482 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1)) 367 return (DECL_ASSEMBLER_NAME (TYPE_NAME (type1))
483 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2))); 368 == DECL_ASSEMBLER_NAME (TYPE_NAME (type2)));
484 } 369 }
485 370
486 /* Return true if we can decide on ODR equivalency. 371 /* Return true if we can decide on ODR equivalency.
487 372
488 In non-LTO it is always decide, in LTO however it depends in the type has 373 In non-LTO it is always decide, in LTO however it depends in the type has
489 ODR info attached. 374 ODR info attached. */
490
491 When STRICT is false, compare main variants. */
492 375
493 bool 376 bool
494 types_odr_comparable (tree t1, tree t2, bool strict) 377 types_odr_comparable (tree t1, tree t2)
495 { 378 {
496 return (!in_lto_p 379 return (!in_lto_p
497 || (strict ? (main_odr_variant (t1) == main_odr_variant (t2) 380 || TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)
498 && main_odr_variant (t1)) 381 || (odr_type_p (TYPE_MAIN_VARIANT (t1))
499 : TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)) 382 && odr_type_p (TYPE_MAIN_VARIANT (t2))));
500 || (odr_type_p (t1) && odr_type_p (t2))
501 || (TREE_CODE (t1) == RECORD_TYPE && TREE_CODE (t2) == RECORD_TYPE
502 && TYPE_BINFO (t1) && TYPE_BINFO (t2)
503 && polymorphic_type_binfo_p (TYPE_BINFO (t1))
504 && polymorphic_type_binfo_p (TYPE_BINFO (t2))));
505 } 383 }
506 384
507 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not 385 /* Return true if T1 and T2 are ODR equivalent. If ODR equivalency is not
508 known, be conservative and return false. */ 386 known, be conservative and return false. */
509 387
540 bool 418 bool
541 odr_or_derived_type_p (const_tree t) 419 odr_or_derived_type_p (const_tree t)
542 { 420 {
543 do 421 do
544 { 422 {
545 if (odr_type_p (t)) 423 if (odr_type_p (TYPE_MAIN_VARIANT (t)))
546 return true; 424 return true;
547 /* Function type is a tricky one. Basically we can consider it 425 /* Function type is a tricky one. Basically we can consider it
548 ODR derived if return type or any of the parameters is. 426 ODR derived if return type or any of the parameters is.
549 We need to check all parameters because LTO streaming merges 427 We need to check all parameters because LTO streaming merges
550 common types (such as void) and they are not considered ODR then. */ 428 common types (such as void) and they are not considered ODR then. */
555 else 433 else
556 { 434 {
557 if (TREE_TYPE (t) && odr_or_derived_type_p (TREE_TYPE (t))) 435 if (TREE_TYPE (t) && odr_or_derived_type_p (TREE_TYPE (t)))
558 return true; 436 return true;
559 for (t = TYPE_ARG_TYPES (t); t; t = TREE_CHAIN (t)) 437 for (t = TYPE_ARG_TYPES (t); t; t = TREE_CHAIN (t))
560 if (odr_or_derived_type_p (TREE_VALUE (t))) 438 if (odr_or_derived_type_p (TYPE_MAIN_VARIANT (TREE_VALUE (t))))
561 return true; 439 return true;
562 return false; 440 return false;
563 } 441 }
564 } 442 }
565 else 443 else
575 inline bool 453 inline bool
576 odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2) 454 odr_name_hasher::equal (const odr_type_d *o1, const tree_node *t2)
577 { 455 {
578 tree t1 = o1->type; 456 tree t1 = o1->type;
579 457
580 gcc_checking_assert (main_odr_variant (t2) == t2); 458 gcc_checking_assert (TYPE_MAIN_VARIANT (t2) == t2);
581 gcc_checking_assert (main_odr_variant (t1) == t1); 459 gcc_checking_assert (TYPE_MAIN_VARIANT (t1) == t1);
582 if (t1 == t2) 460 if (t1 == t2)
583 return true; 461 return true;
584 if (!in_lto_p) 462 if (!in_lto_p)
585 return false; 463 return false;
586 /* Check for anonymous namespaces. Those have !TREE_PUBLIC 464 /* Check for anonymous namespaces. */
587 on the corresponding TYPE_STUB_DECL. */
588 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1)) 465 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
589 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2))) 466 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
590 return false; 467 return false;
591 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))); 468 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)));
592 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2))); 469 gcc_checking_assert (DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
593 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1)) 470 return (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))
594 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2))); 471 == DECL_ASSEMBLER_NAME (TYPE_NAME (t2)));
595 } 472 }
596 473
597 /* Compare types T1 and T2 and return true if they are
598 equivalent. */
599
600 inline bool
601 odr_vtable_hasher::equal (const odr_type_d *o1, const tree_node *t2)
602 {
603 tree t1 = o1->type;
604
605 gcc_checking_assert (main_odr_variant (t2) == t2);
606 gcc_checking_assert (main_odr_variant (t1) == t1);
607 gcc_checking_assert (in_lto_p);
608 t1 = TYPE_MAIN_VARIANT (t1);
609 t2 = TYPE_MAIN_VARIANT (t2);
610 if (t1 == t2)
611 return true;
612 tree v1 = BINFO_VTABLE (TYPE_BINFO (t1));
613 tree v2 = BINFO_VTABLE (TYPE_BINFO (t2));
614 return (operand_equal_p (TREE_OPERAND (v1, 1),
615 TREE_OPERAND (v2, 1), 0)
616 && DECL_ASSEMBLER_NAME
617 (TREE_OPERAND (TREE_OPERAND (v1, 0), 0))
618 == DECL_ASSEMBLER_NAME
619 (TREE_OPERAND (TREE_OPERAND (v2, 0), 0)));
620 }
621
622 /* Free ODR type V. */ 474 /* Free ODR type V. */
623 475
624 inline void 476 inline void
625 odr_name_hasher::remove (odr_type_d *v) 477 odr_name_hasher::remove (odr_type_d *v)
626 { 478 {
633 485
634 /* ODR type hash used to look up ODR type based on tree type node. */ 486 /* ODR type hash used to look up ODR type based on tree type node. */
635 487
636 typedef hash_table<odr_name_hasher> odr_hash_type; 488 typedef hash_table<odr_name_hasher> odr_hash_type;
637 static odr_hash_type *odr_hash; 489 static odr_hash_type *odr_hash;
638 typedef hash_table<odr_vtable_hasher> odr_vtable_hash_type;
639 static odr_vtable_hash_type *odr_vtable_hash;
640 490
641 /* ODR types are also stored into ODR_TYPE vector to allow consistent 491 /* ODR types are also stored into ODR_TYPE vector to allow consistent
642 walking. Bases appear before derived types. Vector is garbage collected 492 walking. Bases appear before derived types. Vector is garbage collected
643 so we won't end up visiting empty types. */ 493 so we won't end up visiting empty types. */
644 494
654 TYPE_BINFO (type) = binfo; 504 TYPE_BINFO (type) = binfo;
655 else 505 else
656 gcc_assert (!TYPE_BINFO (type)); 506 gcc_assert (!TYPE_BINFO (type));
657 } 507 }
658 508
509 /* Return true if type variants match.
510 This assumes that we already verified that T1 and T2 are variants of the
511 same type. */
512
513 static bool
514 type_variants_equivalent_p (tree t1, tree t2)
515 {
516 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
517 return false;
518
519 if (comp_type_attributes (t1, t2) != 1)
520 return false;
521
522 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2)
523 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2))
524 return false;
525
526 return true;
527 }
528
659 /* Compare T1 and T2 based on name or structure. */ 529 /* Compare T1 and T2 based on name or structure. */
660 530
661 static bool 531 static bool
662 odr_subtypes_equivalent_p (tree t1, tree t2, 532 odr_subtypes_equivalent_p (tree t1, tree t2,
663 hash_set<type_pair> *visited, 533 hash_set<type_pair> *visited,
665 { 535 {
666 536
667 /* This can happen in incomplete types that should be handled earlier. */ 537 /* This can happen in incomplete types that should be handled earlier. */
668 gcc_assert (t1 && t2); 538 gcc_assert (t1 && t2);
669 539
670 t1 = main_odr_variant (t1);
671 t2 = main_odr_variant (t2);
672 if (t1 == t2) 540 if (t1 == t2)
673 return true; 541 return true;
674 542
675 /* Anonymous namespace types must match exactly. */ 543 /* Anonymous namespace types must match exactly. */
676 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1)) 544 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
677 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2))) 545 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
546 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
547 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
678 return false; 548 return false;
679 549
680 /* For ODR types be sure to compare their names. 550 /* For ODR types be sure to compare their names.
681 To support -Wno-odr-type-merging we allow one type to be non-ODR 551 To support -Wno-odr-type-merging we allow one type to be non-ODR
682 and other ODR even though it is a violation. */ 552 and other ODR even though it is a violation. */
683 if (types_odr_comparable (t1, t2, true)) 553 if (types_odr_comparable (t1, t2))
684 { 554 {
685 if (!types_same_for_odr (t1, t2, true)) 555 if (t1 != t2
556 && odr_type_p (TYPE_MAIN_VARIANT (t1))
557 && get_odr_type (TYPE_MAIN_VARIANT (t1), true)->odr_violated)
558 return false;
559 if (!types_same_for_odr (t1, t2))
686 return false; 560 return false;
561 if (!type_variants_equivalent_p (t1, t2))
562 return false;
687 /* Limit recursion: If subtypes are ODR types and we know 563 /* Limit recursion: If subtypes are ODR types and we know
688 that they are same, be happy. */ 564 that they are same, be happy. */
689 if (!odr_type_p (t1) || !get_odr_type (t1, true)->odr_violated) 565 if (odr_type_p (TYPE_MAIN_VARIANT (t1)))
690 return true; 566 return true;
691 } 567 }
692 568
693 /* Component types, builtins and possibly violating ODR types 569 /* Component types, builtins and possibly violating ODR types
694 have to be compared structurally. */ 570 have to be compared structurally. */
696 return false; 572 return false;
697 if (AGGREGATE_TYPE_P (t1) 573 if (AGGREGATE_TYPE_P (t1)
698 && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE)) 574 && (TYPE_NAME (t1) == NULL_TREE) != (TYPE_NAME (t2) == NULL_TREE))
699 return false; 575 return false;
700 576
701 type_pair pair={t1,t2}; 577 type_pair pair={TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2)};
702 if (TYPE_UID (t1) > TYPE_UID (t2)) 578 if (TYPE_UID (TYPE_MAIN_VARIANT (t1)) > TYPE_UID (TYPE_MAIN_VARIANT (t2)))
703 { 579 {
704 pair.first = t2; 580 pair.first = TYPE_MAIN_VARIANT (t2);
705 pair.second = t1; 581 pair.second = TYPE_MAIN_VARIANT (t1);
706 } 582 }
707 if (visited->add (pair)) 583 if (visited->add (pair))
708 return true; 584 return true;
709 return odr_types_equivalent_p (t1, t2, false, NULL, visited, loc1, loc2); 585 if (!odr_types_equivalent_p (TYPE_MAIN_VARIANT (t1), TYPE_MAIN_VARIANT (t2),
586 false, NULL, visited, loc1, loc2))
587 return false;
588 if (!type_variants_equivalent_p (t1, t2))
589 return false;
590 return true;
710 } 591 }
711 592
712 /* Return true if DECL1 and DECL2 are identical methods. Consider 593 /* Return true if DECL1 and DECL2 are identical methods. Consider
713 name equivalent to name.localalias.xyz. */ 594 name equivalent to name.localalias.xyz. */
714 595
780 661
781 end1 = !prevailing->iterate_reference (n1, ref1); 662 end1 = !prevailing->iterate_reference (n1, ref1);
782 end2 = !vtable->iterate_reference (n2, ref2); 663 end2 = !vtable->iterate_reference (n2, ref2);
783 664
784 /* !DECL_VIRTUAL_P means RTTI entry; 665 /* !DECL_VIRTUAL_P means RTTI entry;
785 We warn when RTTI is lost because non-RTTI previals; we silently 666 We warn when RTTI is lost because non-RTTI prevails; we silently
786 accept the other case. */ 667 accept the other case. */
787 while (!end2 668 while (!end2
788 && (end1 669 && (end1
789 || (methods_equal_p (ref1->referred->decl, 670 || (methods_equal_p (ref1->referred->decl,
790 ref2->referred->decl) 671 ref2->referred->decl)
834 is not output too often. */ 715 is not output too often. */
835 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl)) 716 if (DECL_SIZE (prevailing->decl) != DECL_SIZE (vtable->decl))
836 { 717 {
837 class_type->odr_violated = true; 718 class_type->odr_violated = true;
838 auto_diagnostic_group d; 719 auto_diagnostic_group d;
839 if (warning_at (DECL_SOURCE_LOCATION 720 tree ctx = TYPE_NAME (DECL_CONTEXT (vtable->decl));
840 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 721 if (warning_at (DECL_SOURCE_LOCATION (ctx), OPT_Wodr,
841 OPT_Wodr,
842 "virtual table of type %qD violates " 722 "virtual table of type %qD violates "
843 "one definition rule ", 723 "one definition rule",
844 DECL_CONTEXT (vtable->decl))) 724 DECL_CONTEXT (vtable->decl)))
845 { 725 {
846 inform (DECL_SOURCE_LOCATION 726 ctx = TYPE_NAME (DECL_CONTEXT (prevailing->decl));
847 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))), 727 inform (DECL_SOURCE_LOCATION (ctx),
848 "the conflicting type defined in another translation " 728 "the conflicting type defined in another translation"
849 "unit has virtual table of different size"); 729 " unit has virtual table of different size");
850 } 730 }
851 } 731 }
852 return; 732 return;
853 } 733 }
854 734
867 auto_diagnostic_group d; 747 auto_diagnostic_group d;
868 if (warning_at (DECL_SOURCE_LOCATION 748 if (warning_at (DECL_SOURCE_LOCATION
869 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), 749 (TYPE_NAME (DECL_CONTEXT (vtable->decl))),
870 OPT_Wodr, 750 OPT_Wodr,
871 "virtual table of type %qD violates " 751 "virtual table of type %qD violates "
872 "one definition rule ", 752 "one definition rule",
873 DECL_CONTEXT (vtable->decl))) 753 DECL_CONTEXT (vtable->decl)))
874 { 754 {
875 inform (DECL_SOURCE_LOCATION 755 inform (DECL_SOURCE_LOCATION
876 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))), 756 (TYPE_NAME (DECL_CONTEXT (prevailing->decl))),
877 "the conflicting type defined in another translation " 757 "the conflicting type defined in another translation "
893 } 773 }
894 } 774 }
895 775
896 class_type->odr_violated = true; 776 class_type->odr_violated = true;
897 777
898 /* Complain about size mismatch. Either we have too many virutal 778 /* Complain about size mismatch. Either we have too many virtual
899 functions or too many virtual table pointers. */ 779 functions or too many virtual table pointers. */
900 if (end1 || end2) 780 if (end1 || end2)
901 { 781 {
902 if (end1) 782 if (end1)
903 { 783 {
934 } 814 }
935 } 815 }
936 return; 816 return;
937 } 817 }
938 818
939 /* And in the last case we have either mistmatch in between two virtual 819 /* And in the last case we have either mismatch in between two virtual
940 methods or two virtual table pointers. */ 820 methods or two virtual table pointers. */
941 auto_diagnostic_group d; 821 auto_diagnostic_group d;
942 if (warning_at (DECL_SOURCE_LOCATION 822 if (warning_at (DECL_SOURCE_LOCATION
943 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr, 823 (TYPE_NAME (DECL_CONTEXT (vtable->decl))), OPT_Wodr,
944 "virtual table of type %qD violates " 824 "virtual table of type %qD violates "
945 "one definition rule ", 825 "one definition rule",
946 DECL_CONTEXT (vtable->decl))) 826 DECL_CONTEXT (vtable->decl)))
947 { 827 {
948 if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL) 828 if (TREE_CODE (ref1->referred->decl) == FUNCTION_DECL)
949 { 829 {
950 inform (DECL_SOURCE_LOCATION 830 inform (DECL_SOURCE_LOCATION
976 Display location of ST1 and ST2 if REASON speaks about field or 856 Display location of ST1 and ST2 if REASON speaks about field or
977 method of the type. 857 method of the type.
978 If WARN is false, do nothing. Set WARNED if warning was indeed 858 If WARN is false, do nothing. Set WARNED if warning was indeed
979 output. */ 859 output. */
980 860
981 void 861 static void
982 warn_odr (tree t1, tree t2, tree st1, tree st2, 862 warn_odr (tree t1, tree t2, tree st1, tree st2,
983 bool warn, bool *warned, const char *reason) 863 bool warn, bool *warned, const char *reason)
984 { 864 {
985 tree decl2 = TYPE_NAME (t2); 865 tree decl2 = TYPE_NAME (TYPE_MAIN_VARIANT (t2));
986 if (warned) 866 if (warned)
987 *warned = false; 867 *warned = false;
988 868
989 if (!warn || !TYPE_NAME(t1)) 869 if (!warn || !TYPE_NAME(TYPE_MAIN_VARIANT (t1)))
990 return; 870 return;
991 871
992 /* ODR warnings are output druing LTO streaming; we must apply location 872 /* ODR warnings are output during LTO streaming; we must apply location
993 cache for potential warnings to be output correctly. */ 873 cache for potential warnings to be output correctly. */
994 if (lto_location_cache::current_cache) 874 if (lto_location_cache::current_cache)
995 lto_location_cache::current_cache->apply_location_cache (); 875 lto_location_cache::current_cache->apply_location_cache ();
996 876
997 auto_diagnostic_group d; 877 auto_diagnostic_group d;
998 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (t1)), OPT_Wodr, 878 if (t1 != TYPE_MAIN_VARIANT (t1)
999 "type %qT violates the C++ One Definition Rule", 879 && TYPE_NAME (t1) != TYPE_NAME (TYPE_MAIN_VARIANT (t1)))
1000 t1)) 880 {
1001 return; 881 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1))),
882 OPT_Wodr, "type %qT (typedef of %qT) violates the "
883 "C++ One Definition Rule",
884 t1, TYPE_MAIN_VARIANT (t1)))
885 return;
886 }
887 else
888 {
889 if (!warning_at (DECL_SOURCE_LOCATION (TYPE_NAME (TYPE_MAIN_VARIANT (t1))),
890 OPT_Wodr, "type %qT violates the C++ One Definition Rule",
891 t1))
892 return;
893 }
1002 if (!st1 && !st2) 894 if (!st1 && !st2)
1003 ; 895 ;
1004 /* For FIELD_DECL support also case where one of fields is 896 /* For FIELD_DECL support also case where one of fields is
1005 NULL - this is used when the structures have mismatching number of 897 NULL - this is used when the structures have mismatching number of
1006 elements. */ 898 elements. */
1034 926
1035 if (warned) 927 if (warned)
1036 *warned = true; 928 *warned = true;
1037 } 929 }
1038 930
1039 /* Return ture if T1 and T2 are incompatible and we want to recusively 931 /* Return true if T1 and T2 are incompatible and we want to recursively
1040 dive into them from warn_type_mismatch to give sensible answer. */ 932 dive into them from warn_type_mismatch to give sensible answer. */
1041 933
1042 static bool 934 static bool
1043 type_mismatch_p (tree t1, tree t2) 935 type_mismatch_p (tree t1, tree t2)
1044 { 936 {
1055 extra information on how the types mismatch. 947 extra information on how the types mismatch.
1056 948
1057 This is hard to do in general. We basically handle the common cases. 949 This is hard to do in general. We basically handle the common cases.
1058 950
1059 If LOC1 and LOC2 are meaningful locations, use it in the case the types 951 If LOC1 and LOC2 are meaningful locations, use it in the case the types
1060 themselves do no thave one.*/ 952 themselves do not have one. */
1061 953
1062 void 954 void
1063 warn_types_mismatch (tree t1, tree t2, location_t loc1, location_t loc2) 955 warn_types_mismatch (tree t1, tree t2, location_t loc1, location_t loc2)
1064 { 956 {
1065 /* Location of type is known only if it has TYPE_NAME and the name is 957 /* Location of type is known only if it has TYPE_NAME and the name is
1100 992
1101 location_t loc = loc_t1 <= BUILTINS_LOCATION ? loc_t2 : loc_t1; 993 location_t loc = loc_t1 <= BUILTINS_LOCATION ? loc_t2 : loc_t1;
1102 994
1103 /* It is a quite common bug to reference anonymous namespace type in 995 /* It is a quite common bug to reference anonymous namespace type in
1104 non-anonymous namespace class. */ 996 non-anonymous namespace class. */
1105 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1)) 997 tree mt1 = TYPE_MAIN_VARIANT (t1);
1106 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2))) 998 tree mt2 = TYPE_MAIN_VARIANT (t2);
1107 { 999 if ((type_with_linkage_p (mt1)
1108 if (type_with_linkage_p (t1) && !type_in_anonymous_namespace_p (t1)) 1000 && type_in_anonymous_namespace_p (mt1))
1001 || (type_with_linkage_p (mt2)
1002 && type_in_anonymous_namespace_p (mt2)))
1003 {
1004 if (!type_with_linkage_p (mt1)
1005 || !type_in_anonymous_namespace_p (mt1))
1109 { 1006 {
1110 std::swap (t1, t2); 1007 std::swap (t1, t2);
1008 std::swap (mt1, mt2);
1111 std::swap (loc_t1, loc_t2); 1009 std::swap (loc_t1, loc_t2);
1112 } 1010 }
1113 gcc_assert (TYPE_NAME (t1) && TYPE_NAME (t2) 1011 gcc_assert (TYPE_NAME (mt1)
1114 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL 1012 && TREE_CODE (TYPE_NAME (mt1)) == TYPE_DECL);
1115 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL); 1013 tree n1 = TYPE_NAME (mt1);
1116 /* Most of the time, the type names will match, do not be unnecesarily 1014 tree n2 = TYPE_NAME (mt2) ? TYPE_NAME (mt2) : NULL;
1015
1016 if (TREE_CODE (n1) == TYPE_DECL)
1017 n1 = DECL_NAME (n1);
1018 if (n2 && TREE_CODE (n2) == TYPE_DECL)
1019 n2 = DECL_NAME (n2);
1020 /* Most of the time, the type names will match, do not be unnecessarily
1117 verbose. */ 1021 verbose. */
1118 if (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t1))) 1022 if (n1 != n2)
1119 != IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (t2))))
1120 inform (loc_t1, 1023 inform (loc_t1,
1121 "type %qT defined in anonymous namespace can not match " 1024 "type %qT defined in anonymous namespace cannot match "
1122 "type %qT across the translation unit boundary", 1025 "type %qT across the translation unit boundary",
1123 t1, t2); 1026 t1, t2);
1124 else 1027 else
1125 inform (loc_t1, 1028 inform (loc_t1,
1126 "type %qT defined in anonymous namespace can not match " 1029 "type %qT defined in anonymous namespace cannot match "
1127 "across the translation unit boundary", 1030 "across the translation unit boundary",
1128 t1); 1031 t1);
1129 if (loc_t2_useful) 1032 if (loc_t2_useful)
1130 inform (loc_t2, 1033 inform (loc_t2,
1131 "the incompatible type defined in another translation unit"); 1034 "the incompatible type defined in another translation unit");
1132 return; 1035 return;
1133 } 1036 }
1134 /* If types have mangled ODR names and they are different, it is most 1037 /* If types have mangled ODR names and they are different, it is most
1135 informative to output those. 1038 informative to output those.
1136 This also covers types defined in different namespaces. */ 1039 This also covers types defined in different namespaces. */
1137 if (TYPE_NAME (t1) && TYPE_NAME (t2) 1040 const char *odr1 = get_odr_name_for_type (mt1);
1138 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL 1041 const char *odr2 = get_odr_name_for_type (mt2);
1139 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL 1042 if (odr1 != NULL && odr2 != NULL && odr1 != odr2)
1140 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t1)) 1043 {
1141 && DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t2)) 1044 const int opts = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
1142 && DECL_ASSEMBLER_NAME (TYPE_NAME (t1)) 1045 char *name1 = xstrdup (cplus_demangle (odr1, opts));
1143 != DECL_ASSEMBLER_NAME (TYPE_NAME (t2))) 1046 char *name2 = cplus_demangle (odr2, opts);
1144 {
1145 char *name1 = xstrdup (cplus_demangle
1146 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t1))),
1147 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES));
1148 char *name2 = cplus_demangle
1149 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (TYPE_NAME (t2))),
1150 DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES);
1151 if (name1 && name2 && strcmp (name1, name2)) 1047 if (name1 && name2 && strcmp (name1, name2))
1152 { 1048 {
1153 inform (loc_t1, 1049 inform (loc_t1,
1154 "type name %qs should match type name %qs", 1050 "type name %qs should match type name %qs",
1155 name1, name2); 1051 name1, name2);
1233 } 1129 }
1234 } 1130 }
1235 return; 1131 return;
1236 } 1132 }
1237 1133
1238 if (types_odr_comparable (t1, t2, true) 1134 if (types_odr_comparable (t1, t2)
1239 && types_same_for_odr (t1, t2, true)) 1135 /* We make assign integers mangled names to be able to handle
1136 signed/unsigned chars. Accepting them here would however lead to
1137 confusing message like
1138 "type ‘const int’ itself violates the C++ One Definition Rule" */
1139 && TREE_CODE (t1) != INTEGER_TYPE
1140 && types_same_for_odr (t1, t2))
1240 inform (loc_t1, 1141 inform (loc_t1,
1241 "type %qT itself violates the C++ One Definition Rule", t1); 1142 "type %qT itself violates the C++ One Definition Rule", t1);
1242 /* Prevent pointless warnings like "struct aa" should match "struct aa". */ 1143 /* Prevent pointless warnings like "struct aa" should match "struct aa". */
1243 else if (TYPE_NAME (t1) == TYPE_NAME (t2) 1144 else if (TYPE_NAME (t1) == TYPE_NAME (t2)
1244 && TREE_CODE (t1) == TREE_CODE (t2) && !loc_t2_useful) 1145 && TREE_CODE (t1) == TREE_CODE (t2) && !loc_t2_useful)
1248 t1, t2); 1149 t1, t2);
1249 if (loc_t2_useful) 1150 if (loc_t2_useful)
1250 inform (loc_t2, "the incompatible type is defined here"); 1151 inform (loc_t2, "the incompatible type is defined here");
1251 } 1152 }
1252 1153
1154 /* Return true if T should be ignored in TYPE_FIELDS for ODR comparison. */
1155
1156 static bool
1157 skip_in_fields_list_p (tree t)
1158 {
1159 if (TREE_CODE (t) != FIELD_DECL)
1160 return true;
1161 /* C++ FE introduces zero sized fields depending on -std setting, see
1162 PR89358. */
1163 if (DECL_SIZE (t)
1164 && integer_zerop (DECL_SIZE (t))
1165 && DECL_ARTIFICIAL (t)
1166 && DECL_IGNORED_P (t)
1167 && !DECL_NAME (t))
1168 return true;
1169 return false;
1170 }
1171
1253 /* Compare T1 and T2, report ODR violations if WARN is true and set 1172 /* Compare T1 and T2, report ODR violations if WARN is true and set
1254 WARNED to true if anything is reported. Return true if types match. 1173 WARNED to true if anything is reported. Return true if types match.
1255 If true is returned, the types are also compatible in the sense of 1174 If true is returned, the types are also compatible in the sense of
1256 gimple_canonical_types_compatible_p. 1175 gimple_canonical_types_compatible_p.
1257 If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning 1176 If LOC1 and LOC2 is not UNKNOWN_LOCATION it may be used to output a warning
1263 location_t loc1, location_t loc2) 1182 location_t loc1, location_t loc2)
1264 { 1183 {
1265 /* Check first for the obvious case of pointer identity. */ 1184 /* Check first for the obvious case of pointer identity. */
1266 if (t1 == t2) 1185 if (t1 == t2)
1267 return true; 1186 return true;
1268 gcc_assert (!type_with_linkage_p (t1) || !type_in_anonymous_namespace_p (t1));
1269 gcc_assert (!type_with_linkage_p (t2) || !type_in_anonymous_namespace_p (t2));
1270 1187
1271 /* Can't be the same type if the types don't have the same code. */ 1188 /* Can't be the same type if the types don't have the same code. */
1272 if (TREE_CODE (t1) != TREE_CODE (t2)) 1189 if (TREE_CODE (t1) != TREE_CODE (t2))
1273 { 1190 {
1274 warn_odr (t1, t2, NULL, NULL, warn, warned, 1191 warn_odr (t1, t2, NULL, NULL, warn, warned,
1275 G_("a different type is defined in another translation unit")); 1192 G_("a different type is defined in another translation unit"));
1276 return false; 1193 return false;
1277 } 1194 }
1278 1195
1279 if (TYPE_QUALS (t1) != TYPE_QUALS (t2)) 1196 if ((type_with_linkage_p (TYPE_MAIN_VARIANT (t1))
1280 { 1197 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t1)))
1281 warn_odr (t1, t2, NULL, NULL, warn, warned, 1198 || (type_with_linkage_p (TYPE_MAIN_VARIANT (t2))
1282 G_("a type with different qualifiers is defined in another " 1199 && type_in_anonymous_namespace_p (TYPE_MAIN_VARIANT (t2))))
1283 "translation unit")); 1200 {
1284 return false; 1201 /* We cannot trip this when comparing ODR types, only when trying to
1285 }
1286
1287 if ((type_with_linkage_p (t1) && type_in_anonymous_namespace_p (t1))
1288 || (type_with_linkage_p (t2) && type_in_anonymous_namespace_p (t2)))
1289 {
1290 /* We can not trip this when comparing ODR types, only when trying to
1291 match different ODR derivations from different declarations. 1202 match different ODR derivations from different declarations.
1292 So WARN should be always false. */ 1203 So WARN should be always false. */
1293 gcc_assert (!warn); 1204 gcc_assert (!warn);
1294 return false;
1295 }
1296
1297 if (comp_type_attributes (t1, t2) != 1)
1298 {
1299 warn_odr (t1, t2, NULL, NULL, warn, warned,
1300 G_("a type with different attributes "
1301 "is defined in another translation unit"));
1302 return false; 1205 return false;
1303 } 1206 }
1304 1207
1305 if (TREE_CODE (t1) == ENUMERAL_TYPE 1208 if (TREE_CODE (t1) == ENUMERAL_TYPE
1306 && TYPE_VALUES (t1) && TYPE_VALUES (t2)) 1209 && TYPE_VALUES (t1) && TYPE_VALUES (t2))
1314 warn_odr (t1, t2, NULL, NULL, warn, warned, 1217 warn_odr (t1, t2, NULL, NULL, warn, warned,
1315 G_("an enum with different value name" 1218 G_("an enum with different value name"
1316 " is defined in another translation unit")); 1219 " is defined in another translation unit"));
1317 return false; 1220 return false;
1318 } 1221 }
1319 if (TREE_VALUE (v1) != TREE_VALUE (v2) 1222 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2), 0))
1320 && !operand_equal_p (DECL_INITIAL (TREE_VALUE (v1)),
1321 DECL_INITIAL (TREE_VALUE (v2)), 0))
1322 { 1223 {
1323 warn_odr (t1, t2, NULL, NULL, warn, warned, 1224 warn_odr (t1, t2, NULL, NULL, warn, warned,
1324 G_("an enum with different values is defined" 1225 G_("an enum with different values is defined"
1325 " in another translation unit")); 1226 " in another translation unit"));
1326 return false; 1227 return false;
1434 tree i2 = TYPE_DOMAIN (t2); 1335 tree i2 = TYPE_DOMAIN (t2);
1435 1336
1436 /* For an incomplete external array, the type domain can be 1337 /* For an incomplete external array, the type domain can be
1437 NULL_TREE. Check this condition also. */ 1338 NULL_TREE. Check this condition also. */
1438 if (i1 == NULL_TREE || i2 == NULL_TREE) 1339 if (i1 == NULL_TREE || i2 == NULL_TREE)
1439 return true; 1340 return type_variants_equivalent_p (t1, t2);
1440 1341
1441 tree min1 = TYPE_MIN_VALUE (i1); 1342 tree min1 = TYPE_MIN_VALUE (i1);
1442 tree min2 = TYPE_MIN_VALUE (i2); 1343 tree min2 = TYPE_MIN_VALUE (i2);
1443 tree max1 = TYPE_MAX_VALUE (i1); 1344 tree max1 = TYPE_MAX_VALUE (i1);
1444 tree max2 = TYPE_MAX_VALUE (i2); 1345 tree max2 = TYPE_MAX_VALUE (i2);
1470 return false; 1371 return false;
1471 } 1372 }
1472 1373
1473 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2) 1374 if (TYPE_ARG_TYPES (t1) == TYPE_ARG_TYPES (t2)
1474 || !prototype_p (t1) || !prototype_p (t2)) 1375 || !prototype_p (t1) || !prototype_p (t2))
1475 return true; 1376 return type_variants_equivalent_p (t1, t2);
1476 else 1377 else
1477 { 1378 {
1478 tree parms1, parms2; 1379 tree parms1, parms2;
1479 1380
1480 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2); 1381 for (parms1 = TYPE_ARG_TYPES (t1), parms2 = TYPE_ARG_TYPES (t2);
1481 parms1 && parms2; 1382 parms1 && parms2;
1482 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2)) 1383 parms1 = TREE_CHAIN (parms1), parms2 = TREE_CHAIN (parms2))
1483 { 1384 {
1484 if (!odr_subtypes_equivalent_p 1385 if (!odr_subtypes_equivalent_p
1485 (TREE_VALUE (parms1), TREE_VALUE (parms2), visited, 1386 (TREE_VALUE (parms1), TREE_VALUE (parms2),
1486 loc1, loc2)) 1387 visited, loc1, loc2))
1487 { 1388 {
1488 warn_odr (t1, t2, NULL, NULL, warn, warned, 1389 warn_odr (t1, t2, NULL, NULL, warn, warned,
1489 G_("has different parameters in another " 1390 G_("has different parameters in another "
1490 "translation unit")); 1391 "translation unit"));
1491 if (warn && warned) 1392 if (warn && warned)
1501 G_("has different parameters " 1402 G_("has different parameters "
1502 "in another translation unit")); 1403 "in another translation unit"));
1503 return false; 1404 return false;
1504 } 1405 }
1505 1406
1506 return true; 1407 return type_variants_equivalent_p (t1, t2);
1507 } 1408 }
1508 1409
1509 case RECORD_TYPE: 1410 case RECORD_TYPE:
1510 case UNION_TYPE: 1411 case UNION_TYPE:
1511 case QUAL_UNION_TYPE: 1412 case QUAL_UNION_TYPE:
1532 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2); 1433 for (f1 = TYPE_FIELDS (t1), f2 = TYPE_FIELDS (t2);
1533 f1 || f2; 1434 f1 || f2;
1534 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2)) 1435 f1 = TREE_CHAIN (f1), f2 = TREE_CHAIN (f2))
1535 { 1436 {
1536 /* Skip non-fields. */ 1437 /* Skip non-fields. */
1537 while (f1 && TREE_CODE (f1) != FIELD_DECL) 1438 while (f1 && skip_in_fields_list_p (f1))
1538 f1 = TREE_CHAIN (f1); 1439 f1 = TREE_CHAIN (f1);
1539 while (f2 && TREE_CODE (f2) != FIELD_DECL) 1440 while (f2 && skip_in_fields_list_p (f2))
1540 f2 = TREE_CHAIN (f2); 1441 f2 = TREE_CHAIN (f2);
1541 if (!f1 || !f2) 1442 if (!f1 || !f2)
1542 break; 1443 break;
1543 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2)) 1444 if (DECL_VIRTUAL_P (f1) != DECL_VIRTUAL_P (f2))
1544 { 1445 {
1561 G_("a field with different name is defined " 1462 G_("a field with different name is defined "
1562 "in another translation unit")); 1463 "in another translation unit"));
1563 return false; 1464 return false;
1564 } 1465 }
1565 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1), 1466 if (!odr_subtypes_equivalent_p (TREE_TYPE (f1),
1566 TREE_TYPE (f2), visited, 1467 TREE_TYPE (f2),
1567 loc1, loc2)) 1468 visited, loc1, loc2))
1568 { 1469 {
1569 /* Do not warn about artificial fields and just go into 1470 /* Do not warn about artificial fields and just go into
1570 generic field mismatch warning. */ 1471 generic field mismatch warning. */
1571 if (DECL_ARTIFICIAL (f1)) 1472 if (DECL_ARTIFICIAL (f1))
1572 break; 1473 break;
1590 return false; 1491 return false;
1591 } 1492 }
1592 if (DECL_BIT_FIELD (f1) != DECL_BIT_FIELD (f2)) 1493 if (DECL_BIT_FIELD (f1) != DECL_BIT_FIELD (f2))
1593 { 1494 {
1594 warn_odr (t1, t2, f1, f2, warn, warned, 1495 warn_odr (t1, t2, f1, f2, warn, warned,
1595 G_("one field is bitfield while other is not")); 1496 G_("one field is a bitfield while the other "
1497 "is not"));
1596 return false; 1498 return false;
1597 } 1499 }
1598 else 1500 else
1599 gcc_assert (DECL_NONADDRESSABLE_P (f1) 1501 gcc_assert (DECL_NONADDRESSABLE_P (f1)
1600 == DECL_NONADDRESSABLE_P (f2)); 1502 == DECL_NONADDRESSABLE_P (f2));
1639 warn_odr (t1, t2, NULL, NULL, warn, warned, 1541 warn_odr (t1, t2, NULL, NULL, warn, warned,
1640 G_("a type with different size " 1542 G_("a type with different size "
1641 "is defined in another translation unit")); 1543 "is defined in another translation unit"));
1642 return false; 1544 return false;
1643 } 1545 }
1644 if (COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2) 1546
1645 && TYPE_ALIGN (t1) != TYPE_ALIGN (t2)) 1547 if (TREE_ADDRESSABLE (t1) != TREE_ADDRESSABLE (t2)
1548 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1646 { 1549 {
1647 warn_odr (t1, t2, NULL, NULL, warn, warned, 1550 warn_odr (t1, t2, NULL, NULL, warn, warned,
1648 G_("a type with different alignment " 1551 G_("one type needs to be constructed while other not"));
1649 "is defined in another translation unit")); 1552 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (t1));
1650 return false; 1553 return false;
1651 } 1554 }
1555 /* There is no really good user facing warning for this.
1556 Either the original reason for modes being different is lost during
1557 streaming or we should catch earlier warnings. We however must detect
1558 the mismatch to avoid type verifier from cmplaining on mismatched
1559 types between type and canonical type. See PR91576. */
1560 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1561 && COMPLETE_TYPE_P (t1) && COMPLETE_TYPE_P (t2))
1562 {
1563 warn_odr (t1, t2, NULL, NULL, warn, warned,
1564 G_("memory layout mismatch"));
1565 return false;
1566 }
1567
1652 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2) 1568 gcc_assert (!TYPE_SIZE_UNIT (t1) || !TYPE_SIZE_UNIT (t2)
1653 || operand_equal_p (TYPE_SIZE_UNIT (t1), 1569 || operand_equal_p (TYPE_SIZE_UNIT (t1),
1654 TYPE_SIZE_UNIT (t2), 0)); 1570 TYPE_SIZE_UNIT (t2), 0));
1655 return true; 1571 return type_variants_equivalent_p (t1, t2);
1656 } 1572 }
1657 1573
1658 /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */ 1574 /* Return true if TYPE1 and TYPE2 are equivalent for One Definition Rule. */
1659 1575
1660 bool 1576 bool
1699 } 1615 }
1700 /* Always prefer complete type to be the leader. */ 1616 /* Always prefer complete type to be the leader. */
1701 else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type)) 1617 else if (!COMPLETE_TYPE_P (val->type) && COMPLETE_TYPE_P (type))
1702 { 1618 {
1703 prevail = true; 1619 prevail = true;
1704 build_bases = TYPE_BINFO (type); 1620 if (TREE_CODE (type) == RECORD_TYPE)
1621 build_bases = TYPE_BINFO (type);
1705 } 1622 }
1706 else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type)) 1623 else if (COMPLETE_TYPE_P (val->type) && !COMPLETE_TYPE_P (type))
1707 ; 1624 ;
1708 else if (TREE_CODE (val->type) == ENUMERAL_TYPE 1625 else if (TREE_CODE (val->type) == ENUMERAL_TYPE
1709 && TREE_CODE (type) == ENUMERAL_TYPE 1626 && TREE_CODE (type) == ENUMERAL_TYPE
1721 if (prevail) 1638 if (prevail)
1722 std::swap (val->type, type); 1639 std::swap (val->type, type);
1723 1640
1724 val->types_set->add (type); 1641 val->types_set->add (type);
1725 1642
1726 /* If we now have a mangled name, be sure to record it to val->type 1643 if (!odr_hash)
1727 so ODR hash can work. */ 1644 return false;
1728 1645
1729 if (can_be_name_hashed_p (type) && !can_be_name_hashed_p (val->type)) 1646 gcc_checking_assert (can_be_name_hashed_p (type)
1730 SET_DECL_ASSEMBLER_NAME (TYPE_NAME (val->type), 1647 && can_be_name_hashed_p (val->type));
1731 DECL_ASSEMBLER_NAME (TYPE_NAME (type)));
1732 1648
1733 bool merge = true; 1649 bool merge = true;
1734 bool base_mismatch = false; 1650 bool base_mismatch = false;
1735 unsigned int i; 1651 unsigned int i;
1736 bool warned = false; 1652 bool warned = false;
1864 The DECL_SOURCE_LOCATIONs in this invocation came from LTO streaming. 1780 The DECL_SOURCE_LOCATIONs in this invocation came from LTO streaming.
1865 We must apply the location cache to ensure that they are valid 1781 We must apply the location cache to ensure that they are valid
1866 before we can pass them to odr_types_equivalent_p (PR lto/83121). */ 1782 before we can pass them to odr_types_equivalent_p (PR lto/83121). */
1867 if (lto_location_cache::current_cache) 1783 if (lto_location_cache::current_cache)
1868 lto_location_cache::current_cache->apply_location_cache (); 1784 lto_location_cache::current_cache->apply_location_cache ();
1869 if (!odr_types_equivalent_p (val->type, type, 1785 /* As a special case we stream mangles names of integer types so we can see
1786 if they are believed to be same even though they have different
1787 representation. Avoid bogus warning on mismatches in these. */
1788 if (TREE_CODE (type) != INTEGER_TYPE
1789 && TREE_CODE (val->type) != INTEGER_TYPE
1790 && !odr_types_equivalent_p (val->type, type,
1870 !flag_ltrans && !val->odr_violated && !warned, 1791 !flag_ltrans && !val->odr_violated && !warned,
1871 &warned, &visited, 1792 &warned, &visited,
1872 DECL_SOURCE_LOCATION (TYPE_NAME (val->type)), 1793 DECL_SOURCE_LOCATION (TYPE_NAME (val->type)),
1873 DECL_SOURCE_LOCATION (TYPE_NAME (type)))) 1794 DECL_SOURCE_LOCATION (TYPE_NAME (type))))
1874 { 1795 {
1969 set_type_binfo (type, master_binfo); 1890 set_type_binfo (type, master_binfo);
1970 } 1891 }
1971 return build_bases; 1892 return build_bases;
1972 } 1893 }
1973 1894
1895 /* REF is OBJ_TYPE_REF, return the class the ref corresponds to. */
1896
1897 tree
1898 obj_type_ref_class (const_tree ref)
1899 {
1900 gcc_checking_assert (TREE_CODE (ref) == OBJ_TYPE_REF);
1901 ref = TREE_TYPE (ref);
1902 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
1903 ref = TREE_TYPE (ref);
1904 /* We look for type THIS points to. ObjC also builds
1905 OBJ_TYPE_REF with non-method calls, Their first parameter
1906 ID however also corresponds to class type. */
1907 gcc_checking_assert (TREE_CODE (ref) == METHOD_TYPE
1908 || TREE_CODE (ref) == FUNCTION_TYPE);
1909 ref = TREE_VALUE (TYPE_ARG_TYPES (ref));
1910 gcc_checking_assert (TREE_CODE (ref) == POINTER_TYPE);
1911 tree ret = TREE_TYPE (ref);
1912 if (!in_lto_p && !TYPE_STRUCTURAL_EQUALITY_P (ret))
1913 ret = TYPE_CANONICAL (ret);
1914 else
1915 ret = get_odr_type (ret)->type;
1916 return ret;
1917 }
1918
1974 /* Get ODR type hash entry for TYPE. If INSERT is true, create 1919 /* Get ODR type hash entry for TYPE. If INSERT is true, create
1975 possibly new entry. */ 1920 possibly new entry. */
1976 1921
1977 odr_type 1922 odr_type
1978 get_odr_type (tree type, bool insert) 1923 get_odr_type (tree type, bool insert)
1979 { 1924 {
1980 odr_type_d **slot = NULL; 1925 odr_type_d **slot = NULL;
1981 odr_type_d **vtable_slot = NULL;
1982 odr_type val = NULL; 1926 odr_type val = NULL;
1983 hashval_t hash; 1927 hashval_t hash;
1984 bool build_bases = false; 1928 bool build_bases = false;
1985 bool insert_to_odr_array = false; 1929 bool insert_to_odr_array = false;
1986 int base_id = -1; 1930 int base_id = -1;
1987 1931
1988 type = main_odr_variant (type); 1932 type = TYPE_MAIN_VARIANT (type);
1989 1933 if (!in_lto_p && !TYPE_STRUCTURAL_EQUALITY_P (type))
1990 gcc_checking_assert (can_be_name_hashed_p (type) 1934 type = TYPE_CANONICAL (type);
1991 || can_be_vtable_hashed_p (type)); 1935
1992 1936 gcc_checking_assert (can_be_name_hashed_p (type));
1993 /* Lookup entry, first try name hash, fallback to vtable hash. */ 1937
1994 if (can_be_name_hashed_p (type)) 1938 hash = hash_odr_name (type);
1995 { 1939 slot = odr_hash->find_slot_with_hash (type, hash,
1996 hash = hash_odr_name (type); 1940 insert ? INSERT : NO_INSERT);
1997 slot = odr_hash->find_slot_with_hash (type, hash, 1941
1998 insert ? INSERT : NO_INSERT); 1942 if (!slot)
1999 }
2000 if ((!slot || !*slot) && in_lto_p && can_be_vtable_hashed_p (type))
2001 {
2002 hash = hash_odr_vtable (type);
2003 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2004 insert ? INSERT : NO_INSERT);
2005 }
2006
2007 if (!slot && !vtable_slot)
2008 return NULL; 1943 return NULL;
2009 1944
2010 /* See if we already have entry for type. */ 1945 /* See if we already have entry for type. */
2011 if ((slot && *slot) || (vtable_slot && *vtable_slot)) 1946 if (*slot)
2012 { 1947 {
2013 if (slot && *slot) 1948 val = *slot;
2014 { 1949
2015 val = *slot; 1950 if (val->type != type && insert
2016 if (flag_checking
2017 && in_lto_p && can_be_vtable_hashed_p (type))
2018 {
2019 hash = hash_odr_vtable (type);
2020 vtable_slot = odr_vtable_hash->find_slot_with_hash (type, hash,
2021 NO_INSERT);
2022 gcc_assert (!vtable_slot || *vtable_slot == *slot);
2023 vtable_slot = NULL;
2024 }
2025 }
2026 else if (*vtable_slot)
2027 val = *vtable_slot;
2028
2029 if (val->type != type
2030 && (!val->types_set || !val->types_set->add (type))) 1951 && (!val->types_set || !val->types_set->add (type)))
2031 { 1952 build_bases = add_type_duplicate (val, type);
2032 gcc_assert (insert);
2033 /* We have type duplicate, but it may introduce vtable name or
2034 mangled name; be sure to keep hashes in sync. */
2035 if (in_lto_p && can_be_vtable_hashed_p (type)
2036 && (!vtable_slot || !*vtable_slot))
2037 {
2038 if (!vtable_slot)
2039 {
2040 hash = hash_odr_vtable (type);
2041 vtable_slot = odr_vtable_hash->find_slot_with_hash
2042 (type, hash, INSERT);
2043 gcc_checking_assert (!*vtable_slot || *vtable_slot == val);
2044 }
2045 *vtable_slot = val;
2046 }
2047 if (slot && !*slot)
2048 *slot = val;
2049 build_bases = add_type_duplicate (val, type);
2050 }
2051 } 1953 }
2052 else 1954 else
2053 { 1955 {
2054 val = ggc_cleared_alloc<odr_type_d> (); 1956 val = ggc_cleared_alloc<odr_type_d> ();
2055 val->type = type; 1957 val->type = type;
2059 val->anonymous_namespace = type_in_anonymous_namespace_p (type); 1961 val->anonymous_namespace = type_in_anonymous_namespace_p (type);
2060 else 1962 else
2061 val->anonymous_namespace = 0; 1963 val->anonymous_namespace = 0;
2062 build_bases = COMPLETE_TYPE_P (val->type); 1964 build_bases = COMPLETE_TYPE_P (val->type);
2063 insert_to_odr_array = true; 1965 insert_to_odr_array = true;
2064 if (slot) 1966 *slot = val;
2065 *slot = val;
2066 if (vtable_slot)
2067 *vtable_slot = val;
2068 } 1967 }
2069 1968
2070 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type) 1969 if (build_bases && TREE_CODE (type) == RECORD_TYPE && TYPE_BINFO (type)
2071 && type_with_linkage_p (type) 1970 && type_with_linkage_p (type)
2072 && type == TYPE_MAIN_VARIANT (type)) 1971 && type == TYPE_MAIN_VARIANT (type))
2077 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type); 1976 gcc_assert (BINFO_TYPE (TYPE_BINFO (val->type)) == type);
2078 1977
2079 val->all_derivations_known = type_all_derivations_known_p (type); 1978 val->all_derivations_known = type_all_derivations_known_p (type);
2080 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++) 1979 for (i = 0; i < BINFO_N_BASE_BINFOS (binfo); i++)
2081 /* For now record only polymorphic types. other are 1980 /* For now record only polymorphic types. other are
2082 pointless for devirtualization and we can not precisely 1981 pointless for devirtualization and we cannot precisely
2083 determine ODR equivalency of these during LTO. */ 1982 determine ODR equivalency of these during LTO. */
2084 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i))) 1983 if (polymorphic_type_binfo_p (BINFO_BASE_BINFO (binfo, i)))
2085 { 1984 {
2086 tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i)); 1985 tree base_type= BINFO_TYPE (BINFO_BASE_BINFO (binfo, i));
2087 odr_type base = get_odr_type (base_type, true); 1986 odr_type base = get_odr_type (base_type, true);
2109 vec_safe_push (odr_types_ptr, val); 2008 vec_safe_push (odr_types_ptr, val);
2110 } 2009 }
2111 return val; 2010 return val;
2112 } 2011 }
2113 2012
2114 /* Add TYPE od ODR type hash. */ 2013 /* Return type that in ODR type hash prevailed TYPE. Be careful and punt
2014 on ODR violations. */
2015
2016 tree
2017 prevailing_odr_type (tree type)
2018 {
2019 odr_type t = get_odr_type (type, false);
2020 if (!t || t->odr_violated)
2021 return type;
2022 return t->type;
2023 }
2024
2025 /* Set tbaa_enabled flag for TYPE. */
2026
2027 void
2028 enable_odr_based_tbaa (tree type)
2029 {
2030 odr_type t = get_odr_type (type, true);
2031 t->tbaa_enabled = true;
2032 }
2033
2034 /* True if canonical type of TYPE is determined using ODR name. */
2035
2036 bool
2037 odr_based_tbaa_p (const_tree type)
2038 {
2039 if (!RECORD_OR_UNION_TYPE_P (type))
2040 return false;
2041 odr_type t = get_odr_type (const_cast <tree> (type), false);
2042 if (!t || !t->tbaa_enabled)
2043 return false;
2044 return true;
2045 }
2046
2047 /* Set TYPE_CANONICAL of type and all its variants and duplicates
2048 to CANONICAL. */
2049
2050 void
2051 set_type_canonical_for_odr_type (tree type, tree canonical)
2052 {
2053 odr_type t = get_odr_type (type, false);
2054 unsigned int i;
2055 tree tt;
2056
2057 for (tree t2 = t->type; t2; t2 = TYPE_NEXT_VARIANT (t2))
2058 TYPE_CANONICAL (t2) = canonical;
2059 if (t->types)
2060 FOR_EACH_VEC_ELT (*t->types, i, tt)
2061 for (tree t2 = tt; t2; t2 = TYPE_NEXT_VARIANT (t2))
2062 TYPE_CANONICAL (t2) = canonical;
2063 }
2064
2065 /* Return true if we reported some ODR violation on TYPE. */
2066
2067 bool
2068 odr_type_violation_reported_p (tree type)
2069 {
2070 return get_odr_type (type, false)->odr_violated;
2071 }
2072
2073 /* Add TYPE of ODR type hash. */
2115 2074
2116 void 2075 void
2117 register_odr_type (tree type) 2076 register_odr_type (tree type)
2118 { 2077 {
2119 if (!odr_hash) 2078 if (!odr_hash)
2120 { 2079 odr_hash = new odr_hash_type (23);
2121 odr_hash = new odr_hash_type (23); 2080 if (type == TYPE_MAIN_VARIANT (type))
2122 if (in_lto_p) 2081 {
2123 odr_vtable_hash = new odr_vtable_hash_type (23); 2082 /* To get ODR warnings right, first register all sub-types. */
2124 } 2083 if (RECORD_OR_UNION_TYPE_P (type)
2125 /* Arrange things to be nicer and insert main variants first. 2084 && COMPLETE_TYPE_P (type))
2126 ??? fundamental prerecorded types do not have mangled names; this 2085 {
2127 makes it possible that non-ODR type is main_odr_variant of ODR type. 2086 /* Limit recursion on types which are already registered. */
2128 Things may get smoother if LTO FE set mangled name of those types same 2087 odr_type ot = get_odr_type (type, false);
2129 way as C++ FE does. */ 2088 if (ot
2130 if (odr_type_p (main_odr_variant (TYPE_MAIN_VARIANT (type))) 2089 && (ot->type == type
2131 && odr_type_p (TYPE_MAIN_VARIANT (type))) 2090 || (ot->types_set
2132 get_odr_type (TYPE_MAIN_VARIANT (type), true); 2091 && ot->types_set->contains (type))))
2133 if (TYPE_MAIN_VARIANT (type) != type && odr_type_p (main_odr_variant (type))) 2092 return;
2134 get_odr_type (type, true); 2093 for (tree f = TYPE_FIELDS (type); f; f = TREE_CHAIN (f))
2094 if (TREE_CODE (f) == FIELD_DECL)
2095 {
2096 tree subtype = TREE_TYPE (f);
2097
2098 while (TREE_CODE (subtype) == ARRAY_TYPE)
2099 subtype = TREE_TYPE (subtype);
2100 if (type_with_linkage_p (TYPE_MAIN_VARIANT (subtype)))
2101 register_odr_type (TYPE_MAIN_VARIANT (subtype));
2102 }
2103 if (TYPE_BINFO (type))
2104 for (unsigned int i = 0;
2105 i < BINFO_N_BASE_BINFOS (TYPE_BINFO (type)); i++)
2106 register_odr_type (BINFO_TYPE (BINFO_BASE_BINFO
2107 (TYPE_BINFO (type), i)));
2108 }
2109 get_odr_type (type, true);
2110 }
2135 } 2111 }
2136 2112
2137 /* Return true if type is known to have no derivations. */ 2113 /* Return true if type is known to have no derivations. */
2138 2114
2139 bool 2115 bool
2156 print_generic_expr (f, t->type, TDF_SLIM); 2132 print_generic_expr (f, t->type, TDF_SLIM);
2157 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":""); 2133 fprintf (f, "%s", t->anonymous_namespace ? " (anonymous namespace)":"");
2158 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":""); 2134 fprintf (f, "%s\n", t->all_derivations_known ? " (derivations known)":"");
2159 if (TYPE_NAME (t->type)) 2135 if (TYPE_NAME (t->type))
2160 { 2136 {
2161 /*fprintf (f, "%*s defined at: %s:%i\n", indent * 2, "",
2162 DECL_SOURCE_FILE (TYPE_NAME (t->type)),
2163 DECL_SOURCE_LINE (TYPE_NAME (t->type)));*/
2164 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type))) 2137 if (DECL_ASSEMBLER_NAME_SET_P (TYPE_NAME (t->type)))
2165 fprintf (f, "%*s mangled name: %s\n", indent * 2, "", 2138 fprintf (f, "%*s mangled name: %s\n", indent * 2, "",
2166 IDENTIFIER_POINTER 2139 IDENTIFIER_POINTER
2167 (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type)))); 2140 (DECL_ASSEMBLER_NAME (TYPE_NAME (t->type))));
2168 } 2141 }
2186 2159
2187 static void 2160 static void
2188 dump_type_inheritance_graph (FILE *f) 2161 dump_type_inheritance_graph (FILE *f)
2189 { 2162 {
2190 unsigned int i; 2163 unsigned int i;
2164 unsigned int num_all_types = 0, num_types = 0, num_duplicates = 0;
2191 if (!odr_types_ptr) 2165 if (!odr_types_ptr)
2192 return; 2166 return;
2193 fprintf (f, "\n\nType inheritance graph:\n"); 2167 fprintf (f, "\n\nType inheritance graph:\n");
2194 for (i = 0; i < odr_types.length (); i++) 2168 for (i = 0; i < odr_types.length (); i++)
2195 { 2169 {
2196 if (odr_types[i] && odr_types[i]->bases.length () == 0) 2170 if (odr_types[i] && odr_types[i]->bases.length () == 0)
2197 dump_odr_type (f, odr_types[i]); 2171 dump_odr_type (f, odr_types[i]);
2198 } 2172 }
2199 for (i = 0; i < odr_types.length (); i++) 2173 for (i = 0; i < odr_types.length (); i++)
2200 { 2174 {
2201 if (odr_types[i] && odr_types[i]->types && odr_types[i]->types->length ()) 2175 if (!odr_types[i])
2202 { 2176 continue;
2203 unsigned int j; 2177
2204 fprintf (f, "Duplicate tree types for odr type %i\n", i); 2178 num_all_types++;
2205 print_node (f, "", odr_types[i]->type, 0); 2179 if (!odr_types[i]->types || !odr_types[i]->types->length ())
2206 for (j = 0; j < odr_types[i]->types->length (); j++) 2180 continue;
2181
2182 /* To aid ODR warnings we also mangle integer constants but do
2183 not consider duplicates there. */
2184 if (TREE_CODE (odr_types[i]->type) == INTEGER_TYPE)
2185 continue;
2186
2187 /* It is normal to have one duplicate and one normal variant. */
2188 if (odr_types[i]->types->length () == 1
2189 && COMPLETE_TYPE_P (odr_types[i]->type)
2190 && !COMPLETE_TYPE_P ((*odr_types[i]->types)[0]))
2191 continue;
2192
2193 num_types ++;
2194
2195 unsigned int j;
2196 fprintf (f, "Duplicate tree types for odr type %i\n", i);
2197 print_node (f, "", odr_types[i]->type, 0);
2198 print_node (f, "", TYPE_NAME (odr_types[i]->type), 0);
2199 putc ('\n',f);
2200 for (j = 0; j < odr_types[i]->types->length (); j++)
2201 {
2202 tree t;
2203 num_duplicates ++;
2204 fprintf (f, "duplicate #%i\n", j);
2205 print_node (f, "", (*odr_types[i]->types)[j], 0);
2206 t = (*odr_types[i]->types)[j];
2207 while (TYPE_P (t) && TYPE_CONTEXT (t))
2207 { 2208 {
2208 tree t; 2209 t = TYPE_CONTEXT (t);
2209 fprintf (f, "duplicate #%i\n", j); 2210 print_node (f, "", t, 0);
2210 print_node (f, "", (*odr_types[i]->types)[j], 0);
2211 t = (*odr_types[i]->types)[j];
2212 while (TYPE_P (t) && TYPE_CONTEXT (t))
2213 {
2214 t = TYPE_CONTEXT (t);
2215 print_node (f, "", t, 0);
2216 }
2217 putc ('\n',f);
2218 } 2211 }
2219 } 2212 print_node (f, "", TYPE_NAME ((*odr_types[i]->types)[j]), 0);
2220 } 2213 putc ('\n',f);
2214 }
2215 }
2216 fprintf (f, "Out of %i types there are %i types with duplicates; "
2217 "%i duplicates overall\n", num_all_types, num_types, num_duplicates);
2218 }
2219
2220 /* Save some WPA->ltrans streaming by freeing stuff needed only for good
2221 ODR warnings.
2222 We free TYPE_VALUES of enums and also make TYPE_DECLs to not point back
2223 to the type (which is needed to keep them in the same SCC and preserve
2224 location information to output warnings) and subsequently we make all
2225 TYPE_DECLS of same assembler name equivalent. */
2226
2227 static void
2228 free_odr_warning_data ()
2229 {
2230 static bool odr_data_freed = false;
2231
2232 if (odr_data_freed || !flag_wpa || !odr_types_ptr)
2233 return;
2234
2235 odr_data_freed = true;
2236
2237 for (unsigned int i = 0; i < odr_types.length (); i++)
2238 if (odr_types[i])
2239 {
2240 tree t = odr_types[i]->type;
2241
2242 if (TREE_CODE (t) == ENUMERAL_TYPE)
2243 TYPE_VALUES (t) = NULL;
2244 TREE_TYPE (TYPE_NAME (t)) = void_type_node;
2245
2246 if (odr_types[i]->types)
2247 for (unsigned int j = 0; j < odr_types[i]->types->length (); j++)
2248 {
2249 tree td = (*odr_types[i]->types)[j];
2250
2251 if (TREE_CODE (td) == ENUMERAL_TYPE)
2252 TYPE_VALUES (td) = NULL;
2253 TYPE_NAME (td) = TYPE_NAME (t);
2254 }
2255 }
2256 odr_data_freed = true;
2221 } 2257 }
2222 2258
2223 /* Initialize IPA devirt and build inheritance tree graph. */ 2259 /* Initialize IPA devirt and build inheritance tree graph. */
2224 2260
2225 void 2261 void
2228 struct symtab_node *n; 2264 struct symtab_node *n;
2229 FILE *inheritance_dump_file; 2265 FILE *inheritance_dump_file;
2230 dump_flags_t flags; 2266 dump_flags_t flags;
2231 2267
2232 if (odr_hash) 2268 if (odr_hash)
2233 return; 2269 {
2270 free_odr_warning_data ();
2271 return;
2272 }
2234 timevar_push (TV_IPA_INHERITANCE); 2273 timevar_push (TV_IPA_INHERITANCE);
2235 inheritance_dump_file = dump_begin (TDI_inheritance, &flags); 2274 inheritance_dump_file = dump_begin (TDI_inheritance, &flags);
2236 odr_hash = new odr_hash_type (23); 2275 odr_hash = new odr_hash_type (23);
2237 if (in_lto_p)
2238 odr_vtable_hash = new odr_vtable_hash_type (23);
2239 2276
2240 /* We reconstruct the graph starting of types of all methods seen in the 2277 /* We reconstruct the graph starting of types of all methods seen in the
2241 unit. */ 2278 unit. */
2242 FOR_EACH_SYMBOL (n) 2279 FOR_EACH_SYMBOL (n)
2243 if (is_a <cgraph_node *> (n) 2280 if (is_a <cgraph_node *> (n)
2273 if (inheritance_dump_file) 2310 if (inheritance_dump_file)
2274 { 2311 {
2275 dump_type_inheritance_graph (inheritance_dump_file); 2312 dump_type_inheritance_graph (inheritance_dump_file);
2276 dump_end (TDI_inheritance, inheritance_dump_file); 2313 dump_end (TDI_inheritance, inheritance_dump_file);
2277 } 2314 }
2315 free_odr_warning_data ();
2278 timevar_pop (TV_IPA_INHERITANCE); 2316 timevar_pop (TV_IPA_INHERITANCE);
2279 } 2317 }
2280 2318
2281 /* Return true if N has reference from live virtual table 2319 /* Return true if N has reference from live virtual table
2282 (and thus can be a destination of polymorphic call). 2320 (and thus can be a destination of polymorphic call).
2330 "__cxa_pure_virtual"); 2368 "__cxa_pure_virtual");
2331 } 2369 }
2332 2370
2333 /* If TARGET has associated node, record it in the NODES array. 2371 /* If TARGET has associated node, record it in the NODES array.
2334 CAN_REFER specify if program can refer to the target directly. 2372 CAN_REFER specify if program can refer to the target directly.
2335 if TARGET is unknown (NULL) or it can not be inserted (for example because 2373 if TARGET is unknown (NULL) or it cannot be inserted (for example because
2336 its body was already removed and there is no way to refer to it), clear 2374 its body was already removed and there is no way to refer to it), clear
2337 COMPLETEP. */ 2375 COMPLETEP. */
2338 2376
2339 static void 2377 static void
2340 maybe_record_node (vec <cgraph_node *> &nodes, 2378 maybe_record_node (vec <cgraph_node *> &nodes,
2402 && (TREE_PUBLIC (target) 2440 && (TREE_PUBLIC (target)
2403 || DECL_EXTERNAL (target) 2441 || DECL_EXTERNAL (target)
2404 || target_node->definition) 2442 || target_node->definition)
2405 && target_node->real_symbol_p ()) 2443 && target_node->real_symbol_p ())
2406 { 2444 {
2407 gcc_assert (!target_node->global.inlined_to); 2445 gcc_assert (!target_node->inlined_to);
2408 gcc_assert (target_node->real_symbol_p ()); 2446 gcc_assert (target_node->real_symbol_p ());
2409 /* When sanitizing, do not assume that __cxa_pure_virtual is not called 2447 /* When sanitizing, do not assume that __cxa_pure_virtual is not called
2410 by valid program. */ 2448 by valid program. */
2411 if (flag_sanitize & SANITIZE_UNREACHABLE) 2449 if (flag_sanitize & SANITIZE_UNREACHABLE)
2412 ; 2450 ;
2431 } 2469 }
2432 } 2470 }
2433 else if (!completep) 2471 else if (!completep)
2434 ; 2472 ;
2435 /* We have definition of __cxa_pure_virtual that is not accessible (it is 2473 /* We have definition of __cxa_pure_virtual that is not accessible (it is
2436 optimized out or partitioned to other unit) so we can not add it. When 2474 optimized out or partitioned to other unit) so we cannot add it. When
2437 not sanitizing, there is nothing to do. 2475 not sanitizing, there is nothing to do.
2438 Otherwise declare the list incomplete. */ 2476 Otherwise declare the list incomplete. */
2439 else if (pure_virtual) 2477 else if (pure_virtual)
2440 { 2478 {
2441 if (flag_sanitize & SANITIZE_UNREACHABLE) 2479 if (flag_sanitize & SANITIZE_UNREACHABLE)
2614 2652
2615 Enumerating all call targets may get expensive when there are many 2653 Enumerating all call targets may get expensive when there are many
2616 polymorphic calls in the program, so we memoize all the previous 2654 polymorphic calls in the program, so we memoize all the previous
2617 queries and avoid duplicated work. */ 2655 queries and avoid duplicated work. */
2618 2656
2619 struct polymorphic_call_target_d 2657 class polymorphic_call_target_d
2620 { 2658 {
2659 public:
2621 HOST_WIDE_INT otr_token; 2660 HOST_WIDE_INT otr_token;
2622 ipa_polymorphic_call_context context; 2661 ipa_polymorphic_call_context context;
2623 odr_type type; 2662 odr_type type;
2624 vec <cgraph_node *> targets; 2663 vec <cgraph_node *> targets;
2625 tree decl_warning; 2664 tree decl_warning;
2626 int type_warning; 2665 int type_warning;
2666 unsigned int n_odr_types;
2627 bool complete; 2667 bool complete;
2628 bool speculative; 2668 bool speculative;
2629 }; 2669 };
2630 2670
2631 /* Polymorphic call target cache helpers. */ 2671 /* Polymorphic call target cache helpers. */
2647 inchash::hash hstate (odr_query->otr_token); 2687 inchash::hash hstate (odr_query->otr_token);
2648 2688
2649 hstate.add_hwi (odr_query->type->id); 2689 hstate.add_hwi (odr_query->type->id);
2650 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type)); 2690 hstate.merge_hash (TYPE_UID (odr_query->context.outer_type));
2651 hstate.add_hwi (odr_query->context.offset); 2691 hstate.add_hwi (odr_query->context.offset);
2692 hstate.add_hwi (odr_query->n_odr_types);
2652 2693
2653 if (odr_query->context.speculative_outer_type) 2694 if (odr_query->context.speculative_outer_type)
2654 { 2695 {
2655 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type)); 2696 hstate.merge_hash (TYPE_UID (odr_query->context.speculative_outer_type));
2656 hstate.add_hwi (odr_query->context.speculative_offset); 2697 hstate.add_hwi (odr_query->context.speculative_offset);
2677 && t1->context.speculative_outer_type == t2->context.speculative_outer_type 2718 && t1->context.speculative_outer_type == t2->context.speculative_outer_type
2678 && t1->context.maybe_in_construction 2719 && t1->context.maybe_in_construction
2679 == t2->context.maybe_in_construction 2720 == t2->context.maybe_in_construction
2680 && t1->context.maybe_derived_type == t2->context.maybe_derived_type 2721 && t1->context.maybe_derived_type == t2->context.maybe_derived_type
2681 && (t1->context.speculative_maybe_derived_type 2722 && (t1->context.speculative_maybe_derived_type
2682 == t2->context.speculative_maybe_derived_type)); 2723 == t2->context.speculative_maybe_derived_type)
2724 /* Adding new type may affect outcome of target search. */
2725 && t1->n_odr_types == t2->n_odr_types);
2683 } 2726 }
2684 2727
2685 /* Remove entry in polymorphic call target cache hash. */ 2728 /* Remove entry in polymorphic call target cache hash. */
2686 2729
2687 inline void 2730 inline void
2719 rebuild_type_inheritance_graph () 2762 rebuild_type_inheritance_graph ()
2720 { 2763 {
2721 if (!odr_hash) 2764 if (!odr_hash)
2722 return; 2765 return;
2723 delete odr_hash; 2766 delete odr_hash;
2724 if (in_lto_p)
2725 delete odr_vtable_hash;
2726 odr_hash = NULL; 2767 odr_hash = NULL;
2727 odr_vtable_hash = NULL;
2728 odr_types_ptr = NULL; 2768 odr_types_ptr = NULL;
2729 free_polymorphic_call_targets_hash (); 2769 free_polymorphic_call_targets_hash ();
2730 } 2770 }
2731 2771
2732 /* When virtual function is removed, we may need to flush the cache. */ 2772 /* When virtual function is removed, we may need to flush the cache. */
2733 2773
2734 static void 2774 static void
2735 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED) 2775 devirt_node_removal_hook (struct cgraph_node *n, void *d ATTRIBUTE_UNUSED)
2736 { 2776 {
2737 if (cached_polymorphic_call_targets 2777 if (cached_polymorphic_call_targets
2778 && !thunk_expansion
2738 && cached_polymorphic_call_targets->contains (n)) 2779 && cached_polymorphic_call_targets->contains (n))
2739 free_polymorphic_call_targets_hash (); 2780 free_polymorphic_call_targets_hash ();
2740 } 2781 }
2741 2782
2742 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */ 2783 /* Look up base of BINFO that has virtual table VTABLE with OFFSET. */
2936 profile_count dyn_count; 2977 profile_count dyn_count;
2937 }; 2978 };
2938 2979
2939 /* Information about type and decl warnings. */ 2980 /* Information about type and decl warnings. */
2940 2981
2941 struct final_warning_record 2982 class final_warning_record
2942 { 2983 {
2984 public:
2943 /* If needed grow type_warnings vector and initialize new decl_warn_count 2985 /* If needed grow type_warnings vector and initialize new decl_warn_count
2944 to have dyn_count set to profile_count::zero (). */ 2986 to have dyn_count set to profile_count::zero (). */
2945 void grow_type_warnings (unsigned newlen); 2987 void grow_type_warnings (unsigned newlen);
2946 2988
2947 profile_count dyn_count; 2989 profile_count dyn_count;
2959 for (unsigned i = len; i < newlen; i++) 3001 for (unsigned i = len; i < newlen; i++)
2960 type_warnings[i].dyn_count = profile_count::zero (); 3002 type_warnings[i].dyn_count = profile_count::zero ();
2961 } 3003 }
2962 } 3004 }
2963 3005
2964 struct final_warning_record *final_warning_records; 3006 class final_warning_record *final_warning_records;
2965 3007
2966 /* Return vector containing possible targets of polymorphic call of type 3008 /* Return vector containing possible targets of polymorphic call of type
2967 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET. 3009 OTR_TYPE calling method OTR_TOKEN within type of OTR_OUTER_TYPE and OFFSET.
2968 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing 3010 If INCLUDE_BASES is true, walk also base types of OUTER_TYPES containing
2969 OTR_TYPE and include their virtual method. This is useful for types 3011 OTR_TYPE and include their virtual method. This is useful for types
2970 possibly in construction or destruction where the virtual table may 3012 possibly in construction or destruction where the virtual table may
2971 temporarily change to one of base types. INCLUDE_DERIVER_TYPES make 3013 temporarily change to one of base types. INCLUDE_DERIVED_TYPES make
2972 us to walk the inheritance graph for all derivations. 3014 us to walk the inheritance graph for all derivations.
2973 3015
2974 If COMPLETEP is non-NULL, store true if the list is complete. 3016 If COMPLETEP is non-NULL, store true if the list is complete.
2975 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry 3017 CACHE_TOKEN (if non-NULL) will get stored to an unique ID of entry
2976 in the target cache. If user needs to visit every target list 3018 in the target cache. If user needs to visit every target list
3083 /* Look up cached answer. */ 3125 /* Look up cached answer. */
3084 key.type = type; 3126 key.type = type;
3085 key.otr_token = otr_token; 3127 key.otr_token = otr_token;
3086 key.speculative = speculative; 3128 key.speculative = speculative;
3087 key.context = context; 3129 key.context = context;
3130 key.n_odr_types = odr_types.length ();
3088 slot = polymorphic_call_target_hash->find_slot (&key, INSERT); 3131 slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
3089 if (cache_token) 3132 if (cache_token)
3090 *cache_token = (void *)*slot; 3133 *cache_token = (void *)*slot;
3091 if (*slot) 3134 if (*slot)
3092 { 3135 {
3299 } 3342 }
3300 } 3343 }
3301 3344
3302 (*slot)->targets = nodes; 3345 (*slot)->targets = nodes;
3303 (*slot)->complete = complete; 3346 (*slot)->complete = complete;
3347 (*slot)->n_odr_types = odr_types.length ();
3304 if (completep) 3348 if (completep)
3305 *completep = complete; 3349 *completep = complete;
3306 3350
3307 timevar_pop (TV_IPA_VIRTUAL_CALL); 3351 timevar_pop (TV_IPA_VIRTUAL_CALL);
3308 return nodes; 3352 return nodes;
3317 } 3361 }
3318 3362
3319 /* Dump target list TARGETS into FILE. */ 3363 /* Dump target list TARGETS into FILE. */
3320 3364
3321 static void 3365 static void
3322 dump_targets (FILE *f, vec <cgraph_node *> targets) 3366 dump_targets (FILE *f, vec <cgraph_node *> targets, bool verbose)
3323 { 3367 {
3324 unsigned int i; 3368 unsigned int i;
3325 3369
3326 for (i = 0; i < targets.length (); i++) 3370 for (i = 0; i < targets.length (); i++)
3327 { 3371 {
3328 char *name = NULL; 3372 char *name = NULL;
3329 if (in_lto_p) 3373 if (in_lto_p)
3330 name = cplus_demangle_v3 (targets[i]->asm_name (), 0); 3374 name = cplus_demangle_v3 (targets[i]->asm_name (), 0);
3331 fprintf (f, " %s/%i", name ? name : targets[i]->name (), 3375 fprintf (f, " %s", name ? name : targets[i]->dump_name ());
3332 targets[i]->order);
3333 if (in_lto_p) 3376 if (in_lto_p)
3334 free (name); 3377 free (name);
3335 if (!targets[i]->definition) 3378 if (!targets[i]->definition)
3336 fprintf (f, " (no definition%s)", 3379 fprintf (f, " (no definition%s)",
3337 DECL_DECLARED_INLINE_P (targets[i]->decl) 3380 DECL_DECLARED_INLINE_P (targets[i]->decl)
3338 ? " inline" : ""); 3381 ? " inline" : "");
3382 /* With many targets for every call polymorphic dumps are going to
3383 be quadratic in size. */
3384 if (i > 10 && !verbose)
3385 {
3386 fprintf (f, " ... and %i more targets\n", targets.length () - i);
3387 return;
3388 }
3339 } 3389 }
3340 fprintf (f, "\n"); 3390 fprintf (f, "\n");
3341 } 3391 }
3342 3392
3343 /* Dump all possible targets of a polymorphic call. */ 3393 /* Dump all possible targets of a polymorphic call. */
3344 3394
3345 void 3395 void
3346 dump_possible_polymorphic_call_targets (FILE *f, 3396 dump_possible_polymorphic_call_targets (FILE *f,
3347 tree otr_type, 3397 tree otr_type,
3348 HOST_WIDE_INT otr_token, 3398 HOST_WIDE_INT otr_token,
3349 const ipa_polymorphic_call_context &ctx) 3399 const ipa_polymorphic_call_context &ctx,
3400 bool verbose)
3350 { 3401 {
3351 vec <cgraph_node *> targets; 3402 vec <cgraph_node *> targets;
3352 bool final; 3403 bool final;
3353 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false); 3404 odr_type type = get_odr_type (TYPE_MAIN_VARIANT (otr_type), false);
3354 unsigned int len; 3405 unsigned int len;
3369 "This is partial list; extra targets may be defined in other units.", 3420 "This is partial list; extra targets may be defined in other units.",
3370 ctx.maybe_in_construction ? " (base types included)" : "", 3421 ctx.maybe_in_construction ? " (base types included)" : "",
3371 ctx.maybe_derived_type ? " (derived types included)" : "", 3422 ctx.maybe_derived_type ? " (derived types included)" : "",
3372 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : ""); 3423 ctx.speculative_maybe_derived_type ? " (speculative derived types included)" : "");
3373 len = targets.length (); 3424 len = targets.length ();
3374 dump_targets (f, targets); 3425 dump_targets (f, targets, verbose);
3375 3426
3376 targets = possible_polymorphic_call_targets (otr_type, otr_token, 3427 targets = possible_polymorphic_call_targets (otr_type, otr_token,
3377 ctx, 3428 ctx,
3378 &final, NULL, true); 3429 &final, NULL, true);
3379 if (targets.length () != len) 3430 if (targets.length () != len)
3380 { 3431 {
3381 fprintf (f, " Speculative targets:"); 3432 fprintf (f, " Speculative targets:");
3382 dump_targets (f, targets); 3433 dump_targets (f, targets, verbose);
3383 } 3434 }
3384 /* Ugly: during callgraph construction the target cache may get populated 3435 /* Ugly: during callgraph construction the target cache may get populated
3385 before all targets are found. While this is harmless (because all local 3436 before all targets are found. While this is harmless (because all local
3386 types are discovered and only in those case we devirtualize fully and we 3437 types are discovered and only in those case we devirtualize fully and we
3387 don't do speculative devirtualization before IPA stage) it triggers 3438 don't do speculative devirtualization before IPA stage) it triggers
3401 const ipa_polymorphic_call_context &ctx, 3452 const ipa_polymorphic_call_context &ctx,
3402 struct cgraph_node *n) 3453 struct cgraph_node *n)
3403 { 3454 {
3404 vec <cgraph_node *> targets; 3455 vec <cgraph_node *> targets;
3405 unsigned int i; 3456 unsigned int i;
3406 enum built_in_function fcode;
3407 bool final; 3457 bool final;
3408 3458
3409 if (TREE_CODE (TREE_TYPE (n->decl)) == FUNCTION_TYPE 3459 if (fndecl_built_in_p (n->decl, BUILT_IN_UNREACHABLE)
3410 && ((fcode = DECL_FUNCTION_CODE (n->decl)) == BUILT_IN_UNREACHABLE 3460 || fndecl_built_in_p (n->decl, BUILT_IN_TRAP))
3411 || fcode == BUILT_IN_TRAP))
3412 return true; 3461 return true;
3413 3462
3414 if (is_cxa_pure_virtual_p (n->decl)) 3463 if (is_cxa_pure_virtual_p (n->decl))
3415 return true; 3464 return true;
3416 3465
3501 } 3550 }
3502 3551
3503 /* Compare type warning records P1 and P2 and choose one with larger count; 3552 /* Compare type warning records P1 and P2 and choose one with larger count;
3504 helper for qsort. */ 3553 helper for qsort. */
3505 3554
3506 int 3555 static int
3507 type_warning_cmp (const void *p1, const void *p2) 3556 type_warning_cmp (const void *p1, const void *p2)
3508 { 3557 {
3509 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1; 3558 const odr_type_warn_count *t1 = (const odr_type_warn_count *)p1;
3510 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2; 3559 const odr_type_warn_count *t2 = (const odr_type_warn_count *)p2;
3511 3560
3517 } 3566 }
3518 3567
3519 /* Compare decl warning records P1 and P2 and choose one with larger count; 3568 /* Compare decl warning records P1 and P2 and choose one with larger count;
3520 helper for qsort. */ 3569 helper for qsort. */
3521 3570
3522 int 3571 static int
3523 decl_warning_cmp (const void *p1, const void *p2) 3572 decl_warning_cmp (const void *p1, const void *p2)
3524 { 3573 {
3525 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1; 3574 const decl_warn_count *t1 = *(const decl_warn_count * const *)p1;
3526 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2; 3575 const decl_warn_count *t2 = *(const decl_warn_count * const *)p2;
3527 3576
3631 if (warn_suggest_final_methods || warn_suggest_final_types) 3680 if (warn_suggest_final_methods || warn_suggest_final_types)
3632 possible_polymorphic_call_targets (e); 3681 possible_polymorphic_call_targets (e);
3633 3682
3634 if (dump_file) 3683 if (dump_file)
3635 dump_possible_polymorphic_call_targets 3684 dump_possible_polymorphic_call_targets
3636 (dump_file, e); 3685 (dump_file, e, (dump_flags & TDF_DETAILS));
3637 3686
3638 npolymorphic++; 3687 npolymorphic++;
3639 3688
3640 /* See if the call can be devirtualized by means of ipa-prop's 3689 /* See if the call can be devirtualized by means of ipa-prop's
3641 polymorphic call context propagation. If not, we can just 3690 polymorphic call context propagation. If not, we can just
3643 lifting in remove_unreachable_nodes that will otherwise try to 3692 lifting in remove_unreachable_nodes that will otherwise try to
3644 keep all possible targets alive until inlining and in the inliner 3693 keep all possible targets alive until inlining and in the inliner
3645 itself. 3694 itself.
3646 3695
3647 This may need to be revisited once we add further ways to use 3696 This may need to be revisited once we add further ways to use
3648 the may edges, but it is a resonable thing to do right now. */ 3697 the may edges, but it is a reasonable thing to do right now. */
3649 3698
3650 if ((e->indirect_info->param_index == -1 3699 if ((e->indirect_info->param_index == -1
3651 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively) 3700 || (!opt_for_fn (n->decl, flag_devirtualize_speculatively)
3652 && e->indirect_info->vptr_changed)) 3701 && e->indirect_info->vptr_changed))
3653 && !flag_ltrans_devirtualize) 3702 && !flag_ltrans_devirtualize)
3654 { 3703 {
3655 e->indirect_info->polymorphic = false; 3704 e->indirect_info->polymorphic = false;
3656 ndropped++; 3705 ndropped++;
3657 if (dump_file) 3706 if (dump_file)
3658 fprintf (dump_file, "Dropping polymorphic call info;" 3707 fprintf (dump_file, "Dropping polymorphic call info;"
3659 " it can not be used by ipa-prop\n"); 3708 " it cannot be used by ipa-prop\n");
3660 } 3709 }
3661 3710
3662 if (!opt_for_fn (n->decl, flag_devirtualize_speculatively)) 3711 if (!opt_for_fn (n->decl, flag_devirtualize_speculatively))
3663 continue; 3712 continue;
3664 3713
3706 } 3755 }
3707 /* This is reached only when dumping; check if we agree or disagree 3756 /* This is reached only when dumping; check if we agree or disagree
3708 with the speculation. */ 3757 with the speculation. */
3709 if (e->speculative) 3758 if (e->speculative)
3710 { 3759 {
3711 struct cgraph_edge *e2; 3760 bool found = e->speculative_call_for_target (likely_target);
3712 struct ipa_ref *ref; 3761 if (found)
3713 e->speculative_call_info (e2, e, ref);
3714 if (e2->callee->ultimate_alias_target ()
3715 == likely_target->ultimate_alias_target ())
3716 { 3762 {
3717 fprintf (dump_file, "We agree with speculation\n\n"); 3763 fprintf (dump_file, "We agree with speculation\n\n");
3718 nok++; 3764 nok++;
3719 } 3765 }
3720 else 3766 else
3952 make_pass_ipa_devirt (gcc::context *ctxt) 3998 make_pass_ipa_devirt (gcc::context *ctxt)
3953 { 3999 {
3954 return new pass_ipa_devirt (ctxt); 4000 return new pass_ipa_devirt (ctxt);
3955 } 4001 }
3956 4002
4003 /* Print ODR name of a TYPE if available.
4004 Use demangler when option DEMANGLE is used. */
4005
4006 DEBUG_FUNCTION void
4007 debug_tree_odr_name (tree type, bool demangle)
4008 {
4009 const char *odr = get_odr_name_for_type (type);
4010 if (demangle)
4011 {
4012 const int opts = DMGL_PARAMS | DMGL_ANSI | DMGL_TYPES;
4013 odr = cplus_demangle (odr, opts);
4014 }
4015
4016 fprintf (stderr, "%s\n", odr);
4017 }
4018
3957 #include "gt-ipa-devirt.h" 4019 #include "gt-ipa-devirt.h"