Hi!

VEC_COND_EXPR is handled in constexpr code like COND_EXPR, but that is
wrong.  VEC_COND_EXPR is more like an arbitrary arithmetics ternary
operation, we need to compute all 3 arguments and based on the elements of
the first argument pick up elements of 2nd and 3rd arguments.
The COND_EXPR handling just checks the whole first argument, not its
elements, and only evaluates one of the arguments.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk and release branches?

2017-11-20   Jakub Jelinek  <ja...@redhat.com>

        PR c++/82781
        * constexpr.c (cxx_eval_vector_conditional_expression): New function.
        (cxx_eval_constant_expression) <case VEC_COND_EXPR>: Use it instead
        of cxx_eval_conditional_expression.

        * g++.dg/ext/constexpr-pr82781.C: New test.

--- gcc/cp/constexpr.c.jj       2017-11-14 09:06:48.000000000 +0100
+++ gcc/cp/constexpr.c  2017-11-16 19:43:16.133854557 +0100
@@ -2086,6 +2086,41 @@ cxx_eval_conditional_expression (const c
                                       jump_target);
 }
 
+/* Subroutine of cxx_eval_constant_expression.
+   Attempt to evaluate vector condition expressions.  */
+
+static tree
+cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
+                                       bool *non_constant_p, bool *overflow_p)
+{
+  tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
+                                           /*lval*/false,
+                                           non_constant_p, overflow_p);
+  VERIFY_CONSTANT (arg1);
+  tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
+                                           /*lval*/false,
+                                           non_constant_p, overflow_p);
+  VERIFY_CONSTANT (arg2);
+  tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
+                                           /*lval*/false,
+                                           non_constant_p, overflow_p);
+  VERIFY_CONSTANT (arg3);
+  location_t loc = EXPR_LOCATION (t);
+  tree type = TREE_TYPE (t);
+  tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
+  if (r == NULL_TREE)
+    {
+      if (arg1 == TREE_OPERAND (t, 0)
+         && arg2 == TREE_OPERAND (t, 1)
+         && arg3 == TREE_OPERAND (t, 2))
+       r = t;
+      else
+       r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
+    }
+  VERIFY_CONSTANT (r);
+  return r;
+}
+
 /* Returns less than, equal to, or greater than zero if KEY is found to be
    less than, to match, or to be greater than the constructor_elt's INDEX.  */
 
@@ -4398,12 +4433,14 @@ cxx_eval_constant_expression (const cons
                                              jump_target);
          break;
        }
-      /* FALLTHRU */
-    case VEC_COND_EXPR:
       r = cxx_eval_conditional_expression (ctx, t, lval,
                                           non_constant_p, overflow_p,
                                           jump_target);
       break;
+    case VEC_COND_EXPR:
+      r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
+                                                 overflow_p);
+      break;
 
     case CONSTRUCTOR:
       if (TREE_CONSTANT (t))
--- gcc/testsuite/g++.dg/ext/constexpr-pr82781.C.jj     2017-11-16 
19:47:27.547966754 +0100
+++ gcc/testsuite/g++.dg/ext/constexpr-pr82781.C        2017-11-16 
19:46:21.000000000 +0100
@@ -0,0 +1,12 @@
+// PR c++/82781
+// { dg-do compile { target c++11 } }
+
+typedef int V __attribute__ ((vector_size (16)));
+constexpr V b1 = { 0, 1, 10, 20 };
+constexpr V b2 = { 0, 2, 10, 0 };
+constexpr V b3 = b1 == b2;
+
+static_assert (b3[0] == -1, "");
+static_assert (b3[1] == 0, "");
+static_assert (b3[2] == -1, "");
+static_assert (b3[3] == 0, "");

        Jakub

Reply via email to