2015-03-02 Robert Dewar <dewar@adacore.com>
[official-gcc.git] / gcc / ipa-polymorphic-call.c
blob13cc7f647ff1c1888a700803fdde469418e8b919
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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "print-tree.h"
37 #include "calls.h"
38 #include "hashtab.h"
39 #include "hard-reg-set.h"
40 #include "function.h"
41 #include "rtl.h"
42 #include "flags.h"
43 #include "statistics.h"
44 #include "real.h"
45 #include "fixed-value.h"
46 #include "insn-config.h"
47 #include "expmed.h"
48 #include "dojump.h"
49 #include "explow.h"
50 #include "emit-rtl.h"
51 #include "varasm.h"
52 #include "stmt.h"
53 #include "expr.h"
54 #include "tree-pass.h"
55 #include "target.h"
56 #include "tree-pretty-print.h"
57 #include "predict.h"
58 #include "basic-block.h"
59 #include "hash-map.h"
60 #include "is-a.h"
61 #include "plugin-api.h"
62 #include "ipa-ref.h"
63 #include "cgraph.h"
64 #include "ipa-utils.h"
65 #include "tree-ssa-alias.h"
66 #include "internal-fn.h"
67 #include "gimple-fold.h"
68 #include "gimple-expr.h"
69 #include "gimple.h"
70 #include "alloc-pool.h"
71 #include "symbol-summary.h"
72 #include "ipa-prop.h"
73 #include "ipa-inline.h"
74 #include "diagnostic.h"
75 #include "tree-dfa.h"
76 #include "demangle.h"
77 #include "dbgcnt.h"
78 #include "gimple-pretty-print.h"
79 #include "stor-layout.h"
80 #include "intl.h"
81 #include "data-streamer.h"
82 #include "lto-streamer.h"
83 #include "streamer-hooks.h"
85 /* Return true when TYPE contains an polymorphic type and thus is interesting
86 for devirtualization machinery. */
88 static bool contains_type_p (tree, HOST_WIDE_INT, tree,
89 bool consider_placement_new = true,
90 bool consider_bases = true);
92 bool
93 contains_polymorphic_type_p (const_tree type)
95 type = TYPE_MAIN_VARIANT (type);
97 if (RECORD_OR_UNION_TYPE_P (type))
99 if (TYPE_BINFO (type)
100 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
101 return true;
102 for (tree fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
103 if (TREE_CODE (fld) == FIELD_DECL
104 && !DECL_ARTIFICIAL (fld)
105 && contains_polymorphic_type_p (TREE_TYPE (fld)))
106 return true;
107 return false;
109 if (TREE_CODE (type) == ARRAY_TYPE)
110 return contains_polymorphic_type_p (TREE_TYPE (type));
111 return false;
114 /* Return true if it seems valid to use placement new to build EXPECTED_TYPE
115 at possition CUR_OFFSET within TYPE.
117 POD can be changed to an instance of a polymorphic type by
118 placement new. Here we play safe and assume that any
119 non-polymorphic type is POD. */
120 bool
121 possible_placement_new (tree type, tree expected_type,
122 HOST_WIDE_INT cur_offset)
124 return ((TREE_CODE (type) != RECORD_TYPE
125 || !TYPE_BINFO (type)
126 || cur_offset >= POINTER_SIZE
127 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))
128 && (!TYPE_SIZE (type)
129 || !tree_fits_shwi_p (TYPE_SIZE (type))
130 || (cur_offset
131 + (expected_type ? tree_to_uhwi (TYPE_SIZE (expected_type))
132 : POINTER_SIZE)
133 <= tree_to_uhwi (TYPE_SIZE (type)))));
136 /* THIS->OUTER_TYPE is a type of memory object where object of OTR_TYPE
137 is contained at THIS->OFFSET. Walk the memory representation of
138 THIS->OUTER_TYPE and find the outermost class type that match
139 OTR_TYPE or contain OTR_TYPE as a base. Update THIS
140 to represent it.
142 If OTR_TYPE is NULL, just find outermost polymorphic type with
143 virtual table present at possition OFFSET.
145 For example when THIS represents type
146 class A
148 int a;
149 class B b;
151 and we look for type at offset sizeof(int), we end up with B and offset 0.
152 If the same is produced by multiple inheritance, we end up with A and offset
153 sizeof(int).
155 If we can not find corresponding class, give up by setting
156 THIS->OUTER_TYPE to OTR_TYPE and THIS->OFFSET to NULL.
157 Return true when lookup was sucesful.
159 When CONSIDER_PLACEMENT_NEW is false, reject contexts that may be made
160 valid only via alocation of new polymorphic type inside by means
161 of placement new.
163 When CONSIDER_BASES is false, only look for actual fields, not base types
164 of TYPE. */
166 bool
167 ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type,
168 bool consider_placement_new,
169 bool consider_bases)
171 tree type = outer_type;
172 HOST_WIDE_INT cur_offset = offset;
173 bool speculative = false;
174 bool size_unknown = false;
175 unsigned HOST_WIDE_INT otr_type_size = POINTER_SIZE;
177 /* Update OUTER_TYPE to match EXPECTED_TYPE if it is not set. */
178 if (!outer_type)
180 clear_outer_type (otr_type);
181 type = otr_type;
182 cur_offset = 0;
184 /* See if OFFSET points inside OUTER_TYPE. If it does not, we know
185 that the context is either invalid, or the instance type must be
186 derived from OUTER_TYPE.
188 Because the instance type may contain field whose type is of OUTER_TYPE,
189 we can not derive any effective information about it.
191 TODO: In the case we know all derrived types, we can definitely do better
192 here. */
193 else if (TYPE_SIZE (outer_type)
194 && tree_fits_shwi_p (TYPE_SIZE (outer_type))
195 && tree_to_shwi (TYPE_SIZE (outer_type)) >= 0
196 && tree_to_shwi (TYPE_SIZE (outer_type)) <= offset)
198 clear_outer_type (otr_type);
199 type = otr_type;
200 cur_offset = 0;
202 /* If derived type is not allowed, we know that the context is invalid.
203 For dynamic types, we really do not have information about
204 size of the memory location. It is possible that completely
205 different type is stored after outer_type. */
206 if (!maybe_derived_type && !dynamic)
208 clear_speculation ();
209 invalid = true;
210 return false;
214 if (otr_type && TYPE_SIZE (otr_type)
215 && tree_fits_shwi_p (TYPE_SIZE (otr_type)))
216 otr_type_size = tree_to_uhwi (TYPE_SIZE (otr_type));
218 if (!type || offset < 0)
219 goto no_useful_type_info;
221 /* Find the sub-object the constant actually refers to and mark whether it is
222 an artificial one (as opposed to a user-defined one).
224 This loop is performed twice; first time for outer_type and second time
225 for speculative_outer_type. The second run has SPECULATIVE set. */
226 while (true)
228 unsigned HOST_WIDE_INT pos, size;
229 tree fld;
231 /* If we do not know size of TYPE, we need to be more conservative
232 about accepting cases where we can not find EXPECTED_TYPE.
233 Generally the types that do matter here are of constant size.
234 Size_unknown case should be very rare. */
235 if (TYPE_SIZE (type)
236 && tree_fits_shwi_p (TYPE_SIZE (type))
237 && tree_to_shwi (TYPE_SIZE (type)) >= 0)
238 size_unknown = false;
239 else
240 size_unknown = true;
242 /* On a match, just return what we found. */
243 if ((otr_type
244 && types_odr_comparable (type, otr_type)
245 && types_same_for_odr (type, otr_type))
246 || (!otr_type
247 && TREE_CODE (type) == RECORD_TYPE
248 && TYPE_BINFO (type)
249 && polymorphic_type_binfo_p (TYPE_BINFO (type))))
251 if (speculative)
253 /* If we did not match the offset, just give up on speculation. */
254 if (cur_offset != 0
255 /* Also check if speculation did not end up being same as
256 non-speculation. */
257 || (types_must_be_same_for_odr (speculative_outer_type,
258 outer_type)
259 && (maybe_derived_type
260 == speculative_maybe_derived_type)))
261 clear_speculation ();
262 return true;
264 else
266 /* If type is known to be final, do not worry about derived
267 types. Testing it here may help us to avoid speculation. */
268 if (otr_type && TREE_CODE (outer_type) == RECORD_TYPE
269 && (!in_lto_p || odr_type_p (outer_type))
270 && type_known_to_have_no_deriavations_p (outer_type))
271 maybe_derived_type = false;
273 /* Type can not contain itself on an non-zero offset. In that case
274 just give up. Still accept the case where size is now known.
275 Either the second copy may appear past the end of type or within
276 the non-POD buffer located inside the variably sized type
277 itself. */
278 if (cur_offset != 0)
279 goto no_useful_type_info;
280 /* If we determined type precisely or we have no clue on
281 speuclation, we are done. */
282 if (!maybe_derived_type || !speculative_outer_type
283 || !speculation_consistent_p (speculative_outer_type,
284 speculative_offset,
285 speculative_maybe_derived_type,
286 otr_type))
288 clear_speculation ();
289 return true;
291 /* Otherwise look into speculation now. */
292 else
294 speculative = true;
295 type = speculative_outer_type;
296 cur_offset = speculative_offset;
297 continue;
302 /* Walk fields and find corresponding on at OFFSET. */
303 if (TREE_CODE (type) == RECORD_TYPE)
305 for (fld = TYPE_FIELDS (type); fld; fld = DECL_CHAIN (fld))
307 if (TREE_CODE (fld) != FIELD_DECL)
308 continue;
310 pos = int_bit_position (fld);
311 if (pos > (unsigned HOST_WIDE_INT)cur_offset)
312 continue;
314 /* Do not consider vptr itself. Not even for placement new. */
315 if (!pos && DECL_ARTIFICIAL (fld)
316 && POINTER_TYPE_P (TREE_TYPE (fld))
317 && TYPE_BINFO (type)
318 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
319 continue;
321 if (!DECL_SIZE (fld) || !tree_fits_uhwi_p (DECL_SIZE (fld)))
322 goto no_useful_type_info;
323 size = tree_to_uhwi (DECL_SIZE (fld));
325 /* We can always skip types smaller than pointer size:
326 those can not contain a virtual table pointer.
328 Disqualifying fields that are too small to fit OTR_TYPE
329 saves work needed to walk them for no benefit.
330 Because of the way the bases are packed into a class, the
331 field's size may be smaller than type size, so it needs
332 to be done with a care. */
334 if (pos <= (unsigned HOST_WIDE_INT)cur_offset
335 && (pos + size) >= (unsigned HOST_WIDE_INT)cur_offset
336 + POINTER_SIZE
337 && (!otr_type
338 || !TYPE_SIZE (TREE_TYPE (fld))
339 || !tree_fits_shwi_p (TYPE_SIZE (TREE_TYPE (fld)))
340 || (pos + tree_to_uhwi (TYPE_SIZE (TREE_TYPE (fld))))
341 >= cur_offset + otr_type_size))
342 break;
345 if (!fld)
346 goto no_useful_type_info;
348 type = TYPE_MAIN_VARIANT (TREE_TYPE (fld));
349 cur_offset -= pos;
350 /* DECL_ARTIFICIAL represents a basetype. */
351 if (!DECL_ARTIFICIAL (fld))
353 if (!speculative)
355 outer_type = type;
356 offset = cur_offset;
357 /* As soon as we se an field containing the type,
358 we know we are not looking for derivations. */
359 maybe_derived_type = false;
361 else
363 speculative_outer_type = type;
364 speculative_offset = cur_offset;
365 speculative_maybe_derived_type = false;
368 else if (!consider_bases)
369 goto no_useful_type_info;
371 else if (TREE_CODE (type) == ARRAY_TYPE)
373 tree subtype = TYPE_MAIN_VARIANT (TREE_TYPE (type));
375 /* Give up if we don't know array field size.
376 Also give up on non-polymorphic types as they are used
377 as buffers for placement new. */
378 if (!TYPE_SIZE (subtype)
379 || !tree_fits_shwi_p (TYPE_SIZE (subtype))
380 || tree_to_shwi (TYPE_SIZE (subtype)) <= 0
381 || !contains_polymorphic_type_p (subtype))
382 goto no_useful_type_info;
384 HOST_WIDE_INT new_offset = cur_offset % tree_to_shwi (TYPE_SIZE (subtype));
386 /* We may see buffer for placement new. In this case the expected type
387 can be bigger than the subtype. */
388 if (TYPE_SIZE (subtype)
389 && (cur_offset + otr_type_size
390 > tree_to_uhwi (TYPE_SIZE (subtype))))
391 goto no_useful_type_info;
393 cur_offset = new_offset;
394 type = subtype;
395 if (!speculative)
397 outer_type = type;
398 offset = cur_offset;
399 maybe_derived_type = false;
401 else
403 speculative_outer_type = type;
404 speculative_offset = cur_offset;
405 speculative_maybe_derived_type = false;
408 /* Give up on anything else. */
409 else
411 no_useful_type_info:
412 if (maybe_derived_type && !speculative
413 && TREE_CODE (outer_type) == RECORD_TYPE
414 && TREE_CODE (otr_type) == RECORD_TYPE
415 && TYPE_BINFO (otr_type)
416 && !offset
417 && get_binfo_at_offset (TYPE_BINFO (otr_type), 0, outer_type))
419 clear_outer_type (otr_type);
420 if (!speculative_outer_type
421 || !speculation_consistent_p (speculative_outer_type,
422 speculative_offset,
423 speculative_maybe_derived_type,
424 otr_type))
425 clear_speculation ();
426 if (speculative_outer_type)
428 speculative = true;
429 type = speculative_outer_type;
430 cur_offset = speculative_offset;
432 else
433 return true;
435 /* We found no way to embedd EXPECTED_TYPE in TYPE.
436 We still permit two special cases - placement new and
437 the case of variadic types containing themselves. */
438 if (!speculative
439 && consider_placement_new
440 && (size_unknown || !type || maybe_derived_type
441 || possible_placement_new (type, otr_type, cur_offset)))
443 /* In these weird cases we want to accept the context.
444 In non-speculative run we have no useful outer_type info
445 (TODO: we may eventually want to record upper bound on the
446 type size that can be used to prune the walk),
447 but we still want to consider speculation that may
448 give useful info. */
449 if (!speculative)
451 clear_outer_type (otr_type);
452 if (!speculative_outer_type
453 || !speculation_consistent_p (speculative_outer_type,
454 speculative_offset,
455 speculative_maybe_derived_type,
456 otr_type))
457 clear_speculation ();
458 if (speculative_outer_type)
460 speculative = true;
461 type = speculative_outer_type;
462 cur_offset = speculative_offset;
464 else
465 return true;
467 else
468 clear_speculation ();
469 return true;
471 else
473 clear_speculation ();
474 if (speculative)
475 return true;
476 clear_outer_type (otr_type);
477 invalid = true;
478 return false;
484 /* Return true if OUTER_TYPE contains OTR_TYPE at OFFSET.
485 CONSIDER_PLACEMENT_NEW makes function to accept cases where OTR_TYPE can
486 be built within OUTER_TYPE by means of placement new. CONSIDER_BASES makes
487 function to accept cases where OTR_TYPE appears as base of OUTER_TYPE or as
488 base of one of fields of OUTER_TYPE. */
490 static bool
491 contains_type_p (tree outer_type, HOST_WIDE_INT offset,
492 tree otr_type,
493 bool consider_placement_new,
494 bool consider_bases)
496 ipa_polymorphic_call_context context;
498 /* Check that type is within range. */
499 if (offset < 0)
500 return false;
501 if (TYPE_SIZE (outer_type) && TYPE_SIZE (otr_type)
502 && TREE_CODE (outer_type) == INTEGER_CST
503 && TREE_CODE (otr_type) == INTEGER_CST
504 && wi::ltu_p (wi::to_offset (outer_type), (wi::to_offset (otr_type) + offset)))
505 return false;
507 context.offset = offset;
508 context.outer_type = TYPE_MAIN_VARIANT (outer_type);
509 context.maybe_derived_type = false;
510 return context.restrict_to_inner_class (otr_type, consider_placement_new, consider_bases);
514 /* We know that the instance is stored in variable or parameter
515 (not dynamically allocated) and we want to disprove the fact
516 that it may be in construction at invocation of CALL.
518 BASE represents memory location where instance is stored.
519 If BASE is NULL, it is assumed to be global memory.
520 OUTER_TYPE is known type of the instance or NULL if not
521 known.
523 For the variable to be in construction we actually need to
524 be in constructor of corresponding global variable or
525 the inline stack of CALL must contain the constructor.
526 Check this condition. This check works safely only before
527 IPA passes, because inline stacks may become out of date
528 later. */
530 bool
531 decl_maybe_in_construction_p (tree base, tree outer_type,
532 gimple call, tree function)
534 if (outer_type)
535 outer_type = TYPE_MAIN_VARIANT (outer_type);
536 gcc_assert (!base || DECL_P (base));
538 /* After inlining the code unification optimizations may invalidate
539 inline stacks. Also we need to give up on global variables after
540 IPA, because addresses of these may have been propagated to their
541 constructors. */
542 if (DECL_STRUCT_FUNCTION (function)->after_inlining)
543 return true;
545 /* Pure functions can not do any changes on the dynamic type;
546 that require writting to memory. */
547 if ((!base || !auto_var_in_fn_p (base, function))
548 && flags_from_decl_or_type (function) & (ECF_PURE | ECF_CONST))
549 return false;
551 for (tree block = gimple_block (call); block && TREE_CODE (block) == BLOCK;
552 block = BLOCK_SUPERCONTEXT (block))
553 if (BLOCK_ABSTRACT_ORIGIN (block)
554 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block)) == FUNCTION_DECL)
556 tree fn = BLOCK_ABSTRACT_ORIGIN (block);
558 if (TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
559 || (!DECL_CXX_CONSTRUCTOR_P (fn)
560 && !DECL_CXX_DESTRUCTOR_P (fn)))
562 /* Watch for clones where we constant propagated the first
563 argument (pointer to the instance). */
564 fn = DECL_ABSTRACT_ORIGIN (fn);
565 if (!fn
566 || (base && !is_global_var (base))
567 || TREE_CODE (TREE_TYPE (fn)) != METHOD_TYPE
568 || (!DECL_CXX_CONSTRUCTOR_P (fn)
569 && !DECL_CXX_DESTRUCTOR_P (fn)))
570 continue;
572 if (flags_from_decl_or_type (fn) & (ECF_PURE | ECF_CONST))
573 continue;
575 tree type = TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (fn)));
577 if (!outer_type || !types_odr_comparable (type, outer_type))
579 if (TREE_CODE (type) == RECORD_TYPE
580 && TYPE_BINFO (type)
581 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
582 return true;
584 else if (types_same_for_odr (type, outer_type))
585 return true;
588 if (!base || (TREE_CODE (base) == VAR_DECL && is_global_var (base)))
590 if (TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
591 || (!DECL_CXX_CONSTRUCTOR_P (function)
592 && !DECL_CXX_DESTRUCTOR_P (function)))
594 if (!DECL_ABSTRACT_ORIGIN (function))
595 return false;
596 /* Watch for clones where we constant propagated the first
597 argument (pointer to the instance). */
598 function = DECL_ABSTRACT_ORIGIN (function);
599 if (!function
600 || TREE_CODE (TREE_TYPE (function)) != METHOD_TYPE
601 || (!DECL_CXX_CONSTRUCTOR_P (function)
602 && !DECL_CXX_DESTRUCTOR_P (function)))
603 return false;
605 tree type = TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (function)));
606 if (!outer_type || !types_odr_comparable (type, outer_type))
608 if (TREE_CODE (type) == RECORD_TYPE
609 && TYPE_BINFO (type)
610 && polymorphic_type_binfo_p (TYPE_BINFO (type)))
611 return true;
613 else if (types_same_for_odr (type, outer_type))
614 return true;
616 return false;
619 /* Dump human readable context to F. If NEWLINE is true, it will be terminated
620 by a newline. */
622 void
623 ipa_polymorphic_call_context::dump (FILE *f, bool newline) const
625 fprintf (f, " ");
626 if (invalid)
627 fprintf (f, "Call is known to be undefined");
628 else
630 if (useless_p ())
631 fprintf (f, "nothing known");
632 if (outer_type || offset)
634 fprintf (f, "Outer type%s:", dynamic ? " (dynamic)":"");
635 print_generic_expr (f, outer_type, TDF_SLIM);
636 if (maybe_derived_type)
637 fprintf (f, " (or a derived type)");
638 if (maybe_in_construction)
639 fprintf (f, " (maybe in construction)");
640 fprintf (f, " offset "HOST_WIDE_INT_PRINT_DEC,
641 offset);
643 if (speculative_outer_type)
645 if (outer_type || offset)
646 fprintf (f, " ");
647 fprintf (f, "Speculative outer type:");
648 print_generic_expr (f, speculative_outer_type, TDF_SLIM);
649 if (speculative_maybe_derived_type)
650 fprintf (f, " (or a derived type)");
651 fprintf (f, " at offset "HOST_WIDE_INT_PRINT_DEC,
652 speculative_offset);
655 if (newline)
656 fprintf(f, "\n");
659 /* Print context to stderr. */
661 void
662 ipa_polymorphic_call_context::debug () const
664 dump (stderr);
667 /* Stream out the context to OB. */
669 void
670 ipa_polymorphic_call_context::stream_out (struct output_block *ob) const
672 struct bitpack_d bp = bitpack_create (ob->main_stream);
674 bp_pack_value (&bp, invalid, 1);
675 bp_pack_value (&bp, maybe_in_construction, 1);
676 bp_pack_value (&bp, maybe_derived_type, 1);
677 bp_pack_value (&bp, speculative_maybe_derived_type, 1);
678 bp_pack_value (&bp, dynamic, 1);
679 bp_pack_value (&bp, outer_type != NULL, 1);
680 bp_pack_value (&bp, offset != 0, 1);
681 bp_pack_value (&bp, speculative_outer_type != NULL, 1);
682 streamer_write_bitpack (&bp);
684 if (outer_type != NULL)
685 stream_write_tree (ob, outer_type, true);
686 if (offset)
687 streamer_write_hwi (ob, offset);
688 if (speculative_outer_type != NULL)
690 stream_write_tree (ob, speculative_outer_type, true);
691 streamer_write_hwi (ob, speculative_offset);
693 else
694 gcc_assert (!speculative_offset);
697 /* Stream in the context from IB and DATA_IN. */
699 void
700 ipa_polymorphic_call_context::stream_in (struct lto_input_block *ib,
701 struct data_in *data_in)
703 struct bitpack_d bp = streamer_read_bitpack (ib);
705 invalid = bp_unpack_value (&bp, 1);
706 maybe_in_construction = bp_unpack_value (&bp, 1);
707 maybe_derived_type = bp_unpack_value (&bp, 1);
708 speculative_maybe_derived_type = bp_unpack_value (&bp, 1);
709 dynamic = bp_unpack_value (&bp, 1);
710 bool outer_type_p = bp_unpack_value (&bp, 1);
711 bool offset_p = bp_unpack_value (&bp, 1);
712 bool speculative_outer_type_p = bp_unpack_value (&bp, 1);
714 if (outer_type_p)
715 outer_type = stream_read_tree (ib, data_in);
716 else
717 outer_type = NULL;
718 if (offset_p)
719 offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
720 else
721 offset = 0;
722 if (speculative_outer_type_p)
724 speculative_outer_type = stream_read_tree (ib, data_in);
725 speculative_offset = (HOST_WIDE_INT) streamer_read_hwi (ib);
727 else
729 speculative_outer_type = NULL;
730 speculative_offset = 0;
734 /* Proudce polymorphic call context for call method of instance
735 that is located within BASE (that is assumed to be a decl) at offset OFF. */
737 void
738 ipa_polymorphic_call_context::set_by_decl (tree base, HOST_WIDE_INT off)
740 gcc_assert (DECL_P (base));
741 clear_speculation ();
743 if (!contains_polymorphic_type_p (TREE_TYPE (base)))
745 clear_outer_type ();
746 offset = off;
747 return;
749 outer_type = TYPE_MAIN_VARIANT (TREE_TYPE (base));
750 offset = off;
751 /* Make very conservative assumption that all objects
752 may be in construction.
754 It is up to caller to revisit this via
755 get_dynamic_type or decl_maybe_in_construction_p. */
756 maybe_in_construction = true;
757 maybe_derived_type = false;
758 dynamic = false;
761 /* CST is an invariant (address of decl), try to get meaningful
762 polymorphic call context for polymorphic call of method
763 if instance of OTR_TYPE that is located at offset OFF of this invariant.
764 Return FALSE if nothing meaningful can be found. */
766 bool
767 ipa_polymorphic_call_context::set_by_invariant (tree cst,
768 tree otr_type,
769 HOST_WIDE_INT off)
771 HOST_WIDE_INT offset2, size, max_size;
772 tree base;
774 invalid = false;
775 off = 0;
776 clear_outer_type (otr_type);
778 if (TREE_CODE (cst) != ADDR_EXPR)
779 return false;
781 cst = TREE_OPERAND (cst, 0);
782 base = get_ref_base_and_extent (cst, &offset2, &size, &max_size);
783 if (!DECL_P (base) || max_size == -1 || max_size != size)
784 return false;
786 /* Only type inconsistent programs can have otr_type that is
787 not part of outer type. */
788 if (otr_type && !contains_type_p (TREE_TYPE (base), off, otr_type))
789 return false;
791 set_by_decl (base, off);
792 return true;
795 /* See if OP is SSA name initialized as a copy or by single assignment.
796 If so, walk the SSA graph up. Because simple PHI conditional is considered
797 copy, GLOBAL_VISITED may be used to avoid infinite loop walking the SSA
798 graph. */
800 static tree
801 walk_ssa_copies (tree op, hash_set<tree> **global_visited = NULL)
803 hash_set <tree> *visited = NULL;
804 STRIP_NOPS (op);
805 while (TREE_CODE (op) == SSA_NAME
806 && !SSA_NAME_IS_DEFAULT_DEF (op)
807 && SSA_NAME_DEF_STMT (op)
808 && (gimple_assign_single_p (SSA_NAME_DEF_STMT (op))
809 || gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI))
811 if (global_visited)
813 if (!*global_visited)
814 *global_visited = new hash_set<tree>;
815 if ((*global_visited)->add (op))
816 goto done;
818 else
820 if (!visited)
821 visited = new hash_set<tree>;
822 if (visited->add (op))
823 goto done;
825 /* Special case
826 if (ptr == 0)
827 ptr = 0;
828 else
829 ptr = ptr.foo;
830 This pattern is implicitly produced for casts to non-primary
831 bases. When doing context analysis, we do not really care
832 about the case pointer is NULL, becuase the call will be
833 undefined anyway. */
834 if (gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_PHI)
836 gimple phi = SSA_NAME_DEF_STMT (op);
838 if (gimple_phi_num_args (phi) > 2
839 /* We can be called while cleaning up the CFG and can
840 have empty PHIs about to be removed. */
841 || gimple_phi_num_args (phi) == 0)
842 goto done;
843 if (gimple_phi_num_args (phi) == 1)
844 op = gimple_phi_arg_def (phi, 0);
845 else if (integer_zerop (gimple_phi_arg_def (phi, 0)))
846 op = gimple_phi_arg_def (phi, 1);
847 else if (integer_zerop (gimple_phi_arg_def (phi, 1)))
848 op = gimple_phi_arg_def (phi, 0);
849 else
850 goto done;
852 else
854 if (gimple_assign_load_p (SSA_NAME_DEF_STMT (op)))
855 goto done;
856 op = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op));
858 STRIP_NOPS (op);
860 done:
861 if (visited)
862 delete (visited);
863 return op;
866 /* Create polymorphic call context from IP invariant CST.
867 This is typically &global_var.
868 OTR_TYPE specify type of polymorphic call or NULL if unknown, OFF
869 is offset of call. */
871 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree cst,
872 tree otr_type,
873 HOST_WIDE_INT off)
875 clear_speculation ();
876 set_by_invariant (cst, otr_type, off);
879 /* Build context for pointer REF contained in FNDECL at statement STMT.
880 if INSTANCE is non-NULL, return pointer to the object described by
881 the context or DECL where context is contained in. */
883 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl,
884 tree ref,
885 gimple stmt,
886 tree *instance)
888 tree otr_type = NULL;
889 tree base_pointer;
890 hash_set <tree> *visited = NULL;
892 if (TREE_CODE (ref) == OBJ_TYPE_REF)
894 otr_type = obj_type_ref_class (ref);
895 base_pointer = OBJ_TYPE_REF_OBJECT (ref);
897 else
898 base_pointer = ref;
900 /* Set up basic info in case we find nothing interesting in the analysis. */
901 clear_speculation ();
902 clear_outer_type (otr_type);
903 invalid = false;
905 /* Walk SSA for outer object. */
906 while (true)
908 base_pointer = walk_ssa_copies (base_pointer, &visited);
909 if (TREE_CODE (base_pointer) == ADDR_EXPR)
911 HOST_WIDE_INT size, max_size;
912 HOST_WIDE_INT offset2;
913 tree base = get_ref_base_and_extent (TREE_OPERAND (base_pointer, 0),
914 &offset2, &size, &max_size);
916 if (max_size != -1 && max_size == size)
917 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base)),
918 offset + offset2,
919 true,
920 NULL /* Do not change outer type. */);
922 /* If this is a varying address, punt. */
923 if ((TREE_CODE (base) == MEM_REF || DECL_P (base))
924 && max_size != -1
925 && max_size == size)
927 /* We found dereference of a pointer. Type of the pointer
928 and MEM_REF is meaningless, but we can look futher. */
929 if (TREE_CODE (base) == MEM_REF)
931 base_pointer = TREE_OPERAND (base, 0);
932 offset
933 += offset2 + mem_ref_offset (base).to_short_addr () * BITS_PER_UNIT;
934 outer_type = NULL;
936 /* We found base object. In this case the outer_type
937 is known. */
938 else if (DECL_P (base))
940 if (visited)
941 delete (visited);
942 /* Only type inconsistent programs can have otr_type that is
943 not part of outer type. */
944 if (otr_type
945 && !contains_type_p (TREE_TYPE (base),
946 offset + offset2, otr_type))
948 invalid = true;
949 if (instance)
950 *instance = base_pointer;
951 return;
953 set_by_decl (base, offset + offset2);
954 if (outer_type && maybe_in_construction && stmt)
955 maybe_in_construction
956 = decl_maybe_in_construction_p (base,
957 outer_type,
958 stmt,
959 fndecl);
960 if (instance)
961 *instance = base;
962 return;
964 else
965 break;
967 else
968 break;
970 else if (TREE_CODE (base_pointer) == POINTER_PLUS_EXPR
971 && tree_fits_uhwi_p (TREE_OPERAND (base_pointer, 1)))
973 offset += tree_to_shwi (TREE_OPERAND (base_pointer, 1))
974 * BITS_PER_UNIT;
975 base_pointer = TREE_OPERAND (base_pointer, 0);
977 else
978 break;
981 if (visited)
982 delete (visited);
984 /* Try to determine type of the outer object. */
985 if (TREE_CODE (base_pointer) == SSA_NAME
986 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
987 && TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL)
989 /* See if parameter is THIS pointer of a method. */
990 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE
991 && SSA_NAME_VAR (base_pointer) == DECL_ARGUMENTS (fndecl))
993 outer_type
994 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
995 gcc_assert (TREE_CODE (outer_type) == RECORD_TYPE
996 || TREE_CODE (outer_type) == UNION_TYPE);
998 /* Dynamic casting has possibly upcasted the type
999 in the hiearchy. In this case outer type is less
1000 informative than inner type and we should forget
1001 about it. */
1002 if ((otr_type
1003 && !contains_type_p (outer_type, offset,
1004 otr_type))
1005 || !contains_polymorphic_type_p (outer_type))
1007 outer_type = NULL;
1008 if (instance)
1009 *instance = base_pointer;
1010 return;
1013 dynamic = true;
1015 /* If the function is constructor or destructor, then
1016 the type is possibly in construction, but we know
1017 it is not derived type. */
1018 if (DECL_CXX_CONSTRUCTOR_P (fndecl)
1019 || DECL_CXX_DESTRUCTOR_P (fndecl))
1021 maybe_in_construction = true;
1022 maybe_derived_type = false;
1024 else
1026 maybe_derived_type = true;
1027 maybe_in_construction = false;
1029 if (instance)
1030 *instance = base_pointer;
1031 return;
1033 /* Non-PODs passed by value are really passed by invisible
1034 reference. In this case we also know the type of the
1035 object. */
1036 if (DECL_BY_REFERENCE (SSA_NAME_VAR (base_pointer)))
1038 outer_type
1039 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer)));
1040 /* Only type inconsistent programs can have otr_type that is
1041 not part of outer type. */
1042 if (otr_type && !contains_type_p (outer_type, offset,
1043 otr_type))
1045 invalid = true;
1046 if (instance)
1047 *instance = base_pointer;
1048 return;
1050 /* Non-polymorphic types have no interest for us. */
1051 else if (!otr_type && !contains_polymorphic_type_p (outer_type))
1053 outer_type = NULL;
1054 if (instance)
1055 *instance = base_pointer;
1056 return;
1058 maybe_derived_type = false;
1059 maybe_in_construction = false;
1060 if (instance)
1061 *instance = base_pointer;
1062 return;
1066 tree base_type = TREE_TYPE (base_pointer);
1068 if (TREE_CODE (base_pointer) == SSA_NAME
1069 && SSA_NAME_IS_DEFAULT_DEF (base_pointer)
1070 && !(TREE_CODE (SSA_NAME_VAR (base_pointer)) == PARM_DECL
1071 || TREE_CODE (SSA_NAME_VAR (base_pointer)) == RESULT_DECL))
1073 invalid = true;
1074 if (instance)
1075 *instance = base_pointer;
1076 return;
1078 if (TREE_CODE (base_pointer) == SSA_NAME
1079 && SSA_NAME_DEF_STMT (base_pointer)
1080 && gimple_assign_single_p (SSA_NAME_DEF_STMT (base_pointer)))
1081 base_type = TREE_TYPE (gimple_assign_rhs1
1082 (SSA_NAME_DEF_STMT (base_pointer)));
1084 if (base_type && POINTER_TYPE_P (base_type))
1085 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base_type)),
1086 offset,
1087 true, NULL /* Do not change type here */);
1088 /* TODO: There are multiple ways to derive a type. For instance
1089 if BASE_POINTER is passed to an constructor call prior our refernece.
1090 We do not make this type of flow sensitive analysis yet. */
1091 if (instance)
1092 *instance = base_pointer;
1093 return;
1096 /* Structure to be passed in between detect_type_change and
1097 check_stmt_for_type_change. */
1099 struct type_change_info
1101 /* Offset into the object where there is the virtual method pointer we are
1102 looking for. */
1103 HOST_WIDE_INT offset;
1104 /* The declaration or SSA_NAME pointer of the base that we are checking for
1105 type change. */
1106 tree instance;
1107 /* The reference to virtual table pointer used. */
1108 tree vtbl_ptr_ref;
1109 tree otr_type;
1110 /* If we actually can tell the type that the object has changed to, it is
1111 stored in this field. Otherwise it remains NULL_TREE. */
1112 tree known_current_type;
1113 HOST_WIDE_INT known_current_offset;
1115 /* Set to true if dynamic type change has been detected. */
1116 bool type_maybe_changed;
1117 /* Set to true if multiple types have been encountered. known_current_type
1118 must be disregarded in that case. */
1119 bool multiple_types_encountered;
1120 /* Set to true if we possibly missed some dynamic type changes and we should
1121 consider the set to be speculative. */
1122 bool speculative;
1123 bool seen_unanalyzed_store;
1126 /* Return true if STMT is not call and can modify a virtual method table pointer.
1127 We take advantage of fact that vtable stores must appear within constructor
1128 and destructor functions. */
1130 static bool
1131 noncall_stmt_may_be_vtbl_ptr_store (gimple stmt)
1133 if (is_gimple_assign (stmt))
1135 tree lhs = gimple_assign_lhs (stmt);
1137 if (gimple_clobber_p (stmt))
1138 return false;
1139 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs)))
1141 if (flag_strict_aliasing
1142 && !POINTER_TYPE_P (TREE_TYPE (lhs)))
1143 return false;
1145 if (TREE_CODE (lhs) == COMPONENT_REF
1146 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1147 return false;
1148 /* In the future we might want to use get_base_ref_and_offset to find
1149 if there is a field corresponding to the offset and if so, proceed
1150 almost like if it was a component ref. */
1154 /* Code unification may mess with inline stacks. */
1155 if (cfun->after_inlining)
1156 return true;
1158 /* Walk the inline stack and watch out for ctors/dtors.
1159 TODO: Maybe we can require the store to appear in toplevel
1160 block of CTOR/DTOR. */
1161 for (tree block = gimple_block (stmt); block && TREE_CODE (block) == BLOCK;
1162 block = BLOCK_SUPERCONTEXT (block))
1163 if (BLOCK_ABSTRACT_ORIGIN (block)
1164 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block)) == FUNCTION_DECL)
1166 tree fn = BLOCK_ABSTRACT_ORIGIN (block);
1168 if (flags_from_decl_or_type (fn) & (ECF_PURE | ECF_CONST))
1169 return false;
1170 return (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1171 && (DECL_CXX_CONSTRUCTOR_P (fn)
1172 || DECL_CXX_DESTRUCTOR_P (fn)));
1174 return (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE
1175 && (DECL_CXX_CONSTRUCTOR_P (current_function_decl)
1176 || DECL_CXX_DESTRUCTOR_P (current_function_decl)));
1179 /* If STMT can be proved to be an assignment to the virtual method table
1180 pointer of ANALYZED_OBJ and the type associated with the new table
1181 identified, return the type. Otherwise return NULL_TREE if type changes
1182 in unknown way or ERROR_MARK_NODE if type is unchanged. */
1184 static tree
1185 extr_type_from_vtbl_ptr_store (gimple stmt, struct type_change_info *tci,
1186 HOST_WIDE_INT *type_offset)
1188 HOST_WIDE_INT offset, size, max_size;
1189 tree lhs, rhs, base;
1191 if (!gimple_assign_single_p (stmt))
1192 return NULL_TREE;
1194 lhs = gimple_assign_lhs (stmt);
1195 rhs = gimple_assign_rhs1 (stmt);
1196 if (TREE_CODE (lhs) != COMPONENT_REF
1197 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs, 1)))
1199 if (dump_file)
1200 fprintf (dump_file, " LHS is not virtual table.\n");
1201 return NULL_TREE;
1204 if (tci->vtbl_ptr_ref && operand_equal_p (lhs, tci->vtbl_ptr_ref, 0))
1206 else
1208 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1209 if (DECL_P (tci->instance))
1211 if (base != tci->instance)
1213 if (dump_file)
1215 fprintf (dump_file, " base:");
1216 print_generic_expr (dump_file, base, TDF_SLIM);
1217 fprintf (dump_file, " does not match instance:");
1218 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1219 fprintf (dump_file, "\n");
1221 return NULL_TREE;
1224 else if (TREE_CODE (base) == MEM_REF)
1226 if (!operand_equal_p (tci->instance, TREE_OPERAND (base, 0), 0))
1228 if (dump_file)
1230 fprintf (dump_file, " base mem ref:");
1231 print_generic_expr (dump_file, base, TDF_SLIM);
1232 fprintf (dump_file, " does not match instance:");
1233 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1234 fprintf (dump_file, "\n");
1236 return NULL_TREE;
1238 if (!integer_zerop (TREE_OPERAND (base, 1)))
1240 if (!tree_fits_shwi_p (TREE_OPERAND (base, 1)))
1242 if (dump_file)
1244 fprintf (dump_file, " base mem ref:");
1245 print_generic_expr (dump_file, base, TDF_SLIM);
1246 fprintf (dump_file, " has non-representable offset:");
1247 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1248 fprintf (dump_file, "\n");
1250 return NULL_TREE;
1252 else
1253 offset += tree_to_shwi (TREE_OPERAND (base, 1)) * BITS_PER_UNIT;
1256 else if (!operand_equal_p (tci->instance, base, 0)
1257 || tci->offset)
1259 if (dump_file)
1261 fprintf (dump_file, " base:");
1262 print_generic_expr (dump_file, base, TDF_SLIM);
1263 fprintf (dump_file, " does not match instance:");
1264 print_generic_expr (dump_file, tci->instance, TDF_SLIM);
1265 fprintf (dump_file, " with offset %i\n", (int)tci->offset);
1267 return tci->offset > POINTER_SIZE ? error_mark_node : NULL_TREE;
1269 if (offset != tci->offset
1270 || size != POINTER_SIZE
1271 || max_size != POINTER_SIZE)
1273 if (dump_file)
1274 fprintf (dump_file, " wrong offset %i!=%i or size %i\n",
1275 (int)offset, (int)tci->offset, (int)size);
1276 return offset + POINTER_SIZE <= tci->offset
1277 || (max_size != -1
1278 && tci->offset + POINTER_SIZE > offset + max_size)
1279 ? error_mark_node : NULL;
1283 tree vtable;
1284 unsigned HOST_WIDE_INT offset2;
1286 if (!vtable_pointer_value_to_vtable (rhs, &vtable, &offset2))
1288 if (dump_file)
1289 fprintf (dump_file, " Failed to lookup binfo\n");
1290 return NULL;
1293 tree binfo = subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable)),
1294 offset2, vtable);
1295 if (!binfo)
1297 if (dump_file)
1298 fprintf (dump_file, " Construction vtable used\n");
1299 /* FIXME: We should suport construction contexts. */
1300 return NULL;
1303 *type_offset = tree_to_shwi (BINFO_OFFSET (binfo)) * BITS_PER_UNIT;
1304 return DECL_CONTEXT (vtable);
1307 /* Record dynamic type change of TCI to TYPE. */
1309 static void
1310 record_known_type (struct type_change_info *tci, tree type, HOST_WIDE_INT offset)
1312 if (dump_file)
1314 if (type)
1316 fprintf (dump_file, " Recording type: ");
1317 print_generic_expr (dump_file, type, TDF_SLIM);
1318 fprintf (dump_file, " at offset %i\n", (int)offset);
1320 else
1321 fprintf (dump_file, " Recording unknown type\n");
1324 /* If we found a constructor of type that is not polymorphic or
1325 that may contain the type in question as a field (not as base),
1326 restrict to the inner class first to make type matching bellow
1327 happier. */
1328 if (type
1329 && (offset
1330 || (TREE_CODE (type) != RECORD_TYPE
1331 || !TYPE_BINFO (type)
1332 || !polymorphic_type_binfo_p (TYPE_BINFO (type)))))
1334 ipa_polymorphic_call_context context;
1336 context.offset = offset;
1337 context.outer_type = type;
1338 context.maybe_in_construction = false;
1339 context.maybe_derived_type = false;
1340 context.dynamic = true;
1341 /* If we failed to find the inner type, we know that the call
1342 would be undefined for type produced here. */
1343 if (!context.restrict_to_inner_class (tci->otr_type))
1345 if (dump_file)
1346 fprintf (dump_file, " Ignoring; does not contain otr_type\n");
1347 return;
1349 /* Watch for case we reached an POD type and anticipate placement
1350 new. */
1351 if (!context.maybe_derived_type)
1353 type = context.outer_type;
1354 offset = context.offset;
1357 if (tci->type_maybe_changed
1358 && (!types_same_for_odr (type, tci->known_current_type)
1359 || offset != tci->known_current_offset))
1360 tci->multiple_types_encountered = true;
1361 tci->known_current_type = TYPE_MAIN_VARIANT (type);
1362 tci->known_current_offset = offset;
1363 tci->type_maybe_changed = true;
1366 /* Callback of walk_aliased_vdefs and a helper function for
1367 detect_type_change to check whether a particular statement may modify
1368 the virtual table pointer, and if possible also determine the new type of
1369 the (sub-)object. It stores its result into DATA, which points to a
1370 type_change_info structure. */
1372 static bool
1373 check_stmt_for_type_change (ao_ref *ao ATTRIBUTE_UNUSED, tree vdef, void *data)
1375 gimple stmt = SSA_NAME_DEF_STMT (vdef);
1376 struct type_change_info *tci = (struct type_change_info *) data;
1377 tree fn;
1379 /* If we already gave up, just terminate the rest of walk. */
1380 if (tci->multiple_types_encountered)
1381 return true;
1383 if (is_gimple_call (stmt))
1385 if (gimple_call_flags (stmt) & (ECF_CONST | ECF_PURE))
1386 return false;
1388 /* Check for a constructor call. */
1389 if ((fn = gimple_call_fndecl (stmt)) != NULL_TREE
1390 && DECL_CXX_CONSTRUCTOR_P (fn)
1391 && TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE
1392 && gimple_call_num_args (stmt))
1394 tree op = walk_ssa_copies (gimple_call_arg (stmt, 0));
1395 tree type = method_class_type (TREE_TYPE (fn));
1396 HOST_WIDE_INT offset = 0, size, max_size;
1398 if (dump_file)
1400 fprintf (dump_file, " Checking constructor call: ");
1401 print_gimple_stmt (dump_file, stmt, 0, 0);
1404 /* See if THIS parameter seems like instance pointer. */
1405 if (TREE_CODE (op) == ADDR_EXPR)
1407 op = get_ref_base_and_extent (TREE_OPERAND (op, 0),
1408 &offset, &size, &max_size);
1409 if (size != max_size || max_size == -1)
1411 tci->speculative = true;
1412 return false;
1414 if (op && TREE_CODE (op) == MEM_REF)
1416 if (!tree_fits_shwi_p (TREE_OPERAND (op, 1)))
1418 tci->speculative = true;
1419 return false;
1421 offset += tree_to_shwi (TREE_OPERAND (op, 1))
1422 * BITS_PER_UNIT;
1423 op = TREE_OPERAND (op, 0);
1425 else if (DECL_P (op))
1427 else
1429 tci->speculative = true;
1430 return false;
1432 op = walk_ssa_copies (op);
1434 if (operand_equal_p (op, tci->instance, 0)
1435 && TYPE_SIZE (type)
1436 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1437 && tree_fits_shwi_p (TYPE_SIZE (type))
1438 && tree_to_shwi (TYPE_SIZE (type)) + offset > tci->offset)
1440 record_known_type (tci, type, tci->offset - offset);
1441 return true;
1444 /* Calls may possibly change dynamic type by placement new. Assume
1445 it will not happen, but make result speculative only. */
1446 if (dump_file)
1448 fprintf (dump_file, " Function call may change dynamic type:");
1449 print_gimple_stmt (dump_file, stmt, 0, 0);
1451 tci->speculative = true;
1452 return false;
1454 /* Check for inlined virtual table store. */
1455 else if (noncall_stmt_may_be_vtbl_ptr_store (stmt))
1457 tree type;
1458 HOST_WIDE_INT offset = 0;
1459 if (dump_file)
1461 fprintf (dump_file, " Checking vtbl store: ");
1462 print_gimple_stmt (dump_file, stmt, 0, 0);
1465 type = extr_type_from_vtbl_ptr_store (stmt, tci, &offset);
1466 if (type == error_mark_node)
1467 return false;
1468 gcc_assert (!type || TYPE_MAIN_VARIANT (type) == type);
1469 if (!type)
1471 if (dump_file)
1472 fprintf (dump_file, " Unanalyzed store may change type.\n");
1473 tci->seen_unanalyzed_store = true;
1474 tci->speculative = true;
1476 else
1477 record_known_type (tci, type, offset);
1478 return true;
1480 else
1481 return false;
1484 /* THIS is polymorphic call context obtained from get_polymorphic_context.
1485 OTR_OBJECT is pointer to the instance returned by OBJ_TYPE_REF_OBJECT.
1486 INSTANCE is pointer to the outer instance as returned by
1487 get_polymorphic_context. To avoid creation of temporary expressions,
1488 INSTANCE may also be an declaration of get_polymorphic_context found the
1489 value to be in static storage.
1491 If the type of instance is not fully determined
1492 (either OUTER_TYPE is unknown or MAYBE_IN_CONSTRUCTION/INCLUDE_DERIVED_TYPES
1493 is set), try to walk memory writes and find the actual construction of the
1494 instance.
1496 Return true if memory is unchanged from function entry.
1498 We do not include this analysis in the context analysis itself, because
1499 it needs memory SSA to be fully built and the walk may be expensive.
1500 So it is not suitable for use withing fold_stmt and similar uses. */
1502 bool
1503 ipa_polymorphic_call_context::get_dynamic_type (tree instance,
1504 tree otr_object,
1505 tree otr_type,
1506 gimple call)
1508 struct type_change_info tci;
1509 ao_ref ao;
1510 bool function_entry_reached = false;
1511 tree instance_ref = NULL;
1512 gimple stmt = call;
1513 /* Remember OFFSET before it is modified by restrict_to_inner_class.
1514 This is because we do not update INSTANCE when walking inwards. */
1515 HOST_WIDE_INT instance_offset = offset;
1517 if (otr_type)
1518 otr_type = TYPE_MAIN_VARIANT (otr_type);
1520 /* Walk into inner type. This may clear maybe_derived_type and save us
1521 from useless work. It also makes later comparsions with static type
1522 easier. */
1523 if (outer_type && otr_type)
1525 if (!restrict_to_inner_class (otr_type))
1526 return false;
1529 if (!maybe_in_construction && !maybe_derived_type)
1530 return false;
1532 /* We need to obtain refernce to virtual table pointer. It is better
1533 to look it up in the code rather than build our own. This require bit
1534 of pattern matching, but we end up verifying that what we found is
1535 correct.
1537 What we pattern match is:
1539 tmp = instance->_vptr.A; // vtbl ptr load
1540 tmp2 = tmp[otr_token]; // vtable lookup
1541 OBJ_TYPE_REF(tmp2;instance->0) (instance);
1543 We want to start alias oracle walk from vtbl pointer load,
1544 but we may not be able to identify it, for example, when PRE moved the
1545 load around. */
1547 if (gimple_code (call) == GIMPLE_CALL)
1549 tree ref = gimple_call_fn (call);
1550 HOST_WIDE_INT offset2, size, max_size;
1552 if (TREE_CODE (ref) == OBJ_TYPE_REF)
1554 ref = OBJ_TYPE_REF_EXPR (ref);
1555 ref = walk_ssa_copies (ref);
1557 /* Check if definition looks like vtable lookup. */
1558 if (TREE_CODE (ref) == SSA_NAME
1559 && !SSA_NAME_IS_DEFAULT_DEF (ref)
1560 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref))
1561 && TREE_CODE (gimple_assign_rhs1
1562 (SSA_NAME_DEF_STMT (ref))) == MEM_REF)
1564 ref = get_base_address
1565 (TREE_OPERAND (gimple_assign_rhs1
1566 (SSA_NAME_DEF_STMT (ref)), 0));
1567 ref = walk_ssa_copies (ref);
1568 /* Find base address of the lookup and see if it looks like
1569 vptr load. */
1570 if (TREE_CODE (ref) == SSA_NAME
1571 && !SSA_NAME_IS_DEFAULT_DEF (ref)
1572 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref)))
1574 tree ref_exp = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (ref));
1575 tree base_ref = get_ref_base_and_extent
1576 (ref_exp, &offset2, &size, &max_size);
1578 /* Finally verify that what we found looks like read from OTR_OBJECT
1579 or from INSTANCE with offset OFFSET. */
1580 if (base_ref
1581 && ((TREE_CODE (base_ref) == MEM_REF
1582 && ((offset2 == instance_offset
1583 && TREE_OPERAND (base_ref, 0) == instance)
1584 || (!offset2 && TREE_OPERAND (base_ref, 0) == otr_object)))
1585 || (DECL_P (instance) && base_ref == instance
1586 && offset2 == instance_offset)))
1588 stmt = SSA_NAME_DEF_STMT (ref);
1589 instance_ref = ref_exp;
1596 /* If we failed to look up the refernece in code, build our own. */
1597 if (!instance_ref)
1599 /* If the statement in question does not use memory, we can't tell
1600 anything. */
1601 if (!gimple_vuse (stmt))
1602 return false;
1603 ao_ref_init_from_ptr_and_size (&ao, otr_object, NULL);
1605 else
1606 /* Otherwise use the real reference. */
1607 ao_ref_init (&ao, instance_ref);
1609 /* We look for vtbl pointer read. */
1610 ao.size = POINTER_SIZE;
1611 ao.max_size = ao.size;
1612 if (otr_type)
1613 ao.ref_alias_set
1614 = get_deref_alias_set (TREE_TYPE (BINFO_VTABLE (TYPE_BINFO (otr_type))));
1616 if (dump_file)
1618 fprintf (dump_file, "Determining dynamic type for call: ");
1619 print_gimple_stmt (dump_file, call, 0, 0);
1620 fprintf (dump_file, " Starting walk at: ");
1621 print_gimple_stmt (dump_file, stmt, 0, 0);
1622 fprintf (dump_file, " instance pointer: ");
1623 print_generic_expr (dump_file, otr_object, TDF_SLIM);
1624 fprintf (dump_file, " Outer instance pointer: ");
1625 print_generic_expr (dump_file, instance, TDF_SLIM);
1626 fprintf (dump_file, " offset: %i (bits)", (int)offset);
1627 fprintf (dump_file, " vtbl reference: ");
1628 print_generic_expr (dump_file, instance_ref, TDF_SLIM);
1629 fprintf (dump_file, "\n");
1632 tci.offset = offset;
1633 tci.instance = instance;
1634 tci.vtbl_ptr_ref = instance_ref;
1635 gcc_assert (TREE_CODE (instance) != MEM_REF);
1636 tci.known_current_type = NULL_TREE;
1637 tci.known_current_offset = 0;
1638 tci.otr_type = otr_type;
1639 tci.type_maybe_changed = false;
1640 tci.multiple_types_encountered = false;
1641 tci.speculative = false;
1642 tci.seen_unanalyzed_store = false;
1644 walk_aliased_vdefs (&ao, gimple_vuse (stmt), check_stmt_for_type_change,
1645 &tci, NULL, &function_entry_reached);
1647 /* If we did not find any type changing statements, we may still drop
1648 maybe_in_construction flag if the context already have outer type.
1650 Here we make special assumptions about both constructors and
1651 destructors which are all the functions that are allowed to alter the
1652 VMT pointers. It assumes that destructors begin with assignment into
1653 all VMT pointers and that constructors essentially look in the
1654 following way:
1656 1) The very first thing they do is that they call constructors of
1657 ancestor sub-objects that have them.
1659 2) Then VMT pointers of this and all its ancestors is set to new
1660 values corresponding to the type corresponding to the constructor.
1662 3) Only afterwards, other stuff such as constructor of member
1663 sub-objects and the code written by the user is run. Only this may
1664 include calling virtual functions, directly or indirectly.
1666 4) placement new can not be used to change type of non-POD statically
1667 allocated variables.
1669 There is no way to call a constructor of an ancestor sub-object in any
1670 other way.
1672 This means that we do not have to care whether constructors get the
1673 correct type information because they will always change it (in fact,
1674 if we define the type to be given by the VMT pointer, it is undefined).
1676 The most important fact to derive from the above is that if, for some
1677 statement in the section 3, we try to detect whether the dynamic type
1678 has changed, we can safely ignore all calls as we examine the function
1679 body backwards until we reach statements in section 2 because these
1680 calls cannot be ancestor constructors or destructors (if the input is
1681 not bogus) and so do not change the dynamic type (this holds true only
1682 for automatically allocated objects but at the moment we devirtualize
1683 only these). We then must detect that statements in section 2 change
1684 the dynamic type and can try to derive the new type. That is enough
1685 and we can stop, we will never see the calls into constructors of
1686 sub-objects in this code.
1688 Therefore if the static outer type was found (outer_type)
1689 we can safely ignore tci.speculative that is set on calls and give up
1690 only if there was dyanmic type store that may affect given variable
1691 (seen_unanalyzed_store) */
1693 if (!tci.type_maybe_changed
1694 || (outer_type
1695 && !dynamic
1696 && !tci.seen_unanalyzed_store
1697 && !tci.multiple_types_encountered
1698 && offset == tci.offset
1699 && types_same_for_odr (tci.known_current_type,
1700 outer_type)))
1702 if (!outer_type || tci.seen_unanalyzed_store)
1703 return false;
1704 if (maybe_in_construction)
1705 maybe_in_construction = false;
1706 if (dump_file)
1707 fprintf (dump_file, " No dynamic type change found.\n");
1708 return true;
1711 if (tci.known_current_type
1712 && !function_entry_reached
1713 && !tci.multiple_types_encountered)
1715 if (!tci.speculative)
1717 outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1718 offset = tci.known_current_offset;
1719 dynamic = true;
1720 maybe_in_construction = false;
1721 maybe_derived_type = false;
1722 if (dump_file)
1723 fprintf (dump_file, " Determined dynamic type.\n");
1725 else if (!speculative_outer_type
1726 || speculative_maybe_derived_type)
1728 speculative_outer_type = TYPE_MAIN_VARIANT (tci.known_current_type);
1729 speculative_offset = tci.known_current_offset;
1730 speculative_maybe_derived_type = false;
1731 if (dump_file)
1732 fprintf (dump_file, " Determined speculative dynamic type.\n");
1735 else if (dump_file)
1737 fprintf (dump_file, " Found multiple types%s%s\n",
1738 function_entry_reached ? " (function entry reached)" : "",
1739 function_entry_reached ? " (multiple types encountered)" : "");
1742 return false;
1745 /* See if speculation given by SPEC_OUTER_TYPE, SPEC_OFFSET and SPEC_MAYBE_DERIVED_TYPE
1746 seems consistent (and useful) with what we already have in the non-speculative context. */
1748 bool
1749 ipa_polymorphic_call_context::speculation_consistent_p (tree spec_outer_type,
1750 HOST_WIDE_INT spec_offset,
1751 bool spec_maybe_derived_type,
1752 tree otr_type) const
1754 if (!flag_devirtualize_speculatively)
1755 return false;
1757 /* Non-polymorphic types are useless for deriving likely polymorphic
1758 call targets. */
1759 if (!spec_outer_type || !contains_polymorphic_type_p (spec_outer_type))
1760 return false;
1762 /* If we know nothing, speculation is always good. */
1763 if (!outer_type)
1764 return true;
1766 /* Speculation is only useful to avoid derived types.
1767 This is not 100% true for placement new, where the outer context may
1768 turn out to be useless, but ignore these for now. */
1769 if (!maybe_derived_type)
1770 return false;
1772 /* If types agrees, speculation is consistent, but it makes sense only
1773 when it says something new. */
1774 if (types_must_be_same_for_odr (spec_outer_type, outer_type))
1775 return maybe_derived_type && !spec_maybe_derived_type;
1777 /* If speculation does not contain the type in question, ignore it. */
1778 if (otr_type
1779 && !contains_type_p (spec_outer_type, spec_offset, otr_type, false, true))
1780 return false;
1782 /* If outer type already contains speculation as a filed,
1783 it is useless. We already know from OUTER_TYPE
1784 SPEC_TYPE and that it is not in the construction. */
1785 if (contains_type_p (outer_type, offset - spec_offset,
1786 spec_outer_type, false, false))
1787 return false;
1789 /* If speculative outer type is not more specified than outer
1790 type, just give up.
1791 We can only decide this safely if we can compare types with OUTER_TYPE.
1793 if ((!in_lto_p || odr_type_p (outer_type))
1794 && !contains_type_p (spec_outer_type,
1795 spec_offset - offset,
1796 outer_type, false))
1797 return false;
1798 return true;
1801 /* Improve THIS with speculation described by NEW_OUTER_TYPE, NEW_OFFSET,
1802 NEW_MAYBE_DERIVED_TYPE
1803 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1805 bool
1806 ipa_polymorphic_call_context::combine_speculation_with
1807 (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1808 tree otr_type)
1810 if (!new_outer_type)
1811 return false;
1813 /* restrict_to_inner_class may eliminate wrong speculation making our job
1814 easeier. */
1815 if (otr_type)
1816 restrict_to_inner_class (otr_type);
1818 if (!speculation_consistent_p (new_outer_type, new_offset,
1819 new_maybe_derived_type, otr_type))
1820 return false;
1822 /* New speculation is a win in case we have no speculation or new
1823 speculation does not consider derivations. */
1824 if (!speculative_outer_type
1825 || (speculative_maybe_derived_type
1826 && !new_maybe_derived_type))
1828 speculative_outer_type = new_outer_type;
1829 speculative_offset = new_offset;
1830 speculative_maybe_derived_type = new_maybe_derived_type;
1831 return true;
1833 else if (types_must_be_same_for_odr (speculative_outer_type,
1834 new_outer_type))
1836 if (speculative_offset != new_offset)
1838 /* OK we have two contexts that seems valid but they disagree,
1839 just give up.
1841 This is not a lattice operation, so we may want to drop it later. */
1842 if (dump_file && (dump_flags & TDF_DETAILS))
1843 fprintf (dump_file,
1844 "Speculative outer types match, "
1845 "offset mismatch -> invalid speculation\n");
1846 clear_speculation ();
1847 return true;
1849 else
1851 if (speculative_maybe_derived_type && !new_maybe_derived_type)
1853 speculative_maybe_derived_type = false;
1854 return true;
1856 else
1857 return false;
1860 /* Choose type that contains the other. This one either contains the outer
1861 as a field (thus giving exactly one target) or is deeper in the type
1862 hiearchy. */
1863 else if (speculative_outer_type
1864 && speculative_maybe_derived_type
1865 && (new_offset > speculative_offset
1866 || (new_offset == speculative_offset
1867 && contains_type_p (new_outer_type,
1868 0, speculative_outer_type, false))))
1870 tree old_outer_type = speculative_outer_type;
1871 HOST_WIDE_INT old_offset = speculative_offset;
1872 bool old_maybe_derived_type = speculative_maybe_derived_type;
1874 speculative_outer_type = new_outer_type;
1875 speculative_offset = new_offset;
1876 speculative_maybe_derived_type = new_maybe_derived_type;
1878 if (otr_type)
1879 restrict_to_inner_class (otr_type);
1881 /* If the speculation turned out to make no sense, revert to sensible
1882 one. */
1883 if (!speculative_outer_type)
1885 speculative_outer_type = old_outer_type;
1886 speculative_offset = old_offset;
1887 speculative_maybe_derived_type = old_maybe_derived_type;
1888 return false;
1890 return (old_offset != speculative_offset
1891 || old_maybe_derived_type != speculative_maybe_derived_type
1892 || types_must_be_same_for_odr (speculative_outer_type,
1893 new_outer_type));
1895 return false;
1898 /* Make speculation less specific so
1899 NEW_OUTER_TYPE, NEW_OFFSET, NEW_MAYBE_DERIVED_TYPE is also included.
1900 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1902 bool
1903 ipa_polymorphic_call_context::meet_speculation_with
1904 (tree new_outer_type, HOST_WIDE_INT new_offset, bool new_maybe_derived_type,
1905 tree otr_type)
1907 if (!new_outer_type && speculative_outer_type)
1909 clear_speculation ();
1910 return true;
1913 /* restrict_to_inner_class may eliminate wrong speculation making our job
1914 easeier. */
1915 if (otr_type)
1916 restrict_to_inner_class (otr_type);
1918 if (!speculative_outer_type
1919 || !speculation_consistent_p (speculative_outer_type,
1920 speculative_offset,
1921 speculative_maybe_derived_type,
1922 otr_type))
1923 return false;
1925 if (!speculation_consistent_p (new_outer_type, new_offset,
1926 new_maybe_derived_type, otr_type))
1928 clear_speculation ();
1929 return true;
1932 else if (types_must_be_same_for_odr (speculative_outer_type,
1933 new_outer_type))
1935 if (speculative_offset != new_offset)
1937 clear_speculation ();
1938 return true;
1940 else
1942 if (!speculative_maybe_derived_type && new_maybe_derived_type)
1944 speculative_maybe_derived_type = true;
1945 return true;
1947 else
1948 return false;
1951 /* See if one type contains the other as a field (not base). */
1952 else if (contains_type_p (new_outer_type, new_offset - speculative_offset,
1953 speculative_outer_type, false, false))
1954 return false;
1955 else if (contains_type_p (speculative_outer_type,
1956 speculative_offset - new_offset,
1957 new_outer_type, false, false))
1959 speculative_outer_type = new_outer_type;
1960 speculative_offset = new_offset;
1961 speculative_maybe_derived_type = new_maybe_derived_type;
1962 return true;
1964 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
1965 else if (contains_type_p (new_outer_type,
1966 new_offset - speculative_offset,
1967 speculative_outer_type, false, true))
1969 if (!speculative_maybe_derived_type)
1971 speculative_maybe_derived_type = true;
1972 return true;
1974 return false;
1976 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
1977 else if (contains_type_p (speculative_outer_type,
1978 speculative_offset - new_offset, new_outer_type, false, true))
1980 speculative_outer_type = new_outer_type;
1981 speculative_offset = new_offset;
1982 speculative_maybe_derived_type = true;
1983 return true;
1985 else
1987 if (dump_file && (dump_flags & TDF_DETAILS))
1988 fprintf (dump_file, "Giving up on speculative meet\n");
1989 clear_speculation ();
1990 return true;
1994 /* Assume that both THIS and a given context is valid and strenghten THIS
1995 if possible. Return true if any strenghtening was made.
1996 If actual type the context is being used in is known, OTR_TYPE should be
1997 set accordingly. This improves quality of combined result. */
1999 bool
2000 ipa_polymorphic_call_context::combine_with (ipa_polymorphic_call_context ctx,
2001 tree otr_type)
2003 bool updated = false;
2005 if (ctx.useless_p () || invalid)
2006 return false;
2008 /* Restricting context to inner type makes merging easier, however do not
2009 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2010 if (otr_type && !invalid && !ctx.invalid)
2012 restrict_to_inner_class (otr_type);
2013 ctx.restrict_to_inner_class (otr_type);
2014 if(invalid)
2015 return false;
2018 if (dump_file && (dump_flags & TDF_DETAILS))
2020 fprintf (dump_file, "Polymorphic call context combine:");
2021 dump (dump_file);
2022 fprintf (dump_file, "With context: ");
2023 ctx.dump (dump_file);
2024 if (otr_type)
2026 fprintf (dump_file, "To be used with type: ");
2027 print_generic_expr (dump_file, otr_type, TDF_SLIM);
2028 fprintf (dump_file, "\n");
2032 /* If call is known to be invalid, we are done. */
2033 if (ctx.invalid)
2035 if (dump_file && (dump_flags & TDF_DETAILS))
2036 fprintf (dump_file, "-> Invalid context\n");
2037 goto invalidate;
2040 if (!ctx.outer_type)
2042 else if (!outer_type)
2044 outer_type = ctx.outer_type;
2045 offset = ctx.offset;
2046 dynamic = ctx.dynamic;
2047 maybe_in_construction = ctx.maybe_in_construction;
2048 maybe_derived_type = ctx.maybe_derived_type;
2049 updated = true;
2051 /* If types are known to be same, merging is quite easy. */
2052 else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2054 if (offset != ctx.offset
2055 && TYPE_SIZE (outer_type)
2056 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2058 if (dump_file && (dump_flags & TDF_DETAILS))
2059 fprintf (dump_file, "Outer types match, offset mismatch -> invalid\n");
2060 clear_speculation ();
2061 clear_outer_type ();
2062 invalid = true;
2063 return true;
2065 if (dump_file && (dump_flags & TDF_DETAILS))
2066 fprintf (dump_file, "Outer types match, merging flags\n");
2067 if (maybe_in_construction && !ctx.maybe_in_construction)
2069 updated = true;
2070 maybe_in_construction = false;
2072 if (maybe_derived_type && !ctx.maybe_derived_type)
2074 updated = true;
2075 maybe_derived_type = false;
2077 if (dynamic && !ctx.dynamic)
2079 updated = true;
2080 dynamic = false;
2083 /* If we know the type precisely, there is not much to improve. */
2084 else if (!maybe_derived_type && !maybe_in_construction
2085 && !ctx.maybe_derived_type && !ctx.maybe_in_construction)
2087 /* It may be easy to check if second context permits the first
2088 and set INVALID otherwise. This is not easy to do in general;
2089 contains_type_p may return false negatives for non-comparable
2090 types.
2092 If OTR_TYPE is known, we however can expect that
2093 restrict_to_inner_class should have discovered the same base
2094 type. */
2095 if (otr_type && !ctx.maybe_in_construction && !ctx.maybe_derived_type)
2097 if (dump_file && (dump_flags & TDF_DETAILS))
2098 fprintf (dump_file, "Contextes disagree -> invalid\n");
2099 goto invalidate;
2102 /* See if one type contains the other as a field (not base).
2103 In this case we want to choose the wider type, because it contains
2104 more information. */
2105 else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2106 outer_type, false, false))
2108 if (dump_file && (dump_flags & TDF_DETAILS))
2109 fprintf (dump_file, "Second type contain the first as a field\n");
2111 if (maybe_derived_type)
2113 outer_type = ctx.outer_type;
2114 maybe_derived_type = ctx.maybe_derived_type;
2115 offset = ctx.offset;
2116 dynamic = ctx.dynamic;
2117 updated = true;
2120 /* If we do not know how the context is being used, we can
2121 not clear MAYBE_IN_CONSTRUCTION because it may be offseted
2122 to other component of OUTER_TYPE later and we know nothing
2123 about it. */
2124 if (otr_type && maybe_in_construction
2125 && !ctx.maybe_in_construction)
2127 maybe_in_construction = false;
2128 updated = true;
2131 else if (contains_type_p (outer_type, offset - ctx.offset,
2132 ctx.outer_type, false, false))
2134 if (dump_file && (dump_flags & TDF_DETAILS))
2135 fprintf (dump_file, "First type contain the second as a field\n");
2137 if (otr_type && maybe_in_construction
2138 && !ctx.maybe_in_construction)
2140 maybe_in_construction = false;
2141 updated = true;
2144 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2145 else if (contains_type_p (ctx.outer_type,
2146 ctx.offset - offset, outer_type, false, true))
2148 if (dump_file && (dump_flags & TDF_DETAILS))
2149 fprintf (dump_file, "First type is base of second\n");
2150 if (!maybe_derived_type)
2152 if (!ctx.maybe_in_construction
2153 && types_odr_comparable (outer_type, ctx.outer_type))
2155 if (dump_file && (dump_flags & TDF_DETAILS))
2156 fprintf (dump_file, "Second context does not permit base -> invalid\n");
2157 goto invalidate;
2160 /* Pick variant deeper in the hiearchy. */
2161 else
2163 outer_type = ctx.outer_type;
2164 maybe_in_construction = ctx.maybe_in_construction;
2165 maybe_derived_type = ctx.maybe_derived_type;
2166 offset = ctx.offset;
2167 dynamic = ctx.dynamic;
2168 updated = true;
2171 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2172 else if (contains_type_p (outer_type,
2173 offset - ctx.offset, ctx.outer_type, false, true))
2175 if (dump_file && (dump_flags & TDF_DETAILS))
2176 fprintf (dump_file, "Second type is base of first\n");
2177 if (!ctx.maybe_derived_type)
2179 if (!maybe_in_construction
2180 && types_odr_comparable (outer_type, ctx.outer_type))
2182 if (dump_file && (dump_flags & TDF_DETAILS))
2183 fprintf (dump_file, "First context does not permit base -> invalid\n");
2184 goto invalidate;
2186 /* Pick the base type. */
2187 else if (maybe_in_construction)
2189 outer_type = ctx.outer_type;
2190 maybe_in_construction = ctx.maybe_in_construction;
2191 maybe_derived_type = ctx.maybe_derived_type;
2192 offset = ctx.offset;
2193 dynamic = ctx.dynamic;
2194 updated = true;
2198 /* TODO handle merging using hiearchy. */
2199 else if (dump_file && (dump_flags & TDF_DETAILS))
2200 fprintf (dump_file, "Giving up on merge\n");
2202 updated |= combine_speculation_with (ctx.speculative_outer_type,
2203 ctx.speculative_offset,
2204 ctx.speculative_maybe_derived_type,
2205 otr_type);
2207 if (updated && dump_file && (dump_flags & TDF_DETAILS))
2209 fprintf (dump_file, "Updated as: ");
2210 dump (dump_file);
2211 fprintf (dump_file, "\n");
2213 return updated;
2215 invalidate:
2216 invalid = true;
2217 clear_speculation ();
2218 clear_outer_type ();
2219 return true;
2222 /* Take non-speculative info, merge it with speculative and clear speculation.
2223 Used when we no longer manage to keep track of actual outer type, but we
2224 think it is still there.
2226 If OTR_TYPE is set, the transformation can be done more effectively assuming
2227 that context is going to be used only that way. */
2229 void
2230 ipa_polymorphic_call_context::make_speculative (tree otr_type)
2232 tree spec_outer_type = outer_type;
2233 HOST_WIDE_INT spec_offset = offset;
2234 bool spec_maybe_derived_type = maybe_derived_type;
2236 if (invalid)
2238 invalid = false;
2239 clear_outer_type ();
2240 clear_speculation ();
2241 return;
2243 if (!outer_type)
2244 return;
2245 clear_outer_type ();
2246 combine_speculation_with (spec_outer_type, spec_offset,
2247 spec_maybe_derived_type,
2248 otr_type);
2251 /* Use when we can not track dynamic type change. This speculatively assume
2252 type change is not happening. */
2254 void
2255 ipa_polymorphic_call_context::possible_dynamic_type_change (bool in_poly_cdtor,
2256 tree otr_type)
2258 if (dynamic)
2259 make_speculative (otr_type);
2260 else if (in_poly_cdtor)
2261 maybe_in_construction = true;
2264 /* Return TRUE if this context conveys the same information as OTHER. */
2266 bool
2267 ipa_polymorphic_call_context::equal_to
2268 (const ipa_polymorphic_call_context &x) const
2270 if (useless_p ())
2271 return x.useless_p ();
2272 if (invalid)
2273 return x.invalid;
2274 if (x.useless_p () || x.invalid)
2275 return false;
2277 if (outer_type)
2279 if (!x.outer_type
2280 || !types_odr_comparable (outer_type, x.outer_type)
2281 || !types_same_for_odr (outer_type, x.outer_type)
2282 || offset != x.offset
2283 || maybe_in_construction != x.maybe_in_construction
2284 || maybe_derived_type != x.maybe_derived_type
2285 || dynamic != x.dynamic)
2286 return false;
2288 else if (x.outer_type)
2289 return false;
2292 if (speculative_outer_type
2293 && speculation_consistent_p (speculative_outer_type, speculative_offset,
2294 speculative_maybe_derived_type, NULL_TREE))
2296 if (!x.speculative_outer_type)
2297 return false;
2299 if (!types_odr_comparable (speculative_outer_type,
2300 x.speculative_outer_type)
2301 || !types_same_for_odr (speculative_outer_type,
2302 x.speculative_outer_type)
2303 || speculative_offset != x.speculative_offset
2304 || speculative_maybe_derived_type != x.speculative_maybe_derived_type)
2305 return false;
2307 else if (x.speculative_outer_type
2308 && x.speculation_consistent_p (x.speculative_outer_type,
2309 x.speculative_offset,
2310 x.speculative_maybe_derived_type,
2311 NULL))
2312 return false;
2314 return true;
2317 /* Modify context to be strictly less restrictive than CTX. */
2319 bool
2320 ipa_polymorphic_call_context::meet_with (ipa_polymorphic_call_context ctx,
2321 tree otr_type)
2323 bool updated = false;
2325 if (useless_p () || ctx.invalid)
2326 return false;
2328 /* Restricting context to inner type makes merging easier, however do not
2329 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2330 if (otr_type && !useless_p () && !ctx.useless_p ())
2332 restrict_to_inner_class (otr_type);
2333 ctx.restrict_to_inner_class (otr_type);
2334 if(invalid)
2335 return false;
2338 if (equal_to (ctx))
2339 return false;
2341 if (ctx.useless_p () || invalid)
2343 *this = ctx;
2344 return true;
2347 if (dump_file && (dump_flags & TDF_DETAILS))
2349 fprintf (dump_file, "Polymorphic call context meet:");
2350 dump (dump_file);
2351 fprintf (dump_file, "With context: ");
2352 ctx.dump (dump_file);
2353 if (otr_type)
2355 fprintf (dump_file, "To be used with type: ");
2356 print_generic_expr (dump_file, otr_type, TDF_SLIM);
2357 fprintf (dump_file, "\n");
2361 if (!dynamic && ctx.dynamic)
2363 dynamic = true;
2364 updated = true;
2367 /* If call is known to be invalid, we are done. */
2368 if (!outer_type)
2370 else if (!ctx.outer_type)
2372 clear_outer_type ();
2373 updated = true;
2375 /* If types are known to be same, merging is quite easy. */
2376 else if (types_must_be_same_for_odr (outer_type, ctx.outer_type))
2378 if (offset != ctx.offset
2379 && TYPE_SIZE (outer_type)
2380 && TREE_CODE (TYPE_SIZE (outer_type)) == INTEGER_CST)
2382 if (dump_file && (dump_flags & TDF_DETAILS))
2383 fprintf (dump_file, "Outer types match, offset mismatch -> clearing\n");
2384 clear_outer_type ();
2385 return true;
2387 if (dump_file && (dump_flags & TDF_DETAILS))
2388 fprintf (dump_file, "Outer types match, merging flags\n");
2389 if (!maybe_in_construction && ctx.maybe_in_construction)
2391 updated = true;
2392 maybe_in_construction = true;
2394 if (!maybe_derived_type && ctx.maybe_derived_type)
2396 updated = true;
2397 maybe_derived_type = true;
2399 if (!dynamic && ctx.dynamic)
2401 updated = true;
2402 dynamic = true;
2405 /* See if one type contains the other as a field (not base). */
2406 else if (contains_type_p (ctx.outer_type, ctx.offset - offset,
2407 outer_type, false, false))
2409 if (dump_file && (dump_flags & TDF_DETAILS))
2410 fprintf (dump_file, "Second type contain the first as a field\n");
2412 /* The second type is more specified, so we keep the first.
2413 We need to set DYNAMIC flag to avoid declaring context INVALID
2414 of OFFSET ends up being out of range. */
2415 if (!dynamic
2416 && (ctx.dynamic
2417 || (!otr_type
2418 && (!TYPE_SIZE (ctx.outer_type)
2419 || !TYPE_SIZE (outer_type)
2420 || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2421 TYPE_SIZE (outer_type), 0)))))
2423 dynamic = true;
2424 updated = true;
2427 else if (contains_type_p (outer_type, offset - ctx.offset,
2428 ctx.outer_type, false, false))
2430 if (dump_file && (dump_flags & TDF_DETAILS))
2431 fprintf (dump_file, "First type contain the second as a field\n");
2433 if (!dynamic
2434 && (ctx.dynamic
2435 || (!otr_type
2436 && (!TYPE_SIZE (ctx.outer_type)
2437 || !TYPE_SIZE (outer_type)
2438 || !operand_equal_p (TYPE_SIZE (ctx.outer_type),
2439 TYPE_SIZE (outer_type), 0)))))
2440 dynamic = true;
2441 outer_type = ctx.outer_type;
2442 offset = ctx.offset;
2443 dynamic = ctx.dynamic;
2444 maybe_in_construction = ctx.maybe_in_construction;
2445 maybe_derived_type = ctx.maybe_derived_type;
2446 updated = true;
2448 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2449 else if (contains_type_p (ctx.outer_type,
2450 ctx.offset - offset, outer_type, false, true))
2452 if (dump_file && (dump_flags & TDF_DETAILS))
2453 fprintf (dump_file, "First type is base of second\n");
2454 if (!maybe_derived_type)
2456 maybe_derived_type = true;
2457 updated = true;
2459 if (!maybe_in_construction && ctx.maybe_in_construction)
2461 maybe_in_construction = true;
2462 updated = true;
2464 if (!dynamic && ctx.dynamic)
2466 dynamic = true;
2467 updated = true;
2470 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2471 else if (contains_type_p (outer_type,
2472 offset - ctx.offset, ctx.outer_type, false, true))
2474 if (dump_file && (dump_flags & TDF_DETAILS))
2475 fprintf (dump_file, "Second type is base of first\n");
2476 outer_type = ctx.outer_type;
2477 offset = ctx.offset;
2478 updated = true;
2479 if (!maybe_derived_type)
2480 maybe_derived_type = true;
2481 if (!maybe_in_construction && ctx.maybe_in_construction)
2482 maybe_in_construction = true;
2483 if (!dynamic && ctx.dynamic)
2484 dynamic = true;
2486 /* TODO handle merging using hiearchy. */
2487 else
2489 if (dump_file && (dump_flags & TDF_DETAILS))
2490 fprintf (dump_file, "Giving up on meet\n");
2491 clear_outer_type ();
2492 updated = true;
2495 updated |= meet_speculation_with (ctx.speculative_outer_type,
2496 ctx.speculative_offset,
2497 ctx.speculative_maybe_derived_type,
2498 otr_type);
2500 if (updated && dump_file && (dump_flags & TDF_DETAILS))
2502 fprintf (dump_file, "Updated as: ");
2503 dump (dump_file);
2504 fprintf (dump_file, "\n");
2506 return updated;