InstCombine: Don't create an unused instruction
authorDavid Majnemer <david.majnemer@gmail.com>
Mon, 24 Nov 2014 16:41:13 +0000 (16:41 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Mon, 24 Nov 2014 16:41:13 +0000 (16:41 +0000)
We would create an instruction but not inserting it.
Not inserting the unused instruction would lead us to verification
failure.

This fixes PR21653.

llvm-svn: 222659

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
llvm/test/Transforms/InstCombine/mul.ll

index d7847ca..3956869 100644 (file)
@@ -300,8 +300,7 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
     if (match(Op0, m_Shl(m_One(), m_Value(Y)))) {
       BO = BinaryOperator::CreateShl(Op1, Y);
       ShlNSW = cast<BinaryOperator>(Op0)->hasNoSignedWrap();
-    }
-    if (match(Op1, m_Shl(m_One(), m_Value(Y)))) {
+    } else if (match(Op1, m_Shl(m_One(), m_Value(Y)))) {
       BO = BinaryOperator::CreateShl(Op0, Y);
       ShlNSW = cast<BinaryOperator>(Op1)->hasNoSignedWrap();
     }
index 9344482..a52c31a 100644 (file)
@@ -245,3 +245,13 @@ define i32 @test27(i32 %A, i32 %B) {
         ret i32 %D
 ; CHECK: shl nuw i32 %A, %B
 }
+
+define i32 @test28(i32 %A) {
+; CHECK-LABEL: @test28(
+        %B = shl i32 1, %A
+        %C = mul nsw i32 %B, %B
+        ret i32 %C
+; CHECK:      %[[shl1:.*]] = shl i32 1, %A
+; CHECK-NEXT: %[[shl2:.*]] = shl i32 %[[shl1]], %A
+; CHECK-NEXT: ret i32 %[[shl2]]
+}