@@ -8471,26 +8471,12 @@ isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS,
84718471 }
84728472}
84738473
8474- // / Return true if the operands of two compares (expanded as "L0 pred L1" and
8475- // / "R0 pred R1") match. IsSwappedOps is true when the operands match, but are
8476- // / swapped.
8477- static bool areMatchingOperands (const Value *L0, const Value *L1, const Value *R0,
8478- const Value *R1, bool &AreSwappedOps) {
8479- bool AreMatchingOps = (L0 == R0 && L1 == R1);
8480- AreSwappedOps = (L0 == R1 && L1 == R0);
8481- return AreMatchingOps || AreSwappedOps;
8482- }
8483-
84848474// / Return true if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is true.
84858475// / Return false if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is false.
84868476// / Otherwise, return std::nullopt if we can't infer anything.
84878477static std::optional<bool >
84888478isImpliedCondMatchingOperands (CmpInst::Predicate LPred,
8489- CmpInst::Predicate RPred, bool AreSwappedOps) {
8490- // Canonicalize the predicate as if the operands were not commuted.
8491- if (AreSwappedOps)
8492- RPred = ICmpInst::getSwappedPredicate (RPred);
8493-
8479+ CmpInst::Predicate RPred) {
84948480 if (CmpInst::isImpliedTrueByMatchingCmp (LPred, RPred))
84958481 return true ;
84968482 if (CmpInst::isImpliedFalseByMatchingCmp (LPred, RPred))
@@ -8532,39 +8518,42 @@ static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
85328518 CmpInst::Predicate LPred =
85338519 LHSIsTrue ? LHS->getPredicate () : LHS->getInversePredicate ();
85348520
8521+ // We can have non-canonical operands, so try to normalize any common operand
8522+ // to L0/R0.
8523+ if (L0 == R1) {
8524+ std::swap (R0, R1);
8525+ RPred = ICmpInst::getSwappedPredicate (RPred);
8526+ }
8527+ if (R0 == L1) {
8528+ std::swap (L0, L1);
8529+ LPred = ICmpInst::getSwappedPredicate (LPred);
8530+ }
8531+ if (L1 == R1) {
8532+ // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
8533+ if (L0 != R0 || match (L0, m_ImmConstant ())) {
8534+ std::swap (L0, L1);
8535+ LPred = ICmpInst::getSwappedPredicate (LPred);
8536+ std::swap (R0, R1);
8537+ RPred = ICmpInst::getSwappedPredicate (RPred);
8538+ }
8539+ }
8540+
85358541 // Can we infer anything when the 0-operands match and the 1-operands are
85368542 // constants (not necessarily matching)?
85378543 const APInt *LC, *RC;
85388544 if (L0 == R0 && match (L1, m_APInt (LC)) && match (R1, m_APInt (RC)))
85398545 return isImpliedCondCommonOperandWithConstants (LPred, *LC, RPred, *RC);
85408546
85418547 // Can we infer anything when the two compares have matching operands?
8542- bool AreSwappedOps;
8543- if (areMatchingOperands (L0, L1, R0, R1, AreSwappedOps))
8544- return isImpliedCondMatchingOperands (LPred, RPred, AreSwappedOps);
8548+ if (L0 == R0 && L1 == R1)
8549+ return isImpliedCondMatchingOperands (LPred, RPred);
85458550
85468551 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
8547- if (ICmpInst::isUnsigned (LPred) && ICmpInst::isUnsigned (RPred)) {
8548- if (L0 == R1) {
8549- std::swap (R0, R1);
8550- RPred = ICmpInst::getSwappedPredicate (RPred);
8551- }
8552- if (L1 == R0) {
8553- std::swap (L0, L1);
8554- LPred = ICmpInst::getSwappedPredicate (LPred);
8555- }
8556- if (L1 == R1) {
8557- std::swap (L0, L1);
8558- LPred = ICmpInst::getSwappedPredicate (LPred);
8559- std::swap (R0, R1);
8560- RPred = ICmpInst::getSwappedPredicate (RPred);
8561- }
8562- if (L0 == R0 &&
8563- (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
8564- (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
8565- match (L0, m_c_Add (m_Specific (L1), m_Specific (R1))))
8566- return LPred == RPred;
8567- }
8552+ if (L0 == R0 &&
8553+ (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
8554+ (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
8555+ match (L0, m_c_Add (m_Specific (L1), m_Specific (R1))))
8556+ return LPred == RPred;
85688557
85698558 if (LPred == RPred)
85708559 return isImpliedCondOperands (LPred, L0, L1, R0, R1, DL, Depth);
0 commit comments