From 9ce27feaeb91b2f30ff8cbe3be1ece3071f3f6b2 Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Fri, 28 Oct 2022 14:14:24 -0700 Subject: cmd/compile: add rule for post-decomposed growslice optimization The recently added rule only works before decomposing slices. Add a rule that works after decomposing slices. The reason we need the latter is because although the length may be a constant, it can be hidden inside a slice that is not constant (its pointer or capacity might be changing). By applying this optimization after decomposing slices, we can find more cases where it applies. Fixes #56440 Change-Id: I0094e59eee3065ab4d210defdda8227a6e897420 Reviewed-on: https://go-review.googlesource.com/c/go/+/446277 Run-TryBot: Keith Randall Reviewed-by: Cherry Mui TryBot-Result: Gopher Robot Reviewed-by: Keith Randall --- test/codegen/issue56440.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'test/codegen') diff --git a/test/codegen/issue56440.go b/test/codegen/issue56440.go index 36b52ace03..c6c1e66789 100644 --- a/test/codegen/issue56440.go +++ b/test/codegen/issue56440.go @@ -16,3 +16,19 @@ func f(x []int) int { // amd64:`MOVQ\t40\(.*\),` return x[len(s)] } + +func g(x []int, p *bool) int { + s := make([]int, 3) + for { + s = s[:3] + if cap(s) < 5 { + s = make([]int, 3, 5) + } + s = append(s, 4, 5) + if *p { + // amd64:`MOVQ\t40\(.*\),` + return x[len(s)] + } + } + return 0 +} -- cgit v1.3