Hi Vladimir,
This change introduces a regression for arm-none-eabi (Cortex-M0 and
Cortex-M0+ verified). LRA appears not to converge when compiling
g++.dg/gcov/gcov-22.C with -O0 -fpath-coverage.
I confirmed that r17-5745-ga8b927a09c7931 completes successfully,
whereas r17-5746-gbb77ebb8b6a4ae never finishes. Cortex-M0 and
Cortex-M0+ both use Thumb-1, with a restricted set of registers
available to many instructions.
Testing gcov/gcov-22.C, -std=gnu++20
doing compile
Executing on host: /build/r17-5746-gbb77ebb8b6a4ae/bin/arm-none-eabi-g++
/build/gcc_src/gcc/testsuite/g++.dg/gcov/gcov-22.C -mthumb -march=armv6s-m
-mcpu=cortex-m0 -mfloat-abi=soft -mfpu=auto -fdiagnostics-plain-output
-fmessage-length=0 -std=gnu++20 -fpath-coverage -S -o gcov-22.s
(timeout = 800)
spawn -ignore SIGHUP /build/r17-5746-gbb77ebb8b6a4ae/bin/arm-none-eabi-g++
/build/gcc_src/gcc/testsuite/g++.dg/gcov/gcov-22.C -mthumb -march=armv6s-m
-mcpu=cortex-m0 -mfloat-abi=soft -mfpu=auto -fdiagnostics-plain-output
-fmessage-length=0 -std=gnu++20 -fpath-coverage -S -o gcov-22.s
pid is 3768 -3768
WARNING: program timed out
doing kill, pid is 3768
pid is 3768
close result is 3768 exp7 0 0 CHILDKILLED SIGINT interrupt
output is status 1
compiler exited with status 1
The script below prints the LRA constraint iteration and next register
number at entry to lra_constraints, restricted to loop_setjmp_continue.
The register number reflects cumulative register creation, not the
number of live registers. The script deliberately terminates
compilation at iteration 30.
python
class LRACounts(gdb.Breakpoint):
def stop(self):
name = gdb.parse_and_eval(
"(char *)cfun->decl->decl_minimal.name->identifier.id.str"
).string()
if name != "loop_setjmp_continue":
return False
iteration = int(gdb.parse_and_eval("lra_constraint_iter")) + 1
next_regno = int(gdb.parse_and_eval("x_rtl.emit.x_reg_rtx_no"))
print("LRA_COUNT,%d,%d" % (iteration, next_regno), flush=True)
if iteration >= 30:
gdb.execute("quit")
return False
LRACounts("lra_constraints")
end
run
$ /build/r17-5745-ga8b927a09c7931/bin/arm-none-eabi-g++
/build/gcc_src/gcc/testsuite/g++.dg/gcov/gcov-22.C -mthumb -mcpu=cortex-m0
-std=gnu++20 -O0 -fpath-coverage -S -wrapper
gdb,-batch,-x,/build/gdb-commands,--args
Breakpoint 1 at 0xe1d460: file /build/gcc_src/gcc/gcc/lra-constraints.cc, line
5995.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
LRA_COUNT,1,373
LRA_COUNT,2,628
LRA_COUNT,3,628
LRA_COUNT,4,1065
LRA_COUNT,5,1071
LRA_COUNT,6,1071
LRA_COUNT,7,1488
LRA_COUNT,8,1489
LRA_COUNT,9,1489
LRA_COUNT,10,2069
LRA_COUNT,11,2088
LRA_COUNT,12,2088
LRA_COUNT,13,2734
LRA_COUNT,14,2735
LRA_COUNT,15,2735
LRA_COUNT,16,3446
[Inferior 1 (process 3580159) exited normally]
$ /build/r17-5746-gbb77ebb8b6a4ae/bin/arm-none-eabi-g++
/build/gcc_src/gcc/testsuite/g++.dg/gcov/gcov-22.C -mthumb -mcpu=cortex-m0
-std=gnu++20 -O0 -fpath-coverage -S -wrapper
gdb,-batch,-x,/build/gdb-commands,--args
Breakpoint 1 at 0xe1d4b0: file /build/gcc_src/gcc/gcc/lra-constraints.cc, line
6010.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
LRA_COUNT,1,373
LRA_COUNT,2,628
LRA_COUNT,3,628
LRA_COUNT,4,1065
LRA_COUNT,5,1071
LRA_COUNT,6,1071
LRA_COUNT,7,1488
LRA_COUNT,8,1489
LRA_COUNT,9,1489
LRA_COUNT,10,2059
LRA_COUNT,11,2078
LRA_COUNT,12,2078
LRA_COUNT,13,2712
LRA_COUNT,14,2715
LRA_COUNT,15,2715
LRA_COUNT,16,3403
LRA_COUNT,17,3404
LRA_COUNT,18,3404
LRA_COUNT,19,4121
LRA_COUNT,20,4128
LRA_COUNT,21,4128
LRA_COUNT,22,4872
LRA_COUNT,23,4872
LRA_COUNT,24,5625
LRA_COUNT,25,6057
LRA_COUNT,26,6057
LRA_COUNT,27,7806
LRA_COUNT,28,8307
LRA_COUNT,29,8307
LRA_COUNT,30,11427
The parent revision completes after 16 LRA constraint iterations. With
r17-5746-gbb77ebb8b6a4ae, register creation continues through
iteration 30, where the script stops compilation.
Can you please look into why the register allocation never converges?
Kind regards,
Torbjörn
On 2026-10-29 19:41, Vladimir Makarov wrote:
The attached patch solves
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128174
The patch was successfully bootstrapped and tested on x86_64 and aarch64.
pr128174.patch
commit bb77ebb8b6a4ae850fd74c7969909dba72d5360c
Author: Vladimir N. Makarov <[email protected]>
Date: Thu Oct 29 14:36:37 2026 -0400
[PR 128174][LRA]: Propagate invariants (equivalence) for inherited pseudos
In the PR test case we have too many insns using invariants (sfp +
constant) and for all of them after sfp elimination the offset is out
of immediate range. So all the insns are reloaded. Invariant
inheritance creates a lot of inheritance pseudos, some of them should
be spilled (thumb1 has few hard regs) and this results in increase of
stack frame making insns containing sfp to be reloaded again and
results in additional inheritance. This process is extremely slow and
requires generation of many reload insns.
To fix this, we record that the inheritance pseudos have equivalence
and use the equivalence instead of inheritance pseudo spilling.
Spilling would be a pessimization anyway (reloading from the slot is
no cheaper than rematerialization). Also the extra slot would change
the frame size and therefore the elimination offsets, which would
invalidate every insn mentioning an eliminable register. With many
invariant occurrences that feedback would keep LRA iterating for a
very long time.
gcc/ChangeLog:
PR middle-end/128174
* lra-constraints.cc (add_invariant): New function.
(process_invariant_for_inheritance): Call add_invariant.
diff --git a/gcc/lra-constraints.cc b/gcc/lra-constraints.cc
index 2aa3b296f49..40bdd26b376 100644
--- a/gcc/lra-constraints.cc
+++ b/gcc/lra-constraints.cc
@@ -608,6 +608,21 @@ get_equiv_with_elimination (rtx x, rtx_insn *insn)
return res;
}
+/* Record invariant INVARIANT_RTX as an equivalence of REG. */
+static void
+add_invariant (rtx reg, rtx invariant_rtx)
+{
+ int regno = REGNO (reg);
+
+ ira_expand_reg_equiv ();
+ ira_reg_equiv[regno].defined_p = true;
+ ira_reg_equiv[regno].profitable_p = true;
+ if (CONSTANT_P (invariant_rtx))
+ ira_reg_equiv[regno].constant = copy_rtx (invariant_rtx);
+ else
+ ira_reg_equiv[regno].invariant = copy_rtx (invariant_rtx);
+}
+
/* Set up curr_operand_mode. */
static void
init_curr_operand_mode (void)
@@ -7272,6 +7287,7 @@ process_invariant_for_inheritance (rtx dst_reg, rtx
invariant_rtx)
"invariant inheritance");
bitmap_set_bit (&lra_inheritance_pseudos, REGNO (new_reg));
bitmap_set_bit (&check_only_regs, REGNO (new_reg));
+ add_invariant (new_reg, invariant_rtx);
lra_reg_info[REGNO (new_reg)].restore_rtx = PATTERN (insn);
start_sequence ();
lra_emit_move (new_reg, dst_reg);