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