summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2022-06-09 17:42:31 +0200
committerJakub Jelinek <jakub@redhat.com>2022-06-19 12:08:34 +0200
commit0ddeeb11e45b9b7e9ebc18292a42769304bf3e44 (patch)
tree5b2889c158213d3ea6af730a0f19032dcfaa7880
parentDaily bump. (diff)
downloadgcc-0ddeeb11e45b9b7e9ebc18292a42769304bf3e44.tar.gz
gcc-0ddeeb11e45b9b7e9ebc18292a42769304bf3e44.tar.bz2
gcc-0ddeeb11e45b9b7e9ebc18292a42769304bf3e44.tar.xz
c++: Fix up ICE on __builtin_shufflevector constexpr evaluation [PR105871]
As the following testcase shows, BIT_FIELD_REF result doesn't have to have just integral type, it can also have vector type. And in that case cxx_eval_bit_field_ref just ICEs on it because it is unprepared for that case, creates the initial value with build_int_cst (sure, that one could be easily replaced with build_zero_cst) and then expects it can through shifts, ands and ors come up with the final value, but that doesn't work for vectors. We already call fold_ternary if whole is a VECTOR_CST, this patch does the same if the result doesn't have integral type. And, there is no guarantee fold_ternary will succeed and the callers certainly don't expect NULL being returned, so it also diagnoses those as non-constant and returns original t in that case. 2022-06-09 Jakub Jelinek <jakub@redhat.com> PR c++/105871 * constexpr.cc (cxx_eval_bit_field_ref): For BIT_FIELD_REF with non-integral result type use fold_ternary too like for BIT_FIELD_REFs from VECTOR_CST. If fold_ternary returns NULL, diagnose non-constant expression, set *non_constant_p and return t, instead of returning NULL. * g++.dg/pr105871.C: New test. (cherry picked from commit 4c334e0e4ff05d3a7ca9ebb832428c446cd0ae8d)
-rw-r--r--gcc/cp/constexpr.cc13
-rw-r--r--gcc/testsuite/g++.dg/pr105871.C12
2 files changed, 22 insertions, 3 deletions
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 1e0d13e6785..1c17f0d14b8 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -4181,9 +4181,16 @@ cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
4181 if (*non_constant_p) 4181 if (*non_constant_p)
4182 return t; 4182 return t;
4183 4183
4184 if (TREE_CODE (whole) == VECTOR_CST) 4184 if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
4185 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, 4185 {
4186 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)); 4186 if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
4187 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
4188 return r;
4189 if (!ctx->quiet)
4190 error ("%qE is not a constant expression", orig_whole);
4191 *non_constant_p = true;
4192 return t;
4193 }
4187 4194
4188 start = TREE_OPERAND (t, 2); 4195 start = TREE_OPERAND (t, 2);
4189 istart = tree_to_shwi (start); 4196 istart = tree_to_shwi (start);
diff --git a/gcc/testsuite/g++.dg/pr105871.C b/gcc/testsuite/g++.dg/pr105871.C
new file mode 100644
index 00000000000..3623c1d1366
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr105871.C
@@ -0,0 +1,12 @@
1// PR c++/105871
2// { dg-do compile }
3// { dg-options "-Wno-psabi" }
4
5typedef __attribute__((__vector_size__ ( 1))) unsigned char U;
6typedef __attribute__((__vector_size__ (16))) unsigned char V;
7
8U
9foo (void)
10{
11 return __builtin_shufflevector ((U){}, (V){}, 0);
12}