[Reassociate] Don't propogate flags when creating negations

Reassociate mutated existing instructions in order to form negations
which would create additional reassociate opportunities.

This fixes PR23926.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240593 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
David Majnemer 2015-06-24 21:27:36 +00:00
parent 613afa1372
commit 95a741163f
3 changed files with 27 additions and 2 deletions

View File

@ -936,6 +936,10 @@ static Value *NegateValue(Value *V, Instruction *BI) {
// Push the negates through the add.
I->setOperand(0, NegateValue(I->getOperand(0), BI));
I->setOperand(1, NegateValue(I->getOperand(1), BI));
if (I->getOpcode() == Instruction::Add) {
I->setHasNoUnsignedWrap(false);
I->setHasNoSignedWrap(false);
}
// We must move the add instruction here, because the neg instructions do
// not dominate the old add instruction in general. By moving it, we are
@ -976,6 +980,12 @@ static Value *NegateValue(Value *V, Instruction *BI) {
InsertPt = TheNeg->getParent()->getParent()->getEntryBlock().begin();
}
TheNeg->moveBefore(InsertPt);
if (TheNeg->getOpcode() == Instruction::Sub) {
TheNeg->setHasNoUnsignedWrap(false);
TheNeg->setHasNoSignedWrap(false);
} else {
TheNeg->andIRFlags(BI);
}
return TheNeg;
}

View File

@ -169,7 +169,11 @@ define i32 @test11(i32 %W) {
; CHECK-NEXT: ret i32
}
declare void @mumble(i32)
define i32 @test12(i32 %X) {
%X.neg = sub nsw nuw i32 0, %X
call void @mumble(i32 %X.neg)
%A = sub i32 1, %X
%B = sub i32 2, %X
%C = sub i32 3, %X
@ -177,8 +181,8 @@ define i32 @test12(i32 %X) {
%Z = add i32 %Y, %C
ret i32 %Z
; CHECK-LABEL: @test12
; CHECK-NEXT: mul i32 %X, -3
; CHECK-NEXT: add i32{{.*}}, 6
; CHECK: %[[mul:.*]] = mul i32 %X, -3
; CHECK-NEXT: add i32 %[[mul]], 6
; CHECK-NEXT: ret i32
}

View File

@ -32,3 +32,14 @@ entry:
%mul2 = add i32 %mul, 1
ret i32 %mul2
}
; CHECK-LABEL: @pr23926(
; CHECK: %[[X1_neg:.*]] = sub i2 0, %X1
; CHECK-NEXT: %[[sub_one:.*]] = add i2 %[[X1_neg]], -1
; CHECK-NEXT: %[[add:.*]] = add i2 %[[sub_one]], %X2
; CHECK-NEXT: ret i2 %[[add]]
define i2 @pr23926(i2 %X1, i2 %X2) {
%add = add nuw i2 %X1, 1
%sub = sub nuw nsw i2 %X2, %add
ret i2 %sub
}