Skip to content

Commit

Permalink
cmd/compile: simplify IsNonNil ConstNil
Browse files Browse the repository at this point in the history
Change-Id: I9ed5a2065cef06708e319b16c801da2eff42004e
Reviewed-on: https://go-review.googlesource.com/35497
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
  • Loading branch information
cherrymui committed Feb 2, 2017
1 parent fddc004 commit 6ad2d6a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/cmd/compile/internal/ssa/gen/generic.rules
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@
(NeqPtr (ConstNil) p) -> (IsNonNil p)
(EqPtr p (ConstNil)) -> (Not (IsNonNil p))
(EqPtr (ConstNil) p) -> (Not (IsNonNil p))
(IsNonNil (ConstNil)) -> (ConstBool [0])

// slice and interface comparisons
// The frontend ensures that we can only compare against nil,
Expand Down
19 changes: 19 additions & 0 deletions src/cmd/compile/internal/ssa/rewritegeneric.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ func rewriteValuegeneric(v *Value, config *Config) bool {
return rewriteValuegeneric_OpIMake(v, config)
case OpIsInBounds:
return rewriteValuegeneric_OpIsInBounds(v, config)
case OpIsNonNil:
return rewriteValuegeneric_OpIsNonNil(v, config)
case OpIsSliceInBounds:
return rewriteValuegeneric_OpIsSliceInBounds(v, config)
case OpLeq16:
Expand Down Expand Up @@ -3407,6 +3409,23 @@ func rewriteValuegeneric_OpIsInBounds(v *Value, config *Config) bool {
}
return false
}
func rewriteValuegeneric_OpIsNonNil(v *Value, config *Config) bool {
b := v.Block
_ = b
// match: (IsNonNil (ConstNil))
// cond:
// result: (ConstBool [0])
for {
v_0 := v.Args[0]
if v_0.Op != OpConstNil {
break
}
v.reset(OpConstBool)
v.AuxInt = 0
return true
}
return false
}
func rewriteValuegeneric_OpIsSliceInBounds(v *Value, config *Config) bool {
b := v.Block
_ = b
Expand Down

0 comments on commit 6ad2d6a

Please sign in to comment.