Commit 9549c06c authored by Alexandru Moșoi's avatar Alexandru Moșoi Committed by Alexandru Moșoi

cmd/compile: fold IsInBounds with small index

For the following example, but there are a few more in the stdlib:
func histogram(b []byte, h *[256]int32) {
        for _, t := range b {
                h[t]++
        }
}

Change-Id: I56615f341ae52e02ef34025588dc6d1c52122295
Reviewed-on: https://go-review.googlesource.com/20924Reviewed-by: default avatarKeith Randall <khr@golang.org>
Run-TryBot: Alexandru Moșoi <alexandru@mosoi.ro>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent e41f527f
......@@ -104,6 +104,12 @@
(Lsh16x16 (Rsh16Ux16 (Lsh16x16 x (Const16 [c1])) (Const16 [c2])) (Const16 [c3])) && c1 >= c2 && c3 >= c2 -> (Lsh16x16 x (Const16 <config.fe.TypeUInt16()> [c1-c2+c3]))
(Lsh8x8 (Rsh8Ux8 (Lsh8x8 x (Const8 [c1])) (Const8 [c2])) (Const8 [c3])) && c1 >= c2 && c3 >= c2 -> (Lsh8x8 x (Const8 <config.fe.TypeUInt8()> [c1-c2+c3]))
// Fold IsInBounds when the range of the index cannot exceed the limt.
(IsInBounds (ZeroExt8to32 _) (Const32 [c])) && (1 << 8) <= int32(c) -> (ConstBool [1])
(IsInBounds (ZeroExt8to64 _) (Const64 [c])) && (1 << 8) <= c -> (ConstBool [1])
(IsInBounds (ZeroExt16to32 _) (Const32 [c])) && (1 << 16) <= int32(c) -> (ConstBool [1])
(IsInBounds (ZeroExt16to64 _) (Const64 [c])) && (1 << 16) <= c -> (ConstBool [1])
(IsInBounds x x) -> (ConstBool [0])
(IsInBounds (And32 (Const32 [c]) _) (Const32 [d])) && inBounds32(c, d) -> (ConstBool [1])
(IsInBounds (And64 (Const64 [c]) _) (Const64 [d])) && inBounds64(c, d) -> (ConstBool [1])
......
......@@ -2602,6 +2602,78 @@ func rewriteValuegeneric_OpITab(v *Value, config *Config) bool {
func rewriteValuegeneric_OpIsInBounds(v *Value, config *Config) bool {
b := v.Block
_ = b
// match: (IsInBounds (ZeroExt8to32 _) (Const32 [c]))
// cond: (1 << 8) <= int32(c)
// result: (ConstBool [1])
for {
if v.Args[0].Op != OpZeroExt8to32 {
break
}
if v.Args[1].Op != OpConst32 {
break
}
c := v.Args[1].AuxInt
if !((1 << 8) <= int32(c)) {
break
}
v.reset(OpConstBool)
v.AuxInt = 1
return true
}
// match: (IsInBounds (ZeroExt8to64 _) (Const64 [c]))
// cond: (1 << 8) <= c
// result: (ConstBool [1])
for {
if v.Args[0].Op != OpZeroExt8to64 {
break
}
if v.Args[1].Op != OpConst64 {
break
}
c := v.Args[1].AuxInt
if !((1 << 8) <= c) {
break
}
v.reset(OpConstBool)
v.AuxInt = 1
return true
}
// match: (IsInBounds (ZeroExt16to32 _) (Const32 [c]))
// cond: (1 << 16) <= int32(c)
// result: (ConstBool [1])
for {
if v.Args[0].Op != OpZeroExt16to32 {
break
}
if v.Args[1].Op != OpConst32 {
break
}
c := v.Args[1].AuxInt
if !((1 << 16) <= int32(c)) {
break
}
v.reset(OpConstBool)
v.AuxInt = 1
return true
}
// match: (IsInBounds (ZeroExt16to64 _) (Const64 [c]))
// cond: (1 << 16) <= c
// result: (ConstBool [1])
for {
if v.Args[0].Op != OpZeroExt16to64 {
break
}
if v.Args[1].Op != OpConst64 {
break
}
c := v.Args[1].AuxInt
if !((1 << 16) <= c) {
break
}
v.reset(OpConstBool)
v.AuxInt = 1
return true
}
// match: (IsInBounds x x)
// cond:
// result: (ConstBool [0])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment