[llvm] InstSimplify: support floating-point equivalences (PR #115152)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 11 06:10:17 PST 2024
================
@@ -4761,27 +4763,51 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
/// Try to simplify a select instruction when its condition operand is a
/// floating-point comparison.
static Value *simplifySelectWithFCmp(Value *Cond, Value *T, Value *F,
- const SimplifyQuery &Q) {
+ const SimplifyQuery &Q,
+ unsigned MaxRecurse) {
FCmpInst::Predicate Pred;
- if (!match(Cond, m_FCmp(Pred, m_Specific(T), m_Specific(F))) &&
- !match(Cond, m_FCmp(Pred, m_Specific(F), m_Specific(T))))
+ Value *CmpLHS, *CmpRHS;
+ if (!match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
+ return nullptr;
+ FCmpInst *I = cast<FCmpInst>(Cond);
+
+ bool IsEquiv = I->isEquivalence(),
+ IsInverseEquiv = I->isEquivalence(/*Invert=*/true);
+
+ if (IsInverseEquiv)
+ std::swap(T, F);
+
+ // Canonicalize CmpLHS to be T, and CmpRHS to be F, if they're swapped.
+ if (CmpLHS == F && CmpRHS == T)
+ std::swap(CmpLHS, CmpRHS);
+
+ // This transforms is safe if at least one operand is known to not be zero.
+ // Otherwise, the select can change the sign of a zero operand.
+ if (IsEquiv || IsInverseEquiv) {
+ if (Value *V =
+ simplifySelectWithEquivalence(CmpLHS, CmpRHS, T, F, Q, MaxRecurse))
+ return V;
+ if (Value *V =
+ simplifySelectWithEquivalence(CmpRHS, CmpLHS, T, F, Q, MaxRecurse))
+ return V;
+
+ // (T == F) ? T : F --> F
+ // (T != F) ? F : T --> F
+ return CmpLHS == T && CmpRHS == F ? F : nullptr;
+ }
+
+ if (CmpLHS != T || CmpRHS != F)
return nullptr;
- // This transform is safe if we do not have (do not care about) -0.0 or if
- // at least one operand is known to not be -0.0. Otherwise, the select can
- // change the sign of a zero operand.
+ // This transform is also safe if we do not have (do not care about) -0.0.
bool HasNoSignedZeros =
Q.CxtI && isa<FPMathOperator>(Q.CxtI) && Q.CxtI->hasNoSignedZeros();
- const APFloat *C;
- if (HasNoSignedZeros || (match(T, m_APFloat(C)) && C->isNonZero()) ||
- (match(F, m_APFloat(C)) && C->isNonZero())) {
+ if (HasNoSignedZeros) {
----------------
nikic wrote:
Why isn't this case already covered by the equivalence handling?
https://github.com/llvm/llvm-project/pull/115152
More information about the llvm-commits
mailing list