diff mbox

[C/C++] Don't emit invalid VEC_COND_EXPR for vector comparisons (PR c/68062)

Message ID 20160113152650.GJ25528@redhat.com
State New
Headers show

Commit Message

Marek Polacek Jan. 13, 2016, 3:26 p.m. UTC
We crash on the following testcase because the FEs create 

  VEC_COND_EXPR < a == b , { -1, -1, -1, -1 } , { 0, 0, 0, 0 } >

where the operands of the comparison are same except for the sign (it's
vector_types_compatible_elements_p that says that).  But GIMPLE doesn't
like the difference in the sign.

As I read the discussion in the PR, the way forward here is to perform
signed -> unsigned conversions.  So that is what I do in the following.

Bootstrapped/regtested on x86_64-linux and ppc64le-linux, ok for trunk?

2016-01-13  Marek Polacek  <polacek@redhat.com>

	PR c/68062
	* c-typeck.c (build_binary_op) [EQ_EXPR, GE_EXPR]: Promote operand
	to unsigned, if needed.

	* typeck.c (cp_build_binary_op): Promote operand to unsigned, if
	needed.

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


	Marek

Comments

Joseph Myers Jan. 13, 2016, 6:53 p.m. UTC | #1
Will -Wsign-compare warnings be generated for the implicit signed / 
unsigned conversions in comparisons, as for scalar comparisons?
Richard Biener Jan. 13, 2016, 7:03 p.m. UTC | #2
On January 13, 2016 4:26:50 PM GMT+01:00, Marek Polacek <polacek@redhat.com> wrote:
>We crash on the following testcase because the FEs create 
>
>  VEC_COND_EXPR < a == b , { -1, -1, -1, -1 } , { 0, 0, 0, 0 } >
>
>where the operands of the comparison are same except for the sign (it's
>vector_types_compatible_elements_p that says that).  But GIMPLE doesn't
>like the difference in the sign.
>
>As I read the discussion in the PR, the way forward here is to perform
>signed -> unsigned conversions.  So that is what I do in the following.
>
>Bootstrapped/regtested on x86_64-linux and ppc64le-linux, ok for trunk?

Looks good from a middle-end perspective.

Richard.

>2016-01-13  Marek Polacek  <polacek@redhat.com>
>
>	PR c/68062
>	* c-typeck.c (build_binary_op) [EQ_EXPR, GE_EXPR]: Promote operand
>	to unsigned, if needed.
>
>	* typeck.c (cp_build_binary_op): Promote operand to unsigned, if
>	needed.
>
>	* c-c++-common/vector-compare-4.c: New test.
>
>diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
>index 952041b..b646451 100644
>--- gcc/c/c-typeck.c
>+++ gcc/c/c-typeck.c
>@@ -11048,6 +11048,18 @@ 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);
>+	    }
>+
>           /* Always construct signed integer vector type.  */
>           intt = c_common_type_for_size (GET_MODE_BITSIZE
> 					   (TYPE_MODE (TREE_TYPE (type0))), 0);
>@@ -11201,6 +11213,18 @@ 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);
>+	    }
>+
>           /* Always construct signed integer vector type.  */
>           intt = c_common_type_for_size (GET_MODE_BITSIZE
> 					   (TYPE_MODE (TREE_TYPE (type0))), 0);
>diff --git gcc/cp/typeck.c gcc/cp/typeck.c
>index 472b41b..ffa9ed4 100644
>--- gcc/cp/typeck.c
>+++ gcc/cp/typeck.c
>@@ -4813,6 +4813,18 @@ 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);
>+	    }
>+
> 	  /* Always construct signed integer vector type.  */
> 	  intt = c_common_type_for_size (GET_MODE_BITSIZE
> 					   (TYPE_MODE (TREE_TYPE (type0))), 0);
>diff --git gcc/testsuite/c-c++-common/vector-compare-4.c
>gcc/testsuite/c-c++-common/vector-compare-4.c
>index e69de29..14faf04 100644
>--- gcc/testsuite/c-c++-common/vector-compare-4.c
>+++ gcc/testsuite/c-c++-common/vector-compare-4.c
>@@ -0,0 +1,41 @@
>+/* PR c/68062 */
>+/* { dg-do compile } */
>+
>+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);
>+  v += (a != b);
>+  v += (a >= b);
>+  v += (a <= b);
>+  v += (a > b);
>+  v += (a < b);
>+
>+  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);
>+  v += (a != b);
>+  v += (a >= b);
>+  v += (a <= b);
>+  v += (a > b);
>+  v += (a < b);
>+
>+  return v;
>+}
>
>	Marek
diff mbox

Patch

diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index 952041b..b646451 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -11048,6 +11048,18 @@  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);
+	    }
+
           /* Always construct signed integer vector type.  */
           intt = c_common_type_for_size (GET_MODE_BITSIZE
 					   (TYPE_MODE (TREE_TYPE (type0))), 0);
@@ -11201,6 +11213,18 @@  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);
+	    }
+
           /* Always construct signed integer vector type.  */
           intt = c_common_type_for_size (GET_MODE_BITSIZE
 					   (TYPE_MODE (TREE_TYPE (type0))), 0);
diff --git gcc/cp/typeck.c gcc/cp/typeck.c
index 472b41b..ffa9ed4 100644
--- gcc/cp/typeck.c
+++ gcc/cp/typeck.c
@@ -4813,6 +4813,18 @@  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);
+	    }
+
 	  /* Always construct signed integer vector type.  */
 	  intt = c_common_type_for_size (GET_MODE_BITSIZE
 					   (TYPE_MODE (TREE_TYPE (type0))), 0);
diff --git gcc/testsuite/c-c++-common/vector-compare-4.c gcc/testsuite/c-c++-common/vector-compare-4.c
index e69de29..14faf04 100644
--- gcc/testsuite/c-c++-common/vector-compare-4.c
+++ gcc/testsuite/c-c++-common/vector-compare-4.c
@@ -0,0 +1,41 @@ 
+/* PR c/68062 */
+/* { dg-do compile } */
+
+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);
+  v += (a != b);
+  v += (a >= b);
+  v += (a <= b);
+  v += (a > b);
+  v += (a < b);
+
+  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);
+  v += (a != b);
+  v += (a >= b);
+  v += (a <= b);
+  v += (a > b);
+  v += (a < b);
+
+  return v;
+}