Index: llvm/include/llvm/IR/PatternMatch.h =================================================================== --- llvm/include/llvm/IR/PatternMatch.h +++ llvm/include/llvm/IR/PatternMatch.h @@ -1189,13 +1189,16 @@ : Predicate(Pred), L(LHS), R(RHS) {} template bool match(OpTy *V) { - if (auto *I = dyn_cast(V)) - if ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || - (Commutable && L.match(I->getOperand(1)) && - R.match(I->getOperand(0)))) { + if (auto *I = dyn_cast(V)) { + if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) { Predicate = I->getPredicate(); return true; + } else if (Commutable && L.match(I->getOperand(1)) && + R.match(I->getOperand(0))) { + Predicate = I->getSwappedPredicate(); + return true; } + } return false; } }; Index: llvm/lib/Analysis/InstructionSimplify.cpp =================================================================== --- llvm/lib/Analysis/InstructionSimplify.cpp +++ llvm/lib/Analysis/InstructionSimplify.cpp @@ -1429,9 +1429,6 @@ if (match(UnsignedICmp, m_c_ICmp(UnsignedPred, m_Specific(A), m_Specific(B))) && ICmpInst::isUnsigned(UnsignedPred)) { - if (UnsignedICmp->getOperand(0) != A) - UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred); - // A >=/<= B || (A - B) != 0 <--> true if ((UnsignedPred == ICmpInst::ICMP_UGE || UnsignedPred == ICmpInst::ICMP_ULE) && @@ -1461,9 +1458,6 @@ // Y < A || Y == 0 --> Y < A iff B != 0 if (match(UnsignedICmp, m_c_ICmp(UnsignedPred, m_Specific(Y), m_Specific(A)))) { - if (UnsignedICmp->getOperand(0) != Y) - UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred); - if (UnsignedPred == ICmpInst::ICMP_UGE && IsAnd && EqPred == ICmpInst::ICMP_NE && isKnownNonZero(B, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT)) Index: llvm/lib/Analysis/ValueTracking.cpp =================================================================== --- llvm/lib/Analysis/ValueTracking.cpp +++ llvm/lib/Analysis/ValueTracking.cpp @@ -592,10 +592,6 @@ CmpInst::Predicate Pred; if (!match(Cmp, m_c_ICmp(Pred, m_V, m_Value(RHS)))) return false; - // Canonicalize 'v' to be on the LHS of the comparison. - if (Cmp->getOperand(1) != RHS) - Pred = CmpInst::getSwappedPredicate(Pred); - // assume(v u> y) -> assume(v != 0) if (Pred == ICmpInst::ICMP_UGT) return true; Index: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -1072,9 +1072,6 @@ m_c_ICmp(UnsignedPred, m_Specific(ZeroCmpOp), m_Value(A))) && match(ZeroCmpOp, m_c_Add(m_Specific(A), m_Value(B))) && (ZeroICmp->hasOneUse() || UnsignedICmp->hasOneUse())) { - if (UnsignedICmp->getOperand(0) != ZeroCmpOp) - UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred); - auto GetKnownNonZeroAndOther = [&](Value *&NonZero, Value *&Other) { if (!IsKnownNonZero(NonZero)) std::swap(NonZero, Other); @@ -1111,8 +1108,6 @@ m_c_ICmp(UnsignedPred, m_Specific(Base), m_Specific(Offset))) || !ICmpInst::isUnsigned(UnsignedPred)) return nullptr; - if (UnsignedICmp->getOperand(0) != Base) - UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred); // Base >=/> Offset && (Base - Offset) != 0 <--> Base > Offset // (no overflow and not null) Index: llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -3300,30 +3300,19 @@ // x & (-1 >> y) != x -> x u> (-1 >> y) DstPred = ICmpInst::Predicate::ICMP_UGT; break; - case ICmpInst::Predicate::ICMP_UGT: + case ICmpInst::Predicate::ICMP_ULT: + // x & (-1 >> y) u< x -> x u> (-1 >> y) // x u> x & (-1 >> y) -> x u> (-1 >> y) - assert(X == I.getOperand(0) && "instsimplify took care of commut. variant"); DstPred = ICmpInst::Predicate::ICMP_UGT; break; case ICmpInst::Predicate::ICMP_UGE: // x & (-1 >> y) u>= x -> x u<= (-1 >> y) - assert(X == I.getOperand(1) && "instsimplify took care of commut. variant"); - DstPred = ICmpInst::Predicate::ICMP_ULE; - break; - case ICmpInst::Predicate::ICMP_ULT: - // x & (-1 >> y) u< x -> x u> (-1 >> y) - assert(X == I.getOperand(1) && "instsimplify took care of commut. variant"); - DstPred = ICmpInst::Predicate::ICMP_UGT; - break; - case ICmpInst::Predicate::ICMP_ULE: // x u<= x & (-1 >> y) -> x u<= (-1 >> y) - assert(X == I.getOperand(0) && "instsimplify took care of commut. variant"); DstPred = ICmpInst::Predicate::ICMP_ULE; break; - case ICmpInst::Predicate::ICMP_SGT: + case ICmpInst::Predicate::ICMP_SLT: + // x & (-1 >> y) s< x -> x s> (-1 >> y) // x s> x & (-1 >> y) -> x s> (-1 >> y) - if (X != I.getOperand(0)) // X must be on LHS of comparison! - return nullptr; // Ignore the other case. if (!match(M, m_Constant())) // Can not do this fold with non-constant. return nullptr; if (!match(M, m_NonNegative())) // Must not have any -1 vector elements. @@ -3332,33 +3321,19 @@ break; case ICmpInst::Predicate::ICMP_SGE: // x & (-1 >> y) s>= x -> x s<= (-1 >> y) - if (X != I.getOperand(1)) // X must be on RHS of comparison! - return nullptr; // Ignore the other case. + // x s<= x & (-1 >> y) -> x s<= (-1 >> y) if (!match(M, m_Constant())) // Can not do this fold with non-constant. return nullptr; if (!match(M, m_NonNegative())) // Must not have any -1 vector elements. return nullptr; DstPred = ICmpInst::Predicate::ICMP_SLE; break; - case ICmpInst::Predicate::ICMP_SLT: - // x & (-1 >> y) s< x -> x s> (-1 >> y) - if (X != I.getOperand(1)) // X must be on RHS of comparison! - return nullptr; // Ignore the other case. - if (!match(M, m_Constant())) // Can not do this fold with non-constant. - return nullptr; - if (!match(M, m_NonNegative())) // Must not have any -1 vector elements. - return nullptr; - DstPred = ICmpInst::Predicate::ICMP_SGT; - break; + case ICmpInst::Predicate::ICMP_SGT: case ICmpInst::Predicate::ICMP_SLE: - // x s<= x & (-1 >> y) -> x s<= (-1 >> y) - if (X != I.getOperand(0)) // X must be on LHS of comparison! - return nullptr; // Ignore the other case. - if (!match(M, m_Constant())) // Can not do this fold with non-constant. - return nullptr; - if (!match(M, m_NonNegative())) // Must not have any -1 vector elements. - return nullptr; - DstPred = ICmpInst::Predicate::ICMP_SLE; + return nullptr; + case ICmpInst::Predicate::ICMP_UGT: + case ICmpInst::Predicate::ICMP_ULE: + llvm_unreachable("Instsimplify took care of commut. variant"); break; default: llvm_unreachable("All possible folds are handled."); @@ -3627,9 +3602,6 @@ match(&I, m_c_ICmp(Pred, m_OneUse(m_UDiv(m_AllOnes(), m_Value(X))), m_Value(Y)))) { Mul = nullptr; - // Canonicalize as-if y was on RHS. - if (I.getOperand(1) != Y) - Pred = I.getSwappedPredicate(); // Are we checking that overflow does not happen, or does happen? switch (Pred) { @@ -5338,10 +5310,6 @@ Value *X, *Y; if (match(&Cmp, m_c_ICmp(Pred, m_OneUse(m_Shl(m_One(), m_Value(Y))), m_Value(X)))) { - // We want X to be the icmp's second operand, so swap predicate if it isn't. - if (Cmp.getOperand(0) == X) - Pred = Cmp.getSwappedPredicate(); - switch (Pred) { case ICmpInst::ICMP_ULE: NewPred = ICmpInst::ICMP_NE; @@ -5361,10 +5329,6 @@ // The variant with 'add' is not canonical, (the variant with 'not' is) // we only get it because it has extra uses, and can't be canonicalized, - // We want X to be the icmp's second operand, so swap predicate if it isn't. - if (Cmp.getOperand(0) == X) - Pred = Cmp.getSwappedPredicate(); - switch (Pred) { case ICmpInst::ICMP_ULT: NewPred = ICmpInst::ICMP_NE;