https://gcc.gnu.org/g:898f22d15805229a932fff7f22a0a8054e1b9b31

commit r15-7773-g898f22d15805229a932fff7f22a0a8054e1b9b31
Author: Jan Dubiec <j...@o2.pl>
Date:   Sat Mar 1 08:21:16 2025 -0700

    [PATCH] H8/300, libgcc: PR target/114222 For HImode call internal ffs() 
implementation instead of an external one
    
    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 <SRCROOT>/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.

Diff:
---
 gcc/config/h8300/h8300.cc    |  8 ++++++++
 libgcc/config/h8300/ffshi2.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 libgcc/config/h8300/t-h8300  |  1 +
 3 files changed, 51 insertions(+)

diff --git a/gcc/config/h8300/h8300.cc b/gcc/config/h8300/h8300.cc
index 02056d0ff19f..c5935f602e24 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.
diff --git a/libgcc/config/h8300/ffshi2.c b/libgcc/config/h8300/ffshi2.c
new file mode 100644
index 000000000000..f08e73f57916
--- /dev/null
+++ b/libgcc/config/h8300/ffshi2.c
@@ -0,0 +1,42 @@
+/* The implementation of __ffshi2.
+   Copyright (C) 2003-2025 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3, or (at your option)
+any later version.
+
+GCC is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+Under Section 7 of GPL version 3, you are granted additional
+permissions described in the GCC Runtime Library Exception, version
+3.1, as published by the Free Software Foundation.
+
+You should have received a copy of the GNU General Public License and
+a copy of the GCC Runtime Library Exception along with this program;
+see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+<http://www.gnu.org/licenses/>.  */
+
+int __ffshi2 (int x);
+
+int
+__ffshi2 (int x)
+{
+  int i;
+
+  if (!x)
+    return 0;
+
+  i = 0;
+  for (;;)
+  {
+    if (((1 << i++) & x) != 0)
+     return i;
+  }
+  return 0;
+}
diff --git a/libgcc/config/h8300/t-h8300 b/libgcc/config/h8300/t-h8300
index b6448523e344..414053fa52c5 100644
--- a/libgcc/config/h8300/t-h8300
+++ b/libgcc/config/h8300/t-h8300
@@ -5,6 +5,7 @@ LIB1ASMFUNCS = _cmpsi2 _ucmpsi2 _divhi3 _divsi3 _mulhi3 _mulsi3 
\
 LIB2ADD = \
        $(srcdir)/config/h8300/clzhi2.c \
        $(srcdir)/config/h8300/ctzhi2.c \
+       $(srcdir)/config/h8300/ffshi2.c \
        $(srcdir)/config/h8300/parityhi2.c \
        $(srcdir)/config/h8300/popcounthi2.c \
        $(srcdir)/config/h8300/fixunssfsi.c

Reply via email to