]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.regabi] cmd/compile: simplify stack temp initialization
authorMatthew Dempsky <mdempsky@google.com>
Sun, 17 Jan 2021 03:35:39 +0000 (19:35 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Sun, 17 Jan 2021 05:08:11 +0000 (05:08 +0000)
This CL simplifies the previous one a little bit further, by combining
reordering stack-temporary initialization and getting rid of an
unneeded temporary variable. (Does not pass toolstash -cmp.)

Change-Id: I17799dfe368484f33a8ddd0ab4f68647d6262147
Reviewed-on: https://go-review.googlesource.com/c/go/+/284225
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
src/cmd/compile/internal/walk/complit.go
src/cmd/compile/internal/walk/temp.go

index a7db4535501376f5eab373854b82a5694127ac64..97e820238bb04053e3aec1f89ab66f409b6c88a7 100644 (file)
@@ -344,21 +344,18 @@ func slicelit(ctxt initContext, n *ir.CompLitExpr, var_ ir.Node, init *ir.Nodes)
                if !types.Identical(t, x.Type()) {
                        panic("dotdotdot base type does not match order's assigned type")
                }
-               a = initStackTemp(init, x, vstat != nil)
+               a = initStackTemp(init, x, vstat)
        } else if n.Esc() == ir.EscNone {
-               if vstat == nil {
-                       // TODO(mdempsky): Remove this useless temporary.
-                       // It's only needed to keep toolstash happy.
-                       typecheck.Temp(t)
-               }
-               a = initStackTemp(init, typecheck.Temp(t), vstat != nil)
+               a = initStackTemp(init, typecheck.Temp(t), vstat)
        } else {
                a = ir.NewUnaryExpr(base.Pos, ir.ONEW, ir.TypeNode(t))
        }
        appendWalkStmt(init, ir.NewAssignStmt(base.Pos, vauto, a))
 
-       if vstat != nil {
-               // copy static to heap (4)
+       if vstat != nil && n.Prealloc == nil && n.Esc() != ir.EscNone {
+               // If we allocated on the heap with ONEW, copy the static to the
+               // heap (4). We skip this for stack temporaries, because
+               // initStackTemp already handled the copy.
                a = ir.NewStarExpr(base.Pos, vauto)
                appendWalkStmt(init, ir.NewAssignStmt(base.Pos, a, vstat))
        }
@@ -535,7 +532,7 @@ func anylit(n ir.Node, var_ ir.Node, init *ir.Nodes) {
                var r ir.Node
                if n.Prealloc != nil {
                        // n.Prealloc is stack temporary used as backing store.
-                       r = initStackTemp(init, n.Prealloc, false)
+                       r = initStackTemp(init, n.Prealloc, nil)
                } else {
                        r = ir.NewUnaryExpr(base.Pos, ir.ONEW, ir.TypeNode(n.X.Type()))
                        r.SetEsc(n.Esc())
index 901cb770f362a56a410ae0894670e551982100cf..9879a6c69d7c604027f232049f3ae7d8e02c6ed4 100644 (file)
@@ -12,19 +12,12 @@ import (
 )
 
 // initStackTemp appends statements to init to initialize the given
-// temporary variable, and then returns the expression &tmp. If vardef
-// is true, then the variable is initialized with OVARDEF, and the
-// caller must ensure the variable is later assigned before use;
-// otherwise, it's zero initialized.
-//
-// TODO(mdempsky): Change callers to provide tmp's initial value,
-// rather than just vardef, to make this safer/easier to use.
-func initStackTemp(init *ir.Nodes, tmp *ir.Name, vardef bool) *ir.AddrExpr {
-       if vardef {
-               init.Append(ir.NewUnaryExpr(base.Pos, ir.OVARDEF, tmp))
-       } else {
-               appendWalkStmt(init, ir.NewAssignStmt(base.Pos, tmp, nil))
+// temporary variable to val, and then returns the expression &tmp.
+func initStackTemp(init *ir.Nodes, tmp *ir.Name, val ir.Node) *ir.AddrExpr {
+       if val != nil && !types.Identical(tmp.Type(), val.Type()) {
+               base.Fatalf("bad initial value for %L: %L", tmp, val)
        }
+       appendWalkStmt(init, ir.NewAssignStmt(base.Pos, tmp, val))
        return typecheck.Expr(typecheck.NodAddr(tmp)).(*ir.AddrExpr)
 }
 
@@ -32,7 +25,7 @@ func initStackTemp(init *ir.Nodes, tmp *ir.Name, vardef bool) *ir.AddrExpr {
 // allocated temporary variable of the given type. Statements to
 // zero-initialize tmp are appended to init.
 func stackTempAddr(init *ir.Nodes, typ *types.Type) *ir.AddrExpr {
-       return initStackTemp(init, typecheck.Temp(typ), false)
+       return initStackTemp(init, typecheck.Temp(typ), nil)
 }
 
 // stackBufAddr returns thte expression &tmp, where tmp is a newly