https://gcc.gnu.org/g:3b99fd4bdc7005333b0c760630f5db6ceeb81e5a
commit r17-4413-g3b99fd4bdc7005333b0c760630f5db6ceeb81e5a Author: Alfie Richards <[email protected]> Date: Wed Sep 16 11:26:54 2026 +0000 machmode: Add parse_machine_mode function Use function to replace manual machine_mode parsing. gcc/ChangeLog: * machmode.h (parse_machine_mode): New function. * read-rtl.cc (find_mode): Use parse_machine_mode. * config/riscv/riscv-v.cc (autovectorize_vector_modes): Likewise. gcc/c-family/ChangeLog: * c-attribs.cc (handle_mode_attribute): Use parse_machine_mode. Diff: --- gcc/c-family/c-attribs.cc | 8 +------- gcc/config/riscv/riscv-v.cc | 10 ++++------ gcc/machmode.h | 15 +++++++++++++++ gcc/read-rtl.cc | 10 ++++------ 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc index ae5c2743ce6b..3c0e780669b4 100644 --- a/gcc/c-family/c-attribs.cc +++ b/gcc/c-family/c-attribs.cc @@ -2429,7 +2429,6 @@ handle_mode_attribute (tree *node, tree name, tree args, warning (OPT_Wattributes, "%qE attribute ignored", name); else { - int j; const char *p = IDENTIFIER_POINTER (ident); int len = strlen (p); machine_mode mode = VOIDmode; @@ -2461,12 +2460,7 @@ handle_mode_attribute (tree *node, tree name, tree args, else if (!strcmp (p, "unwind_word")) mode = targetm.unwind_word_mode (); else - for (j = 0; j < NUM_MACHINE_MODES; j++) - if (!strcmp (p, GET_MODE_NAME (j))) - { - mode = (machine_mode) j; - break; - } + parse_machine_mode (p, &mode); if (mode == VOIDmode) { diff --git a/gcc/config/riscv/riscv-v.cc b/gcc/config/riscv/riscv-v.cc index 398a1003a1d1..1cf53c7841c9 100644 --- a/gcc/config/riscv/riscv-v.cc +++ b/gcc/config/riscv/riscv-v.cc @@ -3068,13 +3068,11 @@ autovectorize_vector_modes (vector_modes *modes, bool) ms.safe_splice (*modes); modes->truncate (0); - for (machine_mode mode : ms) + machine_mode mode; + if (parse_machine_mode (riscv_autovec_mode, &mode)) { - if (!strcmp (GET_MODE_NAME (mode), riscv_autovec_mode)) - { - modes->safe_push (mode); - return 0; - } + modes->safe_push (mode); + return 0; } /* Nothing found, fall back to regular handling. */ diff --git a/gcc/machmode.h b/gcc/machmode.h index b92b1572d8d4..bc564d6bc132 100644 --- a/gcc/machmode.h +++ b/gcc/machmode.h @@ -926,6 +926,21 @@ smallest_int_mode_for_size (poly_uint64 size) return dyn_cast <scalar_int_mode> (smallest_mode_for_size (size, MODE_INT)); } +/* Parses a machine mode from NAME. If successful the machine mode is stored + into RESULT and true is returned. Otherwise false is returned. */ + +inline bool +parse_machine_mode (const char *name, machine_mode *result) +{ + for (unsigned i = 0; i < NUM_MACHINE_MODES; i++) + if (strcmp (GET_MODE_NAME (i), name) == 0) + { + *result = (machine_mode) i; + return true; + } + return false; +} + extern opt_scalar_int_mode int_mode_for_mode (machine_mode); extern opt_machine_mode bitwise_mode_for_mode (machine_mode); extern opt_machine_mode mode_for_vector (scalar_mode, poly_uint64); diff --git a/gcc/read-rtl.cc b/gcc/read-rtl.cc index be9f2da37e19..de95d859f785 100644 --- a/gcc/read-rtl.cc +++ b/gcc/read-rtl.cc @@ -197,13 +197,11 @@ atoll (const char *p) static HOST_WIDE_INT find_mode (const char *name) { - int i; - - for (i = 0; i < NUM_MACHINE_MODES; i++) - if (strcmp (GET_MODE_NAME (i), name) == 0) - return i; + machine_mode mode; + if (!parse_machine_mode (name, &mode)) + fatal_with_file_and_line ("unknown mode `%s'", name); - fatal_with_file_and_line ("unknown mode `%s'", name); + return (HOST_WIDE_INT) mode; } static void
