c: Add u{,l,ll,imax}abs builtins [PR117024]

The following patch adds u{,l,ll,imax}abs builtins, which just fold
to ABSU_EXPR, similarly to how {,l,ll,imax}abs builtins fold to
ABS_EXPR.

2024-11-21  Jakub Jelinek  <jakub@redhat.com>

	PR c/117024
gcc/
	* coretypes.h (enum function_class): Add function_c2y_misc
	enumerator.
	* builtin-types.def (BT_FN_UINTMAX_INTMAX, BT_FN_ULONG_LONG,
	BT_FN_ULONGLONG_LONGLONG): New DEF_FUNCTION_TYPE_1s.
	* builtins.def (DEF_C2Y_BUILTIN): Define.
	(BUILT_IN_UABS, BUILT_IN_UIMAXABS, BUILT_IN_ULABS,
	BUILT_IN_ULLABS): New builtins.
	* builtins.cc (fold_builtin_abs): Handle also folding of u*abs
	to ABSU_EXPR.
	(fold_builtin_1): Handle BUILT_IN_U{,L,LL,IMAX}ABS.
gcc/lto/ChangeLog:
	* lto-lang.cc (flag_isoc2y): New variable.
gcc/ada/ChangeLog:
	* gcc-interface/utils.cc (flag_isoc2y): New variable.
gcc/testsuite/
	* gcc.c-torture/execute/builtins/lib/abs.c (uintmax_t): New typedef.
	(uabs, ulabs, ullabs, uimaxabs): New functions.
	* gcc.c-torture/execute/builtins/uabs-1.c: New test.
	* gcc.c-torture/execute/builtins/uabs-1.x: New file.
	* gcc.c-torture/execute/builtins/uabs-1-lib.c: New file.
	* gcc.c-torture/execute/builtins/uabs-2.c: New test.
	* gcc.c-torture/execute/builtins/uabs-2.x: New file.
	* gcc.c-torture/execute/builtins/uabs-2-lib.c: New file.
	* gcc.c-torture/execute/builtins/uabs-3.c: New test.
	* gcc.c-torture/execute/builtins/uabs-3.x: New test.
	* gcc.c-torture/execute/builtins/uabs-3-lib.c: New test.
This commit is contained in:
Jakub Jelinek 2024-11-21 09:34:28 +01:00 committed by Jakub Jelinek
parent 10e702789e
commit 7272e09c9b
16 changed files with 360 additions and 2 deletions

View File

