diff mbox series

[2/2] ipa-devirt: Add option to assume the whole class hierarchy known

Message ID ri6y0b8b4ox.fsf@virgil.suse.cz
State New
Headers show
Series [1/2] ipa-devirt: Expand check that an odr_type cannot have derivations | expand

Commit Message

Martin Jambor Nov. 4, 2026, 10:41 p.m. UTC
Hi,

this patch adds a C++ option -fcomplete-class-hierarchy which, when
use together with -flto or -fwhole-program, will make devirtualization
optimization assume it knows about all descendants of each class and
specifically that if there are none, that the class is final even if
it not marked as such in the source code.  This allows for aggressive
devirtualization of calls to member functions of such classes.

That of course means the program must not then grab more classes from
a dlopened shared object or other plug-in, which means the option
cannot be on by default.  (A related question is whether it would be a
candidate for -Ofast, which I think would make sense but I know Honza
opposes that idea, so I'm not proposing it, at least not now.)

The option can be used by a lazy programmer who knows the application
does not use plug-ins, cannot be bothered to mark all final classes as
final but would still like to have the benefit of doing so.  Or would
like to see if it worth it before doing the marking.  It is also a
benchmark "hack" because the patch speeds up 710.omnetpp_r from SPEC
CPUrate 2026 by 5.5% (with -Ofast -flto -march-native on a zen5
machine).

LLVM has a similar option -fwhole-program-vtables (which also requires
-flto and also -fvisibility=hidden) to achieve a similar effect.

Implementation wise, the patch basically simply assumes that
all_derivations_known flag is on for every ODR type when the flag is
set.  The reason why the patch is so big is that optimization flags
are per function in GCC and so we have to get the flag value at the
time we know the context (the caller) and propagate it all the way to
the few tests in ipa-devirt.cc.

Bootstrapped and LTO-O3-bootstrapped and tested on x86_64-linux.  I have
also LTO-O3-bootstrapped it there with the default value of the flag set
to true (which we might only want when plugins are disabled but is
probably not worth the inconsistency).  OK for master?

Thanks,

Martin



gcc/c-family/ChangeLog:

2026-10-30  Martin Jambor  <mjambor@suse.cz>

	* c.opt (-fcomplete-class-hierarchy): New.

gcc/ChangeLog:

2026-10-30  Martin Jambor  <mjambor@suse.cz>

	* cgraphunit.cc (walk_polymorphic_call_targets): Pass false to
	parameter complete_hierarchy_p of possible_polymorphic_call_targets
	and dump_possible_polymorphic_call_targets.
	* ipa-cp.cc (ipa_get_indirect_edge_target_1): Pass appropriate values
	of parameter complete_hierarchy_p to possible_polymorphic_call_targets
	and try_speculative_devirtualization.
	* ipa-devirt.cc (odr_type_known_to_have_no_derivations_p): Add
	parameter complete_hierarchy_p.
	(type_known_to_have_no_derivations_p): Pass false to parameter
	complete_hierarchy_p of odr_type_known_to_have_no_derivations_p.
	(possible_polymorphic_call_targets_1): New parameter
	complete_hierarchy_p, use it.
	(class polymorphic_call_target_d): New field complete_hierarchy_p.
	(polymorphic_call_target_hasher::hash): Hash the new field.
	(polymorphic_call_target_hasher::equal): Compare the new field.
	(possible_polymorphic_call_targets): New parameter
	complete_hierarchy_p, use it.
	(dump_possible_polymorphic_call_targets): Likewise.
	(possible_polymorphic_call_target_p): Pass false to parameter
	complete_hierarchy_p of possible_polymorphic_call_targets.
	(try_speculative_devirtualization): New parameter
	complete_hierarchy_p, use it.
	(ipa_devirt): Pass appropriate values of parameter
	complete_hierarchy_p to possible_polymorphic_call_targets and
	dump_possible_polymorphic_call_targets.
	* ipa-prop.cc (try_make_edge_direct_virtual_call): Pass appropriate
	values of parameter complete_hierarchy_p to
	possible_polymorphic_call_targets and
	try_speculative_devirtualization.
	* ipa-utils.h (possible_polymorphic_call_targets): Add parameter
	complete_hierarchy_p to two of the overloads, pass the appropriate
	value to the main implementation from the other two variants.
	(dump_possible_polymorphic_call_targets): Add parameter
	complete_hierarchy_p.
	(try_speculative_devirtualization): Likewise.
	* ipa.cc (walk_polymorphic_call_targets): New parameter
	complete_hierarchy_p, pass it to possible_polymorphic_call_targets.
	(symbol_table::remove_unreachable_nodes): Pass appropriate value as
	complete_hierarchy_p to walk_polymorphic_call_targets.
	* lto-cgraph.cc (compute_ltrans_boundary): Pass false to parameter
	complete_hierarchy_p of possible_polymorphic_call_targets.
	* tree-ssa-sccvn.cc (eliminate_dom_walker::eliminate_stmt): Likewise,
	and to dump_possible_polymorphic_call_targets.
	* doc/invoke.texi (fcomplete-class-hierarchy): New.

gcc/testsuite/ChangeLog:

2026-10-30  Martin Jambor  <mjambor@suse.cz>

	* g++.dg/ipa/whole-hier-1.C: New test.
	* g++.dg/lto/whole-hier-1_0.C: Likewise.
---
 gcc/c-family/c.opt                        |  4 ++
 gcc/cgraphunit.cc                         |  6 +-
 gcc/doc/invoke.texi                       | 12 ++++
 gcc/ipa-cp.cc                             | 10 ++-
 gcc/ipa-devirt.cc                         | 86 ++++++++++++++++-------
 gcc/ipa-prop.cc                           |  8 ++-
 gcc/ipa-utils.h                           | 17 +++--
 gcc/ipa.cc                                | 14 ++--
 gcc/lto-cgraph.cc                         |  4 +-
 gcc/testsuite/g++.dg/ipa/whole-hier-1.C   | 50 +++++++++++++
 gcc/testsuite/g++.dg/lto/whole-hier-1_0.C | 51 ++++++++++++++
 gcc/tree-ssa-sccvn.cc                     |  5 +-
 12 files changed, 221 insertions(+), 46 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/ipa/whole-hier-1.C
 create mode 100644 gcc/testsuite/g++.dg/lto/whole-hier-1_0.C
diff mbox series

Patch

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index d5b973baa85..61e061b04a6 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -1864,6 +1864,10 @@  fcilkplus
 C ObjC C++ ObjC++ LTO Undocumented Ignore
 Removed in GCC 8.  This switch has no effect.
 
+fcomplete-class-hierarchy
+C++ Var(flag_complete_class_hierarchy) Optimization
+Assume the whole C++ class hierarchy is known at compile time.
+
 fconcepts
 C++ ObjC++ Var(flag_concepts)
 Enable support for C++ concepts.
diff --git a/gcc/cgraphunit.cc b/gcc/cgraphunit.cc
index b77f082757b..b430ed0deaa 100644
--- a/gcc/cgraphunit.cc
+++ b/gcc/cgraphunit.cc
@@ -1029,14 +1029,12 @@  walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
   void *cache_token;
   bool final;
   vec <cgraph_node *>targets
-    = possible_polymorphic_call_targets
-	(edge, &final, &cache_token);
+    = possible_polymorphic_call_targets (edge, false, &final, &cache_token);
 
   if (cache_token != NULL && !reachable_call_targets->add (cache_token))
     {
       if (symtab->dump_file)
-	dump_possible_polymorphic_call_targets
-	  (symtab->dump_file, edge);
+	dump_possible_polymorphic_call_targets (symtab->dump_file, edge, false);
 
       for (i = 0; i < targets.length (); i++)
 	{
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 6139462669b..50d5564b87b 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -3270,6 +3270,18 @@  return value even without this option.  In all other cases, when
 exhaustion is signalled by throwing @code{std::bad_alloc}.  See also
 @samp{new (nothrow)}.
 
+@opindex fcomplete-class-hierarchy
+@opindex fno-complete-class-hierarchy
+@item -fcomplete-class-hierarchy
+This flag, when use together with @code{-flto} or
+@code{-fwhole-program}, will make devirtualization optimization assume
+it knows about all descendants of each class and specifically that if
+there are none, that the class is final even if it not marked as such in
+the source code.  This allows for aggressive devirtualization of calls
+to member functions of such classes but will lead to unexpected results
+if there eventually happen to be descendants, for example from
+dynamically loaded plug-in.
+
 @opindex fconcepts
 @opindex fno-concepts
 @item -fconcepts
diff --git a/gcc/ipa-cp.cc b/gcc/ipa-cp.cc
index 76d3b0decda..4b4ae8357b3 100644
--- a/gcc/ipa-cp.cc
+++ b/gcc/ipa-cp.cc
@@ -3287,8 +3287,14 @@  ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
   vec <cgraph_node *>targets;
   bool final;
 
+  if (!opt_for_fn (ie->caller->decl, flag_devirtualize))
+    return NULL_TREE;
+  bool complete_hierarchy_p
+    = ((flag_wpa || flag_whole_program)
+       && opt_for_fn (ie->caller->decl, flag_complete_class_hierarchy));
   targets = possible_polymorphic_call_targets (pii->otr_type, pii->otr_token,
-					       context, &final);
+					       context, complete_hierarchy_p,
+					       &final);
   if (!final || targets.length () > 1)
     {
       struct cgraph_node *node;
@@ -3298,7 +3304,7 @@  ipa_get_indirect_edge_target_1 (struct cgraph_edge *ie,
 	  || ie->speculative || !ie->maybe_hot_p ())
 	return NULL;
       node = try_speculative_devirtualization (pii->otr_type, pii->otr_token,
-					       context);
+					       context, complete_hierarchy_p);
       if (node)
 	{
 	  *speculative = true;
diff --git a/gcc/ipa-devirt.cc b/gcc/ipa-devirt.cc
index 0695bf9e52e..cb97cd7e87a 100644
--- a/gcc/ipa-devirt.cc
+++ b/gcc/ipa-devirt.cc
@@ -2203,13 +2203,14 @@  register_odr_type (tree type)
     }
 }
 
-/* Return true if the type represented by OT is known to have no
-   derivations.  */
+/* Return true if the type represented by OT is known to have no derivations.
+   If COMPLETE_HIERARCHY_P us set, assume that the entire hierarchy of all
+   classes that will ever be part of the program is known at this point.  */
 
 static bool
-odr_type_known_to_have_no_derivations_p (odr_type ot)
+odr_type_known_to_have_no_derivations_p (odr_type ot, bool complete_hierarchy_p)
 {
-  return (ot->all_derivations_known
+  return ((complete_hierarchy_p || ot->all_derivations_known)
 	  && ot->derived_types.is_empty ());
 }
 
@@ -2222,7 +2223,8 @@  type_known_to_have_no_derivations_p (tree t)
     return true;
   if (!odr_hash)
     return false;
-  return odr_type_known_to_have_no_derivations_p (get_odr_type (t, true));
+  return odr_type_known_to_have_no_derivations_p (get_odr_type (t, true),
+						  false);
 }
 
 /* Dump ODR type T and all its derived types.  INDENT specifies indentation for
@@ -2703,7 +2705,10 @@  record_target_from_binfo (vec <cgraph_node *> &nodes,
 
    If CONSIDER_CONSTRUCTION is true, record to BASES_TO_CONSIDER
    all cases where BASE_SKIPPED is true (because the base is abstract
-   class).  */
+   class).
+
+   If COMPLETE_HIERARCHY_P us set, assume that the entire hierarchy of all
+   classes that will ever be part of the program is known at this point.  */
 
 static void
 possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
