-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: suboptimal code generated for a[b]++; a[b]++ #14564
Comments
@tzneal |
I'll have a look at it. Maybe just a more complex isSamePtr that uses cmpVal to compare pointers? |
That would work. I get the feeling that it is just a patch over a deeper phase-ordering problem, though. Another cse would do it, but that is expensive. |
Adding another cse/zcse pair costs 3-6% in compile time (consistency checks disabled). Adding a second opt pass after cse is negligible in cost and resolves the issue:
There's a long standing TODO about splitting the required and optimizing rules. What are your thoughts on doing that split and adding the second optimization rule pass after cse? |
I was also thinking of adding a second opt optional pass. I saw a I don't see why we should split opt. It's generally fast enough
|
I think the intent of the split is primarily to support the -N option so that the generated code is much closer to the input go code and easier to debug with gdb. |
Yes, the split I proposed was intended to separate the required from the optional rewrites. I was more interested in doing this for the lowering pass, but we could do it for the machine-independent one as well. One benefit of separating the passes is that the required rewrites, for lowering at least, don't need to be run in a loop. You can do just one pass over the CFG. |
I don't think a single pass of the required lower rules would work because of rules like these that don't fully lower:
I spent some time splitting rules, but since opt is so fast anyway along with the extra complexity to writing rules (i.e. you can no longer rewrite in an "optional" rule to a form that's an input to a "required" rule), I'm not sure it's worth it. I'll throw up a CL with just the additional pass for now. |
CL https://golang.org/cl/20172 mentions this issue. |
* Move lowering into a separate pass. * SliceLen/SliceCap is now available to various intermediate passes which use useful for bounds checking. * Add a second opt pass to handle the new opportunities Decreases the code size of binaries in pkg/tool/linux_amd64 by ~45K. Updates #14564 #14606 Change-Id: I5b2bd6202181c50623a3585fbf15c0d6db6d4685 Reviewed-on: https://go-review.googlesource.com/20172 Run-TryBot: Alexandru Moșoi <alexandru@mosoi.ro> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com>
Fixed. The ssa code looks like the following:
which is optimal. |
Please answer these questions before submitting your issue. Thanks!
go version
)?dev.ssa branch
go version devel +8107b00 Sun Feb 28 22:29:23 2016 +0000 linux/amd64
go env
)?amd64
http://play.golang.org/p/kEmOTtpIGU
The code is optimized as if it was a[b] += 2
The code generated was:
Read from mem
Inc
Write to mem
Read from mem
Inc
Write to mem
The read to/write from memory in the middle are redundant.
The text was updated successfully, but these errors were encountered: