[PATCH] D130433: [InstCombine] Add fold for redundant sign bits count comparison

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 28 07:07:19 PDT 2022


spatel added inline comments.


================
Comment at: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp:1688-1689
+  unsigned BitWidth = XType->getScalarSizeInBits();
+  if (Shift == 0 || Shift > BitWidth || PowerOf2.logBase2() < 2 ||
+      PowerOf2.logBase2() > BitWidth - 2)
+    return nullptr;
----------------
spatel wrote:
> spatel wrote:
> > I think this is still over-specified. It seems all we need is a non-zero shift and a power-of-2 compare constant:
> > https://alive2.llvm.org/ce/z/adaQ_n
> Sorry - I botched that code. We do have to guard against overflow when we increment the cmp constant:
> https://alive2.llvm.org/ce/z/eEUfx3
We don't need to guard against a poison shift (Shift > BitWidth), and the lower pow2 bound is not needed? 

Please add a test like this:
```
define i1 @src(i8 %x) {
  %y = ashr i8 %x, 5
  %z = xor i8 %y, %x
  %c = icmp ult i8 %z, 2
  ret i1 %c
}
```
We should get that:
https://alive2.llvm.org/ce/z/KemU48

Also, "u< 1" will be reduced to "== 0", so we probably want to canonicalize that pattern (and its inverse) too:
https://alive2.llvm.org/ce/z/ou5byR
...but that should be another patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D130433/new/

https://reviews.llvm.org/D130433



More information about the llvm-commits mailing list