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>
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.
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)
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)
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 {
}
}
-// 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)
}
// 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
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
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
// 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
// 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")
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))
}
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)
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)
}
// 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 {
// 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 {
// 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 {
// 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 {
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 {