[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.
This commit is contained in:
Jan Dubiec 2025-03-01 08:21:16 -07:00 committed by Jeff Law
parent dfdbad87ae
commit 898f22d158
3 changed files with 51 additions and 0 deletions

View 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");
}
/* Worker function for TARGET_FUNCTION_VALUE.

View file

@ -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;
}

View 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