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