From: Josh Bleecher Snyder Date: Sun, 23 Feb 2020 05:47:49 +0000 (-0800) Subject: cmd/compile: constant fold SHLxconst of a constant on amd64 X-Git-Tag: go1.15beta1~1040 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f510cddcd153ea83890b89227275648c04e65a1a;p=gostls13.git cmd/compile: constant fold SHLxconst of a constant on amd64 These rules fire in particular when comparing to a constant string of length two. They should trigger even more after CL 220499. file before after Δ % compile 20639976 20635880 -4096 -0.020% total 116003456 115999360 -4096 -0.004% Change-Id: I21c1c02cf32d710d7a4eb12efab00f02796ccb84 Reviewed-on: https://go-review.googlesource.com/c/go/+/220694 Run-TryBot: Josh Bleecher Snyder TryBot-Result: Gobot Gobot Reviewed-by: Keith Randall --- diff --git a/src/cmd/compile/internal/ssa/gen/AMD64.rules b/src/cmd/compile/internal/ssa/gen/AMD64.rules index 25b618d82e..0b8115249e 100644 --- a/src/cmd/compile/internal/ssa/gen/AMD64.rules +++ b/src/cmd/compile/internal/ssa/gen/AMD64.rules @@ -1482,6 +1482,9 @@ (XORQ x x) -> (MOVQconst [0]) (XORL x x) -> (MOVLconst [0]) +(SHLLconst [d] (MOVLconst [c])) -> (MOVLconst [int64(int32(c)) << uint64(d)]) +(SHLQconst [d] (MOVQconst [c])) -> (MOVQconst [c << uint64(d)]) + // Fold NEG into ADDconst/MULconst. Take care to keep c in 32 bit range. (NEGQ (ADDQconst [c] (NEGQ x))) && c != -(1<<31) -> (ADDQconst [-c] x) (MULQconst [c] (NEGQ x)) && c != -(1<<31) -> (MULQconst [-c] x) diff --git a/src/cmd/compile/internal/ssa/rewriteAMD64.go b/src/cmd/compile/internal/ssa/rewriteAMD64.go index 7cbac3cb1c..0a2669e124 100644 --- a/src/cmd/compile/internal/ssa/rewriteAMD64.go +++ b/src/cmd/compile/internal/ssa/rewriteAMD64.go @@ -31354,6 +31354,18 @@ func rewriteValueAMD64_OpAMD64SHLLconst(v *Value) bool { v.AddArg(x) return true } + // match: (SHLLconst [d] (MOVLconst [c])) + // result: (MOVLconst [int64(int32(c)) << uint64(d)]) + for { + d := v.AuxInt + if v_0.Op != OpAMD64MOVLconst { + break + } + c := v_0.AuxInt + v.reset(OpAMD64MOVLconst) + v.AuxInt = int64(int32(c)) << uint64(d) + return true + } return false } func rewriteValueAMD64_OpAMD64SHLQ(v *Value) bool { @@ -31586,6 +31598,18 @@ func rewriteValueAMD64_OpAMD64SHLQconst(v *Value) bool { v.AddArg(x) return true } + // match: (SHLQconst [d] (MOVQconst [c])) + // result: (MOVQconst [c << uint64(d)]) + for { + d := v.AuxInt + if v_0.Op != OpAMD64MOVQconst { + break + } + c := v_0.AuxInt + v.reset(OpAMD64MOVQconst) + v.AuxInt = c << uint64(d) + return true + } return false } func rewriteValueAMD64_OpAMD64SHRB(v *Value) bool {