From 22c583c3d03a6750d6474ad46e5d52eb9974e2b0 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Mon, 14 Sep 2020 10:32:11 -0400 Subject: [PATCH] [InstSimplify] reduce code duplication for fmin/fmax folds; NFC We use the same code structure for folding integer min/max. --- llvm/lib/Analysis/InstructionSimplify.cpp | 39 +++++++++++++++---------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 271e79df715..9933360a3a1 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -5447,19 +5447,32 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1, // If the arguments are the same, this is a no-op. if (Op0 == Op1) return Op0; - // If one argument is undef, return the other argument. - if (Q.isUndefValue(Op0)) - return Op1; + // Canonicalize constant operand as Op1. + if (isa(Op0)) + std::swap(Op0, Op1); + + // If an argument is undef, return the other argument. if (Q.isUndefValue(Op1)) return Op0; - // If one argument is NaN, return other or NaN appropriately. + // If an argument is NaN, return other or NaN appropriately. bool PropagateNaN = IID == Intrinsic::minimum || IID == Intrinsic::maximum; - if (match(Op0, m_NaN())) - return PropagateNaN ? Op0 : Op1; if (match(Op1, m_NaN())) return PropagateNaN ? Op1 : Op0; + // min(X, -Inf) --> -Inf + // max(X, +Inf) --> +Inf + bool UseNegInf = IID == Intrinsic::minnum || IID == Intrinsic::minimum; + const APFloat *C; + if (match(Op1, m_APFloat(C)) && C->isInfinity() && + C->isNegative() == UseNegInf && !PropagateNaN) + return ConstantFP::getInfinity(ReturnType, UseNegInf); + + // TODO: minimum(nnan x, inf) -> x + // TODO: minnum(nnan ninf x, flt_max) -> x + // TODO: maximum(nnan x, -inf) -> x + // TODO: maxnum(nnan ninf x, -flt_max) -> x + // Min/max of the same operation with common operand: // m(m(X, Y)), X --> m(X, Y) (4 commuted variants) if (auto *M0 = dyn_cast(Op0)) @@ -5471,20 +5484,6 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1, (M1->getOperand(0) == Op0 || M1->getOperand(1) == Op0)) return Op1; - // min(X, -Inf) --> -Inf (and commuted variant) - // max(X, +Inf) --> +Inf (and commuted variant) - bool UseNegInf = IID == Intrinsic::minnum || IID == Intrinsic::minimum; - const APFloat *C; - if ((match(Op0, m_APFloat(C)) && C->isInfinity() && - C->isNegative() == UseNegInf && !PropagateNaN) || - (match(Op1, m_APFloat(C)) && C->isInfinity() && - C->isNegative() == UseNegInf && !PropagateNaN)) - return ConstantFP::getInfinity(ReturnType, UseNegInf); - - // TODO: minnum(nnan x, inf) -> x - // TODO: minnum(nnan ninf x, flt_max) -> x - // TODO: maxnum(nnan x, -inf) -> x - // TODO: maxnum(nnan ninf x, -flt_max) -> x break; } default: -- 2.11.0