public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C/C++ PATCH] Don't emit invalid VEC_COND_EXPR for vector comparisons (PR c/68062)
@ 2016-01-13 15:27 Marek Polacek
  2016-01-13 18:53 ` Joseph Myers
  2016-01-13 19:03 ` Richard Biener
  0 siblings, 2 replies; 15+ messages in thread
From: Marek Polacek @ 2016-01-13 15:27 UTC (permalink / raw)
  To: GCC Patches; +Cc: Richard Biener, Jason Merrill, Joseph Myers

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.

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2016-03-02 12:45 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-13 15:27 [C/C++ PATCH] Don't emit invalid VEC_COND_EXPR for vector comparisons (PR c/68062) Marek Polacek
2016-01-13 18:53 ` Joseph Myers
2016-01-13 19:56   ` Marek Polacek
2016-01-13 23:12     ` Joseph Myers
2016-01-20 11:31       ` Marek Polacek
2016-01-27  7:26         ` Marek Polacek
2016-01-27 17:02           ` Jeff Law
2016-01-27 17:45             ` Marek Polacek
2016-01-28 12:47               ` H.J. Lu
2016-01-28 19:33                 ` Marek Polacek
2016-03-02 11:43                   ` Richard Biener
2016-03-02 12:11                     ` Jakub Jelinek
2016-03-02 12:17                       ` Marek Polacek
2016-03-02 12:45                       ` Richard Biener
2016-01-13 19:03 ` Richard Biener

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).