]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: don't preload registers if destination already scheduled
authorkhr@golang.org <khr@golang.org>
Wed, 14 May 2025 00:53:45 +0000 (17:53 -0700)
committerKeith Randall <khr@golang.org>
Tue, 20 May 2025 00:13:21 +0000 (17:13 -0700)
In regalloc, we allocate some values to registers before loop entry,
so that they don't need to be loaded (from spill locations) during
the loop.

But it is pointless if we've already regalloc'd the loop body.
Whatever restores we needed for the body are already generated.

It's not clear if this code is ever useful. No tests fail if I just
remove it. But at least this change is worthwhile. It doesn't help,
and it actively inserts more restores than we really need (mostly
because the desired register list is approximate - I have seen cases
where the loads implicated here end up being dead because the restores
hit the wrong registers and the edge shuffle pass knows it needs
the restores in different registers).

While we are here, might as well have layoutRegallocOrder return
the standard layout order instead of recomputing it.

Change-Id: Ia624d5121de59b6123492603695de50b272b277f
Reviewed-on: https://go-review.googlesource.com/c/go/+/672735
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: David Chase <drchase@google.com>
src/cmd/compile/internal/ssa/layout.go
src/cmd/compile/internal/ssa/regalloc.go

index e4a8c6ffbf0dde5d4958c177642547216c1f2cdf..927287dc77ec410f851f1669a0f5b98c43e9f7e7 100644 (file)
@@ -15,7 +15,7 @@ func layout(f *Func) {
 // imposed by the linear-scan algorithm.
 func layoutRegallocOrder(f *Func) []*Block {
        // remnant of an experiment; perhaps there will be another.
-       return layoutOrder(f)
+       return f.Blocks
 }
 
 func layoutOrder(f *Func) []*Block {
index 2981bceb2e6910e330aceb9def501eb8f3072260..f1e210fe9b8b085150eb92759d9e51b3b48d4dc7 100644 (file)
@@ -1899,6 +1899,10 @@ func (s *regAllocState) regalloc(f *Func) {
                        if s.f.Config.hasGReg && s.regs[s.GReg].v != nil {
                                s.freeReg(s.GReg) // Spill value in G register before any merge.
                        }
+                       if s.blockOrder[b.ID] > s.blockOrder[b.Succs[0].b.ID] {
+                               // No point if we've already regalloc'd the destination.
+                               goto badloop
+                       }
                        // For this to be worthwhile, the loop must have no calls in it.
                        top := b.Succs[0].b
                        loop := s.loopnest.b2l[top.ID]