From 77b527e283112d33000990c59e766074c9396292 Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Fri, 11 Mar 2016 17:39:00 -0800 Subject: [PATCH] cmd/compile: strength reduce *24 We use *24 a lot for pointer arithmetic when accessing slices of slices ([][]T). Rewrite to use an LEA and a shift. The shift will likely be free, as it often gets folded into an indexed load/store. Update #14606 Change-Id: Ie0bf6dc1093876efd57e88ce5f62c26a9bf21cec Reviewed-on: https://go-review.googlesource.com/20567 Run-TryBot: Keith Randall TryBot-Result: Gobot Gobot Reviewed-by: Todd Neal --- src/cmd/compile/internal/ssa/gen/AMD64.rules | 1 + src/cmd/compile/internal/ssa/rewriteAMD64.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/cmd/compile/internal/ssa/gen/AMD64.rules b/src/cmd/compile/internal/ssa/gen/AMD64.rules index a98301a303..ae55d28c18 100644 --- a/src/cmd/compile/internal/ssa/gen/AMD64.rules +++ b/src/cmd/compile/internal/ssa/gen/AMD64.rules @@ -548,6 +548,7 @@ (MULQconst [3] x) -> (LEAQ2 x x) (MULQconst [5] x) -> (LEAQ4 x x) (MULQconst [9] x) -> (LEAQ8 x x) +(MULQconst [24] x) -> (SHLQconst [3] (LEAQ2 x x)) // Useful for [][]T accesses (MULQconst [c] x) && isPowerOfTwo(c) -> (SHLQconst [log2(c)] x) // combine add/shift into LEAQ diff --git a/src/cmd/compile/internal/ssa/rewriteAMD64.go b/src/cmd/compile/internal/ssa/rewriteAMD64.go index 698e6ab167..c98505cafe 100644 --- a/src/cmd/compile/internal/ssa/rewriteAMD64.go +++ b/src/cmd/compile/internal/ssa/rewriteAMD64.go @@ -8280,6 +8280,22 @@ func rewriteValueAMD64_OpAMD64MULQconst(v *Value, config *Config) bool { v.AddArg(x) return true } + // match: (MULQconst [24] x) + // cond: + // result: (SHLQconst [3] (LEAQ2 x x)) + for { + if v.AuxInt != 24 { + break + } + x := v.Args[0] + v.reset(OpAMD64SHLQconst) + v.AuxInt = 3 + v0 := b.NewValue0(v.Line, OpAMD64LEAQ2, v.Type) + v0.AddArg(x) + v0.AddArg(x) + v.AddArg(v0) + return true + } // match: (MULQconst [c] x) // cond: isPowerOfTwo(c) // result: (SHLQconst [log2(c)] x) -- 2.48.1