[llvm] 369ef9b - [InstCombine] Extract code for or of icmp eq zero and icmp fold (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 22 07:49:06 PDT 2022
Author: Nikita Popov
Date: 2022-04-22T16:48:59+02:00
New Revision: 369ef9bf6056154cf8cededce103f1f035527807
URL: https://github.com/llvm/llvm-project/commit/369ef9bf6056154cf8cededce103f1f035527807
DIFF: https://github.com/llvm/llvm-project/commit/369ef9bf6056154cf8cededce103f1f035527807.diff
LOG: [InstCombine] Extract code for or of icmp eq zero and icmp fold (NFC)
To make it easier to extend this to the congruent and case.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 2e9d49052a91c..2ef03ad4044ed 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2381,6 +2381,34 @@ Value *InstCombinerImpl::matchSelectFromAndOr(Value *A, Value *C, Value *B,
return nullptr;
}
+// (icmp eq X, 0) | (icmp ult Other, X) -> (icmp ule Other, X-1)
+Value *foldAndOrOfICmpEqZeroAndICmp(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
+ IRBuilderBase &Builder) {
+ if (IsAnd)
+ return nullptr;
+
+ ICmpInst::Predicate LPred = LHS->getPredicate();
+ ICmpInst::Predicate RPred = RHS->getPredicate();
+ Value *LHS0 = LHS->getOperand(0);
+ if (LPred != ICmpInst::ICMP_EQ || !match(LHS->getOperand(1), m_Zero()) ||
+ !LHS0->getType()->isIntOrIntVectorTy() ||
+ !(LHS->hasOneUse() || RHS->hasOneUse()))
+ return nullptr;
+
+ Value *Other;
+ if (RPred == ICmpInst::ICMP_ULT && RHS->getOperand(1) == LHS0)
+ Other = RHS->getOperand(0);
+ else if (RPred == ICmpInst::ICMP_UGT && RHS->getOperand(0) == LHS0)
+ Other = RHS->getOperand(1);
+ else
+ return nullptr;
+
+ return Builder.CreateICmp(
+ ICmpInst::ICMP_UGE,
+ Builder.CreateAdd(LHS0, Constant::getAllOnesValue(LHS0->getType())),
+ Other);
+}
+
/// Fold (icmp)&(icmp) or (icmp)|(icmp) if possible.
Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
BinaryOperator &BO, bool IsAnd) {
@@ -2472,31 +2500,10 @@ Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
if (Value *V = foldLogOpOfMaskedICmps(LHS, RHS, IsAnd, Builder))
return V;
- if (!IsAnd && (LHS->hasOneUse() || RHS->hasOneUse())) {
- // (icmp eq B, 0) | (icmp ult A, B) -> (icmp ule A, B-1)
- // (icmp eq B, 0) | (icmp ugt B, A) -> (icmp ule A, B-1)
- Value *A = nullptr, *B = nullptr;
- if (PredL == ICmpInst::ICMP_EQ && match(LHS1, m_Zero())) {
- B = LHS0;
- if (PredR == ICmpInst::ICMP_ULT && LHS0 == RHS1)
- A = RHS0;
- else if (PredR == ICmpInst::ICMP_UGT && LHS0 == RHS0)
- A = RHS1;
- }
- // (icmp ult A, B) | (icmp eq B, 0) -> (icmp ule A, B-1)
- // (icmp ugt B, A) | (icmp eq B, 0) -> (icmp ule A, B-1)
- else if (PredR == ICmpInst::ICMP_EQ && match(RHS1, m_Zero())) {
- B = RHS0;
- if (PredL == ICmpInst::ICMP_ULT && RHS0 == LHS1)
- A = LHS0;
- else if (PredL == ICmpInst::ICMP_UGT && RHS0 == LHS0)
- A = LHS1;
- }
- if (A && B && B->getType()->isIntOrIntVectorTy())
- return Builder.CreateICmp(
- ICmpInst::ICMP_UGE,
- Builder.CreateAdd(B, Constant::getAllOnesValue(B->getType())), A);
- }
+ if (Value *V = foldAndOrOfICmpEqZeroAndICmp(LHS, RHS, IsAnd, Builder))
+ return V;
+ if (Value *V = foldAndOrOfICmpEqZeroAndICmp(RHS, LHS, IsAnd, Builder))
+ return V;
if (Value *V = foldAndOrOfICmpsWithConstEq(LHS, RHS, BO, Builder, Q))
return V;
More information about the llvm-commits
mailing list