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
This commit is contained in:
David Majnemer 2014-11-22 04:52:38 +00:00
parent 5182ad54b2
commit 0f8991742c
2 changed files with 14 additions and 2 deletions

View File

@ -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.
{

View File

@ -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
}