diff options
author | khr@golang.org <khr@golang.org> | 2024-04-13 19:21:15 -0700 |
---|---|---|
committer | Keith Randall <khr@google.com> | 2024-04-19 16:31:02 +0000 |
commit | 1a0b86375fad202048adb88cba4caec535a52a45 (patch) | |
tree | 3ba0daeb3bbb608d528e895cb14fd3a860520595 /test | |
parent | d428a63875d335d1e1b9a3b3b45ad58f46e1e6bc (diff) | |
download | go-1a0b86375fad202048adb88cba4caec535a52a45.tar.gz go-1a0b86375fad202048adb88cba4caec535a52a45.zip |
cmd/compile: remove redundant calls to cmpstring
The results of cmpstring are reuseable if the second call has the
same arguments and memory.
Note that this gets rid of cmpstring, but we still generate a
redundant </<= test and branch afterwards, because the compiler
doesn't know that cmpstring only ever returns -1,0,1.
Update #61725
Change-Id: I93a0d1ccca50d90b1e1a888240ffb75a3b10b59b
Reviewed-on: https://go-review.googlesource.com/c/go/+/578835
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/codegen/comparisons.go | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/test/codegen/comparisons.go b/test/codegen/comparisons.go index 4edf9303df..e585045aa4 100644 --- a/test/codegen/comparisons.go +++ b/test/codegen/comparisons.go @@ -6,7 +6,10 @@ package codegen -import "unsafe" +import ( + "cmp" + "unsafe" +) // This file contains code generation tests related to the comparison // operators. @@ -801,3 +804,24 @@ func invertLessThanNoov(p1, p2, p3 Point) bool { // arm64:`CMP`,`CSET`,`CSEL` return (p1.X-p3.X)*(p2.Y-p3.Y)-(p2.X-p3.X)*(p1.Y-p3.Y) < 0 } + +func cmpstring1(x, y string) int { + // amd64:".*cmpstring" + if x < y { + return -1 + } + // amd64:-".*cmpstring" + if x > y { + return +1 + } + return 0 +} +func cmpstring2(x, y string) int { + // We want to fail if there are two calls to cmpstring. + // They will both have the same line number, so a test + // like in cmpstring1 will not work. Instead, we + // look for spill/restore instructions, which only + // need to exist if there are 2 calls. + //amd64:-`MOVQ\t.*\(SP\)` + return cmp.Compare(x, y) +} |