https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112830

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |rguenth at gcc dot 
gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
Note we end up trying to expand a memcpy between different address spaces
and that does

  addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
  mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr)); 

which assumes the default address-space.  The get_memory_rtx () function
also doesn't correctly preserve the address-space for the MEM_EXPR.

The following fixes the ICE and tries to correct the MEM_EXPR.  But I wonder
if the actual code generation is sane?  It also appears we'd eventually
emit a call to memcpy as we call emit_block_move_hints with BLOCK_OP_NORMAL
which sets may_use_call to 1.

So, can you check whether we generate correct code with the following patch?
Otherwise we should keep it ICEing (wrong-code is IMHO worse).

diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 4fc58a0bda9..244863699be 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -1353,7 +1353,8 @@ get_memory_rtx (tree exp, tree len)
     exp = TREE_OPERAND (exp, 0);

   addr = expand_expr (orig_exp, NULL_RTX, ptr_mode, EXPAND_NORMAL);
-  mem = gen_rtx_MEM (BLKmode, memory_address (BLKmode, addr));
+  addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
+  mem = gen_rtx_MEM (BLKmode, memory_address_addr_space (BLKmode, addr, as));

   /* Get an expression we can use to find the attributes to assign to MEM.
      First remove any nops.  */
@@ -1363,8 +1364,11 @@ get_memory_rtx (tree exp, tree len)

   /* Build a MEM_REF representing the whole accessed area as a byte blob,
      (as builtin stringops may alias with anything).  */
+  tree ctype = char_type_node;
+  if (as != ADDR_SPACE_GENERIC)
+    ctype = build_qualified_type (ctype, ENCODE_QUAL_ADDR_SPACE (as));
   exp = fold_build2 (MEM_REF,
-                    build_array_type (char_type_node,
+                    build_array_type (ctype,
                                       build_range_type (sizetype,
                                                         size_one_node, len)),
                     exp, build_int_cst (ptr_type_node, 0));
@@ -1381,7 +1385,7 @@ get_memory_rtx (tree exp, tree len)
       unsigned int align = get_pointer_alignment (TREE_OPERAND (exp, 0));
       exp = build_fold_addr_expr (base);
       exp = fold_build2 (MEM_REF,
-                        build_array_type (char_type_node,
+                        build_array_type (ctype,
                                           build_range_type (sizetype,
                                                             size_zero_node,
                                                             NULL)),

Reply via email to