[llvm] [InstCombine] Fold negation of unsigned div of non-negatives (PR #84951)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 13 08:03:27 PDT 2024
https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/84951
>From b28d3bb9c598c30aae3f14e103ba9a9130d12ffa Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Tue, 12 Mar 2024 17:27:18 +0100
Subject: [PATCH] [InstCombine] Fold negation of unsigned div of non-negatives
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/2ftsgk.
---
.../InstCombine/InstCombineAddSub.cpp | 25 +++++-
llvm/test/Transforms/InstCombine/sub.ll | 80 +++++++++++++++++++
2 files changed, 104 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index aaf7184a5562cd..91ff653980291a 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -2062,6 +2062,25 @@ 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;
+ Constant *C;
+
+ const auto &SQ = IC.getSimplifyQuery().getWithInstruction(&I);
+ if (match(RHS, m_OneUse(m_UDiv(m_Value(X), m_Constant(C)))) &&
+ isKnownNonNegative(X, SQ) && isKnownNonNegative(C, SQ)) {
+ 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 +2172,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..dd7d17f50c7832 100644
--- a/llvm/test/Transforms/InstCombine/sub.ll
+++ b/llvm/test/Transforms/InstCombine/sub.ll
@@ -2626,3 +2626,83 @@ 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) {
+; 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: ret i8 0
+;
+entry:
+ %cond1 = icmp sgt i8 %a, -1
+ call void @llvm.assume(i1 %cond1)
+ %div = udiv i8 %a, 3
+ %neg = sub nuw i8 0, %div
+ ret i8 %neg
+}
+
+define i8 @test_neg_of_udiv_of_nonnegs_2(i8 %a) {
+; 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: [[NEG:%.*]] = sdiv i8 [[A]], -3
+; CHECK-NEXT: ret i8 [[NEG]]
+;
+entry:
+ %cond1 = icmp sgt i8 %a, -1
+ call void @llvm.assume(i1 %cond1)
+ %div = udiv i8 %a, 3
+ %neg = sub nsw i8 0, %div
+ ret i8 %neg
+}
+
+define i8 @test_neg_of_udiv_of_nonnegs_3(i8 %a) {
+; 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: [[NEG:%.*]] = sdiv i8 [[A]], -3
+; CHECK-NEXT: ret i8 [[NEG]]
+;
+entry:
+ %cond1 = icmp sgt i8 %a, -1
+ call void @llvm.assume(i1 %cond1)
+ %div = udiv i8 %a, 3
+ %neg = sub i8 0, %div
+ ret i8 %neg
+}
+
+define i8 @test_neg_of_udiv_of_nonnegs_4(i8 %a) {
+; CHECK-LABEL: @test_neg_of_udiv_of_nonnegs_4(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[COND1:%.*]] = icmp sgt i8 [[A:%.*]], -1
+; CHECK-NEXT: call void @llvm.assume(i1 [[COND1]])
+; CHECK-NEXT: [[NEG:%.*]] = sdiv i8 [[A]], -3
+; CHECK-NEXT: ret i8 [[NEG]]
+;
+entry:
+ %cond1 = icmp sgt i8 %a, -1
+ call void @llvm.assume(i1 %cond1)
+ %div = udiv exact i8 %a, 3
+ %neg = sub nsw i8 0, %div
+ ret i8 %neg
+}
+
+define i8 @test_neg_of_udiv_of_nonnegs_wrong(i8 %a) {
+; CHECK-LABEL: @test_neg_of_udiv_of_nonnegs_wrong(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[COND1:%.*]] = icmp sgt i8 [[A:%.*]], -1
+; CHECK-NEXT: call void @llvm.assume(i1 [[COND1]])
+; CHECK-NEXT: ret i8 0
+;
+entry:
+ %cond1 = icmp sgt i8 %a, -1
+ call void @llvm.assume(i1 %cond1)
+ %div = udiv i8 %a, -3
+ %neg = sub nsw i8 0, %div
+ ret i8 %neg
+}
+
+declare void @llvm.assume(i1)
More information about the llvm-commits
mailing list