Skip to content

Commit

Permalink
InstCombine: Preserve nsw for (mul %V, -1) -> (sub 0, %V)
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222604 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
majnemer committed Nov 22, 2014
1 parent 5182ad5 commit 0f89917
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,13 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
if (Value *V = SimplifyUsingDistributiveLaws(I))
return ReplaceInstUsesWith(I, V);

if (match(Op1, m_AllOnes())) // X * -1 == 0 - X
return BinaryOperator::CreateNeg(Op0, I.getName());
// X * -1 == 0 - X
if (match(Op1, m_AllOnes())) {
BinaryOperator *BO = BinaryOperator::CreateNeg(Op0, I.getName());
if (I.hasNoSignedWrap())
BO->setHasNoSignedWrap();
return BO;
}

// Also allow combining multiply instructions on vectors.
{
Expand Down
7 changes: 7 additions & 0 deletions test/Transforms/InstCombine/mul.ll
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,10 @@ define <2 x i1> @test21(<2 x i1> %A, <2 x i1> %B) {
ret <2 x i1> %C
; CHECK: %C = and <2 x i1> %A, %B
}

define i32 @test22(i32 %A) {
; CHECK-LABEL: @test22(
%B = mul nsw i32 %A, -1
ret i32 %B
; CHECK: sub nsw i32 0, %A
}

0 comments on commit 0f89917

Please sign in to comment.