Commit 3e7904b6 authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

[dev.ssa] cmd/compile: improve inBounds rewrite rules

Change-Id: Ia238187a89f820cd1620ab5acdbf1c8f003569b1
Reviewed-on: https://go-review.googlesource.com/13587Reviewed-by: default avatarKeith Randall <khr@golang.org>
parent bc1f5768
......@@ -440,22 +440,22 @@
(NE (InvertFlags cmp) yes no) -> (NE cmp yes no)
// get rid of overflow code for constant shifts
(SBBQcarrymask (CMPQconst [c] (MOVQconst [d]))) && inBounds(d, c) -> (MOVQconst [-1])
(SBBQcarrymask (CMPQconst [c] (MOVQconst [d]))) && !inBounds(d, c) -> (MOVQconst [0])
(SBBQcarrymask (CMPLconst [c] (MOVLconst [d]))) && inBounds(int64(int32(d)), int64(int32(c))) -> (MOVQconst [-1])
(SBBQcarrymask (CMPLconst [c] (MOVLconst [d]))) && !inBounds(int64(int32(d)), int64(int32(c))) -> (MOVQconst [0])
(SBBQcarrymask (CMPWconst [c] (MOVWconst [d]))) && inBounds(int64(int16(d)), int64(int16(c))) -> (MOVQconst [-1])
(SBBQcarrymask (CMPWconst [c] (MOVWconst [d]))) && !inBounds(int64(int16(d)), int64(int16(c))) -> (MOVQconst [0])
(SBBQcarrymask (CMPBconst [c] (MOVBconst [d]))) && inBounds(int64(int8(d)), int64(int8(c))) -> (MOVQconst [-1])
(SBBQcarrymask (CMPBconst [c] (MOVBconst [d]))) && !inBounds(int64(int8(d)), int64(int8(c))) -> (MOVQconst [0])
(SBBLcarrymask (CMPQconst [c] (MOVQconst [d]))) && inBounds(d, c) -> (MOVLconst [-1])
(SBBLcarrymask (CMPQconst [c] (MOVQconst [d]))) && !inBounds(d, c) -> (MOVLconst [0])
(SBBLcarrymask (CMPLconst [c] (MOVLconst [d]))) && inBounds(int64(int32(d)), int64(int32(c))) -> (MOVLconst [-1])
(SBBLcarrymask (CMPLconst [c] (MOVLconst [d]))) && !inBounds(int64(int32(d)), int64(int32(c))) -> (MOVLconst [0])
(SBBLcarrymask (CMPWconst [c] (MOVWconst [d]))) && inBounds(int64(int16(d)), int64(int16(c))) -> (MOVLconst [-1])
(SBBLcarrymask (CMPWconst [c] (MOVWconst [d]))) && !inBounds(int64(int16(d)), int64(int16(c))) -> (MOVLconst [0])
(SBBLcarrymask (CMPBconst [c] (MOVBconst [d]))) && inBounds(int64(int8(d)), int64(int8(c))) -> (MOVLconst [-1])
(SBBLcarrymask (CMPBconst [c] (MOVBconst [d]))) && !inBounds(int64(int8(d)), int64(int8(c))) -> (MOVLconst [0])
(SBBQcarrymask (CMPQconst [c] (MOVQconst [d]))) && inBounds64(d, c) -> (MOVQconst [-1])
(SBBQcarrymask (CMPQconst [c] (MOVQconst [d]))) && !inBounds64(d, c) -> (MOVQconst [0])
(SBBQcarrymask (CMPLconst [c] (MOVLconst [d]))) && inBounds32(d, c) -> (MOVQconst [-1])
(SBBQcarrymask (CMPLconst [c] (MOVLconst [d]))) && !inBounds32(d, c) -> (MOVQconst [0])
(SBBQcarrymask (CMPWconst [c] (MOVWconst [d]))) && inBounds16(d, c) -> (MOVQconst [-1])
(SBBQcarrymask (CMPWconst [c] (MOVWconst [d]))) && !inBounds16(d, c) -> (MOVQconst [0])
(SBBQcarrymask (CMPBconst [c] (MOVBconst [d]))) && inBounds8(d, c) -> (MOVQconst [-1])
(SBBQcarrymask (CMPBconst [c] (MOVBconst [d]))) && !inBounds8(d, c) -> (MOVQconst [0])
(SBBLcarrymask (CMPQconst [c] (MOVQconst [d]))) && inBounds64(d, c) -> (MOVLconst [-1])
(SBBLcarrymask (CMPQconst [c] (MOVQconst [d]))) && !inBounds64(d, c) -> (MOVLconst [0])
(SBBLcarrymask (CMPLconst [c] (MOVLconst [d]))) && inBounds32(d, c) -> (MOVLconst [-1])
(SBBLcarrymask (CMPLconst [c] (MOVLconst [d]))) && !inBounds32(d, c) -> (MOVLconst [0])
(SBBLcarrymask (CMPWconst [c] (MOVWconst [d]))) && inBounds16(d, c) -> (MOVLconst [-1])
(SBBLcarrymask (CMPWconst [c] (MOVWconst [d]))) && !inBounds16(d, c) -> (MOVLconst [0])
(SBBLcarrymask (CMPBconst [c] (MOVBconst [d]))) && inBounds8(d, c) -> (MOVLconst [-1])
(SBBLcarrymask (CMPBconst [c] (MOVBconst [d]))) && !inBounds8(d, c) -> (MOVLconst [0])
(ANDQconst [0] _) -> (MOVQconst [0])
(ANDLconst [c] _) && int32(c)==0 -> (MOVLconst [0])
(ANDWconst [c] _) && int16(c)==0 -> (MOVWconst [0])
......
......@@ -24,7 +24,10 @@
(AddPtr (ConstPtr [c]) (ConstPtr [d])) -> (ConstPtr [c+d])
(Mul64 (Const64 [c]) (Const64 [d])) -> (Const64 [c*d])
(MulPtr (ConstPtr [c]) (ConstPtr [d])) -> (ConstPtr [c*d])
(IsInBounds (ConstPtr [c]) (ConstPtr [d])) -> (ConstPtr {inBounds(c,d)})
(IsInBounds (Const32 [c]) (Const32 [d])) -> (ConstBool {inBounds32(c,d)})
(IsInBounds (Const64 [c]) (Const64 [d])) -> (ConstBool {inBounds64(c,d)})
(IsInBounds (ConstPtr [c]) (ConstPtr [d])) && config.PtrSize == 4 -> (ConstBool {inBounds32(c,d)})
(IsInBounds (ConstPtr [c]) (ConstPtr [d])) && config.PtrSize == 8 -> (ConstBool {inBounds64(c,d)})
(Eq64 x x) -> (ConstBool {true})
(Eq32 x x) -> (ConstBool {true})
(Eq16 x x) -> (ConstBool {true})
......
......@@ -125,9 +125,10 @@ func mergeSym(x, y interface{}) interface{} {
return nil
}
func inBounds(idx, len int64) bool {
return idx >= 0 && idx < len
}
func inBounds8(idx, len int64) bool { return int8(idx) >= 0 && int8(idx) < int8(len) }
func inBounds16(idx, len int64) bool { return int16(idx) >= 0 && int16(idx) < int16(len) }
func inBounds32(idx, len int64) bool { return int32(idx) >= 0 && int32(idx) < int32(len) }
func inBounds64(idx, len int64) bool { return idx >= 0 && idx < len }
// log2 returns logarithm in base of n.
// expects n to be a power of 2.
......
......@@ -311,27 +311,99 @@ func rewriteValuegeneric(v *Value, config *Config) bool {
end540dc8dfbc66adcd3db2d7e819c534f6:
;
case OpIsInBounds:
// match: (IsInBounds (ConstPtr [c]) (ConstPtr [d]))
// match: (IsInBounds (Const32 [c]) (Const32 [d]))
// cond:
// result: (ConstBool {inBounds32(c,d)})
{
if v.Args[0].Op != OpConst32 {
goto endc3396bf88b56276e1691abe62811dba5
}
c := v.Args[0].AuxInt
if v.Args[1].Op != OpConst32 {
goto endc3396bf88b56276e1691abe62811dba5
}
d := v.Args[1].AuxInt
v.Op = OpConstBool
v.AuxInt = 0
v.Aux = nil
v.resetArgs()
v.Aux = inBounds32(c, d)
return true
}
goto endc3396bf88b56276e1691abe62811dba5
endc3396bf88b56276e1691abe62811dba5:
;
// match: (IsInBounds (Const64 [c]) (Const64 [d]))
// cond:
// result: (ConstPtr {inBounds(c,d)})
// result: (ConstBool {inBounds64(c,d)})
{
if v.Args[0].Op != OpConst64 {
goto end0b4b8178a54662835b00bfa503cf879a
}
c := v.Args[0].AuxInt
if v.Args[1].Op != OpConst64 {
goto end0b4b8178a54662835b00bfa503cf879a
}
d := v.Args[1].AuxInt
v.Op = OpConstBool
v.AuxInt = 0
v.Aux = nil
v.resetArgs()
v.Aux = inBounds64(c, d)
return true
}
goto end0b4b8178a54662835b00bfa503cf879a
end0b4b8178a54662835b00bfa503cf879a:
;
// match: (IsInBounds (ConstPtr [c]) (ConstPtr [d]))
// cond: config.PtrSize == 4
// result: (ConstBool {inBounds32(c,d)})
{
if v.Args[0].Op != OpConstPtr {
goto enddfd340bc7103ca323354aec96b113c23
goto end2c6938f68a67e08dbd96edb1e693e549
}
c := v.Args[0].AuxInt
if v.Args[1].Op != OpConstPtr {
goto enddfd340bc7103ca323354aec96b113c23
goto end2c6938f68a67e08dbd96edb1e693e549
}
d := v.Args[1].AuxInt
v.Op = OpConstPtr
if !(config.PtrSize == 4) {
goto end2c6938f68a67e08dbd96edb1e693e549
}
v.Op = OpConstBool
v.AuxInt = 0
v.Aux = nil
v.resetArgs()
v.Aux = inBounds32(c, d)
return true
}
goto end2c6938f68a67e08dbd96edb1e693e549
end2c6938f68a67e08dbd96edb1e693e549:
;
// match: (IsInBounds (ConstPtr [c]) (ConstPtr [d]))
// cond: config.PtrSize == 8
// result: (ConstBool {inBounds64(c,d)})
{
if v.Args[0].Op != OpConstPtr {
goto end84d6ae817944985f572ecaac51999d6c
}
c := v.Args[0].AuxInt
if v.Args[1].Op != OpConstPtr {
goto end84d6ae817944985f572ecaac51999d6c
}
d := v.Args[1].AuxInt
if !(config.PtrSize == 8) {
goto end84d6ae817944985f572ecaac51999d6c
}
v.Op = OpConstBool
v.AuxInt = 0
v.Aux = nil
v.resetArgs()
v.Aux = inBounds(c, d)
v.Aux = inBounds64(c, d)
return true
}
goto enddfd340bc7103ca323354aec96b113c23
enddfd340bc7103ca323354aec96b113c23:
goto end84d6ae817944985f572ecaac51999d6c
end84d6ae817944985f572ecaac51999d6c:
;
case OpLoad:
// match: (Load <t> ptr mem)
......
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