Skip to content

Commit 9f03684

Browse files
committed
[dev.regabi] cmd/compile: use ir.DoChildren directly in inlining
Passes toolstash -cmp. Change-Id: Ie35e8163fa0e61ed9e1b259929c8cbe82ee5301e Reviewed-on: https://go-review.googlesource.com/c/go/+/282212 Run-TryBot: Baokun Lee <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Trust: Baokun Lee <[email protected]>
1 parent 213c390 commit 9f03684

File tree

1 file changed

+25
-41
lines changed
  • src/cmd/compile/internal/inline

1 file changed

+25
-41
lines changed

src/cmd/compile/internal/inline/inl.go

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
package inline
2828

2929
import (
30-
"errors"
3130
"fmt"
3231
"go/constant"
3332
"strings"
@@ -256,17 +255,12 @@ type hairyVisitor struct {
256255
reason string
257256
extraCallCost int32
258257
usedLocals map[*ir.Name]bool
259-
do func(ir.Node) error
258+
do func(ir.Node) bool
260259
}
261260

262-
var errBudget = errors.New("too expensive")
263-
264261
func (v *hairyVisitor) tooHairy(fn *ir.Func) bool {
265262
v.do = v.doNode // cache closure
266-
267-
err := errChildren(fn, v.do)
268-
if err != nil {
269-
v.reason = err.Error()
263+
if ir.DoChildren(fn, v.do) {
270264
return true
271265
}
272266
if v.budget < 0 {
@@ -276,11 +270,10 @@ func (v *hairyVisitor) tooHairy(fn *ir.Func) bool {
276270
return false
277271
}
278272

279-
func (v *hairyVisitor) doNode(n ir.Node) error {
273+
func (v *hairyVisitor) doNode(n ir.Node) bool {
280274
if n == nil {
281-
return nil
275+
return false
282276
}
283-
284277
switch n.Op() {
285278
// Call is okay if inlinable and we have the budget for the body.
286279
case ir.OCALLFUNC:
@@ -294,7 +287,8 @@ func (v *hairyVisitor) doNode(n ir.Node) error {
294287
if name.Class == ir.PFUNC && types.IsRuntimePkg(name.Sym().Pkg) {
295288
fn := name.Sym().Name
296289
if fn == "getcallerpc" || fn == "getcallersp" {
297-
return errors.New("call to " + fn)
290+
v.reason = "call to " + fn
291+
return true
298292
}
299293
if fn == "throw" {
300294
v.budget -= inlineExtraThrowCost
@@ -357,7 +351,8 @@ func (v *hairyVisitor) doNode(n ir.Node) error {
357351
case ir.ORECOVER:
358352
// recover matches the argument frame pointer to find
359353
// the right panic value, so it needs an argument frame.
360-
return errors.New("call to recover")
354+
v.reason = "call to recover"
355+
return true
361356

362357
case ir.OCLOSURE:
363358
// TODO(danscales) - fix some bugs when budget is lowered below 30
@@ -371,24 +366,27 @@ func (v *hairyVisitor) doNode(n ir.Node) error {
371366
ir.ODEFER,
372367
ir.ODCLTYPE, // can't print yet
373368
ir.OTAILCALL:
374-
return errors.New("unhandled op " + n.Op().String())
369+
v.reason = "unhandled op " + n.Op().String()
370+
return true
375371

376372
case ir.OAPPEND:
377373
v.budget -= inlineExtraAppendCost
378374

379375
case ir.ODCLCONST, ir.OFALL:
380376
// These nodes don't produce code; omit from inlining budget.
381-
return nil
377+
return false
382378

383379
case ir.OFOR, ir.OFORUNTIL:
384380
n := n.(*ir.ForStmt)
385381
if n.Label != nil {
386-
return errors.New("labeled control")
382+
v.reason = "labeled control"
383+
return true
387384
}
388385
case ir.OSWITCH:
389386
n := n.(*ir.SwitchStmt)
390387
if n.Label != nil {
391-
return errors.New("labeled control")
388+
v.reason = "labeled control"
389+
return true
392390
}
393391
// case ir.ORANGE, ir.OSELECT in "unhandled" above
394392

@@ -404,16 +402,9 @@ func (v *hairyVisitor) doNode(n ir.Node) error {
404402
if ir.IsConst(n.Cond, constant.Bool) {
405403
// This if and the condition cost nothing.
406404
// TODO(rsc): It seems strange that we visit the dead branch.
407-
if err := errList(n.Init(), v.do); err != nil {
408-
return err
409-
}
410-
if err := errList(n.Body, v.do); err != nil {
411-
return err
412-
}
413-
if err := errList(n.Else, v.do); err != nil {
414-
return err
415-
}
416-
return nil
405+
return doList(n.Init(), v.do) ||
406+
doList(n.Body, v.do) ||
407+
doList(n.Else, v.do)
417408
}
418409

419410
case ir.ONAME:
@@ -439,10 +430,11 @@ func (v *hairyVisitor) doNode(n ir.Node) error {
439430

440431
// When debugging, don't stop early, to get full cost of inlining this function
441432
if v.budget < 0 && base.Flag.LowerM < 2 && !logopt.Enabled() {
442-
return errBudget
433+
v.reason = "too expensive"
434+
return true
443435
}
444436

445-
return errChildren(n, v.do)
437+
return ir.DoChildren(n, v.do)
446438
}
447439

448440
func isBigFunc(fn *ir.Func) bool {
@@ -1411,21 +1403,13 @@ func numNonClosures(list []*ir.Func) int {
14111403
return count
14121404
}
14131405

1414-
// TODO(mdempsky): Update inl.go to use ir.DoChildren directly.
1415-
func errChildren(n ir.Node, do func(ir.Node) error) (err error) {
1416-
ir.DoChildren(n, func(x ir.Node) bool {
1417-
err = do(x)
1418-
return err != nil
1419-
})
1420-
return
1421-
}
1422-
func errList(list []ir.Node, do func(ir.Node) error) error {
1406+
func doList(list []ir.Node, do func(ir.Node) bool) bool {
14231407
for _, x := range list {
14241408
if x != nil {
1425-
if err := do(x); err != nil {
1426-
return err
1409+
if do(x) {
1410+
return true
14271411
}
14281412
}
14291413
}
1430-
return nil
1414+
return false
14311415
}

0 commit comments

Comments
 (0)