From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id B34B838346BF; Tue, 10 May 2022 08:26:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B34B838346BF MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r10-10707] builtins: Fix up expand_builtin_int_roundingfn_2 [PR105211] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: 73f5db083a73c6125e0203ea50c4d1f0a1c6421a X-Git-Newrev: b2a09805d7df2d3ba3b8f27161cc37809f37d27a Message-Id: <20220510082617.B34B838346BF@sourceware.org> Date: Tue, 10 May 2022 08:26:17 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 May 2022 08:26:17 -0000 https://gcc.gnu.org/g:b2a09805d7df2d3ba3b8f27161cc37809f37d27a commit r10-10707-gb2a09805d7df2d3ba3b8f27161cc37809f37d27a Author: Jakub Jelinek Date: Tue Apr 12 09:16:06 2022 +0200 builtins: Fix up expand_builtin_int_roundingfn_2 [PR105211] The expansion of __builtin_iround{,f,l} etc. builtins in some cases emits calls to a different fallback builtin. To locate the right builtin it uses mathfn_built_in_1 with the type of the first argument. If its TYPE_MAIN_VARIANT is {float,double,long_double}_type_node, all is fine, but on the following testcase, because GIMPLE considers scalar float conversions between types with the same mode as useless, TYPE_MAIN_VARIANT of the arg's type is float32_type_node and because there isn't __builtin_lroundf32 returns NULL and we ICE. This patch will first try the type of the first argument of the builtin's prototype (so that say on sizeof(double)==sizeof(long double) target it honors whether it was a *l or non-*l call; though even that can't be 100% trusted, user could incorrectly prototype it) and as fallback the type argument. If neither works, doesn't fallback. 2022-04-11 Jakub Jelinek PR rtl-optimization/105211 * builtins.c (expand_builtin_int_roundingfn_2): If mathfn_built_in_1 fails for TREE_TYPE (arg), retry it with TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl))) and if even that fails, emit call normally. * gcc.dg/pr105211.c: New test. (cherry picked from commit 91a38e8a848c61b2e23ee277306dc8cd194d135b) Diff: --- gcc/builtins.c | 30 +++++++++++++++++++++--------- gcc/testsuite/gcc.dg/pr105211.c | 11 +++++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/gcc/builtins.c b/gcc/builtins.c index 10b6fd3bb4d..d89ce37c496 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -2927,16 +2927,28 @@ expand_builtin_int_roundingfn_2 (tree exp, rtx target) BUILT_IN_IROUND and if __builtin_iround is called directly, emit a call to lround in the hope that the target provides at least some C99 functions. This should result in the best user experience for - not full C99 targets. */ - tree fallback_fndecl = mathfn_built_in_1 - (TREE_TYPE (arg), as_combined_fn (fallback_fn), 0); - - exp = build_call_nofold_loc (EXPR_LOCATION (exp), - fallback_fndecl, 1, arg); + not full C99 targets. + As scalar float conversions with same mode are useless in GIMPLE, + we can end up e.g. with _Float32 argument passed to float builtin, + try to get the type from the builtin prototype first. */ + tree fallback_fndecl = NULL_TREE; + if (tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl))) + fallback_fndecl + = mathfn_built_in_1 (TREE_VALUE (argtypes), + as_combined_fn (fallback_fn), 0); + if (fallback_fndecl == NULL_TREE) + fallback_fndecl + = mathfn_built_in_1 (TREE_TYPE (arg), + as_combined_fn (fallback_fn), 0); + if (fallback_fndecl) + { + exp = build_call_nofold_loc (EXPR_LOCATION (exp), + fallback_fndecl, 1, arg); - target = expand_call (exp, NULL_RTX, target == const0_rtx); - target = maybe_emit_group_store (target, TREE_TYPE (exp)); - return convert_to_mode (mode, target, 0); + target = expand_call (exp, NULL_RTX, target == const0_rtx); + target = maybe_emit_group_store (target, TREE_TYPE (exp)); + return convert_to_mode (mode, target, 0); + } } return expand_call (exp, target, target == const0_rtx); diff --git a/gcc/testsuite/gcc.dg/pr105211.c b/gcc/testsuite/gcc.dg/pr105211.c new file mode 100644 index 00000000000..9bafe6feb19 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr105211.c @@ -0,0 +1,11 @@ +/* PR rtl-optimization/105211 */ +/* { dg-do compile } */ +/* { dg-options "-Os -ffast-math" } */ +/* { dg-add-options float32 } */ +/* { dg-require-effective-target float32 } */ + +short +foo (_Float32 f) +{ + return __builtin_roundf (f); +}