Skip to content

Commit def4448

Browse files
randall77bradfitz
authored andcommitted
[release-branch.go1.20] cmd/compile: use correct type for byteswaps on multi-byte stores
Use the type of the store for the byteswap, not the type of the store's value argument. Normally when we're storing a 16-bit value, the value being stored is also typed as 16 bits. But sometimes it is typed as something smaller, usually because it is the result of an upcast from a smaller value, and that upcast needs no instructions. If the type of the store's arg is thinner than the type being stored, and the byteswap'd value uses that thinner type, and the byteswap'd value needs to be spilled & restored, that spill/restore happens using the thinner type, which causes us to lose some of the top bits of the value. Fixes golang#59374 Change-Id: If6ce1e8a76f18bf8e9d79871b6caa438bc3cce4d Reviewed-on: https://go-review.googlesource.com/c/go/+/481395 Reviewed-by: David Chase <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gopher Robot <[email protected]> (cherry picked from commit b3bc862) Reviewed-on: https://go-review.googlesource.com/c/go/+/483176 Auto-Submit: Dmitri Shuralyov <[email protected]>
1 parent fd493ff commit def4448

File tree

5 files changed

+154
-72
lines changed

5 files changed

+154
-72
lines changed

src/cmd/compile/internal/ssa/_gen/AMD64.rules

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,13 +1757,13 @@
17571757
x0:(MOVBstore [i-1] {s} p (SHRWconst [8] w) mem))
17581758
&& x0.Uses == 1
17591759
&& clobber(x0)
1760-
=> (MOVWstore [i-1] {s} p (ROLWconst <w.Type> [8] w) mem)
1760+
=> (MOVWstore [i-1] {s} p (ROLWconst <typ.UInt16> [8] w) mem)
17611761
(MOVBstore [i] {s} p1 w
17621762
x0:(MOVBstore [i] {s} p0 (SHRWconst [8] w) mem))
17631763
&& x0.Uses == 1
17641764
&& sequentialAddresses(p0, p1, 1)
17651765
&& clobber(x0)
1766-
=> (MOVWstore [i] {s} p0 (ROLWconst <w.Type> [8] w) mem)
1766+
=> (MOVWstore [i] {s} p0 (ROLWconst <typ.UInt16> [8] w) mem)
17671767

