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: eliminate unnecessary extend-of-truncate calculations in prove pass #27572

josharian opened this issue Sep 8, 2018 · 2 comments


Copy link

@josharian josharian commented Sep 8, 2018


func f(x uint64) uint64 {
  return uint64(uint32(x))

If, when inlined, x is known to be small, then f can be replaced by the identity function.

I was curious, so I sketched this quickly for a specific case by adding this case to func simplifyBlock in prove.go:

		case OpZeroExt32to64:
			if trunc := v.Args[0]; trunc.Op == OpTrunc64to32 {
				orig := trunc.Args[0]
				lim, ok := ft.limits[orig.ID]
				if !ok {
				if lim.umax < (1<<32) || (lim.max < (1<<31) && ft.isNonNegative(orig)) {
					if b.Func.pass.debug > 0 {
						b.Func.Warnl(v.Pos, "Proved %v bounded", v.Op)

It triggers just a few times during make.bash.

This issue is to explore whether adding more cases makes this trigger enough times to be worth doing. There are lots more variants of signed/zero extension, and maybe other ways to generalize this optimization. (It also needs tests, both of correctness and that the optimization triggers.) Also, my limit tests in the code above could easily have fencepost problems. :)

cc @rasky @randall77

@josharian josharian added this to the Unplanned milestone Sep 8, 2018
@josharian josharian changed the title cmd/compile: eliminate unnecessary extend-of-truncate calculations cmd/compile: eliminate unnecessary extend-of-truncate calculations in prove pass Sep 8, 2018
Copy link

@ChrisALiles ChrisALiles commented Sep 18, 2018

@josharian - do you mind if I take a look at this?

Copy link
Contributor Author

@josharian josharian commented Oct 11, 2018

@ChrisALiles go for it! The result of the investigation might be that we shouldn't do this. That's a fine outcome too. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants