Skip to content

Commit 758431f

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 1da62af commit 758431f

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ func caninl(fn *Node) {
110110

111111
// can't handle ... args yet
112112
if Debug['l'] < 3 {
113-
for _, t := range fn.Type.Params().Fields().Slice() {
114-
if t.Isddd {
113+
f := fn.Type.Params().Fields()
114+
if len := f.Len(); len > 0 {
115+
if t := f.Index(len - 1); t.Isddd {
115116
return
116117
}
117118
}
@@ -128,35 +129,37 @@ func caninl(fn *Node) {
128129
}
129130

130131
const maxBudget = 80
131-
budget := maxBudget // allowed hairyness
132+
budget := int32(maxBudget) // allowed hairyness
132133
if ishairylist(fn.Nbody, &budget) || budget < 0 {
133134
return
134135
}
135136

136137
savefn := Curfn
137138
Curfn = fn
138139

139-
fn.Func.Nname.Func.Inl.Set(fn.Nbody.Slice())
140-
fn.Nbody.Set(inlcopylist(fn.Func.Nname.Func.Inl.Slice()))
141-
inldcl := inlcopylist(fn.Func.Nname.Name.Defn.Func.Dcl)
142-
fn.Func.Nname.Func.Inldcl.Set(inldcl)
143-
fn.Func.Nname.Func.InlCost = int32(maxBudget - budget)
140+
n := fn.Func.Nname
141+
142+
n.Func.Inl.Set(fn.Nbody.Slice())
143+
fn.Nbody.Set(inlcopylist(n.Func.Inl.Slice()))
144+
inldcl := inlcopylist(n.Name.Defn.Func.Dcl)
145+
n.Func.Inldcl.Set(inldcl)
146+
n.Func.InlCost = maxBudget - budget
144147

145148
// hack, TODO, check for better way to link method nodes back to the thing with the ->inl
146149
// this is so export can find the body of a method
147-
fn.Type.SetNname(fn.Func.Nname)
150+
fn.Type.SetNname(n)
148151

149152
if Debug['m'] > 1 {
150-
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))
153+
fmt.Printf("%v: can inline %v as: %v { %v }\n", fn.Line(), Nconv(n, FmtSharp), Tconv(fn.Type, FmtSharp), Hconv(n.Func.Inl, FmtSharp))
151154
} else if Debug['m'] != 0 {
152-
fmt.Printf("%v: can inline %v\n", fn.Line(), fn.Func.Nname)
155+
fmt.Printf("%v: can inline %v\n", fn.Line(), n)
153156
}
154157

155158
Curfn = savefn
156159
}
157160

158161
// Look for anything we want to punt on.
159-
func ishairylist(ll Nodes, budget *int) bool {
162+
func ishairylist(ll Nodes, budget *int32) bool {
160163
for _, n := range ll.Slice() {
161164
if ishairy(n, budget) {
162165
return true
@@ -165,21 +168,21 @@ func ishairylist(ll Nodes, budget *int) bool {
165168
return false
166169
}
167170

168-
func ishairy(n *Node, budget *int) bool {
171+
func ishairy(n *Node, budget *int32) bool {
169172
if n == nil {
170173
return false
171174
}
172175

173176
switch n.Op {
174177
// Call is okay if inlinable and we have the budget for the body.
175178
case OCALLFUNC:
176-
if n.Left.Func != nil && n.Left.Func.Inl.Len() != 0 {
177-
*budget -= int(n.Left.Func.InlCost)
179+
if fn := n.Left.Func; fn != nil && fn.Inl.Len() != 0 {
180+
*budget -= fn.InlCost
178181
break
179182
}
180183
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
181-
if n.Left.Sym.Def != nil && n.Left.Sym.Def.Func.Inl.Len() != 0 {
182-
*budget -= int(n.Left.Sym.Def.Func.InlCost)
184+
if d := n.Left.Sym.Def; d != nil && d.Func.Inl.Len() != 0 {
185+
*budget -= d.Func.InlCost
183186
break
184187
}
185188
}
@@ -189,14 +192,15 @@ func ishairy(n *Node, budget *int) bool {
189192

190193
// Call is okay if inlinable and we have the budget for the body.
191194
case OCALLMETH:
192-
if n.Left.Type == nil {
195+
t := n.Left.Type
196+
if t == nil {
193197
Fatalf("no function type for [%p] %v\n", n.Left, Nconv(n.Left, FmtSign))
194198
}
195-
if n.Left.Type.Nname() == nil {
196-
Fatalf("no function definition for [%p] %v\n", n.Left.Type, Tconv(n.Left.Type, FmtSign))
199+
if t.Nname() == nil {
200+
Fatalf("no function definition for [%p] %v\n", t, Tconv(t, FmtSign))
197201
}
198-
if n.Left.Type.Nname().Func.Inl.Len() != 0 {
199-
*budget -= int(n.Left.Type.Nname().Func.InlCost)
202+
if inlfn := t.Nname().Func; inlfn.Inl.Len() != 0 {
203+
*budget -= inlfn.InlCost
200204
break
201205
}
202206
if Debug['l'] < 4 {

src/cmd/compile/internal/gc/type.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,12 @@ func (f *Fields) Slice() []*Field {
332332
return *f.s
333333
}
334334

335+
// Index returns the i'th element of Fields.
336+
// It panics if f does not have at least i+1 elements.
337+
func (f *Fields) Index(i int) *Field {
338+
return (*f.s)[i]
339+
}
340+
335341
// Set sets f to a slice.
336342
// This takes ownership of the slice.
337343
func (f *Fields) Set(s []*Field) {

0 commit comments

Comments
 (0)