Skip to content

Commit 6897030

Browse files
benshi001cherrymui
authored andcommitted
cmd/internal/obj: continue to optimize ARM's constant pool
Both Keith's https://go-review.googlesource.com/c/41612/ and and Ben's https://go-review.googlesource.com/c/41679/ optimized ARM's constant pool. But neither was complete. First, BIC was forgotten. 1. "BIC $0xff00ff00, Reg" can be optimized to "BIC $0xff000000, Reg BIC $0x0000ff00, Reg" 2. "BIC $0xffff00ff, Reg" can be optimized to "AND $0x0000ff00, Reg" 3. "AND $0xffff00ff, Reg" can be optimized to "BIC $0x0000ff00, Reg" Second, break a non-ARMImmRot to the subtraction of two ARMImmRots was left as TODO. 1. "ADD $0x00fffff0, Reg" can be optimized to "ADD $0x01000000, Reg SUB $0x00000010, Reg" 2. "SUB $0x00fffff0, Reg" can be optimized to "SUB $0x01000000, Reg ADD $0x00000010, Reg" This patch fixes them and issue #19844. The go1 benchmark shows improvements. name old time/op new time/op delta BinaryTree17-4 41.4s ± 1% 41.7s ± 1% +0.54% (p=0.000 n=50+49) Fannkuch11-4 24.7s ± 1% 25.1s ± 0% +1.70% (p=0.000 n=50+49) FmtFprintfEmpty-4 853ns ± 1% 852ns ± 1% ~ (p=0.833 n=50+50) FmtFprintfString-4 1.33µs ± 1% 1.33µs ± 1% ~ (p=0.163 n=50+50) FmtFprintfInt-4 1.40µs ± 1% 1.40µs ± 0% ~ (p=0.293 n=50+35) FmtFprintfIntInt-4 2.09µs ± 1% 2.08µs ± 1% -0.39% (p=0.000 n=50+49) FmtFprintfPrefixedInt-4 2.43µs ± 1% 2.43µs ± 1% ~ (p=0.552 n=50+50) FmtFprintfFloat-4 4.57µs ± 1% 4.42µs ± 1% -3.18% (p=0.000 n=50+50) FmtManyArgs-4 8.62µs ± 1% 8.52µs ± 0% -1.08% (p=0.000 n=50+50) GobDecode-4 101ms ± 1% 101ms ± 2% +0.45% (p=0.001 n=49+49) GobEncode-4 90.7ms ± 1% 91.1ms ± 2% +0.51% (p=0.001 n=50+50) Gzip-4 4.23s ± 1% 4.21s ± 1% -0.62% (p=0.000 n=50+50) Gunzip-4 623ms ± 1% 619ms ± 0% -0.63% (p=0.000 n=50+42) HTTPClientServer-4 721µs ± 5% 683µs ± 3% -5.25% (p=0.000 n=50+47) JSONEncode-4 251ms ± 1% 253ms ± 1% +0.54% (p=0.000 n=49+50) JSONDecode-4 941ms ± 1% 944ms ± 1% +0.30% (p=0.001 n=49+50) Mandelbrot200-4 49.3ms ± 1% 49.3ms ± 0% ~ (p=0.918 n=50+48) GoParse-4 47.1ms ± 1% 47.2ms ± 1% +0.18% (p=0.025 n=50+50) RegexpMatchEasy0_32-4 1.23µs ± 1% 1.24µs ± 1% +0.30% (p=0.000 n=49+50) RegexpMatchEasy0_1K-4 7.74µs ± 7% 7.76µs ± 5% ~ (p=0.888 n=50+50) RegexpMatchEasy1_32-4 1.32µs ± 1% 1.32µs ± 1% +0.23% (p=0.003 n=50+50) RegexpMatchEasy1_1K-4 10.6µs ± 2% 10.5µs ± 3% -1.29% (p=0.000 n=49+50) RegexpMatchMedium_32-4 2.19µs ± 1% 2.10µs ± 1% -3.79% (p=0.000 n=49+49) RegexpMatchMedium_1K-4 544µs ± 0% 545µs ± 0% ~ (p=0.123 n=41+50) RegexpMatchHard_32-4 28.8µs ± 0% 28.8µs ± 1% ~ (p=0.580 n=46+50) RegexpMatchHard_1K-4 863µs ± 1% 865µs ± 1% +0.31% (p=0.027 n=47+50) Revcomp-4 82.2ms ± 2% 82.3ms ± 2% ~ (p=0.894 n=48+49) Template-4 1.06s ± 1% 1.04s ± 1% -1.18% (p=0.000 n=50+49) TimeParse-4 7.25µs ± 1% 7.35µs ± 0% +1.48% (p=0.000 n=50+50) TimeFormat-4 13.3µs ± 1% 13.2µs ± 1% -0.13% (p=0.007 n=50+50) [Geo mean] 736µs 733µs -0.37% name old speed new speed delta GobDecode-4 7.60MB/s ± 1% 7.56MB/s ± 2% -0.46% (p=0.001 n=49+49) GobEncode-4 8.47MB/s ± 1% 8.42MB/s ± 2% -0.50% (p=0.001 n=50+50) Gzip-4 4.58MB/s ± 1% 4.61MB/s ± 1% +0.59% (p=0.000 n=50+50) Gunzip-4 31.2MB/s ± 1% 31.4MB/s ± 0% +0.63% (p=0.000 n=50+42) JSONEncode-4 7.73MB/s ± 1% 7.69MB/s ± 1% -0.53% (p=0.000 n=49+50) JSONDecode-4 2.06MB/s ± 1% 2.06MB/s ± 1% ~ (p=0.052 n=44+50) GoParse-4 1.23MB/s ± 0% 1.23MB/s ± 2% ~ (p=0.526 n=26+50) RegexpMatchEasy0_32-4 25.9MB/s ± 1% 25.9MB/s ± 1% -0.30% (p=0.000 n=49+50) RegexpMatchEasy0_1K-4 132MB/s ± 7% 132MB/s ± 6% ~ (p=0.885 n=50+50) RegexpMatchEasy1_32-4 24.2MB/s ± 1% 24.1MB/s ± 1% -0.22% (p=0.003 n=50+50) RegexpMatchEasy1_1K-4 96.4MB/s ± 2% 97.8MB/s ± 3% +1.36% (p=0.000 n=50+50) RegexpMatchMedium_32-4 460kB/s ± 0% 476kB/s ± 1% +3.43% (p=0.000 n=49+50) RegexpMatchMedium_1K-4 1.88MB/s ± 0% 1.88MB/s ± 0% ~ (all equal) RegexpMatchHard_32-4 1.11MB/s ± 0% 1.11MB/s ± 1% +0.34% (p=0.000 n=45+50) RegexpMatchHard_1K-4 1.19MB/s ± 1% 1.18MB/s ± 1% -0.34% (p=0.033 n=50+50) Revcomp-4 30.9MB/s ± 2% 30.9MB/s ± 2% ~ (p=0.894 n=48+49) Template-4 1.84MB/s ± 1% 1.86MB/s ± 2% +1.19% (p=0.000 n=48+50) [Geo mean] 6.63MB/s 6.65MB/s +0.26% Fixes #19844. Change-Id: I5ad16cc0b29267bb4579aca3dcc10a0b8ade1aa4 Reviewed-on: https://go-review.googlesource.com/42430 Run-TryBot: Cherry Zhang <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent 19b05ac commit 6897030

