Skip to content

Commit 7073ec5

Browse files
committed
[InstCombine] canonicalize more zext-and-of-bool compare to narrow and
https://alive2.llvm.org/ce/z/vBNiiM This matches variants of patterns that were folded with: b5a9361
1 parent 7898426 commit 7073ec5

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,14 +1940,15 @@ Instruction *InstCombinerImpl::foldICmpAndConstant(ICmpInst &Cmp,
19401940

19411941
// ((zext i1 X) & Y) == 0 --> !((trunc Y) & X)
19421942
// ((zext i1 X) & Y) != 0 --> ((trunc Y) & X)
1943+
// ((zext i1 X) & Y) == 1 --> ((trunc Y) & X)
1944+
// ((zext i1 X) & Y) != 1 --> !((trunc Y) & X)
19431945
if (match(And, m_OneUse(m_c_And(m_OneUse(m_ZExt(m_Value(X))), m_Value(Y)))) &&
1944-
C.isZero() && X->getType()->isIntOrIntVectorTy(1)) {
1946+
X->getType()->isIntOrIntVectorTy(1) && (C.isZero() || C.isOne())) {
19451947
Value *TruncY = Builder.CreateTrunc(Y, X->getType());
1946-
if (Pred == CmpInst::ICMP_EQ) {
1948+
if (C.isZero() ^ (Pred == CmpInst::ICMP_NE)) {
19471949
Value *And = Builder.CreateAnd(TruncY, X);
19481950
return BinaryOperator::CreateNot(And);
19491951
}
1950-
assert(Pred == CmpInst::ICMP_NE && "Unexpected predicate");
19511952
return BinaryOperator::CreateAnd(TruncY, X);
19521953
}
19531954

llvm/test/Transforms/InstCombine/icmp.ll

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4346,13 +4346,12 @@ define i1 @zext_bool_and_ne0(i1 %x, i8 %y) {
43464346
ret i1 %r
43474347
}
43484348

4349-
; TODO: This should transform similarly to eq/ne 0.
4350-
43514349
define i1 @zext_bool_and_ne1(i1 %x, i8 %y) {
43524350
; CHECK-LABEL: @zext_bool_and_ne1(
4353-
; CHECK-NEXT: [[ZX:%.*]] = zext i1 [[X:%.*]] to i8
4354-
; CHECK-NEXT: [[A:%.*]] = and i8 [[ZX]], [[Y:%.*]]
4355-
; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[A]], 1
4351+
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[Y:%.*]], 1
4352+
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i8 [[TMP1]], 0
4353+
; CHECK-NEXT: [[TMP3:%.*]] = and i1 [[TMP2]], [[X:%.*]]
4354+
; CHECK-NEXT: [[R:%.*]] = xor i1 [[TMP3]], true
43564355
; CHECK-NEXT: ret i1 [[R]]
43574356
;
43584357
%zx = zext i1 %x to i8
@@ -4363,9 +4362,8 @@ define i1 @zext_bool_and_ne1(i1 %x, i8 %y) {
43634362

43644363
define <2 x i1> @zext_bool_and_eq1(<2 x i1> %x, <2 x i8> %y) {
43654364
; CHECK-LABEL: @zext_bool_and_eq1(
4366-
; CHECK-NEXT: [[ZX:%.*]] = zext <2 x i1> [[X:%.*]] to <2 x i8>
4367-
; CHECK-NEXT: [[A:%.*]] = and <2 x i8> [[ZX]], [[Y:%.*]]
4368-
; CHECK-NEXT: [[R:%.*]] = icmp eq <2 x i8> [[A]], <i8 1, i8 1>
4365+
; CHECK-NEXT: [[TMP1:%.*]] = trunc <2 x i8> [[Y:%.*]] to <2 x i1>
4366+
; CHECK-NEXT: [[R:%.*]] = and <2 x i1> [[TMP1]], [[X:%.*]]
43694367
; CHECK-NEXT: ret <2 x i1> [[R]]
43704368
;
43714369
%zx = zext <2 x i1> %x to <2 x i8>

0 commit comments

Comments
 (0)