From 0f8991742cda2b191519e1247d7a1027cc760d07 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Sat, 22 Nov 2014 04:52:38 +0000 Subject: [PATCH] InstCombine: Preserve nsw for (mul %V, -1) -> (sub 0, %V) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222604 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/InstCombine/InstCombineMulDivRem.cpp | 9 +++++++-- test/Transforms/InstCombine/mul.ll | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp index 8c48dce81de..c96c2d62255 100644 --- a/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -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. { diff --git a/test/Transforms/InstCombine/mul.ll b/test/Transforms/InstCombine/mul.ll index d19bedc7a10..905c33b501f 100644 --- a/test/Transforms/InstCombine/mul.ll +++ b/test/Transforms/InstCombine/mul.ll @@ -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 +}