From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id E91FE3857BB8; Sun, 19 Jun 2022 10:08:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E91FE3857BB8 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-8492] c++: Fix up ICE on __builtin_shufflevector constexpr evaluation [PR105871] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 3e43eeee1686e05bfbdde4b7ff923f2bc949c88e X-Git-Newrev: 0ddeeb11e45b9b7e9ebc18292a42769304bf3e44 Message-Id: <20220619100846.E91FE3857BB8@sourceware.org> Date: Sun, 19 Jun 2022 10:08:46 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 19 Jun 2022 10:08:47 -0000 https://gcc.gnu.org/g:0ddeeb11e45b9b7e9ebc18292a42769304bf3e44 commit r12-8492-g0ddeeb11e45b9b7e9ebc18292a42769304bf3e44 Author: Jakub Jelinek Date: Thu Jun 9 17:42:31 2022 +0200 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 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) Diff: --- gcc/cp/constexpr.cc | 13 ++++++++++--- gcc/testsuite/g++.dg/pr105871.C | 12 ++++++++++++ 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, if (*non_constant_p) return t; - if (TREE_CODE (whole) == VECTOR_CST) - return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, - TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)); + if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t))) + { + if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole, + TREE_OPERAND (t, 1), TREE_OPERAND (t, 2))) + return r; + if (!ctx->quiet) + error ("%qE is not a constant expression", orig_whole); + *non_constant_p = true; + return t; + } start = TREE_OPERAND (t, 2); 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 @@ +// PR c++/105871 +// { dg-do compile } +// { dg-options "-Wno-psabi" } + +typedef __attribute__((__vector_size__ ( 1))) unsigned char U; +typedef __attribute__((__vector_size__ (16))) unsigned char V; + +U +foo (void) +{ + return __builtin_shufflevector ((U){}, (V){}, 0); +}