gcc/
[official-gcc.git] / gcc / ipa-polymorphic-call.c
blobac8c78ea8f1d37c38450ca563e825b07820880f4
1 /* Analysis of polymorphic call context.
2 Copyright (C) 2013-2015 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "print-tree.h"
30 #include "calls.h"
31 #include "hard-reg-set.h"
32 #include "function.h"
33 #include "rtl.h"
34 #include "flags.h"
35 #include "insn-config.h"
36 #include "expmed.h"
37 #include "dojump.h"
38 #include "explow.h"
39 #include "emit-rtl.h"
40 #include "varasm.h"
41 #include "stmt.h"
42 #include "expr.h"
43 #include "tree-pass.h"
44 #include "target.h"
45 #include "tree-pretty-print.h"
46 #include "predict.h"
47 #include "basic-block.h"
48 #include "plugin-api.h"
49 #include "ipa-ref.h"
50 #include "cgraph.h"
51 #include "ipa-utils.h"
52 #include "tree-ssa-alias.h"
53 #include "internal-fn.h"
54 #include "gimple-fold.h"
55 #include "gimple-expr.h"
56 #include "gimple.h"
57 #include "alloc-pool.h"
58 #include "symbol-summary.h"
59 #include "ipa-prop.h"
60 #include "ipa-inline.h"
61 #include "diagnostic.h"
62 #include "tree-dfa.h"
63 #include "demangle.h"
64 #include "dbgcnt.h"
65 #include "gimple-pretty-print.h"
66 #include "stor-layout.h"
67 #include "intl.h"
68 #include "data-streamer.h"
69 #include "lto-streamer.h"
70 #include "streamer-hooks.h"
71 #include "tree-ssa-operands.h"
72 #include "tree-into-ssa.h"
74 /* Return true when TYPE contains an polymorphic type and thus is interesting
75 for devirtualization machinery. */
77 static bool contains_type_p (tree, HOST_WIDE_INT, tree,
78 bool consider_placement_new = true,
79 bool consider_bases = true);
81 bool
82 contains_polymorphic_type_p (const_tree type)
84 type = TYPE_MAIN_VARIANT (type);
86 if (RECORD_OR_UNION_TYPE_P (type))
88 if (TYPE_BINFO (type)
89 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
90 return true;
91 for (tree fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
92 if (TREE_CODE (fld) == FIELD_DECL
93 && !DECL_ARTIFICIAL (fld)
94 && contains_polymorphic_type_p (TREE_TYPE (fld)))
95 return true;
96 return false;
98 if (TREE_CODE (type) == ARRAY_TYPE)
99 return contains_polymorphic_type_p (TREE_TYPE (type));
100 return false;
103 /* Return true if it seems valid to use placement new to build EXPECTED_TYPE
104 at possition CUR_OFFSET within TYPE.
106 POD can be changed to an instance of a polymorphic type by
107 placement new. Here we play safe and assume that any
108 non-polymorphic type is POD. */
109 bool
110 possible_placement_new (tree type, tree expected_type,
111 HOST_WIDE_INT cur_offset)
113 return ((TREE_CODE (type) != RECORD_TYPE
114 || !TYPE_BINFO (type)
115 || cur_offset >= POINTER_SIZE
116 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))
117 && (!TYPE_SIZE (type)
118 || !tree_fits_shwi_p (TYPE_SIZE (type))
119 || (cur_offset
120 + (expected_type ? tree_to_uhwi (TYPE_SIZE (expected_type))
121 : POINTER_SIZE)
122 <= tree_to_uhwi (TYPE_SIZE (type)))));
125 /* THIS->OUTER_TYPE is a type of memory object where object of OTR_TYPE
126 is contained at THIS->OFFSET. Walk the memory representation of
127 THIS->OUTER_TYPE and find the outermost class type that match
128 OTR_TYPE or contain OTR_TYPE as a base. Update THIS
129 to represent it.
131 If OTR_TYPE is NULL, just find outermost polymorphic type with
132 virtual table present at possition OFFSET.
134 For example when THIS represents type
135 class A
137 int a;
138 class B b;
140 and we look for type at offset sizeof(int), we end up with B and offset 0.
141 If the same is produced by multiple inheritance, we end up with A and offset
142 sizeof(int).
144 If we can not find corresponding class, give up by setting
145 THIS->OUTER_TYPE to OTR_TYPE and THIS->OFFSET to NULL.
146 Return true when lookup was sucesful.
148 When CONSIDER_PLACEMENT_NEW is false, reject contexts that may be made
149 valid only via alocation of new polymorphic type inside by means
150 of placement new.
152 When CONSIDER_BASES is false, only look for actual fields, not base types
153 of TYPE. */
155 bool
156 ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type,
157 bool consider_placement_new,
158 bool consider_bases)
160 tree type = outer_type;
161 HOST_WIDE_INT cur_offset = offset;
162 bool speculative = false;
163 bool size_unknown = false;
164 unsigned HOST_WIDE_INT otr_type_size = POINTER_SIZE;
166 /* Update OUTER_TYPE to match EXPECTED_TYPE if it is not set. */
167 if (!outer_type)
169 clear_outer_type (otr_type);
170 type = otr_type;
171 cur_offset = 0;
173 /* See if OFFSET points inside OUTER_TYPE. If it does not, we know
174 that the context is either invalid, or the instance type must be
175 derived from OUTER_TYPE.
177 Because the instance type may contain field whose type is of OUTER_TYPE,
178 we can not derive any effective information about it.
180 TODO: In the case we know all derrived types, we can definitely do better
181 here. */
182 else if (TYPE_SIZE (outer_type)
183 && tree_fits_shwi_p (TYPE_SIZE (outer_type))
184 && tree_to_shwi (TYPE_SIZE (outer_type)) >= 0
185 && tree_to_shwi (TYPE_SIZE (outer_type)) <= offset)
187 clear_outer_type (otr_type);
188 type = otr_type;
189 cur_offset = 0;
191 /* If derived type is not allowed, we know that the context is invalid.
192 For dynamic types, we really do not have information about
193 size of the memory location. It is possible that completely
194 different type is stored after outer_type. */
195 if (!maybe_derived_type && !dynamic)
197 clear_speculation ();
198 invalid = true;
199 return false;
203 if (otr_type && TYPE_SIZE (otr_type)
204 && tree_fits_shwi_p (TYPE_SIZE (otr_type)))
205 otr_type_size = tree_to_uhwi (TYPE_SIZE (otr_type));
207 if (!type || offset < 0)
208 goto no_useful_type_info;
210 /* Find the sub-object the constant actually refers to and mark whether it is
211 an artificial one (as opposed to a user-defined one).
213 This loop is performed twice; first time for outer_type and second time
214 for speculative_outer_type. The second run has SPECULATIVE set. */
215 while (true)
217 unsigned HOST_WIDE_INT pos, size;
218 tree fld;
220 /* If we do not know size of TYPE, we need to be more conservative
221 about accepting cases where we can not find EXPECTED_TYPE.
222 Generally the types that do matter here are of constant size.
223 Size_unknown case should be very rare. */
224 if (TYPE_SIZE (type)
225 && tree_fits_shwi_p (TYPE_SIZE (type))
226 && tree_to_shwi (TYPE_SIZE (type)) >= 0)
227 size_unknown = false;
228 else
229 size_unknown = true;
231 /* On a match, just return what we found. */
232 if ((otr_type
233 && types_odr_comparable (type, otr_type)
234 && types_same_for_odr (type, otr_type))
235 || (!otr_type
236 && TREE_CODE (type) == RECORD_TYPE
237 && TYPE_BINFO (type)
238 && polymorphic_type_binfo_p (TYPE_BINFO (type))))
240 if (speculative)
242 /* If we did not match the offset, just give up on speculation. */
243 if (cur_offset != 0
244 /* Also check if speculation did not end up being same as
245 non-speculation. */
246 || (types_must_be_same_for_odr (speculative_outer_type,
247 outer_type)
248 && (maybe_derived_type
249 == speculative_maybe_derived_type)))
250 clear_speculation ();
251 return true;
253 else
255 /* If type is known to be final, do not worry about derived
256 types. Testing it here may help us to avoid speculation. */
257 if (otr_type && TREE_CODE (outer_type) == RECORD_TYPE
258 && (!in_lto_p || odr_type_p (outer_type))
259 && type_with_linkage_p (outer_type)
260 && type_known_to_have_no_derivations_p (outer_type))
261 maybe_derived_type = false;
263 /* Type can not contain itself on an non-zero offset. In that case
264 just give up. Still accept the case where size is now known.
265 Either the second copy may appear past the end of type or within
266 the non-POD buffer located inside the variably sized type
267 itself. */
268 if (cur_offset != 0)
269 goto no_useful_type_info;
270 /* If we determined type precisely or we have no clue on
271 speuclation, we are done. */
272 if (!maybe_derived_type || !speculative_outer_type
273 || !speculation_consistent_p (speculative_outer_type,
274 speculative_offset,
275 speculative_maybe_derived_type,
276 otr_type))
278 clear_speculation ();
279 return true;
281 /* Otherwise look into speculation now. */
282 else
284 speculative = true;
285 type = speculative_outer_type;
286 cur_offset = speculative_offset;
287 continue;
292 /* Walk fields and find corresponding on at OFFSET. */
293 if (TREE_CODE (type) == RECORD_TYPE)
295 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
297 if (TREE_CODE (fld) != FIELD_DECL)
298 continue;
300 pos = int_bit_position (fld);
301 if (pos > (unsigned HOST_WIDE_INT)cur_offset)
302 continue;
304 /* Do not consider vptr itself. Not even for placement new. */
305 if (!pos && DECL_ARTIFICIAL (fld)
306 && POINTER_TYPE_P (TREE_TYPE (fld))
307 && TYPE_BINFO (type)
308 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
309 continue;
311 if (!DECL_SIZE (fld) || !tree_fits_uhwi_p (DECL_SIZE (fld)))
312 goto no_useful_type_info;
313 size = tree_to_uhwi (DECL_SIZE (fld));
315 /* We can always skip types smaller than pointer size:
316 those can not contain a virtual table pointer.
318 Disqualifying fields that are too small to fit OTR_TYPE
319 saves work needed to walk them for no benefit.
320 Because of the way the bases are packed into a class, the
321 field's size may be smaller than type size, so it needs
322 to be done with a care. */
324 if (pos <= (unsigned HOST_WIDE_INT)cur_offset
325 && (pos + size) >= (unsigned HOST_WIDE_INT)cur_offset
326 + POINTER_SIZE
327 && (!otr_type
328 || !TYPE_SIZE (TREE_TYPE (fld))
329 || !tree_fits_shwi_p (TYPE_SIZE (TREE_TYPE (fld)))
330 || (pos + tree_to_uhwi (TYPE_SIZE (TREE_TYPE (fld))))
331 >= cur_offset + otr_type_size))
332 break;
335 if (!fld)
336 goto no_useful_type_info;
338 type = TYPE_MAIN_VARIANT (TREE_TYPE (fld));
339 cur_offset -= pos;
340 /* DECL_ARTIFICIAL represents a basetype. */
341 if (!DECL_ARTIFICIAL (fld))
343 if (!speculative)
345 outer_type = type;
346 offset = cur_offset;
347 /* As soon as we se an field containing the type,
348 we know we are not looking for derivations. */
349 maybe_derived_type = false;
351 else
353 speculative_outer_type = type;
354 speculative_offset = cur_offset;
355 speculative_maybe_derived_type = false;
358 else if (!consider_bases)
359 goto no_useful_type_info;
361 else if (TREE_CODE (type) == ARRAY_TYPE)
363 tree subtype = TYPE_MAIN_VARIANT (TREE_TYPE (type));
365 /* Give up if we don't know array field size.
366 Also give up on non-polymorphic types as they are used
367 as buffers for placement new. */
368 if (!TYPE_SIZE (subtype)
369 || !tree_fits_shwi_p (TYPE_SIZE (subtype))
370 || tree_to_shwi (TYPE_SIZE (subtype)) <= 0
371 || !contains_polymorphic_type_p (subtype))
372 goto no_useful_type_info;
374 HOST_WIDE_INT new_offset = cur_offset % tree_to_shwi (TYPE_SIZE (subtype));
376 /* We may see buffer for placement new. In this case the expected type
377 can be bigger than the subtype. */
378 if (TYPE_SIZE (subtype)
379 && (cur_offset + otr_type_size
380 > tree_to_uhwi (TYPE_SIZE (subtype))))
381 goto no_useful_type_info;
383 cur_offset = new_offset;
384 type = TYPE_MAIN_VARIANT (subtype);
385 if (!speculative)
387 outer_type = type;
388 offset = cur_offset;
389 maybe_derived_type = false;
391 else
393 speculative_outer_type = type;
394 speculative_offset = cur_offset;
395 speculative_maybe_derived_type = false;
398 /* Give up on anything else. */
399 else
401 no_useful_type_info:
402 if (maybe_derived_type && !speculative
403 && TREE_CODE (outer_type) == RECORD_TYPE
404 && TREE_CODE (otr_type) == RECORD_TYPE
405 && TYPE_BINFO (otr_type)
406 && !offset
407 && get_binfo_at_offset (TYPE_BINFO (otr_type), 0, outer_type))
409 clear_outer_type (otr_type);
410 if (!speculative_outer_type
411 || !speculation_consistent_p (speculative_outer_type,
412 speculative_offset,
413 speculative_maybe_derived_type,
414 otr_type))
415 clear_speculation ();
416 if (speculative_outer_type)
418 speculative = true;
419 type = speculative_outer_type;
420 cur_offset = speculative_offset;
422 else
423 return true;
425 /* We found no way to embedd EXPECTED_TYPE in TYPE.
426 We still permit two special cases - placement new and
427 the case of variadic types containing themselves. */
428 if (!speculative
429 && consider_placement_new
430 && (size_unknown || !type || maybe_derived_type
431 || possible_placement_new (type, otr_type, cur_offset)))
433 /* In these weird cases we want to accept the context.
434 In non-speculative run we have no useful outer_type info
435 (TODO: we may eventually want to record upper bound on the
436 type size that can be used to prune the walk),
437 but we still want to consider speculation that may
438 give useful info. */
439 if (!speculative)
441 clear_outer_type (otr_type);
442 if (!speculative_outer_type
443 || !speculation_consistent_p (speculative_outer_type,
444 speculative_offset,
445 speculative_maybe_derived_type,
446 otr_type))
447 clear_speculation ();
448 if (speculative_outer_type)
450 speculative = true;
451 type = speculative_outer_type;
452 cur_offset = speculative_offset;
454 else
455 return true;
457 else
458 clear_speculation ();
459 return true;
461 else
463 clear_speculation ();
464 if (speculative)
465 return true;
466 clear_outer_type (otr_type);
467 invalid = true;
468 return false;
474 /* Return true if OUTER_TYPE contains OTR_TYPE at OFFSET.
475 CONSIDER_PLACEMENT_NEW makes function to accept cases where OTR_TYPE can
476 be built within OUTER_TYPE by means of placement new. CONSIDER_BASES makes
477 function to accept cases where OTR_TYPE appears as base of OUTER_TYPE or as
478 base of one of fields of OUTER_TYPE. */
480 static bool
481 contains_type_p (tree outer_type, HOST_WIDE_INT offset,
482 tree otr_type,
483 bool consider_placement_new,
484 bool consider_bases)
486 ipa_polymorphic_call_context context;
488 /* Check that type is within range. */
489 if (offset < 0)
490 return false;
491 if (TYPE_SIZE (outer_type) && TYPE_SIZE (otr_type)
492 && TREE_CODE (outer_type) == INTEGER_CST
493 && TREE_CODE (otr_type) == INTEGER_CST
494 && wi::ltu_p (wi::to_offset (outer_type), (wi::to_offset (otr_type) + offset)))
495 return false;
497 context.offset = offset;
498 context.outer_type = TYPE_MAIN_VARIANT (outer_type);
499 context.maybe_derived_type = false;
500 return context.restrict_to_inner_class (otr_type, consider_placement_new, consider_bases);
504 /* Return a FUNCTION_DECL if BLOCK represents a constructor or destructor.
505 If CHECK_CLONES is true, also check for clones of ctor/dtors. */
507 tree
508 inlined_polymorphic_ctor_dtor_block_p (tree block, bool check_clones)
510 tree fn = BLOCK_ABSTRACT_ORIGIN (block);
511 if (fn == NULL || TREE_CODE (fn) != FUNCTION_DECL)
512 return NULL_TREE;
514 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
515 || (!DECL_CXX_CONSTRUCTOR_P (fn) && !DECL_CXX_DESTRUCTOR_P (fn)))
517 if (!check_clones)
518 return NULL_TREE;
520 /* Watch for clones where we constant propagated the first
521 argument (pointer to the instance). */
522 fn = DECL_ABSTRACT_ORIGIN (fn);
523 if (!fn
524 || TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
525 || (!DECL_CXX_CONSTRUCTOR_P (fn) && !DECL_CXX_DESTRUCTOR_P (fn)))
526 return NULL_TREE;
529 if (flags_from_decl_or_type (fn) & (ECF_PURE | ECF_CONST))
530 return NULL_TREE;
532 return fn;
536 /* We know that the instance is stored in variable or parameter
537 (not dynamically allocated) and we want to disprove the fact
538 that it may be in construction at invocation of CALL.
540 BASE represents memory location where instance is stored.
541 If BASE is NULL, it is assumed to be global memory.
542 OUTER_TYPE is known type of the instance or NULL if not
543 known.
545 For the variable to be in construction we actually need to
546 be in constructor of corresponding global variable or
547 the inline stack of CALL must contain the constructor.
548 Check this condition. This check works safely only before
549 IPA passes, because inline stacks may become out of date
550 later. */
552 bool
553 decl_maybe_in_construction_p (tree base, tree outer_type,
554 gimple call, tree function)
556 if (outer_type)
557 outer_type = TYPE_MAIN_VARIANT (outer_type);
558 gcc_assert (!base || DECL_P (base));
560 /* After inlining the code unification optimizations may invalidate
561 inline stacks. Also we need to give up on global variables after
562 IPA, because addresses of these may have been propagated to their
563 constructors. */
564 if (DECL_STRUCT_FUNCTION (function)->after_inlining)
565 return true;
567 /* Pure functions can not do any changes on the dynamic type;
568 that require writting to memory. */
569 if ((!base || !auto_var_in_fn_p (base, function))
570 && flags_from_decl_or_type (function) & (ECF_PURE | ECF_CONST))
571 return false;
573 bool check_clones = !base || is_global_var (base);
574 for (tree block = gimple_block (call); block && TREE_CODE (block) == BLOCK;
575 block = BLOCK_SUPERCONTEXT (block))
576 if (tree fn = inlined_polymorphic_ctor_dtor_block_p (block, check_clones))
578 tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
580 if (!outer_type || !types_odr_comparable (type, outer_type))
582 if (TREE_CODE (type) == RECORD_TYPE
583 && TYPE_BINFO (type)
584 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
585 return true;
587 else if (types_same_for_odr (type, outer_type))
588 return true;
591 if (!base || (TREE_CODE (base) == VAR_DECL && is_global_var (base)))
593 if (TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
594 || (!DECL_CXX_CONSTRUCTOR_P (function)
595 && !DECL_CXX_DESTRUCTOR_P (function)))
597 if (!DECL_ABSTRACT_ORIGIN (function))
598 return false;
599 /* Watch for clones where we constant propagated the first
600 argument (pointer to the instance). */
601 function = DECL_ABSTRACT_ORIGIN (function);
602 if (!function
603 || TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
604 || (!DECL_CXX_CONSTRUCTOR_P (function)
605 && !DECL_CXX_DESTRUCTOR_P (function)))
606 return false;
608 tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (function));
609 if (!outer_type || !types_odr_comparable (type, outer_type))
611 if (TREE_CODE (type) == RECORD_TYPE
612 && TYPE_BINFO (type)
613 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
614 return true;
616 else if (types_same_for_odr (type, outer_type))
617 return true;
619 return false;
622 /* Dump human readable context to F. If NEWLINE is true, it will be terminated
623 by a newline. */
625 void
626 ipa_polymorphic_call_context::dump (FILE *f, bool newline) const
628 fprintf (f, " ");
629 if (invalid)
630 fprintf (f, "Call is known to be undefined");
631 else
633 if (useless_p ())
634 fprintf (f, "nothing known");
635 if (outer_type || offset)
637 fprintf (f, "Outer type%s:", dynamic ? " (dynamic)":"");
638 print_generic_expr (f, outer_type, TDF_SLIM);
639 if (maybe_derived_type)
640 fprintf (f, " (or a derived type)");
641 if (maybe_in_construction)
642 fprintf (f, " (maybe in construction)");
643 fprintf (f, " offset " HOST_WIDE_INT_PRINT_DEC,
644 offset);
646 if (speculative_outer_type)
648 if (outer_type || offset)
649 fprintf (f, " ");
650 fprintf (f, "Speculative outer type:");
651 print_generic_expr (f, speculative_outer_type, TDF_SLIM);
652 if (speculative_maybe_derived_type)
653 fprintf (f, " (or a derived type)");
654 fprintf (f, " at offset " HOST_WIDE_INT_PRINT_DEC,
655 speculative_offset);
658 if (newline)
659 fprintf(f, "\n");
662 /* Print context to stderr. */
664 void
665 ipa_polymorphic_call_context::debug () const
667 dump (stderr);
670 /* Stream out the context to OB. */
672 void
673 ipa_polymorphic_call_context::stream_out (struct output_block *ob) const
675 struct bitpack_d bp = bitpack_create (ob->main_stream);
677 bp_pack_value (&bp, invalid, 1);
678 bp_pack_value (&bp, maybe_in_construction, 1);
679 bp_pack_value (&bp, maybe_derived_type, 1);
680 bp_pack_value (&bp, speculative_maybe_derived_type, 1);
681 bp_pack_value (&bp, dynamic, 1);
682 bp_pack_value (&bp, outer_type != NULL, 1);
683 bp_pack_value (&bp, offset != 0, 1);
684 bp_pack_value (&bp, speculative_outer_type != NULL, 1);
685 streamer_write_bitpack (&bp);
687 if (outer_type != NULL)
688 stream_write_tree (ob, outer_type, true);
689 if (offset)
690 streamer_write_hwi (ob, offset);
691 if (speculative_outer_type != NULL)
693 stream_write_tree (ob, speculative_outer_type, true);
694 streamer_write_hwi (ob, speculative_offset);
696 else
697 gcc_assert (!speculative_offset);
700 /* Stream in the context from IB and DATA_IN. */
702 void
703 ipa_polymorphic_call_context::stream_in (struct lto_input_block *ib,
704 struct data_in *data_in)
706 struct bitpack_d bp = streamer_read_bitpack (ib);
708 invalid = bp_unpack_value (&bp, 1);
709 maybe_in_construction = bp_unpack_value (&bp, 1);
710 maybe_derived_type = bp_unpack_value (&bp, 1);
711 speculative_maybe_derived_type = bp_unpack_value (&bp, 1);
712 dynamic = bp_unpack_value (&bp, 1);
713 bool outer_type_p = bp_unpack_value (&bp, 1);
714 bool offset_p = bp_unpack_value (&bp, 1);
715 bool speculative_outer_type_p = bp_unpack_value (&bp, 1);
717 if (outer_type_p)
718 outer_type = stream_read_tree (ib, data_in);
719 else
720 outer_type = NULL;
721 if (offset_p)
722 offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
723 else
724 offset = 0;
725 if (speculative_outer_type_p)
727 speculative_outer_type = stream_read_tree (ib, data_in);
728 speculative_offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
730 else
732 speculative_outer_type = NULL;
733 speculative_offset = 0;
737 /* Proudce polymorphic call context for call method of instance
738 that is located within BASE (that is assumed to be a decl) at offset OFF. */
740 void
741 ipa_polymorphic_call_context::set_by_decl (tree base, HOST_WIDE_INT off)
743 gcc_assert (DECL_P (base));
744 clear_speculation ();
746 if (!contains_polymorphic_type_p (TREE_TYPE (base)))
748 clear_outer_type ();
749 offset = off;
750 return;
752 outer_type = TYPE_MAIN_VARIANT (TREE_TYPE (base));
753 offset = off;
754 /* Make very conservative assumption that all objects
755 may be in construction.
757 It is up to caller to revisit this via
758 get_dynamic_type or decl_maybe_in_construction_p. */
759 maybe_in_construction = true;
760 maybe_derived_type = false;
761 dynamic = false;
764 /* CST is an invariant (address of decl), try to get meaningful
765 polymorphic call context for polymorphic call of method
766 if instance of OTR_TYPE that is located at offset OFF of this invariant.
767 Return FALSE if nothing meaningful can be found. */
769 bool
770 ipa_polymorphic_call_context::set_by_invariant (tree cst,
771 tree otr_type,
772 HOST_WIDE_INT off)
774 HOST_WIDE_INT offset2, size, max_size;
775 tree base;
777 invalid = false;
778 off = 0;
779 clear_outer_type (otr_type);
781 if (TREE_CODE (cst) != ADDR_EXPR)
782 return false;
784 cst = TREE_OPERAND (cst, 0);
785 base = get_ref_base_and_extent (cst, &offset2, &size, &max_size);
786 if (!DECL_P (base) || max_size == -1 || max_size != size)
787 return false;
789 /* Only type inconsistent programs can have otr_type that is
790 not part of outer type. */
791 if (otr_type && !contains_type_p (TREE_TYPE (base), off, otr_type))
792 return false;
794 set_by_decl (base, off);
795 return true;
798 /* See if OP is SSA name initialized as a copy or by single assignment.
799 If so, walk the SSA graph up. Because simple PHI conditional is considered
800 copy, GLOBAL_VISITED may be used to avoid infinite loop walking the SSA
801 graph. */
803 static tree
804 walk_ssa_copies (tree op, hash_set<tree> **global_visited = NULL)
806 hash_set <tree> *visited = NULL;
807 STRIP_NOPS (op);
808 while (TREE_CODE (op) == SSA_NAME
809 && !SSA_NAME_IS_DEFAULT_DEF (op)
810 /* We might be called via fold_stmt during cfgcleanup where
811 SSA form need not be up-to-date. */
812 && !name_registered_for_update_p (op)
813 && (gimple_assign_single_p (SSA_NAME_DEF_STMT (op))
814 || gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI))
816 if (global_visited)
818 if (!*global_visited)
819 *global_visited = new hash_set<tree>;
820 if ((*global_visited)->add (op))
821 goto done;
823 else
825 if (!visited)
826 visited = new hash_set<tree>;
827 if (visited->add (op))
828 goto done;
830 /* Special case
831 if (ptr == 0)
832 ptr = 0;
833 else
834 ptr = ptr.foo;
835 This pattern is implicitly produced for casts to non-primary
836 bases. When doing context analysis, we do not really care
837 about the case pointer is NULL, becuase the call will be
838 undefined anyway. */
839 if (gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI)
841 gimple phi = SSA_NAME_DEF_STMT (op);
843 if (gimple_phi_num_args (phi) > 2)
844 goto done;
845 if (gimple_phi_num_args (phi) == 1)
846 op = gimple_phi_arg_def (phi, 0);
847 else if (integer_zerop (gimple_phi_arg_def (phi, 0)))
848 op = gimple_phi_arg_def (phi, 1);
849 else if (integer_zerop (gimple_phi_arg_def (phi, 1)))
850 op = gimple_phi_arg_def (phi, 0);
851 else
852 goto done;
854 else
856 if (gimple_assign_load_p (SSA_NAME_DEF_STMT (op)))
857 goto done;
858 op = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op));
860 STRIP_NOPS (op);
862 done:
863 if (visited)
864 delete (visited);
865 return op;
868 /* Create polymorphic call context from IP invariant CST.
869 This is typically &global_var.
870 OTR_TYPE specify type of polymorphic call or NULL if unknown, OFF
871 is offset of call. */
873 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree cst,
874 tree otr_type,
875 HOST_WIDE_INT off)
877 clear_speculation ();
878 set_by_invariant (cst, otr_type, off);
881 /* Build context for pointer REF contained in FNDECL at statement STMT.
882 if INSTANCE is non-NULL, return pointer to the object described by
883 the context or DECL where context is contained in. */
885 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl,
886 tree ref,
887 gimple stmt,
888 tree *instance)
890 tree otr_type = NULL;
891 tree base_pointer;
892 hash_set <tree> *visited = NULL;
894 if (TREE_CODE (ref) == OBJ_TYPE_REF)
896 otr_type = obj_type_ref_class (ref);
897 base_pointer = OBJ_TYPE_REF_OBJECT (ref);
899 else
900 base_pointer = ref;
902 /* Set up basic info in case we find nothing interesting in the analysis. */
903 clear_speculation ();
904 clear_outer_type (otr_type);
905 invalid = false;
907 /* Walk SSA for outer object. */
908 while (true)
910 base_pointer = walk_ssa_copies (base_pointer, &visited);
911 if (TREE_CODE (base_pointer) == ADDR_EXPR)
913 HOST_WIDE_INT size, max_size;
914 HOST_WIDE_INT offset2;
915 tree base = get_ref_base_and_extent (TREE_OPERAND (base_pointer, 0),
916 &offset2, &size, &max_size);
918 if (max_size != -1 && max_size == size)
919 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base)),
920 offset + offset2,
921 true,
922 NULL /* Do not change outer type. */);
924 /* If this is a varying address, punt. */
925 if ((TREE_CODE (base) == MEM_REF || DECL_P (base))
926 && max_size != -1
927 && max_size == size)
929 /* We found dereference of a pointer. Type of the pointer
930 and MEM_REF is meaningless, but we can look futher. */
931 if (TREE_CODE (base) == MEM_REF)
933 base_pointer = TREE_OPERAND (base, 0);
934 offset
935 += offset2 + mem_ref_offset (base).to_short_addr () * BITS_PER_UNIT;
936 outer_type = NULL;
938 /* We found base object. In this case the outer_type
939 is known. */
940 else if (DECL_P (base))
942 if (visited)
943 delete (visited);
944 /* Only type inconsistent programs can have otr_type that is
945 not part of outer type. */
946 if (otr_type
947 && !contains_type_p (TREE_TYPE (base),
948 offset + offset2, otr_type))
950 invalid = true;
951 if (instance)
952 *instance = base_pointer;
953 return;
955 set_by_decl (base, offset + offset2);
956 if (outer_type && maybe_in_construction && stmt)
957 maybe_in_construction
958 = decl_maybe_in_construction_p (base,
959 outer_type,
960 stmt,
961 fndecl);
962 if (instance)
963 *instance = base;
964 return;
966 else
967 break;
969 else
970 break;
972 else if (TREE_CODE (base_pointer) == POINTER_PLUS_EXPR
973 && tree_fits_uhwi_p (TREE_OPERAND (base_pointer, 1)))
975 offset += tree_to_shwi (TREE_OPERAND (base_pointer, 1))
976 * BITS_PER_UNIT;
977 base_pointer = TREE_OPERAND (base_pointer, 0);
979 else
980 break;
983 if (visited)
984 delete (visited);
986 /* Try to determine type of the outer object. */
987 if (TREE_CODE (base_pointer) == SSA_NAME
988 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
989 && TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL)
991 /* See if parameter is THIS pointer of a method. */
992 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE
993 && SSA_NAME_VAR (base_pointer) == DECL_ARGUMENTS (fndecl))
995 outer_type
996 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
997 gcc_assert (TREE_CODE (outer_type) == RECORD_TYPE
998 || TREE_CODE (outer_type) == UNION_TYPE);
1000 /* Dynamic casting has possibly upcasted the type
1001 in the hiearchy. In this case outer type is less
1002 informative than inner type and we should forget
1003 about it. */
1004 if ((otr_type
1005 && !contains_type_p (outer_type, offset,
1006 otr_type))
1007 || !contains_polymorphic_type_p (outer_type))
1009 outer_type = NULL;
1010 if (instance)
1011 *instance = base_pointer;
1012 return;
1015 dynamic = true;
1017 /* If the function is constructor or destructor, then
1018 the type is possibly in construction, but we know
1019 it is not derived type. */
1020 if (DECL_CXX_CONSTRUCTOR_P (fndecl)
1021 || DECL_CXX_DESTRUCTOR_P (fndecl))
1023 maybe_in_construction = true;
1024 maybe_derived_type = false;
1026 else
1028 maybe_derived_type = true;
1029 maybe_in_construction = false;
1031 if (instance)
1032 *instance = base_pointer;
1033 return;
1035 /* Non-PODs passed by value are really passed by invisible
1036 reference. In this case we also know the type of the
1037 object. */
1038 if (DECL_BY_REFERENCE (SSA_NAME_VAR (base_pointer)))
1040 outer_type
1041 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
1042 /* Only type inconsistent programs can have otr_type that is
1043 not part of outer type. */
1044 if (otr_type && !contains_type_p (outer_type, offset,
1045 otr_type))
1047 invalid = true;
1048 if (instance)
1049 *instance = base_pointer;
1050 return;
1052 /* Non-polymorphic types have no interest for us. */
1053 else if (!otr_type && !contains_polymorphic_type_p (outer_type))
1055 outer_type = NULL;
1056 if (instance)
1057 *instance = base_pointer;
1058 return;
1060 maybe_derived_type = false;
1061 maybe_in_construction = false;
1062 if (instance)
1063 *instance = base_pointer;
1064 return;
1068 tree base_type = TREE_TYPE (base_pointer);
1070 if (TREE_CODE (base_pointer) == SSA_NAME
1071 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
1072 && !(TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL
1073 || TREE_CODE (SSA_NAME_VAR (base_pointer)) == RESULT_DECL))
1075 invalid = true;
1076 if (instance)
1077 *instance = base_pointer;
1078 return;
1080 if (TREE_CODE (base_pointer) == SSA_NAME
1081 && SSA_NAME_DEF_STMT (base_pointer)
1082 && gimple_assign_single_p (SSA_NAME_DEF_STMT (base_pointer)))
1083 base_type = TREE_TYPE (gimple_assign_rhs1
1084 (SSA_NAME_DEF_STMT (base_pointer)));
1086 if (base_type && POINTER_TYPE_P (base_type))
1087 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base_type)),
1088 offset,
1089 true, NULL /* Do not change type here */);
1090 /* TODO: There are multiple ways to derive a type. For instance
1091 if BASE_POINTER is passed to an constructor call prior our refernece.
1092 We do not make this type of flow sensitive analysis yet. */
1093 if (instance)
1094 *instance = base_pointer;
1095 return;
1098 /* Structure to be passed in between detect_type_change and
1099 check_stmt_for_type_change. */
1101 struct type_change_info
1103 /* Offset into the object where there is the virtual method pointer we are
1104 looking for. */
1105 HOST_WIDE_INT offset;
1106 /* The declaration or SSA_NAME pointer of the base that we are checking for
1107 type change. */
1108 tree instance;
1109 /* The reference to virtual table pointer used. */
1110 tree vtbl_ptr_ref;
1111 tree otr_type;
1112 /* If we actually can tell the type that the object has changed to, it is
1113 stored in this field. Otherwise it remains NULL_TREE. */
1114 tree known_current_type;
1115 HOST_WIDE_INT known_current_offset;
1117 /* Set to true if dynamic type change has been detected. */
1118 bool type_maybe_changed;
1119 /* Set to true if multiple types have been encountered. known_current_type
1120 must be disregarded in that case. */
1121 bool multiple_types_encountered;
1122 /* Set to true if we possibly missed some dynamic type changes and we should
1123 consider the set to be speculative. */
1124 bool speculative;
1125 bool seen_unanalyzed_store;
1128 /* Return true if STMT is not call and can modify a virtual method table pointer.
1129 We take advantage of fact that vtable stores must appear within constructor
1130 and destructor functions. */
1132 static bool
1133 noncall_stmt_may_be_vtbl_ptr_store (gimple stmt)
1135 if (is_gimple_assign (stmt))
1137 tree lhs = gimple_assign_lhs (stmt);
1139 if (gimple_clobber_p (stmt))
1140 return false;
1141 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
1143 if (flag_strict_aliasing
1144 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
1145 return false;
1147 if (TREE_CODE (lhs) == COMPONENT_REF
1148 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1149 return false;
1150 /* In the future we might want to use get_base_ref_and_offset to find
1151 if there is a field corresponding to the offset and if so, proceed
1152 almost like if it was a component ref. */
1156 /* Code unification may mess with inline stacks. */
1157 if (cfun->after_inlining)
1158 return true;
1160 /* Walk the inline stack and watch out for ctors/dtors.
1161 TODO: Maybe we can require the store to appear in toplevel
1162 block of CTOR/DTOR. */
1163 for (tree block = gimple_block (stmt); block && TREE_CODE (block) == BLOCK;
1164 block = BLOCK_SUPERCONTEXT (block))
1165 if (BLOCK_ABSTRACT_ORIGIN (block)
1166 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block)) == FUNCTION_DECL)
1167 return inlined_polymorphic_ctor_dtor_block_p (block, false);
1168 return (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
1169 && (DECL_CXX_CONSTRUCTOR_P (current_function_decl)
1170 || DECL_CXX_DESTRUCTOR_P (current_function_decl)));
1173 /* If STMT can be proved to be an assignment to the virtual method table
1174 pointer of ANALYZED_OBJ and the type associated with the new table
1175 identified, return the type. Otherwise return NULL_TREE if type changes
1176 in unknown way or ERROR_MARK_NODE if type is unchanged. */
1178 static tree
1179 extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci,
1180 HOST_WIDE_INT *type_offset)
1182 HOST_WIDE_INT offset, size, max_size;
1183 tree lhs, rhs, base;
1185 if (!gimple_assign_single_p (stmt))
1186 return NULL_TREE;
1188 lhs = gimple_assign_lhs (stmt);
1189 rhs = gimple_assign_rhs1 (stmt);
1190 if (TREE_CODE (lhs) != COMPONENT_REF
1191 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1193 if (dump_file)
1194 fprintf (dump_file, " LHS is not virtual table.\n");
1195 return NULL_TREE;
1198 if (tci->vtbl_ptr_ref && operand_equal_p (lhs, tci->vtbl_ptr_ref, 0))
1200 else
1202 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1203 if (DECL_P (tci->instance))
1205 if (base != tci->instance)
1207 if (dump_file)
1209 fprintf (dump_file, " base:");
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;
1218 else if (TREE_CODE (base) == MEM_REF)
1220 if (!operand_equal_p (tci->instance, TREE_OPERAND (base, 0), 0))
1222 if (dump_file)
1224 fprintf (dump_file, " base mem ref:");
1225 print_generic_expr (dump_file, base, TDF_SLIM);
1226 fprintf (dump_file, " does not match instance:");
1227 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1228 fprintf (dump_file, "\n");
1230 return NULL_TREE;
1232 if (!integer_zerop (TREE_OPERAND (base, 1)))
1234 if (!tree_fits_shwi_p (TREE_OPERAND (base, 1)))
1236 if (dump_file)
1238 fprintf (dump_file, " base mem ref:");
1239 print_generic_expr (dump_file, base, TDF_SLIM);
1240 fprintf (dump_file, " has non-representable offset:");
1241 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1242 fprintf (dump_file, "\n");
1244 return NULL_TREE;
1246 else
1247 offset += tree_to_shwi (TREE_OPERAND (base, 1)) * BITS_PER_UNIT;
1250 else if (!operand_equal_p (tci->instance, base, 0)
1251 || tci->offset)
1253 if (dump_file)
1255 fprintf (dump_file, " base:");
1256 print_generic_expr (dump_file, base, TDF_SLIM);
1257 fprintf (dump_file, " does not match instance:");
1258 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1259 fprintf (dump_file, " with offset %i\n", (int)tci->offset);
1261 return tci->offset > POINTER_SIZE ? error_mark_node : NULL_TREE;
1263 if (offset != tci->offset
1264 || size != POINTER_SIZE
1265 || max_size != POINTER_SIZE)
1267 if (dump_file)
1268 fprintf (dump_file, " wrong offset %i!=%i or size %i\n",
1269 (int)offset, (int)tci->offset, (int)size);
1270 return offset + POINTER_SIZE <= tci->offset
1271 || (max_size != -1
1272 && tci->offset + POINTER_SIZE > offset + max_size)
1273 ? error_mark_node : NULL;
1277 tree vtable;
1278 unsigned HOST_WIDE_INT offset2;
1280 if (!vtable_pointer_value_to_vtable (rhs, &vtable, &offset2))
1282 if (dump_file)
1283 fprintf (dump_file, " Failed to lookup binfo\n");
1284 return NULL;
1287 tree binfo = subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
1288 offset2, vtable);
1289 if (!binfo)
1291 if (dump_file)
1292 fprintf (dump_file, " Construction vtable used\n");
1293 /* FIXME: We should suport construction contexts. */
1294 return NULL;
1297 *type_offset = tree_to_shwi (BINFO_OFFSET (binfo)) * BITS_PER_UNIT;
1298 return DECL_CONTEXT (vtable);
1301 /* Record dynamic type change of TCI to TYPE. */
1303 static void
1304 record_known_type (struct type_change_info *tci, tree type, HOST_WIDE_INT offset)
1306 if (dump_file)
1308 if (type)
1310 fprintf (dump_file, " Recording type: ");
1311 print_generic_expr (dump_file, type, TDF_SLIM);
1312 fprintf (dump_file, " at offset %i\n", (int)offset);
1314 else
1315 fprintf (dump_file, " Recording unknown type\n");
1318 /* If we found a constructor of type that is not polymorphic or
1319 that may contain the type in question as a field (not as base),
1320 restrict to the inner class first to make type matching bellow
1321 happier. */
1322 if (type
1323 && (offset
1324 || (TREE_CODE (type) != RECORD_TYPE
1325 || !TYPE_BINFO (type)
1326 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))))
1328 ipa_polymorphic_call_context context;
1330 context.offset = offset;
1331 context.outer_type = type;
1332 context.maybe_in_construction = false;
1333 context.maybe_derived_type = false;
1334 context.dynamic = true;
1335 /* If we failed to find the inner type, we know that the call
1336 would be undefined for type produced here. */
1337 if (!context.restrict_to_inner_class (tci->otr_type))
1339 if (dump_file)
1340 fprintf (dump_file, " Ignoring; does not contain otr_type\n");
1341 return;
1343 /* Watch for case we reached an POD type and anticipate placement
1344 new. */
1345 if (!context.maybe_derived_type)
1347 type = context.outer_type;
1348 offset = context.offset;
1351 if (tci->type_maybe_changed
1352 && (!types_same_for_odr (type, tci->known_current_type)
1353 || offset != tci->known_current_offset))
1354 tci->multiple_types_encountered = true;
1355 tci->known_current_type = TYPE_MAIN_VARIANT (type);
1356 tci->known_current_offset = offset;
1357 tci->type_maybe_changed = true;
1360 /* Callback of walk_aliased_vdefs and a helper function for
1361 detect_type_change to check whether a particular statement may modify
1362 the virtual table pointer, and if possible also determine the new type of
1363 the (sub-)object. It stores its result into DATA, which points to a
1364 type_change_info structure. */
1366 static bool
1367 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
1369 gimple stmt = SSA_NAME_DEF_STMT (vdef);
1370 struct type_change_info *tci = (struct type_change_info *) data;
1371 tree fn;
1373 /* If we already gave up, just terminate the rest of walk. */
1374 if (tci->multiple_types_encountered)
1375 return true;
1377 if (is_gimple_call (stmt))
1379 if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
1380 return false;
1382 /* Check for a constructor call. */
1383 if ((fn = gimple_call_fndecl (stmt)) != NULL_TREE
1384 && DECL_CXX_CONSTRUCTOR_P (fn)
1385 && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1386 && gimple_call_num_args (stmt))
1388 tree op = walk_ssa_copies (gimple_call_arg (stmt, 0));
1389 tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (fn));
1390 HOST_WIDE_INT offset = 0, size, max_size;
1392 if (dump_file)
1394 fprintf (dump_file, " Checking constructor call: ");
1395 print_gimple_stmt (dump_file, stmt, 0, 0);
1398 /* See if THIS parameter seems like instance pointer. */
1399 if (TREE_CODE (op) == ADDR_EXPR)
1401 op = get_ref_base_and_extent (TREE_OPERAND (op, 0),
1402 &offset, &size, &max_size);
1403 if (size != max_size || max_size == -1)
1405 tci->speculative = true;
1406 return false;
1408 if (op && TREE_CODE (op) == MEM_REF)
1410 if (!tree_fits_shwi_p (TREE_OPERAND (op, 1)))
1412 tci->speculative = true;
1413 return false;
1415 offset += tree_to_shwi (TREE_OPERAND (op, 1))
1416 * BITS_PER_UNIT;
1417 op = TREE_OPERAND (op, 0);
1419 else if (DECL_P (op))
1421 else
1423 tci->speculative = true;
1424 return false;
1426 op = walk_ssa_copies (op);
1428 if (operand_equal_p (op, tci->instance, 0)
1429 && TYPE_SIZE (type)
1430 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1431 && tree_fits_shwi_p (TYPE_SIZE (type))
1432 && tree_to_shwi (TYPE_SIZE (type)) + offset > tci->offset)
1434 record_known_type (tci, type, tci->offset - offset);
1435 return true;
1438 /* Calls may possibly change dynamic type by placement new. Assume
1439 it will not happen, but make result speculative only. */
1440 if (dump_file)
1442 fprintf (dump_file, " Function call may change dynamic type:");
1443 print_gimple_stmt (dump_file, stmt, 0, 0);
1445 tci->speculative = true;
1446 return false;
1448 /* Check for inlined virtual table store. */
1449 else if (noncall_stmt_may_be_vtbl_ptr_store (stmt))
1451 tree type;
1452 HOST_WIDE_INT offset = 0;
1453 if (dump_file)
1455 fprintf (dump_file, " Checking vtbl store: ");
1456 print_gimple_stmt (dump_file, stmt, 0, 0);
1459 type = extr_type_from_vtbl_ptr_store (stmt, tci, &offset);
1460 if (type == error_mark_node)
1461 return false;
1462 gcc_assert (!type || TYPE_MAIN_VARIANT (type) == type);
1463 if (!type)
1465 if (dump_file)
1466 fprintf (dump_file, " Unanalyzed store may change type.\n");
1467 tci->seen_unanalyzed_store = true;
1468 tci->speculative = true;
1470 else
1471 record_known_type (tci, type, offset);
1472 return true;
1474 else
1475 return false;
1478 /* THIS is polymorphic call context obtained from get_polymorphic_context.
1479 OTR_OBJECT is pointer to the instance returned by OBJ_TYPE_REF_OBJECT.
1480 INSTANCE is pointer to the outer instance as returned by
1481 get_polymorphic_context. To avoid creation of temporary expressions,
1482 INSTANCE may also be an declaration of get_polymorphic_context found the
1483 value to be in static storage.
1485 If the type of instance is not fully determined
1486 (either OUTER_TYPE is unknown or MAYBE_IN_CONSTRUCTION/INCLUDE_DERIVED_TYPES
1487 is set), try to walk memory writes and find the actual construction of the
1488 instance.
1490 Return true if memory is unchanged from function entry.
1492 We do not include this analysis in the context analysis itself, because
1493 it needs memory SSA to be fully built and the walk may be expensive.
1494 So it is not suitable for use withing fold_stmt and similar uses. */
1496 bool
1497 ipa_polymorphic_call_context::get_dynamic_type (tree instance,
1498 tree otr_object,
1499 tree otr_type,
1500 gimple call)
1502 struct type_change_info tci;
1503 ao_ref ao;
1504 bool function_entry_reached = false;
1505 tree instance_ref = NULL;
1506 gimple stmt = call;
1507 /* Remember OFFSET before it is modified by restrict_to_inner_class.
1508 This is because we do not update INSTANCE when walking inwards. */
1509 HOST_WIDE_INT instance_offset = offset;
1511 if (otr_type)
1512 otr_type = TYPE_MAIN_VARIANT (otr_type);
1514 /* Walk into inner type. This may clear maybe_derived_type and save us
1515 from useless work. It also makes later comparsions with static type
1516 easier. */
1517 if (outer_type && otr_type)
1519 if (!restrict_to_inner_class (otr_type))
1520 return false;
1523 if (!maybe_in_construction && !maybe_derived_type)
1524 return false;
1526 /* We need to obtain refernce to virtual table pointer. It is better
1527 to look it up in the code rather than build our own. This require bit
1528 of pattern matching, but we end up verifying that what we found is
1529 correct.
1531 What we pattern match is:
1533 tmp = instance->_vptr.A; // vtbl ptr load
1534 tmp2 = tmp[otr_token]; // vtable lookup
1535 OBJ_TYPE_REF(tmp2;instance->0) (instance);
1537 We want to start alias oracle walk from vtbl pointer load,
1538 but we may not be able to identify it, for example, when PRE moved the
1539 load around. */
1541 if (gimple_code (call) == GIMPLE_CALL)
1543 tree ref = gimple_call_fn (call);
1544 HOST_WIDE_INT offset2, size, max_size;
1546 if (TREE_CODE (ref) == OBJ_TYPE_REF)
1548 ref = OBJ_TYPE_REF_EXPR (ref);
1549 ref = walk_ssa_copies (ref);
1551 /* If call target is already known, no need to do the expensive
1552 memory walk. */
1553 if (is_gimple_min_invariant (ref))
1554 return false;
1556 /* Check if definition looks like vtable lookup. */
1557 if (TREE_CODE (ref) == SSA_NAME
1558 && !SSA_NAME_IS_DEFAULT_DEF (ref)
1559 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref))
1560 && TREE_CODE (gimple_assign_rhs1
1561 (SSA_NAME_DEF_STMT (ref))) == MEM_REF)
1563 ref = get_base_address
1564 (TREE_OPERAND (gimple_assign_rhs1
1565 (SSA_NAME_DEF_STMT (ref)), 0));
1566 ref = walk_ssa_copies (ref);
1567 /* Find base address of the lookup and see if it looks like
1568 vptr load. */
1569 if (TREE_CODE (ref) == SSA_NAME
1570 && !SSA_NAME_IS_DEFAULT_DEF (ref)
1571 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref)))
1573 tree ref_exp = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (ref));
1574 tree base_ref = get_ref_base_and_extent
1575 (ref_exp, &offset2, &size, &max_size);
1577 /* Finally verify that what we found looks like read from
1578 OTR_OBJECT or from INSTANCE with offset OFFSET. */
1579 if (base_ref
1580 && ((TREE_CODE (base_ref) == MEM_REF
1581 && ((offset2 == instance_offset
1582 && TREE_OPERAND (base_ref, 0) == instance)
1583 || (!offset2
1584 && TREE_OPERAND (base_ref, 0)
1585 == otr_object)))
1586 || (DECL_P (instance) && base_ref == instance
1587 && offset2 == instance_offset)))
1589 stmt = SSA_NAME_DEF_STMT (ref);
1590 instance_ref = ref_exp;
1597 /* If we failed to look up the refernece in code, build our own. */
1598 if (!instance_ref)
1600 /* If the statement in question does not use memory, we can't tell
1601 anything. */
1602 if (!gimple_vuse (stmt))
1603 return false;
1604 ao_ref_init_from_ptr_and_size (&ao, otr_object, NULL);
1606 else
1607 /* Otherwise use the real reference. */
1608 ao_ref_init (&ao, instance_ref);
1610 /* We look for vtbl pointer read. */
1611 ao.size = POINTER_SIZE;
1612 ao.max_size = ao.size;
1613 /* We are looking for stores to vptr pointer within the instance of
1614 outer type.
1615 TODO: The vptr pointer type is globally known, we probably should
1616 keep it and do that even when otr_type is unknown. */
1617 if (otr_type)
1619 ao.base_alias_set
1620 = get_alias_set (outer_type ? outer_type : otr_type);
1621 ao.ref_alias_set
1622 = get_alias_set (TREE_TYPE (BINFO_VTABLE (TYPE_BINFO (otr_type))));
1625 if (dump_file)
1627 fprintf (dump_file, "Determining dynamic type for call: ");
1628 print_gimple_stmt (dump_file, call, 0, 0);
1629 fprintf (dump_file, " Starting walk at: ");
1630 print_gimple_stmt (dump_file, stmt, 0, 0);
1631 fprintf (dump_file, " instance pointer: ");
1632 print_generic_expr (dump_file, otr_object, TDF_SLIM);
1633 fprintf (dump_file, " Outer instance pointer: ");
1634 print_generic_expr (dump_file, instance, TDF_SLIM);
1635 fprintf (dump_file, " offset: %i (bits)", (int)offset);
1636 fprintf (dump_file, " vtbl reference: ");
1637 print_generic_expr (dump_file, instance_ref, TDF_SLIM);
1638 fprintf (dump_file, "\n");
1641 tci.offset = offset;
1642 tci.instance = instance;
1643 tci.vtbl_ptr_ref = instance_ref;
1644 gcc_assert (TREE_CODE (instance) != MEM_REF);
1645 tci.known_current_type = NULL_TREE;
1646 tci.known_current_offset = 0;
1647 tci.otr_type = otr_type;
1648 tci.type_maybe_changed = false;
1649 tci.multiple_types_encountered = false;
1650 tci.speculative = false;
1651 tci.seen_unanalyzed_store = false;
1653 walk_aliased_vdefs (&ao, gimple_vuse (stmt), check_stmt_for_type_change,
1654 &tci, NULL, &function_entry_reached);
1656 /* If we did not find any type changing statements, we may still drop
1657 maybe_in_construction flag if the context already have outer type.
1659 Here we make special assumptions about both constructors and
1660 destructors which are all the functions that are allowed to alter the
1661 VMT pointers. It assumes that destructors begin with assignment into
1662 all VMT pointers and that constructors essentially look in the
1663 following way:
1665 1) The very first thing they do is that they call constructors of
1666 ancestor sub-objects that have them.
1668 2) Then VMT pointers of this and all its ancestors is set to new
1669 values corresponding to the type corresponding to the constructor.
1671 3) Only afterwards, other stuff such as constructor of member
1672 sub-objects and the code written by the user is run. Only this may
1673 include calling virtual functions, directly or indirectly.
1675 4) placement new can not be used to change type of non-POD statically
1676 allocated variables.
1678 There is no way to call a constructor of an ancestor sub-object in any
1679 other way.
1681 This means that we do not have to care whether constructors get the
1682 correct type information because they will always change it (in fact,
1683 if we define the type to be given by the VMT pointer, it is undefined).
1685 The most important fact to derive from the above is that if, for some
1686 statement in the section 3, we try to detect whether the dynamic type
1687 has changed, we can safely ignore all calls as we examine the function
1688 body backwards until we reach statements in section 2 because these
1689 calls cannot be ancestor constructors or destructors (if the input is
1690 not bogus) and so do not change the dynamic type (this holds true only
1691 for automatically allocated objects but at the moment we devirtualize
1692 only these). We then must detect that statements in section 2 change
1693 the dynamic type and can try to derive the new type. That is enough
1694 and we can stop, we will never see the calls into constructors of
1695 sub-objects in this code.
1697 Therefore if the static outer type was found (outer_type)
1698 we can safely ignore tci.speculative that is set on calls and give up
1699 only if there was dyanmic type store that may affect given variable
1700 (seen_unanalyzed_store) */
1702 if (!tci.type_maybe_changed
1703 || (outer_type
1704 && !dynamic
1705 && !tci.seen_unanalyzed_store
1706 && !tci.multiple_types_encountered
1707 && offset == tci.offset
1708 && types_same_for_odr (tci.known_current_type,
1709 outer_type)))
1711 if (!outer_type || tci.seen_unanalyzed_store)
1712 return false;
1713 if (maybe_in_construction)
1714 maybe_in_construction = false;
1715 if (dump_file)
1716 fprintf (dump_file, " No dynamic type change found.\n");
1717 return true;
1720 if (tci.known_current_type
1721 && !function_entry_reached
1722 && !tci.multiple_types_encountered)
1724 if (!tci.speculative)
1726 outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1727 offset = tci.known_current_offset;
1728 dynamic = true;
1729 maybe_in_construction = false;
1730 maybe_derived_type = false;
1731 if (dump_file)
1732 fprintf (dump_file, " Determined dynamic type.\n");
1734 else if (!speculative_outer_type
1735 || speculative_maybe_derived_type)
1737 speculative_outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1738 speculative_offset = tci.known_current_offset;
1739 speculative_maybe_derived_type = false;
1740 if (dump_file)
1741 fprintf (dump_file, " Determined speculative dynamic type.\n");
1744 else if (dump_file)
1746 fprintf (dump_file, " Found multiple types%s%s\n",
1747 function_entry_reached ? " (function entry reached)" : "",
1748 function_entry_reached ? " (multiple types encountered)" : "");
1751 return false;
1754 /* See if speculation given by SPEC_OUTER_TYPE, SPEC_OFFSET and SPEC_MAYBE_DERIVED_TYPE
1755 seems consistent (and useful) with what we already have in the non-speculative context. */
1757 bool
1758 ipa_polymorphic_call_context::speculation_consistent_p (tree spec_outer_type,
1759 HOST_WIDE_INT spec_offset,
1760 bool spec_maybe_derived_type,
1761 tree otr_type) const
1763 if (!flag_devirtualize_speculatively)
1764 return false;
1766 /* Non-polymorphic types are useless for deriving likely polymorphic
1767 call targets. */
1768 if (!spec_outer_type || !contains_polymorphic_type_p (spec_outer_type))
1769 return false;
1771 /* If we know nothing, speculation is always good. */
1772 if (!outer_type)
1773 return true;
1775 /* Speculation is only useful to avoid derived types.
1776 This is not 100% true for placement new, where the outer context may
1777 turn out to be useless, but ignore these for now. */
1778 if (!maybe_derived_type)
1779 return false;
1781 /* If types agrees, speculation is consistent, but it makes sense only
1782 when it says something new. */
1783 if (types_must_be_same_for_odr (spec_outer_type, outer_type))
1784 return maybe_derived_type && !spec_maybe_derived_type;
1786 /* If speculation does not contain the type in question, ignore it. */
1787 if (otr_type
1788 && !contains_type_p (spec_outer_type, spec_offset, otr_type, false, true))
1789 return false;
1791 /* If outer type already contains speculation as a filed,
1792 it is useless. We already know from OUTER_TYPE
1793 SPEC_TYPE and that it is not in the construction. */
1794 if (contains_type_p (outer_type, offset - spec_offset,
1795 spec_outer_type, false, false))
1796 return false;
1798 /* If speculative outer type is not more specified than outer
1799 type, just give up.
1800 We can only decide this safely if we can compare types with OUTER_TYPE.
1802 if ((!in_lto_p || odr_type_p (outer_type))
1803 && !contains_type_p (spec_outer_type,
1804 spec_offset - offset,
1805 outer_type, false))
1806 return false;
1807 return true;
1810 /* Improve THIS with speculation described by NEW_OUTER_TYPE, NEW_OFFSET,
1811 NEW_MAYBE_DERIVED_TYPE
1812 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1814 bool
1815 ipa_polymorphic_call_context::combine_speculation_with
1816 (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1817 tree otr_type)
1819 if (!new_outer_type)
1820 return false;
1822 /* restrict_to_inner_class may eliminate wrong speculation making our job
1823 easeier. */
1824 if (otr_type)
1825 restrict_to_inner_class (otr_type);
1827 if (!speculation_consistent_p (new_outer_type, new_offset,
1828 new_maybe_derived_type, otr_type))
1829 return false;
1831 /* New speculation is a win in case we have no speculation or new
1832 speculation does not consider derivations. */
1833 if (!speculative_outer_type
1834 || (speculative_maybe_derived_type
1835 && !new_maybe_derived_type))
1837 speculative_outer_type = new_outer_type;
1838 speculative_offset = new_offset;
1839 speculative_maybe_derived_type = new_maybe_derived_type;
1840 return true;
1842 else if (types_must_be_same_for_odr (speculative_outer_type,
1843 new_outer_type))
1845 if (speculative_offset != new_offset)
1847 /* OK we have two contexts that seems valid but they disagree,
1848 just give up.
1850 This is not a lattice operation, so we may want to drop it later. */
1851 if (dump_file && (dump_flags & TDF_DETAILS))
1852 fprintf (dump_file,
1853 "Speculative outer types match, "
1854 "offset mismatch -> invalid speculation\n");
1855 clear_speculation ();
1856 return true;
1858 else
1860 if (speculative_maybe_derived_type && !new_maybe_derived_type)
1862 speculative_maybe_derived_type = false;
1863 return true;
1865 else
1866 return false;
1869 /* Choose type that contains the other. This one either contains the outer
1870 as a field (thus giving exactly one target) or is deeper in the type
1871 hiearchy. */
1872 else if (speculative_outer_type
1873 && speculative_maybe_derived_type
1874 && (new_offset > speculative_offset
1875 || (new_offset == speculative_offset
1876 && contains_type_p (new_outer_type,
1877 0, speculative_outer_type, false))))
1879 tree old_outer_type = speculative_outer_type;
1880 HOST_WIDE_INT old_offset = speculative_offset;
1881 bool old_maybe_derived_type = speculative_maybe_derived_type;
1883 speculative_outer_type = new_outer_type;
1884 speculative_offset = new_offset;
1885 speculative_maybe_derived_type = new_maybe_derived_type;
1887 if (otr_type)
1888 restrict_to_inner_class (otr_type);
1890 /* If the speculation turned out to make no sense, revert to sensible
1891 one. */
1892 if (!speculative_outer_type)
1894 speculative_outer_type = old_outer_type;
1895 speculative_offset = old_offset;
1896 speculative_maybe_derived_type = old_maybe_derived_type;
1897 return false;
1899 return (old_offset != speculative_offset
1900 || old_maybe_derived_type != speculative_maybe_derived_type
1901 || types_must_be_same_for_odr (speculative_outer_type,
1902 new_outer_type));
1904 return false;
1907 /* Make speculation less specific so
1908 NEW_OUTER_TYPE, NEW_OFFSET, NEW_MAYBE_DERIVED_TYPE is also included.
1909 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1911 bool
1912 ipa_polymorphic_call_context::meet_speculation_with
1913 (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1914 tree otr_type)
1916 if (!new_outer_type && speculative_outer_type)
1918 clear_speculation ();
1919 return true;
1922 /* restrict_to_inner_class may eliminate wrong speculation making our job
1923 easeier. */
1924 if (otr_type)
1925 restrict_to_inner_class (otr_type);
1927 if (!speculative_outer_type
1928 || !speculation_consistent_p (speculative_outer_type,
1929 speculative_offset,
1930 speculative_maybe_derived_type,
1931 otr_type))
1932 return false;
1934 if (!speculation_consistent_p (new_outer_type, new_offset,
1935 new_maybe_derived_type, otr_type))
1937 clear_speculation ();
1938 return true;
1941 else if (types_must_be_same_for_odr (speculative_outer_type,
1942 new_outer_type))
1944 if (speculative_offset != new_offset)
1946 clear_speculation ();
1947 return true;
1949 else
1951 if (!speculative_maybe_derived_type && new_maybe_derived_type)
1953 speculative_maybe_derived_type = true;
1954 return true;
1956 else
1957 return false;
1960 /* See if one type contains the other as a field (not base). */
1961 else if (contains_type_p (new_outer_type, new_offset - speculative_offset,
1962 speculative_outer_type, false, false))
1963 return false;
1964 else if (contains_type_p (speculative_outer_type,
1965 speculative_offset - new_offset,
1966 new_outer_type, false, false))
1968 speculative_outer_type = new_outer_type;
1969 speculative_offset = new_offset;
1970 speculative_maybe_derived_type = new_maybe_derived_type;
1971 return true;
1973 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
1974 else if (contains_type_p (new_outer_type,
1975 new_offset - speculative_offset,
1976 speculative_outer_type, false, true))
1978 if (!speculative_maybe_derived_type)
1980 speculative_maybe_derived_type = true;
1981 return true;
1983 return false;
1985 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
1986 else if (contains_type_p (speculative_outer_type,
1987 speculative_offset - new_offset, new_outer_type, false, true))
1989 speculative_outer_type = new_outer_type;
1990 speculative_offset = new_offset;
1991 speculative_maybe_derived_type = true;
1992 return true;
1994 else
1996 if (dump_file && (dump_flags & TDF_DETAILS))
1997 fprintf (dump_file, "Giving up on speculative meet\n");
1998 clear_speculation ();
1999 return true;
2003 /* Assume that both THIS and a given context is valid and strenghten THIS
2004 if possible. Return true if any strenghtening was made.
2005 If actual type the context is being used in is known, OTR_TYPE should be
2006 set accordingly. This improves quality of combined result. */
2008 bool
2009 ipa_polymorphic_call_context::combine_with (ipa_polymorphic_call_context ctx,
2010 tree otr_type)
2012 bool updated = false;
2014 if (ctx.useless_p () || invalid)
2015 return false;
2017 /* Restricting context to inner type makes merging easier, however do not
2018 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2019 if (otr_type && !invalid && !ctx.invalid)
2021 restrict_to_inner_class (otr_type);
2022 ctx.restrict_to_inner_class (otr_type);
2023 if(invalid)
2024 return false;
2027 if (dump_file && (dump_flags & TDF_DETAILS))
2029 fprintf (dump_file, "Polymorphic call context combine:");
2030 dump (dump_file);
2031 fprintf (dump_file, "With context: ");
2032 ctx.dump (dump_file);
2033 if (otr_type)
2035 fprintf (dump_file, "To be used with type: ");
2036 print_generic_expr (dump_file, otr_type, TDF_SLIM);
2037 fprintf (dump_file, "\n");
2041 /* If call is known to be invalid, we are done. */
2042 if (ctx.invalid)
2044 if (dump_file && (dump_flags & TDF_DETAILS))
2045 fprintf (dump_file, "-> Invalid context\n");
2046 goto invalidate;
2049 if (!ctx.outer_type)
2051 else if (!outer_type)
2053 outer_type = ctx.outer_type;
2054 offset = ctx.offset;
2055 dynamic = ctx.dynamic;
2056 maybe_in_construction = ctx.maybe_in_construction;
2057 maybe_derived_type = ctx.maybe_derived_type;
2058 updated = true;
2060 /* If types are known to be same, merging is quite easy. */
2061 else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2063 if (offset != ctx.offset
2064 && TYPE_SIZE (outer_type)
2065 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2067 if (dump_file && (dump_flags & TDF_DETAILS))
2068 fprintf (dump_file, "Outer types match, offset mismatch -> invalid\n");
2069 clear_speculation ();
2070 clear_outer_type ();
2071 invalid = true;
2072 return true;
2074 if (dump_file && (dump_flags & TDF_DETAILS))
2075 fprintf (dump_file, "Outer types match, merging flags\n");
2076 if (maybe_in_construction && !ctx.maybe_in_construction)
2078 updated = true;
2079 maybe_in_construction = false;
2081 if (maybe_derived_type && !ctx.maybe_derived_type)
2083 updated = true;
2084 maybe_derived_type = false;
2086 if (dynamic && !ctx.dynamic)
2088 updated = true;
2089 dynamic = false;
2092 /* If we know the type precisely, there is not much to improve. */
2093 else if (!maybe_derived_type && !maybe_in_construction
2094 && !ctx.maybe_derived_type && !ctx.maybe_in_construction)
2096 /* It may be easy to check if second context permits the first
2097 and set INVALID otherwise. This is not easy to do in general;
2098 contains_type_p may return false negatives for non-comparable
2099 types.
2101 If OTR_TYPE is known, we however can expect that
2102 restrict_to_inner_class should have discovered the same base
2103 type. */
2104 if (otr_type && !ctx.maybe_in_construction && !ctx.maybe_derived_type)
2106 if (dump_file && (dump_flags & TDF_DETAILS))
2107 fprintf (dump_file, "Contextes disagree -> invalid\n");
2108 goto invalidate;
2111 /* See if one type contains the other as a field (not base).
2112 In this case we want to choose the wider type, because it contains
2113 more information. */
2114 else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2115 outer_type, false, false))
2117 if (dump_file && (dump_flags & TDF_DETAILS))
2118 fprintf (dump_file, "Second type contain the first as a field\n");
2120 if (maybe_derived_type)
2122 outer_type = ctx.outer_type;
2123 maybe_derived_type = ctx.maybe_derived_type;
2124 offset = ctx.offset;
2125 dynamic = ctx.dynamic;
2126 updated = true;
2129 /* If we do not know how the context is being used, we can
2130 not clear MAYBE_IN_CONSTRUCTION because it may be offseted
2131 to other component of OUTER_TYPE later and we know nothing
2132 about it. */
2133 if (otr_type && maybe_in_construction
2134 && !ctx.maybe_in_construction)
2136 maybe_in_construction = false;
2137 updated = true;
2140 else if (contains_type_p (outer_type, offset - ctx.offset,
2141 ctx.outer_type, false, false))
2143 if (dump_file && (dump_flags & TDF_DETAILS))
2144 fprintf (dump_file, "First type contain the second as a field\n");
2146 if (otr_type && maybe_in_construction
2147 && !ctx.maybe_in_construction)
2149 maybe_in_construction = false;
2150 updated = true;
2153 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2154 else if (contains_type_p (ctx.outer_type,
2155 ctx.offset - offset, outer_type, false, true))
2157 if (dump_file && (dump_flags & TDF_DETAILS))
2158 fprintf (dump_file, "First type is base of second\n");
2159 if (!maybe_derived_type)
2161 if (!ctx.maybe_in_construction
2162 && types_odr_comparable (outer_type, ctx.outer_type))
2164 if (dump_file && (dump_flags & TDF_DETAILS))
2165 fprintf (dump_file, "Second context does not permit base -> invalid\n");
2166 goto invalidate;
2169 /* Pick variant deeper in the hiearchy. */
2170 else
2172 outer_type = ctx.outer_type;
2173 maybe_in_construction = ctx.maybe_in_construction;
2174 maybe_derived_type = ctx.maybe_derived_type;
2175 offset = ctx.offset;
2176 dynamic = ctx.dynamic;
2177 updated = true;
2180 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2181 else if (contains_type_p (outer_type,
2182 offset - ctx.offset, ctx.outer_type, false, true))
2184 if (dump_file && (dump_flags & TDF_DETAILS))
2185 fprintf (dump_file, "Second type is base of first\n");
2186 if (!ctx.maybe_derived_type)
2188 if (!maybe_in_construction
2189 && types_odr_comparable (outer_type, ctx.outer_type))
2191 if (dump_file && (dump_flags & TDF_DETAILS))
2192 fprintf (dump_file, "First context does not permit base -> invalid\n");
2193 goto invalidate;
2195 /* Pick the base type. */
2196 else if (maybe_in_construction)
2198 outer_type = ctx.outer_type;
2199 maybe_in_construction = ctx.maybe_in_construction;
2200 maybe_derived_type = ctx.maybe_derived_type;
2201 offset = ctx.offset;
2202 dynamic = ctx.dynamic;
2203 updated = true;
2207 /* TODO handle merging using hiearchy. */
2208 else if (dump_file && (dump_flags & TDF_DETAILS))
2209 fprintf (dump_file, "Giving up on merge\n");
2211 updated |= combine_speculation_with (ctx.speculative_outer_type,
2212 ctx.speculative_offset,
2213 ctx.speculative_maybe_derived_type,
2214 otr_type);
2216 if (updated && dump_file && (dump_flags & TDF_DETAILS))
2218 fprintf (dump_file, "Updated as: ");
2219 dump (dump_file);
2220 fprintf (dump_file, "\n");
2222 return updated;
2224 invalidate:
2225 invalid = true;
2226 clear_speculation ();
2227 clear_outer_type ();
2228 return true;
2231 /* Take non-speculative info, merge it with speculative and clear speculation.
2232 Used when we no longer manage to keep track of actual outer type, but we
2233 think it is still there.
2235 If OTR_TYPE is set, the transformation can be done more effectively assuming
2236 that context is going to be used only that way. */
2238 void
2239 ipa_polymorphic_call_context::make_speculative (tree otr_type)
2241 tree spec_outer_type = outer_type;
2242 HOST_WIDE_INT spec_offset = offset;
2243 bool spec_maybe_derived_type = maybe_derived_type;
2245 if (invalid)
2247 invalid = false;
2248 clear_outer_type ();
2249 clear_speculation ();
2250 return;
2252 if (!outer_type)
2253 return;
2254 clear_outer_type ();
2255 combine_speculation_with (spec_outer_type, spec_offset,
2256 spec_maybe_derived_type,
2257 otr_type);
2260 /* Use when we can not track dynamic type change. This speculatively assume
2261 type change is not happening. */
2263 void
2264 ipa_polymorphic_call_context::possible_dynamic_type_change (bool in_poly_cdtor,
2265 tree otr_type)
2267 if (dynamic)
2268 make_speculative (otr_type);
2269 else if (in_poly_cdtor)
2270 maybe_in_construction = true;
2273 /* Return TRUE if this context conveys the same information as OTHER. */
2275 bool
2276 ipa_polymorphic_call_context::equal_to
2277 (const ipa_polymorphic_call_context &x) const
2279 if (useless_p ())
2280 return x.useless_p ();
2281 if (invalid)
2282 return x.invalid;
2283 if (x.useless_p () || x.invalid)
2284 return false;
2286 if (outer_type)
2288 if (!x.outer_type
2289 || !types_odr_comparable (outer_type, x.outer_type)
2290 || !types_same_for_odr (outer_type, x.outer_type)
2291 || offset != x.offset
2292 || maybe_in_construction != x.maybe_in_construction
2293 || maybe_derived_type != x.maybe_derived_type
2294 || dynamic != x.dynamic)
2295 return false;
2297 else if (x.outer_type)
2298 return false;
2301 if (speculative_outer_type
2302 && speculation_consistent_p (speculative_outer_type, speculative_offset,
2303 speculative_maybe_derived_type, NULL_TREE))
2305 if (!x.speculative_outer_type)
2306 return false;
2308 if (!types_odr_comparable (speculative_outer_type,
2309 x.speculative_outer_type)
2310 || !types_same_for_odr (speculative_outer_type,
2311 x.speculative_outer_type)
2312 || speculative_offset != x.speculative_offset
2313 || speculative_maybe_derived_type != x.speculative_maybe_derived_type)
2314 return false;
2316 else if (x.speculative_outer_type
2317 && x.speculation_consistent_p (x.speculative_outer_type,
2318 x.speculative_offset,
2319 x.speculative_maybe_derived_type,
2320 NULL))
2321 return false;
2323 return true;
2326 /* Modify context to be strictly less restrictive than CTX. */
2328 bool
2329 ipa_polymorphic_call_context::meet_with (ipa_polymorphic_call_context ctx,
2330 tree otr_type)
2332 bool updated = false;
2334 if (useless_p () || ctx.invalid)
2335 return false;
2337 /* Restricting context to inner type makes merging easier, however do not
2338 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2339 if (otr_type && !useless_p () && !ctx.useless_p ())
2341 restrict_to_inner_class (otr_type);
2342 ctx.restrict_to_inner_class (otr_type);
2343 if(invalid)
2344 return false;
2347 if (equal_to (ctx))
2348 return false;
2350 if (ctx.useless_p () || invalid)
2352 *this = ctx;
2353 return true;
2356 if (dump_file && (dump_flags & TDF_DETAILS))
2358 fprintf (dump_file, "Polymorphic call context meet:");
2359 dump (dump_file);
2360 fprintf (dump_file, "With context: ");
2361 ctx.dump (dump_file);
2362 if (otr_type)
2364 fprintf (dump_file, "To be used with type: ");
2365 print_generic_expr (dump_file, otr_type, TDF_SLIM);
2366 fprintf (dump_file, "\n");
2370 if (!dynamic && ctx.dynamic)
2372 dynamic = true;
2373 updated = true;
2376 /* If call is known to be invalid, we are done. */
2377 if (!outer_type)
2379 else if (!ctx.outer_type)
2381 clear_outer_type ();
2382 updated = true;
2384 /* If types are known to be same, merging is quite easy. */
2385 else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2387 if (offset != ctx.offset
2388 && TYPE_SIZE (outer_type)
2389 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2391 if (dump_file && (dump_flags & TDF_DETAILS))
2392 fprintf (dump_file, "Outer types match, offset mismatch -> clearing\n");
2393 clear_outer_type ();
2394 return true;
2396 if (dump_file && (dump_flags & TDF_DETAILS))
2397 fprintf (dump_file, "Outer types match, merging flags\n");
2398 if (!maybe_in_construction && ctx.maybe_in_construction)
2400 updated = true;
2401 maybe_in_construction = true;
2403 if (!maybe_derived_type && ctx.maybe_derived_type)
2405 updated = true;
2406 maybe_derived_type = true;
2408 if (!dynamic && ctx.dynamic)
2410 updated = true;
2411 dynamic = true;
2414 /* See if one type contains the other as a field (not base). */
2415 else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2416 outer_type, false, false))
2418 if (dump_file && (dump_flags & TDF_DETAILS))
2419 fprintf (dump_file, "Second type contain the first as a field\n");
2421 /* The second type is more specified, so we keep the first.
2422 We need to set DYNAMIC flag to avoid declaring context INVALID
2423 of OFFSET ends up being out of range. */
2424 if (!dynamic
2425 && (ctx.dynamic
2426 || (!otr_type
2427 && (!TYPE_SIZE (ctx.outer_type)
2428 || !TYPE_SIZE (outer_type)
2429 || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2430 TYPE_SIZE (outer_type), 0)))))
2432 dynamic = true;
2433 updated = true;
2436 else if (contains_type_p (outer_type, offset - ctx.offset,
2437 ctx.outer_type, false, false))
2439 if (dump_file && (dump_flags & TDF_DETAILS))
2440 fprintf (dump_file, "First type contain the second as a field\n");
2442 if (!dynamic
2443 && (ctx.dynamic
2444 || (!otr_type
2445 && (!TYPE_SIZE (ctx.outer_type)
2446 || !TYPE_SIZE (outer_type)
2447 || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2448 TYPE_SIZE (outer_type), 0)))))
2449 dynamic = true;
2450 outer_type = ctx.outer_type;
2451 offset = ctx.offset;
2452 dynamic = ctx.dynamic;
2453 maybe_in_construction = ctx.maybe_in_construction;
2454 maybe_derived_type = ctx.maybe_derived_type;
2455 updated = true;
2457 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2458 else if (contains_type_p (ctx.outer_type,
2459 ctx.offset - offset, outer_type, false, true))
2461 if (dump_file && (dump_flags & TDF_DETAILS))
2462 fprintf (dump_file, "First type is base of second\n");
2463 if (!maybe_derived_type)
2465 maybe_derived_type = true;
2466 updated = true;
2468 if (!maybe_in_construction && ctx.maybe_in_construction)
2470 maybe_in_construction = true;
2471 updated = true;
2473 if (!dynamic && ctx.dynamic)
2475 dynamic = true;
2476 updated = true;
2479 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2480 else if (contains_type_p (outer_type,
2481 offset - ctx.offset, ctx.outer_type, false, true))
2483 if (dump_file && (dump_flags & TDF_DETAILS))
2484 fprintf (dump_file, "Second type is base of first\n");
2485 outer_type = ctx.outer_type;
2486 offset = ctx.offset;
2487 updated = true;
2488 if (!maybe_derived_type)
2489 maybe_derived_type = true;
2490 if (!maybe_in_construction && ctx.maybe_in_construction)
2491 maybe_in_construction = true;
2492 if (!dynamic && ctx.dynamic)
2493 dynamic = true;
2495 /* TODO handle merging using hiearchy. */
2496 else
2498 if (dump_file && (dump_flags & TDF_DETAILS))
2499 fprintf (dump_file, "Giving up on meet\n");
2500 clear_outer_type ();
2501 updated = true;
2504 updated |= meet_speculation_with (ctx.speculative_outer_type,
2505 ctx.speculative_offset,
2506 ctx.speculative_maybe_derived_type,
2507 otr_type);
2509 if (updated && dump_file && (dump_flags & TDF_DETAILS))
2511 fprintf (dump_file, "Updated as: ");
2512 dump (dump_file);
2513 fprintf (dump_file, "\n");
2515 return updated;