Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
cmd/compile: optimize unsigned comparisons with zero/one #21439
I started on an minor optimization effort earlier this summer that I won't have time to see through for 1.10. This issue is a snapshot of the work and a description of what to do next, in case anyone else wants to pick an architecture to work on and run with it.
Comparing unsigned ints with zero/one is special and (perhaps surprisingly) not uncommon. One example: Given unsigned x,
Here are some generic rewrite rules that I believe to be sound:
Unfortunately, doing this at the generic level is probably not ideal, since (a) not all architectures have special handling of eq/neq 0, (b) it might interfere with the prove pass.
So the remaining work here is to port these rules to arch-specific rules as applicable, and confirm/disconfirm that they are used and that they are worthwhile.
(Related aside: Why doesn't amd64.rules have rules like
Interesting (and not uncommen) i had a similar thought of checking and ensuring go gc emits TEST instead of CMP where simpler/shorter the other day but wasnt sure how far that was already done.
Im happy to have a look at this for amd64 and 386 (also the SETcc part).
BTW (need to make an extra issue for this): we should clear the SETcc target register with a XOR to avoid false dependencies but need to be careful not to dirty flags after TEST/CMP or clearing what we actually test.
These might also be related, but much less common:
I would have a CL ready for these.