https://gcc.gnu.org/g:fe440c99d6a96498d663c86c0298e1c97e24c410

commit r17-423-gfe440c99d6a96498d663c86c0298e1c97e24c410
Author: Andrew Pinski <[email protected]>
Date:   Fri May 8 18:32:50 2026 -0700

    cfghooks: constifify cfg_hooks [PR117871]
    
    This constifies the hooks so the structs can't be changed at runtime.
    The only odd place where we need to handle special is sel-sched.
    This is because we make a copy of the current cfghooks and then
    use its own.  This changes things slightly there, there is still a
    copy used but instead of copying back into the current cfghooks, we
    just change the pointer back to the original one. This code is only
    enabled by ia64 backend by default so I doubt it will change.
    
    Boostrapped and tested on x86_64-linux-gnu.
    
            PR middle-end/117871
    gcc/ChangeLog:
    
            * cfghooks.cc (cfg_hooks): Change the type
            to be a pointer to a const struct cfg_hooks.
            (get_cfg_hooks): Return the current pointer
            rather the struct.
            (set_cfg_hooks): Change the argument type and
            set the cfg_hooks directly to it.
            * cfghooks.h (gimple_cfg_hooks): Constify.
            (rtl_cfg_hooks): Likewise.
            (cfg_layout_rtl_cfg_hooks): Likewise.
            (get_cfg_hooks): Update declration.
            (set_cfg_hooks): Likewise.
            * cfgrtl.cc (rtl_cfg_hooks): Constify.
            (cfg_layout_rtl_cfg_hooks): Likewise.
            * sel-sched-ir.cc (orig_cfg_hooks): Change to a pointer.
            (sel_create_basic_block): Update
            for orig_cfg_hooks being a pointer.
            (sel_register_cfg_hooks): Update for the constification
            of cfg_hooks.
            * tree-cfg.cc (gimple_cfg_hooks): Constify.
    
    Signed-off-by: Andrew Pinski <[email protected]>

Diff:
---
 gcc/cfghooks.cc     | 10 +++++-----
 gcc/cfghooks.h      | 10 +++++-----
 gcc/cfgrtl.cc       |  4 ++--
 gcc/sel-sched-ir.cc |  8 ++++----
 gcc/tree-cfg.cc     |  2 +-
 5 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/gcc/cfghooks.cc b/gcc/cfghooks.cc
index 9c90c52d5d37..a219b4afce48 100644
--- a/gcc/cfghooks.cc
+++ b/gcc/cfghooks.cc
@@ -45,7 +45,7 @@ along with GCC; see the file COPYING3.  If not see
 #endif
 
 /* A pointer to one of the hooks containers.  */
-static struct cfg_hooks *cfg_hooks;
+static const struct cfg_hooks *cfg_hooks;
 
 /* Initialization of functions specific to the rtl IR.  */
 void
@@ -69,16 +69,16 @@ gimple_register_cfg_hooks (void)
   cfg_hooks = &gimple_cfg_hooks;
 }
 
-struct cfg_hooks
+const struct cfg_hooks *
 get_cfg_hooks (void)
 {
-  return *cfg_hooks;
+  return cfg_hooks;
 }
 
 void
-set_cfg_hooks (struct cfg_hooks new_cfg_hooks)
+set_cfg_hooks (const struct cfg_hooks *new_cfg_hooks)
 {
-  *cfg_hooks = new_cfg_hooks;
+  cfg_hooks = new_cfg_hooks;
 }
 
 /* Returns current ir type.  */
diff --git a/gcc/cfghooks.h b/gcc/cfghooks.h
index e9c1f30b16a4..aa7071ee013f 100644
--- a/gcc/cfghooks.h
+++ b/gcc/cfghooks.h
@@ -283,16 +283,16 @@ void profile_record_check_consistency (profile_record *);
 void profile_record_account_profile (profile_record *);
 
 /* Hooks containers.  */
