]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.regabi] cmd/compile: remove prealloc map
authorRuss Cox <rsc@golang.org>
Thu, 17 Dec 2020 13:49:22 +0000 (08:49 -0500)
committerRuss Cox <rsc@golang.org>
Fri, 18 Dec 2020 17:52:54 +0000 (17:52 +0000)
The prealloc map seems to exist to avoid adding a field to all nodes.
Now we can add a field to just the nodes that need the field,
so let's do that and avoid having a magic global with extra node state
that isn't preserved by operations like Copy nor printed by Dump.

This also makes clear which nodes can be prealloc'ed.
In particular, the code in walkstmt looked up an entry in
prealloc using an ONAME node, but there's no code that
ever stores such an entry, so the lookup never succeeded.
Having fields makes that kind of thing easier to see and fix.

Passes buildall w/ toolstash -cmp.

Change-Id: I418ad0e2847615c08868120c13ee719dc0b2eacb
Reviewed-on: https://go-review.googlesource.com/c/go/+/278915
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/closure.go
src/cmd/compile/internal/gc/order.go
src/cmd/compile/internal/gc/range.go
src/cmd/compile/internal/gc/sinit.go
src/cmd/compile/internal/gc/walk.go
src/cmd/compile/internal/ir/expr.go
src/cmd/compile/internal/ir/stmt.go

index 6a3ee45a12353a853c2336225f5800a892aaf797..85c594787b3f6077761eb54b62f265ed3930acb4 100644 (file)
@@ -378,7 +378,7 @@ func closureType(clo ir.Node) *types.Type {
        return typ
 }
 
-func walkclosure(clo ir.Node, init *ir.Nodes) ir.Node {
+func walkclosure(clo *ir.ClosureExpr, init *ir.Nodes) ir.Node {
        fn := clo.Func()
 
        // If no closure vars, don't bother wrapping.
@@ -403,12 +403,12 @@ func walkclosure(clo ir.Node, init *ir.Nodes) ir.Node {
        cfn := convnop(addr, clo.Type())
 
        // non-escaping temp to use, if any.
-       if x := prealloc[clo]; x != nil {
+       if x := clo.Prealloc; x != nil {
                if !types.Identical(typ, x.Type()) {
                        panic("closure type does not match order's assigned type")
                }
                addr.SetRight(x)
-               delete(prealloc, clo)
+               clo.Prealloc = nil
        }
 
        return walkexpr(cfn, init)
@@ -552,12 +552,12 @@ func walkpartialcall(n *ir.CallPartExpr, init *ir.Nodes) ir.Node {
        cfn := convnop(addr, n.Type())
 
        // non-escaping temp to use, if any.
-       if x := prealloc[n]; x != nil {
+       if x := n.Prealloc; x != nil {
                if !types.Identical(typ, x.Type()) {
                        panic("partial call type does not match order's assigned type")
                }
                addr.SetRight(x)
-               delete(prealloc, n)
+               n.Prealloc = nil
        }
 
        return walkexpr(cfn, init)
index 174037e30a4a7bde27b7b77ce67d7ed6e6b54072..87d7cf3aa93d1e8bf58d1f98cee157b3e50594a4 100644 (file)
@@ -846,9 +846,9 @@ func (o *Order) stmt(n ir.Node) {
                        r := n.Right()
                        n.SetRight(o.copyExpr(r))
 
-                       // prealloc[n] is the temp for the iterator.
+                       // n.Prealloc is the temp for the iterator.
                        // hiter contains pointers and needs to be zeroed.
-                       prealloc[n] = o.newTemp(hiter(n.Type()), true)
+                       n.Prealloc = o.newTemp(hiter(n.Type()), true)
                }
                o.exprListInPlace(n.List())
                if orderBody {
@@ -1040,9 +1040,6 @@ func (o *Order) exprListInPlace(l ir.Nodes) {
        }
 }
 
-// prealloc[x] records the allocation to use for x.
-var prealloc = map[ir.Node]ir.Node{}
-
 func (o *Order) exprNoLHS(n ir.Node) ir.Node {
        return o.expr(n, nil)
 }
@@ -1079,11 +1076,12 @@ func (o *Order) expr1(n, lhs ir.Node) ir.Node {
        // Allocate a temporary to hold the strings.
        // Fewer than 5 strings use direct runtime helpers.
        case ir.OADDSTR:
+               n := n.(*ir.AddStringExpr)
                o.exprList(n.List())
 
                if n.List().Len() > 5 {
                        t := types.NewArray(types.Types[types.TSTRING], int64(n.List().Len()))
-                       prealloc[n] = o.newTemp(t, false)
+                       n.Prealloc = o.newTemp(t, false)
                }
 
                // Mark string(byteSlice) arguments to reuse byteSlice backing
@@ -1268,7 +1266,7 @@ func (o *Order) expr1(n, lhs ir.Node) ir.Node {
        case ir.OCLOSURE:
                n := n.(*ir.ClosureExpr)
                if n.Transient() && len(n.Func().ClosureVars) > 0 {
-                       prealloc[n] = o.newTemp(closureType(n), false)
+                       n.Prealloc = o.newTemp(closureType(n), false)
                }
                return n
 
@@ -1277,15 +1275,16 @@ func (o *Order) expr1(n, lhs ir.Node) ir.Node {
                n.SetLeft(o.expr(n.Left(), nil))
                if n.Transient() {
                        t := partialCallType(n)
-                       prealloc[n] = o.newTemp(t, false)
+                       n.Prealloc = o.newTemp(t, false)
                }
                return n
 
        case ir.OSLICELIT:
+               n := n.(*ir.CompLitExpr)
                o.exprList(n.List())
                if n.Transient() {
                        t := types.NewArray(n.Type().Elem(), ir.Int64Val(n.Right()))
-                       prealloc[n] = o.newTemp(t, false)
+                       n.Prealloc = o.newTemp(t, false)
                }
                return n
 
index 90bee4fc74fdf362f3c8e376719ff0937a10b11c..aa4f0358c99fb6b33f5acee8c907f52d11305c02 100644 (file)
@@ -296,7 +296,7 @@ func walkrange(nrange *ir.RangeStmt) ir.Node {
                // we only use a once, so no copy needed.
                ha := a
 
-               hit := prealloc[nrange]
+               hit := nrange.Prealloc
                th := hit.Type()
                keysym := th.Field(0).Sym  // depends on layout of iterator struct.  See reflect.go:hiter
                elemsym := th.Field(1).Sym // ditto
index e2c31e4dd73378d407843c79cc9339c2220a100c..7b710fd511eaa418c609735619a0b88e85b99a9f 100644 (file)
@@ -668,7 +668,7 @@ func slicelit(ctxt initContext, n *ir.CompLitExpr, var_ ir.Node, init *ir.Nodes)
 
        // set auto to point at new temp or heap (3 assign)
        var a ir.Node
-       if x := prealloc[n]; x != nil {
+       if x := n.Prealloc; x != nil {
                // temp allocated during order.go for dddarg
                if !types.Identical(t, x.Type()) {
                        panic("dotdotdot base type does not match order's assigned type")
index 23d1ce6003615d061d322c7f5bf8c6cbded8beec..a4ecc0c44dc091b57b2124add87d91dde5776ff7 100644 (file)
@@ -202,10 +202,7 @@ func walkstmt(n ir.Node) ir.Node {
                        if base.Flag.CompilingRuntime {
                                base.Errorf("%v escapes to heap, not allowed in runtime", v)
                        }
-                       if prealloc[v] == nil {
-                               prealloc[v] = callnew(v.Type())
-                       }
-                       nn := ir.Nod(ir.OAS, v.Name().Heapaddr, prealloc[v])
+                       nn := ir.Nod(ir.OAS, v.Name().Heapaddr, callnew(v.Type()))
                        nn.SetColas(true)
                        return walkstmt(typecheck(nn, ctxStmt))
                }
@@ -1638,7 +1635,7 @@ func walkexpr1(n ir.Node, init *ir.Nodes) ir.Node {
                return mkcall1(chanfn("chansend1", 2, n.Left().Type()), nil, init, n.Left(), n1)
 
        case ir.OCLOSURE:
-               return walkclosure(n, init)
+               return walkclosure(n.(*ir.ClosureExpr), init)
 
        case ir.OCALLPART:
                return walkpartialcall(n.(*ir.CallPartExpr), init)
@@ -2713,11 +2710,9 @@ func addstr(n *ir.AddStringExpr, init *ir.Nodes) ir.Node {
                fn = "concatstrings"
 
                t := types.NewSlice(types.Types[types.TSTRING])
-               slice := ir.Nod(ir.OCOMPLIT, nil, ir.TypeNode(t))
-               if prealloc[n] != nil {
-                       prealloc[slice] = prealloc[n]
-               }
-               slice.PtrList().Set(args[1:]) // skip buf arg
+               // args[1:] to skip buf arg
+               slice := ir.NewCompLitExpr(base.Pos, ir.OCOMPLIT, ir.TypeNode(t), args[1:])
+               slice.Prealloc = n.Prealloc
                args = []ir.Node{buf, slice}
                slice.SetEsc(EscNone)
        }
index b18975d0633789433d7d93ada6fed6f5049d9246..8f43eb0fb21cd03d196fb7899b0fa34645a3e811 100644 (file)
@@ -89,7 +89,8 @@ func toNtype(x Node) Ntype {
 // An AddStringExpr is a string concatenation Expr[0] + Exprs[1] + ... + Expr[len(Expr)-1].
 type AddStringExpr struct {
        miniExpr
-       List_ Nodes
+       List_    Nodes
+       Prealloc *Name
 }
 
 func NewAddStringExpr(pos src.XPos, list []Node) *AddStringExpr {
@@ -233,9 +234,10 @@ func (n *CallExpr) SetOp(op Op) {
 // A CallPartExpr is a method expression X.Method (uncalled).
 type CallPartExpr struct {
        miniExpr
-       Func_  *Func
-       X      Node
-       Method *types.Field
+       Func_    *Func
+       X        Node
+       Method   *types.Field
+       Prealloc *Name
 }
 
 func NewCallPartExpr(pos src.XPos, x Node, method *types.Field, fn *Func) *CallPartExpr {
@@ -255,7 +257,8 @@ func (n *CallPartExpr) SetLeft(x Node)  { n.X = x }
 // A ClosureExpr is a function literal expression.
 type ClosureExpr struct {
        miniExpr
-       Func_ *Func
+       Func_    *Func
+       Prealloc *Name
 }
 
 func NewClosureExpr(pos src.XPos, fn *Func) *ClosureExpr {
@@ -287,9 +290,10 @@ func (n *ClosureReadExpr) Offset() int64     { return n.Offset_ }
 // Before type-checking, the type is Ntype.
 type CompLitExpr struct {
        miniExpr
-       orig  Node
-       Ntype Ntype
-       List_ Nodes // initialized values
+       orig     Node
+       Ntype    Ntype
+       List_    Nodes // initialized values
+       Prealloc *Name
 }
 
 func NewCompLitExpr(pos src.XPos, op Op, typ Ntype, list []Node) *CompLitExpr {
index 4dd1733074868655b8f590655efe30ac5448aaa1..12811821ad9eea79a6b4ee8f1399445ba2311d0b 100644 (file)
@@ -368,6 +368,7 @@ type RangeStmt struct {
        Body_     Nodes
        HasBreak_ bool
        typ       *types.Type // TODO(rsc): Remove - use X.Type() instead
+       Prealloc  *Name
 }
 
 func NewRangeStmt(pos src.XPos, vars []Node, x Node, body []Node) *RangeStmt {