@@ -2714,6 +2719,7 @@  possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
 				     HOST_WIDE_INT otr_token,
 				     tree outer_type,
 				     HOST_WIDE_INT offset,
+				     bool complete_hierarchy_p,
 				     bool *completep,
 				     vec <tree> &bases_to_consider,
 				     bool consider_construction)
@@ -2732,7 +2738,8 @@  possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
     {
       record_target_from_binfo (nodes,
 				(!possibly_instantiated
-				 && type->all_derivations_known)
+				 && (complete_hierarchy_p ||
+				     type->all_derivations_known))
 				? &bases_to_consider : NULL,
 				binfo, otr_type, type_binfos, otr_token,
 				outer_type, offset,
@@ -2744,8 +2751,10 @@  possible_polymorphic_call_targets_1 (vec <cgraph_node *> &nodes,
 					 matched_vtables,
 					 otr_type,
 					 type->derived_types[i],
-					 otr_token, outer_type, offset, completep,
-					 bases_to_consider, consider_construction);
+					 otr_token, outer_type, offset,
+					 complete_hierarchy_p, completep,
+					 bases_to_consider,
+					 consider_construction);
 }
 
 /* Cache of queries for polymorphic call targets.
@@ -2766,6 +2775,7 @@  public:
   unsigned int n_odr_types;
   bool complete;
   bool speculative;
+  bool complete_hierarchy_p;
 };
 
 /* Polymorphic call target cache helpers.  */
