[llvm] [InstCombine] Missed optimization: Fold (sext(a) & sext(c1)) == c2 to (a & c1) == c2 (PR #112646)
Lee Wei via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 17 20:59:36 PDT 2024
leewei05 wrote:
> Instead you can do `match(Op1, m_APInt(C2))` then check its okay with `C->getNumSignBits() <= A->getType()->getScalarSizeInBits() && C2->getNumSignBits() <= A->getType()->getScalarSizeInBits()` which is all the constraint that the proof needs.
@goldsteinn
Thanks for the pointers! I tried your approach but I think the approach is a bit different from your example. Example: https://alive2.llvm.org/ce/z/GPZPJD. I can see that `C1->getNumSignBits()` relates to `%b_signbits = select i1 %b_neg, i32 %b_cl1, i32 %b_clz`. However, I also see that there is a `%b_bits = sub i32 33, %b_signbits` before comparing with `%a`'s bit width. Is this step also required to make it work?
Also, I printed the bit value for the following example.
```
%conv = sext i8 %a to i32
%0 = and i32 %conv, -2147483647
%cmp = icmp eq i32 %0, 1
ret i1 %cmp
```
Compare directly with `C->getNumSignBits() <= A->getType()->getScalarSizeInBits() && C2->getNumSignBits() <= A->getType()->getScalarSizeInBits()` couldn't match the above example. Is there a step I missed?
```
if (match(Op0, m_And(m_SExtLike(m_Value(A)), m_APInt(C1))) && match(Op1, m_APInt(C2))) {
auto *InputTy = A->getType();
errs() << "A: " << InputTy->getScalarSizeInBits() << "\n";
errs() << "C1: " << C1->getNumSignBits() << "\n";
errs() << "C2: " << C2->getNumSignBits() << "\n";
...
}
A: 8
C1: 1 (-2147483647 = 0x80000001) count leading 1s get 1
C2: 31 (1 = 0x00000001) count leading 0s get 31
```
https://github.com/llvm/llvm-project/pull/112646
More information about the llvm-commits
mailing list