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
// 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
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;
}
; 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
%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
}
%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
+}