File tree

6 files changed

+225
-58
lines changed

6 files changed

+225
-58
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,8 @@
798798
// generic constant folding
799799
(ADDconst [c] x) && !isARMImmRot(uint32(c)) && isARMImmRot(uint32(-c)) -> (SUBconst [int64(int32(-c))] x)
800800
(SUBconst [c] x) && !isARMImmRot(uint32(c)) && isARMImmRot(uint32(-c)) -> (ADDconst [int64(int32(-c))] x)
801+
(ANDconst [c] x) && !isARMImmRot(uint32(c)) && isARMImmRot(^uint32(c)) -> (BICconst [int64(^uint32(c))] x)
802+
(BICconst [c] x) && !isARMImmRot(uint32(c)) && isARMImmRot(^uint32(c)) -> (ANDconst [int64(^uint32(c))] x)
801803
(ADDconst [c] (MOVWconst [d])) -> (MOVWconst [int64(int32(c+d))])
802804
(ADDconst [c] (ADDconst [d] x)) -> (ADDconst [int64(int32(c+d))] x)
803805
(ADDconst [c] (SUBconst [d] x)) -> (ADDconst [int64(int32(c-d))] x)

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

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/internal/obj/arm/a.out.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,11 @@ const (
121121
C_PSR
122122
C_FCR
123123

124-
C_RCON /* 0xff rotated */
125-
C_NCON /* ~RCON */
126-
C_RCON2 /* OR of two disjoint C_RCON constants */
127-
C_SCON /* 0xffff */
124+
C_RCON /* 0xff rotated */
125+
C_NCON /* ~RCON */
126+
C_RCON2A /* OR of two disjoint C_RCON constants */
127+
C_RCON2S /* subtraction of two disjoint C_RCON constants */
128+
C_SCON /* 0xffff */
128129
C_LCON
129130
C_LCONADDR
130131
C_ZFCON

src/cmd/internal/obj/arm/anames5.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ var cnames5 = []string{
1616
"FCR",
1717
"RCON",
1818
"NCON",
19-
"RCON2",
19+
"RCON2A",
20+
"RCON2S",
2021
"SCON",
2122
"LCON",
2223
"LCONADDR",

src/cmd/internal/obj/arm/asm5.go

+88-19
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,26 @@ var optab = []Optab{
8888
{AADD, C_REG, C_NONE, C_REG, 1, 4, 0, 0, 0},
8989
{AAND, C_REG, C_REG, C_REG, 1, 4, 0, 0, 0},
9090
{AAND, C_REG, C_NONE, C_REG, 1, 4, 0, 0, 0},
91+
{AORR, C_REG, C_REG, C_REG, 1, 4, 0, 0, 0},
92+
{AORR, C_REG, C_NONE, C_REG, 1, 4, 0, 0, 0},
9193
{AMOVW, C_REG, C_NONE, C_REG, 1, 4, 0, 0, 0},
9294
{AMVN, C_REG, C_NONE, C_REG, 1, 4, 0, 0, 0},
9395
{ACMP, C_REG, C_REG, C_NONE, 1, 4, 0, 0, 0},
9496
{AADD, C_RCON, C_REG, C_REG, 2, 4, 0, 0, 0},
9597
{AADD, C_RCON, C_NONE, C_REG, 2, 4, 0, 0, 0},
9698
{AAND, C_RCON, C_REG, C_REG, 2, 4, 0, 0, 0},
9799
{AAND, C_RCON, C_NONE, C_REG, 2, 4, 0, 0, 0},
100+
{AORR, C_RCON, C_REG, C_REG, 2, 4, 0, 0, 0},
101+
{AORR, C_RCON, C_NONE, C_REG, 2, 4, 0, 0, 0},
98102
{AMOVW, C_RCON, C_NONE, C_REG, 2, 4, 0, 0, 0},
99103
{AMVN, C_RCON, C_NONE, C_REG, 2, 4, 0, 0, 0},
100104
{ACMP, C_RCON, C_REG, C_NONE, 2, 4, 0, 0, 0},
101105
{AADD, C_SHIFT, C_REG, C_REG, 3, 4, 0, 0, 0},
102106
{AADD, C_SHIFT, C_NONE, C_REG, 3, 4, 0, 0, 0},
103107
{AAND, C_SHIFT, C_REG, C_REG, 3, 4, 0, 0, 0},
104108
{AAND, C_SHIFT, C_NONE, C_REG, 3, 4, 0, 0, 0},
109+
{AORR, C_SHIFT, C_REG, C_REG, 3, 4, 0, 0, 0},
110+
{AORR, C_SHIFT, C_NONE, C_REG, 3, 4, 0, 0, 0},
105111
{AMVN, C_SHIFT, C_NONE, C_REG, 3, 4, 0, 0, 0},
106112
{ACMP, C_SHIFT, C_REG, C_NONE, 3, 4, 0, 0, 0},
107113
{AMOVW, C_RACON, C_NONE, C_REG, 4, 4, REGSP, 0, 0},
@@ -136,20 +142,27 @@ var optab = []Optab{
136142
{AADD, C_NCON, C_NONE, C_REG, 13, 8, 0, 0, 0},
137143
{AAND, C_NCON, C_REG, C_REG, 13, 8, 0, 0, 0},
138144
{AAND, C_NCON, C_NONE, C_REG, 13, 8, 0, 0, 0},
145+
{AORR, C_NCON, C_REG, C_REG, 13, 8, 0, 0, 0},
146+
{AORR, C_NCON, C_NONE, C_REG, 13, 8, 0, 0, 0},
139147
{AMVN, C_NCON, C_NONE, C_REG, 13, 8, 0, 0, 0},
140148
{ACMP, C_NCON, C_REG, C_NONE, 13, 8, 0, 0, 0},
141149
{AADD, C_SCON, C_REG, C_REG, 13, 8, 0, 0, 0},
142150
{AADD, C_SCON, C_NONE, C_REG, 13, 8, 0, 0, 0},
143151
{AAND, C_SCON, C_REG, C_REG, 13, 8, 0, 0, 0},
144152
{AAND, C_SCON, C_NONE, C_REG, 13, 8, 0, 0, 0},
153+
{AORR, C_SCON, C_REG, C_REG, 13, 8, 0, 0, 0},
154+
{AORR, C_SCON, C_NONE, C_REG, 13, 8, 0, 0, 0},
145155
{AMVN, C_SCON, C_NONE, C_REG, 13, 8, 0, 0, 0},
146156
{ACMP, C_SCON, C_REG, C_NONE, 13, 8, 0, 0, 0},
147-
{AADD, C_RCON2, C_REG, C_REG, 106, 8, 0, 0, 0},
148-
// TODO: RCON2: how to do AND and BIC?
157+
{AADD, C_RCON2A, C_REG, C_REG, 106, 8, 0, 0, 0},
158+
{AORR, C_RCON2A, C_REG, C_REG, 106, 8, 0, 0, 0},
159+
{AADD, C_RCON2S, C_REG, C_REG, 107, 8, 0, 0, 0},
149160
{AADD, C_LCON, C_REG, C_REG, 13, 8, 0, LFROM, 0},
150161
{AADD, C_LCON, C_NONE, C_REG, 13, 8, 0, LFROM, 0},
151162
{AAND, C_LCON, C_REG, C_REG, 13, 8, 0, LFROM, 0},
152163
{AAND, C_LCON, C_NONE, C_REG, 13, 8, 0, LFROM, 0},
164+
{AORR, C_LCON, C_REG, C_REG, 13, 8, 0, LFROM, 0},
165+
{AORR, C_LCON, C_NONE, C_REG, 13, 8, 0, LFROM, 0},
153166
{AMVN, C_LCON, C_NONE, C_REG, 13, 8, 0, LFROM, 0},
154167
{ACMP, C_LCON, C_REG, C_NONE, 13, 8, 0, LFROM, 0},
155168
{AMOVB, C_REG, C_NONE, C_REG, 1, 4, 0, 0, 0},
@@ -970,10 +983,10 @@ func immrot(v uint32) int32 {
970983
return 0
971984
}
972985

973-
// immrot2 returns bits encoding the immediate constant fields of two instructions,
986+
// immrot2a returns bits encoding the immediate constant fields of two instructions,
974987
// such that the encoded constants x, y satisfy x|y==v, x&y==0.
975988
// Returns 0,0 if no such decomposition of v exists.
976-
func immrot2(v uint32) (uint32, uint32) {
989+
func immrot2a(v uint32) (uint32, uint32) {
977990
for i := uint(1); i < 32; i++ {
978991
m := uint32(1<<i - 1)
979992
if x, y := immrot(v&m), immrot(v&^m); x != 0 && y != 0 {
@@ -985,6 +998,32 @@ func immrot2(v uint32) (uint32, uint32) {
985998
return 0, 0
986999
}
9871000

1001+
// immrot2s returns bits encoding the immediate constant fields of two instructions,
1002+
// such that the encoded constants y, x satisfy y-x==v, y&x==0.
1003+
// Returns 0,0 if no such decomposition of v exists.
1004+
func immrot2s(v uint32) (uint32, uint32) {
1005+
if immrot(v) == 0 {
1006+
return v, 0
1007+
}
1008+
// suppose v in the form of {leading 00, upper effective bits, lower 8 effective bits, trailing 00}
1009+
// omit trailing 00
1010+
var i uint32
1011+
for i = 2; i < 32; i += 2 {
1012+
if v&(1<<i-1) != 0 {
1013+
break
1014+
}
1015+
}
1016+
// i must be <= 24, then adjust i just above lower 8 effective bits of v
1017+
i += 6
1018+
// let x = {the complement of lower 8 effective bits, trailing 00}, y = x + v
1019+
x := 1<<i - v&(1<<i-1)
1020+
y := v + x
1021+
if y, x = uint32(immrot(y)), uint32(immrot(x)); y != 0 && x != 0 {
1022+
return y, x
1023+
}
1024+
return 0, 0
1025+
}
1026+
9881027
func immaddr(v int32) int32 {
9891028
if v >= 0 && v <= 0xfff {
9901029
return v&0xfff | 1<<24 | 1<<23 /* pre indexing */ /* pre indexing, up */
@@ -1159,8 +1198,11 @@ func (c *ctxt5) aclass(a *obj.Addr) int {
11591198
if uint32(c.instoffset) <= 0xffff && objabi.GOARM == 7 {
11601199
return C_SCON
11611200
}
1162-
if x, y := immrot2(uint32(c.instoffset)); x != 0 && y != 0 {
1163-
return C_RCON2
1201+
if x, y := immrot2a(uint32(c.instoffset)); x != 0 && y != 0 {
1202+
return C_RCON2A
1203+
}
1204+
if y, x := immrot2s(uint32(c.instoffset)); x != 0 && y != 0 {
1205+
return C_RCON2S
11641206
}
11651207
return C_LCON
11661208

@@ -1226,13 +1268,12 @@ func (c *ctxt5) oplook(p *obj.Prog) *Optab {
12261268
a2 = C_REG
12271269
}
12281270

1229-
// If Scond != 0, we must use the constant pool instead of
1230-
// splitting the instruction in two. The most common reason is
1231-
// .S (flag updating) instructions. There may be others.
1232-
if a1 == C_RCON2 && p.Scond != 0 {
1271+
// If current instruction has a .S suffix (flags update),
1272+
// we must use the constant pool instead of splitting it.
1273+
if (a1 == C_RCON2A || a1 == C_RCON2S) && p.Scond&C_SBIT != 0 {
12331274
a1 = C_LCON
12341275
}
1235-
if a3 == C_RCON2 && p.Scond != 0 {
1276+
if (a3 == C_RCON2A || a3 == C_RCON2S) && p.Scond&C_SBIT != 0 {
12361277
a3 = C_LCON
12371278
}
12381279

@@ -1266,7 +1307,7 @@ func cmp(a int, b int) bool {
12661307
}
12671308
switch a {
12681309
case C_LCON:
1269-
if b == C_RCON || b == C_NCON || b == C_SCON || b == C_RCON2 {
1310+
if b == C_RCON || b == C_NCON || b == C_SCON || b == C_RCON2A || b == C_RCON2S {
12701311
return true
12711312
}
12721313

@@ -1406,16 +1447,14 @@ func buildop(ctxt *obj.Link) {
14061447
log.Fatalf("bad code")
14071448

14081449
case AADD:
1409-
opset(AEOR, r0)
14101450
opset(ASUB, r0)
14111451
opset(ARSB, r0)
14121452
opset(AADC, r0)
14131453
opset(ASBC, r0)
14141454
opset(ARSC, r0)
1415-
opset(AORR, r0)
14161455

1417-
case AAND:
1418-
opset(AAND, r0)
1456+
case AORR:
1457+
opset(AEOR, r0)
14191458
opset(ABIC, r0)
14201459

14211460
case ACMP:
@@ -1541,6 +1580,7 @@ func buildop(ctxt *obj.Link) {
15411580
ALDREXD,
15421581
ASTREXD,
15431582
APLD,
1583+
AAND,
15441584
obj.AUNDEF,
15451585
obj.AFUNCDATA,
15461586
obj.APCDATA,
@@ -1609,11 +1649,11 @@ func (c *ctxt5) asmout(p *obj.Prog, o *Optab, out []uint32) {
16091649
c.aclass(&p.From)
16101650
r := int(p.Reg)
16111651
rt := int(p.To.Reg)
1612-
x, y := immrot2(uint32(c.instoffset))
1652+
x, y := immrot2a(uint32(c.instoffset))
16131653
var as2 obj.As
16141654
switch p.As {
1615-
case AADD, ASUB, AORR, AEOR:
1616-
as2 = p.As // ADD, SUB, ORR, EOR
1655+
case AADD, ASUB, AORR, AEOR, ABIC:
1656+
as2 = p.As // ADD, SUB, ORR, EOR, BIC
16171657
case ARSB:
16181658
as2 = AADD // RSB -> RSB/ADD pair
16191659
case AADC:
@@ -1632,6 +1672,35 @@ func (c *ctxt5) asmout(p *obj.Prog, o *Optab, out []uint32) {
16321672
o1 |= x
16331673
o2 |= y
16341674

1675+
case 107: /* op $I,R,R where I can be decomposed into 2 immediates */
1676+
c.aclass(&p.From)
1677+
r := int(p.Reg)
1678+
rt := int(p.To.Reg)
1679+
y, x := immrot2s(uint32(c.instoffset))
1680+
var as2 obj.As
1681+
switch p.As {
1682+
case AADD:
1683+
as2 = ASUB // ADD -> ADD/SUB pair
1684+
case ASUB:
1685+
as2 = AADD // SUB -> SUB/ADD pair
1686+
case ARSB:
1687+
as2 = ASUB // RSB -> RSB/SUB pair
1688+
case AADC:
1689+
as2 = ASUB // ADC -> ADC/SUB pair
1690+
case ASBC:
1691+
as2 = AADD // SBC -> SBC/ADD pair
1692+
case ARSC:
1693+
as2 = ASUB // RSC -> RSC/SUB pair
1694+
default:
1695+
c.ctxt.Diag("unknown second op for %v", p)
1696+
}
1697+
o1 = c.oprrr(p, p.As, int(p.Scond))
1698+
o2 = c.oprrr(p, as2, int(p.Scond))
1699+
o1 |= (uint32(r)&15)<<16 | (uint32(rt)&15)<<12
1700+
o2 |= (uint32(rt)&15)<<16 | (uint32(rt)&15)<<12
1701+
o1 |= y
1702+
o2 |= x
1703+
16351704
case 3: /* add R<<[IR],[R],R */
16361705
o1 = c.mov(p)
16371706

0 commit comments

Comments
 (0)