]> git.ipfire.org Git - thirdparty/gcc.git/commit
arc: Add const attribute support for mathematical ARC builtins
authorKees Cook <kees@kernel.org>
Tue, 26 Aug 2025 04:09:41 +0000 (21:09 -0700)
committerClaudiu Zissulescu <claudiu.zissulescu-ianculescu@oracle.com>
Tue, 4 Nov 2025 15:27:58 +0000 (17:27 +0200)
commit7e7091bfa87dab24d36c50233ca04cb6319c5b91
treea1511384160961168f8b66e7cc6032942bc0e9a5
parent7fa8420170b7a8ca40d7414ddcddaed425442cee
arc: Add const attribute support for mathematical ARC builtins

The ARC builtin functions __builtin_arc_ffs and __builtin_arc_fls
perform pure mathematical operations equivalent to the standard
GCC __builtin_ffs function, which is marked with the const attribute.
However, the ARC target-specific versions were not marked as const,
preventing compiler optimizations like common subexpression elimination.

Extend the ARC builtin infrastructure to support function attributes
and mark the appropriate mathematical builtins as const:

- __builtin_arc_ffs: Find first set bit (const)
- __builtin_arc_fls: Find last set bit (const)
- __builtin_arc_norm: Count leading zeros (const)
- __builtin_arc_normw: Count leading zeros for 16-bit (const)
- __builtin_arc_swap: Endian swap (const)

gcc/ChangeLog:

* config/arc/builtins.def: Add ATTRS parameter to DEF_BUILTIN
macro calls.  Mark mathematical builtins (FFS, FLS, NORM, NORMW,
SWAP) with attr_const, leave others as NULL_TREE.
* config/arc/arc.cc: Add support for builtin function attributes.
Create attr_const using tree_cons.  Update DEF_BUILTIN macro to
pass ATTRS parameter to add_builtin_function.

gcc/testsuite/ChangeLog:

* gcc.target/arc/builtin_fls_const.c: New test.  Verify that
const attribute enables CSE optimization for mathematical ARC
builtins by checking that duplicate calls are eliminated and
results are optimized to shift operations.

Signed-off-by: Kees Cook <kees@kernel.org>
gcc/config/arc/arc.cc
gcc/config/arc/builtins.def
gcc/testsuite/gcc.target/arc/builtin_fls_const.c [new file with mode: 0644]