Commit 584ef455 authored by Cuong Manh Le's avatar Cuong Manh Le Committed by Matthew Dempsky

cmd/compile: skip empty init function in fninit

Fixes #34869

Change-Id: I21bc60b9a5d1204dade1cceed6cddccf5b537b0e
Reviewed-on: https://go-review.googlesource.com/c/go/+/200958
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarMatthew Dempsky <mdempsky@google.com>
parent bc529506
......@@ -73,6 +73,14 @@ func fninit(n []*Node) {
// Record user init functions.
for i := 0; i < renameinitgen; i++ {
s := lookupN("init.", i)
fn := asNode(s.Def).Name.Defn
// Skip init functions with empty bodies.
// noder.go doesn't allow external init functions, and
// order.go has already removed any OEMPTY nodes, so
// checking Len() == 0 is sufficient here.
if fn.Nbody.Len() == 0 {
continue
}
fns = append(fns, s.Linksym())
}
......
......@@ -3812,6 +3812,33 @@ func checkreturn(fn *Node) {
func deadcode(fn *Node) {
deadcodeslice(fn.Nbody)
deadcodefn(fn)
}
func deadcodefn(fn *Node) {
if fn.Nbody.Len() == 0 {
return
}
for _, n := range fn.Nbody.Slice() {
if n.Ninit.Len() > 0 {
return
}
switch n.Op {
case OIF:
if !Isconst(n.Left, CTBOOL) || n.Nbody.Len() > 0 || n.Rlist.Len() > 0 {
return
}
case OFOR:
if !Isconst(n.Left, CTBOOL) || n.Left.Bool() {
return
}
default:
return
}
}
fn.Nbody.Set([]*Node{nod(OEMPTY, nil, nil)})
}
func deadcodeslice(nn Nodes) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment