Commit fb2f99d5 authored by Alexandru Moșoi's avatar Alexandru Moșoi Committed by Alexandru Moșoi

cmd/compile/internal/ssa: simplify nil checks in opt.

* Simplify the nilcheck generated by
for _, e := range a {}
* No effect on the generated code because these nil checks
don't end up in the generated code.
* Useful for other analysis, e.g. it'll remove one dependecy
on the induction variable.

Change-Id: I6ee66ddfdc010ae22aea8dca48163303d93de7a9
Reviewed-on: https://go-review.googlesource.com/20307
Run-TryBot: Alexandru Moșoi <alexandru@mosoi.ro>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarKeith Randall <khr@golang.org>
parent e806cd9f
......@@ -19,6 +19,10 @@
// succ* fields must be variables
// For now, the generated successors must be a permutation of the matched successors.
// Simplify nil checks.
// These are inserted by for _, e := range a {}
(NilCheck (Phi x (Add64 (Const64 [c]) y)) mem) && c > 0 && v.Args[0] == y -> (NilCheck x mem)
// constant folding
(Trunc16to8 (Const16 [c])) -> (Const8 [int64(int8(c))])
(Trunc32to8 (Const32 [c])) -> (Const8 [int64(int8(c))])
......
......@@ -206,6 +206,8 @@ func rewriteValuegeneric(v *Value, config *Config) bool {
return rewriteValuegeneric_OpNeqPtr(v, config)
case OpNeqSlice:
return rewriteValuegeneric_OpNeqSlice(v, config)
case OpNilCheck:
return rewriteValuegeneric_OpNilCheck(v, config)
case OpOffPtr:
return rewriteValuegeneric_OpOffPtr(v, config)
case OpOr16:
......@@ -4823,6 +4825,39 @@ func rewriteValuegeneric_OpNeqSlice(v *Value, config *Config) bool {
}
return false
}
func rewriteValuegeneric_OpNilCheck(v *Value, config *Config) bool {
b := v.Block
_ = b
// match: (NilCheck (Phi x (Add64 (Const64 [c]) y)) mem)
// cond: c > 0 && v.Args[0] == y
// result: (NilCheck x mem)
for {
if v.Args[0].Op != OpPhi {
break
}
x := v.Args[0].Args[0]
if v.Args[0].Args[1].Op != OpAdd64 {
break
}
if v.Args[0].Args[1].Args[0].Op != OpConst64 {
break
}
c := v.Args[0].Args[1].Args[0].AuxInt
y := v.Args[0].Args[1].Args[1]
if len(v.Args[0].Args) != 2 {
break
}
mem := v.Args[1]
if !(c > 0 && v.Args[0] == y) {
break
}
v.reset(OpNilCheck)
v.AddArg(x)
v.AddArg(mem)
return true
}
return false
}
func rewriteValuegeneric_OpOffPtr(v *Value, config *Config) bool {
b := v.Block
_ = b
......
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