[llvm] [ValueTracking] Let ComputeKnownSignBits handle (shl (zext X), C) (PR #97693)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 4 05:59:38 PDT 2024
goldsteinn wrote:
> > This looks reasonable to me. Can you replace the unit tests with lit tests please?
>
> I don't really know how to make good lit tests for ComputeNumSignBits. All I've got is something like this
>
> ```
> ; RUN: opt -passes=instcombine -S < %s | FileCheck %s
>
> define i16 @numsignbits_shl_zext(i8 %a) {
> %nsb6 = ashr i8 %a, 6
> %zext = zext i8 %nsb6 to i16
> %nsb4 = shl i16 %zext, 10
> %or = or i16 %nsb4, 1
> ; With four sign bits this should be nsw.
> %shl = add i16 %nsb4, 4096
> ret i16 %shl
> }
> ```
>
> to show that the `add` will get a `nsw`. But it far from verifies that ComputeNumSignBits actually is returning the correct value. Isn't it much easier to verify the correct behavior via the unit test? Maybe I should add a lit test like the one above as a complement, to show that it is possible to trigger this via a lit test as well (even though it doesn't validate that the calculation in ComputeNumSignBits is correct).
You could use the:
```
// (A & 2^C1) + A => A & (2^C1 - 1) iff bit C1 in A is a sign bit
if (match(&I, m_c_Add(m_And(m_Value(A), m_APInt(C1)), m_Deferred(A))) &&
C1->isPowerOf2() && (ComputeNumSignBits(A) > C1->countl_zero())) {
Constant *NewMask = ConstantInt::get(RHS->getType(), *C1 - 1);
return BinaryOperator::CreateAnd(A, NewMask);
}
```
fold and be precise (although typicaly I would do this with `nsw` flag on `shl`, but that doesn't really work here).
https://github.com/llvm/llvm-project/pull/97693
More information about the llvm-commits
mailing list