From d681270714bc91b28a2cd97c3eae138b3112ff1d Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Wed, 14 May 2025 16:00:25 -0700 Subject: cmd/compile: allow load-op merging in additional situations x += *p We want to do this with a single load+add operation on amd64. The tricky part is that we don't want to combine if there are other uses of x after this instruction. Implement a simple detector that seems to capture a common situation - x += *p is in a loop, and the other use of x is after loop exit. In that case, it does not hurt to do the load+add combo. Change-Id: I466174cce212e78bde83f908cc1f2752b560c49c Reviewed-on: https://go-review.googlesource.com/c/go/+/672957 Reviewed-by: Keith Randall Reviewed-by: David Chase LUCI-TryBot-Result: Go LUCI --- test/codegen/memcombine.go | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'test/codegen') diff --git a/test/codegen/memcombine.go b/test/codegen/memcombine.go index 9ede80132c..fa0e902ac2 100644 --- a/test/codegen/memcombine.go +++ b/test/codegen/memcombine.go @@ -396,6 +396,15 @@ func load_op_no_merge(p, q *int) { } } +func load_op_in_loop(a []int) int { + r := 0 + for _, x := range a { + // amd64:`ADDQ\t\([A-Z]+\)\([A-Z]+\*8\), [A-Z]+` + r += x + } + return r +} + // Make sure offsets are folded into loads and stores. func offsets_fold(_, a [20]byte) (b [20]byte) { // arm64:`MOVD\tcommand-line-arguments\.a\+[0-9]+\(FP\), R[0-9]+`,`MOVD\tR[0-9]+, command-line-arguments\.b\+[0-9]+\(FP\)` -- cgit v1.3