Line data Source code
1 : /* Analysis of polymorphic call context.
2 : Copyright (C) 2013-2026 Free Software Foundation, Inc.
3 : Contributed by Jan Hubicka
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "rtl.h"
26 : #include "tree-core.h"
27 : #include "tree.h"
28 : #include "gimple.h"
29 : #include "tree-pass.h"
30 : #include "tree-ssa-operands.h"
31 : #include "streamer-hooks.h"
32 : #include "cgraph.h"
33 : #include "data-streamer.h"
34 : #include "diagnostic.h"
35 : #include "alias.h"
36 : #include "fold-const.h"
37 : #include "calls.h"
38 : #include "ipa-utils.h"
39 : #include "tree-dfa.h"
40 : #include "gimple-pretty-print.h"
41 : #include "tree-into-ssa.h"
42 : #include "alloc-pool.h"
43 : #include "symbol-summary.h"
44 : #include "symtab-thunks.h"
45 : #include "attr-callback.h"
46 : #include "callback-info.h"
47 :
48 : /* Return true when TYPE contains an polymorphic type and thus is interesting
49 : for devirtualization machinery. */
50 :
51 : static bool contains_type_p (tree, HOST_WIDE_INT, tree,
52 : bool consider_placement_new = true,
53 : bool consider_bases = true);
54 :
55 : bool
56 37919530 : contains_polymorphic_type_p (const_tree type)
57 : {
58 43192152 : type = TYPE_MAIN_VARIANT (type);
59 :
60 43192152 : if (RECORD_OR_UNION_TYPE_P (type))
61 : {
62 10676937 : if (TYPE_BINFO (type)
63 10676937 : && polymorphic_type_binfo_p (TYPE_BINFO (type)))
64 : return true;
65 499985606 : for (tree fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
66 491059842 : if (TREE_CODE (fld) == FIELD_DECL
67 33679716 : && !DECL_ARTIFICIAL (fld)
68 521857082 : && contains_polymorphic_type_p (TREE_TYPE (fld)))
69 : return true;
70 : return false;
71 : }
72 32515215 : if (TREE_CODE (type) == ARRAY_TYPE)
73 5272622 : return contains_polymorphic_type_p (TREE_TYPE (type));
74 : return false;
75 : }
76 :
77 : /* Return true if it seems valid to use placement new to build EXPECTED_TYPE
78 : at position CUR_OFFSET within TYPE.
79 :
80 : POD can be changed to an instance of a polymorphic type by
81 : placement new. Here we play safe and assume that any
82 : non-polymorphic type is POD. */
83 : bool
84 139818 : possible_placement_new (tree type, tree expected_type,
85 : HOST_WIDE_INT cur_offset)
86 : {
87 139818 : if (cur_offset < 0)
88 : return true;
89 136265 : return ((TREE_CODE (type) != RECORD_TYPE
90 20427 : || !TYPE_BINFO (type)
91 20460 : || cur_offset >= POINTER_SIZE
92 18063 : || !polymorphic_type_binfo_p (TYPE_BINFO (type)))
93 156456 : && (!TYPE_SIZE (type)
94 136029 : || !tree_fits_shwi_p (TYPE_SIZE (type))
95 272058 : || (cur_offset
96 136029 : + (expected_type ? tree_to_uhwi (TYPE_SIZE (expected_type))
97 126324 : : POINTER_SIZE)
98 136029 : <= tree_to_uhwi (TYPE_SIZE (type)))));
99 : }
100 :
101 : /* THIS->OUTER_TYPE is a type of memory object where object of OTR_TYPE
102 : is contained at THIS->OFFSET. Walk the memory representation of
103 : THIS->OUTER_TYPE and find the outermost class type that match
104 : OTR_TYPE or contain OTR_TYPE as a base. Update THIS
105 : to represent it.
106 :
107 : If OTR_TYPE is NULL, just find outermost polymorphic type with
108 : virtual table present at position OFFSET.
109 :
110 : For example when THIS represents type
111 : class A
112 : {
113 : int a;
114 : class B b;
115 : }
116 : and we look for type at offset sizeof(int), we end up with B and offset 0.
117 : If the same is produced by multiple inheritance, we end up with A and offset
118 : sizeof(int).
119 :
120 : If we cannot find corresponding class, give up by setting
121 : THIS->OUTER_TYPE to OTR_TYPE and THIS->OFFSET to NULL.
122 : Return true when lookup was successful.
123 :
124 : When CONSIDER_PLACEMENT_NEW is false, reject contexts that may be made
125 : valid only via allocation of new polymorphic type inside by means
126 : of placement new.
127 :
128 : When CONSIDER_BASES is false, only look for actual fields, not base types
129 : of TYPE. */
130 :
131 : bool
132 2254874 : ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type,
133 : bool consider_placement_new,
134 : bool consider_bases)
135 : {
136 2254874 : tree type = outer_type;
137 2254874 : HOST_WIDE_INT cur_offset = offset;
138 2254874 : bool speculative = false;
139 2254874 : bool size_unknown = false;
140 2254874 : unsigned HOST_WIDE_INT otr_type_size = POINTER_SIZE;
141 :
142 : /* Update OUTER_TYPE to match EXPECTED_TYPE if it is not set. */
143 2254874 : if (!outer_type)
144 : {
145 90635 : clear_outer_type (otr_type);
146 90635 : type = otr_type;
147 90635 : cur_offset = 0;
148 : }
149 : /* See if OFFSET points inside OUTER_TYPE. If it does not, we know
150 : that the context is either invalid, or the instance type must be
151 : derived from OUTER_TYPE.
152 :
153 : Because the instance type may contain field whose type is of OUTER_TYPE,
154 : we cannot derive any effective information about it.
155 :
156 : TODO: In the case we know all derived types, we can definitely do better
157 : here. */
158 2164239 : else if (TYPE_SIZE (outer_type)
159 2164216 : && tree_fits_shwi_p (TYPE_SIZE (outer_type))
160 2164216 : && tree_to_shwi (TYPE_SIZE (outer_type)) >= 0
161 4328455 : && tree_to_shwi (TYPE_SIZE (outer_type)) <= offset)
162 : {
163 4063 : bool der = maybe_derived_type; /* clear_outer_type will reset it. */
164 4063 : bool dyn = dynamic;
165 4063 : clear_outer_type (otr_type);
166 4063 : type = otr_type;
167 4063 : cur_offset = 0;
168 :
169 : /* If derived type is not allowed, we know that the context is invalid.
170 : For dynamic types, we really do not have information about
171 : size of the memory location. It is possible that completely
172 : different type is stored after outer_type. */
173 4063 : if (!der && !dyn)
174 : {
175 2640 : clear_speculation ();
176 2640 : invalid = true;
177 2640 : return false;
178 : }
179 : }
180 :
181 2120242 : if (otr_type && TYPE_SIZE (otr_type)
182 4372471 : && tree_fits_shwi_p (TYPE_SIZE (otr_type)))
183 2120237 : otr_type_size = tree_to_uhwi (TYPE_SIZE (otr_type));
184 :
185 2252234 : if (!type || offset < 0)
186 3553 : goto no_useful_type_info;
187 :
188 : /* Find the sub-object the constant actually refers to and mark whether it is
189 : an artificial one (as opposed to a user-defined one).
190 :
191 : This loop is performed twice; first time for outer_type and second time
192 : for speculative_outer_type. The second run has SPECULATIVE set. */
193 2915402 : while (true)
194 : {
195 2915402 : unsigned HOST_WIDE_INT pos, size;
196 2915402 : tree fld;
197 :
198 : /* If we do not know size of TYPE, we need to be more conservative
199 : about accepting cases where we cannot find EXPECTED_TYPE.
200 : Generally the types that do matter here are of constant size.
201 : Size_unknown case should be very rare. */
202 2915402 : if (TYPE_SIZE (type)
203 2915379 : && tree_fits_shwi_p (TYPE_SIZE (type))
204 5830781 : && tree_to_shwi (TYPE_SIZE (type)) >= 0)
205 : size_unknown = false;
206 : else
207 : size_unknown = true;
208 :
209 : /* On a match, just return what we found. */
210 2915402 : if ((otr_type
211 2470932 : && types_odr_comparable (type, otr_type)
212 2470932 : && types_same_for_odr (type, otr_type))
213 2915402 : || (!otr_type
214 444470 : && TREE_CODE (type) == RECORD_TYPE
215 338028 : && TYPE_BINFO (type)
216 337193 : && polymorphic_type_binfo_p (TYPE_BINFO (type))))
217 : {
218 1723597 : if (speculative)
219 : {
220 : /* If we did not match the offset, just give up on speculation. */
221 10183 : if (cur_offset != 0
222 : /* Also check if speculation did not end up being same as
223 : non-speculation. */
224 10183 : || (types_must_be_same_for_odr (speculative_outer_type,
225 : outer_type)
226 38 : && (maybe_derived_type
227 38 : == speculative_maybe_derived_type)))
228 0 : clear_speculation ();
229 : return true;
230 : }
231 : else
232 : {
233 : /* If type is known to be final, do not worry about derived
234 : types. Testing it here may help us to avoid speculation. */
235 1709252 : if (otr_type && TREE_CODE (outer_type) == RECORD_TYPE
236 1709252 : && (!in_lto_p || odr_type_p (outer_type))
237 1709252 : && type_with_linkage_p (outer_type)
238 3422666 : && type_known_to_have_no_derivations_p (outer_type))
239 8728 : maybe_derived_type = false;
240 :
241 : /* Type cannot contain itself on an non-zero offset. In that case
242 : just give up. Still accept the case where size is now known.
243 : Either the second copy may appear past the end of type or within
244 : the non-POD buffer located inside the variably sized type
245 : itself. */
246 1713414 : if (cur_offset != 0)
247 2016 : goto no_useful_type_info;
248 : /* If we determined type precisely or we have no clue on
249 : speculation, we are done. */
250 1328883 : if (!maybe_derived_type || !speculative_outer_type
251 1803510 : || !speculation_consistent_p (speculative_outer_type,
252 : speculative_offset,
253 : speculative_maybe_derived_type,
254 : otr_type))
255 : {
256 1701219 : clear_speculation ();
257 1701219 : return true;
258 : }
259 : /* Otherwise look into speculation now. */
260 : else
261 : {
262 10179 : speculative = true;
263 10179 : type = speculative_outer_type;
264 10179 : cur_offset = speculative_offset;
265 10179 : continue;
266 : }
267 : }
268 : }
269 :
270 : /* Walk fields and find corresponding on at OFFSET. */
271 1191805 : if (TREE_CODE (type) == RECORD_TYPE)
272 : {
273 17453944 : for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
274 : {
275 32605437 : if (TREE_CODE (fld) != FIELD_DECL
276 17175928 : || TREE_TYPE (fld) == error_mark_node)
277 15429509 : continue;
278 :
279 1746419 : pos = int_bit_position (fld);
280 1746419 : if (pos > (unsigned HOST_WIDE_INT)cur_offset)
281 438594 : continue;
282 :
283 : /* Do not consider vptr itself. Not even for placement new. */
284 1272629 : if (!pos && DECL_ARTIFICIAL (fld)
285 1008802 : && POINTER_TYPE_P (TREE_TYPE (fld))
286 184715 : && TYPE_BINFO (type)
287 1492540 : && polymorphic_type_binfo_p (TYPE_BINFO (type)))
288 184715 : continue;
289 :
290 1123110 : if (!DECL_SIZE (fld) || !tree_fits_uhwi_p (DECL_SIZE (fld)))
291 0 : goto no_useful_type_info;
292 1123110 : size = tree_to_uhwi (DECL_SIZE (fld));
293 :
294 : /* We can always skip types smaller than pointer size:
295 : those cannot contain a virtual table pointer.
296 :
297 : Disqualifying fields that are too small to fit OTR_TYPE
298 : saves work needed to walk them for no benefit.
299 : Because of the way the bases are packed into a class, the
300 : field's size may be smaller than type size, so it needs
301 : to be done with a care. */
302 :
303 1123110 : if (pos <= (unsigned HOST_WIDE_INT)cur_offset
304 2246220 : && (pos + size) >= (unsigned HOST_WIDE_INT)cur_offset
305 1145385 : + POINTER_SIZE
306 873167 : && (!otr_type
307 557167 : || !TYPE_SIZE (TREE_TYPE (fld))
308 557167 : || !tree_fits_shwi_p (TYPE_SIZE (TREE_TYPE (fld)))
309 557167 : || (pos + tree_to_uhwi (TYPE_SIZE (TREE_TYPE (fld))))
310 557167 : >= cur_offset + otr_type_size))
311 : break;
312 : }
313 :
314 797702 : if (!fld)
315 278016 : goto no_useful_type_info;
316 :
317 797702 : type = TYPE_MAIN_VARIANT (TREE_TYPE (fld));
318 797702 : cur_offset -= pos;
319 : /* DECL_ARTIFICIAL represents a basetype. */
320 797702 : if (!DECL_ARTIFICIAL (fld))
321 : {
322 268997 : if (!speculative)
323 : {
324 268602 : outer_type = type;
325 268602 : offset = cur_offset;
326 : /* As soon as we see an field containing the type,
327 : we know we are not looking for derivations. */
328 268602 : maybe_derived_type = false;
329 : }
330 : else
331 : {
332 395 : speculative_outer_type = type;
333 395 : speculative_offset = cur_offset;
334 395 : speculative_maybe_derived_type = false;
335 : }
336 : }
337 528705 : else if (!consider_bases)
338 141190 : goto no_useful_type_info;
339 : }
340 116087 : else if (TREE_CODE (type) == ARRAY_TYPE)
341 : {
342 9496 : tree subtype = TYPE_MAIN_VARIANT (TREE_TYPE (type));
343 :
344 : /* Give up if we don't know array field size.
345 : Also give up on non-polymorphic types as they are used
346 : as buffers for placement new. */
347 9496 : if (!TYPE_SIZE (subtype)
348 9496 : || !tree_fits_shwi_p (TYPE_SIZE (subtype))
349 9496 : || tree_to_shwi (TYPE_SIZE (subtype)) <= 0
350 18992 : || !contains_polymorphic_type_p (subtype))
351 9083 : goto no_useful_type_info;
352 :
353 413 : HOST_WIDE_INT new_offset = cur_offset % tree_to_shwi (TYPE_SIZE (subtype));
354 :
355 : /* We may see buffer for placement new. In this case the expected type
356 : can be bigger than the subtype. */
357 413 : if (TYPE_SIZE (subtype)
358 413 : && (cur_offset + otr_type_size
359 413 : > tree_to_uhwi (TYPE_SIZE (subtype))))
360 387 : goto no_useful_type_info;
361 :
362 26 : cur_offset = new_offset;
363 26 : type = TYPE_MAIN_VARIANT (subtype);
364 26 : if (!speculative)
365 : {
366 26 : outer_type = type;
367 26 : offset = cur_offset;
368 26 : maybe_derived_type = false;
369 : }
370 : else
371 : {
372 0 : speculative_outer_type = type;
373 0 : speculative_offset = cur_offset;
374 0 : speculative_maybe_derived_type = false;
375 : }
376 : }
377 : /* Give up on anything else. */
378 : else
379 : {
380 106591 : no_useful_type_info:
381 336 : if (maybe_derived_type && !speculative
382 336 : && TREE_CODE (outer_type) == RECORD_TYPE
383 336 : && TREE_CODE (otr_type) == RECORD_TYPE
384 336 : && TYPE_BINFO (otr_type)
385 336 : && !offset
386 541172 : && get_binfo_at_offset (TYPE_BINFO (otr_type), 0, outer_type))
387 : {
388 336 : clear_outer_type (otr_type);
389 336 : if (!speculative_outer_type
390 336 : || !speculation_consistent_p (speculative_outer_type,
391 : speculative_offset,
392 0 : speculative_maybe_derived_type,
393 : otr_type))
394 336 : clear_speculation ();
395 336 : if (speculative_outer_type)
396 : {
397 0 : speculative = true;
398 0 : type = speculative_outer_type;
399 0 : cur_offset = speculative_offset;
400 : }
401 : else
402 : return true;
403 : }
404 : /* We found no way to embed EXPECTED_TYPE in TYPE.
405 : We still permit two special cases - placement new and
406 : the case of variadic types containing themselves. */
407 540500 : if (!speculative
408 540500 : && consider_placement_new
409 540500 : && (size_unknown || !type || maybe_derived_type
410 139818 : || possible_placement_new (type, otr_type, cur_offset)))
411 : {
412 : /* In these weird cases we want to accept the context.
413 : In non-speculative run we have no useful outer_type info
414 : (TODO: we may eventually want to record upper bound on the
415 : type size that can be used to prune the walk),
416 : but we still want to consider speculation that may
417 : give useful info. */
418 138583 : if (!speculative)
419 : {
420 138583 : clear_outer_type (otr_type);
421 138583 : if (!speculative_outer_type
422 138589 : || !speculation_consistent_p (speculative_outer_type,
423 : speculative_offset,
424 6 : speculative_maybe_derived_type,
425 : otr_type))
426 138579 : clear_speculation ();
427 138583 : if (speculative_outer_type)
428 : {
429 4 : speculative = true;
430 4 : type = speculative_outer_type;
431 4 : cur_offset = speculative_offset;
432 : }
433 : else
434 : return true;
435 : }
436 : else
437 : {
438 : clear_speculation ();
439 : return true;
440 : }
441 : }
442 : else
443 : {
444 401917 : clear_speculation ();
445 401917 : if (speculative)
446 : return true;
447 401917 : clear_outer_type (otr_type);
448 401917 : invalid = true;
449 401917 : return false;
450 : }
451 : }
452 : }
453 : }
454 :
455 : /* Return true if OUTER_TYPE contains OTR_TYPE at OFFSET.
456 : CONSIDER_PLACEMENT_NEW makes function to accept cases where OTR_TYPE can
457 : be built within OUTER_TYPE by means of placement new. CONSIDER_BASES makes
458 : function to accept cases where OTR_TYPE appears as base of OUTER_TYPE or as
459 : base of one of fields of OUTER_TYPE. */
460 :
461 : static bool
462 741706 : contains_type_p (tree outer_type, HOST_WIDE_INT offset,
463 : tree otr_type,
464 : bool consider_placement_new,
465 : bool consider_bases)
466 : {
467 741706 : ipa_polymorphic_call_context context;
468 :
469 : /* Check that type is within range. */
470 741706 : if (offset < 0)
471 : return false;
472 :
473 : /* PR ipa/71207
474 : As OUTER_TYPE can be a type which has a diamond virtual inheritance,
475 : it's not necessary that INNER_TYPE will fit within OUTER_TYPE with
476 : a given offset. It can happen that INNER_TYPE also contains a base object,
477 : however it would point to the same instance in the OUTER_TYPE. */
478 :
479 735234 : context.offset = offset;
480 735234 : context.outer_type = TYPE_MAIN_VARIANT (outer_type);
481 735234 : context.maybe_derived_type = false;
482 735234 : context.dynamic = false;
483 735234 : return context.restrict_to_inner_class (otr_type, consider_placement_new,
484 735234 : consider_bases);
485 : }
486 :
487 :
488 : /* Return a FUNCTION_DECL if FN represent a constructor or destructor.
489 : If CHECK_CLONES is true, also check for clones of ctor/dtors. */
490 :
491 : tree
492 70938755 : polymorphic_ctor_dtor_p (tree fn, bool check_clones)
493 : {
494 70938755 : if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
495 117447568 : || (!DECL_CXX_CONSTRUCTOR_P (fn) && !DECL_CXX_DESTRUCTOR_P (fn)))
496 : {
497 50937314 : if (!check_clones)
498 : return NULL_TREE;
499 :
500 : /* Watch for clones where we constant propagated the first
501 : argument (pointer to the instance). */
502 50879426 : fn = DECL_ABSTRACT_ORIGIN (fn);
503 50879426 : if (!fn
504 37602177 : || TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
505 74258870 : || (!DECL_CXX_CONSTRUCTOR_P (fn) && !DECL_CXX_DESTRUCTOR_P (fn)))
506 50879045 : return NULL_TREE;
507 : }
508 :
509 : /* We used to check that the ctor/dtor is not pure/const.
510 : However this may interact with pass ordering. It is possible that the
511 : store of vtable is optimized out in offline copy, but inline copies keep
512 : it and then local-pure-const overwrites the flag. See PR120098. */
513 :
514 : return fn;
515 : }
516 :
517 : /* Return a FUNCTION_DECL if BLOCK represents a constructor or destructor.
518 : If CHECK_CLONES is true, also check for clones of ctor/dtors. */
519 :
520 : tree
521 192449114 : inlined_polymorphic_ctor_dtor_block_p (tree block, bool check_clones)
522 : {
523 192449114 : tree fn = block_ultimate_origin (block);
524 192449114 : if (fn == NULL || TREE_CODE (fn) != FUNCTION_DECL)
525 : return NULL_TREE;
526 :
527 58913298 : return polymorphic_ctor_dtor_p (fn, check_clones);
528 : }
529 :
530 :
531 : /* We know that the instance is stored in variable or parameter
532 : (not dynamically allocated) and we want to disprove the fact
533 : that it may be in construction at invocation of CALL.
534 :
535 : BASE represents memory location where instance is stored.
536 : If BASE is NULL, it is assumed to be global memory.
537 : OUTER_TYPE is known type of the instance or NULL if not
538 : known.
539 :
540 : For the variable to be in construction we actually need to
541 : be in constructor of corresponding global variable or
542 : the inline stack of CALL must contain the constructor.
543 : Check this condition. This check works safely only before
544 : IPA passes, because inline stacks may become out of date
545 : later. */
546 :
547 : bool
548 30907072 : decl_maybe_in_construction_p (tree base, tree outer_type,
549 : gimple *call, tree function)
550 : {
551 30907072 : if (outer_type)
552 62100 : outer_type = TYPE_MAIN_VARIANT (outer_type);
553 30907072 : gcc_assert (!base || DECL_P (base));
554 :
555 : /* After inlining the code unification optimizations may invalidate
556 : inline stacks. Also we need to give up on global variables after
557 : IPA, because addresses of these may have been propagated to their
558 : constructors. */
559 30907072 : if (DECL_STRUCT_FUNCTION (function)->after_inlining)
560 : return true;
561 :
562 : /* Pure functions cannot do any changes on the dynamic type;
563 : that require writing to memory. */
564 52383 : if ((!base || !auto_var_in_fn_p (base, function))
565 30882251 : && flags_from_decl_or_type (function) & (ECF_PURE | ECF_CONST))
566 : return false;
567 :
568 30527092 : bool check_clones = !base || is_global_var (base);
569 160693714 : for (tree block = gimple_block (call); block && TREE_CODE (block) == BLOCK;
570 65083311 : block = BLOCK_SUPERCONTEXT (block))
571 65113347 : if (tree fn = inlined_polymorphic_ctor_dtor_block_p (block, check_clones))
572 : {
573 383168 : tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
574 :
575 383168 : if (!outer_type || !types_odr_comparable (type, outer_type))
576 : {
577 373697 : if (TREE_CODE (type) == RECORD_TYPE
578 372253 : && TYPE_BINFO (type)
579 743350 : && polymorphic_type_binfo_p (TYPE_BINFO (type)))
580 : return true;
581 : }
582 9471 : else if (types_same_for_odr (type, outer_type))
583 : return true;
584 : }
585 :
586 30497056 : if (!base || (VAR_P (base) && is_global_var (base)))
587 : {
588 30469043 : if (TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
589 39062794 : || (!DECL_CXX_CONSTRUCTOR_P (function)
590 7261102 : && !DECL_CXX_DESTRUCTOR_P (function)))
591 : {
592 28612852 : if (!DECL_ABSTRACT_ORIGIN (function))
593 : return false;
594 : /* Watch for clones where we constant propagated the first
595 : argument (pointer to the instance). */
596 96598 : function = DECL_ABSTRACT_ORIGIN (function);
597 96598 : if (!function
598 96598 : || TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
599 154051 : || (!DECL_CXX_CONSTRUCTOR_P (function)
600 57421 : && !DECL_CXX_DESTRUCTOR_P (function)))
601 : return false;
602 : }
603 1856223 : tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (function));
604 1856223 : if (!outer_type || !types_odr_comparable (type, outer_type))
605 : {
606 1855942 : if (TREE_CODE (type) == RECORD_TYPE
607 1854387 : && TYPE_BINFO (type)
608 3700665 : && polymorphic_type_binfo_p (TYPE_BINFO (type)))
609 : return true;
610 : }
611 281 : else if (types_same_for_odr (type, outer_type))
612 : return true;
613 : }
614 : return false;
615 : }
616 :
617 : /* Dump human readable context to F. If NEWLINE is true, it will be terminated
618 : by a newline. */
619 :
620 : void
621 1417 : ipa_polymorphic_call_context::dump (FILE *f, bool newline) const
622 : {
623 1417 : fprintf (f, " ");
624 1417 : if (invalid)
625 0 : fprintf (f, "Call is known to be undefined");
626 : else
627 : {
628 1417 : if (useless_p ())
629 1 : fprintf (f, "nothing known");
630 1417 : if (outer_type || offset)
631 : {
632 1624 : fprintf (f, "Outer type%s:", dynamic ? " (dynamic)" : "");
633 1259 : print_generic_expr (f, outer_type, TDF_SLIM);
634 1259 : if (maybe_derived_type)
635 737 : fprintf (f, " (or a derived type)");
636 1259 : if (maybe_in_construction)
637 190 : fprintf (f, " (maybe in construction)");
638 1259 : fprintf (f, " offset " HOST_WIDE_INT_PRINT_DEC,
639 1259 : offset);
640 : }
641 1417 : if (speculative_outer_type)
642 : {
643 300 : if (outer_type || offset)
644 143 : fprintf (f, " ");
645 300 : fprintf (f, "Speculative outer type:");
646 300 : print_generic_expr (f, speculative_outer_type, TDF_SLIM);
647 300 : if (speculative_maybe_derived_type)
648 267 : fprintf (f, " (or a derived type)");
649 300 : fprintf (f, " at offset " HOST_WIDE_INT_PRINT_DEC,
650 300 : speculative_offset);
651 : }
652 : }
653 1417 : if (newline)
654 691 : fprintf(f, "\n");
655 1417 : }
656 :
657 : /* Print context to stderr. */
658 :
659 : void
660 0 : ipa_polymorphic_call_context::debug () const
661 : {
662 0 : dump (stderr);
663 0 : }
664 :
665 : /* Stream out the context to OB. */
666 :
667 : void
668 4859 : ipa_polymorphic_call_context::stream_out (struct output_block *ob) const
669 : {
670 4859 : struct bitpack_d bp = bitpack_create (ob->main_stream);
671 :
672 4859 : bp_pack_value (&bp, invalid, 1);
673 4859 : bp_pack_value (&bp, maybe_in_construction, 1);
674 4859 : bp_pack_value (&bp, maybe_derived_type, 1);
675 4859 : bp_pack_value (&bp, speculative_maybe_derived_type, 1);
676 4859 : bp_pack_value (&bp, dynamic, 1);
677 4859 : bp_pack_value (&bp, outer_type != NULL, 1);
678 4859 : bp_pack_value (&bp, offset != 0, 1);
679 4859 : bp_pack_value (&bp, speculative_outer_type != NULL, 1);
680 4859 : streamer_write_bitpack (&bp);
681 :
682 4859 : if (outer_type != NULL)
683 2367 : stream_write_tree (ob, outer_type, true);
684 4859 : if (offset)
685 563 : streamer_write_hwi (ob, offset);
686 4859 : if (speculative_outer_type != NULL)
687 : {
688 1737 : stream_write_tree (ob, speculative_outer_type, true);
689 1737 : streamer_write_hwi (ob, speculative_offset);
690 : }
691 : else
692 3122 : gcc_assert (!speculative_offset);
693 4859 : }
694 :
695 : /* Stream in the context from IB and DATA_IN. */
696 :
697 : void
698 1609 : ipa_polymorphic_call_context::stream_in (class lto_input_block *ib,
699 : class data_in *data_in)
700 : {
701 1609 : struct bitpack_d bp = streamer_read_bitpack (ib);
702 :
703 1609 : invalid = bp_unpack_value (&bp, 1);
704 1609 : maybe_in_construction = bp_unpack_value (&bp, 1);
705 1609 : maybe_derived_type = bp_unpack_value (&bp, 1);
706 1609 : speculative_maybe_derived_type = bp_unpack_value (&bp, 1);
707 1609 : dynamic = bp_unpack_value (&bp, 1);
708 1609 : bool outer_type_p = bp_unpack_value (&bp, 1);
709 1609 : bool offset_p = bp_unpack_value (&bp, 1);
710 1609 : bool speculative_outer_type_p = bp_unpack_value (&bp, 1);
711 :
712 1609 : if (outer_type_p)
713 781 : outer_type = stream_read_tree (ib, data_in);
714 : else
715 828 : outer_type = NULL;
716 1609 : if (offset_p)
717 400 : offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
718 : else
719 1209 : offset = 0;
720 1609 : if (speculative_outer_type_p)
721 : {
722 556 : speculative_outer_type = stream_read_tree (ib, data_in);
723 556 : speculative_offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
724 : }
725 : else
726 : {
727 1053 : speculative_outer_type = NULL;
728 1053 : speculative_offset = 0;
729 : }
730 1609 : }
731 :
732 : /* Produce polymorphic call context for call method of instance
733 : that is located within BASE (that is assumed to be a decl) at offset OFF. */
734 :
735 : void
736 1359660 : ipa_polymorphic_call_context::set_by_decl (tree base, HOST_WIDE_INT off)
737 : {
738 1359660 : gcc_assert (DECL_P (base));
739 1359660 : clear_speculation ();
740 :
741 1359660 : if (!contains_polymorphic_type_p (TREE_TYPE (base)))
742 : {
743 1296414 : clear_outer_type ();
744 1296414 : offset = off;
745 1296414 : return;
746 : }
747 63246 : outer_type = TYPE_MAIN_VARIANT (TREE_TYPE (base));
748 63246 : offset = off;
749 : /* Make very conservative assumption that all objects
750 : may be in construction.
751 :
752 : It is up to caller to revisit this via
753 : get_dynamic_type or decl_maybe_in_construction_p. */
754 63246 : maybe_in_construction = true;
755 63246 : maybe_derived_type = false;
756 63246 : dynamic = false;
757 : }
758 :
759 : /* CST is an invariant (address of decl), try to get meaningful
760 : polymorphic call context for polymorphic call of method
761 : if instance of OTR_TYPE that is located at offset OFF of this invariant.
762 : Return FALSE if nothing meaningful can be found. */
763 :
764 : bool
765 12530 : ipa_polymorphic_call_context::set_by_invariant (tree cst,
766 : tree otr_type,
767 : HOST_WIDE_INT off)
768 : {
769 12530 : poly_int64 offset2, size, max_size;
770 12530 : bool reverse;
771 12530 : tree base;
772 :
773 12530 : invalid = false;
774 12530 : clear_outer_type (otr_type);
775 :
776 12530 : if (TREE_CODE (cst) != ADDR_EXPR)
777 : return false;
778 :
779 2692 : cst = TREE_OPERAND (cst, 0);
780 2692 : base = get_ref_base_and_extent (cst, &offset2, &size, &max_size, &reverse);
781 2692 : if (!DECL_P (base) || !known_size_p (max_size) || maybe_ne (max_size, size))
782 : return false;
783 :
784 : /* Only type inconsistent programs can have otr_type that is
785 : not part of outer type. */
786 2683 : if (otr_type && !contains_type_p (TREE_TYPE (base), off, otr_type))
787 : return false;
788 :
789 2683 : set_by_decl (base, off);
790 2683 : return true;
791 : }
792 :
793 : /* See if OP is SSA name initialized as a copy or by single assignment.
794 : If so, walk the SSA graph up. Because simple PHI conditional is considered
795 : copy, GLOBAL_VISITED may be used to avoid infinite loop walking the SSA
796 : graph. */
797 :
798 : static tree
799 7126116 : walk_ssa_copies (tree op, hash_set<tree> **global_visited = NULL)
800 : {
801 7126116 : hash_set <tree> *visited = NULL;
802 7126116 : STRIP_NOPS (op);
803 7126116 : while (TREE_CODE (op) == SSA_NAME
804 3441397 : && !SSA_NAME_IS_DEFAULT_DEF (op)
805 : /* We might be called via fold_stmt during cfgcleanup where
806 : SSA form need not be up-to-date. */
807 2349726 : && !name_registered_for_update_p (op)
808 9920220 : && (gimple_assign_single_p (SSA_NAME_DEF_STMT (op))
809 492266 : || gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI))
810 : {
811 1982669 : if (global_visited)
812 : {
813 1358677 : if (!*global_visited)
814 1268785 : *global_visited = new hash_set<tree>;
815 1358677 : if ((*global_visited)->add (op))
816 0 : goto done;
817 : }
818 : else
819 : {
820 623992 : if (!visited)
821 612226 : visited = new hash_set<tree>;
822 623992 : if (visited->add (op))
823 0 : goto done;
824 : }
825 : /* Special case
826 : if (ptr == 0)
827 : ptr = 0;
828 : else
829 : ptr = ptr.foo;
830 : This pattern is implicitly produced for casts to non-primary
831 : bases. When doing context analysis, we do not really care
832 : about the case pointer is NULL, because the call will be
833 : undefined anyway. */
834 1982669 : if (gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI)
835 : {
836 125209 : gimple *phi = SSA_NAME_DEF_STMT (op);
837 :
838 125209 : if (gimple_phi_num_args (phi) > 2)
839 23236 : goto done;
840 101973 : if (gimple_phi_num_args (phi) == 1)
841 928 : op = gimple_phi_arg_def (phi, 0);
842 101045 : else if (integer_zerop (gimple_phi_arg_def (phi, 0)))
843 5830 : op = gimple_phi_arg_def (phi, 1);
844 95215 : else if (integer_zerop (gimple_phi_arg_def (phi, 1)))
845 14399 : op = gimple_phi_arg_def (phi, 0);
846 : else
847 80816 : goto done;
848 : }
849 : else
850 : {
851 1857460 : if (gimple_assign_load_p (SSA_NAME_DEF_STMT (op)))
852 1434239 : goto done;
853 423221 : op = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op));
854 : }
855 444378 : STRIP_NOPS (op);
856 : }
857 7126116 : done:
858 7126116 : if (visited)
859 612226 : delete (visited);
860 7126116 : return op;
861 : }
862 :
863 : /* Create polymorphic call context from IP invariant CST.
864 : This is typically &global_var.
865 : OTR_TYPE specify type of polymorphic call or NULL if unknown, OFF
866 : is offset of call. */
867 :
868 12530 : ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree cst,
869 : tree otr_type,
870 : HOST_WIDE_INT off)
871 : {
872 12530 : clear_speculation ();
873 12530 : set_by_invariant (cst, otr_type, off);
874 12530 : }
875 :
876 : /* Build context for pointer REF contained in FNDECL at statement STMT.
877 : if INSTANCE is non-NULL, return pointer to the object described by
878 : the context or DECL where context is contained in. */
879 :
880 4292698 : ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl,
881 : tree ref,
882 : gimple *stmt,
883 : tree *instance)
884 : {
885 4292698 : tree otr_type = NULL;
886 4292698 : tree base_pointer;
887 4292698 : hash_set <tree> *visited = NULL;
888 :
889 4292698 : if (TREE_CODE (ref) == OBJ_TYPE_REF)
890 : {
891 924591 : otr_type = obj_type_ref_class (ref);
892 924591 : base_pointer = OBJ_TYPE_REF_OBJECT (ref);
893 : }
894 : else
895 : base_pointer = ref;
896 :
897 : /* Set up basic info in case we find nothing interesting in the analysis. */
898 4292698 : clear_speculation ();
899 4292698 : clear_outer_type (otr_type);
900 4292698 : invalid = false;
901 :
902 : /* Walk SSA for outer object. */
903 4589202 : while (true)
904 : {
905 4589202 : base_pointer = walk_ssa_copies (base_pointer, &visited);
906 4589202 : if (TREE_CODE (base_pointer) == ADDR_EXPR)
907 : {
908 2096392 : HOST_WIDE_INT offset2, size;
909 2096392 : bool reverse;
910 2096392 : tree base
911 2096392 : = get_ref_base_and_extent_hwi (TREE_OPERAND (base_pointer, 0),
912 : &offset2, &size, &reverse);
913 2096392 : if (!base)
914 : break;
915 :
916 2072891 : combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base)),
917 2072891 : offset + offset2,
918 : true,
919 : NULL /* Do not change outer type. */);
920 :
921 : /* If this is a varying address, punt. */
922 2072891 : if (TREE_CODE (base) == MEM_REF || DECL_P (base))
923 : {
924 : /* We found dereference of a pointer. Type of the pointer
925 : and MEM_REF is meaningless, but we can look further. */
926 1653489 : offset_int mem_offset;
927 1653489 : if (TREE_CODE (base) == MEM_REF
928 1653489 : && mem_ref_offset (base).is_constant (&mem_offset))
929 : {
930 296512 : offset_int o = mem_offset * BITS_PER_UNIT;
931 296512 : o += offset;
932 296512 : o += offset2;
933 296512 : if (!wi::fits_shwi_p (o))
934 : break;
935 296504 : base_pointer = TREE_OPERAND (base, 0);
936 296504 : offset = o.to_shwi ();
937 296504 : outer_type = NULL;
938 : }
939 : /* We found base object. In this case the outer_type
940 : is known. */
941 1356977 : else if (DECL_P (base))
942 : {
943 1356977 : if (visited)
944 728 : delete (visited);
945 : /* Only type inconsistent programs can have otr_type that is
946 : not part of outer type. */
947 1356977 : if (otr_type
948 1371975 : && !contains_type_p (TREE_TYPE (base),
949 14998 : offset + offset2, otr_type))
950 : {
951 0 : invalid = true;
952 0 : if (instance)
953 0 : *instance = base_pointer;
954 1356977 : return;
955 : }
956 1356977 : set_by_decl (base, offset + offset2);
957 1356977 : if (outer_type && maybe_in_construction && stmt)
958 62100 : maybe_in_construction
959 62100 : = decl_maybe_in_construction_p (base,
960 : outer_type,
961 : stmt,
962 : fndecl);
963 1356977 : if (instance)
964 1346154 : *instance = base;
965 : return;
966 : }
967 : else
968 : break;
969 : }
970 : else
971 : break;
972 : }
973 2492810 : else if (TREE_CODE (base_pointer) == POINTER_PLUS_EXPR
974 2492810 : && TREE_CODE (TREE_OPERAND (base_pointer, 1)) == INTEGER_CST)
975 : {
976 0 : offset_int o
977 0 : = offset_int::from (wi::to_wide (TREE_OPERAND (base_pointer, 1)),
978 : SIGNED);
979 0 : o *= BITS_PER_UNIT;
980 0 : o += offset;
981 0 : if (!wi::fits_shwi_p (o))
982 : break;
983 0 : offset = o.to_shwi ();
984 0 : base_pointer = TREE_OPERAND (base_pointer, 0);
985 : }
986 : else
987 : break;
988 : }
989 :
990 2935721 : if (visited)
991 1268057 : delete (visited);
992 :
993 : /* Try to determine type of the outer object. */
994 2935721 : if (TREE_CODE (base_pointer) == SSA_NAME
995 2387712 : && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
996 3984143 : && TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL)
997 : {
998 : /* See if parameter is THIS pointer of a method. */
999 1036755 : if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE
1000 1622610 : && SSA_NAME_VAR (base_pointer) == DECL_ARGUMENTS (fndecl))
1001 : {
1002 443980 : outer_type
1003 443980 : = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
1004 443980 : cgraph_node *node = cgraph_node::get (current_function_decl);
1005 443980 : gcc_assert (TREE_CODE (outer_type) == RECORD_TYPE
1006 : || TREE_CODE (outer_type) == UNION_TYPE);
1007 :
1008 : /* Handle the case we inlined into a thunk. In this case
1009 : thunk has THIS pointer of type bar, but it really receives
1010 : address to its base type foo which sits in bar at
1011 : 0-thunk.fixed_offset. It starts with code that adds
1012 : think.fixed_offset to the pointer to compensate for this.
1013 :
1014 : Because we walked all the way to the beginning of thunk, we now
1015 : see pointer &bar-thunk.fixed_offset and need to compensate
1016 : for it. */
1017 443980 : thunk_info *info = thunk_info::get (node);
1018 443980 : if (info && info->fixed_offset)
1019 7 : offset -= info->fixed_offset * BITS_PER_UNIT;
1020 :
1021 : /* Dynamic casting has possibly upcasted the type
1022 : in the hierarchy. In this case outer type is less
1023 : informative than inner type and we should forget
1024 : about it. */
1025 443980 : if ((otr_type
1026 158474 : && !contains_type_p (outer_type, offset,
1027 : otr_type))
1028 441905 : || !contains_polymorphic_type_p (outer_type)
1029 : /* If we compile thunk with virtual offset, the THIS pointer
1030 : is adjusted by unknown value. We can't thus use outer info
1031 : at all. */
1032 670973 : || (info && info->virtual_offset_p))
1033 : {
1034 216987 : outer_type = NULL;
1035 216987 : if (instance)
1036 215220 : *instance = base_pointer;
1037 : return;
1038 : }
1039 :
1040 226993 : dynamic = true;
1041 :
1042 : /* If the function is constructor or destructor, then
1043 : the type is possibly in construction, but we know
1044 : it is not derived type. */
1045 226993 : if (DECL_CXX_CONSTRUCTOR_P (fndecl)
1046 226993 : || DECL_CXX_DESTRUCTOR_P (fndecl))
1047 : {
1048 31767 : maybe_in_construction = true;
1049 31767 : maybe_derived_type = false;
1050 : }
1051 : else
1052 : {
1053 195226 : maybe_derived_type = true;
1054 195226 : maybe_in_construction = false;
1055 : }
1056 226993 : if (instance)
1057 : {
1058 106823 : thunk_info *info = thunk_info::get (node);
1059 : /* If method is expanded thunk, we need to apply thunk offset
1060 : to instance pointer. */
1061 106823 : if (info && (info->virtual_offset_p || info->fixed_offset))
1062 5 : *instance = NULL;
1063 : else
1064 106818 : *instance = base_pointer;
1065 : }
1066 : return;
1067 : }
1068 : /* Non-PODs passed by value are really passed by invisible
1069 : reference. In this case we also know the type of the
1070 : object. */
1071 592775 : if (DECL_BY_REFERENCE (SSA_NAME_VAR (base_pointer)))
1072 : {
1073 20938 : outer_type
1074 20938 : = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
1075 : /* Only type inconsistent programs can have otr_type that is
1076 : not part of outer type. */
1077 20938 : if (otr_type && !contains_type_p (outer_type, offset,
1078 : otr_type))
1079 : {
1080 0 : invalid = true;
1081 0 : if (instance)
1082 0 : *instance = base_pointer;
1083 : return;
1084 : }
1085 : /* Non-polymorphic types have no interest for us. */
1086 20938 : else if (!otr_type && !contains_polymorphic_type_p (outer_type))
1087 : {
1088 20850 : outer_type = NULL;
1089 20850 : if (instance)
1090 20850 : *instance = base_pointer;
1091 : return;
1092 : }
1093 88 : maybe_derived_type = false;
1094 88 : maybe_in_construction = false;
1095 88 : if (instance)
1096 69 : *instance = base_pointer;
1097 : return;
1098 : }
1099 : }
1100 :
1101 2470803 : tree base_type = TREE_TYPE (base_pointer);
1102 :
1103 2470803 : if (TREE_CODE (base_pointer) == SSA_NAME
1104 1922794 : && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
1105 3054307 : && !(TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL
1106 11667 : || TREE_CODE (SSA_NAME_VAR (base_pointer)) == RESULT_DECL))
1107 : {
1108 362 : invalid = true;
1109 362 : if (instance)
1110 248 : *instance = base_pointer;
1111 : return;
1112 : }
1113 2470441 : if (TREE_CODE (base_pointer) == SSA_NAME
1114 1922432 : && SSA_NAME_DEF_STMT (base_pointer)
1115 4392873 : && gimple_assign_single_p (SSA_NAME_DEF_STMT (base_pointer)))
1116 931877 : base_type = TREE_TYPE (gimple_assign_rhs1
1117 : (SSA_NAME_DEF_STMT (base_pointer)));
1118 :
1119 2470441 : if (base_type && POINTER_TYPE_P (base_type))
1120 2470441 : combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base_type)),
1121 : offset,
1122 : true, NULL /* Do not change type here */);
1123 : /* TODO: There are multiple ways to derive a type. For instance
1124 : if BASE_POINTER is passed to an constructor call prior our reference.
1125 : We do not make this type of flow sensitive analysis yet. */
1126 2470441 : if (instance)
1127 1887726 : *instance = base_pointer;
1128 : return;
1129 : }
1130 :
1131 : /* Structure to be passed in between detect_type_change and
1132 : check_stmt_for_type_change. */
1133 :
1134 : struct type_change_info
1135 : {
1136 : /* Offset into the object where there is the virtual method pointer we are
1137 : looking for. */
1138 : HOST_WIDE_INT offset;
1139 : /* The declaration or SSA_NAME pointer of the base that we are checking for
1140 : type change. */
1141 : tree instance;
1142 : /* The reference to virtual table pointer used. */
1143 : tree vtbl_ptr_ref;
1144 : tree otr_type;
1145 : /* If we actually can tell the type that the object has changed to, it is
1146 : stored in this field. Otherwise it remains NULL_TREE. */
1147 : tree known_current_type;
1148 : HOST_WIDE_INT known_current_offset;
1149 :
1150 : /* Set to nonzero if we possibly missed some dynamic type changes and we
1151 : should consider the set to be speculative. */
1152 : unsigned speculative;
1153 :
1154 : /* Set to true if dynamic type change has been detected. */
1155 : bool type_maybe_changed;
1156 : /* Set to true if multiple types have been encountered. known_current_type
1157 : must be disregarded in that case. */
1158 : bool multiple_types_encountered;
1159 : bool seen_unanalyzed_store;
1160 : };
1161 :
1162 : /* Return true if STMT is not call and can modify a virtual method table pointer.
1163 : We take advantage of fact that vtable stores must appear within constructor
1164 : and destructor functions. */
1165 :
1166 : static bool
1167 7725099 : noncall_stmt_may_be_vtbl_ptr_store (gimple *stmt)
1168 : {
1169 7725099 : if (is_gimple_assign (stmt))
1170 : {
1171 7568653 : tree lhs = gimple_assign_lhs (stmt);
1172 :
1173 7568653 : if (gimple_clobber_p (stmt))
1174 : return false;
1175 6537819 : if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
1176 : {
1177 5815744 : if (flag_strict_aliasing
1178 5815744 : && !POINTER_TYPE_P (TREE_TYPE (lhs)))
1179 : return false;
1180 :
1181 2642694 : if (TREE_CODE (lhs) == COMPONENT_REF
1182 2642694 : && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1183 : return false;
1184 : /* In the future we might want to use get_ref_base_and_extent to find
1185 : if there is a field corresponding to the offset and if so, proceed
1186 : almost like if it was a component ref. */
1187 : }
1188 : }
1189 :
1190 : /* Code unification may mess with inline stacks. */
1191 1537486 : if (cfun->after_inlining)
1192 : return true;
1193 :
1194 : /* Walk the inline stack and watch out for ctors/dtors.
1195 : TODO: Maybe we can require the store to appear in toplevel
1196 : block of CTOR/DTOR. */
1197 11714250 : for (tree block = gimple_block (stmt); block && TREE_CODE (block) == BLOCK;
1198 5111672 : block = BLOCK_SUPERCONTEXT (block))
1199 5185382 : if (BLOCK_ABSTRACT_ORIGIN (block)
1200 5185382 : && TREE_CODE (block_ultimate_origin (block)) == FUNCTION_DECL)
1201 73710 : return inlined_polymorphic_ctor_dtor_block_p (block, false);
1202 1417196 : return (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
1203 1417196 : && (DECL_CXX_CONSTRUCTOR_P (current_function_decl)
1204 89132 : || DECL_CXX_DESTRUCTOR_P (current_function_decl)));
1205 : }
1206 :
1207 : /* If STMT can be proved to be an assignment to the virtual method table
1208 : pointer of ANALYZED_OBJ and the type associated with the new table
1209 : identified, return the type. Otherwise return NULL_TREE if type changes
1210 : in unknown way or ERROR_MARK_NODE if type is unchanged. */
1211 :
1212 : static tree
1213 89920 : extr_type_from_vtbl_ptr_store (gimple *stmt, struct type_change_info *tci,
1214 : HOST_WIDE_INT *type_offset)
1215 : {
1216 89920 : poly_int64 offset, size, max_size;
1217 89920 : tree lhs, rhs, base;
1218 89920 : bool reverse;
1219 :
1220 89920 : if (!gimple_assign_single_p (stmt))
1221 : return NULL_TREE;
1222 :
1223 89496 : lhs = gimple_assign_lhs (stmt);
1224 89496 : rhs = gimple_assign_rhs1 (stmt);
1225 89496 : if (TREE_CODE (lhs) != COMPONENT_REF
1226 89496 : || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1227 : {
1228 38523 : if (dump_file)
1229 0 : fprintf (dump_file, " LHS is not virtual table.\n");
1230 : return NULL_TREE;
1231 : }
1232 :
1233 50973 : if (tci->vtbl_ptr_ref && operand_equal_p (lhs, tci->vtbl_ptr_ref, 0))
1234 : ;
1235 : else
1236 : {
1237 50891 : base = get_ref_base_and_extent (lhs, &offset, &size, &max_size, &reverse);
1238 50891 : if (DECL_P (tci->instance))
1239 : {
1240 2080 : if (base != tci->instance)
1241 : {
1242 16 : if (dump_file)
1243 : {
1244 0 : fprintf (dump_file, " base:");
1245 0 : print_generic_expr (dump_file, base, TDF_SLIM);
1246 0 : fprintf (dump_file, " does not match instance:");
1247 0 : print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1248 0 : fprintf (dump_file, "\n");
1249 : }
1250 : return NULL_TREE;
1251 : }
1252 : }
1253 48811 : else if (TREE_CODE (base) == MEM_REF)
1254 : {
1255 40736 : if (!operand_equal_p (tci->instance, TREE_OPERAND (base, 0), 0))
1256 : {
1257 27737 : if (dump_file)
1258 : {
1259 0 : fprintf (dump_file, " base mem ref:");
1260 0 : print_generic_expr (dump_file, base, TDF_SLIM);
1261 0 : fprintf (dump_file, " does not match instance:");
1262 0 : print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1263 0 : fprintf (dump_file, "\n");
1264 : }
1265 : return NULL_TREE;
1266 : }
1267 12999 : if (!integer_zerop (TREE_OPERAND (base, 1)))
1268 : {
1269 1423 : if (!tree_fits_shwi_p (TREE_OPERAND (base, 1)))
1270 : {
1271 0 : if (dump_file)
1272 : {
1273 0 : fprintf (dump_file, " base mem ref:");
1274 0 : print_generic_expr (dump_file, base, TDF_SLIM);
1275 0 : fprintf (dump_file, " has non-representable offset:");
1276 0 : print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1277 0 : fprintf (dump_file, "\n");
1278 : }
1279 : return NULL_TREE;
1280 : }
1281 : else
1282 1423 : offset += tree_to_shwi (TREE_OPERAND (base, 1)) * BITS_PER_UNIT;
1283 : }
1284 : }
1285 8075 : else if (!operand_equal_p (tci->instance, base, 0)
1286 8075 : || tci->offset)
1287 : {
1288 8075 : if (dump_file)
1289 : {
1290 0 : fprintf (dump_file, " base:");
1291 0 : print_generic_expr (dump_file, base, TDF_SLIM);
1292 0 : fprintf (dump_file, " does not match instance:");
1293 0 : print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1294 0 : fprintf (dump_file, " with offset %i\n", (int)tci->offset);
1295 : }
1296 8149 : return tci->offset > POINTER_SIZE ? error_mark_node : NULL_TREE;
1297 : }
1298 15063 : if (maybe_ne (offset, tci->offset)
1299 15467 : || maybe_ne (size, POINTER_SIZE)
1300 30530 : || maybe_ne (max_size, POINTER_SIZE))
1301 : {
1302 35 : if (dump_file)
1303 : {
1304 0 : fprintf (dump_file, " wrong offset ");
1305 0 : print_dec (offset, dump_file);
1306 0 : fprintf (dump_file, "!=%i or size ", (int) tci->offset);
1307 0 : print_dec (size, dump_file);
1308 0 : fprintf (dump_file, "\n");
1309 : }
1310 35 : return (known_le (offset + POINTER_SIZE, tci->offset)
1311 0 : || (known_size_p (max_size)
1312 0 : && known_gt (tci->offset + POINTER_SIZE,
1313 : offset + max_size))
1314 35 : ? error_mark_node : NULL);
1315 : }
1316 : }
1317 :
1318 15110 : tree vtable;
1319 15110 : unsigned HOST_WIDE_INT offset2;
1320 :
1321 15110 : if (!vtable_pointer_value_to_vtable (rhs, &vtable, &offset2))
1322 : {
1323 68 : if (dump_file)
1324 4 : fprintf (dump_file, " Failed to lookup binfo\n");
1325 : return NULL;
1326 : }
1327 :
1328 15042 : tree binfo = subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
1329 : offset2, vtable);
1330 15042 : if (!binfo)
1331 : {
1332 175 : if (dump_file)
1333 13 : fprintf (dump_file, " Construction vtable used\n");
1334 : /* FIXME: We should support construction contexts. */
1335 : return NULL;
1336 : }
1337 :
1338 14867 : *type_offset = tree_to_shwi (BINFO_OFFSET (binfo)) * BITS_PER_UNIT;
1339 14867 : return DECL_CONTEXT (vtable);
1340 : }
1341 :
1342 : /* Record dynamic type change of TCI to TYPE. */
1343 :
1344 : static void
1345 150811 : record_known_type (struct type_change_info *tci, tree type, HOST_WIDE_INT offset)
1346 : {
1347 150811 : if (dump_file)
1348 : {
1349 39 : if (type)
1350 : {
1351 39 : fprintf (dump_file, " Recording type: ");
1352 39 : print_generic_expr (dump_file, type, TDF_SLIM);
1353 39 : fprintf (dump_file, " at offset %i\n", (int)offset);
1354 : }
1355 : else
1356 0 : fprintf (dump_file, " Recording unknown type\n");
1357 : }
1358 :
1359 : /* If we found a constructor of type that is not polymorphic or
1360 : that may contain the type in question as a field (not as base),
1361 : restrict to the inner class first to make type matching below
1362 : happier. */
1363 150811 : if (type
1364 150811 : && (offset
1365 145067 : || (TREE_CODE (type) != RECORD_TYPE
1366 145067 : || !TYPE_BINFO (type)
1367 144596 : || !polymorphic_type_binfo_p (TYPE_BINFO (type)))))
1368 : {
1369 132190 : ipa_polymorphic_call_context context;
1370 :
1371 132190 : context.offset = offset;
1372 132190 : context.outer_type = type;
1373 132190 : context.maybe_in_construction = false;
1374 132190 : context.maybe_derived_type = false;
1375 132190 : context.dynamic = true;
1376 : /* If we failed to find the inner type, we know that the call
1377 : would be undefined for type produced here. */
1378 132190 : if (!context.restrict_to_inner_class (tci->otr_type))
1379 : {
1380 876 : if (dump_file)
1381 0 : fprintf (dump_file, " Ignoring; does not contain otr_type\n");
1382 876 : return;
1383 : }
1384 : /* Watch for case we reached an POD type and anticipate placement
1385 : new. */
1386 131314 : if (!context.maybe_derived_type)
1387 : {
1388 2326 : type = context.outer_type;
1389 2326 : offset = context.offset;
1390 : }
1391 : }
1392 149935 : if (tci->type_maybe_changed
1393 149935 : && (!types_same_for_odr (type, tci->known_current_type)
1394 21 : || offset != tci->known_current_offset))
1395 2 : tci->multiple_types_encountered = true;
1396 149935 : tci->known_current_type = TYPE_MAIN_VARIANT (type);
1397 149935 : tci->known_current_offset = offset;
1398 149935 : tci->type_maybe_changed = true;
1399 : }
1400 :
1401 :
1402 : /* The maximum number of may-defs we visit when looking for a must-def
1403 : that changes the dynamic type in check_stmt_for_type_change. Tuned
1404 : after the PR12392 testcase which unlimited spends 40% time within
1405 : these alias walks and 8% with the following limit. */
1406 :
1407 : static inline bool
1408 16614653 : csftc_abort_walking_p (unsigned speculative)
1409 : {
1410 16614653 : unsigned max = param_max_speculative_devirt_maydefs;
1411 16614653 : return speculative > max;
1412 : }
1413 :
1414 : /* Callback of walk_aliased_vdefs and a helper function for
1415 : detect_type_change to check whether a particular statement may modify
1416 : the virtual table pointer, and if possible also determine the new type of
1417 : the (sub-)object. It stores its result into DATA, which points to a
1418 : type_change_info structure. */
1419 :
1420 : static bool
1421 24565435 : check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
1422 : {
1423 24565435 : gimple *stmt = SSA_NAME_DEF_STMT (vdef);
1424 24565435 : struct type_change_info *tci = (struct type_change_info *) data;
1425 24565435 : tree fn;
1426 :
1427 : /* If we already gave up, just terminate the rest of walk. */
1428 24565435 : if (tci->multiple_types_encountered)
1429 : return true;
1430 :
1431 24565435 : if (is_gimple_call (stmt))
1432 : {
1433 16840336 : if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
1434 : return false;
1435 :
1436 : /* Check for a constructor call. */
1437 16750597 : if ((fn = gimple_call_fndecl (stmt)) != NULL_TREE
1438 15193069 : && DECL_CXX_CONSTRUCTOR_P (fn)
1439 1043122 : && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1440 17793719 : && gimple_call_num_args (stmt))
1441 : {
1442 1043122 : tree op = walk_ssa_copies (gimple_call_arg (stmt, 0));
1443 1043122 : tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
1444 1043122 : HOST_WIDE_INT offset = 0;
1445 1043122 : bool reverse;
1446 :
1447 1043122 : if (dump_file)
1448 : {
1449 31 : fprintf (dump_file, " Checking constructor call: ");
1450 31 : print_gimple_stmt (dump_file, stmt, 0);
1451 : }
1452 :
1453 : /* See if THIS parameter seems like instance pointer. */
1454 1043122 : if (TREE_CODE (op) == ADDR_EXPR)
1455 : {
1456 1003782 : HOST_WIDE_INT size;
1457 1003782 : op = get_ref_base_and_extent_hwi (TREE_OPERAND (op, 0),
1458 : &offset, &size, &reverse);
1459 1003782 : if (!op)
1460 : {
1461 0 : tci->speculative++;
1462 0 : return csftc_abort_walking_p (tci->speculative);
1463 : }
1464 1003782 : if (TREE_CODE (op) == MEM_REF)
1465 : {
1466 80097 : if (!tree_fits_shwi_p (TREE_OPERAND (op, 1)))
1467 : {
1468 0 : tci->speculative++;
1469 0 : return csftc_abort_walking_p (tci->speculative);
1470 : }
1471 80097 : offset += tree_to_shwi (TREE_OPERAND (op, 1))
1472 80097 : * BITS_PER_UNIT;
1473 80097 : op = TREE_OPERAND (op, 0);
1474 : }
1475 923685 : else if (DECL_P (op))
1476 : ;
1477 : else
1478 : {
1479 0 : tci->speculative++;
1480 0 : return csftc_abort_walking_p (tci->speculative);
1481 : }
1482 1003782 : op = walk_ssa_copies (op);
1483 : }
1484 1043122 : if (operand_equal_p (op, tci->instance, 0)
1485 165474 : && TYPE_SIZE (type)
1486 165474 : && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1487 165474 : && tree_fits_shwi_p (TYPE_SIZE (type))
1488 165474 : && tree_to_shwi (TYPE_SIZE (type)) + offset > tci->offset
1489 : /* Some inlined constructors may look as follows:
1490 : _3 = operator new (16);
1491 : MEM[(struct &)_3] ={v} {CLOBBER};
1492 : MEM[(struct CompositeClass *)_3]._vptr.CompositeClass
1493 : = &MEM[(void *)&_ZTV14CompositeClass + 16B];
1494 : _7 = &MEM[(struct CompositeClass *)_3].object;
1495 : EmptyClass::EmptyClass (_7);
1496 :
1497 : When determining dynamic type of _3 and because we stop at first
1498 : dynamic type found, we would stop on EmptyClass::EmptyClass (_7).
1499 : In this case the emptyclass is not even polymorphic and we miss
1500 : it is contained in an outer type that is polymorphic. */
1501 :
1502 1200208 : && (tci->offset == offset || contains_polymorphic_type_p (type)))
1503 : {
1504 135944 : record_known_type (tci, type, tci->offset - offset);
1505 135944 : return true;
1506 : }
1507 : }
1508 : /* Calls may possibly change dynamic type by placement new. Assume
1509 : it will not happen, but make result speculative only. */
1510 16614653 : if (dump_file)
1511 : {
1512 731 : fprintf (dump_file, " Function call may change dynamic type:");
1513 731 : print_gimple_stmt (dump_file, stmt, 0);
1514 : }
1515 16614653 : tci->speculative++;
1516 16614653 : return csftc_abort_walking_p (tci->speculative);
1517 : }
1518 : /* Check for inlined virtual table store. */
1519 7725099 : else if (noncall_stmt_may_be_vtbl_ptr_store (stmt))
1520 : {
1521 89920 : tree type;
1522 89920 : HOST_WIDE_INT offset = 0;
1523 89920 : if (dump_file)
1524 : {
1525 38 : fprintf (dump_file, " Checking vtbl store: ");
1526 38 : print_gimple_stmt (dump_file, stmt, 0);
1527 : }
1528 :
1529 89920 : type = extr_type_from_vtbl_ptr_store (stmt, tci, &offset);
1530 89920 : if (type == error_mark_node)
1531 : return false;
1532 89885 : gcc_assert (!type || TYPE_MAIN_VARIANT (type) == type);
1533 14867 : if (!type)
1534 : {
1535 75018 : if (dump_file)
1536 17 : fprintf (dump_file, " Unanalyzed store may change type.\n");
1537 75018 : tci->seen_unanalyzed_store = true;
1538 75018 : tci->speculative++;
1539 : }
1540 : else
1541 14867 : record_known_type (tci, type, offset);
1542 : return true;
1543 : }
1544 : else
1545 : return false;
1546 : }
1547 :
1548 : /* THIS is polymorphic call context obtained from get_polymorphic_context.
1549 : OTR_OBJECT is pointer to the instance returned by OBJ_TYPE_REF_OBJECT.
1550 : INSTANCE is pointer to the outer instance as returned by
1551 : get_polymorphic_context. To avoid creation of temporary expressions,
1552 : INSTANCE may also be an declaration of get_polymorphic_context found the
1553 : value to be in static storage.
1554 :
1555 : If the type of instance is not fully determined
1556 : (either OUTER_TYPE is unknown or MAYBE_IN_CONSTRUCTION/INCLUDE_DERIVED_TYPES
1557 : is set), try to walk memory writes and find the actual construction of the
1558 : instance.
1559 :
1560 : Return true if memory is unchanged from function entry.
1561 :
1562 : We do not include this analysis in the context analysis itself, because
1563 : it needs memory SSA to be fully built and the walk may be expensive.
1564 : So it is not suitable for use withing fold_stmt and similar uses.
1565 :
1566 : AA_WALK_BUDGET_P, if not NULL, is how statements we should allow
1567 : walk_aliased_vdefs to examine. The value should be decremented by the
1568 : number of statements we examined or set to zero if exhausted. */
1569 :
1570 : bool
1571 3577090 : ipa_polymorphic_call_context::get_dynamic_type (tree instance,
1572 : tree otr_object,
1573 : tree otr_type,
1574 : gimple *call,
1575 : unsigned *aa_walk_budget_p)
1576 : {
1577 3577090 : struct type_change_info tci;
1578 3577090 : ao_ref ao;
1579 3577090 : bool function_entry_reached = false;
1580 3577090 : tree instance_ref = NULL;
1581 3577090 : gimple *stmt = call;
1582 : /* Remember OFFSET before it is modified by restrict_to_inner_class.
1583 : This is because we do not update INSTANCE when walking inwards. */
1584 3577090 : HOST_WIDE_INT instance_offset = offset;
1585 3577090 : tree instance_outer_type = outer_type;
1586 :
1587 3577090 : if (!instance)
1588 : return false;
1589 :
1590 : /* Bail to avoid segfaults for IFN_ASSUME internal calls. They are not
1591 : polymorphic anyway. */
1592 3577085 : if (is_gimple_call (call) && gimple_call_internal_p (call, IFN_ASSUME))
1593 : return false;
1594 :
1595 3577066 : if (otr_type)
1596 208983 : otr_type = TYPE_MAIN_VARIANT (otr_type);
1597 :
1598 : /* Walk into inner type. This may clear maybe_derived_type and save us
1599 : from useless work. It also makes later comparisons with static type
1600 : easier. */
1601 3577066 : if (outer_type && otr_type)
1602 : {
1603 184196 : if (!restrict_to_inner_class (otr_type))
1604 : return false;
1605 : }
1606 :
1607 3577066 : if (!maybe_in_construction && !maybe_derived_type)
1608 : return false;
1609 :
1610 : /* If we are in fact not looking at any object or the instance is
1611 : some placement new into a random load, give up straight away. */
1612 3533601 : if (TREE_CODE (instance) == MEM_REF)
1613 : return false;
1614 :
1615 : /* We need to obtain reference to virtual table pointer. It is better
1616 : to look it up in the code rather than build our own. This require bit
1617 : of pattern matching, but we end up verifying that what we found is
1618 : correct.
1619 :
1620 : What we pattern match is:
1621 :
1622 : tmp = instance->_vptr.A; // vtbl ptr load
1623 : tmp2 = tmp[otr_token]; // vtable lookup
1624 : OBJ_TYPE_REF(tmp2;instance->0) (instance);
1625 :
1626 : We want to start alias oracle walk from vtbl pointer load,
1627 : but we may not be able to identify it, for example, when PRE moved the
1628 : load around. */
1629 :
1630 3533601 : if (gimple_code (call) == GIMPLE_CALL)
1631 : {
1632 3533601 : tree ref = gimple_call_fn (call);
1633 3533601 : bool reverse;
1634 :
1635 3533601 : if (TREE_CODE (ref) == OBJ_TYPE_REF)
1636 : {
1637 245267 : ref = OBJ_TYPE_REF_EXPR (ref);
1638 245267 : ref = walk_ssa_copies (ref);
1639 :
1640 : /* If call target is already known, no need to do the expensive
1641 : memory walk. */
1642 245267 : if (is_gimple_min_invariant (ref))
1643 0 : return false;
1644 :
1645 : /* Check if definition looks like vtable lookup. */
1646 245267 : if (TREE_CODE (ref) == SSA_NAME
1647 245267 : && !SSA_NAME_IS_DEFAULT_DEF (ref)
1648 245267 : && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref))
1649 490036 : && TREE_CODE (gimple_assign_rhs1
1650 : (SSA_NAME_DEF_STMT (ref))) == MEM_REF)
1651 : {
1652 244743 : ref = get_base_address
1653 244743 : (TREE_OPERAND (gimple_assign_rhs1
1654 : (SSA_NAME_DEF_STMT (ref)), 0));
1655 244743 : ref = walk_ssa_copies (ref);
1656 : /* Find base address of the lookup and see if it looks like
1657 : vptr load. */
1658 244743 : if (TREE_CODE (ref) == SSA_NAME
1659 244603 : && !SSA_NAME_IS_DEFAULT_DEF (ref)
1660 489346 : && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref)))
1661 : {
1662 242490 : HOST_WIDE_INT offset2, size;
1663 242490 : tree ref_exp = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (ref));
1664 242490 : tree base_ref
1665 242490 : = get_ref_base_and_extent_hwi (ref_exp, &offset2,
1666 : &size, &reverse);
1667 :
1668 : /* Finally verify that what we found looks like read from
1669 : OTR_OBJECT or from INSTANCE with offset OFFSET. */
1670 242490 : if (base_ref
1671 242490 : && ((TREE_CODE (base_ref) == MEM_REF
1672 238871 : && ((offset2 == instance_offset
1673 235887 : && TREE_OPERAND (base_ref, 0) == instance)
1674 16981 : || (!offset2
1675 16981 : && TREE_OPERAND (base_ref, 0)
1676 : == otr_object)))
1677 18556 : || (DECL_P (instance) && base_ref == instance
1678 3455 : && offset2 == instance_offset)))
1679 : {
1680 227389 : stmt = SSA_NAME_DEF_STMT (ref);
1681 227389 : instance_ref = ref_exp;
1682 : }
1683 : }
1684 : }
1685 : }
1686 : }
1687 :
1688 : /* If we failed to look up the reference in code, build our own. */
1689 3533601 : if (!instance_ref)
1690 : {
1691 : /* If the statement in question does not use memory, we can't tell
1692 : anything. */
1693 6612424 : if (!gimple_vuse (stmt))
1694 : return false;
1695 3297350 : ao_ref_init_from_ptr_and_size (&ao, otr_object, NULL);
1696 : }
1697 : else
1698 : /* Otherwise use the real reference. */
1699 227389 : ao_ref_init (&ao, instance_ref);
1700 :
1701 : /* We look for vtbl pointer read. */
1702 3763955 : ao.size = POINTER_SIZE;
1703 3524739 : ao.max_size = ao.size;
1704 : /* We are looking for stores to vptr pointer within the instance of
1705 : outer type.
1706 : TODO: The vptr pointer type is globally known, we probably should
1707 : keep it and do that even when otr_type is unknown. */
1708 3524739 : if (otr_type)
1709 : {
1710 208307 : ao.base_alias_set
1711 391827 : = get_alias_set (outer_type ? outer_type : otr_type);
1712 208307 : ao.ref_alias_set
1713 208307 : = get_alias_set (TREE_TYPE (BINFO_VTABLE (TYPE_BINFO (otr_type))));
1714 : }
1715 :
1716 3524739 : if (dump_file)
1717 : {
1718 632 : fprintf (dump_file, "Determining dynamic type for call: ");
1719 632 : print_gimple_stmt (dump_file, call, 0);
1720 632 : fprintf (dump_file, " Starting walk at: ");
1721 632 : print_gimple_stmt (dump_file, stmt, 0);
1722 632 : fprintf (dump_file, " instance pointer: ");
1723 632 : print_generic_expr (dump_file, otr_object, TDF_SLIM);
1724 632 : fprintf (dump_file, " Outer instance pointer: ");
1725 632 : print_generic_expr (dump_file, instance, TDF_SLIM);
1726 632 : fprintf (dump_file, " offset: %i (bits)", (int)instance_offset);
1727 632 : fprintf (dump_file, " vtbl reference: ");
1728 632 : print_generic_expr (dump_file, instance_ref, TDF_SLIM);
1729 632 : fprintf (dump_file, "\n");
1730 : }
1731 :
1732 3524739 : tci.offset = instance_offset;
1733 3524739 : tci.instance = instance;
1734 3524739 : tci.vtbl_ptr_ref = instance_ref;
1735 3524739 : tci.known_current_type = NULL_TREE;
1736 3524739 : tci.known_current_offset = 0;
1737 3524739 : tci.otr_type = otr_type;
1738 3524739 : tci.type_maybe_changed = false;
1739 3524739 : tci.multiple_types_encountered = false;
1740 3524739 : tci.speculative = 0;
1741 3524739 : tci.seen_unanalyzed_store = false;
1742 :
1743 3524739 : unsigned aa_walk_budget = 0;
1744 3524739 : if (aa_walk_budget_p)
1745 3341091 : aa_walk_budget = *aa_walk_budget_p + 1;
1746 :
1747 3524739 : int walked
1748 3524739 : = walk_aliased_vdefs (&ao, gimple_vuse (stmt), check_stmt_for_type_change,
1749 : &tci, NULL, &function_entry_reached, aa_walk_budget);
1750 :
1751 : /* If we did not find any type changing statements, we may still drop
1752 : maybe_in_construction flag if the context already have outer type.
1753 :
1754 : Here we make special assumptions about both constructors and
1755 : destructors which are all the functions that are allowed to alter the
1756 : VMT pointers. It assumes that destructors begin with assignment into
1757 : all VMT pointers and that constructors essentially look in the
1758 : following way:
1759 :
1760 : 1) The very first thing they do is that they call constructors of
1761 : ancestor sub-objects that have them.
1762 :
1763 : 2) Then VMT pointers of this and all its ancestors is set to new
1764 : values corresponding to the type corresponding to the constructor.
1765 :
1766 : 3) Only afterwards, other stuff such as constructor of member
1767 : sub-objects and the code written by the user is run. Only this may
1768 : include calling virtual functions, directly or indirectly.
1769 :
1770 : 4) placement new cannot be used to change type of non-POD statically
1771 : allocated variables.
1772 :
1773 : There is no way to call a constructor of an ancestor sub-object in any
1774 : other way.
1775 :
1776 : This means that we do not have to care whether constructors get the
1777 : correct type information because they will always change it (in fact,
1778 : if we define the type to be given by the VMT pointer, it is undefined).
1779 :
1780 : The most important fact to derive from the above is that if, for some
1781 : statement in the section 3, we try to detect whether the dynamic type
1782 : has changed, we can safely ignore all calls as we examine the function
1783 : body backwards until we reach statements in section 2 because these
1784 : calls cannot be ancestor constructors or destructors (if the input is
1785 : not bogus) and so do not change the dynamic type (this holds true only
1786 : for automatically allocated objects but at the moment we devirtualize
1787 : only these). We then must detect that statements in section 2 change
1788 : the dynamic type and can try to derive the new type. That is enough
1789 : and we can stop, we will never see the calls into constructors of
1790 : sub-objects in this code.
1791 :
1792 : Therefore if the static outer type was found (outer_type)
1793 : we can safely ignore tci.speculative that is set on calls and give up
1794 : only if there was dynamic type store that may affect given variable
1795 : (seen_unanalyzed_store) */
1796 :
1797 3524739 : if (walked < 0)
1798 : {
1799 111150 : if (dump_file)
1800 0 : fprintf (dump_file, " AA walk budget exhausted.\n");
1801 111150 : *aa_walk_budget_p = 0;
1802 111150 : return false;
1803 : }
1804 3413589 : else if (aa_walk_budget_p)
1805 3229941 : *aa_walk_budget_p -= walked;
1806 :
1807 3413589 : if (!tci.type_maybe_changed
1808 3413589 : || (outer_type
1809 22424 : && !dynamic
1810 3559 : && !tci.seen_unanalyzed_store
1811 3559 : && !tci.multiple_types_encountered
1812 3559 : && ((offset == tci.offset
1813 3211 : && types_same_for_odr (tci.known_current_type,
1814 : outer_type))
1815 770 : || (instance_offset == offset
1816 422 : && types_same_for_odr (tci.known_current_type,
1817 : instance_outer_type)))))
1818 : {
1819 3266470 : if (!outer_type || tci.seen_unanalyzed_store)
1820 : return false;
1821 211003 : if (maybe_in_construction)
1822 137430 : maybe_in_construction = false;
1823 211003 : if (dump_file)
1824 225 : fprintf (dump_file, " No dynamic type change found.\n");
1825 : return true;
1826 : }
1827 :
1828 147119 : if (tci.known_current_type
1829 147119 : && !function_entry_reached
1830 143372 : && !tci.multiple_types_encountered)
1831 : {
1832 143371 : if (!tci.speculative)
1833 : {
1834 46684 : outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1835 46684 : offset = tci.known_current_offset;
1836 46684 : dynamic = true;
1837 46684 : maybe_in_construction = false;
1838 46684 : maybe_derived_type = false;
1839 46684 : if (dump_file)
1840 28 : fprintf (dump_file, " Determined dynamic type.\n");
1841 : }
1842 96687 : else if (!speculative_outer_type
1843 5010 : || speculative_maybe_derived_type)
1844 : {
1845 96687 : speculative_outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1846 96687 : speculative_offset = tci.known_current_offset;
1847 96687 : speculative_maybe_derived_type = false;
1848 96687 : if (dump_file)
1849 8 : fprintf (dump_file, " Determined speculative dynamic type.\n");
1850 : }
1851 : }
1852 3748 : else if (dump_file)
1853 : {
1854 0 : fprintf (dump_file, " Found multiple types%s%s\n",
1855 : function_entry_reached ? " (function entry reached)" : "",
1856 : function_entry_reached ? " (multiple types encountered)" : "");
1857 : }
1858 :
1859 : return false;
1860 : }
1861 :
1862 : /* See if speculation given by SPEC_OUTER_TYPE, SPEC_OFFSET and SPEC_MAYBE_DERIVED_TYPE
1863 : seems consistent (and useful) with what we already have in the non-speculative context. */
1864 :
1865 : bool
1866 4946353 : ipa_polymorphic_call_context::speculation_consistent_p (tree spec_outer_type,
1867 : HOST_WIDE_INT spec_offset,
1868 : bool spec_maybe_derived_type,
1869 : tree otr_type) const
1870 : {
1871 4946353 : if (!flag_devirtualize_speculatively)
1872 : return false;
1873 :
1874 : /* Non-polymorphic types are useless for deriving likely polymorphic
1875 : call targets. */
1876 4937912 : if (!spec_outer_type || !contains_polymorphic_type_p (spec_outer_type))
1877 : return false;
1878 :
1879 : /* If we know nothing, speculation is always good. */
1880 1371266 : if (!outer_type)
1881 : return true;
1882 :
1883 : /* Speculation is only useful to avoid derived types.
1884 : This is not 100% true for placement new, where the outer context may
1885 : turn out to be useless, but ignore these for now. */
1886 893852 : if (!maybe_derived_type)
1887 : return false;
1888 :
1889 : /* If types agrees, speculation is consistent, but it makes sense only
1890 : when it says something new. */
1891 871798 : if (types_must_be_same_for_odr (spec_outer_type, outer_type))
1892 643046 : return maybe_derived_type && !spec_maybe_derived_type;
1893 :
1894 : /* If speculation does not contain the type in question, ignore it. */
1895 228752 : if (otr_type
1896 228752 : && !contains_type_p (spec_outer_type, spec_offset, otr_type, false, true))
1897 : return false;
1898 :
1899 : /* If outer type already contains speculation as a filed,
1900 : it is useless. We already know from OUTER_TYPE
1901 : SPEC_TYPE and that it is not in the construction. */
1902 226353 : if (contains_type_p (outer_type, offset - spec_offset,
1903 : spec_outer_type, false, false))
1904 : return false;
1905 :
1906 : /* If speculative outer type is not more specified than outer
1907 : type, just give up.
1908 : We can only decide this safely if we can compare types with OUTER_TYPE.
1909 : */
1910 94 : if ((!in_lto_p || odr_type_p (outer_type))
1911 226092 : && !contains_type_p (spec_outer_type,
1912 226092 : spec_offset - offset,
1913 226092 : outer_type, false))
1914 : return false;
1915 : return true;
1916 : }
1917 :
1918 : /* Improve THIS with speculation described by NEW_OUTER_TYPE, NEW_OFFSET,
1919 : NEW_MAYBE_DERIVED_TYPE
1920 : If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1921 :
1922 : bool
1923 4722849 : ipa_polymorphic_call_context::combine_speculation_with
1924 : (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1925 : tree otr_type)
1926 : {
1927 4722849 : if (!new_outer_type)
1928 : return false;
1929 :
1930 : /* restrict_to_inner_class may eliminate wrong speculation making our job
1931 : easier. */
1932 4638831 : if (otr_type)
1933 4090 : restrict_to_inner_class (otr_type);
1934 :
1935 4638831 : if (!speculation_consistent_p (new_outer_type, new_offset,
1936 : new_maybe_derived_type, otr_type))
1937 : return false;
1938 :
1939 : /* New speculation is a win in case we have no speculation or new
1940 : speculation does not consider derivations. */
1941 387660 : if (!speculative_outer_type
1942 95240 : || (speculative_maybe_derived_type
1943 95006 : && !new_maybe_derived_type))
1944 : {
1945 292722 : speculative_outer_type = new_outer_type;
1946 292722 : speculative_offset = new_offset;
1947 292722 : speculative_maybe_derived_type = new_maybe_derived_type;
1948 292722 : return true;
1949 : }
1950 94938 : else if (types_must_be_same_for_odr (speculative_outer_type,
1951 : new_outer_type))
1952 : {
1953 73647 : if (speculative_offset != new_offset)
1954 : {
1955 : /* OK we have two contexts that seems valid but they disagree,
1956 : just give up.
1957 :
1958 : This is not a lattice operation, so we may want to drop it later. */
1959 4 : if (dump_file && (dump_flags & TDF_DETAILS))
1960 0 : fprintf (dump_file,
1961 : "Speculative outer types match, "
1962 : "offset mismatch -> invalid speculation\n");
1963 4 : clear_speculation ();
1964 4 : return true;
1965 : }
1966 : else
1967 : {
1968 73643 : if (speculative_maybe_derived_type && !new_maybe_derived_type)
1969 : {
1970 0 : speculative_maybe_derived_type = false;
1971 0 : return true;
1972 : }
1973 : else
1974 : return false;
1975 : }
1976 : }
1977 : /* Choose type that contains the other. This one either contains the outer
1978 : as a field (thus giving exactly one target) or is deeper in the type
1979 : hierarchy. */
1980 21291 : else if (speculative_outer_type
1981 21291 : && speculative_maybe_derived_type
1982 42381 : && (new_offset > speculative_offset
1983 20525 : || (new_offset == speculative_offset
1984 20477 : && contains_type_p (new_outer_type,
1985 : 0, speculative_outer_type, false))))
1986 : {
1987 1229 : tree old_outer_type = speculative_outer_type;
1988 1229 : HOST_WIDE_INT old_offset = speculative_offset;
1989 1229 : bool old_maybe_derived_type = speculative_maybe_derived_type;
1990 :
1991 1229 : speculative_outer_type = new_outer_type;
1992 1229 : speculative_offset = new_offset;
1993 1229 : speculative_maybe_derived_type = new_maybe_derived_type;
1994 :
1995 1229 : if (otr_type)
1996 0 : restrict_to_inner_class (otr_type);
1997 :
1998 : /* If the speculation turned out to make no sense, revert to sensible
1999 : one. */
2000 1229 : if (!speculative_outer_type)
2001 : {
2002 0 : speculative_outer_type = old_outer_type;
2003 0 : speculative_offset = old_offset;
2004 0 : speculative_maybe_derived_type = old_maybe_derived_type;
2005 0 : return false;
2006 : }
2007 1229 : return (old_offset != speculative_offset
2008 664 : || old_maybe_derived_type != speculative_maybe_derived_type
2009 1893 : || types_must_be_same_for_odr (speculative_outer_type,
2010 : new_outer_type));
2011 : }
2012 : return false;
2013 : }
2014 :
2015 : /* Make speculation less specific so
2016 : NEW_OUTER_TYPE, NEW_OFFSET, NEW_MAYBE_DERIVED_TYPE is also included.
2017 : If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
2018 :
2019 : bool
2020 73 : ipa_polymorphic_call_context::meet_speculation_with
2021 : (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
2022 : tree otr_type)
2023 : {
2024 73 : if (!new_outer_type && speculative_outer_type)
2025 : {
2026 2 : clear_speculation ();
2027 2 : return true;
2028 : }
2029 :
2030 : /* restrict_to_inner_class may eliminate wrong speculation making our job
2031 : easier. */
2032 71 : if (otr_type)
2033 0 : restrict_to_inner_class (otr_type);
2034 :
2035 71 : if (!speculative_outer_type
2036 95 : || !speculation_consistent_p (speculative_outer_type,
2037 : speculative_offset,
2038 24 : speculative_maybe_derived_type,
2039 : otr_type))
2040 : return false;
2041 :
2042 23 : if (!speculation_consistent_p (new_outer_type, new_offset,
2043 : new_maybe_derived_type, otr_type))
2044 : {
2045 0 : clear_speculation ();
2046 0 : return true;
2047 : }
2048 :
2049 23 : else if (types_must_be_same_for_odr (speculative_outer_type,
2050 : new_outer_type))
2051 : {
2052 4 : if (speculative_offset != new_offset)
2053 : {
2054 0 : clear_speculation ();
2055 0 : return true;
2056 : }
2057 : else
2058 : {
2059 4 : if (!speculative_maybe_derived_type && new_maybe_derived_type)
2060 : {
2061 0 : speculative_maybe_derived_type = true;
2062 0 : return true;
2063 : }
2064 : else
2065 : return false;
2066 : }
2067 : }
2068 : /* See if one type contains the other as a field (not base). */
2069 19 : else if (contains_type_p (new_outer_type, new_offset - speculative_offset,
2070 : speculative_outer_type, false, false))
2071 : return false;
2072 19 : else if (contains_type_p (speculative_outer_type,
2073 19 : speculative_offset - new_offset,
2074 : new_outer_type, false, false))
2075 : {
2076 0 : speculative_outer_type = new_outer_type;
2077 0 : speculative_offset = new_offset;
2078 0 : speculative_maybe_derived_type = new_maybe_derived_type;
2079 0 : return true;
2080 : }
2081 : /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2082 19 : else if (contains_type_p (new_outer_type,
2083 19 : new_offset - speculative_offset,
2084 : speculative_outer_type, false, true))
2085 : {
2086 19 : if (!speculative_maybe_derived_type)
2087 : {
2088 0 : speculative_maybe_derived_type = true;
2089 0 : return true;
2090 : }
2091 : return false;
2092 : }
2093 : /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2094 0 : else if (contains_type_p (speculative_outer_type,
2095 0 : speculative_offset - new_offset, new_outer_type, false, true))
2096 : {
2097 0 : speculative_outer_type = new_outer_type;
2098 0 : speculative_offset = new_offset;
2099 0 : speculative_maybe_derived_type = true;
2100 0 : return true;
2101 : }
2102 : else
2103 : {
2104 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2105 0 : fprintf (dump_file, "Giving up on speculative meet\n");
2106 0 : clear_speculation ();
2107 0 : return true;
2108 : }
2109 : }
2110 :
2111 : /* Assume that both THIS and a given context is valid and strengthen THIS
2112 : if possible. Return true if any strengthening was made.
2113 : If actual type the context is being used in is known, OTR_TYPE should be
2114 : set accordingly. This improves quality of combined result. */
2115 :
2116 : bool
2117 147144 : ipa_polymorphic_call_context::combine_with (ipa_polymorphic_call_context ctx,
2118 : tree otr_type)
2119 : {
2120 147144 : bool updated = false;
2121 :
2122 202944 : if (ctx.useless_p () || invalid)
2123 : return false;
2124 :
2125 : /* Restricting context to inner type makes merging easier, however do not
2126 : do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2127 146037 : if (otr_type && !invalid && !ctx.invalid)
2128 : {
2129 29596 : restrict_to_inner_class (otr_type);
2130 29596 : ctx.restrict_to_inner_class (otr_type);
2131 29596 : if(invalid)
2132 : return false;
2133 : }
2134 :
2135 145986 : if (dump_file && (dump_flags & TDF_DETAILS))
2136 : {
2137 101 : fprintf (dump_file, "Polymorphic call context combine:");
2138 101 : dump (dump_file);
2139 101 : fprintf (dump_file, "With context: ");
2140 101 : ctx.dump (dump_file);
2141 101 : if (otr_type)
2142 : {
2143 59 : fprintf (dump_file, "To be used with type: ");
2144 59 : print_generic_expr (dump_file, otr_type, TDF_SLIM);
2145 59 : fprintf (dump_file, "\n");
2146 : }
2147 : }
2148 :
2149 : /* If call is known to be invalid, we are done. */
2150 145986 : if (ctx.invalid)
2151 : {
2152 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2153 0 : fprintf (dump_file, "-> Invalid context\n");
2154 0 : goto invalidate;
2155 : }
2156 :
2157 145986 : if (!ctx.outer_type)
2158 : ;
2159 90375 : else if (!outer_type)
2160 : {
2161 23113 : outer_type = ctx.outer_type;
2162 23113 : offset = ctx.offset;
2163 23113 : dynamic = ctx.dynamic;
2164 23113 : maybe_in_construction = ctx.maybe_in_construction;
2165 23113 : maybe_derived_type = ctx.maybe_derived_type;
2166 23113 : updated = true;
2167 : }
2168 : /* If types are known to be same, merging is quite easy. */
2169 67262 : else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2170 : {
2171 40903 : if (offset != ctx.offset
2172 4 : && TYPE_SIZE (outer_type)
2173 40907 : && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2174 : {
2175 4 : if (dump_file && (dump_flags & TDF_DETAILS))
2176 0 : fprintf (dump_file, "Outer types match, offset mismatch -> invalid\n");
2177 4 : clear_speculation ();
2178 4 : clear_outer_type ();
2179 4 : invalid = true;
2180 4 : return true;
2181 : }
2182 40899 : if (dump_file && (dump_flags & TDF_DETAILS))
2183 21 : fprintf (dump_file, "Outer types match, merging flags\n");
2184 40899 : if (maybe_in_construction && !ctx.maybe_in_construction)
2185 : {
2186 10146 : updated = true;
2187 10146 : maybe_in_construction = false;
2188 : }
2189 40899 : if (maybe_derived_type && !ctx.maybe_derived_type)
2190 : {
2191 2983 : updated = true;
2192 2983 : maybe_derived_type = false;
2193 : }
2194 40899 : if (dynamic && !ctx.dynamic)
2195 : {
2196 12065 : updated = true;
2197 12065 : dynamic = false;
2198 : }
2199 : }
2200 : /* If we know the type precisely, there is not much to improve. */
2201 26359 : else if (!maybe_derived_type && !maybe_in_construction
2202 15837 : && !ctx.maybe_derived_type && !ctx.maybe_in_construction)
2203 : {
2204 : /* It may be easy to check if second context permits the first
2205 : and set INVALID otherwise. This is not easy to do in general;
2206 : contains_type_p may return false negatives for non-comparable
2207 : types.
2208 :
2209 : If OTR_TYPE is known, we however can expect that
2210 : restrict_to_inner_class should have discovered the same base
2211 : type. */
2212 1049 : if (otr_type && !ctx.maybe_in_construction && !ctx.maybe_derived_type)
2213 : {
2214 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2215 0 : fprintf (dump_file, "Contextes disagree -> invalid\n");
2216 0 : goto invalidate;
2217 : }
2218 : }
2219 : /* See if one type contains the other as a field (not base).
2220 : In this case we want to choose the wider type, because it contains
2221 : more information. */
2222 25310 : else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2223 : outer_type, false, false))
2224 : {
2225 3950 : if (dump_file && (dump_flags & TDF_DETAILS))
2226 0 : fprintf (dump_file, "Second type contain the first as a field\n");
2227 :
2228 3950 : if (maybe_derived_type)
2229 : {
2230 3513 : outer_type = ctx.outer_type;
2231 3513 : maybe_derived_type = ctx.maybe_derived_type;
2232 3513 : offset = ctx.offset;
2233 3513 : dynamic = ctx.dynamic;
2234 3513 : updated = true;
2235 : }
2236 :
2237 : /* If we do not know how the context is being used, we cannot
2238 : clear MAYBE_IN_CONSTRUCTION because it may be offsetted
2239 : to other component of OUTER_TYPE later and we know nothing
2240 : about it. */
2241 3950 : if (otr_type && maybe_in_construction
2242 0 : && !ctx.maybe_in_construction)
2243 : {
2244 0 : maybe_in_construction = false;
2245 0 : updated = true;
2246 : }
2247 : }
2248 21360 : else if (contains_type_p (outer_type, offset - ctx.offset,
2249 : ctx.outer_type, false, false))
2250 : {
2251 78 : if (dump_file && (dump_flags & TDF_DETAILS))
2252 0 : fprintf (dump_file, "First type contain the second as a field\n");
2253 :
2254 78 : if (otr_type && maybe_in_construction
2255 0 : && !ctx.maybe_in_construction)
2256 : {
2257 0 : maybe_in_construction = false;
2258 0 : updated = true;
2259 : }
2260 : }
2261 : /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2262 21282 : else if (contains_type_p (ctx.outer_type,
2263 21282 : ctx.offset - offset, outer_type, false, true))
2264 : {
2265 9801 : if (dump_file && (dump_flags & TDF_DETAILS))
2266 12 : fprintf (dump_file, "First type is base of second\n");
2267 9801 : if (!maybe_derived_type)
2268 : {
2269 4276 : if (!ctx.maybe_in_construction
2270 4276 : && types_odr_comparable (outer_type, ctx.outer_type))
2271 : {
2272 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2273 0 : fprintf (dump_file, "Second context does not permit base -> invalid\n");
2274 0 : goto invalidate;
2275 : }
2276 : }
2277 : /* Pick variant deeper in the hierarchy. */
2278 : else
2279 : {
2280 5525 : outer_type = ctx.outer_type;
2281 5525 : maybe_in_construction = ctx.maybe_in_construction;
2282 5525 : maybe_derived_type = ctx.maybe_derived_type;
2283 5525 : offset = ctx.offset;
2284 5525 : dynamic = ctx.dynamic;
2285 5525 : updated = true;
2286 : }
2287 : }
2288 : /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2289 11481 : else if (contains_type_p (outer_type,
2290 11481 : offset - ctx.offset, ctx.outer_type, false, true))
2291 : {
2292 11366 : if (dump_file && (dump_flags & TDF_DETAILS))
2293 50 : fprintf (dump_file, "Second type is base of first\n");
2294 11366 : if (!ctx.maybe_derived_type)
2295 : {
2296 139 : if (!maybe_in_construction
2297 139 : && types_odr_comparable (outer_type, ctx.outer_type))
2298 : {
2299 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2300 0 : fprintf (dump_file, "First context does not permit base -> invalid\n");
2301 0 : goto invalidate;
2302 : }
2303 : /* Pick the base type. */
2304 139 : else if (maybe_in_construction)
2305 : {
2306 139 : outer_type = ctx.outer_type;
2307 139 : maybe_in_construction = ctx.maybe_in_construction;
2308 139 : maybe_derived_type = ctx.maybe_derived_type;
2309 139 : offset = ctx.offset;
2310 139 : dynamic = ctx.dynamic;
2311 139 : updated = true;
2312 : }
2313 : }
2314 : }
2315 : /* TODO handle merging using hierarchy. */
2316 115 : else if (dump_file && (dump_flags & TDF_DETAILS))
2317 3 : fprintf (dump_file, "Giving up on merge\n");
2318 :
2319 291964 : updated |= combine_speculation_with (ctx.speculative_outer_type,
2320 : ctx.speculative_offset,
2321 145982 : ctx.speculative_maybe_derived_type,
2322 : otr_type);
2323 :
2324 145982 : if (updated && dump_file && (dump_flags & TDF_DETAILS))
2325 : {
2326 33 : fprintf (dump_file, "Updated as: ");
2327 33 : dump (dump_file);
2328 33 : fprintf (dump_file, "\n");
2329 : }
2330 : return updated;
2331 :
2332 0 : invalidate:
2333 0 : invalid = true;
2334 0 : clear_speculation ();
2335 0 : clear_outer_type ();
2336 0 : return true;
2337 : }
2338 :
2339 : /* Take non-speculative info, merge it with speculative and clear speculation.
2340 : Used when we no longer manage to keep track of actual outer type, but we
2341 : think it is still there.
2342 :
2343 : If OTR_TYPE is set, the transformation can be done more effectively assuming
2344 : that context is going to be used only that way. */
2345 :
2346 : void
2347 109355 : ipa_polymorphic_call_context::make_speculative (tree otr_type)
2348 : {
2349 109355 : tree spec_outer_type = outer_type;
2350 109355 : HOST_WIDE_INT spec_offset = offset;
2351 109355 : bool spec_maybe_derived_type = maybe_derived_type;
2352 :
2353 109355 : if (invalid)
2354 : {
2355 0 : invalid = false;
2356 0 : clear_outer_type ();
2357 0 : clear_speculation ();
2358 0 : return;
2359 : }
2360 109355 : if (!outer_type)
2361 : return;
2362 33535 : clear_outer_type ();
2363 33535 : combine_speculation_with (spec_outer_type, spec_offset,
2364 : spec_maybe_derived_type,
2365 : otr_type);
2366 : }
2367 :
2368 : /* Use when we cannot track dynamic type change. This speculatively assume
2369 : type change is not happening. */
2370 :
2371 : void
2372 131078 : ipa_polymorphic_call_context::possible_dynamic_type_change (bool in_poly_cdtor,
2373 : tree otr_type)
2374 : {
2375 131078 : if (dynamic)
2376 : {
2377 : /* See if existing speculation was inconsistent before type change.
2378 : If so drop it first, so we do not lose track about it being
2379 : impossible. */
2380 109322 : if (speculative_outer_type
2381 109322 : && !speculation_consistent_p (speculative_outer_type,
2382 : speculative_offset,
2383 : speculative_maybe_derived_type,
2384 : otr_type))
2385 8443 : clear_speculation ();
2386 109322 : make_speculative (otr_type);
2387 : }
2388 21756 : else if (in_poly_cdtor)
2389 11849 : maybe_in_construction = true;
2390 131078 : }
2391 :
2392 : /* Return TRUE if this context conveys the same information as OTHER. */
2393 :
2394 : bool
2395 148074 : ipa_polymorphic_call_context::equal_to
2396 : (const ipa_polymorphic_call_context &x) const
2397 : {
2398 148074 : if (useless_p ())
2399 119 : return x.useless_p ();
2400 147955 : if (invalid)
2401 0 : return x.invalid;
2402 277207 : if (x.useless_p () || x.invalid)
2403 : return false;
2404 :
2405 147939 : if (outer_type)
2406 : {
2407 18379 : if (!x.outer_type
2408 18038 : || !types_odr_comparable (outer_type, x.outer_type)
2409 18038 : || !types_same_for_odr (outer_type, x.outer_type)
2410 17659 : || offset != x.offset
2411 17629 : || maybe_in_construction != x.maybe_in_construction
2412 17618 : || maybe_derived_type != x.maybe_derived_type
2413 35934 : || dynamic != x.dynamic)
2414 : return false;
2415 : }
2416 129560 : else if (x.outer_type)
2417 : return false;
2418 :
2419 :
2420 146340 : if (speculative_outer_type
2421 277284 : && speculation_consistent_p (speculative_outer_type, speculative_offset,
2422 130944 : speculative_maybe_derived_type, NULL_TREE))
2423 : {
2424 127698 : if (!x.speculative_outer_type)
2425 : return false;
2426 :
2427 127698 : if (!types_odr_comparable (speculative_outer_type,
2428 : x.speculative_outer_type)
2429 127698 : || !types_same_for_odr (speculative_outer_type,
2430 127698 : x.speculative_outer_type)
2431 126991 : || speculative_offset != x.speculative_offset
2432 254625 : || speculative_maybe_derived_type != x.speculative_maybe_derived_type)
2433 : return false;
2434 : }
2435 18642 : else if (x.speculative_outer_type
2436 21943 : && x.speculation_consistent_p (x.speculative_outer_type,
2437 3301 : x.speculative_offset,
2438 3301 : x.speculative_maybe_derived_type,
2439 : NULL))
2440 : return false;
2441 :
2442 : return true;
2443 : }
2444 :
2445 : /* Modify context to be strictly less restrictive than CTX. */
2446 :
2447 : bool
2448 1610 : ipa_polymorphic_call_context::meet_with (ipa_polymorphic_call_context ctx,
2449 : tree otr_type)
2450 : {
2451 1610 : bool updated = false;
2452 :
2453 2804 : if (useless_p () || ctx.invalid)
2454 : return false;
2455 :
2456 : /* Restricting context to inner type makes merging easier, however do not
2457 : do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2458 1610 : if (otr_type && !useless_p () && !ctx.useless_p ())
2459 : {
2460 0 : restrict_to_inner_class (otr_type);
2461 0 : ctx.restrict_to_inner_class (otr_type);
2462 0 : if(invalid)
2463 : return false;
2464 : }
2465 :
2466 1610 : if (equal_to (ctx))
2467 : return false;
2468 :
2469 97 : if (ctx.useless_p () || invalid)
2470 : {
2471 16 : *this = ctx;
2472 16 : return true;
2473 : }
2474 :
2475 73 : if (dump_file && (dump_flags & TDF_DETAILS))
2476 : {
2477 0 : fprintf (dump_file, "Polymorphic call context meet:");
2478 0 : dump (dump_file);
2479 0 : fprintf (dump_file, "With context: ");
2480 0 : ctx.dump (dump_file);
2481 0 : if (otr_type)
2482 : {
2483 0 : fprintf (dump_file, "To be used with type: ");
2484 0 : print_generic_expr (dump_file, otr_type, TDF_SLIM);
2485 0 : fprintf (dump_file, "\n");
2486 : }
2487 : }
2488 :
2489 73 : if (!dynamic && ctx.dynamic)
2490 : {
2491 6 : dynamic = true;
2492 6 : updated = true;
2493 : }
2494 :
2495 : /* If a certain context is known to be invalid, we can move to comparing
2496 : speculation. Because the second context might be a certain one which can
2497 : be met with the speculation of the first, make it speculative. */
2498 73 : if (!outer_type)
2499 25 : ctx.make_speculative ();
2500 48 : else if (!ctx.outer_type)
2501 : {
2502 8 : make_speculative ();
2503 8 : updated = true;
2504 : }
2505 : /* If types are known to be same, merging is quite easy. */
2506 40 : else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2507 : {
2508 37 : if (offset != ctx.offset
2509 0 : && TYPE_SIZE (outer_type)
2510 37 : && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2511 : {
2512 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2513 0 : fprintf (dump_file, "Outer types match, offset mismatch -> clearing\n");
2514 0 : clear_outer_type ();
2515 0 : return true;
2516 : }
2517 37 : if (dump_file && (dump_flags & TDF_DETAILS))
2518 0 : fprintf (dump_file, "Outer types match, merging flags\n");
2519 37 : if (!maybe_in_construction && ctx.maybe_in_construction)
2520 : {
2521 0 : updated = true;
2522 0 : maybe_in_construction = true;
2523 : }
2524 37 : if (!maybe_derived_type && ctx.maybe_derived_type)
2525 : {
2526 6 : updated = true;
2527 6 : maybe_derived_type = true;
2528 : }
2529 37 : if (!dynamic && ctx.dynamic)
2530 : {
2531 0 : updated = true;
2532 0 : dynamic = true;
2533 : }
2534 : }
2535 : /* See if one type contains the other as a field (not base). */
2536 3 : else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2537 : outer_type, false, false))
2538 : {
2539 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2540 0 : fprintf (dump_file, "Second type contain the first as a field\n");
2541 :
2542 : /* The second type is more specified, so we keep the first.
2543 : We need to set DYNAMIC flag to avoid declaring context INVALID
2544 : of OFFSET ends up being out of range. */
2545 0 : if (!dynamic
2546 0 : && (ctx.dynamic
2547 0 : || (!otr_type
2548 0 : && (!TYPE_SIZE (ctx.outer_type)
2549 0 : || !TYPE_SIZE (outer_type)
2550 0 : || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2551 0 : TYPE_SIZE (outer_type), 0)))))
2552 : {
2553 0 : dynamic = true;
2554 0 : updated = true;
2555 : }
2556 : }
2557 3 : else if (contains_type_p (outer_type, offset - ctx.offset,
2558 : ctx.outer_type, false, false))
2559 : {
2560 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2561 0 : fprintf (dump_file, "First type contain the second as a field\n");
2562 :
2563 0 : if (!dynamic
2564 0 : && (ctx.dynamic
2565 0 : || (!otr_type
2566 0 : && (!TYPE_SIZE (ctx.outer_type)
2567 0 : || !TYPE_SIZE (outer_type)
2568 0 : || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2569 0 : TYPE_SIZE (outer_type), 0)))))
2570 : dynamic = true;
2571 0 : outer_type = ctx.outer_type;
2572 0 : offset = ctx.offset;
2573 0 : dynamic = ctx.dynamic;
2574 0 : maybe_in_construction = ctx.maybe_in_construction;
2575 0 : maybe_derived_type = ctx.maybe_derived_type;
2576 0 : updated = true;
2577 : }
2578 : /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2579 3 : else if (contains_type_p (ctx.outer_type,
2580 3 : ctx.offset - offset, outer_type, false, true))
2581 : {
2582 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2583 0 : fprintf (dump_file, "First type is base of second\n");
2584 0 : if (!maybe_derived_type)
2585 : {
2586 0 : maybe_derived_type = true;
2587 0 : updated = true;
2588 : }
2589 0 : if (!maybe_in_construction && ctx.maybe_in_construction)
2590 : {
2591 0 : maybe_in_construction = true;
2592 0 : updated = true;
2593 : }
2594 0 : if (!dynamic && ctx.dynamic)
2595 : {
2596 0 : dynamic = true;
2597 0 : updated = true;
2598 : }
2599 : }
2600 : /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2601 3 : else if (contains_type_p (outer_type,
2602 3 : offset - ctx.offset, ctx.outer_type, false, true))
2603 : {
2604 3 : if (dump_file && (dump_flags & TDF_DETAILS))
2605 0 : fprintf (dump_file, "Second type is base of first\n");
2606 3 : outer_type = ctx.outer_type;
2607 3 : offset = ctx.offset;
2608 3 : updated = true;
2609 3 : if (!maybe_derived_type)
2610 3 : maybe_derived_type = true;
2611 3 : if (!maybe_in_construction && ctx.maybe_in_construction)
2612 0 : maybe_in_construction = true;
2613 3 : if (!dynamic && ctx.dynamic)
2614 0 : dynamic = true;
2615 : }
2616 : /* TODO handle merging using hierarchy. */
2617 : else
2618 : {
2619 0 : if (dump_file && (dump_flags & TDF_DETAILS))
2620 0 : fprintf (dump_file, "Giving up on meet\n");
2621 0 : clear_outer_type ();
2622 0 : updated = true;
2623 : }
2624 :
2625 146 : updated |= meet_speculation_with (ctx.speculative_outer_type,
2626 : ctx.speculative_offset,
2627 73 : ctx.speculative_maybe_derived_type,
2628 : otr_type);
2629 :
2630 73 : if (updated && dump_file && (dump_flags & TDF_DETAILS))
2631 : {
2632 0 : fprintf (dump_file, "Updated as: ");
2633 0 : dump (dump_file);
2634 0 : fprintf (dump_file, "\n");
2635 : }
2636 : return updated;
2637 : }
|