Commit 04f1b65c authored by Michael Munday's avatar Michael Munday

cmd/compile: try and access last argument first in rulegen

This reduces the number of extra bounds check hints we need to
insert. For example, rather than producing:

	_ = v.Args[2]
	x := v.Args[0]
	y := v.Args[1]
	z := v.Args[2]

We now produce:

	z := v.Args[2]
	x := v.Args[0]
	y := v.Args[1]

This gets rid of about 7000 lines of code from the rewrite rules.

Change-Id: I1291cf0f82e8d035a6d65bce7dee6cedee04cbcd
Reviewed-on: https://go-review.googlesource.com/c/go/+/167397Reviewed-by: default avatarDaniel Martí <mvdan@mvdan.cc>
Reviewed-by: default avatarCherry Zhang <cherryyz@google.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 5d4fa147
......@@ -449,7 +449,6 @@ func genMatch0(w io.Writer, arch arch, match, v string, m map[string]struct{}, t
}
if aux != "" {
if !isVariable(aux) {
// code
fmt.Fprintf(w, "if %s.Aux != %s {\nbreak\n}\n", v, aux)
......@@ -466,8 +465,18 @@ func genMatch0(w io.Writer, arch arch, match, v string, m map[string]struct{}, t
}
}
// Access last argument first to minimize bounds checks.
if n := len(args); n > 1 {
fmt.Fprintf(w, "_ = %s.Args[%d]\n", v, n-1) // combine some bounds checks
a := args[n-1]
if _, set := m[a]; !set && a != "_" && isVariable(a) {
m[a] = struct{}{}
fmt.Fprintf(w, "%s := %s.Args[%d]\n", a, v, n-1)
// delete the last argument so it is not reprocessed
args = args[:n-1]
} else {
fmt.Fprintf(w, "_ = %s.Args[%d]\n", v, n-1)
}
}
for i, arg := range args {
if arg == "_" {
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -51,7 +51,6 @@ func rewriteValuedec_OpComplexImag_0(v *Value) bool {
if v_0.Op != OpComplexMake {
break
}
_ = v_0.Args[1]
imag := v_0.Args[1]
v.reset(OpCopy)
v.Type = imag.Type
......@@ -87,7 +86,6 @@ func rewriteValuedec_OpIData_0(v *Value) bool {
if v_0.Op != OpIMake {
break
}
_ = v_0.Args[1]
data := v_0.Args[1]
v.reset(OpCopy)
v.Type = data.Type
......@@ -123,9 +121,8 @@ func rewriteValuedec_OpLoad_0(v *Value) bool {
// result: (ComplexMake (Load <typ.Float32> ptr mem) (Load <typ.Float32> (OffPtr <typ.Float32Ptr> [4] ptr) mem) )
for {
t := v.Type
_ = v.Args[1]
ptr := v.Args[0]
mem := v.Args[1]
ptr := v.Args[0]
if !(t.IsComplex() && t.Size() == 8) {
break
}
......@@ -148,9 +145,8 @@ func rewriteValuedec_OpLoad_0(v *Value) bool {
// result: (ComplexMake (Load <typ.Float64> ptr mem) (Load <typ.Float64> (OffPtr <typ.Float64Ptr> [8] ptr) mem) )
for {
t := v.Type
_ = v.Args[1]
ptr := v.Args[0]
mem := v.Args[1]
ptr := v.Args[0]
if !(t.IsComplex() && t.Size() == 16) {
break
}
......@@ -173,9 +169,8 @@ func rewriteValuedec_OpLoad_0(v *Value) bool {
// result: (StringMake (Load <typ.BytePtr> ptr mem) (Load <typ.Int> (OffPtr <typ.IntPtr> [config.PtrSize] ptr) mem))
for {
t := v.Type
_ = v.Args[1]
ptr := v.Args[0]
mem := v.Args[1]
ptr := v.Args[0]
if !(t.IsString()) {
break
}
......@@ -198,9 +193,8 @@ func rewriteValuedec_OpLoad_0(v *Value) bool {
// result: (SliceMake (Load <t.Elem().PtrTo()> ptr mem) (Load <typ.Int> (OffPtr <typ.IntPtr> [config.PtrSize] ptr) mem) (Load <typ.Int> (OffPtr <typ.IntPtr> [2*config.PtrSize] ptr) mem))
for {
t := v.Type
_ = v.Args[1]
ptr := v.Args[0]
mem := v.Args[1]
ptr := v.Args[0]
if !(t.IsSlice()) {
break
}
......@@ -230,9 +224,8 @@ func rewriteValuedec_OpLoad_0(v *Value) bool {
// result: (IMake (Load <typ.Uintptr> ptr mem) (Load <typ.BytePtr> (OffPtr <typ.BytePtrPtr> [config.PtrSize] ptr) mem))
for {
t := v.Type
_ = v.Args[1]
ptr := v.Args[0]
mem := v.Args[1]
ptr := v.Args[0]
if !(t.IsInterface()) {
break
}
......@@ -261,7 +254,6 @@ func rewriteValuedec_OpSliceCap_0(v *Value) bool {
if v_0.Op != OpSliceMake {
break
}
_ = v_0.Args[2]
cap := v_0.Args[2]
v.reset(OpCopy)
v.Type = cap.Type
......@@ -315,16 +307,14 @@ func rewriteValuedec_OpStore_0(v *Value) bool {
// result: (Store {typ.Float32} (OffPtr <typ.Float32Ptr> [4] dst) imag (Store {typ.Float32} dst real mem))
for {
t := v.Aux
_ = v.Args[2]
mem := v.Args[2]
dst := v.Args[0]
v_1 := v.Args[1]
if v_1.Op != OpComplexMake {
break
}
_ = v_1.Args[1]
real := v_1.Args[0]
imag := v_1.Args[1]
mem := v.Args[2]
real := v_1.Args[0]
if !(t.(*types.Type).Size() == 8) {
break
}
......@@ -348,16 +338,14 @@ func rewriteValuedec_OpStore_0(v *Value) bool {
// result: (Store {typ.Float64} (OffPtr <typ.Float64Ptr> [8] dst) imag (Store {typ.Float64} dst real mem))
for {
t := v.Aux
_ = v.Args[2]
mem := v.Args[2]
dst := v.Args[0]
v_1 := v.Args[1]
if v_1.Op != OpComplexMake {
break
}
_ = v_1.Args[1]
real := v_1.Args[0]
imag := v_1.Args[1]
mem := v.Args[2]
real := v_1.Args[0]
if !(t.(*types.Type).Size() == 16) {
break
}
......@@ -380,16 +368,14 @@ func rewriteValuedec_OpStore_0(v *Value) bool {
// cond:
// result: (Store {typ.Int} (OffPtr <typ.IntPtr> [config.PtrSize] dst) len (Store {typ.BytePtr} dst ptr mem))
for {
_ = v.Args[2]
mem := v.Args[2]
dst := v.Args[0]
v_1 := v.Args[1]
if v_1.Op != OpStringMake {
break
}
_ = v_1.Args[1]
ptr := v_1.Args[0]
len := v_1.Args[1]
mem := v.Args[2]
ptr := v_1.Args[0]
v.reset(OpStore)
v.Aux = typ.Int
v0 := b.NewValue0(v.Pos, OpOffPtr, typ.IntPtr)
......@@ -409,17 +395,15 @@ func rewriteValuedec_OpStore_0(v *Value) bool {
// cond:
// result: (Store {typ.Int} (OffPtr <typ.IntPtr> [2*config.PtrSize] dst) cap (Store {typ.Int} (OffPtr <typ.IntPtr> [config.PtrSize] dst) len (Store {typ.BytePtr} dst ptr mem)))
for {
_ = v.Args[2]
mem := v.Args[2]
dst := v.Args[0]
v_1 := v.Args[1]
if v_1.Op != OpSliceMake {
break
}
_ = v_1.Args[2]
cap := v_1.Args[2]
ptr := v_1.Args[0]
len := v_1.Args[1]
cap := v_1.Args[2]
mem := v.Args[2]
v.reset(OpStore)
v.Aux = typ.Int
v0 := b.NewValue0(v.Pos, OpOffPtr, typ.IntPtr)
......@@ -447,16 +431,14 @@ func rewriteValuedec_OpStore_0(v *Value) bool {
// cond:
// result: (Store {typ.BytePtr} (OffPtr <typ.BytePtrPtr> [config.PtrSize] dst) data (Store {typ.Uintptr} dst itab mem))
for {
_ = v.Args[2]
mem := v.Args[2]
dst := v.Args[0]
v_1 := v.Args[1]
if v_1.Op != OpIMake {
break
}
_ = v_1.Args[1]
itab := v_1.Args[0]
data := v_1.Args[1]
mem := v.Args[2]
itab := v_1.Args[0]
v.reset(OpStore)
v.Aux = typ.BytePtr
v0 := b.NewValue0(v.Pos, OpOffPtr, typ.BytePtrPtr)
......@@ -483,7 +465,6 @@ func rewriteValuedec_OpStringLen_0(v *Value) bool {
if v_0.Op != OpStringMake {
break
}
_ = v_0.Args[1]
len := v_0.Args[1]
v.reset(OpCopy)
v.Type = len.Type
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment