[llvm] r368175 - [InstCombine] Propagate fast math flags through selects

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 7 08:16:28 PDT 2019


Author: foad
Date: Wed Aug  7 08:16:28 2019
New Revision: 368175

URL: http://llvm.org/viewvc/llvm-project?rev=368175&view=rev
Log:
[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

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
    llvm/trunk/test/Transforms/InstCombine/select_arithmetic.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp?rev=368175&r1=368174&r2=368175&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp Wed Aug  7 08:16:28 2019
@@ -763,12 +763,16 @@ Value *InstCombiner::SimplifySelectsFeed
   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<FPMathOperator>(&I))
-      Builder.setFastMathFlags(I.getFastMathFlags());
+    if (isa<FPMathOperator>(&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)

Modified: llvm/trunk/test/Transforms/InstCombine/select_arithmetic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/select_arithmetic.ll?rev=368175&r1=368174&r2=368175&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/select_arithmetic.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/select_arithmetic.ll Wed Aug  7 08:16:28 2019
@@ -94,10 +94,7 @@ define float @test5(i1 zeroext %arg, flo
 
 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, fl
 
 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> %c
 
 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




More information about the llvm-commits mailing list