1 /* Analysis of polymorphic call context.
2 Copyright (C) 2013-2024 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
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
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/>. */
23 #include "coretypes.h"
28 #include "tree-pass.h"
29 #include "tree-ssa-operands.h"
30 #include "streamer-hooks.h"
32 #include "data-streamer.h"
33 #include "diagnostic.h"
35 #include "fold-const.h"
37 #include "ipa-utils.h"
39 #include "gimple-pretty-print.h"
40 #include "tree-into-ssa.h"
41 #include "alloc-pool.h"
42 #include "symbol-summary.h"
43 #include "symtab-thunks.h"
45 /* Return true when TYPE contains an polymorphic type and thus is interesting
46 for devirtualization machinery. */
48 static bool contains_type_p (tree
, HOST_WIDE_INT
, tree
,
49 bool consider_placement_new
= true,
50 bool consider_bases
= true);
53 contains_polymorphic_type_p (const_tree type
)
55 type
= TYPE_MAIN_VARIANT (type
);
57 if (RECORD_OR_UNION_TYPE_P (type
))
60 && polymorphic_type_binfo_p (TYPE_BINFO (type
)))
62 for (tree fld
= TYPE_FIELDS (type
); fld
; fld
= DECL_CHAIN (fld
))
63 if (TREE_CODE (fld
) == FIELD_DECL
64 && !DECL_ARTIFICIAL (fld
)
65 && contains_polymorphic_type_p (TREE_TYPE (fld
)))
69 if (TREE_CODE (type
) == ARRAY_TYPE
)
70 return contains_polymorphic_type_p (TREE_TYPE (type
));
74 /* Return true if it seems valid to use placement new to build EXPECTED_TYPE
75 at position CUR_OFFSET within TYPE.
77 POD can be changed to an instance of a polymorphic type by
78 placement new. Here we play safe and assume that any
79 non-polymorphic type is POD. */
81 possible_placement_new (tree type
, tree expected_type
,
82 HOST_WIDE_INT cur_offset
)
86 return ((TREE_CODE (type
) != RECORD_TYPE
88 || cur_offset
>= POINTER_SIZE
89 || !polymorphic_type_binfo_p (TYPE_BINFO (type
)))
91 || !tree_fits_shwi_p (TYPE_SIZE (type
))
93 + (expected_type
? tree_to_uhwi (TYPE_SIZE (expected_type
))
95 <= tree_to_uhwi (TYPE_SIZE (type
)))));
98 /* THIS->OUTER_TYPE is a type of memory object where object of OTR_TYPE
99 is contained at THIS->OFFSET. Walk the memory representation of
100 THIS->OUTER_TYPE and find the outermost class type that match
101 OTR_TYPE or contain OTR_TYPE as a base. Update THIS
104 If OTR_TYPE is NULL, just find outermost polymorphic type with
105 virtual table present at position OFFSET.
107 For example when THIS represents type
113 and we look for type at offset sizeof(int), we end up with B and offset 0.
114 If the same is produced by multiple inheritance, we end up with A and offset
117 If we cannot find corresponding class, give up by setting
118 THIS->OUTER_TYPE to OTR_TYPE and THIS->OFFSET to NULL.
119 Return true when lookup was successful.
121 When CONSIDER_PLACEMENT_NEW is false, reject contexts that may be made
122 valid only via allocation of new polymorphic type inside by means
125 When CONSIDER_BASES is false, only look for actual fields, not base types
129 ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type
,
130 bool consider_placement_new
,
133 tree type
= outer_type
;
134 HOST_WIDE_INT cur_offset
= offset
;
135 bool speculative
= false;
136 bool size_unknown
= false;
137 unsigned HOST_WIDE_INT otr_type_size
= POINTER_SIZE
;
139 /* Update OUTER_TYPE to match EXPECTED_TYPE if it is not set. */
142 clear_outer_type (otr_type
);
146 /* See if OFFSET points inside OUTER_TYPE. If it does not, we know
147 that the context is either invalid, or the instance type must be
148 derived from OUTER_TYPE.
150 Because the instance type may contain field whose type is of OUTER_TYPE,
151 we cannot derive any effective information about it.
153 TODO: In the case we know all derived types, we can definitely do better
155 else if (TYPE_SIZE (outer_type
)
156 && tree_fits_shwi_p (TYPE_SIZE (outer_type
))
157 && tree_to_shwi (TYPE_SIZE (outer_type
)) >= 0
158 && tree_to_shwi (TYPE_SIZE (outer_type
)) <= offset
)
160 bool der
= maybe_derived_type
; /* clear_outer_type will reset it. */
162 clear_outer_type (otr_type
);
166 /* If derived type is not allowed, we know that the context is invalid.
167 For dynamic types, we really do not have information about
168 size of the memory location. It is possible that completely
169 different type is stored after outer_type. */
172 clear_speculation ();
178 if (otr_type
&& TYPE_SIZE (otr_type
)
179 && tree_fits_shwi_p (TYPE_SIZE (otr_type
)))
180 otr_type_size
= tree_to_uhwi (TYPE_SIZE (otr_type
));
182 if (!type
|| offset
< 0)
183 goto no_useful_type_info
;
185 /* Find the sub-object the constant actually refers to and mark whether it is
186 an artificial one (as opposed to a user-defined one).
188 This loop is performed twice; first time for outer_type and second time
189 for speculative_outer_type. The second run has SPECULATIVE set. */
192 unsigned HOST_WIDE_INT pos
, size
;
195 /* If we do not know size of TYPE, we need to be more conservative
196 about accepting cases where we cannot find EXPECTED_TYPE.
197 Generally the types that do matter here are of constant size.
198 Size_unknown case should be very rare. */
200 && tree_fits_shwi_p (TYPE_SIZE (type
))
201 && tree_to_shwi (TYPE_SIZE (type
)) >= 0)
202 size_unknown
= false;
206 /* On a match, just return what we found. */
208 && types_odr_comparable (type
, otr_type
)
209 && types_same_for_odr (type
, otr_type
))
211 && TREE_CODE (type
) == RECORD_TYPE
213 && polymorphic_type_binfo_p (TYPE_BINFO (type
))))
217 /* If we did not match the offset, just give up on speculation. */
219 /* Also check if speculation did not end up being same as
221 || (types_must_be_same_for_odr (speculative_outer_type
,
223 && (maybe_derived_type
224 == speculative_maybe_derived_type
)))
225 clear_speculation ();
230 /* If type is known to be final, do not worry about derived
231 types. Testing it here may help us to avoid speculation. */
232 if (otr_type
&& TREE_CODE (outer_type
) == RECORD_TYPE
233 && (!in_lto_p
|| odr_type_p (outer_type
))
234 && type_with_linkage_p (outer_type
)
235 && type_known_to_have_no_derivations_p (outer_type
))
236 maybe_derived_type
= false;
238 /* Type cannot contain itself on an non-zero offset. In that case
239 just give up. Still accept the case where size is now known.
240 Either the second copy may appear past the end of type or within
241 the non-POD buffer located inside the variably sized type
244 goto no_useful_type_info
;
245 /* If we determined type precisely or we have no clue on
246 speculation, we are done. */
247 if (!maybe_derived_type
|| !speculative_outer_type
248 || !speculation_consistent_p (speculative_outer_type
,
250 speculative_maybe_derived_type
,
253 clear_speculation ();
256 /* Otherwise look into speculation now. */
260 type
= speculative_outer_type
;
261 cur_offset
= speculative_offset
;
267 /* Walk fields and find corresponding on at OFFSET. */
268 if (TREE_CODE (type
) == RECORD_TYPE
)
270 for (fld
= TYPE_FIELDS (type
); fld
; fld
= DECL_CHAIN (fld
))
272 if (TREE_CODE (fld
) != FIELD_DECL
273 || TREE_TYPE (fld
) == error_mark_node
)
276 pos
= int_bit_position (fld
);
277 if (pos
> (unsigned HOST_WIDE_INT
)cur_offset
)
280 /* Do not consider vptr itself. Not even for placement new. */
281 if (!pos
&& DECL_ARTIFICIAL (fld
)
282 && POINTER_TYPE_P (TREE_TYPE (fld
))
284 && polymorphic_type_binfo_p (TYPE_BINFO (type
)))
287 if (!DECL_SIZE (fld
) || !tree_fits_uhwi_p (DECL_SIZE (fld
)))
288 goto no_useful_type_info
;
289 size
= tree_to_uhwi (DECL_SIZE (fld
));
291 /* We can always skip types smaller than pointer size:
292 those cannot contain a virtual table pointer.
294 Disqualifying fields that are too small to fit OTR_TYPE
295 saves work needed to walk them for no benefit.
296 Because of the way the bases are packed into a class, the
297 field's size may be smaller than type size, so it needs
298 to be done with a care. */
300 if (pos
<= (unsigned HOST_WIDE_INT
)cur_offset
301 && (pos
+ size
) >= (unsigned HOST_WIDE_INT
)cur_offset
304 || !TYPE_SIZE (TREE_TYPE (fld
))
305 || !tree_fits_shwi_p (TYPE_SIZE (TREE_TYPE (fld
)))
306 || (pos
+ tree_to_uhwi (TYPE_SIZE (TREE_TYPE (fld
))))
307 >= cur_offset
+ otr_type_size
))
312 goto no_useful_type_info
;
314 type
= TYPE_MAIN_VARIANT (TREE_TYPE (fld
));
316 /* DECL_ARTIFICIAL represents a basetype. */
317 if (!DECL_ARTIFICIAL (fld
))
323 /* As soon as we see an field containing the type,
324 we know we are not looking for derivations. */
325 maybe_derived_type
= false;
329 speculative_outer_type
= type
;
330 speculative_offset
= cur_offset
;
331 speculative_maybe_derived_type
= false;
334 else if (!consider_bases
)
335 goto no_useful_type_info
;
337 else if (TREE_CODE (type
) == ARRAY_TYPE
)
339 tree subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
341 /* Give up if we don't know array field size.
342 Also give up on non-polymorphic types as they are used
343 as buffers for placement new. */
344 if (!TYPE_SIZE (subtype
)
345 || !tree_fits_shwi_p (TYPE_SIZE (subtype
))
346 || tree_to_shwi (TYPE_SIZE (subtype
)) <= 0
347 || !contains_polymorphic_type_p (subtype
))
348 goto no_useful_type_info
;
350 HOST_WIDE_INT new_offset
= cur_offset
% tree_to_shwi (TYPE_SIZE (subtype
));
352 /* We may see buffer for placement new. In this case the expected type
353 can be bigger than the subtype. */
354 if (TYPE_SIZE (subtype
)
355 && (cur_offset
+ otr_type_size
356 > tree_to_uhwi (TYPE_SIZE (subtype
))))
357 goto no_useful_type_info
;
359 cur_offset
= new_offset
;
360 type
= TYPE_MAIN_VARIANT (subtype
);
365 maybe_derived_type
= false;
369 speculative_outer_type
= type
;
370 speculative_offset
= cur_offset
;
371 speculative_maybe_derived_type
= false;
374 /* Give up on anything else. */
378 if (maybe_derived_type
&& !speculative
379 && TREE_CODE (outer_type
) == RECORD_TYPE
380 && TREE_CODE (otr_type
) == RECORD_TYPE
381 && TYPE_BINFO (otr_type
)
383 && get_binfo_at_offset (TYPE_BINFO (otr_type
), 0, outer_type
))
385 clear_outer_type (otr_type
);
386 if (!speculative_outer_type
387 || !speculation_consistent_p (speculative_outer_type
,
389 speculative_maybe_derived_type
,
391 clear_speculation ();
392 if (speculative_outer_type
)
395 type
= speculative_outer_type
;
396 cur_offset
= speculative_offset
;
401 /* We found no way to embed EXPECTED_TYPE in TYPE.
402 We still permit two special cases - placement new and
403 the case of variadic types containing themselves. */
405 && consider_placement_new
406 && (size_unknown
|| !type
|| maybe_derived_type
407 || possible_placement_new (type
, otr_type
, cur_offset
)))
409 /* In these weird cases we want to accept the context.
410 In non-speculative run we have no useful outer_type info
411 (TODO: we may eventually want to record upper bound on the
412 type size that can be used to prune the walk),
413 but we still want to consider speculation that may
417 clear_outer_type (otr_type
);
418 if (!speculative_outer_type
419 || !speculation_consistent_p (speculative_outer_type
,
421 speculative_maybe_derived_type
,
423 clear_speculation ();
424 if (speculative_outer_type
)
427 type
= speculative_outer_type
;
428 cur_offset
= speculative_offset
;
435 clear_speculation ();
441 clear_speculation ();
444 clear_outer_type (otr_type
);
452 /* Return true if OUTER_TYPE contains OTR_TYPE at OFFSET.
453 CONSIDER_PLACEMENT_NEW makes function to accept cases where OTR_TYPE can
454 be built within OUTER_TYPE by means of placement new. CONSIDER_BASES makes
455 function to accept cases where OTR_TYPE appears as base of OUTER_TYPE or as
456 base of one of fields of OUTER_TYPE. */
459 contains_type_p (tree outer_type
, HOST_WIDE_INT offset
,
461 bool consider_placement_new
,
464 ipa_polymorphic_call_context context
;
466 /* Check that type is within range. */
471 As OUTER_TYPE can be a type which has a diamond virtual inheritance,
472 it's not necessary that INNER_TYPE will fit within OUTER_TYPE with
473 a given offset. It can happen that INNER_TYPE also contains a base object,
474 however it would point to the same instance in the OUTER_TYPE. */
476 context
.offset
= offset
;
477 context
.outer_type
= TYPE_MAIN_VARIANT (outer_type
);
478 context
.maybe_derived_type
= false;
479 context
.dynamic
= false;
480 return context
.restrict_to_inner_class (otr_type
, consider_placement_new
,
485 /* Return a FUNCTION_DECL if FN represent a constructor or destructor.
486 If CHECK_CLONES is true, also check for clones of ctor/dtors. */
489 polymorphic_ctor_dtor_p (tree fn
, bool check_clones
)
491 if (TREE_CODE (TREE_TYPE (fn
)) != METHOD_TYPE
492 || (!DECL_CXX_CONSTRUCTOR_P (fn
) && !DECL_CXX_DESTRUCTOR_P (fn
)))
497 /* Watch for clones where we constant propagated the first
498 argument (pointer to the instance). */
499 fn
= DECL_ABSTRACT_ORIGIN (fn
);
501 || TREE_CODE (TREE_TYPE (fn
)) != METHOD_TYPE
502 || (!DECL_CXX_CONSTRUCTOR_P (fn
) && !DECL_CXX_DESTRUCTOR_P (fn
)))
506 if (flags_from_decl_or_type (fn
) & (ECF_PURE
| ECF_CONST
))
512 /* Return a FUNCTION_DECL if BLOCK represents a constructor or destructor.
513 If CHECK_CLONES is true, also check for clones of ctor/dtors. */
516 inlined_polymorphic_ctor_dtor_block_p (tree block
, bool check_clones
)
518 tree fn
= block_ultimate_origin (block
);
519 if (fn
== NULL
|| TREE_CODE (fn
) != FUNCTION_DECL
)
522 return polymorphic_ctor_dtor_p (fn
, check_clones
);
526 /* We know that the instance is stored in variable or parameter
527 (not dynamically allocated) and we want to disprove the fact
528 that it may be in construction at invocation of CALL.
530 BASE represents memory location where instance is stored.
531 If BASE is NULL, it is assumed to be global memory.
532 OUTER_TYPE is known type of the instance or NULL if not
535 For the variable to be in construction we actually need to
536 be in constructor of corresponding global variable or
537 the inline stack of CALL must contain the constructor.
538 Check this condition. This check works safely only before
539 IPA passes, because inline stacks may become out of date
543 decl_maybe_in_construction_p (tree base
, tree outer_type
,
544 gimple
*call
, tree function
)
547 outer_type
= TYPE_MAIN_VARIANT (outer_type
);
548 gcc_assert (!base
|| DECL_P (base
));
550 /* After inlining the code unification optimizations may invalidate
551 inline stacks. Also we need to give up on global variables after
552 IPA, because addresses of these may have been propagated to their
554 if (DECL_STRUCT_FUNCTION (function
)->after_inlining
)
557 /* Pure functions cannot do any changes on the dynamic type;
558 that require writing to memory. */
559 if ((!base
|| !auto_var_in_fn_p (base
, function
))
560 && flags_from_decl_or_type (function
) & (ECF_PURE
| ECF_CONST
))
563 bool check_clones
= !base
|| is_global_var (base
);
564 for (tree block
= gimple_block (call
); block
&& TREE_CODE (block
) == BLOCK
;
565 block
= BLOCK_SUPERCONTEXT (block
))
566 if (tree fn
= inlined_polymorphic_ctor_dtor_block_p (block
, check_clones
))
568 tree type
= TYPE_METHOD_BASETYPE (TREE_TYPE (fn
));
570 if (!outer_type
|| !types_odr_comparable (type
, outer_type
))
572 if (TREE_CODE (type
) == RECORD_TYPE
574 && polymorphic_type_binfo_p (TYPE_BINFO (type
)))
577 else if (types_same_for_odr (type
, outer_type
))
581 if (!base
|| (VAR_P (base
) && is_global_var (base
)))
583 if (TREE_CODE (TREE_TYPE (function
)) != METHOD_TYPE
584 || (!DECL_CXX_CONSTRUCTOR_P (function
)
585 && !DECL_CXX_DESTRUCTOR_P (function
)))
587 if (!DECL_ABSTRACT_ORIGIN (function
))
589 /* Watch for clones where we constant propagated the first
590 argument (pointer to the instance). */
591 function
= DECL_ABSTRACT_ORIGIN (function
);
593 || TREE_CODE (TREE_TYPE (function
)) != METHOD_TYPE
594 || (!DECL_CXX_CONSTRUCTOR_P (function
)
595 && !DECL_CXX_DESTRUCTOR_P (function
)))
598 tree type
= TYPE_METHOD_BASETYPE (TREE_TYPE (function
));
599 if (!outer_type
|| !types_odr_comparable (type
, outer_type
))
601 if (TREE_CODE (type
) == RECORD_TYPE
603 && polymorphic_type_binfo_p (TYPE_BINFO (type
)))
606 else if (types_same_for_odr (type
, outer_type
))
612 /* Dump human readable context to F. If NEWLINE is true, it will be terminated
616 ipa_polymorphic_call_context::dump (FILE *f
, bool newline
) const
620 fprintf (f
, "Call is known to be undefined");
624 fprintf (f
, "nothing known");
625 if (outer_type
|| offset
)
627 fprintf (f
, "Outer type%s:", dynamic
? " (dynamic)":"");
628 print_generic_expr (f
, outer_type
, TDF_SLIM
);
629 if (maybe_derived_type
)
630 fprintf (f
, " (or a derived type)");
631 if (maybe_in_construction
)
632 fprintf (f
, " (maybe in construction)");
633 fprintf (f
, " offset " HOST_WIDE_INT_PRINT_DEC
,
636 if (speculative_outer_type
)
638 if (outer_type
|| offset
)
640 fprintf (f
, "Speculative outer type:");
641 print_generic_expr (f
, speculative_outer_type
, TDF_SLIM
);
642 if (speculative_maybe_derived_type
)
643 fprintf (f
, " (or a derived type)");
644 fprintf (f
, " at offset " HOST_WIDE_INT_PRINT_DEC
,
652 /* Print context to stderr. */
655 ipa_polymorphic_call_context::debug () const
660 /* Stream out the context to OB. */
663 ipa_polymorphic_call_context::stream_out (struct output_block
*ob
) const
665 struct bitpack_d bp
= bitpack_create (ob
->main_stream
);
667 bp_pack_value (&bp
, invalid
, 1);
668 bp_pack_value (&bp
, maybe_in_construction
, 1);
669 bp_pack_value (&bp
, maybe_derived_type
, 1);
670 bp_pack_value (&bp
, speculative_maybe_derived_type
, 1);
671 bp_pack_value (&bp
, dynamic
, 1);
672 bp_pack_value (&bp
, outer_type
!= NULL
, 1);
673 bp_pack_value (&bp
, offset
!= 0, 1);
674 bp_pack_value (&bp
, speculative_outer_type
!= NULL
, 1);
675 streamer_write_bitpack (&bp
);
677 if (outer_type
!= NULL
)
678 stream_write_tree (ob
, outer_type
, true);
680 streamer_write_hwi (ob
, offset
);
681 if (speculative_outer_type
!= NULL
)
683 stream_write_tree (ob
, speculative_outer_type
, true);
684 streamer_write_hwi (ob
, speculative_offset
);
687 gcc_assert (!speculative_offset
);
690 /* Stream in the context from IB and DATA_IN. */
693 ipa_polymorphic_call_context::stream_in (class lto_input_block
*ib
,
694 class data_in
*data_in
)
696 struct bitpack_d bp
= streamer_read_bitpack (ib
);
698 invalid
= bp_unpack_value (&bp
, 1);
699 maybe_in_construction
= bp_unpack_value (&bp
, 1);
700 maybe_derived_type
= bp_unpack_value (&bp
, 1);
701 speculative_maybe_derived_type
= bp_unpack_value (&bp
, 1);
702 dynamic
= bp_unpack_value (&bp
, 1);
703 bool outer_type_p
= bp_unpack_value (&bp
, 1);
704 bool offset_p
= bp_unpack_value (&bp
, 1);
705 bool speculative_outer_type_p
= bp_unpack_value (&bp
, 1);
708 outer_type
= stream_read_tree (ib
, data_in
);
712 offset
= (HOST_WIDE_INT
) streamer_read_hwi (ib
);
715 if (speculative_outer_type_p
)
717 speculative_outer_type
= stream_read_tree (ib
, data_in
);
718 speculative_offset
= (HOST_WIDE_INT
) streamer_read_hwi (ib
);
722 speculative_outer_type
= NULL
;
723 speculative_offset
= 0;
727 /* Produce polymorphic call context for call method of instance
728 that is located within BASE (that is assumed to be a decl) at offset OFF. */
731 ipa_polymorphic_call_context::set_by_decl (tree base
, HOST_WIDE_INT off
)
733 gcc_assert (DECL_P (base
));
734 clear_speculation ();
736 if (!contains_polymorphic_type_p (TREE_TYPE (base
)))
742 outer_type
= TYPE_MAIN_VARIANT (TREE_TYPE (base
));
744 /* Make very conservative assumption that all objects
745 may be in construction.
747 It is up to caller to revisit this via
748 get_dynamic_type or decl_maybe_in_construction_p. */
749 maybe_in_construction
= true;
750 maybe_derived_type
= false;
754 /* CST is an invariant (address of decl), try to get meaningful
755 polymorphic call context for polymorphic call of method
756 if instance of OTR_TYPE that is located at offset OFF of this invariant.
757 Return FALSE if nothing meaningful can be found. */
760 ipa_polymorphic_call_context::set_by_invariant (tree cst
,
764 poly_int64 offset2
, size
, max_size
;
769 clear_outer_type (otr_type
);
771 if (TREE_CODE (cst
) != ADDR_EXPR
)
774 cst
= TREE_OPERAND (cst
, 0);
775 base
= get_ref_base_and_extent (cst
, &offset2
, &size
, &max_size
, &reverse
);
776 if (!DECL_P (base
) || !known_size_p (max_size
) || maybe_ne (max_size
, size
))
779 /* Only type inconsistent programs can have otr_type that is
780 not part of outer type. */
781 if (otr_type
&& !contains_type_p (TREE_TYPE (base
), off
, otr_type
))
784 set_by_decl (base
, off
);
788 /* See if OP is SSA name initialized as a copy or by single assignment.
789 If so, walk the SSA graph up. Because simple PHI conditional is considered
790 copy, GLOBAL_VISITED may be used to avoid infinite loop walking the SSA
794 walk_ssa_copies (tree op
, hash_set
<tree
> **global_visited
= NULL
)
796 hash_set
<tree
> *visited
= NULL
;
798 while (TREE_CODE (op
) == SSA_NAME
799 && !SSA_NAME_IS_DEFAULT_DEF (op
)
800 /* We might be called via fold_stmt during cfgcleanup where
801 SSA form need not be up-to-date. */
802 && !name_registered_for_update_p (op
)
803 && (gimple_assign_single_p (SSA_NAME_DEF_STMT (op
))
804 || gimple_code (SSA_NAME_DEF_STMT (op
)) == GIMPLE_PHI
))
808 if (!*global_visited
)
809 *global_visited
= new hash_set
<tree
>;
810 if ((*global_visited
)->add (op
))
816 visited
= new hash_set
<tree
>;
817 if (visited
->add (op
))
825 This pattern is implicitly produced for casts to non-primary
826 bases. When doing context analysis, we do not really care
827 about the case pointer is NULL, because the call will be
829 if (gimple_code (SSA_NAME_DEF_STMT (op
)) == GIMPLE_PHI
)
831 gimple
*phi
= SSA_NAME_DEF_STMT (op
);
833 if (gimple_phi_num_args (phi
) > 2)
835 if (gimple_phi_num_args (phi
) == 1)
836 op
= gimple_phi_arg_def (phi
, 0);
837 else if (integer_zerop (gimple_phi_arg_def (phi
, 0)))
838 op
= gimple_phi_arg_def (phi
, 1);
839 else if (integer_zerop (gimple_phi_arg_def (phi
, 1)))
840 op
= gimple_phi_arg_def (phi
, 0);
846 if (gimple_assign_load_p (SSA_NAME_DEF_STMT (op
)))
848 op
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op
));
858 /* Create polymorphic call context from IP invariant CST.
859 This is typically &global_var.
860 OTR_TYPE specify type of polymorphic call or NULL if unknown, OFF
861 is offset of call. */
863 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree cst
,
867 clear_speculation ();
868 set_by_invariant (cst
, otr_type
, off
);
871 /* Build context for pointer REF contained in FNDECL at statement STMT.
872 if INSTANCE is non-NULL, return pointer to the object described by
873 the context or DECL where context is contained in. */
875 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl
,
880 tree otr_type
= NULL
;
882 hash_set
<tree
> *visited
= NULL
;
884 if (TREE_CODE (ref
) == OBJ_TYPE_REF
)
886 otr_type
= obj_type_ref_class (ref
);
887 base_pointer
= OBJ_TYPE_REF_OBJECT (ref
);
892 /* Set up basic info in case we find nothing interesting in the analysis. */
893 clear_speculation ();
894 clear_outer_type (otr_type
);
897 /* Walk SSA for outer object. */
900 base_pointer
= walk_ssa_copies (base_pointer
, &visited
);
901 if (TREE_CODE (base_pointer
) == ADDR_EXPR
)
903 HOST_WIDE_INT offset2
, size
;
906 = get_ref_base_and_extent_hwi (TREE_OPERAND (base_pointer
, 0),
907 &offset2
, &size
, &reverse
);
911 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base
)),
914 NULL
/* Do not change outer type. */);
916 /* If this is a varying address, punt. */
917 if (TREE_CODE (base
) == MEM_REF
|| DECL_P (base
))
919 /* We found dereference of a pointer. Type of the pointer
920 and MEM_REF is meaningless, but we can look further. */
921 offset_int mem_offset
;
922 if (TREE_CODE (base
) == MEM_REF
923 && mem_ref_offset (base
).is_constant (&mem_offset
))
925 offset_int o
= mem_offset
* BITS_PER_UNIT
;
928 if (!wi::fits_shwi_p (o
))
930 base_pointer
= TREE_OPERAND (base
, 0);
931 offset
= o
.to_shwi ();
934 /* We found base object. In this case the outer_type
936 else if (DECL_P (base
))
940 /* Only type inconsistent programs can have otr_type that is
941 not part of outer type. */
943 && !contains_type_p (TREE_TYPE (base
),
944 offset
+ offset2
, otr_type
))
948 *instance
= base_pointer
;
951 set_by_decl (base
, offset
+ offset2
);
952 if (outer_type
&& maybe_in_construction
&& stmt
)
953 maybe_in_construction
954 = decl_maybe_in_construction_p (base
,
968 else if (TREE_CODE (base_pointer
) == POINTER_PLUS_EXPR
969 && TREE_CODE (TREE_OPERAND (base_pointer
, 1)) == INTEGER_CST
)
972 = offset_int::from (wi::to_wide (TREE_OPERAND (base_pointer
, 1)),
976 if (!wi::fits_shwi_p (o
))
978 offset
= o
.to_shwi ();
979 base_pointer
= TREE_OPERAND (base_pointer
, 0);
988 /* Try to determine type of the outer object. */
989 if (TREE_CODE (base_pointer
) == SSA_NAME
990 && SSA_NAME_IS_DEFAULT_DEF (base_pointer
)
991 && TREE_CODE (SSA_NAME_VAR (base_pointer
)) == PARM_DECL
)
993 /* See if parameter is THIS pointer of a method. */
994 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
995 && SSA_NAME_VAR (base_pointer
) == DECL_ARGUMENTS (fndecl
))
998 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer
)));
999 cgraph_node
*node
= cgraph_node::get (current_function_decl
);
1000 gcc_assert (TREE_CODE (outer_type
) == RECORD_TYPE
1001 || TREE_CODE (outer_type
) == UNION_TYPE
);
1003 /* Handle the case we inlined into a thunk. In this case
1004 thunk has THIS pointer of type bar, but it really receives
1005 address to its base type foo which sits in bar at
1006 0-thunk.fixed_offset. It starts with code that adds
1007 think.fixed_offset to the pointer to compensate for this.
1009 Because we walked all the way to the beginning of thunk, we now
1010 see pointer &bar-thunk.fixed_offset and need to compensate
1012 thunk_info
*info
= thunk_info::get (node
);
1013 if (info
&& info
->fixed_offset
)
1014 offset
-= info
->fixed_offset
* BITS_PER_UNIT
;
1016 /* Dynamic casting has possibly upcasted the type
1017 in the hierarchy. In this case outer type is less
1018 informative than inner type and we should forget
1021 && !contains_type_p (outer_type
, offset
,
1023 || !contains_polymorphic_type_p (outer_type
)
1024 /* If we compile thunk with virtual offset, the THIS pointer
1025 is adjusted by unknown value. We can't thus use outer info
1027 || (info
&& info
->virtual_offset_p
))
1031 *instance
= base_pointer
;
1037 /* If the function is constructor or destructor, then
1038 the type is possibly in construction, but we know
1039 it is not derived type. */
1040 if (DECL_CXX_CONSTRUCTOR_P (fndecl
)
1041 || DECL_CXX_DESTRUCTOR_P (fndecl
))
1043 maybe_in_construction
= true;
1044 maybe_derived_type
= false;
1048 maybe_derived_type
= true;
1049 maybe_in_construction
= false;
1053 thunk_info
*info
= thunk_info::get (node
);
1054 /* If method is expanded thunk, we need to apply thunk offset
1055 to instance pointer. */
1056 if (info
&& (info
->virtual_offset_p
|| info
->fixed_offset
))
1059 *instance
= base_pointer
;
1063 /* Non-PODs passed by value are really passed by invisible
1064 reference. In this case we also know the type of the
1066 if (DECL_BY_REFERENCE (SSA_NAME_VAR (base_pointer
)))
1069 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer
)));
1070 /* Only type inconsistent programs can have otr_type that is
1071 not part of outer type. */
1072 if (otr_type
&& !contains_type_p (outer_type
, offset
,
1077 *instance
= base_pointer
;
1080 /* Non-polymorphic types have no interest for us. */
1081 else if (!otr_type
&& !contains_polymorphic_type_p (outer_type
))
1085 *instance
= base_pointer
;
1088 maybe_derived_type
= false;
1089 maybe_in_construction
= false;
1091 *instance
= base_pointer
;
1096 tree base_type
= TREE_TYPE (base_pointer
);
1098 if (TREE_CODE (base_pointer
) == SSA_NAME
1099 && SSA_NAME_IS_DEFAULT_DEF (base_pointer
)
1100 && !(TREE_CODE (SSA_NAME_VAR (base_pointer
)) == PARM_DECL
1101 || TREE_CODE (SSA_NAME_VAR (base_pointer
)) == RESULT_DECL
))
1105 *instance
= base_pointer
;
1108 if (TREE_CODE (base_pointer
) == SSA_NAME
1109 && SSA_NAME_DEF_STMT (base_pointer
)
1110 && gimple_assign_single_p (SSA_NAME_DEF_STMT (base_pointer
)))
1111 base_type
= TREE_TYPE (gimple_assign_rhs1
1112 (SSA_NAME_DEF_STMT (base_pointer
)));
1114 if (base_type
&& POINTER_TYPE_P (base_type
))
1115 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base_type
)),
1117 true, NULL
/* Do not change type here */);
1118 /* TODO: There are multiple ways to derive a type. For instance
1119 if BASE_POINTER is passed to an constructor call prior our reference.
1120 We do not make this type of flow sensitive analysis yet. */
1122 *instance
= base_pointer
;
1126 /* Structure to be passed in between detect_type_change and
1127 check_stmt_for_type_change. */
1129 struct type_change_info
1131 /* Offset into the object where there is the virtual method pointer we are
1133 HOST_WIDE_INT offset
;
1134 /* The declaration or SSA_NAME pointer of the base that we are checking for
1137 /* The reference to virtual table pointer used. */
1140 /* If we actually can tell the type that the object has changed to, it is
1141 stored in this field. Otherwise it remains NULL_TREE. */
1142 tree known_current_type
;
1143 HOST_WIDE_INT known_current_offset
;
1145 /* Set to nonzero if we possibly missed some dynamic type changes and we
1146 should consider the set to be speculative. */
1147 unsigned speculative
;
1149 /* Set to true if dynamic type change has been detected. */
1150 bool type_maybe_changed
;
1151 /* Set to true if multiple types have been encountered. known_current_type
1152 must be disregarded in that case. */
1153 bool multiple_types_encountered
;
1154 bool seen_unanalyzed_store
;
1157 /* Return true if STMT is not call and can modify a virtual method table pointer.
1158 We take advantage of fact that vtable stores must appear within constructor
1159 and destructor functions. */
1162 noncall_stmt_may_be_vtbl_ptr_store (gimple
*stmt
)
1164 if (is_gimple_assign (stmt
))
1166 tree lhs
= gimple_assign_lhs (stmt
);
1168 if (gimple_clobber_p (stmt
))
1170 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs
)))
1172 if (flag_strict_aliasing
1173 && !POINTER_TYPE_P (TREE_TYPE (lhs
)))
1176 if (TREE_CODE (lhs
) == COMPONENT_REF
1177 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs
, 1)))
1179 /* In the future we might want to use get_ref_base_and_extent to find
1180 if there is a field corresponding to the offset and if so, proceed
1181 almost like if it was a component ref. */
1185 /* Code unification may mess with inline stacks. */
1186 if (cfun
->after_inlining
)
1189 /* Walk the inline stack and watch out for ctors/dtors.
1190 TODO: Maybe we can require the store to appear in toplevel
1191 block of CTOR/DTOR. */
1192 for (tree block
= gimple_block (stmt
); block
&& TREE_CODE (block
) == BLOCK
;
1193 block
= BLOCK_SUPERCONTEXT (block
))
1194 if (BLOCK_ABSTRACT_ORIGIN (block
)
1195 && TREE_CODE (block_ultimate_origin (block
)) == FUNCTION_DECL
)
1196 return inlined_polymorphic_ctor_dtor_block_p (block
, false);
1197 return (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
1198 && (DECL_CXX_CONSTRUCTOR_P (current_function_decl
)
1199 || DECL_CXX_DESTRUCTOR_P (current_function_decl
)));
1202 /* If STMT can be proved to be an assignment to the virtual method table
1203 pointer of ANALYZED_OBJ and the type associated with the new table
1204 identified, return the type. Otherwise return NULL_TREE if type changes
1205 in unknown way or ERROR_MARK_NODE if type is unchanged. */
1208 extr_type_from_vtbl_ptr_store (gimple
*stmt
, struct type_change_info
*tci
,
1209 HOST_WIDE_INT
*type_offset
)
1211 poly_int64 offset
, size
, max_size
;
1212 tree lhs
, rhs
, base
;
1215 if (!gimple_assign_single_p (stmt
))
1218 lhs
= gimple_assign_lhs (stmt
);
1219 rhs
= gimple_assign_rhs1 (stmt
);
1220 if (TREE_CODE (lhs
) != COMPONENT_REF
1221 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs
, 1)))
1224 fprintf (dump_file
, " LHS is not virtual table.\n");
1228 if (tci
->vtbl_ptr_ref
&& operand_equal_p (lhs
, tci
->vtbl_ptr_ref
, 0))
1232 base
= get_ref_base_and_extent (lhs
, &offset
, &size
, &max_size
, &reverse
);
1233 if (DECL_P (tci
->instance
))
1235 if (base
!= tci
->instance
)
1239 fprintf (dump_file
, " base:");
1240 print_generic_expr (dump_file
, base
, TDF_SLIM
);
1241 fprintf (dump_file
, " does not match instance:");
1242 print_generic_expr (dump_file
, tci
->instance
, TDF_SLIM
);
1243 fprintf (dump_file
, "\n");
1248 else if (TREE_CODE (base
) == MEM_REF
)
1250 if (!operand_equal_p (tci
->instance
, TREE_OPERAND (base
, 0), 0))
1254 fprintf (dump_file
, " base mem ref:");
1255 print_generic_expr (dump_file
, base
, TDF_SLIM
);
1256 fprintf (dump_file
, " does not match instance:");
1257 print_generic_expr (dump_file
, tci
->instance
, TDF_SLIM
);
1258 fprintf (dump_file
, "\n");
1262 if (!integer_zerop (TREE_OPERAND (base
, 1)))
1264 if (!tree_fits_shwi_p (TREE_OPERAND (base
, 1)))
1268 fprintf (dump_file
, " base mem ref:");
1269 print_generic_expr (dump_file
, base
, TDF_SLIM
);
1270 fprintf (dump_file
, " has non-representable offset:");
1271 print_generic_expr (dump_file
, tci
->instance
, TDF_SLIM
);
1272 fprintf (dump_file
, "\n");
1277 offset
+= tree_to_shwi (TREE_OPERAND (base
, 1)) * BITS_PER_UNIT
;
1280 else if (!operand_equal_p (tci
->instance
, base
, 0)
1285 fprintf (dump_file
, " base:");
1286 print_generic_expr (dump_file
, base
, TDF_SLIM
);
1287 fprintf (dump_file
, " does not match instance:");
1288 print_generic_expr (dump_file
, tci
->instance
, TDF_SLIM
);
1289 fprintf (dump_file
, " with offset %i\n", (int)tci
->offset
);
1291 return tci
->offset
> POINTER_SIZE
? error_mark_node
: NULL_TREE
;
1293 if (maybe_ne (offset
, tci
->offset
)
1294 || maybe_ne (size
, POINTER_SIZE
)
1295 || maybe_ne (max_size
, POINTER_SIZE
))
1299 fprintf (dump_file
, " wrong offset ");
1300 print_dec (offset
, dump_file
);
1301 fprintf (dump_file
, "!=%i or size ", (int) tci
->offset
);
1302 print_dec (size
, dump_file
);
1303 fprintf (dump_file
, "\n");
1305 return (known_le (offset
+ POINTER_SIZE
, tci
->offset
)
1306 || (known_size_p (max_size
)
1307 && known_gt (tci
->offset
+ POINTER_SIZE
,
1309 ? error_mark_node
: NULL
);
1314 unsigned HOST_WIDE_INT offset2
;
1316 if (!vtable_pointer_value_to_vtable (rhs
, &vtable
, &offset2
))
1319 fprintf (dump_file
, " Failed to lookup binfo\n");
1323 tree binfo
= subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable
)),
1328 fprintf (dump_file
, " Construction vtable used\n");
1329 /* FIXME: We should support construction contexts. */
1333 *type_offset
= tree_to_shwi (BINFO_OFFSET (binfo
)) * BITS_PER_UNIT
;
1334 return DECL_CONTEXT (vtable
);
1337 /* Record dynamic type change of TCI to TYPE. */
1340 record_known_type (struct type_change_info
*tci
, tree type
, HOST_WIDE_INT offset
)
1346 fprintf (dump_file
, " Recording type: ");
1347 print_generic_expr (dump_file
, type
, TDF_SLIM
);
1348 fprintf (dump_file
, " at offset %i\n", (int)offset
);
1351 fprintf (dump_file
, " Recording unknown type\n");
1354 /* If we found a constructor of type that is not polymorphic or
1355 that may contain the type in question as a field (not as base),
1356 restrict to the inner class first to make type matching bellow
1360 || (TREE_CODE (type
) != RECORD_TYPE
1361 || !TYPE_BINFO (type
)
1362 || !polymorphic_type_binfo_p (TYPE_BINFO (type
)))))
1364 ipa_polymorphic_call_context context
;
1366 context
.offset
= offset
;
1367 context
.outer_type
= type
;
1368 context
.maybe_in_construction
= false;
1369 context
.maybe_derived_type
= false;
1370 context
.dynamic
= true;
1371 /* If we failed to find the inner type, we know that the call
1372 would be undefined for type produced here. */
1373 if (!context
.restrict_to_inner_class (tci
->otr_type
))
1376 fprintf (dump_file
, " Ignoring; does not contain otr_type\n");
1379 /* Watch for case we reached an POD type and anticipate placement
1381 if (!context
.maybe_derived_type
)
1383 type
= context
.outer_type
;
1384 offset
= context
.offset
;
1387 if (tci
->type_maybe_changed
1388 && (!types_same_for_odr (type
, tci
->known_current_type
)
1389 || offset
!= tci
->known_current_offset
))
1390 tci
->multiple_types_encountered
= true;
1391 tci
->known_current_type
= TYPE_MAIN_VARIANT (type
);
1392 tci
->known_current_offset
= offset
;
1393 tci
->type_maybe_changed
= true;
1397 /* The maximum number of may-defs we visit when looking for a must-def
1398 that changes the dynamic type in check_stmt_for_type_change. Tuned
1399 after the PR12392 testcase which unlimited spends 40% time within
1400 these alias walks and 8% with the following limit. */
1403 csftc_abort_walking_p (unsigned speculative
)
1405 unsigned max
= param_max_speculative_devirt_maydefs
;
1406 return speculative
> max
? true : false;
1409 /* Callback of walk_aliased_vdefs and a helper function for
1410 detect_type_change to check whether a particular statement may modify
1411 the virtual table pointer, and if possible also determine the new type of
1412 the (sub-)object. It stores its result into DATA, which points to a
1413 type_change_info structure. */
1416 check_stmt_for_type_change (ao_ref
*ao ATTRIBUTE_UNUSED
, tree vdef
, void *data
)
1418 gimple
*stmt
= SSA_NAME_DEF_STMT (vdef
);
1419 struct type_change_info
*tci
= (struct type_change_info
*) data
;
1422 /* If we already gave up, just terminate the rest of walk. */
1423 if (tci
->multiple_types_encountered
)
1426 if (is_gimple_call (stmt
))
1428 if (gimple_call_flags (stmt
) & (ECF_CONST
| ECF_PURE
))
1431 /* Check for a constructor call. */
1432 if ((fn
= gimple_call_fndecl (stmt
)) != NULL_TREE
1433 && DECL_CXX_CONSTRUCTOR_P (fn
)
1434 && TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
1435 && gimple_call_num_args (stmt
))
1437 tree op
= walk_ssa_copies (gimple_call_arg (stmt
, 0));
1438 tree type
= TYPE_METHOD_BASETYPE (TREE_TYPE (fn
));
1439 HOST_WIDE_INT offset
= 0;
1444 fprintf (dump_file
, " Checking constructor call: ");
1445 print_gimple_stmt (dump_file
, stmt
, 0);
1448 /* See if THIS parameter seems like instance pointer. */
1449 if (TREE_CODE (op
) == ADDR_EXPR
)
1452 op
= get_ref_base_and_extent_hwi (TREE_OPERAND (op
, 0),
1453 &offset
, &size
, &reverse
);
1457 return csftc_abort_walking_p (tci
->speculative
);
1459 if (TREE_CODE (op
) == MEM_REF
)
1461 if (!tree_fits_shwi_p (TREE_OPERAND (op
, 1)))
1464 return csftc_abort_walking_p (tci
->speculative
);
1466 offset
+= tree_to_shwi (TREE_OPERAND (op
, 1))
1468 op
= TREE_OPERAND (op
, 0);
1470 else if (DECL_P (op
))
1475 return csftc_abort_walking_p (tci
->speculative
);
1477 op
= walk_ssa_copies (op
);
1479 if (operand_equal_p (op
, tci
->instance
, 0)
1481 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
1482 && tree_fits_shwi_p (TYPE_SIZE (type
))
1483 && tree_to_shwi (TYPE_SIZE (type
)) + offset
> tci
->offset
1484 /* Some inlined constructors may look as follows:
1485 _3 = operator new (16);
1486 MEM[(struct &)_3] ={v} {CLOBBER};
1487 MEM[(struct CompositeClass *)_3]._vptr.CompositeClass
1488 = &MEM[(void *)&_ZTV14CompositeClass + 16B];
1489 _7 = &MEM[(struct CompositeClass *)_3].object;
1490 EmptyClass::EmptyClass (_7);
1492 When determining dynamic type of _3 and because we stop at first
1493 dynamic type found, we would stop on EmptyClass::EmptyClass (_7).
1494 In this case the emptyclass is not even polymorphic and we miss
1495 it is contained in an outer type that is polymorphic. */
1497 && (tci
->offset
== offset
|| contains_polymorphic_type_p (type
)))
1499 record_known_type (tci
, type
, tci
->offset
- offset
);
1503 /* Calls may possibly change dynamic type by placement new. Assume
1504 it will not happen, but make result speculative only. */
1507 fprintf (dump_file
, " Function call may change dynamic type:");
1508 print_gimple_stmt (dump_file
, stmt
, 0);
1511 return csftc_abort_walking_p (tci
->speculative
);
1513 /* Check for inlined virtual table store. */
1514 else if (noncall_stmt_may_be_vtbl_ptr_store (stmt
))
1517 HOST_WIDE_INT offset
= 0;
1520 fprintf (dump_file
, " Checking vtbl store: ");
1521 print_gimple_stmt (dump_file
, stmt
, 0);
1524 type
= extr_type_from_vtbl_ptr_store (stmt
, tci
, &offset
);
1525 if (type
== error_mark_node
)
1527 gcc_assert (!type
|| TYPE_MAIN_VARIANT (type
) == type
);
1531 fprintf (dump_file
, " Unanalyzed store may change type.\n");
1532 tci
->seen_unanalyzed_store
= true;
1536 record_known_type (tci
, type
, offset
);
1543 /* THIS is polymorphic call context obtained from get_polymorphic_context.
1544 OTR_OBJECT is pointer to the instance returned by OBJ_TYPE_REF_OBJECT.
1545 INSTANCE is pointer to the outer instance as returned by
1546 get_polymorphic_context. To avoid creation of temporary expressions,
1547 INSTANCE may also be an declaration of get_polymorphic_context found the
1548 value to be in static storage.
1550 If the type of instance is not fully determined
1551 (either OUTER_TYPE is unknown or MAYBE_IN_CONSTRUCTION/INCLUDE_DERIVED_TYPES
1552 is set), try to walk memory writes and find the actual construction of the
1555 Return true if memory is unchanged from function entry.
1557 We do not include this analysis in the context analysis itself, because
1558 it needs memory SSA to be fully built and the walk may be expensive.
1559 So it is not suitable for use withing fold_stmt and similar uses.
1561 AA_WALK_BUDGET_P, if not NULL, is how statements we should allow
1562 walk_aliased_vdefs to examine. The value should be decremented by the
1563 number of statements we examined or set to zero if exhausted. */
1566 ipa_polymorphic_call_context::get_dynamic_type (tree instance
,
1570 unsigned *aa_walk_budget_p
)
1572 struct type_change_info tci
;
1574 bool function_entry_reached
= false;
1575 tree instance_ref
= NULL
;
1576 gimple
*stmt
= call
;
1577 /* Remember OFFSET before it is modified by restrict_to_inner_class.
1578 This is because we do not update INSTANCE when walking inwards. */
1579 HOST_WIDE_INT instance_offset
= offset
;
1580 tree instance_outer_type
= outer_type
;
1586 otr_type
= TYPE_MAIN_VARIANT (otr_type
);
1588 /* Walk into inner type. This may clear maybe_derived_type and save us
1589 from useless work. It also makes later comparisons with static type
1591 if (outer_type
&& otr_type
)
1593 if (!restrict_to_inner_class (otr_type
))
1597 if (!maybe_in_construction
&& !maybe_derived_type
)
1600 /* If we are in fact not looking at any object or the instance is
1601 some placement new into a random load, give up straight away. */
1602 if (TREE_CODE (instance
) == MEM_REF
)
1605 /* We need to obtain reference to virtual table pointer. It is better
1606 to look it up in the code rather than build our own. This require bit
1607 of pattern matching, but we end up verifying that what we found is
1610 What we pattern match is:
1612 tmp = instance->_vptr.A; // vtbl ptr load
1613 tmp2 = tmp[otr_token]; // vtable lookup
1614 OBJ_TYPE_REF(tmp2;instance->0) (instance);
1616 We want to start alias oracle walk from vtbl pointer load,
1617 but we may not be able to identify it, for example, when PRE moved the
1620 if (gimple_code (call
) == GIMPLE_CALL
)
1622 tree ref
= gimple_call_fn (call
);
1625 if (TREE_CODE (ref
) == OBJ_TYPE_REF
)
1627 ref
= OBJ_TYPE_REF_EXPR (ref
);
1628 ref
= walk_ssa_copies (ref
);
1630 /* If call target is already known, no need to do the expensive
1632 if (is_gimple_min_invariant (ref
))
1635 /* Check if definition looks like vtable lookup. */
1636 if (TREE_CODE (ref
) == SSA_NAME
1637 && !SSA_NAME_IS_DEFAULT_DEF (ref
)
1638 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref
))
1639 && TREE_CODE (gimple_assign_rhs1
1640 (SSA_NAME_DEF_STMT (ref
))) == MEM_REF
)
1642 ref
= get_base_address
1643 (TREE_OPERAND (gimple_assign_rhs1
1644 (SSA_NAME_DEF_STMT (ref
)), 0));
1645 ref
= walk_ssa_copies (ref
);
1646 /* Find base address of the lookup and see if it looks like
1648 if (TREE_CODE (ref
) == SSA_NAME
1649 && !SSA_NAME_IS_DEFAULT_DEF (ref
)
1650 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref
)))
1652 HOST_WIDE_INT offset2
, size
;
1653 tree ref_exp
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (ref
));
1655 = get_ref_base_and_extent_hwi (ref_exp
, &offset2
,
1658 /* Finally verify that what we found looks like read from
1659 OTR_OBJECT or from INSTANCE with offset OFFSET. */
1661 && ((TREE_CODE (base_ref
) == MEM_REF
1662 && ((offset2
== instance_offset
1663 && TREE_OPERAND (base_ref
, 0) == instance
)
1665 && TREE_OPERAND (base_ref
, 0)
1667 || (DECL_P (instance
) && base_ref
== instance
1668 && offset2
== instance_offset
)))
1670 stmt
= SSA_NAME_DEF_STMT (ref
);
1671 instance_ref
= ref_exp
;
1678 /* If we failed to look up the reference in code, build our own. */
1681 /* If the statement in question does not use memory, we can't tell
1683 if (!gimple_vuse (stmt
))
1685 ao_ref_init_from_ptr_and_size (&ao
, otr_object
, NULL
);
1688 /* Otherwise use the real reference. */
1689 ao_ref_init (&ao
, instance_ref
);
1691 /* We look for vtbl pointer read. */
1692 ao
.size
= POINTER_SIZE
;
1693 ao
.max_size
= ao
.size
;
1694 /* We are looking for stores to vptr pointer within the instance of
1696 TODO: The vptr pointer type is globally known, we probably should
1697 keep it and do that even when otr_type is unknown. */
1701 = get_alias_set (outer_type
? outer_type
: otr_type
);
1703 = get_alias_set (TREE_TYPE (BINFO_VTABLE (TYPE_BINFO (otr_type
))));
1708 fprintf (dump_file
, "Determining dynamic type for call: ");
1709 print_gimple_stmt (dump_file
, call
, 0);
1710 fprintf (dump_file
, " Starting walk at: ");
1711 print_gimple_stmt (dump_file
, stmt
, 0);
1712 fprintf (dump_file
, " instance pointer: ");
1713 print_generic_expr (dump_file
, otr_object
, TDF_SLIM
);
1714 fprintf (dump_file
, " Outer instance pointer: ");
1715 print_generic_expr (dump_file
, instance
, TDF_SLIM
);
1716 fprintf (dump_file
, " offset: %i (bits)", (int)instance_offset
);
1717 fprintf (dump_file
, " vtbl reference: ");
1718 print_generic_expr (dump_file
, instance_ref
, TDF_SLIM
);
1719 fprintf (dump_file
, "\n");
1722 tci
.offset
= instance_offset
;
1723 tci
.instance
= instance
;
1724 tci
.vtbl_ptr_ref
= instance_ref
;
1725 tci
.known_current_type
= NULL_TREE
;
1726 tci
.known_current_offset
= 0;
1727 tci
.otr_type
= otr_type
;
1728 tci
.type_maybe_changed
= false;
1729 tci
.multiple_types_encountered
= false;
1730 tci
.speculative
= 0;
1731 tci
.seen_unanalyzed_store
= false;
1733 unsigned aa_walk_budget
= 0;
1734 if (aa_walk_budget_p
)
1735 aa_walk_budget
= *aa_walk_budget_p
+ 1;
1738 = walk_aliased_vdefs (&ao
, gimple_vuse (stmt
), check_stmt_for_type_change
,
1739 &tci
, NULL
, &function_entry_reached
, aa_walk_budget
);
1741 /* If we did not find any type changing statements, we may still drop
1742 maybe_in_construction flag if the context already have outer type.
1744 Here we make special assumptions about both constructors and
1745 destructors which are all the functions that are allowed to alter the
1746 VMT pointers. It assumes that destructors begin with assignment into
1747 all VMT pointers and that constructors essentially look in the
1750 1) The very first thing they do is that they call constructors of
1751 ancestor sub-objects that have them.
1753 2) Then VMT pointers of this and all its ancestors is set to new
1754 values corresponding to the type corresponding to the constructor.
1756 3) Only afterwards, other stuff such as constructor of member
1757 sub-objects and the code written by the user is run. Only this may
1758 include calling virtual functions, directly or indirectly.
1760 4) placement new cannot be used to change type of non-POD statically
1761 allocated variables.
1763 There is no way to call a constructor of an ancestor sub-object in any
1766 This means that we do not have to care whether constructors get the
1767 correct type information because they will always change it (in fact,
1768 if we define the type to be given by the VMT pointer, it is undefined).
1770 The most important fact to derive from the above is that if, for some
1771 statement in the section 3, we try to detect whether the dynamic type
1772 has changed, we can safely ignore all calls as we examine the function
1773 body backwards until we reach statements in section 2 because these
1774 calls cannot be ancestor constructors or destructors (if the input is
1775 not bogus) and so do not change the dynamic type (this holds true only
1776 for automatically allocated objects but at the moment we devirtualize
1777 only these). We then must detect that statements in section 2 change
1778 the dynamic type and can try to derive the new type. That is enough
1779 and we can stop, we will never see the calls into constructors of
1780 sub-objects in this code.
1782 Therefore if the static outer type was found (outer_type)
1783 we can safely ignore tci.speculative that is set on calls and give up
1784 only if there was dynamic type store that may affect given variable
1785 (seen_unanalyzed_store) */
1790 fprintf (dump_file
, " AA walk budget exhausted.\n");
1791 *aa_walk_budget_p
= 0;
1794 else if (aa_walk_budget_p
)
1795 *aa_walk_budget_p
-= walked
;
1797 if (!tci
.type_maybe_changed
1800 && !tci
.seen_unanalyzed_store
1801 && !tci
.multiple_types_encountered
1802 && ((offset
== tci
.offset
1803 && types_same_for_odr (tci
.known_current_type
,
1805 || (instance_offset
== offset
1806 && types_same_for_odr (tci
.known_current_type
,
1807 instance_outer_type
)))))
1809 if (!outer_type
|| tci
.seen_unanalyzed_store
)
1811 if (maybe_in_construction
)
1812 maybe_in_construction
= false;
1814 fprintf (dump_file
, " No dynamic type change found.\n");
1818 if (tci
.known_current_type
1819 && !function_entry_reached
1820 && !tci
.multiple_types_encountered
)
1822 if (!tci
.speculative
)
1824 outer_type
= TYPE_MAIN_VARIANT (tci
.known_current_type
);
1825 offset
= tci
.known_current_offset
;
1827 maybe_in_construction
= false;
1828 maybe_derived_type
= false;
1830 fprintf (dump_file
, " Determined dynamic type.\n");
1832 else if (!speculative_outer_type
1833 || speculative_maybe_derived_type
)
1835 speculative_outer_type
= TYPE_MAIN_VARIANT (tci
.known_current_type
);
1836 speculative_offset
= tci
.known_current_offset
;
1837 speculative_maybe_derived_type
= false;
1839 fprintf (dump_file
, " Determined speculative dynamic type.\n");
1844 fprintf (dump_file
, " Found multiple types%s%s\n",
1845 function_entry_reached
? " (function entry reached)" : "",
1846 function_entry_reached
? " (multiple types encountered)" : "");
1852 /* See if speculation given by SPEC_OUTER_TYPE, SPEC_OFFSET and SPEC_MAYBE_DERIVED_TYPE
1853 seems consistent (and useful) with what we already have in the non-speculative context. */
1856 ipa_polymorphic_call_context::speculation_consistent_p (tree spec_outer_type
,
1857 HOST_WIDE_INT spec_offset
,
1858 bool spec_maybe_derived_type
,
1859 tree otr_type
) const
1861 if (!flag_devirtualize_speculatively
)
1864 /* Non-polymorphic types are useless for deriving likely polymorphic
1866 if (!spec_outer_type
|| !contains_polymorphic_type_p (spec_outer_type
))
1869 /* If we know nothing, speculation is always good. */
1873 /* Speculation is only useful to avoid derived types.
1874 This is not 100% true for placement new, where the outer context may
1875 turn out to be useless, but ignore these for now. */
1876 if (!maybe_derived_type
)
1879 /* If types agrees, speculation is consistent, but it makes sense only
1880 when it says something new. */
1881 if (types_must_be_same_for_odr (spec_outer_type
, outer_type
))
1882 return maybe_derived_type
&& !spec_maybe_derived_type
;
1884 /* If speculation does not contain the type in question, ignore it. */
1886 && !contains_type_p (spec_outer_type
, spec_offset
, otr_type
, false, true))
1889 /* If outer type already contains speculation as a filed,
1890 it is useless. We already know from OUTER_TYPE
1891 SPEC_TYPE and that it is not in the construction. */
1892 if (contains_type_p (outer_type
, offset
- spec_offset
,
1893 spec_outer_type
, false, false))
1896 /* If speculative outer type is not more specified than outer
1898 We can only decide this safely if we can compare types with OUTER_TYPE.
1900 if ((!in_lto_p
|| odr_type_p (outer_type
))
1901 && !contains_type_p (spec_outer_type
,
1902 spec_offset
- offset
,
1908 /* Improve THIS with speculation described by NEW_OUTER_TYPE, NEW_OFFSET,
1909 NEW_MAYBE_DERIVED_TYPE
1910 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1913 ipa_polymorphic_call_context::combine_speculation_with
1914 (tree new_outer_type
, HOST_WIDE_INT new_offset
, bool new_maybe_derived_type
,
1917 if (!new_outer_type
)
1920 /* restrict_to_inner_class may eliminate wrong speculation making our job
1923 restrict_to_inner_class (otr_type
);
1925 if (!speculation_consistent_p (new_outer_type
, new_offset
,
1926 new_maybe_derived_type
, otr_type
))
1929 /* New speculation is a win in case we have no speculation or new
1930 speculation does not consider derivations. */
1931 if (!speculative_outer_type
1932 || (speculative_maybe_derived_type
1933 && !new_maybe_derived_type
))
1935 speculative_outer_type
= new_outer_type
;
1936 speculative_offset
= new_offset
;
1937 speculative_maybe_derived_type
= new_maybe_derived_type
;
1940 else if (types_must_be_same_for_odr (speculative_outer_type
,
1943 if (speculative_offset
!= new_offset
)
1945 /* OK we have two contexts that seems valid but they disagree,
1948 This is not a lattice operation, so we may want to drop it later. */
1949 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1951 "Speculative outer types match, "
1952 "offset mismatch -> invalid speculation\n");
1953 clear_speculation ();
1958 if (speculative_maybe_derived_type
&& !new_maybe_derived_type
)
1960 speculative_maybe_derived_type
= false;
1967 /* Choose type that contains the other. This one either contains the outer
1968 as a field (thus giving exactly one target) or is deeper in the type
1970 else if (speculative_outer_type
1971 && speculative_maybe_derived_type
1972 && (new_offset
> speculative_offset
1973 || (new_offset
== speculative_offset
1974 && contains_type_p (new_outer_type
,
1975 0, speculative_outer_type
, false))))
1977 tree old_outer_type
= speculative_outer_type
;
1978 HOST_WIDE_INT old_offset
= speculative_offset
;
1979 bool old_maybe_derived_type
= speculative_maybe_derived_type
;
1981 speculative_outer_type
= new_outer_type
;
1982 speculative_offset
= new_offset
;
1983 speculative_maybe_derived_type
= new_maybe_derived_type
;
1986 restrict_to_inner_class (otr_type
);
1988 /* If the speculation turned out to make no sense, revert to sensible
1990 if (!speculative_outer_type
)
1992 speculative_outer_type
= old_outer_type
;
1993 speculative_offset
= old_offset
;
1994 speculative_maybe_derived_type
= old_maybe_derived_type
;
1997 return (old_offset
!= speculative_offset
1998 || old_maybe_derived_type
!= speculative_maybe_derived_type
1999 || types_must_be_same_for_odr (speculative_outer_type
,
2005 /* Make speculation less specific so
2006 NEW_OUTER_TYPE, NEW_OFFSET, NEW_MAYBE_DERIVED_TYPE is also included.
2007 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
2010 ipa_polymorphic_call_context::meet_speculation_with
2011 (tree new_outer_type
, HOST_WIDE_INT new_offset
, bool new_maybe_derived_type
,
2014 if (!new_outer_type
&& speculative_outer_type
)
2016 clear_speculation ();
2020 /* restrict_to_inner_class may eliminate wrong speculation making our job
2023 restrict_to_inner_class (otr_type
);
2025 if (!speculative_outer_type
2026 || !speculation_consistent_p (speculative_outer_type
,
2028 speculative_maybe_derived_type
,
2032 if (!speculation_consistent_p (new_outer_type
, new_offset
,
2033 new_maybe_derived_type
, otr_type
))
2035 clear_speculation ();
2039 else if (types_must_be_same_for_odr (speculative_outer_type
,
2042 if (speculative_offset
!= new_offset
)
2044 clear_speculation ();
2049 if (!speculative_maybe_derived_type
&& new_maybe_derived_type
)
2051 speculative_maybe_derived_type
= true;
2058 /* See if one type contains the other as a field (not base). */
2059 else if (contains_type_p (new_outer_type
, new_offset
- speculative_offset
,
2060 speculative_outer_type
, false, false))
2062 else if (contains_type_p (speculative_outer_type
,
2063 speculative_offset
- new_offset
,
2064 new_outer_type
, false, false))
2066 speculative_outer_type
= new_outer_type
;
2067 speculative_offset
= new_offset
;
2068 speculative_maybe_derived_type
= new_maybe_derived_type
;
2071 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2072 else if (contains_type_p (new_outer_type
,
2073 new_offset
- speculative_offset
,
2074 speculative_outer_type
, false, true))
2076 if (!speculative_maybe_derived_type
)
2078 speculative_maybe_derived_type
= true;
2083 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2084 else if (contains_type_p (speculative_outer_type
,
2085 speculative_offset
- new_offset
, new_outer_type
, false, true))
2087 speculative_outer_type
= new_outer_type
;
2088 speculative_offset
= new_offset
;
2089 speculative_maybe_derived_type
= true;
2094 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2095 fprintf (dump_file
, "Giving up on speculative meet\n");
2096 clear_speculation ();
2101 /* Assume that both THIS and a given context is valid and strengthen THIS
2102 if possible. Return true if any strengthening was made.
2103 If actual type the context is being used in is known, OTR_TYPE should be
2104 set accordingly. This improves quality of combined result. */
2107 ipa_polymorphic_call_context::combine_with (ipa_polymorphic_call_context ctx
,
2110 bool updated
= false;
2112 if (ctx
.useless_p () || invalid
)
2115 /* Restricting context to inner type makes merging easier, however do not
2116 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2117 if (otr_type
&& !invalid
&& !ctx
.invalid
)
2119 restrict_to_inner_class (otr_type
);
2120 ctx
.restrict_to_inner_class (otr_type
);
2125 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2127 fprintf (dump_file
, "Polymorphic call context combine:");
2129 fprintf (dump_file
, "With context: ");
2130 ctx
.dump (dump_file
);
2133 fprintf (dump_file
, "To be used with type: ");
2134 print_generic_expr (dump_file
, otr_type
, TDF_SLIM
);
2135 fprintf (dump_file
, "\n");
2139 /* If call is known to be invalid, we are done. */
2142 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2143 fprintf (dump_file
, "-> Invalid context\n");
2147 if (!ctx
.outer_type
)
2149 else if (!outer_type
)
2151 outer_type
= ctx
.outer_type
;
2152 offset
= ctx
.offset
;
2153 dynamic
= ctx
.dynamic
;
2154 maybe_in_construction
= ctx
.maybe_in_construction
;
2155 maybe_derived_type
= ctx
.maybe_derived_type
;
2158 /* If types are known to be same, merging is quite easy. */
2159 else if (types_must_be_same_for_odr (outer_type
, ctx
.outer_type
))
2161 if (offset
!= ctx
.offset
2162 && TYPE_SIZE (outer_type
)
2163 && TREE_CODE (TYPE_SIZE (outer_type
)) == INTEGER_CST
)
2165 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2166 fprintf (dump_file
, "Outer types match, offset mismatch -> invalid\n");
2167 clear_speculation ();
2168 clear_outer_type ();
2172 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2173 fprintf (dump_file
, "Outer types match, merging flags\n");
2174 if (maybe_in_construction
&& !ctx
.maybe_in_construction
)
2177 maybe_in_construction
= false;
2179 if (maybe_derived_type
&& !ctx
.maybe_derived_type
)
2182 maybe_derived_type
= false;
2184 if (dynamic
&& !ctx
.dynamic
)
2190 /* If we know the type precisely, there is not much to improve. */
2191 else if (!maybe_derived_type
&& !maybe_in_construction
2192 && !ctx
.maybe_derived_type
&& !ctx
.maybe_in_construction
)
2194 /* It may be easy to check if second context permits the first
2195 and set INVALID otherwise. This is not easy to do in general;
2196 contains_type_p may return false negatives for non-comparable
2199 If OTR_TYPE is known, we however can expect that
2200 restrict_to_inner_class should have discovered the same base
2202 if (otr_type
&& !ctx
.maybe_in_construction
&& !ctx
.maybe_derived_type
)
2204 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2205 fprintf (dump_file
, "Contextes disagree -> invalid\n");
2209 /* See if one type contains the other as a field (not base).
2210 In this case we want to choose the wider type, because it contains
2211 more information. */
2212 else if (contains_type_p (ctx
.outer_type
, ctx
.offset
- offset
,
2213 outer_type
, false, false))
2215 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2216 fprintf (dump_file
, "Second type contain the first as a field\n");
2218 if (maybe_derived_type
)
2220 outer_type
= ctx
.outer_type
;
2221 maybe_derived_type
= ctx
.maybe_derived_type
;
2222 offset
= ctx
.offset
;
2223 dynamic
= ctx
.dynamic
;
2227 /* If we do not know how the context is being used, we cannot
2228 clear MAYBE_IN_CONSTRUCTION because it may be offseted
2229 to other component of OUTER_TYPE later and we know nothing
2231 if (otr_type
&& maybe_in_construction
2232 && !ctx
.maybe_in_construction
)
2234 maybe_in_construction
= false;
2238 else if (contains_type_p (outer_type
, offset
- ctx
.offset
,
2239 ctx
.outer_type
, false, false))
2241 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2242 fprintf (dump_file
, "First type contain the second as a field\n");
2244 if (otr_type
&& maybe_in_construction
2245 && !ctx
.maybe_in_construction
)
2247 maybe_in_construction
= false;
2251 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2252 else if (contains_type_p (ctx
.outer_type
,
2253 ctx
.offset
- offset
, outer_type
, false, true))
2255 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2256 fprintf (dump_file
, "First type is base of second\n");
2257 if (!maybe_derived_type
)
2259 if (!ctx
.maybe_in_construction
2260 && types_odr_comparable (outer_type
, ctx
.outer_type
))
2262 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2263 fprintf (dump_file
, "Second context does not permit base -> invalid\n");
2267 /* Pick variant deeper in the hierarchy. */
2270 outer_type
= ctx
.outer_type
;
2271 maybe_in_construction
= ctx
.maybe_in_construction
;
2272 maybe_derived_type
= ctx
.maybe_derived_type
;
2273 offset
= ctx
.offset
;
2274 dynamic
= ctx
.dynamic
;
2278 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2279 else if (contains_type_p (outer_type
,
2280 offset
- ctx
.offset
, ctx
.outer_type
, false, true))
2282 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2283 fprintf (dump_file
, "Second type is base of first\n");
2284 if (!ctx
.maybe_derived_type
)
2286 if (!maybe_in_construction
2287 && types_odr_comparable (outer_type
, ctx
.outer_type
))
2289 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2290 fprintf (dump_file
, "First context does not permit base -> invalid\n");
2293 /* Pick the base type. */
2294 else if (maybe_in_construction
)
2296 outer_type
= ctx
.outer_type
;
2297 maybe_in_construction
= ctx
.maybe_in_construction
;
2298 maybe_derived_type
= ctx
.maybe_derived_type
;
2299 offset
= ctx
.offset
;
2300 dynamic
= ctx
.dynamic
;
2305 /* TODO handle merging using hierarchy. */
2306 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2307 fprintf (dump_file
, "Giving up on merge\n");
2309 updated
|= combine_speculation_with (ctx
.speculative_outer_type
,
2310 ctx
.speculative_offset
,
2311 ctx
.speculative_maybe_derived_type
,
2314 if (updated
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
2316 fprintf (dump_file
, "Updated as: ");
2318 fprintf (dump_file
, "\n");
2324 clear_speculation ();
2325 clear_outer_type ();
2329 /* Take non-speculative info, merge it with speculative and clear speculation.
2330 Used when we no longer manage to keep track of actual outer type, but we
2331 think it is still there.
2333 If OTR_TYPE is set, the transformation can be done more effectively assuming
2334 that context is going to be used only that way. */
2337 ipa_polymorphic_call_context::make_speculative (tree otr_type
)
2339 tree spec_outer_type
= outer_type
;
2340 HOST_WIDE_INT spec_offset
= offset
;
2341 bool spec_maybe_derived_type
= maybe_derived_type
;
2346 clear_outer_type ();
2347 clear_speculation ();
2352 clear_outer_type ();
2353 combine_speculation_with (spec_outer_type
, spec_offset
,
2354 spec_maybe_derived_type
,
2358 /* Use when we cannot track dynamic type change. This speculatively assume
2359 type change is not happening. */
2362 ipa_polymorphic_call_context::possible_dynamic_type_change (bool in_poly_cdtor
,
2366 make_speculative (otr_type
);
2367 else if (in_poly_cdtor
)
2368 maybe_in_construction
= true;
2371 /* Return TRUE if this context conveys the same information as OTHER. */
2374 ipa_polymorphic_call_context::equal_to
2375 (const ipa_polymorphic_call_context
&x
) const
2378 return x
.useless_p ();
2381 if (x
.useless_p () || x
.invalid
)
2387 || !types_odr_comparable (outer_type
, x
.outer_type
)
2388 || !types_same_for_odr (outer_type
, x
.outer_type
)
2389 || offset
!= x
.offset
2390 || maybe_in_construction
!= x
.maybe_in_construction
2391 || maybe_derived_type
!= x
.maybe_derived_type
2392 || dynamic
!= x
.dynamic
)
2395 else if (x
.outer_type
)
2399 if (speculative_outer_type
2400 && speculation_consistent_p (speculative_outer_type
, speculative_offset
,
2401 speculative_maybe_derived_type
, NULL_TREE
))
2403 if (!x
.speculative_outer_type
)
2406 if (!types_odr_comparable (speculative_outer_type
,
2407 x
.speculative_outer_type
)
2408 || !types_same_for_odr (speculative_outer_type
,
2409 x
.speculative_outer_type
)
2410 || speculative_offset
!= x
.speculative_offset
2411 || speculative_maybe_derived_type
!= x
.speculative_maybe_derived_type
)
2414 else if (x
.speculative_outer_type
2415 && x
.speculation_consistent_p (x
.speculative_outer_type
,
2416 x
.speculative_offset
,
2417 x
.speculative_maybe_derived_type
,
2424 /* Modify context to be strictly less restrictive than CTX. */
2427 ipa_polymorphic_call_context::meet_with (ipa_polymorphic_call_context ctx
,
2430 bool updated
= false;
2432 if (useless_p () || ctx
.invalid
)
2435 /* Restricting context to inner type makes merging easier, however do not
2436 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2437 if (otr_type
&& !useless_p () && !ctx
.useless_p ())
2439 restrict_to_inner_class (otr_type
);
2440 ctx
.restrict_to_inner_class (otr_type
);
2448 if (ctx
.useless_p () || invalid
)
2454 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2456 fprintf (dump_file
, "Polymorphic call context meet:");
2458 fprintf (dump_file
, "With context: ");
2459 ctx
.dump (dump_file
);
2462 fprintf (dump_file
, "To be used with type: ");
2463 print_generic_expr (dump_file
, otr_type
, TDF_SLIM
);
2464 fprintf (dump_file
, "\n");
2468 if (!dynamic
&& ctx
.dynamic
)
2474 /* If call is known to be invalid, we are done. */
2477 else if (!ctx
.outer_type
)
2479 clear_outer_type ();
2482 /* If types are known to be same, merging is quite easy. */
2483 else if (types_must_be_same_for_odr (outer_type
, ctx
.outer_type
))
2485 if (offset
!= ctx
.offset
2486 && TYPE_SIZE (outer_type
)
2487 && TREE_CODE (TYPE_SIZE (outer_type
)) == INTEGER_CST
)
2489 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2490 fprintf (dump_file
, "Outer types match, offset mismatch -> clearing\n");
2491 clear_outer_type ();
2494 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2495 fprintf (dump_file
, "Outer types match, merging flags\n");
2496 if (!maybe_in_construction
&& ctx
.maybe_in_construction
)
2499 maybe_in_construction
= true;
2501 if (!maybe_derived_type
&& ctx
.maybe_derived_type
)
2504 maybe_derived_type
= true;
2506 if (!dynamic
&& ctx
.dynamic
)
2512 /* See if one type contains the other as a field (not base). */
2513 else if (contains_type_p (ctx
.outer_type
, ctx
.offset
- offset
,
2514 outer_type
, false, false))
2516 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2517 fprintf (dump_file
, "Second type contain the first as a field\n");
2519 /* The second type is more specified, so we keep the first.
2520 We need to set DYNAMIC flag to avoid declaring context INVALID
2521 of OFFSET ends up being out of range. */
2525 && (!TYPE_SIZE (ctx
.outer_type
)
2526 || !TYPE_SIZE (outer_type
)
2527 || !operand_equal_p (TYPE_SIZE (ctx
.outer_type
),
2528 TYPE_SIZE (outer_type
), 0)))))
2534 else if (contains_type_p (outer_type
, offset
- ctx
.offset
,
2535 ctx
.outer_type
, false, false))
2537 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2538 fprintf (dump_file
, "First type contain the second as a field\n");
2543 && (!TYPE_SIZE (ctx
.outer_type
)
2544 || !TYPE_SIZE (outer_type
)
2545 || !operand_equal_p (TYPE_SIZE (ctx
.outer_type
),
2546 TYPE_SIZE (outer_type
), 0)))))
2548 outer_type
= ctx
.outer_type
;
2549 offset
= ctx
.offset
;
2550 dynamic
= ctx
.dynamic
;
2551 maybe_in_construction
= ctx
.maybe_in_construction
;
2552 maybe_derived_type
= ctx
.maybe_derived_type
;
2555 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2556 else if (contains_type_p (ctx
.outer_type
,
2557 ctx
.offset
- offset
, outer_type
, false, true))
2559 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2560 fprintf (dump_file
, "First type is base of second\n");
2561 if (!maybe_derived_type
)
2563 maybe_derived_type
= true;
2566 if (!maybe_in_construction
&& ctx
.maybe_in_construction
)
2568 maybe_in_construction
= true;
2571 if (!dynamic
&& ctx
.dynamic
)
2577 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2578 else if (contains_type_p (outer_type
,
2579 offset
- ctx
.offset
, ctx
.outer_type
, false, true))
2581 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2582 fprintf (dump_file
, "Second type is base of first\n");
2583 outer_type
= ctx
.outer_type
;
2584 offset
= ctx
.offset
;
2586 if (!maybe_derived_type
)
2587 maybe_derived_type
= true;
2588 if (!maybe_in_construction
&& ctx
.maybe_in_construction
)
2589 maybe_in_construction
= true;
2590 if (!dynamic
&& ctx
.dynamic
)
2593 /* TODO handle merging using hierarchy. */
2596 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2597 fprintf (dump_file
, "Giving up on meet\n");
2598 clear_outer_type ();
2602 updated
|= meet_speculation_with (ctx
.speculative_outer_type
,
2603 ctx
.speculative_offset
,
2604 ctx
.speculative_maybe_derived_type
,
2607 if (updated
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
2609 fprintf (dump_file
, "Updated as: ");
2611 fprintf (dump_file
, "\n");