From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7810) id 87D55382E29A; Fri, 20 May 2022 12:34:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 87D55382E29A 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)] cp: Fix folding of REPLACE_ADDRESS_VALUE X-Act-Checkin: gcc X-Git-Author: Alex Coplan X-Git-Refname: refs/vendors/ARM/heads/morello X-Git-Oldrev: 68c1db55253b24240615350f80479bc20bc6cc03 X-Git-Newrev: 291268154c24266ac510e8606b0466d56ff1a8d5 Message-Id: <20220520123411.87D55382E29A@sourceware.org> Date: Fri, 20 May 2022 12:34:11 +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: Fri, 20 May 2022 12:34:11 -0000 https://gcc.gnu.org/g:291268154c24266ac510e8606b0466d56ff1a8d5 commit 291268154c24266ac510e8606b0466d56ff1a8d5 Author: Alex Coplan Date: Fri Mar 18 11:21:59 2022 +0000 cp: Fix folding of REPLACE_ADDRESS_VALUE For code such as (char *)1 in the C++ frontend, we were producing the following IR: .REPLACE_ADDRESS_VALUE(0B, 1) but this should be folded to a ponter-typed INTEGER_CST with value 1. This was causing various problems including e.g. an ICE at expand time when we see the IFN in a context where we don't expect it. The reason for the lack of folding is that when the C++ frontend sees (char *)1, it wraps the INTEGER_CST 1 in a NON_LVALUE_EXPR. Prior to this change, fold_build_replace_address_value_loc didn't know to look inside such a tree code, so failed to do the expected folding. To remedy this, we teach fold_build_replace_address_value_loc to strip location wrappers on its operands (like various other fold routines), and adjust cp_cap_from_noncap to ensure we preserve any location wrappers that might get stripped in order to do the folding. This follows existing practice in other conversion routines in the C++ frontend (see e.g. convert_to_pointer_maybe_fold). gcc/cp/ChangeLog: * cvt.c (cp_cap_from_noncap): Call preserve_any_location_wrapper to restore location wrappers that might get stripped from constant folding. gcc/ChangeLog: * fold-const.c (fold_build_replace_address_value_loc): Call STRIP_ANY_LOCATION_WRAPPER on the operands. Diff: --- gcc/cp/cvt.c | 9 ++++++--- gcc/fold-const.c | 12 +++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c index fe18bd0c3db..83ec3aff0b3 100644 --- a/gcc/cp/cvt.c +++ b/gcc/cp/cvt.c @@ -52,9 +52,12 @@ cp_cap_from_noncap (tree type, tree expr, tsubst_flags_t complain) expr = cp_convert (noncapability_type (type), expr, complain); location_t loc = EXPR_LOCATION (expr); - return fold_build_replace_address_value_loc (loc, - build_int_cst (type, 0), - expr); + tree result + = fold_build_replace_address_value_loc (loc, + build_int_cst (type, 0), + expr); + + return preserve_any_location_wrapper (result, expr); } /* Change of width--truncation and extension of integers or reals-- diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 5f1db095cf6..07c54a87bba 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -13576,6 +13576,12 @@ fold_build_replace_address_value_loc (location_t loc, tree c, tree cv) gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (cv)) && TYPE_PRECISION (TREE_TYPE (cv)) <= TYPE_PRECISION (noncapability_type (TREE_TYPE (c)))); + + tree orig_c = c; + tree orig_cv = cv; + STRIP_ANY_LOCATION_WRAPPER (c); + STRIP_ANY_LOCATION_WRAPPER (cv); + if (TREE_CODE (c) == INTEGER_CST && TREE_CODE (cv) == INTEGER_CST) { tree cap_type = TREE_TYPE (c); @@ -13602,7 +13608,7 @@ fold_build_replace_address_value_loc (location_t loc, tree c, tree cv) { gcc_assert (TREE_TYPE (c) == TREE_TYPE (CALL_EXPR_ARG (c, 0))); return fold_build_replace_address_value_loc (loc, - CALL_EXPR_ARG (c, 0), cv); + CALL_EXPR_ARG (c, 0), orig_cv); } /* If the capability C is an INTEGER_CST or a REPLACE_ADDRESS_VALUE inside a NOP conversion, allow this function to recurse and re-apply the @@ -13615,9 +13621,9 @@ fold_build_replace_address_value_loc (location_t loc, tree c, tree cv) && types_compatible_p (noncapability_type (TREE_TYPE (TREE_OPERAND (c, 0))), TREE_TYPE (cv))) return convert (TREE_TYPE (c), fold_build_replace_address_value_loc (loc, - TREE_OPERAND (c, 0), cv)); + TREE_OPERAND (c, 0), orig_cv)); - return build_replace_address_value_loc (loc, c, cv); + return build_replace_address_value_loc (loc, orig_c, orig_cv); } tree