From: Jakub Jelinek Date: Fri, 15 Dec 2023 09:10:58 +0000 (+0100) Subject: lower-bitint: Fix .{ADD,SUB,MUL}_OVERFLOW with _BitInt large/huge INTEGER_CST argumen... X-Git-Tag: basepoints/gcc-15~3560 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d50d3d0a688e8dac31b307b3aad3fbc99283ebc4;p=thirdparty%2Fgcc.git lower-bitint: Fix .{ADD,SUB,MUL}_OVERFLOW with _BitInt large/huge INTEGER_CST arguments [PR113003] As shown in the testcase, .{ADD,SUB,MUL}_OVERFLOW calls are another exception to the middle/large/huge _BitInt discovery through SSA_NAMEs next to stores of INTEGER_CSTs to memory and their conversions to floating point. The calls can have normal COMPLEX_TYPE with INTEGER_TYPE elts return type (or BITINT_TYPE with small precision) and one of the arguments can be SSA_NAME with an INTEGER_TYPE or small BITINT_TYPE as well; still, when there is an INTEGER_CST argument with large/huge BITINT_TYPE, we need to lower it that way. 2023-12-15 Jakub Jelinek PR tree-optimization/113003 * gimple-lower-bitint.cc (arith_overflow_arg_kind): New function. (gimple_lower_bitint): Use it to catch .{ADD,SUB,MUL}_OVERFLOW calls with large/huge INTEGER_CST arguments. * gcc.dg/bitint-54.c: New test. --- diff --git a/gcc/gimple-lower-bitint.cc b/gcc/gimple-lower-bitint.cc index d52e4c44f065..696bba4cb56c 100644 --- a/gcc/gimple-lower-bitint.cc +++ b/gcc/gimple-lower-bitint.cc @@ -5641,6 +5641,37 @@ build_bitint_stmt_ssa_conflicts (gimple *stmt, live_track *live, def (live, lhs, graph); } +/* If STMT is .{ADD,SUB,MUL}_OVERFLOW with INTEGER_CST arguments, + return the largest bitint_prec_kind of them, otherwise return + bitint_prec_small. */ + +static bitint_prec_kind +arith_overflow_arg_kind (gimple *stmt) +{ + bitint_prec_kind ret = bitint_prec_small; + if (is_gimple_call (stmt) && gimple_call_internal_p (stmt)) + switch (gimple_call_internal_fn (stmt)) + { + case IFN_ADD_OVERFLOW: + case IFN_SUB_OVERFLOW: + case IFN_MUL_OVERFLOW: + for (int i = 0; i < 2; ++i) + { + tree a = gimple_call_arg (stmt, i); + if (TREE_CODE (a) == INTEGER_CST + && TREE_CODE (TREE_TYPE (a)) == BITINT_TYPE) + { + bitint_prec_kind kind = bitint_precision_kind (TREE_TYPE (a)); + ret = MAX (ret, kind); + } + } + break; + default: + break; + } + return ret; +} + /* Entry point for _BitInt(N) operation lowering during optimization. */ static unsigned int @@ -5657,7 +5688,12 @@ gimple_lower_bitint (void) continue; tree type = TREE_TYPE (s); if (TREE_CODE (type) == COMPLEX_TYPE) - type = TREE_TYPE (type); + { + if (arith_overflow_arg_kind (SSA_NAME_DEF_STMT (s)) + != bitint_prec_small) + break; + type = TREE_TYPE (type); + } if (TREE_CODE (type) == BITINT_TYPE && bitint_precision_kind (type) != bitint_prec_small) break; @@ -5745,7 +5781,12 @@ gimple_lower_bitint (void) continue; tree type = TREE_TYPE (s); if (TREE_CODE (type) == COMPLEX_TYPE) - type = TREE_TYPE (type); + { + if (arith_overflow_arg_kind (SSA_NAME_DEF_STMT (s)) + >= bitint_prec_large) + has_large_huge = true; + type = TREE_TYPE (type); + } if (TREE_CODE (type) == BITINT_TYPE && bitint_precision_kind (type) >= bitint_prec_large) { @@ -6245,8 +6286,7 @@ gimple_lower_bitint (void) { bitint_prec_kind this_kind = bitint_precision_kind (TREE_TYPE (t)); - if (this_kind > kind) - kind = this_kind; + kind = MAX (kind, this_kind); } if (is_gimple_assign (stmt) && gimple_store_p (stmt)) { @@ -6255,8 +6295,7 @@ gimple_lower_bitint (void) { bitint_prec_kind this_kind = bitint_precision_kind (TREE_TYPE (t)); - if (this_kind > kind) - kind = this_kind; + kind = MAX (kind, this_kind); } } if (is_gimple_assign (stmt) @@ -6268,21 +6307,22 @@ gimple_lower_bitint (void) { bitint_prec_kind this_kind = bitint_precision_kind (TREE_TYPE (t)); - if (this_kind > kind) - kind = this_kind; + kind = MAX (kind, this_kind); } } if (is_gimple_call (stmt)) { t = gimple_call_lhs (stmt); - if (t - && TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE - && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) == BITINT_TYPE) + if (t && TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE) { - bitint_prec_kind this_kind - = bitint_precision_kind (TREE_TYPE (TREE_TYPE (t))); - if (this_kind > kind) - kind = this_kind; + bitint_prec_kind this_kind = arith_overflow_arg_kind (stmt); + kind = MAX (kind, this_kind); + if (TREE_CODE (TREE_TYPE (TREE_TYPE (t))) == BITINT_TYPE) + { + this_kind + = bitint_precision_kind (TREE_TYPE (TREE_TYPE (t))); + kind = MAX (kind, this_kind); + } } } if (kind == bitint_prec_small) diff --git a/gcc/testsuite/gcc.dg/bitint-54.c b/gcc/testsuite/gcc.dg/bitint-54.c new file mode 100644 index 000000000000..f23d4e1c2f6b --- /dev/null +++ b/gcc/testsuite/gcc.dg/bitint-54.c @@ -0,0 +1,21 @@ +/* PR tree-optimization/113003 */ +/* { dg-do compile { target bitint } } */ +/* { dg-options "-std=c23 -O2" } */ + +#if __BITINT_MAXWIDTH__ >= 131 +int +foo (_BitInt(7) x) +{ + return __builtin_mul_overflow_p (x, 1046555807606105294475452482332716433408wb, 0); +} + +#ifdef __SIZEOF_INT128__ +int +bar (unsigned __int128 x) +{ + return __builtin_sub_overflow_p (340282366920938463463374607431768211457uwb, x, 0); +} +#endif +#else +int i; +#endif