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: teach compiler that uint(x) > 0 #36347

Closed
josharian opened this issue Jan 1, 2020 · 1 comment
Closed

cmd/compile: teach compiler that uint(x) > 0 #36347

josharian opened this issue Jan 1, 2020 · 1 comment

Comments

@josharian
Copy link
Contributor

func isNegative(x uint) bool {
	if x < 0 {
		return true
	}
	return false
}

I was surprised to see that this compiles to:

	0x0000 00000 (x.go:4)	MOVQ	"".x+8(SP), AX
	0x0005 00005 (x.go:4)	TESTQ	AX, AX
	0x0008 00008 (x.go:4)	JCC	16
	0x000a 00010 (x.go:5)	MOVB	$1, "".~r1+16(SP)
	0x000f 00015 (x.go:5)	RET
	0x0010 00016 (x.go:8)	MOVB	$0, "".~r1+16(SP)
	0x0015 00021 (x.go:8)	RET

I expected just return false.

Noticed while poking around at #36346. Not sure how often this would fire in real code.

@josharian josharian added this to the Unplanned milestone Jan 1, 2020
@josharian
Copy link
Contributor Author

When I started writing rewrite rules to fix these, I realized I've been here before. Oops. Duplicate of #21439.

@golang golang locked and limited conversation to collaborators Dec 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants