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
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 "double-int.h"
35 #include "fold-const.h"
36 #include "print-tree.h"
39 #include "hard-reg-set.h"
43 #include "statistics.h"
45 #include "fixed-value.h"
46 #include "insn-config.h"
54 #include "tree-pass.h"
56 #include "tree-pretty-print.h"
58 #include "basic-block.h"
61 #include "plugin-api.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"
70 #include "alloc-pool.h"
71 #include "symbol-summary.h"
73 #include "ipa-inline.h"
74 #include "diagnostic.h"
78 #include "gimple-pretty-print.h"
79 #include "stor-layout.h"
81 #include "data-streamer.h"
82 #include "lto-streamer.h"
83 #include "streamer-hooks.h"
84 #include "tree-ssa-operands.h"
85 #include "tree-into-ssa.h"
87 /* Return true when TYPE contains an polymorphic type and thus is interesting
88 for devirtualization machinery. */
90 static bool contains_type_p (tree
, HOST_WIDE_INT
, tree
,
91 bool consider_placement_new
= true,
92 bool consider_bases
= true);
95 contains_polymorphic_type_p (const_tree type
)
97 type
= TYPE_MAIN_VARIANT (type
);
99 if (RECORD_OR_UNION_TYPE_P (type
))
101 if (TYPE_BINFO (type
)
102 && polymorphic_type_binfo_p (TYPE_BINFO (type
)))
104 for (tree fld
= TYPE_FIELDS (type
); fld
; fld
= DECL_CHAIN (fld
))
105 if (TREE_CODE (fld
) == FIELD_DECL
106 && !DECL_ARTIFICIAL (fld
)
107 && contains_polymorphic_type_p (TREE_TYPE (fld
)))
111 if (TREE_CODE (type
) == ARRAY_TYPE
)
112 return contains_polymorphic_type_p (TREE_TYPE (type
));
116 /* Return true if it seems valid to use placement new to build EXPECTED_TYPE
117 at possition CUR_OFFSET within TYPE.
119 POD can be changed to an instance of a polymorphic type by
120 placement new. Here we play safe and assume that any
121 non-polymorphic type is POD. */
123 possible_placement_new (tree type
, tree expected_type
,
124 HOST_WIDE_INT cur_offset
)
126 return ((TREE_CODE (type
) != RECORD_TYPE
127 || !TYPE_BINFO (type
)
128 || cur_offset
>= POINTER_SIZE
129 || !polymorphic_type_binfo_p (TYPE_BINFO (type
)))
130 && (!TYPE_SIZE (type
)
131 || !tree_fits_shwi_p (TYPE_SIZE (type
))
133 + (expected_type
? tree_to_uhwi (TYPE_SIZE (expected_type
))
135 <= tree_to_uhwi (TYPE_SIZE (type
)))));
138 /* THIS->OUTER_TYPE is a type of memory object where object of OTR_TYPE
139 is contained at THIS->OFFSET. Walk the memory representation of
140 THIS->OUTER_TYPE and find the outermost class type that match
141 OTR_TYPE or contain OTR_TYPE as a base. Update THIS
144 If OTR_TYPE is NULL, just find outermost polymorphic type with
145 virtual table present at possition OFFSET.
147 For example when THIS represents type
153 and we look for type at offset sizeof(int), we end up with B and offset 0.
154 If the same is produced by multiple inheritance, we end up with A and offset
157 If we can not find corresponding class, give up by setting
158 THIS->OUTER_TYPE to OTR_TYPE and THIS->OFFSET to NULL.
159 Return true when lookup was sucesful.
161 When CONSIDER_PLACEMENT_NEW is false, reject contexts that may be made
162 valid only via alocation of new polymorphic type inside by means
165 When CONSIDER_BASES is false, only look for actual fields, not base types
169 ipa_polymorphic_call_context::restrict_to_inner_class (tree otr_type
,
170 bool consider_placement_new
,
173 tree type
= outer_type
;
174 HOST_WIDE_INT cur_offset
= offset
;
175 bool speculative
= false;
176 bool size_unknown
= false;
177 unsigned HOST_WIDE_INT otr_type_size
= POINTER_SIZE
;
179 /* Update OUTER_TYPE to match EXPECTED_TYPE if it is not set. */
182 clear_outer_type (otr_type
);
186 /* See if OFFSET points inside OUTER_TYPE. If it does not, we know
187 that the context is either invalid, or the instance type must be
188 derived from OUTER_TYPE.
190 Because the instance type may contain field whose type is of OUTER_TYPE,
191 we can not derive any effective information about it.
193 TODO: In the case we know all derrived types, we can definitely do better
195 else if (TYPE_SIZE (outer_type
)
196 && tree_fits_shwi_p (TYPE_SIZE (outer_type
))
197 && tree_to_shwi (TYPE_SIZE (outer_type
)) >= 0
198 && tree_to_shwi (TYPE_SIZE (outer_type
)) <= offset
)
200 clear_outer_type (otr_type
);
204 /* If derived type is not allowed, we know that the context is invalid.
205 For dynamic types, we really do not have information about
206 size of the memory location. It is possible that completely
207 different type is stored after outer_type. */
208 if (!maybe_derived_type
&& !dynamic
)
210 clear_speculation ();
216 if (otr_type
&& TYPE_SIZE (otr_type
)
217 && tree_fits_shwi_p (TYPE_SIZE (otr_type
)))
218 otr_type_size
= tree_to_uhwi (TYPE_SIZE (otr_type
));
220 if (!type
|| offset
< 0)
221 goto no_useful_type_info
;
223 /* Find the sub-object the constant actually refers to and mark whether it is
224 an artificial one (as opposed to a user-defined one).
226 This loop is performed twice; first time for outer_type and second time
227 for speculative_outer_type. The second run has SPECULATIVE set. */
230 unsigned HOST_WIDE_INT pos
, size
;
233 /* If we do not know size of TYPE, we need to be more conservative
234 about accepting cases where we can not find EXPECTED_TYPE.
235 Generally the types that do matter here are of constant size.
236 Size_unknown case should be very rare. */
238 && tree_fits_shwi_p (TYPE_SIZE (type
))
239 && tree_to_shwi (TYPE_SIZE (type
)) >= 0)
240 size_unknown
= false;
244 /* On a match, just return what we found. */
246 && types_odr_comparable (type
, otr_type
)
247 && types_same_for_odr (type
, otr_type
))
249 && TREE_CODE (type
) == RECORD_TYPE
251 && polymorphic_type_binfo_p (TYPE_BINFO (type
))))
255 /* If we did not match the offset, just give up on speculation. */
257 /* Also check if speculation did not end up being same as
259 || (types_must_be_same_for_odr (speculative_outer_type
,
261 && (maybe_derived_type
262 == speculative_maybe_derived_type
)))
263 clear_speculation ();
268 /* If type is known to be final, do not worry about derived
269 types. Testing it here may help us to avoid speculation. */
270 if (otr_type
&& TREE_CODE (outer_type
) == RECORD_TYPE
271 && (!in_lto_p
|| odr_type_p (outer_type
))
272 && type_known_to_have_no_deriavations_p (outer_type
))
273 maybe_derived_type
= false;
275 /* Type can not contain itself on an non-zero offset. In that case
276 just give up. Still accept the case where size is now known.
277 Either the second copy may appear past the end of type or within
278 the non-POD buffer located inside the variably sized type
281 goto no_useful_type_info
;
282 /* If we determined type precisely or we have no clue on
283 speuclation, we are done. */
284 if (!maybe_derived_type
|| !speculative_outer_type
285 || !speculation_consistent_p (speculative_outer_type
,
287 speculative_maybe_derived_type
,
290 clear_speculation ();
293 /* Otherwise look into speculation now. */
297 type
= speculative_outer_type
;
298 cur_offset
= speculative_offset
;
304 /* Walk fields and find corresponding on at OFFSET. */
305 if (TREE_CODE (type
) == RECORD_TYPE
)
307 for (fld
= TYPE_FIELDS (type
); fld
; fld
= DECL_CHAIN (fld
))
309 if (TREE_CODE (fld
) != FIELD_DECL
)
312 pos
= int_bit_position (fld
);
313 if (pos
> (unsigned HOST_WIDE_INT
)cur_offset
)
316 /* Do not consider vptr itself. Not even for placement new. */
317 if (!pos
&& DECL_ARTIFICIAL (fld
)
318 && POINTER_TYPE_P (TREE_TYPE (fld
))
320 && polymorphic_type_binfo_p (TYPE_BINFO (type
)))
323 if (!DECL_SIZE (fld
) || !tree_fits_uhwi_p (DECL_SIZE (fld
)))
324 goto no_useful_type_info
;
325 size
= tree_to_uhwi (DECL_SIZE (fld
));
327 /* We can always skip types smaller than pointer size:
328 those can not contain a virtual table pointer.
330 Disqualifying fields that are too small to fit OTR_TYPE
331 saves work needed to walk them for no benefit.
332 Because of the way the bases are packed into a class, the
333 field's size may be smaller than type size, so it needs
334 to be done with a care. */
336 if (pos
<= (unsigned HOST_WIDE_INT
)cur_offset
337 && (pos
+ size
) >= (unsigned HOST_WIDE_INT
)cur_offset
340 || !TYPE_SIZE (TREE_TYPE (fld
))
341 || !tree_fits_shwi_p (TYPE_SIZE (TREE_TYPE (fld
)))
342 || (pos
+ tree_to_uhwi (TYPE_SIZE (TREE_TYPE (fld
))))
343 >= cur_offset
+ otr_type_size
))
348 goto no_useful_type_info
;
350 type
= TYPE_MAIN_VARIANT (TREE_TYPE (fld
));
352 /* DECL_ARTIFICIAL represents a basetype. */
353 if (!DECL_ARTIFICIAL (fld
))
359 /* As soon as we se an field containing the type,
360 we know we are not looking for derivations. */
361 maybe_derived_type
= false;
365 speculative_outer_type
= type
;
366 speculative_offset
= cur_offset
;
367 speculative_maybe_derived_type
= false;
370 else if (!consider_bases
)
371 goto no_useful_type_info
;
373 else if (TREE_CODE (type
) == ARRAY_TYPE
)
375 tree subtype
= TYPE_MAIN_VARIANT (TREE_TYPE (type
));
377 /* Give up if we don't know array field size.
378 Also give up on non-polymorphic types as they are used
379 as buffers for placement new. */
380 if (!TYPE_SIZE (subtype
)
381 || !tree_fits_shwi_p (TYPE_SIZE (subtype
))
382 || tree_to_shwi (TYPE_SIZE (subtype
)) <= 0
383 || !contains_polymorphic_type_p (subtype
))
384 goto no_useful_type_info
;
386 HOST_WIDE_INT new_offset
= cur_offset
% tree_to_shwi (TYPE_SIZE (subtype
));
388 /* We may see buffer for placement new. In this case the expected type
389 can be bigger than the subtype. */
390 if (TYPE_SIZE (subtype
)
391 && (cur_offset
+ otr_type_size
392 > tree_to_uhwi (TYPE_SIZE (subtype
))))
393 goto no_useful_type_info
;
395 cur_offset
= new_offset
;
401 maybe_derived_type
= false;
405 speculative_outer_type
= type
;
406 speculative_offset
= cur_offset
;
407 speculative_maybe_derived_type
= false;
410 /* Give up on anything else. */
414 if (maybe_derived_type
&& !speculative
415 && TREE_CODE (outer_type
) == RECORD_TYPE
416 && TREE_CODE (otr_type
) == RECORD_TYPE
417 && TYPE_BINFO (otr_type
)
419 && get_binfo_at_offset (TYPE_BINFO (otr_type
), 0, outer_type
))
421 clear_outer_type (otr_type
);
422 if (!speculative_outer_type
423 || !speculation_consistent_p (speculative_outer_type
,
425 speculative_maybe_derived_type
,
427 clear_speculation ();
428 if (speculative_outer_type
)
431 type
= speculative_outer_type
;
432 cur_offset
= speculative_offset
;
437 /* We found no way to embedd EXPECTED_TYPE in TYPE.
438 We still permit two special cases - placement new and
439 the case of variadic types containing themselves. */
441 && consider_placement_new
442 && (size_unknown
|| !type
|| maybe_derived_type
443 || possible_placement_new (type
, otr_type
, cur_offset
)))
445 /* In these weird cases we want to accept the context.
446 In non-speculative run we have no useful outer_type info
447 (TODO: we may eventually want to record upper bound on the
448 type size that can be used to prune the walk),
449 but we still want to consider speculation that may
453 clear_outer_type (otr_type
);
454 if (!speculative_outer_type
455 || !speculation_consistent_p (speculative_outer_type
,
457 speculative_maybe_derived_type
,
459 clear_speculation ();
460 if (speculative_outer_type
)
463 type
= speculative_outer_type
;
464 cur_offset
= speculative_offset
;
470 clear_speculation ();
475 clear_speculation ();
478 clear_outer_type (otr_type
);
486 /* Return true if OUTER_TYPE contains OTR_TYPE at OFFSET.
487 CONSIDER_PLACEMENT_NEW makes function to accept cases where OTR_TYPE can
488 be built within OUTER_TYPE by means of placement new. CONSIDER_BASES makes
489 function to accept cases where OTR_TYPE appears as base of OUTER_TYPE or as
490 base of one of fields of OUTER_TYPE. */
493 contains_type_p (tree outer_type
, HOST_WIDE_INT offset
,
495 bool consider_placement_new
,
498 ipa_polymorphic_call_context context
;
500 /* Check that type is within range. */
503 if (TYPE_SIZE (outer_type
) && TYPE_SIZE (otr_type
)
504 && TREE_CODE (outer_type
) == INTEGER_CST
505 && TREE_CODE (otr_type
) == INTEGER_CST
506 && wi::ltu_p (wi::to_offset (outer_type
), (wi::to_offset (otr_type
) + offset
)))
509 context
.offset
= offset
;
510 context
.outer_type
= TYPE_MAIN_VARIANT (outer_type
);
511 context
.maybe_derived_type
= false;
512 return context
.restrict_to_inner_class (otr_type
, consider_placement_new
, consider_bases
);
516 /* Return a FUNCTION_DECL if BLOCK represents a constructor or destructor.
517 If CHECK_CLONES is true, also check for clones of ctor/dtors. */
520 inlined_polymorphic_ctor_dtor_block_p (tree block
, bool check_clones
)
522 tree fn
= BLOCK_ABSTRACT_ORIGIN (block
);
523 if (fn
== NULL
|| TREE_CODE (fn
) != FUNCTION_DECL
)
526 if (TREE_CODE (TREE_TYPE (fn
)) != METHOD_TYPE
527 || (!DECL_CXX_CONSTRUCTOR_P (fn
) && !DECL_CXX_DESTRUCTOR_P (fn
)))
532 /* Watch for clones where we constant propagated the first
533 argument (pointer to the instance). */
534 fn
= DECL_ABSTRACT_ORIGIN (fn
);
536 || TREE_CODE (TREE_TYPE (fn
)) != METHOD_TYPE
537 || (!DECL_CXX_CONSTRUCTOR_P (fn
) && !DECL_CXX_DESTRUCTOR_P (fn
)))
541 if (flags_from_decl_or_type (fn
) & (ECF_PURE
| ECF_CONST
))
548 /* We know that the instance is stored in variable or parameter
549 (not dynamically allocated) and we want to disprove the fact
550 that it may be in construction at invocation of CALL.
552 BASE represents memory location where instance is stored.
553 If BASE is NULL, it is assumed to be global memory.
554 OUTER_TYPE is known type of the instance or NULL if not
557 For the variable to be in construction we actually need to
558 be in constructor of corresponding global variable or
559 the inline stack of CALL must contain the constructor.
560 Check this condition. This check works safely only before
561 IPA passes, because inline stacks may become out of date
565 decl_maybe_in_construction_p (tree base
, tree outer_type
,
566 gimple call
, tree function
)
569 outer_type
= TYPE_MAIN_VARIANT (outer_type
);
570 gcc_assert (!base
|| DECL_P (base
));
572 /* After inlining the code unification optimizations may invalidate
573 inline stacks. Also we need to give up on global variables after
574 IPA, because addresses of these may have been propagated to their
576 if (DECL_STRUCT_FUNCTION (function
)->after_inlining
)
579 /* Pure functions can not do any changes on the dynamic type;
580 that require writting to memory. */
581 if ((!base
|| !auto_var_in_fn_p (base
, function
))
582 && flags_from_decl_or_type (function
) & (ECF_PURE
| ECF_CONST
))
585 bool check_clones
= !base
|| is_global_var (base
);
586 for (tree block
= gimple_block (call
); block
&& TREE_CODE (block
) == BLOCK
;
587 block
= BLOCK_SUPERCONTEXT (block
))
588 if (tree fn
= inlined_polymorphic_ctor_dtor_block_p (block
, check_clones
))
590 tree type
= TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (fn
)));
592 if (!outer_type
|| !types_odr_comparable (type
, outer_type
))
594 if (TREE_CODE (type
) == RECORD_TYPE
596 && polymorphic_type_binfo_p (TYPE_BINFO (type
)))
599 else if (types_same_for_odr (type
, outer_type
))
603 if (!base
|| (TREE_CODE (base
) == VAR_DECL
&& is_global_var (base
)))
605 if (TREE_CODE (TREE_TYPE (function
)) != METHOD_TYPE
606 || (!DECL_CXX_CONSTRUCTOR_P (function
)
607 && !DECL_CXX_DESTRUCTOR_P (function
)))
609 if (!DECL_ABSTRACT_ORIGIN (function
))
611 /* Watch for clones where we constant propagated the first
612 argument (pointer to the instance). */
613 function
= DECL_ABSTRACT_ORIGIN (function
);
615 || TREE_CODE (TREE_TYPE (function
)) != METHOD_TYPE
616 || (!DECL_CXX_CONSTRUCTOR_P (function
)
617 && !DECL_CXX_DESTRUCTOR_P (function
)))
620 tree type
= TYPE_MAIN_VARIANT (method_class_type (TREE_TYPE (function
)));
621 if (!outer_type
|| !types_odr_comparable (type
, outer_type
))
623 if (TREE_CODE (type
) == RECORD_TYPE
625 && polymorphic_type_binfo_p (TYPE_BINFO (type
)))
628 else if (types_same_for_odr (type
, outer_type
))
634 /* Dump human readable context to F. If NEWLINE is true, it will be terminated
638 ipa_polymorphic_call_context::dump (FILE *f
, bool newline
) const
642 fprintf (f
, "Call is known to be undefined");
646 fprintf (f
, "nothing known");
647 if (outer_type
|| offset
)
649 fprintf (f
, "Outer type%s:", dynamic
? " (dynamic)":"");
650 print_generic_expr (f
, outer_type
, TDF_SLIM
);
651 if (maybe_derived_type
)
652 fprintf (f
, " (or a derived type)");
653 if (maybe_in_construction
)
654 fprintf (f
, " (maybe in construction)");
655 fprintf (f
, " offset "HOST_WIDE_INT_PRINT_DEC
,
658 if (speculative_outer_type
)
660 if (outer_type
|| offset
)
662 fprintf (f
, "Speculative outer type:");
663 print_generic_expr (f
, speculative_outer_type
, TDF_SLIM
);
664 if (speculative_maybe_derived_type
)
665 fprintf (f
, " (or a derived type)");
666 fprintf (f
, " at offset "HOST_WIDE_INT_PRINT_DEC
,
674 /* Print context to stderr. */
677 ipa_polymorphic_call_context::debug () const
682 /* Stream out the context to OB. */
685 ipa_polymorphic_call_context::stream_out (struct output_block
*ob
) const
687 struct bitpack_d bp
= bitpack_create (ob
->main_stream
);
689 bp_pack_value (&bp
, invalid
, 1);
690 bp_pack_value (&bp
, maybe_in_construction
, 1);
691 bp_pack_value (&bp
, maybe_derived_type
, 1);
692 bp_pack_value (&bp
, speculative_maybe_derived_type
, 1);
693 bp_pack_value (&bp
, dynamic
, 1);
694 bp_pack_value (&bp
, outer_type
!= NULL
, 1);
695 bp_pack_value (&bp
, offset
!= 0, 1);
696 bp_pack_value (&bp
, speculative_outer_type
!= NULL
, 1);
697 streamer_write_bitpack (&bp
);
699 if (outer_type
!= NULL
)
700 stream_write_tree (ob
, outer_type
, true);
702 streamer_write_hwi (ob
, offset
);
703 if (speculative_outer_type
!= NULL
)
705 stream_write_tree (ob
, speculative_outer_type
, true);
706 streamer_write_hwi (ob
, speculative_offset
);
709 gcc_assert (!speculative_offset
);
712 /* Stream in the context from IB and DATA_IN. */
715 ipa_polymorphic_call_context::stream_in (struct lto_input_block
*ib
,
716 struct data_in
*data_in
)
718 struct bitpack_d bp
= streamer_read_bitpack (ib
);
720 invalid
= bp_unpack_value (&bp
, 1);
721 maybe_in_construction
= bp_unpack_value (&bp
, 1);
722 maybe_derived_type
= bp_unpack_value (&bp
, 1);
723 speculative_maybe_derived_type
= bp_unpack_value (&bp
, 1);
724 dynamic
= bp_unpack_value (&bp
, 1);
725 bool outer_type_p
= bp_unpack_value (&bp
, 1);
726 bool offset_p
= bp_unpack_value (&bp
, 1);
727 bool speculative_outer_type_p
= bp_unpack_value (&bp
, 1);
730 outer_type
= stream_read_tree (ib
, data_in
);
734 offset
= (HOST_WIDE_INT
) streamer_read_hwi (ib
);
737 if (speculative_outer_type_p
)
739 speculative_outer_type
= stream_read_tree (ib
, data_in
);
740 speculative_offset
= (HOST_WIDE_INT
) streamer_read_hwi (ib
);
744 speculative_outer_type
= NULL
;
745 speculative_offset
= 0;
749 /* Proudce polymorphic call context for call method of instance
750 that is located within BASE (that is assumed to be a decl) at offset OFF. */
753 ipa_polymorphic_call_context::set_by_decl (tree base
, HOST_WIDE_INT off
)
755 gcc_assert (DECL_P (base
));
756 clear_speculation ();
758 if (!contains_polymorphic_type_p (TREE_TYPE (base
)))
764 outer_type
= TYPE_MAIN_VARIANT (TREE_TYPE (base
));
766 /* Make very conservative assumption that all objects
767 may be in construction.
769 It is up to caller to revisit this via
770 get_dynamic_type or decl_maybe_in_construction_p. */
771 maybe_in_construction
= true;
772 maybe_derived_type
= false;
776 /* CST is an invariant (address of decl), try to get meaningful
777 polymorphic call context for polymorphic call of method
778 if instance of OTR_TYPE that is located at offset OFF of this invariant.
779 Return FALSE if nothing meaningful can be found. */
782 ipa_polymorphic_call_context::set_by_invariant (tree cst
,
786 HOST_WIDE_INT offset2
, size
, max_size
;
791 clear_outer_type (otr_type
);
793 if (TREE_CODE (cst
) != ADDR_EXPR
)
796 cst
= TREE_OPERAND (cst
, 0);
797 base
= get_ref_base_and_extent (cst
, &offset2
, &size
, &max_size
);
798 if (!DECL_P (base
) || max_size
== -1 || max_size
!= size
)
801 /* Only type inconsistent programs can have otr_type that is
802 not part of outer type. */
803 if (otr_type
&& !contains_type_p (TREE_TYPE (base
), off
, otr_type
))
806 set_by_decl (base
, off
);
810 /* See if OP is SSA name initialized as a copy or by single assignment.
811 If so, walk the SSA graph up. Because simple PHI conditional is considered
812 copy, GLOBAL_VISITED may be used to avoid infinite loop walking the SSA
816 walk_ssa_copies (tree op
, hash_set
<tree
> **global_visited
= NULL
)
818 hash_set
<tree
> *visited
= NULL
;
820 while (TREE_CODE (op
) == SSA_NAME
821 && !SSA_NAME_IS_DEFAULT_DEF (op
)
822 /* We might be called via fold_stmt during cfgcleanup where
823 SSA form need not be up-to-date. */
824 && !name_registered_for_update_p (op
)
825 && (gimple_assign_single_p (SSA_NAME_DEF_STMT (op
))
826 || gimple_code (SSA_NAME_DEF_STMT (op
)) == GIMPLE_PHI
))
830 if (!*global_visited
)
831 *global_visited
= new hash_set
<tree
>;
832 if ((*global_visited
)->add (op
))
838 visited
= new hash_set
<tree
>;
839 if (visited
->add (op
))
847 This pattern is implicitly produced for casts to non-primary
848 bases. When doing context analysis, we do not really care
849 about the case pointer is NULL, becuase the call will be
851 if (gimple_code (SSA_NAME_DEF_STMT (op
)) == GIMPLE_PHI
)
853 gimple phi
= SSA_NAME_DEF_STMT (op
);
855 if (gimple_phi_num_args (phi
) > 2)
857 if (gimple_phi_num_args (phi
) == 1)
858 op
= gimple_phi_arg_def (phi
, 0);
859 else if (integer_zerop (gimple_phi_arg_def (phi
, 0)))
860 op
= gimple_phi_arg_def (phi
, 1);
861 else if (integer_zerop (gimple_phi_arg_def (phi
, 1)))
862 op
= gimple_phi_arg_def (phi
, 0);
868 if (gimple_assign_load_p (SSA_NAME_DEF_STMT (op
)))
870 op
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op
));
880 /* Create polymorphic call context from IP invariant CST.
881 This is typically &global_var.
882 OTR_TYPE specify type of polymorphic call or NULL if unknown, OFF
883 is offset of call. */
885 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree cst
,
889 clear_speculation ();
890 set_by_invariant (cst
, otr_type
, off
);
893 /* Build context for pointer REF contained in FNDECL at statement STMT.
894 if INSTANCE is non-NULL, return pointer to the object described by
895 the context or DECL where context is contained in. */
897 ipa_polymorphic_call_context::ipa_polymorphic_call_context (tree fndecl
,
902 tree otr_type
= NULL
;
904 hash_set
<tree
> *visited
= NULL
;
906 if (TREE_CODE (ref
) == OBJ_TYPE_REF
)
908 otr_type
= obj_type_ref_class (ref
);
909 base_pointer
= OBJ_TYPE_REF_OBJECT (ref
);
914 /* Set up basic info in case we find nothing interesting in the analysis. */
915 clear_speculation ();
916 clear_outer_type (otr_type
);
919 /* Walk SSA for outer object. */
922 base_pointer
= walk_ssa_copies (base_pointer
, &visited
);
923 if (TREE_CODE (base_pointer
) == ADDR_EXPR
)
925 HOST_WIDE_INT size
, max_size
;
926 HOST_WIDE_INT offset2
;
927 tree base
= get_ref_base_and_extent (TREE_OPERAND (base_pointer
, 0),
928 &offset2
, &size
, &max_size
);
930 if (max_size
!= -1 && max_size
== size
)
931 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base
)),
934 NULL
/* Do not change outer type. */);
936 /* If this is a varying address, punt. */
937 if ((TREE_CODE (base
) == MEM_REF
|| DECL_P (base
))
941 /* We found dereference of a pointer. Type of the pointer
942 and MEM_REF is meaningless, but we can look futher. */
943 if (TREE_CODE (base
) == MEM_REF
)
945 base_pointer
= TREE_OPERAND (base
, 0);
947 += offset2
+ mem_ref_offset (base
).to_short_addr () * BITS_PER_UNIT
;
950 /* We found base object. In this case the outer_type
952 else if (DECL_P (base
))
956 /* Only type inconsistent programs can have otr_type that is
957 not part of outer type. */
959 && !contains_type_p (TREE_TYPE (base
),
960 offset
+ offset2
, otr_type
))
964 *instance
= base_pointer
;
967 set_by_decl (base
, offset
+ offset2
);
968 if (outer_type
&& maybe_in_construction
&& stmt
)
969 maybe_in_construction
970 = decl_maybe_in_construction_p (base
,
984 else if (TREE_CODE (base_pointer
) == POINTER_PLUS_EXPR
985 && tree_fits_uhwi_p (TREE_OPERAND (base_pointer
, 1)))
987 offset
+= tree_to_shwi (TREE_OPERAND (base_pointer
, 1))
989 base_pointer
= TREE_OPERAND (base_pointer
, 0);
998 /* Try to determine type of the outer object. */
999 if (TREE_CODE (base_pointer
) == SSA_NAME
1000 && SSA_NAME_IS_DEFAULT_DEF (base_pointer
)
1001 && TREE_CODE (SSA_NAME_VAR (base_pointer
)) == PARM_DECL
)
1003 /* See if parameter is THIS pointer of a method. */
1004 if (TREE_CODE (TREE_TYPE (fndecl
)) == METHOD_TYPE
1005 && SSA_NAME_VAR (base_pointer
) == DECL_ARGUMENTS (fndecl
))
1008 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer
)));
1009 gcc_assert (TREE_CODE (outer_type
) == RECORD_TYPE
1010 || TREE_CODE (outer_type
) == UNION_TYPE
);
1012 /* Dynamic casting has possibly upcasted the type
1013 in the hiearchy. In this case outer type is less
1014 informative than inner type and we should forget
1017 && !contains_type_p (outer_type
, offset
,
1019 || !contains_polymorphic_type_p (outer_type
))
1023 *instance
= base_pointer
;
1029 /* If the function is constructor or destructor, then
1030 the type is possibly in construction, but we know
1031 it is not derived type. */
1032 if (DECL_CXX_CONSTRUCTOR_P (fndecl
)
1033 || DECL_CXX_DESTRUCTOR_P (fndecl
))
1035 maybe_in_construction
= true;
1036 maybe_derived_type
= false;
1040 maybe_derived_type
= true;
1041 maybe_in_construction
= false;
1044 *instance
= base_pointer
;
1047 /* Non-PODs passed by value are really passed by invisible
1048 reference. In this case we also know the type of the
1050 if (DECL_BY_REFERENCE (SSA_NAME_VAR (base_pointer
)))
1053 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (base_pointer
)));
1054 /* Only type inconsistent programs can have otr_type that is
1055 not part of outer type. */
1056 if (otr_type
&& !contains_type_p (outer_type
, offset
,
1061 *instance
= base_pointer
;
1064 /* Non-polymorphic types have no interest for us. */
1065 else if (!otr_type
&& !contains_polymorphic_type_p (outer_type
))
1069 *instance
= base_pointer
;
1072 maybe_derived_type
= false;
1073 maybe_in_construction
= false;
1075 *instance
= base_pointer
;
1080 tree base_type
= TREE_TYPE (base_pointer
);
1082 if (TREE_CODE (base_pointer
) == SSA_NAME
1083 && SSA_NAME_IS_DEFAULT_DEF (base_pointer
)
1084 && !(TREE_CODE (SSA_NAME_VAR (base_pointer
)) == PARM_DECL
1085 || TREE_CODE (SSA_NAME_VAR (base_pointer
)) == RESULT_DECL
))
1089 *instance
= base_pointer
;
1092 if (TREE_CODE (base_pointer
) == SSA_NAME
1093 && SSA_NAME_DEF_STMT (base_pointer
)
1094 && gimple_assign_single_p (SSA_NAME_DEF_STMT (base_pointer
)))
1095 base_type
= TREE_TYPE (gimple_assign_rhs1
1096 (SSA_NAME_DEF_STMT (base_pointer
)));
1098 if (base_type
&& POINTER_TYPE_P (base_type
))
1099 combine_speculation_with (TYPE_MAIN_VARIANT (TREE_TYPE (base_type
)),
1101 true, NULL
/* Do not change type here */);
1102 /* TODO: There are multiple ways to derive a type. For instance
1103 if BASE_POINTER is passed to an constructor call prior our refernece.
1104 We do not make this type of flow sensitive analysis yet. */
1106 *instance
= base_pointer
;
1110 /* Structure to be passed in between detect_type_change and
1111 check_stmt_for_type_change. */
1113 struct type_change_info
1115 /* Offset into the object where there is the virtual method pointer we are
1117 HOST_WIDE_INT offset
;
1118 /* The declaration or SSA_NAME pointer of the base that we are checking for
1121 /* The reference to virtual table pointer used. */
1124 /* If we actually can tell the type that the object has changed to, it is
1125 stored in this field. Otherwise it remains NULL_TREE. */
1126 tree known_current_type
;
1127 HOST_WIDE_INT known_current_offset
;
1129 /* Set to true if dynamic type change has been detected. */
1130 bool type_maybe_changed
;
1131 /* Set to true if multiple types have been encountered. known_current_type
1132 must be disregarded in that case. */
1133 bool multiple_types_encountered
;
1134 /* Set to true if we possibly missed some dynamic type changes and we should
1135 consider the set to be speculative. */
1137 bool seen_unanalyzed_store
;
1140 /* Return true if STMT is not call and can modify a virtual method table pointer.
1141 We take advantage of fact that vtable stores must appear within constructor
1142 and destructor functions. */
1145 noncall_stmt_may_be_vtbl_ptr_store (gimple stmt
)
1147 if (is_gimple_assign (stmt
))
1149 tree lhs
= gimple_assign_lhs (stmt
);
1151 if (gimple_clobber_p (stmt
))
1153 if (!AGGREGATE_TYPE_P (TREE_TYPE (lhs
)))
1155 if (flag_strict_aliasing
1156 && !POINTER_TYPE_P (TREE_TYPE (lhs
)))
1159 if (TREE_CODE (lhs
) == COMPONENT_REF
1160 && !DECL_VIRTUAL_P (TREE_OPERAND (lhs
, 1)))
1162 /* In the future we might want to use get_base_ref_and_offset to find
1163 if there is a field corresponding to the offset and if so, proceed
1164 almost like if it was a component ref. */
1168 /* Code unification may mess with inline stacks. */
1169 if (cfun
->after_inlining
)
1172 /* Walk the inline stack and watch out for ctors/dtors.
1173 TODO: Maybe we can require the store to appear in toplevel
1174 block of CTOR/DTOR. */
1175 for (tree block
= gimple_block (stmt
); block
&& TREE_CODE (block
) == BLOCK
;
1176 block
= BLOCK_SUPERCONTEXT (block
))
1177 if (BLOCK_ABSTRACT_ORIGIN (block
)
1178 && TREE_CODE (BLOCK_ABSTRACT_ORIGIN (block
)) == FUNCTION_DECL
)
1179 return inlined_polymorphic_ctor_dtor_block_p (block
, false);
1180 return (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
1181 && (DECL_CXX_CONSTRUCTOR_P (current_function_decl
)
1182 || DECL_CXX_DESTRUCTOR_P (current_function_decl
)));
1185 /* If STMT can be proved to be an assignment to the virtual method table
1186 pointer of ANALYZED_OBJ and the type associated with the new table
1187 identified, return the type. Otherwise return NULL_TREE if type changes
1188 in unknown way or ERROR_MARK_NODE if type is unchanged. */
1191 extr_type_from_vtbl_ptr_store (gimple stmt
, struct type_change_info
*tci
,
1192 HOST_WIDE_INT
*type_offset
)
1194 HOST_WIDE_INT offset
, size
, max_size
;
1195 tree lhs
, rhs
, base
;
1197 if (!gimple_assign_single_p (stmt
))
1200 lhs
= gimple_assign_lhs (stmt
);
1201 rhs
= gimple_assign_rhs1 (stmt
);
1202 if (TREE_CODE (lhs
) != COMPONENT_REF
1203 || !DECL_VIRTUAL_P (TREE_OPERAND (lhs
, 1)))
1206 fprintf (dump_file
, " LHS is not virtual table.\n");
1210 if (tci
->vtbl_ptr_ref
&& operand_equal_p (lhs
, tci
->vtbl_ptr_ref
, 0))
1214 base
= get_ref_base_and_extent (lhs
, &offset
, &size
, &max_size
);
1215 if (DECL_P (tci
->instance
))
1217 if (base
!= tci
->instance
)
1221 fprintf (dump_file
, " base:");
1222 print_generic_expr (dump_file
, base
, TDF_SLIM
);
1223 fprintf (dump_file
, " does not match instance:");
1224 print_generic_expr (dump_file
, tci
->instance
, TDF_SLIM
);
1225 fprintf (dump_file
, "\n");
1230 else if (TREE_CODE (base
) == MEM_REF
)
1232 if (!operand_equal_p (tci
->instance
, TREE_OPERAND (base
, 0), 0))
1236 fprintf (dump_file
, " base mem ref:");
1237 print_generic_expr (dump_file
, base
, TDF_SLIM
);
1238 fprintf (dump_file
, " does not match instance:");
1239 print_generic_expr (dump_file
, tci
->instance
, TDF_SLIM
);
1240 fprintf (dump_file
, "\n");
1244 if (!integer_zerop (TREE_OPERAND (base
, 1)))
1246 if (!tree_fits_shwi_p (TREE_OPERAND (base
, 1)))
1250 fprintf (dump_file
, " base mem ref:");
1251 print_generic_expr (dump_file
, base
, TDF_SLIM
);
1252 fprintf (dump_file
, " has non-representable offset:");
1253 print_generic_expr (dump_file
, tci
->instance
, TDF_SLIM
);
1254 fprintf (dump_file
, "\n");
1259 offset
+= tree_to_shwi (TREE_OPERAND (base
, 1)) * BITS_PER_UNIT
;
1262 else if (!operand_equal_p (tci
->instance
, base
, 0)
1267 fprintf (dump_file
, " base:");
1268 print_generic_expr (dump_file
, base
, TDF_SLIM
);
1269 fprintf (dump_file
, " does not match instance:");
1270 print_generic_expr (dump_file
, tci
->instance
, TDF_SLIM
);
1271 fprintf (dump_file
, " with offset %i\n", (int)tci
->offset
);
1273 return tci
->offset
> POINTER_SIZE
? error_mark_node
: NULL_TREE
;
1275 if (offset
!= tci
->offset
1276 || size
!= POINTER_SIZE
1277 || max_size
!= POINTER_SIZE
)
1280 fprintf (dump_file
, " wrong offset %i!=%i or size %i\n",
1281 (int)offset
, (int)tci
->offset
, (int)size
);
1282 return offset
+ POINTER_SIZE
<= tci
->offset
1284 && tci
->offset
+ POINTER_SIZE
> offset
+ max_size
)
1285 ? error_mark_node
: NULL
;
1290 unsigned HOST_WIDE_INT offset2
;
1292 if (!vtable_pointer_value_to_vtable (rhs
, &vtable
, &offset2
))
1295 fprintf (dump_file
, " Failed to lookup binfo\n");
1299 tree binfo
= subbinfo_with_vtable_at_offset (TYPE_BINFO (DECL_CONTEXT (vtable
)),
1304 fprintf (dump_file
, " Construction vtable used\n");
1305 /* FIXME: We should suport construction contexts. */
1309 *type_offset
= tree_to_shwi (BINFO_OFFSET (binfo
)) * BITS_PER_UNIT
;
1310 return DECL_CONTEXT (vtable
);
1313 /* Record dynamic type change of TCI to TYPE. */
1316 record_known_type (struct type_change_info
*tci
, tree type
, HOST_WIDE_INT offset
)
1322 fprintf (dump_file
, " Recording type: ");
1323 print_generic_expr (dump_file
, type
, TDF_SLIM
);
1324 fprintf (dump_file
, " at offset %i\n", (int)offset
);
1327 fprintf (dump_file
, " Recording unknown type\n");
1330 /* If we found a constructor of type that is not polymorphic or
1331 that may contain the type in question as a field (not as base),
1332 restrict to the inner class first to make type matching bellow
1336 || (TREE_CODE (type
) != RECORD_TYPE
1337 || !TYPE_BINFO (type
)
1338 || !polymorphic_type_binfo_p (TYPE_BINFO (type
)))))
1340 ipa_polymorphic_call_context context
;
1342 context
.offset
= offset
;
1343 context
.outer_type
= type
;
1344 context
.maybe_in_construction
= false;
1345 context
.maybe_derived_type
= false;
1346 context
.dynamic
= true;
1347 /* If we failed to find the inner type, we know that the call
1348 would be undefined for type produced here. */
1349 if (!context
.restrict_to_inner_class (tci
->otr_type
))
1352 fprintf (dump_file
, " Ignoring; does not contain otr_type\n");
1355 /* Watch for case we reached an POD type and anticipate placement
1357 if (!context
.maybe_derived_type
)
1359 type
= context
.outer_type
;
1360 offset
= context
.offset
;
1363 if (tci
->type_maybe_changed
1364 && (!types_same_for_odr (type
, tci
->known_current_type
)
1365 || offset
!= tci
->known_current_offset
))
1366 tci
->multiple_types_encountered
= true;
1367 tci
->known_current_type
= TYPE_MAIN_VARIANT (type
);
1368 tci
->known_current_offset
= offset
;
1369 tci
->type_maybe_changed
= true;
1372 /* Callback of walk_aliased_vdefs and a helper function for
1373 detect_type_change to check whether a particular statement may modify
1374 the virtual table pointer, and if possible also determine the new type of
1375 the (sub-)object. It stores its result into DATA, which points to a
1376 type_change_info structure. */
1379 check_stmt_for_type_change (ao_ref
*ao ATTRIBUTE_UNUSED
, tree vdef
, void *data
)
1381 gimple stmt
= SSA_NAME_DEF_STMT (vdef
);
1382 struct type_change_info
*tci
= (struct type_change_info
*) data
;
1385 /* If we already gave up, just terminate the rest of walk. */
1386 if (tci
->multiple_types_encountered
)
1389 if (is_gimple_call (stmt
))
1391 if (gimple_call_flags (stmt
) & (ECF_CONST
| ECF_PURE
))
1394 /* Check for a constructor call. */
1395 if ((fn
= gimple_call_fndecl (stmt
)) != NULL_TREE
1396 && DECL_CXX_CONSTRUCTOR_P (fn
)
1397 && TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
1398 && gimple_call_num_args (stmt
))
1400 tree op
= walk_ssa_copies (gimple_call_arg (stmt
, 0));
1401 tree type
= method_class_type (TREE_TYPE (fn
));
1402 HOST_WIDE_INT offset
= 0, size
, max_size
;
1406 fprintf (dump_file
, " Checking constructor call: ");
1407 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1410 /* See if THIS parameter seems like instance pointer. */
1411 if (TREE_CODE (op
) == ADDR_EXPR
)
1413 op
= get_ref_base_and_extent (TREE_OPERAND (op
, 0),
1414 &offset
, &size
, &max_size
);
1415 if (size
!= max_size
|| max_size
== -1)
1417 tci
->speculative
= true;
1420 if (op
&& TREE_CODE (op
) == MEM_REF
)
1422 if (!tree_fits_shwi_p (TREE_OPERAND (op
, 1)))
1424 tci
->speculative
= true;
1427 offset
+= tree_to_shwi (TREE_OPERAND (op
, 1))
1429 op
= TREE_OPERAND (op
, 0);
1431 else if (DECL_P (op
))
1435 tci
->speculative
= true;
1438 op
= walk_ssa_copies (op
);
1440 if (operand_equal_p (op
, tci
->instance
, 0)
1442 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
1443 && tree_fits_shwi_p (TYPE_SIZE (type
))
1444 && tree_to_shwi (TYPE_SIZE (type
)) + offset
> tci
->offset
)
1446 record_known_type (tci
, type
, tci
->offset
- offset
);
1450 /* Calls may possibly change dynamic type by placement new. Assume
1451 it will not happen, but make result speculative only. */
1454 fprintf (dump_file
, " Function call may change dynamic type:");
1455 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1457 tci
->speculative
= true;
1460 /* Check for inlined virtual table store. */
1461 else if (noncall_stmt_may_be_vtbl_ptr_store (stmt
))
1464 HOST_WIDE_INT offset
= 0;
1467 fprintf (dump_file
, " Checking vtbl store: ");
1468 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1471 type
= extr_type_from_vtbl_ptr_store (stmt
, tci
, &offset
);
1472 if (type
== error_mark_node
)
1474 gcc_assert (!type
|| TYPE_MAIN_VARIANT (type
) == type
);
1478 fprintf (dump_file
, " Unanalyzed store may change type.\n");
1479 tci
->seen_unanalyzed_store
= true;
1480 tci
->speculative
= true;
1483 record_known_type (tci
, type
, offset
);
1490 /* THIS is polymorphic call context obtained from get_polymorphic_context.
1491 OTR_OBJECT is pointer to the instance returned by OBJ_TYPE_REF_OBJECT.
1492 INSTANCE is pointer to the outer instance as returned by
1493 get_polymorphic_context. To avoid creation of temporary expressions,
1494 INSTANCE may also be an declaration of get_polymorphic_context found the
1495 value to be in static storage.
1497 If the type of instance is not fully determined
1498 (either OUTER_TYPE is unknown or MAYBE_IN_CONSTRUCTION/INCLUDE_DERIVED_TYPES
1499 is set), try to walk memory writes and find the actual construction of the
1502 Return true if memory is unchanged from function entry.
1504 We do not include this analysis in the context analysis itself, because
1505 it needs memory SSA to be fully built and the walk may be expensive.
1506 So it is not suitable for use withing fold_stmt and similar uses. */
1509 ipa_polymorphic_call_context::get_dynamic_type (tree instance
,
1514 struct type_change_info tci
;
1516 bool function_entry_reached
= false;
1517 tree instance_ref
= NULL
;
1519 /* Remember OFFSET before it is modified by restrict_to_inner_class.
1520 This is because we do not update INSTANCE when walking inwards. */
1521 HOST_WIDE_INT instance_offset
= offset
;
1524 otr_type
= TYPE_MAIN_VARIANT (otr_type
);
1526 /* Walk into inner type. This may clear maybe_derived_type and save us
1527 from useless work. It also makes later comparsions with static type
1529 if (outer_type
&& otr_type
)
1531 if (!restrict_to_inner_class (otr_type
))
1535 if (!maybe_in_construction
&& !maybe_derived_type
)
1538 /* We need to obtain refernce to virtual table pointer. It is better
1539 to look it up in the code rather than build our own. This require bit
1540 of pattern matching, but we end up verifying that what we found is
1543 What we pattern match is:
1545 tmp = instance->_vptr.A; // vtbl ptr load
1546 tmp2 = tmp[otr_token]; // vtable lookup
1547 OBJ_TYPE_REF(tmp2;instance->0) (instance);
1549 We want to start alias oracle walk from vtbl pointer load,
1550 but we may not be able to identify it, for example, when PRE moved the
1553 if (gimple_code (call
) == GIMPLE_CALL
)
1555 tree ref
= gimple_call_fn (call
);
1556 HOST_WIDE_INT offset2
, size
, max_size
;
1558 if (TREE_CODE (ref
) == OBJ_TYPE_REF
)
1560 ref
= OBJ_TYPE_REF_EXPR (ref
);
1561 ref
= walk_ssa_copies (ref
);
1563 /* Check if definition looks like vtable lookup. */
1564 if (TREE_CODE (ref
) == SSA_NAME
1565 && !SSA_NAME_IS_DEFAULT_DEF (ref
)
1566 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref
))
1567 && TREE_CODE (gimple_assign_rhs1
1568 (SSA_NAME_DEF_STMT (ref
))) == MEM_REF
)
1570 ref
= get_base_address
1571 (TREE_OPERAND (gimple_assign_rhs1
1572 (SSA_NAME_DEF_STMT (ref
)), 0));
1573 ref
= walk_ssa_copies (ref
);
1574 /* Find base address of the lookup and see if it looks like
1576 if (TREE_CODE (ref
) == SSA_NAME
1577 && !SSA_NAME_IS_DEFAULT_DEF (ref
)
1578 && gimple_assign_load_p (SSA_NAME_DEF_STMT (ref
)))
1580 tree ref_exp
= gimple_assign_rhs1 (SSA_NAME_DEF_STMT (ref
));
1581 tree base_ref
= get_ref_base_and_extent
1582 (ref_exp
, &offset2
, &size
, &max_size
);
1584 /* Finally verify that what we found looks like read from OTR_OBJECT
1585 or from INSTANCE with offset OFFSET. */
1587 && ((TREE_CODE (base_ref
) == MEM_REF
1588 && ((offset2
== instance_offset
1589 && TREE_OPERAND (base_ref
, 0) == instance
)
1590 || (!offset2
&& TREE_OPERAND (base_ref
, 0) == otr_object
)))
1591 || (DECL_P (instance
) && base_ref
== instance
1592 && offset2
== instance_offset
)))
1594 stmt
= SSA_NAME_DEF_STMT (ref
);
1595 instance_ref
= ref_exp
;
1602 /* If we failed to look up the refernece in code, build our own. */
1605 /* If the statement in question does not use memory, we can't tell
1607 if (!gimple_vuse (stmt
))
1609 ao_ref_init_from_ptr_and_size (&ao
, otr_object
, NULL
);
1612 /* Otherwise use the real reference. */
1613 ao_ref_init (&ao
, instance_ref
);
1615 /* We look for vtbl pointer read. */
1616 ao
.size
= POINTER_SIZE
;
1617 ao
.max_size
= ao
.size
;
1620 = get_deref_alias_set (TREE_TYPE (BINFO_VTABLE (TYPE_BINFO (otr_type
))));
1624 fprintf (dump_file
, "Determining dynamic type for call: ");
1625 print_gimple_stmt (dump_file
, call
, 0, 0);
1626 fprintf (dump_file
, " Starting walk at: ");
1627 print_gimple_stmt (dump_file
, stmt
, 0, 0);
1628 fprintf (dump_file
, " instance pointer: ");
1629 print_generic_expr (dump_file
, otr_object
, TDF_SLIM
);
1630 fprintf (dump_file
, " Outer instance pointer: ");
1631 print_generic_expr (dump_file
, instance
, TDF_SLIM
);
1632 fprintf (dump_file
, " offset: %i (bits)", (int)offset
);
1633 fprintf (dump_file
, " vtbl reference: ");
1634 print_generic_expr (dump_file
, instance_ref
, TDF_SLIM
);
1635 fprintf (dump_file
, "\n");
1638 tci
.offset
= offset
;
1639 tci
.instance
= instance
;
1640 tci
.vtbl_ptr_ref
= instance_ref
;
1641 gcc_assert (TREE_CODE (instance
) != MEM_REF
);
1642 tci
.known_current_type
= NULL_TREE
;
1643 tci
.known_current_offset
= 0;
1644 tci
.otr_type
= otr_type
;
1645 tci
.type_maybe_changed
= false;
1646 tci
.multiple_types_encountered
= false;
1647 tci
.speculative
= false;
1648 tci
.seen_unanalyzed_store
= false;
1650 walk_aliased_vdefs (&ao
, gimple_vuse (stmt
), check_stmt_for_type_change
,
1651 &tci
, NULL
, &function_entry_reached
);
1653 /* If we did not find any type changing statements, we may still drop
1654 maybe_in_construction flag if the context already have outer type.
1656 Here we make special assumptions about both constructors and
1657 destructors which are all the functions that are allowed to alter the
1658 VMT pointers. It assumes that destructors begin with assignment into
1659 all VMT pointers and that constructors essentially look in the
1662 1) The very first thing they do is that they call constructors of
1663 ancestor sub-objects that have them.
1665 2) Then VMT pointers of this and all its ancestors is set to new
1666 values corresponding to the type corresponding to the constructor.
1668 3) Only afterwards, other stuff such as constructor of member
1669 sub-objects and the code written by the user is run. Only this may
1670 include calling virtual functions, directly or indirectly.
1672 4) placement new can not be used to change type of non-POD statically
1673 allocated variables.
1675 There is no way to call a constructor of an ancestor sub-object in any
1678 This means that we do not have to care whether constructors get the
1679 correct type information because they will always change it (in fact,
1680 if we define the type to be given by the VMT pointer, it is undefined).
1682 The most important fact to derive from the above is that if, for some
1683 statement in the section 3, we try to detect whether the dynamic type
1684 has changed, we can safely ignore all calls as we examine the function
1685 body backwards until we reach statements in section 2 because these
1686 calls cannot be ancestor constructors or destructors (if the input is
1687 not bogus) and so do not change the dynamic type (this holds true only
1688 for automatically allocated objects but at the moment we devirtualize
1689 only these). We then must detect that statements in section 2 change
1690 the dynamic type and can try to derive the new type. That is enough
1691 and we can stop, we will never see the calls into constructors of
1692 sub-objects in this code.
1694 Therefore if the static outer type was found (outer_type)
1695 we can safely ignore tci.speculative that is set on calls and give up
1696 only if there was dyanmic type store that may affect given variable
1697 (seen_unanalyzed_store) */
1699 if (!tci
.type_maybe_changed
1702 && !tci
.seen_unanalyzed_store
1703 && !tci
.multiple_types_encountered
1704 && offset
== tci
.offset
1705 && types_same_for_odr (tci
.known_current_type
,
1708 if (!outer_type
|| tci
.seen_unanalyzed_store
)
1710 if (maybe_in_construction
)
1711 maybe_in_construction
= false;
1713 fprintf (dump_file
, " No dynamic type change found.\n");
1717 if (tci
.known_current_type
1718 && !function_entry_reached
1719 && !tci
.multiple_types_encountered
)
1721 if (!tci
.speculative
)
1723 outer_type
= TYPE_MAIN_VARIANT (tci
.known_current_type
);
1724 offset
= tci
.known_current_offset
;
1726 maybe_in_construction
= false;
1727 maybe_derived_type
= false;
1729 fprintf (dump_file
, " Determined dynamic type.\n");
1731 else if (!speculative_outer_type
1732 || speculative_maybe_derived_type
)
1734 speculative_outer_type
= TYPE_MAIN_VARIANT (tci
.known_current_type
);
1735 speculative_offset
= tci
.known_current_offset
;
1736 speculative_maybe_derived_type
= false;
1738 fprintf (dump_file
, " Determined speculative dynamic type.\n");
1743 fprintf (dump_file
, " Found multiple types%s%s\n",
1744 function_entry_reached
? " (function entry reached)" : "",
1745 function_entry_reached
? " (multiple types encountered)" : "");
1751 /* See if speculation given by SPEC_OUTER_TYPE, SPEC_OFFSET and SPEC_MAYBE_DERIVED_TYPE
1752 seems consistent (and useful) with what we already have in the non-speculative context. */
1755 ipa_polymorphic_call_context::speculation_consistent_p (tree spec_outer_type
,
1756 HOST_WIDE_INT spec_offset
,
1757 bool spec_maybe_derived_type
,
1758 tree otr_type
) const
1760 if (!flag_devirtualize_speculatively
)
1763 /* Non-polymorphic types are useless for deriving likely polymorphic
1765 if (!spec_outer_type
|| !contains_polymorphic_type_p (spec_outer_type
))
1768 /* If we know nothing, speculation is always good. */
1772 /* Speculation is only useful to avoid derived types.
1773 This is not 100% true for placement new, where the outer context may
1774 turn out to be useless, but ignore these for now. */
1775 if (!maybe_derived_type
)
1778 /* If types agrees, speculation is consistent, but it makes sense only
1779 when it says something new. */
1780 if (types_must_be_same_for_odr (spec_outer_type
, outer_type
))
1781 return maybe_derived_type
&& !spec_maybe_derived_type
;
1783 /* If speculation does not contain the type in question, ignore it. */
1785 && !contains_type_p (spec_outer_type
, spec_offset
, otr_type
, false, true))
1788 /* If outer type already contains speculation as a filed,
1789 it is useless. We already know from OUTER_TYPE
1790 SPEC_TYPE and that it is not in the construction. */
1791 if (contains_type_p (outer_type
, offset
- spec_offset
,
1792 spec_outer_type
, false, false))
1795 /* If speculative outer type is not more specified than outer
1797 We can only decide this safely if we can compare types with OUTER_TYPE.
1799 if ((!in_lto_p
|| odr_type_p (outer_type
))
1800 && !contains_type_p (spec_outer_type
,
1801 spec_offset
- offset
,
1807 /* Improve THIS with speculation described by NEW_OUTER_TYPE, NEW_OFFSET,
1808 NEW_MAYBE_DERIVED_TYPE
1809 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1812 ipa_polymorphic_call_context::combine_speculation_with
1813 (tree new_outer_type
, HOST_WIDE_INT new_offset
, bool new_maybe_derived_type
,
1816 if (!new_outer_type
)
1819 /* restrict_to_inner_class may eliminate wrong speculation making our job
1822 restrict_to_inner_class (otr_type
);
1824 if (!speculation_consistent_p (new_outer_type
, new_offset
,
1825 new_maybe_derived_type
, otr_type
))
1828 /* New speculation is a win in case we have no speculation or new
1829 speculation does not consider derivations. */
1830 if (!speculative_outer_type
1831 || (speculative_maybe_derived_type
1832 && !new_maybe_derived_type
))
1834 speculative_outer_type
= new_outer_type
;
1835 speculative_offset
= new_offset
;
1836 speculative_maybe_derived_type
= new_maybe_derived_type
;
1839 else if (types_must_be_same_for_odr (speculative_outer_type
,
1842 if (speculative_offset
!= new_offset
)
1844 /* OK we have two contexts that seems valid but they disagree,
1847 This is not a lattice operation, so we may want to drop it later. */
1848 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1850 "Speculative outer types match, "
1851 "offset mismatch -> invalid speculation\n");
1852 clear_speculation ();
1857 if (speculative_maybe_derived_type
&& !new_maybe_derived_type
)
1859 speculative_maybe_derived_type
= false;
1866 /* Choose type that contains the other. This one either contains the outer
1867 as a field (thus giving exactly one target) or is deeper in the type
1869 else if (speculative_outer_type
1870 && speculative_maybe_derived_type
1871 && (new_offset
> speculative_offset
1872 || (new_offset
== speculative_offset
1873 && contains_type_p (new_outer_type
,
1874 0, speculative_outer_type
, false))))
1876 tree old_outer_type
= speculative_outer_type
;
1877 HOST_WIDE_INT old_offset
= speculative_offset
;
1878 bool old_maybe_derived_type
= speculative_maybe_derived_type
;
1880 speculative_outer_type
= new_outer_type
;
1881 speculative_offset
= new_offset
;
1882 speculative_maybe_derived_type
= new_maybe_derived_type
;
1885 restrict_to_inner_class (otr_type
);
1887 /* If the speculation turned out to make no sense, revert to sensible
1889 if (!speculative_outer_type
)
1891 speculative_outer_type
= old_outer_type
;
1892 speculative_offset
= old_offset
;
1893 speculative_maybe_derived_type
= old_maybe_derived_type
;
1896 return (old_offset
!= speculative_offset
1897 || old_maybe_derived_type
!= speculative_maybe_derived_type
1898 || types_must_be_same_for_odr (speculative_outer_type
,
1904 /* Make speculation less specific so
1905 NEW_OUTER_TYPE, NEW_OFFSET, NEW_MAYBE_DERIVED_TYPE is also included.
1906 If OTR_TYPE is set, assume the context is used with OTR_TYPE. */
1909 ipa_polymorphic_call_context::meet_speculation_with
1910 (tree new_outer_type
, HOST_WIDE_INT new_offset
, bool new_maybe_derived_type
,
1913 if (!new_outer_type
&& speculative_outer_type
)
1915 clear_speculation ();
1919 /* restrict_to_inner_class may eliminate wrong speculation making our job
1922 restrict_to_inner_class (otr_type
);
1924 if (!speculative_outer_type
1925 || !speculation_consistent_p (speculative_outer_type
,
1927 speculative_maybe_derived_type
,
1931 if (!speculation_consistent_p (new_outer_type
, new_offset
,
1932 new_maybe_derived_type
, otr_type
))
1934 clear_speculation ();
1938 else if (types_must_be_same_for_odr (speculative_outer_type
,
1941 if (speculative_offset
!= new_offset
)
1943 clear_speculation ();
1948 if (!speculative_maybe_derived_type
&& new_maybe_derived_type
)
1950 speculative_maybe_derived_type
= true;
1957 /* See if one type contains the other as a field (not base). */
1958 else if (contains_type_p (new_outer_type
, new_offset
- speculative_offset
,
1959 speculative_outer_type
, false, false))
1961 else if (contains_type_p (speculative_outer_type
,
1962 speculative_offset
- new_offset
,
1963 new_outer_type
, false, false))
1965 speculative_outer_type
= new_outer_type
;
1966 speculative_offset
= new_offset
;
1967 speculative_maybe_derived_type
= new_maybe_derived_type
;
1970 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
1971 else if (contains_type_p (new_outer_type
,
1972 new_offset
- speculative_offset
,
1973 speculative_outer_type
, false, true))
1975 if (!speculative_maybe_derived_type
)
1977 speculative_maybe_derived_type
= true;
1982 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
1983 else if (contains_type_p (speculative_outer_type
,
1984 speculative_offset
- new_offset
, new_outer_type
, false, true))
1986 speculative_outer_type
= new_outer_type
;
1987 speculative_offset
= new_offset
;
1988 speculative_maybe_derived_type
= true;
1993 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1994 fprintf (dump_file
, "Giving up on speculative meet\n");
1995 clear_speculation ();
2000 /* Assume that both THIS and a given context is valid and strenghten THIS
2001 if possible. Return true if any strenghtening was made.
2002 If actual type the context is being used in is known, OTR_TYPE should be
2003 set accordingly. This improves quality of combined result. */
2006 ipa_polymorphic_call_context::combine_with (ipa_polymorphic_call_context ctx
,
2009 bool updated
= false;
2011 if (ctx
.useless_p () || invalid
)
2014 /* Restricting context to inner type makes merging easier, however do not
2015 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2016 if (otr_type
&& !invalid
&& !ctx
.invalid
)
2018 restrict_to_inner_class (otr_type
);
2019 ctx
.restrict_to_inner_class (otr_type
);
2024 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2026 fprintf (dump_file
, "Polymorphic call context combine:");
2028 fprintf (dump_file
, "With context: ");
2029 ctx
.dump (dump_file
);
2032 fprintf (dump_file
, "To be used with type: ");
2033 print_generic_expr (dump_file
, otr_type
, TDF_SLIM
);
2034 fprintf (dump_file
, "\n");
2038 /* If call is known to be invalid, we are done. */
2041 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2042 fprintf (dump_file
, "-> Invalid context\n");
2046 if (!ctx
.outer_type
)
2048 else if (!outer_type
)
2050 outer_type
= ctx
.outer_type
;
2051 offset
= ctx
.offset
;
2052 dynamic
= ctx
.dynamic
;
2053 maybe_in_construction
= ctx
.maybe_in_construction
;
2054 maybe_derived_type
= ctx
.maybe_derived_type
;
2057 /* If types are known to be same, merging is quite easy. */
2058 else if (types_must_be_same_for_odr (outer_type
, ctx
.outer_type
))
2060 if (offset
!= ctx
.offset
2061 && TYPE_SIZE (outer_type
)
2062 && TREE_CODE (TYPE_SIZE (outer_type
)) == INTEGER_CST
)
2064 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2065 fprintf (dump_file
, "Outer types match, offset mismatch -> invalid\n");
2066 clear_speculation ();
2067 clear_outer_type ();
2071 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2072 fprintf (dump_file
, "Outer types match, merging flags\n");
2073 if (maybe_in_construction
&& !ctx
.maybe_in_construction
)
2076 maybe_in_construction
= false;
2078 if (maybe_derived_type
&& !ctx
.maybe_derived_type
)
2081 maybe_derived_type
= false;
2083 if (dynamic
&& !ctx
.dynamic
)
2089 /* If we know the type precisely, there is not much to improve. */
2090 else if (!maybe_derived_type
&& !maybe_in_construction
2091 && !ctx
.maybe_derived_type
&& !ctx
.maybe_in_construction
)
2093 /* It may be easy to check if second context permits the first
2094 and set INVALID otherwise. This is not easy to do in general;
2095 contains_type_p may return false negatives for non-comparable
2098 If OTR_TYPE is known, we however can expect that
2099 restrict_to_inner_class should have discovered the same base
2101 if (otr_type
&& !ctx
.maybe_in_construction
&& !ctx
.maybe_derived_type
)
2103 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2104 fprintf (dump_file
, "Contextes disagree -> invalid\n");
2108 /* See if one type contains the other as a field (not base).
2109 In this case we want to choose the wider type, because it contains
2110 more information. */
2111 else if (contains_type_p (ctx
.outer_type
, ctx
.offset
- offset
,
2112 outer_type
, false, false))
2114 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2115 fprintf (dump_file
, "Second type contain the first as a field\n");
2117 if (maybe_derived_type
)
2119 outer_type
= ctx
.outer_type
;
2120 maybe_derived_type
= ctx
.maybe_derived_type
;
2121 offset
= ctx
.offset
;
2122 dynamic
= ctx
.dynamic
;
2126 /* If we do not know how the context is being used, we can
2127 not clear MAYBE_IN_CONSTRUCTION because it may be offseted
2128 to other component of OUTER_TYPE later and we know nothing
2130 if (otr_type
&& maybe_in_construction
2131 && !ctx
.maybe_in_construction
)
2133 maybe_in_construction
= false;
2137 else if (contains_type_p (outer_type
, offset
- ctx
.offset
,
2138 ctx
.outer_type
, false, false))
2140 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2141 fprintf (dump_file
, "First type contain the second as a field\n");
2143 if (otr_type
&& maybe_in_construction
2144 && !ctx
.maybe_in_construction
)
2146 maybe_in_construction
= false;
2150 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2151 else if (contains_type_p (ctx
.outer_type
,
2152 ctx
.offset
- offset
, outer_type
, false, true))
2154 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2155 fprintf (dump_file
, "First type is base of second\n");
2156 if (!maybe_derived_type
)
2158 if (!ctx
.maybe_in_construction
2159 && types_odr_comparable (outer_type
, ctx
.outer_type
))
2161 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2162 fprintf (dump_file
, "Second context does not permit base -> invalid\n");
2166 /* Pick variant deeper in the hiearchy. */
2169 outer_type
= ctx
.outer_type
;
2170 maybe_in_construction
= ctx
.maybe_in_construction
;
2171 maybe_derived_type
= ctx
.maybe_derived_type
;
2172 offset
= ctx
.offset
;
2173 dynamic
= ctx
.dynamic
;
2177 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2178 else if (contains_type_p (outer_type
,
2179 offset
- ctx
.offset
, ctx
.outer_type
, false, true))
2181 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2182 fprintf (dump_file
, "Second type is base of first\n");
2183 if (!ctx
.maybe_derived_type
)
2185 if (!maybe_in_construction
2186 && types_odr_comparable (outer_type
, ctx
.outer_type
))
2188 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2189 fprintf (dump_file
, "First context does not permit base -> invalid\n");
2192 /* Pick the base type. */
2193 else if (maybe_in_construction
)
2195 outer_type
= ctx
.outer_type
;
2196 maybe_in_construction
= ctx
.maybe_in_construction
;
2197 maybe_derived_type
= ctx
.maybe_derived_type
;
2198 offset
= ctx
.offset
;
2199 dynamic
= ctx
.dynamic
;
2204 /* TODO handle merging using hiearchy. */
2205 else if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2206 fprintf (dump_file
, "Giving up on merge\n");
2208 updated
|= combine_speculation_with (ctx
.speculative_outer_type
,
2209 ctx
.speculative_offset
,
2210 ctx
.speculative_maybe_derived_type
,
2213 if (updated
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
2215 fprintf (dump_file
, "Updated as: ");
2217 fprintf (dump_file
, "\n");
2223 clear_speculation ();
2224 clear_outer_type ();
2228 /* Take non-speculative info, merge it with speculative and clear speculation.
2229 Used when we no longer manage to keep track of actual outer type, but we
2230 think it is still there.
2232 If OTR_TYPE is set, the transformation can be done more effectively assuming
2233 that context is going to be used only that way. */
2236 ipa_polymorphic_call_context::make_speculative (tree otr_type
)
2238 tree spec_outer_type
= outer_type
;
2239 HOST_WIDE_INT spec_offset
= offset
;
2240 bool spec_maybe_derived_type
= maybe_derived_type
;
2245 clear_outer_type ();
2246 clear_speculation ();
2251 clear_outer_type ();
2252 combine_speculation_with (spec_outer_type
, spec_offset
,
2253 spec_maybe_derived_type
,
2257 /* Use when we can not track dynamic type change. This speculatively assume
2258 type change is not happening. */
2261 ipa_polymorphic_call_context::possible_dynamic_type_change (bool in_poly_cdtor
,
2265 make_speculative (otr_type
);
2266 else if (in_poly_cdtor
)
2267 maybe_in_construction
= true;
2270 /* Return TRUE if this context conveys the same information as OTHER. */
2273 ipa_polymorphic_call_context::equal_to
2274 (const ipa_polymorphic_call_context
&x
) const
2277 return x
.useless_p ();
2280 if (x
.useless_p () || x
.invalid
)
2286 || !types_odr_comparable (outer_type
, x
.outer_type
)
2287 || !types_same_for_odr (outer_type
, x
.outer_type
)
2288 || offset
!= x
.offset
2289 || maybe_in_construction
!= x
.maybe_in_construction
2290 || maybe_derived_type
!= x
.maybe_derived_type
2291 || dynamic
!= x
.dynamic
)
2294 else if (x
.outer_type
)
2298 if (speculative_outer_type
2299 && speculation_consistent_p (speculative_outer_type
, speculative_offset
,
2300 speculative_maybe_derived_type
, NULL_TREE
))
2302 if (!x
.speculative_outer_type
)
2305 if (!types_odr_comparable (speculative_outer_type
,
2306 x
.speculative_outer_type
)
2307 || !types_same_for_odr (speculative_outer_type
,
2308 x
.speculative_outer_type
)
2309 || speculative_offset
!= x
.speculative_offset
2310 || speculative_maybe_derived_type
!= x
.speculative_maybe_derived_type
)
2313 else if (x
.speculative_outer_type
2314 && x
.speculation_consistent_p (x
.speculative_outer_type
,
2315 x
.speculative_offset
,
2316 x
.speculative_maybe_derived_type
,
2323 /* Modify context to be strictly less restrictive than CTX. */
2326 ipa_polymorphic_call_context::meet_with (ipa_polymorphic_call_context ctx
,
2329 bool updated
= false;
2331 if (useless_p () || ctx
.invalid
)
2334 /* Restricting context to inner type makes merging easier, however do not
2335 do that unless we know how the context is used (OTR_TYPE is non-NULL) */
2336 if (otr_type
&& !useless_p () && !ctx
.useless_p ())
2338 restrict_to_inner_class (otr_type
);
2339 ctx
.restrict_to_inner_class (otr_type
);
2347 if (ctx
.useless_p () || invalid
)
2353 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2355 fprintf (dump_file
, "Polymorphic call context meet:");
2357 fprintf (dump_file
, "With context: ");
2358 ctx
.dump (dump_file
);
2361 fprintf (dump_file
, "To be used with type: ");
2362 print_generic_expr (dump_file
, otr_type
, TDF_SLIM
);
2363 fprintf (dump_file
, "\n");
2367 if (!dynamic
&& ctx
.dynamic
)
2373 /* If call is known to be invalid, we are done. */
2376 else if (!ctx
.outer_type
)
2378 clear_outer_type ();
2381 /* If types are known to be same, merging is quite easy. */
2382 else if (types_must_be_same_for_odr (outer_type
, ctx
.outer_type
))
2384 if (offset
!= ctx
.offset
2385 && TYPE_SIZE (outer_type
)
2386 && TREE_CODE (TYPE_SIZE (outer_type
)) == INTEGER_CST
)
2388 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2389 fprintf (dump_file
, "Outer types match, offset mismatch -> clearing\n");
2390 clear_outer_type ();
2393 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2394 fprintf (dump_file
, "Outer types match, merging flags\n");
2395 if (!maybe_in_construction
&& ctx
.maybe_in_construction
)
2398 maybe_in_construction
= true;
2400 if (!maybe_derived_type
&& ctx
.maybe_derived_type
)
2403 maybe_derived_type
= true;
2405 if (!dynamic
&& ctx
.dynamic
)
2411 /* See if one type contains the other as a field (not base). */
2412 else if (contains_type_p (ctx
.outer_type
, ctx
.offset
- offset
,
2413 outer_type
, false, false))
2415 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2416 fprintf (dump_file
, "Second type contain the first as a field\n");
2418 /* The second type is more specified, so we keep the first.
2419 We need to set DYNAMIC flag to avoid declaring context INVALID
2420 of OFFSET ends up being out of range. */
2424 && (!TYPE_SIZE (ctx
.outer_type
)
2425 || !TYPE_SIZE (outer_type
)
2426 || !operand_equal_p (TYPE_SIZE (ctx
.outer_type
),
2427 TYPE_SIZE (outer_type
), 0)))))
2433 else if (contains_type_p (outer_type
, offset
- ctx
.offset
,
2434 ctx
.outer_type
, false, false))
2436 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2437 fprintf (dump_file
, "First type contain the second as a field\n");
2442 && (!TYPE_SIZE (ctx
.outer_type
)
2443 || !TYPE_SIZE (outer_type
)
2444 || !operand_equal_p (TYPE_SIZE (ctx
.outer_type
),
2445 TYPE_SIZE (outer_type
), 0)))))
2447 outer_type
= ctx
.outer_type
;
2448 offset
= ctx
.offset
;
2449 dynamic
= ctx
.dynamic
;
2450 maybe_in_construction
= ctx
.maybe_in_construction
;
2451 maybe_derived_type
= ctx
.maybe_derived_type
;
2454 /* See if OUTER_TYPE is base of CTX.OUTER_TYPE. */
2455 else if (contains_type_p (ctx
.outer_type
,
2456 ctx
.offset
- offset
, outer_type
, false, true))
2458 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2459 fprintf (dump_file
, "First type is base of second\n");
2460 if (!maybe_derived_type
)
2462 maybe_derived_type
= true;
2465 if (!maybe_in_construction
&& ctx
.maybe_in_construction
)
2467 maybe_in_construction
= true;
2470 if (!dynamic
&& ctx
.dynamic
)
2476 /* See if CTX.OUTER_TYPE is base of OUTER_TYPE. */
2477 else if (contains_type_p (outer_type
,
2478 offset
- ctx
.offset
, ctx
.outer_type
, false, true))
2480 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2481 fprintf (dump_file
, "Second type is base of first\n");
2482 outer_type
= ctx
.outer_type
;
2483 offset
= ctx
.offset
;
2485 if (!maybe_derived_type
)
2486 maybe_derived_type
= true;
2487 if (!maybe_in_construction
&& ctx
.maybe_in_construction
)
2488 maybe_in_construction
= true;
2489 if (!dynamic
&& ctx
.dynamic
)
2492 /* TODO handle merging using hiearchy. */
2495 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2496 fprintf (dump_file
, "Giving up on meet\n");
2497 clear_outer_type ();
2501 updated
|= meet_speculation_with (ctx
.speculative_outer_type
,
2502 ctx
.speculative_offset
,
2503 ctx
.speculative_maybe_derived_type
,
2506 if (updated
&& dump_file
&& (dump_flags
& TDF_DETAILS
))
2508 fprintf (dump_file
, "Updated as: ");
2510 fprintf (dump_file
, "\n");