@ -7513,6 +7513,7 @@ static int flag_isoc94 = 0;
static int flag_isoc99 = 0;
static int flag_isoc11 = 0;
static int flag_isoc23 = 0;
static int flag_isoc2y = 0;
/* Install what the common builtins.def offers plus our local additions.

View File

@ -256,6 +256,7 @@ DEF_FUNCTION_TYPE_0 (BT_FN_DFLOAT64X, BT_DFLOAT64X)
DEF_FUNCTION_TYPE_1 (BT_FN_LONG_LONG, BT_LONG, BT_LONG)
DEF_FUNCTION_TYPE_1 (BT_FN_LONGLONG_LONGLONG, BT_LONGLONG, BT_LONGLONG)
DEF_FUNCTION_TYPE_1 (BT_FN_UINTMAX_INTMAX, BT_UINTMAX, BT_INTMAX)
DEF_FUNCTION_TYPE_1 (BT_FN_INTMAX_INTMAX, BT_INTMAX, BT_INTMAX)
DEF_FUNCTION_TYPE_1 (BT_FN_FLOAT_FLOAT, BT_FLOAT, BT_FLOAT)
DEF_FUNCTION_TYPE_1 (BT_FN_DOUBLE_DOUBLE, BT_DOUBLE, BT_DOUBLE)
@ -403,7 +404,9 @@ DEF_FUNCTION_TYPE_1 (BT_FN_UINT_CONST_PTR, BT_UINT, BT_CONST_PTR)
DEF_FUNCTION_TYPE_1 (BT_FN_ULONG_PTR, BT_ULONG, BT_PTR)
DEF_FUNCTION_TYPE_1 (BT_FN_ULONG_CONST_PTR, BT_ULONG, BT_CONST_PTR)
DEF_FUNCTION_TYPE_1 (BT_FN_ULONG_ULONG, BT_ULONG, BT_ULONG)
DEF_FUNCTION_TYPE_1 (BT_FN_ULONG_LONG, BT_ULONG, BT_LONG)
DEF_FUNCTION_TYPE_1 (BT_FN_ULONGLONG_ULONGLONG, BT_ULONGLONG, BT_ULONGLONG)
DEF_FUNCTION_TYPE_1 (BT_FN_ULONGLONG_LONGLONG, BT_ULONGLONG, BT_LONGLONG)
DEF_FUNCTION_TYPE_1 (BT_FN_INT8_FLOAT, BT_INT8, BT_FLOAT)
DEF_FUNCTION_TYPE_1 (BT_FN_INT16_FLOAT, BT_INT16, BT_FLOAT)
DEF_FUNCTION_TYPE_1 (BT_FN_UINT32_FLOAT, BT_UINT32, BT_FLOAT)

View File

@ -9378,7 +9378,8 @@ fold_builtin_fabs (location_t loc, tree arg, tree type)
return fold_build1_loc (loc, ABS_EXPR, type, arg);
}
/* Fold a call to abs, labs, llabs or imaxabs with argument ARG. */
/* Fold a call to abs, labs, llabs, imaxabs, uabs, ulabs, ullabs or uimaxabs
with argument ARG. */
static tree
fold_builtin_abs (location_t loc, tree arg, tree type)
@ -9386,6 +9387,14 @@ fold_builtin_abs (location_t loc, tree arg, tree type)
if (!validate_arg (arg, INTEGER_TYPE))
return NULL_TREE;
if (TYPE_UNSIGNED (type))
{
if (TYPE_PRECISION (TREE_TYPE (arg))
!= TYPE_PRECISION (type)
|| TYPE_UNSIGNED (TREE_TYPE (arg)))
return NULL_TREE;
return fold_build1_loc (loc, ABSU_EXPR, type, arg);
}
arg = fold_convert_loc (loc, type, arg);
return fold_build1_loc (loc, ABS_EXPR, type, arg);
}
@ -10518,6 +10527,10 @@ fold_builtin_1 (location_t loc, tree expr, tree fndecl, tree arg0)
case BUILT_IN_LABS:
case BUILT_IN_LLABS:
case BUILT_IN_IMAXABS:
case BUILT_IN_UABS:
case BUILT_IN_ULABS:
case BUILT_IN_ULLABS:
case BUILT_IN_UIMAXABS:
return fold_builtin_abs (loc, arg0, type);
CASE_FLT_FN (BUILT_IN_CONJ):

View File

