Hi, this patch adds API for varpool node removal/insertion hooks that are fully symmetric to cgraph code.
Bootstrapped/regtested x86_64-linux after working around PR58340, will commit it shortly. Honza * cgraph.h (varpool_node_hook, varpool_node_hook_list, varpool_add_node_removal_hook, varpool_add_variable_insertion_hook, varpool_remove_variable_insertion_hook): Declare. * varpool.c (varpool_node_hook_list): New structure. (first_varpool_node_removal_hook, first_varpool_variable_insertion_hook): New variables. (varpool_add_node_removal_hook, varpool_remove_node_removal_hook, varpool_call_node_removal_hooks, varpool_add_variable_insertion_hook, varpool_remove_variable_insertion_hook, varpool_call_variable_insertion_hooks): New functions. (varpool_remove_node): Use it. Index: cgraph.h =================================================================== --- cgraph.h (revision 202364) +++ cgraph.h (working copy) @@ -701,12 +701,14 @@ void cgraph_mark_address_taken_node (str typedef void (*cgraph_edge_hook)(struct cgraph_edge *, void *); typedef void (*cgraph_node_hook)(struct cgraph_node *, void *); +typedef void (*varpool_node_hook)(struct varpool_node *, void *); typedef void (*cgraph_2edge_hook)(struct cgraph_edge *, struct cgraph_edge *, void *); typedef void (*cgraph_2node_hook)(struct cgraph_node *, struct cgraph_node *, void *); struct cgraph_edge_hook_list; struct cgraph_node_hook_list; +struct varpool_node_hook_list; struct cgraph_2edge_hook_list; struct cgraph_2node_hook_list; struct cgraph_edge_hook_list *cgraph_add_edge_removal_hook (cgraph_edge_hook, void *); @@ -714,9 +716,15 @@ void cgraph_remove_edge_removal_hook (st struct cgraph_node_hook_list *cgraph_add_node_removal_hook (cgraph_node_hook, void *); void cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *); +struct varpool_node_hook_list *varpool_add_node_removal_hook (varpool_node_hook, + void *); +void varpool_remove_node_removal_hook (struct varpool_node_hook_list *); struct cgraph_node_hook_list *cgraph_add_function_insertion_hook (cgraph_node_hook, void *); void cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *); +struct varpool_node_hook_list *varpool_add_variable_insertion_hook (varpool_node_hook, + void *); +void varpool_remove_variable_insertion_hook (struct varpool_node_hook_list *); void cgraph_call_function_insertion_hooks (struct cgraph_node *node); struct cgraph_2edge_hook_list *cgraph_add_edge_duplication_hook (cgraph_2edge_hook, void *); void cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *); Index: varpool.c =================================================================== --- varpool.c (revision 202364) +++ varpool.c (working copy) @@ -36,6 +36,100 @@ along with GCC; see the file COPYING3. #include "tree-flow.h" #include "flags.h" +/* List of hooks triggered on varpool_node events. */ +struct varpool_node_hook_list { + varpool_node_hook hook; + void *data; + struct varpool_node_hook_list *next; +}; + +/* List of hooks triggered when a node is removed. */ +struct varpool_node_hook_list *first_varpool_node_removal_hook; +/* List of hooks triggered when an variable is inserted. */ +struct varpool_node_hook_list *first_varpool_variable_insertion_hook; + +/* Register HOOK to be called with DATA on each removed node. */ +struct varpool_node_hook_list * +varpool_add_node_removal_hook (varpool_node_hook hook, void *data) +{ + struct varpool_node_hook_list *entry; + struct varpool_node_hook_list **ptr = &first_varpool_node_removal_hook; + + entry = (struct varpool_node_hook_list *) xmalloc (sizeof (*entry)); + entry->hook = hook; + entry->data = data; + entry->next = NULL; + while (*ptr) + ptr = &(*ptr)->next; + *ptr = entry; + return entry; +} + +/* Remove ENTRY from the list of hooks called on removing nodes. */ +void +varpool_remove_node_removal_hook (struct varpool_node_hook_list *entry) +{ + struct varpool_node_hook_list **ptr = &first_varpool_node_removal_hook; + + while (*ptr != entry) + ptr = &(*ptr)->next; + *ptr = entry->next; + free (entry); +} + +/* Call all node removal hooks. */ +static void +varpool_call_node_removal_hooks (struct varpool_node *node) +{ + struct varpool_node_hook_list *entry = first_varpool_node_removal_hook; + while (entry) + { + entry->hook (node, entry->data); + entry = entry->next; + } +} + +/* Register HOOK to be called with DATA on each inserted node. */ +struct varpool_node_hook_list * +varpool_add_variable_insertion_hook (varpool_node_hook hook, void *data) +{ + struct varpool_node_hook_list *entry; + struct varpool_node_hook_list **ptr = &first_varpool_variable_insertion_hook; + + entry = (struct varpool_node_hook_list *) xmalloc (sizeof (*entry)); + entry->hook = hook; + entry->data = data; + entry->next = NULL; + while (*ptr) + ptr = &(*ptr)->next; + *ptr = entry; + return entry; +} + +/* Remove ENTRY from the list of hooks called on inserted nodes. */ +void +varpool_remove_variable_insertion_hook (struct varpool_node_hook_list *entry) +{ + struct varpool_node_hook_list **ptr = &first_varpool_variable_insertion_hook; + + while (*ptr != entry) + ptr = &(*ptr)->next; + *ptr = entry->next; + free (entry); +} + +/* Call all node insertion hooks. */ +void +varpool_call_variable_insertion_hooks (struct varpool_node *node) +{ + struct varpool_node_hook_list *entry = first_varpool_variable_insertion_hook; + while (entry) + { + entry->hook (node, entry->data); + entry = entry->next; + } +} + /* Allocate new callgraph node and insert it into basic data structures. */ struct varpool_node * @@ -65,8 +159,9 @@ varpool_node_for_decl (tree decl) void varpool_remove_node (struct varpool_node *node) { - symtab_unregister_node ((symtab_node)node); tree init; + varpool_call_node_removal_hooks (node); + symtab_unregister_node ((symtab_node)node); /* Because we remove references from external functions before final compilation, we may end up removing useful constructors. @@ -246,6 +341,7 @@ varpool_add_new_variable (tree decl) struct varpool_node *node; varpool_finalize_decl (decl); node = varpool_node_for_decl (decl); + varpool_call_variable_insertion_hooks (node); if (varpool_externally_visible_p (node)) node->symbol.externally_visible = true; }