-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[InstCombine] Fold X - Y + Y * C -> X + Y * (C - 1) #117722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a590cb0 to
dbfd14e
Compare
Member
|
@llvm/pr-subscribers-llvm-transforms Author: Rose (AreaZR) ChangesAlive2 Proof: This is based on and closes #108451 Note that no nsw or nuw saving works because they all result in poison or undef mismatches. Full diff: https://github.com/llvm/llvm-project/pull/117722.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 6fe96935818531..b127f88090e289 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1612,6 +1612,18 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
Value *Sub = Builder.CreateSub(B, A, "reass.sub");
return BinaryOperator::CreateAdd(Sub, C1);
}
+
+ // A - B + B * C -> A + B * (C - 1)
+ const APInt *C;
+ if (((match(LHS, m_Sub(m_Value(A), m_Value(B))) &&
+ match(RHS, m_Mul(m_Specific(B), m_APInt(C)))) ||
+ (match(RHS, m_Sub(m_Value(A), m_Value(B))) &&
+ match(LHS, m_Mul(m_Specific(B), m_APInt(C))))) &&
+ LHS->hasOneUse() && RHS->hasOneUse()) {
+ Value *Mul =
+ Builder.CreateMul(B, ConstantInt::get(RHS->getType(), *C - 1));
+ return BinaryOperator::CreateAdd(A, Mul);
+ }
}
// X % C0 + (( X / C0 ) % C1) * C0 => X % (C0 * C1)
diff --git a/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll b/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll
index b38466f6cf0ec2..26b4ff3e400198 100644
--- a/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll
+++ b/llvm/test/Transforms/InstCombine/addsub-constant-folding.ll
@@ -750,3 +750,107 @@ define i8 @sub_from_constant_extra_use(i8 %x, i8 %y) {
%r = add i8 %sub, %y
ret i8 %r
}
+
+define i32 @sub_plus_mul(i32 %x, i32 %y) {
+; CHECK-LABEL: @sub_plus_mul(
+; CHECK-NEXT: [[B:%.*]] = mul i32 [[Y:%.*]], 9
+; CHECK-NEXT: [[C:%.*]] = add i32 [[A:%.*]], [[B]]
+; CHECK-NEXT: ret i32 [[C]]
+;
+ %a = sub i32 %x, %y
+ %b = mul i32 %y, 10
+ %c = add i32 %a, %b
+ ret i32 %c
+}
+
+define i32 @sub_plus_mul_2(i32 %x, i32 %y) {
+; CHECK-LABEL: @sub_plus_mul_2(
+; CHECK-NEXT: [[B:%.*]] = mul i32 [[Y:%.*]], -11
+; CHECK-NEXT: [[C:%.*]] = add i32 [[A:%.*]], [[B]]
+; CHECK-NEXT: ret i32 [[C]]
+;
+ %a = sub i32 %x, %y
+ %b = mul i32 %y, -10
+ %c = add i32 %a, %b
+ ret i32 %c
+}
+
+define i32 @sub_plus_mul3(i32 %x, i32 %y) {
+; CHECK-LABEL: @sub_plus_mul3(
+; CHECK-NEXT: [[A:%.*]] = mul i32 [[Y:%.*]], 9
+; CHECK-NEXT: [[C:%.*]] = add i32 [[B:%.*]], [[A]]
+; CHECK-NEXT: ret i32 [[C]]
+;
+ %a = mul i32 %y, 10
+ %b = sub i32 %x, %y
+ %c = add i32 %b, %a
+ ret i32 %c
+}
+
+define i32 @sub_plus_mul4(i32 %x, i32 %y) {
+; CHECK-LABEL: @sub_plus_mul4(
+; CHECK-NEXT: [[A:%.*]] = mul i32 [[Y:%.*]], -11
+; CHECK-NEXT: [[C:%.*]] = add i32 [[B:%.*]], [[A]]
+; CHECK-NEXT: ret i32 [[C]]
+;
+ %a = mul i32 %y, -10
+ %b = sub i32 %x, %y
+ %c = add i32 %b, %a
+ ret i32 %c
+}
+
+define <2 x i8> @sub_plus_mul_splat(<2 x i8> %x, <2 x i8> %y) {
+; CHECK-LABEL: @sub_plus_mul_splat(
+; CHECK-NEXT: [[B:%.*]] = mul <2 x i8> [[Y:%.*]], splat (i8 6)
+; CHECK-NEXT: [[C:%.*]] = add <2 x i8> [[A:%.*]], [[B]]
+; CHECK-NEXT: ret <2 x i8> [[C]]
+;
+ %a = sub <2 x i8> %x, %y
+ %b = mul <2 x i8> %y, splat(i8 7)
+ %c = add <2 x i8> %a, %b
+ ret <2 x i8> %c
+}
+
+;; <- Negative Tests ->
+
+;; mul does not work if it is a powerof2, because it results in worse codegen
+define i32 @neg_sub_plus_mul_pow2(i32 %x, i32 %y) {
+; CHECK-LABEL: @neg_sub_plus_mul_pow2(
+; CHECK-NEXT: [[A:%.*]] = sub i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[B:%.*]] = shl i32 [[Y]], 3
+; CHECK-NEXT: [[C:%.*]] = add i32 [[A]], [[B]]
+; CHECK-NEXT: ret i32 [[C]]
+;
+ %a = sub i32 %x, %y
+ %b = mul i32 %y, 8
+ %c = add i32 %a, %b
+ ret i32 %c
+}
+
+define <2 x i8> @sub_plus_mul_splat_shl(<2 x i8> %x, <2 x i8> %y) {
+; CHECK-LABEL: @sub_plus_mul_splat_shl(
+; CHECK-NEXT: [[A:%.*]] = sub <2 x i8> [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[B:%.*]] = shl <2 x i8> [[Y]], splat (i8 3)
+; CHECK-NEXT: [[C:%.*]] = add <2 x i8> [[A]], [[B]]
+; CHECK-NEXT: ret <2 x i8> [[C]]
+;
+ %a = sub <2 x i8> %x, %y
+ %b = mul <2 x i8> %y, splat(i8 8)
+ %c = add <2 x i8> %a, %b
+ ret <2 x i8> %c
+}
+
+define i32 @neg_sub_plus_mul_multiuse(i32 %x, i32 %y) {
+; CHECK-LABEL: @neg_sub_plus_mul_multiuse(
+; CHECK-NEXT: [[A:%.*]] = sub i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[B:%.*]] = mul i32 [[Y]], 7
+; CHECK-NEXT: call void @use(i32 [[B]])
+; CHECK-NEXT: [[C:%.*]] = add i32 [[A]], [[B]]
+; CHECK-NEXT: ret i32 [[C]]
+;
+ %a = sub i32 %x, %y
+ %b = mul i32 %y, 7
+ call void @use(i32 %b)
+ %c = add i32 %a, %b
+ ret i32 %c
+}
|
911129a to
8e48b77
Compare
Alive2 Proof: https://alive2.llvm.org/ce/z/gS9Wb7 This is based on and closes llvm#108451 Note that no nsw or nuw saving works because they all result in poison or undef mismatches.
8e48b77 to
05e8ccc
Compare
Contributor
Author
|
cc @regehr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
llvm:instcombine
Covers the InstCombine, InstSimplify and AggressiveInstCombine passes
llvm:transforms
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Alive2 Proof:
https://alive2.llvm.org/ce/z/gS9Wb7
This is based on and closes #108451
Note that no nsw or nuw saving works because they all result in poison or undef mismatches.