re PR c/68062 (ICE when comparing vectors)
authorMarek Polacek <polacek@redhat.com>
Wed, 27 Jan 2016 19:13:42 +0000 (19:13 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Wed, 27 Jan 2016 19:13:42 +0000 (19:13 +0000)
PR c/68062
* c-typeck.c (build_binary_op) [EQ_EXPR, GE_EXPR]: Promote operand
to unsigned, if needed.  Add -Wsign-compare warning.

* typeck.c (cp_build_binary_op): Promote operand to unsigned, if
needed.  Add -Wsign-compare warning.

* c-c++-common/vector-compare-4.c: New test.

From-SVN: r232894

gcc/c/ChangeLog
gcc/c/c-typeck.c
gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/vector-compare-4.c [new file with mode: 0644]

index 7d6250e586a63c4ae731a3ab11b216123cbc37a7..7999d2a987a82a5707bc7e1d661ecd1244861a95 100644 (file)
@@ -1,3 +1,9 @@
+2016-01-27  Marek Polacek  <polacek@redhat.com>
+
+       PR c/68062
+       * c-typeck.c (build_binary_op) [EQ_EXPR, GE_EXPR]: Promote operand
+       to unsigned, if needed.  Add -Wsign-compare warning.
+
 2016-01-26  Jakub Jelinek  <jakub@redhat.com>
 
        PR tree-optimization/69483
index d602e3383e9e84d4c37e6bc08e8c96c08b833507..65925cbc8e7f87f7ed8f5ed8170bda500459a685 100644 (file)
@@ -11048,6 +11048,20 @@ build_binary_op (location_t location, enum tree_code code,
               return error_mark_node;
             }
 
+         /* It's not precisely specified how the usual arithmetic
+            conversions apply to the vector types.  Here, we use
+            the unsigned type if one of the operands is signed and
+            the other one is unsigned.  */
+         if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
+           {
+             if (!TYPE_UNSIGNED (type0))
+               op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
+             else
+               op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
+             warning_at (location, OPT_Wsign_compare, "comparison between "
+                         "types %qT and %qT", type0, type1);
+           }
+
           /* Always construct signed integer vector type.  */
           intt = c_common_type_for_size (GET_MODE_BITSIZE
                                           (TYPE_MODE (TREE_TYPE (type0))), 0);
@@ -11201,6 +11215,20 @@ build_binary_op (location_t location, enum tree_code code,
               return error_mark_node;
             }
 
+         /* It's not precisely specified how the usual arithmetic
+            conversions apply to the vector types.  Here, we use
+            the unsigned type if one of the operands is signed and
+            the other one is unsigned.  */
+         if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
+           {
+             if (!TYPE_UNSIGNED (type0))
+               op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
+             else
+               op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
+             warning_at (location, OPT_Wsign_compare, "comparison between "
+                         "types %qT and %qT", type0, type1);
+           }
+
           /* Always construct signed integer vector type.  */
           intt = c_common_type_for_size (GET_MODE_BITSIZE
                                           (TYPE_MODE (TREE_TYPE (type0))), 0);
index 493b17afee32acf61c50b36af40754e5ff4e37ec..52fb9d8c36428830a2f093f0103452d03da1e714 100644 (file)
@@ -1,3 +1,9 @@
+2016-01-27  Marek Polacek  <polacek@redhat.com>
+
+       PR c/68062
+       * typeck.c (cp_build_binary_op): Promote operand to unsigned, if
+       needed.  Add -Wsign-compare warning.
+
 2016-01-27  Ryan Burn  <contact@rnburn.com>
 
        PR cilkplus/69267
index 0503c6f0c738e027d86cc1e7a240ab549a8320d7..d2c23f42df2f1f8249c71814285f52f554e89491 100644 (file)
@@ -4823,6 +4823,20 @@ cp_build_binary_op (location_t location,
              return error_mark_node;
            }
 
+         /* It's not precisely specified how the usual arithmetic
+            conversions apply to the vector types.  Here, we use
+            the unsigned type if one of the operands is signed and
+            the other one is unsigned.  */
+         if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
+           {
+             if (!TYPE_UNSIGNED (type0))
+               op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
+             else
+               op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
+             warning_at (location, OPT_Wsign_compare, "comparison between "
+                         "types %qT and %qT", type0, type1);
+           }
+
          /* Always construct signed integer vector type.  */
          intt = c_common_type_for_size (GET_MODE_BITSIZE
                                           (TYPE_MODE (TREE_TYPE (type0))), 0);
index e674bf495bab48b21540f44da72027f7adfd3327..2cad0c2585bde363a48d022ad92f0e3142c27d39 100644 (file)
@@ -1,3 +1,8 @@
+2016-01-27  Marek Polacek  <polacek@redhat.com>
+
+       PR c/68062
+       * c-c++-common/vector-compare-4.c: New test.
+
 2016-01-27  David Malcolm  <dmalcolm@redhat.com>
 
        PR preprocessor/69126
diff --git a/gcc/testsuite/c-c++-common/vector-compare-4.c b/gcc/testsuite/c-c++-common/vector-compare-4.c
new file mode 100644 (file)
index 0000000..b44f474
--- /dev/null
@@ -0,0 +1,42 @@
+/* PR c/68062 */
+/* { dg-do compile } */
+/* { dg-options "-Wsign-compare" } */
+
+typedef signed char __attribute__ ((vector_size (4))) v4qi;
+typedef unsigned char __attribute__ ((vector_size (4))) uv4qi;
+typedef signed int __attribute__ ((vector_size (4 * __SIZEOF_INT__))) v4si;
+typedef unsigned int __attribute__ ((vector_size (4 * __SIZEOF_INT__))) uv4si;
+
+v4qi
+fn1 (void)
+{
+  v4qi a = { 1, 2, 3, 4 };
+  uv4qi b = { 4, 3, 2, 1 };
+  v4qi v = { 0, 0, 0, 0 };
+
+  v += (a == b); /* { dg-warning "comparison between types" } */
+  v += (a != b); /* { dg-warning "comparison between types" } */
+  v += (a >= b); /* { dg-warning "comparison between types" } */
+  v += (a <= b); /* { dg-warning "comparison between types" } */
+  v += (a > b); /* { dg-warning "comparison between types" } */
+  v += (a < b); /* { dg-warning "comparison between types" } */
+
+  return v;
+}
+
+v4si
+fn2 (void)
+{
+  v4si a = { 1, 2, 3, 4 };
+  uv4si b = { 4, 3, 2, 1 };
+  v4si v = { 0, 0, 0, 0 };
+
+  v += (a == b); /* { dg-warning "comparison between types" } */
+  v += (a != b); /* { dg-warning "comparison between types" } */
+  v += (a >= b); /* { dg-warning "comparison between types" } */
+  v += (a <= b); /* { dg-warning "comparison between types" } */
+  v += (a > b); /* { dg-warning "comparison between types" } */
+  v += (a < b); /* { dg-warning "comparison between types" } */
+
+  return v;
+}