* gcc.dg/store-motion-fgcse-sm.c (dg-final): Cleanup
[official-gcc.git] / gcc / ipa-polymorphic-call.c
blob6fed594269c7f662e35ba56cefe6451dff3cd7dc
1 /* Analysis of polymorphic call context.
2 Copyright (C) 2013-2014 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 "tree.h"
26 #include "print-tree.h"
27 #include "calls.h"
28 #include "expr.h"
29 #include "tree-pass.h"
30 #include "hash-set.h"
31 #include "target.h"
32 #include "hash-table.h"
33 #include "inchash.h"
34 #include "tree-pretty-print.h"
35 #include "predict.h"
36 #include "basic-block.h"
37 #include "hash-map.h"
38 #include "is-a.h"
39 #include "plugin-api.h"
40 #include "vec.h"
41 #include "hashtab.h"
42 #include "machmode.h"
43 #include "hard-reg-set.h"
44 #include "input.h"
45 #include "function.h"
46 #include "ipa-ref.h"
47 #include "cgraph.h"
48 #include "ipa-utils.h"
49 #include "tree-ssa-alias.h"
50 #include "internal-fn.h"
51 #include "gimple-fold.h"
52 #include "gimple-expr.h"
53 #include "gimple.h"
54 #include "alloc-pool.h"
55 #include "ipa-prop.h"
56 #include "ipa-inline.h"
57 #include "diagnostic.h"
58 #include "tree-dfa.h"
59 #include "demangle.h"
60 #include "dbgcnt.h"
61 #include "gimple-pretty-print.h"
62 #include "stor-layout.h"
63 #include "intl.h"
64 #include "data-streamer.h"
65 #include "lto-streamer.h"
66 #include "streamer-hooks.h"
68 /* Return true when TYPE contains an polymorphic type and thus is interesting
69 for devirtualization machinery. */
71 static bool contains_type_p (tree, HOST_WIDE_INT, tree,
72 bool consider_placement_new = true,
73 bool consider_bases = true);
75 bool
76 contains_polymorphic_type_p (const_tree type)
78 type = TYPE_MAIN_VARIANT (type);
80 if (RECORD_OR_UNION_TYPE_P (type))
82 if (TYPE_BINFO (type)
83 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
84 return true;
85 for (tree fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
86 if (TREE_CODE (fld) == FIELD_DECL
87 && !DECL_ARTIFICIAL (fld)
88 && contains_polymorphic_type_p (TREE_TYPE (fld)))
89 return true;
90 return false;
92 if (TREE_CODE (type) == ARRAY_TYPE)
93 return contains_polymorphic_type_p (TREE_TYPE (type));
94 return false;
97 /* Return true if it seems valid to use placement new to build EXPECTED_TYPE
98 at possition CUR_OFFSET within TYPE.
100 POD can be changed to an instance of a polymorphic type by
101 placement new. Here we play safe and assume that any
102 non-polymorphic type is POD. */
103 bool
104 possible_placement_new (tree type, tree expected_type,
105 HOST_WIDE_INT cur_offset)
107 return ((TREE_CODE (type) != RECORD_TYPE
108 || !TYPE_BINFO (type)
109 || cur_offset >= POINTER_SIZE
110 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))
111 && (!TYPE_SIZE (type)
112 || !tree_fits_shwi_p (TYPE_SIZE (type))
113 || (cur_offset
114 + (expected_type ? tree_to_uhwi (TYPE_SIZE (expected_type))
115 : POINTER_SIZE)
116 <= tree_to_uhwi (TYPE_SIZE (type)))));
119 /* THIS->OUTER_TYPE is a type of memory object where object of OTR_TYPE
120 is contained at THIS->OFFSET. Walk the memory representation of
121 THIS->OUTER_TYPE and find the outermost class type that match
122 OTR_TYPE or contain OTR_TYPE as a base. Update THIS
123 to represent it.
125 If OTR_TYPE is NULL, just find outermost polymorphic type with
126 virtual table present at possition OFFSET.
128 For example when THIS represents type
129 class A
131 int a;
132 class B b;
134 and we look for type at offset sizeof(int), we end up with B and offset 0.
135 If the same is produced by multiple inheritance, we end up with A and offset
136 sizeof(int).
138 If we can not find corresponding class, give up by setting
139 THIS->OUTER_TYPE to OTR_TYPE and THIS->OFFSET to NULL.
140 Return true when lookup was sucesful.
142 When CONSIDER_PLACEMENT_NEW is false, reject contexts that may be made
143 valid only via alocation of new polymorphic type inside by means
144 of placement new.
146 When CONSIDER_BASES is false, only look for actual fields, not base types
147 of TYPE. */
149 bool
150 ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type,
151 bool consider_placement_new,
152 bool consider_bases)
154 tree type = outer_type;
155 HOST_WIDE_INT cur_offset = offset;
156 bool speculative = false;
157 bool size_unknown = false;
158 unsigned HOST_WIDE_INT otr_type_size = POINTER_SIZE;
160 /* Update OUTER_TYPE to match EXPECTED_TYPE if it is not set. */
161 if (!outer_type)
163 clear_outer_type (otr_type);
164 type = otr_type;
165 cur_offset = 0;
167 /* See if OFFSET points inside OUTER_TYPE. If it does not, we know
168 that the context is either invalid, or the instance type must be
169 derived from OUTER_TYPE.
171 Because the instance type may contain field whose type is of OUTER_TYPE,
172 we can not derive any effective information about it.
174 TODO: In the case we know all derrived types, we can definitely do better
175 here. */
176 else if (TYPE_SIZE (outer_type)
177 && tree_fits_shwi_p (TYPE_SIZE (outer_type))
178 && tree_to_shwi (TYPE_SIZE (outer_type)) >= 0
179 && tree_to_shwi (TYPE_SIZE (outer_type)) <= offset)
181 clear_outer_type (otr_type);
182 type = otr_type;
183 cur_offset = 0;
185 /* If derived type is not allowed, we know that the context is invalid.
186 For dynamic types, we really do not have information about
187 size of the memory location. It is possible that completely
188 different type is stored after outer_type. */
189 if (!maybe_derived_type && !dynamic)
191 clear_speculation ();
192 invalid = true;
193 return false;
197 if (otr_type && TYPE_SIZE (otr_type)
198 && tree_fits_shwi_p (TYPE_SIZE (otr_type)))
199 otr_type_size = tree_to_uhwi (TYPE_SIZE (otr_type));
201 if (!type || offset < 0)
202 goto no_useful_type_info;
204 /* Find the sub-object the constant actually refers to and mark whether it is
205 an artificial one (as opposed to a user-defined one).
207 This loop is performed twice; first time for outer_type and second time
208 for speculative_outer_type. The second run has SPECULATIVE set. */
209 while (true)
211 unsigned HOST_WIDE_INT pos, size;
212 tree fld;
214 /* If we do not know size of TYPE, we need to be more conservative
215 about accepting cases where we can not find EXPECTED_TYPE.
216 Generally the types that do matter here are of constant size.
217 Size_unknown case should be very rare. */
218 if (TYPE_SIZE (type)
219 && tree_fits_shwi_p (TYPE_SIZE (type))
220 && tree_to_shwi (TYPE_SIZE (type)) >= 0)
221 size_unknown = false;
222 else
223 size_unknown = true;
225 /* On a match, just return what we found. */
226 if ((otr_type
227 && types_odr_comparable (type, otr_type)
228 && types_same_for_odr (type, otr_type))
229 || (!otr_type
230 && TREE_CODE (type) == RECORD_TYPE
231 && TYPE_BINFO (type)
232 && polymorphic_type_binfo_p (TYPE_BINFO (type))))
234 if (speculative)
236 /* If we did not match the offset, just give up on speculation. */
237 if (cur_offset != 0
238 /* Also check if speculation did not end up being same as
239 non-speculation. */
240 || (types_must_be_same_for_odr (speculative_outer_type,
241 outer_type)
242 && (maybe_derived_type
243 == speculative_maybe_derived_type)))
244 clear_speculation ();
245 return true;
247 else
249 /* If type is known to be final, do not worry about derived
250 types. Testing it here may help us to avoid speculation. */
251 if (otr_type && TREE_CODE (outer_type) == RECORD_TYPE
252 && (!in_lto_p || odr_type_p (outer_type))
253 && type_known_to_have_no_deriavations_p (outer_type))
254 maybe_derived_type = false;
256 /* Type can not contain itself on an non-zero offset. In that case
257 just give up. Still accept the case where size is now known.
258 Either the second copy may appear past the end of type or within
259 the non-POD buffer located inside the variably sized type
260 itself. */
261 if (cur_offset != 0)
262 goto no_useful_type_info;
263 /* If we determined type precisely or we have no clue on
264 speuclation, we are done. */
265 if (!maybe_derived_type || !speculative_outer_type
266 || !speculation_consistent_p (speculative_outer_type,
267 speculative_offset,
268 speculative_maybe_derived_type,
269 otr_type))
271 clear_speculation ();
272 return true;
274 /* Otherwise look into speculation now. */
275 else
277 speculative = true;
278 type = speculative_outer_type;
279 cur_offset = speculative_offset;
280 continue;
285 /* Walk fields and find corresponding on at OFFSET. */
286 if (TREE_CODE (type) == RECORD_TYPE)
288 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
290 if (TREE_CODE (fld) != FIELD_DECL)
291 continue;
293 pos = int_bit_position (fld);
294 if (pos > (unsigned HOST_WIDE_INT)cur_offset)
295 continue;
297 /* Do not consider vptr itself. Not even for placement new. */
298 if (!pos && DECL_ARTIFICIAL (fld)
299 && POINTER_TYPE_P (TREE_TYPE (fld))
300 && TYPE_BINFO (type)
301 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
302 continue;
304 if (!DECL_SIZE (fld) || !tree_fits_uhwi_p (DECL_SIZE (fld)))
305 goto no_useful_type_info;
306 size = tree_to_uhwi (DECL_SIZE (fld));
308 /* We can always skip types smaller than pointer size:
309 those can not contain a virtual table pointer.
311 Disqualifying fields that are too small to fit OTR_TYPE
312 saves work needed to walk them for no benefit.
313 Because of the way the bases are packed into a class, the
314 field's size may be smaller than type size, so it needs
315 to be done with a care. */
317 if (pos <= (unsigned HOST_WIDE_INT)cur_offset
318 && (pos + size) >= (unsigned HOST_WIDE_INT)cur_offset
319 + POINTER_SIZE
320 && (!otr_type
321 || !TYPE_SIZE (TREE_TYPE (fld))
322 || !tree_fits_shwi_p (TYPE_SIZE (TREE_TYPE (fld)))
323 || (pos + tree_to_uhwi (TYPE_SIZE (TREE_TYPE (fld))))
324 >= cur_offset + otr_type_size))
325 break;
328 if (!fld)
329 goto no_useful_type_info;
331 type = TYPE_MAIN_VARIANT (TREE_TYPE (fld));
332 cur_offset -= pos;
333 /* DECL_ARTIFICIAL represents a basetype. */
334 if (!DECL_ARTIFICIAL (fld))
336 if (!speculative)
338 outer_type = type;
339 offset = cur_offset;
340 /* As soon as we se an field containing the type,
341 we know we are not looking for derivations. */
342 maybe_derived_type = false;
344 else
346 speculative_outer_type = type;
347 speculative_offset = cur_offset;
348 speculative_maybe_derived_type = false;
351 else if (!consider_bases)
352 goto no_useful_type_info;
354 else if (TREE_CODE (type) == ARRAY_TYPE)
356 tree subtype = TYPE_MAIN_VARIANT (TREE_TYPE (type));
358 /* Give up if we don't know array field size.
359 Also give up on non-polymorphic types as they are used
360 as buffers for placement new. */
361 if (!TYPE_SIZE (subtype)
362 || !tree_fits_shwi_p (TYPE_SIZE (subtype))
363 || tree_to_shwi (TYPE_SIZE (subtype)) <= 0
364 || !contains_polymorphic_type_p (subtype))
365 goto no_useful_type_info;
367 HOST_WIDE_INT new_offset = cur_offset % tree_to_shwi (TYPE_SIZE (subtype));
369 /* We may see buffer for placement new. In this case the expected type
370 can be bigger than the subtype. */
371 if (TYPE_SIZE (subtype)
372 && (cur_offset + otr_type_size
373 > tree_to_uhwi (TYPE_SIZE (subtype))))
374 goto no_useful_type_info;
376 cur_offset = new_offset;
377 type = subtype;
378 if (!speculative)
380 outer_type = type;
381 offset = cur_offset;
382 maybe_derived_type = false;
384 else
386 speculative_outer_type = type;
387 speculative_offset = cur_offset;
388 speculative_maybe_derived_type = false;
391 /* Give up on anything else. */
392 else
394 no_useful_type_info:
395 if (maybe_derived_type && !speculative
396 && TREE_CODE (outer_type) == RECORD_TYPE
397 && TREE_CODE (otr_type) == RECORD_TYPE
398 && TYPE_BINFO (otr_type)
399 && !offset
400 && get_binfo_at_offset (TYPE_BINFO (otr_type), 0, outer_type))
402 clear_outer_type (otr_type);
403 if (!speculative_outer_type
404 || !speculation_consistent_p (speculative_outer_type,
405 speculative_offset,
406 speculative_maybe_derived_type,
407 otr_type))
408 clear_speculation ();
409 if (speculative_outer_type)
411 speculative = true;
412 type = speculative_outer_type;
413 cur_offset = speculative_offset;
415 else
416 return true;
418 /* We found no way to embedd EXPECTED_TYPE in TYPE.
419 We still permit two special cases - placement new and
420 the case of variadic types containing themselves. */
421 if (!speculative
422 && consider_placement_new
423 && (size_unknown || !type || maybe_derived_type
424 || possible_placement_new (type, otr_type, cur_offset)))
426 /* In these weird cases we want to accept the context.
427 In non-speculative run we have no useful outer_type info
428 (TODO: we may eventually want to record upper bound on the
429 type size that can be used to prune the walk),
430 but we still want to consider speculation that may
431 give useful info. */
432 if (!speculative)
434 clear_outer_type (otr_type);
435 if (!speculative_outer_type
436 || !speculation_consistent_p (speculative_outer_type,
437 speculative_offset,
438 speculative_maybe_derived_type,
439 otr_type))
440 clear_speculation ();
441 if (speculative_outer_type)
443 speculative = true;
444 type = speculative_outer_type;
445 cur_offset = speculative_offset;
447 else
448 return true;
450 else
451 clear_speculation ();
452 return true;
454 else
456 clear_speculation ();
457 if (speculative)
458 return true;
459 clear_outer_type (otr_type);
460 invalid = true;
461 return false;
467 /* Return true if OUTER_TYPE contains OTR_TYPE at OFFSET.
468 CONSIDER_PLACEMENT_NEW makes function to accept cases where OTR_TYPE can
469 be built within OUTER_TYPE by means of placement new. CONSIDER_BASES makes
470 function to accept cases where OTR_TYPE appears as base of OUTER_TYPE or as
471 base of one of fields of OUTER_TYPE. */
473 static bool
474 contains_type_p (tree outer_type, HOST_WIDE_INT offset,
475 tree otr_type,
476 bool consider_placement_new,
477 bool consider_bases)
479 ipa_polymorphic_call_context context;
481 /* Check that type is within range. */
482 if (offset < 0)
483 return false;
484 if (TYPE_SIZE (outer_type) && TYPE_SIZE (otr_type)
485 && TREE_CODE (outer_type) == INTEGER_CST
486 && TREE_CODE (otr_type) == INTEGER_CST
487 && wi::ltu_p (wi::to_offset (outer_type), (wi::to_offset (otr_type) + offset)))
488 return false;
490 context.offset = offset;
491 context.outer_type = TYPE_MAIN_VARIANT (outer_type);
492 context.maybe_derived_type = false;
493 return context.restrict_to_inner_class (otr_type, consider_placement_new, consider_bases);
497 /* We know that the instance is stored in variable or parameter
498 (not dynamically allocated) and we want to disprove the fact
499 that it may be in construction at invocation of CALL.
501 BASE represents memory location where instance is stored.
502 If BASE is NULL, it is assumed to be global memory.
503 OUTER_TYPE is known type of the instance or NULL if not
504 known.
506 For the variable to be in construction we actually need to
507 be in constructor of corresponding global variable or
508 the inline stack of CALL must contain the constructor.
509 Check this condition. This check works safely only before
510 IPA passes, because inline stacks may become out of date
511 later. */
513 bool
514 decl_maybe_in_construction_p (tree base, tree outer_type,
515 gimple call, tree function)
517 if (outer_type)
518 outer_type = TYPE_MAIN_VARIANT (outer_type);
519 gcc_assert (!base || DECL_P (base));
521 /* After inlining the code unification optimizations may invalidate
522 inline stacks. Also we need to give up on global variables after
523 IPA, because addresses of these may have been propagated to their
524 constructors. */
525 if (DECL_STRUCT_FUNCTION (function)->after_inlining)
526 return true;
528 /* Pure functions can not do any changes on the dynamic type;
529 that require writting to memory. */
530 if ((!base || !auto_var_in_fn_p (base, function))
531 && flags_from_decl_or_type (function) & (ECF_PURE | ECF_CONST))
532 return false;
534 for (tree block = gimple_block (call); block && TREE_CODE (block) == BLOCK;
535 block = BLOCK_SUPERCONTEXT (block))
536 if (BLOCK_ABSTRACT_ORIGIN (block)
537 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block)) == FUNCTION_DECL)
539 tree fn = BLOCK_ABSTRACT_ORIGIN (block);
541 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
542 || (!DECL_CXX_CONSTRUCTOR_P (fn)
543 && !DECL_CXX_DESTRUCTOR_P (fn)))
545 /* Watch for clones where we constant propagated the first
546 argument (pointer to the instance). */
547 fn = DECL_ABSTRACT_ORIGIN (fn);
548 if (!fn
549 || (base && !is_global_var (base))
550 || TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
551 || (!DECL_CXX_CONSTRUCTOR_P (fn)
552 && !DECL_CXX_DESTRUCTOR_P (fn)))
553 continue;
555 if (flags_from_decl_or_type (fn) & (ECF_PURE | ECF_CONST))
556 continue;
558 tree type = TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (fn)));
560 if (!outer_type || !types_odr_comparable (type, outer_type))
562 if (TREE_CODE (type) == RECORD_TYPE
563 && TYPE_BINFO (type)
564 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
565 return true;
567 else if (types_same_for_odr (type, outer_type))
568 return true;
571 if (!base || (TREE_CODE (base) == VAR_DECL && is_global_var (base)))
573 if (TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
574 || (!DECL_CXX_CONSTRUCTOR_P (function)
575 && !DECL_CXX_DESTRUCTOR_P (function)))
577 if (!DECL_ABSTRACT_ORIGIN (function))
578 return false;
579 /* Watch for clones where we constant propagated the first
580 argument (pointer to the instance). */
581 function = DECL_ABSTRACT_ORIGIN (function);
582 if (!function
583 || TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
584 || (!DECL_CXX_CONSTRUCTOR_P (function)
585 && !DECL_CXX_DESTRUCTOR_P (function)))
586 return false;
588 tree type = TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (function)));
589 if (!outer_type || !types_odr_comparable (type, outer_type))
591 if (TREE_CODE (type) == RECORD_TYPE
592 && TYPE_BINFO (type)
593 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
594 return true;
596 else if (types_same_for_odr (type, outer_type))
597 return true;
599 return false;
602 /* Dump human readable context to F. If NEWLINE is true, it will be terminated
603 by a newline. */
605 void
606 ipa_polymorphic_call_context::dump (FILE *f, bool newline) const
608 fprintf (f, " ");
609 if (invalid)
610 fprintf (f, "Call is known to be undefined");
611 else
613 if (useless_p ())
614 fprintf (f, "nothing known");
615 if (outer_type || offset)
617 fprintf (f, "Outer type%s:", dynamic ? " (dynamic)":"");
618 print_generic_expr (f, outer_type, TDF_SLIM);
619 if (maybe_derived_type)
620 fprintf (f, " (or a derived type)");
621 if (maybe_in_construction)
622 fprintf (f, " (maybe in construction)");
623 fprintf (f, " offset "HOST_WIDE_INT_PRINT_DEC,
624 offset);
626 if (speculative_outer_type)
628 if (outer_type || offset)
629 fprintf (f, " ");
630 fprintf (f, "Speculative outer type:");
631 print_generic_expr (f, speculative_outer_type, TDF_SLIM);
632 if (speculative_maybe_derived_type)
633 fprintf (f, " (or a derived type)");
634 fprintf (f, " at offset "HOST_WIDE_INT_PRINT_DEC,
635 speculative_offset);
638 if (newline)
639 fprintf(f, "\n");
642 /* Print context to stderr. */
644 void
645 ipa_polymorphic_call_context::debug () const
647 dump (stderr);
650 /* Stream out the context to OB. */
652 void
653 ipa_polymorphic_call_context::stream_out (struct output_block *ob) const
655 struct bitpack_d bp = bitpack_create (ob->main_stream);
657 bp_pack_value (&bp, invalid, 1);
658 bp_pack_value (&bp, maybe_in_construction, 1);
659 bp_pack_value (&bp, maybe_derived_type, 1);
660 bp_pack_value (&bp, speculative_maybe_derived_type, 1);
661 bp_pack_value (&bp, dynamic, 1);
662 bp_pack_value (&bp, outer_type != NULL, 1);
663 bp_pack_value (&bp, offset != 0, 1);
664 bp_pack_value (&bp, speculative_outer_type != NULL, 1);
665 streamer_write_bitpack (&bp);
667 if (outer_type != NULL)
668 stream_write_tree (ob, outer_type, true);
669 if (offset)
670 streamer_write_hwi (ob, offset);
671 if (speculative_outer_type != NULL)
673 stream_write_tree (ob, speculative_outer_type, true);
674 streamer_write_hwi (ob, speculative_offset);
676 else
677 gcc_assert (!speculative_offset);
680 /* Stream in the context from IB and DATA_IN. */
682 void
683 ipa_polymorphic_call_context::stream_in (struct lto_input_block *ib,
684 struct data_in *data_in)
686 struct bitpack_d bp = streamer_read_bitpack (ib);
688 invalid = bp_unpack_value (&bp, 1);
689 maybe_in_construction = bp_unpack_value (&bp, 1);
690 maybe_derived_type = bp_unpack_value (&bp, 1);
691 speculative_maybe_derived_type = bp_unpack_value (&bp, 1);
692 dynamic = bp_unpack_value (&bp, 1);
693 bool outer_type_p = bp_unpack_value (&bp, 1);
694 bool offset_p = bp_unpack_value (&bp, 1);
695 bool speculative_outer_type_p = bp_unpack_value (&bp, 1);
697 if (outer_type_p)
698 outer_type = stream_read_tree (ib, data_in);
699 else
700 outer_type = NULL;
701 if (offset_p)
702 offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
703 else
704 offset = 0;
705 if (speculative_outer_type_p)
707 speculative_outer_type = stream_read_tree (ib, data_in);
708 speculative_offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
710 else
712 speculative_outer_type = NULL;
713 speculative_offset = 0;
717 /* Proudce polymorphic call context for call method of instance
718 that is located within BASE (that is assumed to be a decl) at offset OFF. */
720 void
721 ipa_polymorphic_call_context::set_by_decl (tree base, HOST_WIDE_INT off)
723 gcc_assert (DECL_P (base));
724 clear_speculation ();
726 if (!contains_polymorphic_type_p (TREE_TYPE (base)))
728 clear_outer_type ();
729 offset = off;
730 return;
732 outer_type = TYPE_MAIN_VARIANT (TREE_TYPE (base));
733 offset = off;
734 /* Make very conservative assumption that all objects
735 may be in construction.
737 It is up to caller to revisit this via
738 get_dynamic_type or decl_maybe_in_construction_p. */
739 maybe_in_construction = true;
740 maybe_derived_type = false;
741 dynamic = false;
744 /* CST is an invariant (address of decl), try to get meaningful
745 polymorphic call context for polymorphic call of method
746 if instance of OTR_TYPE that is located at offset OFF of this invariant.
747 Return FALSE if nothing meaningful can be found. */
749 bool
750 ipa_polymorphic_call_context::set_by_invariant (tree cst,
751 tree otr_type,
752 HOST_WIDE_INT off)
754 HOST_WIDE_INT offset2, size, max_size;
755 tree base;
757 invalid = false;
758 off = 0;
759 clear_outer_type (otr_type);
761 if (TREE_CODE (cst) != ADDR_EXPR)
762 return false;
764 cst = TREE_OPERAND (cst, 0);
765 base = get_ref_base_and_extent (cst, &offset2, &size, &max_size);
766 if (!DECL_P (base) || max_size == -1 || max_size != size)
767 return false;
769 /* Only type inconsistent programs can have otr_type that is
770 not part of outer type. */
771 if (otr_type && !contains_type_p (TREE_TYPE (base), off, otr_type))
772 return false;
774 set_by_decl (base, off);
775 return true;
778 /* See if OP is SSA name initialized as a copy or by single assignment.
779 If so, walk the SSA graph up. Because simple PHI conditional is considered
780 copy, GLOBAL_VISITED may be used to avoid infinite loop walking the SSA
781 graph. */
783 static tree
784 walk_ssa_copies (tree op, hash_set<tree> **global_visited = NULL)
786 hash_set <tree> *visited = NULL;
787 STRIP_NOPS (op);
788 while (TREE_CODE (op) == SSA_NAME
789 && !SSA_NAME_IS_DEFAULT_DEF (op)
790 && SSA_NAME_DEF_STMT (op)
791 && (gimple_assign_single_p (SSA_NAME_DEF_STMT (op))
792 || gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI))
794 if (global_visited)
796 if (!*global_visited)
797 *global_visited = new hash_set<tree>;
798 if ((*global_visited)->add (op))
799 goto done;
801 else
803 if (!visited)
804 visited = new hash_set<tree>;
805 if (visited->add (op))
806 goto done;
808 /* Special case
809 if (ptr == 0)
810 ptr = 0;
811 else
812 ptr = ptr.foo;
813 This pattern is implicitly produced for casts to non-primary
814 bases. When doing context analysis, we do not really care
815 about the case pointer is NULL, becuase the call will be
816 undefined anyway. */
817 if (gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI)
819 gimple phi = SSA_NAME_DEF_STMT (op);
821 if (gimple_phi_num_args (phi) > 2)
822 goto done;
823 if (gimple_phi_num_args (phi) == 1)
824 op = gimple_phi_arg_def (phi, 0);
825 else if (integer_zerop (gimple_phi_arg_def (phi, 0)))
826 op = gimple_phi_arg_def (phi, 1);
827 else if (integer_zerop (gimple_phi_arg_def (phi, 1)))
828 op = gimple_phi_arg_def (phi, 0);
829 else
830 goto done;
832 else
834 if (gimple_assign_load_p (SSA_NAME_DEF_STMT (op)))
835 goto done;
836 op = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op));
838 STRIP_NOPS (op);
840 done:
841 if (visited)
842 delete (visited);
843 return op;
846 /* Create polymorphic call context from IP invariant CST.
847 This is typically &global_var.
848 OTR_TYPE specify type of polymorphic call or NULL if unknown, OFF
849 is offset of call. */
851 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree cst,
852 tree otr_type,
853 HOST_WIDE_INT off)
855 clear_speculation ();
856 set_by_invariant (cst, otr_type, off);
859 /* Build context for pointer REF contained in FNDECL at statement STMT.
860 if INSTANCE is non-NULL, return pointer to the object described by
861 the context or DECL where context is contained in. */
863 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl,
864 tree ref,
865 gimple stmt,
866 tree *instance)
868 tree otr_type = NULL;
869 tree base_pointer;
870 hash_set <tree> *visited = NULL;
872 if (TREE_CODE (ref) == OBJ_TYPE_REF)
874 otr_type = obj_type_ref_class (ref);
875 base_pointer = OBJ_TYPE_REF_OBJECT (ref);
877 else
878 base_pointer = ref;
880 /* Set up basic info in case we find nothing interesting in the analysis. */
881 clear_speculation ();
882 clear_outer_type (otr_type);
883 invalid = false;
885 /* Walk SSA for outer object. */
886 while (true)
888 base_pointer = walk_ssa_copies (base_pointer, &visited);
889 if (TREE_CODE (base_pointer) == ADDR_EXPR)
891 HOST_WIDE_INT size, max_size;
892 HOST_WIDE_INT offset2;
893 tree base = get_ref_base_and_extent (TREE_OPERAND (base_pointer, 0),
894 &offset2, &size, &max_size);
896 if (max_size != -1 && max_size == size)
897 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base)),
898 offset + offset2,
899 true,
900 NULL /* Do not change outer type. */);
902 /* If this is a varying address, punt. */
903 if ((TREE_CODE (base) == MEM_REF || DECL_P (base))
904 && max_size != -1
905 && max_size == size)
907 /* We found dereference of a pointer. Type of the pointer
908 and MEM_REF is meaningless, but we can look futher. */
909 if (TREE_CODE (base) == MEM_REF)
911 base_pointer = TREE_OPERAND (base, 0);
912 offset
913 += offset2 + mem_ref_offset (base).to_short_addr () * BITS_PER_UNIT;
914 outer_type = NULL;
916 /* We found base object. In this case the outer_type
917 is known. */
918 else if (DECL_P (base))
920 if (visited)
921 delete (visited);
922 /* Only type inconsistent programs can have otr_type that is
923 not part of outer type. */
924 if (otr_type
925 && !contains_type_p (TREE_TYPE (base),
926 offset + offset2, otr_type))
928 invalid = true;
929 if (instance)
930 *instance = base_pointer;
931 return;
933 set_by_decl (base, offset + offset2);
934 if (outer_type && maybe_in_construction && stmt)
935 maybe_in_construction
936 = decl_maybe_in_construction_p (base,
937 outer_type,
938 stmt,
939 fndecl);
940 if (instance)
941 *instance = base;
942 return;
944 else
945 break;
947 else
948 break;
950 else if (TREE_CODE (base_pointer) == POINTER_PLUS_EXPR
951 && tree_fits_uhwi_p (TREE_OPERAND (base_pointer, 1)))
953 offset += tree_to_shwi (TREE_OPERAND (base_pointer, 1))
954 * BITS_PER_UNIT;
955 base_pointer = TREE_OPERAND (base_pointer, 0);
957 else
958 break;
961 if (visited)
962 delete (visited);
964 /* Try to determine type of the outer object. */
965 if (TREE_CODE (base_pointer) == SSA_NAME
966 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
967 && TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL)
969 /* See if parameter is THIS pointer of a method. */
970 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE
971 && SSA_NAME_VAR (base_pointer) == DECL_ARGUMENTS (fndecl))
973 outer_type
974 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
975 gcc_assert (TREE_CODE (outer_type) == RECORD_TYPE
976 || TREE_CODE (outer_type) == UNION_TYPE);
978 /* Dynamic casting has possibly upcasted the type
979 in the hiearchy. In this case outer type is less
980 informative than inner type and we should forget
981 about it. */
982 if ((otr_type
983 && !contains_type_p (outer_type, offset,
984 otr_type))
985 || !contains_polymorphic_type_p (outer_type))
987 outer_type = NULL;
988 if (instance)
989 *instance = base_pointer;
990 return;
993 dynamic = true;
995 /* If the function is constructor or destructor, then
996 the type is possibly in construction, but we know
997 it is not derived type. */
998 if (DECL_CXX_CONSTRUCTOR_P (fndecl)
999 || DECL_CXX_DESTRUCTOR_P (fndecl))
1001 maybe_in_construction = true;
1002 maybe_derived_type = false;
1004 else
1006 maybe_derived_type = true;
1007 maybe_in_construction = false;
1009 if (instance)
1010 *instance = base_pointer;
1011 return;
1013 /* Non-PODs passed by value are really passed by invisible
1014 reference. In this case we also know the type of the
1015 object. */
1016 if (DECL_BY_REFERENCE (SSA_NAME_VAR (base_pointer)))
1018 outer_type
1019 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
1020 /* Only type inconsistent programs can have otr_type that is
1021 not part of outer type. */
1022 if (otr_type && !contains_type_p (outer_type, offset,
1023 otr_type))
1025 invalid = true;
1026 if (instance)
1027 *instance = base_pointer;
1028 return;
1030 /* Non-polymorphic types have no interest for us. */
1031 else if (!otr_type && !contains_polymorphic_type_p (outer_type))
1033 outer_type = NULL;
1034 if (instance)
1035 *instance = base_pointer;
1036 return;
1038 maybe_derived_type = false;
1039 maybe_in_construction = false;
1040 if (instance)
1041 *instance = base_pointer;
1042 return;
1046 tree base_type = TREE_TYPE (base_pointer);
1048 if (TREE_CODE (base_pointer) == SSA_NAME
1049 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
1050 && TREE_CODE (SSA_NAME_VAR (base_pointer)) != PARM_DECL)
1052 invalid = true;
1053 if (instance)
1054 *instance = base_pointer;
1055 return;
1057 if (TREE_CODE (base_pointer) == SSA_NAME
1058 && SSA_NAME_DEF_STMT (base_pointer)
1059 && gimple_assign_single_p (SSA_NAME_DEF_STMT (base_pointer)))
1060 base_type = TREE_TYPE (gimple_assign_rhs1
1061 (SSA_NAME_DEF_STMT (base_pointer)));
1063 if (POINTER_TYPE_P (base_type))
1064 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base_type)),
1065 offset,
1066 true, NULL /* Do not change type here */);
1067 /* TODO: There are multiple ways to derive a type. For instance
1068 if BASE_POINTER is passed to an constructor call prior our refernece.
1069 We do not make this type of flow sensitive analysis yet. */
1070 if (instance)
1071 *instance = base_pointer;
1072 return;
1075 /* Structure to be passed in between detect_type_change and
1076 check_stmt_for_type_change. */
1078 struct type_change_info
1080 /* Offset into the object where there is the virtual method pointer we are
1081 looking for. */
1082 HOST_WIDE_INT offset;
1083 /* The declaration or SSA_NAME pointer of the base that we are checking for
1084 type change. */
1085 tree instance;
1086 /* The reference to virtual table pointer used. */
1087 tree vtbl_ptr_ref;
1088 tree otr_type;
1089 /* If we actually can tell the type that the object has changed to, it is
1090 stored in this field. Otherwise it remains NULL_TREE. */
1091 tree known_current_type;
1092 HOST_WIDE_INT known_current_offset;
1094 /* Set to true if dynamic type change has been detected. */
1095 bool type_maybe_changed;
1096 /* Set to true if multiple types have been encountered. known_current_type
1097 must be disregarded in that case. */
1098 bool multiple_types_encountered;
1099 /* Set to true if we possibly missed some dynamic type changes and we should
1100 consider the set to be speculative. */
1101 bool speculative;
1102 bool seen_unanalyzed_store;
1105 /* Return true if STMT is not call and can modify a virtual method table pointer.
1106 We take advantage of fact that vtable stores must appear within constructor
1107 and destructor functions. */
1109 static bool
1110 noncall_stmt_may_be_vtbl_ptr_store (gimple stmt)
1112 if (is_gimple_assign (stmt))
1114 tree lhs = gimple_assign_lhs (stmt);
1116 if (gimple_clobber_p (stmt))
1117 return false;
1118 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
1120 if (flag_strict_aliasing
1121 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
1122 return false;
1124 if (TREE_CODE (lhs) == COMPONENT_REF
1125 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1126 return false;
1127 /* In the future we might want to use get_base_ref_and_offset to find
1128 if there is a field corresponding to the offset and if so, proceed
1129 almost like if it was a component ref. */
1133 /* Code unification may mess with inline stacks. */
1134 if (cfun->after_inlining)
1135 return true;
1137 /* Walk the inline stack and watch out for ctors/dtors.
1138 TODO: Maybe we can require the store to appear in toplevel
1139 block of CTOR/DTOR. */
1140 for (tree block = gimple_block (stmt); block && TREE_CODE (block) == BLOCK;
1141 block = BLOCK_SUPERCONTEXT (block))
1142 if (BLOCK_ABSTRACT_ORIGIN (block)
1143 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block)) == FUNCTION_DECL)
1145 tree fn = BLOCK_ABSTRACT_ORIGIN (block);
1147 if (flags_from_decl_or_type (fn) & (ECF_PURE | ECF_CONST))
1148 return false;
1149 return (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1150 && (DECL_CXX_CONSTRUCTOR_P (fn)
1151 || DECL_CXX_DESTRUCTOR_P (fn)));
1153 return (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
1154 && (DECL_CXX_CONSTRUCTOR_P (current_function_decl)
1155 || DECL_CXX_DESTRUCTOR_P (current_function_decl)));
1158 /* If STMT can be proved to be an assignment to the virtual method table
1159 pointer of ANALYZED_OBJ and the type associated with the new table
1160 identified, return the type. Otherwise return NULL_TREE if type changes
1161 in unknown way or ERROR_MARK_NODE if type is unchanged. */
1163 static tree
1164 extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci,
1165 HOST_WIDE_INT *type_offset)
1167 HOST_WIDE_INT offset, size, max_size;
1168 tree lhs, rhs, base;
1170 if (!gimple_assign_single_p (stmt))
1171 return NULL_TREE;
1173 lhs = gimple_assign_lhs (stmt);
1174 rhs = gimple_assign_rhs1 (stmt);
1175 if (TREE_CODE (lhs) != COMPONENT_REF
1176 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1178 if (dump_file)
1179 fprintf (dump_file, " LHS is not virtual table.\n");
1180 return NULL_TREE;
1183 if (tci->vtbl_ptr_ref && operand_equal_p (lhs, tci->vtbl_ptr_ref, 0))
1185 else
1187 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1188 if (DECL_P (tci->instance))
1190 if (base != tci->instance)
1192 if (dump_file)
1194 fprintf (dump_file, " base:");
1195 print_generic_expr (dump_file, base, TDF_SLIM);
1196 fprintf (dump_file, " does not match instance:");
1197 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1198 fprintf (dump_file, "\n");
1200 return NULL_TREE;
1203 else if (TREE_CODE (base) == MEM_REF)
1205 if (!operand_equal_p (tci->instance, TREE_OPERAND (base, 0), 0))
1207 if (dump_file)
1209 fprintf (dump_file, " base mem ref:");
1210 print_generic_expr (dump_file, base, TDF_SLIM);
1211 fprintf (dump_file, " does not match instance:");
1212 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1213 fprintf (dump_file, "\n");
1215 return NULL_TREE;
1217 if (!integer_zerop (TREE_OPERAND (base, 1)))
1219 if (!tree_fits_shwi_p (TREE_OPERAND (base, 1)))
1221 if (dump_file)
1223 fprintf (dump_file, " base mem ref:");
1224 print_generic_expr (dump_file, base, TDF_SLIM);
1225 fprintf (dump_file, " has non-representable offset:");
1226 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1227 fprintf (dump_file, "\n");
1229 return NULL_TREE;
1231 else
1232 offset += tree_to_shwi (TREE_OPERAND (base, 1)) * BITS_PER_UNIT;
1235 else if (!operand_equal_p (tci->instance, base, 0)
1236 || tci->offset)
1238 if (dump_file)
1240 fprintf (dump_file, " base:");
1241 print_generic_expr (dump_file, base, TDF_SLIM);
1242 fprintf (dump_file, " does not match instance:");
1243 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1244 fprintf (dump_file, " with offset %i\n", (int)tci->offset);
1246 return tci->offset > POINTER_SIZE ? error_mark_node : NULL_TREE;
1248 if (offset != tci->offset
1249 || size != POINTER_SIZE
1250 || max_size != POINTER_SIZE)
1252 if (dump_file)
1253 fprintf (dump_file, " wrong offset %i!=%i or size %i\n",
1254 (int)offset, (int)tci->offset, (int)size);
1255 return offset + POINTER_SIZE <= tci->offset
1256 || (max_size != -1
1257 && tci->offset + POINTER_SIZE > offset + max_size)
1258 ? error_mark_node : NULL;
1262 tree vtable;
1263 unsigned HOST_WIDE_INT offset2;
1265 if (!vtable_pointer_value_to_vtable (rhs, &vtable, &offset2))
1267 if (dump_file)
1268 fprintf (dump_file, " Failed to lookup binfo\n");
1269 return NULL;
1272 tree binfo = subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
1273 offset2, vtable);
1274 if (!binfo)
1276 if (dump_file)
1277 fprintf (dump_file, " Construction vtable used\n");
1278 /* FIXME: We should suport construction contexts. */
1279 return NULL;
1282 *type_offset = tree_to_shwi (BINFO_OFFSET (binfo)) * BITS_PER_UNIT;
1283 return DECL_CONTEXT (vtable);
1286 /* Record dynamic type change of TCI to TYPE. */
1288 static void
1289 record_known_type (struct type_change_info *tci, tree type, HOST_WIDE_INT offset)
1291 if (dump_file)
1293 if (type)
1295 fprintf (dump_file, " Recording type: ");
1296 print_generic_expr (dump_file, type, TDF_SLIM);
1297 fprintf (dump_file, " at offset %i\n", (int)offset);
1299 else
1300 fprintf (dump_file, " Recording unknown type\n");
1303 /* If we found a constructor of type that is not polymorphic or
1304 that may contain the type in question as a field (not as base),
1305 restrict to the inner class first to make type matching bellow
1306 happier. */
1307 if (type
1308 && (offset
1309 || (TREE_CODE (type) != RECORD_TYPE
1310 || !TYPE_BINFO (type)
1311 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))))
1313 ipa_polymorphic_call_context context;
1315 context.offset = offset;
1316 context.outer_type = type;
1317 context.maybe_in_construction = false;
1318 context.maybe_derived_type = false;
1319 context.dynamic = true;
1320 /* If we failed to find the inner type, we know that the call
1321 would be undefined for type produced here. */
1322 if (!context.restrict_to_inner_class (tci->otr_type))
1324 if (dump_file)
1325 fprintf (dump_file, " Ignoring; does not contain otr_type\n");
1326 return;
1328 /* Watch for case we reached an POD type and anticipate placement
1329 new. */
1330 if (!context.maybe_derived_type)
1332 type = context.outer_type;
1333 offset = context.offset;
1336 if (tci->type_maybe_changed
1337 && (!types_same_for_odr (type, tci->known_current_type)
1338 || offset != tci->known_current_offset))
1339 tci->multiple_types_encountered = true;
1340 tci->known_current_type = TYPE_MAIN_VARIANT (type);
1341 tci->known_current_offset = offset;
1342 tci->type_maybe_changed = true;
1345 /* Callback of walk_aliased_vdefs and a helper function for
1346 detect_type_change to check whether a particular statement may modify
1347 the virtual table pointer, and if possible also determine the new type of
1348 the (sub-)object. It stores its result into DATA, which points to a
1349 type_change_info structure. */
1351 static bool
1352 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
1354 gimple stmt = SSA_NAME_DEF_STMT (vdef);
1355 struct type_change_info *tci = (struct type_change_info *) data;
1356 tree fn;
1358 /* If we already gave up, just terminate the rest of walk. */
1359 if (tci->multiple_types_encountered)
1360 return true;
1362 if (is_gimple_call (stmt))
1364 if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
1365 return false;
1367 /* Check for a constructor call. */
1368 if ((fn = gimple_call_fndecl (stmt)) != NULL_TREE
1369 && DECL_CXX_CONSTRUCTOR_P (fn)
1370 && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1371 && gimple_call_num_args (stmt))
1373 tree op = walk_ssa_copies (gimple_call_arg (stmt, 0));
1374 tree type = method_class_type (TREE_TYPE (fn));
1375 HOST_WIDE_INT offset = 0, size, max_size;
1377 if (dump_file)
1379 fprintf (dump_file, " Checking constructor call: ");
1380 print_gimple_stmt (dump_file, stmt, 0, 0);
1383 /* See if THIS parameter seems like instance pointer. */
1384 if (TREE_CODE (op) == ADDR_EXPR)
1386 op = get_ref_base_and_extent (TREE_OPERAND (op, 0),
1387 &offset, &size, &max_size);
1388 if (size != max_size || max_size == -1)
1390 tci->speculative = true;
1391 return false;
1393 if (op && TREE_CODE (op) == MEM_REF)
1395 if (!tree_fits_shwi_p (TREE_OPERAND (op, 1)))
1397 tci->speculative = true;
1398 return false;
1400 offset += tree_to_shwi (TREE_OPERAND (op, 1))
1401 * BITS_PER_UNIT;
1402 op = TREE_OPERAND (op, 0);
1404 else if (DECL_P (op))
1406 else
1408 tci->speculative = true;
1409 return false;
1411 op = walk_ssa_copies (op);
1413 if (operand_equal_p (op, tci->instance, 0)
1414 && TYPE_SIZE (type)
1415 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1416 && tree_fits_shwi_p (TYPE_SIZE (type))
1417 && tree_to_shwi (TYPE_SIZE (type)) + offset > tci->offset)
1419 record_known_type (tci, type, tci->offset - offset);
1420 return true;
1423 /* Calls may possibly change dynamic type by placement new. Assume
1424 it will not happen, but make result speculative only. */
1425 if (dump_file)
1427 fprintf (dump_file, " Function call may change dynamic type:");
1428 print_gimple_stmt (dump_file, stmt, 0, 0);
1430 tci->speculative = true;
1431 return false;
1433 /* Check for inlined virtual table store. */
1434 else if (noncall_stmt_may_be_vtbl_ptr_store (stmt))
1436 tree type;
1437 HOST_WIDE_INT offset = 0;
1438 if (dump_file)
1440 fprintf (dump_file, " Checking vtbl store: ");
1441 print_gimple_stmt (dump_file, stmt, 0, 0);
1444 type = extr_type_from_vtbl_ptr_store (stmt, tci, &offset);
1445 if (type == error_mark_node)
1446 return false;
1447 gcc_assert (!type || TYPE_MAIN_VARIANT (type) == type);
1448 if (!type)
1450 if (dump_file)
1451 fprintf (dump_file, " Unanalyzed store may change type.\n");
1452 tci->seen_unanalyzed_store = true;
1453 tci->speculative = true;
1455 else
1456 record_known_type (tci, type, offset);
1457 return true;
1459 else
1460 return false;
1463 /* THIS is polymorphic call context obtained from get_polymorphic_context.
1464 OTR_OBJECT is pointer to the instance returned by OBJ_TYPE_REF_OBJECT.
1465 INSTANCE is pointer to the outer instance as returned by
1466 get_polymorphic_context. To avoid creation of temporary expressions,
1467 INSTANCE may also be an declaration of get_polymorphic_context found the
1468 value to be in static storage.
1470 If the type of instance is not fully determined
1471 (either OUTER_TYPE is unknown or MAYBE_IN_CONSTRUCTION/INCLUDE_DERIVED_TYPES
1472 is set), try to walk memory writes and find the actual construction of the
1473 instance.
1475 Return true if memory is unchanged from function entry.
1477 We do not include this analysis in the context analysis itself, because
1478 it needs memory SSA to be fully built and the walk may be expensive.
1479 So it is not suitable for use withing fold_stmt and similar uses. */
1481 bool
1482 ipa_polymorphic_call_context::get_dynamic_type (tree instance,
1483 tree otr_object,
1484 tree otr_type,
1485 gimple call)
1487 struct type_change_info tci;
1488 ao_ref ao;
1489 bool function_entry_reached = false;
1490 tree instance_ref = NULL;
1491 gimple stmt = call;
1492 /* Remember OFFSET before it is modified by restrict_to_inner_class.
1493 This is because we do not update INSTANCE when walking inwards. */
1494 HOST_WIDE_INT instance_offset = offset;
1496 if (otr_type)
1497 otr_type = TYPE_MAIN_VARIANT (otr_type);
1499 /* Walk into inner type. This may clear maybe_derived_type and save us
1500 from useless work. It also makes later comparsions with static type
1501 easier. */
1502 if (outer_type && otr_type)
1504 if (!restrict_to_inner_class (otr_type))
1505 return false;
1508 if (!maybe_in_construction && !maybe_derived_type)
1509 return false;
1511 /* We need to obtain refernce to virtual table pointer. It is better
1512 to look it up in the code rather than build our own. This require bit
1513 of pattern matching, but we end up verifying that what we found is
1514 correct.
1516 What we pattern match is:
1518 tmp = instance->_vptr.A; // vtbl ptr load
1519 tmp2 = tmp[otr_token]; // vtable lookup
1520 OBJ_TYPE_REF(tmp2;instance->0) (instance);
1522 We want to start alias oracle walk from vtbl pointer load,
1523 but we may not be able to identify it, for example, when PRE moved the
1524 load around. */
1526 if (gimple_code (call) == GIMPLE_CALL)
1528 tree ref = gimple_call_fn (call);
1529 HOST_WIDE_INT offset2, size, max_size;
1531 if (TREE_CODE (ref) == OBJ_TYPE_REF)
1533 ref = OBJ_TYPE_REF_EXPR (ref);
1534 ref = walk_ssa_copies (ref);
1536 /* Check if definition looks like vtable lookup. */
1537 if (TREE_CODE (ref) == SSA_NAME
1538 && !SSA_NAME_IS_DEFAULT_DEF (ref)
1539 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref))
1540 && TREE_CODE (gimple_assign_rhs1
1541 (SSA_NAME_DEF_STMT (ref))) == MEM_REF)
1543 ref = get_base_address
1544 (TREE_OPERAND (gimple_assign_rhs1
1545 (SSA_NAME_DEF_STMT (ref)), 0));
1546 ref = walk_ssa_copies (ref);
1547 /* Find base address of the lookup and see if it looks like
1548 vptr load. */
1549 if (TREE_CODE (ref) == SSA_NAME
1550 && !SSA_NAME_IS_DEFAULT_DEF (ref)
1551 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref)))
1553 tree ref_exp = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (ref));
1554 tree base_ref = get_ref_base_and_extent
1555 (ref_exp, &offset2, &size, &max_size);
1557 /* Finally verify that what we found looks like read from OTR_OBJECT
1558 or from INSTANCE with offset OFFSET. */
1559 if (base_ref
1560 && ((TREE_CODE (base_ref) == MEM_REF
1561 && ((offset2 == instance_offset
1562 && TREE_OPERAND (base_ref, 0) == instance)
1563 || (!offset2 && TREE_OPERAND (base_ref, 0) == otr_object)))
1564 || (DECL_P (instance) && base_ref == instance
1565 && offset2 == instance_offset)))
1567 stmt = SSA_NAME_DEF_STMT (ref);
1568 instance_ref = ref_exp;
1575 /* If we failed to look up the refernece in code, build our own. */
1576 if (!instance_ref)
1578 /* If the statement in question does not use memory, we can't tell
1579 anything. */
1580 if (!gimple_vuse (stmt))
1581 return false;
1582 ao_ref_init_from_ptr_and_size (&ao, otr_object, NULL);
1584 else
1585 /* Otherwise use the real reference. */
1586 ao_ref_init (&ao, instance_ref);
1588 /* We look for vtbl pointer read. */
1589 ao.size = POINTER_SIZE;
1590 ao.max_size = ao.size;
1591 if (otr_type)
1592 ao.ref_alias_set
1593 = get_deref_alias_set (TREE_TYPE (BINFO_VTABLE (TYPE_BINFO (otr_type))));
1595 if (dump_file)
1597 fprintf (dump_file, "Determining dynamic type for call: ");
1598 print_gimple_stmt (dump_file, call, 0, 0);
1599 fprintf (dump_file, " Starting walk at: ");
1600 print_gimple_stmt (dump_file, stmt, 0, 0);
1601 fprintf (dump_file, " instance pointer: ");
1602 print_generic_expr (dump_file, otr_object, TDF_SLIM);
1603 fprintf (dump_file, " Outer instance pointer: ");
1604 print_generic_expr (dump_file, instance, TDF_SLIM);
1605 fprintf (dump_file, " offset: %i (bits)", (int)offset);
1606 fprintf (dump_file, " vtbl reference: ");
1607 print_generic_expr (dump_file, instance_ref, TDF_SLIM);
1608 fprintf (dump_file, "\n");
1611 tci.offset = offset;
1612 tci.instance = instance;
1613 tci.vtbl_ptr_ref = instance_ref;
1614 gcc_assert (TREE_CODE (instance) != MEM_REF);
1615 tci.known_current_type = NULL_TREE;
1616 tci.known_current_offset = 0;
1617 tci.otr_type = otr_type;
1618 tci.type_maybe_changed = false;
1619 tci.multiple_types_encountered = false;
1620 tci.speculative = false;
1621 tci.seen_unanalyzed_store = false;
1623 walk_aliased_vdefs (&ao, gimple_vuse (stmt), check_stmt_for_type_change,
1624 &tci, NULL, &function_entry_reached);
1626 /* If we did not find any type changing statements, we may still drop
1627 maybe_in_construction flag if the context already have outer type.
1629 Here we make special assumptions about both constructors and
1630 destructors which are all the functions that are allowed to alter the
1631 VMT pointers. It assumes that destructors begin with assignment into
1632 all VMT pointers and that constructors essentially look in the
1633 following way:
1635 1) The very first thing they do is that they call constructors of
1636 ancestor sub-objects that have them.
1638 2) Then VMT pointers of this and all its ancestors is set to new
1639 values corresponding to the type corresponding to the constructor.
1641 3) Only afterwards, other stuff such as constructor of member
1642 sub-objects and the code written by the user is run. Only this may
1643 include calling virtual functions, directly or indirectly.
1645 4) placement new can not be used to change type of non-POD statically
1646 allocated variables.
1648 There is no way to call a constructor of an ancestor sub-object in any
1649 other way.
1651 This means that we do not have to care whether constructors get the
1652 correct type information because they will always change it (in fact,
1653 if we define the type to be given by the VMT pointer, it is undefined).
1655 The most important fact to derive from the above is that if, for some
1656 statement in the section 3, we try to detect whether the dynamic type
1657 has changed, we can safely ignore all calls as we examine the function
1658 body backwards until we reach statements in section 2 because these
1659 calls cannot be ancestor constructors or destructors (if the input is
1660 not bogus) and so do not change the dynamic type (this holds true only
1661 for automatically allocated objects but at the moment we devirtualize
1662 only these). We then must detect that statements in section 2 change
1663 the dynamic type and can try to derive the new type. That is enough
1664 and we can stop, we will never see the calls into constructors of
1665 sub-objects in this code.
1667 Therefore if the static outer type was found (outer_type)
1668 we can safely ignore tci.speculative that is set on calls and give up
1669 only if there was dyanmic type store that may affect given variable
1670 (seen_unanalyzed_store) */
1672 if (!tci.type_maybe_changed
1673 || (outer_type
1674 && !dynamic
1675 && !tci.seen_unanalyzed_store
1676 && !tci.multiple_types_encountered
1677 && offset == tci.offset
1678 && types_same_for_odr (tci.known_current_type,
1679 outer_type)))
1681 if (!outer_type || tci.seen_unanalyzed_store)
1682 return false;
1683 if (maybe_in_construction)
1684 maybe_in_construction = false;
1685 if (dump_file)
1686 fprintf (dump_file, " No dynamic type change found.\n");
1687 return true;
1690 if (tci.known_current_type
1691 && !function_entry_reached
1692 && !tci.multiple_types_encountered)
1694 if (!tci.speculative)
1696 outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1697 offset = tci.known_current_offset;
1698 dynamic = true;
1699 maybe_in_construction = false;
1700 maybe_derived_type = false;
1701 if (dump_file)
1702 fprintf (dump_file, " Determined dynamic type.\n");
1704 else if (!speculative_outer_type
1705 || speculative_maybe_derived_type)
1707 speculative_outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1708 speculative_offset = tci.known_current_offset;
1709 speculative_maybe_derived_type = false;
1710 if (dump_file)
1711 fprintf (dump_file, " Determined speculative dynamic type.\n");
1714 else if (dump_file)
1716 fprintf (dump_file, " Found multiple types%s%s\n",
1717 function_entry_reached ? " (function entry reached)" : "",
1718 function_entry_reached ? " (multiple types encountered)" : "");
1721 return false;
1724 /* See if speculation given by SPEC_OUTER_TYPE, SPEC_OFFSET and SPEC_MAYBE_DERIVED_TYPE
1725 seems consistent (and useful) with what we already have in the non-speculative context. */
1727 bool
1728 ipa_polymorphic_call_context::speculation_consistent_p (tree spec_outer_type,
1729 HOST_WIDE_INT spec_offset,
1730 bool spec_maybe_derived_type,
1731 tree otr_type) const
1733 if (!flag_devirtualize_speculatively)
1734 return false;
1736 /* Non-polymorphic types are useless for deriving likely polymorphic
1737 call targets. */
1738 if (!spec_outer_type || !contains_polymorphic_type_p (spec_outer_type))
1739 return false;
1741 /* If we know nothing, speculation is always good. */
1742 if (!outer_type)
1743 return true;
1745 /* Speculation is only useful to avoid derived types.
1746 This is not 100% true for placement new, where the outer context may
1747 turn out to be useless, but ignore these for now. */
1748 if (!maybe_derived_type)
1749 return false;
1751 /* If types agrees, speculation is consistent, but it makes sense only
1752 when it says something new. */
1753 if (types_must_be_same_for_odr (spec_outer_type, outer_type))
1754 return maybe_derived_type && !spec_maybe_derived_type;
1756 /* If speculation does not contain the type in question, ignore it. */
1757 if (otr_type
1758 && !contains_type_p (spec_outer_type, spec_offset, otr_type, false, true))
1759 return false;
1761 /* If outer type already contains speculation as a filed,
1762 it is useless. We already know from OUTER_TYPE
1763 SPEC_TYPE and that it is not in the construction. */
1764 if (contains_type_p (outer_type, offset - spec_offset,
1765 spec_outer_type, false, false))
1766 return false;
1768 /* If speculative outer type is not more specified than outer
1769 type, just give up.
1770 We can only decide this safely if we can compare types with OUTER_TYPE.
1772 if ((!in_lto_p || odr_type_p (outer_type))
1773 && !contains_type_p (spec_outer_type,
1774 spec_offset - offset,
1775 outer_type, false))
1776 return false;
1777 return true;
1780 /* Improve THIS with speculation described by NEW_OUTER_TYPE, NEW_OFFSET,
1781 NEW_MAYBE_DERIVED_TYPE
1782 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1784 bool
1785 ipa_polymorphic_call_context::combine_speculation_with
1786 (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1787 tree otr_type)
1789 if (!new_outer_type)
1790 return false;
1792 /* restrict_to_inner_class may eliminate wrong speculation making our job
1793 easeier. */
1794 if (otr_type)
1795 restrict_to_inner_class (otr_type);
1797 if (!speculation_consistent_p (new_outer_type, new_offset,
1798 new_maybe_derived_type, otr_type))
1799 return false;
1801 /* New speculation is a win in case we have no speculation or new
1802 speculation does not consider derivations. */
1803 if (!speculative_outer_type
1804 || (speculative_maybe_derived_type
1805 && !new_maybe_derived_type))
1807 speculative_outer_type = new_outer_type;
1808 speculative_offset = new_offset;
1809 speculative_maybe_derived_type = new_maybe_derived_type;
1810 return true;
1812 else if (types_must_be_same_for_odr (speculative_outer_type,
1813 new_outer_type))
1815 if (speculative_offset != new_offset)
1817 /* OK we have two contexts that seems valid but they disagree,
1818 just give up.
1820 This is not a lattice operation, so we may want to drop it later. */
1821 if (dump_file && (dump_flags & TDF_DETAILS))
1822 fprintf (dump_file,
1823 "Speculative outer types match, "
1824 "offset mismatch -> invalid speculation\n");
1825 clear_speculation ();
1826 return true;
1828 else
1830 if (speculative_maybe_derived_type && !new_maybe_derived_type)
1832 speculative_maybe_derived_type = false;
1833 return true;
1835 else
1836 return false;
1839 /* Choose type that contains the other. This one either contains the outer
1840 as a field (thus giving exactly one target) or is deeper in the type
1841 hiearchy. */
1842 else if (speculative_outer_type
1843 && speculative_maybe_derived_type
1844 && (new_offset > speculative_offset
1845 || (new_offset == speculative_offset
1846 && contains_type_p (new_outer_type,
1847 0, speculative_outer_type, false))))
1849 tree old_outer_type = speculative_outer_type;
1850 HOST_WIDE_INT old_offset = speculative_offset;
1851 bool old_maybe_derived_type = speculative_maybe_derived_type;
1853 speculative_outer_type = new_outer_type;
1854 speculative_offset = new_offset;
1855 speculative_maybe_derived_type = new_maybe_derived_type;
1857 if (otr_type)
1858 restrict_to_inner_class (otr_type);
1860 /* If the speculation turned out to make no sense, revert to sensible
1861 one. */
1862 if (!speculative_outer_type)
1864 speculative_outer_type = old_outer_type;
1865 speculative_offset = old_offset;
1866 speculative_maybe_derived_type = old_maybe_derived_type;
1867 return false;
1869 return (old_offset != speculative_offset
1870 || old_maybe_derived_type != speculative_maybe_derived_type
1871 || types_must_be_same_for_odr (speculative_outer_type,
1872 new_outer_type));
1874 return false;
1877 /* Make speculation less specific so
1878 NEW_OUTER_TYPE, NEW_OFFSET, NEW_MAYBE_DERIVED_TYPE is also included.
1879 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1881 bool
1882 ipa_polymorphic_call_context::meet_speculation_with
1883 (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1884 tree otr_type)
1886 if (!new_outer_type && speculative_outer_type)
1888 clear_speculation ();
1889 return true;
1892 /* restrict_to_inner_class may eliminate wrong speculation making our job
1893 easeier. */
1894 if (otr_type)
1895 restrict_to_inner_class (otr_type);
1897 if (!speculative_outer_type
1898 || !speculation_consistent_p (speculative_outer_type,
1899 speculative_offset,
1900 speculative_maybe_derived_type,
1901 otr_type))
1902 return false;
1904 if (!speculation_consistent_p (new_outer_type, new_offset,
1905 new_maybe_derived_type, otr_type))
1907 clear_speculation ();
1908 return true;
1911 else if (types_must_be_same_for_odr (speculative_outer_type,
1912 new_outer_type))
1914 if (speculative_offset != new_offset)
1916 clear_speculation ();
1917 return true;
1919 else
1921 if (!speculative_maybe_derived_type && new_maybe_derived_type)
1923 speculative_maybe_derived_type = true;
1924 return true;
1926 else
1927 return false;
1930 /* See if one type contains the other as a field (not base). */
1931 else if (contains_type_p (new_outer_type, new_offset - speculative_offset,
1932 speculative_outer_type, false, false))
1933 return false;
1934 else if (contains_type_p (speculative_outer_type,
1935 speculative_offset - new_offset,
1936 new_outer_type, false, false))
1938 speculative_outer_type = new_outer_type;
1939 speculative_offset = new_offset;
1940 speculative_maybe_derived_type = new_maybe_derived_type;
1941 return true;
1943 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
1944 else if (contains_type_p (new_outer_type,
1945 new_offset - speculative_offset,
1946 speculative_outer_type, false, true))
1948 if (!speculative_maybe_derived_type)
1950 speculative_maybe_derived_type = true;
1951 return true;
1953 return false;
1955 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
1956 else if (contains_type_p (speculative_outer_type,
1957 speculative_offset - new_offset, new_outer_type, false, true))
1959 speculative_outer_type = new_outer_type;
1960 speculative_offset = new_offset;
1961 speculative_maybe_derived_type = true;
1962 return true;
1964 else
1966 if (dump_file && (dump_flags & TDF_DETAILS))
1967 fprintf (dump_file, "Giving up on speculative meet\n");
1968 clear_speculation ();
1969 return true;
1973 /* Assume that both THIS and a given context is valid and strenghten THIS
1974 if possible. Return true if any strenghtening was made.
1975 If actual type the context is being used in is known, OTR_TYPE should be
1976 set accordingly. This improves quality of combined result. */
1978 bool
1979 ipa_polymorphic_call_context::combine_with (ipa_polymorphic_call_context ctx,
1980 tree otr_type)
1982 bool updated = false;
1984 if (ctx.useless_p () || invalid)
1985 return false;
1987 /* Restricting context to inner type makes merging easier, however do not
1988 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
1989 if (otr_type && !invalid && !ctx.invalid)
1991 restrict_to_inner_class (otr_type);
1992 ctx.restrict_to_inner_class (otr_type);
1993 if(invalid)
1994 return false;
1997 if (dump_file && (dump_flags & TDF_DETAILS))
1999 fprintf (dump_file, "Polymorphic call context combine:");
2000 dump (dump_file);
2001 fprintf (dump_file, "With context: ");
2002 ctx.dump (dump_file);
2003 if (otr_type)
2005 fprintf (dump_file, "To be used with type: ");
2006 print_generic_expr (dump_file, otr_type, TDF_SLIM);
2007 fprintf (dump_file, "\n");
2011 /* If call is known to be invalid, we are done. */
2012 if (ctx.invalid)
2014 if (dump_file && (dump_flags & TDF_DETAILS))
2015 fprintf (dump_file, "-> Invalid context\n");
2016 goto invalidate;
2019 if (!ctx.outer_type)
2021 else if (!outer_type)
2023 outer_type = ctx.outer_type;
2024 offset = ctx.offset;
2025 dynamic = ctx.dynamic;
2026 maybe_in_construction = ctx.maybe_in_construction;
2027 maybe_derived_type = ctx.maybe_derived_type;
2028 updated = true;
2030 /* If types are known to be same, merging is quite easy. */
2031 else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2033 if (offset != ctx.offset
2034 && TYPE_SIZE (outer_type)
2035 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2037 if (dump_file && (dump_flags & TDF_DETAILS))
2038 fprintf (dump_file, "Outer types match, offset mismatch -> invalid\n");
2039 clear_speculation ();
2040 clear_outer_type ();
2041 invalid = true;
2042 return true;
2044 if (dump_file && (dump_flags & TDF_DETAILS))
2045 fprintf (dump_file, "Outer types match, merging flags\n");
2046 if (maybe_in_construction && !ctx.maybe_in_construction)
2048 updated = true;
2049 maybe_in_construction = false;
2051 if (maybe_derived_type && !ctx.maybe_derived_type)
2053 updated = true;
2054 maybe_derived_type = false;
2056 if (dynamic && !ctx.dynamic)
2058 updated = true;
2059 dynamic = false;
2062 /* If we know the type precisely, there is not much to improve. */
2063 else if (!maybe_derived_type && !maybe_in_construction
2064 && !ctx.maybe_derived_type && !ctx.maybe_in_construction)
2066 /* It may be easy to check if second context permits the first
2067 and set INVALID otherwise. This is not easy to do in general;
2068 contains_type_p may return false negatives for non-comparable
2069 types.
2071 If OTR_TYPE is known, we however can expect that
2072 restrict_to_inner_class should have discovered the same base
2073 type. */
2074 if (otr_type && !ctx.maybe_in_construction && !ctx.maybe_derived_type)
2076 if (dump_file && (dump_flags & TDF_DETAILS))
2077 fprintf (dump_file, "Contextes disagree -> invalid\n");
2078 goto invalidate;
2081 /* See if one type contains the other as a field (not base).
2082 In this case we want to choose the wider type, because it contains
2083 more information. */
2084 else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2085 outer_type, false, false))
2087 if (dump_file && (dump_flags & TDF_DETAILS))
2088 fprintf (dump_file, "Second type contain the first as a field\n");
2090 if (maybe_derived_type)
2092 outer_type = ctx.outer_type;
2093 maybe_derived_type = ctx.maybe_derived_type;
2094 offset = ctx.offset;
2095 dynamic = ctx.dynamic;
2096 updated = true;
2099 /* If we do not know how the context is being used, we can
2100 not clear MAYBE_IN_CONSTRUCTION because it may be offseted
2101 to other component of OUTER_TYPE later and we know nothing
2102 about it. */
2103 if (otr_type && maybe_in_construction
2104 && !ctx.maybe_in_construction)
2106 maybe_in_construction = false;
2107 updated = true;
2110 else if (contains_type_p (outer_type, offset - ctx.offset,
2111 ctx.outer_type, false, false))
2113 if (dump_file && (dump_flags & TDF_DETAILS))
2114 fprintf (dump_file, "First type contain the second as a field\n");
2116 if (otr_type && maybe_in_construction
2117 && !ctx.maybe_in_construction)
2119 maybe_in_construction = false;
2120 updated = true;
2123 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2124 else if (contains_type_p (ctx.outer_type,
2125 ctx.offset - offset, outer_type, false, true))
2127 if (dump_file && (dump_flags & TDF_DETAILS))
2128 fprintf (dump_file, "First type is base of second\n");
2129 if (!maybe_derived_type)
2131 if (!ctx.maybe_in_construction
2132 && types_odr_comparable (outer_type, ctx.outer_type))
2134 if (dump_file && (dump_flags & TDF_DETAILS))
2135 fprintf (dump_file, "Second context does not permit base -> invalid\n");
2136 goto invalidate;
2139 /* Pick variant deeper in the hiearchy. */
2140 else
2142 outer_type = ctx.outer_type;
2143 maybe_in_construction = ctx.maybe_in_construction;
2144 maybe_derived_type = ctx.maybe_derived_type;
2145 offset = ctx.offset;
2146 dynamic = ctx.dynamic;
2147 updated = true;
2150 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2151 else if (contains_type_p (outer_type,
2152 offset - ctx.offset, ctx.outer_type, false, true))
2154 if (dump_file && (dump_flags & TDF_DETAILS))
2155 fprintf (dump_file, "Second type is base of first\n");
2156 if (!ctx.maybe_derived_type)
2158 if (!maybe_in_construction
2159 && types_odr_comparable (outer_type, ctx.outer_type))
2161 if (dump_file && (dump_flags & TDF_DETAILS))
2162 fprintf (dump_file, "First context does not permit base -> invalid\n");
2163 goto invalidate;
2165 /* Pick the base type. */
2166 else if (maybe_in_construction)
2168 outer_type = ctx.outer_type;
2169 maybe_in_construction = ctx.maybe_in_construction;
2170 maybe_derived_type = ctx.maybe_derived_type;
2171 offset = ctx.offset;
2172 dynamic = ctx.dynamic;
2173 updated = true;
2177 /* TODO handle merging using hiearchy. */
2178 else if (dump_file && (dump_flags & TDF_DETAILS))
2179 fprintf (dump_file, "Giving up on merge\n");
2181 updated |= combine_speculation_with (ctx.speculative_outer_type,
2182 ctx.speculative_offset,
2183 ctx.speculative_maybe_derived_type,
2184 otr_type);
2186 if (updated && dump_file && (dump_flags & TDF_DETAILS))
2188 fprintf (dump_file, "Updated as: ");
2189 dump (dump_file);
2190 fprintf (dump_file, "\n");
2192 return updated;
2194 invalidate:
2195 invalid = true;
2196 clear_speculation ();
2197 clear_outer_type ();
2198 return true;
2201 /* Take non-speculative info, merge it with speculative and clear speculation.
2202 Used when we no longer manage to keep track of actual outer type, but we
2203 think it is still there.
2205 If OTR_TYPE is set, the transformation can be done more effectively assuming
2206 that context is going to be used only that way. */
2208 void
2209 ipa_polymorphic_call_context::make_speculative (tree otr_type)
2211 tree spec_outer_type = outer_type;
2212 HOST_WIDE_INT spec_offset = offset;
2213 bool spec_maybe_derived_type = maybe_derived_type;
2215 if (invalid)
2217 invalid = false;
2218 clear_outer_type ();
2219 clear_speculation ();
2220 return;
2222 if (!outer_type)
2223 return;
2224 clear_outer_type ();
2225 combine_speculation_with (spec_outer_type, spec_offset,
2226 spec_maybe_derived_type,
2227 otr_type);
2230 /* Use when we can not track dynamic type change. This speculatively assume
2231 type change is not happening. */
2233 void
2234 ipa_polymorphic_call_context::possible_dynamic_type_change (bool in_poly_cdtor,
2235 tree otr_type)
2237 if (dynamic)
2238 make_speculative (otr_type);
2239 else if (in_poly_cdtor)
2240 maybe_in_construction = true;
2243 /* Return TRUE if this context conveys the same information as OTHER. */
2245 bool
2246 ipa_polymorphic_call_context::equal_to
2247 (const ipa_polymorphic_call_context &x) const
2249 if (useless_p ())
2250 return x.useless_p ();
2251 if (invalid)
2252 return x.invalid;
2253 if (x.useless_p () || x.invalid)
2254 return false;
2256 if (outer_type)
2258 if (!x.outer_type
2259 || !types_odr_comparable (outer_type, x.outer_type)
2260 || !types_same_for_odr (outer_type, x.outer_type)
2261 || offset != x.offset
2262 || maybe_in_construction != x.maybe_in_construction
2263 || maybe_derived_type != x.maybe_derived_type
2264 || dynamic != x.dynamic)
2265 return false;
2267 else if (x.outer_type)
2268 return false;
2271 if (speculative_outer_type
2272 && speculation_consistent_p (speculative_outer_type, speculative_offset,
2273 speculative_maybe_derived_type, NULL_TREE))
2275 if (!x.speculative_outer_type)
2276 return false;
2278 if (!types_odr_comparable (speculative_outer_type,
2279 x.speculative_outer_type)
2280 || !types_same_for_odr (speculative_outer_type,
2281 x.speculative_outer_type)
2282 || speculative_offset != x.speculative_offset
2283 || speculative_maybe_derived_type != x.speculative_maybe_derived_type)
2284 return false;
2286 else if (x.speculative_outer_type
2287 && x.speculation_consistent_p (x.speculative_outer_type,
2288 x.speculative_offset,
2289 x.speculative_maybe_derived_type,
2290 NULL))
2291 return false;
2293 return true;
2296 /* Modify context to be strictly less restrictive than CTX. */
2298 bool
2299 ipa_polymorphic_call_context::meet_with (ipa_polymorphic_call_context ctx,
2300 tree otr_type)
2302 bool updated = false;
2304 if (useless_p () || ctx.invalid)
2305 return false;
2307 /* Restricting context to inner type makes merging easier, however do not
2308 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2309 if (otr_type && !useless_p () && !ctx.useless_p ())
2311 restrict_to_inner_class (otr_type);
2312 ctx.restrict_to_inner_class (otr_type);
2313 if(invalid)
2314 return false;
2317 if (equal_to (ctx))
2318 return false;
2320 if (ctx.useless_p () || invalid)
2322 *this = ctx;
2323 return true;
2326 if (dump_file && (dump_flags & TDF_DETAILS))
2328 fprintf (dump_file, "Polymorphic call context meet:");
2329 dump (dump_file);
2330 fprintf (dump_file, "With context: ");
2331 ctx.dump (dump_file);
2332 if (otr_type)
2334 fprintf (dump_file, "To be used with type: ");
2335 print_generic_expr (dump_file, otr_type, TDF_SLIM);
2336 fprintf (dump_file, "\n");
2340 if (!dynamic && ctx.dynamic)
2342 dynamic = true;
2343 updated = true;
2346 /* If call is known to be invalid, we are done. */
2347 if (!outer_type)
2349 else if (!ctx.outer_type)
2351 clear_outer_type ();
2352 updated = true;
2354 /* If types are known to be same, merging is quite easy. */
2355 else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2357 if (offset != ctx.offset
2358 && TYPE_SIZE (outer_type)
2359 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2361 if (dump_file && (dump_flags & TDF_DETAILS))
2362 fprintf (dump_file, "Outer types match, offset mismatch -> clearing\n");
2363 clear_outer_type ();
2364 return true;
2366 if (dump_file && (dump_flags & TDF_DETAILS))
2367 fprintf (dump_file, "Outer types match, merging flags\n");
2368 if (!maybe_in_construction && ctx.maybe_in_construction)
2370 updated = true;
2371 maybe_in_construction = true;
2373 if (!maybe_derived_type && ctx.maybe_derived_type)
2375 updated = true;
2376 maybe_derived_type = true;
2378 if (!dynamic && ctx.dynamic)
2380 updated = true;
2381 dynamic = true;
2384 /* See if one type contains the other as a field (not base). */
2385 else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2386 outer_type, false, false))
2388 if (dump_file && (dump_flags & TDF_DETAILS))
2389 fprintf (dump_file, "Second type contain the first as a field\n");
2391 /* The second type is more specified, so we keep the first.
2392 We need to set DYNAMIC flag to avoid declaring context INVALID
2393 of OFFSET ends up being out of range. */
2394 if (!dynamic
2395 && (ctx.dynamic
2396 || (!otr_type
2397 && (!TYPE_SIZE (ctx.outer_type)
2398 || !TYPE_SIZE (outer_type)
2399 || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2400 TYPE_SIZE (outer_type), 0)))))
2402 dynamic = true;
2403 updated = true;
2406 else if (contains_type_p (outer_type, offset - ctx.offset,
2407 ctx.outer_type, false, false))
2409 if (dump_file && (dump_flags & TDF_DETAILS))
2410 fprintf (dump_file, "First type contain the second as a field\n");
2412 if (!dynamic
2413 && (ctx.dynamic
2414 || (!otr_type
2415 && (!TYPE_SIZE (ctx.outer_type)
2416 || !TYPE_SIZE (outer_type)
2417 || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2418 TYPE_SIZE (outer_type), 0)))))
2419 dynamic = true;
2420 outer_type = ctx.outer_type;
2421 offset = ctx.offset;
2422 dynamic = ctx.dynamic;
2423 maybe_in_construction = ctx.maybe_in_construction;
2424 maybe_derived_type = ctx.maybe_derived_type;
2425 updated = true;
2427 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2428 else if (contains_type_p (ctx.outer_type,
2429 ctx.offset - offset, outer_type, false, true))
2431 if (dump_file && (dump_flags & TDF_DETAILS))
2432 fprintf (dump_file, "First type is base of second\n");
2433 if (!maybe_derived_type)
2435 maybe_derived_type = true;
2436 updated = true;
2438 if (!maybe_in_construction && ctx.maybe_in_construction)
2440 maybe_in_construction = true;
2441 updated = true;
2443 if (!dynamic && ctx.dynamic)
2445 dynamic = true;
2446 updated = true;
2449 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2450 else if (contains_type_p (outer_type,
2451 offset - ctx.offset, ctx.outer_type, false, true))
2453 if (dump_file && (dump_flags & TDF_DETAILS))
2454 fprintf (dump_file, "Second type is base of first\n");
2455 outer_type = ctx.outer_type;
2456 offset = ctx.offset;
2457 updated = true;
2458 if (!maybe_derived_type)
2459 maybe_derived_type = true;
2460 if (!maybe_in_construction && ctx.maybe_in_construction)
2461 maybe_in_construction = true;
2462 if (!dynamic && ctx.dynamic)
2463 dynamic = true;
2465 /* TODO handle merging using hiearchy. */
2466 else
2468 if (dump_file && (dump_flags & TDF_DETAILS))
2469 fprintf (dump_file, "Giving up on meet\n");
2470 clear_outer_type ();
2471 updated = true;
2474 updated |= meet_speculation_with (ctx.speculative_outer_type,
2475 ctx.speculative_offset,
2476 ctx.speculative_maybe_derived_type,
2477 otr_type);
2479 if (updated && dump_file && (dump_flags & TDF_DETAILS))
2481 fprintf (dump_file, "Updated as: ");
2482 dump (dump_file);
2483 fprintf (dump_file, "\n");
2485 return updated;