[InstCombine] Propagate fast math flags through selects

Summary:
In SimplifySelectsFeedingBinaryOp, propagate fast math flags from the
outer op into both arms of the new select, to take advantage of
simplifications that require fast math flags.

Reviewers: mcberg2017, majnemer, spatel, arsenm, xbolva00

Subscribers: wdng, javed.absar, kristof.beyls, hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D65658

llvm-svn: 368175
This commit is contained in:
Jay Foad 2019-08-07 15:16:28 +00:00
parent 7169ea391a
commit 8e8b295835
2 changed files with 13 additions and 16 deletions

View File

@ -763,12 +763,16 @@ Value *InstCombiner::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
if (match(LHS, m_Select(m_Value(A), m_Value(B), m_Value(C))) &&
match(RHS, m_Select(m_Specific(A), m_Value(D), m_Value(E)))) {
bool SelectsHaveOneUse = LHS->hasOneUse() && RHS->hasOneUse();
BuilderTy::FastMathFlagGuard Guard(Builder);
if (isa<FPMathOperator>(&I))
Builder.setFastMathFlags(I.getFastMathFlags());
Value *V1 = SimplifyBinOp(Opcode, C, E, SQ.getWithInstruction(&I));
Value *V2 = SimplifyBinOp(Opcode, B, D, SQ.getWithInstruction(&I));
FastMathFlags FMF;
BuilderTy::FastMathFlagGuard Guard(Builder);
if (isa<FPMathOperator>(&I)) {
FMF = I.getFastMathFlags();
Builder.setFastMathFlags(FMF);
}
Value *V1 = SimplifyBinOp(Opcode, C, E, FMF, SQ.getWithInstruction(&I));
Value *V2 = SimplifyBinOp(Opcode, B, D, FMF, SQ.getWithInstruction(&I));
if (V1 && V2)
SI = Builder.CreateSelect(A, V2, V1);
else if (V2 && SelectsHaveOneUse)

View File

@ -94,10 +94,7 @@ define float @test5(i1 zeroext %arg, float %div) {
define float @fmul_nnan_nsz(i1 %cond, float %val) {
; CHECK-LABEL: @fmul_nnan_nsz(
; CHECK-NEXT: [[LHS:%.*]] = select i1 [[COND:%.*]], float [[VAL:%.*]], float 0.000000e+00
; CHECK-NEXT: [[RHS:%.*]] = select i1 [[COND]], float -0.000000e+00, float [[VAL]]
; CHECK-NEXT: [[MUL:%.*]] = fmul nnan nsz float [[LHS]], [[RHS]]
; CHECK-NEXT: ret float [[MUL]]
; CHECK-NEXT: ret float 0.000000e+00
;
%lhs = select i1 %cond, float %val, float +0.0
%rhs = select i1 %cond, float -0.0, float %val
@ -107,10 +104,7 @@ define float @fmul_nnan_nsz(i1 %cond, float %val) {
define <2 x float> @fadd_nsz(<2 x i1> %cond, <2 x float> %val) {
; CHECK-LABEL: @fadd_nsz(
; CHECK-NEXT: [[LHS:%.*]] = select <2 x i1> [[COND:%.*]], <2 x float> [[VAL:%.*]], <2 x float> zeroinitializer
; CHECK-NEXT: [[RHS:%.*]] = select <2 x i1> [[COND]], <2 x float> zeroinitializer, <2 x float> [[VAL]]
; CHECK-NEXT: [[ADD:%.*]] = fadd nsz <2 x float> [[LHS]], [[RHS]]
; CHECK-NEXT: ret <2 x float> [[ADD]]
; CHECK-NEXT: ret <2 x float> [[VAL:%.*]]
;
%lhs = select <2 x i1> %cond, <2 x float> %val, <2 x float> <float +0.0, float +0.0>
%rhs = select <2 x i1> %cond, <2 x float> <float +0.0, float +0.0>, <2 x float> %val
@ -120,9 +114,8 @@ define <2 x float> @fadd_nsz(<2 x i1> %cond, <2 x float> %val) {
define double @fsub_nnan(i1 %cond, double %val, double %val2) {
; CHECK-LABEL: @fsub_nnan(
; CHECK-NEXT: [[LHS:%.*]] = select i1 [[COND:%.*]], double [[VAL:%.*]], double [[VAL2:%.*]]
; CHECK-NEXT: [[RHS:%.*]] = select i1 [[COND]], double [[VAL]], double 7.000000e+00
; CHECK-NEXT: [[ADD:%.*]] = fsub nnan double [[LHS]], [[RHS]]
; CHECK-NEXT: [[TMP1:%.*]] = fadd nnan double [[VAL2:%.*]], -7.000000e+00
; CHECK-NEXT: [[ADD:%.*]] = select nnan i1 [[COND:%.*]], double 0.000000e+00, double [[TMP1]]
; CHECK-NEXT: ret double [[ADD]]
;
%lhs = select i1 %cond, double %val, double %val2