diff options
| author | Matthew Dempsky <mdempsky@google.com> | 2023-08-09 04:05:35 -0700 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-08-11 18:03:52 +0000 |
| commit | 59037ac93a49889eb6a7d6b3b8fbc70321615f1f (patch) | |
| tree | ae6897c814f010098508ea40f297ecbed12210ff /src/cmd/compile/internal/noder/reader.go | |
| parent | 832212df9aba985bdc6a8378a821e1030554fa2f (diff) | |
| download | go-59037ac93a49889eb6a7d6b3b8fbc70321615f1f.tar.xz | |
cmd/compile: move early deadcode into unified writer
This CL moves the early deadcode elimination pass into the unified
writer. This allows shrinking the export data, by simplifying
expressions and removing unreachable statements. It also means we
don't need to repeatedly apply deadcode elimination on inlined calls
or instantiated generics.
Change-Id: I19bdb04861e50815fccdab39790f4aaa076121fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/517775
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/noder/reader.go')
| -rw-r--r-- | src/cmd/compile/internal/noder/reader.go | 42 |
1 files changed, 17 insertions, 25 deletions
diff --git a/src/cmd/compile/internal/noder/reader.go b/src/cmd/compile/internal/noder/reader.go index f63040ae13..a92a890437 100644 --- a/src/cmd/compile/internal/noder/reader.go +++ b/src/cmd/compile/internal/noder/reader.go @@ -13,7 +13,6 @@ import ( "strings" "cmd/compile/internal/base" - "cmd/compile/internal/deadcode" "cmd/compile/internal/dwarfgen" "cmd/compile/internal/inline" "cmd/compile/internal/ir" @@ -1900,6 +1899,10 @@ func (r *reader) forStmt(label *types.Sym) ir.Node { perLoopVars := r.Bool() r.closeAnotherScope() + if ir.IsConst(cond, constant.Bool) && !ir.BoolVal(cond) { + return init // simplify "for init; false; post { ... }" into "init" + } + stmt := ir.NewForStmt(pos, init, cond, post, body, perLoopVars) stmt.Label = label return stmt @@ -1913,9 +1916,14 @@ func (r *reader) ifStmt() ir.Node { cond := r.expr() then := r.blockStmt() els := r.stmts() + r.closeAnotherScope() + + if ir.IsConst(cond, constant.Bool) && len(init)+len(then)+len(els) == 0 { + return nil // drop empty if statement + } + n := ir.NewIfStmt(pos, cond, then, els) n.SetInit(init) - r.closeAnotherScope() return n } @@ -3531,8 +3539,6 @@ func unifiedInlineCall(call *ir.CallExpr, fn *ir.Func, inlIndex int) *ir.Inlined // Note issue 28603. init.Append(ir.NewInlineMarkStmt(call.Pos().WithIsStmt(), int64(r.inlTreeIndex))) - nparams := len(r.curfn.Dcl) - ir.WithFunc(r.curfn, func() { if !r.syntheticBody(call.Pos()) { assert(r.Bool()) // have body @@ -3548,8 +3554,6 @@ func unifiedInlineCall(call *ir.CallExpr, fn *ir.Func, inlIndex int) *ir.Inlined // themselves. But currently it's an easy fix to #50552. readBodies(typecheck.Target, true) - deadcode.Func(r.curfn) - // Replace any "return" statements within the function body. var edit func(ir.Node) ir.Node edit = func(n ir.Node) ir.Node { @@ -3564,22 +3568,14 @@ func unifiedInlineCall(call *ir.CallExpr, fn *ir.Func, inlIndex int) *ir.Inlined body := ir.Nodes(r.curfn.Body) - // Quirkish: We need to eagerly prune variables added during - // inlining, but removed by deadcode.FuncBody above. Unused - // variables will get removed during stack frame layout anyway, but - // len(fn.Dcl) ends up influencing things like autotmp naming. - - used := usedLocals(body) - - for i, name := range r.curfn.Dcl { - if i < nparams || used.Has(name) { - name.Curfn = callerfn - callerfn.Dcl = append(callerfn.Dcl, name) + // Reparent any declarations into the caller function. + for _, name := range r.curfn.Dcl { + name.Curfn = callerfn + callerfn.Dcl = append(callerfn.Dcl, name) - if name.AutoTemp() { - name.SetEsc(ir.EscUnknown) - name.SetInlLocal(true) - } + if name.AutoTemp() { + name.SetEsc(ir.EscUnknown) + name.SetInlLocal(true) } } @@ -3647,10 +3643,6 @@ func expandInline(fn *ir.Func, pri pkgReaderIndex) { r.funarghack = true r.funcBody(tmpfn) - - ir.WithFunc(tmpfn, func() { - deadcode.Func(tmpfn) - }) } used := usedLocals(tmpfn.Body) |
