[llvm] ValueTracking: Merge fcmpImpliesClass and fcmpToClassTest (PR #66522)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 5 19:47:25 PST 2023
https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/66522
>From 076ab2374d84c4112e0bf3fb11ecda2f5774785e Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Mon, 11 Sep 2023 10:56:40 +0300
Subject: [PATCH] ValueTracking: Merge fcmpImpliesClass and fcmpToClassTest
---
llvm/include/llvm/Analysis/ValueTracking.h | 13 +-
llvm/lib/Analysis/ValueTracking.cpp | 250 ++++++++++++---------
2 files changed, 147 insertions(+), 116 deletions(-)
diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h
index c25dcad5e2242a..ce2571cccf77cf 100644
--- a/llvm/include/llvm/Analysis/ValueTracking.h
+++ b/llvm/include/llvm/Analysis/ValueTracking.h
@@ -214,8 +214,10 @@ std::pair<Value *, FPClassTest> fcmpToClassTest(CmpInst::Predicate Pred,
const APFloat *ConstRHS,
bool LookThroughSrc = true);
-/// Compute the possible floating-point classes that \p LHS could be based on an
-/// fcmp returning true. Returns { TestedValue, ClassesIfTrue, ClassesIfFalse }
+/// Compute the possible floating-point classes that \p LHS could be based on
+/// fcmp \Pred \p LHS, \p RHS.
+///
+/// Returns { TestedValue, ClassesIfTrue, ClassesIfFalse }
///
/// If the compare returns an exact class test, ClassesIfTrue == ~ClassesIfFalse
///
@@ -230,10 +232,13 @@ std::pair<Value *, FPClassTest> fcmpToClassTest(CmpInst::Predicate Pred,
///
std::tuple<Value *, FPClassTest, FPClassTest>
fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
- const APFloat *ConstRHS, bool LookThroughSrc = true);
+ Value *RHS, bool LookThroughSrc = true);
std::tuple<Value *, FPClassTest, FPClassTest>
fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
- Value *RHS, bool LookThroughSrc = true);
+ FPClassTest RHS, bool LookThroughSrc = true);
+std::tuple<Value *, FPClassTest, FPClassTest>
+fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
+ const APFloat &RHS, bool LookThroughSrc = true);
struct KnownFPClass {
/// Floating-point classes the value could be one of.
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 1f09d912f7339f..d1c453807cea5e 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -3948,67 +3948,104 @@ std::pair<Value *, FPClassTest> llvm::fcmpToClassTest(FCmpInst::Predicate Pred,
std::pair<Value *, FPClassTest>
llvm::fcmpToClassTest(FCmpInst::Predicate Pred, const Function &F, Value *LHS,
const APFloat *ConstRHS, bool LookThroughSrc) {
+
+ auto [Src, ClassIfTrue, ClassIfFalse] =
+ fcmpImpliesClass(Pred, F, LHS, *ConstRHS, LookThroughSrc);
+ if (Src && ClassIfTrue == ~ClassIfFalse)
+ return {Src, ClassIfTrue};
+ return {nullptr, fcAllFlags};
+}
+
+/// Return the return value for fcmpImpliesClass for a compare that produces an
+/// exact class test.
+static std::tuple<Value *, FPClassTest, FPClassTest> exactClass(Value *V,
+ FPClassTest M) {
+ return {V, M, ~M};
+}
+
+std::tuple<Value *, FPClassTest, FPClassTest>
+llvm::fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
+ FPClassTest RHSClass, bool LookThroughSrc) {
+ assert(RHSClass != fcNone);
+
+ const FPClassTest OrigClass = RHSClass;
+
+ Value *Src = LHS;
+ const bool IsNegativeRHS = (RHSClass & fcNegative) == RHSClass;
+ const bool IsPositiveRHS = (RHSClass & fcPositive) == RHSClass;
+ const bool IsNaN = (RHSClass & ~fcNan) == fcNone;
+
+ if (IsNaN) {
+ // fcmp o__ x, nan -> false
+ // fcmp u__ x, nan -> true
+ return exactClass(Src, CmpInst::isOrdered(Pred) ? fcNone : fcAllFlags);
+ }
+
// fcmp ord x, zero|normal|subnormal|inf -> ~fcNan
- if (Pred == FCmpInst::FCMP_ORD && !ConstRHS->isNaN())
- return {LHS, ~fcNan};
+ if (Pred == FCmpInst::FCMP_ORD)
+ return {Src, ~fcNan, fcNan};
// fcmp uno x, zero|normal|subnormal|inf -> fcNan
- if (Pred == FCmpInst::FCMP_UNO && !ConstRHS->isNaN())
- return {LHS, fcNan};
+ if (Pred == FCmpInst::FCMP_UNO)
+ return {Src, fcNan, ~fcNan};
+
+ const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
+ if (IsFabs)
+ RHSClass = llvm::inverse_fabs(RHSClass);
- if (ConstRHS->isZero()) {
+ const bool IsZero = (OrigClass & fcZero) == OrigClass;
+ if (IsZero) {
// Compares with fcNone are only exactly equal to fcZero if input denormals
// are not flushed.
// TODO: Handle DAZ by expanding masks to cover subnormal cases.
if (Pred != FCmpInst::FCMP_ORD && Pred != FCmpInst::FCMP_UNO &&
!inputDenormalIsIEEE(F, LHS->getType()))
- return {nullptr, fcAllFlags};
+ return {nullptr, fcAllFlags, fcAllFlags};
switch (Pred) {
case FCmpInst::FCMP_OEQ: // Match x == 0.0
- return {LHS, fcZero};
+ return exactClass(Src, fcZero);
case FCmpInst::FCMP_UEQ: // Match isnan(x) || (x == 0.0)
- return {LHS, fcZero | fcNan};
+ return exactClass(Src, fcZero | fcNan);
case FCmpInst::FCMP_UNE: // Match (x != 0.0)
- return {LHS, ~fcZero};
+ return exactClass(Src, ~fcZero);
case FCmpInst::FCMP_ONE: // Match !isnan(x) && x != 0.0
- return {LHS, ~fcNan & ~fcZero};
+ return exactClass(Src, ~fcNan & ~fcZero);
case FCmpInst::FCMP_ORD:
// Canonical form of ord/uno is with a zero. We could also handle
// non-canonical other non-NaN constants or LHS == RHS.
- return {LHS, ~fcNan};
+ return exactClass(Src, ~fcNan);
case FCmpInst::FCMP_UNO:
- return {LHS, fcNan};
+ return exactClass(Src, fcNan);
case FCmpInst::FCMP_OGT: // x > 0
- return {LHS, fcPosSubnormal | fcPosNormal | fcPosInf};
+ return exactClass(Src, fcPosSubnormal | fcPosNormal | fcPosInf);
case FCmpInst::FCMP_UGT: // isnan(x) || x > 0
- return {LHS, fcPosSubnormal | fcPosNormal | fcPosInf | fcNan};
+ return exactClass(Src, fcPosSubnormal | fcPosNormal | fcPosInf | fcNan);
case FCmpInst::FCMP_OGE: // x >= 0
- return {LHS, fcPositive | fcNegZero};
+ return exactClass(Src, fcPositive | fcNegZero);
case FCmpInst::FCMP_UGE: // isnan(x) || x >= 0
- return {LHS, fcPositive | fcNegZero | fcNan};
+ return exactClass(Src, fcPositive | fcNegZero | fcNan);
case FCmpInst::FCMP_OLT: // x < 0
- return {LHS, fcNegSubnormal | fcNegNormal | fcNegInf};
+ return exactClass(Src, fcNegSubnormal | fcNegNormal | fcNegInf);
case FCmpInst::FCMP_ULT: // isnan(x) || x < 0
- return {LHS, fcNegSubnormal | fcNegNormal | fcNegInf | fcNan};
+ return exactClass(Src, fcNegSubnormal | fcNegNormal | fcNegInf | fcNan);
case FCmpInst::FCMP_OLE: // x <= 0
- return {LHS, fcNegative | fcPosZero};
+ return exactClass(Src, fcNegative | fcPosZero);
case FCmpInst::FCMP_ULE: // isnan(x) || x <= 0
- return {LHS, fcNegative | fcPosZero | fcNan};
+ return exactClass(Src, fcNegative | fcPosZero | fcNan);
default:
break;
}
- return {nullptr, fcAllFlags};
+ return {nullptr, fcAllFlags, fcAllFlags};
}
- Value *Src = LHS;
- const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
+ const bool IsDenormalRHS = (OrigClass & fcSubnormal) == OrigClass;
- // Compute the test mask that would return true for the ordered comparisons.
- FPClassTest Mask;
+ const bool IsInf = (OrigClass & fcInf) == OrigClass;
+ if (IsInf) {
+ FPClassTest Mask = fcAllFlags;
- if (ConstRHS->isInfinity()) {
switch (Pred) {
case FCmpInst::FCMP_OEQ:
case FCmpInst::FCMP_UNE: {
@@ -4023,8 +4060,7 @@ llvm::fcmpToClassTest(FCmpInst::Predicate Pred, const Function &F, Value *LHS,
// fcmp une fabs(x), +inf -> is_fpclass x, ~fcInf
// fcmp une x, -inf -> is_fpclass x, ~fcNegInf
// fcmp une fabs(x), -inf -> is_fpclass x, fcAllFlags -> true
-
- if (ConstRHS->isNegative()) {
+ if (IsNegativeRHS) {
Mask = fcNegInf;
if (IsFabs)
Mask = fcNone;
@@ -4033,7 +4069,6 @@ llvm::fcmpToClassTest(FCmpInst::Predicate Pred, const Function &F, Value *LHS,
if (IsFabs)
Mask |= fcNegInf;
}
-
break;
}
case FCmpInst::FCMP_ONE:
@@ -4048,7 +4083,7 @@ llvm::fcmpToClassTest(FCmpInst::Predicate Pred, const Function &F, Value *LHS,
// fcmp ueq (fabs x), +inf -> is_fpclass x, fcInf|fcNan
// fcmp ueq x, -inf -> is_fpclass x, fcNegInf|fcNan
// fcmp ueq fabs(x), -inf -> is_fpclass x, fcNan
- if (ConstRHS->isNegative()) {
+ if (IsNegativeRHS) {
Mask = ~fcNegInf & ~fcNan;
if (IsFabs)
Mask = ~fcNan;
@@ -4062,7 +4097,7 @@ llvm::fcmpToClassTest(FCmpInst::Predicate Pred, const Function &F, Value *LHS,
}
case FCmpInst::FCMP_OLT:
case FCmpInst::FCMP_UGE: {
- if (ConstRHS->isNegative()) {
+ if (IsNegativeRHS) {
// No value is ordered and less than negative infinity.
// All values are unordered with or at least negative infinity.
// fcmp olt x, -inf -> false
@@ -4082,7 +4117,7 @@ llvm::fcmpToClassTest(FCmpInst::Predicate Pred, const Function &F, Value *LHS,
}
case FCmpInst::FCMP_OGE:
case FCmpInst::FCMP_ULT: {
- if (ConstRHS->isNegative()) {
+ if (IsNegativeRHS) {
// fcmp oge x, -inf -> ~fcNan
// fcmp oge fabs(x), -inf -> ~fcNan
// fcmp ult x, -inf -> fcNan
@@ -4102,7 +4137,7 @@ llvm::fcmpToClassTest(FCmpInst::Predicate Pred, const Function &F, Value *LHS,
}
case FCmpInst::FCMP_OGT:
case FCmpInst::FCMP_ULE: {
- if (ConstRHS->isNegative()) {
+ if (IsNegativeRHS) {
// fcmp ogt x, -inf -> fcmp one x, -inf
// fcmp ogt fabs(x), -inf -> fcmp ord x, x
// fcmp ule x, -inf -> fcmp ueq x, -inf
@@ -4116,83 +4151,15 @@ llvm::fcmpToClassTest(FCmpInst::Predicate Pred, const Function &F, Value *LHS,
break;
}
default:
- return {nullptr, fcAllFlags};
- }
- } else if (ConstRHS->isSmallestNormalized() && !ConstRHS->isNegative()) {
- // Match pattern that's used in __builtin_isnormal.
- switch (Pred) {
- case FCmpInst::FCMP_OLT:
- case FCmpInst::FCMP_UGE: {
- // fcmp olt x, smallest_normal -> fcNegInf|fcNegNormal|fcSubnormal|fcZero
- // fcmp olt fabs(x), smallest_normal -> fcSubnormal|fcZero
- // fcmp uge x, smallest_normal -> fcNan|fcPosNormal|fcPosInf
- // fcmp uge fabs(x), smallest_normal -> ~(fcSubnormal|fcZero)
- Mask = fcZero | fcSubnormal;
- if (!IsFabs)
- Mask |= fcNegNormal | fcNegInf;
-
- break;
- }
- case FCmpInst::FCMP_OGE:
- case FCmpInst::FCMP_ULT: {
- // fcmp oge x, smallest_normal -> fcPosNormal | fcPosInf
- // fcmp oge fabs(x), smallest_normal -> fcInf | fcNormal
- // fcmp ult x, smallest_normal -> ~(fcPosNormal | fcPosInf)
- // fcmp ult fabs(x), smallest_normal -> ~(fcInf | fcNormal)
- Mask = fcPosInf | fcPosNormal;
- if (IsFabs)
- Mask |= fcNegInf | fcNegNormal;
- break;
- }
- default:
- return {nullptr, fcAllFlags};
+ return {nullptr, fcAllFlags, fcAllFlags};
}
- } else if (ConstRHS->isNaN()) {
- // fcmp o__ x, nan -> false
- // fcmp u__ x, nan -> true
- Mask = fcNone;
- } else
- return {nullptr, fcAllFlags};
- // Invert the comparison for the unordered cases.
- if (FCmpInst::isUnordered(Pred))
- Mask = ~Mask;
+ // Invert the comparison for the unordered cases.
+ if (FCmpInst::isUnordered(Pred))
+ Mask = ~Mask;
- return {Src, Mask};
-}
-
-std::tuple<Value *, FPClassTest, FPClassTest>
-llvm::fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
- const APFloat *ConstRHS, bool LookThroughSrc) {
- auto [Val, ClassMask] =
- fcmpToClassTest(Pred, F, LHS, ConstRHS, LookThroughSrc);
- if (Val)
- return {Val, ClassMask, ~ClassMask};
-
- FPClassTest RHSClass = ConstRHS->classify();
-
- // If we see a zero here, we are using dynamic denormal-fp-math, and can't
- // treat comparisons to 0 as an exact class test.
- //
- // TODO: We could do better and still recognize non-equality cases.
- if (RHSClass == fcPosZero || RHSClass == fcNegZero)
- return {nullptr, fcAllFlags, fcAllFlags};
-
- assert((RHSClass == fcPosNormal || RHSClass == fcNegNormal ||
- RHSClass == fcPosSubnormal || RHSClass == fcNegSubnormal) &&
- "should have been recognized as an exact class test");
-
- const bool IsNegativeRHS = (RHSClass & fcNegative) == RHSClass;
- const bool IsPositiveRHS = (RHSClass & fcPositive) == RHSClass;
-
- assert(IsNegativeRHS == ConstRHS->isNegative());
- assert(IsPositiveRHS == !ConstRHS->isNegative());
-
- Value *Src = LHS;
- const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
-
- if (IsFabs)
- RHSClass = llvm::inverse_fabs(RHSClass);
+ return exactClass(Src, Mask);
+ }
if (Pred == FCmpInst::FCMP_OEQ)
return {Src, RHSClass, fcAllFlags};
@@ -4208,6 +4175,12 @@ llvm::fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
if (Pred == FCmpInst::FCMP_UNE)
return {Src, fcAllFlags, RHSClass};
+ assert((RHSClass == fcNone || RHSClass == fcPosNormal ||
+ RHSClass == fcNegNormal || RHSClass == fcNormal ||
+ RHSClass == fcPosSubnormal || RHSClass == fcNegSubnormal ||
+ RHSClass == fcSubnormal) &&
+ "should have been recognized as an exact class test");
+
if (IsNegativeRHS) {
// TODO: Handle fneg(fabs)
if (IsFabs) {
@@ -4238,7 +4211,7 @@ llvm::fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
FPClassTest ClassesLE = fcNegInf | fcNegNormal;
FPClassTest ClassesGE = fcPositive | fcNegZero | fcNegSubnormal;
- if (ConstRHS->isDenormal())
+ if (IsDenormalRHS)
ClassesLE |= fcNegSubnormal;
else
ClassesGE |= fcNegNormal;
@@ -4262,7 +4235,7 @@ llvm::fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
} else if (IsPositiveRHS) {
FPClassTest ClassesGE = fcPosNormal | fcPosInf;
FPClassTest ClassesLE = fcNegative | fcPosZero | fcPosNormal;
- if (ConstRHS->isDenormal())
+ if (IsDenormalRHS)
ClassesGE |= fcPosNormal;
else
ClassesLE |= fcPosSubnormal;
@@ -4293,13 +4266,65 @@ llvm::fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
return {nullptr, fcAllFlags, fcAllFlags};
}
+std::tuple<Value *, FPClassTest, FPClassTest>
+llvm::fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
+ const APFloat &ConstRHS, bool LookThroughSrc) {
+ // We can refine checks against smallest normal / largest denormal to an
+ // exact class test.
+ if (!ConstRHS.isNegative() && ConstRHS.isSmallestNormalized()) {
+ Value *Src = LHS;
+ const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
+
+ FPClassTest Mask;
+ // Match pattern that's used in __builtin_isnormal.
+ switch (Pred) {
+ case FCmpInst::FCMP_OLT:
+ case FCmpInst::FCMP_UGE: {
+ // fcmp olt x, smallest_normal -> fcNegInf|fcNegNormal|fcSubnormal|fcZero
+ // fcmp olt fabs(x), smallest_normal -> fcSubnormal|fcZero
+ // fcmp uge x, smallest_normal -> fcNan|fcPosNormal|fcPosInf
+ // fcmp uge fabs(x), smallest_normal -> ~(fcSubnormal|fcZero)
+ Mask = fcZero | fcSubnormal;
+ if (!IsFabs)
+ Mask |= fcNegNormal | fcNegInf;
+
+ break;
+ }
+ case FCmpInst::FCMP_OGE:
+ case FCmpInst::FCMP_ULT: {
+ // fcmp oge x, smallest_normal -> fcPosNormal | fcPosInf
+ // fcmp oge fabs(x), smallest_normal -> fcInf | fcNormal
+ // fcmp ult x, smallest_normal -> ~(fcPosNormal | fcPosInf)
+ // fcmp ult fabs(x), smallest_normal -> ~(fcInf | fcNormal)
+ Mask = fcPosInf | fcPosNormal;
+ if (IsFabs)
+ Mask |= fcNegInf | fcNegNormal;
+ break;
+ }
+ default:
+ return fcmpImpliesClass(Pred, F, LHS, ConstRHS.classify(),
+ LookThroughSrc);
+ }
+
+ // Invert the comparison for the unordered cases.
+ if (FCmpInst::isUnordered(Pred))
+ Mask = ~Mask;
+
+ return exactClass(Src, Mask);
+ }
+
+ return fcmpImpliesClass(Pred, F, LHS, ConstRHS.classify(), LookThroughSrc);
+}
+
std::tuple<Value *, FPClassTest, FPClassTest>
llvm::fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS,
Value *RHS, bool LookThroughSrc) {
const APFloat *ConstRHS;
if (!match(RHS, m_APFloatAllowUndef(ConstRHS)))
return {nullptr, fcAllFlags, fcNone};
- return fcmpImpliesClass(Pred, F, LHS, ConstRHS, LookThroughSrc);
+
+ // TODO: Just call computeKnownFPClass for RHS to handle non-constants.
+ return fcmpImpliesClass(Pred, F, LHS, *ConstRHS, LookThroughSrc);
}
static FPClassTest computeKnownFPClassFromAssumes(const Value *V,
@@ -4330,14 +4355,15 @@ static FPClassTest computeKnownFPClassFromAssumes(const Value *V,
if (match(RHS, m_APFloat(CRHS))) {
// First see if we can fold in fabs/fneg into the test.
auto [CmpVal, MaskIfTrue, MaskIfFalse] =
- fcmpImpliesClass(Pred, *F, LHS, CRHS, true);
+ fcmpImpliesClass(Pred, *F, LHS, *CRHS, true);
if (CmpVal == V)
KnownFromAssume &= MaskIfTrue;
+
else {
// Try again without the lookthrough if we found a different source
// value.
auto [CmpVal, MaskIfTrue, MaskIfFalse] =
- fcmpImpliesClass(Pred, *F, LHS, CRHS, false);
+ fcmpImpliesClass(Pred, *F, LHS, *CRHS, false);
if (CmpVal == V)
KnownFromAssume &= MaskIfTrue;
}
More information about the llvm-commits
mailing list