diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index d7847ca5ccdf..3956869e2d3f 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -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(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(Op1)->hasNoSignedWrap(); } diff --git a/llvm/test/Transforms/InstCombine/mul.ll b/llvm/test/Transforms/InstCombine/mul.ll index 934448290e01..a52c31ab4afa 100644 --- a/llvm/test/Transforms/InstCombine/mul.ll +++ b/llvm/test/Transforms/InstCombine/mul.ll @@ -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]] +}