Skip to content

Commit 7e7091b

Browse files
keesclaziss
authored andcommitted
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>
1 parent 7fa8420 commit 7e7091b

File tree

3 files changed

+197
-157
lines changed

3 files changed

+197
-157
lines changed

gcc/config/arc/arc.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6705,7 +6705,7 @@ arc_cannot_force_const_mem (machine_mode mode, rtx x)
67056705

67066706
enum arc_builtin_id
67076707
{
6708-
#define DEF_BUILTIN(NAME, N_ARGS, TYPE, ICODE, MASK) \
6708+
#define DEF_BUILTIN(NAME, N_ARGS, TYPE, ICODE, MASK, ATTRS) \
67096709
ARC_BUILTIN_ ## NAME,
67106710
#include "builtins.def"
67116711
#undef DEF_BUILTIN
@@ -6723,7 +6723,7 @@ struct GTY(()) arc_builtin_description
67236723
static GTY(()) struct arc_builtin_description
67246724
arc_bdesc[ARC_BUILTIN_COUNT] =
67256725
{
6726-
#define DEF_BUILTIN(NAME, N_ARGS, TYPE, ICODE, MASK) \
6726+
#define DEF_BUILTIN(NAME, N_ARGS, TYPE, ICODE, MASK, ATTRS) \
67276727
{ (enum insn_code) CODE_FOR_ ## ICODE, N_ARGS, NULL_TREE },
67286728
#include "builtins.def"
67296729
#undef DEF_BUILTIN
@@ -6855,8 +6855,11 @@ arc_init_builtins (void)
68556855
= build_function_type_list (long_long_integer_type_node,
68566856
V2SI_type_node, V2HI_type_node, NULL_TREE);
68576857

6858+
/* Create const attribute for mathematical functions. */
6859+
tree attr_const = tree_cons (get_identifier ("const"), NULL, NULL);
6860+
68586861
/* Add the builtins. */
6859-
#define DEF_BUILTIN(NAME, N_ARGS, TYPE, ICODE, MASK) \
6862+
#define DEF_BUILTIN(NAME, N_ARGS, TYPE, ICODE, MASK, ATTRS) \
68606863
{ \
68616864
int id = ARC_BUILTIN_ ## NAME; \
68626865
const char *Name = "__builtin_arc_" #NAME; \
@@ -6866,7 +6869,7 @@ arc_init_builtins (void)
68666869
if (MASK) \
68676870
arc_bdesc[id].fndecl \
68686871
= add_builtin_function (arc_tolower(name, Name), TYPE, id, \
6869-
BUILT_IN_MD, NULL, NULL_TREE); \
6872+
BUILT_IN_MD, NULL, ATTRS); \
68706873
}
68716874
#include "builtins.def"
68726875
#undef DEF_BUILTIN

0 commit comments

Comments
 (0)