]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
lra: Fix computing reg class for hard register constraints [PR121198]
authorStefan Schulze Frielinghaus <stefansf@gcc.gnu.org>
Thu, 30 Oct 2025 12:50:46 +0000 (13:50 +0100)
committerStefan Schulze Frielinghaus <stefansf@gcc.gnu.org>
Thu, 30 Oct 2025 12:50:46 +0000 (13:50 +0100)
Currently the register class derived from a hard register constraint is
solely determined from a single register.  This even works for register
pairs if all the required registers are contained in this very register
class and falls apart if not.  For example:

long
test (void)
{
  long x;
  __asm__ ("..." : "={r22}" (x));
  return x;
}

For AVR -mmcu=atmega8, variable `x` requires a register quadruple and
the minimal class for single register r22 is SIMPLE_LD_REGS which itself
entails registers r16 up to r23.  However, variable `x` is bound to
registers r22 up to r25.  Thus, the minimal class containing those is
LD_REGS.  Therefore, compute the least upper bound of all register
classes over all required registers.

PR rtl-optimization/121198

gcc/ChangeLog:

* lra-constraints.cc (process_alt_operands): Compute least upper
bound of all register classes over all required registers in
order to determine register class for a hard register constraint.

gcc/testsuite/ChangeLog:

* gcc.target/avr/pr121198.c: New test.

gcc/lra-constraints.cc
gcc/testsuite/gcc.target/avr/pr121198.c [new file with mode: 0644]

index b152997917455029b2341a0fc7574fb67386361f..48ce75781d4bd23979095f090c899ed7cef199a5 100644 (file)
@@ -2552,7 +2552,10 @@ process_alt_operands (int only_alternative)
                    {
                      int regno = decode_hard_reg_constraint (p);
                      gcc_assert (regno >= 0);
-                     cl = REGNO_REG_CLASS (regno);
+                     cl = NO_REGS;
+                     int nregs = hard_regno_nregs (regno, mode);
+                     for (int i = 0; i < nregs; ++i)
+                       cl = reg_class_superunion[cl][REGNO_REG_CLASS (regno + i)];
                      CLEAR_HARD_REG_SET (hard_reg_constraint);
                      SET_HARD_REG_BIT (hard_reg_constraint, regno);
                      cl_filter = &hard_reg_constraint;
diff --git a/gcc/testsuite/gcc.target/avr/pr121198.c b/gcc/testsuite/gcc.target/avr/pr121198.c
new file mode 100644 (file)
index 0000000..551247e
--- /dev/null
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-Os -mmcu=atmega8" } */
+
+long
+test (void)
+{
+  long x;
+  __asm__ ("" : "={r22}" (x));
+  return x;
+}