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: tighten CFG as well as values #24177

Open
josharian opened this Issue Feb 28, 2018 · 0 comments

Comments

Projects
None yet
2 participants
@josharian
Contributor

josharian commented Feb 28, 2018

func f(x int, b bool) int {
	if x < 0 {
		x *= -1
	}
	if b {
		return x
	}
	return 0
}

The compiled code for f matches the input closely: It checks x < 0, negates it if so, then checks b.

But we only need to check and modify x if b is true. The compiler should rewrite this code into:

func f(x int, b bool) int {
	if b {
		if x < 0 {
			x *= -1
		}
		return x
	}
	return 0
}

This is similar to the tighten pass, but instead of operating on values, it should operate on subsections of the CFG. Similar to the tighten pass, it should avoid moving work into a loop.

Among other things, this would help avoid doing needless work when the return value of copy is unused; see https://go-review.googlesource.com/c/go/+/94596.

Marking 1.11 optimistically, although I have no plans to work on this myself.

cc @randall77 @dr2chase @cherrymui

@josharian josharian added this to the Go1.11 milestone Feb 28, 2018

@bradfitz bradfitz modified the milestones: Go1.11, Unplanned May 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment