From 4e878a928ff7e9fe4eee0ea4b241c01c4440bd60 Mon Sep 17 00:00:00 2001
From: mliska <mliska@suse.cz>
Date: Mon, 16 Feb 2015 16:48:01 +0100
Subject: [PATCH] ipa-inline: introduce computed value that speeds up IPA
inliner.
---
gcc/cgraph.c | 77 -------------
gcc/cgraph.h | 309 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
gcc/ipa-inline.c | 2 +
gcc/lto-streamer.c | 2 +
gcc/symtab.c | 48 ++++++---
5 files changed, 345 insertions(+), 93 deletions(-)
@@ -2403,83 +2403,6 @@ cgraph_edge::maybe_hot_p (void)
return true;
}
-/* Worker for cgraph_can_remove_if_no_direct_calls_p. */
-
-static bool
-nonremovable_p (cgraph_node *node, void *)
-{
- return !node->can_remove_if_no_direct_calls_and_refs_p ();
-}
-
-/* Return true when function cgraph_node and its aliases can be removed from
- callgraph if all direct calls are eliminated. */
-
-bool
-cgraph_node::can_remove_if_no_direct_calls_p (void)
-{
- /* Extern inlines can always go, we will use the external definition. */
- if (DECL_EXTERNAL (decl))
- return true;
- if (address_taken)
- return false;
- return !call_for_symbol_and_aliases (nonremovable_p, NULL, true);
-}
-
-/* Return true when function cgraph_node can be expected to be removed
- from program when direct calls in this compilation unit are removed.
-
- As a special case COMDAT functions are
- cgraph_can_remove_if_no_direct_calls_p while the are not
- cgraph_only_called_directly_p (it is possible they are called from other
- unit)
-
- This function behaves as cgraph_only_called_directly_p because eliminating
- all uses of COMDAT function does not make it necessarily disappear from
- the program unless we are compiling whole program or we do LTO. In this
- case we know we win since dynamic linking will not really discard the
- linkonce section. */
-
-bool
-cgraph_node::will_be_removed_from_program_if_no_direct_calls_p (void)
-{
- gcc_assert (!global.inlined_to);
-
- if (call_for_symbol_and_aliases (used_from_object_file_p_worker,
- NULL, true))
- return false;
- if (!in_lto_p && !flag_whole_program)
- return only_called_directly_p ();
- else
- {
- if (DECL_EXTERNAL (decl))
- return true;
- return can_remove_if_no_direct_calls_p ();
- }
-}
-
-
-/* Worker for cgraph_only_called_directly_p. */
-
-static bool
-cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
-{
- return !node->only_called_directly_or_aliased_p ();
-}
-
-/* Return true when function cgraph_node and all its aliases are only called
- directly.
- i.e. it is not externally visible, address was not taken and
- it is not used in any other non-standard way. */
-
-bool
-cgraph_node::only_called_directly_p (void)
-{
- gcc_assert (ultimate_alias_target () == this);
- return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
- NULL, true);
-}
-
-
/* Collect all callers of NODE. Worker for collect_callers_of_node. */
static bool
@@ -261,17 +261,29 @@ public:
void *data,
bool include_overwrite);
+ /* Call callback on symtab node and aliases associated to this node.
+ When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
+ skipped. */
+ template <typename Arg, bool (*callback) (symtab_node*, Arg arg)>
+ bool call_for_symbol_and_aliases (Arg data, bool include_overwrite);
+
/* If node can not be interposable by static or dynamic linker to point to
different definition, return this symbol. Otherwise look for alias with
such property and if none exists, introduce new one. */
symtab_node *noninterposable_alias (void);
+ /* Worker searching noninterposable alias. */
+ static bool noninterposable_alias (symtab_node *node, symtab_node **data);
+
/* Return node that alias is aliasing. */
inline symtab_node *get_alias_target (void);
/* Set section for symbol and its aliases. */
void set_section (const char *section);
+ /* Worker for set_section. */
+ static bool set_section (symtab_node *n, const char *s);
+
/* Set section, do not recurse into aliases.
When one wants to change section of symbol and its aliases,
use set_section. */
@@ -523,6 +535,11 @@ protected:
bool call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *, void *),
void *data,
bool include_overwrite);
+
+ /* Worker for call_for_symbol_and_aliases. */
+ template <typename Arg, bool (*callback) (symtab_node *, Arg)>
+ bool call_for_symbol_and_aliases_1 (Arg data, bool include_overwritable);
+
private:
/* Worker for set_section. */
static bool set_section (symtab_node *n, void *s);
@@ -1042,6 +1059,13 @@ public:
void *),
void *data, bool include_overwritable);
+ /* Call callback on function and aliases associated to the function.
+ When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
+ skipped. */
+ template <typename Arg, bool (*callback) (cgraph_node *, Arg)>
+ bool call_for_symbol_and_aliases (Arg data, bool include_overwritable);
+
+
/* Call callback on cgraph_node, thunks and aliases associated to NODE.
When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
skipped. When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
@@ -1052,6 +1076,15 @@ public:
bool include_overwritable,
bool exclude_virtual_thunks = false);
+ /* Call callback on cgraph_node, thunks and aliases associated to NODE.
+ When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
+ skipped. When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
+ skipped. */
+ template <typename Arg, bool (*callback) (cgraph_node *, Arg)>
+ bool call_for_symbol_thunks_and_aliases (Arg data,
+ bool include_overwritable,
+ bool exclude_virtual_thunks = false);
+
/* Likewise indicate that a node is needed, i.e. reachable via some
external means. */
inline void mark_force_output (void);
@@ -1093,6 +1126,9 @@ public:
the program unless we are compiling whole program or we do LTO. In this
case we know we win since dynamic linking will not really discard the
linkonce section. */
+ bool will_be_removed_from_program_if_no_direct_calls_compute_p (void);
+
+ /* Wrapper for will_be_removed_from_program_if_no_direct_calls_compute_p. */
bool will_be_removed_from_program_if_no_direct_calls_p (void);
/* Return true when function can be removed from callgraph
@@ -1101,8 +1137,15 @@ public:
/* Return true when function cgraph_node and its aliases can be removed from
callgraph if all direct calls are eliminated. */
+ bool can_remove_if_no_direct_calls_compute_p (void);
+
+ /* Wrapper for can_remove_if_no_direct_calls_compute_p. */
bool can_remove_if_no_direct_calls_p (void);
+ /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
+ static bool nonremovable_p (cgraph_node *node, void *);
+ static bool nonremovable_compute_p (cgraph_node *node, void *);
+
/* Return true when callgraph node is a function with Gimple body defined
in current unit. Functions can also be define externally or they
can be thunks with no Gimple representation.
@@ -1295,11 +1338,24 @@ public:
/* True if there was multiple COMDAT bodies merged by lto-symtab. */
unsigned merged : 1;
+ /* IPA inline cached values. */
+ unsigned inline_nonremovable_init: 1;
+ unsigned inline_can_remove_if_no_direct_calls_init: 1;
+ unsigned inline_will_be_removed_if_no_direct_calls_init: 1;
+
+ unsigned inline_nonremovable: 1;
+ unsigned inline_can_remove_if_no_direct_calls: 1;
+ unsigned inline_will_be_removed_if_no_direct_calls: 1;
+
private:
/* Worker for call_for_symbol_and_aliases. */
bool call_for_symbol_and_aliases_1 (bool (*callback) (cgraph_node *,
void *),
void *data, bool include_overwritable);
+
+ /* Worker for call_for_symbol_and_aliases. */
+ template <typename Arg, bool (*callback) (cgraph_node *, Arg)>
+ bool call_for_symbol_and_aliases_1 (Arg data, bool include_overwritable);
};
/* A cgraph node set is a collection of cgraph nodes. A cgraph node
@@ -1683,6 +1739,12 @@ public:
void *data,
bool include_overwritable);
+ /* Call calback on varpool symbol and aliases associated to varpool symbol.
+ When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
+ skipped. */
+ template <typename Arg, bool (*callback) (varpool_node *, Arg)>
+ bool call_for_symbol_and_aliases (Arg data, bool include_overwritable);
+
/* Return true when variable should be considered externally visible. */
bool externally_visible_p (void);
@@ -1761,6 +1823,10 @@ private:
bool call_for_symbol_and_aliases_1 (bool (*callback) (varpool_node *, void *),
void *data,
bool include_overwritable);
+
+ /* Worker for call_for_symbol_and_aliases. */
+ template <typename Arg, bool (*callback) (varpool_node*, Arg arg)>
+ bool call_for_symbol_and_aliases_1 (Arg data, bool include_overwritable);
};
/* Every top level asm statement is put into a asm_node. */
@@ -1862,7 +1928,7 @@ public:
friend class cgraph_node;
friend class cgraph_edge;
- symbol_table (): cgraph_max_summary_uid (1)
+ symbol_table (): cgraph_max_summary_uid (1), enable_inline_cache (false)
{
}
@@ -2101,6 +2167,9 @@ public:
FILE* GTY ((skip)) dump_file;
+ /* Inline cache flag. */
+ bool enable_inline_cache;
+
private:
/* Allocate new callgraph node. */
inline cgraph_node * allocate_cgraph_symbol (void);
@@ -2987,6 +3056,21 @@ symtab_node::call_for_symbol_and_aliases (bool (*callback) (symtab_node *,
return false;
}
+template <typename Arg, bool (*callback) (symtab_node *, Arg arg)>
+inline bool
+symtab_node::call_for_symbol_and_aliases (Arg data, bool include_overwritable)
+{
+ ipa_ref *ref;
+
+ if (callback (this, data))
+ return true;
+ if (iterate_direct_aliases (0, ref))
+ return call_for_symbol_and_aliases_1 <Arg, callback>
+ (data, include_overwritable);
+ return false;
+}
+
+
/* Call callback on function and aliases associated to the function.
When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
skipped. */
@@ -3004,6 +3088,43 @@ cgraph_node::call_for_symbol_and_aliases (bool (*callback) (cgraph_node *,
return false;
}
+/* Call callback on function and aliases associated to the function.
+ When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
+ skipped. */
+
+template <typename Arg, bool (*callback) (cgraph_node *, Arg arg)>
+inline bool
+cgraph_node::call_for_symbol_and_aliases (Arg data, bool include_overwritable)
+{
+ ipa_ref *ref;
+
+ if (callback (this, data))
+ return true;
+
+ if (iterate_direct_aliases (0, ref))
+ return call_for_symbol_and_aliases_1 <Arg, callback> (data, include_overwritable);
+
+ return false;
+}
+
+template <typename Arg, bool (*callback) (cgraph_node *, Arg arg)>
+inline bool
+cgraph_node::call_for_symbol_and_aliases_1 (Arg data, bool include_overwritable)
+{
+ ipa_ref *ref;
+ FOR_EACH_ALIAS (this, ref)
+ {
+ cgraph_node *alias = dyn_cast <cgraph_node *> (ref->referring);
+ if (include_overwritable
+ || alias->get_availability () > AVAIL_INTERPOSABLE)
+ if (alias->call_for_symbol_and_aliases <Arg, callback> (data, include_overwritable))
+ return true;
+ }
+
+ return false;
+}
+
+
/* Call calback on varpool symbol and aliases associated to varpool symbol.
When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
skipped. */
@@ -3021,6 +3142,47 @@ varpool_node::call_for_symbol_and_aliases (bool (*callback) (varpool_node *,
return false;
}
+
+/* Call calback on varpool symbol and aliases associated to varpool symbol.
+ When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
+ skipped. */
+
+template <typename Arg, bool (*callback) (varpool_node*, Arg arg)>
+inline bool
+varpool_node::call_for_symbol_and_aliases (Arg data, bool include_overwritable)
+{
+ ipa_ref *ref;
+
+ if (callback (this, data))
+ return true;
+ if (iterate_direct_aliases (0, ref))
+ return call_for_symbol_and_aliases_1 <Arg, callback>
+ (data, include_overwritable);
+
+ return false;
+}
+
+/* Worker for call_for_symbol_and_aliases. */
+
+template <typename Arg, bool (*callback) (varpool_node*, Arg arg)>
+bool
+varpool_node::call_for_symbol_and_aliases_1 (Arg data,
+ bool include_overwritable)
+{
+ ipa_ref *ref;
+
+ FOR_EACH_ALIAS (this, ref)
+ {
+ varpool_node *alias = dyn_cast <varpool_node *> (ref->referring);
+ if (include_overwritable
+ || alias->get_availability () > AVAIL_INTERPOSABLE)
+ if (alias->call_for_symbol_and_aliases <Arg, callback>
+ (data, include_overwritable))
+ return true;
+ }
+ return false;
+}
+
/* Build polymorphic call context for indirect call E. */
inline
@@ -3094,6 +3256,151 @@ cgraph_local_p (cgraph_node *node)
return node->local.local && node->instrumented_version->local.local;
}
+inline bool
+cgraph_node::nonremovable_compute_p (cgraph_node *node, void *)
+{
+ return !node->can_remove_if_no_direct_calls_and_refs_p ();
+}
+
+inline bool
+cgraph_node::nonremovable_p (cgraph_node *node, void *)
+{
+ bool retval;
+
+ if (symtab->enable_inline_cache)
+ {
+ if (!node->inline_nonremovable_init)
+ {
+ node->inline_nonremovable = nonremovable_compute_p (node, NULL);
+ node->inline_nonremovable_init = true;
+ }
+
+ retval = node->inline_nonremovable;
+
+ gcc_checking_assert (retval == nonremovable_compute_p (node, NULL));
+ }
+ else
+ retval = nonremovable_compute_p (node, NULL);
+
+ return retval;
+}
+
+inline bool
+cgraph_node::can_remove_if_no_direct_calls_compute_p (void)
+{
+ if (DECL_EXTERNAL (decl))
+ return true;
+ if (address_taken)
+ return false;
+
+ return !call_for_symbol_and_aliases <void *, cgraph_node::nonremovable_compute_p>
+ (NULL, true);
+}
+
+/* Return true when function cgraph_node and its aliases can be removed from
+ callgraph if all direct calls are eliminated. */
+
+inline bool
+cgraph_node::can_remove_if_no_direct_calls_p (void)
+{
+ bool retval;
+
+ if (symtab->enable_inline_cache)
+ {
+ if (!inline_can_remove_if_no_direct_calls_init)
+ {
+ inline_can_remove_if_no_direct_calls = can_remove_if_no_direct_calls_compute_p ();
+ inline_can_remove_if_no_direct_calls_init = true;
+ }
+
+ retval = inline_can_remove_if_no_direct_calls;
+
+ gcc_checking_assert
+ (retval == can_remove_if_no_direct_calls_compute_p ());
+ }
+ else
+ retval = can_remove_if_no_direct_calls_compute_p ();
+
+ return retval;
+}
+
+/* Return true when function cgraph_node can be expected to be removed
+ from program when direct calls in this compilation unit are removed.
+
+ As a special case COMDAT functions are
+ cgraph_can_remove_if_no_direct_calls_p while the are not
+ cgraph_only_called_directly_p (it is possible they are called from other
+ unit)
+
+ This function behaves as cgraph_only_called_directly_p because eliminating
+ all uses of COMDAT function does not make it necessarily disappear from
+ the program unless we are compiling whole program or we do LTO. In this
+ case we know we win since dynamic linking will not really discard the
+ linkonce section. */
+
+inline bool
+cgraph_node::will_be_removed_from_program_if_no_direct_calls_compute_p (void)
+{
+ gcc_assert (!global.inlined_to);
+
+ if (call_for_symbol_and_aliases <void *, used_from_object_file_p_worker>
+ (NULL, true))
+ return false;
+ if (!in_lto_p && !flag_whole_program)
+ return only_called_directly_p ();
+ else
+ {
+ if (DECL_EXTERNAL (decl))
+ return true;
+ return can_remove_if_no_direct_calls_p ();
+ }
+}
+
+/* Wrapper for will_be_removed_from_program_if_no_direct_calls_computed_p. */
+
+inline bool
+cgraph_node::will_be_removed_from_program_if_no_direct_calls_p (void)
+{
+ if (symtab->enable_inline_cache)
+ {
+ if (!inline_will_be_removed_if_no_direct_calls_init)
+ {
+ inline_will_be_removed_if_no_direct_calls
+ = will_be_removed_from_program_if_no_direct_calls_compute_p ();
+
+ inline_will_be_removed_if_no_direct_calls_init = true;
+ }
+
+ gcc_checking_assert (inline_will_be_removed_if_no_direct_calls ==
+ will_be_removed_from_program_if_no_direct_calls_compute_p ());
+ return inline_will_be_removed_if_no_direct_calls;
+ }
+
+ return will_be_removed_from_program_if_no_direct_calls_compute_p ();
+}
+
+/* Worker for cgraph_only_called_directly_p. */
+
+static bool
+cgraph_not_only_called_directly_p_1 (cgraph_node *node, void *)
+{
+ return !node->only_called_directly_or_aliased_p ();
+}
+
+/* Return true when function cgraph_node and all its aliases are only called
+ directly.
+ i.e. it is not externally visible, address was not taken and
+ it is not used in any other non-standard way. */
+
+inline bool
+cgraph_node::only_called_directly_p (void)
+{
+ gcc_assert (ultimate_alias_target () == this);
+ return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1,
+ NULL, true);
+}
+
+
/* When using fprintf (or similar), problems can arise with
transient generated strings. Many string-generation APIs
only support one result being alive at once (e.g. by
@@ -1651,6 +1651,7 @@ inline_small_functions (void)
ipa_reduced_postorder (order, true, true, NULL);
free (order);
+ symtab->enable_inline_cache = true;
FOR_EACH_DEFINED_FUNCTION (node)
if (!node->global.inlined_to)
{
@@ -1966,6 +1967,7 @@ inline_small_functions (void)
}
}
+ symtab->enable_inline_cache = false;
free_growth_caches ();
if (dump_file)
fprintf (dump_file,
@@ -319,11 +319,13 @@ static hash_table<tree_hash_entry> *tree_htab;
void
lto_streamer_init (void)
{
+#ifdef ENABLE_CHECKING
/* Check that all the TS_* handled by the reader and writer routines
match exactly the structures defined in treestruct.def. When a
new TS_* astructure is added, the streamer should be updated to
handle it. */
streamer_check_handled_ts_structures ();
+#endif
#ifdef LTO_STREAMER_DEBUG
tree_htab = new hash_table<tree_hash_entry> (31);
@@ -1337,9 +1337,9 @@ symtab_node::set_section_for_node (const char *section)
/* Worker for set_section. */
bool
-symtab_node::set_section (symtab_node *n, void *s)
+symtab_node::set_section (symtab_node *n, const char *s)
{
- n->set_section_for_node ((char *)s);
+ n->set_section_for_node (s);
return false;
}
@@ -1349,8 +1349,7 @@ void
symtab_node::set_section (const char *section)
{
gcc_assert (!this->alias);
- call_for_symbol_and_aliases
- (symtab_node::set_section, const_cast<char *>(section), true);
+ call_for_symbol_and_aliases <const char *, symtab_node::set_section> (section, true);
}
/* Return the initialization priority. */
@@ -1491,10 +1490,11 @@ symtab_node::resolve_alias (symtab_node *target)
{
error ("section of alias %q+D must match section of its target", decl);
}
- call_for_symbol_and_aliases (symtab_node::set_section,
- const_cast<char *>(target->get_section ()), true);
+ call_for_symbol_and_aliases <const char *, symtab_node::set_section>
+ (const_cast<char *>(target->get_section ()), true);
if (target->implicit_section)
- call_for_symbol_and_aliases (set_implicit_section, NULL, true);
+ call_for_symbol_and_aliases <void *, symtab_node::set_implicit_section>
+ (NULL, true);
/* Alias targets become redundant after alias is resolved into an reference.
We do not want to keep it around or we would have to mind updating them
@@ -1513,7 +1513,7 @@ symtab_node::resolve_alias (symtab_node *target)
/* Worker searching noninterposable alias. */
bool
-symtab_node::noninterposable_alias (symtab_node *node, void *data)
+symtab_node::noninterposable_alias (symtab_node *node, symtab_node **data)
{
if (decl_binds_to_current_def_p (node->decl))
{
@@ -1530,7 +1530,7 @@ symtab_node::noninterposable_alias (symtab_node *node, void *data)
|| DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
return false;
- *(symtab_node **)data = node;
+ *data = node;
return true;
}
return false;
@@ -1550,8 +1550,8 @@ symtab_node::noninterposable_alias (void)
(if that is already non-overwritable). */
symtab_node *node = ultimate_alias_target ();
gcc_assert (!node->alias && !node->weakref);
- node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
- (void *)&new_node, true);
+ node->call_for_symbol_and_aliases
+ <symtab_node **, symtab_node::noninterposable_alias> (&new_node, true);
if (new_node)
return new_node;
#ifndef ASM_OUTPUT_DEF
@@ -1840,10 +1840,8 @@ symtab_node::equal_address_to (symtab_node *s2)
/* Worker for call_for_symbol_and_aliases. */
bool
-symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
- void *),
- void *data,
- bool include_overwritable)
+symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,void *),
+ void *data, bool include_overwritable)
{
ipa_ref *ref;
FOR_EACH_ALIAS (this, ref)
@@ -1857,3 +1855,23 @@ symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
}
return false;
}
+
+/* Worker for call_for_symbol_and_aliases. */
+
+template <typename Arg, bool (*callback) (symtab_node*, Arg arg)>
+bool
+symtab_node::call_for_symbol_and_aliases_1 (Arg data,
+ bool include_overwritable)
+{
+ ipa_ref *ref;
+ FOR_EACH_ALIAS (this, ref)
+ {
+ symtab_node *alias = ref->referring;
+ if (include_overwritable
+ || alias->get_availability () > AVAIL_INTERPOSABLE)
+ if (alias->call_for_symbol_and_aliases <Arg, callback> (data,
+ include_overwritable))
+ return true;
+ }
+ return false;
+}
--
2.1.2