Skip to content

Commit f74a334

Browse files
committed
[ConstantFolding] add undef handling for fmin/fmax intrinsics
The output here may not be optimal (yet), but it should be consistent for commuted operands (it was not before) and correct. We can do better by checking FMF and NaN if needed. Code in InstSimplify generally assumes that we have already folded code like this, so it was not handling 2 constant inputs by commuting consistently.
1 parent d4dd961 commit f74a334

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,6 +2305,25 @@ static Constant *ConstantFoldScalarCall2(StringRef Name,
23052305
const CallBase *Call) {
23062306
assert(Operands.size() == 2 && "Wrong number of operands.");
23072307

2308+
if (Ty->isFloatingPointTy()) {
2309+
// TODO: We should have undef handling for all of the FP intrinsics that
2310+
// are attempted to be folded in this function.
2311+
bool IsOp0Undef = isa<UndefValue>(Operands[0]);
2312+
bool IsOp1Undef = isa<UndefValue>(Operands[1]);
2313+
switch (IntrinsicID) {
2314+
case Intrinsic::maxnum:
2315+
case Intrinsic::minnum:
2316+
case Intrinsic::maximum:
2317+
case Intrinsic::minimum:
2318+
// If one argument is undef, return the other argument.
2319+
if (IsOp0Undef)
2320+
return Operands[1];
2321+
if (IsOp1Undef)
2322+
return Operands[0];
2323+
break;
2324+
}
2325+
}
2326+
23082327
if (auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
23092328
if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
23102329
return nullptr;

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ define <2 x double> @frem_undef_op0_constant_vec(<2 x double> %x) {
540540

541541
define <2 x double> @maximum_nan_op0_vec_partial_undef_op1_undef(<2 x double> %x) {
542542
; CHECK-LABEL: @maximum_nan_op0_vec_partial_undef_op1_undef(
543-
; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>
543+
; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double undef>
544544
;
545545
%r = call <2 x double> @llvm.maximum.v2f64(<2 x double> <double 0x7ff8000000000000, double undef>, <2 x double> undef)
546546
ret <2 x double> %r
@@ -556,7 +556,7 @@ define <2 x double> @maximum_nan_op1_vec_partial_undef_op0_undef(<2 x double> %x
556556

557557
define <2 x double> @minimum_nan_op0_vec_partial_undef_op1_undef(<2 x double> %x) {
558558
; CHECK-LABEL: @minimum_nan_op0_vec_partial_undef_op1_undef(
559-
; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>
559+
; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double undef>
560560
;
561561
%r = call <2 x double> @llvm.minimum.v2f64(<2 x double> <double 0x7ff8000000000000, double undef>, <2 x double> undef)
562562
ret <2 x double> %r
@@ -572,7 +572,7 @@ define <2 x double> @minimum_nan_op1_vec_partial_undef_op0_undef(<2 x double> %x
572572

573573
define <2 x double> @maxnum_nan_op0_vec_partial_undef_op1_undef(<2 x double> %x) {
574574
; CHECK-LABEL: @maxnum_nan_op0_vec_partial_undef_op1_undef(
575-
; CHECK-NEXT: ret <2 x double> undef
575+
; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double undef>
576576
;
577577
%r = call <2 x double> @llvm.maxnum.v2f64(<2 x double> <double 0x7ff8000000000000, double undef>, <2 x double> undef)
578578
ret <2 x double> %r
@@ -588,7 +588,7 @@ define <2 x double> @maxnum_nan_op1_vec_partial_undef_op0_undef(<2 x double> %x)
588588

589589
define <2 x double> @minnum_nan_op0_vec_partial_undef_op1_undef(<2 x double> %x) {
590590
; CHECK-LABEL: @minnum_nan_op0_vec_partial_undef_op1_undef(
591-
; CHECK-NEXT: ret <2 x double> undef
591+
; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double undef>
592592
;
593593
%r = call <2 x double> @llvm.minnum.v2f64(<2 x double> <double 0x7ff8000000000000, double undef>, <2 x double> undef)
594594
ret <2 x double> %r

0 commit comments

Comments
 (0)