Skip to content

Commit 94de5cd

Browse files
committed
[InstCombine] Extend Fold of Zero-extended Bit Test
Previously, (zext (icmp ne (and X, (1 << ShAmt)), 0)) has only been folded if the bit width of X and the result were equal. Use a trunc or zext instruction to also support other bit widths. This is a follow-up to commit 533190a, which introduced a regression: (zext (icmp ne (and (lshr X ShAmt) 1) 0)) is not folded any longer to (zext/trunc (and (lshr X ShAmt) 1)) since the commit introduced the fold of (icmp ne (and (lshr X ShAmt) 1) 0) to (icmp ne (and X (1 << ShAmt)) 0). The change introduced by this commit restores this fold. Alive proof: https://alive2.llvm.org/ce/z/MFkNXs Relates to issue #86813.
1 parent f065a8e commit 94de5cd

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -985,19 +985,26 @@ Instruction *InstCombinerImpl::transformZExtICmp(ICmpInst *Cmp,
985985
}
986986
}
987987

988-
if (Cmp->isEquality() && Zext.getType() == Cmp->getOperand(0)->getType()) {
988+
if (Cmp->isEquality()) {
989989
// Test if a bit is clear/set using a shifted-one mask:
990990
// zext (icmp eq (and X, (1 << ShAmt)), 0) --> and (lshr (not X), ShAmt), 1
991991
// zext (icmp ne (and X, (1 << ShAmt)), 0) --> and (lshr X, ShAmt), 1
992992
Value *X, *ShAmt;
993993
if (Cmp->hasOneUse() && match(Cmp->getOperand(1), m_ZeroInt()) &&
994994
match(Cmp->getOperand(0),
995995
m_OneUse(m_c_And(m_Shl(m_One(), m_Value(ShAmt)), m_Value(X))))) {
996-
if (Cmp->getPredicate() == ICmpInst::ICMP_EQ)
997-
X = Builder.CreateNot(X);
998-
Value *Lshr = Builder.CreateLShr(X, ShAmt);
999-
Value *And1 = Builder.CreateAnd(Lshr, ConstantInt::get(X->getType(), 1));
1000-
return replaceInstUsesWith(Zext, And1);
996+
auto *And = cast<BinaryOperator>(Cmp->getOperand(0));
997+
Value *Shift = And->getOperand(X == And->getOperand(0) ? 1 : 0);
998+
if (Zext.getType() == And->getType() ||
999+
Cmp->getPredicate() != ICmpInst::ICMP_EQ || Shift->hasOneUse()) {
1000+
if (Cmp->getPredicate() == ICmpInst::ICMP_EQ)
1001+
X = Builder.CreateNot(X);
1002+
Value *Lshr = Builder.CreateLShr(X, ShAmt);
1003+
Value *And1 =
1004+
Builder.CreateAnd(Lshr, ConstantInt::get(X->getType(), 1));
1005+
return replaceInstUsesWith(
1006+
Zext, Builder.CreateZExtOrTrunc(And1, Zext.getType()));
1007+
}
10011008
}
10021009
}
10031010

llvm/test/Transforms/InstCombine/zext.ll

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,10 @@ define i32 @zext_or_masked_bit_test_uses(i32 %a, i32 %b, i32 %x) {
456456

457457
define i16 @zext_masked_bit_zero_to_smaller_bitwidth(i32 %a, i32 %b) {
458458
; CHECK-LABEL: @zext_masked_bit_zero_to_smaller_bitwidth(
459-
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 1, [[B:%.*]]
460-
; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHL]], [[A:%.*]]
461-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0
462-
; CHECK-NEXT: [[Z:%.*]] = zext i1 [[CMP]] to i16
459+
; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[A:%.*]], -1
460+
; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[TMP1]], [[B:%.*]]
461+
; CHECK-NEXT: [[TMP3:%.*]] = trunc i32 [[TMP2]] to i16
462+
; CHECK-NEXT: [[Z:%.*]] = and i16 [[TMP3]], 1
463463
; CHECK-NEXT: ret i16 [[Z]]
464464
;
465465
%shl = shl i32 1, %b
@@ -471,10 +471,10 @@ define i16 @zext_masked_bit_zero_to_smaller_bitwidth(i32 %a, i32 %b) {
471471

472472
define <4 x i16> @zext_masked_bit_zero_to_smaller_bitwidth_v4i32(<4 x i32> %a, <4 x i32> %b) {
473473
; CHECK-LABEL: @zext_masked_bit_zero_to_smaller_bitwidth_v4i32(
474-
; CHECK-NEXT: [[SHL:%.*]] = shl nuw <4 x i32> <i32 1, i32 1, i32 1, i32 1>, [[B:%.*]]
475-
; CHECK-NEXT: [[AND:%.*]] = and <4 x i32> [[SHL]], [[A:%.*]]
476-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <4 x i32> [[AND]], zeroinitializer
477-
; CHECK-NEXT: [[Z:%.*]] = zext <4 x i1> [[CMP]] to <4 x i16>
474+
; CHECK-NEXT: [[TMP1:%.*]] = xor <4 x i32> [[A:%.*]], <i32 -1, i32 -1, i32 -1, i32 -1>
475+
; CHECK-NEXT: [[TMP2:%.*]] = lshr <4 x i32> [[TMP1]], [[B:%.*]]
476+
; CHECK-NEXT: [[TMP3:%.*]] = trunc <4 x i32> [[TMP2]] to <4 x i16>
477+
; CHECK-NEXT: [[Z:%.*]] = and <4 x i16> [[TMP3]], <i16 1, i16 1, i16 1, i16 1>
478478
; CHECK-NEXT: ret <4 x i16> [[Z]]
479479
;
480480
%shl = shl <4 x i32> <i32 1, i32 1, i32 1, i32 1>, %b
@@ -504,10 +504,9 @@ define i16 @zext_masked_bit_zero_to_smaller_bitwidth_multi_use_shl(i32 %a, i32 %
504504

505505
define i16 @zext_masked_bit_nonzero_to_smaller_bitwidth(i32 %a, i32 %b) {
506506
; CHECK-LABEL: @zext_masked_bit_nonzero_to_smaller_bitwidth(
507-
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 1, [[B:%.*]]
508-
; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHL]], [[A:%.*]]
509-
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[AND]], 0
510-
; CHECK-NEXT: [[Z:%.*]] = zext i1 [[CMP]] to i16
507+
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[A:%.*]], [[B:%.*]]
508+
; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[TMP1]] to i16
509+
; CHECK-NEXT: [[Z:%.*]] = and i16 [[TMP2]], 1
511510
; CHECK-NEXT: ret i16 [[Z]]
512511
;
513512
%shl = shl i32 1, %b
@@ -520,9 +519,9 @@ define i16 @zext_masked_bit_nonzero_to_smaller_bitwidth(i32 %a, i32 %b) {
520519
define i16 @zext_masked_bit_nonzero_to_smaller_bitwidth_multi_use_shl(i32 %a, i32 %b) {
521520
; CHECK-LABEL: @zext_masked_bit_nonzero_to_smaller_bitwidth_multi_use_shl(
522521
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 1, [[B:%.*]]
523-
; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHL]], [[A:%.*]]
524-
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[AND]], 0
525-
; CHECK-NEXT: [[Z:%.*]] = zext i1 [[CMP]] to i16
522+
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[A:%.*]], [[B]]
523+
; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[TMP1]] to i16
524+
; CHECK-NEXT: [[Z:%.*]] = and i16 [[TMP2]], 1
526525
; CHECK-NEXT: call void @use32(i32 [[SHL]])
527526
; CHECK-NEXT: ret i16 [[Z]]
528527
;
@@ -536,10 +535,10 @@ define i16 @zext_masked_bit_nonzero_to_smaller_bitwidth_multi_use_shl(i32 %a, i3
536535

537536
define i64 @zext_masked_bit_zero_to_larger_bitwidth(i32 %a, i32 %b) {
538537
; CHECK-LABEL: @zext_masked_bit_zero_to_larger_bitwidth(
539-
; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 1, [[B:%.*]]
540-
; CHECK-NEXT: [[AND:%.*]] = and i32 [[SHL]], [[A:%.*]]
541-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[AND]], 0
542-
; CHECK-NEXT: [[Z:%.*]] = zext i1 [[CMP]] to i64
538+
; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[A:%.*]], -1
539+
; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[TMP1]], [[B:%.*]]
540+
; CHECK-NEXT: [[TMP3:%.*]] = and i32 [[TMP2]], 1
541+
; CHECK-NEXT: [[Z:%.*]] = zext nneg i32 [[TMP3]] to i64
543542
; CHECK-NEXT: ret i64 [[Z]]
544543
;
545544
%shl = shl i32 1, %b
@@ -551,10 +550,10 @@ define i64 @zext_masked_bit_zero_to_larger_bitwidth(i32 %a, i32 %b) {
551550

552551
define <4 x i64> @zext_masked_bit_zero_to_larger_bitwidth_v4i32(<4 x i32> %a, <4 x i32> %b) {
553552
; CHECK-LABEL: @zext_masked_bit_zero_to_larger_bitwidth_v4i32(
554-
; CHECK-NEXT: [[SHL:%.*]] = shl nuw <4 x i32> <i32 1, i32 1, i32 1, i32 1>, [[B:%.*]]
555-
; CHECK-NEXT: [[AND:%.*]] = and <4 x i32> [[SHL]], [[A:%.*]]
556-
; CHECK-NEXT: [[CMP:%.*]] = icmp eq <4 x i32> [[AND]], zeroinitializer
557-
; CHECK-NEXT: [[Z:%.*]] = zext <4 x i1> [[CMP]] to <4 x i64>
553+
; CHECK-NEXT: [[TMP1:%.*]] = xor <4 x i32> [[A:%.*]], <i32 -1, i32 -1, i32 -1, i32 -1>
554+
; CHECK-NEXT: [[TMP2:%.*]] = lshr <4 x i32> [[TMP1]], [[B:%.*]]
555+
; CHECK-NEXT: [[TMP3:%.*]] = and <4 x i32> [[TMP2]], <i32 1, i32 1, i32 1, i32 1>
556+
; CHECK-NEXT: [[Z:%.*]] = zext nneg <4 x i32> [[TMP3]] to <4 x i64>
558557
; CHECK-NEXT: ret <4 x i64> [[Z]]
559558
;
560559
%shl = shl <4 x i32> <i32 1, i32 1, i32 1, i32 1>, %b

0 commit comments

Comments
 (0)