17681768
// Combine stores + shifts into bswap and larger (unaligned) stores
17691769
(MOVBstore [i] {s} p w
@@ -1774,7 +1774,7 @@
17741774
&& x1.Uses == 1
17751775
&& x2.Uses == 1
17761776
&& clobber(x0, x1, x2)
1777-
=> (MOVLstore [i-3] {s} p (BSWAPL <w.Type> w) mem)
1777+
=> (MOVLstore [i-3] {s} p (BSWAPL <typ.UInt32> w) mem)
17781778
(MOVBstore [i] {s} p3 w
17791779
x2:(MOVBstore [i] {s} p2 (SHRLconst [8] w)
17801780
x1:(MOVBstore [i] {s} p1 (SHRLconst [16] w)
@@ -1786,7 +1786,7 @@
17861786
&& sequentialAddresses(p1, p2, 1)
17871787
&& sequentialAddresses(p2, p3, 1)
17881788
&& clobber(x0, x1, x2)
1789-
=> (MOVLstore [i] {s} p0 (BSWAPL <w.Type> w) mem)
1789+
=> (MOVLstore [i] {s} p0 (BSWAPL <typ.UInt32> w) mem)
17901790

17911791
(MOVBstore [i] {s} p w
17921792
x6:(MOVBstore [i-1] {s} p (SHRQconst [8] w)
@@ -1804,7 +1804,7 @@
18041804
&& x5.Uses == 1
18051805
&& x6.Uses == 1
18061806
&& clobber(x0, x1, x2, x3, x4, x5, x6)
1807-
=> (MOVQstore [i-7] {s} p (BSWAPQ <w.Type> w) mem)
1807+
=> (MOVQstore [i-7] {s} p (BSWAPQ <typ.UInt64> w) mem)
18081808
(MOVBstore [i] {s} p7 w
18091809
x6:(MOVBstore [i] {s} p6 (SHRQconst [8] w)
18101810
x5:(MOVBstore [i] {s} p5 (SHRQconst [16] w)
@@ -1828,7 +1828,7 @@
18281828
&& sequentialAddresses(p5, p6, 1)
18291829
&& sequentialAddresses(p6, p7, 1)
18301830
&& clobber(x0, x1, x2, x3, x4, x5, x6)
1831-
=> (MOVQstore [i] {s} p0 (BSWAPQ <w.Type> w) mem)
1831+
=> (MOVQstore [i] {s} p0 (BSWAPQ <typ.UInt64> w) mem)
18321832

18331833
// Combine constant stores into larger (unaligned) stores.
18341834
(MOVBstoreconst [c] {s} p1 x:(MOVBstoreconst [a] {s} p0 mem))

src/cmd/compile/internal/ssa/_gen/ARM64.rules

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2821,7 +2821,7 @@
28212821
&& x5.Uses == 1
28222822
&& x6.Uses == 1
28232823
&& clobber(x0, x1, x2, x3, x4, x5, x6)
2824-
=> (MOVDstore [i-7] {s} ptr (REV <w.Type> w) mem)
2824+
=> (MOVDstore [i-7] {s} ptr (REV <typ.UInt64> w) mem)
28252825
(MOVBstore [7] {s} p w
28262826
x0:(MOVBstore [6] {s} p (SRLconst [8] w)
28272827
x1:(MOVBstore [5] {s} p (SRLconst [16] w)
@@ -2841,7 +2841,7 @@
28412841
&& (isSamePtr(ptr0, ptr1) && isSamePtr(idx0, idx1) || isSamePtr(ptr0, idx1) && isSamePtr(idx0, ptr1))
28422842
&& isSamePtr(p1, p)
28432843
&& clobber(x0, x1, x2, x3, x4, x5, x6)
2844-
=> (MOVDstoreidx ptr0 idx0 (REV <w.Type> w) mem)
2844+
=> (MOVDstoreidx ptr0 idx0 (REV <typ.UInt64> w) mem)
28452845
(MOVBstore [i] {s} ptr w
28462846
x0:(MOVBstore [i-1] {s} ptr (UBFX [armBFAuxInt(8, 24)] w)
28472847
x1:(MOVBstore [i-2] {s} ptr (UBFX [armBFAuxInt(16, 16)] w)
@@ -2850,7 +2850,7 @@
28502850
&& x1.Uses == 1
28512851
&& x2.Uses == 1
28522852
&& clobber(x0, x1, x2)
2853-
=> (MOVWstore [i-3] {s} ptr (REVW <w.Type> w) mem)
2853+
=> (MOVWstore [i-3] {s} ptr (REVW <typ.UInt32> w) mem)
28542854
(MOVBstore [3] {s} p w
28552855
x0:(MOVBstore [2] {s} p (UBFX [armBFAuxInt(8, 24)] w)
28562856
x1:(MOVBstore [1] {s} p1:(ADD ptr1 idx1) (UBFX [armBFAuxInt(16, 16)] w)
@@ -2862,7 +2862,7 @@
28622862
&& (isSamePtr(ptr0, ptr1) && isSamePtr(idx0, idx1) || isSamePtr(ptr0, idx1) && isSamePtr(idx0, ptr1))
28632863
&& isSamePtr(p1, p)
28642864
&& clobber(x0, x1, x2)
2865-
=> (MOVWstoreidx ptr0 idx0 (REVW <w.Type> w) mem)
2865+
=> (MOVWstoreidx ptr0 idx0 (REVW <typ.UInt32> w) mem)
28662866
(MOVBstoreidx ptr (ADDconst [3] idx) w
28672867
x0:(MOVBstoreidx ptr (ADDconst [2] idx) (UBFX [armBFAuxInt(8, 24)] w)
28682868
x1:(MOVBstoreidx ptr (ADDconst [1] idx) (UBFX [armBFAuxInt(16, 16)] w)
@@ -2871,7 +2871,7 @@
28712871
&& x1.Uses == 1
28722872
&& x2.Uses == 1
28732873
&& clobber(x0, x1, x2)
2874-
=> (MOVWstoreidx ptr idx (REVW <w.Type> w) mem)
2874+
=> (MOVWstoreidx ptr idx (REVW <typ.UInt32> w) mem)
28752875
(MOVBstoreidx ptr idx w
28762876
x0:(MOVBstoreidx ptr (ADDconst [1] idx) (UBFX [armBFAuxInt(8, 24)] w)
28772877
x1:(MOVBstoreidx ptr (ADDconst [2] idx) (UBFX [armBFAuxInt(16, 16)] w)
@@ -2889,7 +2889,7 @@
28892889
&& x1.Uses == 1
28902890
&& x2.Uses == 1
28912891
&& clobber(x0, x1, x2)
2892-
=> (MOVWstore [i-3] {s} ptr (REVW <w.Type> w) mem)
2892+
=> (MOVWstore [i-3] {s} ptr (REVW <typ.UInt32> w) mem)
28932893
(MOVBstore [3] {s} p w
28942894
x0:(MOVBstore [2] {s} p (SRLconst [8] (MOVDreg w))
28952895
x1:(MOVBstore [1] {s} p1:(ADD ptr1 idx1) (SRLconst [16] (MOVDreg w))
@@ -2901,7 +2901,7 @@
29012901
&& (isSamePtr(ptr0, ptr1) && isSamePtr(idx0, idx1) || isSamePtr(ptr0, idx1) && isSamePtr(idx0, ptr1))
29022902
&& isSamePtr(p1, p)
29032903
&& clobber(x0, x1, x2)
2904-
=> (MOVWstoreidx ptr0 idx0 (REVW <w.Type> w) mem)
2904+
=> (MOVWstoreidx ptr0 idx0 (REVW <typ.UInt32> w) mem)
29052905
(MOVBstore [i] {s} ptr w
29062906
x0:(MOVBstore [i-1] {s} ptr (SRLconst [8] w)
29072907
x1:(MOVBstore [i-2] {s} ptr (SRLconst [16] w)
@@ -2910,7 +2910,7 @@
29102910
&& x1.Uses == 1
29112911
&& x2.Uses == 1
29122912
&& clobber(x0, x1, x2)
2913-
=> (MOVWstore [i-3] {s} ptr (REVW <w.Type> w) mem)
2913+
=> (MOVWstore [i-3] {s} ptr (REVW <typ.UInt32> w) mem)
29142914
(MOVBstore [3] {s} p w
29152915
x0:(MOVBstore [2] {s} p (SRLconst [8] w)
29162916
x1:(MOVBstore [1] {s} p1:(ADD ptr1 idx1) (SRLconst [16] w)
@@ -2922,55 +2922,55 @@
29222922
&& (isSamePtr(ptr0, ptr1) && isSamePtr(idx0, idx1) || isSamePtr(ptr0, idx1) && isSamePtr(idx0, ptr1))
29232923
&& isSamePtr(p1, p)
29242924
&& clobber(x0, x1, x2)
2925-
=> (MOVWstoreidx ptr0 idx0 (REVW <w.Type> w) mem)
2925+
=> (MOVWstoreidx ptr0 idx0 (REVW <typ.UInt32> w) mem)
29262926
(MOVBstore [i] {s} ptr w x:(MOVBstore [i-1] {s} ptr (SRLconst [8] w) mem))
29272927
&& x.Uses == 1
29282928
&& clobber(x)
2929-
=> (MOVHstore [i-1] {s} ptr (REV16W <w.Type> w) mem)
2929+
=> (MOVHstore [i-1] {s} ptr (REV16W <typ.UInt16> w) mem)
29302930
(MOVBstore [1] {s} (ADD ptr1 idx1) w x:(MOVBstoreidx ptr0 idx0 (SRLconst [8] w) mem))
29312931
&& x.Uses == 1
29322932
&& s == nil
29332933
&& (isSamePtr(ptr0, ptr1) && isSamePtr(idx0, idx1) || isSamePtr(ptr0, idx1) && isSamePtr(idx0, ptr1))
29342934
&& clobber(x)
2935-
=> (MOVHstoreidx ptr0 idx0 (REV16W <w.Type> w) mem)
2935+
=> (MOVHstoreidx ptr0 idx0 (REV16W <typ.UInt16> w) mem)
29362936
(MOVBstore [i] {s} ptr w x:(MOVBstore [i-1] {s} ptr (UBFX [armBFAuxInt(8, 8)] w) mem))
29372937
&& x.Uses == 1
29382938
&& clobber(x)
2939-
=> (MOVHstore [i-1] {s} ptr (REV16W <w.Type> w) mem)
2939+
=> (MOVHstore [i-1] {s} ptr (REV16W <typ.UInt16> w) mem)
29402940
(MOVBstore [1] {s} (ADD ptr1 idx1) w x:(MOVBstoreidx ptr0 idx0 (UBFX [armBFAuxInt(8, 8)] w) mem))
29412941
&& x.Uses == 1
29422942
&& s == nil
29432943
&& (isSamePtr(ptr0, ptr1) && isSamePtr(idx0, idx1) || isSamePtr(ptr0, idx1) && isSamePtr(idx0, ptr1))
29442944
&& clobber(x)
2945-
=> (MOVHstoreidx ptr0 idx0 (REV16W <w.Type> w) mem)
2945+
=> (MOVHstoreidx ptr0 idx0 (REV16W <typ.UInt16> w) mem)
29462946
(MOVBstoreidx ptr (ADDconst [1] idx) w x:(MOVBstoreidx ptr idx (UBFX [armBFAuxInt(8, 8)] w) mem))
29472947
&& x.Uses == 1
29482948
&& clobber(x)
2949-
=> (MOVHstoreidx ptr idx (REV16W <w.Type> w) mem)
2949+
=> (MOVHstoreidx ptr idx (REV16W <typ.UInt16> w) mem)
29502950
(MOVBstoreidx ptr idx w x:(MOVBstoreidx ptr (ADDconst [1] idx) (UBFX [armBFAuxInt(8, 8)] w) mem))
29512951
&& x.Uses == 1
29522952
&& clobber(x)
29532953
=> (MOVHstoreidx ptr idx w mem)
29542954
(MOVBstore [i] {s} ptr w x:(MOVBstore [i-1] {s} ptr (SRLconst [8] (MOVDreg w)) mem))
29552955
&& x.Uses == 1
29562956
&& clobber(x)
2957-
=> (MOVHstore [i-1] {s} ptr (REV16W <w.Type> w) mem)
2957+
=> (MOVHstore [i-1] {s} ptr (REV16W <typ.UInt16> w) mem)
29582958
(MOVBstore [1] {s} (ADD ptr1 idx1) w x:(MOVBstoreidx ptr0 idx0 (SRLconst [8] (MOVDreg w)) mem))
29592959
&& x.Uses == 1
29602960
&& s == nil
29612961
&& (isSamePtr(ptr0, ptr1) && isSamePtr(idx0, idx1) || isSamePtr(ptr0, idx1) && isSamePtr(idx0, ptr1))
29622962
&& clobber(x)
2963-
=> (MOVHstoreidx ptr0 idx0 (REV16W <w.Type> w) mem)
2963+
=> (MOVHstoreidx ptr0 idx0 (REV16W <typ.UInt16> w) mem)
29642964
(MOVBstore [i] {s} ptr w x:(MOVBstore [i-1] {s} ptr (UBFX [armBFAuxInt(8, 24)] w) mem))
29652965
&& x.Uses == 1
29662966
&& clobber(x)
2967-
=> (MOVHstore [i-1] {s} ptr (REV16W <w.Type> w) mem)
2967+
=> (MOVHstore [i-1] {s} ptr (REV16W <typ.UInt16> w) mem)
29682968
(MOVBstore [1] {s} (ADD ptr1 idx1) w x:(MOVBstoreidx ptr0 idx0 (UBFX [armBFAuxInt(8, 24)] w) mem))
29692969
&& x.Uses == 1
29702970
&& s == nil
29712971
&& (isSamePtr(ptr0, ptr1) && isSamePtr(idx0, idx1) || isSamePtr(ptr0, idx1) && isSamePtr(idx0, ptr1))
29722972
&& clobber(x)
2973-
=> (MOVHstoreidx ptr0 idx0 (REV16W <w.Type> w) mem)
2973+
=> (MOVHstoreidx ptr0 idx0 (REV16W <typ.UInt16> w) mem)
29742974

29752975
// FP simplification
29762976
(FNEGS (FMULS x y)) => (FNMULS x y)

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

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

0 commit comments

Comments
 (0)