InstCombine: Verify the type before transforming uitofp into select.
authorBenjamin Kramer <benny.kra@googlemail.com>
Fri, 10 May 2013 09:16:52 +0000 (09:16 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Fri, 10 May 2013 09:16:52 +0000 (09:16 +0000)
PR15952.

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

lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
test/Transforms/InstCombine/add4.ll

index 0ac0ca27acf60e548474d6663c4c895b9335fa61..ecc9fc3e456066c80bc0dce119a6dcca93fa34d8 100644 (file)
@@ -525,31 +525,32 @@ 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);
-        }
+    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))) && C->getType()->isIntegerTy(1)) {
+        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 (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)))) &&
+          C->getType()->isIntegerTy(1)) {
+        A = LHS;
+        Value *Zero = ConstantFP::getNegativeZero(A->getType());
+        return SelectInst::Create(C, Zero, A);
+      }
     }
 
     if (!isa<Constant>(Op1))
index 3b19e9ef103d78c624d4800532d5a5e1dbb3286c..0fc0a6c1ac263a5c4111921fb25559c91ba50da4 100644 (file)
@@ -38,3 +38,21 @@ EntryBlock:
 ; CHECK: select i1 %C, float %B, float %A
 }
 
+; PR15952
+define float @test4(float %A, float %B, i32 %C) {
+  %cf = uitofp i32 %C to float
+  %mc = fsub float 1.000000e+00, %cf
+  %p1 = fmul fast float %A, %mc
+  ret float %p1
+; CHECK: @test4
+; CHECK: uitofp
+}
+
+define float @test5(float %A, float %B, i32 %C) {
+  %cf = uitofp i32 %C to float
+  %p2 = fmul fast float %B, %cf
+  ret float %p2
+; CHECK: @test5
+; CHECK: uitofp
+}
+