[llvm] [ValueTracking] Extend LHS/RHS with matching operand to work without constants. (PR #85557)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 26 03:27:29 PDT 2024


================
@@ -8845,20 +8845,20 @@ isImpliedCondMatchingOperands(CmpInst::Predicate LPred,
   return std::nullopt;
 }
 
-/// Return true if "icmp LPred X, LC" implies "icmp RPred X, RC" is true.
-/// Return false if "icmp LPred X, LC" implies "icmp RPred X, RC" is false.
+/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
+/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
 /// Otherwise, return std::nullopt if we can't infer anything.
-static std::optional<bool> isImpliedCondCommonOperandWithConstants(
-    CmpInst::Predicate LPred, const APInt &LC, CmpInst::Predicate RPred,
-    const APInt &RC) {
-  ConstantRange DomCR = ConstantRange::makeExactICmpRegion(LPred, LC);
-  ConstantRange CR = ConstantRange::makeExactICmpRegion(RPred, RC);
-  ConstantRange Intersection = DomCR.intersectWith(CR);
-  ConstantRange Difference = DomCR.difference(CR);
-  if (Intersection.isEmptySet())
-    return false;
-  if (Difference.isEmptySet())
+static std::optional<bool> isImpliedCondCommonOperandWithCR(
+    CmpInst::Predicate LPred, const ConstantRange &LCR,
+    CmpInst::Predicate RPred, const ConstantRange &RCR) {
+  ConstantRange DomCR = ConstantRange::makeAllowedICmpRegion(LPred, LCR);
+  // If all true values for lhs and true for rhs, lhs implies rhs
+  if (DomCR.icmp(RPred, RCR))
----------------
nikic wrote:

This code makes no sense to me. Why is this doing an icmp on the allowed icmp region?

Let's say you have `x ult [2, 5)` and `x ult [4, 5)`. Then the allowed range of the first icmp is `[0, 4)` and the satisfying range of the second is `[0, 4)`. The first is contained in the second, so icmp() return true. But obviously the first comparison does not imply the second.

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


More information about the llvm-commits mailing list