re PR c++/82781 (Vector extension operators return wrong result in constexpr)
authorJakub Jelinek <jakub@redhat.com>
Mon, 20 Nov 2017 12:57:50 +0000 (13:57 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Mon, 20 Nov 2017 12:57:50 +0000 (13:57 +0100)
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.

From-SVN: r254952

gcc/cp/ChangeLog
gcc/cp/constexpr.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/ext/constexpr-pr82781.C [new file with mode: 0644]

index 410b18b42fd8599855a9c90a024789738c4ef6ee..6f4d30e1dfbe2177d5d5fc65d95c94545b7aebfb 100644 (file)
@@ -1,3 +1,10 @@
+2017-11-20   Jakub Jelinek  <jakub@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.
+
 2017-11-19  Jakub Jelinek  <jakub@redhat.com>
 
        PR c/66618
index d6b6843e8048dca0a6ad35f8f76fa45e5280efc9..5eac64bd8a3e5967c48e2833a9d2522a58154cad 100644 (file)
@@ -2086,6 +2086,45 @@ cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
                                       jump_target);
 }
 
+/* Subroutine of cxx_eval_constant_expression.
+   Attempt to evaluate vector condition expressions.  Unlike
+   cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
+   ternary arithmetics operation, where all 3 arguments have to be
+   evaluated as constants and then folding computes the result from
+   them.  */
+
+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 +4437,14 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
                                              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))
index c6b860c6ce97c5aaae412ce6d222311d398d6907..6148d4b5384c687c0594fbed6523d1003bf720c8 100644 (file)
@@ -1,3 +1,8 @@
+2017-11-20   Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/82781
+       * g++.dg/ext/constexpr-pr82781.C: New test.
+
 2017-11-20  Christophe Lyon  <christophe.lyon@linaro.org>
 
        * gcc.target/arm/pr69180.c: Use -: for no column in expected
diff --git a/gcc/testsuite/g++.dg/ext/constexpr-pr82781.C b/gcc/testsuite/g++.dg/ext/constexpr-pr82781.C
new file mode 100644 (file)
index 0000000..eee0159
--- /dev/null
@@ -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, "");