diff options
Diffstat (limited to 'llvm/lib/Analysis/InstructionSimplify.cpp')
-rw-r--r-- | llvm/lib/Analysis/InstructionSimplify.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index db929aa7059..fd6f4ba476e 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -3874,6 +3874,34 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal, return nullptr; } +/// Try to simplify a select instruction when its condition operand is a +/// floating-point comparison. +static Value *simplifySelectWithFCmp(Value *Cond, Value *T, Value *F) { + 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)))) + return nullptr; + + // TODO: The transform may not be valid with -0.0. An incomplete way of + // testing for that possibility is to check if at least one operand is a + // non-zero constant. + const APFloat *C; + if ((match(T, m_APFloat(C)) && C->isNonZero()) || + (match(F, m_APFloat(C)) && C->isNonZero())) { + // (T == F) ? T : F --> F + // (F == T) ? T : F --> F + if (Pred == FCmpInst::FCMP_OEQ) + return F; + + // (T != F) ? T : F --> T + // (F != T) ? T : F --> T + if (Pred == FCmpInst::FCMP_UNE) + return T; + } + + return nullptr; +} + /// Given operands for a SelectInst, see if we can fold the result. /// If not, this returns null. static Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, @@ -3910,6 +3938,9 @@ static Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse)) return V; + if (Value *V = simplifySelectWithFCmp(Cond, TrueVal, FalseVal)) + return V; + if (Value *V = foldSelectWithBinaryOp(Cond, TrueVal, FalseVal)) return V; |