From 898f22d15805229a932fff7f22a0a8054e1b9b31 Mon Sep 17 00:00:00 2001 From: Jan Dubiec Date: Sat, 1 Mar 2025 08:21:16 -0700 Subject: [PATCH] H8/300, libgcc: PR target/114222 For HImode call internal ffs() implementation instead of an external one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When INT_TYPE_SIZE < BITS_PER_WORD gcc emits a call to an external ffs() implementation instead of a call to "__builtin_ffs()" – see function init_optabs() in /gcc/optabs-libfuncs.cc. External ffs() (which is usually the one from newlib) in turn calls __builtin_ffs() what causes infinite recursion and stack overflow. This patch overrides default gcc bahaviour for H8/300H (and newer) and provides a generic ffs() implementation for HImode. PR target/114222 gcc/ChangeLog: * config/h8300/h8300.cc (h8300_init_libfuncs): For HImode override calls to external ffs() (from newlib) with calls to __ffshi2() from libgcc. The implementation of ffs() in newlib calls __builtin_ffs() what causes infinite recursion and finally a stack overflow. libgcc/ChangeLog: * config/h8300/t-h8300: Add __ffshi2(). * config/h8300/ffshi2.c: New file. --- gcc/config/h8300/h8300.cc | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'gcc') diff --git a/gcc/config/h8300/h8300.cc b/gcc/config/h8300/h8300.cc index 02056d0..c5935f6 100644 --- a/gcc/config/h8300/h8300.cc +++ b/gcc/config/h8300/h8300.cc @@ -5410,6 +5410,14 @@ h8300_init_libfuncs (void) set_optab_libfunc (udiv_optab, HImode, "__udivhi3"); set_optab_libfunc (smod_optab, HImode, "__modhi3"); set_optab_libfunc (umod_optab, HImode, "__umodhi3"); + +/* The comment below comes from config/mmix/mmix.cc. + + Unfortunately, by default __builtin_ffs is expanded to ffs for + targets where INT_TYPE_SIZE < BITS_PER_WORD. That together with + newlib since 2017-07-04 implementing ffs as __builtin_ffs leads to + (newlib) ffs recursively calling itself. */ + set_optab_libfunc (ffs_optab, HImode, "__ffshi2"); } /* Worker function for TARGET_FUNCTION_VALUE. -- cgit v1.1