annotate gcc/ipa-devirt.c @ 127:4c56639505ff

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