2018-03-20 03:42:06 -06:00
|
|
|
// asmcheck
|
|
|
|
|
|
|
|
// Copyright 2018 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.
|
|
|
|
|
|
|
|
package codegen
|
|
|
|
|
|
|
|
import "unsafe"
|
|
|
|
|
|
|
|
// This file contains code generation tests related to the comparison
|
|
|
|
// operators.
|
|
|
|
|
|
|
|
// -------------- //
|
|
|
|
// Equality //
|
|
|
|
// -------------- //
|
|
|
|
|
|
|
|
// Check that compare to constant string use 2/4/8 byte compares
|
|
|
|
|
|
|
|
func CompareString1(s string) bool {
|
|
|
|
// amd64:`CMPW\t\(.*\), [$]`
|
2018-03-26 14:18:27 -06:00
|
|
|
// arm64:`MOVHU\t\(.*\), [R]`,`CMPW\t[$]`
|
|
|
|
// ppc64le:`MOVHZ\t\(.*\), [R]`,`CMPW\t.*, [$]`
|
|
|
|
// s390x:`MOVHBR\t\(.*\), [R]`,`CMPW\t.*, [$]`
|
2018-03-20 03:42:06 -06:00
|
|
|
return s == "xx"
|
|
|
|
}
|
|
|
|
|
|
|
|
func CompareString2(s string) bool {
|
|
|
|
// amd64:`CMPL\t\(.*\), [$]`
|
2018-03-26 14:18:27 -06:00
|
|
|
// arm64:`MOVWU\t\(.*\), [R]`,`CMPW\t.*, [R]`
|
|
|
|
// ppc64le:`MOVWZ\t\(.*\), [R]`,`CMPW\t.*, [R]`
|
|
|
|
// s390x:`MOVWBR\t\(.*\), [R]`,`CMPW\t.*, [$]`
|
2018-03-20 03:42:06 -06:00
|
|
|
return s == "xxxx"
|
|
|
|
}
|
|
|
|
|
|
|
|
func CompareString3(s string) bool {
|
|
|
|
// amd64:`CMPQ\t\(.*\), [A-Z]`
|
2018-03-26 14:18:27 -06:00
|
|
|
// arm64:-`CMPW\t`
|
2018-10-15 10:53:07 -06:00
|
|
|
// ppc64:-`CMPW\t`
|
2018-03-26 14:18:27 -06:00
|
|
|
// ppc64le:-`CMPW\t`
|
|
|
|
// s390x:-`CMPW\t`
|
2018-03-20 03:42:06 -06:00
|
|
|
return s == "xxxxxxxx"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that arrays compare use 2/4/8 byte compares
|
|
|
|
|
|
|
|
func CompareArray1(a, b [2]byte) bool {
|
|
|
|
// amd64:`CMPW\t""[.+_a-z0-9]+\(SP\), [A-Z]`
|
2018-03-26 14:18:27 -06:00
|
|
|
// arm64:-`MOVBU\t`
|
|
|
|
// ppc64le:-`MOVBZ\t`
|
|
|
|
// s390x:-`MOVBZ\t`
|
2018-03-20 03:42:06 -06:00
|
|
|
return a == b
|
|
|
|
}
|
|
|
|
|
|
|
|
func CompareArray2(a, b [3]uint16) bool {
|
|
|
|
// amd64:`CMPL\t""[.+_a-z0-9]+\(SP\), [A-Z]`
|
|
|
|
// amd64:`CMPW\t""[.+_a-z0-9]+\(SP\), [A-Z]`
|
|
|
|
return a == b
|
|
|
|
}
|
|
|
|
|
|
|
|
func CompareArray3(a, b [3]int16) bool {
|
|
|
|
// amd64:`CMPL\t""[.+_a-z0-9]+\(SP\), [A-Z]`
|
|
|
|
// amd64:`CMPW\t""[.+_a-z0-9]+\(SP\), [A-Z]`
|
|
|
|
return a == b
|
|
|
|
}
|
|
|
|
|
|
|
|
func CompareArray4(a, b [12]int8) bool {
|
|
|
|
// amd64:`CMPQ\t""[.+_a-z0-9]+\(SP\), [A-Z]`
|
|
|
|
// amd64:`CMPL\t""[.+_a-z0-9]+\(SP\), [A-Z]`
|
|
|
|
return a == b
|
|
|
|
}
|
|
|
|
|
|
|
|
func CompareArray5(a, b [15]byte) bool {
|
|
|
|
// amd64:`CMPQ\t""[.+_a-z0-9]+\(SP\), [A-Z]`
|
|
|
|
return a == b
|
|
|
|
}
|
|
|
|
|
|
|
|
// This was a TODO in mapaccess1_faststr
|
|
|
|
func CompareArray6(a, b unsafe.Pointer) bool {
|
|
|
|
// amd64:`CMPL\t\(.*\), [A-Z]`
|
2018-03-26 14:18:27 -06:00
|
|
|
// arm64:`MOVWU\t\(.*\), [R]`,`CMPW\t.*, [R]`
|
|
|
|
// ppc64le:`MOVWZ\t\(.*\), [R]`,`CMPW\t.*, [R]`
|
|
|
|
// s390x:`MOVWBR\t\(.*\), [R]`,`CMPW\t.*, [R]`
|
2018-03-20 03:42:06 -06:00
|
|
|
return *((*[4]byte)(a)) != *((*[4]byte)(b))
|
|
|
|
}
|
2018-03-22 08:00:12 -06:00
|
|
|
|
|
|
|
// -------------- //
|
|
|
|
// Ordering //
|
|
|
|
// -------------- //
|
|
|
|
|
|
|
|
// Test that LEAQ/ADDQconst are folded into SETx ops
|
|
|
|
|
|
|
|
func CmpFold(x uint32) bool {
|
|
|
|
// amd64:`SETHI\t.*\(SP\)`
|
|
|
|
return x > 4
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that direct comparisons with memory are generated when
|
|
|
|
// possible
|
|
|
|
|
|
|
|
func CmpMem1(p int, q *int) bool {
|
|
|
|
// amd64:`CMPQ\t\(.*\), [A-Z]`
|
|
|
|
return p < *q
|
|
|
|
}
|
|
|
|
|
|
|
|
func CmpMem2(p *int, q int) bool {
|
|
|
|
// amd64:`CMPQ\t\(.*\), [A-Z]`
|
|
|
|
return *p < q
|
|
|
|
}
|
|
|
|
|
|
|
|
func CmpMem3(p *int) bool {
|
|
|
|
// amd64:`CMPQ\t\(.*\), [$]7`
|
|
|
|
return *p < 7
|
|
|
|
}
|
|
|
|
|
|
|
|
func CmpMem4(p *int) bool {
|
|
|
|
// amd64:`CMPQ\t\(.*\), [$]7`
|
|
|
|
return 7 < *p
|
|
|
|
}
|
|
|
|
|
|
|
|
func CmpMem5(p **int) {
|
|
|
|
// amd64:`CMPL\truntime.writeBarrier\(SB\), [$]0`
|
|
|
|
*p = nil
|
|
|
|
}
|
2018-03-23 11:50:39 -06:00
|
|
|
|
2018-07-29 06:50:50 -06:00
|
|
|
func CmpMem6(a []int) int {
|
|
|
|
// 386:`CMPL\s8\([A-Z]+\),`
|
|
|
|
// amd64:`CMPQ\s16\([A-Z]+\),`
|
|
|
|
if a[1] > a[2] {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 11:50:39 -06:00
|
|
|
// Check tbz/tbnz are generated when comparing against zero on arm64
|
|
|
|
|
|
|
|
func CmpZero1(a int32, ptr *int) {
|
|
|
|
if a < 0 { // arm64:"TBZ"
|
|
|
|
*ptr = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CmpZero2(a int64, ptr *int) {
|
|
|
|
if a < 0 { // arm64:"TBZ"
|
|
|
|
*ptr = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CmpZero3(a int32, ptr *int) {
|
|
|
|
if a >= 0 { // arm64:"TBNZ"
|
|
|
|
*ptr = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CmpZero4(a int64, ptr *int) {
|
|
|
|
if a >= 0 { // arm64:"TBNZ"
|
|
|
|
*ptr = 0
|
|
|
|
}
|
|
|
|
}
|
2018-07-09 19:48:55 -06:00
|
|
|
|
2018-08-29 19:19:58 -06:00
|
|
|
func CmpToZero(a, b, d int32, e, f int64) int32 {
|
2018-07-19 02:09:13 -06:00
|
|
|
// arm:`TST`,-`AND`
|
|
|
|
// arm64:`TSTW`,-`AND`
|
2018-08-29 19:19:58 -06:00
|
|
|
// 386:`TESTL`,-`ANDL`
|
|
|
|
// amd64:`TESTL`,-`ANDL`
|
2018-07-19 02:09:13 -06:00
|
|
|
c0 := a&b < 0
|
|
|
|
// arm:`CMN`,-`ADD`
|
|
|
|
// arm64:`CMNW`,-`ADD`
|
|
|
|
c1 := a+b < 0
|
|
|
|
// arm:`TEQ`,-`XOR`
|
|
|
|
c2 := a^b < 0
|
|
|
|
// arm64:`TST`,-`AND`
|
2018-08-29 19:19:58 -06:00
|
|
|
// amd64:`TESTQ`,-`ANDQ`
|
|
|
|
c3 := e&f < 0
|
2018-07-19 02:09:13 -06:00
|
|
|
// arm64:`CMN`,-`ADD`
|
2018-08-29 19:19:58 -06:00
|
|
|
c4 := e+f < 0
|
|
|
|
// not optimized to single CMNW/CMN due to further use of b+d
|
2018-07-19 02:09:13 -06:00
|
|
|
// arm64:`ADD`,-`CMNW`
|
2018-08-29 19:19:58 -06:00
|
|
|
// arm:`ADD`,-`CMN`
|
2018-07-19 02:09:13 -06:00
|
|
|
c5 := b+d == 0
|
2018-08-29 19:19:58 -06:00
|
|
|
// not optimized to single TSTW/TST due to further use of a&d
|
2018-07-19 02:09:13 -06:00
|
|
|
// arm64:`AND`,-`TSTW`
|
2018-08-29 19:19:58 -06:00
|
|
|
// arm:`AND`,-`TST`
|
2018-09-05 19:13:14 -06:00
|
|
|
// 386:`ANDL`
|
2018-07-19 02:09:13 -06:00
|
|
|
c6 := a&d >= 0
|
2018-09-27 00:21:14 -06:00
|
|
|
// arm64:`TST\sR[0-9]+<<3,\sR[0-9]+`
|
|
|
|
c7 := e&(f<<3) < 0
|
|
|
|
// arm64:`CMN\sR[0-9]+<<3,\sR[0-9]+`
|
|
|
|
c8 := e+(f<<3) < 0
|
2018-07-19 02:09:13 -06:00
|
|
|
if c0 {
|
2018-07-09 19:48:55 -06:00
|
|
|
return 1
|
2018-07-19 02:09:13 -06:00
|
|
|
} else if c1 {
|
2018-07-09 19:48:55 -06:00
|
|
|
return 2
|
2018-07-19 02:09:13 -06:00
|
|
|
} else if c2 {
|
2018-07-09 19:48:55 -06:00
|
|
|
return 3
|
2018-07-19 02:09:13 -06:00
|
|
|
} else if c3 {
|
|
|
|
return 4
|
|
|
|
} else if c4 {
|
|
|
|
return 5
|
|
|
|
} else if c5 {
|
|
|
|
return b + d
|
|
|
|
} else if c6 {
|
|
|
|
return a & d
|
2018-09-27 00:21:14 -06:00
|
|
|
} else if c7 {
|
|
|
|
return 7
|
|
|
|
} else if c8 {
|
|
|
|
return 8
|
2018-07-09 19:48:55 -06:00
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|