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

Open
josharian opened this issue Sep 8, 2018 · 2 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Milestone

Comments

@josharian
Copy link
Contributor

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 Performance NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Sep 8, 2018
@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
@ChrisALiles
Copy link
Contributor

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

@josharian
Copy link
Contributor Author

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

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Performance
Projects
None yet
Development

No branches or pull requests

3 participants