aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/ssa/loopbce.go
diff options
context:
space:
mode:
authorJorropo <jorropo.pgm@gmail.com>2023-07-25 16:19:10 +0200
committerGopher Robot <gobot@golang.org>2023-07-31 18:33:29 +0000
commitbac4e2f241ca8df3d5be6ddf83214b9a681f4086 (patch)
tree6fcd6b62c19354d7e6ba9496a764976982db0c16 /src/cmd/compile/internal/ssa/loopbce.go
parent8613ef81e676bda77f6413a587e68c3dcc5b03ae (diff)
downloadgo-bac4e2f241ca8df3d5be6ddf83214b9a681f4086.tar.xz
cmd/compile: try to rewrite loops to count down
Fixes #61629 This reduce the pressure on regalloc because then the loop only keep alive one value (the iterator) instead of the iterator and the upper bound since the comparison now acts against an immediate, often zero which can be skipped. This optimize things like: for i := 0; i < n; i++ { Or a range over a slice where the index is not used: for _, v := range someSlice { Or the new range over int from #61405: for range n { It is hit in 975 unique places while doing ./make.bash. Change-Id: I5facff8b267a0b60ea3c1b9a58c4d74cdb38f03f Reviewed-on: https://go-review.googlesource.com/c/go/+/512935 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Jorropo <jorropo.pgm@gmail.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Keith Randall <khr@golang.org> Auto-Submit: Keith Randall <khr@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/ssa/loopbce.go')
-rw-r--r--src/cmd/compile/internal/ssa/loopbce.go8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/cmd/compile/internal/ssa/loopbce.go b/src/cmd/compile/internal/ssa/loopbce.go
index b7dfaa33e3..3dbd7350ae 100644
--- a/src/cmd/compile/internal/ssa/loopbce.go
+++ b/src/cmd/compile/internal/ssa/loopbce.go
@@ -13,12 +13,14 @@ import (
type indVarFlags uint8
const (
- indVarMinExc indVarFlags = 1 << iota // minimum value is exclusive (default: inclusive)
- indVarMaxInc // maximum value is inclusive (default: exclusive)
+ indVarMinExc indVarFlags = 1 << iota // minimum value is exclusive (default: inclusive)
+ indVarMaxInc // maximum value is inclusive (default: exclusive)
+ indVarCountDown // if set the iteration starts at max and count towards min (default: min towards max)
)
type indVar struct {
ind *Value // induction variable
+ nxt *Value // the incremented variable
min *Value // minimum value, inclusive/exclusive depends on flags
max *Value // maximum value, inclusive/exclusive depends on flags
entry *Block // entry block in the loop.
@@ -277,6 +279,7 @@ func findIndVar(f *Func) []indVar {
if !inclusive {
flags |= indVarMinExc
}
+ flags |= indVarCountDown
step = -step
}
if f.pass.debug >= 1 {
@@ -285,6 +288,7 @@ func findIndVar(f *Func) []indVar {
iv = append(iv, indVar{
ind: ind,
+ nxt: nxt,
min: min,
max: max,
entry: b.Succs[0].b,