[dev.regabi] cmd/compile: simplify ir.Find, replace ir.Inspect with ir.Visit

It seems clear after using these for a week that Find need not return
anything other than a bool saying whether the target was found.
The main reason for not using the boolean earlier was to avoid confusion
with Inspect: for Find, returning true means "it was found! stop walking"
while for Inspect, returning true means "keep walking the children".

But it turns out that none of the uses of Inspect need the boolean.
This makes sense because types can contain expressions, expressions
can contain statements (inside function literals), and so on, so there
are essentially no times when you can say based on the current AST node
that the children are irrelevant to a particular operation.

So this CL makes two changes:

1) Change Find to return a boolean and to take a callback function
returning a boolean. This simplifies all existing calls to Find.

2) Rename Inspect to Visit and change it to take a callback with no
result at all. This simplifies all existing calls to Inspect.

Removing the boolean result from Inspect's callback avoids having
two callbacks with contradictory boolean results in different APIs.
Renaming Inspect to Visit avoids confusion with ast.Inspect.

Passes buildall w/ toolstash -cmp.

Change-Id: I344ebb5e00b6842012be33e779db483c28e5f350
Reviewed-on: https://go-review.googlesource.com/c/go/+/277919
Trust: Russ Cox <rsc@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Russ Cox 2020-12-12 18:50:21 -05:00
parent f6d2834f8f
commit f6efa3d4a4
9 changed files with 77 additions and 108 deletions

View file

@ -255,7 +255,7 @@ func inlFlood(n *ir.Name) {
// Recursively identify all referenced functions for
// reexport. We want to include even non-called functions,
// because after inlining they might be callable.
ir.InspectList(ir.AsNodes(fn.Inl.Body), func(n ir.Node) bool {
ir.VisitList(ir.AsNodes(fn.Inl.Body), func(n ir.Node) {
switch n.Op() {
case ir.OMETHEXPR, ir.ODOTMETH:
inlFlood(methodExprName(n))
@ -282,7 +282,6 @@ func inlFlood(n *ir.Name) {
// inlFlood(n.Func.Closure.Func.Nname)
base.Fatalf("unexpected closure in inlinable function")
}
return true
})
}
@ -458,14 +457,10 @@ func (v *hairyVisitor) doNode(n ir.Node) error {
func isBigFunc(fn *ir.Func) bool {
budget := inlineBigFunctionNodes
over := ir.Find(fn, func(n ir.Node) interface{} {
return ir.Find(fn, func(n ir.Node) bool {
budget--
if budget <= 0 {
return n
}
return nil
return budget <= 0
})
return over != nil
}
// Inlcalls/nodelist/node walks fn's statements and expressions and substitutes any
@ -707,8 +702,6 @@ FindRHS:
return rhs
}
var errFound = errors.New("found")
// reassigned takes an ONAME node, walks the function in which it is defined, and returns a boolean
// indicating whether the name has any assignments other than its declaration.
// The second return value is the first such assignment encountered in the walk, if any. It is mostly
@ -723,22 +716,21 @@ func reassigned(name *ir.Name) bool {
if name.Curfn == nil {
return true
}
a := ir.Find(name.Curfn, func(n ir.Node) interface{} {
return ir.Find(name.Curfn, func(n ir.Node) bool {
switch n.Op() {
case ir.OAS:
if n.Left() == name && n != name.Defn {
return n
return true
}
case ir.OAS2, ir.OAS2FUNC, ir.OAS2MAPR, ir.OAS2DOTTYPE:
for _, p := range n.List().Slice() {
if p == name && n != name.Defn {
return n
return true
}
}
}
return nil
return false
})
return a != nil
}
func inlParam(t *types.Field, as ir.Node, inlvars map[*ir.Name]ir.Node) ir.Node {
@ -916,11 +908,10 @@ func mkinlcall(n ir.Node, fn *ir.Func, maxCost int32, inlMap map[*ir.Func]bool,
}
nreturns := 0
ir.InspectList(ir.AsNodes(fn.Inl.Body), func(n ir.Node) bool {
ir.VisitList(ir.AsNodes(fn.Inl.Body), func(n ir.Node) {
if n != nil && n.Op() == ir.ORETURN {
nreturns++
}
return true
})
// We can delay declaring+initializing result parameters if:
@ -1287,11 +1278,10 @@ func pruneUnusedAutos(ll []*ir.Name, vis *hairyVisitor) []*ir.Name {
// concrete-type method calls where applicable.
func devirtualize(fn *ir.Func) {
Curfn = fn
ir.InspectList(fn.Body(), func(n ir.Node) bool {
ir.VisitList(fn.Body(), func(n ir.Node) {
if n.Op() == ir.OCALLINTER {
devirtualizeCall(n)
}
return true
})
}