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

Open
josharian opened this Issue Sep 8, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@josharian
Contributor

josharian commented Sep 8, 2018

Consider:

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 {
					continue
				}
				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)
					}
					v.reset(OpCopy)
					v.AddArg(orig)
				}
			}

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 from cmd/compile: eliminate unnecessary extend-of-truncate calculations to cmd/compile: eliminate unnecessary extend-of-truncate calculations in prove pass Sep 8, 2018

@ChrisALiles

This comment has been minimized.

Contributor

ChrisALiles commented Sep 18, 2018

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

@josharian

This comment has been minimized.

Contributor

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