-extern struct cfg_hooks gimple_cfg_hooks;
-extern struct cfg_hooks rtl_cfg_hooks;
-extern struct cfg_hooks cfg_layout_rtl_cfg_hooks;
+extern const struct cfg_hooks gimple_cfg_hooks;
+extern const struct cfg_hooks rtl_cfg_hooks;
+extern const struct cfg_hooks cfg_layout_rtl_cfg_hooks;
 
 /* Declarations.  */
 extern enum ir_type current_ir_type (void);
 extern void rtl_register_cfg_hooks (void);
 extern void cfg_layout_rtl_register_cfg_hooks (void);
 extern void gimple_register_cfg_hooks (void);
-extern struct cfg_hooks get_cfg_hooks (void);
-extern void set_cfg_hooks (struct cfg_hooks);
+extern const struct cfg_hooks *get_cfg_hooks (void);
+extern void set_cfg_hooks (const struct cfg_hooks *);
 
 #endif /* GCC_CFGHOOKS_H */
diff --git a/gcc/cfgrtl.cc b/gcc/cfgrtl.cc
index 3f6c9c67a8c4..7714e5548c2d 100644
--- a/gcc/cfgrtl.cc
+++ b/gcc/cfgrtl.cc
@@ -5384,7 +5384,7 @@ rtl_account_profile_record (basic_block bb, struct 
profile_record *record)
 }
 
 /* Implementation of CFG manipulation for linearized RTL.  */
-struct cfg_hooks rtl_cfg_hooks = {
+const struct cfg_hooks rtl_cfg_hooks = {
   IR_RTL_CFGRTL,
   rtl_verify_flow_info,
   rtl_dump_bb,
@@ -5427,7 +5427,7 @@ struct cfg_hooks rtl_cfg_hooks = {
    This representation will hopefully become the default one in future
    version of the compiler.  */
 
-struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
+const struct cfg_hooks cfg_layout_rtl_cfg_hooks = {
   IR_RTL_CFGLAYOUT,
   rtl_verify_flow_info_1,
   rtl_dump_bb,
diff --git a/gcc/sel-sched-ir.cc b/gcc/sel-sched-ir.cc
index 3e0ce5895f6a..866b2d9875e5 100644
--- a/gcc/sel-sched-ir.cc
+++ b/gcc/sel-sched-ir.cc
@@ -70,7 +70,7 @@ static vec<loop_p> loop_nests;
 static sbitmap bbs_in_loop_rgns = NULL;
 
 /* CFG hooks that are saved before changing create_basic_block hook.  */
-static struct cfg_hooks orig_cfg_hooks;
+static const struct cfg_hooks *orig_cfg_hooks;
 
 
 /* Array containing reverse topological index of function basic blocks,
@@ -5359,7 +5359,7 @@ sel_create_basic_block (void *headp, void *endp, 
basic_block after)
   new_bb_note = get_bb_note_from_pool ();
 
   if (new_bb_note == NULL_RTX)
-    new_bb = orig_cfg_hooks.create_basic_block (headp, endp, after);
+    new_bb = orig_cfg_hooks->create_basic_block (headp, endp, after);
   else
     {
       new_bb = create_basic_block_structure ((rtx_insn *) headp,
@@ -5709,11 +5709,11 @@ sel_register_cfg_hooks (void)
   sched_split_block = sel_split_block;
 
   orig_cfg_hooks = get_cfg_hooks ();
-  sel_cfg_hooks = orig_cfg_hooks;
+  sel_cfg_hooks = *orig_cfg_hooks;
 
   sel_cfg_hooks.create_basic_block = sel_create_basic_block;
 
-  set_cfg_hooks (sel_cfg_hooks);
+  set_cfg_hooks (&sel_cfg_hooks);
 
   sched_init_only_bb = sel_init_only_bb;
   sched_split_block = sel_split_block;
diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc
index b9c1edc30c56..b2968c412ed6 100644
--- a/gcc/tree-cfg.cc
+++ b/gcc/tree-cfg.cc
@@ -9359,7 +9359,7 @@ gimple_account_profile_record (basic_block bb,
     }
 }
 
-struct cfg_hooks gimple_cfg_hooks = {
+const struct cfg_hooks gimple_cfg_hooks = {
   IR_GIMPLE,
   gimple_verify_flow_info,
   gimple_dump_bb,              /* dump_bb  */

Reply via email to