From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7810) id D51B43858D28; Tue, 17 Jan 2023 10:47:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D51B43858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1673952430; bh=5cV8rfsjCJityE0WTZ7/tT9BkZYtXAteD6+RnfUXi3w=; h=From:To:Subject:Date:From; b=e+Ou6XzsJqAkrZCPQuk5lVOtG76l10fEchzabJa2hyN7aAPtv6VAFKoLk3Av0OfNc oZ7TJxi+iV+PDE0TySM8MWw7+9ptF3TQUfF0h9VKoVp/8+M+OJlJ3d2sJ3tXT5n3G0 HAOpHVRgJGWmUbUuQ7ymYrHQdhVkomXGy9BoeSzI= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Alex Coplan To: gcc-cvs@gcc.gnu.org Subject: [gcc(refs/vendors/ARM/heads/morello)] c: Ensure intcap save_expr operands are folded X-Act-Checkin: gcc X-Git-Author: Alex Coplan X-Git-Refname: refs/vendors/ARM/heads/morello X-Git-Oldrev: f557c7a51be5621ee9f7cd6930140c20a181f8fe X-Git-Newrev: aa8dde4799379b90301785bf0beff2c20935eacc Message-Id: <20230117104710.D51B43858D28@sourceware.org> Date: Tue, 17 Jan 2023 10:47:10 +0000 (GMT) List-Id: https://gcc.gnu.org/g:aa8dde4799379b90301785bf0beff2c20935eacc commit aa8dde4799379b90301785bf0beff2c20935eacc Author: Alex Coplan Date: Mon Dec 12 18:17:10 2022 +0000 c: Ensure intcap save_expr operands are folded The commment above c_fully_fold notes the following requirement: Function arguments have already been folded before calling this function, as have the contents of SAVE_EXPR, TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and C_MAYBE_CONST_EXPR. When we recently introduced save_exprs to avoid duplicate side-effects when forming .REPLACE_ADDRESS_VALUEs in the C front-end, we didn't take this requirement into account. As the testcase added with the patch shows, this leads to c_maybe_const_expr nodes escaping the C front-end, and subsequent ICEs in the gimplifier. This patch fixes the issue by calling c_fully_fold prior to calling save_expr on intcap operands. Diff: --- gcc/c/c-typeck.c | 6 +++--- gcc/testsuite/c-c++-common/torture/intptr-fold.c | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 1fcaa7dbebb..817b8ac844b 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -4401,7 +4401,7 @@ build_unary_op (location_t location, enum tree_code code, tree xarg, default: intcap = unary_op_get_intcap_provenance (arg); if (arg == intcap) - intcap = arg = save_expr (arg); + intcap = arg = save_expr (c_fully_fold (arg, false, NULL)); arg = drop_capability (arg); } } @@ -11970,9 +11970,9 @@ build_binary_op_1 (location_t location, enum tree_code code, c + cv --> REPLACE_ADDRESS_VALUE (c, (non-cap type) c + cv) we need to avoid evaluating c twice. */ if (intcap == op0) - intcap = op0 = save_expr (op0); + intcap = op0 = save_expr (c_fully_fold (op0, false, NULL)); else if (intcap == op1) - intcap = op1 = save_expr (op1); + intcap = op1 = save_expr (c_fully_fold (op1, false, NULL)); if (common_ic_type) { diff --git a/gcc/testsuite/c-c++-common/torture/intptr-fold.c b/gcc/testsuite/c-c++-common/torture/intptr-fold.c new file mode 100644 index 00000000000..f62b398ae17 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/intptr-fold.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* This would ICE in the gimplifier on capability targets due to + c_maybe_const_expr escaping the C front-end. */ +typedef __INTPTR_TYPE__ T; +typedef struct { + T a; +} S; +T f(T x) { + return -(x ? ((S *)x)->a : x); +}