]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: support pointers to arrays in arrayClear
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Sun, 1 May 2022 17:25:16 +0000 (00:25 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Tue, 3 May 2022 05:42:48 +0000 (05:42 +0000)
Fixes #52635

Change-Id: I85f182931e30292983ef86c55a0ab6e01282395c
Reviewed-on: https://go-review.googlesource.com/c/go/+/403337
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/cmd/compile/internal/walk/range.go
test/codegen/issue52635.go [new file with mode: 0644]

index dcf7a786e76f2b21d2e6870dfc314007ce36cdca..6c30fa2877216f75b74529b4f423b843f0f2d79b 100644 (file)
@@ -403,8 +403,14 @@ func arrayClear(loop *ir.RangeStmt, v1, v2, a ir.Node) ir.Node {
                return nil
        }
        lhs := stmt.X.(*ir.IndexExpr)
+       x := lhs.X
+       if a.Type().IsPtr() && a.Type().Elem().IsArray() {
+               if s, ok := x.(*ir.StarExpr); ok && s.Op() == ir.ODEREF {
+                       x = s.X
+               }
+       }
 
-       if !ir.SameSafeExpr(lhs.X, a) || !ir.SameSafeExpr(lhs.Index, v1) {
+       if !ir.SameSafeExpr(x, a) || !ir.SameSafeExpr(lhs.Index, v1) {
                return nil
        }
 
diff --git a/test/codegen/issue52635.go b/test/codegen/issue52635.go
new file mode 100644 (file)
index 0000000..0e4d169
--- /dev/null
@@ -0,0 +1,36 @@
+// asmcheck
+
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Test that optimized range memclr works with pointers to arrays.
+
+package codegen
+
+type T struct {
+       a *[10]int
+       b [10]int
+}
+
+func (t *T) f() {
+       // amd64:".*runtime.memclrNoHeapPointers"
+       for i := range t.a {
+               t.a[i] = 0
+       }
+
+       // amd64:".*runtime.memclrNoHeapPointers"
+       for i := range *t.a {
+               t.a[i] = 0
+       }
+
+       // amd64:".*runtime.memclrNoHeapPointers"
+       for i := range t.a {
+               (*t.a)[i] = 0
+       }
+
+       // amd64:".*runtime.memclrNoHeapPointers"
+       for i := range *t.a {
+               (*t.a)[i] = 0
+       }
+}