Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: cannot elim binary search typeHash on a type switch for a known type #48220

Open
wdvxdr1123 opened this issue Sep 7, 2021 · 0 comments

Comments

@wdvxdr1123
Copy link
Contributor

@wdvxdr1123 wdvxdr1123 commented Sep 7, 2021

In cmd/compile/internal/walk/switch.go, gc rewrites type switch into a binary search for typeHash and some type assertions.
The SSA backend can correctly eliminate the type assertions for a known type, but can't eliminate the binary search.

What did you do?

package a

func test(y int) int {
	var x = interface{}(y)
	var a = 1
	switch x.(type) {
	case int:
		a = 2
	case string:
		a = 3
	case int32:
		a = 4
	default:
		a = 5
	}
	return a
}

What did you expect to see?

00000 (3) TEXT "".test(SB), ABIInternal
    MOVL $2, AX
    RET

What did you see instead?

00000 (3) TEXT "".test(SB), ABIInternal
00001 (3) FUNCDATA $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
00002 (3) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
00003 (3) FUNCDATA $5, "".test.arginfo1(SB)
00004 (+6) MOVL type.int+16(SB), CX
00005 (6) CMPL CX, $-1146273534
00006 (6) JEQ 13
00007 (6) CMPL CX, $-520135500
00008 (6) JEQ 13
00009 (6) CMPL CX, $-145526278
00010 (6) JNE 13
00011 (+6) MOVL $2, AX
00012 (6) JMP 14
00013 (6) MOVL $5, AX
00014 (+16) RET
00015 (?) END
@ALTree ALTree changed the title cmd/compile: cannot elim binary search typeHash on a type switch for a known type. cmd/compile: cannot elim binary search typeHash on a type switch for a known type Sep 7, 2021
@thanm thanm added this to the Backlog milestone Sep 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants