Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[InstCombine] Simplify the pattern a ne/eq (zext/sext (a ne/eq c)) #65852

Merged
merged 15 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6380,7 +6380,74 @@ Instruction *InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {
Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);

// icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
ICmpInst::Predicate Pred1, Pred2;
const APInt *C;
Instruction *ExtI;
if (match(&I, m_c_ICmp(Pred1, m_Value(X),
m_CombineAnd(m_Instruction(ExtI),
m_ZExtOrSExt(m_ICmp(Pred2, m_Deferred(X),
m_APInt(C))))))) {
dtcxzyw marked this conversation as resolved.
Show resolved Hide resolved
bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();
if (C->isZero()) {
if (Pred2 == ICmpInst::ICMP_EQ) {
// icmp eq X, (zext/sext (icmp eq X, 0)) --> false
// icmp ne X, (zext/sext (icmp eq X, 0)) --> true
return replaceInstUsesWith(
I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
} else if (!IsSExt || HasOneUse) {
// icmp eq X, (zext (icmp ne X, 0)) --> icmp ult X, 2
// icmp ne X, (zext (icmp ne X, 0)) --> icmp ugt X, 1
// icmp eq X, (sext (icmp ne X, 0)) --> icmp ult (X + 1), 2
// icmp ne X, (sext (icmp ne X, 0)) --> icmp ugt (X + 1), 1
return ICmpInst::Create(
Instruction::ICmp,
Pred1 == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGT
: ICmpInst::ICMP_ULT,
IsSExt ? Builder.CreateAdd(X, ConstantInt::get(X->getType(), 1))
: X,
ConstantInt::get(X->getType(), Pred1 == ICmpInst::ICMP_NE ? 1 : 2));
}
} else if (IsSExt ? C->isAllOnes() : C->isOne()) {
if (Pred2 == ICmpInst::ICMP_NE) {
// icmp eq X, (zext (icmp ne X, 1)) --> false
// icmp ne X, (zext (icmp ne X, 1)) --> true
// icmp eq X, (sext (icmp ne X, -1)) --> false
// icmp ne X, (sext (icmp ne X, -1)) --> true
return replaceInstUsesWith(
I, ConstantInt::getBool(I.getType(), Pred1 == ICmpInst::ICMP_NE));
} else if (!IsSExt || HasOneUse) {
// icmp eq X, (zext (icmp eq X, 1)) --> icmp ult X, 2
// icmp ne X, (zext (icmp eq X, 1)) --> icmp ugt X, 1
// icmp eq X, (sext (icmp eq X, -1)) --> icmp ult (X + 1), 2
// icmp ne X, (sext (icmp eq X, -1)) --> icmp ugt (X + 1), 1
dtcxzyw marked this conversation as resolved.
Show resolved Hide resolved
dtcxzyw marked this conversation as resolved.
Show resolved Hide resolved
return ICmpInst::Create(
Instruction::ICmp,
Pred1 == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGT
: ICmpInst::ICMP_ULT,
IsSExt ? Builder.CreateAdd(X, ConstantInt::get(X->getType(), 1))
: X,
ConstantInt::get(X->getType(), Pred1 == ICmpInst::ICMP_NE ? 1 : 2));
}
} else {
// when C != 0 && C != 1:
// icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
// icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
// icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
// icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
// when C != 0 && C != -1:
// icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
// icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, -1
// icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
// icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, -1
dtcxzyw marked this conversation as resolved.
Show resolved Hide resolved
return ICmpInst::Create(
Instruction::ICmp, Pred1, X,
ConstantInt::get(X->getType(),
Pred2 == ICmpInst::ICMP_NE ? (IsSExt ? -1 : 1) : 0));
dtcxzyw marked this conversation as resolved.
Show resolved Hide resolved
}
}

dtcxzyw marked this conversation as resolved.
Show resolved Hide resolved
if (match(I.getOperand(0), m_c_Add(m_ZExt(m_Value(X)), m_SExt(m_Value(Y)))) &&
match(I.getOperand(1), m_APInt(C)) &&
X->getType()->isIntOrIntVectorTy(1) &&
Expand Down
17 changes: 4 additions & 13 deletions llvm/test/Transforms/InstCombine/and-or-icmps.ll
Original file line number Diff line number Diff line change
Expand Up @@ -366,19 +366,10 @@ define void @simplify_before_foldAndOfICmps(ptr %p) {
; CHECK-LABEL: @simplify_before_foldAndOfICmps(
; CHECK-NEXT: [[A8:%.*]] = alloca i16, align 2
; CHECK-NEXT: [[L7:%.*]] = load i16, ptr [[A8]], align 2
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i16 [[L7]], -1
; CHECK-NEXT: [[B11:%.*]] = zext i1 [[TMP1]] to i16
; CHECK-NEXT: [[C10:%.*]] = icmp ugt i16 [[L7]], [[B11]]
; CHECK-NEXT: [[C5:%.*]] = icmp slt i16 [[L7]], 1
; CHECK-NEXT: [[C7:%.*]] = icmp slt i16 [[L7]], 0
; CHECK-NEXT: [[B15:%.*]] = xor i1 [[C7]], [[C10]]
; CHECK-NEXT: [[C6:%.*]] = xor i1 [[B15]], true
; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[C5]], [[C6]]
; CHECK-NEXT: [[C3:%.*]] = and i1 [[TMP2]], [[C10]]
; CHECK-NEXT: [[TMP3:%.*]] = xor i1 [[C10]], true
; CHECK-NEXT: [[C18:%.*]] = or i1 [[C7]], [[TMP3]]
; CHECK-NEXT: [[TMP4:%.*]] = sext i1 [[C3]] to i64
; CHECK-NEXT: [[G26:%.*]] = getelementptr i1, ptr null, i64 [[TMP4]]
; CHECK-NEXT: [[C18:%.*]] = icmp slt i16 [[L7]], 1
; CHECK-NEXT: [[L7_LOBIT:%.*]] = ashr i16 [[L7]], 15
; CHECK-NEXT: [[TMP1:%.*]] = sext i16 [[L7_LOBIT]] to i64
; CHECK-NEXT: [[G26:%.*]] = getelementptr i1, ptr null, i64 [[TMP1]]
; CHECK-NEXT: store i16 [[L7]], ptr [[P:%.*]], align 2
dtcxzyw marked this conversation as resolved.
Show resolved Hide resolved
; CHECK-NEXT: store i1 [[C18]], ptr [[P]], align 1
; CHECK-NEXT: store ptr [[G26]], ptr [[P]], align 8
Expand Down
Loading