Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,11 @@ Instruction *InstCombinerImpl::foldSelectIntoOp(SelectInst &SI, Value *TrueVal,

Value *NewSel = Builder.CreateSelect(SI.getCondition(), Swapped ? C : OOp,
Swapped ? OOp : C, "", &SI);
if (isa<FPMathOperator>(&SI))
if (isa<FPMathOperator>(&SI)) {
cast<Instruction>(NewSel)->setFastMathFlags(FMF);
if (!TVI->hasNoInfs() && !FMF.noNaNs())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a TODO that you can preserve this for operators which cannot introduce infs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fold works for fadd, fsub, and fmul. Unfortunately, there are no exceptions. All of these patterns have counterexamples to block the flag propagation. I have updated comments and proofs.

cast<Instruction>(NewSel)->setHasNoInfs(false);
}
NewSel->takeName(TVI);
BinaryOperator *BO =
BinaryOperator::Create(TVI->getOpcode(), FalseVal, NewSel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,50 @@ define float @select_fpclass_fadd(i1 %cond, float nofpclass(nan) %A, float %B) {
ret float %D
}

define float @select_fpclass_fadd_ninf1(i1 %cond, float nofpclass(nan) %A, float %B) {
; CHECK-LABEL: @select_fpclass_fadd_ninf1(
; CHECK-NEXT: [[C:%.*]] = select i1 [[COND:%.*]], float [[B:%.*]], float -0.000000e+00
; CHECK-NEXT: [[D:%.*]] = fadd float [[A:%.*]], [[C]]
; CHECK-NEXT: ret float [[D]]
;
%C = fadd ninf float %A, %B
%D = select i1 %cond, float %C, float %A
ret float %D
}

define float @select_fpclass_fadd_ninf2(i1 %cond, float nofpclass(nan) %A, float %B) {
; CHECK-LABEL: @select_fpclass_fadd_ninf2(
; CHECK-NEXT: [[C:%.*]] = select i1 [[COND:%.*]], float [[B:%.*]], float -0.000000e+00
; CHECK-NEXT: [[D:%.*]] = fadd float [[A:%.*]], [[C]]
; CHECK-NEXT: ret float [[D]]
;
%C = fadd float %A, %B
%D = select ninf i1 %cond, float %C, float %A
ret float %D
}

define float @select_fpclass_fadd_ninf3(i1 %cond, float nofpclass(nan) %A, float %B) {
; CHECK-LABEL: @select_fpclass_fadd_ninf3(
; CHECK-NEXT: [[C:%.*]] = select ninf i1 [[COND:%.*]], float [[B:%.*]], float -0.000000e+00
; CHECK-NEXT: [[D:%.*]] = fadd ninf float [[A:%.*]], [[C]]
; CHECK-NEXT: ret float [[D]]
;
%C = fadd ninf float %A, %B
%D = select ninf i1 %cond, float %C, float %A
ret float %D
}

define float @select_fpclass_fadd_nnan_ninf(i1 %cond, float nofpclass(nan) %A, float %B) {
; CHECK-LABEL: @select_fpclass_fadd_nnan_ninf(
; CHECK-NEXT: [[C:%.*]] = select nnan ninf i1 [[COND:%.*]], float [[B:%.*]], float -0.000000e+00
; CHECK-NEXT: [[D:%.*]] = fadd float [[A:%.*]], [[C]]
; CHECK-NEXT: ret float [[D]]
;
%C = fadd float %A, %B
%D = select nnan ninf i1 %cond, float %C, float %A
ret float %D
}

define float @select_nnan_fadd(i1 %cond, float %A, float %B) {
; CHECK-LABEL: @select_nnan_fadd(
; CHECK-NEXT: [[C:%.*]] = select nnan i1 [[COND:%.*]], float [[B:%.*]], float -0.000000e+00
Expand Down