Fix for ICE with -g on testcase with incomplete types.
[official-gcc.git] / gcc / ipa-polymorphic-call.c
blob9cf09b821da24a4688ff3f869d3feb32a48ea45f
1 /* Analysis of polymorphic call context.
2 Copyright (C) 2013-2015 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "rtl.h"
28 #include "alias.h"
29 #include "fold-const.h"
30 #include "print-tree.h"
31 #include "calls.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "expmed.h"
35 #include "dojump.h"
36 #include "explow.h"
37 #include "emit-rtl.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "expr.h"
41 #include "tree-pass.h"
42 #include "target.h"
43 #include "cgraph.h"
44 #include "ipa-utils.h"
45 #include "internal-fn.h"
46 #include "gimple-fold.h"
47 #include "alloc-pool.h"
48 #include "symbol-summary.h"
49 #include "ipa-prop.h"
50 #include "ipa-inline.h"
51 #include "diagnostic.h"
52 #include "tree-dfa.h"
53 #include "demangle.h"
54 #include "dbgcnt.h"
55 #include "gimple-pretty-print.h"
56 #include "stor-layout.h"
57 #include "intl.h"
58 #include "data-streamer.h"
59 #include "streamer-hooks.h"
60 #include "tree-ssa-operands.h"
61 #include "tree-into-ssa.h"
63 /* Return true when TYPE contains an polymorphic type and thus is interesting
64 for devirtualization machinery. */
66 static bool contains_type_p (tree, HOST_WIDE_INT, tree,
67 bool consider_placement_new = true,
68 bool consider_bases = true);
70 bool
71 contains_polymorphic_type_p (const_tree type)
73 type = TYPE_MAIN_VARIANT (type);
75 if (RECORD_OR_UNION_TYPE_P (type))
77 if (TYPE_BINFO (type)
78 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
79 return true;
80 for (tree fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
81 if (TREE_CODE (fld) == FIELD_DECL
82 && !DECL_ARTIFICIAL (fld)
83 && contains_polymorphic_type_p (TREE_TYPE (fld)))
84 return true;
85 return false;
87 if (TREE_CODE (type) == ARRAY_TYPE)
88 return contains_polymorphic_type_p (TREE_TYPE (type));
89 return false;
92 /* Return true if it seems valid to use placement new to build EXPECTED_TYPE
93 at possition CUR_OFFSET within TYPE.
95 POD can be changed to an instance of a polymorphic type by
96 placement new. Here we play safe and assume that any
97 non-polymorphic type is POD. */
98 bool
99 possible_placement_new (tree type, tree expected_type,
100 HOST_WIDE_INT cur_offset)
102 if (cur_offset < 0)
103 return true;
104 return ((TREE_CODE (type) != RECORD_TYPE
105 || !TYPE_BINFO (type)
106 || cur_offset >= POINTER_SIZE
107 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))
108 && (!TYPE_SIZE (type)
109 || !tree_fits_shwi_p (TYPE_SIZE (type))
110 || (cur_offset
111 + (expected_type ? tree_to_uhwi (TYPE_SIZE (expected_type))
112 : POINTER_SIZE)
113 <= tree_to_uhwi (TYPE_SIZE (type)))));
116 /* THIS->OUTER_TYPE is a type of memory object where object of OTR_TYPE
117 is contained at THIS->OFFSET. Walk the memory representation of
118 THIS->OUTER_TYPE and find the outermost class type that match
119 OTR_TYPE or contain OTR_TYPE as a base. Update THIS
120 to represent it.
122 If OTR_TYPE is NULL, just find outermost polymorphic type with
123 virtual table present at possition OFFSET.
125 For example when THIS represents type
126 class A
128 int a;
129 class B b;
131 and we look for type at offset sizeof(int), we end up with B and offset 0.
132 If the same is produced by multiple inheritance, we end up with A and offset
133 sizeof(int).
135 If we can not find corresponding class, give up by setting
136 THIS->OUTER_TYPE to OTR_TYPE and THIS->OFFSET to NULL.
137 Return true when lookup was sucesful.
139 When CONSIDER_PLACEMENT_NEW is false, reject contexts that may be made
140 valid only via alocation of new polymorphic type inside by means
141 of placement new.
143 When CONSIDER_BASES is false, only look for actual fields, not base types
144 of TYPE. */
146 bool
147 ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type,
148 bool consider_placement_new,
149 bool consider_bases)
151 tree type = outer_type;
152 HOST_WIDE_INT cur_offset = offset;
153 bool speculative = false;
154 bool size_unknown = false;
155 unsigned HOST_WIDE_INT otr_type_size = POINTER_SIZE;
157 /* Update OUTER_TYPE to match EXPECTED_TYPE if it is not set. */
158 if (!outer_type)
160 clear_outer_type (otr_type);
161 type = otr_type;
162 cur_offset = 0;
164 /* See if OFFSET points inside OUTER_TYPE. If it does not, we know
165 that the context is either invalid, or the instance type must be
166 derived from OUTER_TYPE.
168 Because the instance type may contain field whose type is of OUTER_TYPE,
169 we can not derive any effective information about it.
171 TODO: In the case we know all derrived types, we can definitely do better
172 here. */
173 else if (TYPE_SIZE (outer_type)
174 && tree_fits_shwi_p (TYPE_SIZE (outer_type))
175 && tree_to_shwi (TYPE_SIZE (outer_type)) >= 0
176 && tree_to_shwi (TYPE_SIZE (outer_type)) <= offset)
178 clear_outer_type (otr_type);
179 type = otr_type;
180 cur_offset = 0;
182 /* If derived type is not allowed, we know that the context is invalid.
183 For dynamic types, we really do not have information about
184 size of the memory location. It is possible that completely
185 different type is stored after outer_type. */
186 if (!maybe_derived_type && !dynamic)
188 clear_speculation ();
189 invalid = true;
190 return false;
194 if (otr_type && TYPE_SIZE (otr_type)
195 && tree_fits_shwi_p (TYPE_SIZE (otr_type)))
196 otr_type_size = tree_to_uhwi (TYPE_SIZE (otr_type));
198 if (!type || offset < 0)
199 goto no_useful_type_info;
201 /* Find the sub-object the constant actually refers to and mark whether it is
202 an artificial one (as opposed to a user-defined one).
204 This loop is performed twice; first time for outer_type and second time
205 for speculative_outer_type. The second run has SPECULATIVE set. */
206 while (true)
208 unsigned HOST_WIDE_INT pos, size;
209 tree fld;
211 /* If we do not know size of TYPE, we need to be more conservative
212 about accepting cases where we can not find EXPECTED_TYPE.
213 Generally the types that do matter here are of constant size.
214 Size_unknown case should be very rare. */
215 if (TYPE_SIZE (type)
216 && tree_fits_shwi_p (TYPE_SIZE (type))
217 && tree_to_shwi (TYPE_SIZE (type)) >= 0)
218 size_unknown = false;
219 else
220 size_unknown = true;
222 /* On a match, just return what we found. */
223 if ((otr_type
224 && types_odr_comparable (type, otr_type)
225 && types_same_for_odr (type, otr_type))
226 || (!otr_type
227 && TREE_CODE (type) == RECORD_TYPE
228 && TYPE_BINFO (type)
229 && polymorphic_type_binfo_p (TYPE_BINFO (type))))
231 if (speculative)
233 /* If we did not match the offset, just give up on speculation. */
234 if (cur_offset != 0
235 /* Also check if speculation did not end up being same as
236 non-speculation. */
237 || (types_must_be_same_for_odr (speculative_outer_type,
238 outer_type)
239 && (maybe_derived_type
240 == speculative_maybe_derived_type)))
241 clear_speculation ();
242 return true;
244 else
246 /* If type is known to be final, do not worry about derived
247 types. Testing it here may help us to avoid speculation. */
248 if (otr_type && TREE_CODE (outer_type) == RECORD_TYPE
249 && (!in_lto_p || odr_type_p (outer_type))
250 && type_with_linkage_p (outer_type)
251 && type_known_to_have_no_derivations_p (outer_type))
252 maybe_derived_type = false;
254 /* Type can not contain itself on an non-zero offset. In that case
255 just give up. Still accept the case where size is now known.
256 Either the second copy may appear past the end of type or within
257 the non-POD buffer located inside the variably sized type
258 itself. */
259 if (cur_offset != 0)
260 goto no_useful_type_info;
261 /* If we determined type precisely or we have no clue on
262 speuclation, we are done. */
263 if (!maybe_derived_type || !speculative_outer_type
264 || !speculation_consistent_p (speculative_outer_type,
265 speculative_offset,
266 speculative_maybe_derived_type,
267 otr_type))
269 clear_speculation ();
270 return true;
272 /* Otherwise look into speculation now. */
273 else
275 speculative = true;
276 type = speculative_outer_type;
277 cur_offset = speculative_offset;
278 continue;
283 /* Walk fields and find corresponding on at OFFSET. */
284 if (TREE_CODE (type) == RECORD_TYPE)
286 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
288 if (TREE_CODE (fld) != FIELD_DECL)
289 continue;
291 pos = int_bit_position (fld);
292 if (pos > (unsigned HOST_WIDE_INT)cur_offset)
293 continue;
295 /* Do not consider vptr itself. Not even for placement new. */
296 if (!pos && DECL_ARTIFICIAL (fld)
297 && POINTER_TYPE_P (TREE_TYPE (fld))
298 && TYPE_BINFO (type)
299 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
300 continue;
302 if (!DECL_SIZE (fld) || !tree_fits_uhwi_p (DECL_SIZE (fld)))
303 goto no_useful_type_info;
304 size = tree_to_uhwi (DECL_SIZE (fld));
306 /* We can always skip types smaller than pointer size:
307 those can not contain a virtual table pointer.
309 Disqualifying fields that are too small to fit OTR_TYPE
310 saves work needed to walk them for no benefit.
311 Because of the way the bases are packed into a class, the
312 field's size may be smaller than type size, so it needs
313 to be done with a care. */
315 if (pos <= (unsigned HOST_WIDE_INT)cur_offset
316 && (pos + size) >= (unsigned HOST_WIDE_INT)cur_offset
317 + POINTER_SIZE
318 && (!otr_type
319 || !TYPE_SIZE (TREE_TYPE (fld))
320 || !tree_fits_shwi_p (TYPE_SIZE (TREE_TYPE (fld)))
321 || (pos + tree_to_uhwi (TYPE_SIZE (TREE_TYPE (fld))))
322 >= cur_offset + otr_type_size))
323 break;
326 if (!fld)
327 goto no_useful_type_info;
329 type = TYPE_MAIN_VARIANT (TREE_TYPE (fld));
330 cur_offset -= pos;
331 /* DECL_ARTIFICIAL represents a basetype. */
332 if (!DECL_ARTIFICIAL (fld))
334 if (!speculative)
336 outer_type = type;
337 offset = cur_offset;
338 /* As soon as we se an field containing the type,
339 we know we are not looking for derivations. */
340 maybe_derived_type = false;
342 else
344 speculative_outer_type = type;
345 speculative_offset = cur_offset;
346 speculative_maybe_derived_type = false;
349 else if (!consider_bases)
350 goto no_useful_type_info;
352 else if (TREE_CODE (type) == ARRAY_TYPE)
354 tree subtype = TYPE_MAIN_VARIANT (TREE_TYPE (type));
356 /* Give up if we don't know array field size.
357 Also give up on non-polymorphic types as they are used
358 as buffers for placement new. */
359 if (!TYPE_SIZE (subtype)
360 || !tree_fits_shwi_p (TYPE_SIZE (subtype))
361 || tree_to_shwi (TYPE_SIZE (subtype)) <= 0
362 || !contains_polymorphic_type_p (subtype))
363 goto no_useful_type_info;
365 HOST_WIDE_INT new_offset = cur_offset % tree_to_shwi (TYPE_SIZE (subtype));
367 /* We may see buffer for placement new. In this case the expected type
368 can be bigger than the subtype. */
369 if (TYPE_SIZE (subtype)
370 && (cur_offset + otr_type_size
371 > tree_to_uhwi (TYPE_SIZE (subtype))))
372 goto no_useful_type_info;
374 cur_offset = new_offset;
375 type = TYPE_MAIN_VARIANT (subtype);
376 if (!speculative)
378 outer_type = type;
379 offset = cur_offset;
380 maybe_derived_type = false;
382 else
384 speculative_outer_type = type;
385 speculative_offset = cur_offset;
386 speculative_maybe_derived_type = false;
389 /* Give up on anything else. */
390 else
392 no_useful_type_info:
393 if (maybe_derived_type && !speculative
394 && TREE_CODE (outer_type) == RECORD_TYPE
395 && TREE_CODE (otr_type) == RECORD_TYPE
396 && TYPE_BINFO (otr_type)
397 && !offset
398 && get_binfo_at_offset (TYPE_BINFO (otr_type), 0, outer_type))
400 clear_outer_type (otr_type);
401 if (!speculative_outer_type
402 || !speculation_consistent_p (speculative_outer_type,
403 speculative_offset,
404 speculative_maybe_derived_type,
405 otr_type))
406 clear_speculation ();
407 if (speculative_outer_type)
409 speculative = true;
410 type = speculative_outer_type;
411 cur_offset = speculative_offset;
413 else
414 return true;
416 /* We found no way to embedd EXPECTED_TYPE in TYPE.
417 We still permit two special cases - placement new and
418 the case of variadic types containing themselves. */
419 if (!speculative
420 && consider_placement_new
421 && (size_unknown || !type || maybe_derived_type
422 || possible_placement_new (type, otr_type, cur_offset)))
424 /* In these weird cases we want to accept the context.
425 In non-speculative run we have no useful outer_type info
426 (TODO: we may eventually want to record upper bound on the
427 type size that can be used to prune the walk),
428 but we still want to consider speculation that may
429 give useful info. */
430 if (!speculative)
432 clear_outer_type (otr_type);
433 if (!speculative_outer_type
434 || !speculation_consistent_p (speculative_outer_type,
435 speculative_offset,
436 speculative_maybe_derived_type,
437 otr_type))
438 clear_speculation ();
439 if (speculative_outer_type)
441 speculative = true;
442 type = speculative_outer_type;
443 cur_offset = speculative_offset;
445 else
446 return true;
448 else
449 clear_speculation ();
450 return true;
452 else
454 clear_speculation ();
455 if (speculative)
456 return true;
457 clear_outer_type (otr_type);
458 invalid = true;
459 return false;
465 /* Return true if OUTER_TYPE contains OTR_TYPE at OFFSET.
466 CONSIDER_PLACEMENT_NEW makes function to accept cases where OTR_TYPE can
467 be built within OUTER_TYPE by means of placement new. CONSIDER_BASES makes
468 function to accept cases where OTR_TYPE appears as base of OUTER_TYPE or as
469 base of one of fields of OUTER_TYPE. */
471 static bool
472 contains_type_p (tree outer_type, HOST_WIDE_INT offset,
473 tree otr_type,
474 bool consider_placement_new,
475 bool consider_bases)
477 ipa_polymorphic_call_context context;
479 /* Check that type is within range. */
480 if (offset < 0)
481 return false;
482 if (TYPE_SIZE (outer_type) && TYPE_SIZE (otr_type)
483 && TREE_CODE (outer_type) == INTEGER_CST
484 && TREE_CODE (otr_type) == INTEGER_CST
485 && wi::ltu_p (wi::to_offset (outer_type), (wi::to_offset (otr_type) + offset)))
486 return false;
488 context.offset = offset;
489 context.outer_type = TYPE_MAIN_VARIANT (outer_type);
490 context.maybe_derived_type = false;
491 return context.restrict_to_inner_class (otr_type, consider_placement_new, consider_bases);
495 /* Return a FUNCTION_DECL if BLOCK represents a constructor or destructor.
496 If CHECK_CLONES is true, also check for clones of ctor/dtors. */
498 tree
499 inlined_polymorphic_ctor_dtor_block_p (tree block, bool check_clones)
501 tree fn = BLOCK_ABSTRACT_ORIGIN (block);
502 if (fn == NULL || TREE_CODE (fn) != FUNCTION_DECL)
503 return NULL_TREE;
505 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
506 || (!DECL_CXX_CONSTRUCTOR_P (fn) && !DECL_CXX_DESTRUCTOR_P (fn)))
508 if (!check_clones)
509 return NULL_TREE;
511 /* Watch for clones where we constant propagated the first
512 argument (pointer to the instance). */
513 fn = DECL_ABSTRACT_ORIGIN (fn);
514 if (!fn
515 || TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
516 || (!DECL_CXX_CONSTRUCTOR_P (fn) && !DECL_CXX_DESTRUCTOR_P (fn)))
517 return NULL_TREE;
520 if (flags_from_decl_or_type (fn) & (ECF_PURE | ECF_CONST))
521 return NULL_TREE;
523 return fn;
527 /* We know that the instance is stored in variable or parameter
528 (not dynamically allocated) and we want to disprove the fact
529 that it may be in construction at invocation of CALL.
531 BASE represents memory location where instance is stored.
532 If BASE is NULL, it is assumed to be global memory.
533 OUTER_TYPE is known type of the instance or NULL if not
534 known.
536 For the variable to be in construction we actually need to
537 be in constructor of corresponding global variable or
538 the inline stack of CALL must contain the constructor.
539 Check this condition. This check works safely only before
540 IPA passes, because inline stacks may become out of date
541 later. */
543 bool
544 decl_maybe_in_construction_p (tree base, tree outer_type,
545 gimple *call, tree function)
547 if (outer_type)
548 outer_type = TYPE_MAIN_VARIANT (outer_type);
549 gcc_assert (!base || DECL_P (base));
551 /* After inlining the code unification optimizations may invalidate
552 inline stacks. Also we need to give up on global variables after
553 IPA, because addresses of these may have been propagated to their
554 constructors. */
555 if (DECL_STRUCT_FUNCTION (function)->after_inlining)
556 return true;
558 /* Pure functions can not do any changes on the dynamic type;
559 that require writting to memory. */
560 if ((!base || !auto_var_in_fn_p (base, function))
561 && flags_from_decl_or_type (function) & (ECF_PURE | ECF_CONST))
562 return false;
564 bool check_clones = !base || is_global_var (base);
565 for (tree block = gimple_block (call); block && TREE_CODE (block) == BLOCK;
566 block = BLOCK_SUPERCONTEXT (block))
567 if (tree fn = inlined_polymorphic_ctor_dtor_block_p (block, check_clones))
569 tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
571 if (!outer_type || !types_odr_comparable (type, outer_type))
573 if (TREE_CODE (type) == RECORD_TYPE
574 && TYPE_BINFO (type)
575 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
576 return true;
578 else if (types_same_for_odr (type, outer_type))
579 return true;
582 if (!base || (TREE_CODE (base) == VAR_DECL && is_global_var (base)))
584 if (TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
585 || (!DECL_CXX_CONSTRUCTOR_P (function)
586 && !DECL_CXX_DESTRUCTOR_P (function)))
588 if (!DECL_ABSTRACT_ORIGIN (function))
589 return false;
590 /* Watch for clones where we constant propagated the first
591 argument (pointer to the instance). */
592 function = DECL_ABSTRACT_ORIGIN (function);
593 if (!function
594 || TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
595 || (!DECL_CXX_CONSTRUCTOR_P (function)
596 && !DECL_CXX_DESTRUCTOR_P (function)))
597 return false;
599 tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (function));
600 if (!outer_type || !types_odr_comparable (type, outer_type))
602 if (TREE_CODE (type) == RECORD_TYPE
603 && TYPE_BINFO (type)
604 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
605 return true;
607 else if (types_same_for_odr (type, outer_type))
608 return true;
610 return false;
613 /* Dump human readable context to F. If NEWLINE is true, it will be terminated
614 by a newline. */
616 void
617 ipa_polymorphic_call_context::dump (FILE *f, bool newline) const
619 fprintf (f, " ");
620 if (invalid)
621 fprintf (f, "Call is known to be undefined");
622 else
624 if (useless_p ())
625 fprintf (f, "nothing known");
626 if (outer_type || offset)
628 fprintf (f, "Outer type%s:", dynamic ? " (dynamic)":"");
629 print_generic_expr (f, outer_type, TDF_SLIM);
630 if (maybe_derived_type)
631 fprintf (f, " (or a derived type)");
632 if (maybe_in_construction)
633 fprintf (f, " (maybe in construction)");
634 fprintf (f, " offset " HOST_WIDE_INT_PRINT_DEC,
635 offset);
637 if (speculative_outer_type)
639 if (outer_type || offset)
640 fprintf (f, " ");
641 fprintf (f, "Speculative outer type:");
642 print_generic_expr (f, speculative_outer_type, TDF_SLIM);
643 if (speculative_maybe_derived_type)
644 fprintf (f, " (or a derived type)");
645 fprintf (f, " at offset " HOST_WIDE_INT_PRINT_DEC,
646 speculative_offset);
649 if (newline)
650 fprintf(f, "\n");
653 /* Print context to stderr. */
655 void
656 ipa_polymorphic_call_context::debug () const
658 dump (stderr);
661 /* Stream out the context to OB. */
663 void
664 ipa_polymorphic_call_context::stream_out (struct output_block *ob) const
666 struct bitpack_d bp = bitpack_create (ob->main_stream);
668 bp_pack_value (&bp, invalid, 1);
669 bp_pack_value (&bp, maybe_in_construction, 1);
670 bp_pack_value (&bp, maybe_derived_type, 1);
671 bp_pack_value (&bp, speculative_maybe_derived_type, 1);
672 bp_pack_value (&bp, dynamic, 1);
673 bp_pack_value (&bp, outer_type != NULL, 1);
674 bp_pack_value (&bp, offset != 0, 1);
675 bp_pack_value (&bp, speculative_outer_type != NULL, 1);
676 streamer_write_bitpack (&bp);
678 if (outer_type != NULL)
679 stream_write_tree (ob, outer_type, true);
680 if (offset)
681 streamer_write_hwi (ob, offset);
682 if (speculative_outer_type != NULL)
684 stream_write_tree (ob, speculative_outer_type, true);
685 streamer_write_hwi (ob, speculative_offset);
687 else
688 gcc_assert (!speculative_offset);
691 /* Stream in the context from IB and DATA_IN. */
693 void
694 ipa_polymorphic_call_context::stream_in (struct lto_input_block *ib,
695 struct data_in *data_in)
697 struct bitpack_d bp = streamer_read_bitpack (ib);
699 invalid = bp_unpack_value (&bp, 1);
700 maybe_in_construction = bp_unpack_value (&bp, 1);
701 maybe_derived_type = bp_unpack_value (&bp, 1);
702 speculative_maybe_derived_type = bp_unpack_value (&bp, 1);
703 dynamic = bp_unpack_value (&bp, 1);
704 bool outer_type_p = bp_unpack_value (&bp, 1);
705 bool offset_p = bp_unpack_value (&bp, 1);
706 bool speculative_outer_type_p = bp_unpack_value (&bp, 1);
708 if (outer_type_p)
709 outer_type = stream_read_tree (ib, data_in);
710 else
711 outer_type = NULL;
712 if (offset_p)
713 offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
714 else
715 offset = 0;
716 if (speculative_outer_type_p)
718 speculative_outer_type = stream_read_tree (ib, data_in);
719 speculative_offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
721 else
723 speculative_outer_type = NULL;
724 speculative_offset = 0;
728 /* Proudce polymorphic call context for call method of instance
729 that is located within BASE (that is assumed to be a decl) at offset OFF. */
731 void
732 ipa_polymorphic_call_context::set_by_decl (tree base, HOST_WIDE_INT off)
734 gcc_assert (DECL_P (base));
735 clear_speculation ();
737 if (!contains_polymorphic_type_p (TREE_TYPE (base)))
739 clear_outer_type ();
740 offset = off;
741 return;
743 outer_type = TYPE_MAIN_VARIANT (TREE_TYPE (base));
744 offset = off;
745 /* Make very conservative assumption that all objects
746 may be in construction.
748 It is up to caller to revisit this via
749 get_dynamic_type or decl_maybe_in_construction_p. */
750 maybe_in_construction = true;
751 maybe_derived_type = false;
752 dynamic = false;
755 /* CST is an invariant (address of decl), try to get meaningful
756 polymorphic call context for polymorphic call of method
757 if instance of OTR_TYPE that is located at offset OFF of this invariant.
758 Return FALSE if nothing meaningful can be found. */
760 bool
761 ipa_polymorphic_call_context::set_by_invariant (tree cst,
762 tree otr_type,
763 HOST_WIDE_INT off)
765 HOST_WIDE_INT offset2, size, max_size;
766 tree base;
768 invalid = false;
769 off = 0;
770 clear_outer_type (otr_type);
772 if (TREE_CODE (cst) != ADDR_EXPR)
773 return false;
775 cst = TREE_OPERAND (cst, 0);
776 base = get_ref_base_and_extent (cst, &offset2, &size, &max_size);
777 if (!DECL_P (base) || max_size == -1 || max_size != size)
778 return false;
780 /* Only type inconsistent programs can have otr_type that is
781 not part of outer type. */
782 if (otr_type && !contains_type_p (TREE_TYPE (base), off, otr_type))
783 return false;
785 set_by_decl (base, off);
786 return true;
789 /* See if OP is SSA name initialized as a copy or by single assignment.
790 If so, walk the SSA graph up. Because simple PHI conditional is considered
791 copy, GLOBAL_VISITED may be used to avoid infinite loop walking the SSA
792 graph. */
794 static tree
795 walk_ssa_copies (tree op, hash_set<tree> **global_visited = NULL)
797 hash_set <tree> *visited = NULL;
798 STRIP_NOPS (op);
799 while (TREE_CODE (op) == SSA_NAME
800 && !SSA_NAME_IS_DEFAULT_DEF (op)
801 /* We might be called via fold_stmt during cfgcleanup where
802 SSA form need not be up-to-date. */
803 && !name_registered_for_update_p (op)
804 && (gimple_assign_single_p (SSA_NAME_DEF_STMT (op))
805 || gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI))
807 if (global_visited)
809 if (!*global_visited)
810 *global_visited = new hash_set<tree>;
811 if ((*global_visited)->add (op))
812 goto done;
814 else
816 if (!visited)
817 visited = new hash_set<tree>;
818 if (visited->add (op))
819 goto done;
821 /* Special case
822 if (ptr == 0)
823 ptr = 0;
824 else
825 ptr = ptr.foo;
826 This pattern is implicitly produced for casts to non-primary
827 bases. When doing context analysis, we do not really care
828 about the case pointer is NULL, becuase the call will be
829 undefined anyway. */
830 if (gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI)
832 gimple *phi = SSA_NAME_DEF_STMT (op);
834 if (gimple_phi_num_args (phi) > 2)
835 goto done;
836 if (gimple_phi_num_args (phi) == 1)
837 op = gimple_phi_arg_def (phi, 0);
838 else if (integer_zerop (gimple_phi_arg_def (phi, 0)))
839 op = gimple_phi_arg_def (phi, 1);
840 else if (integer_zerop (gimple_phi_arg_def (phi, 1)))
841 op = gimple_phi_arg_def (phi, 0);
842 else
843 goto done;
845 else
847 if (gimple_assign_load_p (SSA_NAME_DEF_STMT (op)))
848 goto done;
849 op = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op));
851 STRIP_NOPS (op);
853 done:
854 if (visited)
855 delete (visited);
856 return op;
859 /* Create polymorphic call context from IP invariant CST.
860 This is typically &global_var.
861 OTR_TYPE specify type of polymorphic call or NULL if unknown, OFF
862 is offset of call. */
864 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree cst,
865 tree otr_type,
866 HOST_WIDE_INT off)
868 clear_speculation ();
869 set_by_invariant (cst, otr_type, off);
872 /* Build context for pointer REF contained in FNDECL at statement STMT.
873 if INSTANCE is non-NULL, return pointer to the object described by
874 the context or DECL where context is contained in. */
876 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl,
877 tree ref,
878 gimple *stmt,
879 tree *instance)
881 tree otr_type = NULL;
882 tree base_pointer;
883 hash_set <tree> *visited = NULL;
885 if (TREE_CODE (ref) == OBJ_TYPE_REF)
887 otr_type = obj_type_ref_class (ref);
888 base_pointer = OBJ_TYPE_REF_OBJECT (ref);
890 else
891 base_pointer = ref;
893 /* Set up basic info in case we find nothing interesting in the analysis. */
894 clear_speculation ();
895 clear_outer_type (otr_type);
896 invalid = false;
898 /* Walk SSA for outer object. */
899 while (true)
901 base_pointer = walk_ssa_copies (base_pointer, &visited);
902 if (TREE_CODE (base_pointer) == ADDR_EXPR)
904 HOST_WIDE_INT size, max_size;
905 HOST_WIDE_INT offset2;
906 tree base = get_ref_base_and_extent (TREE_OPERAND (base_pointer, 0),
907 &offset2, &size, &max_size);
909 if (max_size != -1 && max_size == size)
910 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base)),
911 offset + offset2,
912 true,
913 NULL /* Do not change outer type. */);
915 /* If this is a varying address, punt. */
916 if ((TREE_CODE (base) == MEM_REF || DECL_P (base))
917 && max_size != -1
918 && max_size == size)
920 /* We found dereference of a pointer. Type of the pointer
921 and MEM_REF is meaningless, but we can look futher. */
922 if (TREE_CODE (base) == MEM_REF)
924 base_pointer = TREE_OPERAND (base, 0);
925 offset
926 += offset2 + mem_ref_offset (base).to_short_addr () * BITS_PER_UNIT;
927 outer_type = NULL;
929 /* We found base object. In this case the outer_type
930 is known. */
931 else if (DECL_P (base))
933 if (visited)
934 delete (visited);
935 /* Only type inconsistent programs can have otr_type that is
936 not part of outer type. */
937 if (otr_type
938 && !contains_type_p (TREE_TYPE (base),
939 offset + offset2, otr_type))
941 invalid = true;
942 if (instance)
943 *instance = base_pointer;
944 return;
946 set_by_decl (base, offset + offset2);
947 if (outer_type && maybe_in_construction && stmt)
948 maybe_in_construction
949 = decl_maybe_in_construction_p (base,
950 outer_type,
951 stmt,
952 fndecl);
953 if (instance)
954 *instance = base;
955 return;
957 else
958 break;
960 else
961 break;
963 else if (TREE_CODE (base_pointer) == POINTER_PLUS_EXPR
964 && tree_fits_uhwi_p (TREE_OPERAND (base_pointer, 1)))
966 offset += tree_to_shwi (TREE_OPERAND (base_pointer, 1))
967 * BITS_PER_UNIT;
968 base_pointer = TREE_OPERAND (base_pointer, 0);
970 else
971 break;
974 if (visited)
975 delete (visited);
977 /* Try to determine type of the outer object. */
978 if (TREE_CODE (base_pointer) == SSA_NAME
979 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
980 && TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL)
982 /* See if parameter is THIS pointer of a method. */
983 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE
984 && SSA_NAME_VAR (base_pointer) == DECL_ARGUMENTS (fndecl))
986 outer_type
987 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
988 gcc_assert (TREE_CODE (outer_type) == RECORD_TYPE
989 || TREE_CODE (outer_type) == UNION_TYPE);
991 /* Dynamic casting has possibly upcasted the type
992 in the hiearchy. In this case outer type is less
993 informative than inner type and we should forget
994 about it. */
995 if ((otr_type
996 && !contains_type_p (outer_type, offset,
997 otr_type))
998 || !contains_polymorphic_type_p (outer_type))
1000 outer_type = NULL;
1001 if (instance)
1002 *instance = base_pointer;
1003 return;
1006 dynamic = true;
1008 /* If the function is constructor or destructor, then
1009 the type is possibly in construction, but we know
1010 it is not derived type. */
1011 if (DECL_CXX_CONSTRUCTOR_P (fndecl)
1012 || DECL_CXX_DESTRUCTOR_P (fndecl))
1014 maybe_in_construction = true;
1015 maybe_derived_type = false;
1017 else
1019 maybe_derived_type = true;
1020 maybe_in_construction = false;
1022 if (instance)
1023 *instance = base_pointer;
1024 return;
1026 /* Non-PODs passed by value are really passed by invisible
1027 reference. In this case we also know the type of the
1028 object. */
1029 if (DECL_BY_REFERENCE (SSA_NAME_VAR (base_pointer)))
1031 outer_type
1032 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
1033 /* Only type inconsistent programs can have otr_type that is
1034 not part of outer type. */
1035 if (otr_type && !contains_type_p (outer_type, offset,
1036 otr_type))
1038 invalid = true;
1039 if (instance)
1040 *instance = base_pointer;
1041 return;
1043 /* Non-polymorphic types have no interest for us. */
1044 else if (!otr_type && !contains_polymorphic_type_p (outer_type))
1046 outer_type = NULL;
1047 if (instance)
1048 *instance = base_pointer;
1049 return;
1051 maybe_derived_type = false;
1052 maybe_in_construction = false;
1053 if (instance)
1054 *instance = base_pointer;
1055 return;
1059 tree base_type = TREE_TYPE (base_pointer);
1061 if (TREE_CODE (base_pointer) == SSA_NAME
1062 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
1063 && !(TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL
1064 || TREE_CODE (SSA_NAME_VAR (base_pointer)) == RESULT_DECL))
1066 invalid = true;
1067 if (instance)
1068 *instance = base_pointer;
1069 return;
1071 if (TREE_CODE (base_pointer) == SSA_NAME
1072 && SSA_NAME_DEF_STMT (base_pointer)
1073 && gimple_assign_single_p (SSA_NAME_DEF_STMT (base_pointer)))
1074 base_type = TREE_TYPE (gimple_assign_rhs1
1075 (SSA_NAME_DEF_STMT (base_pointer)));
1077 if (base_type && POINTER_TYPE_P (base_type))
1078 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base_type)),
1079 offset,
1080 true, NULL /* Do not change type here */);
1081 /* TODO: There are multiple ways to derive a type. For instance
1082 if BASE_POINTER is passed to an constructor call prior our refernece.
1083 We do not make this type of flow sensitive analysis yet. */
1084 if (instance)
1085 *instance = base_pointer;
1086 return;
1089 /* Structure to be passed in between detect_type_change and
1090 check_stmt_for_type_change. */
1092 struct type_change_info
1094 /* Offset into the object where there is the virtual method pointer we are
1095 looking for. */
1096 HOST_WIDE_INT offset;
1097 /* The declaration or SSA_NAME pointer of the base that we are checking for
1098 type change. */
1099 tree instance;
1100 /* The reference to virtual table pointer used. */
1101 tree vtbl_ptr_ref;
1102 tree otr_type;
1103 /* If we actually can tell the type that the object has changed to, it is
1104 stored in this field. Otherwise it remains NULL_TREE. */
1105 tree known_current_type;
1106 HOST_WIDE_INT known_current_offset;
1108 /* Set to true if dynamic type change has been detected. */
1109 bool type_maybe_changed;
1110 /* Set to true if multiple types have been encountered. known_current_type
1111 must be disregarded in that case. */
1112 bool multiple_types_encountered;
1113 /* Set to true if we possibly missed some dynamic type changes and we should
1114 consider the set to be speculative. */
1115 bool speculative;
1116 bool seen_unanalyzed_store;
1119 /* Return true if STMT is not call and can modify a virtual method table pointer.
1120 We take advantage of fact that vtable stores must appear within constructor
1121 and destructor functions. */
1123 static bool
1124 noncall_stmt_may_be_vtbl_ptr_store (gimple *stmt)
1126 if (is_gimple_assign (stmt))
1128 tree lhs = gimple_assign_lhs (stmt);
1130 if (gimple_clobber_p (stmt))
1131 return false;
1132 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
1134 if (flag_strict_aliasing
1135 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
1136 return false;
1138 if (TREE_CODE (lhs) == COMPONENT_REF
1139 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1140 return false;
1141 /* In the future we might want to use get_base_ref_and_offset to find
1142 if there is a field corresponding to the offset and if so, proceed
1143 almost like if it was a component ref. */
1147 /* Code unification may mess with inline stacks. */
1148 if (cfun->after_inlining)
1149 return true;
1151 /* Walk the inline stack and watch out for ctors/dtors.
1152 TODO: Maybe we can require the store to appear in toplevel
1153 block of CTOR/DTOR. */
1154 for (tree block = gimple_block (stmt); block && TREE_CODE (block) == BLOCK;
1155 block = BLOCK_SUPERCONTEXT (block))
1156 if (BLOCK_ABSTRACT_ORIGIN (block)
1157 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block)) == FUNCTION_DECL)
1158 return inlined_polymorphic_ctor_dtor_block_p (block, false);
1159 return (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
1160 && (DECL_CXX_CONSTRUCTOR_P (current_function_decl)
1161 || DECL_CXX_DESTRUCTOR_P (current_function_decl)));
1164 /* If STMT can be proved to be an assignment to the virtual method table
1165 pointer of ANALYZED_OBJ and the type associated with the new table
1166 identified, return the type. Otherwise return NULL_TREE if type changes
1167 in unknown way or ERROR_MARK_NODE if type is unchanged. */
1169 static tree
1170 extr_type_from_vtbl_ptr_store (gimple *stmt, struct type_change_info *tci,
1171 HOST_WIDE_INT *type_offset)
1173 HOST_WIDE_INT offset, size, max_size;
1174 tree lhs, rhs, base;
1176 if (!gimple_assign_single_p (stmt))
1177 return NULL_TREE;
1179 lhs = gimple_assign_lhs (stmt);
1180 rhs = gimple_assign_rhs1 (stmt);
1181 if (TREE_CODE (lhs) != COMPONENT_REF
1182 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1184 if (dump_file)
1185 fprintf (dump_file, " LHS is not virtual table.\n");
1186 return NULL_TREE;
1189 if (tci->vtbl_ptr_ref && operand_equal_p (lhs, tci->vtbl_ptr_ref, 0))
1191 else
1193 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1194 if (DECL_P (tci->instance))
1196 if (base != tci->instance)
1198 if (dump_file)
1200 fprintf (dump_file, " base:");
1201 print_generic_expr (dump_file, base, TDF_SLIM);
1202 fprintf (dump_file, " does not match instance:");
1203 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1204 fprintf (dump_file, "\n");
1206 return NULL_TREE;
1209 else if (TREE_CODE (base) == MEM_REF)
1211 if (!operand_equal_p (tci->instance, TREE_OPERAND (base, 0), 0))
1213 if (dump_file)
1215 fprintf (dump_file, " base mem ref:");
1216 print_generic_expr (dump_file, base, TDF_SLIM);
1217 fprintf (dump_file, " does not match instance:");
1218 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1219 fprintf (dump_file, "\n");
1221 return NULL_TREE;
1223 if (!integer_zerop (TREE_OPERAND (base, 1)))
1225 if (!tree_fits_shwi_p (TREE_OPERAND (base, 1)))
1227 if (dump_file)
1229 fprintf (dump_file, " base mem ref:");
1230 print_generic_expr (dump_file, base, TDF_SLIM);
1231 fprintf (dump_file, " has non-representable offset:");
1232 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1233 fprintf (dump_file, "\n");
1235 return NULL_TREE;
1237 else
1238 offset += tree_to_shwi (TREE_OPERAND (base, 1)) * BITS_PER_UNIT;
1241 else if (!operand_equal_p (tci->instance, base, 0)
1242 || tci->offset)
1244 if (dump_file)
1246 fprintf (dump_file, " base:");
1247 print_generic_expr (dump_file, base, TDF_SLIM);
1248 fprintf (dump_file, " does not match instance:");
1249 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1250 fprintf (dump_file, " with offset %i\n", (int)tci->offset);
1252 return tci->offset > POINTER_SIZE ? error_mark_node : NULL_TREE;
1254 if (offset != tci->offset
1255 || size != POINTER_SIZE
1256 || max_size != POINTER_SIZE)
1258 if (dump_file)
1259 fprintf (dump_file, " wrong offset %i!=%i or size %i\n",
1260 (int)offset, (int)tci->offset, (int)size);
1261 return offset + POINTER_SIZE <= tci->offset
1262 || (max_size != -1
1263 && tci->offset + POINTER_SIZE > offset + max_size)
1264 ? error_mark_node : NULL;
1268 tree vtable;
1269 unsigned HOST_WIDE_INT offset2;
1271 if (!vtable_pointer_value_to_vtable (rhs, &vtable, &offset2))
1273 if (dump_file)
1274 fprintf (dump_file, " Failed to lookup binfo\n");
1275 return NULL;
1278 tree binfo = subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
1279 offset2, vtable);
1280 if (!binfo)
1282 if (dump_file)
1283 fprintf (dump_file, " Construction vtable used\n");
1284 /* FIXME: We should suport construction contexts. */
1285 return NULL;
1288 *type_offset = tree_to_shwi (BINFO_OFFSET (binfo)) * BITS_PER_UNIT;
1289 return DECL_CONTEXT (vtable);
1292 /* Record dynamic type change of TCI to TYPE. */
1294 static void
1295 record_known_type (struct type_change_info *tci, tree type, HOST_WIDE_INT offset)
1297 if (dump_file)
1299 if (type)
1301 fprintf (dump_file, " Recording type: ");
1302 print_generic_expr (dump_file, type, TDF_SLIM);
1303 fprintf (dump_file, " at offset %i\n", (int)offset);
1305 else
1306 fprintf (dump_file, " Recording unknown type\n");
1309 /* If we found a constructor of type that is not polymorphic or
1310 that may contain the type in question as a field (not as base),
1311 restrict to the inner class first to make type matching bellow
1312 happier. */
1313 if (type
1314 && (offset
1315 || (TREE_CODE (type) != RECORD_TYPE
1316 || !TYPE_BINFO (type)
1317 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))))
1319 ipa_polymorphic_call_context context;
1321 context.offset = offset;
1322 context.outer_type = type;
1323 context.maybe_in_construction = false;
1324 context.maybe_derived_type = false;
1325 context.dynamic = true;
1326 /* If we failed to find the inner type, we know that the call
1327 would be undefined for type produced here. */
1328 if (!context.restrict_to_inner_class (tci->otr_type))
1330 if (dump_file)
1331 fprintf (dump_file, " Ignoring; does not contain otr_type\n");
1332 return;
1334 /* Watch for case we reached an POD type and anticipate placement
1335 new. */
1336 if (!context.maybe_derived_type)
1338 type = context.outer_type;
1339 offset = context.offset;
1342 if (tci->type_maybe_changed
1343 && (!types_same_for_odr (type, tci->known_current_type)
1344 || offset != tci->known_current_offset))
1345 tci->multiple_types_encountered = true;
1346 tci->known_current_type = TYPE_MAIN_VARIANT (type);
1347 tci->known_current_offset = offset;
1348 tci->type_maybe_changed = true;
1351 /* Callback of walk_aliased_vdefs and a helper function for
1352 detect_type_change to check whether a particular statement may modify
1353 the virtual table pointer, and if possible also determine the new type of
1354 the (sub-)object. It stores its result into DATA, which points to a
1355 type_change_info structure. */
1357 static bool
1358 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
1360 gimple *stmt = SSA_NAME_DEF_STMT (vdef);
1361 struct type_change_info *tci = (struct type_change_info *) data;
1362 tree fn;
1364 /* If we already gave up, just terminate the rest of walk. */
1365 if (tci->multiple_types_encountered)
1366 return true;
1368 if (is_gimple_call (stmt))
1370 if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
1371 return false;
1373 /* Check for a constructor call. */
1374 if ((fn = gimple_call_fndecl (stmt)) != NULL_TREE
1375 && DECL_CXX_CONSTRUCTOR_P (fn)
1376 && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1377 && gimple_call_num_args (stmt))
1379 tree op = walk_ssa_copies (gimple_call_arg (stmt, 0));
1380 tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
1381 HOST_WIDE_INT offset = 0, size, max_size;
1383 if (dump_file)
1385 fprintf (dump_file, " Checking constructor call: ");
1386 print_gimple_stmt (dump_file, stmt, 0, 0);
1389 /* See if THIS parameter seems like instance pointer. */
1390 if (TREE_CODE (op) == ADDR_EXPR)
1392 op = get_ref_base_and_extent (TREE_OPERAND (op, 0),
1393 &offset, &size, &max_size);
1394 if (size != max_size || max_size == -1)
1396 tci->speculative = true;
1397 return false;
1399 if (op && TREE_CODE (op) == MEM_REF)
1401 if (!tree_fits_shwi_p (TREE_OPERAND (op, 1)))
1403 tci->speculative = true;
1404 return false;
1406 offset += tree_to_shwi (TREE_OPERAND (op, 1))
1407 * BITS_PER_UNIT;
1408 op = TREE_OPERAND (op, 0);
1410 else if (DECL_P (op))
1412 else
1414 tci->speculative = true;
1415 return false;
1417 op = walk_ssa_copies (op);
1419 if (operand_equal_p (op, tci->instance, 0)
1420 && TYPE_SIZE (type)
1421 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1422 && tree_fits_shwi_p (TYPE_SIZE (type))
1423 && tree_to_shwi (TYPE_SIZE (type)) + offset > tci->offset
1424 /* Some inlined constructors may look as follows:
1425 _3 = operator new (16);
1426 MEM[(struct &)_3] ={v} {CLOBBER};
1427 MEM[(struct CompositeClass *)_3]._vptr.CompositeClass
1428 = &MEM[(void *)&_ZTV14CompositeClass + 16B];
1429 _7 = &MEM[(struct CompositeClass *)_3].object;
1430 EmptyClass::EmptyClass (_7);
1432 When determining dynamic type of _3 and because we stop at first
1433 dynamic type found, we would stop on EmptyClass::EmptyClass (_7).
1434 In this case the emptyclass is not even polymorphic and we miss
1435 it is contained in an outer type that is polymorphic. */
1437 && (tci->offset == offset || contains_polymorphic_type_p (type)))
1439 record_known_type (tci, type, tci->offset - offset);
1440 return true;
1443 /* Calls may possibly change dynamic type by placement new. Assume
1444 it will not happen, but make result speculative only. */
1445 if (dump_file)
1447 fprintf (dump_file, " Function call may change dynamic type:");
1448 print_gimple_stmt (dump_file, stmt, 0, 0);
1450 tci->speculative = true;
1451 return false;
1453 /* Check for inlined virtual table store. */
1454 else if (noncall_stmt_may_be_vtbl_ptr_store (stmt))
1456 tree type;
1457 HOST_WIDE_INT offset = 0;
1458 if (dump_file)
1460 fprintf (dump_file, " Checking vtbl store: ");
1461 print_gimple_stmt (dump_file, stmt, 0, 0);
1464 type = extr_type_from_vtbl_ptr_store (stmt, tci, &offset);
1465 if (type == error_mark_node)
1466 return false;
1467 gcc_assert (!type || TYPE_MAIN_VARIANT (type) == type);
1468 if (!type)
1470 if (dump_file)
1471 fprintf (dump_file, " Unanalyzed store may change type.\n");
1472 tci->seen_unanalyzed_store = true;
1473 tci->speculative = true;
1475 else
1476 record_known_type (tci, type, offset);
1477 return true;
1479 else
1480 return false;
1483 /* THIS is polymorphic call context obtained from get_polymorphic_context.
1484 OTR_OBJECT is pointer to the instance returned by OBJ_TYPE_REF_OBJECT.
1485 INSTANCE is pointer to the outer instance as returned by
1486 get_polymorphic_context. To avoid creation of temporary expressions,
1487 INSTANCE may also be an declaration of get_polymorphic_context found the
1488 value to be in static storage.
1490 If the type of instance is not fully determined
1491 (either OUTER_TYPE is unknown or MAYBE_IN_CONSTRUCTION/INCLUDE_DERIVED_TYPES
1492 is set), try to walk memory writes and find the actual construction of the
1493 instance.
1495 Return true if memory is unchanged from function entry.
1497 We do not include this analysis in the context analysis itself, because
1498 it needs memory SSA to be fully built and the walk may be expensive.
1499 So it is not suitable for use withing fold_stmt and similar uses. */
1501 bool
1502 ipa_polymorphic_call_context::get_dynamic_type (tree instance,
1503 tree otr_object,
1504 tree otr_type,
1505 gimple *call)
1507 struct type_change_info tci;
1508 ao_ref ao;
1509 bool function_entry_reached = false;
1510 tree instance_ref = NULL;
1511 gimple *stmt = call;
1512 /* Remember OFFSET before it is modified by restrict_to_inner_class.
1513 This is because we do not update INSTANCE when walking inwards. */
1514 HOST_WIDE_INT instance_offset = offset;
1515 tree instance_outer_type = outer_type;
1517 if (otr_type)
1518 otr_type = TYPE_MAIN_VARIANT (otr_type);
1520 /* Walk into inner type. This may clear maybe_derived_type and save us
1521 from useless work. It also makes later comparsions with static type
1522 easier. */
1523 if (outer_type && otr_type)
1525 if (!restrict_to_inner_class (otr_type))
1526 return false;
1529 if (!maybe_in_construction && !maybe_derived_type)
1530 return false;
1532 /* We need to obtain refernce to virtual table pointer. It is better
1533 to look it up in the code rather than build our own. This require bit
1534 of pattern matching, but we end up verifying that what we found is
1535 correct.
1537 What we pattern match is:
1539 tmp = instance->_vptr.A; // vtbl ptr load
1540 tmp2 = tmp[otr_token]; // vtable lookup
1541 OBJ_TYPE_REF(tmp2;instance->0) (instance);
1543 We want to start alias oracle walk from vtbl pointer load,
1544 but we may not be able to identify it, for example, when PRE moved the
1545 load around. */
1547 if (gimple_code (call) == GIMPLE_CALL)
1549 tree ref = gimple_call_fn (call);
1550 HOST_WIDE_INT offset2, size, max_size;
1552 if (TREE_CODE (ref) == OBJ_TYPE_REF)
1554 ref = OBJ_TYPE_REF_EXPR (ref);
1555 ref = walk_ssa_copies (ref);
1557 /* If call target is already known, no need to do the expensive
1558 memory walk. */
1559 if (is_gimple_min_invariant (ref))
1560 return false;
1562 /* Check if definition looks like vtable lookup. */
1563 if (TREE_CODE (ref) == SSA_NAME
1564 && !SSA_NAME_IS_DEFAULT_DEF (ref)
1565 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref))
1566 && TREE_CODE (gimple_assign_rhs1
1567 (SSA_NAME_DEF_STMT (ref))) == MEM_REF)
1569 ref = get_base_address
1570 (TREE_OPERAND (gimple_assign_rhs1
1571 (SSA_NAME_DEF_STMT (ref)), 0));
1572 ref = walk_ssa_copies (ref);
1573 /* Find base address of the lookup and see if it looks like
1574 vptr load. */
1575 if (TREE_CODE (ref) == SSA_NAME
1576 && !SSA_NAME_IS_DEFAULT_DEF (ref)
1577 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref)))
1579 tree ref_exp = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (ref));
1580 tree base_ref = get_ref_base_and_extent
1581 (ref_exp, &offset2, &size, &max_size);
1583 /* Finally verify that what we found looks like read from
1584 OTR_OBJECT or from INSTANCE with offset OFFSET. */
1585 if (base_ref
1586 && ((TREE_CODE (base_ref) == MEM_REF
1587 && ((offset2 == instance_offset
1588 && TREE_OPERAND (base_ref, 0) == instance)
1589 || (!offset2
1590 && TREE_OPERAND (base_ref, 0)
1591 == otr_object)))
1592 || (DECL_P (instance) && base_ref == instance
1593 && offset2 == instance_offset)))
1595 stmt = SSA_NAME_DEF_STMT (ref);
1596 instance_ref = ref_exp;
1603 /* If we failed to look up the reference in code, build our own. */
1604 if (!instance_ref)
1606 /* If the statement in question does not use memory, we can't tell
1607 anything. */
1608 if (!gimple_vuse (stmt))
1609 return false;
1610 ao_ref_init_from_ptr_and_size (&ao, otr_object, NULL);
1612 else
1613 /* Otherwise use the real reference. */
1614 ao_ref_init (&ao, instance_ref);
1616 /* We look for vtbl pointer read. */
1617 ao.size = POINTER_SIZE;
1618 ao.max_size = ao.size;
1619 /* We are looking for stores to vptr pointer within the instance of
1620 outer type.
1621 TODO: The vptr pointer type is globally known, we probably should
1622 keep it and do that even when otr_type is unknown. */
1623 if (otr_type)
1625 ao.base_alias_set
1626 = get_alias_set (outer_type ? outer_type : otr_type);
1627 ao.ref_alias_set
1628 = get_alias_set (TREE_TYPE (BINFO_VTABLE (TYPE_BINFO (otr_type))));
1631 if (dump_file)
1633 fprintf (dump_file, "Determining dynamic type for call: ");
1634 print_gimple_stmt (dump_file, call, 0, 0);
1635 fprintf (dump_file, " Starting walk at: ");
1636 print_gimple_stmt (dump_file, stmt, 0, 0);
1637 fprintf (dump_file, " instance pointer: ");
1638 print_generic_expr (dump_file, otr_object, TDF_SLIM);
1639 fprintf (dump_file, " Outer instance pointer: ");
1640 print_generic_expr (dump_file, instance, TDF_SLIM);
1641 fprintf (dump_file, " offset: %i (bits)", (int)instance_offset);
1642 fprintf (dump_file, " vtbl reference: ");
1643 print_generic_expr (dump_file, instance_ref, TDF_SLIM);
1644 fprintf (dump_file, "\n");
1647 tci.offset = instance_offset;
1648 tci.instance = instance;
1649 tci.vtbl_ptr_ref = instance_ref;
1650 gcc_assert (TREE_CODE (instance) != MEM_REF);
1651 tci.known_current_type = NULL_TREE;
1652 tci.known_current_offset = 0;
1653 tci.otr_type = otr_type;
1654 tci.type_maybe_changed = false;
1655 tci.multiple_types_encountered = false;
1656 tci.speculative = false;
1657 tci.seen_unanalyzed_store = false;
1659 walk_aliased_vdefs (&ao, gimple_vuse (stmt), check_stmt_for_type_change,
1660 &tci, NULL, &function_entry_reached);
1662 /* If we did not find any type changing statements, we may still drop
1663 maybe_in_construction flag if the context already have outer type.
1665 Here we make special assumptions about both constructors and
1666 destructors which are all the functions that are allowed to alter the
1667 VMT pointers. It assumes that destructors begin with assignment into
1668 all VMT pointers and that constructors essentially look in the
1669 following way:
1671 1) The very first thing they do is that they call constructors of
1672 ancestor sub-objects that have them.
1674 2) Then VMT pointers of this and all its ancestors is set to new
1675 values corresponding to the type corresponding to the constructor.
1677 3) Only afterwards, other stuff such as constructor of member
1678 sub-objects and the code written by the user is run. Only this may
1679 include calling virtual functions, directly or indirectly.
1681 4) placement new can not be used to change type of non-POD statically
1682 allocated variables.
1684 There is no way to call a constructor of an ancestor sub-object in any
1685 other way.
1687 This means that we do not have to care whether constructors get the
1688 correct type information because they will always change it (in fact,
1689 if we define the type to be given by the VMT pointer, it is undefined).
1691 The most important fact to derive from the above is that if, for some
1692 statement in the section 3, we try to detect whether the dynamic type
1693 has changed, we can safely ignore all calls as we examine the function
1694 body backwards until we reach statements in section 2 because these
1695 calls cannot be ancestor constructors or destructors (if the input is
1696 not bogus) and so do not change the dynamic type (this holds true only
1697 for automatically allocated objects but at the moment we devirtualize
1698 only these). We then must detect that statements in section 2 change
1699 the dynamic type and can try to derive the new type. That is enough
1700 and we can stop, we will never see the calls into constructors of
1701 sub-objects in this code.
1703 Therefore if the static outer type was found (outer_type)
1704 we can safely ignore tci.speculative that is set on calls and give up
1705 only if there was dyanmic type store that may affect given variable
1706 (seen_unanalyzed_store) */
1708 if (!tci.type_maybe_changed
1709 || (outer_type
1710 && !dynamic
1711 && !tci.seen_unanalyzed_store
1712 && !tci.multiple_types_encountered
1713 && ((offset == tci.offset
1714 && types_same_for_odr (tci.known_current_type,
1715 outer_type))
1716 || (instance_offset == offset
1717 && types_same_for_odr (tci.known_current_type,
1718 instance_outer_type)))))
1720 if (!outer_type || tci.seen_unanalyzed_store)
1721 return false;
1722 if (maybe_in_construction)
1723 maybe_in_construction = false;
1724 if (dump_file)
1725 fprintf (dump_file, " No dynamic type change found.\n");
1726 return true;
1729 if (tci.known_current_type
1730 && !function_entry_reached
1731 && !tci.multiple_types_encountered)
1733 if (!tci.speculative)
1735 outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1736 offset = tci.known_current_offset;
1737 dynamic = true;
1738 maybe_in_construction = false;
1739 maybe_derived_type = false;
1740 if (dump_file)
1741 fprintf (dump_file, " Determined dynamic type.\n");
1743 else if (!speculative_outer_type
1744 || speculative_maybe_derived_type)
1746 speculative_outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1747 speculative_offset = tci.known_current_offset;
1748 speculative_maybe_derived_type = false;
1749 if (dump_file)
1750 fprintf (dump_file, " Determined speculative dynamic type.\n");
1753 else if (dump_file)
1755 fprintf (dump_file, " Found multiple types%s%s\n",
1756 function_entry_reached ? " (function entry reached)" : "",
1757 function_entry_reached ? " (multiple types encountered)" : "");
1760 return false;
1763 /* See if speculation given by SPEC_OUTER_TYPE, SPEC_OFFSET and SPEC_MAYBE_DERIVED_TYPE
1764 seems consistent (and useful) with what we already have in the non-speculative context. */
1766 bool
1767 ipa_polymorphic_call_context::speculation_consistent_p (tree spec_outer_type,
1768 HOST_WIDE_INT spec_offset,
1769 bool spec_maybe_derived_type,
1770 tree otr_type) const
1772 if (!flag_devirtualize_speculatively)
1773 return false;
1775 /* Non-polymorphic types are useless for deriving likely polymorphic
1776 call targets. */
1777 if (!spec_outer_type || !contains_polymorphic_type_p (spec_outer_type))
1778 return false;
1780 /* If we know nothing, speculation is always good. */
1781 if (!outer_type)
1782 return true;
1784 /* Speculation is only useful to avoid derived types.
1785 This is not 100% true for placement new, where the outer context may
1786 turn out to be useless, but ignore these for now. */
1787 if (!maybe_derived_type)
1788 return false;
1790 /* If types agrees, speculation is consistent, but it makes sense only
1791 when it says something new. */
1792 if (types_must_be_same_for_odr (spec_outer_type, outer_type))
1793 return maybe_derived_type && !spec_maybe_derived_type;
1795 /* If speculation does not contain the type in question, ignore it. */
1796 if (otr_type
1797 && !contains_type_p (spec_outer_type, spec_offset, otr_type, false, true))
1798 return false;
1800 /* If outer type already contains speculation as a filed,
1801 it is useless. We already know from OUTER_TYPE
1802 SPEC_TYPE and that it is not in the construction. */
1803 if (contains_type_p (outer_type, offset - spec_offset,
1804 spec_outer_type, false, false))
1805 return false;
1807 /* If speculative outer type is not more specified than outer
1808 type, just give up.
1809 We can only decide this safely if we can compare types with OUTER_TYPE.
1811 if ((!in_lto_p || odr_type_p (outer_type))
1812 && !contains_type_p (spec_outer_type,
1813 spec_offset - offset,
1814 outer_type, false))
1815 return false;
1816 return true;
1819 /* Improve THIS with speculation described by NEW_OUTER_TYPE, NEW_OFFSET,
1820 NEW_MAYBE_DERIVED_TYPE
1821 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1823 bool
1824 ipa_polymorphic_call_context::combine_speculation_with
1825 (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1826 tree otr_type)
1828 if (!new_outer_type)
1829 return false;
1831 /* restrict_to_inner_class may eliminate wrong speculation making our job
1832 easeier. */
1833 if (otr_type)
1834 restrict_to_inner_class (otr_type);
1836 if (!speculation_consistent_p (new_outer_type, new_offset,
1837 new_maybe_derived_type, otr_type))
1838 return false;
1840 /* New speculation is a win in case we have no speculation or new
1841 speculation does not consider derivations. */
1842 if (!speculative_outer_type
1843 || (speculative_maybe_derived_type
1844 && !new_maybe_derived_type))
1846 speculative_outer_type = new_outer_type;
1847 speculative_offset = new_offset;
1848 speculative_maybe_derived_type = new_maybe_derived_type;
1849 return true;
1851 else if (types_must_be_same_for_odr (speculative_outer_type,
1852 new_outer_type))
1854 if (speculative_offset != new_offset)
1856 /* OK we have two contexts that seems valid but they disagree,
1857 just give up.
1859 This is not a lattice operation, so we may want to drop it later. */
1860 if (dump_file && (dump_flags & TDF_DETAILS))
1861 fprintf (dump_file,
1862 "Speculative outer types match, "
1863 "offset mismatch -> invalid speculation\n");
1864 clear_speculation ();
1865 return true;
1867 else
1869 if (speculative_maybe_derived_type && !new_maybe_derived_type)
1871 speculative_maybe_derived_type = false;
1872 return true;
1874 else
1875 return false;
1878 /* Choose type that contains the other. This one either contains the outer
1879 as a field (thus giving exactly one target) or is deeper in the type
1880 hiearchy. */
1881 else if (speculative_outer_type
1882 && speculative_maybe_derived_type
1883 && (new_offset > speculative_offset
1884 || (new_offset == speculative_offset
1885 && contains_type_p (new_outer_type,
1886 0, speculative_outer_type, false))))
1888 tree old_outer_type = speculative_outer_type;
1889 HOST_WIDE_INT old_offset = speculative_offset;
1890 bool old_maybe_derived_type = speculative_maybe_derived_type;
1892 speculative_outer_type = new_outer_type;
1893 speculative_offset = new_offset;
1894 speculative_maybe_derived_type = new_maybe_derived_type;
1896 if (otr_type)
1897 restrict_to_inner_class (otr_type);
1899 /* If the speculation turned out to make no sense, revert to sensible
1900 one. */
1901 if (!speculative_outer_type)
1903 speculative_outer_type = old_outer_type;
1904 speculative_offset = old_offset;
1905 speculative_maybe_derived_type = old_maybe_derived_type;
1906 return false;
1908 return (old_offset != speculative_offset
1909 || old_maybe_derived_type != speculative_maybe_derived_type
1910 || types_must_be_same_for_odr (speculative_outer_type,
1911 new_outer_type));
1913 return false;
1916 /* Make speculation less specific so
1917 NEW_OUTER_TYPE, NEW_OFFSET, NEW_MAYBE_DERIVED_TYPE is also included.
1918 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1920 bool
1921 ipa_polymorphic_call_context::meet_speculation_with
1922 (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1923 tree otr_type)
1925 if (!new_outer_type && speculative_outer_type)
1927 clear_speculation ();
1928 return true;
1931 /* restrict_to_inner_class may eliminate wrong speculation making our job
1932 easeier. */
1933 if (otr_type)
1934 restrict_to_inner_class (otr_type);
1936 if (!speculative_outer_type
1937 || !speculation_consistent_p (speculative_outer_type,
1938 speculative_offset,
1939 speculative_maybe_derived_type,
1940 otr_type))
1941 return false;
1943 if (!speculation_consistent_p (new_outer_type, new_offset,
1944 new_maybe_derived_type, otr_type))
1946 clear_speculation ();
1947 return true;
1950 else if (types_must_be_same_for_odr (speculative_outer_type,
1951 new_outer_type))
1953 if (speculative_offset != new_offset)
1955 clear_speculation ();
1956 return true;
1958 else
1960 if (!speculative_maybe_derived_type && new_maybe_derived_type)
1962 speculative_maybe_derived_type = true;
1963 return true;
1965 else
1966 return false;
1969 /* See if one type contains the other as a field (not base). */
1970 else if (contains_type_p (new_outer_type, new_offset - speculative_offset,
1971 speculative_outer_type, false, false))
1972 return false;
1973 else if (contains_type_p (speculative_outer_type,
1974 speculative_offset - new_offset,
1975 new_outer_type, false, false))
1977 speculative_outer_type = new_outer_type;
1978 speculative_offset = new_offset;
1979 speculative_maybe_derived_type = new_maybe_derived_type;
1980 return true;
1982 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
1983 else if (contains_type_p (new_outer_type,
1984 new_offset - speculative_offset,
1985 speculative_outer_type, false, true))
1987 if (!speculative_maybe_derived_type)
1989 speculative_maybe_derived_type = true;
1990 return true;
1992 return false;
1994 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
1995 else if (contains_type_p (speculative_outer_type,
1996 speculative_offset - new_offset, new_outer_type, false, true))
1998 speculative_outer_type = new_outer_type;
1999 speculative_offset = new_offset;
2000 speculative_maybe_derived_type = true;
2001 return true;
2003 else
2005 if (dump_file && (dump_flags & TDF_DETAILS))
2006 fprintf (dump_file, "Giving up on speculative meet\n");
2007 clear_speculation ();
2008 return true;
2012 /* Assume that both THIS and a given context is valid and strenghten THIS
2013 if possible. Return true if any strenghtening was made.
2014 If actual type the context is being used in is known, OTR_TYPE should be
2015 set accordingly. This improves quality of combined result. */
2017 bool
2018 ipa_polymorphic_call_context::combine_with (ipa_polymorphic_call_context ctx,
2019 tree otr_type)
2021 bool updated = false;
2023 if (ctx.useless_p () || invalid)
2024 return false;
2026 /* Restricting context to inner type makes merging easier, however do not
2027 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2028 if (otr_type && !invalid && !ctx.invalid)
2030 restrict_to_inner_class (otr_type);
2031 ctx.restrict_to_inner_class (otr_type);
2032 if(invalid)
2033 return false;
2036 if (dump_file && (dump_flags & TDF_DETAILS))
2038 fprintf (dump_file, "Polymorphic call context combine:");
2039 dump (dump_file);
2040 fprintf (dump_file, "With context: ");
2041 ctx.dump (dump_file);
2042 if (otr_type)
2044 fprintf (dump_file, "To be used with type: ");
2045 print_generic_expr (dump_file, otr_type, TDF_SLIM);
2046 fprintf (dump_file, "\n");
2050 /* If call is known to be invalid, we are done. */
2051 if (ctx.invalid)
2053 if (dump_file && (dump_flags & TDF_DETAILS))
2054 fprintf (dump_file, "-> Invalid context\n");
2055 goto invalidate;
2058 if (!ctx.outer_type)
2060 else if (!outer_type)
2062 outer_type = ctx.outer_type;
2063 offset = ctx.offset;
2064 dynamic = ctx.dynamic;
2065 maybe_in_construction = ctx.maybe_in_construction;
2066 maybe_derived_type = ctx.maybe_derived_type;
2067 updated = true;
2069 /* If types are known to be same, merging is quite easy. */
2070 else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2072 if (offset != ctx.offset
2073 && TYPE_SIZE (outer_type)
2074 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2076 if (dump_file && (dump_flags & TDF_DETAILS))
2077 fprintf (dump_file, "Outer types match, offset mismatch -> invalid\n");
2078 clear_speculation ();
2079 clear_outer_type ();
2080 invalid = true;
2081 return true;
2083 if (dump_file && (dump_flags & TDF_DETAILS))
2084 fprintf (dump_file, "Outer types match, merging flags\n");
2085 if (maybe_in_construction && !ctx.maybe_in_construction)
2087 updated = true;
2088 maybe_in_construction = false;
2090 if (maybe_derived_type && !ctx.maybe_derived_type)
2092 updated = true;
2093 maybe_derived_type = false;
2095 if (dynamic && !ctx.dynamic)
2097 updated = true;
2098 dynamic = false;
2101 /* If we know the type precisely, there is not much to improve. */
2102 else if (!maybe_derived_type && !maybe_in_construction
2103 && !ctx.maybe_derived_type && !ctx.maybe_in_construction)
2105 /* It may be easy to check if second context permits the first
2106 and set INVALID otherwise. This is not easy to do in general;
2107 contains_type_p may return false negatives for non-comparable
2108 types.
2110 If OTR_TYPE is known, we however can expect that
2111 restrict_to_inner_class should have discovered the same base
2112 type. */
2113 if (otr_type && !ctx.maybe_in_construction && !ctx.maybe_derived_type)
2115 if (dump_file && (dump_flags & TDF_DETAILS))
2116 fprintf (dump_file, "Contextes disagree -> invalid\n");
2117 goto invalidate;
2120 /* See if one type contains the other as a field (not base).
2121 In this case we want to choose the wider type, because it contains
2122 more information. */
2123 else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2124 outer_type, false, false))
2126 if (dump_file && (dump_flags & TDF_DETAILS))
2127 fprintf (dump_file, "Second type contain the first as a field\n");
2129 if (maybe_derived_type)
2131 outer_type = ctx.outer_type;
2132 maybe_derived_type = ctx.maybe_derived_type;
2133 offset = ctx.offset;
2134 dynamic = ctx.dynamic;
2135 updated = true;
2138 /* If we do not know how the context is being used, we can
2139 not clear MAYBE_IN_CONSTRUCTION because it may be offseted
2140 to other component of OUTER_TYPE later and we know nothing
2141 about it. */
2142 if (otr_type && maybe_in_construction
2143 && !ctx.maybe_in_construction)
2145 maybe_in_construction = false;
2146 updated = true;
2149 else if (contains_type_p (outer_type, offset - ctx.offset,
2150 ctx.outer_type, false, false))
2152 if (dump_file && (dump_flags & TDF_DETAILS))
2153 fprintf (dump_file, "First type contain the second as a field\n");
2155 if (otr_type && maybe_in_construction
2156 && !ctx.maybe_in_construction)
2158 maybe_in_construction = false;
2159 updated = true;
2162 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2163 else if (contains_type_p (ctx.outer_type,
2164 ctx.offset - offset, outer_type, false, true))
2166 if (dump_file && (dump_flags & TDF_DETAILS))
2167 fprintf (dump_file, "First type is base of second\n");
2168 if (!maybe_derived_type)
2170 if (!ctx.maybe_in_construction
2171 && types_odr_comparable (outer_type, ctx.outer_type))
2173 if (dump_file && (dump_flags & TDF_DETAILS))
2174 fprintf (dump_file, "Second context does not permit base -> invalid\n");
2175 goto invalidate;
2178 /* Pick variant deeper in the hiearchy. */
2179 else
2181 outer_type = ctx.outer_type;
2182 maybe_in_construction = ctx.maybe_in_construction;
2183 maybe_derived_type = ctx.maybe_derived_type;
2184 offset = ctx.offset;
2185 dynamic = ctx.dynamic;
2186 updated = true;
2189 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2190 else if (contains_type_p (outer_type,
2191 offset - ctx.offset, ctx.outer_type, false, true))
2193 if (dump_file && (dump_flags & TDF_DETAILS))
2194 fprintf (dump_file, "Second type is base of first\n");
2195 if (!ctx.maybe_derived_type)
2197 if (!maybe_in_construction
2198 && types_odr_comparable (outer_type, ctx.outer_type))
2200 if (dump_file && (dump_flags & TDF_DETAILS))
2201 fprintf (dump_file, "First context does not permit base -> invalid\n");
2202 goto invalidate;
2204 /* Pick the base type. */
2205 else if (maybe_in_construction)
2207 outer_type = ctx.outer_type;
2208 maybe_in_construction = ctx.maybe_in_construction;
2209 maybe_derived_type = ctx.maybe_derived_type;
2210 offset = ctx.offset;
2211 dynamic = ctx.dynamic;
2212 updated = true;
2216 /* TODO handle merging using hiearchy. */
2217 else if (dump_file && (dump_flags & TDF_DETAILS))
2218 fprintf (dump_file, "Giving up on merge\n");
2220 updated |= combine_speculation_with (ctx.speculative_outer_type,
2221 ctx.speculative_offset,
2222 ctx.speculative_maybe_derived_type,
2223 otr_type);
2225 if (updated && dump_file && (dump_flags & TDF_DETAILS))
2227 fprintf (dump_file, "Updated as: ");
2228 dump (dump_file);
2229 fprintf (dump_file, "\n");
2231 return updated;
2233 invalidate:
2234 invalid = true;
2235 clear_speculation ();
2236 clear_outer_type ();
2237 return true;
2240 /* Take non-speculative info, merge it with speculative and clear speculation.
2241 Used when we no longer manage to keep track of actual outer type, but we
2242 think it is still there.
2244 If OTR_TYPE is set, the transformation can be done more effectively assuming
2245 that context is going to be used only that way. */
2247 void
2248 ipa_polymorphic_call_context::make_speculative (tree otr_type)
2250 tree spec_outer_type = outer_type;
2251 HOST_WIDE_INT spec_offset = offset;
2252 bool spec_maybe_derived_type = maybe_derived_type;
2254 if (invalid)
2256 invalid = false;
2257 clear_outer_type ();
2258 clear_speculation ();
2259 return;
2261 if (!outer_type)
2262 return;
2263 clear_outer_type ();
2264 combine_speculation_with (spec_outer_type, spec_offset,
2265 spec_maybe_derived_type,
2266 otr_type);
2269 /* Use when we can not track dynamic type change. This speculatively assume
2270 type change is not happening. */
2272 void
2273 ipa_polymorphic_call_context::possible_dynamic_type_change (bool in_poly_cdtor,
2274 tree otr_type)
2276 if (dynamic)
2277 make_speculative (otr_type);
2278 else if (in_poly_cdtor)
2279 maybe_in_construction = true;
2282 /* Return TRUE if this context conveys the same information as OTHER. */
2284 bool
2285 ipa_polymorphic_call_context::equal_to
2286 (const ipa_polymorphic_call_context &x) const
2288 if (useless_p ())
2289 return x.useless_p ();
2290 if (invalid)
2291 return x.invalid;
2292 if (x.useless_p () || x.invalid)
2293 return false;
2295 if (outer_type)
2297 if (!x.outer_type
2298 || !types_odr_comparable (outer_type, x.outer_type)
2299 || !types_same_for_odr (outer_type, x.outer_type)
2300 || offset != x.offset
2301 || maybe_in_construction != x.maybe_in_construction
2302 || maybe_derived_type != x.maybe_derived_type
2303 || dynamic != x.dynamic)
2304 return false;
2306 else if (x.outer_type)
2307 return false;
2310 if (speculative_outer_type
2311 && speculation_consistent_p (speculative_outer_type, speculative_offset,
2312 speculative_maybe_derived_type, NULL_TREE))
2314 if (!x.speculative_outer_type)
2315 return false;
2317 if (!types_odr_comparable (speculative_outer_type,
2318 x.speculative_outer_type)
2319 || !types_same_for_odr (speculative_outer_type,
2320 x.speculative_outer_type)
2321 || speculative_offset != x.speculative_offset
2322 || speculative_maybe_derived_type != x.speculative_maybe_derived_type)
2323 return false;
2325 else if (x.speculative_outer_type
2326 && x.speculation_consistent_p (x.speculative_outer_type,
2327 x.speculative_offset,
2328 x.speculative_maybe_derived_type,
2329 NULL))
2330 return false;
2332 return true;
2335 /* Modify context to be strictly less restrictive than CTX. */
2337 bool
2338 ipa_polymorphic_call_context::meet_with (ipa_polymorphic_call_context ctx,
2339 tree otr_type)
2341 bool updated = false;
2343 if (useless_p () || ctx.invalid)
2344 return false;
2346 /* Restricting context to inner type makes merging easier, however do not
2347 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2348 if (otr_type && !useless_p () && !ctx.useless_p ())
2350 restrict_to_inner_class (otr_type);
2351 ctx.restrict_to_inner_class (otr_type);
2352 if(invalid)
2353 return false;
2356 if (equal_to (ctx))
2357 return false;
2359 if (ctx.useless_p () || invalid)
2361 *this = ctx;
2362 return true;
2365 if (dump_file && (dump_flags & TDF_DETAILS))
2367 fprintf (dump_file, "Polymorphic call context meet:");
2368 dump (dump_file);
2369 fprintf (dump_file, "With context: ");
2370 ctx.dump (dump_file);
2371 if (otr_type)
2373 fprintf (dump_file, "To be used with type: ");
2374 print_generic_expr (dump_file, otr_type, TDF_SLIM);
2375 fprintf (dump_file, "\n");
2379 if (!dynamic && ctx.dynamic)
2381 dynamic = true;
2382 updated = true;
2385 /* If call is known to be invalid, we are done. */
2386 if (!outer_type)
2388 else if (!ctx.outer_type)
2390 clear_outer_type ();
2391 updated = true;
2393 /* If types are known to be same, merging is quite easy. */
2394 else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2396 if (offset != ctx.offset
2397 && TYPE_SIZE (outer_type)
2398 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2400 if (dump_file && (dump_flags & TDF_DETAILS))
2401 fprintf (dump_file, "Outer types match, offset mismatch -> clearing\n");
2402 clear_outer_type ();
2403 return true;
2405 if (dump_file && (dump_flags & TDF_DETAILS))
2406 fprintf (dump_file, "Outer types match, merging flags\n");
2407 if (!maybe_in_construction && ctx.maybe_in_construction)
2409 updated = true;
2410 maybe_in_construction = true;
2412 if (!maybe_derived_type && ctx.maybe_derived_type)
2414 updated = true;
2415 maybe_derived_type = true;
2417 if (!dynamic && ctx.dynamic)
2419 updated = true;
2420 dynamic = true;
2423 /* See if one type contains the other as a field (not base). */
2424 else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2425 outer_type, false, false))
2427 if (dump_file && (dump_flags & TDF_DETAILS))
2428 fprintf (dump_file, "Second type contain the first as a field\n");
2430 /* The second type is more specified, so we keep the first.
2431 We need to set DYNAMIC flag to avoid declaring context INVALID
2432 of OFFSET ends up being out of range. */
2433 if (!dynamic
2434 && (ctx.dynamic
2435 || (!otr_type
2436 && (!TYPE_SIZE (ctx.outer_type)
2437 || !TYPE_SIZE (outer_type)
2438 || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2439 TYPE_SIZE (outer_type), 0)))))
2441 dynamic = true;
2442 updated = true;
2445 else if (contains_type_p (outer_type, offset - ctx.offset,
2446 ctx.outer_type, false, false))
2448 if (dump_file && (dump_flags & TDF_DETAILS))
2449 fprintf (dump_file, "First type contain the second as a field\n");
2451 if (!dynamic
2452 && (ctx.dynamic
2453 || (!otr_type
2454 && (!TYPE_SIZE (ctx.outer_type)
2455 || !TYPE_SIZE (outer_type)
2456 || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2457 TYPE_SIZE (outer_type), 0)))))
2458 dynamic = true;
2459 outer_type = ctx.outer_type;
2460 offset = ctx.offset;
2461 dynamic = ctx.dynamic;
2462 maybe_in_construction = ctx.maybe_in_construction;
2463 maybe_derived_type = ctx.maybe_derived_type;
2464 updated = true;
2466 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2467 else if (contains_type_p (ctx.outer_type,
2468 ctx.offset - offset, outer_type, false, true))
2470 if (dump_file && (dump_flags & TDF_DETAILS))
2471 fprintf (dump_file, "First type is base of second\n");
2472 if (!maybe_derived_type)
2474 maybe_derived_type = true;
2475 updated = true;
2477 if (!maybe_in_construction && ctx.maybe_in_construction)
2479 maybe_in_construction = true;
2480 updated = true;
2482 if (!dynamic && ctx.dynamic)
2484 dynamic = true;
2485 updated = true;
2488 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2489 else if (contains_type_p (outer_type,
2490 offset - ctx.offset, ctx.outer_type, false, true))
2492 if (dump_file && (dump_flags & TDF_DETAILS))
2493 fprintf (dump_file, "Second type is base of first\n");
2494 outer_type = ctx.outer_type;
2495 offset = ctx.offset;
2496 updated = true;
2497 if (!maybe_derived_type)
2498 maybe_derived_type = true;
2499 if (!maybe_in_construction && ctx.maybe_in_construction)
2500 maybe_in_construction = true;
2501 if (!dynamic && ctx.dynamic)
2502 dynamic = true;
2504 /* TODO handle merging using hiearchy. */
2505 else
2507 if (dump_file && (dump_flags & TDF_DETAILS))
2508 fprintf (dump_file, "Giving up on meet\n");
2509 clear_outer_type ();
2510 updated = true;
2513 updated |= meet_speculation_with (ctx.speculative_outer_type,
2514 ctx.speculative_offset,
2515 ctx.speculative_maybe_derived_type,
2516 otr_type);
2518 if (updated && dump_file && (dump_flags & TDF_DETAILS))
2520 fprintf (dump_file, "Updated as: ");
2521 dump (dump_file);
2522 fprintf (dump_file, "\n");
2524 return updated;