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

Jay Foad via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 2 07:44:23 PDT 2019


foad created this revision.
foad added reviewers: mcberg2017, majnemer, spatel, arsenm, xbolva00.
Herald added subscribers: hiraditya, kristof.beyls, javed.absar, wdng.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

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
@@ -92,3 +92,14 @@
   ret float %mul
 }
 
+; Tests simplifications that require fast math flags.
+
+define float @test6(i1 zeroext %arg, float %val) #0 {
+; CHECK-LABEL: @test6(
+; CHECK-NEXT:    ret float 0.000000e+00
+;
+  %lhs = select i1 %arg, float %val, float +0.0
+  %rhs = select i1 %arg, float -0.0, float %val
+  %mul = fmul fast float %lhs, %rhs
+  ret float %mul
+}
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.213049.patch
Type: text/x-patch
Size: 1792 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190802/16586670/attachment.bin>


More information about the llvm-commits mailing list