Skip to content

Commit c0d8f81

Browse files
committed
fixup! [InstCombine] Simplify the pattern a ne/eq (zext (a ne/eq c))
1 parent 55d52b1 commit c0d8f81

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6387,14 +6387,15 @@ Instruction *InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {
63876387
if (match(&I, m_c_ICmp(Pred1, m_Value(X),
63886388
m_CombineAnd(m_Instruction(ExtI),
63896389
m_ZExtOrSExt(m_ICmp(Pred2, m_Deferred(X),
6390-
m_APInt(C))))))) {
6390+
m_APInt(C)))))) &&
6391+
ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {
63916392
bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
63926393
bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();
63936394
auto CreateRangeCheck = [&] {
63946395
Value *CmpV1 =
63956396
Builder.CreateICmp(Pred1, X, Constant::getNullValue(X->getType()));
63966397
Value *CmpV2 = Builder.CreateICmp(
6397-
Pred1, X, ConstantInt::get(X->getType(), IsSExt ? -1 : 1));
6398+
Pred1, X, ConstantInt::getSigned(X->getType(), IsSExt ? -1 : 1));
63986399
return BinaryOperator::Create(
63996400
Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
64006401
CmpV1, CmpV2);
@@ -6440,8 +6441,9 @@ Instruction *InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {
64406441
// icmp ne X, (sext (icmp ne X, C)) --> icmp ne X, -1
64416442
return ICmpInst::Create(
64426443
Instruction::ICmp, Pred1, X,
6443-
ConstantInt::get(X->getType(),
6444-
Pred2 == ICmpInst::ICMP_NE ? (IsSExt ? -1 : 1) : 0));
6444+
ConstantInt::getSigned(X->getType(), Pred2 == ICmpInst::ICMP_NE
6445+
? (IsSExt ? -1 : 1)
6446+
: 0));
64456447
}
64466448
}
64476449

llvm/test/Transforms/InstCombine/and-or-icmps.ll

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,19 @@ define void @simplify_before_foldAndOfICmps(ptr %p) {
366366
; CHECK-LABEL: @simplify_before_foldAndOfICmps(
367367
; CHECK-NEXT: [[A8:%.*]] = alloca i16, align 2
368368
; CHECK-NEXT: [[L7:%.*]] = load i16, ptr [[A8]], align 2
369-
; CHECK-NEXT: [[C18:%.*]] = icmp slt i16 [[L7]], 1
370-
; CHECK-NEXT: [[L7_LOBIT:%.*]] = ashr i16 [[L7]], 15
371-
; CHECK-NEXT: [[TMP1:%.*]] = sext i16 [[L7_LOBIT]] to i64
372-
; CHECK-NEXT: [[G26:%.*]] = getelementptr i1, ptr null, i64 [[TMP1]]
369+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i16 [[L7]], -1
370+
; CHECK-NEXT: [[B11:%.*]] = zext i1 [[TMP1]] to i16
371+
; CHECK-NEXT: [[C10:%.*]] = icmp ugt i16 [[L7]], [[B11]]
372+
; CHECK-NEXT: [[C5:%.*]] = icmp slt i16 [[L7]], 1
373+
; CHECK-NEXT: [[C7:%.*]] = icmp slt i16 [[L7]], 0
374+
; CHECK-NEXT: [[B15:%.*]] = xor i1 [[C7]], [[C10]]
375+
; CHECK-NEXT: [[C6:%.*]] = xor i1 [[B15]], true
376+
; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[C5]], [[C6]]
377+
; CHECK-NEXT: [[C3:%.*]] = and i1 [[TMP2]], [[C10]]
378+
; CHECK-NEXT: [[TMP3:%.*]] = xor i1 [[C10]], true
379+
; CHECK-NEXT: [[C18:%.*]] = or i1 [[C7]], [[TMP3]]
380+
; CHECK-NEXT: [[TMP4:%.*]] = sext i1 [[C3]] to i64
381+
; CHECK-NEXT: [[G26:%.*]] = getelementptr i1, ptr null, i64 [[TMP4]]
373382
; CHECK-NEXT: store i16 [[L7]], ptr [[P:%.*]], align 2
374383
; CHECK-NEXT: store i1 [[C18]], ptr [[P]], align 1
375384
; CHECK-NEXT: store ptr [[G26]], ptr [[P]], align 8

llvm/test/Transforms/InstCombine/icmp-range.ll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,29 @@ define <2 x i1> @icmp_ne_sext_eq_otherwise_vec(<2 x i32> %a) {
14031403
ret <2 x i1> %cmp1
14041404
}
14051405

1406+
define i1 @icmp_ne_sext_ne_zero_i128(i128 %a) {
1407+
; CHECK-LABEL: @icmp_ne_sext_ne_zero_i128(
1408+
; CHECK-NEXT: [[TMP1:%.*]] = add i128 [[A:%.*]], -1
1409+
; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i128 [[TMP1]], -2
1410+
; CHECK-NEXT: ret i1 [[CMP1]]
1411+
;
1412+
%cmp = icmp ne i128 %a, 0
1413+
%conv = sext i1 %cmp to i128
1414+
%cmp1 = icmp ne i128 %conv, %a
1415+
ret i1 %cmp1
1416+
}
1417+
1418+
define i1 @icmp_ne_sext_ne_otherwise_i128(i128 %a) {
1419+
; CHECK-LABEL: @icmp_ne_sext_ne_otherwise_i128(
1420+
; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i128 [[A:%.*]], -1
1421+
; CHECK-NEXT: ret i1 [[CMP1]]
1422+
;
1423+
%cmp = icmp ne i128 %a, 2
1424+
%conv = sext i1 %cmp to i128
1425+
%cmp1 = icmp ne i128 %conv, %a
1426+
ret i1 %cmp1
1427+
}
1428+
14061429
!0 = !{i32 1, i32 6}
14071430
!1 = !{i32 0, i32 6}
14081431
!2 = !{i8 0, i8 1}

0 commit comments

Comments
 (0)