[PATCH] D65658: [InstCombine] Propagate fast math flags through selects

Jay Foad via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 7 04:06:35 PDT 2019


foad updated this revision to Diff 213839.
foad added a comment.

Rebase.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D65658/new/

https://reviews.llvm.org/D65658

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


Index: llvm/test/Transforms/InstCombine/select_arithmetic.ll
===================================================================
--- llvm/test/Transforms/InstCombine/select_arithmetic.ll
+++ llvm/test/Transforms/InstCombine/select_arithmetic.ll
@@ -94,10 +94,7 @@
 
 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 <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 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
Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -763,12 +763,16 @@
   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)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D65658.213839.patch
Type: text/x-patch
Size: 3259 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190807/380100c1/attachment.bin>


More information about the llvm-commits mailing list