Please use GitHub pull requests for new patches. Phabricator shutdown timeline
Changeset View
Changeset View
Standalone View
Standalone View
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Show First 20 Lines • Show All 2,956 Lines • ▼ Show 20 Lines | Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) { | ||||
// (X >= +/-0.0) ? X : -X --> fabs(X) | // (X >= +/-0.0) ? X : -X --> fabs(X) | ||||
if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) && | if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) && | ||||
match(FalseVal, m_FNeg(m_Specific(TrueVal))) && SI.hasNoSignedZeros() && | match(FalseVal, m_FNeg(m_Specific(TrueVal))) && SI.hasNoSignedZeros() && | ||||
(Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE || | (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE || | ||||
Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE)) { | Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE)) { | ||||
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI); | Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI); | ||||
return replaceInstUsesWith(SI, Fabs); | return replaceInstUsesWith(SI, Fabs); | ||||
} | } | ||||
// Negative Fabs: | |||||
// 1. (X < +/-0.0) ? X : (0.0 - X) --> (0.0 - fabs(X)) | |||||
// 2. nsz on the sub (canonicalized to FNeg) or on the select: | |||||
// (X < +/-0.0) ? X : -X --> -X | |||||
spatel: "--> -X" should be "--> -fabs(X)" | |||||
if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) && | |||||
(Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_ULT)) { | |||||
mnadeemAuthorUnsubmitted <= needs nsz on the select, so I'll just add that condition here and the rest should work fine. mnadeem: `<=` needs nsz on the select, so I'll just add that condition here and the rest should work… | |||||
if (match(FalseVal, m_FSub(m_PosZeroFP(), m_Specific(TrueVal)))) { | |||||
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI); | |||||
return BinaryOperator::CreateFSubFMF( | |||||
ConstantFP::get(Fabs->getType(), 0.0), Fabs, | |||||
cast<Instruction>(FalseVal)); | |||||
} else if (match(FalseVal, m_FNeg(m_Specific(TrueVal))) && | |||||
(cast<Instruction>(FalseVal)->hasNoSignedZeros() || | |||||
SI.hasNoSignedZeros())) { | |||||
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI); | |||||
return UnaryOperator::CreateFNegFMF(Fabs, cast<Instruction>(FalseVal)); | |||||
} | |||||
} | |||||
// See if we are selecting two values based on a comparison of the two values. | // See if we are selecting two values based on a comparison of the two values. | ||||
if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) | if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) | ||||
if (Instruction *Result = foldSelectInstWithICmp(SI, ICI)) | if (Instruction *Result = foldSelectInstWithICmp(SI, ICI)) | ||||
return Result; | return Result; | ||||
if (Instruction *Add = foldAddSubSelect(SI, Builder)) | if (Instruction *Add = foldAddSubSelect(SI, Builder)) | ||||
return Add; | return Add; | ||||
▲ Show 20 Lines • Show All 338 Lines • Show Last 20 Lines |
"--> -X" should be "--> -fabs(X)"