@@ -2797,6 +2807,7 @@  polymorphic_call_target_hasher::hash (const polymorphic_call_target_d *odr_query
       hstate.add_hwi (odr_query->context.speculative_offset);
     }
   hstate.add_flag (odr_query->speculative);
+  hstate.add_flag (odr_query->complete_hierarchy_p);
   hstate.add_flag (odr_query->context.maybe_in_construction);
   hstate.add_flag (odr_query->context.maybe_derived_type);
   hstate.add_flag (odr_query->context.speculative_maybe_derived_type);
@@ -2812,6 +2823,7 @@  polymorphic_call_target_hasher::equal (const polymorphic_call_target_d *t1,
 {
   return (t1->type == t2->type && t1->otr_token == t2->otr_token
 	  && t1->speculative == t2->speculative
+	  && t1->complete_hierarchy_p == t2->complete_hierarchy_p
 	  && t1->context.offset == t2->context.offset
 	  && t1->context.speculative_offset == t2->context.speculative_offset
 	  && t1->context.outer_type == t2->context.outer_type
@@ -3121,6 +3133,9 @@  class final_warning_record *final_warning_records;
    If SPECULATIVE is set, the list will not contain targets that
    are not speculatively taken.
 
+   If COMPLETE_HIERARCHY_P us set, assume that the entire hierarchy of all
+   classes that will ever be part of the program is known at this point.
+
    Returned vector is placed into cache.  It is NOT caller's responsibility
    to free it.  The vector can be freed on cgraph_remove_node call if
    the particular node is a virtual function present in the cache.  */
@@ -3129,6 +3144,7 @@  vec <cgraph_node *>
 possible_polymorphic_call_targets (tree otr_type,
 			           HOST_WIDE_INT otr_token,
 				   ipa_polymorphic_call_context context,
+				   bool complete_hierarchy_p,
 			           bool *completep,
 			           void **cache_token,
 				   bool speculative)
@@ -3194,7 +3210,8 @@  possible_polymorphic_call_targets (tree otr_type,
   /* We need to update our hierarchy if the type does not exist.  */
   outer_type = get_odr_type (context.outer_type, true);
   /* If the type is complete, there are no derivations.  */
-  if (odr_type_known_to_have_no_derivations_p (outer_type))
+  if (odr_type_known_to_have_no_derivations_p (outer_type,
+					       complete_hierarchy_p))
     context.maybe_derived_type = false;
 
   /* Initialize query cache.  */
@@ -3228,6 +3245,7 @@  possible_polymorphic_call_targets (tree otr_type,
   key.speculative = speculative;
   key.context = context;
   key.n_odr_types = odr_types.length ();
+  key.complete_hierarchy_p = complete_hierarchy_p;
   slot = polymorphic_call_target_hash->find_slot (&key, INSERT);
   if (cache_token)
    *cache_token = (void *)*slot;
@@ -3283,7 +3301,8 @@  possible_polymorphic_call_targets (tree otr_type,
       /* First insert target from type itself and check if it may have
 	 derived types.  */
       speculative_outer_type = get_odr_type (context.speculative_outer_type, true);
-      if (odr_type_known_to_have_no_derivations_p (speculative_outer_type))
+      if (odr_type_known_to_have_no_derivations_p (speculative_outer_type,
+						   complete_hierarchy_p))
 	context.speculative_maybe_derived_type = false;
       binfo = get_binfo_at_offset (TYPE_BINFO (speculative_outer_type->type),
 				   context.speculative_offset, otr_type);
@@ -3316,6 +3335,7 @@  possible_polymorphic_call_targets (tree otr_type,
 					       speculative_outer_type->derived_types[i],
 					       otr_token, speculative_outer_type->type,
 					       context.speculative_offset,
+					       complete_hierarchy_p,
 					       &speculation_complete,
 					       bases_to_consider,
 					       false);
@@ -3369,11 +3389,12 @@  possible_polymorphic_call_targets (tree otr_type,
 						 otr_type,
 						 outer_type->derived_types[i],
 						 otr_token, outer_type->type,
-						 context.offset, &complete,
-						 bases_to_consider,
+						 context.offset,
+						 complete_hierarchy_p,
+						 &complete, bases_to_consider,
 						 context.maybe_in_construction);
 
-	  if (!outer_type->all_derivations_known)
+	  if (!complete_hierarchy_p && !outer_type->all_derivations_known)
 	    {
 	      if (!speculative && final_warning_records
 		  && nodes.length () == 1
@@ -3438,6 +3459,7 @@  possible_polymorphic_call_targets (tree otr_type,
 	      if (type != outer_type
 		  && (!skipped
 		      || (context.maybe_derived_type
+			  && !complete_hierarchy_p
 			  && !outer_type->all_derivations_known)))
 		record_targets_from_bases (otr_type, otr_token, outer_type->type,
 					   context.offset, nodes, &inserted,
@@ -3498,13 +3520,16 @@  dump_targets (FILE *f, vec <cgraph_node *> targets, bool verbose)
   fprintf (f, "\n");
 }
 
-/* Dump all possible targets of a polymorphic call.  */
+/* Dump all possible targets of a polymorphic call.  If COMPLETE_HIERARCHY_P us
+   set, assume that the entire hierarchy of all classes that will ever be part
+   of the program is known at this point.  */
 
 void
 dump_possible_polymorphic_call_targets (FILE *f,
 					tree otr_type,
 					HOST_WIDE_INT otr_token,
 					const ipa_polymorphic_call_context &ctx,
+					bool complete_hierarchy_p,
 					bool verbose)
 {
   vec <cgraph_node *> targets;
@@ -3515,7 +3540,7 @@  dump_possible_polymorphic_call_targets (FILE *f,
   if (!type)
     return;
   targets = possible_polymorphic_call_targets (otr_type, otr_token,
-					       ctx,
+					       ctx, complete_hierarchy_p,
 					       &final, NULL, false);
   fprintf (f, "  Targets of polymorphic call of type %i:", type->id);
   print_generic_expr (f, type->type, TDF_SLIM);
@@ -3533,7 +3558,7 @@  dump_possible_polymorphic_call_targets (FILE *f,
   dump_targets (f, targets, verbose);
 
   targets = possible_polymorphic_call_targets (otr_type, otr_token,
-					       ctx,
+					       ctx, complete_hierarchy_p,
 					       &final, NULL, true);
   if (targets.length () != len)
     {
@@ -3575,7 +3600,8 @@  possible_polymorphic_call_target_p (tree otr_type,
 
   if (!odr_hash)
     return true;
-  targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, &final);
+  targets = possible_polymorphic_call_targets (otr_type, otr_token, ctx, false,
+					       &final);
   for (i = 0; i < targets.length (); i++)
     if (n->semantically_equivalent_p (targets[i]))
       return true;
@@ -3693,15 +3719,19 @@  decl_warning_cmp (const void *p1, const void *p2)
 
 
 /* Try to speculatively devirtualize call to OTR_TYPE with OTR_TOKEN with
-   context CTX.  */
+   context CTX.  If COMPLETE_HIERARCHY_P us set, assume that the entire
+   hierarchy of all classes that will ever be part of the program is known at
+   this point.  */
 
 struct cgraph_node *
 try_speculative_devirtualization (tree otr_type, HOST_WIDE_INT otr_token,
-				  ipa_polymorphic_call_context ctx)
+				  ipa_polymorphic_call_context ctx,
+				  bool complete_hierarchy_p)
 {
   vec <cgraph_node *>targets
-     = possible_polymorphic_call_targets
-	  (otr_type, otr_token, ctx, NULL, NULL, true);
+     = possible_polymorphic_call_targets (otr_type, otr_token, ctx,
+					  complete_hierarchy_p,
+					  NULL, NULL, true);
   unsigned int i;
   struct cgraph_node *likely_target = NULL;
 
@@ -3844,18 +3874,22 @@  ipa_devirt (void)
 	    if (final_warning_records)
 	      final_warning_records->dyn_count = e->count.ipa ();
 
+	    bool complete_hierarchy_p
+	      = ((flag_wpa || flag_whole_program)
+		 && opt_for_fn (n->decl, flag_complete_class_hierarchy));
 	    vec <cgraph_node *>targets
-	       = possible_polymorphic_call_targets
-		    (e, &final, &cache_token, true);
+	      = possible_polymorphic_call_targets (e, complete_hierarchy_p,
+						   &final, &cache_token, true);
 	    unsigned int i;
 
 	    /* Trigger warnings by calculating non-speculative targets.  */
 	    if (warn_suggest_final_methods || warn_suggest_final_types)
-	      possible_polymorphic_call_targets (e);
+	      possible_polymorphic_call_targets (e, false);
 
 	    if (dump_file)
 	      dump_possible_polymorphic_call_targets
-		(dump_file, e, (dump_flags & TDF_DETAILS));
+		(dump_file, e, complete_hierarchy_p,
+		 (dump_flags & TDF_DETAILS));
 
 	    stats.npolymorphic++;
 
diff --git a/gcc/ipa-prop.cc b/gcc/ipa-prop.cc
index 473239db36d..25cc6169c5a 100644
--- a/gcc/ipa-prop.cc
+++ b/gcc/ipa-prop.cc
@@ -4547,8 +4547,12 @@  try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
     ctx.possible_dynamic_type_change (ie->in_polymorphic_cdtor,
 				      pii->otr_type);
   ctx.combine_with (ie_context, pii->otr_type);
+  bool complete_hierarchy_p
+    = ((flag_wpa || flag_whole_program)
+       && opt_for_fn (ie->caller->decl, flag_complete_class_hierarchy));
   targets = possible_polymorphic_call_targets (pii->otr_type, pii->otr_token,
-					       ctx, &final);
+					       ctx, complete_hierarchy_p,
+					       &final);
   if (final && targets.length () <= 1)
     {
       speculative = false;
@@ -4563,7 +4567,7 @@  try_make_edge_direct_virtual_call (struct cgraph_edge *ie,
     {
       cgraph_node *n;
       n = try_speculative_devirtualization (pii->otr_type, pii->otr_token,
-					    pii->context);
+					    pii->context, complete_hierarchy_p);
       if (n)
 	{
 	  target = n->decl;
diff --git a/gcc/ipa-utils.h b/gcc/ipa-utils.h
index d689d45c70c..90143688388 100644
--- a/gcc/ipa-utils.h
+++ b/gcc/ipa-utils.h
@@ -76,6 +76,7 @@  void update_type_inheritance_graph (void);
 vec <cgraph_node *>
 possible_polymorphic_call_targets (tree, HOST_WIDE_INT,
 				   ipa_polymorphic_call_context,
+				   bool complete_hierarchy_p,
 				   bool *copletep = NULL,
 				   void **cache_token = NULL,
 				   bool speuclative = false);
@@ -84,6 +85,7 @@  bool odr_type_p (const_tree);
 bool possible_polymorphic_call_target_p (tree ref, gimple *stmt, struct cgraph_node *n);
 void dump_possible_polymorphic_call_targets (FILE *, tree, HOST_WIDE_INT,
 					     const ipa_polymorphic_call_context &,
+					     bool complete_hierarchy_p,
 					     bool verbose = true);
 bool possible_polymorphic_call_target_p (tree, HOST_WIDE_INT,
 				         const ipa_polymorphic_call_context &,
@@ -102,7 +104,8 @@  void register_odr_type (tree);
 bool types_must_be_same_for_odr (tree, tree);
 bool types_odr_comparable (tree, tree);
 cgraph_node *try_speculative_devirtualization (tree, HOST_WIDE_INT,
-					       ipa_polymorphic_call_context);
+					       ipa_polymorphic_call_context,
+					       bool);
 void warn_types_mismatch (tree t1, tree t2, location_t loc1 = UNKNOWN_LOCATION,
 			  location_t loc2 = UNKNOWN_LOCATION);
 bool odr_or_derived_type_p (const_tree t);
@@ -128,6 +131,7 @@  void register_odr_enum (tree type);
 
 inline vec <cgraph_node *>
 possible_polymorphic_call_targets (struct cgraph_edge *e,
+				   bool complete_hierarchy_p,
 				   bool *completep = NULL,
 				   void **cache_token = NULL,
 				   bool speculative = false)
@@ -140,6 +144,7 @@  possible_polymorphic_call_targets (struct cgraph_edge *e,
   return possible_polymorphic_call_targets (pii->otr_type,
 					    pii->otr_token,
 					    context,
+					    complete_hierarchy_p,
 					    completep, cache_token,
 					    speculative);
 }
@@ -158,13 +163,17 @@  possible_polymorphic_call_targets (tree ref,
 					    tree_to_uhwi
 					      (OBJ_TYPE_REF_TOKEN (ref)),
 					    context,
+					    false,
 					    completep, cache_token);
 }
 
-/* Dump possible targets of a polymorphic call E into F.  */
+/* Dump possible targets of a polymorphic call E into F.  If
+   COMPLETE_HIERARCHY_P us set, assume that the entire hierarchy of all classes
+   that will ever be part of the program is known at this point.  */
 
 inline void
 dump_possible_polymorphic_call_targets (FILE *f, struct cgraph_edge *e,
+					bool complete_hierarchy_p,
 					bool verbose = true)
 {
   ipa_polymorphic_call_context context(e);
@@ -174,8 +183,8 @@  dump_possible_polymorphic_call_targets (FILE *f, struct cgraph_edge *e,
   if (!pii->usable_p ())
     return;
   dump_possible_polymorphic_call_targets (f, pii->otr_type,
-					  pii->otr_token,
-					  context, verbose);
+					  pii->otr_token, context,
+					  complete_hierarchy_p, verbose);
 }
 
 /* Return true if N can be possibly target of a polymorphic call of
diff --git a/gcc/ipa.cc b/gcc/ipa.cc
index 0a5f031c639..850d25fb9b9 100644
--- a/gcc/ipa.cc
+++ b/gcc/ipa.cc
@@ -174,14 +174,15 @@  static void
 walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
 			       struct cgraph_edge *edge,
 			       symtab_node **first,
-			       hash_set<symtab_node *> *reachable)
+			       hash_set<symtab_node *> *reachable,
+			       bool complete_hierarchy_p)
 {
   unsigned int i;
   void *cache_token;
   bool final;
   vec <cgraph_node *>targets
-    = possible_polymorphic_call_targets
-	(edge, &final, &cache_token);
+    = possible_polymorphic_call_targets (edge, complete_hierarchy_p, &final,
+					 &cache_token);
 
   if (cache_token != NULL && !reachable_call_targets->add (cache_token))
     {
@@ -430,13 +431,18 @@  symbol_table::remove_unreachable_nodes (FILE *file)
 	      if (opt_for_fn (cnode->decl, optimize)
 		  && opt_for_fn (cnode->decl, flag_devirtualize))
 		{
+		  bool complete_hierarchy_p
+		    = ((flag_wpa || flag_whole_program)
+		       && opt_for_fn (cnode->decl,
+				      flag_complete_class_hierarchy));
 		  struct cgraph_edge *next;
 		  for (e = cnode->indirect_calls; e; e = next)
 		    {
 		      next = e->next_callee;
 		      if (usable_polymorphic_info_p (e->indirect_info))
 			walk_polymorphic_call_targets (&reachable_call_targets,
-						       e, &first, &reachable);
+						       e, &first, &reachable,
+						       complete_hierarchy_p);
 		    }
 		}
 
diff --git a/gcc/lto-cgraph.cc b/gcc/lto-cgraph.cc
index b3644e47bec..e4bcb273fe2 100644
--- a/gcc/lto-cgraph.cc
+++ b/gcc/lto-cgraph.cc
@@ -972,8 +972,8 @@  compute_ltrans_boundary (lto_symtab_encoder_t in_encoder)
 	      void *cache_token;
 	      bool final;
 	      vec <cgraph_node *>targets
-		= possible_polymorphic_call_targets
-		    (edge, &final, &cache_token);
+		= possible_polymorphic_call_targets (edge, false, &final,
+						     &cache_token);
 	      if (cache_token != NULL
 		  && !reachable_call_targets.add (cache_token))
 		{
diff --git a/gcc/testsuite/g++.dg/ipa/whole-hier-1.C b/gcc/testsuite/g++.dg/ipa/whole-hier-1.C
new file mode 100644
index 00000000000..ae771a80220
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ipa/whole-hier-1.C
@@ -0,0 +1,50 @@ 
+// { dg-do compile { target c++11 } }
+// { dg-options "-O2 -fdump-tree-optimized -fwhole-program -fcomplete-class-hierarchy" }
+
+struct Base {
+   virtual void foo();
+};
+
+/* Should have been final, but is not.  */
+struct Desc : Base {
+  void foo () override;
+};
+
+volatile int vr;
+
+Desc *gdp;
+
+void Base::foo ()
+{
+  vr = 1;
+}
+
+void Desc::foo ()
+{
+  vr = 2;
+}
+
+void call [[gnu::noinline]] (Desc *d)
+{
+  d->foo();
+}
+
+void intermediary [[gnu::noipa]] ()
+{
+  call (gdp);
+}
+
+
+int main (int argc, char *argv[])
+{
+  gdp = new Desc;
+  intermediary ();
+
+  if (vr != 2)
+    __builtin_abort ();
+
+  delete gdp;
+  return 0;
+}
+
+// { dg-final { scan-tree-dump-not "OBJ_TYPE_REF" "optimized" } }
diff --git a/gcc/testsuite/g++.dg/lto/whole-hier-1_0.C b/gcc/testsuite/g++.dg/lto/whole-hier-1_0.C
new file mode 100644
index 00000000000..ae1ff197796
--- /dev/null
+++ b/gcc/testsuite/g++.dg/lto/whole-hier-1_0.C
@@ -0,0 +1,51 @@ 
+/* { dg-lto-do link } */
+/* { dg-lto-options {{ -O2 -flto -std=c++17 -fcomplete-class-hierarchy -fdump-ipa-inline }} } */
+
+struct Base {
+   virtual void foo();
+};
+
+/* Should have been final, but is not.  */
+struct Desc : Base {
+  void foo () override;
+};
+
+volatile int vr;
+
+Desc *gdp;
+
+void Base::foo ()
+{
+  vr = 1;
+}
+
+void Desc::foo ()
+{
+  vr = 2;
+}
+
+void call [[gnu::noinline]] (Desc *d)
+{
+  d->foo();
+}
+
+void intermediary [[gnu::noipa]] ()
+{
+  call (gdp);
+}
+
+
+int main (int argc, char *argv[])
+{
+  gdp = new Desc;
+  intermediary ();
+
+  if (vr != 2)
+    __builtin_abort ();
+
+  delete gdp;
+  return 0;
+}
+
+
+// { dg-final { scan-wpa-ipa-dump-not "indirect polymorphic callsite" "inline" } }
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index 49557bb637a..8eadf7c5e7e 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -7717,11 +7717,12 @@  eliminate_dom_walker::eliminate_stmt (basic_block b, gimple_stmt_iterator *gsi)
 	  bool final;
 	  vec <cgraph_node *> targets
 	      = possible_polymorphic_call_targets (obj_type_ref_class (fn),
-						   otr_tok, context, &final);
+						   otr_tok, context, false,
+						   &final);
 	  if (dump_file)
 	    dump_possible_polymorphic_call_targets (dump_file,
 						    obj_type_ref_class (fn),
-						    otr_tok, context);
+						    otr_tok, context, false);
 	  if (final && targets.length () <= 1 && dbg_cnt (devirt))
 	    {
 	      tree fn;