Skip to content

Commit 32679e1

Browse files
committed
[InstCombine] Handle logical op for and/or of icmp 0/-1
This aligns the transform with what foldLogOpOfMaskedICmp() does.
1 parent c8f40e7 commit 32679e1

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3384,19 +3384,21 @@ Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
33843384
// (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0)
33853385
// (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0)
33863386
// TODO: Remove this and below when foldLogOpOfMaskedICmps can handle undefs.
3387-
if (!IsLogical && PredL == (IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE) &&
3387+
if (PredL == (IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE) &&
33883388
PredL == PredR && match(LHS1, m_ZeroInt()) && match(RHS1, m_ZeroInt()) &&
3389-
LHS0->getType() == RHS0->getType()) {
3389+
LHS0->getType() == RHS0->getType() &&
3390+
(!IsLogical || isGuaranteedNotToBePoison(RHS0))) {
33903391
Value *NewOr = Builder.CreateOr(LHS0, RHS0);
33913392
return Builder.CreateICmp(PredL, NewOr,
33923393
Constant::getNullValue(NewOr->getType()));
33933394
}
33943395

33953396
// (icmp ne A, -1) | (icmp ne B, -1) --> (icmp ne (A&B), -1)
33963397
// (icmp eq A, -1) & (icmp eq B, -1) --> (icmp eq (A&B), -1)
3397-
if (!IsLogical && PredL == (IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE) &&
3398+
if (PredL == (IsAnd ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE) &&
33983399
PredL == PredR && match(LHS1, m_AllOnes()) && match(RHS1, m_AllOnes()) &&
3399-
LHS0->getType() == RHS0->getType()) {
3400+
LHS0->getType() == RHS0->getType() &&
3401+
(!IsLogical || isGuaranteedNotToBePoison(RHS0))) {
34003402
Value *NewAnd = Builder.CreateAnd(LHS0, RHS0);
34013403
return Builder.CreateICmp(PredL, NewAnd,
34023404
Constant::getAllOnesValue(LHS0->getType()));

llvm/test/Transforms/InstCombine/bit-checks.ll

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,11 +1357,10 @@ define i1 @no_masks_with_logical_or2(i32 %a, i32 %b, i32 noundef %c) {
13571357

13581358
define <2 x i1> @no_masks_with_logical_or_vec_poison1(<2 x i32> %a, <2 x i32> %b, <2 x i32> noundef %c) {
13591359
; CHECK-LABEL: @no_masks_with_logical_or_vec_poison1(
1360-
; CHECK-NEXT: [[CMP1:%.*]] = icmp ne <2 x i32> [[A:%.*]], <i32 0, i32 poison>
13611360
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne <2 x i32> [[B:%.*]], <i32 63, i32 poison>
1362-
; CHECK-NEXT: [[OR1:%.*]] = select <2 x i1> [[CMP1]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[CMP2]]
1363-
; CHECK-NEXT: [[CMP3:%.*]] = icmp ne <2 x i32> [[C:%.*]], <i32 0, i32 poison>
1364-
; CHECK-NEXT: [[OR2:%.*]] = or <2 x i1> [[OR1]], [[CMP3]]
1361+
; CHECK-NEXT: [[TMP1:%.*]] = or <2 x i32> [[A:%.*]], [[C:%.*]]
1362+
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne <2 x i32> [[TMP1]], zeroinitializer
1363+
; CHECK-NEXT: [[OR2:%.*]] = select <2 x i1> [[TMP2]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[CMP2]]
13651364
; CHECK-NEXT: ret <2 x i1> [[OR2]]
13661365
;
13671366
%cmp1 = icmp ne <2 x i32> %a, <i32 0, i32 poison>
@@ -1374,11 +1373,10 @@ define <2 x i1> @no_masks_with_logical_or_vec_poison1(<2 x i32> %a, <2 x i32> %b
13741373

13751374
define <2 x i1> @no_masks_with_logical_or_vec_poison2(<2 x i32> %a, <2 x i32> %b, <2 x i32> noundef %c) {
13761375
; CHECK-LABEL: @no_masks_with_logical_or_vec_poison2(
1377-
; CHECK-NEXT: [[CMP1:%.*]] = icmp ne <2 x i32> [[A:%.*]], <i32 -1, i32 poison>
13781376
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne <2 x i32> [[B:%.*]], <i32 63, i32 poison>
1379-
; CHECK-NEXT: [[OR1:%.*]] = select <2 x i1> [[CMP1]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[CMP2]]
1380-
; CHECK-NEXT: [[CMP3:%.*]] = icmp ne <2 x i32> [[C:%.*]], <i32 -1, i32 poison>
1381-
; CHECK-NEXT: [[OR2:%.*]] = or <2 x i1> [[OR1]], [[CMP3]]
1377+
; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[A:%.*]], [[C:%.*]]
1378+
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne <2 x i32> [[TMP1]], <i32 -1, i32 -1>
1379+
; CHECK-NEXT: [[OR2:%.*]] = select <2 x i1> [[TMP2]], <2 x i1> <i1 true, i1 true>, <2 x i1> [[CMP2]]
13821380
; CHECK-NEXT: ret <2 x i1> [[OR2]]
13831381
;
13841382
%cmp1 = icmp ne <2 x i32> %a, <i32 -1, i32 poison>

0 commit comments

Comments
 (0)