From 2dc9831fc1c17840efc629788027aeededa44d2f Mon Sep 17 00:00:00 2001 From: crowl Date: Wed, 31 Oct 2012 23:15:10 +0000 Subject: 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 (pointer) Tests whether the pointer actually points to a more derived TYPE. TYPE *as_a (pointer) Converts pointer to a TYPE*. TYPE *dyn_cast (pointer) Converts pointer to TYPE* if and only if "is_a 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 (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 (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 and is_a . 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 * is-a.h: New. (is_a (U*)): New. Test for is-a relationship. (as_a (U*)): New. Treat as a derived type. (dyn_cast (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 ::test (symtab_node_def *)): New. (is_a_helper ::test (symtab_node_def *)): New. (symtab_node_def::try_function): New. Change most calls to symtab_function_p with calls to dyn_cast (p). (symtab_node_def::try_variable): New. Change most calls to symtab_variable_p with calls to dyn_cast (p). (symtab_function_p): Remove. Change callers to use is_a (p) instead. (symtab_variable_p): Remove. Change callers to use is_a (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. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@193051 138bc75d-0d04-0410-961f-82ee72b054a4 --- gcc/lto-cgraph.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'gcc/lto-cgraph.c') diff --git a/gcc/lto-cgraph.c b/gcc/lto-cgraph.c index b23f192e3bd..e8f171824a1 100644 --- a/gcc/lto-cgraph.c +++ b/gcc/lto-cgraph.c @@ -679,7 +679,7 @@ add_references (lto_symtab_encoder_t encoder, int i; struct ipa_ref *ref; for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++) - if (symtab_function_p (ref->referred)) + if (is_a (ref->referred)) add_node_to (encoder, ipa_ref_node (ref), false); else lto_symtab_encoder_encode (encoder, ref->referred); @@ -730,9 +730,8 @@ compute_ltrans_boundary (lto_symtab_encoder_t in_encoder) for (i = 0; i < lto_symtab_encoder_size (encoder); i++) { symtab_node node = lto_symtab_encoder_deref (encoder, i); - if (symtab_variable_p (node)) + if (varpool_node *vnode = dyn_cast (node)) { - struct varpool_node *vnode = varpool (node); if (DECL_INITIAL (vnode->symbol.decl) && !lto_symtab_encoder_encode_initializer_p (encoder, vnode) @@ -796,8 +795,8 @@ output_symtab (void) for (i = 0; i < n_nodes; i++) { symtab_node node = lto_symtab_encoder_deref (encoder, i); - if (symtab_function_p (node)) - lto_output_node (ob, cgraph (node), encoder); + if (cgraph_node *cnode = dyn_cast (node)) + lto_output_node (ob, cnode, encoder); else lto_output_varpool_node (ob, varpool (node), encoder); @@ -983,7 +982,7 @@ input_varpool_node (struct lto_file_decl_data *file_data, order = streamer_read_hwi (ib) + order_base; decl_index = streamer_read_uhwi (ib); var_decl = lto_file_decl_data_get_var_decl (file_data, decl_index); - node = varpool_node (var_decl); + node = varpool_node_for_decl (var_decl); node->symbol.order = order; if (order >= symtab_order) symtab_order = order + 1; @@ -1144,14 +1143,14 @@ input_cgraph_1 (struct lto_file_decl_data *file_data, /* AUX pointers should be all non-zero for function nodes read from the stream. */ #ifdef ENABLE_CHECKING FOR_EACH_VEC_ELT (symtab_node, nodes, i, node) - gcc_assert (node->symbol.aux || !symtab_function_p (node)); + gcc_assert (node->symbol.aux || !is_a (node)); #endif FOR_EACH_VEC_ELT (symtab_node, nodes, i, node) { int ref; - if (symtab_function_p (node)) + if (cgraph_node *cnode = dyn_cast (node)) { - ref = (int) (intptr_t) cgraph (node)->global.inlined_to; + ref = (int) (intptr_t) cnode->global.inlined_to; /* We share declaration of builtins, so we may read same node twice. */ if (!node->symbol.aux) @@ -1160,9 +1159,9 @@ input_cgraph_1 (struct lto_file_decl_data *file_data, /* Fixup inlined_to from reference to pointer. */ if (ref != LCC_NOT_FOUND) - cgraph (node)->global.inlined_to = cgraph (VEC_index (symtab_node, nodes, ref)); + cnode->global.inlined_to = cgraph (VEC_index (symtab_node, nodes, ref)); else - cgraph (node)->global.inlined_to = NULL; + cnode->global.inlined_to = NULL; } ref = (int) (intptr_t) node->symbol.same_comdat_group; @@ -1174,7 +1173,7 @@ input_cgraph_1 (struct lto_file_decl_data *file_data, node->symbol.same_comdat_group = NULL; } FOR_EACH_VEC_ELT (symtab_node, nodes, i, node) - node->symbol.aux = symtab_function_p (node) ? (void *)1 : NULL; + node->symbol.aux = is_a (node) ? (void *)1 : NULL; return nodes; } @@ -1449,7 +1448,6 @@ output_node_opt_summary (struct output_block *ob, static void output_cgraph_opt_summary (void) { - symtab_node node; int i, n_nodes; lto_symtab_encoder_t encoder; struct output_block *ob = create_output_block (LTO_section_cgraph_opt_sum); @@ -1459,18 +1457,21 @@ output_cgraph_opt_summary (void) encoder = ob->decl_state->symtab_node_encoder; n_nodes = lto_symtab_encoder_size (encoder); for (i = 0; i < n_nodes; i++) - if (symtab_function_p (node = lto_symtab_encoder_deref (encoder, i)) - && output_cgraph_opt_summary_p (cgraph (node))) - count++; + { + symtab_node node = lto_symtab_encoder_deref (encoder, i); + cgraph_node *cnode = dyn_cast (node); + if (cnode && output_cgraph_opt_summary_p (cnode)) + count++; + } streamer_write_uhwi (ob, count); for (i = 0; i < n_nodes; i++) { - node = lto_symtab_encoder_deref (encoder, i); - if (symtab_function_p (node) - && output_cgraph_opt_summary_p (cgraph (node))) + symtab_node node = lto_symtab_encoder_deref (encoder, i); + cgraph_node *cnode = dyn_cast (node); + if (cnode && output_cgraph_opt_summary_p (cnode)) { streamer_write_uhwi (ob, i); - output_node_opt_summary (ob, cgraph (node), encoder); + output_node_opt_summary (ob, cnode, encoder); } } produce_asm (ob, NULL); -- cgit v1.2.3