]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[PATCH] H8/300, libgcc: PR target/114222 For HImode call internal ffs() implementatio...
authorJan Dubiec <jdx@o2.pl>
Sat, 1 Mar 2025 15:21:16 +0000 (08:21 -0700)
committerJeff Law <jlaw@ventanamicro.com>
Sat, 1 Mar 2025 15:21:16 +0000 (08:21 -0700)
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.

gcc/config/h8300/h8300.cc
libgcc/config/h8300/ffshi2.c [new file with mode: 0644]
libgcc/config/h8300/t-h8300

index 02056d0ff19f89b3f7ed504c558b5cd6eb69104f..c5935f602e243e7ba7cdeba29338490e0faefca0 100644 (file)
@@ -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");
 }
 \f
 /* Worker function for TARGET_FUNCTION_VALUE.
diff --git a/libgcc/config/h8300/ffshi2.c b/libgcc/config/h8300/ffshi2.c
new file mode 100644 (file)
index 0000000..f08e73f
--- /dev/null
@@ -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;
+}
index b6448523e344242199e33a2c821add795acb3cc3..414053fa52c5e68115b688600795f02a7ab3724e 100644 (file)
@@ -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