[llvm] [ValueTracking] Improve `isImpliedCondICmps` to handle binops (PR #69840)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 21 11:05:50 PDT 2023


================
@@ -8298,6 +8306,32 @@ static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
       return LPred == RPred;
   }
 
+  // handle R0 = L0 binop V
+  Value *R0Op1 = nullptr;
+  if (match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)) &&
+      match(R0, m_c_BinOp(m_Specific(L0), m_Value(R0Op1)))) {
+    ConstantRange LHSRange = ConstantRange::makeExactICmpRegion(LPred, *LC);
+    ConstantRange CR = ConstantRange::makeExactICmpRegion(RPred, *RC);
+    // TODO: use contextual information from SimplifyQuery
+    ConstantRange RHSRange = computeConstantRange(
+        R0Op1, ICmpInst::isSigned(RPred), /*UseInstrInfo*/ true, /*AC*/ nullptr,
+        /*CtxI*/ nullptr, /*DT*/ nullptr, Depth);
+    auto BO = cast<BinaryOperator>(R0);
+    if (BO->getOperand(0) != L0)
+      std::swap(LHSRange, RHSRange);
+    unsigned NoWrapKind = 0;
+    if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(BO)) {
+      if (OBO->hasNoUnsignedWrap())
+        NoWrapKind |= OverflowingBinaryOperator::NoUnsignedWrap;
+      if (OBO->hasNoSignedWrap())
+        NoWrapKind |= OverflowingBinaryOperator::NoSignedWrap;
+    }
+    ConstantRange Range =
+        LHSRange.overflowingBinaryOp(BO->getOpcode(), RHSRange, NoWrapKind);
----------------
dtcxzyw wrote:

No. We also handle `R0 = V binop L0`. I will update comments soon.

https://github.com/llvm/llvm-project/pull/69840


More information about the llvm-commits mailing list