From 681ba4307781a1f1581bda3c52255186e4b79546 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Tue, 21 Apr 2020 15:59:11 -0700 Subject: [PATCH] cmd/compile: move fixVariadicCall from walk to order 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 TryBot-Result: Gobot Gobot Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/gc/order.go | 29 +++++++++++++++------------- src/cmd/compile/internal/gc/walk.go | 15 +++++--------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/cmd/compile/internal/gc/order.go b/src/cmd/compile/internal/gc/order.go index dc37f97ff7b..28c3677dbef 100644 --- a/src/cmd/compile/internal/gc/order.go +++ b/src/cmd/compile/internal/gc/order.go @@ -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) diff --git a/src/cmd/compile/internal/gc/walk.go b/src/cmd/compile/internal/gc/walk.go index fb8f1873d23..8adaabc6944 100644 --- a/src/cmd/compile/internal/gc/walk.go +++ b/src/cmd/compile/internal/gc/walk.go @@ -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)