r16-160-ge6f89d78c1a752 made a change to simplify-rtx that exposes an
existing latent bug in gen_lowpart_if_possible, which widens a MEM with
byte_lowpart_offset. That offset is negative on big-endian targets,
so on m68k a byte load from foo becomes a longword load from foo-3,
causing an address error.

Based on how gen_lowpart_for_combine refuses to widen a MEM, this patch
makes gen_lowpart_if_possible return NULL_RTX for a paradoxical lowpart.

Bootstrapped and regtested on x86_64-pc-linux-gnu.  Also tested on
m68k-elf with a Musashi-based 68000 simulator, where it fixes address
errors in 20040709-2.c, 20040709-3.c, pr57875.c and pr79737-1.c at
several optimization levels, with no regressions.

gcc/ChangeLog:

        PR rtl-optimization/125728
        * rtlhooks.cc (gen_lowpart_if_possible): Return NULL_RTX for a
        paradoxical lowpart of a MEM instead of widening the access.

gcc/testsuite/ChangeLog:

        PR rtl-optimization/125728
        * gcc.target/m68k/pr125728.c: New test.

Signed-off-by: Andrew DeRosier <[email protected]>
---
 gcc/rtlhooks.cc                          |  3 +++
 gcc/testsuite/gcc.target/m68k/pr125728.c | 23 +++++++++++++++++++++++
 2 files changed, 26 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/m68k/pr125728.c

diff --git a/gcc/rtlhooks.cc b/gcc/rtlhooks.cc
index a236a6e..d6464f9 100644
--- a/gcc/rtlhooks.cc
+++ b/gcc/rtlhooks.cc
@@ -115,6 +115,9 @@ gen_lowpart_if_possible (machine_mode mode, rtx x)
   else if (MEM_P (x))
     {
       /* This is the only other case we handle.  */
+      if (paradoxical_subreg_p (mode, GET_MODE (x)))
+       return 0;
+
       poly_int64 offset = byte_lowpart_offset (mode, GET_MODE (x));
       rtx new_rtx = adjust_address_nv (x, mode, offset);
       if (! memory_address_addr_space_p (mode, XEXP (new_rtx, 0),
diff --git a/gcc/testsuite/gcc.target/m68k/pr125728.c 
b/gcc/testsuite/gcc.target/m68k/pr125728.c
new file mode 100644
index 0000000..b31d9b8
--- /dev/null
+++ b/gcc/testsuite/gcc.target/m68k/pr125728.c
@@ -0,0 +1,23 @@
+/* PR rtl-optimization/125728 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -m68000" } */
+/* { dg-final { scan-assembler-not "foo-3" } } */
+/* { dg-final { scan-assembler-not "osc\\+9" } } */
+
+extern unsigned char foo;
+extern const int table[8];
+
+int
+index_byte (void)
+{
+  return table[foo & 7];
+}
+
+extern unsigned char osc[66] __attribute__ ((aligned (2)));
+extern unsigned char lani __attribute__ ((aligned (2)));
+
+unsigned short
+sum_byte (void)
+{
+  return (unsigned short) lani * 16 + (osc[10] & 15);
+}
-- 
2.56.0

Reply via email to