Provide InstCombines for the following 3 cases:
authorJean-Luc Duprat <jduprat@apple.com>
Mon, 6 May 2013 16:55:50 +0000 (16:55 +0000)
committerJean-Luc Duprat <jduprat@apple.com>
Mon, 6 May 2013 16:55:50 +0000 (16:55 +0000)
A * (1 - (uitofp i1 C)) -> select C, 0, A
B * (uitofp i1 C) -> select C, B, 0
select C, 0, A + select C, B, 0 -> select C, B, A

These come up in code that has been hand-optimized from a select to a linear blend,
on platforms where that may have mattered. We want to undo such changes
with the following transform:
A*(1 - uitofp i1 C) + B*(uitofp i1 C) -> select C, A, B

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@181216 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/PatternMatch.h
lib/Transforms/InstCombine/InstCombineAddSub.cpp
lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
test/Transforms/InstCombine/add4.ll [new file with mode: 0644]

index 51b66b0dcd8f3365d015d4e938e3ad24c0e02199..95d9d782ec04350b35f797cb2aa53410c4f3beed 100644 (file)
@@ -693,6 +693,12 @@ m_ZExt(const OpTy &Op) {
   return CastClass_match<OpTy, Instruction::ZExt>(Op);
 }
 
+/// m_UIToFP
+template<typename OpTy>
+inline CastClass_match<OpTy, Instruction::UIToFP>
+m_UIToFp(const OpTy &Op) { 
+  return CastClass_match<OpTy, Instruction::UIToFP>(Op);
+}
 
 //===----------------------------------------------------------------------===//
 // Matchers for unary operators
index b96eb51081ee6aa7f938ca6aba8448647cc36277..1aa51d06cb09547e9f28f4ea4a8c7e7dfb8481d2 100644 (file)
@@ -1232,6 +1232,31 @@ Instruction *InstCombiner::visitFAdd(BinaryOperator &I) {
     }
   }
 
+  // select C, 0, B + select C, A, 0 -> select C, A, B
+  {
+    Value *A1, *B1, *C1, *A2, *B2, *C2;
+    if (match(LHS, m_Select(m_Value(C1), m_Value(A1), m_Value(B1))) &&
+        match(RHS, m_Select(m_Value(C2), m_Value(A2), m_Value(B2)))) {
+      if (C1 == C2) {
+        Constant *Z1=0, *Z2=0;
+        Value *A, *B, *C=C1;
+        if (match(A1, m_AnyZero()) && match(B2, m_AnyZero())) {
+            Z1 = dyn_cast<Constant>(A1); A = A2;
+            Z2 = dyn_cast<Constant>(B2); B = B1;
+        } else if (match(B1, m_AnyZero()) && match(A2, m_AnyZero())) {
+            Z1 = dyn_cast<Constant>(B1); B = B2;
+            Z2 = dyn_cast<Constant>(A2); A = A1; 
+        }
+        
+        if (Z1 && Z2 && 
+            (I.hasNoSignedZeros() || 
+             (Z1->isNegativeZeroValue() && Z2->isNegativeZeroValue()))) {
+          return SelectInst::Create(C, A, B);
+        }
+      }
+    }
+  }
+
   if (I.hasUnsafeAlgebra()) {
     if (Value *V = FAddCombine(Builder).simplify(&I))
       return ReplaceInstUsesWith(I, V);
index df7390652f10d51a47d8ff2fd5469ccf6895b6e5..0ac0ca27acf60e548474d6663c4c895b9335fa61 100644 (file)
@@ -524,6 +524,34 @@ Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
       }
     }
 
+    // B * (uitofp i1 C) -> select C, B, 0
+    if(I.hasNoNaNs() && I.hasNoInfs() && I.hasNoSignedZeros()) {
+        Value *LHS=Op0, *RHS=Op1;
+        Value *B, *C;
+        if (!match(RHS, m_UIToFp(m_Value(C))))
+            std::swap(LHS, RHS);
+
+        if (match(RHS, m_UIToFp(m_Value(C)))) {
+            B=LHS;
+            Value *Zero = ConstantFP::getNegativeZero(B->getType());
+            return SelectInst::Create(C, B, Zero);
+        }
+    }
+
+    // A * (1 - uitofp i1 C) -> select C, 0, A
+    if(I.hasNoNaNs() && I.hasNoInfs() && I.hasNoSignedZeros()) { 
+        Value *LHS=Op0, *RHS=Op1;
+        Value *A, *C;
+        if (!match(RHS, m_FSub(m_FPOne(), m_UIToFp(m_Value(C)))))
+            std::swap(LHS, RHS);
+
+        if (match(RHS, m_FSub(m_FPOne(), m_UIToFp(m_Value(C))))) {
+            A=LHS;
+            Value *Zero = ConstantFP::getNegativeZero(A->getType());
+            return SelectInst::Create(C, Zero, A);
+        }
+    }
+
     if (!isa<Constant>(Op1))
       std::swap(Opnd0, Opnd1);
     else
diff --git a/test/Transforms/InstCombine/add4.ll b/test/Transforms/InstCombine/add4.ll
new file mode 100644 (file)
index 0000000..9cc24e9
--- /dev/null
@@ -0,0 +1,34 @@
+; RUN: opt < %s -instcombine -S | grep inttoptr | count 2
+
+;; Target triple for gep raising case below.
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
+target triple = "i686-apple-darwin8"
+
+define float @test1(float %A, float %B, i1 %C) {
+EntryBlock:
+  ;; A*(1 - uitofp i1 C) -> select C, 0, A
+  %cf = uitofp i1 %C to float
+  %mc = fsub float 1.000000e+00, %cf
+  %p1 = fmul fast float %A, %mc
+  ret float %p1
+}
+
+define float @test2(float %A, float %B, i1 %C) {
+EntryBlock:
+  ;; B*(uitofp i1 C) -> select C, B, 0
+  %cf = uitofp i1 %C to float
+  %p2 = fmul fast float %B, %cf
+  ret float %p2
+}
+
+define float @test3(float %A, float %B, i1 %C) {
+EntryBlock:
+  ;; A*(1 - uitofp i1 C) + B*(uitofp i1 C) -> select C, A, B
+  %cf = uitofp i1 %C to float
+  %mc = fsub float 1.000000e+00, %cf
+  %p1 = fmul fast float %A, %mc
+  %p2 = fmul fast float %B, %cf
+  %s1 = fadd fast float %p1, %p2
+  ret float %s1
+}
+