Skip to content

Commit c74cd72

Browse files
dtcxzywAnkur-0429
authored andcommitted
[InstCombine] Fix ninf propagation for fcmp+sel -> minmax (llvm#136433)
Proof: https://alive2.llvm.org/ce/z/nCrvfr Closes llvm#136430
1 parent 55dbed1 commit c74cd72

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3929,16 +3929,20 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
39293929
if (match(&SI, m_OrdOrUnordFMax(m_Value(X), m_Value(Y)))) {
39303930
Value *BinIntr =
39313931
Builder.CreateBinaryIntrinsic(Intrinsic::maxnum, X, Y, &SI);
3932-
if (auto *BinIntrInst = dyn_cast<Instruction>(BinIntr))
3932+
if (auto *BinIntrInst = dyn_cast<Instruction>(BinIntr)) {
39333933
BinIntrInst->setHasNoNaNs(FCmp->hasNoNaNs());
3934+
BinIntrInst->setHasNoInfs(FCmp->hasNoInfs());
3935+
}
39343936
return replaceInstUsesWith(SI, BinIntr);
39353937
}
39363938

39373939
if (match(&SI, m_OrdOrUnordFMin(m_Value(X), m_Value(Y)))) {
39383940
Value *BinIntr =
39393941
Builder.CreateBinaryIntrinsic(Intrinsic::minnum, X, Y, &SI);
3940-
if (auto *BinIntrInst = dyn_cast<Instruction>(BinIntr))
3942+
if (auto *BinIntrInst = dyn_cast<Instruction>(BinIntr)) {
39413943
BinIntrInst->setHasNoNaNs(FCmp->hasNoNaNs());
3944+
BinIntrInst->setHasNoInfs(FCmp->hasNoInfs());
3945+
}
39423946
return replaceInstUsesWith(SI, BinIntr);
39433947
}
39443948
}

llvm/test/Transforms/InstCombine/fcmp-fadd-select.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ define float @test_fcmp_ogt_fadd_select_rewrite_flags2(float %in) {
663663
define float @test_fcmp_ogt_fadd_select_rewrite_and_fastmath(float %in) {
664664
; CHECK-LABEL: define float @test_fcmp_ogt_fadd_select_rewrite_and_fastmath(
665665
; CHECK-SAME: float [[IN:%.*]]) {
666-
; CHECK-NEXT: [[SEL_NEW:%.*]] = call fast float @llvm.maxnum.f32(float [[IN]], float 0.000000e+00)
666+
; CHECK-NEXT: [[SEL_NEW:%.*]] = call reassoc nnan nsz arcp contract afn float @llvm.maxnum.f32(float [[IN]], float 0.000000e+00)
667667
; CHECK-NEXT: [[ADD_NEW:%.*]] = fadd fast float [[SEL_NEW]], 1.000000e+00
668668
; CHECK-NEXT: ret float [[ADD_NEW]]
669669
;

llvm/test/Transforms/InstCombine/minmax-fp.ll

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ define float @maxnum_ogt_fmf_on_select(float %a, float %b) {
331331

332332
define <2 x float> @maxnum_oge_fmf_on_select(<2 x float> %a, <2 x float> %b) {
333333
; CHECK-LABEL: @maxnum_oge_fmf_on_select(
334-
; CHECK-NEXT: [[F:%.*]] = call ninf nsz <2 x float> @llvm.maxnum.v2f32(<2 x float> [[A:%.*]], <2 x float> [[B:%.*]])
334+
; CHECK-NEXT: [[F:%.*]] = call nsz <2 x float> @llvm.maxnum.v2f32(<2 x float> [[A:%.*]], <2 x float> [[B:%.*]])
335335
; CHECK-NEXT: ret <2 x float> [[F]]
336336
;
337337
%cond = fcmp oge <2 x float> %a, %b
@@ -383,6 +383,16 @@ define float @maxnum_no_nnan(float %a, float %b) {
383383
ret float %f
384384
}
385385

386+
define float @minnum_olt_fmf_on_select_both_ninf(float %a, float %b) {
387+
; CHECK-LABEL: @minnum_olt_fmf_on_select_both_ninf(
388+
; CHECK-NEXT: [[F:%.*]] = call ninf nsz float @llvm.minnum.f32(float [[A:%.*]], float [[B:%.*]])
389+
; CHECK-NEXT: ret float [[F]]
390+
;
391+
%cond = fcmp ninf olt float %a, %b
392+
%f = select nnan ninf nsz i1 %cond, float %a, float %b
393+
ret float %f
394+
}
395+
386396
define float @minnum_olt_fmf_on_select(float %a, float %b) {
387397
; CHECK-LABEL: @minnum_olt_fmf_on_select(
388398
; CHECK-NEXT: [[F:%.*]] = call nsz float @llvm.minnum.f32(float [[A:%.*]], float [[B:%.*]])
@@ -395,7 +405,7 @@ define float @minnum_olt_fmf_on_select(float %a, float %b) {
395405

396406
define <2 x float> @minnum_ole_fmf_on_select(<2 x float> %a, <2 x float> %b) {
397407
; CHECK-LABEL: @minnum_ole_fmf_on_select(
398-
; CHECK-NEXT: [[F:%.*]] = call ninf nsz <2 x float> @llvm.minnum.v2f32(<2 x float> [[A:%.*]], <2 x float> [[B:%.*]])
408+
; CHECK-NEXT: [[F:%.*]] = call nsz <2 x float> @llvm.minnum.v2f32(<2 x float> [[A:%.*]], <2 x float> [[B:%.*]])
399409
; CHECK-NEXT: ret <2 x float> [[F]]
400410
;
401411
%cond = fcmp ole <2 x float> %a, %b

llvm/test/Transforms/InstCombine/unordered-fcmp-select.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ define float @select_max_ugt_2_use_cmp(float %a, float %b) {
115115
; CHECK-LABEL: @select_max_ugt_2_use_cmp(
116116
; CHECK-NEXT: [[CMP:%.*]] = fcmp reassoc ugt float [[A:%.*]], [[B:%.*]]
117117
; CHECK-NEXT: call void @foo(i1 [[CMP]])
118-
; CHECK-NEXT: [[SEL:%.*]] = call reassoc ninf nsz arcp contract afn float @llvm.maxnum.f32(float [[A]], float [[B]])
118+
; CHECK-NEXT: [[SEL:%.*]] = call reassoc nsz arcp contract afn float @llvm.maxnum.f32(float [[A]], float [[B]])
119119
; CHECK-NEXT: ret float [[SEL]]
120120
;
121121
%cmp = fcmp reassoc ugt float %a, %b

0 commit comments

Comments
 (0)