rtlhooks: Don't widen a MEM in gen_lowpart_if_possible [PR125728]

Message ID UKLdwyvPY-zGlJUe3VfgFr6scXKvmA6hQhpu76J3f54dkFq3PZuSUC_1S2Yxg2hAnHCnS7Jju3U2R1rSOHmmMFjlmaJ9xKzQJujiymuk7tw=@skychase.zone
State New
Headers
Series rtlhooks: Don't widen a MEM in gen_lowpart_if_possible [PR125728] |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap success Build passed

Commit Message

Andrew DeRosier Nov. 4, 2026, 3:35 p.m. UTC
  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 <andy@skychase.zone>
---
 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
  

Comments

Jeff Law Nov. 10, 2026, 4:16 p.m. UTC | #1
On 11/4/26 8:35 AM, Andrew DeRosier wrote:
> 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 <andy@skychase.zone>
Changes in these routines are always tricky, so before even thinking 
hard about the patch I threw it into my tester and multiple targets are 
throwing errors during self-test.  So I don't *think* you need a full 
cross environment.  You should be able to configure a cross compiler for 
one of these targets and try to build.  What you'll be looking for is 
something like this:





cc1: internal compiler error: in gen_rtx_SUBREG, at emit-rtl.cc:1062
0x1f4939d internal_error(char const*, ...)
     ../../..//gcc/gcc/diagnostic-global-context.cc:787
0x892d3d fancy_abort(char const*, int, char const*)
     ../../..//gcc/gcc/diagnostics/context.cc:1813
0x661e02 gen_rtx_SUBREG(machine_mode, rtx_def*, poly_int<1u, unsigned long>)
     ../../..//gcc/gcc/emit-rtl.cc:1062
0x661e02 gen_rtx_SUBREG(machine_mode, rtx_def*, poly_int<1u, unsigned long>)
     ../../..//gcc/gcc/emit-rtl.cc:1060
0xbaa785 test_output_operand_modifier_operand
     ../../..//gcc/gcc/final.cc:4798
0xbaa785 selftest::final_cc_tests()
     ../../..//gcc/gcc/final.cc:4820
0x1eaabbb selftest::run_tests()
     ../../..//gcc/gcc/selftest-run-tests.cc:109
0xff471a toplev::run_self_tests()
     ../../..//gcc/gcc/toplev.cc:2287

This is from H8 I think, but the others I spot checked had similar 
behavior (microblaze-linux, moxie-elf, iq2000-elf, h8300-elf).

Jeff

Jeff
  
Andrea Pinski Nov. 10, 2026, 6:14 p.m. UTC | #2
On Tue, Nov 10, 2026 at 8:20 AM Jeff Law <jeffrey.law@oss.qualcomm.com> wrote:
>
>
>
> On 11/4/26 8:35 AM, Andrew DeRosier wrote:
> > 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 <andy@skychase.zone>
> Changes in these routines are always tricky, so before even thinking
> hard about the patch I threw it into my tester and multiple targets are
> throwing errors during self-test.  So I don't *think* you need a full
> cross environment.  You should be able to configure a cross compiler for
> one of these targets and try to build.  What you'll be looking for is
> something like this:
>
>
>
>
>
> cc1: internal compiler error: in gen_rtx_SUBREG, at emit-rtl.cc:1062
> 0x1f4939d internal_error(char const*, ...)
>      ../../..//gcc/gcc/diagnostic-global-context.cc:787
> 0x892d3d fancy_abort(char const*, int, char const*)
>      ../../..//gcc/gcc/diagnostics/context.cc:1813
> 0x661e02 gen_rtx_SUBREG(machine_mode, rtx_def*, poly_int<1u, unsigned long>)
>      ../../..//gcc/gcc/emit-rtl.cc:1062
> 0x661e02 gen_rtx_SUBREG(machine_mode, rtx_def*, poly_int<1u, unsigned long>)
>      ../../..//gcc/gcc/emit-rtl.cc:1060
> 0xbaa785 test_output_operand_modifier_operand
>      ../../..//gcc/gcc/final.cc:4798
> 0xbaa785 selftest::final_cc_tests()
>      ../../..//gcc/gcc/final.cc:4820
> 0x1eaabbb selftest::run_tests()
>      ../../..//gcc/gcc/selftest-run-tests.cc:109
> 0xff471a toplev::run_self_tests()
>      ../../..//gcc/gcc/toplev.cc:2287
>
> This is from H8 I think, but the others I spot checked had similar
> behavior (microblaze-linux, moxie-elf, iq2000-elf, h8300-elf).

Yes I was very much worried about fallout here even. I did try to warn
about this fallout in
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125728#c5 but I could
have been more verbose there.

Thanks,
Andrea

>
> Jeff
>
> Jeff
  

Patch

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);
+}