@ -164,6 +164,14 @@ along with GCC; see the file COPYING3. If not see
true, true, !flag_isoc23, ATTRS, \
targetm.libc_has_function (function_c23_misc, NULL_TREE), true)
/* Like DEF_LIB_BUILTIN, except that the function is only a part of
the standard in C2Y or above. */
#undef DEF_C2Y_BUILTIN
#define DEF_C2Y_BUILTIN(ENUM, NAME, TYPE, ATTRS) \
DEF_BUILTIN (ENUM, "__builtin_" NAME, BUILT_IN_NORMAL, TYPE, TYPE, \
true, true, !flag_isoc2y, ATTRS, \
targetm.libc_has_function (function_c2y_misc, NULL_TREE), true)
/* Like DEF_C99_BUILTIN, but for complex math functions. */
#undef DEF_C99_COMPL_BUILTIN
#define DEF_C99_COMPL_BUILTIN(ENUM, NAME, TYPE, ATTRS) \
@ -1073,6 +1081,10 @@ DEF_GCC_BUILTIN (BUILT_IN_SETJMP, "setjmp", BT_FN_INT_PTR, ATTR_RT_NOTHRO
DEF_EXT_LIB_BUILTIN (BUILT_IN_STRFMON, "strfmon", BT_FN_SSIZE_STRING_SIZE_CONST_STRING_VAR, ATTR_FORMAT_STRFMON_NOTHROW_3_4)
DEF_LIB_BUILTIN (BUILT_IN_STRFTIME, "strftime", BT_FN_SIZE_STRING_SIZE_CONST_STRING_CONST_TM_PTR, ATTR_FORMAT_STRFTIME_NOTHROW_3_0)
DEF_GCC_BUILTIN (BUILT_IN_TRAP, "trap", BT_FN_VOID, ATTR_NORETURN_NOTHROW_LEAF_COLD_LIST)
DEF_C2Y_BUILTIN (BUILT_IN_UABS, "uabs", BT_FN_UINT_INT, ATTR_CONST_NOTHROW_LEAF_LIST)
DEF_C2Y_BUILTIN (BUILT_IN_UIMAXABS, "uimaxabs", BT_FN_UINTMAX_INTMAX, ATTR_CONST_NOTHROW_LEAF_LIST)
DEF_C2Y_BUILTIN (BUILT_IN_ULABS, "ulabs", BT_FN_ULONG_LONG, ATTR_CONST_NOTHROW_LEAF_LIST)
DEF_C2Y_BUILTIN (BUILT_IN_ULLABS, "ullabs", BT_FN_ULONGLONG_LONGLONG, ATTR_CONST_NOTHROW_LEAF_LIST)
DEF_GCC_BUILTIN (BUILT_IN_UNREACHABLE_TRAP, "unreachable trap", BT_FN_VOID, ATTR_CONST_NORETURN_NOTHROW_LEAF_COLD_LIST)
DEF_GCC_BUILTIN (BUILT_IN_UNREACHABLE, "unreachable", BT_FN_VOID, ATTR_CONST_NORETURN_NOTHROW_LEAF_COLD_LIST)
DEF_GCC_BUILTIN (BUILT_IN_UNWIND_INIT, "unwind_init", BT_FN_VOID, ATTR_NULL)

View File

@ -421,7 +421,8 @@ enum function_class {
function_c99_math_complex,
function_sincos,
function_c11_misc,
function_c23_misc
function_c23_misc,
function_c2y_misc
};
/* Enumerate visibility settings. This is deliberately ordered from most

View File

@ -262,6 +262,7 @@ int flag_isoc94;
int flag_isoc99;
int flag_isoc11;
int flag_isoc23;
int flag_isoc2y;
/* Attribute handlers. */

View File

@ -7,6 +7,7 @@ extern void abort (void);
#endif
typedef __INTMAX_TYPE__ intmax_t;
typedef unsigned __INTMAX_TYPE__ uintmax_t;
__attribute__ ((__noinline__))
int
@ -39,3 +40,35 @@ imaxabs (intmax_t x)
ABORT_INSIDE_MAIN;
return x < 0 ? -x : x;
}
__attribute__ ((__noinline__))
unsigned int
uabs (int x)
{
ABORT_INSIDE_MAIN;
return x < 0 ? -(unsigned int) x : x;
}
__attribute__ ((__noinline__))
unsigned long
ulabs (long x)
{
ABORT_INSIDE_MAIN;
return x < 0 ? -(unsigned long) x : x;
}
__attribute__ ((__noinline__))
unsigned long long
ullabs (long long x)
{
ABORT_INSIDE_MAIN;
return x < 0 ? -(unsigned long long) x : x;
}
__attribute__ ((__noinline__))
uintmax_t
uimaxabs (intmax_t x)
{
ABORT_INSIDE_MAIN;
return x < 0 ? -(uintmax_t) x : x;
}

