aboutsummaryrefslogtreecommitdiff
path: root/test/codegen
diff options
context:
space:
mode:
authorerifan01 <eric.fang@arm.com>2023-03-14 09:25:07 +0800
committerEric Fang <eric.fang@arm.com>2023-03-24 01:19:09 +0000
commit42f99b203d2990429ba9d13bd1b71d31057ce30a (patch)
tree0ba36c9e028f4bc94d818186374f81fed1a55310 /test/codegen
parente81cc9119f7906ebded91f4cdc149866ac2acc0d (diff)
downloadgo-42f99b203d2990429ba9d13bd1b71d31057ce30a.tar.xz
cmd/compile: optimize cmp to cmn under conditions < and >= on arm64
Under the right conditions we can optimize cmp comparisons to cmn comparisons, such as: func foo(a, b int) int { var c int if a + b < 0 { c = 1 } return c } Previously it's compiled as: ADD R1, R0, R1 CMP $0, R1 CSET LT, R0 With this CL it's compiled as: CMN R1, R0 CSET MI, R0 Here we need to pay attention to the overflow situation of a+b, the MI flag means N==1, which doesn't honor the overflow flag V, its value depends only on the sign of the result. So it has the same semantic of the Go code, so it's correct. Similarly, this CL also optimizes the case of >= comparison using the PL conditional flag. Change-Id: I47179faba5b30cca84ea69bafa2ad5241bf6dfba Reviewed-on: https://go-review.googlesource.com/c/go/+/476116 Run-TryBot: Eric Fang <eric.fang@arm.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: David Chase <drchase@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'test/codegen')
-rw-r--r--test/codegen/comparisons.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/test/codegen/comparisons.go b/test/codegen/comparisons.go
index ee732e06d9..99589c4ce8 100644
--- a/test/codegen/comparisons.go
+++ b/test/codegen/comparisons.go
@@ -456,6 +456,7 @@ func CmpToZero_ex5(e, f int32, u uint32) int {
}
return 0
}
+
func UintLtZero(a uint8, b uint16, c uint32, d uint64) int {
// amd64: -`(TESTB|TESTW|TESTL|TESTQ|JCC|JCS)`
// arm64: -`(CMPW|CMP|BHS|BLO)`
@@ -704,3 +705,45 @@ func cmpToCmn(a, b, c, d int) int {
}
return c1 + c2 + c3 + c4 + c5 + c6 + c7 + c8 + c9 + c10 + c11
}
+
+func cmpToCmnLessThan(a, b, c, d int) int {
+ var c1, c2, c3, c4 int
+ // arm64:`CMN`,`CSET\tMI`,-`CMP`
+ if a+1 < 0 {
+ c1 = 1
+ }
+ // arm64:`CMN`,`CSET\tMI`,-`CMP`
+ if a+b < 0 {
+ c2 = 1
+ }
+ // arm64:`CMN`,`CSET\tMI`,-`CMP`
+ if a*b+c < 0 {
+ c3 = 1
+ }
+ // arm64:`CMP`,`CSET\tMI`,-`CMN`
+ if a-b*c < 0 {
+ c4 = 1
+ }
+ return c1 + c2 + c3 + c4
+}
+
+func cmpToCmnGreaterThanEqual(a, b, c, d int) int {
+ var c1, c2, c3, c4 int
+ // arm64:`CMN`,`CSET\tPL`,-`CMP`
+ if a+1 >= 0 {
+ c1 = 1
+ }
+ // arm64:`CMN`,`CSET\tPL`,-`CMP`
+ if a+b >= 0 {
+ c2 = 1
+ }
+ // arm64:`CMN`,`CSET\tPL`,-`CMP`
+ if a*b+c >= 0 {
+ c3 = 1
+ }
+ // arm64:`CMP`,`CSET\tPL`,-`CMN`
+ if a-b*c >= 0 {
+ c4 = 1
+ }
+ return c1 + c2 + c3 + c4
+}