[PATCH] D38263: Gating select arithmetic optimization

Michael Berg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 26 15:31:30 PDT 2017


mcberg2017 updated this revision to Diff 116702.
mcberg2017 added a comment.

Updated for full context diff, the patch file now contains all the code.


https://reviews.llvm.org/D38263

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


Index: test/Transforms/InstCombine/select_arithmetic.ll
===================================================================
--- test/Transforms/InstCombine/select_arithmetic.ll
+++ test/Transforms/InstCombine/select_arithmetic.ll
@@ -3,17 +3,32 @@
 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
 
 ; Tests folding constants from two similar selects that feed a add
-define float @test1(i1 zeroext %arg) #0 {
+define float @test1a(i1 zeroext %arg) #0 {
   %tmp = select i1 %arg, float 5.000000e+00, float 6.000000e+00
   %tmp1 = select i1 %arg, float 1.000000e+00, float 9.000000e+00
   %tmp2 = fadd float %tmp, %tmp1
   ret float %tmp2
-; CHECK-LABEL: @test1(
+; CHECK-LABEL: @test1a(
 ; CHECK: %tmp2 = select i1 %arg, float 6.000000e+00, float 1.500000e+01
 ; CHECK-NOT: fadd
 ; CHECK: ret float %tmp2
 }
 
+; Tests folding multiple expression constants from similar selects that feed a adds
+define float @test1b(i1 zeroext %arg) #0 {
+  %tmp = select i1 %arg, float 5.000000e+00, float 6.000000e+00
+  %tmp1 = select i1 %arg, float 1.000000e+00, float 9.000000e+00
+  %tmp2 = select i1 %arg, float 2.500000e-01, float 4.000000e+00
+  %tmp3 = fadd float %tmp, %tmp1
+  %tmp4 = fadd float %tmp2, %tmp1
+  %tmp5 = fadd float %tmp4, %tmp3
+  ret float %tmp5
+; CHECK-LABEL: @test1b(
+; CHECK: %tmp5 = select i1 %arg, float 7.250000e+00, float 2.800000e+01
+; CHECK-NOT: fadd
+; CHECK: ret float %tmp5
+}
+
 ; Tests folding constants from two similar selects that feed a sub
 define float @test2(i1 zeroext %arg) #0 {
   %tmp = select i1 %arg, float 5.000000e+00, float 6.000000e+00
Index: lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- lib/Transforms/InstCombine/InstructionCombining.cpp
+++ lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -731,17 +731,18 @@
   Value *SI = nullptr;
   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));
     if (V1 && V2)
       SI = Builder.CreateSelect(A, V2, V1);
-    else if (V2)
+    else if (V2 && SelectsHaveOneUse)
       SI = Builder.CreateSelect(A, V2, Builder.CreateBinOp(Opcode, C, E));
-    else if (V1)
+    else if (V1 && SelectsHaveOneUse)
       SI = Builder.CreateSelect(A, Builder.CreateBinOp(Opcode, B, D), V1);
 
     if (SI)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38263.116702.patch
Type: text/x-patch
Size: 2717 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170926/eba04327/attachment-0001.bin>


More information about the llvm-commits mailing list