View File

@ -0,0 +1,22 @@
extern void abort (void);
extern int abs_called;
extern int inside_main;
/* The ulabs call should have been optimized, but the uabs call
shouldn't have been. */
unsigned int
uabs (int x)
{
if (inside_main)
abs_called = 1;
return (x < 0 ? -(unsigned int) x : x);
}
unsigned long
ulabs (long x)
{
if (inside_main)
abort ();
return (x < 0 ? -(unsigned long) x : x);
}

View File

@ -0,0 +1,22 @@
/* Test for -fno-builtin-FUNCTION. */
/* Origin: Joseph Myers <jsm28@cam.ac.uk>. */
/* GCC normally handles uabs and ulabs as built-in functions even without
optimization. So test that with -fno-builtin-uabs, ulabs is so handled
but uabs isn't. */
int abs_called = 0;
extern unsigned int uabs (int);
extern unsigned long ulabs (long);
extern void abort (void);
void
main_test (void)
{
if (ulabs (0) != 0)
abort ();
if (uabs (0) != 0)
abort ();
if (!abs_called)
abort ();
}

View File

@ -0,0 +1,2 @@
set additional_flags "-fno-builtin-uabs -std=c2y"
return 0

View File

@ -0,0 +1 @@
#include "lib/abs.c"

View File

@ -0,0 +1,123 @@
/* Test for builtin uabs, ulabs, ullabs, uimaxabs. */
/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
#include <limits.h>
typedef __INTMAX_TYPE__ intmax_t;
typedef unsigned __INTMAX_TYPE__ uintmax_t;
#define INTMAX_MAX __INTMAX_MAX__
extern unsigned int uabs (int);
extern unsigned long ulabs (long);
extern unsigned long long ullabs (long long);
extern uintmax_t uimaxabs (intmax_t);
extern void abort (void);
extern void link_error (void);
void
main_test (void)
{
/* For each type, test both runtime and compile time (constant folding)
optimization. */
volatile int i0 = 0, i1 = 1, im1 = -1, imin = -INT_MAX, imax = INT_MAX;
volatile long l0 = 0L, l1 = 1L, lm1 = -1L, lmin = -LONG_MAX, lmax = LONG_MAX;
volatile long long ll0 = 0LL, ll1 = 1LL, llm1 = -1LL;
volatile long long llmin = -__LONG_LONG_MAX__, llmax = __LONG_LONG_MAX__;
volatile intmax_t imax0 = 0, imax1 = 1, imaxm1 = -1;
volatile intmax_t imaxmin = -INTMAX_MAX, imaxmax = INTMAX_MAX;
if (uabs (i0) != 0)
abort ();
if (uabs (0) != 0)
link_error ();
if (uabs (i1) != 1)
abort ();
if (uabs (1) != 1)
link_error ();
if (uabs (im1) != 1)
abort ();
if (uabs (-1) != 1)
link_error ();
if (uabs (imin) != INT_MAX)
abort ();
if (uabs (imin - 1) != 1U + INT_MAX)
abort ();
if (uabs (-INT_MAX) != INT_MAX)
link_error ();
if (uabs (-INT_MAX - 1) != 1U + INT_MAX)
link_error ();
if (uabs (imax) != INT_MAX)
abort ();
if (uabs (INT_MAX) != INT_MAX)
link_error ();
if (ulabs (l0) != 0L)
abort ();
if (ulabs (0L) != 0L)
link_error ();
if (ulabs (l1) != 1L)
abort ();
if (ulabs (1L) != 1L)
link_error ();
if (ulabs (lm1) != 1L)
abort ();
if (ulabs (-1L) != 1L)
link_error ();
if (ulabs (lmin) != LONG_MAX)
abort ();
if (ulabs (lmin - 1) != 1UL + LONG_MAX)
abort ();
if (ulabs (-LONG_MAX) != LONG_MAX)
link_error ();
if (ulabs (-LONG_MAX - 1) != 1UL + LONG_MAX)
link_error ();
if (ulabs (lmax) != LONG_MAX)
abort ();
if (ulabs (LONG_MAX) != LONG_MAX)
link_error ();
if (ullabs (ll0) != 0LL)
abort ();
if (ullabs (0LL) != 0LL)
link_error ();
if (ullabs (ll1) != 1LL)
abort ();
if (ullabs (1LL) != 1LL)
link_error ();
if (ullabs (llm1) != 1LL)
abort ();
if (ullabs (-1LL) != 1LL)
link_error ();
if (ullabs (llmin) != __LONG_LONG_MAX__)
abort ();
if (ullabs (llmin - 1) != 1ULL + __LONG_LONG_MAX__)
abort ();
if (ullabs (-__LONG_LONG_MAX__) != __LONG_LONG_MAX__)
link_error ();
if (ullabs (-__LONG_LONG_MAX__ - 1) != 1ULL + __LONG_LONG_MAX__)
link_error ();
if (ullabs (llmax) != __LONG_LONG_MAX__)
abort ();
if (ullabs (__LONG_LONG_MAX__) != __LONG_LONG_MAX__)
link_error ();
if (uimaxabs (imax0) != 0)
abort ();
if (uimaxabs (0) != 0)
link_error ();
if (uimaxabs (imax1) != 1)
abort ();
if (uimaxabs (1) != 1)
link_error ();
if (uimaxabs (imaxm1) != 1)
abort ();
if (uimaxabs (-1) != 1)
link_error ();
if (uimaxabs (imaxmin) != INTMAX_MAX)
abort ();
if (uimaxabs (imaxmin - 1) != (uintmax_t) 1 + INTMAX_MAX)
abort ();
if (uimaxabs (-INTMAX_MAX) != INTMAX_MAX)
link_error ();
if (uimaxabs (-INTMAX_MAX - 1) != (uintmax_t) 1 + INTMAX_MAX)
link_error ();
if (uimaxabs (imaxmax) != INTMAX_MAX)
abort ();
if (uimaxabs (INTMAX_MAX) != INTMAX_MAX)
link_error ();
}

