]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: move fixVariadicCall from walk to order
authorMatthew Dempsky <mdempsky@google.com>
Tue, 21 Apr 2020 22:59:11 +0000 (15:59 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Thu, 23 Apr 2020 18:29:42 +0000 (18:29 +0000)
This CL moves fixVariadicCall from mid-Walk of function calls to
early-Order, in preparation for moving it even earlier in the future.

Notably, rewriting variadic calls this early introduces two
compilation output changes:

1. Previously, Order visited the ODDDARG before the rest of the
arguments list, whereas the natural time to visit it is at the end of
the list (as we visit arguments left-to-right, and the ... argument is
the rightmost one). Changing this ordering permutes the autotmp
allocation order, which in turn permutes autotmp naming and stack
offsets.

2. Previously, Walk separately walked all of the variadic arguments
before walking the entire slice literal, whereas the more natural
thing to do is just walk the entire slice literal. This triggers
slightly different code paths for composite literal construction in
some cases.

Neither of these have semantic impact. They simply mean we're now
compiling f(a,b,c) the same way as we were already compiling
f([]T{a,b,c}...).

Change-Id: I40ccc5725697a116370111ebe746b2639562fe87
Reviewed-on: https://go-review.googlesource.com/c/go/+/229601
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
src/cmd/compile/internal/gc/order.go
src/cmd/compile/internal/gc/walk.go

index dc37f97ff7b27003231431342d0abd479818e1e3..28c3677dbefd6b84fc256eed952d14cd5342d69e 100644 (file)
@@ -407,11 +407,20 @@ func (o *Order) call(n *Node) {
                // Caller should have already called o.init(n).
                Fatalf("%v with unexpected ninit", n.Op)
        }
+
+       // Builtin functions.
+       if n.Op != OCALLFUNC && n.Op != OCALLMETH && n.Op != OCALLINTER {
+               n.Left = o.expr(n.Left, nil)
+               n.Right = o.expr(n.Right, nil)
+               o.exprList(n.List)
+               return
+       }
+
+       fixVariadicCall(n)
        n.Left = o.expr(n.Left, nil)
-       n.Right = o.expr(n.Right, nil) // ODDDARG temp
        o.exprList(n.List)
 
-       if n.Op != OCALLFUNC && n.Op != OCALLMETH {
+       if n.Op == OCALLINTER {
                return
        }
        keepAlive := func(arg *Node) {
@@ -429,12 +438,12 @@ func (o *Order) call(n *Node) {
        // Check for "unsafe-uintptr" tag provided by escape analysis.
        for i, param := range n.Left.Type.Params().FieldSlice() {
                if param.Note == unsafeUintptrTag || param.Note == uintptrEscapesTag {
-                       if param.IsDDD() && !n.IsDDD() {
-                               for _, arg := range n.List.Slice()[i:] {
-                                       keepAlive(arg)
+                       if arg := n.List.Index(i); arg.Op == OSLICELIT {
+                               for _, elt := range arg.List.Slice() {
+                                       keepAlive(elt)
                                }
                        } else {
-                               keepAlive(n.List.Index(i))
+                               keepAlive(arg)
                        }
                }
        }
@@ -1208,13 +1217,7 @@ func (o *Order) expr(n, lhs *Node) *Node {
                }
 
        case ODDDARG:
-               if n.Transient() {
-                       // The ddd argument does not live beyond the call it is created for.
-                       // Allocate a temporary that will be cleaned up when this statement
-                       // completes. We could be more aggressive and try to arrange for it
-                       // to be cleaned up when the call completes.
-                       prealloc[n] = o.newTemp(n.Type.Elem(), false)
-               }
+               Fatalf("unreachable")
 
        case ODOTTYPE, ODOTTYPE2:
                n.Left = o.expr(n.Left, nil)
index fb8f1873d2300b67f5941c0dcb98f63d80268eca..8adaabc69446f79186a1f237bfc622a517c21006 100644 (file)
@@ -1736,7 +1736,7 @@ func mkdotargslice(typ *types.Type, args []*Node) *Node {
 
 // fixVariadicCall rewrites calls to variadic functions to use an
 // explicit ... argument if one is not already present.
-func fixVariadicCall(call *Node, init *Nodes) {
+func fixVariadicCall(call *Node) {
        fntype := call.Left.Type
        if !fntype.IsVariadic() || call.IsDDD() {
                return
@@ -1754,13 +1754,9 @@ func fixVariadicCall(call *Node, init *Nodes) {
 
        if ddd := call.Right; ddd != nil && slice.Op == OSLICELIT {
                slice.Esc = ddd.Esc
-               if prealloc[ddd] != nil {
-                       prealloc[slice] = prealloc[ddd] // temporary to use
-               }
+               slice.SetTransient(ddd.Transient())
        }
 
-       slice = walkexpr(slice, init)
-
        call.List.Set(append(args[:vi], slice))
        call.SetIsDDD(true)
 }
@@ -1770,13 +1766,12 @@ func walkCall(n *Node, init *Nodes) {
                return // already walked
        }
 
-       n.Left = walkexpr(n.Left, init)
-       walkexprlist(n.List.Slice(), init)
-       fixVariadicCall(n, init)
-
        params := n.Left.Type.Params()
        args := n.List.Slice()
 
+       n.Left = walkexpr(n.Left, init)
+       walkexprlist(args, init)
+
        // If this is a method call, add the receiver at the beginning of the args.
        if n.Op == OCALLMETH {
                withRecv := make([]*Node, len(args)+1)