@@ -8471,26 +8471,12 @@ isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS,
8471
8471
}
8472
8472
}
8473
8473
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
-
8484
8474
// / Return true if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is true.
8485
8475
// / Return false if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is false.
8486
8476
// / Otherwise, return std::nullopt if we can't infer anything.
8487
8477
static std::optional<bool >
8488
8478
isImpliedCondMatchingOperands (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) {
8494
8480
if (CmpInst::isImpliedTrueByMatchingCmp (LPred, RPred))
8495
8481
return true ;
8496
8482
if (CmpInst::isImpliedFalseByMatchingCmp (LPred, RPred))
@@ -8532,39 +8518,42 @@ static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
8532
8518
CmpInst::Predicate LPred =
8533
8519
LHSIsTrue ? LHS->getPredicate () : LHS->getInversePredicate ();
8534
8520
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
+
8535
8541
// Can we infer anything when the 0-operands match and the 1-operands are
8536
8542
// constants (not necessarily matching)?
8537
8543
const APInt *LC, *RC;
8538
8544
if (L0 == R0 && match (L1, m_APInt (LC)) && match (R1, m_APInt (RC)))
8539
8545
return isImpliedCondCommonOperandWithConstants (LPred, *LC, RPred, *RC);
8540
8546
8541
8547
// 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);
8545
8550
8546
8551
// 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;
8568
8557
8569
8558
if (LPred == RPred)
8570
8559
return isImpliedCondOperands (LPred, L0, L1, R0, R1, DL, Depth);
0 commit comments