-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[InstCombine] Fold (X / C) < X and (X >> C) < X into X > 0 #85555
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
[InstCombine] Fold (X / C) < X and (X >> C) < X into X > 0 #85555
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-transforms Author: None (Poseydon42) ChangesProofs: Full diff: https://github.com/llvm/llvm-project/pull/85555.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 0dce0077bf1588..415ea61362776d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -7406,6 +7406,31 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
if (Instruction *Res = foldReductionIdiom(I, Builder, DL))
return Res;
+ // Folding (X / Y) < X => X > 0 for some constant Y other than 0 or 1
+ {
+ Constant *Divisor;
+ Value *Dividend;
+ if (match(Op0,
+ m_CombineOr(m_SDiv(m_Value(Dividend), m_ImmConstant(Divisor)),
+ m_UDiv(m_Value(Dividend), m_ImmConstant(Divisor)))) &&
+ match(Op1, m_Deferred(Dividend)) &&
+ !(Divisor->isZeroValue() || Divisor->isOneValue())) {
+ return new ICmpInst(ICmpInst::ICMP_SGT, Dividend,
+ Constant::getNullValue(Dividend->getType()));
+ }
+ }
+
+ // Another case of this fold is (X >> Y) < X => X > 0 if Y != 0
+ {
+ Constant *Shift;
+ Value *V;
+ if (match(Op0, m_LShr(m_Value(V), m_ImmConstant(Shift))) &&
+ match(Op1, m_Deferred(V)) && !Shift->isZeroValue()) {
+ return new ICmpInst(ICmpInst::ICMP_SGT, V,
+ Constant::getNullValue(V->getType()));
+ }
+ }
+
return Changed ? &I : nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/icmp-div-constant.ll b/llvm/test/Transforms/InstCombine/icmp-div-constant.ll
index 8dcb96284685ff..0bdfa5daa547a5 100644
--- a/llvm/test/Transforms/InstCombine/icmp-div-constant.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-div-constant.ll
@@ -375,3 +375,79 @@ define i1 @sdiv_eq_smin_use(i32 %x, i32 %y) {
%r = icmp eq i32 %d, -2147483648
ret i1 %r
}
+
+; Folding (X / C) < X => X > 0 for C != 1
+
+define i1 @sdiv_x_by_const_cmp_x(i32 %x) {
+; CHECK-LABEL: @sdiv_x_by_const_cmp_x(
+; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 0
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %1 = sdiv i32 %x, 123
+ %2 = icmp slt i32 %1, %x
+ ret i1 %2
+}
+
+define i1 @udiv_x_by_const_cmp_x(i32 %x) {
+; CHECK-LABEL: @udiv_x_by_const_cmp_x(
+; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 0
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %1 = udiv i32 %x, 123
+ %2 = icmp slt i32 %1, %x
+ ret i1 %2
+}
+
+; Same as above but with right shift instead of division (C != 0)
+
+define i1 @lshr_x_by_const_cmp_x(i32 %x) {
+; CHECK-LABEL: @lshr_x_by_const_cmp_x(
+; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], 0
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %1 = lshr i32 %x, 1
+ %2 = icmp slt i32 %1, %x
+ ret i1 %2
+}
+
+; Negative tests for the folds above
+
+define i1 @sdiv_x_by_1_cmp_x_negative(i32 %x) {
+; CHECK-LABEL: @sdiv_x_by_1_cmp_x_negative(
+; CHECK-NEXT: ret i1 false
+;
+ %1 = sdiv i32 %x, 1
+ %2 = icmp slt i32 %1, %x
+ ret i1 %2
+}
+
+define i1 @sdiv_x_by_1_cmp_y(i32 %x, i32 %y) {
+; CHECK-LABEL: @sdiv_x_by_1_cmp_y(
+; CHECK-NEXT: [[TMP1:%.*]] = sdiv i32 [[X:%.*]], 123
+; CHECK-NEXT: [[TMP2:%.*]] = icmp slt i32 [[TMP1]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[TMP2]]
+;
+ %1 = sdiv i32 %x, 123
+ %2 = icmp slt i32 %1, %y
+ ret i1 %2
+}
+
+define i1 @lshr_x_by_0_cmp_x(i32 %x) {
+; CHECK-LABEL: @lshr_x_by_0_cmp_x(
+; CHECK-NEXT: ret i1 false
+;
+ %1 = lshr i32 %x, 0
+ %2 = icmp slt i32 %1, %x
+ ret i1 %2
+}
+
+define i1 @lshr_x_by_const_cmp_y(i32 %x, i32 %y) {
+; CHECK-LABEL: @lshr_x_by_const_cmp_y(
+; CHECK-NEXT: [[X:%.*]] = lshr i32 [[X1:%.*]], 1
+; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X]], [[Y:%.*]]
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %1 = lshr i32 %x, 1
+ %2 = icmp slt i32 %1, %y
+ ret i1 %2
+}
|
Just a note on the proofs. Edit: added proofs for eq/ne case. |
Also welcome and thank you for the commit. There a rough draft of a contribution guide for You might find it useful. |
I have modified the code to work with all predicates for |
Think made a mistake when merging your updates. Also please update your proofs with all cases you plan to handle. |
Yeah, that merge was an accident, I'll remove it before pushing the most recent changes. It seems to me like handling |
So generally you should be testing different control flows in your code moreso than IR variants. In this case you have all predicates have the same control flow for For Then you can pretty randomly make 1-2 of the tests use vec. Please be sure to have a negative test with a vec where one vec element is invalid and the other is valid. |
3bf2533
to
3f08ea9
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
3f08ea9
to
65de414
Compare
Updated proofs (26 in total) can be found here. I've fixed most of the issues mentioned here, the only two things I'm worried about are: |
65de414
to
47cc91b
Compare
Yes that is one of the drawbacks of APInt, it only match if the vector is a splat (all elements that same).
See: https://github.com/llvm/llvm-project/blob/main/llvm/lib/Analysis/InstructionSimplify.cpp#L3172 |
47cc91b
to
634bbc3
Compare
Right, after a painful hour of rewriting git history multiple times, it seems to me that I've covered everything that was mentioned here before. Please let me know if there's anything else for me to fix or improve. |
Oops, looks like I forgot to run tests on my machine before pushing the changes. I've localised the source of one of many failing tests to this function in
The crash report indicates assertion failure in |
I'd say just handle the splat vector case for now. If #85676 gets in before this, you could use that API. |
634bbc3
to
92dd571
Compare
Done, should be passing tests now. |
92dd571
to
0d36e34
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated proofs with |
0d36e34
to
52da7a7
Compare
Constant::getNullValue(Op1->getType())); | ||
} | ||
|
||
if (match(Op0, m_AShr(m_Specific(Op1), m_APInt(Shift))) && Shift->ugt(0) && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: !Shift->isZero()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it is fixed now.
return new ICmpInst(ICmpInst::getSwappedPredicate(Pred), Op1, | ||
Constant::getNullValue(Op1->getType())); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: There is a bit of redundancy in that each of the return cases is identical, but at least imo its fine.
LGTM, wait on one more approval. |
52da7a7
to
51d44fd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
51d44fd
to
c203d1f
Compare
Thanks nikic, looks like it is now ready for another review. |
@Poseydon42 Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Proofs:
https://alive2.llvm.org/ce/z/52droC
This resolves #85313