]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: when inlining ==, don’t take the address of the values
authorJosh Bleecher Snyder <josharian@gmail.com>
Fri, 15 Apr 2016 22:49:00 +0000 (15:49 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Thu, 25 Aug 2016 17:51:10 +0000 (17:51 +0000)
This CL reworks walkcompare for clarity and concision.
It also makes one significant functional change.
(The functional change is hard to separate cleanly
from the cleanup, so I just did them together.)
When inlining and unrolling an equality comparison
for a small struct or array, compare the elements like:

a[0] == b[0] && a[1] == b[1]

rather than

pa := &a
pb := &b
pa[0] == pb[0] && pa[1] == pb[1]

The result is the same, but taking the address
and working through the indirect
forces the backends to generate less efficient code.

This is only an improvement with the SSA backend.
However, every port but s390x now has a working
SSA backend, and switching to the SSA backend
by default everywhere is a priority for Go 1.8.
It thus seems reasonable to start to prioritize
SSA performance over the old backend.

Updates #15303

Sample code:

type T struct {
a, b int8
}

func g(a T) bool {
return a == T{1, 2}
}

SSA before:

"".g t=1 size=80 args=0x10 locals=0x8
0x0000 00000 (badeq.go:7) TEXT "".g(SB), $8-16
0x0000 00000 (badeq.go:7) SUBQ $8, SP
0x0004 00004 (badeq.go:7) FUNCDATA $0, gclocals·23e8278e2b69a3a75fa59b23c49ed6ad(SB)
0x0004 00004 (badeq.go:7) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0004 00004 (badeq.go:8) MOVBLZX "".a+16(FP), AX
0x0009 00009 (badeq.go:8) MOVB AL, "".autotmp_0+6(SP)
0x000d 00013 (badeq.go:8) MOVBLZX "".a+17(FP), AX
0x0012 00018 (badeq.go:8) MOVB AL, "".autotmp_0+7(SP)
0x0016 00022 (badeq.go:8) MOVB $0, "".autotmp_1+4(SP)
0x001b 00027 (badeq.go:8) MOVB $1, "".autotmp_1+4(SP)
0x0020 00032 (badeq.go:8) MOVB $2, "".autotmp_1+5(SP)
0x0025 00037 (badeq.go:8) MOVBLZX "".autotmp_0+6(SP), AX
0x002a 00042 (badeq.go:8) MOVBLZX "".autotmp_1+4(SP), CX
0x002f 00047 (badeq.go:8) CMPB AL, CL
0x0031 00049 (badeq.go:8) JNE 70
0x0033 00051 (badeq.go:8) MOVBLZX "".autotmp_0+7(SP), AX
0x0038 00056 (badeq.go:8) CMPB AL, $2
0x003a 00058 (badeq.go:8) SETEQ AL
0x003d 00061 (badeq.go:8) MOVB AL, "".~r1+24(FP)
0x0041 00065 (badeq.go:8) ADDQ $8, SP
0x0045 00069 (badeq.go:8) RET
0x0046 00070 (badeq.go:8) MOVB $0, AL
0x0048 00072 (badeq.go:8) JMP 61

SSA after:

"".g t=1 size=32 args=0x10 locals=0x0
0x0000 00000 (badeq.go:7) TEXT "".g(SB), $0-16
0x0000 00000 (badeq.go:7) NOP
0x0000 00000 (badeq.go:7) NOP
0x0000 00000 (badeq.go:7) FUNCDATA $0, gclocals·23e8278e2b69a3a75fa59b23c49ed6ad(SB)
0x0000 00000 (badeq.go:7) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
0x0000 00000 (badeq.go:8) MOVBLZX "".a+8(FP), AX
0x0005 00005 (badeq.go:8) CMPB AL, $1
0x0007 00007 (badeq.go:8) JNE 25
0x0009 00009 (badeq.go:8) MOVBLZX "".a+9(FP), CX
0x000e 00014 (badeq.go:8) CMPB CL, $2
0x0011 00017 (badeq.go:8) SETEQ AL
0x0014 00020 (badeq.go:8) MOVB AL, "".~r1+16(FP)
0x0018 00024 (badeq.go:8) RET
0x0019 00025 (badeq.go:8) MOVB $0, AL
0x001b 00027 (badeq.go:8) JMP 20

Change-Id: I120185d58012b7bbcdb1ec01225b5b08d0855d86
Reviewed-on: https://go-review.googlesource.com/22277
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/walk.go
test/fixedbugs/issue15303.go [new file with mode: 0644]

index 1ba2f7ba4be5743faad91d68ab58831eb740a902..8173a2e0cba396b6650ea820ebf7cd849d7747d7 100644 (file)
@@ -3178,14 +3178,17 @@ func walkcompare(n *Node, init *Nodes) *Node {
 
        // Must be comparison of array or struct.
        // Otherwise back end handles it.
+       // While we're here, decide whether to
+       // inline or call an eq alg.
        t := n.Left.Type
-
+       var inline bool
        switch t.Etype {
        default:
                return n
-
-       case TARRAY, TSTRUCT:
-               break
+       case TARRAY:
+               inline = t.NumElem() <= 1 || (t.NumElem() <= 4 && issimple[t.Elem().Etype])
+       case TSTRUCT:
+               inline = t.NumFields() <= 4
        }
 
        cmpl := n.Left
@@ -3201,103 +3204,75 @@ func walkcompare(n *Node, init *Nodes) *Node {
                Fatalf("arguments of comparison must be lvalues - %v %v", cmpl, cmpr)
        }
 
-       l = temp(Ptrto(t))
-       a := Nod(OAS, l, Nod(OADDR, cmpl, nil))
-       a.Right.Etype = 1 // addr does not escape
-       a = typecheck(a, Etop)
-       init.Append(a)
-
-       r = temp(Ptrto(t))
-       a = Nod(OAS, r, Nod(OADDR, cmpr, nil))
-       a.Right.Etype = 1 // addr does not escape
-       a = typecheck(a, Etop)
-       init.Append(a)
+       // Chose not to inline. Call equality function directly.
+       if !inline {
+               // eq algs take pointers
+               pl := temp(Ptrto(t))
+               al := Nod(OAS, pl, Nod(OADDR, cmpl, nil))
+               al.Right.Etype = 1 // addr does not escape
+               al = typecheck(al, Etop)
+               init.Append(al)
+
+               pr := temp(Ptrto(t))
+               ar := Nod(OAS, pr, Nod(OADDR, cmpr, nil))
+               ar.Right.Etype = 1 // addr does not escape
+               ar = typecheck(ar, Etop)
+               init.Append(ar)
+
+               var needsize int
+               call := Nod(OCALL, eqfor(t, &needsize), nil)
+               call.List.Append(pl)
+               call.List.Append(pr)
+               if needsize != 0 {
+                       call.List.Append(Nodintconst(t.Width))
+               }
+               res := call
+               if n.Op != OEQ {
+                       res = Nod(ONOT, res, nil)
+               }
+               n = finishcompare(n, res, init)
+               return n
+       }
 
-       var andor Op = OANDAND
+       // inline: build boolean expression comparing element by element
+       andor := OANDAND
        if n.Op == ONE {
                andor = OOROR
        }
-
        var expr *Node
-       if t.Etype == TARRAY && t.NumElem() <= 4 && issimple[t.Elem().Etype] {
-               // Four or fewer elements of a basic type.
-               // Unroll comparisons.
-               var li *Node
-               var ri *Node
-               for i := 0; int64(i) < t.NumElem(); i++ {
-                       li = Nod(OINDEX, l, Nodintconst(int64(i)))
-                       ri = Nod(OINDEX, r, Nodintconst(int64(i)))
-                       a = Nod(n.Op, li, ri)
-                       if expr == nil {
-                               expr = a
-                       } else {
-                               expr = Nod(andor, expr, a)
-                       }
-               }
-
+       compare := func(el, er *Node) {
+               a := Nod(n.Op, el, er)
                if expr == nil {
-                       expr = Nodbool(n.Op == OEQ)
-               }
-               n = finishcompare(n, expr, init)
-               return n
-       }
-
-       if t.Etype == TARRAY {
-               // Zero- or single-element array, of any type.
-               switch t.NumElem() {
-               case 0:
-                       n = finishcompare(n, Nodbool(n.Op == OEQ), init)
-                       return n
-               case 1:
-                       l0 := Nod(OINDEX, l, Nodintconst(0))
-                       r0 := Nod(OINDEX, r, Nodintconst(0))
-                       a := Nod(n.Op, l0, r0)
-                       n = finishcompare(n, a, init)
-                       return n
+                       expr = a
+               } else {
+                       expr = Nod(andor, expr, a)
                }
        }
-
-       if t.IsStruct() && t.NumFields() <= 4 {
-               // Struct of four or fewer fields.
-               // Inline comparisons.
-               var li *Node
-               var ri *Node
-               for _, t1 := range t.Fields().Slice() {
-                       if isblanksym(t1.Sym) {
+       cmpl = safeexpr(cmpl, init)
+       cmpr = safeexpr(cmpr, init)
+       if t.IsStruct() {
+               for _, f := range t.Fields().Slice() {
+                       sym := f.Sym
+                       if isblanksym(sym) {
                                continue
                        }
-                       li = NodSym(OXDOT, l, t1.Sym)
-                       ri = NodSym(OXDOT, r, t1.Sym)
-                       a = Nod(n.Op, li, ri)
-                       if expr == nil {
-                               expr = a
-                       } else {
-                               expr = Nod(andor, expr, a)
-                       }
+                       compare(
+                               NodSym(OXDOT, cmpl, sym),
+                               NodSym(OXDOT, cmpr, sym),
+                       )
                }
-
-               if expr == nil {
-                       expr = Nodbool(n.Op == OEQ)
+       } else {
+               for i := 0; int64(i) < t.NumElem(); i++ {
+                       compare(
+                               Nod(OINDEX, cmpl, Nodintconst(int64(i))),
+                               Nod(OINDEX, cmpr, Nodintconst(int64(i))),
+                       )
                }
-               n = finishcompare(n, expr, init)
-               return n
        }
-
-       // Chose not to inline. Call equality function directly.
-       var needsize int
-       call := Nod(OCALL, eqfor(t, &needsize), nil)
-
-       call.List.Append(l)
-       call.List.Append(r)
-       if needsize != 0 {
-               call.List.Append(Nodintconst(t.Width))
+       if expr == nil {
+               expr = Nodbool(n.Op == OEQ)
        }
-       r = call
-       if n.Op != OEQ {
-               r = Nod(ONOT, r, nil)
-       }
-
-       n = finishcompare(n, r, init)
+       n = finishcompare(n, expr, init)
        return n
 }
 
diff --git a/test/fixedbugs/issue15303.go b/test/fixedbugs/issue15303.go
new file mode 100644 (file)
index 0000000..c8dfa30
--- /dev/null
@@ -0,0 +1,24 @@
+// run
+
+// Copyright 2016 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.
+
+// Ensure that inlined struct/array comparisons have the right side-effects.
+
+package main
+
+import "os"
+
+func main() {
+       var x int
+       f := func() (r [4]int) {
+               x++
+               return
+       }
+       _ = f() == f()
+       if x != 2 {
+               println("f evaluated ", x, " times, want 2")
+               os.Exit(1)
+       }
+}