Skip to content

Commit

Permalink
cmd/compile: optimize s==s for strings
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Cherry Mui <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Run-TryBot: Keith Randall <[email protected]>
  • Loading branch information
randall77 committed Jul 26, 2023
1 parent f0894a0 commit d0964e1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/cmd/compile/internal/ssa/_gen/generic.rules
Original file line number Diff line number Diff line change
Expand Up @@ -2121,6 +2121,11 @@
&& isSameCall(callAux, "runtime.memequal")
=> (MakeResult (ConstBool <typ.Bool> [true]) mem)

(Static(Call|LECall) {callAux} p q _ mem)
&& isSameCall(callAux, "runtime.memequal")
&& isSamePtr(p, q)
=> (MakeResult (ConstBool <typ.Bool> [true]) mem)

// Turn known-size calls to memclrNoHeapPointers into a Zero.
// Note that we are using types.Types[types.TUINT8] instead of sptr.Type.Elem() - see issue 55122 and CL 431496 for more details.
(SelectN [0] call:(StaticCall {sym} sptr (Const(64|32) [c]) mem))
Expand Down
47 changes: 47 additions & 0 deletions src/cmd/compile/internal/ssa/rewritegeneric.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions test/codegen/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,14 @@ func ConstantLoad() {
bsink = []byte("0123456789ab")
}

// self-equality is always true. See issue 60777.
func EqualSelf(s string) bool {
// amd64:`MOVL\t\$1, AX`,-`.*memequal.*`
return s == s
}
func NotEqualSelf(s string) bool {
// amd64:`XORL\tAX, AX`,-`.*memequal.*`
return s != s
}

var bsink []byte

0 comments on commit d0964e1

Please sign in to comment.