2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ipa-polymorphic-call.c
blob7aa9663ace8fdc95049215515158055bef6d89b8
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 "tm.h"
25 #include "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "print-tree.h"
31 #include "calls.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "rtl.h"
35 #include "flags.h"
36 #include "insn-config.h"
37 #include "expmed.h"
38 #include "dojump.h"
39 #include "explow.h"
40 #include "emit-rtl.h"
41 #include "varasm.h"
42 #include "stmt.h"
43 #include "expr.h"
44 #include "tree-pass.h"
45 #include "target.h"
46 #include "tree-pretty-print.h"
47 #include "predict.h"
48 #include "basic-block.h"
49 #include "is-a.h"
50 #include "plugin-api.h"
51 #include "ipa-ref.h"
52 #include "cgraph.h"
53 #include "ipa-utils.h"
54 #include "tree-ssa-alias.h"
55 #include "internal-fn.h"
56 #include "gimple-fold.h"
57 #include "gimple-expr.h"
58 #include "gimple.h"
59 #include "alloc-pool.h"
60 #include "symbol-summary.h"
61 #include "ipa-prop.h"
62 #include "ipa-inline.h"
63 #include "diagnostic.h"
64 #include "tree-dfa.h"
65 #include "demangle.h"
66 #include "dbgcnt.h"
67 #include "gimple-pretty-print.h"
68 #include "stor-layout.h"
69 #include "intl.h"
70 #include "data-streamer.h"
71 #include "lto-streamer.h"
72 #include "streamer-hooks.h"
73 #include "tree-ssa-operands.h"
74 #include "tree-into-ssa.h"
76 /* Return true when TYPE contains an polymorphic type and thus is interesting
77 for devirtualization machinery. */
79 static bool contains_type_p (tree, HOST_WIDE_INT, tree,
80 bool consider_placement_new = true,
81 bool consider_bases = true);
83 bool
84 contains_polymorphic_type_p (const_tree type)
86 type = TYPE_MAIN_VARIANT (type);
88 if (RECORD_OR_UNION_TYPE_P (type))
90 if (TYPE_BINFO (type)
91 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
92 return true;
93 for (tree fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
94 if (TREE_CODE (fld) == FIELD_DECL
95 && !DECL_ARTIFICIAL (fld)
96 && contains_polymorphic_type_p (TREE_TYPE (fld)))
97 return true;
98 return false;
100 if (TREE_CODE (type) == ARRAY_TYPE)
101 return contains_polymorphic_type_p (TREE_TYPE (type));
102 return false;
105 /* Return true if it seems valid to use placement new to build EXPECTED_TYPE
106 at possition CUR_OFFSET within TYPE.
108 POD can be changed to an instance of a polymorphic type by
109 placement new. Here we play safe and assume that any
110 non-polymorphic type is POD. */
111 bool
112 possible_placement_new (tree type, tree expected_type,
113 HOST_WIDE_INT cur_offset)
115 return ((TREE_CODE (type) != RECORD_TYPE
116 || !TYPE_BINFO (type)
117 || cur_offset >= POINTER_SIZE
118 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))
119 && (!TYPE_SIZE (type)
120 || !tree_fits_shwi_p (TYPE_SIZE (type))
121 || (cur_offset
122 + (expected_type ? tree_to_uhwi (TYPE_SIZE (expected_type))
123 : POINTER_SIZE)
124 <= tree_to_uhwi (TYPE_SIZE (type)))));
127 /* THIS->OUTER_TYPE is a type of memory object where object of OTR_TYPE
128 is contained at THIS->OFFSET. Walk the memory representation of
129 THIS->OUTER_TYPE and find the outermost class type that match
130 OTR_TYPE or contain OTR_TYPE as a base. Update THIS
131 to represent it.
133 If OTR_TYPE is NULL, just find outermost polymorphic type with
134 virtual table present at possition OFFSET.
136 For example when THIS represents type
137 class A
139 int a;
140 class B b;
142 and we look for type at offset sizeof(int), we end up with B and offset 0.
143 If the same is produced by multiple inheritance, we end up with A and offset
144 sizeof(int).
146 If we can not find corresponding class, give up by setting
147 THIS->OUTER_TYPE to OTR_TYPE and THIS->OFFSET to NULL.
148 Return true when lookup was sucesful.
150 When CONSIDER_PLACEMENT_NEW is false, reject contexts that may be made
151 valid only via alocation of new polymorphic type inside by means
152 of placement new.
154 When CONSIDER_BASES is false, only look for actual fields, not base types
155 of TYPE. */
157 bool
158 ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type,
159 bool consider_placement_new,
160 bool consider_bases)
162 tree type = outer_type;
163 HOST_WIDE_INT cur_offset = offset;
164 bool speculative = false;
165 bool size_unknown = false;
166 unsigned HOST_WIDE_INT otr_type_size = POINTER_SIZE;
168 /* Update OUTER_TYPE to match EXPECTED_TYPE if it is not set. */
169 if (!outer_type)
171 clear_outer_type (otr_type);
172 type = otr_type;
173 cur_offset = 0;
175 /* See if OFFSET points inside OUTER_TYPE. If it does not, we know
176 that the context is either invalid, or the instance type must be
177 derived from OUTER_TYPE.
179 Because the instance type may contain field whose type is of OUTER_TYPE,
180 we can not derive any effective information about it.
182 TODO: In the case we know all derrived types, we can definitely do better
183 here. */
184 else if (TYPE_SIZE (outer_type)
185 && tree_fits_shwi_p (TYPE_SIZE (outer_type))
186 && tree_to_shwi (TYPE_SIZE (outer_type)) >= 0
187 && tree_to_shwi (TYPE_SIZE (outer_type)) <= offset)
189 clear_outer_type (otr_type);
190 type = otr_type;
191 cur_offset = 0;
193 /* If derived type is not allowed, we know that the context is invalid.
194 For dynamic types, we really do not have information about
195 size of the memory location. It is possible that completely
196 different type is stored after outer_type. */
197 if (!maybe_derived_type && !dynamic)
199 clear_speculation ();
200 invalid = true;
201 return false;
205 if (otr_type && TYPE_SIZE (otr_type)
206 && tree_fits_shwi_p (TYPE_SIZE (otr_type)))
207 otr_type_size = tree_to_uhwi (TYPE_SIZE (otr_type));
209 if (!type || offset < 0)
210 goto no_useful_type_info;
212 /* Find the sub-object the constant actually refers to and mark whether it is
213 an artificial one (as opposed to a user-defined one).
215 This loop is performed twice; first time for outer_type and second time
216 for speculative_outer_type. The second run has SPECULATIVE set. */
217 while (true)
219 unsigned HOST_WIDE_INT pos, size;
220 tree fld;
222 /* If we do not know size of TYPE, we need to be more conservative
223 about accepting cases where we can not find EXPECTED_TYPE.
224 Generally the types that do matter here are of constant size.
225 Size_unknown case should be very rare. */
226 if (TYPE_SIZE (type)
227 && tree_fits_shwi_p (TYPE_SIZE (type))
228 && tree_to_shwi (TYPE_SIZE (type)) >= 0)
229 size_unknown = false;
230 else
231 size_unknown = true;
233 /* On a match, just return what we found. */
234 if ((otr_type
235 && types_odr_comparable (type, otr_type)
236 && types_same_for_odr (type, otr_type))
237 || (!otr_type
238 && TREE_CODE (type) == RECORD_TYPE
239 && TYPE_BINFO (type)
240 && polymorphic_type_binfo_p (TYPE_BINFO (type))))
242 if (speculative)
244 /* If we did not match the offset, just give up on speculation. */
245 if (cur_offset != 0
246 /* Also check if speculation did not end up being same as
247 non-speculation. */
248 || (types_must_be_same_for_odr (speculative_outer_type,
249 outer_type)
250 && (maybe_derived_type
251 == speculative_maybe_derived_type)))
252 clear_speculation ();
253 return true;
255 else
257 /* If type is known to be final, do not worry about derived
258 types. Testing it here may help us to avoid speculation. */
259 if (otr_type && TREE_CODE (outer_type) == RECORD_TYPE
260 && (!in_lto_p || odr_type_p (outer_type))
261 && type_with_linkage_p (outer_type)
262 && type_known_to_have_no_derivations_p (outer_type))
263 maybe_derived_type = false;
265 /* Type can not contain itself on an non-zero offset. In that case
266 just give up. Still accept the case where size is now known.
267 Either the second copy may appear past the end of type or within
268 the non-POD buffer located inside the variably sized type
269 itself. */
270 if (cur_offset != 0)
271 goto no_useful_type_info;
272 /* If we determined type precisely or we have no clue on
273 speuclation, we are done. */
274 if (!maybe_derived_type || !speculative_outer_type
275 || !speculation_consistent_p (speculative_outer_type,
276 speculative_offset,
277 speculative_maybe_derived_type,
278 otr_type))
280 clear_speculation ();
281 return true;
283 /* Otherwise look into speculation now. */
284 else
286 speculative = true;
287 type = speculative_outer_type;
288 cur_offset = speculative_offset;
289 continue;
294 /* Walk fields and find corresponding on at OFFSET. */
295 if (TREE_CODE (type) == RECORD_TYPE)
297 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
299 if (TREE_CODE (fld) != FIELD_DECL)
300 continue;
302 pos = int_bit_position (fld);
303 if (pos > (unsigned HOST_WIDE_INT)cur_offset)
304 continue;
306 /* Do not consider vptr itself. Not even for placement new. */
307 if (!pos && DECL_ARTIFICIAL (fld)
308 && POINTER_TYPE_P (TREE_TYPE (fld))
309 && TYPE_BINFO (type)
310 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
311 continue;
313 if (!DECL_SIZE (fld) || !tree_fits_uhwi_p (DECL_SIZE (fld)))
314 goto no_useful_type_info;
315 size = tree_to_uhwi (DECL_SIZE (fld));
317 /* We can always skip types smaller than pointer size:
318 those can not contain a virtual table pointer.
320 Disqualifying fields that are too small to fit OTR_TYPE
321 saves work needed to walk them for no benefit.
322 Because of the way the bases are packed into a class, the
323 field's size may be smaller than type size, so it needs
324 to be done with a care. */
326 if (pos <= (unsigned HOST_WIDE_INT)cur_offset
327 && (pos + size) >= (unsigned HOST_WIDE_INT)cur_offset
328 + POINTER_SIZE
329 && (!otr_type
330 || !TYPE_SIZE (TREE_TYPE (fld))
331 || !tree_fits_shwi_p (TYPE_SIZE (TREE_TYPE (fld)))
332 || (pos + tree_to_uhwi (TYPE_SIZE (TREE_TYPE (fld))))
333 >= cur_offset + otr_type_size))
334 break;
337 if (!fld)
338 goto no_useful_type_info;
340 type = TYPE_MAIN_VARIANT (TREE_TYPE (fld));
341 cur_offset -= pos;
342 /* DECL_ARTIFICIAL represents a basetype. */
343 if (!DECL_ARTIFICIAL (fld))
345 if (!speculative)
347 outer_type = type;
348 offset = cur_offset;
349 /* As soon as we se an field containing the type,
350 we know we are not looking for derivations. */
351 maybe_derived_type = false;
353 else
355 speculative_outer_type = type;
356 speculative_offset = cur_offset;
357 speculative_maybe_derived_type = false;
360 else if (!consider_bases)
361 goto no_useful_type_info;
363 else if (TREE_CODE (type) == ARRAY_TYPE)
365 tree subtype = TYPE_MAIN_VARIANT (TREE_TYPE (type));
367 /* Give up if we don't know array field size.
368 Also give up on non-polymorphic types as they are used
369 as buffers for placement new. */
370 if (!TYPE_SIZE (subtype)
371 || !tree_fits_shwi_p (TYPE_SIZE (subtype))
372 || tree_to_shwi (TYPE_SIZE (subtype)) <= 0
373 || !contains_polymorphic_type_p (subtype))
374 goto no_useful_type_info;
376 HOST_WIDE_INT new_offset = cur_offset % tree_to_shwi (TYPE_SIZE (subtype));
378 /* We may see buffer for placement new. In this case the expected type
379 can be bigger than the subtype. */
380 if (TYPE_SIZE (subtype)
381 && (cur_offset + otr_type_size
382 > tree_to_uhwi (TYPE_SIZE (subtype))))
383 goto no_useful_type_info;
385 cur_offset = new_offset;
386 type = TYPE_MAIN_VARIANT (subtype);
387 if (!speculative)
389 outer_type = type;
390 offset = cur_offset;
391 maybe_derived_type = false;
393 else
395 speculative_outer_type = type;
396 speculative_offset = cur_offset;
397 speculative_maybe_derived_type = false;
400 /* Give up on anything else. */
401 else
403 no_useful_type_info:
404 if (maybe_derived_type && !speculative
405 && TREE_CODE (outer_type) == RECORD_TYPE
406 && TREE_CODE (otr_type) == RECORD_TYPE
407 && TYPE_BINFO (otr_type)
408 && !offset
409 && get_binfo_at_offset (TYPE_BINFO (otr_type), 0, outer_type))
411 clear_outer_type (otr_type);
412 if (!speculative_outer_type
413 || !speculation_consistent_p (speculative_outer_type,
414 speculative_offset,
415 speculative_maybe_derived_type,
416 otr_type))
417 clear_speculation ();
418 if (speculative_outer_type)
420 speculative = true;
421 type = speculative_outer_type;
422 cur_offset = speculative_offset;
424 else
425 return true;
427 /* We found no way to embedd EXPECTED_TYPE in TYPE.
428 We still permit two special cases - placement new and
429 the case of variadic types containing themselves. */
430 if (!speculative
431 && consider_placement_new
432 && (size_unknown || !type || maybe_derived_type
433 || possible_placement_new (type, otr_type, cur_offset)))
435 /* In these weird cases we want to accept the context.
436 In non-speculative run we have no useful outer_type info
437 (TODO: we may eventually want to record upper bound on the
438 type size that can be used to prune the walk),
439 but we still want to consider speculation that may
440 give useful info. */
441 if (!speculative)
443 clear_outer_type (otr_type);
444 if (!speculative_outer_type
445 || !speculation_consistent_p (speculative_outer_type,
446 speculative_offset,
447 speculative_maybe_derived_type,
448 otr_type))
449 clear_speculation ();
450 if (speculative_outer_type)
452 speculative = true;
453 type = speculative_outer_type;
454 cur_offset = speculative_offset;
456 else
457 return true;
459 else
460 clear_speculation ();
461 return true;
463 else
465 clear_speculation ();
466 if (speculative)
467 return true;
468 clear_outer_type (otr_type);
469 invalid = true;
470 return false;
476 /* Return true if OUTER_TYPE contains OTR_TYPE at OFFSET.
477 CONSIDER_PLACEMENT_NEW makes function to accept cases where OTR_TYPE can
478 be built within OUTER_TYPE by means of placement new. CONSIDER_BASES makes
479 function to accept cases where OTR_TYPE appears as base of OUTER_TYPE or as
480 base of one of fields of OUTER_TYPE. */
482 static bool
483 contains_type_p (tree outer_type, HOST_WIDE_INT offset,
484 tree otr_type,
485 bool consider_placement_new,
486 bool consider_bases)
488 ipa_polymorphic_call_context context;
490 /* Check that type is within range. */
491 if (offset < 0)
492 return false;
493 if (TYPE_SIZE (outer_type) && TYPE_SIZE (otr_type)
494 && TREE_CODE (outer_type) == INTEGER_CST
495 && TREE_CODE (otr_type) == INTEGER_CST
496 && wi::ltu_p (wi::to_offset (outer_type), (wi::to_offset (otr_type) + offset)))
497 return false;
499 context.offset = offset;
500 context.outer_type = TYPE_MAIN_VARIANT (outer_type);
501 context.maybe_derived_type = false;
502 return context.restrict_to_inner_class (otr_type, consider_placement_new, consider_bases);
506 /* Return a FUNCTION_DECL if BLOCK represents a constructor or destructor.
507 If CHECK_CLONES is true, also check for clones of ctor/dtors. */
509 tree
510 inlined_polymorphic_ctor_dtor_block_p (tree block, bool check_clones)
512 tree fn = BLOCK_ABSTRACT_ORIGIN (block);
513 if (fn == NULL || TREE_CODE (fn) != FUNCTION_DECL)
514 return NULL_TREE;
516 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
517 || (!DECL_CXX_CONSTRUCTOR_P (fn) && !DECL_CXX_DESTRUCTOR_P (fn)))
519 if (!check_clones)
520 return NULL_TREE;
522 /* Watch for clones where we constant propagated the first
523 argument (pointer to the instance). */
524 fn = DECL_ABSTRACT_ORIGIN (fn);
525 if (!fn
526 || TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
527 || (!DECL_CXX_CONSTRUCTOR_P (fn) && !DECL_CXX_DESTRUCTOR_P (fn)))
528 return NULL_TREE;
531 if (flags_from_decl_or_type (fn) & (ECF_PURE | ECF_CONST))
532 return NULL_TREE;
534 return fn;
538 /* We know that the instance is stored in variable or parameter
539 (not dynamically allocated) and we want to disprove the fact
540 that it may be in construction at invocation of CALL.
542 BASE represents memory location where instance is stored.
543 If BASE is NULL, it is assumed to be global memory.
544 OUTER_TYPE is known type of the instance or NULL if not
545 known.
547 For the variable to be in construction we actually need to
548 be in constructor of corresponding global variable or
549 the inline stack of CALL must contain the constructor.
550 Check this condition. This check works safely only before
551 IPA passes, because inline stacks may become out of date
552 later. */
554 bool
555 decl_maybe_in_construction_p (tree base, tree outer_type,
556 gimple call, tree function)
558 if (outer_type)
559 outer_type = TYPE_MAIN_VARIANT (outer_type);
560 gcc_assert (!base || DECL_P (base));
562 /* After inlining the code unification optimizations may invalidate
563 inline stacks. Also we need to give up on global variables after
564 IPA, because addresses of these may have been propagated to their
565 constructors. */
566 if (DECL_STRUCT_FUNCTION (function)->after_inlining)
567 return true;
569 /* Pure functions can not do any changes on the dynamic type;
570 that require writting to memory. */
571 if ((!base || !auto_var_in_fn_p (base, function))
572 && flags_from_decl_or_type (function) & (ECF_PURE | ECF_CONST))
573 return false;
575 bool check_clones = !base || is_global_var (base);
576 for (tree block = gimple_block (call); block && TREE_CODE (block) == BLOCK;
577 block = BLOCK_SUPERCONTEXT (block))
578 if (tree fn = inlined_polymorphic_ctor_dtor_block_p (block, check_clones))
580 tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
582 if (!outer_type || !types_odr_comparable (type, outer_type))
584 if (TREE_CODE (type) == RECORD_TYPE
585 && TYPE_BINFO (type)
586 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
587 return true;
589 else if (types_same_for_odr (type, outer_type))
590 return true;
593 if (!base || (TREE_CODE (base) == VAR_DECL && is_global_var (base)))
595 if (TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
596 || (!DECL_CXX_CONSTRUCTOR_P (function)
597 && !DECL_CXX_DESTRUCTOR_P (function)))
599 if (!DECL_ABSTRACT_ORIGIN (function))
600 return false;
601 /* Watch for clones where we constant propagated the first
602 argument (pointer to the instance). */
603 function = DECL_ABSTRACT_ORIGIN (function);
604 if (!function
605 || TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
606 || (!DECL_CXX_CONSTRUCTOR_P (function)
607 && !DECL_CXX_DESTRUCTOR_P (function)))
608 return false;
610 tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (function));
611 if (!outer_type || !types_odr_comparable (type, outer_type))
613 if (TREE_CODE (type) == RECORD_TYPE
614 && TYPE_BINFO (type)
615 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
616 return true;
618 else if (types_same_for_odr (type, outer_type))
619 return true;
621 return false;
624 /* Dump human readable context to F. If NEWLINE is true, it will be terminated
625 by a newline. */
627 void
628 ipa_polymorphic_call_context::dump (FILE *f, bool newline) const
630 fprintf (f, " ");
631 if (invalid)
632 fprintf (f, "Call is known to be undefined");
633 else
635 if (useless_p ())
636 fprintf (f, "nothing known");
637 if (outer_type || offset)
639 fprintf (f, "Outer type%s:", dynamic ? " (dynamic)":"");
640 print_generic_expr (f, outer_type, TDF_SLIM);
641 if (maybe_derived_type)
642 fprintf (f, " (or a derived type)");
643 if (maybe_in_construction)
644 fprintf (f, " (maybe in construction)");
645 fprintf (f, " offset " HOST_WIDE_INT_PRINT_DEC,
646 offset);
648 if (speculative_outer_type)
650 if (outer_type || offset)
651 fprintf (f, " ");
652 fprintf (f, "Speculative outer type:");
653 print_generic_expr (f, speculative_outer_type, TDF_SLIM);
654 if (speculative_maybe_derived_type)
655 fprintf (f, " (or a derived type)");
656 fprintf (f, " at offset " HOST_WIDE_INT_PRINT_DEC,
657 speculative_offset);
660 if (newline)
661 fprintf(f, "\n");
664 /* Print context to stderr. */
666 void
667 ipa_polymorphic_call_context::debug () const
669 dump (stderr);
672 /* Stream out the context to OB. */
674 void
675 ipa_polymorphic_call_context::stream_out (struct output_block *ob) const
677 struct bitpack_d bp = bitpack_create (ob->main_stream);
679 bp_pack_value (&bp, invalid, 1);
680 bp_pack_value (&bp, maybe_in_construction, 1);
681 bp_pack_value (&bp, maybe_derived_type, 1);
682 bp_pack_value (&bp, speculative_maybe_derived_type, 1);
683 bp_pack_value (&bp, dynamic, 1);
684 bp_pack_value (&bp, outer_type != NULL, 1);
685 bp_pack_value (&bp, offset != 0, 1);
686 bp_pack_value (&bp, speculative_outer_type != NULL, 1);
687 streamer_write_bitpack (&bp);
689 if (outer_type != NULL)
690 stream_write_tree (ob, outer_type, true);
691 if (offset)
692 streamer_write_hwi (ob, offset);
693 if (speculative_outer_type != NULL)
695 stream_write_tree (ob, speculative_outer_type, true);
696 streamer_write_hwi (ob, speculative_offset);
698 else
699 gcc_assert (!speculative_offset);
702 /* Stream in the context from IB and DATA_IN. */
704 void
705 ipa_polymorphic_call_context::stream_in (struct lto_input_block *ib,
706 struct data_in *data_in)
708 struct bitpack_d bp = streamer_read_bitpack (ib);
710 invalid = bp_unpack_value (&bp, 1);
711 maybe_in_construction = bp_unpack_value (&bp, 1);
712 maybe_derived_type = bp_unpack_value (&bp, 1);
713 speculative_maybe_derived_type = bp_unpack_value (&bp, 1);
714 dynamic = bp_unpack_value (&bp, 1);
715 bool outer_type_p = bp_unpack_value (&bp, 1);
716 bool offset_p = bp_unpack_value (&bp, 1);
717 bool speculative_outer_type_p = bp_unpack_value (&bp, 1);
719 if (outer_type_p)
720 outer_type = stream_read_tree (ib, data_in);
721 else
722 outer_type = NULL;
723 if (offset_p)
724 offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
725 else
726 offset = 0;
727 if (speculative_outer_type_p)
729 speculative_outer_type = stream_read_tree (ib, data_in);
730 speculative_offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
732 else
734 speculative_outer_type = NULL;
735 speculative_offset = 0;
739 /* Proudce polymorphic call context for call method of instance
740 that is located within BASE (that is assumed to be a decl) at offset OFF. */
742 void
743 ipa_polymorphic_call_context::set_by_decl (tree base, HOST_WIDE_INT off)
745 gcc_assert (DECL_P (base));
746 clear_speculation ();
748 if (!contains_polymorphic_type_p (TREE_TYPE (base)))
750 clear_outer_type ();
751 offset = off;
752 return;
754 outer_type = TYPE_MAIN_VARIANT (TREE_TYPE (base));
755 offset = off;
756 /* Make very conservative assumption that all objects
757 may be in construction.
759 It is up to caller to revisit this via
760 get_dynamic_type or decl_maybe_in_construction_p. */
761 maybe_in_construction = true;
762 maybe_derived_type = false;
763 dynamic = false;
766 /* CST is an invariant (address of decl), try to get meaningful
767 polymorphic call context for polymorphic call of method
768 if instance of OTR_TYPE that is located at offset OFF of this invariant.
769 Return FALSE if nothing meaningful can be found. */
771 bool
772 ipa_polymorphic_call_context::set_by_invariant (tree cst,
773 tree otr_type,
774 HOST_WIDE_INT off)
776 HOST_WIDE_INT offset2, size, max_size;
777 tree base;
779 invalid = false;
780 off = 0;
781 clear_outer_type (otr_type);
783 if (TREE_CODE (cst) != ADDR_EXPR)
784 return false;
786 cst = TREE_OPERAND (cst, 0);
787 base = get_ref_base_and_extent (cst, &offset2, &size, &max_size);
788 if (!DECL_P (base) || max_size == -1 || max_size != size)
789 return false;
791 /* Only type inconsistent programs can have otr_type that is
792 not part of outer type. */
793 if (otr_type && !contains_type_p (TREE_TYPE (base), off, otr_type))
794 return false;
796 set_by_decl (base, off);
797 return true;
800 /* See if OP is SSA name initialized as a copy or by single assignment.
801 If so, walk the SSA graph up. Because simple PHI conditional is considered
802 copy, GLOBAL_VISITED may be used to avoid infinite loop walking the SSA
803 graph. */
805 static tree
806 walk_ssa_copies (tree op, hash_set<tree> **global_visited = NULL)
808 hash_set <tree> *visited = NULL;
809 STRIP_NOPS (op);
810 while (TREE_CODE (op) == SSA_NAME
811 && !SSA_NAME_IS_DEFAULT_DEF (op)
812 /* We might be called via fold_stmt during cfgcleanup where
813 SSA form need not be up-to-date. */
814 && !name_registered_for_update_p (op)
815 && (gimple_assign_single_p (SSA_NAME_DEF_STMT (op))
816 || gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI))
818 if (global_visited)
820 if (!*global_visited)
821 *global_visited = new hash_set<tree>;
822 if ((*global_visited)->add (op))
823 goto done;
825 else
827 if (!visited)
828 visited = new hash_set<tree>;
829 if (visited->add (op))
830 goto done;
832 /* Special case
833 if (ptr == 0)
834 ptr = 0;
835 else
836 ptr = ptr.foo;
837 This pattern is implicitly produced for casts to non-primary
838 bases. When doing context analysis, we do not really care
839 about the case pointer is NULL, becuase the call will be
840 undefined anyway. */
841 if (gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI)
843 gimple phi = SSA_NAME_DEF_STMT (op);
845 if (gimple_phi_num_args (phi) > 2)
846 goto done;
847 if (gimple_phi_num_args (phi) == 1)
848 op = gimple_phi_arg_def (phi, 0);
849 else if (integer_zerop (gimple_phi_arg_def (phi, 0)))
850 op = gimple_phi_arg_def (phi, 1);
851 else if (integer_zerop (gimple_phi_arg_def (phi, 1)))
852 op = gimple_phi_arg_def (phi, 0);
853 else
854 goto done;
856 else
858 if (gimple_assign_load_p (SSA_NAME_DEF_STMT (op)))
859 goto done;
860 op = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op));
862 STRIP_NOPS (op);
864 done:
865 if (visited)
866 delete (visited);
867 return op;
870 /* Create polymorphic call context from IP invariant CST.
871 This is typically &global_var.
872 OTR_TYPE specify type of polymorphic call or NULL if unknown, OFF
873 is offset of call. */
875 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree cst,
876 tree otr_type,
877 HOST_WIDE_INT off)
879 clear_speculation ();
880 set_by_invariant (cst, otr_type, off);
883 /* Build context for pointer REF contained in FNDECL at statement STMT.
884 if INSTANCE is non-NULL, return pointer to the object described by
885 the context or DECL where context is contained in. */
887 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl,
888 tree ref,
889 gimple stmt,
890 tree *instance)
892 tree otr_type = NULL;
893 tree base_pointer;
894 hash_set <tree> *visited = NULL;
896 if (TREE_CODE (ref) == OBJ_TYPE_REF)
898 otr_type = obj_type_ref_class (ref);
899 base_pointer = OBJ_TYPE_REF_OBJECT (ref);
901 else
902 base_pointer = ref;
904 /* Set up basic info in case we find nothing interesting in the analysis. */
905 clear_speculation ();
906 clear_outer_type (otr_type);
907 invalid = false;
909 /* Walk SSA for outer object. */
910 while (true)
912 base_pointer = walk_ssa_copies (base_pointer, &visited);
913 if (TREE_CODE (base_pointer) == ADDR_EXPR)
915 HOST_WIDE_INT size, max_size;
916 HOST_WIDE_INT offset2;
917 tree base = get_ref_base_and_extent (TREE_OPERAND (base_pointer, 0),
918 &offset2, &size, &max_size);
920 if (max_size != -1 && max_size == size)
921 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base)),
922 offset + offset2,
923 true,
924 NULL /* Do not change outer type. */);
926 /* If this is a varying address, punt. */
927 if ((TREE_CODE (base) == MEM_REF || DECL_P (base))
928 && max_size != -1
929 && max_size == size)
931 /* We found dereference of a pointer. Type of the pointer
932 and MEM_REF is meaningless, but we can look futher. */
933 if (TREE_CODE (base) == MEM_REF)
935 base_pointer = TREE_OPERAND (base, 0);
936 offset
937 += offset2 + mem_ref_offset (base).to_short_addr () * BITS_PER_UNIT;
938 outer_type = NULL;
940 /* We found base object. In this case the outer_type
941 is known. */
942 else if (DECL_P (base))
944 if (visited)
945 delete (visited);
946 /* Only type inconsistent programs can have otr_type that is
947 not part of outer type. */
948 if (otr_type
949 && !contains_type_p (TREE_TYPE (base),
950 offset + offset2, otr_type))
952 invalid = true;
953 if (instance)
954 *instance = base_pointer;
955 return;
957 set_by_decl (base, offset + offset2);
958 if (outer_type && maybe_in_construction && stmt)
959 maybe_in_construction
960 = decl_maybe_in_construction_p (base,
961 outer_type,
962 stmt,
963 fndecl);
964 if (instance)
965 *instance = base;
966 return;
968 else
969 break;
971 else
972 break;
974 else if (TREE_CODE (base_pointer) == POINTER_PLUS_EXPR
975 && tree_fits_uhwi_p (TREE_OPERAND (base_pointer, 1)))
977 offset += tree_to_shwi (TREE_OPERAND (base_pointer, 1))
978 * BITS_PER_UNIT;
979 base_pointer = TREE_OPERAND (base_pointer, 0);
981 else
982 break;
985 if (visited)
986 delete (visited);
988 /* Try to determine type of the outer object. */
989 if (TREE_CODE (base_pointer) == SSA_NAME
990 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
991 && TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL)
993 /* See if parameter is THIS pointer of a method. */
994 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE
995 && SSA_NAME_VAR (base_pointer) == DECL_ARGUMENTS (fndecl))
997 outer_type
998 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
999 gcc_assert (TREE_CODE (outer_type) == RECORD_TYPE
1000 || TREE_CODE (outer_type) == UNION_TYPE);
1002 /* Dynamic casting has possibly upcasted the type
1003 in the hiearchy. In this case outer type is less
1004 informative than inner type and we should forget
1005 about it. */
1006 if ((otr_type
1007 && !contains_type_p (outer_type, offset,
1008 otr_type))
1009 || !contains_polymorphic_type_p (outer_type))
1011 outer_type = NULL;
1012 if (instance)
1013 *instance = base_pointer;
1014 return;
1017 dynamic = true;
1019 /* If the function is constructor or destructor, then
1020 the type is possibly in construction, but we know
1021 it is not derived type. */
1022 if (DECL_CXX_CONSTRUCTOR_P (fndecl)
1023 || DECL_CXX_DESTRUCTOR_P (fndecl))
1025 maybe_in_construction = true;
1026 maybe_derived_type = false;
1028 else
1030 maybe_derived_type = true;
1031 maybe_in_construction = false;
1033 if (instance)
1034 *instance = base_pointer;
1035 return;
1037 /* Non-PODs passed by value are really passed by invisible
1038 reference. In this case we also know the type of the
1039 object. */
1040 if (DECL_BY_REFERENCE (SSA_NAME_VAR (base_pointer)))
1042 outer_type
1043 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
1044 /* Only type inconsistent programs can have otr_type that is
1045 not part of outer type. */
1046 if (otr_type && !contains_type_p (outer_type, offset,
1047 otr_type))
1049 invalid = true;
1050 if (instance)
1051 *instance = base_pointer;
1052 return;
1054 /* Non-polymorphic types have no interest for us. */
1055 else if (!otr_type && !contains_polymorphic_type_p (outer_type))
1057 outer_type = NULL;
1058 if (instance)
1059 *instance = base_pointer;
1060 return;
1062 maybe_derived_type = false;
1063 maybe_in_construction = false;
1064 if (instance)
1065 *instance = base_pointer;
1066 return;
1070 tree base_type = TREE_TYPE (base_pointer);
1072 if (TREE_CODE (base_pointer) == SSA_NAME
1073 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
1074 && !(TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL
1075 || TREE_CODE (SSA_NAME_VAR (base_pointer)) == RESULT_DECL))
1077 invalid = true;
1078 if (instance)
1079 *instance = base_pointer;
1080 return;
1082 if (TREE_CODE (base_pointer) == SSA_NAME
1083 && SSA_NAME_DEF_STMT (base_pointer)
1084 && gimple_assign_single_p (SSA_NAME_DEF_STMT (base_pointer)))
1085 base_type = TREE_TYPE (gimple_assign_rhs1
1086 (SSA_NAME_DEF_STMT (base_pointer)));
1088 if (base_type && POINTER_TYPE_P (base_type))
1089 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base_type)),
1090 offset,
1091 true, NULL /* Do not change type here */);
1092 /* TODO: There are multiple ways to derive a type. For instance
1093 if BASE_POINTER is passed to an constructor call prior our refernece.
1094 We do not make this type of flow sensitive analysis yet. */
1095 if (instance)
1096 *instance = base_pointer;
1097 return;
1100 /* Structure to be passed in between detect_type_change and
1101 check_stmt_for_type_change. */
1103 struct type_change_info
1105 /* Offset into the object where there is the virtual method pointer we are
1106 looking for. */
1107 HOST_WIDE_INT offset;
1108 /* The declaration or SSA_NAME pointer of the base that we are checking for
1109 type change. */
1110 tree instance;
1111 /* The reference to virtual table pointer used. */
1112 tree vtbl_ptr_ref;
1113 tree otr_type;
1114 /* If we actually can tell the type that the object has changed to, it is
1115 stored in this field. Otherwise it remains NULL_TREE. */
1116 tree known_current_type;
1117 HOST_WIDE_INT known_current_offset;
1119 /* Set to true if dynamic type change has been detected. */
1120 bool type_maybe_changed;
1121 /* Set to true if multiple types have been encountered. known_current_type
1122 must be disregarded in that case. */
1123 bool multiple_types_encountered;
1124 /* Set to true if we possibly missed some dynamic type changes and we should
1125 consider the set to be speculative. */
1126 bool speculative;
1127 bool seen_unanalyzed_store;
1130 /* Return true if STMT is not call and can modify a virtual method table pointer.
1131 We take advantage of fact that vtable stores must appear within constructor
1132 and destructor functions. */
1134 static bool
1135 noncall_stmt_may_be_vtbl_ptr_store (gimple stmt)
1137 if (is_gimple_assign (stmt))
1139 tree lhs = gimple_assign_lhs (stmt);
1141 if (gimple_clobber_p (stmt))
1142 return false;
1143 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
1145 if (flag_strict_aliasing
1146 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
1147 return false;
1149 if (TREE_CODE (lhs) == COMPONENT_REF
1150 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1151 return false;
1152 /* In the future we might want to use get_base_ref_and_offset to find
1153 if there is a field corresponding to the offset and if so, proceed
1154 almost like if it was a component ref. */
1158 /* Code unification may mess with inline stacks. */
1159 if (cfun->after_inlining)
1160 return true;
1162 /* Walk the inline stack and watch out for ctors/dtors.
1163 TODO: Maybe we can require the store to appear in toplevel
1164 block of CTOR/DTOR. */
1165 for (tree block = gimple_block (stmt); block && TREE_CODE (block) == BLOCK;
1166 block = BLOCK_SUPERCONTEXT (block))
1167 if (BLOCK_ABSTRACT_ORIGIN (block)
1168 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block)) == FUNCTION_DECL)
1169 return inlined_polymorphic_ctor_dtor_block_p (block, false);
1170 return (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
1171 && (DECL_CXX_CONSTRUCTOR_P (current_function_decl)
1172 || DECL_CXX_DESTRUCTOR_P (current_function_decl)));
1175 /* If STMT can be proved to be an assignment to the virtual method table
1176 pointer of ANALYZED_OBJ and the type associated with the new table
1177 identified, return the type. Otherwise return NULL_TREE if type changes
1178 in unknown way or ERROR_MARK_NODE if type is unchanged. */
1180 static tree
1181 extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci,
1182 HOST_WIDE_INT *type_offset)
1184 HOST_WIDE_INT offset, size, max_size;
1185 tree lhs, rhs, base;
1187 if (!gimple_assign_single_p (stmt))
1188 return NULL_TREE;
1190 lhs = gimple_assign_lhs (stmt);
1191 rhs = gimple_assign_rhs1 (stmt);
1192 if (TREE_CODE (lhs) != COMPONENT_REF
1193 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1195 if (dump_file)
1196 fprintf (dump_file, " LHS is not virtual table.\n");
1197 return NULL_TREE;
1200 if (tci->vtbl_ptr_ref && operand_equal_p (lhs, tci->vtbl_ptr_ref, 0))
1202 else
1204 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1205 if (DECL_P (tci->instance))
1207 if (base != tci->instance)
1209 if (dump_file)
1211 fprintf (dump_file, " base:");
1212 print_generic_expr (dump_file, base, TDF_SLIM);
1213 fprintf (dump_file, " does not match instance:");
1214 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1215 fprintf (dump_file, "\n");
1217 return NULL_TREE;
1220 else if (TREE_CODE (base) == MEM_REF)
1222 if (!operand_equal_p (tci->instance, TREE_OPERAND (base, 0), 0))
1224 if (dump_file)
1226 fprintf (dump_file, " base mem ref:");
1227 print_generic_expr (dump_file, base, TDF_SLIM);
1228 fprintf (dump_file, " does not match instance:");
1229 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1230 fprintf (dump_file, "\n");
1232 return NULL_TREE;
1234 if (!integer_zerop (TREE_OPERAND (base, 1)))
1236 if (!tree_fits_shwi_p (TREE_OPERAND (base, 1)))
1238 if (dump_file)
1240 fprintf (dump_file, " base mem ref:");
1241 print_generic_expr (dump_file, base, TDF_SLIM);
1242 fprintf (dump_file, " has non-representable offset:");
1243 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1244 fprintf (dump_file, "\n");
1246 return NULL_TREE;
1248 else
1249 offset += tree_to_shwi (TREE_OPERAND (base, 1)) * BITS_PER_UNIT;
1252 else if (!operand_equal_p (tci->instance, base, 0)
1253 || tci->offset)
1255 if (dump_file)
1257 fprintf (dump_file, " base:");
1258 print_generic_expr (dump_file, base, TDF_SLIM);
1259 fprintf (dump_file, " does not match instance:");
1260 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1261 fprintf (dump_file, " with offset %i\n", (int)tci->offset);
1263 return tci->offset > POINTER_SIZE ? error_mark_node : NULL_TREE;
1265 if (offset != tci->offset
1266 || size != POINTER_SIZE
1267 || max_size != POINTER_SIZE)
1269 if (dump_file)
1270 fprintf (dump_file, " wrong offset %i!=%i or size %i\n",
1271 (int)offset, (int)tci->offset, (int)size);
1272 return offset + POINTER_SIZE <= tci->offset
1273 || (max_size != -1
1274 && tci->offset + POINTER_SIZE > offset + max_size)
1275 ? error_mark_node : NULL;
1279 tree vtable;
1280 unsigned HOST_WIDE_INT offset2;
1282 if (!vtable_pointer_value_to_vtable (rhs, &vtable, &offset2))
1284 if (dump_file)
1285 fprintf (dump_file, " Failed to lookup binfo\n");
1286 return NULL;
1289 tree binfo = subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
1290 offset2, vtable);
1291 if (!binfo)
1293 if (dump_file)
1294 fprintf (dump_file, " Construction vtable used\n");
1295 /* FIXME: We should suport construction contexts. */
1296 return NULL;
1299 *type_offset = tree_to_shwi (BINFO_OFFSET (binfo)) * BITS_PER_UNIT;
1300 return DECL_CONTEXT (vtable);
1303 /* Record dynamic type change of TCI to TYPE. */
1305 static void
1306 record_known_type (struct type_change_info *tci, tree type, HOST_WIDE_INT offset)
1308 if (dump_file)
1310 if (type)
1312 fprintf (dump_file, " Recording type: ");
1313 print_generic_expr (dump_file, type, TDF_SLIM);
1314 fprintf (dump_file, " at offset %i\n", (int)offset);
1316 else
1317 fprintf (dump_file, " Recording unknown type\n");
1320 /* If we found a constructor of type that is not polymorphic or
1321 that may contain the type in question as a field (not as base),
1322 restrict to the inner class first to make type matching bellow
1323 happier. */
1324 if (type
1325 && (offset
1326 || (TREE_CODE (type) != RECORD_TYPE
1327 || !TYPE_BINFO (type)
1328 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))))
1330 ipa_polymorphic_call_context context;
1332 context.offset = offset;
1333 context.outer_type = type;
1334 context.maybe_in_construction = false;
1335 context.maybe_derived_type = false;
1336 context.dynamic = true;
1337 /* If we failed to find the inner type, we know that the call
1338 would be undefined for type produced here. */
1339 if (!context.restrict_to_inner_class (tci->otr_type))
1341 if (dump_file)
1342 fprintf (dump_file, " Ignoring; does not contain otr_type\n");
1343 return;
1345 /* Watch for case we reached an POD type and anticipate placement
1346 new. */
1347 if (!context.maybe_derived_type)
1349 type = context.outer_type;
1350 offset = context.offset;
1353 if (tci->type_maybe_changed
1354 && (!types_same_for_odr (type, tci->known_current_type)
1355 || offset != tci->known_current_offset))
1356 tci->multiple_types_encountered = true;
1357 tci->known_current_type = TYPE_MAIN_VARIANT (type);
1358 tci->known_current_offset = offset;
1359 tci->type_maybe_changed = true;
1362 /* Callback of walk_aliased_vdefs and a helper function for
1363 detect_type_change to check whether a particular statement may modify
1364 the virtual table pointer, and if possible also determine the new type of
1365 the (sub-)object. It stores its result into DATA, which points to a
1366 type_change_info structure. */
1368 static bool
1369 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
1371 gimple stmt = SSA_NAME_DEF_STMT (vdef);
1372 struct type_change_info *tci = (struct type_change_info *) data;
1373 tree fn;
1375 /* If we already gave up, just terminate the rest of walk. */
1376 if (tci->multiple_types_encountered)
1377 return true;
1379 if (is_gimple_call (stmt))
1381 if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
1382 return false;
1384 /* Check for a constructor call. */
1385 if ((fn = gimple_call_fndecl (stmt)) != NULL_TREE
1386 && DECL_CXX_CONSTRUCTOR_P (fn)
1387 && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1388 && gimple_call_num_args (stmt))
1390 tree op = walk_ssa_copies (gimple_call_arg (stmt, 0));
1391 tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
1392 HOST_WIDE_INT offset = 0, size, max_size;
1394 if (dump_file)
1396 fprintf (dump_file, " Checking constructor call: ");
1397 print_gimple_stmt (dump_file, stmt, 0, 0);
1400 /* See if THIS parameter seems like instance pointer. */
1401 if (TREE_CODE (op) == ADDR_EXPR)
1403 op = get_ref_base_and_extent (TREE_OPERAND (op, 0),
1404 &offset, &size, &max_size);
1405 if (size != max_size || max_size == -1)
1407 tci->speculative = true;
1408 return false;
1410 if (op && TREE_CODE (op) == MEM_REF)
1412 if (!tree_fits_shwi_p (TREE_OPERAND (op, 1)))
1414 tci->speculative = true;
1415 return false;
1417 offset += tree_to_shwi (TREE_OPERAND (op, 1))
1418 * BITS_PER_UNIT;
1419 op = TREE_OPERAND (op, 0);
1421 else if (DECL_P (op))
1423 else
1425 tci->speculative = true;
1426 return false;
1428 op = walk_ssa_copies (op);
1430 if (operand_equal_p (op, tci->instance, 0)
1431 && TYPE_SIZE (type)
1432 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1433 && tree_fits_shwi_p (TYPE_SIZE (type))
1434 && tree_to_shwi (TYPE_SIZE (type)) + offset > tci->offset)
1436 record_known_type (tci, type, tci->offset - offset);
1437 return true;
1440 /* Calls may possibly change dynamic type by placement new. Assume
1441 it will not happen, but make result speculative only. */
1442 if (dump_file)
1444 fprintf (dump_file, " Function call may change dynamic type:");
1445 print_gimple_stmt (dump_file, stmt, 0, 0);
1447 tci->speculative = true;
1448 return false;
1450 /* Check for inlined virtual table store. */
1451 else if (noncall_stmt_may_be_vtbl_ptr_store (stmt))
1453 tree type;
1454 HOST_WIDE_INT offset = 0;
1455 if (dump_file)
1457 fprintf (dump_file, " Checking vtbl store: ");
1458 print_gimple_stmt (dump_file, stmt, 0, 0);
1461 type = extr_type_from_vtbl_ptr_store (stmt, tci, &offset);
1462 if (type == error_mark_node)
1463 return false;
1464 gcc_assert (!type || TYPE_MAIN_VARIANT (type) == type);
1465 if (!type)
1467 if (dump_file)
1468 fprintf (dump_file, " Unanalyzed store may change type.\n");
1469 tci->seen_unanalyzed_store = true;
1470 tci->speculative = true;
1472 else
1473 record_known_type (tci, type, offset);
1474 return true;
1476 else
1477 return false;
1480 /* THIS is polymorphic call context obtained from get_polymorphic_context.
1481 OTR_OBJECT is pointer to the instance returned by OBJ_TYPE_REF_OBJECT.
1482 INSTANCE is pointer to the outer instance as returned by
1483 get_polymorphic_context. To avoid creation of temporary expressions,
1484 INSTANCE may also be an declaration of get_polymorphic_context found the
1485 value to be in static storage.
1487 If the type of instance is not fully determined
1488 (either OUTER_TYPE is unknown or MAYBE_IN_CONSTRUCTION/INCLUDE_DERIVED_TYPES
1489 is set), try to walk memory writes and find the actual construction of the
1490 instance.
1492 Return true if memory is unchanged from function entry.
1494 We do not include this analysis in the context analysis itself, because
1495 it needs memory SSA to be fully built and the walk may be expensive.
1496 So it is not suitable for use withing fold_stmt and similar uses. */
1498 bool
1499 ipa_polymorphic_call_context::get_dynamic_type (tree instance,
1500 tree otr_object,
1501 tree otr_type,
1502 gimple call)
1504 struct type_change_info tci;
1505 ao_ref ao;
1506 bool function_entry_reached = false;
1507 tree instance_ref = NULL;
1508 gimple stmt = call;
1509 /* Remember OFFSET before it is modified by restrict_to_inner_class.
1510 This is because we do not update INSTANCE when walking inwards. */
1511 HOST_WIDE_INT instance_offset = offset;
1513 if (otr_type)
1514 otr_type = TYPE_MAIN_VARIANT (otr_type);
1516 /* Walk into inner type. This may clear maybe_derived_type and save us
1517 from useless work. It also makes later comparsions with static type
1518 easier. */
1519 if (outer_type && otr_type)
1521 if (!restrict_to_inner_class (otr_type))
1522 return false;
1525 if (!maybe_in_construction && !maybe_derived_type)
1526 return false;
1528 /* We need to obtain refernce to virtual table pointer. It is better
1529 to look it up in the code rather than build our own. This require bit
1530 of pattern matching, but we end up verifying that what we found is
1531 correct.
1533 What we pattern match is:
1535 tmp = instance->_vptr.A; // vtbl ptr load
1536 tmp2 = tmp[otr_token]; // vtable lookup
1537 OBJ_TYPE_REF(tmp2;instance->0) (instance);
1539 We want to start alias oracle walk from vtbl pointer load,
1540 but we may not be able to identify it, for example, when PRE moved the
1541 load around. */
1543 if (gimple_code (call) == GIMPLE_CALL)
1545 tree ref = gimple_call_fn (call);
1546 HOST_WIDE_INT offset2, size, max_size;
1548 if (TREE_CODE (ref) == OBJ_TYPE_REF)
1550 ref = OBJ_TYPE_REF_EXPR (ref);
1551 ref = walk_ssa_copies (ref);
1553 /* If call target is already known, no need to do the expensive
1554 memory walk. */
1555 if (is_gimple_min_invariant (ref))
1556 return false;
1558 /* Check if definition looks like vtable lookup. */
1559 if (TREE_CODE (ref) == SSA_NAME
1560 && !SSA_NAME_IS_DEFAULT_DEF (ref)
1561 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref))
1562 && TREE_CODE (gimple_assign_rhs1
1563 (SSA_NAME_DEF_STMT (ref))) == MEM_REF)
1565 ref = get_base_address
1566 (TREE_OPERAND (gimple_assign_rhs1
1567 (SSA_NAME_DEF_STMT (ref)), 0));
1568 ref = walk_ssa_copies (ref);
1569 /* Find base address of the lookup and see if it looks like
1570 vptr load. */
1571 if (TREE_CODE (ref) == SSA_NAME
1572 && !SSA_NAME_IS_DEFAULT_DEF (ref)
1573 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref)))
1575 tree ref_exp = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (ref));
1576 tree base_ref = get_ref_base_and_extent
1577 (ref_exp, &offset2, &size, &max_size);
1579 /* Finally verify that what we found looks like read from OTR_OBJECT
1580 or from INSTANCE with offset OFFSET. */
1581 if (base_ref
1582 && ((TREE_CODE (base_ref) == MEM_REF
1583 && ((offset2 == instance_offset
1584 && TREE_OPERAND (base_ref, 0) == instance)
1585 || (!offset2 && TREE_OPERAND (base_ref, 0) == otr_object)))
1586 || (DECL_P (instance) && base_ref == instance
1587 && offset2 == instance_offset)))
1589 stmt = SSA_NAME_DEF_STMT (ref);
1590 instance_ref = ref_exp;
1597 /* If we failed to look up the refernece in code, build our own. */
1598 if (!instance_ref)
1600 /* If the statement in question does not use memory, we can't tell
1601 anything. */
1602 if (!gimple_vuse (stmt))
1603 return false;
1604 ao_ref_init_from_ptr_and_size (&ao, otr_object, NULL);
1606 else
1607 /* Otherwise use the real reference. */
1608 ao_ref_init (&ao, instance_ref);
1610 /* We look for vtbl pointer read. */
1611 ao.size = POINTER_SIZE;
1612 ao.max_size = ao.size;
1613 if (otr_type)
1614 ao.ref_alias_set
1615 = get_deref_alias_set (TREE_TYPE (BINFO_VTABLE (TYPE_BINFO (otr_type))));
1617 if (dump_file)
1619 fprintf (dump_file, "Determining dynamic type for call: ");
1620 print_gimple_stmt (dump_file, call, 0, 0);
1621 fprintf (dump_file, " Starting walk at: ");
1622 print_gimple_stmt (dump_file, stmt, 0, 0);
1623 fprintf (dump_file, " instance pointer: ");
1624 print_generic_expr (dump_file, otr_object, TDF_SLIM);
1625 fprintf (dump_file, " Outer instance pointer: ");
1626 print_generic_expr (dump_file, instance, TDF_SLIM);
1627 fprintf (dump_file, " offset: %i (bits)", (int)offset);
1628 fprintf (dump_file, " vtbl reference: ");
1629 print_generic_expr (dump_file, instance_ref, TDF_SLIM);
1630 fprintf (dump_file, "\n");
1633 tci.offset = offset;
1634 tci.instance = instance;
1635 tci.vtbl_ptr_ref = instance_ref;
1636 gcc_assert (TREE_CODE (instance) != MEM_REF);
1637 tci.known_current_type = NULL_TREE;
1638 tci.known_current_offset = 0;
1639 tci.otr_type = otr_type;
1640 tci.type_maybe_changed = false;
1641 tci.multiple_types_encountered = false;
1642 tci.speculative = false;
1643 tci.seen_unanalyzed_store = false;
1645 walk_aliased_vdefs (&ao, gimple_vuse (stmt), check_stmt_for_type_change,
1646 &tci, NULL, &function_entry_reached);
1648 /* If we did not find any type changing statements, we may still drop
1649 maybe_in_construction flag if the context already have outer type.
1651 Here we make special assumptions about both constructors and
1652 destructors which are all the functions that are allowed to alter the
1653 VMT pointers. It assumes that destructors begin with assignment into
1654 all VMT pointers and that constructors essentially look in the
1655 following way:
1657 1) The very first thing they do is that they call constructors of
1658 ancestor sub-objects that have them.
1660 2) Then VMT pointers of this and all its ancestors is set to new
1661 values corresponding to the type corresponding to the constructor.
1663 3) Only afterwards, other stuff such as constructor of member
1664 sub-objects and the code written by the user is run. Only this may
1665 include calling virtual functions, directly or indirectly.
1667 4) placement new can not be used to change type of non-POD statically
1668 allocated variables.
1670 There is no way to call a constructor of an ancestor sub-object in any
1671 other way.
1673 This means that we do not have to care whether constructors get the
1674 correct type information because they will always change it (in fact,
1675 if we define the type to be given by the VMT pointer, it is undefined).
1677 The most important fact to derive from the above is that if, for some
1678 statement in the section 3, we try to detect whether the dynamic type
1679 has changed, we can safely ignore all calls as we examine the function
1680 body backwards until we reach statements in section 2 because these
1681 calls cannot be ancestor constructors or destructors (if the input is
1682 not bogus) and so do not change the dynamic type (this holds true only
1683 for automatically allocated objects but at the moment we devirtualize
1684 only these). We then must detect that statements in section 2 change
1685 the dynamic type and can try to derive the new type. That is enough
1686 and we can stop, we will never see the calls into constructors of
1687 sub-objects in this code.
1689 Therefore if the static outer type was found (outer_type)
1690 we can safely ignore tci.speculative that is set on calls and give up
1691 only if there was dyanmic type store that may affect given variable
1692 (seen_unanalyzed_store) */
1694 if (!tci.type_maybe_changed
1695 || (outer_type
1696 && !dynamic
1697 && !tci.seen_unanalyzed_store
1698 && !tci.multiple_types_encountered
1699 && offset == tci.offset
1700 && types_same_for_odr (tci.known_current_type,
1701 outer_type)))
1703 if (!outer_type || tci.seen_unanalyzed_store)
1704 return false;
1705 if (maybe_in_construction)
1706 maybe_in_construction = false;
1707 if (dump_file)
1708 fprintf (dump_file, " No dynamic type change found.\n");
1709 return true;
1712 if (tci.known_current_type
1713 && !function_entry_reached
1714 && !tci.multiple_types_encountered)
1716 if (!tci.speculative)
1718 outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1719 offset = tci.known_current_offset;
1720 dynamic = true;
1721 maybe_in_construction = false;
1722 maybe_derived_type = false;
1723 if (dump_file)
1724 fprintf (dump_file, " Determined dynamic type.\n");
1726 else if (!speculative_outer_type
1727 || speculative_maybe_derived_type)
1729 speculative_outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1730 speculative_offset = tci.known_current_offset;
1731 speculative_maybe_derived_type = false;
1732 if (dump_file)
1733 fprintf (dump_file, " Determined speculative dynamic type.\n");
1736 else if (dump_file)
1738 fprintf (dump_file, " Found multiple types%s%s\n",
1739 function_entry_reached ? " (function entry reached)" : "",
1740 function_entry_reached ? " (multiple types encountered)" : "");
1743 return false;
1746 /* See if speculation given by SPEC_OUTER_TYPE, SPEC_OFFSET and SPEC_MAYBE_DERIVED_TYPE
1747 seems consistent (and useful) with what we already have in the non-speculative context. */
1749 bool
1750 ipa_polymorphic_call_context::speculation_consistent_p (tree spec_outer_type,
1751 HOST_WIDE_INT spec_offset,
1752 bool spec_maybe_derived_type,
1753 tree otr_type) const
1755 if (!flag_devirtualize_speculatively)
1756 return false;
1758 /* Non-polymorphic types are useless for deriving likely polymorphic
1759 call targets. */
1760 if (!spec_outer_type || !contains_polymorphic_type_p (spec_outer_type))
1761 return false;
1763 /* If we know nothing, speculation is always good. */
1764 if (!outer_type)
1765 return true;
1767 /* Speculation is only useful to avoid derived types.
1768 This is not 100% true for placement new, where the outer context may
1769 turn out to be useless, but ignore these for now. */
1770 if (!maybe_derived_type)
1771 return false;
1773 /* If types agrees, speculation is consistent, but it makes sense only
1774 when it says something new. */
1775 if (types_must_be_same_for_odr (spec_outer_type, outer_type))
1776 return maybe_derived_type && !spec_maybe_derived_type;
1778 /* If speculation does not contain the type in question, ignore it. */
1779 if (otr_type
1780 && !contains_type_p (spec_outer_type, spec_offset, otr_type, false, true))
1781 return false;
1783 /* If outer type already contains speculation as a filed,
1784 it is useless. We already know from OUTER_TYPE
1785 SPEC_TYPE and that it is not in the construction. */
1786 if (contains_type_p (outer_type, offset - spec_offset,
1787 spec_outer_type, false, false))
1788 return false;
1790 /* If speculative outer type is not more specified than outer
1791 type, just give up.
1792 We can only decide this safely if we can compare types with OUTER_TYPE.
1794 if ((!in_lto_p || odr_type_p (outer_type))
1795 && !contains_type_p (spec_outer_type,
1796 spec_offset - offset,
1797 outer_type, false))
1798 return false;
1799 return true;
1802 /* Improve THIS with speculation described by NEW_OUTER_TYPE, NEW_OFFSET,
1803 NEW_MAYBE_DERIVED_TYPE
1804 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1806 bool
1807 ipa_polymorphic_call_context::combine_speculation_with
1808 (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1809 tree otr_type)
1811 if (!new_outer_type)
1812 return false;
1814 /* restrict_to_inner_class may eliminate wrong speculation making our job
1815 easeier. */
1816 if (otr_type)
1817 restrict_to_inner_class (otr_type);
1819 if (!speculation_consistent_p (new_outer_type, new_offset,
1820 new_maybe_derived_type, otr_type))
1821 return false;
1823 /* New speculation is a win in case we have no speculation or new
1824 speculation does not consider derivations. */
1825 if (!speculative_outer_type
1826 || (speculative_maybe_derived_type
1827 && !new_maybe_derived_type))
1829 speculative_outer_type = new_outer_type;
1830 speculative_offset = new_offset;
1831 speculative_maybe_derived_type = new_maybe_derived_type;
1832 return true;
1834 else if (types_must_be_same_for_odr (speculative_outer_type,
1835 new_outer_type))
1837 if (speculative_offset != new_offset)
1839 /* OK we have two contexts that seems valid but they disagree,
1840 just give up.
1842 This is not a lattice operation, so we may want to drop it later. */
1843 if (dump_file && (dump_flags & TDF_DETAILS))
1844 fprintf (dump_file,
1845 "Speculative outer types match, "
1846 "offset mismatch -> invalid speculation\n");
1847 clear_speculation ();
1848 return true;
1850 else
1852 if (speculative_maybe_derived_type && !new_maybe_derived_type)
1854 speculative_maybe_derived_type = false;
1855 return true;
1857 else
1858 return false;
1861 /* Choose type that contains the other. This one either contains the outer
1862 as a field (thus giving exactly one target) or is deeper in the type
1863 hiearchy. */
1864 else if (speculative_outer_type
1865 && speculative_maybe_derived_type
1866 && (new_offset > speculative_offset
1867 || (new_offset == speculative_offset
1868 && contains_type_p (new_outer_type,
1869 0, speculative_outer_type, false))))
1871 tree old_outer_type = speculative_outer_type;
1872 HOST_WIDE_INT old_offset = speculative_offset;
1873 bool old_maybe_derived_type = speculative_maybe_derived_type;
1875 speculative_outer_type = new_outer_type;
1876 speculative_offset = new_offset;
1877 speculative_maybe_derived_type = new_maybe_derived_type;
1879 if (otr_type)
1880 restrict_to_inner_class (otr_type);
1882 /* If the speculation turned out to make no sense, revert to sensible
1883 one. */
1884 if (!speculative_outer_type)
1886 speculative_outer_type = old_outer_type;
1887 speculative_offset = old_offset;
1888 speculative_maybe_derived_type = old_maybe_derived_type;
1889 return false;
1891 return (old_offset != speculative_offset
1892 || old_maybe_derived_type != speculative_maybe_derived_type
1893 || types_must_be_same_for_odr (speculative_outer_type,
1894 new_outer_type));
1896 return false;
1899 /* Make speculation less specific so
1900 NEW_OUTER_TYPE, NEW_OFFSET, NEW_MAYBE_DERIVED_TYPE is also included.
1901 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1903 bool
1904 ipa_polymorphic_call_context::meet_speculation_with
1905 (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1906 tree otr_type)
1908 if (!new_outer_type && speculative_outer_type)
1910 clear_speculation ();
1911 return true;
1914 /* restrict_to_inner_class may eliminate wrong speculation making our job
1915 easeier. */
1916 if (otr_type)
1917 restrict_to_inner_class (otr_type);
1919 if (!speculative_outer_type
1920 || !speculation_consistent_p (speculative_outer_type,
1921 speculative_offset,
1922 speculative_maybe_derived_type,
1923 otr_type))
1924 return false;
1926 if (!speculation_consistent_p (new_outer_type, new_offset,
1927 new_maybe_derived_type, otr_type))
1929 clear_speculation ();
1930 return true;
1933 else if (types_must_be_same_for_odr (speculative_outer_type,
1934 new_outer_type))
1936 if (speculative_offset != new_offset)
1938 clear_speculation ();
1939 return true;
1941 else
1943 if (!speculative_maybe_derived_type && new_maybe_derived_type)
1945 speculative_maybe_derived_type = true;
1946 return true;
1948 else
1949 return false;
1952 /* See if one type contains the other as a field (not base). */
1953 else if (contains_type_p (new_outer_type, new_offset - speculative_offset,
1954 speculative_outer_type, false, false))
1955 return false;
1956 else if (contains_type_p (speculative_outer_type,
1957 speculative_offset - new_offset,
1958 new_outer_type, false, false))
1960 speculative_outer_type = new_outer_type;
1961 speculative_offset = new_offset;
1962 speculative_maybe_derived_type = new_maybe_derived_type;
1963 return true;
1965 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
1966 else if (contains_type_p (new_outer_type,
1967 new_offset - speculative_offset,
1968 speculative_outer_type, false, true))
1970 if (!speculative_maybe_derived_type)
1972 speculative_maybe_derived_type = true;
1973 return true;
1975 return false;
1977 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
1978 else if (contains_type_p (speculative_outer_type,
1979 speculative_offset - new_offset, new_outer_type, false, true))
1981 speculative_outer_type = new_outer_type;
1982 speculative_offset = new_offset;
1983 speculative_maybe_derived_type = true;
1984 return true;
1986 else
1988 if (dump_file && (dump_flags & TDF_DETAILS))
1989 fprintf (dump_file, "Giving up on speculative meet\n");
1990 clear_speculation ();
1991 return true;
1995 /* Assume that both THIS and a given context is valid and strenghten THIS
1996 if possible. Return true if any strenghtening was made.
1997 If actual type the context is being used in is known, OTR_TYPE should be
1998 set accordingly. This improves quality of combined result. */
2000 bool
2001 ipa_polymorphic_call_context::combine_with (ipa_polymorphic_call_context ctx,
2002 tree otr_type)
2004 bool updated = false;
2006 if (ctx.useless_p () || invalid)
2007 return false;
2009 /* Restricting context to inner type makes merging easier, however do not
2010 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2011 if (otr_type && !invalid && !ctx.invalid)
2013 restrict_to_inner_class (otr_type);
2014 ctx.restrict_to_inner_class (otr_type);
2015 if(invalid)
2016 return false;
2019 if (dump_file && (dump_flags & TDF_DETAILS))
2021 fprintf (dump_file, "Polymorphic call context combine:");
2022 dump (dump_file);
2023 fprintf (dump_file, "With context: ");
2024 ctx.dump (dump_file);
2025 if (otr_type)
2027 fprintf (dump_file, "To be used with type: ");
2028 print_generic_expr (dump_file, otr_type, TDF_SLIM);
2029 fprintf (dump_file, "\n");
2033 /* If call is known to be invalid, we are done. */
2034 if (ctx.invalid)
2036 if (dump_file && (dump_flags & TDF_DETAILS))
2037 fprintf (dump_file, "-> Invalid context\n");
2038 goto invalidate;
2041 if (!ctx.outer_type)
2043 else if (!outer_type)
2045 outer_type = ctx.outer_type;
2046 offset = ctx.offset;
2047 dynamic = ctx.dynamic;
2048 maybe_in_construction = ctx.maybe_in_construction;
2049 maybe_derived_type = ctx.maybe_derived_type;
2050 updated = true;
2052 /* If types are known to be same, merging is quite easy. */
2053 else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2055 if (offset != ctx.offset
2056 && TYPE_SIZE (outer_type)
2057 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2059 if (dump_file && (dump_flags & TDF_DETAILS))
2060 fprintf (dump_file, "Outer types match, offset mismatch -> invalid\n");
2061 clear_speculation ();
2062 clear_outer_type ();
2063 invalid = true;
2064 return true;
2066 if (dump_file && (dump_flags & TDF_DETAILS))
2067 fprintf (dump_file, "Outer types match, merging flags\n");
2068 if (maybe_in_construction && !ctx.maybe_in_construction)
2070 updated = true;
2071 maybe_in_construction = false;
2073 if (maybe_derived_type && !ctx.maybe_derived_type)
2075 updated = true;
2076 maybe_derived_type = false;
2078 if (dynamic && !ctx.dynamic)
2080 updated = true;
2081 dynamic = false;
2084 /* If we know the type precisely, there is not much to improve. */
2085 else if (!maybe_derived_type && !maybe_in_construction
2086 && !ctx.maybe_derived_type && !ctx.maybe_in_construction)
2088 /* It may be easy to check if second context permits the first
2089 and set INVALID otherwise. This is not easy to do in general;
2090 contains_type_p may return false negatives for non-comparable
2091 types.
2093 If OTR_TYPE is known, we however can expect that
2094 restrict_to_inner_class should have discovered the same base
2095 type. */
2096 if (otr_type && !ctx.maybe_in_construction && !ctx.maybe_derived_type)
2098 if (dump_file && (dump_flags & TDF_DETAILS))
2099 fprintf (dump_file, "Contextes disagree -> invalid\n");
2100 goto invalidate;
2103 /* See if one type contains the other as a field (not base).
2104 In this case we want to choose the wider type, because it contains
2105 more information. */
2106 else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2107 outer_type, false, false))
2109 if (dump_file && (dump_flags & TDF_DETAILS))
2110 fprintf (dump_file, "Second type contain the first as a field\n");
2112 if (maybe_derived_type)
2114 outer_type = ctx.outer_type;
2115 maybe_derived_type = ctx.maybe_derived_type;
2116 offset = ctx.offset;
2117 dynamic = ctx.dynamic;
2118 updated = true;
2121 /* If we do not know how the context is being used, we can
2122 not clear MAYBE_IN_CONSTRUCTION because it may be offseted
2123 to other component of OUTER_TYPE later and we know nothing
2124 about it. */
2125 if (otr_type && maybe_in_construction
2126 && !ctx.maybe_in_construction)
2128 maybe_in_construction = false;
2129 updated = true;
2132 else if (contains_type_p (outer_type, offset - ctx.offset,
2133 ctx.outer_type, false, false))
2135 if (dump_file && (dump_flags & TDF_DETAILS))
2136 fprintf (dump_file, "First type contain the second as a field\n");
2138 if (otr_type && maybe_in_construction
2139 && !ctx.maybe_in_construction)
2141 maybe_in_construction = false;
2142 updated = true;
2145 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2146 else if (contains_type_p (ctx.outer_type,
2147 ctx.offset - offset, outer_type, false, true))
2149 if (dump_file && (dump_flags & TDF_DETAILS))
2150 fprintf (dump_file, "First type is base of second\n");
2151 if (!maybe_derived_type)
2153 if (!ctx.maybe_in_construction
2154 && types_odr_comparable (outer_type, ctx.outer_type))
2156 if (dump_file && (dump_flags & TDF_DETAILS))
2157 fprintf (dump_file, "Second context does not permit base -> invalid\n");
2158 goto invalidate;
2161 /* Pick variant deeper in the hiearchy. */
2162 else
2164 outer_type = ctx.outer_type;
2165 maybe_in_construction = ctx.maybe_in_construction;
2166 maybe_derived_type = ctx.maybe_derived_type;
2167 offset = ctx.offset;
2168 dynamic = ctx.dynamic;
2169 updated = true;
2172 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2173 else if (contains_type_p (outer_type,
2174 offset - ctx.offset, ctx.outer_type, false, true))
2176 if (dump_file && (dump_flags & TDF_DETAILS))
2177 fprintf (dump_file, "Second type is base of first\n");
2178 if (!ctx.maybe_derived_type)
2180 if (!maybe_in_construction
2181 && types_odr_comparable (outer_type, ctx.outer_type))
2183 if (dump_file && (dump_flags & TDF_DETAILS))
2184 fprintf (dump_file, "First context does not permit base -> invalid\n");
2185 goto invalidate;
2187 /* Pick the base type. */
2188 else if (maybe_in_construction)
2190 outer_type = ctx.outer_type;
2191 maybe_in_construction = ctx.maybe_in_construction;
2192 maybe_derived_type = ctx.maybe_derived_type;
2193 offset = ctx.offset;
2194 dynamic = ctx.dynamic;
2195 updated = true;
2199 /* TODO handle merging using hiearchy. */
2200 else if (dump_file && (dump_flags & TDF_DETAILS))
2201 fprintf (dump_file, "Giving up on merge\n");
2203 updated |= combine_speculation_with (ctx.speculative_outer_type,
2204 ctx.speculative_offset,
2205 ctx.speculative_maybe_derived_type,
2206 otr_type);
2208 if (updated && dump_file && (dump_flags & TDF_DETAILS))
2210 fprintf (dump_file, "Updated as: ");
2211 dump (dump_file);
2212 fprintf (dump_file, "\n");
2214 return updated;
2216 invalidate:
2217 invalid = true;
2218 clear_speculation ();
2219 clear_outer_type ();
2220 return true;
2223 /* Take non-speculative info, merge it with speculative and clear speculation.
2224 Used when we no longer manage to keep track of actual outer type, but we
2225 think it is still there.
2227 If OTR_TYPE is set, the transformation can be done more effectively assuming
2228 that context is going to be used only that way. */
2230 void
2231 ipa_polymorphic_call_context::make_speculative (tree otr_type)
2233 tree spec_outer_type = outer_type;
2234 HOST_WIDE_INT spec_offset = offset;
2235 bool spec_maybe_derived_type = maybe_derived_type;
2237 if (invalid)
2239 invalid = false;
2240 clear_outer_type ();
2241 clear_speculation ();
2242 return;
2244 if (!outer_type)
2245 return;
2246 clear_outer_type ();
2247 combine_speculation_with (spec_outer_type, spec_offset,
2248 spec_maybe_derived_type,
2249 otr_type);
2252 /* Use when we can not track dynamic type change. This speculatively assume
2253 type change is not happening. */
2255 void
2256 ipa_polymorphic_call_context::possible_dynamic_type_change (bool in_poly_cdtor,
2257 tree otr_type)
2259 if (dynamic)
2260 make_speculative (otr_type);
2261 else if (in_poly_cdtor)
2262 maybe_in_construction = true;
2265 /* Return TRUE if this context conveys the same information as OTHER. */
2267 bool
2268 ipa_polymorphic_call_context::equal_to
2269 (const ipa_polymorphic_call_context &x) const
2271 if (useless_p ())
2272 return x.useless_p ();
2273 if (invalid)
2274 return x.invalid;
2275 if (x.useless_p () || x.invalid)
2276 return false;
2278 if (outer_type)
2280 if (!x.outer_type
2281 || !types_odr_comparable (outer_type, x.outer_type)
2282 || !types_same_for_odr (outer_type, x.outer_type)
2283 || offset != x.offset
2284 || maybe_in_construction != x.maybe_in_construction
2285 || maybe_derived_type != x.maybe_derived_type
2286 || dynamic != x.dynamic)
2287 return false;
2289 else if (x.outer_type)
2290 return false;
2293 if (speculative_outer_type
2294 && speculation_consistent_p (speculative_outer_type, speculative_offset,
2295 speculative_maybe_derived_type, NULL_TREE))
2297 if (!x.speculative_outer_type)
2298 return false;
2300 if (!types_odr_comparable (speculative_outer_type,
2301 x.speculative_outer_type)
2302 || !types_same_for_odr (speculative_outer_type,
2303 x.speculative_outer_type)
2304 || speculative_offset != x.speculative_offset
2305 || speculative_maybe_derived_type != x.speculative_maybe_derived_type)
2306 return false;
2308 else if (x.speculative_outer_type
2309 && x.speculation_consistent_p (x.speculative_outer_type,
2310 x.speculative_offset,
2311 x.speculative_maybe_derived_type,
2312 NULL))
2313 return false;
2315 return true;
2318 /* Modify context to be strictly less restrictive than CTX. */
2320 bool
2321 ipa_polymorphic_call_context::meet_with (ipa_polymorphic_call_context ctx,
2322 tree otr_type)
2324 bool updated = false;
2326 if (useless_p () || ctx.invalid)
2327 return false;
2329 /* Restricting context to inner type makes merging easier, however do not
2330 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2331 if (otr_type && !useless_p () && !ctx.useless_p ())
2333 restrict_to_inner_class (otr_type);
2334 ctx.restrict_to_inner_class (otr_type);
2335 if(invalid)
2336 return false;
2339 if (equal_to (ctx))
2340 return false;
2342 if (ctx.useless_p () || invalid)
2344 *this = ctx;
2345 return true;
2348 if (dump_file && (dump_flags & TDF_DETAILS))
2350 fprintf (dump_file, "Polymorphic call context meet:");
2351 dump (dump_file);
2352 fprintf (dump_file, "With context: ");
2353 ctx.dump (dump_file);
2354 if (otr_type)
2356 fprintf (dump_file, "To be used with type: ");
2357 print_generic_expr (dump_file, otr_type, TDF_SLIM);
2358 fprintf (dump_file, "\n");
2362 if (!dynamic && ctx.dynamic)
2364 dynamic = true;
2365 updated = true;
2368 /* If call is known to be invalid, we are done. */
2369 if (!outer_type)
2371 else if (!ctx.outer_type)
2373 clear_outer_type ();
2374 updated = true;
2376 /* If types are known to be same, merging is quite easy. */
2377 else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2379 if (offset != ctx.offset
2380 && TYPE_SIZE (outer_type)
2381 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2383 if (dump_file && (dump_flags & TDF_DETAILS))
2384 fprintf (dump_file, "Outer types match, offset mismatch -> clearing\n");
2385 clear_outer_type ();
2386 return true;
2388 if (dump_file && (dump_flags & TDF_DETAILS))
2389 fprintf (dump_file, "Outer types match, merging flags\n");
2390 if (!maybe_in_construction && ctx.maybe_in_construction)
2392 updated = true;
2393 maybe_in_construction = true;
2395 if (!maybe_derived_type && ctx.maybe_derived_type)
2397 updated = true;
2398 maybe_derived_type = true;
2400 if (!dynamic && ctx.dynamic)
2402 updated = true;
2403 dynamic = true;
2406 /* See if one type contains the other as a field (not base). */
2407 else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2408 outer_type, false, false))
2410 if (dump_file && (dump_flags & TDF_DETAILS))
2411 fprintf (dump_file, "Second type contain the first as a field\n");
2413 /* The second type is more specified, so we keep the first.
2414 We need to set DYNAMIC flag to avoid declaring context INVALID
2415 of OFFSET ends up being out of range. */
2416 if (!dynamic
2417 && (ctx.dynamic
2418 || (!otr_type
2419 && (!TYPE_SIZE (ctx.outer_type)
2420 || !TYPE_SIZE (outer_type)
2421 || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2422 TYPE_SIZE (outer_type), 0)))))
2424 dynamic = true;
2425 updated = true;
2428 else if (contains_type_p (outer_type, offset - ctx.offset,
2429 ctx.outer_type, false, false))
2431 if (dump_file && (dump_flags & TDF_DETAILS))
2432 fprintf (dump_file, "First type contain the second as a field\n");
2434 if (!dynamic
2435 && (ctx.dynamic
2436 || (!otr_type
2437 && (!TYPE_SIZE (ctx.outer_type)
2438 || !TYPE_SIZE (outer_type)
2439 || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2440 TYPE_SIZE (outer_type), 0)))))
2441 dynamic = true;
2442 outer_type = ctx.outer_type;
2443 offset = ctx.offset;
2444 dynamic = ctx.dynamic;
2445 maybe_in_construction = ctx.maybe_in_construction;
2446 maybe_derived_type = ctx.maybe_derived_type;
2447 updated = true;
2449 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2450 else if (contains_type_p (ctx.outer_type,
2451 ctx.offset - offset, outer_type, false, true))
2453 if (dump_file && (dump_flags & TDF_DETAILS))
2454 fprintf (dump_file, "First type is base of second\n");
2455 if (!maybe_derived_type)
2457 maybe_derived_type = true;
2458 updated = true;
2460 if (!maybe_in_construction && ctx.maybe_in_construction)
2462 maybe_in_construction = true;
2463 updated = true;
2465 if (!dynamic && ctx.dynamic)
2467 dynamic = true;
2468 updated = true;
2471 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2472 else if (contains_type_p (outer_type,
2473 offset - ctx.offset, ctx.outer_type, false, true))
2475 if (dump_file && (dump_flags & TDF_DETAILS))
2476 fprintf (dump_file, "Second type is base of first\n");
2477 outer_type = ctx.outer_type;
2478 offset = ctx.offset;
2479 updated = true;
2480 if (!maybe_derived_type)
2481 maybe_derived_type = true;
2482 if (!maybe_in_construction && ctx.maybe_in_construction)
2483 maybe_in_construction = true;
2484 if (!dynamic && ctx.dynamic)
2485 dynamic = true;
2487 /* TODO handle merging using hiearchy. */
2488 else
2490 if (dump_file && (dump_flags & TDF_DETAILS))
2491 fprintf (dump_file, "Giving up on meet\n");
2492 clear_outer_type ();
2493 updated = true;
2496 updated |= meet_speculation_with (ctx.speculative_outer_type,
2497 ctx.speculative_offset,
2498 ctx.speculative_maybe_derived_type,
2499 otr_type);
2501 if (updated && dump_file && (dump_flags & TDF_DETAILS))
2503 fprintf (dump_file, "Updated as: ");
2504 dump (dump_file);
2505 fprintf (dump_file, "\n");
2507 return updated;