Skip to content

Commit 9c88b6d

Browse files
[ConstantFolding] Fold maximumnum and minimumnum (#138700)
Add constant-folding support for the maximumnum and minimumnum intrinsics, and extend the tests to show the qnan vs snan behavior differences between maxnum/maximum/maximumnum.
1 parent 37fecfa commit 9c88b6d

File tree

3 files changed

+368
-0
lines changed

3 files changed

+368
-0
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,8 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
16421642
case Intrinsic::maxnum:
16431643
case Intrinsic::minimum:
16441644
case Intrinsic::maximum:
1645+
case Intrinsic::minimumnum:
1646+
case Intrinsic::maximumnum:
16451647
case Intrinsic::log:
16461648
case Intrinsic::log2:
16471649
case Intrinsic::log10:
@@ -2930,6 +2932,8 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
29302932
case Intrinsic::minnum:
29312933
case Intrinsic::maximum:
29322934
case Intrinsic::minimum:
2935+
case Intrinsic::maximumnum:
2936+
case Intrinsic::minimumnum:
29332937
case Intrinsic::nvvm_fmax_d:
29342938
case Intrinsic::nvvm_fmin_d:
29352939
// If one argument is undef, return the other argument.
@@ -3030,6 +3034,10 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
30303034
return ConstantFP::get(Ty->getContext(), minimum(Op1V, Op2V));
30313035
case Intrinsic::maximum:
30323036
return ConstantFP::get(Ty->getContext(), maximum(Op1V, Op2V));
3037+
case Intrinsic::minimumnum:
3038+
return ConstantFP::get(Ty->getContext(), minimumnum(Op1V, Op2V));
3039+
case Intrinsic::maximumnum:
3040+
return ConstantFP::get(Ty->getContext(), maximumnum(Op1V, Op2V));
30333041

30343042
case Intrinsic::nvvm_fmax_d:
30353043
case Intrinsic::nvvm_fmax_f:

llvm/test/Transforms/InstSimplify/ConstProp/fp-undef.ll

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ declare <2 x double> @llvm.minnum.v2f64(<2 x double>, <2 x double>)
55
declare <2 x double> @llvm.maxnum.v2f64(<2 x double>, <2 x double>)
66
declare <2 x double> @llvm.minimum.v2f64(<2 x double>, <2 x double>)
77
declare <2 x double> @llvm.maximum.v2f64(<2 x double>, <2 x double>)
8+
declare <2 x double> @llvm.minimumnum.v2f64(<2 x double>, <2 x double>)
9+
declare <2 x double> @llvm.maximumnum.v2f64(<2 x double>, <2 x double>)
810

911
; Constant folding - undef undef.
1012

@@ -538,6 +540,38 @@ define <2 x double> @frem_undef_op0_constant_vec(<2 x double> %x) {
538540
ret <2 x double> %r
539541
}
540542

543+
define <2 x double> @maximumnum_nan_op0_vec_partial_poison_op1_poison(<2 x double> %x) {
544+
; CHECK-LABEL: @maximumnum_nan_op0_vec_partial_poison_op1_poison(
545+
; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double poison>
546+
;
547+
%r = call <2 x double> @llvm.maximumnum.v2f64(<2 x double> <double 0x7ff8000000000000, double poison>, <2 x double> poison)
548+
ret <2 x double> %r
549+
}
550+
551+
define <2 x double> @maximumnum_nan_op1_vec_partial_poison_op0_poison(<2 x double> %x) {
552+
; CHECK-LABEL: @maximumnum_nan_op1_vec_partial_poison_op0_poison(
553+
; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double poison>
554+
;
555+
%r = call <2 x double> @llvm.maximumnum.v2f64(<2 x double> poison, <2 x double> <double 0x7ff8000000000000, double poison>)
556+
ret <2 x double> %r
557+
}
558+
559+
define <2 x double> @minimumnum_nan_op0_vec_partial_poison_op1_poison(<2 x double> %x) {
560+
; CHECK-LABEL: @minimumnum_nan_op0_vec_partial_poison_op1_poison(
561+
; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double poison>
562+
;
563+
%r = call <2 x double> @llvm.minimumnum.v2f64(<2 x double> <double 0x7ff8000000000000, double poison>, <2 x double> poison)
564+
ret <2 x double> %r
565+
}
566+
567+
define <2 x double> @minimumnum_nan_op1_vec_partial_poison_op0_poison(<2 x double> %x) {
568+
; CHECK-LABEL: @minimumnum_nan_op1_vec_partial_poison_op0_poison(
569+
; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double poison>
570+
;
571+
%r = call <2 x double> @llvm.minimumnum.v2f64(<2 x double> poison, <2 x double> <double 0x7ff8000000000000, double poison>)
572+
ret <2 x double> %r
573+
}
574+
541575
define <2 x double> @maximum_nan_op0_vec_partial_undef_op1_undef(<2 x double> %x) {
542576
; CHECK-LABEL: @maximum_nan_op0_vec_partial_undef_op1_undef(
543577
; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double undef>

0 commit comments

Comments
 (0)