* gcc.dg/vect/slp-9.c: Disable einline.
[official-gcc.git] / gcc / ipa-polymorphic-call.c
blob75820a99934e575634323b0e6166e394b0404977
1 /* Analysis of polymorphic call context.
2 Copyright (C) 2013-2014 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "print-tree.h"
27 #include "calls.h"
28 #include "expr.h"
29 #include "tree-pass.h"
30 #include "hash-set.h"
31 #include "target.h"
32 #include "hash-table.h"
33 #include "inchash.h"
34 #include "tree-pretty-print.h"
35 #include "predict.h"
36 #include "basic-block.h"
37 #include "hash-map.h"
38 #include "is-a.h"
39 #include "plugin-api.h"
40 #include "vec.h"
41 #include "hashtab.h"
42 #include "machmode.h"
43 #include "hard-reg-set.h"
44 #include "input.h"
45 #include "function.h"
46 #include "ipa-ref.h"
47 #include "cgraph.h"
48 #include "ipa-utils.h"
49 #include "tree-ssa-alias.h"
50 #include "internal-fn.h"
51 #include "gimple-fold.h"
52 #include "gimple-expr.h"
53 #include "gimple.h"
54 #include "alloc-pool.h"
55 #include "symbol-summary.h"
56 #include "ipa-prop.h"
57 #include "ipa-inline.h"
58 #include "diagnostic.h"
59 #include "tree-dfa.h"
60 #include "demangle.h"
61 #include "dbgcnt.h"
62 #include "gimple-pretty-print.h"
63 #include "stor-layout.h"
64 #include "intl.h"
65 #include "data-streamer.h"
66 #include "lto-streamer.h"
67 #include "streamer-hooks.h"
69 /* Return true when TYPE contains an polymorphic type and thus is interesting
70 for devirtualization machinery. */
72 static bool contains_type_p (tree, HOST_WIDE_INT, tree,
73 bool consider_placement_new = true,
74 bool consider_bases = true);
76 bool
77 contains_polymorphic_type_p (const_tree type)
79 type = TYPE_MAIN_VARIANT (type);
81 if (RECORD_OR_UNION_TYPE_P (type))
83 if (TYPE_BINFO (type)
84 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
85 return true;
86 for (tree fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
87 if (TREE_CODE (fld) == FIELD_DECL
88 && !DECL_ARTIFICIAL (fld)
89 && contains_polymorphic_type_p (TREE_TYPE (fld)))
90 return true;
91 return false;
93 if (TREE_CODE (type) == ARRAY_TYPE)
94 return contains_polymorphic_type_p (TREE_TYPE (type));
95 return false;
98 /* Return true if it seems valid to use placement new to build EXPECTED_TYPE
99 at possition CUR_OFFSET within TYPE.
101 POD can be changed to an instance of a polymorphic type by
102 placement new. Here we play safe and assume that any
103 non-polymorphic type is POD. */
104 bool
105 possible_placement_new (tree type, tree expected_type,
106 HOST_WIDE_INT cur_offset)
108 return ((TREE_CODE (type) != RECORD_TYPE
109 || !TYPE_BINFO (type)
110 || cur_offset >= POINTER_SIZE
111 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))
112 && (!TYPE_SIZE (type)
113 || !tree_fits_shwi_p (TYPE_SIZE (type))
114 || (cur_offset
115 + (expected_type ? tree_to_uhwi (TYPE_SIZE (expected_type))
116 : POINTER_SIZE)
117 <= tree_to_uhwi (TYPE_SIZE (type)))));
120 /* THIS->OUTER_TYPE is a type of memory object where object of OTR_TYPE
121 is contained at THIS->OFFSET. Walk the memory representation of
122 THIS->OUTER_TYPE and find the outermost class type that match
123 OTR_TYPE or contain OTR_TYPE as a base. Update THIS
124 to represent it.
126 If OTR_TYPE is NULL, just find outermost polymorphic type with
127 virtual table present at possition OFFSET.
129 For example when THIS represents type
130 class A
132 int a;
133 class B b;
135 and we look for type at offset sizeof(int), we end up with B and offset 0.
136 If the same is produced by multiple inheritance, we end up with A and offset
137 sizeof(int).
139 If we can not find corresponding class, give up by setting
140 THIS->OUTER_TYPE to OTR_TYPE and THIS->OFFSET to NULL.
141 Return true when lookup was sucesful.
143 When CONSIDER_PLACEMENT_NEW is false, reject contexts that may be made
144 valid only via alocation of new polymorphic type inside by means
145 of placement new.
147 When CONSIDER_BASES is false, only look for actual fields, not base types
148 of TYPE. */
150 bool
151 ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type,
152 bool consider_placement_new,
153 bool consider_bases)
155 tree type = outer_type;
156 HOST_WIDE_INT cur_offset = offset;
157 bool speculative = false;
158 bool size_unknown = false;
159 unsigned HOST_WIDE_INT otr_type_size = POINTER_SIZE;
161 /* Update OUTER_TYPE to match EXPECTED_TYPE if it is not set. */
162 if (!outer_type)
164 clear_outer_type (otr_type);
165 type = otr_type;
166 cur_offset = 0;
168 /* See if OFFSET points inside OUTER_TYPE. If it does not, we know
169 that the context is either invalid, or the instance type must be
170 derived from OUTER_TYPE.
172 Because the instance type may contain field whose type is of OUTER_TYPE,
173 we can not derive any effective information about it.
175 TODO: In the case we know all derrived types, we can definitely do better
176 here. */
177 else if (TYPE_SIZE (outer_type)
178 && tree_fits_shwi_p (TYPE_SIZE (outer_type))
179 && tree_to_shwi (TYPE_SIZE (outer_type)) >= 0
180 && tree_to_shwi (TYPE_SIZE (outer_type)) <= offset)
182 clear_outer_type (otr_type);
183 type = otr_type;
184 cur_offset = 0;
186 /* If derived type is not allowed, we know that the context is invalid.
187 For dynamic types, we really do not have information about
188 size of the memory location. It is possible that completely
189 different type is stored after outer_type. */
190 if (!maybe_derived_type && !dynamic)
192 clear_speculation ();
193 invalid = true;
194 return false;
198 if (otr_type && TYPE_SIZE (otr_type)
199 && tree_fits_shwi_p (TYPE_SIZE (otr_type)))
200 otr_type_size = tree_to_uhwi (TYPE_SIZE (otr_type));
202 if (!type || offset < 0)
203 goto no_useful_type_info;
205 /* Find the sub-object the constant actually refers to and mark whether it is
206 an artificial one (as opposed to a user-defined one).
208 This loop is performed twice; first time for outer_type and second time
209 for speculative_outer_type. The second run has SPECULATIVE set. */
210 while (true)
212 unsigned HOST_WIDE_INT pos, size;
213 tree fld;
215 /* If we do not know size of TYPE, we need to be more conservative
216 about accepting cases where we can not find EXPECTED_TYPE.
217 Generally the types that do matter here are of constant size.
218 Size_unknown case should be very rare. */
219 if (TYPE_SIZE (type)
220 && tree_fits_shwi_p (TYPE_SIZE (type))
221 && tree_to_shwi (TYPE_SIZE (type)) >= 0)
222 size_unknown = false;
223 else
224 size_unknown = true;
226 /* On a match, just return what we found. */
227 if ((otr_type
228 && types_odr_comparable (type, otr_type)
229 && types_same_for_odr (type, otr_type))
230 || (!otr_type
231 && TREE_CODE (type) == RECORD_TYPE
232 && TYPE_BINFO (type)
233 && polymorphic_type_binfo_p (TYPE_BINFO (type))))
235 if (speculative)
237 /* If we did not match the offset, just give up on speculation. */
238 if (cur_offset != 0
239 /* Also check if speculation did not end up being same as
240 non-speculation. */
241 || (types_must_be_same_for_odr (speculative_outer_type,
242 outer_type)
243 && (maybe_derived_type
244 == speculative_maybe_derived_type)))
245 clear_speculation ();
246 return true;
248 else
250 /* If type is known to be final, do not worry about derived
251 types. Testing it here may help us to avoid speculation. */
252 if (otr_type && TREE_CODE (outer_type) == RECORD_TYPE
253 && (!in_lto_p || odr_type_p (outer_type))
254 && type_known_to_have_no_deriavations_p (outer_type))
255 maybe_derived_type = false;
257 /* Type can not contain itself on an non-zero offset. In that case
258 just give up. Still accept the case where size is now known.
259 Either the second copy may appear past the end of type or within
260 the non-POD buffer located inside the variably sized type
261 itself. */
262 if (cur_offset != 0)
263 goto no_useful_type_info;
264 /* If we determined type precisely or we have no clue on
265 speuclation, we are done. */
266 if (!maybe_derived_type || !speculative_outer_type
267 || !speculation_consistent_p (speculative_outer_type,
268 speculative_offset,
269 speculative_maybe_derived_type,
270 otr_type))
272 clear_speculation ();
273 return true;
275 /* Otherwise look into speculation now. */
276 else
278 speculative = true;
279 type = speculative_outer_type;
280 cur_offset = speculative_offset;
281 continue;
286 /* Walk fields and find corresponding on at OFFSET. */
287 if (TREE_CODE (type) == RECORD_TYPE)
289 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
291 if (TREE_CODE (fld) != FIELD_DECL)
292 continue;
294 pos = int_bit_position (fld);
295 if (pos > (unsigned HOST_WIDE_INT)cur_offset)
296 continue;
298 /* Do not consider vptr itself. Not even for placement new. */
299 if (!pos && DECL_ARTIFICIAL (fld)
300 && POINTER_TYPE_P (TREE_TYPE (fld))
301 && TYPE_BINFO (type)
302 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
303 continue;
305 if (!DECL_SIZE (fld) || !tree_fits_uhwi_p (DECL_SIZE (fld)))
306 goto no_useful_type_info;
307 size = tree_to_uhwi (DECL_SIZE (fld));
309 /* We can always skip types smaller than pointer size:
310 those can not contain a virtual table pointer.
312 Disqualifying fields that are too small to fit OTR_TYPE
313 saves work needed to walk them for no benefit.
314 Because of the way the bases are packed into a class, the
315 field's size may be smaller than type size, so it needs
316 to be done with a care. */
318 if (pos <= (unsigned HOST_WIDE_INT)cur_offset
319 && (pos + size) >= (unsigned HOST_WIDE_INT)cur_offset
320 + POINTER_SIZE
321 && (!otr_type
322 || !TYPE_SIZE (TREE_TYPE (fld))
323 || !tree_fits_shwi_p (TYPE_SIZE (TREE_TYPE (fld)))
324 || (pos + tree_to_uhwi (TYPE_SIZE (TREE_TYPE (fld))))
325 >= cur_offset + otr_type_size))
326 break;
329 if (!fld)
330 goto no_useful_type_info;
332 type = TYPE_MAIN_VARIANT (TREE_TYPE (fld));
333 cur_offset -= pos;
334 /* DECL_ARTIFICIAL represents a basetype. */
335 if (!DECL_ARTIFICIAL (fld))
337 if (!speculative)
339 outer_type = type;
340 offset = cur_offset;
341 /* As soon as we se an field containing the type,
342 we know we are not looking for derivations. */
343 maybe_derived_type = false;
345 else
347 speculative_outer_type = type;
348 speculative_offset = cur_offset;
349 speculative_maybe_derived_type = false;
352 else if (!consider_bases)
353 goto no_useful_type_info;
355 else if (TREE_CODE (type) == ARRAY_TYPE)
357 tree subtype = TYPE_MAIN_VARIANT (TREE_TYPE (type));
359 /* Give up if we don't know array field size.
360 Also give up on non-polymorphic types as they are used
361 as buffers for placement new. */
362 if (!TYPE_SIZE (subtype)
363 || !tree_fits_shwi_p (TYPE_SIZE (subtype))
364 || tree_to_shwi (TYPE_SIZE (subtype)) <= 0
365 || !contains_polymorphic_type_p (subtype))
366 goto no_useful_type_info;
368 HOST_WIDE_INT new_offset = cur_offset % tree_to_shwi (TYPE_SIZE (subtype));
370 /* We may see buffer for placement new. In this case the expected type
371 can be bigger than the subtype. */
372 if (TYPE_SIZE (subtype)
373 && (cur_offset + otr_type_size
374 > tree_to_uhwi (TYPE_SIZE (subtype))))
375 goto no_useful_type_info;
377 cur_offset = new_offset;
378 type = subtype;
379 if (!speculative)
381 outer_type = type;
382 offset = cur_offset;
383 maybe_derived_type = false;
385 else
387 speculative_outer_type = type;
388 speculative_offset = cur_offset;
389 speculative_maybe_derived_type = false;
392 /* Give up on anything else. */
393 else
395 no_useful_type_info:
396 if (maybe_derived_type && !speculative
397 && TREE_CODE (outer_type) == RECORD_TYPE
398 && TREE_CODE (otr_type) == RECORD_TYPE
399 && TYPE_BINFO (otr_type)
400 && !offset
401 && get_binfo_at_offset (TYPE_BINFO (otr_type), 0, outer_type))
403 clear_outer_type (otr_type);
404 if (!speculative_outer_type
405 || !speculation_consistent_p (speculative_outer_type,
406 speculative_offset,
407 speculative_maybe_derived_type,
408 otr_type))
409 clear_speculation ();
410 if (speculative_outer_type)
412 speculative = true;
413 type = speculative_outer_type;
414 cur_offset = speculative_offset;
416 else
417 return true;
419 /* We found no way to embedd EXPECTED_TYPE in TYPE.
420 We still permit two special cases - placement new and
421 the case of variadic types containing themselves. */
422 if (!speculative
423 && consider_placement_new
424 && (size_unknown || !type || maybe_derived_type
425 || possible_placement_new (type, otr_type, cur_offset)))
427 /* In these weird cases we want to accept the context.
428 In non-speculative run we have no useful outer_type info
429 (TODO: we may eventually want to record upper bound on the
430 type size that can be used to prune the walk),
431 but we still want to consider speculation that may
432 give useful info. */
433 if (!speculative)
435 clear_outer_type (otr_type);
436 if (!speculative_outer_type
437 || !speculation_consistent_p (speculative_outer_type,
438 speculative_offset,
439 speculative_maybe_derived_type,
440 otr_type))
441 clear_speculation ();
442 if (speculative_outer_type)
444 speculative = true;
445 type = speculative_outer_type;
446 cur_offset = speculative_offset;
448 else
449 return true;
451 else
452 clear_speculation ();
453 return true;
455 else
457 clear_speculation ();
458 if (speculative)
459 return true;
460 clear_outer_type (otr_type);
461 invalid = true;
462 return false;
468 /* Return true if OUTER_TYPE contains OTR_TYPE at OFFSET.
469 CONSIDER_PLACEMENT_NEW makes function to accept cases where OTR_TYPE can
470 be built within OUTER_TYPE by means of placement new. CONSIDER_BASES makes
471 function to accept cases where OTR_TYPE appears as base of OUTER_TYPE or as
472 base of one of fields of OUTER_TYPE. */
474 static bool
475 contains_type_p (tree outer_type, HOST_WIDE_INT offset,
476 tree otr_type,
477 bool consider_placement_new,
478 bool consider_bases)
480 ipa_polymorphic_call_context context;
482 /* Check that type is within range. */
483 if (offset < 0)
484 return false;
485 if (TYPE_SIZE (outer_type) && TYPE_SIZE (otr_type)
486 && TREE_CODE (outer_type) == INTEGER_CST
487 && TREE_CODE (otr_type) == INTEGER_CST
488 && wi::ltu_p (wi::to_offset (outer_type), (wi::to_offset (otr_type) + offset)))
489 return false;
491 context.offset = offset;
492 context.outer_type = TYPE_MAIN_VARIANT (outer_type);
493 context.maybe_derived_type = false;
494 return context.restrict_to_inner_class (otr_type, consider_placement_new, consider_bases);
498 /* We know that the instance is stored in variable or parameter
499 (not dynamically allocated) and we want to disprove the fact
500 that it may be in construction at invocation of CALL.
502 BASE represents memory location where instance is stored.
503 If BASE is NULL, it is assumed to be global memory.
504 OUTER_TYPE is known type of the instance or NULL if not
505 known.
507 For the variable to be in construction we actually need to
508 be in constructor of corresponding global variable or
509 the inline stack of CALL must contain the constructor.
510 Check this condition. This check works safely only before
511 IPA passes, because inline stacks may become out of date
512 later. */
514 bool
515 decl_maybe_in_construction_p (tree base, tree outer_type,
516 gimple call, tree function)
518 if (outer_type)
519 outer_type = TYPE_MAIN_VARIANT (outer_type);
520 gcc_assert (!base || DECL_P (base));
522 /* After inlining the code unification optimizations may invalidate
523 inline stacks. Also we need to give up on global variables after
524 IPA, because addresses of these may have been propagated to their
525 constructors. */
526 if (DECL_STRUCT_FUNCTION (function)->after_inlining)
527 return true;
529 /* Pure functions can not do any changes on the dynamic type;
530 that require writting to memory. */
531 if ((!base || !auto_var_in_fn_p (base, function))
532 && flags_from_decl_or_type (function) & (ECF_PURE | ECF_CONST))
533 return false;
535 for (tree block = gimple_block (call); block && TREE_CODE (block) == BLOCK;
536 block = BLOCK_SUPERCONTEXT (block))
537 if (BLOCK_ABSTRACT_ORIGIN (block)
538 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block)) == FUNCTION_DECL)
540 tree fn = BLOCK_ABSTRACT_ORIGIN (block);
542 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
543 || (!DECL_CXX_CONSTRUCTOR_P (fn)
544 && !DECL_CXX_DESTRUCTOR_P (fn)))
546 /* Watch for clones where we constant propagated the first
547 argument (pointer to the instance). */
548 fn = DECL_ABSTRACT_ORIGIN (fn);
549 if (!fn
550 || (base && !is_global_var (base))
551 || TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
552 || (!DECL_CXX_CONSTRUCTOR_P (fn)
553 && !DECL_CXX_DESTRUCTOR_P (fn)))
554 continue;
556 if (flags_from_decl_or_type (fn) & (ECF_PURE | ECF_CONST))
557 continue;
559 tree type = TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (fn)));
561 if (!outer_type || !types_odr_comparable (type, outer_type))
563 if (TREE_CODE (type) == RECORD_TYPE
564 && TYPE_BINFO (type)
565 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
566 return true;
568 else if (types_same_for_odr (type, outer_type))
569 return true;
572 if (!base || (TREE_CODE (base) == VAR_DECL && is_global_var (base)))
574 if (TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
575 || (!DECL_CXX_CONSTRUCTOR_P (function)
576 && !DECL_CXX_DESTRUCTOR_P (function)))
578 if (!DECL_ABSTRACT_ORIGIN (function))
579 return false;
580 /* Watch for clones where we constant propagated the first
581 argument (pointer to the instance). */
582 function = DECL_ABSTRACT_ORIGIN (function);
583 if (!function
584 || TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
585 || (!DECL_CXX_CONSTRUCTOR_P (function)
586 && !DECL_CXX_DESTRUCTOR_P (function)))
587 return false;
589 tree type = TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (function)));
590 if (!outer_type || !types_odr_comparable (type, outer_type))
592 if (TREE_CODE (type) == RECORD_TYPE
593 && TYPE_BINFO (type)
594 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
595 return true;
597 else if (types_same_for_odr (type, outer_type))
598 return true;
600 return false;
603 /* Dump human readable context to F. If NEWLINE is true, it will be terminated
604 by a newline. */
606 void
607 ipa_polymorphic_call_context::dump (FILE *f, bool newline) const
609 fprintf (f, " ");
610 if (invalid)
611 fprintf (f, "Call is known to be undefined");
612 else
614 if (useless_p ())
615 fprintf (f, "nothing known");
616 if (outer_type || offset)
618 fprintf (f, "Outer type%s:", dynamic ? " (dynamic)":"");
619 print_generic_expr (f, outer_type, TDF_SLIM);
620 if (maybe_derived_type)
621 fprintf (f, " (or a derived type)");
622 if (maybe_in_construction)
623 fprintf (f, " (maybe in construction)");
624 fprintf (f, " offset "HOST_WIDE_INT_PRINT_DEC,
625 offset);
627 if (speculative_outer_type)
629 if (outer_type || offset)
630 fprintf (f, " ");
631 fprintf (f, "Speculative outer type:");
632 print_generic_expr (f, speculative_outer_type, TDF_SLIM);
633 if (speculative_maybe_derived_type)
634 fprintf (f, " (or a derived type)");
635 fprintf (f, " at offset "HOST_WIDE_INT_PRINT_DEC,
636 speculative_offset);
639 if (newline)
640 fprintf(f, "\n");
643 /* Print context to stderr. */
645 void
646 ipa_polymorphic_call_context::debug () const
648 dump (stderr);
651 /* Stream out the context to OB. */
653 void
654 ipa_polymorphic_call_context::stream_out (struct output_block *ob) const
656 struct bitpack_d bp = bitpack_create (ob->main_stream);
658 bp_pack_value (&bp, invalid, 1);
659 bp_pack_value (&bp, maybe_in_construction, 1);
660 bp_pack_value (&bp, maybe_derived_type, 1);
661 bp_pack_value (&bp, speculative_maybe_derived_type, 1);
662 bp_pack_value (&bp, dynamic, 1);
663 bp_pack_value (&bp, outer_type != NULL, 1);
664 bp_pack_value (&bp, offset != 0, 1);
665 bp_pack_value (&bp, speculative_outer_type != NULL, 1);
666 streamer_write_bitpack (&bp);
668 if (outer_type != NULL)
669 stream_write_tree (ob, outer_type, true);
670 if (offset)
671 streamer_write_hwi (ob, offset);
672 if (speculative_outer_type != NULL)
674 stream_write_tree (ob, speculative_outer_type, true);
675 streamer_write_hwi (ob, speculative_offset);
677 else
678 gcc_assert (!speculative_offset);
681 /* Stream in the context from IB and DATA_IN. */
683 void
684 ipa_polymorphic_call_context::stream_in (struct lto_input_block *ib,
685 struct data_in *data_in)
687 struct bitpack_d bp = streamer_read_bitpack (ib);
689 invalid = bp_unpack_value (&bp, 1);
690 maybe_in_construction = bp_unpack_value (&bp, 1);
691 maybe_derived_type = bp_unpack_value (&bp, 1);
692 speculative_maybe_derived_type = bp_unpack_value (&bp, 1);
693 dynamic = bp_unpack_value (&bp, 1);
694 bool outer_type_p = bp_unpack_value (&bp, 1);
695 bool offset_p = bp_unpack_value (&bp, 1);
696 bool speculative_outer_type_p = bp_unpack_value (&bp, 1);
698 if (outer_type_p)
699 outer_type = stream_read_tree (ib, data_in);
700 else
701 outer_type = NULL;
702 if (offset_p)
703 offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
704 else
705 offset = 0;
706 if (speculative_outer_type_p)
708 speculative_outer_type = stream_read_tree (ib, data_in);
709 speculative_offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
711 else
713 speculative_outer_type = NULL;
714 speculative_offset = 0;
718 /* Proudce polymorphic call context for call method of instance
719 that is located within BASE (that is assumed to be a decl) at offset OFF. */
721 void
722 ipa_polymorphic_call_context::set_by_decl (tree base, HOST_WIDE_INT off)
724 gcc_assert (DECL_P (base));
725 clear_speculation ();
727 if (!contains_polymorphic_type_p (TREE_TYPE (base)))
729 clear_outer_type ();
730 offset = off;
731 return;
733 outer_type = TYPE_MAIN_VARIANT (TREE_TYPE (base));
734 offset = off;
735 /* Make very conservative assumption that all objects
736 may be in construction.
738 It is up to caller to revisit this via
739 get_dynamic_type or decl_maybe_in_construction_p. */
740 maybe_in_construction = true;
741 maybe_derived_type = false;
742 dynamic = false;
745 /* CST is an invariant (address of decl), try to get meaningful
746 polymorphic call context for polymorphic call of method
747 if instance of OTR_TYPE that is located at offset OFF of this invariant.
748 Return FALSE if nothing meaningful can be found. */
750 bool
751 ipa_polymorphic_call_context::set_by_invariant (tree cst,
752 tree otr_type,
753 HOST_WIDE_INT off)
755 HOST_WIDE_INT offset2, size, max_size;
756 tree base;
758 invalid = false;
759 off = 0;
760 clear_outer_type (otr_type);
762 if (TREE_CODE (cst) != ADDR_EXPR)
763 return false;
765 cst = TREE_OPERAND (cst, 0);
766 base = get_ref_base_and_extent (cst, &offset2, &size, &max_size);
767 if (!DECL_P (base) || max_size == -1 || max_size != size)
768 return false;
770 /* Only type inconsistent programs can have otr_type that is
771 not part of outer type. */
772 if (otr_type && !contains_type_p (TREE_TYPE (base), off, otr_type))
773 return false;
775 set_by_decl (base, off);
776 return true;
779 /* See if OP is SSA name initialized as a copy or by single assignment.
780 If so, walk the SSA graph up. Because simple PHI conditional is considered
781 copy, GLOBAL_VISITED may be used to avoid infinite loop walking the SSA
782 graph. */
784 static tree
785 walk_ssa_copies (tree op, hash_set<tree> **global_visited = NULL)
787 hash_set <tree> *visited = NULL;
788 STRIP_NOPS (op);
789 while (TREE_CODE (op) == SSA_NAME
790 && !SSA_NAME_IS_DEFAULT_DEF (op)
791 && SSA_NAME_DEF_STMT (op)
792 && (gimple_assign_single_p (SSA_NAME_DEF_STMT (op))
793 || gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI))
795 if (global_visited)
797 if (!*global_visited)
798 *global_visited = new hash_set<tree>;
799 if ((*global_visited)->add (op))
800 goto done;
802 else
804 if (!visited)
805 visited = new hash_set<tree>;
806 if (visited->add (op))
807 goto done;
809 /* Special case
810 if (ptr == 0)
811 ptr = 0;
812 else
813 ptr = ptr.foo;
814 This pattern is implicitly produced for casts to non-primary
815 bases. When doing context analysis, we do not really care
816 about the case pointer is NULL, becuase the call will be
817 undefined anyway. */
818 if (gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI)
820 gimple phi = SSA_NAME_DEF_STMT (op);
822 if (gimple_phi_num_args (phi) > 2)
823 goto done;
824 if (gimple_phi_num_args (phi) == 1)
825 op = gimple_phi_arg_def (phi, 0);
826 else if (integer_zerop (gimple_phi_arg_def (phi, 0)))
827 op = gimple_phi_arg_def (phi, 1);
828 else if (integer_zerop (gimple_phi_arg_def (phi, 1)))
829 op = gimple_phi_arg_def (phi, 0);
830 else
831 goto done;
833 else
835 if (gimple_assign_load_p (SSA_NAME_DEF_STMT (op)))
836 goto done;
837 op = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op));
839 STRIP_NOPS (op);
841 done:
842 if (visited)
843 delete (visited);
844 return op;
847 /* Create polymorphic call context from IP invariant CST.
848 This is typically &global_var.
849 OTR_TYPE specify type of polymorphic call or NULL if unknown, OFF
850 is offset of call. */
852 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree cst,
853 tree otr_type,
854 HOST_WIDE_INT off)
856 clear_speculation ();
857 set_by_invariant (cst, otr_type, off);
860 /* Build context for pointer REF contained in FNDECL at statement STMT.
861 if INSTANCE is non-NULL, return pointer to the object described by
862 the context or DECL where context is contained in. */
864 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl,
865 tree ref,
866 gimple stmt,
867 tree *instance)
869 tree otr_type = NULL;
870 tree base_pointer;
871 hash_set <tree> *visited = NULL;
873 if (TREE_CODE (ref) == OBJ_TYPE_REF)
875 otr_type = obj_type_ref_class (ref);
876 base_pointer = OBJ_TYPE_REF_OBJECT (ref);
878 else
879 base_pointer = ref;
881 /* Set up basic info in case we find nothing interesting in the analysis. */
882 clear_speculation ();
883 clear_outer_type (otr_type);
884 invalid = false;
886 /* Walk SSA for outer object. */
887 while (true)
889 base_pointer = walk_ssa_copies (base_pointer, &visited);
890 if (TREE_CODE (base_pointer) == ADDR_EXPR)
892 HOST_WIDE_INT size, max_size;
893 HOST_WIDE_INT offset2;
894 tree base = get_ref_base_and_extent (TREE_OPERAND (base_pointer, 0),
895 &offset2, &size, &max_size);
897 if (max_size != -1 && max_size == size)
898 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base)),
899 offset + offset2,
900 true,
901 NULL /* Do not change outer type. */);
903 /* If this is a varying address, punt. */
904 if ((TREE_CODE (base) == MEM_REF || DECL_P (base))
905 && max_size != -1
906 && max_size == size)
908 /* We found dereference of a pointer. Type of the pointer
909 and MEM_REF is meaningless, but we can look futher. */
910 if (TREE_CODE (base) == MEM_REF)
912 base_pointer = TREE_OPERAND (base, 0);
913 offset
914 += offset2 + mem_ref_offset (base).to_short_addr () * BITS_PER_UNIT;
915 outer_type = NULL;
917 /* We found base object. In this case the outer_type
918 is known. */
919 else if (DECL_P (base))
921 if (visited)
922 delete (visited);
923 /* Only type inconsistent programs can have otr_type that is
924 not part of outer type. */
925 if (otr_type
926 && !contains_type_p (TREE_TYPE (base),
927 offset + offset2, otr_type))
929 invalid = true;
930 if (instance)
931 *instance = base_pointer;
932 return;
934 set_by_decl (base, offset + offset2);
935 if (outer_type && maybe_in_construction && stmt)
936 maybe_in_construction
937 = decl_maybe_in_construction_p (base,
938 outer_type,
939 stmt,
940 fndecl);
941 if (instance)
942 *instance = base;
943 return;
945 else
946 break;
948 else
949 break;
951 else if (TREE_CODE (base_pointer) == POINTER_PLUS_EXPR
952 && tree_fits_uhwi_p (TREE_OPERAND (base_pointer, 1)))
954 offset += tree_to_shwi (TREE_OPERAND (base_pointer, 1))
955 * BITS_PER_UNIT;
956 base_pointer = TREE_OPERAND (base_pointer, 0);
958 else
959 break;
962 if (visited)
963 delete (visited);
965 /* Try to determine type of the outer object. */
966 if (TREE_CODE (base_pointer) == SSA_NAME
967 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
968 && TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL)
970 /* See if parameter is THIS pointer of a method. */
971 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE
972 && SSA_NAME_VAR (base_pointer) == DECL_ARGUMENTS (fndecl))
974 outer_type
975 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
976 gcc_assert (TREE_CODE (outer_type) == RECORD_TYPE
977 || TREE_CODE (outer_type) == UNION_TYPE);
979 /* Dynamic casting has possibly upcasted the type
980 in the hiearchy. In this case outer type is less
981 informative than inner type and we should forget
982 about it. */
983 if ((otr_type
984 && !contains_type_p (outer_type, offset,
985 otr_type))
986 || !contains_polymorphic_type_p (outer_type))
988 outer_type = NULL;
989 if (instance)
990 *instance = base_pointer;
991 return;
994 dynamic = true;
996 /* If the function is constructor or destructor, then
997 the type is possibly in construction, but we know
998 it is not derived type. */
999 if (DECL_CXX_CONSTRUCTOR_P (fndecl)
1000 || DECL_CXX_DESTRUCTOR_P (fndecl))
1002 maybe_in_construction = true;
1003 maybe_derived_type = false;
1005 else
1007 maybe_derived_type = true;
1008 maybe_in_construction = false;
1010 if (instance)
1011 *instance = base_pointer;
1012 return;
1014 /* Non-PODs passed by value are really passed by invisible
1015 reference. In this case we also know the type of the
1016 object. */
1017 if (DECL_BY_REFERENCE (SSA_NAME_VAR (base_pointer)))
1019 outer_type
1020 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
1021 /* Only type inconsistent programs can have otr_type that is
1022 not part of outer type. */
1023 if (otr_type && !contains_type_p (outer_type, offset,
1024 otr_type))
1026 invalid = true;
1027 if (instance)
1028 *instance = base_pointer;
1029 return;
1031 /* Non-polymorphic types have no interest for us. */
1032 else if (!otr_type && !contains_polymorphic_type_p (outer_type))
1034 outer_type = NULL;
1035 if (instance)
1036 *instance = base_pointer;
1037 return;
1039 maybe_derived_type = false;
1040 maybe_in_construction = false;
1041 if (instance)
1042 *instance = base_pointer;
1043 return;
1047 tree base_type = TREE_TYPE (base_pointer);
1049 if (TREE_CODE (base_pointer) == SSA_NAME
1050 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
1051 && !(TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL
1052 || TREE_CODE (SSA_NAME_VAR (base_pointer)) == RESULT_DECL))
1054 invalid = true;
1055 if (instance)
1056 *instance = base_pointer;
1057 return;
1059 if (TREE_CODE (base_pointer) == SSA_NAME
1060 && SSA_NAME_DEF_STMT (base_pointer)
1061 && gimple_assign_single_p (SSA_NAME_DEF_STMT (base_pointer)))
1062 base_type = TREE_TYPE (gimple_assign_rhs1
1063 (SSA_NAME_DEF_STMT (base_pointer)));
1065 if (POINTER_TYPE_P (base_type))
1066 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base_type)),
1067 offset,
1068 true, NULL /* Do not change type here */);
1069 /* TODO: There are multiple ways to derive a type. For instance
1070 if BASE_POINTER is passed to an constructor call prior our refernece.
1071 We do not make this type of flow sensitive analysis yet. */
1072 if (instance)
1073 *instance = base_pointer;
1074 return;
1077 /* Structure to be passed in between detect_type_change and
1078 check_stmt_for_type_change. */
1080 struct type_change_info
1082 /* Offset into the object where there is the virtual method pointer we are
1083 looking for. */
1084 HOST_WIDE_INT offset;
1085 /* The declaration or SSA_NAME pointer of the base that we are checking for
1086 type change. */
1087 tree instance;
1088 /* The reference to virtual table pointer used. */
1089 tree vtbl_ptr_ref;
1090 tree otr_type;
1091 /* If we actually can tell the type that the object has changed to, it is
1092 stored in this field. Otherwise it remains NULL_TREE. */
1093 tree known_current_type;
1094 HOST_WIDE_INT known_current_offset;
1096 /* Set to true if dynamic type change has been detected. */
1097 bool type_maybe_changed;
1098 /* Set to true if multiple types have been encountered. known_current_type
1099 must be disregarded in that case. */
1100 bool multiple_types_encountered;
1101 /* Set to true if we possibly missed some dynamic type changes and we should
1102 consider the set to be speculative. */
1103 bool speculative;
1104 bool seen_unanalyzed_store;
1107 /* Return true if STMT is not call and can modify a virtual method table pointer.
1108 We take advantage of fact that vtable stores must appear within constructor
1109 and destructor functions. */
1111 static bool
1112 noncall_stmt_may_be_vtbl_ptr_store (gimple stmt)
1114 if (is_gimple_assign (stmt))
1116 tree lhs = gimple_assign_lhs (stmt);
1118 if (gimple_clobber_p (stmt))
1119 return false;
1120 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
1122 if (flag_strict_aliasing
1123 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
1124 return false;
1126 if (TREE_CODE (lhs) == COMPONENT_REF
1127 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1128 return false;
1129 /* In the future we might want to use get_base_ref_and_offset to find
1130 if there is a field corresponding to the offset and if so, proceed
1131 almost like if it was a component ref. */
1135 /* Code unification may mess with inline stacks. */
1136 if (cfun->after_inlining)
1137 return true;
1139 /* Walk the inline stack and watch out for ctors/dtors.
1140 TODO: Maybe we can require the store to appear in toplevel
1141 block of CTOR/DTOR. */
1142 for (tree block = gimple_block (stmt); block && TREE_CODE (block) == BLOCK;
1143 block = BLOCK_SUPERCONTEXT (block))
1144 if (BLOCK_ABSTRACT_ORIGIN (block)
1145 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block)) == FUNCTION_DECL)
1147 tree fn = BLOCK_ABSTRACT_ORIGIN (block);
1149 if (flags_from_decl_or_type (fn) & (ECF_PURE | ECF_CONST))
1150 return false;
1151 return (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1152 && (DECL_CXX_CONSTRUCTOR_P (fn)
1153 || DECL_CXX_DESTRUCTOR_P (fn)));
1155 return (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
1156 && (DECL_CXX_CONSTRUCTOR_P (current_function_decl)
1157 || DECL_CXX_DESTRUCTOR_P (current_function_decl)));
1160 /* If STMT can be proved to be an assignment to the virtual method table
1161 pointer of ANALYZED_OBJ and the type associated with the new table
1162 identified, return the type. Otherwise return NULL_TREE if type changes
1163 in unknown way or ERROR_MARK_NODE if type is unchanged. */
1165 static tree
1166 extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci,
1167 HOST_WIDE_INT *type_offset)
1169 HOST_WIDE_INT offset, size, max_size;
1170 tree lhs, rhs, base;
1172 if (!gimple_assign_single_p (stmt))
1173 return NULL_TREE;
1175 lhs = gimple_assign_lhs (stmt);
1176 rhs = gimple_assign_rhs1 (stmt);
1177 if (TREE_CODE (lhs) != COMPONENT_REF
1178 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1180 if (dump_file)
1181 fprintf (dump_file, " LHS is not virtual table.\n");
1182 return NULL_TREE;
1185 if (tci->vtbl_ptr_ref && operand_equal_p (lhs, tci->vtbl_ptr_ref, 0))
1187 else
1189 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1190 if (DECL_P (tci->instance))
1192 if (base != tci->instance)
1194 if (dump_file)
1196 fprintf (dump_file, " base:");
1197 print_generic_expr (dump_file, base, TDF_SLIM);
1198 fprintf (dump_file, " does not match instance:");
1199 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1200 fprintf (dump_file, "\n");
1202 return NULL_TREE;
1205 else if (TREE_CODE (base) == MEM_REF)
1207 if (!operand_equal_p (tci->instance, TREE_OPERAND (base, 0), 0))
1209 if (dump_file)
1211 fprintf (dump_file, " base mem ref:");
1212 print_generic_expr (dump_file, base, TDF_SLIM);
1213 fprintf (dump_file, " does not match instance:");
1214 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1215 fprintf (dump_file, "\n");
1217 return NULL_TREE;
1219 if (!integer_zerop (TREE_OPERAND (base, 1)))
1221 if (!tree_fits_shwi_p (TREE_OPERAND (base, 1)))
1223 if (dump_file)
1225 fprintf (dump_file, " base mem ref:");
1226 print_generic_expr (dump_file, base, TDF_SLIM);
1227 fprintf (dump_file, " has non-representable offset:");
1228 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1229 fprintf (dump_file, "\n");
1231 return NULL_TREE;
1233 else
1234 offset += tree_to_shwi (TREE_OPERAND (base, 1)) * BITS_PER_UNIT;
1237 else if (!operand_equal_p (tci->instance, base, 0)
1238 || tci->offset)
1240 if (dump_file)
1242 fprintf (dump_file, " base:");
1243 print_generic_expr (dump_file, base, TDF_SLIM);
1244 fprintf (dump_file, " does not match instance:");
1245 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1246 fprintf (dump_file, " with offset %i\n", (int)tci->offset);
1248 return tci->offset > POINTER_SIZE ? error_mark_node : NULL_TREE;
1250 if (offset != tci->offset
1251 || size != POINTER_SIZE
1252 || max_size != POINTER_SIZE)
1254 if (dump_file)
1255 fprintf (dump_file, " wrong offset %i!=%i or size %i\n",
1256 (int)offset, (int)tci->offset, (int)size);
1257 return offset + POINTER_SIZE <= tci->offset
1258 || (max_size != -1
1259 && tci->offset + POINTER_SIZE > offset + max_size)
1260 ? error_mark_node : NULL;
1264 tree vtable;
1265 unsigned HOST_WIDE_INT offset2;
1267 if (!vtable_pointer_value_to_vtable (rhs, &vtable, &offset2))
1269 if (dump_file)
1270 fprintf (dump_file, " Failed to lookup binfo\n");
1271 return NULL;
1274 tree binfo = subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
1275 offset2, vtable);
1276 if (!binfo)
1278 if (dump_file)
1279 fprintf (dump_file, " Construction vtable used\n");
1280 /* FIXME: We should suport construction contexts. */
1281 return NULL;
1284 *type_offset = tree_to_shwi (BINFO_OFFSET (binfo)) * BITS_PER_UNIT;
1285 return DECL_CONTEXT (vtable);
1288 /* Record dynamic type change of TCI to TYPE. */
1290 static void
1291 record_known_type (struct type_change_info *tci, tree type, HOST_WIDE_INT offset)
1293 if (dump_file)
1295 if (type)
1297 fprintf (dump_file, " Recording type: ");
1298 print_generic_expr (dump_file, type, TDF_SLIM);
1299 fprintf (dump_file, " at offset %i\n", (int)offset);
1301 else
1302 fprintf (dump_file, " Recording unknown type\n");
1305 /* If we found a constructor of type that is not polymorphic or
1306 that may contain the type in question as a field (not as base),
1307 restrict to the inner class first to make type matching bellow
1308 happier. */
1309 if (type
1310 && (offset
1311 || (TREE_CODE (type) != RECORD_TYPE
1312 || !TYPE_BINFO (type)
1313 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))))
1315 ipa_polymorphic_call_context context;
1317 context.offset = offset;
1318 context.outer_type = type;
1319 context.maybe_in_construction = false;
1320 context.maybe_derived_type = false;
1321 context.dynamic = true;
1322 /* If we failed to find the inner type, we know that the call
1323 would be undefined for type produced here. */
1324 if (!context.restrict_to_inner_class (tci->otr_type))
1326 if (dump_file)
1327 fprintf (dump_file, " Ignoring; does not contain otr_type\n");
1328 return;
1330 /* Watch for case we reached an POD type and anticipate placement
1331 new. */
1332 if (!context.maybe_derived_type)
1334 type = context.outer_type;
1335 offset = context.offset;
1338 if (tci->type_maybe_changed
1339 && (!types_same_for_odr (type, tci->known_current_type)
1340 || offset != tci->known_current_offset))
1341 tci->multiple_types_encountered = true;
1342 tci->known_current_type = TYPE_MAIN_VARIANT (type);
1343 tci->known_current_offset = offset;
1344 tci->type_maybe_changed = true;
1347 /* Callback of walk_aliased_vdefs and a helper function for
1348 detect_type_change to check whether a particular statement may modify
1349 the virtual table pointer, and if possible also determine the new type of
1350 the (sub-)object. It stores its result into DATA, which points to a
1351 type_change_info structure. */
1353 static bool
1354 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
1356 gimple stmt = SSA_NAME_DEF_STMT (vdef);
1357 struct type_change_info *tci = (struct type_change_info *) data;
1358 tree fn;
1360 /* If we already gave up, just terminate the rest of walk. */
1361 if (tci->multiple_types_encountered)
1362 return true;
1364 if (is_gimple_call (stmt))
1366 if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
1367 return false;
1369 /* Check for a constructor call. */
1370 if ((fn = gimple_call_fndecl (stmt)) != NULL_TREE
1371 && DECL_CXX_CONSTRUCTOR_P (fn)
1372 && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1373 && gimple_call_num_args (stmt))
1375 tree op = walk_ssa_copies (gimple_call_arg (stmt, 0));
1376 tree type = method_class_type (TREE_TYPE (fn));
1377 HOST_WIDE_INT offset = 0, size, max_size;
1379 if (dump_file)
1381 fprintf (dump_file, " Checking constructor call: ");
1382 print_gimple_stmt (dump_file, stmt, 0, 0);
1385 /* See if THIS parameter seems like instance pointer. */
1386 if (TREE_CODE (op) == ADDR_EXPR)
1388 op = get_ref_base_and_extent (TREE_OPERAND (op, 0),
1389 &offset, &size, &max_size);
1390 if (size != max_size || max_size == -1)
1392 tci->speculative = true;
1393 return false;
1395 if (op && TREE_CODE (op) == MEM_REF)
1397 if (!tree_fits_shwi_p (TREE_OPERAND (op, 1)))
1399 tci->speculative = true;
1400 return false;
1402 offset += tree_to_shwi (TREE_OPERAND (op, 1))
1403 * BITS_PER_UNIT;
1404 op = TREE_OPERAND (op, 0);
1406 else if (DECL_P (op))
1408 else
1410 tci->speculative = true;
1411 return false;
1413 op = walk_ssa_copies (op);
1415 if (operand_equal_p (op, tci->instance, 0)
1416 && TYPE_SIZE (type)
1417 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1418 && tree_fits_shwi_p (TYPE_SIZE (type))
1419 && tree_to_shwi (TYPE_SIZE (type)) + offset > tci->offset)
1421 record_known_type (tci, type, tci->offset - offset);
1422 return true;
1425 /* Calls may possibly change dynamic type by placement new. Assume
1426 it will not happen, but make result speculative only. */
1427 if (dump_file)
1429 fprintf (dump_file, " Function call may change dynamic type:");
1430 print_gimple_stmt (dump_file, stmt, 0, 0);
1432 tci->speculative = true;
1433 return false;
1435 /* Check for inlined virtual table store. */
1436 else if (noncall_stmt_may_be_vtbl_ptr_store (stmt))
1438 tree type;
1439 HOST_WIDE_INT offset = 0;
1440 if (dump_file)
1442 fprintf (dump_file, " Checking vtbl store: ");
1443 print_gimple_stmt (dump_file, stmt, 0, 0);
1446 type = extr_type_from_vtbl_ptr_store (stmt, tci, &offset);
1447 if (type == error_mark_node)
1448 return false;
1449 gcc_assert (!type || TYPE_MAIN_VARIANT (type) == type);
1450 if (!type)
1452 if (dump_file)
1453 fprintf (dump_file, " Unanalyzed store may change type.\n");
1454 tci->seen_unanalyzed_store = true;
1455 tci->speculative = true;
1457 else
1458 record_known_type (tci, type, offset);
1459 return true;
1461 else
1462 return false;
1465 /* THIS is polymorphic call context obtained from get_polymorphic_context.
1466 OTR_OBJECT is pointer to the instance returned by OBJ_TYPE_REF_OBJECT.
1467 INSTANCE is pointer to the outer instance as returned by
1468 get_polymorphic_context. To avoid creation of temporary expressions,
1469 INSTANCE may also be an declaration of get_polymorphic_context found the
1470 value to be in static storage.
1472 If the type of instance is not fully determined
1473 (either OUTER_TYPE is unknown or MAYBE_IN_CONSTRUCTION/INCLUDE_DERIVED_TYPES
1474 is set), try to walk memory writes and find the actual construction of the
1475 instance.
1477 Return true if memory is unchanged from function entry.
1479 We do not include this analysis in the context analysis itself, because
1480 it needs memory SSA to be fully built and the walk may be expensive.
1481 So it is not suitable for use withing fold_stmt and similar uses. */
1483 bool
1484 ipa_polymorphic_call_context::get_dynamic_type (tree instance,
1485 tree otr_object,
1486 tree otr_type,
1487 gimple call)
1489 struct type_change_info tci;
1490 ao_ref ao;
1491 bool function_entry_reached = false;
1492 tree instance_ref = NULL;
1493 gimple stmt = call;
1494 /* Remember OFFSET before it is modified by restrict_to_inner_class.
1495 This is because we do not update INSTANCE when walking inwards. */
1496 HOST_WIDE_INT instance_offset = offset;
1498 if (otr_type)
1499 otr_type = TYPE_MAIN_VARIANT (otr_type);
1501 /* Walk into inner type. This may clear maybe_derived_type and save us
1502 from useless work. It also makes later comparsions with static type
1503 easier. */
1504 if (outer_type && otr_type)
1506 if (!restrict_to_inner_class (otr_type))
1507 return false;
1510 if (!maybe_in_construction && !maybe_derived_type)
1511 return false;
1513 /* We need to obtain refernce to virtual table pointer. It is better
1514 to look it up in the code rather than build our own. This require bit
1515 of pattern matching, but we end up verifying that what we found is
1516 correct.
1518 What we pattern match is:
1520 tmp = instance->_vptr.A; // vtbl ptr load
1521 tmp2 = tmp[otr_token]; // vtable lookup
1522 OBJ_TYPE_REF(tmp2;instance->0) (instance);
1524 We want to start alias oracle walk from vtbl pointer load,
1525 but we may not be able to identify it, for example, when PRE moved the
1526 load around. */
1528 if (gimple_code (call) == GIMPLE_CALL)
1530 tree ref = gimple_call_fn (call);
1531 HOST_WIDE_INT offset2, size, max_size;
1533 if (TREE_CODE (ref) == OBJ_TYPE_REF)
1535 ref = OBJ_TYPE_REF_EXPR (ref);
1536 ref = walk_ssa_copies (ref);
1538 /* Check if definition looks like vtable lookup. */
1539 if (TREE_CODE (ref) == SSA_NAME
1540 && !SSA_NAME_IS_DEFAULT_DEF (ref)
1541 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref))
1542 && TREE_CODE (gimple_assign_rhs1
1543 (SSA_NAME_DEF_STMT (ref))) == MEM_REF)
1545 ref = get_base_address
1546 (TREE_OPERAND (gimple_assign_rhs1
1547 (SSA_NAME_DEF_STMT (ref)), 0));
1548 ref = walk_ssa_copies (ref);
1549 /* Find base address of the lookup and see if it looks like
1550 vptr load. */
1551 if (TREE_CODE (ref) == SSA_NAME
1552 && !SSA_NAME_IS_DEFAULT_DEF (ref)
1553 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref)))
1555 tree ref_exp = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (ref));
1556 tree base_ref = get_ref_base_and_extent
1557 (ref_exp, &offset2, &size, &max_size);
1559 /* Finally verify that what we found looks like read from OTR_OBJECT
1560 or from INSTANCE with offset OFFSET. */
1561 if (base_ref
1562 && ((TREE_CODE (base_ref) == MEM_REF
1563 && ((offset2 == instance_offset
1564 && TREE_OPERAND (base_ref, 0) == instance)
1565 || (!offset2 && TREE_OPERAND (base_ref, 0) == otr_object)))
1566 || (DECL_P (instance) && base_ref == instance
1567 && offset2 == instance_offset)))
1569 stmt = SSA_NAME_DEF_STMT (ref);
1570 instance_ref = ref_exp;
1577 /* If we failed to look up the refernece in code, build our own. */
1578 if (!instance_ref)
1580 /* If the statement in question does not use memory, we can't tell
1581 anything. */
1582 if (!gimple_vuse (stmt))
1583 return false;
1584 ao_ref_init_from_ptr_and_size (&ao, otr_object, NULL);
1586 else
1587 /* Otherwise use the real reference. */
1588 ao_ref_init (&ao, instance_ref);
1590 /* We look for vtbl pointer read. */
1591 ao.size = POINTER_SIZE;
1592 ao.max_size = ao.size;
1593 if (otr_type)
1594 ao.ref_alias_set
1595 = get_deref_alias_set (TREE_TYPE (BINFO_VTABLE (TYPE_BINFO (otr_type))));
1597 if (dump_file)
1599 fprintf (dump_file, "Determining dynamic type for call: ");
1600 print_gimple_stmt (dump_file, call, 0, 0);
1601 fprintf (dump_file, " Starting walk at: ");
1602 print_gimple_stmt (dump_file, stmt, 0, 0);
1603 fprintf (dump_file, " instance pointer: ");
1604 print_generic_expr (dump_file, otr_object, TDF_SLIM);
1605 fprintf (dump_file, " Outer instance pointer: ");
1606 print_generic_expr (dump_file, instance, TDF_SLIM);
1607 fprintf (dump_file, " offset: %i (bits)", (int)offset);
1608 fprintf (dump_file, " vtbl reference: ");
1609 print_generic_expr (dump_file, instance_ref, TDF_SLIM);
1610 fprintf (dump_file, "\n");
1613 tci.offset = offset;
1614 tci.instance = instance;
1615 tci.vtbl_ptr_ref = instance_ref;
1616 gcc_assert (TREE_CODE (instance) != MEM_REF);
1617 tci.known_current_type = NULL_TREE;
1618 tci.known_current_offset = 0;
1619 tci.otr_type = otr_type;
1620 tci.type_maybe_changed = false;
1621 tci.multiple_types_encountered = false;
1622 tci.speculative = false;
1623 tci.seen_unanalyzed_store = false;
1625 walk_aliased_vdefs (&ao, gimple_vuse (stmt), check_stmt_for_type_change,
1626 &tci, NULL, &function_entry_reached);
1628 /* If we did not find any type changing statements, we may still drop
1629 maybe_in_construction flag if the context already have outer type.
1631 Here we make special assumptions about both constructors and
1632 destructors which are all the functions that are allowed to alter the
1633 VMT pointers. It assumes that destructors begin with assignment into
1634 all VMT pointers and that constructors essentially look in the
1635 following way:
1637 1) The very first thing they do is that they call constructors of
1638 ancestor sub-objects that have them.
1640 2) Then VMT pointers of this and all its ancestors is set to new
1641 values corresponding to the type corresponding to the constructor.
1643 3) Only afterwards, other stuff such as constructor of member
1644 sub-objects and the code written by the user is run. Only this may
1645 include calling virtual functions, directly or indirectly.
1647 4) placement new can not be used to change type of non-POD statically
1648 allocated variables.
1650 There is no way to call a constructor of an ancestor sub-object in any
1651 other way.
1653 This means that we do not have to care whether constructors get the
1654 correct type information because they will always change it (in fact,
1655 if we define the type to be given by the VMT pointer, it is undefined).
1657 The most important fact to derive from the above is that if, for some
1658 statement in the section 3, we try to detect whether the dynamic type
1659 has changed, we can safely ignore all calls as we examine the function
1660 body backwards until we reach statements in section 2 because these
1661 calls cannot be ancestor constructors or destructors (if the input is
1662 not bogus) and so do not change the dynamic type (this holds true only
1663 for automatically allocated objects but at the moment we devirtualize
1664 only these). We then must detect that statements in section 2 change
1665 the dynamic type and can try to derive the new type. That is enough
1666 and we can stop, we will never see the calls into constructors of
1667 sub-objects in this code.
1669 Therefore if the static outer type was found (outer_type)
1670 we can safely ignore tci.speculative that is set on calls and give up
1671 only if there was dyanmic type store that may affect given variable
1672 (seen_unanalyzed_store) */
1674 if (!tci.type_maybe_changed
1675 || (outer_type
1676 && !dynamic
1677 && !tci.seen_unanalyzed_store
1678 && !tci.multiple_types_encountered
1679 && offset == tci.offset
1680 && types_same_for_odr (tci.known_current_type,
1681 outer_type)))
1683 if (!outer_type || tci.seen_unanalyzed_store)
1684 return false;
1685 if (maybe_in_construction)
1686 maybe_in_construction = false;
1687 if (dump_file)
1688 fprintf (dump_file, " No dynamic type change found.\n");
1689 return true;
1692 if (tci.known_current_type
1693 && !function_entry_reached
1694 && !tci.multiple_types_encountered)
1696 if (!tci.speculative)
1698 outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1699 offset = tci.known_current_offset;
1700 dynamic = true;
1701 maybe_in_construction = false;
1702 maybe_derived_type = false;
1703 if (dump_file)
1704 fprintf (dump_file, " Determined dynamic type.\n");
1706 else if (!speculative_outer_type
1707 || speculative_maybe_derived_type)
1709 speculative_outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1710 speculative_offset = tci.known_current_offset;
1711 speculative_maybe_derived_type = false;
1712 if (dump_file)
1713 fprintf (dump_file, " Determined speculative dynamic type.\n");
1716 else if (dump_file)
1718 fprintf (dump_file, " Found multiple types%s%s\n",
1719 function_entry_reached ? " (function entry reached)" : "",
1720 function_entry_reached ? " (multiple types encountered)" : "");
1723 return false;
1726 /* See if speculation given by SPEC_OUTER_TYPE, SPEC_OFFSET and SPEC_MAYBE_DERIVED_TYPE
1727 seems consistent (and useful) with what we already have in the non-speculative context. */
1729 bool
1730 ipa_polymorphic_call_context::speculation_consistent_p (tree spec_outer_type,
1731 HOST_WIDE_INT spec_offset,
1732 bool spec_maybe_derived_type,
1733 tree otr_type) const
1735 if (!flag_devirtualize_speculatively)
1736 return false;
1738 /* Non-polymorphic types are useless for deriving likely polymorphic
1739 call targets. */
1740 if (!spec_outer_type || !contains_polymorphic_type_p (spec_outer_type))
1741 return false;
1743 /* If we know nothing, speculation is always good. */
1744 if (!outer_type)
1745 return true;
1747 /* Speculation is only useful to avoid derived types.
1748 This is not 100% true for placement new, where the outer context may
1749 turn out to be useless, but ignore these for now. */
1750 if (!maybe_derived_type)
1751 return false;
1753 /* If types agrees, speculation is consistent, but it makes sense only
1754 when it says something new. */
1755 if (types_must_be_same_for_odr (spec_outer_type, outer_type))
1756 return maybe_derived_type && !spec_maybe_derived_type;
1758 /* If speculation does not contain the type in question, ignore it. */
1759 if (otr_type
1760 && !contains_type_p (spec_outer_type, spec_offset, otr_type, false, true))
1761 return false;
1763 /* If outer type already contains speculation as a filed,
1764 it is useless. We already know from OUTER_TYPE
1765 SPEC_TYPE and that it is not in the construction. */
1766 if (contains_type_p (outer_type, offset - spec_offset,
1767 spec_outer_type, false, false))
1768 return false;
1770 /* If speculative outer type is not more specified than outer
1771 type, just give up.
1772 We can only decide this safely if we can compare types with OUTER_TYPE.
1774 if ((!in_lto_p || odr_type_p (outer_type))
1775 && !contains_type_p (spec_outer_type,
1776 spec_offset - offset,
1777 outer_type, false))
1778 return false;
1779 return true;
1782 /* Improve THIS with speculation described by NEW_OUTER_TYPE, NEW_OFFSET,
1783 NEW_MAYBE_DERIVED_TYPE
1784 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1786 bool
1787 ipa_polymorphic_call_context::combine_speculation_with
1788 (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1789 tree otr_type)
1791 if (!new_outer_type)
1792 return false;
1794 /* restrict_to_inner_class may eliminate wrong speculation making our job
1795 easeier. */
1796 if (otr_type)
1797 restrict_to_inner_class (otr_type);
1799 if (!speculation_consistent_p (new_outer_type, new_offset,
1800 new_maybe_derived_type, otr_type))
1801 return false;
1803 /* New speculation is a win in case we have no speculation or new
1804 speculation does not consider derivations. */
1805 if (!speculative_outer_type
1806 || (speculative_maybe_derived_type
1807 && !new_maybe_derived_type))
1809 speculative_outer_type = new_outer_type;
1810 speculative_offset = new_offset;
1811 speculative_maybe_derived_type = new_maybe_derived_type;
1812 return true;
1814 else if (types_must_be_same_for_odr (speculative_outer_type,
1815 new_outer_type))
1817 if (speculative_offset != new_offset)
1819 /* OK we have two contexts that seems valid but they disagree,
1820 just give up.
1822 This is not a lattice operation, so we may want to drop it later. */
1823 if (dump_file && (dump_flags & TDF_DETAILS))
1824 fprintf (dump_file,
1825 "Speculative outer types match, "
1826 "offset mismatch -> invalid speculation\n");
1827 clear_speculation ();
1828 return true;
1830 else
1832 if (speculative_maybe_derived_type && !new_maybe_derived_type)
1834 speculative_maybe_derived_type = false;
1835 return true;
1837 else
1838 return false;
1841 /* Choose type that contains the other. This one either contains the outer
1842 as a field (thus giving exactly one target) or is deeper in the type
1843 hiearchy. */
1844 else if (speculative_outer_type
1845 && speculative_maybe_derived_type
1846 && (new_offset > speculative_offset
1847 || (new_offset == speculative_offset
1848 && contains_type_p (new_outer_type,
1849 0, speculative_outer_type, false))))
1851 tree old_outer_type = speculative_outer_type;
1852 HOST_WIDE_INT old_offset = speculative_offset;
1853 bool old_maybe_derived_type = speculative_maybe_derived_type;
1855 speculative_outer_type = new_outer_type;
1856 speculative_offset = new_offset;
1857 speculative_maybe_derived_type = new_maybe_derived_type;
1859 if (otr_type)
1860 restrict_to_inner_class (otr_type);
1862 /* If the speculation turned out to make no sense, revert to sensible
1863 one. */
1864 if (!speculative_outer_type)
1866 speculative_outer_type = old_outer_type;
1867 speculative_offset = old_offset;
1868 speculative_maybe_derived_type = old_maybe_derived_type;
1869 return false;
1871 return (old_offset != speculative_offset
1872 || old_maybe_derived_type != speculative_maybe_derived_type
1873 || types_must_be_same_for_odr (speculative_outer_type,
1874 new_outer_type));
1876 return false;
1879 /* Make speculation less specific so
1880 NEW_OUTER_TYPE, NEW_OFFSET, NEW_MAYBE_DERIVED_TYPE is also included.
1881 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1883 bool
1884 ipa_polymorphic_call_context::meet_speculation_with
1885 (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1886 tree otr_type)
1888 if (!new_outer_type && speculative_outer_type)
1890 clear_speculation ();
1891 return true;
1894 /* restrict_to_inner_class may eliminate wrong speculation making our job
1895 easeier. */
1896 if (otr_type)
1897 restrict_to_inner_class (otr_type);
1899 if (!speculative_outer_type
1900 || !speculation_consistent_p (speculative_outer_type,
1901 speculative_offset,
1902 speculative_maybe_derived_type,
1903 otr_type))
1904 return false;
1906 if (!speculation_consistent_p (new_outer_type, new_offset,
1907 new_maybe_derived_type, otr_type))
1909 clear_speculation ();
1910 return true;
1913 else if (types_must_be_same_for_odr (speculative_outer_type,
1914 new_outer_type))
1916 if (speculative_offset != new_offset)
1918 clear_speculation ();
1919 return true;
1921 else
1923 if (!speculative_maybe_derived_type && new_maybe_derived_type)
1925 speculative_maybe_derived_type = true;
1926 return true;
1928 else
1929 return false;
1932 /* See if one type contains the other as a field (not base). */
1933 else if (contains_type_p (new_outer_type, new_offset - speculative_offset,
1934 speculative_outer_type, false, false))
1935 return false;
1936 else if (contains_type_p (speculative_outer_type,
1937 speculative_offset - new_offset,
1938 new_outer_type, false, false))
1940 speculative_outer_type = new_outer_type;
1941 speculative_offset = new_offset;
1942 speculative_maybe_derived_type = new_maybe_derived_type;
1943 return true;
1945 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
1946 else if (contains_type_p (new_outer_type,
1947 new_offset - speculative_offset,
1948 speculative_outer_type, false, true))
1950 if (!speculative_maybe_derived_type)
1952 speculative_maybe_derived_type = true;
1953 return true;
1955 return false;
1957 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
1958 else if (contains_type_p (speculative_outer_type,
1959 speculative_offset - new_offset, new_outer_type, false, true))
1961 speculative_outer_type = new_outer_type;
1962 speculative_offset = new_offset;
1963 speculative_maybe_derived_type = true;
1964 return true;
1966 else
1968 if (dump_file && (dump_flags & TDF_DETAILS))
1969 fprintf (dump_file, "Giving up on speculative meet\n");
1970 clear_speculation ();
1971 return true;
1975 /* Assume that both THIS and a given context is valid and strenghten THIS
1976 if possible. Return true if any strenghtening was made.
1977 If actual type the context is being used in is known, OTR_TYPE should be
1978 set accordingly. This improves quality of combined result. */
1980 bool
1981 ipa_polymorphic_call_context::combine_with (ipa_polymorphic_call_context ctx,
1982 tree otr_type)
1984 bool updated = false;
1986 if (ctx.useless_p () || invalid)
1987 return false;
1989 /* Restricting context to inner type makes merging easier, however do not
1990 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
1991 if (otr_type && !invalid && !ctx.invalid)
1993 restrict_to_inner_class (otr_type);
1994 ctx.restrict_to_inner_class (otr_type);
1995 if(invalid)
1996 return false;
1999 if (dump_file && (dump_flags & TDF_DETAILS))
2001 fprintf (dump_file, "Polymorphic call context combine:");
2002 dump (dump_file);
2003 fprintf (dump_file, "With context: ");
2004 ctx.dump (dump_file);
2005 if (otr_type)
2007 fprintf (dump_file, "To be used with type: ");
2008 print_generic_expr (dump_file, otr_type, TDF_SLIM);
2009 fprintf (dump_file, "\n");
2013 /* If call is known to be invalid, we are done. */
2014 if (ctx.invalid)
2016 if (dump_file && (dump_flags & TDF_DETAILS))
2017 fprintf (dump_file, "-> Invalid context\n");
2018 goto invalidate;
2021 if (!ctx.outer_type)
2023 else if (!outer_type)
2025 outer_type = ctx.outer_type;
2026 offset = ctx.offset;
2027 dynamic = ctx.dynamic;
2028 maybe_in_construction = ctx.maybe_in_construction;
2029 maybe_derived_type = ctx.maybe_derived_type;
2030 updated = true;
2032 /* If types are known to be same, merging is quite easy. */
2033 else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2035 if (offset != ctx.offset
2036 && TYPE_SIZE (outer_type)
2037 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2039 if (dump_file && (dump_flags & TDF_DETAILS))
2040 fprintf (dump_file, "Outer types match, offset mismatch -> invalid\n");
2041 clear_speculation ();
2042 clear_outer_type ();
2043 invalid = true;
2044 return true;
2046 if (dump_file && (dump_flags & TDF_DETAILS))
2047 fprintf (dump_file, "Outer types match, merging flags\n");
2048 if (maybe_in_construction && !ctx.maybe_in_construction)
2050 updated = true;
2051 maybe_in_construction = false;
2053 if (maybe_derived_type && !ctx.maybe_derived_type)
2055 updated = true;
2056 maybe_derived_type = false;
2058 if (dynamic && !ctx.dynamic)
2060 updated = true;
2061 dynamic = false;
2064 /* If we know the type precisely, there is not much to improve. */
2065 else if (!maybe_derived_type && !maybe_in_construction
2066 && !ctx.maybe_derived_type && !ctx.maybe_in_construction)
2068 /* It may be easy to check if second context permits the first
2069 and set INVALID otherwise. This is not easy to do in general;
2070 contains_type_p may return false negatives for non-comparable
2071 types.
2073 If OTR_TYPE is known, we however can expect that
2074 restrict_to_inner_class should have discovered the same base
2075 type. */
2076 if (otr_type && !ctx.maybe_in_construction && !ctx.maybe_derived_type)
2078 if (dump_file && (dump_flags & TDF_DETAILS))
2079 fprintf (dump_file, "Contextes disagree -> invalid\n");
2080 goto invalidate;
2083 /* See if one type contains the other as a field (not base).
2084 In this case we want to choose the wider type, because it contains
2085 more information. */
2086 else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2087 outer_type, false, false))
2089 if (dump_file && (dump_flags & TDF_DETAILS))
2090 fprintf (dump_file, "Second type contain the first as a field\n");
2092 if (maybe_derived_type)
2094 outer_type = ctx.outer_type;
2095 maybe_derived_type = ctx.maybe_derived_type;
2096 offset = ctx.offset;
2097 dynamic = ctx.dynamic;
2098 updated = true;
2101 /* If we do not know how the context is being used, we can
2102 not clear MAYBE_IN_CONSTRUCTION because it may be offseted
2103 to other component of OUTER_TYPE later and we know nothing
2104 about it. */
2105 if (otr_type && maybe_in_construction
2106 && !ctx.maybe_in_construction)
2108 maybe_in_construction = false;
2109 updated = true;
2112 else if (contains_type_p (outer_type, offset - ctx.offset,
2113 ctx.outer_type, false, false))
2115 if (dump_file && (dump_flags & TDF_DETAILS))
2116 fprintf (dump_file, "First type contain the second as a field\n");
2118 if (otr_type && maybe_in_construction
2119 && !ctx.maybe_in_construction)
2121 maybe_in_construction = false;
2122 updated = true;
2125 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2126 else if (contains_type_p (ctx.outer_type,
2127 ctx.offset - offset, outer_type, false, true))
2129 if (dump_file && (dump_flags & TDF_DETAILS))
2130 fprintf (dump_file, "First type is base of second\n");
2131 if (!maybe_derived_type)
2133 if (!ctx.maybe_in_construction
2134 && types_odr_comparable (outer_type, ctx.outer_type))
2136 if (dump_file && (dump_flags & TDF_DETAILS))
2137 fprintf (dump_file, "Second context does not permit base -> invalid\n");
2138 goto invalidate;
2141 /* Pick variant deeper in the hiearchy. */
2142 else
2144 outer_type = ctx.outer_type;
2145 maybe_in_construction = ctx.maybe_in_construction;
2146 maybe_derived_type = ctx.maybe_derived_type;
2147 offset = ctx.offset;
2148 dynamic = ctx.dynamic;
2149 updated = true;
2152 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2153 else if (contains_type_p (outer_type,
2154 offset - ctx.offset, ctx.outer_type, false, true))
2156 if (dump_file && (dump_flags & TDF_DETAILS))
2157 fprintf (dump_file, "Second type is base of first\n");
2158 if (!ctx.maybe_derived_type)
2160 if (!maybe_in_construction
2161 && types_odr_comparable (outer_type, ctx.outer_type))
2163 if (dump_file && (dump_flags & TDF_DETAILS))
2164 fprintf (dump_file, "First context does not permit base -> invalid\n");
2165 goto invalidate;
2167 /* Pick the base type. */
2168 else if (maybe_in_construction)
2170 outer_type = ctx.outer_type;
2171 maybe_in_construction = ctx.maybe_in_construction;
2172 maybe_derived_type = ctx.maybe_derived_type;
2173 offset = ctx.offset;
2174 dynamic = ctx.dynamic;
2175 updated = true;
2179 /* TODO handle merging using hiearchy. */
2180 else if (dump_file && (dump_flags & TDF_DETAILS))
2181 fprintf (dump_file, "Giving up on merge\n");
2183 updated |= combine_speculation_with (ctx.speculative_outer_type,
2184 ctx.speculative_offset,
2185 ctx.speculative_maybe_derived_type,
2186 otr_type);
2188 if (updated && dump_file && (dump_flags & TDF_DETAILS))
2190 fprintf (dump_file, "Updated as: ");
2191 dump (dump_file);
2192 fprintf (dump_file, "\n");
2194 return updated;
2196 invalidate:
2197 invalid = true;
2198 clear_speculation ();
2199 clear_outer_type ();
2200 return true;
2203 /* Take non-speculative info, merge it with speculative and clear speculation.
2204 Used when we no longer manage to keep track of actual outer type, but we
2205 think it is still there.
2207 If OTR_TYPE is set, the transformation can be done more effectively assuming
2208 that context is going to be used only that way. */
2210 void
2211 ipa_polymorphic_call_context::make_speculative (tree otr_type)
2213 tree spec_outer_type = outer_type;
2214 HOST_WIDE_INT spec_offset = offset;
2215 bool spec_maybe_derived_type = maybe_derived_type;
2217 if (invalid)
2219 invalid = false;
2220 clear_outer_type ();
2221 clear_speculation ();
2222 return;
2224 if (!outer_type)
2225 return;
2226 clear_outer_type ();
2227 combine_speculation_with (spec_outer_type, spec_offset,
2228 spec_maybe_derived_type,
2229 otr_type);
2232 /* Use when we can not track dynamic type change. This speculatively assume
2233 type change is not happening. */
2235 void
2236 ipa_polymorphic_call_context::possible_dynamic_type_change (bool in_poly_cdtor,
2237 tree otr_type)
2239 if (dynamic)
2240 make_speculative (otr_type);
2241 else if (in_poly_cdtor)
2242 maybe_in_construction = true;
2245 /* Return TRUE if this context conveys the same information as OTHER. */
2247 bool
2248 ipa_polymorphic_call_context::equal_to
2249 (const ipa_polymorphic_call_context &x) const
2251 if (useless_p ())
2252 return x.useless_p ();
2253 if (invalid)
2254 return x.invalid;
2255 if (x.useless_p () || x.invalid)
2256 return false;
2258 if (outer_type)
2260 if (!x.outer_type
2261 || !types_odr_comparable (outer_type, x.outer_type)
2262 || !types_same_for_odr (outer_type, x.outer_type)
2263 || offset != x.offset
2264 || maybe_in_construction != x.maybe_in_construction
2265 || maybe_derived_type != x.maybe_derived_type
2266 || dynamic != x.dynamic)
2267 return false;
2269 else if (x.outer_type)
2270 return false;
2273 if (speculative_outer_type
2274 && speculation_consistent_p (speculative_outer_type, speculative_offset,
2275 speculative_maybe_derived_type, NULL_TREE))
2277 if (!x.speculative_outer_type)
2278 return false;
2280 if (!types_odr_comparable (speculative_outer_type,
2281 x.speculative_outer_type)
2282 || !types_same_for_odr (speculative_outer_type,
2283 x.speculative_outer_type)
2284 || speculative_offset != x.speculative_offset
2285 || speculative_maybe_derived_type != x.speculative_maybe_derived_type)
2286 return false;
2288 else if (x.speculative_outer_type
2289 && x.speculation_consistent_p (x.speculative_outer_type,
2290 x.speculative_offset,
2291 x.speculative_maybe_derived_type,
2292 NULL))
2293 return false;
2295 return true;
2298 /* Modify context to be strictly less restrictive than CTX. */
2300 bool
2301 ipa_polymorphic_call_context::meet_with (ipa_polymorphic_call_context ctx,
2302 tree otr_type)
2304 bool updated = false;
2306 if (useless_p () || ctx.invalid)
2307 return false;
2309 /* Restricting context to inner type makes merging easier, however do not
2310 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2311 if (otr_type && !useless_p () && !ctx.useless_p ())
2313 restrict_to_inner_class (otr_type);
2314 ctx.restrict_to_inner_class (otr_type);
2315 if(invalid)
2316 return false;
2319 if (equal_to (ctx))
2320 return false;
2322 if (ctx.useless_p () || invalid)
2324 *this = ctx;
2325 return true;
2328 if (dump_file && (dump_flags & TDF_DETAILS))
2330 fprintf (dump_file, "Polymorphic call context meet:");
2331 dump (dump_file);
2332 fprintf (dump_file, "With context: ");
2333 ctx.dump (dump_file);
2334 if (otr_type)
2336 fprintf (dump_file, "To be used with type: ");
2337 print_generic_expr (dump_file, otr_type, TDF_SLIM);
2338 fprintf (dump_file, "\n");
2342 if (!dynamic && ctx.dynamic)
2344 dynamic = true;
2345 updated = true;
2348 /* If call is known to be invalid, we are done. */
2349 if (!outer_type)
2351 else if (!ctx.outer_type)
2353 clear_outer_type ();
2354 updated = true;
2356 /* If types are known to be same, merging is quite easy. */
2357 else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2359 if (offset != ctx.offset
2360 && TYPE_SIZE (outer_type)
2361 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2363 if (dump_file && (dump_flags & TDF_DETAILS))
2364 fprintf (dump_file, "Outer types match, offset mismatch -> clearing\n");
2365 clear_outer_type ();
2366 return true;
2368 if (dump_file && (dump_flags & TDF_DETAILS))
2369 fprintf (dump_file, "Outer types match, merging flags\n");
2370 if (!maybe_in_construction && ctx.maybe_in_construction)
2372 updated = true;
2373 maybe_in_construction = true;
2375 if (!maybe_derived_type && ctx.maybe_derived_type)
2377 updated = true;
2378 maybe_derived_type = true;
2380 if (!dynamic && ctx.dynamic)
2382 updated = true;
2383 dynamic = true;
2386 /* See if one type contains the other as a field (not base). */
2387 else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2388 outer_type, false, false))
2390 if (dump_file && (dump_flags & TDF_DETAILS))
2391 fprintf (dump_file, "Second type contain the first as a field\n");
2393 /* The second type is more specified, so we keep the first.
2394 We need to set DYNAMIC flag to avoid declaring context INVALID
2395 of OFFSET ends up being out of range. */
2396 if (!dynamic
2397 && (ctx.dynamic
2398 || (!otr_type
2399 && (!TYPE_SIZE (ctx.outer_type)
2400 || !TYPE_SIZE (outer_type)
2401 || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2402 TYPE_SIZE (outer_type), 0)))))
2404 dynamic = true;
2405 updated = true;
2408 else if (contains_type_p (outer_type, offset - ctx.offset,
2409 ctx.outer_type, false, false))
2411 if (dump_file && (dump_flags & TDF_DETAILS))
2412 fprintf (dump_file, "First type contain the second as a field\n");
2414 if (!dynamic
2415 && (ctx.dynamic
2416 || (!otr_type
2417 && (!TYPE_SIZE (ctx.outer_type)
2418 || !TYPE_SIZE (outer_type)
2419 || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2420 TYPE_SIZE (outer_type), 0)))))
2421 dynamic = true;
2422 outer_type = ctx.outer_type;
2423 offset = ctx.offset;
2424 dynamic = ctx.dynamic;
2425 maybe_in_construction = ctx.maybe_in_construction;
2426 maybe_derived_type = ctx.maybe_derived_type;
2427 updated = true;
2429 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2430 else if (contains_type_p (ctx.outer_type,
2431 ctx.offset - offset, outer_type, false, true))
2433 if (dump_file && (dump_flags & TDF_DETAILS))
2434 fprintf (dump_file, "First type is base of second\n");
2435 if (!maybe_derived_type)
2437 maybe_derived_type = true;
2438 updated = true;
2440 if (!maybe_in_construction && ctx.maybe_in_construction)
2442 maybe_in_construction = true;
2443 updated = true;
2445 if (!dynamic && ctx.dynamic)
2447 dynamic = true;
2448 updated = true;
2451 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2452 else if (contains_type_p (outer_type,
2453 offset - ctx.offset, ctx.outer_type, false, true))
2455 if (dump_file && (dump_flags & TDF_DETAILS))
2456 fprintf (dump_file, "Second type is base of first\n");
2457 outer_type = ctx.outer_type;
2458 offset = ctx.offset;
2459 updated = true;
2460 if (!maybe_derived_type)
2461 maybe_derived_type = true;
2462 if (!maybe_in_construction && ctx.maybe_in_construction)
2463 maybe_in_construction = true;
2464 if (!dynamic && ctx.dynamic)
2465 dynamic = true;
2467 /* TODO handle merging using hiearchy. */
2468 else
2470 if (dump_file && (dump_flags & TDF_DETAILS))
2471 fprintf (dump_file, "Giving up on meet\n");
2472 clear_outer_type ();
2473 updated = true;
2476 updated |= meet_speculation_with (ctx.speculative_outer_type,
2477 ctx.speculative_offset,
2478 ctx.speculative_maybe_derived_type,
2479 otr_type);
2481 if (updated && dump_file && (dump_flags & TDF_DETAILS))
2483 fprintf (dump_file, "Updated as: ");
2484 dump (dump_file);
2485 fprintf (dump_file, "\n");
2487 return updated;