diff options
-rw-r--r-- | llvm/lib/Analysis/InstructionSimplify.cpp | 17 | ||||
-rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp | 35 | ||||
-rw-r--r-- | llvm/test/Transforms/InstCombine/maxnum.ll | 17 | ||||
-rw-r--r-- | llvm/test/Transforms/InstCombine/minnum.ll | 17 | ||||
-rw-r--r-- | llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll | 57 |
5 files changed, 73 insertions, 70 deletions
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 13805437b54..9d317b46b9e 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -4812,7 +4812,7 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1, } break; case Intrinsic::maxnum: - case Intrinsic::minnum: + case Intrinsic::minnum: { // If the arguments are the same, this is a no-op. if (Op0 == Op1) return Op0; @@ -4831,7 +4831,22 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1, (M1->getOperand(0) == Op0 || M1->getOperand(1) == Op0)) return Op1; + // minnum(X, -Inf) --> -Inf (and commuted variant) + // maxnum(X, +Inf) --> +Inf (and commuted variant) + bool UseNegInf = IID == Intrinsic::minnum; + const APFloat *C; + if ((match(Op0, m_APFloat(C)) && C->isInfinity() && + C->isNegative() == UseNegInf) || + (match(Op1, m_APFloat(C)) && C->isInfinity() && + C->isNegative() == UseNegInf)) + 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: break; } diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index dcd080e3305..310b87e931c 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -1133,37 +1133,6 @@ static Value *simplifyX86vpcom(const IntrinsicInst &II, return nullptr; } -static Value *simplifyMinnumMaxnum(const IntrinsicInst &II) { - Value *Arg0 = II.getArgOperand(0); - Value *Arg1 = II.getArgOperand(1); - - const auto *C1 = dyn_cast<ConstantFP>(Arg1); - - // fmin(x, nan) -> x - if (C1 && C1->isNaN()) - return Arg0; - - if (II.getIntrinsicID() == Intrinsic::minnum) { - // TODO: fmin(nnan x, inf) -> x - // TODO: fmin(nnan ninf x, flt_max) -> x - if (C1 && C1->isInfinity()) { - // fmin(x, -inf) -> -inf - if (C1->isNegative()) - return Arg1; - } - } else { - assert(II.getIntrinsicID() == Intrinsic::maxnum); - // TODO: fmax(nnan x, -inf) -> x - // TODO: fmax(nnan ninf x, -flt_max) -> x - if (C1 && C1->isInfinity()) { - // fmax(x, inf) -> inf - if (!C1->isNegative()) - return Arg1; - } - } - return nullptr; -} - static bool maskIsAllOneOrUndef(Value *Mask) { auto *ConstMask = dyn_cast<Constant>(Mask); if (!ConstMask) @@ -2000,10 +1969,6 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) { return II; } - // FIXME: Simplifications should be in instsimplify. - if (Value *V = simplifyMinnumMaxnum(*II)) - return replaceInstUsesWith(*II, V); - Value *X, *Y; if (match(Arg0, m_FNeg(m_Value(X))) && match(Arg1, m_FNeg(m_Value(Y))) && (Arg0->hasOneUse() || Arg1->hasOneUse())) { diff --git a/llvm/test/Transforms/InstCombine/maxnum.ll b/llvm/test/Transforms/InstCombine/maxnum.ll index 7f94a722a03..e3630ba3ea5 100644 --- a/llvm/test/Transforms/InstCombine/maxnum.ll +++ b/llvm/test/Transforms/InstCombine/maxnum.ll @@ -158,23 +158,6 @@ define float @maxnum4(float %x, float %y, float %z, float %w) { ret float %c } -define float @fold_maxnum_f32_inf_val(float %x) { -; CHECK-LABEL: @fold_maxnum_f32_inf_val( -; CHECK-NEXT: ret float 0x7FF0000000000000 -; - %val = call float @llvm.maxnum.f32(float 0x7FF0000000000000, float %x) - ret float %val -} - -define float @fold_maxnum_f32_neginf_val(float %x) { -; CHECK-LABEL: @fold_maxnum_f32_neginf_val( -; CHECK-NEXT: [[VAL:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float 0xFFF0000000000000) -; CHECK-NEXT: ret float [[VAL]] -; - %val = call float @llvm.maxnum.f32(float 0xFFF0000000000000, float %x) - ret float %val -} - ; PR37404 - https://bugs.llvm.org/show_bug.cgi?id=37404 define <2 x float> @neg_neg(<2 x float> %x, <2 x float> %y) { diff --git a/llvm/test/Transforms/InstCombine/minnum.ll b/llvm/test/Transforms/InstCombine/minnum.ll index 94c8b5518b2..a5236d2e50f 100644 --- a/llvm/test/Transforms/InstCombine/minnum.ll +++ b/llvm/test/Transforms/InstCombine/minnum.ll @@ -182,23 +182,6 @@ define float @maxnum_x_minnum_x_y(float %x, float %y) { ret float %b } -define float @fold_minnum_f32_inf_val(float %x) { -; CHECK-LABEL: @fold_minnum_f32_inf_val( -; CHECK-NEXT: [[VAL:%.*]] = call float @llvm.minnum.f32(float [[X:%.*]], float 0x7FF0000000000000) -; CHECK-NEXT: ret float [[VAL]] -; - %val = call float @llvm.minnum.f32(float 0x7FF0000000000000, float %x) - ret float %val -} - -define float @fold_minnum_f32_minf_val(float %x) { -; CHECK-LABEL: @fold_minnum_f32_minf_val( -; CHECK-NEXT: ret float 0xFFF0000000000000 -; - %val = call float @llvm.minnum.f32(float 0xFFF0000000000000, float %x) - ret float %val -} - ; PR37405 - https://bugs.llvm.org/show_bug.cgi?id=37405 define double @neg_neg(double %x, double %y) { diff --git a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll index 305e508480f..6b6ae48f516 100644 --- a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll +++ b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll @@ -667,6 +667,34 @@ define float @minnum_x_y_minnum_z(float %x, float %y, float %z) { ret float %b } +; minnum(X, -INF) --> -INF + +define float @minnum_neginf(float %x) { +; CHECK-LABEL: @minnum_neginf( +; CHECK-NEXT: ret float 0xFFF0000000000000 +; + %val = call float @llvm.minnum.f32(float %x, float 0xFFF0000000000000) + ret float %val +} + +define <2 x double> @minnum_neginf_commute_vec(<2 x double> %x) { +; CHECK-LABEL: @minnum_neginf_commute_vec( +; CHECK-NEXT: ret <2 x double> <double 0xFFF0000000000000, double 0xFFF0000000000000> +; + %r = call <2 x double> @llvm.minnum.v2f64(<2 x double> <double 0xFFF0000000000000, double 0xFFF0000000000000>, <2 x double> %x) + ret <2 x double> %r +} + +; negative test + +define float @minnum_inf(float %x) { +; CHECK-LABEL: @minnum_inf( +; CHECK-NEXT: [[VAL:%.*]] = call float @llvm.minnum.f32(float 0x7FF0000000000000, float [[X:%.*]]) +; CHECK-NEXT: ret float [[VAL]] +; + %val = call float @llvm.minnum.f32(float 0x7FF0000000000000, float %x) + ret float %val +} define float @maxnum_x_maxnum_x_y(float %x, float %y) { ; CHECK-LABEL: @maxnum_x_maxnum_x_y( ; CHECK-NEXT: [[A:%.*]] = call float @llvm.maxnum.f32(float [[X:%.*]], float [[Y:%.*]]) @@ -733,6 +761,35 @@ define float @maxnum_x_y_maxnum_z(float %x, float %y, float %z) { ret float %b } +; maxnum(X, INF) --> INF + +define <2 x double> @maxnum_inf(<2 x double> %x) { +; CHECK-LABEL: @maxnum_inf( +; CHECK-NEXT: ret <2 x double> <double 0x7FF0000000000000, double 0x7FF0000000000000> +; + %val = call <2 x double> @llvm.maxnum.v2f64(<2 x double> %x, <2 x double><double 0x7FF0000000000000, double 0x7FF0000000000000>) + ret <2 x double> %val +} + +define float @maxnum_inf_commute(float %x) { +; CHECK-LABEL: @maxnum_inf_commute( +; CHECK-NEXT: ret float 0x7FF0000000000000 +; + %val = call float @llvm.maxnum.f32(float 0x7FF0000000000000, float %x) + ret float %val +} + +; negative test + +define float @maxnum_neginf(float %x) { +; CHECK-LABEL: @maxnum_neginf( +; CHECK-NEXT: [[VAL:%.*]] = call float @llvm.maxnum.f32(float 0xFFF0000000000000, float [[X:%.*]]) +; CHECK-NEXT: ret float [[VAL]] +; + %val = call float @llvm.maxnum.f32(float 0xFFF0000000000000, float %x) + ret float %val +} + ; Y - (Y - X) --> X define float @fsub_fsub_common_op(float %x, float %y) { |