public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc(refs/vendors/ARM/heads/morello)] cp: Fix folding of REPLACE_ADDRESS_VALUE
@ 2022-05-20 12:34 Alex Coplan
  0 siblings, 0 replies; only message in thread
From: Alex Coplan @ 2022-05-20 12:34 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:291268154c24266ac510e8606b0466d56ff1a8d5

commit 291268154c24266ac510e8606b0466d56ff1a8d5
Author: Alex Coplan <alex.coplan@arm.com>
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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-05-20 12:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-20 12:34 [gcc(refs/vendors/ARM/heads/morello)] cp: Fix folding of REPLACE_ADDRESS_VALUE Alex Coplan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).