View File

@ -0,0 +1,2 @@
set additional_flags -std=c2y
return 0

View File

@ -0,0 +1 @@
#include "lib/abs.c"

View File

@ -0,0 +1,119 @@
/* Test for builtin uabs, ulabs, ullabs, uimaxabs. Test for __builtin versions. */
/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
#include <limits.h>
typedef __INTMAX_TYPE__ intmax_t;
typedef unsigned __INTMAX_TYPE__ uintmax_t;
#define INTMAX_MAX __INTMAX_MAX__
extern void abort (void);
extern void link_error (void);
void
main_test (void)
{
/* For each type, test both runtime and compile time (constant folding)
optimization. */
volatile int i0 = 0, i1 = 1, im1 = -1, imin = -INT_MAX, imax = INT_MAX;
volatile long l0 = 0L, l1 = 1L, lm1 = -1L, lmin = -LONG_MAX, lmax = LONG_MAX;
volatile long long ll0 = 0LL, ll1 = 1LL, llm1 = -1LL;
volatile long long llmin = -__LONG_LONG_MAX__, llmax = __LONG_LONG_MAX__;
volatile intmax_t imax0 = 0, imax1 = 1, imaxm1 = -1;
volatile intmax_t imaxmin = -INTMAX_MAX, imaxmax = INTMAX_MAX;
if (__builtin_uabs (i0) != 0)
abort ();
if (__builtin_uabs (0) != 0)
link_error ();
if (__builtin_uabs (i1) != 1)
abort ();
if (__builtin_uabs (1) != 1)
link_error ();
if (__builtin_uabs (im1) != 1)
abort ();
if (__builtin_uabs (-1) != 1)
link_error ();
if (__builtin_uabs (imin) != INT_MAX)
abort ();
if (__builtin_uabs (imin - 1) != 1U + INT_MAX)
abort ();
if (__builtin_uabs (-INT_MAX) != INT_MAX)
link_error ();
if (__builtin_uabs (-INT_MAX - 1) != 1U + INT_MAX)
link_error ();
if (__builtin_uabs (imax) != INT_MAX)
abort ();
if (__builtin_uabs (INT_MAX) != INT_MAX)
link_error ();
if (__builtin_ulabs (l0) != 0L)
abort ();
if (__builtin_ulabs (0L) != 0L)
link_error ();
if (__builtin_ulabs (l1) != 1L)
abort ();
if (__builtin_ulabs (1L) != 1L)
link_error ();
if (__builtin_ulabs (lm1) != 1L)
abort ();
if (__builtin_ulabs (-1L) != 1L)
link_error ();
if (__builtin_ulabs (lmin) != LONG_MAX)
abort ();
if (__builtin_ulabs (lmin - 1) != 1UL + LONG_MAX)
abort ();
if (__builtin_ulabs (-LONG_MAX) != LONG_MAX)
link_error ();
if (__builtin_ulabs (-LONG_MAX - 1) != 1UL + LONG_MAX)
link_error ();
if (__builtin_ulabs (lmax) != LONG_MAX)
abort ();
if (__builtin_ulabs (LONG_MAX) != LONG_MAX)
link_error ();
if (__builtin_ullabs (ll0) != 0LL)
abort ();
if (__builtin_ullabs (0LL) != 0LL)
link_error ();
if (__builtin_ullabs (ll1) != 1LL)
abort ();
if (__builtin_ullabs (1LL) != 1LL)
link_error ();
if (__builtin_ullabs (llm1) != 1LL)
abort ();
if (__builtin_ullabs (-1LL) != 1LL)
link_error ();
if (__builtin_ullabs (llmin) != __LONG_LONG_MAX__)
abort ();
if (__builtin_ullabs (llmin - 1) != 1ULL + __LONG_LONG_MAX__)
abort ();
if (__builtin_ullabs (-__LONG_LONG_MAX__) != __LONG_LONG_MAX__)
link_error ();
if (__builtin_ullabs (-__LONG_LONG_MAX__ - 1) != 1ULL + __LONG_LONG_MAX__)
link_error ();
if (__builtin_ullabs (llmax) != __LONG_LONG_MAX__)
abort ();
if (__builtin_ullabs (__LONG_LONG_MAX__) != __LONG_LONG_MAX__)
link_error ();
if (__builtin_uimaxabs (imax0) != 0)
abort ();
if (__builtin_uimaxabs (0) != 0)
link_error ();
if (__builtin_uimaxabs (imax1) != 1)
abort ();
if (__builtin_uimaxabs (1) != 1)
link_error ();
if (__builtin_uimaxabs (imaxm1) != 1)
abort ();
if (__builtin_uimaxabs (-1) != 1)
link_error ();
if (__builtin_uimaxabs (imaxmin) != INTMAX_MAX)
abort ();
if (__builtin_uimaxabs (imaxmin - 1) != (uintmax_t) 1 + INTMAX_MAX)
abort ();
if (__builtin_uimaxabs (-INTMAX_MAX) != INTMAX_MAX)
link_error ();
if (__builtin_uimaxabs (-INTMAX_MAX - 1) != (uintmax_t) 1 + INTMAX_MAX)
link_error ();
if (__builtin_uimaxabs (imaxmax) != INTMAX_MAX)
abort ();
if (__builtin_uimaxabs (INTMAX_MAX) != INTMAX_MAX)
link_error ();
}

View File

@ -0,0 +1,2 @@
set additional_flags -std=c2y
return 0