https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129142
Bug ID: 129142
Summary: [14/15/16/17 Regression] xtensa: a + __builtin_ctz (x)
needs one more instruction since r14-1295
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: shingekinocore at gmail dot com
Target Milestone: ---
Target: xtensa-linux
Known to work: 8.1.0, 9.5.0, 10.5.0, 11.5.0, 12.5.0, 13.5.0
Known to fail: 14.1.0, 14.4.0, 15.3.0, 16.2.0, trunk (r17-6711)
unsigned f(unsigned a, unsigned x)
{
return a + __builtin_ctz(x);
}
xtensa-linux-gcc -O2 -S, GCC 13.5.0 (default core, which has NSA):
f:
entry sp, 32
neg a8, a3
and a8, a8, a3
nsau a8, a8
sub a2, a2, a8
addi a2, a2, 31
retw.n
GCC 16.2.0, same command:
f:
entry sp, 32
neg a8, a3
and a8, a8, a3
nsau a8, a8
addi a8, a8, -31
neg a8, a8
add.n a2, a8, a2
retw.n
GCC 14 and later need one instruction more than GCC 13.
a + __builtin_ffs(x) shows the same difference, and both expressions
show it at -O1, -Os and -O3 and with -O2 -mabi=call0 as well.
Started with r14-1295-g9b867c8281ee ("xtensa: Add 'subtraction
from constant' insn pattern"). A compiler built from its parent
r14-1294 gives the GCC 13.5.0 listing, a compiler built from r14-1295
the GCC 16.2.0 listing. Trunk r17-6711-g48688cfeadf6 (2026-11-26)
still gives the GCC 16.2.0 listing.
What -fdump-rtl-combine-details shows: ctzsi2 emits
t = clz (x & -x); r = 31; r = r - t, and s = r + a follows. Before
r14-1295, combine merges the last three insns into sub and addi.
Since r14-1295, r = 31; r = r - t are first merged into r = 31 - t,
which matches the new *subsi3_from_const pattern and becomes addi;
neg; the add of a then stays a separate instruction. Trunk includes
r17-5647-g80395d8d52da, which changed that pattern, and shows the
same.
Compilers: the releases are the prebuilt cross compilers from
https://mirrors.edge.kernel.org/pub/tools/crosstool/ (aarch64 host);
r14-1294, r14-1295 and trunk were configured with
--target=xtensa-linux --enable-languages=c.
On Compiler Explorer, Espressif's ESP32 GCC 12.2.0 gives the GCC
13.5.0 listing and its GCC 14.2.0 the GCC 16.2.0 listing:
https://godbolt.org/z/snKhz5M1P