Skip to content

Commit 5b21846

Browse files
jakebaileygopherbot
authored andcommitted
cmd/compile: optimize loads from abi.Type.{Size_,PtrBytes,Kind_}
With the previous CL in place, we can now pretty easily optimize a few more loads from abi.Type. I've done Size_, PtrBytes, and Kind_, which are easily calculated. Among std/cmd, this rule fires a number of times: 75 abi.Type field Kind_ 50 abi.PtrType field Elem 14 abi.Type field Hash 4 abi.Type field Size_ 2 abi.Type field PtrBytes The other ones that show up when compiling std/cmd are TFlag and GCData, but these are not trivially calculated. Doing TFlag would probably be a decent help given it's often used in things like switches where statically knowing the kind could eliminate a bunch of dead code. Change-Id: Ic7fd2113fa7479af914d06916edbca60cc71819f Reviewed-on: https://go-review.googlesource.com/c/go/+/701298 Reviewed-by: Mark Freeman <[email protected]> Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent b915e14 commit 5b21846

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/cmd/compile/internal/reflectdata/reflect.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,10 @@ var kinds = []abi.Kind{
414414
types.TUNSAFEPTR: abi.UnsafePointer,
415415
}
416416

417+
func ABIKindOfType(t *types.Type) abi.Kind {
418+
return kinds[t.Kind()]
419+
}
420+
417421
var (
418422
memhashvarlen *obj.LSym
419423
memequalvarlen *obj.LSym
@@ -512,8 +516,7 @@ func dcommontype(c rttype.Cursor, t *types.Type) {
512516
c.Field("Align_").WriteUint8(uint8(t.Alignment()))
513517
c.Field("FieldAlign_").WriteUint8(uint8(t.Alignment()))
514518

515-
kind := kinds[t.Kind()]
516-
c.Field("Kind_").WriteUint8(uint8(kind))
519+
c.Field("Kind_").WriteUint8(uint8(ABIKindOfType(t)))
517520

518521
c.Field("Equal").WritePtr(eqfunc)
519522
c.Field("GCData").WritePtr(gcsym)

src/cmd/compile/internal/ssa/rewrite.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2005,7 +2005,7 @@ func isFixedLoad(v *Value, sym Sym, off int64) bool {
20052005
for _, f := range rttype.Type.Fields() {
20062006
if f.Offset == off && copyCompatibleType(v.Type, f.Type) {
20072007
switch f.Sym.Name {
2008-
case "Hash":
2008+
case "Size_", "PtrBytes", "Hash", "Kind_":
20092009
return true
20102010
default:
20112011
// fmt.Println("unknown field", f.Sym.Name)
@@ -2061,13 +2061,30 @@ func rewriteFixedLoad(v *Value, sym Sym, sb *Value, off int64) *Value {
20612061

20622062
t := (*lsym.Extra).(*obj.TypeInfo).Type.(*types.Type)
20632063

2064+
ptrSizedOpConst := OpConst64
2065+
if f.Config.PtrSize == 4 {
2066+
ptrSizedOpConst = OpConst32
2067+
}
2068+
20642069
for _, f := range rttype.Type.Fields() {
20652070
if f.Offset == off && copyCompatibleType(v.Type, f.Type) {
20662071
switch f.Sym.Name {
2072+
case "Size_":
2073+
v.reset(ptrSizedOpConst)
2074+
v.AuxInt = int64(t.Size())
2075+
return v
2076+
case "PtrBytes":
2077+
v.reset(ptrSizedOpConst)
2078+
v.AuxInt = int64(types.PtrDataSize(t))
2079+
return v
20672080
case "Hash":
20682081
v.reset(OpConst32)
20692082
v.AuxInt = int64(types.TypeHash(t))
20702083
return v
2084+
case "Kind_":
2085+
v.reset(OpConst8)
2086+
v.AuxInt = int64(reflectdata.ABIKindOfType(t))
2087+
return v
20712088
default:
20722089
base.Fatalf("unknown field %s for fixedLoad of %s at offset %d", f.Sym.Name, lsym.Name, off)
20732090
}

0 commit comments

Comments
 (0)