diff options
author | Lawrence Crowl <crowl@google.com> | 2012-10-31 23:15:10 +0000 |
---|---|---|
committer | Lawrence Crowl <crowl@gcc.gnu.org> | 2012-10-31 23:15:10 +0000 |
commit | 5d59b5e18a878c2201471e529c40f15d49905fc8 (patch) | |
tree | 7209857044c64b9be2543946aeb701d483464efa /gcc/symtab.c | |
parent | 2a381a57f3061e171923a14083ac37d957c9c98d (diff) | |
download | gcc-5d59b5e18a878c2201471e529c40f15d49905fc8.zip gcc-5d59b5e18a878c2201471e529c40f15d49905fc8.tar.gz gcc-5d59b5e18a878c2201471e529c40f15d49905fc8.tar.bz2 |
This patch implements generic type query and conversion functions,
and applies them to the use of cgraph_node, varpool_node, and symtab_node.
The functions are:
bool is_a <TYPE> (pointer)
Tests whether the pointer actually points to a more derived TYPE.
TYPE *as_a <TYPE> (pointer)
Converts pointer to a TYPE*.
TYPE *dyn_cast <TYPE> (pointer)
Converts pointer to TYPE* if and only if "is_a <TYPE> pointer".
Otherwise, returns NULL.
This function is essentially a checked down cast.
These functions reduce compile time and increase type safety when treating a
generic item as a more specific item. In essence, the code change is from
if (symtab_function_p (node))
{
struct cgraph_node *cnode = cgraph (node);
....
}
to
if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
{
....
}
The necessary conditional test defines a variable that holds a known good
pointer to the specific item and avoids subsequent conversion calls and
the assertion checks that may come with them.
When, the property test is embedded within a larger condition, the variable
declaration gets pulled out of the condition. (This leaves some room for
using the variable inappropriately.)
if (symtab_variable_p (node)
&& varpool (node)->finalized)
varpool_analyze_node (varpool (node));
becomes
varpool_node *vnode = dyn_cast <varpool_node> (node);
if (vnode && vnode->finalized)
varpool_analyze_node (vnode);
Note that we have converted two sets of assertions in the calls to varpool
into safe and efficient use of a variable.
There are remaining calls to symtab_function_p and symtab_variable_p that
do not involve a pointer to a more specific type. These have been converted
to calls to a functions is_a <cgraph_node> and is_a <varpool_node>. The
original predicate functions have been removed.
The cgraph.h header defined both a struct and a function with the name
varpool_node. This name overloading can cause some unintuitive error messages
when, as is common in C++, one omits the struct keyword when using the type.
I have renamed the function to varpool_node_for_decl.
Tested on x86_64.
Index: gcc/ChangeLog
2012-10-31 Lawrence Crowl <crowl@google.com>
* is-a.h: New.
(is_a <T> (U*)): New. Test for is-a relationship.
(as_a <T> (U*)): New. Treat as a derived type.
(dyn_cast <T> (U*)): New. Conditionally cast based on is_a.
* cgraph.h (varpool_node): Rename to varpool_node_for_decl.
Adjust callers to match.
(is_a_helper <cgraph_node>::test (symtab_node_def *)): New.
(is_a_helper <varpool_node>::test (symtab_node_def *)): New.
(symtab_node_def::try_function): New. Change most calls to
symtab_function_p with calls to dyn_cast <cgraph_node> (p).
(symtab_node_def::try_variable): New. Change most calls to
symtab_variable_p with calls to dyn_cast <varpool_node> (p).
(symtab_function_p): Remove. Change callers to use
is_a <cgraph_node> (p) instead.
(symtab_variable_p): Remove. Change callers to use
is_a <varpool_node> (p) instead.
* cgraph.c (cgraph_node_for_asm): Remove redundant call to
symtab_node_for_asm.
* cgraphunit.c (symbol_finalized_and_needed): New.
(symbol_finalized): New.
(cgraph_analyze_functions): Split complicated conditionals out into
above new functions.
* Makefile.in (CGRAPH_H): Add is-a.h as used by cgraph.h.
From-SVN: r193051
Diffstat (limited to 'gcc/symtab.c')
-rw-r--r-- | gcc/symtab.c | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/gcc/symtab.c b/gcc/symtab.c index c21edcb..39753e2 100644 --- a/gcc/symtab.c +++ b/gcc/symtab.c @@ -104,7 +104,7 @@ eq_assembler_name (const void *p1, const void *p2) static void insert_to_assembler_name_hash (symtab_node node) { - if (symtab_variable_p (node) && DECL_HARD_REGISTER (node->symbol.decl)) + if (is_a <varpool_node> (node) && DECL_HARD_REGISTER (node->symbol.decl)) return; gcc_checking_assert (!node->symbol.previous_sharing_asm_name && !node->symbol.next_sharing_asm_name); @@ -252,8 +252,8 @@ symtab_unregister_node (symtab_node node) if (*slot == node) { symtab_node replacement_node = NULL; - if (symtab_function_p (node)) - replacement_node = (symtab_node)cgraph_find_replacement_node (cgraph (node)); + if (cgraph_node *cnode = dyn_cast <cgraph_node> (node)) + replacement_node = (symtab_node)cgraph_find_replacement_node (cnode); if (!replacement_node) htab_clear_slot (symtab_hash, slot); else @@ -294,10 +294,10 @@ symtab_get_node (const_tree decl) void symtab_remove_node (symtab_node node) { - if (symtab_function_p (node)) - cgraph_remove_node (cgraph (node)); - else if (symtab_variable_p (node)) - varpool_remove_node (varpool (node)); + if (cgraph_node *cnode = dyn_cast <cgraph_node> (node)) + cgraph_remove_node (cnode); + else if (varpool_node *vnode = dyn_cast <varpool_node> (node)) + varpool_remove_node (vnode); } /* Initalize asm name hash unless. */ @@ -538,10 +538,10 @@ dump_symtab_base (FILE *f, symtab_node node) void dump_symtab_node (FILE *f, symtab_node node) { - if (symtab_function_p (node)) - dump_cgraph_node (f, cgraph (node)); - else if (symtab_variable_p (node)) - dump_varpool_node (f, varpool (node)); + if (cgraph_node *cnode = dyn_cast <cgraph_node> (node)) + dump_cgraph_node (f, cnode); + else if (varpool_node *vnode = dyn_cast <varpool_node> (node)) + dump_varpool_node (f, vnode); } /* Dump symbol table. */ @@ -579,7 +579,7 @@ verify_symtab_base (symtab_node node) bool error_found = false; symtab_node hashed_node; - if (symtab_function_p (node)) + if (is_a <cgraph_node> (node)) { if (TREE_CODE (node->symbol.decl) != FUNCTION_DECL) { @@ -587,7 +587,7 @@ verify_symtab_base (symtab_node node) error_found = true; } } - else if (symtab_variable_p (node)) + else if (is_a <varpool_node> (node)) { if (TREE_CODE (node->symbol.decl) != VAR_DECL) { @@ -622,7 +622,8 @@ verify_symtab_base (symtab_node node) hashed_node = hashed_node->symbol.next_sharing_asm_name; } if (!hashed_node - && !(symtab_variable_p (node) || DECL_HARD_REGISTER (node->symbol.decl))) + && !(is_a <varpool_node> (node) + || DECL_HARD_REGISTER (node->symbol.decl))) { error ("node not found in symtab assembler name hash"); error_found = true; @@ -676,8 +677,8 @@ verify_symtab_node (symtab_node node) return; timevar_push (TV_CGRAPH_VERIFY); - if (symtab_function_p (node)) - verify_cgraph_node (cgraph (node)); + if (cgraph_node *cnode = dyn_cast <cgraph_node> (node)) + verify_cgraph_node (cnode); else if (verify_symtab_base (node)) { |