From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7810) id 0A9F83858418; Tue, 17 Jan 2023 10:47:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0A9F83858418 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1673952441; bh=qeQQbQkmF8Sey36LSSCjoNOnhPhMwXgJYQfYzltM7DQ=; h=From:To:Subject:Date:From; b=FHu11BjHxtU3aJPRWlxtAv7PcufoWtQh+Fx/qB4wEDpQHWjaYxxHQdAsck3KRxYgd RuBkcNeNBRxhekPbKJrfYvy+p4J+DeR3HB+4YWFEEThnA6vmFujIwjQ81e4ioCE530 P26dQVRizY1ddQGm+7U9Tb4vFClg9JoY7pHWuX2I= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Alex Coplan To: gcc-cvs@gcc.gnu.org Subject: [gcc(refs/vendors/ARM/heads/morello)] c: Fix bogus -Woverflow warnings with intcap types X-Act-Checkin: gcc X-Git-Author: Alex Coplan X-Git-Refname: refs/vendors/ARM/heads/morello X-Git-Oldrev: 350b66b67e7cac124347bceda754a82c2c440360 X-Git-Newrev: e5aa9a91a270cee887b49630da14cd62cd7ce04a Message-Id: <20230117104721.0A9F83858418@sourceware.org> Date: Tue, 17 Jan 2023 10:47:21 +0000 (GMT) List-Id: https://gcc.gnu.org/g:e5aa9a91a270cee887b49630da14cd62cd7ce04a commit e5aa9a91a270cee887b49630da14cd62cd7ce04a Author: Alex Coplan Date: Thu Dec 22 19:14:26 2022 +0000 c: Fix bogus -Woverflow warnings with intcap types For this testcase: unsigned __intcap f(void) { return -4; } we would incorrectly diagnose it with: warning: unsigned conversion from 'int' to 'unsigned __intcap' changes value from '-4' to '18446744073709551612' [-Woverflow] which was due to a bug in c_common_signed_or_unsigned_type which would give the wrong answer for unsignedp = 0 and type = unsigned __intcap. It would return unsigned __intcap but should of course return __intcap. This patch fixes that bug. After applying that fix, further testing showed that this change revealed another problem. In particular, the test gcc.target/aarch64/morello/conversion-warnings-1.c has the function: __intcap_t overflow_intcap() { return 1ULL << 63; } which was getting diagnosed inconsistently between -mfake-capability and -march=morello. In this case, c-warn.c:warnings_for_convert_and_check uses tree.c:int_fits_type_p to determine whether (and how) to diagnose the conversion. The problem here is that int_fits_type_p compares the given constant against TYPE_{MIN,MAX}_VALUE. However, for an INTCAP_TYPE, these values are as for a 128-bit integer type. If we want to know whether a given integer (non-capability) constant fits in an intcap, we really want to know if the constant fits in the arithmetic range of the intcap instead (that is, the range of the non-capability type of the intcap type). Hence, in this patch, we adjust int_fits_type_p to compare integer constants against the TYPE_{MIN,MAX}_VALUE of the non-capability type of the INTCAP_TYPE. We also adjust the conversion-warnings-1.c test since we now diagnose this case differently. The warning we now give for the overflow_intcap function matches that given for the corresponding function where the return type is long, so this seems like a reasonable change. Diff: --- gcc/c-family/c-common.c | 2 +- gcc/testsuite/gcc.target/aarch64/morello/conversion-warnings-1.c | 2 +- gcc/testsuite/gcc.target/aarch64/morello/intcap-literal-conv.c | 6 ++++++ gcc/tree.c | 6 ++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 65e8a80286b..e0c7f18fd5d 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -2581,7 +2581,7 @@ c_common_signed_or_unsigned_type (int unsignedp, tree type) types, and producing a signed or unsigned variant of an ENUMERAL_TYPE may cause other problems as well. */ - if (!INTEGRAL_TYPE_P (type) + if ((!INTEGRAL_TYPE_P (type) && !INTCAP_TYPE_P (type)) || TYPE_UNSIGNED (type) == unsignedp) return type; diff --git a/gcc/testsuite/gcc.target/aarch64/morello/conversion-warnings-1.c b/gcc/testsuite/gcc.target/aarch64/morello/conversion-warnings-1.c index 78d94a5659e..21404dd3c21 100644 --- a/gcc/testsuite/gcc.target/aarch64/morello/conversion-warnings-1.c +++ b/gcc/testsuite/gcc.target/aarch64/morello/conversion-warnings-1.c @@ -2,7 +2,7 @@ #include -__intcap_t overflow_intcap() { return 1ULL << 63; } /* { dg-warning "overflow in conversion" } */ +__intcap_t overflow_intcap() { return 1ULL << 63; } /* { dg-warning "signed conversion" } */ __uintcap_t overflow_uintcap() { return -1; } /* { dg-warning "unsigned conversion" } */ __intcap_t compare_to_intcap(int x) { return x == 0; } diff --git a/gcc/testsuite/gcc.target/aarch64/morello/intcap-literal-conv.c b/gcc/testsuite/gcc.target/aarch64/morello/intcap-literal-conv.c new file mode 100644 index 00000000000..6b5bd4396fd --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/morello/intcap-literal-conv.c @@ -0,0 +1,6 @@ +/* { dg-do compile } */ +/* Check that we don't warn here by default. */ +unsigned __intcap f(void) +{ + return -4; +} diff --git a/gcc/tree.c b/gcc/tree.c index c44b89fdf42..9b778220490 100644 --- a/gcc/tree.c +++ b/gcc/tree.c @@ -9247,6 +9247,12 @@ int_fits_type_p (const_tree c, const_tree type) if (TREE_CODE (type) == BOOLEAN_TYPE) return wi::fits_to_boolean_p (wi::to_wide (c), type); + /* If converting from a non-capability constant to an intcap type, + we want to know if the non-capability constant fits in the arithmetic + range of the intcap type. */ + if (!INTCAP_TYPE_P (TREE_TYPE (c)) && INTCAP_TYPE_P (type)) + type = TREE_TYPE (type); + retry: type_low_bound = TYPE_MIN_VALUE (type); type_high_bound = TYPE_MAX_VALUE (type);