[dev.ssa] cmd/compile: tweak init function prologue

We used to compare the init state with == to 0 and 2, which
requires 2 comparisons.  Instead, compare with 1 and use
<, ==.  That requires only one comparison.

This isn't a big deal performance-wise, as it is just init
code.  But there is a fair amount of init code, so this
should help a bit with code size.

Change-Id: I4a2765f1005776f0edce28ac143f4b7596d95a68
Reviewed-on: https://go-review.googlesource.com/18948
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Keith Randall 2016-01-26 15:55:05 -08:00
parent f94e0745b3
commit f3575a9561

View file

@ -33,10 +33,10 @@ func renameinit() *Sym {
// hand-craft the following initialization code // hand-craft the following initialization code
// var initdone· uint8 (1) // var initdone· uint8 (1)
// func init() (2) // func init() (2)
// if initdone· != 0 { (3) // if initdone· > 1 { (3)
// if initdone· == 2 (4) // return (3a)
// return // if initdone· == 1 { (4)
// throw(); (5) // throw(); (4a)
// } // }
// initdone· = 1; (6) // initdone· = 1; (6)
// // over all matching imported symbols // // over all matching imported symbols
@ -118,22 +118,21 @@ func fninit(n *NodeList) {
// (3) // (3)
a := Nod(OIF, nil, nil) a := Nod(OIF, nil, nil)
a.Left = Nod(OGT, gatevar, Nodintconst(1))
a.Left = Nod(ONE, gatevar, Nodintconst(0)) a.Likely = 1
r = list(r, a) r = list(r, a)
// (3a)
a.Nbody = list1(Nod(ORETURN, nil, nil))
// (4) // (4)
b := Nod(OIF, nil, nil) b := Nod(OIF, nil, nil)
b.Left = Nod(OEQ, gatevar, Nodintconst(1))
b.Left = Nod(OEQ, gatevar, Nodintconst(2)) // this actually isn't likely, but code layout is better
b.Nbody = list1(Nod(ORETURN, nil, nil)) // like this: no JMP needed after the call.
a.Nbody = list1(b) b.Likely = 1
r = list(r, b)
// (5) // (4a)
b = syslook("throwinit", 0) b.Nbody = list1(Nod(OCALL, syslook("throwinit", 0), nil))
b = Nod(OCALL, b, nil)
a.Nbody = list(a.Nbody, b)
// (6) // (6)
a = Nod(OAS, gatevar, Nodintconst(1)) a = Nod(OAS, gatevar, Nodintconst(1))