cmd/compile: fix slice-in-bound check on amd64p32

Should use CMPL instead of CMPQ.

Fixes #20811.

Change-Id: I610d487949c2c8a08b3743656149069d931a51bb
Reviewed-on: https://go-review.googlesource.com/46870
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Cherry Zhang 2017-06-27 16:34:06 -04:00
parent 5e585830d3
commit c920fa537f
3 changed files with 75 additions and 4 deletions

View file

@ -441,8 +441,10 @@
(Convert <t> x mem) && config.PtrSize == 4 -> (MOVLconvert <t> x mem)
(IsNonNil p) && config.PtrSize == 8 -> (SETNE (TESTQ p p))
(IsNonNil p) && config.PtrSize == 4 -> (SETNE (TESTL p p))
(IsInBounds idx len) -> (SETB (CMPQ idx len))
(IsSliceInBounds idx len) -> (SETBE (CMPQ idx len))
(IsInBounds idx len) && config.PtrSize == 8 -> (SETB (CMPQ idx len))
(IsInBounds idx len) && config.PtrSize == 4 -> (SETB (CMPL idx len))
(IsSliceInBounds idx len) && config.PtrSize == 8 -> (SETBE (CMPQ idx len))
(IsSliceInBounds idx len) && config.PtrSize == 4 -> (SETBE (CMPL idx len))
(NilCheck ptr mem) -> (LoweredNilCheck ptr mem)
(GetG mem) -> (LoweredGetG mem)
(GetClosurePtr) -> (LoweredGetClosurePtr)

View file

@ -39253,13 +39253,18 @@ func rewriteValueAMD64_OpInterCall_0(v *Value) bool {
func rewriteValueAMD64_OpIsInBounds_0(v *Value) bool {
b := v.Block
_ = b
config := b.Func.Config
_ = config
// match: (IsInBounds idx len)
// cond:
// cond: config.PtrSize == 8
// result: (SETB (CMPQ idx len))
for {
_ = v.Args[1]
idx := v.Args[0]
len := v.Args[1]
if !(config.PtrSize == 8) {
break
}
v.reset(OpAMD64SETB)
v0 := b.NewValue0(v.Pos, OpAMD64CMPQ, types.TypeFlags)
v0.AddArg(idx)
@ -39267,6 +39272,24 @@ func rewriteValueAMD64_OpIsInBounds_0(v *Value) bool {
v.AddArg(v0)
return true
}
// match: (IsInBounds idx len)
// cond: config.PtrSize == 4
// result: (SETB (CMPL idx len))
for {
_ = v.Args[1]
idx := v.Args[0]
len := v.Args[1]
if !(config.PtrSize == 4) {
break
}
v.reset(OpAMD64SETB)
v0 := b.NewValue0(v.Pos, OpAMD64CMPL, types.TypeFlags)
v0.AddArg(idx)
v0.AddArg(len)
v.AddArg(v0)
return true
}
return false
}
func rewriteValueAMD64_OpIsNonNil_0(v *Value) bool {
b := v.Block
@ -39308,13 +39331,18 @@ func rewriteValueAMD64_OpIsNonNil_0(v *Value) bool {
func rewriteValueAMD64_OpIsSliceInBounds_0(v *Value) bool {
b := v.Block
_ = b
config := b.Func.Config
_ = config
// match: (IsSliceInBounds idx len)
// cond:
// cond: config.PtrSize == 8
// result: (SETBE (CMPQ idx len))
for {
_ = v.Args[1]
idx := v.Args[0]
len := v.Args[1]
if !(config.PtrSize == 8) {
break
}
v.reset(OpAMD64SETBE)
v0 := b.NewValue0(v.Pos, OpAMD64CMPQ, types.TypeFlags)
v0.AddArg(idx)
@ -39322,6 +39350,24 @@ func rewriteValueAMD64_OpIsSliceInBounds_0(v *Value) bool {
v.AddArg(v0)
return true
}
// match: (IsSliceInBounds idx len)
// cond: config.PtrSize == 4
// result: (SETBE (CMPL idx len))
for {
_ = v.Args[1]
idx := v.Args[0]
len := v.Args[1]
if !(config.PtrSize == 4) {
break
}
v.reset(OpAMD64SETBE)
v0 := b.NewValue0(v.Pos, OpAMD64CMPL, types.TypeFlags)
v0.AddArg(idx)
v0.AddArg(len)
v.AddArg(v0)
return true
}
return false
}
func rewriteValueAMD64_OpLeq16_0(v *Value) bool {
b := v.Block

View file

@ -0,0 +1,23 @@
// run
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Issue 20811: slice-in-bound check is lowered incorrectly on
// amd64p32.
package main
func main() {
i := g()
_ = "x"[int32(i)]
j := g()
_ = "x"[:int32(j)]
}
//go:noinline
func g() int64 {
return 4398046511104
}