@@ -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.
@@ -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++)
{
@@ -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
@@ -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;
@@ -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++;
@@ -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;
@@ -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
@@ -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);
}
}
@@ -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))
{
new file mode 100644
@@ -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" } }
new file mode 100644
@@ -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" } }
@@ -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;