diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -4085,15 +4085,34 @@ Constant::getNullValue(X->getType()), I.getName()); } +static std::optional +getKnownSign(Value *Op, Instruction *CxtI, const DataLayout &DL, + AssumptionCache *AC, const DominatorTree *DT, KnownBits &Known) { + Known = computeKnownBits(Op, DL, 0, AC, CxtI, DT); + if (Known.isNonNegative()) + return false; + if (Known.isNegative()) + return true; + + Value *X, *Y; + if (match(Op, m_NSWSub(m_Value(X), m_Value(Y)))) + return isImpliedByDomCondition(ICmpInst::ICMP_SLT, X, Y, CxtI, DL); + + return isImpliedByDomCondition( + ICmpInst::ICMP_SLT, Op, Constant::getNullValue(Op->getType()), CxtI, DL); +} + static Instruction *foldICmpXorXX(ICmpInst &I, const SimplifyQuery &Q, InstCombinerImpl &IC) { Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1), *A; + // Normalize xor operand as operand 0. CmpInst::Predicate Pred = I.getPredicate(); if (match(Op1, m_c_Xor(m_Specific(Op0), m_Value()))) { std::swap(Op0, Op1); Pred = ICmpInst::getSwappedPredicate(Pred); } + if (!match(Op0, m_c_Xor(m_Specific(Op1), m_Value(A)))) return nullptr; @@ -4106,6 +4125,75 @@ isKnownNonZero(A, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT)) return new ICmpInst(PredOut, Op0, Op1); + // icmp (X ^ Y) u> X --> X & MSB(Y) == 0 + // icmp (X ^ Y) u< X --> X & MSB(Y) != 0 + // icmp (X ^ Pos_Y) s> X --> X & MSB(Pow2_Y) == 0 + // icmp (X ^ Pos_Y) s< X --> X & MSB(Pow2_Y) != 0 + switch (Pred) { + case ICmpInst::ICMP_UGT: + case ICmpInst::ICMP_SGT: + PredOut = ICmpInst::ICMP_EQ; + break; + case ICmpInst::ICMP_ULT: + case ICmpInst::ICMP_SLT: + PredOut = ICmpInst::ICMP_NE; + break; + default: + break; + } + + if (PredOut == Pred) + return nullptr; + + bool Usable = true; + std::optional KnownOpt; + // If signed version we need extra for negative / positive. + if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SLT) { + KnownBits Known; + auto KnownSign = getKnownSign(A, &I, Q.DL, Q.AC, Q.DT, Known); + KnownOpt = Known; + Usable = false; + if (KnownSign != std::nullopt) { + // icmp (X ^ Neg_Y) s> X --> X s< 0 + // icmp (X ^ Neg_Y) s< X --> X s>= 0 + if (*KnownSign /* true is Signed. */) { + // Negative power of 2 must be IntMin so this just a sign + // comparison. + if (Pred == ICmpInst::ICMP_SGT) + PredOut = ICmpInst::ICMP_SLT; + if (Pred == ICmpInst::ICMP_SLT) + PredOut = ICmpInst::ICMP_SGE; + return new ICmpInst(PredOut, Op1, + Constant::getNullValue(Op0->getType())); + } + Usable = !*KnownSign; + } + } + + if (Usable && Op0->hasOneUse()) { + // If known power of 2, then MSB == A. + if (IC.isKnownToBeAPowerOfTwo(A, /*OrZero*/ true, 0, &I)) + return new ICmpInst(PredOut, IC.Builder.CreateAnd(Op1, A), + Constant::getNullValue(Op0->getType())); + + // Otherwise try and find known MSB. + if (KnownOpt == std::nullopt) + KnownOpt = IC.computeKnownBits(A, 0, &I); + + unsigned MSBPos = KnownOpt->countMaxLeadingZeros(); + unsigned BitWidth = KnownOpt->getBitWidth(); + if (MSBPos == 0 || + (MSBPos != BitWidth && MSBPos == KnownOpt->countMinLeadingZeros())) { + Type *Ty = Op0->getType(); + return new ICmpInst( + PredOut, + IC.Builder.CreateAnd( + Op1, ConstantInt::get(Ty, APInt::getOneBitSet( + BitWidth, BitWidth - MSBPos - 1))), + Constant::getNullValue(Ty)); + } + } + return nullptr; } diff --git a/llvm/test/Transforms/InstCombine/icmp-of-xor-x.ll b/llvm/test/Transforms/InstCombine/icmp-of-xor-x.ll --- a/llvm/test/Transforms/InstCombine/icmp-of-xor-x.ll +++ b/llvm/test/Transforms/InstCombine/icmp-of-xor-x.ll @@ -62,9 +62,7 @@ define i1 @xor_sge(i8 %x, i8 %yy) { ; CHECK-LABEL: @xor_sge( -; CHECK-NEXT: [[Y:%.*]] = or i8 [[YY:%.*]], -128 -; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[Y]], [[X:%.*]] -; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[XOR]], [[X]] +; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[X:%.*]], 0 ; CHECK-NEXT: ret i1 [[R]] ; %y = or i8 %yy, 128 @@ -76,10 +74,8 @@ define i1 @xor_ugt_2(i8 %xx, i8 %y, i8 %z) { ; CHECK-LABEL: @xor_ugt_2( ; CHECK-NEXT: [[X:%.*]] = add i8 [[XX:%.*]], [[Z:%.*]] -; CHECK-NEXT: [[YZ:%.*]] = and i8 [[Y:%.*]], 63 -; CHECK-NEXT: [[Y1:%.*]] = or i8 [[YZ]], 64 -; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[X]], [[Y1]] -; CHECK-NEXT: [[R:%.*]] = icmp ugt i8 [[X]], [[XOR]] +; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X]], 64 +; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[TMP1]], 0 ; CHECK-NEXT: ret i1 [[R]] ; %x = add i8 %xx, %z @@ -92,8 +88,8 @@ define i1 @xor_ult(i8 %x) { ; CHECK-LABEL: @xor_ult( -; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[X:%.*]], 123 -; CHECK-NEXT: [[R:%.*]] = icmp ult i8 [[XOR]], [[X]] +; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], 64 +; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[TMP1]], 0 ; CHECK-NEXT: ret i1 [[R]] ; %xor = xor i8 %x, 123 @@ -103,10 +99,8 @@ define <2 x i1> @xor_sgt(<2 x i8> %x, <2 x i8> %y) { ; CHECK-LABEL: @xor_sgt( -; CHECK-NEXT: [[YZ:%.*]] = and <2 x i8> [[Y:%.*]], -; CHECK-NEXT: [[Y1:%.*]] = or <2 x i8> [[YZ]], -; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i8> [[Y1]], [[X:%.*]] -; CHECK-NEXT: [[R:%.*]] = icmp sgt <2 x i8> [[XOR]], [[X]] +; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i8> [[X:%.*]], +; CHECK-NEXT: [[R:%.*]] = icmp eq <2 x i8> [[TMP1]], zeroinitializer ; CHECK-NEXT: ret <2 x i1> [[R]] ; %yz = and <2 x i8> %y, @@ -133,8 +127,8 @@ define i1 @xor_slt_2(i8 %x, i8 %y, i8 %z) { ; CHECK-LABEL: @xor_slt_2( -; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[X:%.*]], 88 -; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[XOR]], [[X]] +; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], 64 +; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[TMP1]], 0 ; CHECK-NEXT: ret i1 [[R]] ; %xor = xor i8 %x, 88 @@ -145,9 +139,7 @@ define <2 x i1> @xor_sgt_intmin_2(<2 x i8> %xx, <2 x i8> %yy, <2 x i8> %z) { ; CHECK-LABEL: @xor_sgt_intmin_2( ; CHECK-NEXT: [[X:%.*]] = add <2 x i8> [[XX:%.*]], [[Z:%.*]] -; CHECK-NEXT: [[Y:%.*]] = or <2 x i8> [[YY:%.*]], -; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i8> [[X]], [[Y]] -; CHECK-NEXT: [[R:%.*]] = icmp sgt <2 x i8> [[X]], [[XOR]] +; CHECK-NEXT: [[R:%.*]] = icmp sgt <2 x i8> [[X]], ; CHECK-NEXT: ret <2 x i1> [[R]] ; %x = add <2 x i8> %xx, %z @@ -165,8 +157,7 @@ ; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = phi i1 [ [[R:%.*]], [[NEG]] ], [ false, [[POS]] ] ; CHECK-NEXT: ret i1 [[COMMON_RET_OP]] ; CHECK: neg: -; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[C]], [[X:%.*]] -; CHECK-NEXT: [[R]] = icmp slt i8 [[XOR]], [[X]] +; CHECK-NEXT: [[R]] = icmp sgt i8 [[X:%.*]], -1 ; CHECK-NEXT: br label [[COMMON_RET:%.*]] ; CHECK: pos: ; CHECK-NEXT: tail call void @barrier()