-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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] Fix Failure to fold (and %x, (sext i1 %m)) -> (select %m, %x, 0) with multiple uses of %m #81409
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 (SahilPatidar) ChangesResolve #81288 Full diff: https://github.com/llvm/llvm-project/pull/81409.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 5fd944a859ef09..516af05b4cc531 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2637,7 +2637,7 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
// arm may not be reversible due to poison semantics. Is that a good
// canonicalization?
Value *A, *B;
- if (match(&I, m_c_And(m_OneUse(m_SExt(m_Value(A))), m_Value(B))) &&
+ if (match(&I, m_c_And(m_SExt(m_Value(A)), m_Value(B))) &&
A->getType()->isIntOrIntVectorTy(1))
return SelectInst::Create(A, B, Constant::getNullValue(Ty));
diff --git a/llvm/test/Transforms/InstCombine/and.ll b/llvm/test/Transforms/InstCombine/and.ll
index 2e37fee07cc47b..0931c7a1c4053d 100644
--- a/llvm/test/Transforms/InstCombine/and.ll
+++ b/llvm/test/Transforms/InstCombine/and.ll
@@ -1818,7 +1818,7 @@ define i8 @not_ashr_bitwidth_mask_use2(i8 %x, i8 %y) {
; CHECK-NEXT: [[ISNOTNEG:%.*]] = icmp sgt i8 [[X:%.*]], -1
; CHECK-NEXT: [[NOT:%.*]] = sext i1 [[ISNOTNEG]] to i8
; CHECK-NEXT: call void @use8(i8 [[NOT]])
-; CHECK-NEXT: [[R:%.*]] = and i8 [[NOT]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[ISNOTNEG]], i8 [[Y:%.*]], i8 0
; CHECK-NEXT: ret i8 [[R]]
;
%sign = ashr i8 %x, 7
@@ -1925,7 +1925,7 @@ define i16 @invert_signbit_splat_mask_use3(i8 %x, i16 %y) {
; CHECK-NEXT: [[ISNOTNEG:%.*]] = icmp sgt i8 [[X:%.*]], -1
; CHECK-NEXT: [[S:%.*]] = sext i1 [[ISNOTNEG]] to i16
; CHECK-NEXT: call void @use16(i16 [[S]])
-; CHECK-NEXT: [[R:%.*]] = and i16 [[S]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[ISNOTNEG]], i16 [[Y:%.*]], i16 0
; CHECK-NEXT: ret i16 [[R]]
;
%a = ashr i8 %x, 7
@@ -2768,3 +2768,18 @@ define i32 @add_constant_equal_with_the_top_bit_of_demandedbits_insertpt(i32 %x,
%and = and i32 %or, 24
ret i32 %and
}
+
+define i32 @and_sext(i32 %x, i32 %y, i32 %a, i32 %b) {
+; CHECK-LABEL: @and_sext(
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: [[ADD:%.*]] = select i1 [[CMP]], i32 [[TMP1]], i32 0
+; CHECK-NEXT: ret i32 [[ADD]]
+;
+ %cmp = icmp sgt i32 %x, %y
+ %sext = sext i1 %cmp to i32
+ %and1 = and i32 %sext, %a
+ %and2 = and i32 %sext, %b
+ %add = add i32 %and1, %and2
+ ret i32 %add
+}
|
%and2 = and i32 %sext, %b | ||
%add = add i32 %and1, %and2 | ||
ret i32 %add | ||
} |
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.
You need a better name - and_sext_multiuse at the very least. You also need to add some variants of the test with commuted operands / different types of use of the sext - maybe see what happens if you temporarily disable the and(sext(A), B) fold entirely and use the update script to see what other tests are dependent on this fold and what form they take.
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.
TBH, I don't think we need anything beyond this test. We already have all the necessary commuted / vector / multi-use coverage above. This test essentially just serves to illustrate why relaxing the one-use restriction is useful.
… multiple uses of %m Resolve llvm#81288
d5da9cf
to
c745528
Compare
@nikic are you happy with the test coverage here? |
%and2 = and i32 %sext, %b | ||
%add = add i32 %and1, %and2 | ||
ret i32 %add | ||
} |
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.
TBH, I don't think we need anything beyond this test. We already have all the necessary commuted / vector / multi-use coverage above. This test essentially just serves to illustrate why relaxing the one-use restriction is useful.
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
Actually, it looks like there are two legitimate test failures:
Can you please update these tests as well? |
ok |
Please let me know if any changes need to be made. |
@@ -50,7 +50,7 @@ define i32 @and_sext_to_sel_multi_use(i32 %x, i1 %y) { | |||
; CHECK-LABEL: @and_sext_to_sel_multi_use( | |||
; CHECK-NEXT: [[SEXT:%.*]] = sext i1 [[Y:%.*]] to i32 | |||
; CHECK-NEXT: call void @use(i32 [[SEXT]]) | |||
; CHECK-NEXT: [[R:%.*]] = and i32 [[SEXT]], [[X:%.*]] | |||
; CHECK-NEXT: [[R:%.*]] = select i1 [[Y]], i32 [[X:%.*]], i32 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.
Think this one could classified as regression but probably not a big deal.
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
@SahilPatidar 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! |
Resolve #81288