From: Jay Foad Date: Wed, 7 Aug 2019 15:16:28 +0000 (+0000) Subject: [InstCombine] Propagate fast math flags through selects X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dc389f37ff12f9038f536af7788bc0c8189a8af3;p=llvm [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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@368175 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp index 385f4926b84..e8282b3bc13 100644 --- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -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(); + + FastMathFlags FMF; BuilderTy::FastMathFlagGuard Guard(Builder); - if (isa(&I)) - Builder.setFastMathFlags(I.getFastMathFlags()); + if (isa(&I)) { + FMF = I.getFastMathFlags(); + Builder.setFastMathFlags(FMF); + } - Value *V1 = SimplifyBinOp(Opcode, C, E, SQ.getWithInstruction(&I)); - Value *V2 = SimplifyBinOp(Opcode, B, D, SQ.getWithInstruction(&I)); + 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) diff --git a/test/Transforms/InstCombine/select_arithmetic.ll b/test/Transforms/InstCombine/select_arithmetic.ll index d250dd93cd7..79f24a1aa95 100644 --- a/test/Transforms/InstCombine/select_arithmetic.ll +++ b/test/Transforms/InstCombine/select_arithmetic.ll @@ -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> %rhs = select <2 x i1> %cond, <2 x float> , <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