[llvm] [InstCombine] Fold negation of unsigned div of non-negatives (PR #84951)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 12 10:21:14 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Antonio Frighetto (antoniofrighetto)
<details>
<summary>Changes</summary>
Let InstCombine carry out the following fold: `sub 0, (udiv nneg X, nneg C)` -> `sdiv nneg X, -C`.
Proofs: https://alive2.llvm.org/ce/z/9vyBvs.
Not sure if we need to go through `assumptionsFor` to update AC for the second assume.
---
Full diff: https://github.com/llvm/llvm-project/pull/84951.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp (+25-1)
- (modified) llvm/test/Transforms/InstCombine/sub.ll (+63)
``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index aaf7184a5562cd..7ec55c6aef00d4 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2062,6 +2062,26 @@ static Instruction *foldSubOfMinMax(BinaryOperator &I,
return nullptr;
}
+/// Fold `sub 0, (udiv nneg X, nneg C)` into `sdiv nneg X, -C`
+static Instruction *foldNegationOfUDivOfNonNegatives(BinaryOperator &I,
+ InstCombinerImpl &IC) {
+ Value *RHS = I.getOperand(1);
+ Value *X, *C;
+
+ if (match(RHS, m_OneUse(m_UDiv(m_Value(X), m_Value(C))))) {
+ KnownBits KnownX = IC.computeKnownBits(X, 0, &I);
+ KnownBits KnownC = IC.computeKnownBits(C, 0, &I);
+ if (KnownX.isNonNegative() && KnownC.isNonNegative()) {
+ bool HasNSW = I.hasNoSignedWrap();
+ auto *NegC = HasNSW ? IC.Builder.CreateNSWNeg(C, C->getName() + ".neg")
+ : IC.Builder.CreateNeg(C, C->getName() + ".neg");
+ return BinaryOperator::CreateSDiv(X, NegC);
+ }
+ }
+
+ return nullptr;
+}
+
Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
if (Value *V = simplifySubInst(I.getOperand(0), I.getOperand(1),
I.hasNoSignedWrap(), I.hasNoUnsignedWrap(),
@@ -2153,8 +2173,12 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
Op1, *this))
return BinaryOperator::CreateAdd(NegOp1, Op0);
}
- if (IsNegation)
+ if (IsNegation) {
+ if (Instruction *Res = foldNegationOfUDivOfNonNegatives(I, *this))
+ return Res;
+
return TryToNarrowDeduceFlags(); // Should have been handled in Negator!
+ }
// (A*B)-(A*C) -> A*(B-C) etc
if (Value *V = foldUsingDistributiveLaws(I))
diff --git a/llvm/test/Transforms/InstCombine/sub.ll b/llvm/test/Transforms/InstCombine/sub.ll
index 249b5673c8acfd..7cd1a0ea1646c7 100644
--- a/llvm/test/Transforms/InstCombine/sub.ll
+++ b/llvm/test/Transforms/InstCombine/sub.ll
@@ -2626,3 +2626,66 @@ define i8 @sub_of_adds_2xc(i8 %x, i8 %y) {
%r = sub i8 %xc, %yc
ret i8 %r
}
+
+define i8 @test_neg_of_udiv_of_nonnegs(i8 %a, i8 %b) {
+; CHECK-LABEL: @test_neg_of_udiv_of_nonnegs(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[COND1:%.*]] = icmp sgt i8 [[A:%.*]], -1
+; CHECK-NEXT: call void @llvm.assume(i1 [[COND1]])
+; CHECK-NEXT: [[COND2:%.*]] = icmp sgt i8 [[B:%.*]], -1
+; CHECK-NEXT: call void @llvm.assume(i1 [[COND2]])
+; CHECK-NEXT: ret i8 0
+;
+entry:
+ %cond1 = icmp sgt i8 %a, -1
+ call void @llvm.assume(i1 %cond1)
+ %cond2 = icmp sgt i8 %b, -1
+ call void @llvm.assume(i1 %cond2)
+ %div = udiv i8 %a, %b
+ %neg = sub nuw i8 0, %div
+ ret i8 %neg
+}
+
+define i8 @test_neg_of_udiv_of_nonnegs_2(i8 %a, i8 %b) {
+; CHECK-LABEL: @test_neg_of_udiv_of_nonnegs_2(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[COND1:%.*]] = icmp sgt i8 [[A:%.*]], -1
+; CHECK-NEXT: call void @llvm.assume(i1 [[COND1]])
+; CHECK-NEXT: [[COND2:%.*]] = icmp sgt i8 [[B:%.*]], -1
+; CHECK-NEXT: call void @llvm.assume(i1 [[COND2]])
+; CHECK-NEXT: [[B_NEG:%.*]] = sub nsw i8 0, [[B]]
+; CHECK-NEXT: [[NEG:%.*]] = sdiv i8 [[A]], [[B_NEG]]
+; CHECK-NEXT: ret i8 [[NEG]]
+;
+entry:
+ %cond1 = icmp sgt i8 %a, -1
+ call void @llvm.assume(i1 %cond1)
+ %cond2 = icmp sgt i8 %b, -1
+ call void @llvm.assume(i1 %cond2)
+ %div = udiv i8 %a, %b
+ %neg = sub nsw i8 0, %div
+ ret i8 %neg
+}
+
+define i8 @test_neg_of_udiv_of_nonnegs_3(i8 %a, i8 %b) {
+; CHECK-LABEL: @test_neg_of_udiv_of_nonnegs_3(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[COND1:%.*]] = icmp sgt i8 [[A:%.*]], -1
+; CHECK-NEXT: call void @llvm.assume(i1 [[COND1]])
+; CHECK-NEXT: [[COND2:%.*]] = icmp sgt i8 [[B:%.*]], -1
+; CHECK-NEXT: call void @llvm.assume(i1 [[COND2]])
+; CHECK-NEXT: [[B_NEG:%.*]] = sub nsw i8 0, [[B]]
+; CHECK-NEXT: [[NEG:%.*]] = sdiv i8 [[A]], [[B_NEG]]
+; CHECK-NEXT: ret i8 [[NEG]]
+;
+entry:
+ %cond1 = icmp sgt i8 %a, -1
+ call void @llvm.assume(i1 %cond1)
+ %cond2 = icmp sgt i8 %b, -1
+ call void @llvm.assume(i1 %cond2)
+ %div = udiv i8 %a, %b
+ %neg = sub i8 0, %div
+ ret i8 %neg
+}
+
+declare void @llvm.assume(i1)
``````````
</details>
https://github.com/llvm/llvm-project/pull/84951
More information about the llvm-commits
mailing list