Add C++11 header <cuchar>.
[official-gcc.git] / gcc / ipa-polymorphic-call.c
blobfd3fb196886a65b022704d64117a03c8b2b20e99
1 /* Analysis of polymorphic call context.
2 Copyright (C) 2013-2015 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "rtl.h"
28 #include "alias.h"
29 #include "fold-const.h"
30 #include "print-tree.h"
31 #include "calls.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "expmed.h"
35 #include "dojump.h"
36 #include "explow.h"
37 #include "emit-rtl.h"
38 #include "varasm.h"
39 #include "stmt.h"
40 #include "expr.h"
41 #include "tree-pass.h"
42 #include "target.h"
43 #include "cgraph.h"
44 #include "ipa-utils.h"
45 #include "internal-fn.h"
46 #include "gimple-fold.h"
47 #include "alloc-pool.h"
48 #include "symbol-summary.h"
49 #include "ipa-prop.h"
50 #include "ipa-inline.h"
51 #include "diagnostic.h"
52 #include "tree-dfa.h"
53 #include "demangle.h"
54 #include "dbgcnt.h"
55 #include "gimple-pretty-print.h"
56 #include "stor-layout.h"
57 #include "intl.h"
58 #include "data-streamer.h"
59 #include "streamer-hooks.h"
60 #include "tree-ssa-operands.h"
61 #include "tree-into-ssa.h"
63 /* Return true when TYPE contains an polymorphic type and thus is interesting
64 for devirtualization machinery. */
66 static bool contains_type_p (tree, HOST_WIDE_INT, tree,
67 bool consider_placement_new = true,
68 bool consider_bases = true);
70 bool
71 contains_polymorphic_type_p (const_tree type)
73 type = TYPE_MAIN_VARIANT (type);
75 if (RECORD_OR_UNION_TYPE_P (type))
77 if (TYPE_BINFO (type)
78 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
79 return true;
80 for (tree fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
81 if (TREE_CODE (fld) == FIELD_DECL
82 && !DECL_ARTIFICIAL (fld)
83 && contains_polymorphic_type_p (TREE_TYPE (fld)))
84 return true;
85 return false;
87 if (TREE_CODE (type) == ARRAY_TYPE)
88 return contains_polymorphic_type_p (TREE_TYPE (type));
89 return false;
92 /* Return true if it seems valid to use placement new to build EXPECTED_TYPE
93 at possition CUR_OFFSET within TYPE.
95 POD can be changed to an instance of a polymorphic type by
96 placement new. Here we play safe and assume that any
97 non-polymorphic type is POD. */
98 bool
99 possible_placement_new (tree type, tree expected_type,
100 HOST_WIDE_INT cur_offset)
102 return ((TREE_CODE (type) != RECORD_TYPE
103 || !TYPE_BINFO (type)
104 || cur_offset >= POINTER_SIZE
105 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))
106 && (!TYPE_SIZE (type)
107 || !tree_fits_shwi_p (TYPE_SIZE (type))
108 || (cur_offset
109 + (expected_type ? tree_to_uhwi (TYPE_SIZE (expected_type))
110 : POINTER_SIZE)
111 <= tree_to_uhwi (TYPE_SIZE (type)))));
114 /* THIS->OUTER_TYPE is a type of memory object where object of OTR_TYPE
115 is contained at THIS->OFFSET. Walk the memory representation of
116 THIS->OUTER_TYPE and find the outermost class type that match
117 OTR_TYPE or contain OTR_TYPE as a base. Update THIS
118 to represent it.
120 If OTR_TYPE is NULL, just find outermost polymorphic type with
121 virtual table present at possition OFFSET.
123 For example when THIS represents type
124 class A
126 int a;
127 class B b;
129 and we look for type at offset sizeof(int), we end up with B and offset 0.
130 If the same is produced by multiple inheritance, we end up with A and offset
131 sizeof(int).
133 If we can not find corresponding class, give up by setting
134 THIS->OUTER_TYPE to OTR_TYPE and THIS->OFFSET to NULL.
135 Return true when lookup was sucesful.
137 When CONSIDER_PLACEMENT_NEW is false, reject contexts that may be made
138 valid only via alocation of new polymorphic type inside by means
139 of placement new.
141 When CONSIDER_BASES is false, only look for actual fields, not base types
142 of TYPE. */
144 bool
145 ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type,
146 bool consider_placement_new,
147 bool consider_bases)
149 tree type = outer_type;
150 HOST_WIDE_INT cur_offset = offset;
151 bool speculative = false;
152 bool size_unknown = false;
153 unsigned HOST_WIDE_INT otr_type_size = POINTER_SIZE;
155 /* Update OUTER_TYPE to match EXPECTED_TYPE if it is not set. */
156 if (!outer_type)
158 clear_outer_type (otr_type);
159 type = otr_type;
160 cur_offset = 0;
162 /* See if OFFSET points inside OUTER_TYPE. If it does not, we know
163 that the context is either invalid, or the instance type must be
164 derived from OUTER_TYPE.
166 Because the instance type may contain field whose type is of OUTER_TYPE,
167 we can not derive any effective information about it.
169 TODO: In the case we know all derrived types, we can definitely do better
170 here. */
171 else if (TYPE_SIZE (outer_type)
172 && tree_fits_shwi_p (TYPE_SIZE (outer_type))
173 && tree_to_shwi (TYPE_SIZE (outer_type)) >= 0
174 && tree_to_shwi (TYPE_SIZE (outer_type)) <= offset)
176 clear_outer_type (otr_type);
177 type = otr_type;
178 cur_offset = 0;
180 /* If derived type is not allowed, we know that the context is invalid.
181 For dynamic types, we really do not have information about
182 size of the memory location. It is possible that completely
183 different type is stored after outer_type. */
184 if (!maybe_derived_type && !dynamic)
186 clear_speculation ();
187 invalid = true;
188 return false;
192 if (otr_type && TYPE_SIZE (otr_type)
193 && tree_fits_shwi_p (TYPE_SIZE (otr_type)))
194 otr_type_size = tree_to_uhwi (TYPE_SIZE (otr_type));
196 if (!type || offset < 0)
197 goto no_useful_type_info;
199 /* Find the sub-object the constant actually refers to and mark whether it is
200 an artificial one (as opposed to a user-defined one).
202 This loop is performed twice; first time for outer_type and second time
203 for speculative_outer_type. The second run has SPECULATIVE set. */
204 while (true)
206 unsigned HOST_WIDE_INT pos, size;
207 tree fld;
209 /* If we do not know size of TYPE, we need to be more conservative
210 about accepting cases where we can not find EXPECTED_TYPE.
211 Generally the types that do matter here are of constant size.
212 Size_unknown case should be very rare. */
213 if (TYPE_SIZE (type)
214 && tree_fits_shwi_p (TYPE_SIZE (type))
215 && tree_to_shwi (TYPE_SIZE (type)) >= 0)
216 size_unknown = false;
217 else
218 size_unknown = true;
220 /* On a match, just return what we found. */
221 if ((otr_type
222 && types_odr_comparable (type, otr_type)
223 && types_same_for_odr (type, otr_type))
224 || (!otr_type
225 && TREE_CODE (type) == RECORD_TYPE
226 && TYPE_BINFO (type)
227 && polymorphic_type_binfo_p (TYPE_BINFO (type))))
229 if (speculative)
231 /* If we did not match the offset, just give up on speculation. */
232 if (cur_offset != 0
233 /* Also check if speculation did not end up being same as
234 non-speculation. */
235 || (types_must_be_same_for_odr (speculative_outer_type,
236 outer_type)
237 && (maybe_derived_type
238 == speculative_maybe_derived_type)))
239 clear_speculation ();
240 return true;
242 else
244 /* If type is known to be final, do not worry about derived
245 types. Testing it here may help us to avoid speculation. */
246 if (otr_type && TREE_CODE (outer_type) == RECORD_TYPE
247 && (!in_lto_p || odr_type_p (outer_type))
248 && type_with_linkage_p (outer_type)
249 && type_known_to_have_no_derivations_p (outer_type))
250 maybe_derived_type = false;
252 /* Type can not contain itself on an non-zero offset. In that case
253 just give up. Still accept the case where size is now known.
254 Either the second copy may appear past the end of type or within
255 the non-POD buffer located inside the variably sized type
256 itself. */
257 if (cur_offset != 0)
258 goto no_useful_type_info;
259 /* If we determined type precisely or we have no clue on
260 speuclation, we are done. */
261 if (!maybe_derived_type || !speculative_outer_type
262 || !speculation_consistent_p (speculative_outer_type,
263 speculative_offset,
264 speculative_maybe_derived_type,
265 otr_type))
267 clear_speculation ();
268 return true;
270 /* Otherwise look into speculation now. */
271 else
273 speculative = true;
274 type = speculative_outer_type;
275 cur_offset = speculative_offset;
276 continue;
281 /* Walk fields and find corresponding on at OFFSET. */
282 if (TREE_CODE (type) == RECORD_TYPE)
284 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
286 if (TREE_CODE (fld) != FIELD_DECL)
287 continue;
289 pos = int_bit_position (fld);
290 if (pos > (unsigned HOST_WIDE_INT)cur_offset)
291 continue;
293 /* Do not consider vptr itself. Not even for placement new. */
294 if (!pos && DECL_ARTIFICIAL (fld)
295 && POINTER_TYPE_P (TREE_TYPE (fld))
296 && TYPE_BINFO (type)
297 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
298 continue;
300 if (!DECL_SIZE (fld) || !tree_fits_uhwi_p (DECL_SIZE (fld)))
301 goto no_useful_type_info;
302 size = tree_to_uhwi (DECL_SIZE (fld));
304 /* We can always skip types smaller than pointer size:
305 those can not contain a virtual table pointer.
307 Disqualifying fields that are too small to fit OTR_TYPE
308 saves work needed to walk them for no benefit.
309 Because of the way the bases are packed into a class, the
310 field's size may be smaller than type size, so it needs
311 to be done with a care. */
313 if (pos <= (unsigned HOST_WIDE_INT)cur_offset
314 && (pos + size) >= (unsigned HOST_WIDE_INT)cur_offset
315 + POINTER_SIZE
316 && (!otr_type
317 || !TYPE_SIZE (TREE_TYPE (fld))
318 || !tree_fits_shwi_p (TYPE_SIZE (TREE_TYPE (fld)))
319 || (pos + tree_to_uhwi (TYPE_SIZE (TREE_TYPE (fld))))
320 >= cur_offset + otr_type_size))
321 break;
324 if (!fld)
325 goto no_useful_type_info;
327 type = TYPE_MAIN_VARIANT (TREE_TYPE (fld));
328 cur_offset -= pos;
329 /* DECL_ARTIFICIAL represents a basetype. */
330 if (!DECL_ARTIFICIAL (fld))
332 if (!speculative)
334 outer_type = type;
335 offset = cur_offset;
336 /* As soon as we se an field containing the type,
337 we know we are not looking for derivations. */
338 maybe_derived_type = false;
340 else
342 speculative_outer_type = type;
343 speculative_offset = cur_offset;
344 speculative_maybe_derived_type = false;
347 else if (!consider_bases)
348 goto no_useful_type_info;
350 else if (TREE_CODE (type) == ARRAY_TYPE)
352 tree subtype = TYPE_MAIN_VARIANT (TREE_TYPE (type));
354 /* Give up if we don't know array field size.
355 Also give up on non-polymorphic types as they are used
356 as buffers for placement new. */
357 if (!TYPE_SIZE (subtype)
358 || !tree_fits_shwi_p (TYPE_SIZE (subtype))
359 || tree_to_shwi (TYPE_SIZE (subtype)) <= 0
360 || !contains_polymorphic_type_p (subtype))
361 goto no_useful_type_info;
363 HOST_WIDE_INT new_offset = cur_offset % tree_to_shwi (TYPE_SIZE (subtype));
365 /* We may see buffer for placement new. In this case the expected type
366 can be bigger than the subtype. */
367 if (TYPE_SIZE (subtype)
368 && (cur_offset + otr_type_size
369 > tree_to_uhwi (TYPE_SIZE (subtype))))
370 goto no_useful_type_info;
372 cur_offset = new_offset;
373 type = TYPE_MAIN_VARIANT (subtype);
374 if (!speculative)
376 outer_type = type;
377 offset = cur_offset;
378 maybe_derived_type = false;
380 else
382 speculative_outer_type = type;
383 speculative_offset = cur_offset;
384 speculative_maybe_derived_type = false;
387 /* Give up on anything else. */
388 else
390 no_useful_type_info:
391 if (maybe_derived_type && !speculative
392 && TREE_CODE (outer_type) == RECORD_TYPE
393 && TREE_CODE (otr_type) == RECORD_TYPE
394 && TYPE_BINFO (otr_type)
395 && !offset
396 && get_binfo_at_offset (TYPE_BINFO (otr_type), 0, outer_type))
398 clear_outer_type (otr_type);
399 if (!speculative_outer_type
400 || !speculation_consistent_p (speculative_outer_type,
401 speculative_offset,
402 speculative_maybe_derived_type,
403 otr_type))
404 clear_speculation ();
405 if (speculative_outer_type)
407 speculative = true;
408 type = speculative_outer_type;
409 cur_offset = speculative_offset;
411 else
412 return true;
414 /* We found no way to embedd EXPECTED_TYPE in TYPE.
415 We still permit two special cases - placement new and
416 the case of variadic types containing themselves. */
417 if (!speculative
418 && consider_placement_new
419 && (size_unknown || !type || maybe_derived_type
420 || possible_placement_new (type, otr_type, cur_offset)))
422 /* In these weird cases we want to accept the context.
423 In non-speculative run we have no useful outer_type info
424 (TODO: we may eventually want to record upper bound on the
425 type size that can be used to prune the walk),
426 but we still want to consider speculation that may
427 give useful info. */
428 if (!speculative)
430 clear_outer_type (otr_type);
431 if (!speculative_outer_type
432 || !speculation_consistent_p (speculative_outer_type,
433 speculative_offset,
434 speculative_maybe_derived_type,
435 otr_type))
436 clear_speculation ();
437 if (speculative_outer_type)
439 speculative = true;
440 type = speculative_outer_type;
441 cur_offset = speculative_offset;
443 else
444 return true;
446 else
447 clear_speculation ();
448 return true;
450 else
452 clear_speculation ();
453 if (speculative)
454 return true;
455 clear_outer_type (otr_type);
456 invalid = true;
457 return false;
463 /* Return true if OUTER_TYPE contains OTR_TYPE at OFFSET.
464 CONSIDER_PLACEMENT_NEW makes function to accept cases where OTR_TYPE can
465 be built within OUTER_TYPE by means of placement new. CONSIDER_BASES makes
466 function to accept cases where OTR_TYPE appears as base of OUTER_TYPE or as
467 base of one of fields of OUTER_TYPE. */
469 static bool
470 contains_type_p (tree outer_type, HOST_WIDE_INT offset,
471 tree otr_type,
472 bool consider_placement_new,
473 bool consider_bases)
475 ipa_polymorphic_call_context context;
477 /* Check that type is within range. */
478 if (offset < 0)
479 return false;
480 if (TYPE_SIZE (outer_type) && TYPE_SIZE (otr_type)
481 && TREE_CODE (outer_type) == INTEGER_CST
482 && TREE_CODE (otr_type) == INTEGER_CST
483 && wi::ltu_p (wi::to_offset (outer_type), (wi::to_offset (otr_type) + offset)))
484 return false;
486 context.offset = offset;
487 context.outer_type = TYPE_MAIN_VARIANT (outer_type);
488 context.maybe_derived_type = false;
489 return context.restrict_to_inner_class (otr_type, consider_placement_new, consider_bases);
493 /* Return a FUNCTION_DECL if BLOCK represents a constructor or destructor.
494 If CHECK_CLONES is true, also check for clones of ctor/dtors. */
496 tree
497 inlined_polymorphic_ctor_dtor_block_p (tree block, bool check_clones)
499 tree fn = BLOCK_ABSTRACT_ORIGIN (block);
500 if (fn == NULL || TREE_CODE (fn) != FUNCTION_DECL)
501 return NULL_TREE;
503 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
504 || (!DECL_CXX_CONSTRUCTOR_P (fn) && !DECL_CXX_DESTRUCTOR_P (fn)))
506 if (!check_clones)
507 return NULL_TREE;
509 /* Watch for clones where we constant propagated the first
510 argument (pointer to the instance). */
511 fn = DECL_ABSTRACT_ORIGIN (fn);
512 if (!fn
513 || TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
514 || (!DECL_CXX_CONSTRUCTOR_P (fn) && !DECL_CXX_DESTRUCTOR_P (fn)))
515 return NULL_TREE;
518 if (flags_from_decl_or_type (fn) & (ECF_PURE | ECF_CONST))
519 return NULL_TREE;
521 return fn;
525 /* We know that the instance is stored in variable or parameter
526 (not dynamically allocated) and we want to disprove the fact
527 that it may be in construction at invocation of CALL.
529 BASE represents memory location where instance is stored.
530 If BASE is NULL, it is assumed to be global memory.
531 OUTER_TYPE is known type of the instance or NULL if not
532 known.
534 For the variable to be in construction we actually need to
535 be in constructor of corresponding global variable or
536 the inline stack of CALL must contain the constructor.
537 Check this condition. This check works safely only before
538 IPA passes, because inline stacks may become out of date
539 later. */
541 bool
542 decl_maybe_in_construction_p (tree base, tree outer_type,
543 gimple call, tree function)
545 if (outer_type)
546 outer_type = TYPE_MAIN_VARIANT (outer_type);
547 gcc_assert (!base || DECL_P (base));
549 /* After inlining the code unification optimizations may invalidate
550 inline stacks. Also we need to give up on global variables after
551 IPA, because addresses of these may have been propagated to their
552 constructors. */
553 if (DECL_STRUCT_FUNCTION (function)->after_inlining)
554 return true;
556 /* Pure functions can not do any changes on the dynamic type;
557 that require writting to memory. */
558 if ((!base || !auto_var_in_fn_p (base, function))
559 && flags_from_decl_or_type (function) & (ECF_PURE | ECF_CONST))
560 return false;
562 bool check_clones = !base || is_global_var (base);
563 for (tree block = gimple_block (call); block && TREE_CODE (block) == BLOCK;
564 block = BLOCK_SUPERCONTEXT (block))
565 if (tree fn = inlined_polymorphic_ctor_dtor_block_p (block, check_clones))
567 tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
569 if (!outer_type || !types_odr_comparable (type, outer_type))
571 if (TREE_CODE (type) == RECORD_TYPE
572 && TYPE_BINFO (type)
573 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
574 return true;
576 else if (types_same_for_odr (type, outer_type))
577 return true;
580 if (!base || (TREE_CODE (base) == VAR_DECL && is_global_var (base)))
582 if (TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
583 || (!DECL_CXX_CONSTRUCTOR_P (function)
584 && !DECL_CXX_DESTRUCTOR_P (function)))
586 if (!DECL_ABSTRACT_ORIGIN (function))
587 return false;
588 /* Watch for clones where we constant propagated the first
589 argument (pointer to the instance). */
590 function = DECL_ABSTRACT_ORIGIN (function);
591 if (!function
592 || TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
593 || (!DECL_CXX_CONSTRUCTOR_P (function)
594 && !DECL_CXX_DESTRUCTOR_P (function)))
595 return false;
597 tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (function));
598 if (!outer_type || !types_odr_comparable (type, outer_type))
600 if (TREE_CODE (type) == RECORD_TYPE
601 && TYPE_BINFO (type)
602 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
603 return true;
605 else if (types_same_for_odr (type, outer_type))
606 return true;
608 return false;
611 /* Dump human readable context to F. If NEWLINE is true, it will be terminated
612 by a newline. */
614 void
615 ipa_polymorphic_call_context::dump (FILE *f, bool newline) const
617 fprintf (f, " ");
618 if (invalid)
619 fprintf (f, "Call is known to be undefined");
620 else
622 if (useless_p ())
623 fprintf (f, "nothing known");
624 if (outer_type || offset)
626 fprintf (f, "Outer type%s:", dynamic ? " (dynamic)":"");
627 print_generic_expr (f, outer_type, TDF_SLIM);
628 if (maybe_derived_type)
629 fprintf (f, " (or a derived type)");
630 if (maybe_in_construction)
631 fprintf (f, " (maybe in construction)");
632 fprintf (f, " offset " HOST_WIDE_INT_PRINT_DEC,
633 offset);
635 if (speculative_outer_type)
637 if (outer_type || offset)
638 fprintf (f, " ");
639 fprintf (f, "Speculative outer type:");
640 print_generic_expr (f, speculative_outer_type, TDF_SLIM);
641 if (speculative_maybe_derived_type)
642 fprintf (f, " (or a derived type)");
643 fprintf (f, " at offset " HOST_WIDE_INT_PRINT_DEC,
644 speculative_offset);
647 if (newline)
648 fprintf(f, "\n");
651 /* Print context to stderr. */
653 void
654 ipa_polymorphic_call_context::debug () const
656 dump (stderr);
659 /* Stream out the context to OB. */
661 void
662 ipa_polymorphic_call_context::stream_out (struct output_block *ob) const
664 struct bitpack_d bp = bitpack_create (ob->main_stream);
666 bp_pack_value (&bp, invalid, 1);
667 bp_pack_value (&bp, maybe_in_construction, 1);
668 bp_pack_value (&bp, maybe_derived_type, 1);
669 bp_pack_value (&bp, speculative_maybe_derived_type, 1);
670 bp_pack_value (&bp, dynamic, 1);
671 bp_pack_value (&bp, outer_type != NULL, 1);
672 bp_pack_value (&bp, offset != 0, 1);
673 bp_pack_value (&bp, speculative_outer_type != NULL, 1);
674 streamer_write_bitpack (&bp);
676 if (outer_type != NULL)
677 stream_write_tree (ob, outer_type, true);
678 if (offset)
679 streamer_write_hwi (ob, offset);
680 if (speculative_outer_type != NULL)
682 stream_write_tree (ob, speculative_outer_type, true);
683 streamer_write_hwi (ob, speculative_offset);
685 else
686 gcc_assert (!speculative_offset);
689 /* Stream in the context from IB and DATA_IN. */
691 void
692 ipa_polymorphic_call_context::stream_in (struct lto_input_block *ib,
693 struct data_in *data_in)
695 struct bitpack_d bp = streamer_read_bitpack (ib);
697 invalid = bp_unpack_value (&bp, 1);
698 maybe_in_construction = bp_unpack_value (&bp, 1);
699 maybe_derived_type = bp_unpack_value (&bp, 1);
700 speculative_maybe_derived_type = bp_unpack_value (&bp, 1);
701 dynamic = bp_unpack_value (&bp, 1);
702 bool outer_type_p = bp_unpack_value (&bp, 1);
703 bool offset_p = bp_unpack_value (&bp, 1);
704 bool speculative_outer_type_p = bp_unpack_value (&bp, 1);
706 if (outer_type_p)
707 outer_type = stream_read_tree (ib, data_in);
708 else
709 outer_type = NULL;
710 if (offset_p)
711 offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
712 else
713 offset = 0;
714 if (speculative_outer_type_p)
716 speculative_outer_type = stream_read_tree (ib, data_in);
717 speculative_offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
719 else
721 speculative_outer_type = NULL;
722 speculative_offset = 0;
726 /* Proudce polymorphic call context for call method of instance
727 that is located within BASE (that is assumed to be a decl) at offset OFF. */
729 void
730 ipa_polymorphic_call_context::set_by_decl (tree base, HOST_WIDE_INT off)
732 gcc_assert (DECL_P (base));
733 clear_speculation ();
735 if (!contains_polymorphic_type_p (TREE_TYPE (base)))
737 clear_outer_type ();
738 offset = off;
739 return;
741 outer_type = TYPE_MAIN_VARIANT (TREE_TYPE (base));
742 offset = off;
743 /* Make very conservative assumption that all objects
744 may be in construction.
746 It is up to caller to revisit this via
747 get_dynamic_type or decl_maybe_in_construction_p. */
748 maybe_in_construction = true;
749 maybe_derived_type = false;
750 dynamic = false;
753 /* CST is an invariant (address of decl), try to get meaningful
754 polymorphic call context for polymorphic call of method
755 if instance of OTR_TYPE that is located at offset OFF of this invariant.
756 Return FALSE if nothing meaningful can be found. */
758 bool
759 ipa_polymorphic_call_context::set_by_invariant (tree cst,
760 tree otr_type,
761 HOST_WIDE_INT off)
763 HOST_WIDE_INT offset2, size, max_size;
764 tree base;
766 invalid = false;
767 off = 0;
768 clear_outer_type (otr_type);
770 if (TREE_CODE (cst) != ADDR_EXPR)
771 return false;
773 cst = TREE_OPERAND (cst, 0);
774 base = get_ref_base_and_extent (cst, &offset2, &size, &max_size);
775 if (!DECL_P (base) || max_size == -1 || max_size != size)
776 return false;
778 /* Only type inconsistent programs can have otr_type that is
779 not part of outer type. */
780 if (otr_type && !contains_type_p (TREE_TYPE (base), off, otr_type))
781 return false;
783 set_by_decl (base, off);
784 return true;
787 /* See if OP is SSA name initialized as a copy or by single assignment.
788 If so, walk the SSA graph up. Because simple PHI conditional is considered
789 copy, GLOBAL_VISITED may be used to avoid infinite loop walking the SSA
790 graph. */
792 static tree
793 walk_ssa_copies (tree op, hash_set<tree> **global_visited = NULL)
795 hash_set <tree> *visited = NULL;
796 STRIP_NOPS (op);
797 while (TREE_CODE (op) == SSA_NAME
798 && !SSA_NAME_IS_DEFAULT_DEF (op)
799 /* We might be called via fold_stmt during cfgcleanup where
800 SSA form need not be up-to-date. */
801 && !name_registered_for_update_p (op)
802 && (gimple_assign_single_p (SSA_NAME_DEF_STMT (op))
803 || gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI))
805 if (global_visited)
807 if (!*global_visited)
808 *global_visited = new hash_set<tree>;
809 if ((*global_visited)->add (op))
810 goto done;
812 else
814 if (!visited)
815 visited = new hash_set<tree>;
816 if (visited->add (op))
817 goto done;
819 /* Special case
820 if (ptr == 0)
821 ptr = 0;
822 else
823 ptr = ptr.foo;
824 This pattern is implicitly produced for casts to non-primary
825 bases. When doing context analysis, we do not really care
826 about the case pointer is NULL, becuase the call will be
827 undefined anyway. */
828 if (gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI)
830 gimple phi = SSA_NAME_DEF_STMT (op);
832 if (gimple_phi_num_args (phi) > 2)
833 goto done;
834 if (gimple_phi_num_args (phi) == 1)
835 op = gimple_phi_arg_def (phi, 0);
836 else if (integer_zerop (gimple_phi_arg_def (phi, 0)))
837 op = gimple_phi_arg_def (phi, 1);
838 else if (integer_zerop (gimple_phi_arg_def (phi, 1)))
839 op = gimple_phi_arg_def (phi, 0);
840 else
841 goto done;
843 else
845 if (gimple_assign_load_p (SSA_NAME_DEF_STMT (op)))
846 goto done;
847 op = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op));
849 STRIP_NOPS (op);
851 done:
852 if (visited)
853 delete (visited);
854 return op;
857 /* Create polymorphic call context from IP invariant CST.
858 This is typically &global_var.
859 OTR_TYPE specify type of polymorphic call or NULL if unknown, OFF
860 is offset of call. */
862 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree cst,
863 tree otr_type,
864 HOST_WIDE_INT off)
866 clear_speculation ();
867 set_by_invariant (cst, otr_type, off);
870 /* Build context for pointer REF contained in FNDECL at statement STMT.
871 if INSTANCE is non-NULL, return pointer to the object described by
872 the context or DECL where context is contained in. */
874 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl,
875 tree ref,
876 gimple stmt,
877 tree *instance)
879 tree otr_type = NULL;
880 tree base_pointer;
881 hash_set <tree> *visited = NULL;
883 if (TREE_CODE (ref) == OBJ_TYPE_REF)
885 otr_type = obj_type_ref_class (ref);
886 base_pointer = OBJ_TYPE_REF_OBJECT (ref);
888 else
889 base_pointer = ref;
891 /* Set up basic info in case we find nothing interesting in the analysis. */
892 clear_speculation ();
893 clear_outer_type (otr_type);
894 invalid = false;
896 /* Walk SSA for outer object. */
897 while (true)
899 base_pointer = walk_ssa_copies (base_pointer, &visited);
900 if (TREE_CODE (base_pointer) == ADDR_EXPR)
902 HOST_WIDE_INT size, max_size;
903 HOST_WIDE_INT offset2;
904 tree base = get_ref_base_and_extent (TREE_OPERAND (base_pointer, 0),
905 &offset2, &size, &max_size);
907 if (max_size != -1 && max_size == size)
908 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base)),
909 offset + offset2,
910 true,
911 NULL /* Do not change outer type. */);
913 /* If this is a varying address, punt. */
914 if ((TREE_CODE (base) == MEM_REF || DECL_P (base))
915 && max_size != -1
916 && max_size == size)
918 /* We found dereference of a pointer. Type of the pointer
919 and MEM_REF is meaningless, but we can look futher. */
920 if (TREE_CODE (base) == MEM_REF)
922 base_pointer = TREE_OPERAND (base, 0);
923 offset
924 += offset2 + mem_ref_offset (base).to_short_addr () * BITS_PER_UNIT;
925 outer_type = NULL;
927 /* We found base object. In this case the outer_type
928 is known. */
929 else if (DECL_P (base))
931 if (visited)
932 delete (visited);
933 /* Only type inconsistent programs can have otr_type that is
934 not part of outer type. */
935 if (otr_type
936 && !contains_type_p (TREE_TYPE (base),
937 offset + offset2, otr_type))
939 invalid = true;
940 if (instance)
941 *instance = base_pointer;
942 return;
944 set_by_decl (base, offset + offset2);
945 if (outer_type && maybe_in_construction && stmt)
946 maybe_in_construction
947 = decl_maybe_in_construction_p (base,
948 outer_type,
949 stmt,
950 fndecl);
951 if (instance)
952 *instance = base;
953 return;
955 else
956 break;
958 else
959 break;
961 else if (TREE_CODE (base_pointer) == POINTER_PLUS_EXPR
962 && tree_fits_uhwi_p (TREE_OPERAND (base_pointer, 1)))
964 offset += tree_to_shwi (TREE_OPERAND (base_pointer, 1))
965 * BITS_PER_UNIT;
966 base_pointer = TREE_OPERAND (base_pointer, 0);
968 else
969 break;
972 if (visited)
973 delete (visited);
975 /* Try to determine type of the outer object. */
976 if (TREE_CODE (base_pointer) == SSA_NAME
977 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
978 && TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL)
980 /* See if parameter is THIS pointer of a method. */
981 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE
982 && SSA_NAME_VAR (base_pointer) == DECL_ARGUMENTS (fndecl))
984 outer_type
985 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
986 gcc_assert (TREE_CODE (outer_type) == RECORD_TYPE
987 || TREE_CODE (outer_type) == UNION_TYPE);
989 /* Dynamic casting has possibly upcasted the type
990 in the hiearchy. In this case outer type is less
991 informative than inner type and we should forget
992 about it. */
993 if ((otr_type
994 && !contains_type_p (outer_type, offset,
995 otr_type))
996 || !contains_polymorphic_type_p (outer_type))
998 outer_type = NULL;
999 if (instance)
1000 *instance = base_pointer;
1001 return;
1004 dynamic = true;
1006 /* If the function is constructor or destructor, then
1007 the type is possibly in construction, but we know
1008 it is not derived type. */
1009 if (DECL_CXX_CONSTRUCTOR_P (fndecl)
1010 || DECL_CXX_DESTRUCTOR_P (fndecl))
1012 maybe_in_construction = true;
1013 maybe_derived_type = false;
1015 else
1017 maybe_derived_type = true;
1018 maybe_in_construction = false;
1020 if (instance)
1021 *instance = base_pointer;
1022 return;
1024 /* Non-PODs passed by value are really passed by invisible
1025 reference. In this case we also know the type of the
1026 object. */
1027 if (DECL_BY_REFERENCE (SSA_NAME_VAR (base_pointer)))
1029 outer_type
1030 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
1031 /* Only type inconsistent programs can have otr_type that is
1032 not part of outer type. */
1033 if (otr_type && !contains_type_p (outer_type, offset,
1034 otr_type))
1036 invalid = true;
1037 if (instance)
1038 *instance = base_pointer;
1039 return;
1041 /* Non-polymorphic types have no interest for us. */
1042 else if (!otr_type && !contains_polymorphic_type_p (outer_type))
1044 outer_type = NULL;
1045 if (instance)
1046 *instance = base_pointer;
1047 return;
1049 maybe_derived_type = false;
1050 maybe_in_construction = false;
1051 if (instance)
1052 *instance = base_pointer;
1053 return;
1057 tree base_type = TREE_TYPE (base_pointer);
1059 if (TREE_CODE (base_pointer) == SSA_NAME
1060 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
1061 && !(TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL
1062 || TREE_CODE (SSA_NAME_VAR (base_pointer)) == RESULT_DECL))
1064 invalid = true;
1065 if (instance)
1066 *instance = base_pointer;
1067 return;
1069 if (TREE_CODE (base_pointer) == SSA_NAME
1070 && SSA_NAME_DEF_STMT (base_pointer)
1071 && gimple_assign_single_p (SSA_NAME_DEF_STMT (base_pointer)))
1072 base_type = TREE_TYPE (gimple_assign_rhs1
1073 (SSA_NAME_DEF_STMT (base_pointer)));
1075 if (base_type && POINTER_TYPE_P (base_type))
1076 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base_type)),
1077 offset,
1078 true, NULL /* Do not change type here */);
1079 /* TODO: There are multiple ways to derive a type. For instance
1080 if BASE_POINTER is passed to an constructor call prior our refernece.
1081 We do not make this type of flow sensitive analysis yet. */
1082 if (instance)
1083 *instance = base_pointer;
1084 return;
1087 /* Structure to be passed in between detect_type_change and
1088 check_stmt_for_type_change. */
1090 struct type_change_info
1092 /* Offset into the object where there is the virtual method pointer we are
1093 looking for. */
1094 HOST_WIDE_INT offset;
1095 /* The declaration or SSA_NAME pointer of the base that we are checking for
1096 type change. */
1097 tree instance;
1098 /* The reference to virtual table pointer used. */
1099 tree vtbl_ptr_ref;
1100 tree otr_type;
1101 /* If we actually can tell the type that the object has changed to, it is
1102 stored in this field. Otherwise it remains NULL_TREE. */
1103 tree known_current_type;
1104 HOST_WIDE_INT known_current_offset;
1106 /* Set to true if dynamic type change has been detected. */
1107 bool type_maybe_changed;
1108 /* Set to true if multiple types have been encountered. known_current_type
1109 must be disregarded in that case. */
1110 bool multiple_types_encountered;
1111 /* Set to true if we possibly missed some dynamic type changes and we should
1112 consider the set to be speculative. */
1113 bool speculative;
1114 bool seen_unanalyzed_store;
1117 /* Return true if STMT is not call and can modify a virtual method table pointer.
1118 We take advantage of fact that vtable stores must appear within constructor
1119 and destructor functions. */
1121 static bool
1122 noncall_stmt_may_be_vtbl_ptr_store (gimple stmt)
1124 if (is_gimple_assign (stmt))
1126 tree lhs = gimple_assign_lhs (stmt);
1128 if (gimple_clobber_p (stmt))
1129 return false;
1130 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
1132 if (flag_strict_aliasing
1133 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
1134 return false;
1136 if (TREE_CODE (lhs) == COMPONENT_REF
1137 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1138 return false;
1139 /* In the future we might want to use get_base_ref_and_offset to find
1140 if there is a field corresponding to the offset and if so, proceed
1141 almost like if it was a component ref. */
1145 /* Code unification may mess with inline stacks. */
1146 if (cfun->after_inlining)
1147 return true;
1149 /* Walk the inline stack and watch out for ctors/dtors.
1150 TODO: Maybe we can require the store to appear in toplevel
1151 block of CTOR/DTOR. */
1152 for (tree block = gimple_block (stmt); block && TREE_CODE (block) == BLOCK;
1153 block = BLOCK_SUPERCONTEXT (block))
1154 if (BLOCK_ABSTRACT_ORIGIN (block)
1155 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block)) == FUNCTION_DECL)
1156 return inlined_polymorphic_ctor_dtor_block_p (block, false);
1157 return (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
1158 && (DECL_CXX_CONSTRUCTOR_P (current_function_decl)
1159 || DECL_CXX_DESTRUCTOR_P (current_function_decl)));
1162 /* If STMT can be proved to be an assignment to the virtual method table
1163 pointer of ANALYZED_OBJ and the type associated with the new table
1164 identified, return the type. Otherwise return NULL_TREE if type changes
1165 in unknown way or ERROR_MARK_NODE if type is unchanged. */
1167 static tree
1168 extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci,
1169 HOST_WIDE_INT *type_offset)
1171 HOST_WIDE_INT offset, size, max_size;
1172 tree lhs, rhs, base;
1174 if (!gimple_assign_single_p (stmt))
1175 return NULL_TREE;
1177 lhs = gimple_assign_lhs (stmt);
1178 rhs = gimple_assign_rhs1 (stmt);
1179 if (TREE_CODE (lhs) != COMPONENT_REF
1180 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1182 if (dump_file)
1183 fprintf (dump_file, " LHS is not virtual table.\n");
1184 return NULL_TREE;
1187 if (tci->vtbl_ptr_ref && operand_equal_p (lhs, tci->vtbl_ptr_ref, 0))
1189 else
1191 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1192 if (DECL_P (tci->instance))
1194 if (base != tci->instance)
1196 if (dump_file)
1198 fprintf (dump_file, " base:");
1199 print_generic_expr (dump_file, base, TDF_SLIM);
1200 fprintf (dump_file, " does not match instance:");
1201 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1202 fprintf (dump_file, "\n");
1204 return NULL_TREE;
1207 else if (TREE_CODE (base) == MEM_REF)
1209 if (!operand_equal_p (tci->instance, TREE_OPERAND (base, 0), 0))
1211 if (dump_file)
1213 fprintf (dump_file, " base mem ref:");
1214 print_generic_expr (dump_file, base, TDF_SLIM);
1215 fprintf (dump_file, " does not match instance:");
1216 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1217 fprintf (dump_file, "\n");
1219 return NULL_TREE;
1221 if (!integer_zerop (TREE_OPERAND (base, 1)))
1223 if (!tree_fits_shwi_p (TREE_OPERAND (base, 1)))
1225 if (dump_file)
1227 fprintf (dump_file, " base mem ref:");
1228 print_generic_expr (dump_file, base, TDF_SLIM);
1229 fprintf (dump_file, " has non-representable offset:");
1230 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1231 fprintf (dump_file, "\n");
1233 return NULL_TREE;
1235 else
1236 offset += tree_to_shwi (TREE_OPERAND (base, 1)) * BITS_PER_UNIT;
1239 else if (!operand_equal_p (tci->instance, base, 0)
1240 || tci->offset)
1242 if (dump_file)
1244 fprintf (dump_file, " base:");
1245 print_generic_expr (dump_file, base, TDF_SLIM);
1246 fprintf (dump_file, " does not match instance:");
1247 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1248 fprintf (dump_file, " with offset %i\n", (int)tci->offset);
1250 return tci->offset > POINTER_SIZE ? error_mark_node : NULL_TREE;
1252 if (offset != tci->offset
1253 || size != POINTER_SIZE
1254 || max_size != POINTER_SIZE)
1256 if (dump_file)
1257 fprintf (dump_file, " wrong offset %i!=%i or size %i\n",
1258 (int)offset, (int)tci->offset, (int)size);
1259 return offset + POINTER_SIZE <= tci->offset
1260 || (max_size != -1
1261 && tci->offset + POINTER_SIZE > offset + max_size)
1262 ? error_mark_node : NULL;
1266 tree vtable;
1267 unsigned HOST_WIDE_INT offset2;
1269 if (!vtable_pointer_value_to_vtable (rhs, &vtable, &offset2))
1271 if (dump_file)
1272 fprintf (dump_file, " Failed to lookup binfo\n");
1273 return NULL;
1276 tree binfo = subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
1277 offset2, vtable);
1278 if (!binfo)
1280 if (dump_file)
1281 fprintf (dump_file, " Construction vtable used\n");
1282 /* FIXME: We should suport construction contexts. */
1283 return NULL;
1286 *type_offset = tree_to_shwi (BINFO_OFFSET (binfo)) * BITS_PER_UNIT;
1287 return DECL_CONTEXT (vtable);
1290 /* Record dynamic type change of TCI to TYPE. */
1292 static void
1293 record_known_type (struct type_change_info *tci, tree type, HOST_WIDE_INT offset)
1295 if (dump_file)
1297 if (type)
1299 fprintf (dump_file, " Recording type: ");
1300 print_generic_expr (dump_file, type, TDF_SLIM);
1301 fprintf (dump_file, " at offset %i\n", (int)offset);
1303 else
1304 fprintf (dump_file, " Recording unknown type\n");
1307 /* If we found a constructor of type that is not polymorphic or
1308 that may contain the type in question as a field (not as base),
1309 restrict to the inner class first to make type matching bellow
1310 happier. */
1311 if (type
1312 && (offset
1313 || (TREE_CODE (type) != RECORD_TYPE
1314 || !TYPE_BINFO (type)
1315 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))))
1317 ipa_polymorphic_call_context context;
1319 context.offset = offset;
1320 context.outer_type = type;
1321 context.maybe_in_construction = false;
1322 context.maybe_derived_type = false;
1323 context.dynamic = true;
1324 /* If we failed to find the inner type, we know that the call
1325 would be undefined for type produced here. */
1326 if (!context.restrict_to_inner_class (tci->otr_type))
1328 if (dump_file)
1329 fprintf (dump_file, " Ignoring; does not contain otr_type\n");
1330 return;
1332 /* Watch for case we reached an POD type and anticipate placement
1333 new. */
1334 if (!context.maybe_derived_type)
1336 type = context.outer_type;
1337 offset = context.offset;
1340 if (tci->type_maybe_changed
1341 && (!types_same_for_odr (type, tci->known_current_type)
1342 || offset != tci->known_current_offset))
1343 tci->multiple_types_encountered = true;
1344 tci->known_current_type = TYPE_MAIN_VARIANT (type);
1345 tci->known_current_offset = offset;
1346 tci->type_maybe_changed = true;
1349 /* Callback of walk_aliased_vdefs and a helper function for
1350 detect_type_change to check whether a particular statement may modify
1351 the virtual table pointer, and if possible also determine the new type of
1352 the (sub-)object. It stores its result into DATA, which points to a
1353 type_change_info structure. */
1355 static bool
1356 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
1358 gimple stmt = SSA_NAME_DEF_STMT (vdef);
1359 struct type_change_info *tci = (struct type_change_info *) data;
1360 tree fn;
1362 /* If we already gave up, just terminate the rest of walk. */
1363 if (tci->multiple_types_encountered)
1364 return true;
1366 if (is_gimple_call (stmt))
1368 if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
1369 return false;
1371 /* Check for a constructor call. */
1372 if ((fn = gimple_call_fndecl (stmt)) != NULL_TREE
1373 && DECL_CXX_CONSTRUCTOR_P (fn)
1374 && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1375 && gimple_call_num_args (stmt))
1377 tree op = walk_ssa_copies (gimple_call_arg (stmt, 0));
1378 tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
1379 HOST_WIDE_INT offset = 0, size, max_size;
1381 if (dump_file)
1383 fprintf (dump_file, " Checking constructor call: ");
1384 print_gimple_stmt (dump_file, stmt, 0, 0);
1387 /* See if THIS parameter seems like instance pointer. */
1388 if (TREE_CODE (op) == ADDR_EXPR)
1390 op = get_ref_base_and_extent (TREE_OPERAND (op, 0),
1391 &offset, &size, &max_size);
1392 if (size != max_size || max_size == -1)
1394 tci->speculative = true;
1395 return false;
1397 if (op && TREE_CODE (op) == MEM_REF)
1399 if (!tree_fits_shwi_p (TREE_OPERAND (op, 1)))
1401 tci->speculative = true;
1402 return false;
1404 offset += tree_to_shwi (TREE_OPERAND (op, 1))
1405 * BITS_PER_UNIT;
1406 op = TREE_OPERAND (op, 0);
1408 else if (DECL_P (op))
1410 else
1412 tci->speculative = true;
1413 return false;
1415 op = walk_ssa_copies (op);
1417 if (operand_equal_p (op, tci->instance, 0)
1418 && TYPE_SIZE (type)
1419 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1420 && tree_fits_shwi_p (TYPE_SIZE (type))
1421 && tree_to_shwi (TYPE_SIZE (type)) + offset > tci->offset)
1423 record_known_type (tci, type, tci->offset - offset);
1424 return true;
1427 /* Calls may possibly change dynamic type by placement new. Assume
1428 it will not happen, but make result speculative only. */
1429 if (dump_file)
1431 fprintf (dump_file, " Function call may change dynamic type:");
1432 print_gimple_stmt (dump_file, stmt, 0, 0);
1434 tci->speculative = true;
1435 return false;
1437 /* Check for inlined virtual table store. */
1438 else if (noncall_stmt_may_be_vtbl_ptr_store (stmt))
1440 tree type;
1441 HOST_WIDE_INT offset = 0;
1442 if (dump_file)
1444 fprintf (dump_file, " Checking vtbl store: ");
1445 print_gimple_stmt (dump_file, stmt, 0, 0);
1448 type = extr_type_from_vtbl_ptr_store (stmt, tci, &offset);
1449 if (type == error_mark_node)
1450 return false;
1451 gcc_assert (!type || TYPE_MAIN_VARIANT (type) == type);
1452 if (!type)
1454 if (dump_file)
1455 fprintf (dump_file, " Unanalyzed store may change type.\n");
1456 tci->seen_unanalyzed_store = true;
1457 tci->speculative = true;
1459 else
1460 record_known_type (tci, type, offset);
1461 return true;
1463 else
1464 return false;
1467 /* THIS is polymorphic call context obtained from get_polymorphic_context.
1468 OTR_OBJECT is pointer to the instance returned by OBJ_TYPE_REF_OBJECT.
1469 INSTANCE is pointer to the outer instance as returned by
1470 get_polymorphic_context. To avoid creation of temporary expressions,
1471 INSTANCE may also be an declaration of get_polymorphic_context found the
1472 value to be in static storage.
1474 If the type of instance is not fully determined
1475 (either OUTER_TYPE is unknown or MAYBE_IN_CONSTRUCTION/INCLUDE_DERIVED_TYPES
1476 is set), try to walk memory writes and find the actual construction of the
1477 instance.
1479 Return true if memory is unchanged from function entry.
1481 We do not include this analysis in the context analysis itself, because
1482 it needs memory SSA to be fully built and the walk may be expensive.
1483 So it is not suitable for use withing fold_stmt and similar uses. */
1485 bool
1486 ipa_polymorphic_call_context::get_dynamic_type (tree instance,
1487 tree otr_object,
1488 tree otr_type,
1489 gimple call)
1491 struct type_change_info tci;
1492 ao_ref ao;
1493 bool function_entry_reached = false;
1494 tree instance_ref = NULL;
1495 gimple stmt = call;
1496 /* Remember OFFSET before it is modified by restrict_to_inner_class.
1497 This is because we do not update INSTANCE when walking inwards. */
1498 HOST_WIDE_INT instance_offset = offset;
1500 if (otr_type)
1501 otr_type = TYPE_MAIN_VARIANT (otr_type);
1503 /* Walk into inner type. This may clear maybe_derived_type and save us
1504 from useless work. It also makes later comparsions with static type
1505 easier. */
1506 if (outer_type && otr_type)
1508 if (!restrict_to_inner_class (otr_type))
1509 return false;
1512 if (!maybe_in_construction && !maybe_derived_type)
1513 return false;
1515 /* We need to obtain refernce to virtual table pointer. It is better
1516 to look it up in the code rather than build our own. This require bit
1517 of pattern matching, but we end up verifying that what we found is
1518 correct.
1520 What we pattern match is:
1522 tmp = instance->_vptr.A; // vtbl ptr load
1523 tmp2 = tmp[otr_token]; // vtable lookup
1524 OBJ_TYPE_REF(tmp2;instance->0) (instance);
1526 We want to start alias oracle walk from vtbl pointer load,
1527 but we may not be able to identify it, for example, when PRE moved the
1528 load around. */
1530 if (gimple_code (call) == GIMPLE_CALL)
1532 tree ref = gimple_call_fn (call);
1533 HOST_WIDE_INT offset2, size, max_size;
1535 if (TREE_CODE (ref) == OBJ_TYPE_REF)
1537 ref = OBJ_TYPE_REF_EXPR (ref);
1538 ref = walk_ssa_copies (ref);
1540 /* If call target is already known, no need to do the expensive
1541 memory walk. */
1542 if (is_gimple_min_invariant (ref))
1543 return false;
1545 /* Check if definition looks like vtable lookup. */
1546 if (TREE_CODE (ref) == SSA_NAME
1547 && !SSA_NAME_IS_DEFAULT_DEF (ref)
1548 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref))
1549 && TREE_CODE (gimple_assign_rhs1
1550 (SSA_NAME_DEF_STMT (ref))) == MEM_REF)
1552 ref = get_base_address
1553 (TREE_OPERAND (gimple_assign_rhs1
1554 (SSA_NAME_DEF_STMT (ref)), 0));
1555 ref = walk_ssa_copies (ref);
1556 /* Find base address of the lookup and see if it looks like
1557 vptr load. */
1558 if (TREE_CODE (ref) == SSA_NAME
1559 && !SSA_NAME_IS_DEFAULT_DEF (ref)
1560 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref)))
1562 tree ref_exp = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (ref));
1563 tree base_ref = get_ref_base_and_extent
1564 (ref_exp, &offset2, &size, &max_size);
1566 /* Finally verify that what we found looks like read from
1567 OTR_OBJECT or from INSTANCE with offset OFFSET. */
1568 if (base_ref
1569 && ((TREE_CODE (base_ref) == MEM_REF
1570 && ((offset2 == instance_offset
1571 && TREE_OPERAND (base_ref, 0) == instance)
1572 || (!offset2
1573 && TREE_OPERAND (base_ref, 0)
1574 == otr_object)))
1575 || (DECL_P (instance) && base_ref == instance
1576 && offset2 == instance_offset)))
1578 stmt = SSA_NAME_DEF_STMT (ref);
1579 instance_ref = ref_exp;
1586 /* If we failed to look up the refernece in code, build our own. */
1587 if (!instance_ref)
1589 /* If the statement in question does not use memory, we can't tell
1590 anything. */
1591 if (!gimple_vuse (stmt))
1592 return false;
1593 ao_ref_init_from_ptr_and_size (&ao, otr_object, NULL);
1595 else
1596 /* Otherwise use the real reference. */
1597 ao_ref_init (&ao, instance_ref);
1599 /* We look for vtbl pointer read. */
1600 ao.size = POINTER_SIZE;
1601 ao.max_size = ao.size;
1602 /* We are looking for stores to vptr pointer within the instance of
1603 outer type.
1604 TODO: The vptr pointer type is globally known, we probably should
1605 keep it and do that even when otr_type is unknown. */
1606 if (otr_type)
1608 ao.base_alias_set
1609 = get_alias_set (outer_type ? outer_type : otr_type);
1610 ao.ref_alias_set
1611 = get_alias_set (TREE_TYPE (BINFO_VTABLE (TYPE_BINFO (otr_type))));
1614 if (dump_file)
1616 fprintf (dump_file, "Determining dynamic type for call: ");
1617 print_gimple_stmt (dump_file, call, 0, 0);
1618 fprintf (dump_file, " Starting walk at: ");
1619 print_gimple_stmt (dump_file, stmt, 0, 0);
1620 fprintf (dump_file, " instance pointer: ");
1621 print_generic_expr (dump_file, otr_object, TDF_SLIM);
1622 fprintf (dump_file, " Outer instance pointer: ");
1623 print_generic_expr (dump_file, instance, TDF_SLIM);
1624 fprintf (dump_file, " offset: %i (bits)", (int)offset);
1625 fprintf (dump_file, " vtbl reference: ");
1626 print_generic_expr (dump_file, instance_ref, TDF_SLIM);
1627 fprintf (dump_file, "\n");
1630 tci.offset = offset;
1631 tci.instance = instance;
1632 tci.vtbl_ptr_ref = instance_ref;
1633 gcc_assert (TREE_CODE (instance) != MEM_REF);
1634 tci.known_current_type = NULL_TREE;
1635 tci.known_current_offset = 0;
1636 tci.otr_type = otr_type;
1637 tci.type_maybe_changed = false;
1638 tci.multiple_types_encountered = false;
1639 tci.speculative = false;
1640 tci.seen_unanalyzed_store = false;
1642 walk_aliased_vdefs (&ao, gimple_vuse (stmt), check_stmt_for_type_change,
1643 &tci, NULL, &function_entry_reached);
1645 /* If we did not find any type changing statements, we may still drop
1646 maybe_in_construction flag if the context already have outer type.
1648 Here we make special assumptions about both constructors and
1649 destructors which are all the functions that are allowed to alter the
1650 VMT pointers. It assumes that destructors begin with assignment into
1651 all VMT pointers and that constructors essentially look in the
1652 following way:
1654 1) The very first thing they do is that they call constructors of
1655 ancestor sub-objects that have them.
1657 2) Then VMT pointers of this and all its ancestors is set to new
1658 values corresponding to the type corresponding to the constructor.
1660 3) Only afterwards, other stuff such as constructor of member
1661 sub-objects and the code written by the user is run. Only this may
1662 include calling virtual functions, directly or indirectly.
1664 4) placement new can not be used to change type of non-POD statically
1665 allocated variables.
1667 There is no way to call a constructor of an ancestor sub-object in any
1668 other way.
1670 This means that we do not have to care whether constructors get the
1671 correct type information because they will always change it (in fact,
1672 if we define the type to be given by the VMT pointer, it is undefined).
1674 The most important fact to derive from the above is that if, for some
1675 statement in the section 3, we try to detect whether the dynamic type
1676 has changed, we can safely ignore all calls as we examine the function
1677 body backwards until we reach statements in section 2 because these
1678 calls cannot be ancestor constructors or destructors (if the input is
1679 not bogus) and so do not change the dynamic type (this holds true only
1680 for automatically allocated objects but at the moment we devirtualize
1681 only these). We then must detect that statements in section 2 change
1682 the dynamic type and can try to derive the new type. That is enough
1683 and we can stop, we will never see the calls into constructors of
1684 sub-objects in this code.
1686 Therefore if the static outer type was found (outer_type)
1687 we can safely ignore tci.speculative that is set on calls and give up
1688 only if there was dyanmic type store that may affect given variable
1689 (seen_unanalyzed_store) */
1691 if (!tci.type_maybe_changed
1692 || (outer_type
1693 && !dynamic
1694 && !tci.seen_unanalyzed_store
1695 && !tci.multiple_types_encountered
1696 && offset == tci.offset
1697 && types_same_for_odr (tci.known_current_type,
1698 outer_type)))
1700 if (!outer_type || tci.seen_unanalyzed_store)
1701 return false;
1702 if (maybe_in_construction)
1703 maybe_in_construction = false;
1704 if (dump_file)
1705 fprintf (dump_file, " No dynamic type change found.\n");
1706 return true;
1709 if (tci.known_current_type
1710 && !function_entry_reached
1711 && !tci.multiple_types_encountered)
1713 if (!tci.speculative)
1715 outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1716 offset = tci.known_current_offset;
1717 dynamic = true;
1718 maybe_in_construction = false;
1719 maybe_derived_type = false;
1720 if (dump_file)
1721 fprintf (dump_file, " Determined dynamic type.\n");
1723 else if (!speculative_outer_type
1724 || speculative_maybe_derived_type)
1726 speculative_outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1727 speculative_offset = tci.known_current_offset;
1728 speculative_maybe_derived_type = false;
1729 if (dump_file)
1730 fprintf (dump_file, " Determined speculative dynamic type.\n");
1733 else if (dump_file)
1735 fprintf (dump_file, " Found multiple types%s%s\n",
1736 function_entry_reached ? " (function entry reached)" : "",
1737 function_entry_reached ? " (multiple types encountered)" : "");
1740 return false;
1743 /* See if speculation given by SPEC_OUTER_TYPE, SPEC_OFFSET and SPEC_MAYBE_DERIVED_TYPE
1744 seems consistent (and useful) with what we already have in the non-speculative context. */
1746 bool
1747 ipa_polymorphic_call_context::speculation_consistent_p (tree spec_outer_type,
1748 HOST_WIDE_INT spec_offset,
1749 bool spec_maybe_derived_type,
1750 tree otr_type) const
1752 if (!flag_devirtualize_speculatively)
1753 return false;
1755 /* Non-polymorphic types are useless for deriving likely polymorphic
1756 call targets. */
1757 if (!spec_outer_type || !contains_polymorphic_type_p (spec_outer_type))
1758 return false;
1760 /* If we know nothing, speculation is always good. */
1761 if (!outer_type)
1762 return true;
1764 /* Speculation is only useful to avoid derived types.
1765 This is not 100% true for placement new, where the outer context may
1766 turn out to be useless, but ignore these for now. */
1767 if (!maybe_derived_type)
1768 return false;
1770 /* If types agrees, speculation is consistent, but it makes sense only
1771 when it says something new. */
1772 if (types_must_be_same_for_odr (spec_outer_type, outer_type))
1773 return maybe_derived_type && !spec_maybe_derived_type;
1775 /* If speculation does not contain the type in question, ignore it. */
1776 if (otr_type
1777 && !contains_type_p (spec_outer_type, spec_offset, otr_type, false, true))
1778 return false;
1780 /* If outer type already contains speculation as a filed,
1781 it is useless. We already know from OUTER_TYPE
1782 SPEC_TYPE and that it is not in the construction. */
1783 if (contains_type_p (outer_type, offset - spec_offset,
1784 spec_outer_type, false, false))
1785 return false;
1787 /* If speculative outer type is not more specified than outer
1788 type, just give up.
1789 We can only decide this safely if we can compare types with OUTER_TYPE.
1791 if ((!in_lto_p || odr_type_p (outer_type))
1792 && !contains_type_p (spec_outer_type,
1793 spec_offset - offset,
1794 outer_type, false))
1795 return false;
1796 return true;
1799 /* Improve THIS with speculation described by NEW_OUTER_TYPE, NEW_OFFSET,
1800 NEW_MAYBE_DERIVED_TYPE
1801 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1803 bool
1804 ipa_polymorphic_call_context::combine_speculation_with
1805 (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1806 tree otr_type)
1808 if (!new_outer_type)
1809 return false;
1811 /* restrict_to_inner_class may eliminate wrong speculation making our job
1812 easeier. */
1813 if (otr_type)
1814 restrict_to_inner_class (otr_type);
1816 if (!speculation_consistent_p (new_outer_type, new_offset,
1817 new_maybe_derived_type, otr_type))
1818 return false;
1820 /* New speculation is a win in case we have no speculation or new
1821 speculation does not consider derivations. */
1822 if (!speculative_outer_type
1823 || (speculative_maybe_derived_type
1824 && !new_maybe_derived_type))
1826 speculative_outer_type = new_outer_type;
1827 speculative_offset = new_offset;
1828 speculative_maybe_derived_type = new_maybe_derived_type;
1829 return true;
1831 else if (types_must_be_same_for_odr (speculative_outer_type,
1832 new_outer_type))
1834 if (speculative_offset != new_offset)
1836 /* OK we have two contexts that seems valid but they disagree,
1837 just give up.
1839 This is not a lattice operation, so we may want to drop it later. */
1840 if (dump_file && (dump_flags & TDF_DETAILS))
1841 fprintf (dump_file,
1842 "Speculative outer types match, "
1843 "offset mismatch -> invalid speculation\n");
1844 clear_speculation ();
1845 return true;
1847 else
1849 if (speculative_maybe_derived_type && !new_maybe_derived_type)
1851 speculative_maybe_derived_type = false;
1852 return true;
1854 else
1855 return false;
1858 /* Choose type that contains the other. This one either contains the outer
1859 as a field (thus giving exactly one target) or is deeper in the type
1860 hiearchy. */
1861 else if (speculative_outer_type
1862 && speculative_maybe_derived_type
1863 && (new_offset > speculative_offset
1864 || (new_offset == speculative_offset
1865 && contains_type_p (new_outer_type,
1866 0, speculative_outer_type, false))))
1868 tree old_outer_type = speculative_outer_type;
1869 HOST_WIDE_INT old_offset = speculative_offset;
1870 bool old_maybe_derived_type = speculative_maybe_derived_type;
1872 speculative_outer_type = new_outer_type;
1873 speculative_offset = new_offset;
1874 speculative_maybe_derived_type = new_maybe_derived_type;
1876 if (otr_type)
1877 restrict_to_inner_class (otr_type);
1879 /* If the speculation turned out to make no sense, revert to sensible
1880 one. */
1881 if (!speculative_outer_type)
1883 speculative_outer_type = old_outer_type;
1884 speculative_offset = old_offset;
1885 speculative_maybe_derived_type = old_maybe_derived_type;
1886 return false;
1888 return (old_offset != speculative_offset
1889 || old_maybe_derived_type != speculative_maybe_derived_type
1890 || types_must_be_same_for_odr (speculative_outer_type,
1891 new_outer_type));
1893 return false;
1896 /* Make speculation less specific so
1897 NEW_OUTER_TYPE, NEW_OFFSET, NEW_MAYBE_DERIVED_TYPE is also included.
1898 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1900 bool
1901 ipa_polymorphic_call_context::meet_speculation_with
1902 (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1903 tree otr_type)
1905 if (!new_outer_type && speculative_outer_type)
1907 clear_speculation ();
1908 return true;
1911 /* restrict_to_inner_class may eliminate wrong speculation making our job
1912 easeier. */
1913 if (otr_type)
1914 restrict_to_inner_class (otr_type);
1916 if (!speculative_outer_type
1917 || !speculation_consistent_p (speculative_outer_type,
1918 speculative_offset,
1919 speculative_maybe_derived_type,
1920 otr_type))
1921 return false;
1923 if (!speculation_consistent_p (new_outer_type, new_offset,
1924 new_maybe_derived_type, otr_type))
1926 clear_speculation ();
1927 return true;
1930 else if (types_must_be_same_for_odr (speculative_outer_type,
1931 new_outer_type))
1933 if (speculative_offset != new_offset)
1935 clear_speculation ();
1936 return true;
1938 else
1940 if (!speculative_maybe_derived_type && new_maybe_derived_type)
1942 speculative_maybe_derived_type = true;
1943 return true;
1945 else
1946 return false;
1949 /* See if one type contains the other as a field (not base). */
1950 else if (contains_type_p (new_outer_type, new_offset - speculative_offset,
1951 speculative_outer_type, false, false))
1952 return false;
1953 else if (contains_type_p (speculative_outer_type,
1954 speculative_offset - new_offset,
1955 new_outer_type, false, false))
1957 speculative_outer_type = new_outer_type;
1958 speculative_offset = new_offset;
1959 speculative_maybe_derived_type = new_maybe_derived_type;
1960 return true;
1962 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
1963 else if (contains_type_p (new_outer_type,
1964 new_offset - speculative_offset,
1965 speculative_outer_type, false, true))
1967 if (!speculative_maybe_derived_type)
1969 speculative_maybe_derived_type = true;
1970 return true;
1972 return false;
1974 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
1975 else if (contains_type_p (speculative_outer_type,
1976 speculative_offset - new_offset, new_outer_type, false, true))
1978 speculative_outer_type = new_outer_type;
1979 speculative_offset = new_offset;
1980 speculative_maybe_derived_type = true;
1981 return true;
1983 else
1985 if (dump_file && (dump_flags & TDF_DETAILS))
1986 fprintf (dump_file, "Giving up on speculative meet\n");
1987 clear_speculation ();
1988 return true;
1992 /* Assume that both THIS and a given context is valid and strenghten THIS
1993 if possible. Return true if any strenghtening was made.
1994 If actual type the context is being used in is known, OTR_TYPE should be
1995 set accordingly. This improves quality of combined result. */
1997 bool
1998 ipa_polymorphic_call_context::combine_with (ipa_polymorphic_call_context ctx,
1999 tree otr_type)
2001 bool updated = false;
2003 if (ctx.useless_p () || invalid)
2004 return false;
2006 /* Restricting context to inner type makes merging easier, however do not
2007 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2008 if (otr_type && !invalid && !ctx.invalid)
2010 restrict_to_inner_class (otr_type);
2011 ctx.restrict_to_inner_class (otr_type);
2012 if(invalid)
2013 return false;
2016 if (dump_file && (dump_flags & TDF_DETAILS))
2018 fprintf (dump_file, "Polymorphic call context combine:");
2019 dump (dump_file);
2020 fprintf (dump_file, "With context: ");
2021 ctx.dump (dump_file);
2022 if (otr_type)
2024 fprintf (dump_file, "To be used with type: ");
2025 print_generic_expr (dump_file, otr_type, TDF_SLIM);
2026 fprintf (dump_file, "\n");
2030 /* If call is known to be invalid, we are done. */
2031 if (ctx.invalid)
2033 if (dump_file && (dump_flags & TDF_DETAILS))
2034 fprintf (dump_file, "-> Invalid context\n");
2035 goto invalidate;
2038 if (!ctx.outer_type)
2040 else if (!outer_type)
2042 outer_type = ctx.outer_type;
2043 offset = ctx.offset;
2044 dynamic = ctx.dynamic;
2045 maybe_in_construction = ctx.maybe_in_construction;
2046 maybe_derived_type = ctx.maybe_derived_type;
2047 updated = true;
2049 /* If types are known to be same, merging is quite easy. */
2050 else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2052 if (offset != ctx.offset
2053 && TYPE_SIZE (outer_type)
2054 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2056 if (dump_file && (dump_flags & TDF_DETAILS))
2057 fprintf (dump_file, "Outer types match, offset mismatch -> invalid\n");
2058 clear_speculation ();
2059 clear_outer_type ();
2060 invalid = true;
2061 return true;
2063 if (dump_file && (dump_flags & TDF_DETAILS))
2064 fprintf (dump_file, "Outer types match, merging flags\n");
2065 if (maybe_in_construction && !ctx.maybe_in_construction)
2067 updated = true;
2068 maybe_in_construction = false;
2070 if (maybe_derived_type && !ctx.maybe_derived_type)
2072 updated = true;
2073 maybe_derived_type = false;
2075 if (dynamic && !ctx.dynamic)
2077 updated = true;
2078 dynamic = false;
2081 /* If we know the type precisely, there is not much to improve. */
2082 else if (!maybe_derived_type && !maybe_in_construction
2083 && !ctx.maybe_derived_type && !ctx.maybe_in_construction)
2085 /* It may be easy to check if second context permits the first
2086 and set INVALID otherwise. This is not easy to do in general;
2087 contains_type_p may return false negatives for non-comparable
2088 types.
2090 If OTR_TYPE is known, we however can expect that
2091 restrict_to_inner_class should have discovered the same base
2092 type. */
2093 if (otr_type && !ctx.maybe_in_construction && !ctx.maybe_derived_type)
2095 if (dump_file && (dump_flags & TDF_DETAILS))
2096 fprintf (dump_file, "Contextes disagree -> invalid\n");
2097 goto invalidate;
2100 /* See if one type contains the other as a field (not base).
2101 In this case we want to choose the wider type, because it contains
2102 more information. */
2103 else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2104 outer_type, false, false))
2106 if (dump_file && (dump_flags & TDF_DETAILS))
2107 fprintf (dump_file, "Second type contain the first as a field\n");
2109 if (maybe_derived_type)
2111 outer_type = ctx.outer_type;
2112 maybe_derived_type = ctx.maybe_derived_type;
2113 offset = ctx.offset;
2114 dynamic = ctx.dynamic;
2115 updated = true;
2118 /* If we do not know how the context is being used, we can
2119 not clear MAYBE_IN_CONSTRUCTION because it may be offseted
2120 to other component of OUTER_TYPE later and we know nothing
2121 about it. */
2122 if (otr_type && maybe_in_construction
2123 && !ctx.maybe_in_construction)
2125 maybe_in_construction = false;
2126 updated = true;
2129 else if (contains_type_p (outer_type, offset - ctx.offset,
2130 ctx.outer_type, false, false))
2132 if (dump_file && (dump_flags & TDF_DETAILS))
2133 fprintf (dump_file, "First type contain the second as a field\n");
2135 if (otr_type && maybe_in_construction
2136 && !ctx.maybe_in_construction)
2138 maybe_in_construction = false;
2139 updated = true;
2142 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2143 else if (contains_type_p (ctx.outer_type,
2144 ctx.offset - offset, outer_type, false, true))
2146 if (dump_file && (dump_flags & TDF_DETAILS))
2147 fprintf (dump_file, "First type is base of second\n");
2148 if (!maybe_derived_type)
2150 if (!ctx.maybe_in_construction
2151 && types_odr_comparable (outer_type, ctx.outer_type))
2153 if (dump_file && (dump_flags & TDF_DETAILS))
2154 fprintf (dump_file, "Second context does not permit base -> invalid\n");
2155 goto invalidate;
2158 /* Pick variant deeper in the hiearchy. */
2159 else
2161 outer_type = ctx.outer_type;
2162 maybe_in_construction = ctx.maybe_in_construction;
2163 maybe_derived_type = ctx.maybe_derived_type;
2164 offset = ctx.offset;
2165 dynamic = ctx.dynamic;
2166 updated = true;
2169 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2170 else if (contains_type_p (outer_type,
2171 offset - ctx.offset, ctx.outer_type, false, true))
2173 if (dump_file && (dump_flags & TDF_DETAILS))
2174 fprintf (dump_file, "Second type is base of first\n");
2175 if (!ctx.maybe_derived_type)
2177 if (!maybe_in_construction
2178 && types_odr_comparable (outer_type, ctx.outer_type))
2180 if (dump_file && (dump_flags & TDF_DETAILS))
2181 fprintf (dump_file, "First context does not permit base -> invalid\n");
2182 goto invalidate;
2184 /* Pick the base type. */
2185 else if (maybe_in_construction)
2187 outer_type = ctx.outer_type;
2188 maybe_in_construction = ctx.maybe_in_construction;
2189 maybe_derived_type = ctx.maybe_derived_type;
2190 offset = ctx.offset;
2191 dynamic = ctx.dynamic;
2192 updated = true;
2196 /* TODO handle merging using hiearchy. */
2197 else if (dump_file && (dump_flags & TDF_DETAILS))
2198 fprintf (dump_file, "Giving up on merge\n");
2200 updated |= combine_speculation_with (ctx.speculative_outer_type,
2201 ctx.speculative_offset,
2202 ctx.speculative_maybe_derived_type,
2203 otr_type);
2205 if (updated && dump_file && (dump_flags & TDF_DETAILS))
2207 fprintf (dump_file, "Updated as: ");
2208 dump (dump_file);
2209 fprintf (dump_file, "\n");
2211 return updated;
2213 invalidate:
2214 invalid = true;
2215 clear_speculation ();
2216 clear_outer_type ();
2217 return true;
2220 /* Take non-speculative info, merge it with speculative and clear speculation.
2221 Used when we no longer manage to keep track of actual outer type, but we
2222 think it is still there.
2224 If OTR_TYPE is set, the transformation can be done more effectively assuming
2225 that context is going to be used only that way. */
2227 void
2228 ipa_polymorphic_call_context::make_speculative (tree otr_type)
2230 tree spec_outer_type = outer_type;
2231 HOST_WIDE_INT spec_offset = offset;
2232 bool spec_maybe_derived_type = maybe_derived_type;
2234 if (invalid)
2236 invalid = false;
2237 clear_outer_type ();
2238 clear_speculation ();
2239 return;
2241 if (!outer_type)
2242 return;
2243 clear_outer_type ();
2244 combine_speculation_with (spec_outer_type, spec_offset,
2245 spec_maybe_derived_type,
2246 otr_type);
2249 /* Use when we can not track dynamic type change. This speculatively assume
2250 type change is not happening. */
2252 void
2253 ipa_polymorphic_call_context::possible_dynamic_type_change (bool in_poly_cdtor,
2254 tree otr_type)
2256 if (dynamic)
2257 make_speculative (otr_type);
2258 else if (in_poly_cdtor)
2259 maybe_in_construction = true;
2262 /* Return TRUE if this context conveys the same information as OTHER. */
2264 bool
2265 ipa_polymorphic_call_context::equal_to
2266 (const ipa_polymorphic_call_context &x) const
2268 if (useless_p ())
2269 return x.useless_p ();
2270 if (invalid)
2271 return x.invalid;
2272 if (x.useless_p () || x.invalid)
2273 return false;
2275 if (outer_type)
2277 if (!x.outer_type
2278 || !types_odr_comparable (outer_type, x.outer_type)
2279 || !types_same_for_odr (outer_type, x.outer_type)
2280 || offset != x.offset
2281 || maybe_in_construction != x.maybe_in_construction
2282 || maybe_derived_type != x.maybe_derived_type
2283 || dynamic != x.dynamic)
2284 return false;
2286 else if (x.outer_type)
2287 return false;
2290 if (speculative_outer_type
2291 && speculation_consistent_p (speculative_outer_type, speculative_offset,
2292 speculative_maybe_derived_type, NULL_TREE))
2294 if (!x.speculative_outer_type)
2295 return false;
2297 if (!types_odr_comparable (speculative_outer_type,
2298 x.speculative_outer_type)
2299 || !types_same_for_odr (speculative_outer_type,
2300 x.speculative_outer_type)
2301 || speculative_offset != x.speculative_offset
2302 || speculative_maybe_derived_type != x.speculative_maybe_derived_type)
2303 return false;
2305 else if (x.speculative_outer_type
2306 && x.speculation_consistent_p (x.speculative_outer_type,
2307 x.speculative_offset,
2308 x.speculative_maybe_derived_type,
2309 NULL))
2310 return false;
2312 return true;
2315 /* Modify context to be strictly less restrictive than CTX. */
2317 bool
2318 ipa_polymorphic_call_context::meet_with (ipa_polymorphic_call_context ctx,
2319 tree otr_type)
2321 bool updated = false;
2323 if (useless_p () || ctx.invalid)
2324 return false;
2326 /* Restricting context to inner type makes merging easier, however do not
2327 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2328 if (otr_type && !useless_p () && !ctx.useless_p ())
2330 restrict_to_inner_class (otr_type);
2331 ctx.restrict_to_inner_class (otr_type);
2332 if(invalid)
2333 return false;
2336 if (equal_to (ctx))
2337 return false;
2339 if (ctx.useless_p () || invalid)
2341 *this = ctx;
2342 return true;
2345 if (dump_file && (dump_flags & TDF_DETAILS))
2347 fprintf (dump_file, "Polymorphic call context meet:");
2348 dump (dump_file);
2349 fprintf (dump_file, "With context: ");
2350 ctx.dump (dump_file);
2351 if (otr_type)
2353 fprintf (dump_file, "To be used with type: ");
2354 print_generic_expr (dump_file, otr_type, TDF_SLIM);
2355 fprintf (dump_file, "\n");
2359 if (!dynamic && ctx.dynamic)
2361 dynamic = true;
2362 updated = true;
2365 /* If call is known to be invalid, we are done. */
2366 if (!outer_type)
2368 else if (!ctx.outer_type)
2370 clear_outer_type ();
2371 updated = true;
2373 /* If types are known to be same, merging is quite easy. */
2374 else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2376 if (offset != ctx.offset
2377 && TYPE_SIZE (outer_type)
2378 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2380 if (dump_file && (dump_flags & TDF_DETAILS))
2381 fprintf (dump_file, "Outer types match, offset mismatch -> clearing\n");
2382 clear_outer_type ();
2383 return true;
2385 if (dump_file && (dump_flags & TDF_DETAILS))
2386 fprintf (dump_file, "Outer types match, merging flags\n");
2387 if (!maybe_in_construction && ctx.maybe_in_construction)
2389 updated = true;
2390 maybe_in_construction = true;
2392 if (!maybe_derived_type && ctx.maybe_derived_type)
2394 updated = true;
2395 maybe_derived_type = true;
2397 if (!dynamic && ctx.dynamic)
2399 updated = true;
2400 dynamic = true;
2403 /* See if one type contains the other as a field (not base). */
2404 else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2405 outer_type, false, false))
2407 if (dump_file && (dump_flags & TDF_DETAILS))
2408 fprintf (dump_file, "Second type contain the first as a field\n");
2410 /* The second type is more specified, so we keep the first.
2411 We need to set DYNAMIC flag to avoid declaring context INVALID
2412 of OFFSET ends up being out of range. */
2413 if (!dynamic
2414 && (ctx.dynamic
2415 || (!otr_type
2416 && (!TYPE_SIZE (ctx.outer_type)
2417 || !TYPE_SIZE (outer_type)
2418 || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2419 TYPE_SIZE (outer_type), 0)))))
2421 dynamic = true;
2422 updated = true;
2425 else if (contains_type_p (outer_type, offset - ctx.offset,
2426 ctx.outer_type, false, false))
2428 if (dump_file && (dump_flags & TDF_DETAILS))
2429 fprintf (dump_file, "First type contain the second as a field\n");
2431 if (!dynamic
2432 && (ctx.dynamic
2433 || (!otr_type
2434 && (!TYPE_SIZE (ctx.outer_type)
2435 || !TYPE_SIZE (outer_type)
2436 || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2437 TYPE_SIZE (outer_type), 0)))))
2438 dynamic = true;
2439 outer_type = ctx.outer_type;
2440 offset = ctx.offset;
2441 dynamic = ctx.dynamic;
2442 maybe_in_construction = ctx.maybe_in_construction;
2443 maybe_derived_type = ctx.maybe_derived_type;
2444 updated = true;
2446 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2447 else if (contains_type_p (ctx.outer_type,
2448 ctx.offset - offset, outer_type, false, true))
2450 if (dump_file && (dump_flags & TDF_DETAILS))
2451 fprintf (dump_file, "First type is base of second\n");
2452 if (!maybe_derived_type)
2454 maybe_derived_type = true;
2455 updated = true;
2457 if (!maybe_in_construction && ctx.maybe_in_construction)
2459 maybe_in_construction = true;
2460 updated = true;
2462 if (!dynamic && ctx.dynamic)
2464 dynamic = true;
2465 updated = true;
2468 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2469 else if (contains_type_p (outer_type,
2470 offset - ctx.offset, ctx.outer_type, false, true))
2472 if (dump_file && (dump_flags & TDF_DETAILS))
2473 fprintf (dump_file, "Second type is base of first\n");
2474 outer_type = ctx.outer_type;
2475 offset = ctx.offset;
2476 updated = true;
2477 if (!maybe_derived_type)
2478 maybe_derived_type = true;
2479 if (!maybe_in_construction && ctx.maybe_in_construction)
2480 maybe_in_construction = true;
2481 if (!dynamic && ctx.dynamic)
2482 dynamic = true;
2484 /* TODO handle merging using hiearchy. */
2485 else
2487 if (dump_file && (dump_flags & TDF_DETAILS))
2488 fprintf (dump_file, "Giving up on meet\n");
2489 clear_outer_type ();
2490 updated = true;
2493 updated |= meet_speculation_with (ctx.speculative_outer_type,
2494 ctx.speculative_offset,
2495 ctx.speculative_maybe_derived_type,
2496 otr_type);
2498 if (updated && dump_file && (dump_flags & TDF_DETAILS))
2500 fprintf (dump_file, "Updated as: ");
2501 dump (dump_file);
2502 fprintf (dump_file, "\n");
2504 return updated;