[llvm] [InstCombine] Fold zext(i1) == lshr(A, BW - 1) => i1 == A s< 0 (PR #68244)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 12 12:49:16 PDT 2023
================
@@ -5325,17 +5321,37 @@ Instruction *InstCombinerImpl::foldICmpEquality(ICmpInst &I) {
ICmpInst::Predicate Pred2;
if (match(Op0, m_CombineAnd(m_Instruction(ShiftI),
m_Shr(m_Value(X),
- m_SpecificIntAllowUndef(OpWidth - 1)))) &&
- match(A, m_ICmp(Pred2, m_Value(Y), m_AllOnes())) &&
- Pred2 == ICmpInst::ICMP_SGT && X->getType() == Y->getType()) {
+ m_SpecificIntAllowUndef(OpWidth - 1))))) {
+ // Test if 2 values have different or same signbits:
+ // (X u>> BitWidth - 1) == zext (Y s> -1) --> (X ^ Y) < 0
+ // (X u>> BitWidth - 1) != zext (Y s> -1) --> (X ^ Y) > -1
+ // (X s>> BitWidth - 1) == sext (Y s> -1) --> (X ^ Y) < 0
+ // (X s>> BitWidth - 1) != sext (Y s> -1) --> (X ^ Y) > -1
unsigned ExtOpc = ExtI->getOpcode();
unsigned ShiftOpc = ShiftI->getOpcode();
- if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) ||
- (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) {
- Value *Xor = Builder.CreateXor(X, Y, "xor.signbits");
- Value *R = (Pred == ICmpInst::ICMP_EQ) ? Builder.CreateIsNeg(Xor)
- : Builder.CreateIsNotNeg(Xor);
- return replaceInstUsesWith(I, R);
+
+ if (match(A, m_ICmp(Pred2, m_Value(Y), m_AllOnes())) &&
+ Pred2 == ICmpInst::ICMP_SGT && X->getType() == Y->getType()) {
+ if ((ExtOpc == Instruction::ZExt && ShiftOpc == Instruction::LShr) ||
+ (ExtOpc == Instruction::SExt && ShiftOpc == Instruction::AShr)) {
+ Value *Xor = Builder.CreateXor(X, Y, "xor.signbits");
+ Value *R = (Pred == ICmpInst::ICMP_EQ) ? Builder.CreateIsNeg(Xor)
+ : Builder.CreateIsNotNeg(Xor);
+ return replaceInstUsesWith(I, R);
+ }
+ }
+
+ // Transform (X u>> BitWidth - 1 == zext(i1)) into X s< 0 == i1
+ // Transform (X u>> BitWidth - 1 != zext(i1)) into X s< 0 != i1
+ if (A->getType()->getScalarSizeInBits() == 1 && Op0->hasOneUse() &&
+ Op1->hasOneUse() && ExtOpc == Instruction::ZExt &&
+ ShiftOpc == Instruction::LShr) {
+
+ Value *SLTZero =
+ Builder.CreateICmpSLT(X, Constant::getNullValue(X->getType()));
+ SLTZero->takeName(Op0);
----------------
goldsteinn wrote:
This doesn't seem necessary.
https://github.com/llvm/llvm-project/pull/68244
More information about the llvm-commits
mailing list