Skip to content

Commit f389247

Browse files
committed
Added intersection of rewrite-based flags.
1 parent 1a434e9 commit f389247

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,11 +3675,36 @@ static Value *foldSelectIntoAddConstant(SelectInst &SI,
36753675
return nullptr;
36763676

36773677
Value *NewSelect = Builder.CreateSelect(SI.getCondition(), X, Z, "", &SI);
3678-
cast<Instruction>(NewSelect)->setFastMathFlags(SI.getFastMathFlags());
36793678
NewSelect->takeName(&SI);
36803679

3681-
Value *NewFAdd = Builder.CreateFAddFMF(NewSelect, C, FAdd);
3680+
Value *NewFAdd = Builder.CreateFAdd(NewSelect, C);
36823681
NewFAdd->takeName(FAdd);
3682+
3683+
// Propagate rewrite-based flags
3684+
auto SelectFMF = SI.getFastMathFlags();
3685+
auto FAddFMF = FAdd->getFastMathFlags();
3686+
FastMathFlags CommonFMF, NewFAddFMF, NewSelectFMF;
3687+
3688+
CommonFMF.setAllowReassoc(SelectFMF.allowReassoc() &&
3689+
FAddFMF.allowReassoc());
3690+
CommonFMF.setAllowReciprocal(SelectFMF.allowReciprocal() &&
3691+
FAddFMF.allowReciprocal());
3692+
CommonFMF.setAllowContract(SelectFMF.allowContract() &&
3693+
FAddFMF.allowContract());
3694+
CommonFMF.setApproxFunc(SelectFMF.approxFunc() && FAddFMF.approxFunc());
3695+
NewSelectFMF = NewFAddFMF = CommonFMF;
3696+
3697+
// Propagate FastMath flags
3698+
NewFAddFMF.setNoNaNs(FAddFMF.noNaNs());
3699+
NewFAddFMF.setNoInfs(FAddFMF.noInfs());
3700+
NewFAddFMF.setNoSignedZeros(FAddFMF.noSignedZeros());
3701+
cast<Instruction>(NewFAdd)->setFastMathFlags(NewFAddFMF);
3702+
3703+
NewSelectFMF.setNoNaNs(SelectFMF.noNaNs());
3704+
NewSelectFMF.setNoInfs(SelectFMF.noInfs());
3705+
NewSelectFMF.setNoSignedZeros(SelectFMF.noSignedZeros());
3706+
cast<Instruction>(NewSelect)->setFastMathFlags(NewSelectFMF);
3707+
36833708
return NewFAdd;
36843709
}
36853710

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,30 @@ define float @test_fcmp_multiple_uses(float %in) {
632632
%res = fadd float %sel.1, %sel.2
633633
ret float %res
634634
}
635+
636+
; Rewrite-based flags propagation
637+
define float @test_fcmp_ogt_fadd_select_rewrite_flags(float %in) {
638+
; CHECK-LABEL: define float @test_fcmp_ogt_fadd_select_rewrite_flags(
639+
; CHECK-SAME: float [[IN:%.*]]) {
640+
; CHECK-NEXT: [[SEL_NEW:%.*]] = call reassoc nnan nsz arcp contract afn float @llvm.maxnum.f32(float [[IN]], float 0.000000e+00)
641+
; CHECK-NEXT: [[ADD_NEW:%.*]] = fadd reassoc arcp contract afn float [[SEL_NEW]], 1.000000e+00
642+
; CHECK-NEXT: ret float [[ADD_NEW]]
643+
;
644+
%cmp1 = fcmp ogt float %in, 0.000000e+00
645+
%add = fadd reassoc afn arcp contract float %in, 1.000000e+00
646+
%sel = select nnan nsz reassoc afn arcp contract i1 %cmp1, float %add, float 1.000000e+00
647+
ret float %sel
648+
}
649+
650+
define float @test_fcmp_ogt_fadd_select_rewrite_and_fastmath(float %in) {
651+
; CHECK-LABEL: define float @test_fcmp_ogt_fadd_select_rewrite_and_fastmath(
652+
; CHECK-SAME: float [[IN:%.*]]) {
653+
; CHECK-NEXT: [[SEL_NEW:%.*]] = call fast float @llvm.maxnum.f32(float [[IN]], float 0.000000e+00)
654+
; CHECK-NEXT: [[ADD_NEW:%.*]] = fadd fast float [[SEL_NEW]], 1.000000e+00
655+
; CHECK-NEXT: ret float [[ADD_NEW]]
656+
;
657+
%cmp1 = fcmp ogt float %in, 0.000000e+00
658+
%add = fadd fast float %in, 1.000000e+00
659+
%sel = select fast i1 %cmp1, float %add, float 1.000000e+00
660+
ret float %sel
661+
}

0 commit comments

Comments
 (0)