public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/ARM/heads/morello)] c: Fix bogus -Woverflow warnings with intcap types
@ 2023-01-17 10:47 Alex Coplan
  0 siblings, 0 replies; only message in thread
From: Alex Coplan @ 2023-01-17 10:47 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:e5aa9a91a270cee887b49630da14cd62cd7ce04a

commit e5aa9a91a270cee887b49630da14cd62cd7ce04a
Author: Alex Coplan <alex.coplan@arm.com>
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 <stdint.h>
 
-__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);

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-01-17 10:47 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-17 10:47 [gcc(refs/vendors/ARM/heads/morello)] c: Fix bogus -Woverflow warnings with intcap types Alex Coplan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).