Skip to content

Commit a9ba3e3

Browse files
neelancebradfitz
authored andcommitted
cmd/compile: add SSA config options useAvg and useHmul
This commit allows architectures to disable optimizations that need the Avg* and Hmul* operations. WebAssembly has no such operations, so using them as an optimization but then having to emulate them with multiple instructions makes no sense, especially since the WebAssembly compiler may do the same optimizations internally. Updates #18892 Change-Id: If57b59e3235482a9e0ec334a7312b3e3b5fc2b61 Reviewed-on: https://go-review.googlesource.com/103256 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Josh Bleecher Snyder <[email protected]>
1 parent 80e6922 commit a9ba3e3

File tree

3 files changed

+42
-36
lines changed

3 files changed

+42
-36
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type Config struct {
3434
optimize bool // Do optimization
3535
noDuffDevice bool // Don't use Duff's device
3636
useSSE bool // Use SSE for non-float operations
37+
useAvg bool // Use optimizations that need Avg* operations
38+
useHmul bool // Use optimizations that need Hmul* operations
3739
nacl bool // GOOS=nacl
3840
use387 bool // GO386=387
3941
SoftFloat bool //
@@ -190,6 +192,8 @@ const (
190192
// NewConfig returns a new configuration object for the given architecture.
191193
func NewConfig(arch string, types Types, ctxt *obj.Link, optimize bool) *Config {
192194
c := &Config{arch: arch, Types: types}
195+
c.useAvg = true
196+
c.useHmul = true
193197
switch arch {
194198
case "amd64":
195199
c.PtrSize = 8

src/cmd/compile/internal/ssa/gen/generic.rules

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@
845845
(Const32 <typ.UInt32> [int64(1<<15+(umagic(16,c).m+1)/2)])
846846
(Rsh32Ux64 <typ.UInt32> (ZeroExt16to32 x) (Const64 <typ.UInt64> [1])))
847847
(Const64 <typ.UInt64> [16+umagic(16,c).s-2])))
848-
(Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 ->
848+
(Div16u x (Const16 [c])) && umagicOK(16, c) && config.RegSize == 4 && config.useAvg ->
849849
(Trunc32to16
850850
(Rsh32Ux64 <typ.UInt32>
851851
(Avg32u
@@ -856,19 +856,19 @@
856856
(Const64 <typ.UInt64> [16+umagic(16,c).s-1])))
857857

858858
// For 32-bit divides on 32-bit machines
859-
(Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && umagic(32,c).m&1 == 0 ->
859+
(Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && umagic(32,c).m&1 == 0 && config.useHmul ->
860860
(Rsh32Ux64 <typ.UInt32>
861861
(Hmul32u <typ.UInt32>
862862
(Const32 <typ.UInt32> [int64(int32(1<<31+umagic(32,c).m/2))])
863863
x)
864864
(Const64 <typ.UInt64> [umagic(32,c).s-1]))
865-
(Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && c&1 == 0 ->
865+
(Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && c&1 == 0 && config.useHmul ->
866866
(Rsh32Ux64 <typ.UInt32>
867867
(Hmul32u <typ.UInt32>
868868
(Const32 <typ.UInt32> [int64(int32(1<<31+(umagic(32,c).m+1)/2))])
869869
(Rsh32Ux64 <typ.UInt32> x (Const64 <typ.UInt64> [1])))
870870
(Const64 <typ.UInt64> [umagic(32,c).s-2]))
871-
(Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 ->
871+
(Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 4 && config.useAvg && config.useHmul ->
872872
(Rsh32Ux64 <typ.UInt32>
873873
(Avg32u
874874
x
@@ -893,7 +893,7 @@
893893
(Const64 <typ.UInt64> [int64(1<<31+(umagic(32,c).m+1)/2)])
894894
(Rsh64Ux64 <typ.UInt64> (ZeroExt32to64 x) (Const64 <typ.UInt64> [1])))
895895
(Const64 <typ.UInt64> [32+umagic(32,c).s-2])))
896-
(Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 ->
896+
(Div32u x (Const32 [c])) && umagicOK(32, c) && config.RegSize == 8 && config.useAvg ->
897897
(Trunc64to32
898898
(Rsh64Ux64 <typ.UInt64>
899899
(Avg64u
@@ -905,19 +905,19 @@
905905

906906
// For 64-bit divides on 64-bit machines
907907
// (64-bit divides on 32-bit machines are lowered to a runtime call by the walk pass.)
908-
(Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && umagic(64,c).m&1 == 0 ->
908+
(Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && umagic(64,c).m&1 == 0 && config.useHmul ->
909909
(Rsh64Ux64 <typ.UInt64>
910910
(Hmul64u <typ.UInt64>
911911
(Const64 <typ.UInt64> [int64(1<<63+umagic(64,c).m/2)])
912912
x)
913913
(Const64 <typ.UInt64> [umagic(64,c).s-1]))
914-
(Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && c&1 == 0 ->
914+
(Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && c&1 == 0 && config.useHmul ->
915915
(Rsh64Ux64 <typ.UInt64>
916916
(Hmul64u <typ.UInt64>
917917
(Const64 <typ.UInt64> [int64(1<<63+(umagic(64,c).m+1)/2)])
918918
(Rsh64Ux64 <typ.UInt64> x (Const64 <typ.UInt64> [1])))
919919
(Const64 <typ.UInt64> [umagic(64,c).s-2]))
920-
(Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 ->
920+
(Div64u x (Const64 [c])) && umagicOK(64, c) && config.RegSize == 8 && config.useAvg && config.useHmul ->
921921
(Rsh64Ux64 <typ.UInt64>
922922
(Avg64u
923923
x
@@ -992,7 +992,7 @@
992992
(Rsh64x64 <t>
993993
(SignExt32to64 x)
994994
(Const64 <typ.UInt64> [63])))
995-
(Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 == 0 ->
995+
(Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 == 0 && config.useHmul ->
996996
(Sub32 <t>
997997
(Rsh32x64 <t>
998998
(Hmul32 <t>
@@ -1002,7 +1002,7 @@
10021002
(Rsh32x64 <t>
10031003
x
10041004
(Const64 <typ.UInt64> [31])))
1005-
(Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 != 0 ->
1005+
(Div32 <t> x (Const32 [c])) && smagicOK(32,c) && config.RegSize == 4 && smagic(32,c).m&1 != 0 && config.useHmul ->
10061006
(Sub32 <t>
10071007
(Rsh32x64 <t>
10081008
(Add32 <t>
@@ -1014,7 +1014,7 @@
10141014
(Rsh32x64 <t>
10151015
x
10161016
(Const64 <typ.UInt64> [31])))
1017-
(Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 == 0 ->
1017+
(Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 == 0 && config.useHmul ->
10181018
(Sub64 <t>
10191019
(Rsh64x64 <t>
10201020
(Hmul64 <t>
@@ -1024,7 +1024,7 @@
10241024
(Rsh64x64 <t>
10251025
x
10261026
(Const64 <typ.UInt64> [63])))
1027-
(Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 != 0 ->
1027+
(Div64 <t> x (Const64 [c])) && smagicOK(64,c) && smagic(64,c).m&1 != 0 && config.useHmul ->
10281028
(Sub64 <t>
10291029
(Rsh64x64 <t>
10301030
(Add64 <t>

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

Lines changed: 26 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)