cmd/compile: minor cleanup in inl

* Make budget an int32 to avoid needless conversions.
* Introduce some temporary variables to reduce repetition.
* If ... args are present, they will be the last argument
  to the function. No need to scan all arguments.

Passes toolstash -cmp.

Change-Id: I55203609f5d2f25a4e238cd48c63214651120cfc
Reviewed-on: https://go-review.googlesource.com/22421
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/src/cmd/compile/internal/gc/inl.go b/src/cmd/compile/internal/gc/inl.go
index da026e1..c863b84 100644
--- a/src/cmd/compile/internal/gc/inl.go
+++ b/src/cmd/compile/internal/gc/inl.go
@@ -110,8 +110,9 @@
 
 	// can't handle ... args yet
 	if Debug['l'] < 3 {
-		for _, t := range fn.Type.Params().Fields().Slice() {
-			if t.Isddd {
+		f := fn.Type.Params().Fields()
+		if len := f.Len(); len > 0 {
+			if t := f.Index(len - 1); t.Isddd {
 				return
 			}
 		}
@@ -128,7 +129,7 @@
 	}
 
 	const maxBudget = 80
-	budget := maxBudget // allowed hairyness
+	budget := int32(maxBudget) // allowed hairyness
 	if ishairylist(fn.Nbody, &budget) || budget < 0 {
 		return
 	}
@@ -136,27 +137,29 @@
 	savefn := Curfn
 	Curfn = fn
 
-	fn.Func.Nname.Func.Inl.Set(fn.Nbody.Slice())
-	fn.Nbody.Set(inlcopylist(fn.Func.Nname.Func.Inl.Slice()))
-	inldcl := inlcopylist(fn.Func.Nname.Name.Defn.Func.Dcl)
-	fn.Func.Nname.Func.Inldcl.Set(inldcl)
-	fn.Func.Nname.Func.InlCost = int32(maxBudget - budget)
+	n := fn.Func.Nname
+
+	n.Func.Inl.Set(fn.Nbody.Slice())
+	fn.Nbody.Set(inlcopylist(n.Func.Inl.Slice()))
+	inldcl := inlcopylist(n.Name.Defn.Func.Dcl)
+	n.Func.Inldcl.Set(inldcl)
+	n.Func.InlCost = maxBudget - budget
 
 	// hack, TODO, check for better way to link method nodes back to the thing with the ->inl
 	// this is so export can find the body of a method
-	fn.Type.SetNname(fn.Func.Nname)
+	fn.Type.SetNname(n)
 
 	if Debug['m'] > 1 {
-		fmt.Printf("%v: can inline %v as: %v { %v }\n", fn.Line(), Nconv(fn.Func.Nname, FmtSharp), Tconv(fn.Type, FmtSharp), Hconv(fn.Func.Nname.Func.Inl, FmtSharp))
+		fmt.Printf("%v: can inline %v as: %v { %v }\n", fn.Line(), Nconv(n, FmtSharp), Tconv(fn.Type, FmtSharp), Hconv(n.Func.Inl, FmtSharp))
 	} else if Debug['m'] != 0 {
-		fmt.Printf("%v: can inline %v\n", fn.Line(), fn.Func.Nname)
+		fmt.Printf("%v: can inline %v\n", fn.Line(), n)
 	}
 
 	Curfn = savefn
 }
 
 // Look for anything we want to punt on.
-func ishairylist(ll Nodes, budget *int) bool {
+func ishairylist(ll Nodes, budget *int32) bool {
 	for _, n := range ll.Slice() {
 		if ishairy(n, budget) {
 			return true
@@ -165,7 +168,7 @@
 	return false
 }
 
-func ishairy(n *Node, budget *int) bool {
+func ishairy(n *Node, budget *int32) bool {
 	if n == nil {
 		return false
 	}
@@ -173,13 +176,13 @@
 	switch n.Op {
 	// Call is okay if inlinable and we have the budget for the body.
 	case OCALLFUNC:
-		if n.Left.Func != nil && n.Left.Func.Inl.Len() != 0 {
-			*budget -= int(n.Left.Func.InlCost)
+		if fn := n.Left.Func; fn != nil && fn.Inl.Len() != 0 {
+			*budget -= fn.InlCost
 			break
 		}
 		if n.Left.Op == ONAME && n.Left.Left != nil && n.Left.Left.Op == OTYPE && n.Left.Right != nil && n.Left.Right.Op == ONAME { // methods called as functions
-			if n.Left.Sym.Def != nil && n.Left.Sym.Def.Func.Inl.Len() != 0 {
-				*budget -= int(n.Left.Sym.Def.Func.InlCost)
+			if d := n.Left.Sym.Def; d != nil && d.Func.Inl.Len() != 0 {
+				*budget -= d.Func.InlCost
 				break
 			}
 		}
@@ -189,14 +192,15 @@
 
 	// Call is okay if inlinable and we have the budget for the body.
 	case OCALLMETH:
-		if n.Left.Type == nil {
+		t := n.Left.Type
+		if t == nil {
 			Fatalf("no function type for [%p] %v\n", n.Left, Nconv(n.Left, FmtSign))
 		}
-		if n.Left.Type.Nname() == nil {
-			Fatalf("no function definition for [%p] %v\n", n.Left.Type, Tconv(n.Left.Type, FmtSign))
+		if t.Nname() == nil {
+			Fatalf("no function definition for [%p] %v\n", t, Tconv(t, FmtSign))
 		}
-		if n.Left.Type.Nname().Func.Inl.Len() != 0 {
-			*budget -= int(n.Left.Type.Nname().Func.InlCost)
+		if inlfn := t.Nname().Func; inlfn.Inl.Len() != 0 {
+			*budget -= inlfn.InlCost
 			break
 		}
 		if Debug['l'] < 4 {
diff --git a/src/cmd/compile/internal/gc/type.go b/src/cmd/compile/internal/gc/type.go
index 1401332..da295bb 100644
--- a/src/cmd/compile/internal/gc/type.go
+++ b/src/cmd/compile/internal/gc/type.go
@@ -332,6 +332,12 @@
 	return *f.s
 }
 
+// Index returns the i'th element of Fields.
+// It panics if f does not have at least i+1 elements.
+func (f *Fields) Index(i int) *Field {
+	return (*f.s)[i]
+}
+
 // Set sets f to a slice.
 // This takes ownership of the slice.
 func (f *Fields) Set(s []*Field) {