The following makes sure we are not losing address-space info
when expanding a __builtin_memcpy (synthesized by gimplification,
which _might_ be the other actual problem).  The issue is with
get_memory_rtx which is also used by other builtin expansions
but is not aware of address-spaces.  The following fixes that
which resolves an ICE with AVR and properly dispatches to
inline expansion then in emit_block_move_hints.

The alternative, to not intoduce memcpy by gimplification is
possible but IL verification rejects WITH_SIZE_EXPR as part
of GIMPLE assignments, removing that restriction also "works"
(for the testcase).

Alternative is in [2/2].

Sofar untested, will test on x86_64 - in principle I'd prefer 2/2.

Richard.

        PR middle-end/112830
        * builtins.cc (get_memory_rtx): Make address-space aware.

        * gcc.target/avr/pr112830.c: New testcase.
---
 gcc/builtins.cc                         | 10 +++++++---
 gcc/testsuite/gcc.target/avr/pr112830.c | 11 +++++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/avr/pr112830.c

diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 4fc58a0bda9..05c50b782d7 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 (!ADDR_SPACE_GENERIC_P (as))
+    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)),
diff --git a/gcc/testsuite/gcc.target/avr/pr112830.c 
b/gcc/testsuite/gcc.target/avr/pr112830.c
new file mode 100644
index 00000000000..576107b9909
--- /dev/null
+++ b/gcc/testsuite/gcc.target/avr/pr112830.c
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+
+typedef __SIZE_TYPE__ size_t;
+
+void copy_n (void *vdst, const __memx void *vsrc, size_t n)
+{
+    typedef struct { char a[n]; } T;
+    T *dst = (T*) vdst;
+    const __memx T *src = (const __memx T*) vsrc;
+    *dst = *src;
+}
-- 
2.35.3

Reply via email to