Skip to content

Commit 12c58bb

Browse files
committed
cmd/compile: optimize (ZeroExt (Const [c]))
These rules trigger 116 times while running make.bash. And at least for the sample code at #18906 (comment) they are providing optimizations not already present in amd64. Updates #18906 Change-Id: I410a480f566f5ab176fc573fb5ac74f9cffec225 Reviewed-on: https://go-review.googlesource.com/36217 Run-TryBot: Josh Bleecher Snyder <[email protected]> Reviewed-by: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 61fb2f6 commit 12c58bb

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@
6868
(Trunc64to32 (SignExt16to64 x)) -> (SignExt16to32 x)
6969
(Trunc64to32 (SignExt32to64 x)) -> x
7070

71+
(ZeroExt8to16 (Const8 [c])) -> (Const16 [int64( uint8(c))])
72+
(ZeroExt8to32 (Const8 [c])) -> (Const32 [int64( uint8(c))])
73+
(ZeroExt8to64 (Const8 [c])) -> (Const64 [int64( uint8(c))])
74+
(ZeroExt16to32 (Const16 [c])) -> (Const32 [int64(uint16(c))])
75+
(ZeroExt16to64 (Const16 [c])) -> (Const64 [int64(uint16(c))])
76+
(ZeroExt32to64 (Const32 [c])) -> (Const64 [int64(uint32(c))])
77+
(SignExt8to16 (Const8 [c])) -> (Const16 [int64( int8(c))])
78+
(SignExt8to32 (Const8 [c])) -> (Const32 [int64( int8(c))])
79+
(SignExt8to64 (Const8 [c])) -> (Const64 [int64( int8(c))])
80+
(SignExt16to32 (Const16 [c])) -> (Const32 [int64( int16(c))])
81+
(SignExt16to64 (Const16 [c])) -> (Const64 [int64( int16(c))])
82+
(SignExt32to64 (Const32 [c])) -> (Const64 [int64( int32(c))])
83+
7184
// const negation is currently handled by frontend
7285
//(Neg8 (Const8 [c])) -> (Const8 [-c])
7386
//(Neg16 (Const16 [c])) -> (Const16 [-c])

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

