Skip to content

Commit 4c334e0

Browse files
committed
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 <[email protected]> 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.
1 parent 702a11a commit 4c334e0

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

gcc/cp/constexpr.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4198,9 +4198,16 @@ cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
41984198
if (*non_constant_p)
41994199
return t;
42004200

4201-
if (TREE_CODE (whole) == VECTOR_CST)
4202-
return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
4203-
TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
4201+
if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
4202+
{
4203+
if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
4204+
TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
4205+
return r;
4206+
if (!ctx->quiet)
4207+
error ("%qE is not a constant expression", orig_whole);
4208+
*non_constant_p = true;
4209+
return t;
4210+
}
42044211

42054212
start = TREE_OPERAND (t, 2);
42064213
istart = tree_to_shwi (start);

gcc/testsuite/g++.dg/pr105871.C

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// PR c++/105871
2+
// { dg-do compile }
3+
// { dg-options "-Wno-psabi" }
4+
5+
typedef __attribute__((__vector_size__ ( 1))) unsigned char U;
6+
typedef __attribute__((__vector_size__ (16))) unsigned char V;
7+
8+
U
9+
foo (void)
10+
{
11+
return __builtin_shufflevector ((U){}, (V){}, 0);
12+
}

0 commit comments

Comments
 (0)