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
Labels
Milestone

Comments

@josharian
Copy link
Contributor

@josharian josharian commented Jan 1, 2020

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

This comment has been minimized.

Copy link
Contributor Author

@josharian josharian commented Jan 1, 2020

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

@josharian josharian closed this Jan 1, 2020
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
1 participant
You can’t perform that action at this time.