This is a small patch with a long backstory.
First, I'm proposing to remove a fold of (zext i1) + C to select of constants.
A pair of sibling folds for this was added at:
https://reviews.llvm.org/rL75531
The commit was made with a request for feedback in case selects might not be the best form. 3 years later, we lost half of the transform with:
https://reviews.llvm.org/rL159230
Sadly, that commit just removed code and didn't bother to canonicalize the other way (from select to ext+add). That left us in a state where logically equivalent code could be present in multiple forms - PR30327:
https://llvm.org/bugs/show_bug.cgi?id=30327
So in addition to removing the remaining piece of r75531, I'm proposing to canonicalize selects to ext+add and filling in whatever folds may be missing as follow-up patches (see the motivating case based on the source code for InstCombine itself in PR30273).
There are a few reasons to prefer ext+add to select:
- Adds with constants are easier to combine with other adds and subs.
- We are already canonicalizing selects with {-1,0,1} to extends; doing this for all constants is more consistent.
- As noted, we already lost half of the original patch, so we're not even consistent about C+1 vs. C-1.
- The stated reason for killing the C-1 half of the patch was poor codegen. This is not a valid reason by itself to change a canonicalization (just fix the backend). However, as a practical matter, it is 7 years later and we still have not fixed the backend! See the codegen examples for AArch64 and PowerPC here:
https://llvm.org/bugs/show_bug.cgi?id=30327#c2
In order to avoid a regression on the tests in test/Transforms/InstCombine/icmp-div-constant.ll (note that those are phantom diffs), I'm adding a fold that converts icmp (add (zext), -1) to icmp' (sext) in this patch. I'm not sure how to expose this difference independently of removing the select fold.
I guess we should prefer sext(cmp) over add(zext(cmp), -1), but we prefer add(zext(cmp), N) over add(sext(cmp), N-1) for any other N? That works, but you might want to explain that here.