+156
Original file line numberDiff line numberDiff line change
@@ -9801,6 +9801,19 @@ func rewriteValuegeneric_OpRsh8x8(v *Value, config *Config) bool {
98019801
func rewriteValuegeneric_OpSignExt16to32(v *Value, config *Config) bool {
98029802
b := v.Block
98039803
_ = b
9804+
// match: (SignExt16to32 (Const16 [c]))
9805+
// cond:
9806+
// result: (Const32 [int64( int16(c))])
9807+
for {
9808+
v_0 := v.Args[0]
9809+
if v_0.Op != OpConst16 {
9810+
break
9811+
}
9812+
c := v_0.AuxInt
9813+
v.reset(OpConst32)
9814+
v.AuxInt = int64(int16(c))
9815+
return true
9816+
}
98049817
// match: (SignExt16to32 (Trunc32to16 x:(Rsh32x64 _ (Const64 [s]))))
98059818
// cond: s >= 16
98069819
// result: x
@@ -9831,6 +9844,19 @@ func rewriteValuegeneric_OpSignExt16to32(v *Value, config *Config) bool {
98319844
func rewriteValuegeneric_OpSignExt16to64(v *Value, config *Config) bool {
98329845
b := v.Block
98339846
_ = b
9847+
// match: (SignExt16to64 (Const16 [c]))
9848+
// cond:
9849+
// result: (Const64 [int64( int16(c))])
9850+
for {
9851+
v_0 := v.Args[0]
9852+
if v_0.Op != OpConst16 {
9853+
break
9854+
}
9855+
c := v_0.AuxInt
9856+
v.reset(OpConst64)
9857+
v.AuxInt = int64(int16(c))
9858+
return true
9859+
}
98349860
// match: (SignExt16to64 (Trunc64to16 x:(Rsh64x64 _ (Const64 [s]))))
98359861
// cond: s >= 48
98369862
// result: x
@@ -9861,6 +9887,19 @@ func rewriteValuegeneric_OpSignExt16to64(v *Value, config *Config) bool {
98619887
func rewriteValuegeneric_OpSignExt32to64(v *Value, config *Config) bool {
98629888
b := v.Block
98639889
_ = b
9890+
// match: (SignExt32to64 (Const32 [c]))
9891+
// cond:
9892+
// result: (Const64 [int64( int32(c))])
9893+
for {
9894+
v_0 := v.Args[0]
9895+
if v_0.Op != OpConst32 {
9896+
break
9897+
}
9898+
c := v_0.AuxInt
9899+
v.reset(OpConst64)
9900+
v.AuxInt = int64(int32(c))
9901+
return true
9902+
}
98649903
// match: (SignExt32to64 (Trunc64to32 x:(Rsh64x64 _ (Const64 [s]))))
98659904
// cond: s >= 32
98669905
// result: x
@@ -9891,6 +9930,19 @@ func rewriteValuegeneric_OpSignExt32to64(v *Value, config *Config) bool {
98919930
func rewriteValuegeneric_OpSignExt8to16(v *Value, config *Config) bool {
98929931
b := v.Block
98939932
_ = b
9933+
// match: (SignExt8to16 (Const8 [c]))
9934+
// cond:
9935+
// result: (Const16 [int64( int8(c))])
9936+
for {
9937+
v_0 := v.Args[0]
9938+
if v_0.Op != OpConst8 {
9939+
break
9940+
}
9941+
c := v_0.AuxInt
9942+
v.reset(OpConst16)
9943+
v.AuxInt = int64(int8(c))
9944+
return true
9945+
}
98949946
// match: (SignExt8to16 (Trunc16to8 x:(Rsh16x64 _ (Const64 [s]))))
98959947
// cond: s >= 8
98969948
// result: x
@@ -9921,6 +9973,19 @@ func rewriteValuegeneric_OpSignExt8to16(v *Value, config *Config) bool {
99219973
func rewriteValuegeneric_OpSignExt8to32(v *Value, config *Config) bool {
99229974
b := v.Block
99239975
_ = b
9976+
// match: (SignExt8to32 (Const8 [c]))
9977+
// cond:
9978+
// result: (Const32 [int64( int8(c))])
9979+
for {
9980+
v_0 := v.Args[0]
9981+
if v_0.Op != OpConst8 {
9982+
break
9983+
}
9984+
c := v_0.AuxInt
9985+
v.reset(OpConst32)
9986+
v.AuxInt = int64(int8(c))
9987+
return true
9988+
}
99249989
// match: (SignExt8to32 (Trunc32to8 x:(Rsh32x64 _ (Const64 [s]))))
99259990
// cond: s >= 24
99269991
// result: x
@@ -9951,6 +10016,19 @@ func rewriteValuegeneric_OpSignExt8to32(v *Value, config *Config) bool {
995110016
func rewriteValuegeneric_OpSignExt8to64(v *Value, config *Config) bool {
995210017
b := v.Block
995310018
_ = b
10019+
// match: (SignExt8to64 (Const8 [c]))
10020+
// cond:
10021+
// result: (Const64 [int64( int8(c))])
10022+
for {
10023+
v_0 := v.Args[0]
10024+
if v_0.Op != OpConst8 {
10025+
break
10026+
}
10027+
c := v_0.AuxInt
10028+
v.reset(OpConst64)
10029+
v.AuxInt = int64(int8(c))
10030+
return true
10031+
}
995410032
// match: (SignExt8to64 (Trunc64to8 x:(Rsh64x64 _ (Const64 [s]))))
995510033
// cond: s >= 56
995610034
// result: x
@@ -12225,6 +12303,19 @@ func rewriteValuegeneric_OpZero(v *Value, config *Config) bool {
1222512303
func rewriteValuegeneric_OpZeroExt16to32(v *Value, config *Config) bool {
1222612304
b := v.Block
1222712305
_ = b
12306+
// match: (ZeroExt16to32 (Const16 [c]))
12307+
// cond:
12308+
// result: (Const32 [int64(uint16(c))])
12309+
for {
12310+
v_0 := v.Args[0]
12311+
if v_0.Op != OpConst16 {
12312+
break
12313+
}
12314+
c := v_0.AuxInt
12315+
v.reset(OpConst32)
12316+
v.AuxInt = int64(uint16(c))
12317+
return true
12318+
}
1222812319
// match: (ZeroExt16to32 (Trunc32to16 x:(Rsh32Ux64 _ (Const64 [s]))))
1222912320
// cond: s >= 16
1223012321
// result: x
@@ -12255,6 +12346,19 @@ func rewriteValuegeneric_OpZeroExt16to32(v *Value, config *Config) bool {
1225512346
func rewriteValuegeneric_OpZeroExt16to64(v *Value, config *Config) bool {
1225612347
b := v.Block
1225712348
_ = b
12349+
// match: (ZeroExt16to64 (Const16 [c]))
12350+
// cond:
12351+
// result: (Const64 [int64(uint16(c))])
12352+
for {
12353+
v_0 := v.Args[0]
12354+
if v_0.Op != OpConst16 {
12355+
break
12356+
}
12357+
c := v_0.AuxInt
12358+
v.reset(OpConst64)
12359+
v.AuxInt = int64(uint16(c))
12360+
return true
12361+
}
1225812362
// match: (ZeroExt16to64 (Trunc64to16 x:(Rsh64Ux64 _ (Const64 [s]))))
1225912363
// cond: s >= 48
1226012364
// result: x
@@ -12285,6 +12389,19 @@ func rewriteValuegeneric_OpZeroExt16to64(v *Value, config *Config) bool {
1228512389
func rewriteValuegeneric_OpZeroExt32to64(v *Value, config *Config) bool {
1228612390
b := v.Block
1228712391
_ = b
12392+
// match: (ZeroExt32to64 (Const32 [c]))
12393+
// cond:
12394+
// result: (Const64 [int64(uint32(c))])
12395+
for {
12396+
v_0 := v.Args[0]
12397+
if v_0.Op != OpConst32 {
12398+
break
12399+
}
12400+
c := v_0.AuxInt
12401+
v.reset(OpConst64)
12402+
v.AuxInt = int64(uint32(c))
12403+
return true
12404+
}
1228812405
// match: (ZeroExt32to64 (Trunc64to32 x:(Rsh64Ux64 _ (Const64 [s]))))
1228912406
// cond: s >= 32
1229012407
// result: x
@@ -12315,6 +12432,19 @@ func rewriteValuegeneric_OpZeroExt32to64(v *Value, config *Config) bool {
1231512432
func rewriteValuegeneric_OpZeroExt8to16(v *Value, config *Config) bool {
1231612433
b := v.Block
1231712434
_ = b
12435+
// match: (ZeroExt8to16 (Const8 [c]))
12436+
// cond:
12437+
// result: (Const16 [int64( uint8(c))])
12438+
for {
12439+
v_0 := v.Args[0]
12440+
if v_0.Op != OpConst8 {
12441+
break
12442+
}
12443+
c := v_0.AuxInt
12444+
v.reset(OpConst16)
12445+
v.AuxInt = int64(uint8(c))
12446+
return true
12447+
}
1231812448
// match: (ZeroExt8to16 (Trunc16to8 x:(Rsh16Ux64 _ (Const64 [s]))))
1231912449
// cond: s >= 8
1232012450
// result: x
@@ -12345,6 +12475,19 @@ func rewriteValuegeneric_OpZeroExt8to16(v *Value, config *Config) bool {
1234512475
func rewriteValuegeneric_OpZeroExt8to32(v *Value, config *Config) bool {
1234612476
b := v.Block
1234712477
_ = b
12478+
// match: (ZeroExt8to32 (Const8 [c]))
12479+
// cond:
12480+
// result: (Const32 [int64( uint8(c))])
12481+
for {
12482+
v_0 := v.Args[0]
12483+
if v_0.Op != OpConst8 {
12484+
break
12485+
}
12486+
c := v_0.AuxInt
12487+
v.reset(OpConst32)
12488+
v.AuxInt = int64(uint8(c))
12489+
return true
12490+
}
1234812491
// match: (ZeroExt8to32 (Trunc32to8 x:(Rsh32Ux64 _ (Const64 [s]))))
1234912492
// cond: s >= 24
1235012493
// result: x
@@ -12375,6 +12518,19 @@ func rewriteValuegeneric_OpZeroExt8to32(v *Value, config *Config) bool {
1237512518
func rewriteValuegeneric_OpZeroExt8to64(v *Value, config *Config) bool {
1237612519
b := v.Block
1237712520
_ = b
12521+
// match: (ZeroExt8to64 (Const8 [c]))
12522+
// cond:
12523+
// result: (Const64 [int64( uint8(c))])
12524+
for {
12525+
v_0 := v.Args[0]
12526+
if v_0.Op != OpConst8 {
12527+
break
12528+
}
12529+
c := v_0.AuxInt
12530+
v.reset(OpConst64)
12531+
v.AuxInt = int64(uint8(c))
12532+
return true
12533+
}
1237812534
// match: (ZeroExt8to64 (Trunc64to8 x:(Rsh64Ux64 _ (Const64 [s]))))
1237912535
// cond: s >= 56
1238012536
// result: x

0 commit comments

Comments
 (0)