Skip to content

Commit d0964e1

Browse files
committed
cmd/compile: optimize s==s for strings
s==s is always true for strings. This comes up in NaN testing in generic code, where we want x==x to compile completely away except for float types. Fixes #60777 Change-Id: I3ce054b5121354de2f9751b010fb409f148cb637 Reviewed-on: https://go-review.googlesource.com/c/go/+/503795 Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Keith Randall <khr@golang.org>
1 parent f0894a0 commit d0964e1

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/cmd/compile/internal/ssa/_gen/generic.rules

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,6 +2121,11 @@
21212121
&& isSameCall(callAux, "runtime.memequal")
21222122
=> (MakeResult (ConstBool <typ.Bool> [true]) mem)
21232123

2124+
(Static(Call|LECall) {callAux} p q _ mem)
2125+
&& isSameCall(callAux, "runtime.memequal")
2126+
&& isSamePtr(p, q)
2127+
=> (MakeResult (ConstBool <typ.Bool> [true]) mem)
2128+
21242129
// Turn known-size calls to memclrNoHeapPointers into a Zero.
21252130
// Note that we are using types.Types[types.TUINT8] instead of sptr.Type.Elem() - see issue 55122 and CL 431496 for more details.
21262131
(SelectN [0] call:(StaticCall {sym} sptr (Const(64|32) [c]) mem))

src/cmd/compile/internal/ssa/rewritegeneric.go

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/codegen/strings.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,14 @@ func ConstantLoad() {
6767
bsink = []byte("0123456789ab")
6868
}
6969

70+
// self-equality is always true. See issue 60777.
71+
func EqualSelf(s string) bool {
72+
// amd64:`MOVL\t\$1, AX`,-`.*memequal.*`
73+
return s == s
74+
}
75+
func NotEqualSelf(s string) bool {
76+
// amd64:`XORL\tAX, AX`,-`.*memequal.*`
77+
return s != s
78+
}
79+
7080
var bsink []byte

0 commit comments

Comments
 (0)