[llvm] [InstCombine] Generalise optimisation of redundant floating point comparisons with `ConstantFPRange` (PR #159315)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 19 08:31:15 PDT 2025
================
@@ -1812,6 +1813,61 @@ static Value *simplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1,
return nullptr;
}
+/// Test if a pair of compares with a shared operand and 2 constants has an
+/// empty set intersection, full set union, or if one compare is a superset of
+/// the other.
+static Value *simplifyAndOrOfFCmpsWithConstants(FCmpInst *Cmp0, FCmpInst *Cmp1,
+ bool IsAnd) {
+ // Look for this pattern: {and/or} (fcmp X, C0), (fcmp X, C1)).
+ if (Cmp0->getOperand(0) != Cmp1->getOperand(0))
+ return nullptr;
+
+ const APFloat *C0, *C1;
+ if (!match(Cmp0->getOperand(1), m_APFloat(C0)) ||
+ !match(Cmp1->getOperand(1), m_APFloat(C1)))
+ return nullptr;
+
+ auto Range0 = ConstantFPRange::makeExactFCmpRegion(Cmp0->getPredicate(), *C0);
+ auto Range1 = ConstantFPRange::makeExactFCmpRegion(Cmp1->getPredicate(), *C1);
+
+ if (!Range0 || !Range1)
+ return nullptr;
+
+ // For and-of-compares, check if the intersection is empty:
+ // (fcmp X, C0) && (fcmp X, C1) --> empty set --> false
+ if (IsAnd && (*Range0).intersectWith(*Range1).isEmptySet())
+ return getFalse(Cmp0->getType());
+
+ // For or-of-compares, check if the union is full:
+ // (fcmp X, C0) || (fcmp X, C1) --> full set --> true
+ //
+ // TODO: `unionWith` is not precise at the moment, so
+ // we can invert the predicate and check:
+ // inv(fcmp X, C0) && inv(fcmp X, C1) --> empty set --> false
+ if (!IsAnd) {
+ auto Range0Inv = ConstantFPRange::makeExactFCmpRegion(
+ FCmpInst::getInversePredicate(Cmp0->getPredicate()), *C0);
+ auto Range1Inv = ConstantFPRange::makeExactFCmpRegion(
+ FCmpInst::getInversePredicate(Cmp1->getPredicate()), *C1);
+ if (Range0Inv && Range1Inv) {
+ if ((*Range0Inv).intersectWith(*Range1Inv).isEmptySet())
+ return getFalse(Cmp0->getType());
+ }
+ }
----------------
dtcxzyw wrote:
```suggestion
```
Handled by the logic above.
https://github.com/llvm/llvm-project/pull/159315
More information about the llvm-commits
mailing list