[InstCombine] canonicalize icmp with trunc op into mask and cmp, part 2
authorSanjay Patel <spatel@rotateright.com>
Tue, 16 Nov 2021 14:18:18 +0000 (09:18 -0500)
committerSanjay Patel <spatel@rotateright.com>
Tue, 16 Nov 2021 14:27:30 +0000 (09:27 -0500)
commit8fce94f91610bc4614b1fe0bff8f143ea2b54742
tree0924e93db11b0f806ddcfb4e6aa8f19aa8dee2e4
parent900cc1a226c8b8512fd3d4af90f9d7e5ecc1e722
[InstCombine] canonicalize icmp with trunc op into mask and cmp, part 2

If C is a high-bit mask:
(trunc X) u< C --> (X & C) != C (are any masked-high-bits clear?)

If C is low-bit mask:
(trunc X) u> C --> (X & ~C) != 0 (are any masked-high-bits set?)

If C is not-of-power-of-2 (one clear bit):
(trunc X) u> C --> (X & (C+1)) == C+1 (are all masked-high-bits set?)

This extends the fold added with:
acabad9ff6bf (https://alive2.llvm.org/ce/z/aFr7qV)

Using decomposeBitTestICmp() to generalize this is a planned follow-up, but that requires removing an inverse fold.

Here are Alive2 generalizations for these folds:
https://alive2.llvm.org/ce/z/u-ZpC_ (ult, the previous patch)
https://alive2.llvm.org/ce/z/YsuAu2 (ult, this patch)
https://alive2.llvm.org/ce/z/ekktQP (ugt, low bitmask)
https://alive2.llvm.org/ce/z/pJY9wR (ugt, one clear bit)

Differential Revision: https://reviews.llvm.org/D112634
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/icmp-trunc.ll